-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Merged
ZeroIntensity
merged 15 commits into
python:main
from
ZeroIntensity:datetime-interp-init
Jul 21, 2025
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
77d117c
Load _datetime during interpreter initialization
ZeroIntensity 963a9ee
Add a test case.
ZeroIntensity e16fb54
Add blurb.
ZeroIntensity ed65682
Move to pycore_init_types()
ZeroIntensity 43b4843
Fix lint.
ZeroIntensity 0ad304f
Add _datetime to the frozen modules.
ZeroIntensity d762ed5
Remove obsolete 'reloading' state.
ZeroIntensity db327e7
Some test refactoring and improvements.
ZeroIntensity 9456147
Fix test.
ZeroIntensity 6adafa1
Make tp_base stores atomic.
ZeroIntensity 6e2f891
Revert "Make tp_base stores atomic."
ZeroIntensity a15843f
Set tp_base in the PyTypeObject definition.
ZeroIntensity d6064c4
Add a comment.
ZeroIntensity 9a3b1c2
Move the test.
ZeroIntensity 3520514
Remove dead tp_base code.
ZeroIntensity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Load _datetime during interpreter initialization
- Loading branch information
commit 77d117c145ee7a2ced7f716e8b1815fb5d7cf587
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
|
@@ -7329,13 +7330,9 @@ clear_state(datetime_state *st) | |
} | ||
|
||
|
||
static int | ||
init_static_types(PyInterpreterState *interp, int reloading) | ||
PyStatus | ||
_PyDateTime_Init(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 | ||
|
@@ -7347,11 +7344,11 @@ init_static_types(PyInterpreterState *interp, int reloading) | |
for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) { | ||
PyTypeObject *type = capi_types[i]; | ||
if (_PyStaticType_InitForExtension(interp, type) < 0) { | ||
return -1; | ||
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. 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. |
||
return _PyStatus_ERR("could not initialize static types"); | ||
} | ||
} | ||
|
||
return 0; | ||
return _PyStatus_OK(); | ||
} | ||
|
||
|
||
|
@@ -7379,10 +7376,6 @@ _datetime_exec(PyObject *module) | |
} | ||
/* We actually set the "current" module right before a successful return. */ | ||
|
||
if (init_static_types(interp, reloading) < 0) { | ||
kumaraditya303 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
goto error; | ||
} | ||
|
||
for (size_t i = 0; i < Py_ARRAY_LENGTH(capi_types); i++) { | ||
PyTypeObject *type = capi_types[i]; | ||
const char *name = _PyType_Name(type); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This PR does not address the possible races in
PyDateTime_*.tp_base = &PyDateTime_*Type;
below, right?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 not. I don't think there's an easy way to do this here, because atomically storing
tp_base
will continue to race with all the non-atomic reads elsewhere.Do we even need to load it at runtime like this? We have other examples of directly storing it in the
PyTypeObject
structure:cpython/Objects/methodobject.c
Line 396 in 958657b
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.
Ah, TIL. That's definitely the best option here then.
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 afterPy_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.