Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-117398: datetime: Access C-API via PyInterpreterState #118357

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions Include/datetime.h
Expand Up @@ -187,17 +187,18 @@ typedef struct {

#define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI"

PyAPI_FUNC(void) _PyDateTimeAPI_Import(void);
PyAPI_FUNC(void) _PyDateTimeAPI_Clear(void);
PyAPI_FUNC(PyDateTime_CAPI *) _PyDateTimeAPI_Get(void);

/* This block is only used as part of the public API and should not be
* included in _datetimemodule.c, which does not use the C API capsule.
* See bpo-35081 for more details.
* */
#ifndef _PY_DATETIME_IMPL
/* Define global variable for the C API and a macro for setting it. */
static PyDateTime_CAPI *PyDateTimeAPI = NULL;

#define PyDateTime_IMPORT \
PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
#define PyDateTimeAPI _PyDateTimeAPI_Get()
#define PyDateTime_IMPORT _PyDateTimeAPI_Import()

/* Macro for access to the UTC singleton */
#define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_interp.h
Expand Up @@ -236,6 +236,7 @@ struct _is {
// more comments.
struct _obmalloc_state *obmalloc;

void *datetime_capi;
PyObject *audit_hooks;
PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS];
PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS];
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_capi/test_misc.py
Expand Up @@ -2282,6 +2282,22 @@ def test_module_state_shared_in_global(self):
subinterp_attr_id = os.read(r, 100)
self.assertEqual(main_attr_id, subinterp_attr_id)

@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
def test_datetime_capi_client(self):
script = textwrap.dedent("""
import importlib.machinery
import importlib.util
fullname = '_test_datetime_capi_client'
origin = importlib.util.find_spec('_testmultiphase').origin
loader = importlib.machinery.ExtensionFileLoader(fullname, origin)
spec = importlib.util.spec_from_loader(fullname, loader)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
""")
exec(script)
ret = support.run_in_subinterp(script)
self.assertEqual(ret, 0)


@requires_subinterpreters
class InterpreterConfigTests(unittest.TestCase):
Expand Down
31 changes: 31 additions & 0 deletions Modules/_datetimemodule.c
Expand Up @@ -63,6 +63,35 @@ static datetime_state _datetime_global_state;

#define STATIC_STATE() (&_datetime_global_state)

static inline void
set_datetime_capi_by_interp(PyDateTime_CAPI *capi)
{
_PyInterpreterState_GET()->datetime_capi = capi;
}

void
_PyDateTimeAPI_Import(void)
{
PyDateTime_CAPI *capi = PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0);
if (capi) {
// PyInit__datetime() is not called when the module is already loaded
// with single-phase init.
set_datetime_capi_by_interp((PyDateTime_CAPI *)capi);
}
}

PyDateTime_CAPI *
_PyDateTimeAPI_Get(void)
{
return (PyDateTime_CAPI *)_PyInterpreterState_GET()->datetime_capi;
}

void
_PyDateTimeAPI_Clear(void)
{
set_datetime_capi_by_interp(NULL);
}

/* We require that C int be at least 32 bits, and use int virtually
* everywhere. In just a few cases we use a temp long, where a Python
* API returns a C long. In such cases, we have to ensure that the
Expand Down Expand Up @@ -6943,6 +6972,8 @@ _datetime_exec(PyObject *module)
PyMem_Free(capi);
goto error;
}
/* Ensure that the newest capi is used on multi-phase init */
set_datetime_capi_by_interp(capi);

/* A 4-year cycle has an extra leap day over what we'd get from
* pasting together 4 single years.
Expand Down
3 changes: 3 additions & 0 deletions Modules/_testcapi/datetime.c
Expand Up @@ -8,6 +8,9 @@ static int test_run_counter = 0;
static PyObject *
test_datetime_capi(PyObject *self, PyObject *args)
{
if (!test_run_counter) {
_PyDateTimeAPI_Clear();
}
if (PyDateTimeAPI) {
if (test_run_counter) {
/* Probably regrtest.py -R */
Expand Down
34 changes: 34 additions & 0 deletions Modules/_testmultiphase.c
Expand Up @@ -952,3 +952,37 @@ PyInit__test_shared_gil_only(void)
{
return PyModuleDef_Init(&shared_gil_only_def);
}


#include "datetime.h"

static int
datetime_capi_client_exec(PyObject *m)
{
_PyDateTimeAPI_Clear();
if (PyDateTimeAPI != NULL) {
neonene marked this conversation as resolved.
Show resolved Hide resolved
neonene marked this conversation as resolved.
Show resolved Hide resolved
return -1;
}
PyDateTime_IMPORT;
neonene marked this conversation as resolved.
Show resolved Hide resolved
PyErr_Clear();
neonene marked this conversation as resolved.
Show resolved Hide resolved
if (PyDateTimeAPI != PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)) {
return -1;
}
PyErr_Clear();
return 0;
}

static PyModuleDef_Slot datetime_capi_client_slots[] = {
{Py_mod_exec, datetime_capi_client_exec},
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
{0, NULL},
};

static PyModuleDef datetime_capi_client_def = TEST_MODULE_DEF(
"_testmultiphase_datetime_capi_client", datetime_capi_client_slots, NULL);

PyMODINIT_FUNC
PyInit__test_datetime_capi_client(void)
{
return PyModuleDef_Init(&datetime_capi_client_def);
}
1 change: 0 additions & 1 deletion Tools/c-analyzer/cpython/globals-to-fix.tsv
Expand Up @@ -446,7 +446,6 @@ Modules/_tkinter.c - trbInCmd -
## initialized once

## other
Include/datetime.h - PyDateTimeAPI -
Modules/_ctypes/cfield.c _ctypes_get_fielddesc initialized -
Modules/_ctypes/malloc_closure.c - _pagesize -
Modules/_cursesmodule.c - initialised -
Expand Down