8000 gh-117398: gh-119655: datetime: Init static state once & don't free it by encukou · Pull Request #119662 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117398: gh-119655: datetime: Init static state once & don't free it #119662

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 3 commits into from
May 28, 2024
Merged
Changes from 1 commit
Commits
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
Next Next commit
While datetime uses global state, only initialize it once
  • Loading branch information
encukou committed May 28, 2024
commit 3dbfcc52593d62d183ef4df93526deeac23ca4d4
12 changes: 12 additions & 0 deletions Modules/_datetimemodule.c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ typedef struct {

/* The interned Unix epoch datetime instance */
PyObject *epoch;

/* While we use a global state, we ensure it's only initialized once */
int initialized;
} datetime_state;

static datetime_state _datetime_global_state;
Expand Down Expand Up @@ -6841,6 +6844,12 @@ create_timezone_from_delta(int days, int sec, int ms, int normalize)
static int
init_state(datetime_state *st)
{
// While datetime uses global module "state", we unly initialize it once.
// The PyLong objects created here (once per process) are not decref'd.
if (st->initialized) {
return 0;
}

st->date_type = &PyDateTime_DateType;
st->datetime_type = &PyDateTime_DateTimeType;
st->delta_type = &PyDateTime_DeltaType;
Expand Down Expand Up @@ -6893,6 +6902,9 @@ init_state(datetime_state *st)
if (st->epoch == NULL) {
return -1;
}

st->initialized = 1;

return 0;
}

Expand Down
0