-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-136421: Load _datetime
static types during interpreter initialization
#136583
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
Changes from 9 commits
77d117c
963a9ee
e16fb54
ed65682
43b4843
0ad304f
d762ed5
db327e7
9456147
6adafa1
6e2f891
a15843f
d6064c4
9a3b1c2
3520514
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3651,6 +3651,32 @@ def test_repr_subclass(self): | |
td = SubclassDatetime(2010, 10, 2, second=3) | ||
self.assertEqual(repr(td), "SubclassDatetime(2010, 10, 2, 0, 0, 3)") | ||
|
||
@support.cpython_only | ||
def test_concurrent_initialization_subinterpreter(self): | ||
# Run in a subprocess to ensure we get a clean version of _datetime | ||
script = """if True: | ||
from concurrent.futures import InterpreterPoolExecutor | ||
|
||
def func(): | ||
import _datetime | ||
print('a', end='') | ||
|
||
with InterpreterPoolExecutor() as executor: | ||
for _ in range(8): | ||
|
||
executor.submit(func) | ||
""" | ||
rc, out, err = script_helper.assert_python_ok("-c", script) | ||
self.assertEqual(rc, 0) | ||
self.assertEqual(out, b"a" * 8) | ||
self.assertEqual(err, b"") | ||
|
||
# Now test against concurrent reinitialization | ||
script = "import _datetime\n" + script | ||
rc, out, err = script_helper.assert_python_ok("-c", script) | ||
self.assertEqual(rc, 0) | ||
self.assertEqual(out, b"a" * 8) | ||
self.assertEqual(err, b"") | ||
|
||
|
||
class TestSubclassDateTime(TestDateTime): | ||
theclass = SubclassDatetime | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix crash when initializing :mod:`datetime` concurrently. |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -14,6 +14,7 @@ | |||
#include "pycore_object.h" // _PyObject_Init() | ||||
#include "pycore_time.h" // _PyTime_ObjectToTime_t() | ||||
#include "pycore_unicodeobject.h" // _PyUnicode_Copy() | ||||
#include "pycore_initconfig.h" // _PyStatus_OK() | ||||
|
||||
#include "datetime.h" | ||||
|
||||
|
@@ -124,10 +125,9 @@ get_module_state(PyObject *module) | |||
#define INTERP_KEY ((PyObject *)&_Py_ID(cached_datetime_module)) | ||||
|
||||
static PyObject * | ||||
get_current_module(PyInterpreterState *interp, int *p_reloading) | ||||
get_current_module(PyInterpreterState *interp) | ||||
{ | ||||
PyObject *mod = NULL; | ||||
int reloading = 0; | ||||
|
||||
PyObject *dict = PyInterpreterState_GetDict(interp); | ||||
if (dict == NULL) { | ||||
|
@@ -138,7 +138,6 @@ get_current_module(PyInterpreterState *interp, int *p_reloading) | |||
goto error; | ||||
} | ||||
if (ref != NULL) { | ||||
reloading = 1; | ||||
if (ref != Py_None) { | ||||
(void)PyWeakref_GetRef(ref, &mod); | ||||
if (mod == Py_None) { | ||||
|
@@ -147,9 +146,6 @@ get_current_module(PyInterpreterState *interp, int *p_reloading) | |||
Py_DECREF(ref); | ||||
} | ||||
} | ||||
if (p_reloading != NULL) { | ||||
*p_reloading = reloading; | ||||
} | ||||
return mod; | ||||
|
||||
error: | ||||
|
@@ -163,7 +159,7 @@ static datetime_state * | |||
_get_current_state(PyObject **p_mod) | ||||
{ | ||||
PyInterpreterState *interp = PyInterpreterState_Get(); | ||||
PyObject *mod = get_current_module(interp, NULL); | ||||
PyObject *mod = get_current_module(interp); | ||||
if (mod == NULL) { | ||||
assert(!PyErr_Occurred()); | ||||
if (PyErr_Occurred()) { | ||||
|
@@ -7329,13 +7325,9 @@ clear_state(datetime_state *st) | |||
} | ||||
|
||||
|
||||
static int | ||||
init_static_types(PyInterpreterState *interp, int reloading) | ||||
PyStatus | ||||
_PyDateTime_InitTypes(PyInterpreterState *interp) | ||||
{ | ||||
if (reloading) { | ||||
return 0; | ||||
} | ||||
|
||||
// `&...` is not a constant expression according to a strict reading | ||||
// of C standards. Fill tp_base at run-time rather than statically. | ||||
// See https://bugs.python.org/issue40777 | ||||
|
.tp_base = &PyCFunction_Type, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not needed now because the module is statically linked, that issue happens only with dynamic loaded modules so you can define it statically now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess a _Py_IsMainInterPreter()
check will suffice in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not needed now because the module is statically linked, that issue happens only with dynamic loaded modules so you can define it statically now.
Ah, TIL. That's definitely the best option here then.
I guess a _Py_IsMainInterPreter() check will suffice in this PR?
For what?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For ensuring tp_base
is set only once after Py_Initialize()
. But I missed the Kumar 's comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove PyDateTime_DateTimeType.tp_base = &PyDateTime_DateType;
as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for this PR but as a follow up I think it would be better to now remove _PyStaticType_InitForExtension and just use _PyStaticType_InitBuiltin for it, there's a lot of special casing that could be removed.
kumaraditya303 marked this conversation as resolved.
Show resolved
Hide resolved
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -760,6 +760,11 @@ pycore_init_types(PyInterpreterState *interp) | |||||||||||||||||||||
return status; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
status = _PyDateTime_InitTypes(interp); | ||||||||||||||||||||||
if (_PyStatus_EXCEPTION(status)) { | ||||||||||||||||||||||
return status; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
Comment on lines
+763
to
+766
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Reply from #136620 (comment) Can you run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, what are you trying to achieve here? This will just break the types for the main interpreter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note that Correction: Run the script of the test without running |
||||||||||||||||||||||
|
||||||||||||||||||||||
return _PyStatus_OK(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put an anchor instead of the well-known explanation of
assert_python_ok()
. Also, move the test toExtensionModuleTests
(@support.cpython_only
is redundant there).TestDateTime
is the place to test the datetime class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'm not sure what you mean by "an anchor".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about that. I meant the gh-issue number or the url.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, did both.