Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-117398: Add datetime Module State #119810

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
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8463ae8
Use the static types directly.
ericsnowcurrently May 28, 2024
ead0083
Use the UTC singleton directly.
ericsnowcurrently May 28, 2024
4c52d5d
Add module state.
ericsnowcurrently May 27, 2024
6b291c8
Clear the module state when destroying the module.
ericsnowcurrently May 28, 2024
69c7e1f
Create PyDateTime_IsoCalendarDateType in init_state().
ericsnowcurrently May 29, 2024
36dbeea
Track the "current" module in the internal per-interpreter dict.
ericsnowcurrently May 29, 2024
d0c0b2d
Copy the "current" module state, if available.
ericsnowcurrently May 29, 2024
0599dd6
Make state usage explicit.
ericsnowcurrently May 29, 2024
1f1f6fc
Reduce churn.
ericsnowcurrently May 29, 2024
d44d6e9
Drop _datetime_global_state.
ericsnowcurrently May 29, 2024
a309474
Drop datetime_state.initialized.
ericsnowcurrently May 29, 2024
ede4415
Fix refleaks.
ericsnowcurrently May 30, 2024
5a8b1aa
Clear the "current" module when finalizing the module.
ericsnowcurrently May 30, 2024
3de1cd3
Use a weakref when tracking the "current" module.
ericsnowcurrently May 30, 2024
62b3d5e
Fix clear_current_module().
ericsnowcurrently May 30, 2024
5c25927
Give each module its own heap types.
ericsnowcurrently May 31, 2024
da24674
Consolidate into init_state().
ericsnowcurrently May 31, 2024
f8420ea
Use _Py_ID() for INTERP_KEY.
ericsnowcurrently May 31, 2024
2a2c0b1
Handle PyWeakref_GetRef() returning None.
ericsnowcurrently May 31, 2024
c519e3c
Fix refleak.
ericsnowcurrently May 31, 2024
05acc56
Make sure the module exists in static type methods.
ericsnowcurrently May 31, 2024
07e3b65
Don't bother making the module temporary.
ericsnowcurrently May 31, 2024
dc4b458
Add a comment about the macros.
ericsnowcurrently Jun 3, 2024
3c55035
Simplify GET_CURRENT_STATE().
ericsnowcurrently Jun 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make sure the module exists in static type methods.
  • Loading branch information
ericsnowcurrently committed May 31, 2024
commit 05acc56c6bdd017fc02a2fb586597f66249353a4
47 changes: 38 additions & 9 deletions 47 Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,44 @@ get_current_module(PyInterpreterState *interp)
return mod;
}

#define GET_CURRENT_STATE(ST_VAR) \
NULL; \
PyObject *current_mod = NULL; \
do { \
PyInterpreterState *interp = PyInterpreterState_Get(); \
current_mod = get_current_module(interp); \
assert(current_mod != NULL); \
ST_VAR = get_module_state(current_mod); \
} while (0)
static PyModuleDef datetimemodule;

static datetime_state *
_get_current_state(PyObject **p_mod)
{
PyInterpreterState *interp = PyInterpreterState_Get();
PyObject *mod = get_current_module(interp);
if (mod == NULL) {
assert(!PyErr_Occurred());
if (PyErr_Occurred()) {
return NULL;
}
/* The static types can outlive the module, so we must
* temporarily load the module if one of the static types'
* methods needs module state. We can cut some corners
* since the module was necessarily already imported successfully
* and the module will only be around temporarily. We don't even
* need a spec. Normally the module would have been found
* and the temporary module would never happen. */
mod = PyModule_New("_datetime");
if (mod == NULL) {
return NULL;
}
((PyModuleObject*)mod)->md_def = &datetimemodule;
if (PyModule_ExecDef(mod, &datetimemodule) < 0) {
Py_DECREF(mod);
return NULL;
}
}
datetime_state *st = get_module_state(mod);
*p_mod = mod;
return st;
}

#define GET_CURRENT_STATE(ST_VAR) \
NULL; \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why NULL?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's there so the macro can transparently declare the current_mod local var. However, that made more sense in an earlier iteration of the PR. I'll simplify this for clarity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

PyObject *current_mod = NULL; \
ST_VAR = _get_current_state(&current_mod);
#define RELEASE_CURRENT_STATE(ST_VAR) \
Py_DECREF(current_mod)

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.