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-71587: Drop local reference cache to _strptime module in _datetime #120224

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 10 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
9 changes: 9 additions & 0 deletions 9 Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,15 @@ def test_ucnhash_capi_reset(self):
out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
self.assertEqual(out, '9\n' * INIT_LOOPS)

def test_datetime_reset_strptime(self):
code = (
"import datetime;"
"d = datetime.datetime.strptime('2000-01-01', '%Y-%m-%d');"
"print(d.strftime('%Y%m%d'))"
)
out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
self.assertEqual(out, '20000101\n' * INIT_LOOPS)


@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash in C version of :meth:`datetime.datetime.strptime` when called again
on the restarted interpreter.
26 changes: 20 additions & 6 deletions 26 Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ typedef struct {

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

/* _strptime module */
PyObject *strptime;
} datetime_state;

/* The module has a fixed number of static objects, due to being exposed
Expand Down Expand Up @@ -5514,19 +5517,27 @@ datetime_utcfromtimestamp(PyObject *cls, PyObject *args)
static PyObject *
datetime_strptime(PyObject *cls, PyObject *args)
{
static PyObject *module = NULL;
neonene marked this conversation as resolved.
Show resolved Hide resolved
PyObject *string, *format;

if (!PyArg_ParseTuple(args, "UU:strptime", &string, &format))
return NULL;

PyObject *result = NULL;
PyObject *current_mod = NULL;
datetime_state *st = GET_CURRENT_STATE(current_mod);
PyObject *module = st->strptime;
if (module == NULL) {
module = PyImport_ImportModule("_strptime");
ericsnowcurrently marked this conversation as resolved.
Show resolved Hide resolved
neonene marked this conversation as resolved.
Show resolved Hide resolved
if (module == NULL)
return NULL;
if (module == NULL) {
goto exit;
}
st->strptime = module;
}
return PyObject_CallMethodObjArgs(module, &_Py_ID(_strptime_datetime),
cls, string, format, NULL);
result = PyObject_CallMethodObjArgs(module, &_Py_ID(_strptime_datetime),
cls, string, format, NULL);
exit:
RELEASE_CURRENT_STATE(st, current_mod);
return result;
}

/* Return new datetime from date/datetime and time arguments. */
Expand Down Expand Up @@ -7057,6 +7068,7 @@ init_state(datetime_state *st, PyObject *module, PyObject *old_module)
.us_per_week = Py_NewRef(st_old->us_per_week),
.seconds_per_day = Py_NewRef(st_old->seconds_per_day),
.epoch = Py_NewRef(st_old->epoch),
.strptime = st->strptime,
};
return 0;
}
Expand Down Expand Up @@ -7109,7 +7121,8 @@ traverse_state(datetime_state *st, visitproc visit, void *arg)
{
/* heap types */
Py_VISIT(st->isocalendar_date_type);

/* containers */
Py_VISIT(st->strptime);
return 0;
}

Expand All @@ -7125,6 +7138,7 @@ clear_state(datetime_state *st)
Py_CLEAR(st->us_per_week);
Py_CLEAR(st->seconds_per_day);
Py_CLEAR(st->epoch);
Py_CLEAR(st->strptime);
return 0;
}

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