Skip to content

Commit c2758bd

Browse files
committed
Get module without interp-dict and weakref
1 parent b285b46 commit c2758bd

File tree

5 files changed

+18
-104
lines changed

5 files changed

+18
-104
lines changed

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ struct _Py_global_strings {
326326
STRUCT_FOR_ID(c_call)
327327
STRUCT_FOR_ID(c_exception)
328328
STRUCT_FOR_ID(c_return)
329-
STRUCT_FOR_ID(cached_datetime_module)
330329
STRUCT_FOR_ID(cached_statements)
331330
STRUCT_FOR_ID(cadata)
332331
STRUCT_FOR_ID(cafile)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_datetimemodule.c

Lines changed: 18 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ static PyTypeObject PyDateTime_TimeZoneType;
3636

3737

3838
typedef struct {
39-
/* Corresponding module that can be referred even after
40-
* its weak reference stops working at shutdown. */
39+
/* Corresponding module that is referenced via the interpreter state. */
4140
PyObject *module;
4241

4342
/* Module heap types. */
@@ -126,68 +125,37 @@ get_module_state(PyObject *module)
126125
}
127126

128127

129-
#define INTERP_KEY ((PyObject *)&_Py_ID(cached_datetime_module))
130-
131128
static PyObject *
132129
get_current_module(PyInterpreterState *interp, int *p_reloading)
133130
{
134-
PyObject *mod = NULL;
135-
int reloading = 0;
136-
137-
PyObject *dict = PyInterpreterState_GetDict(interp);
138-
if (dict == NULL) {
139-
goto error;
140-
}
141-
PyObject *ref = NULL;
142-
if (PyDict_GetItemRef(dict, INTERP_KEY, &ref) < 0) {
143-
goto error;
144-
}
145-
if (ref != NULL) {
146-
reloading = 1;
147-
if (ref != Py_None) {
148-
(void)PyWeakref_GetRef(ref, &mod);
149-
if (mod == Py_None) {
150-
Py_CLEAR(mod);
151-
}
152-
Py_DECREF(ref);
153-
}
154-
}
131+
datetime_state *st = interp->datetime_module_state;
155132
if (p_reloading != NULL) {
156-
*p_reloading = reloading;
133+
*p_reloading = st != NULL ? 1 : 0;
134+
}
135+
if (st != NULL && st != (void *)Py_None && st->module != NULL) {
136+
assert(PyModule_CheckExact(st->module));
137+
return Py_NewRef(st->module);
157138
}
158-
return mod;
159-
160-
error:
161-
assert(PyErr_Occurred());
162139
return NULL;
163140
}
164141

165-
static PyModuleDef datetimemodule;
166-
167142
static datetime_state *
168143
_get_current_state(PyObject **p_mod)
169144
{
170145
PyInterpreterState *interp = PyInterpreterState_Get();
171146
datetime_state *st = interp->datetime_module_state;
172-
if (st != NULL && st->module != NULL) {
147+
if (st != NULL && st != (void *)Py_None && st->module != NULL) {
173148
assert(PyModule_CheckExact(st->module));
174149
*p_mod = Py_NewRef(st->module);
175150
return st;
176151
}
177152

178-
PyObject *mod = get_current_module(interp, NULL);
153+
assert(!_Py_IsInterpreterFinalizing(interp));
154+
/* The static types can outlive the module,
155+
* so we must re-import the module. */
156+
PyObject *mod = PyImport_ImportModule("_datetime");
179157
if (mod == NULL) {
180-
assert(!PyErr_Occurred());
181-
if (PyErr_Occurred()) {
182-
return NULL;
183-
}
184-
assert(!_Py_IsInterpreterFinalizing(interp));
185-
/* The static types can outlive the module,
186-
* so we must re-import the module. */
187-
mod = PyImport_ImportModule("_datetime");
188-
if (mod == NULL) {
189-
return NULL;
190-
}
158+
return NULL;
191159
}
192160
st = get_module_state(mod);
193161
*p_mod = mod;
@@ -203,65 +171,18 @@ static int
203171
set_current_module(PyInterpreterState *interp, PyObject *mod)
204172
{
205173
assert(mod != NULL);
206-
PyObject *dict = PyInterpreterState_GetDict(interp);
207-
if (dict == NULL) {
208-
return -1;
209-
}
210-
PyObject *ref = PyWeakref_NewRef(mod, NULL);
211-
if (ref == NULL) {
212-
return -1;
213-
}
214-
int rc = PyDict_SetItem(dict, INTERP_KEY, ref);
215-
Py_DECREF(ref);
216-
interp->datetime_module_state = rc == 0 ? get_module_state(mod) : NULL;
217-
return rc;
174+
interp->datetime_module_state = get_module_state(mod);
175+
return 0;
218176
}
219177

220178
static void
221179
clear_current_module(PyInterpreterState *interp, PyObject *expected)
222180
{
223-
PyObject *exc = PyErr_GetRaisedException();
224-
225-
PyObject *dict = PyInterpreterState_GetDict(interp);
226-
if (dict == NULL) {
227-
goto error;
228-
}
229-
230-
if (expected != NULL) {
231-
PyObject *ref = NULL;
232-
if (PyDict_GetItemRef(dict, INTERP_KEY, &ref) < 0) {
233-
goto error;
234-
}
235-
if (ref != NULL) {
236-
PyObject *current = NULL;
237-
int rc = PyWeakref_GetRef(ref, &current);
238-
/* We only need "current" for pointer comparison. */
239-
Py_XDECREF(current);
240-
Py_DECREF(ref);
241-
if (rc < 0) {
242-
goto error;
243-
}
244-
if (current != expected) {
245-
goto finally;
246-
}
247-
}
181+
if (expected && get_module_state(expected) != interp->datetime_module_state) {
182+
return;
248183
}
249-
250184
/* We use None to identify that the module was previously loaded. */
251-
if (PyDict_SetItem(dict, INTERP_KEY, Py_None) < 0) {
252-
goto error;
253-
}
254-
255-
goto finally;
256-
257-
error:
258-
PyErr_FormatUnraisable("Exception ignored while clearing _datetime module");
259-
260-
finally:
261-
if (!expected || get_module_state(expected) == interp->datetime_module_state) {
262-
interp->datetime_module_state = NULL;
263-
}
264-
PyErr_SetRaisedException(exc);
185+
interp->datetime_module_state = Py_None;
265186
}
266187

267188

0 commit comments

Comments
 (0)