Skip to content

Commit fa0cc40

Browse files
committed
Update tests
1 parent 0e4a263 commit fa0cc40

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

Lib/test/datetimetester.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7272,7 +7272,7 @@ def test_update_type_cache(self):
72727272
res = script_helper.assert_python_ok('-c', script)
72737273
self.assertFalse(res.err)
72747274

7275-
def test_module_state_at_shutdown(self):
7275+
def test_static_type_at_shutdown1(self):
72767276
# gh-132413
72777277
script = textwrap.dedent("""
72787278
import sys
@@ -7283,7 +7283,27 @@ def gen():
72837283
yield
72847284
finally:
72857285
assert not sys.modules
7286-
td = _datetime.timedelta(days=1) # crash
7286+
td = _datetime.timedelta(days=1)
7287+
assert td.days == 1
7288+
assert not sys.modules
7289+
7290+
it = gen()
7291+
next(it)
7292+
""")
7293+
res = script_helper.assert_python_ok('-c', script)
7294+
self.assertFalse(res.err)
7295+
7296+
def test_static_type_at_shutdown2(self):
7297+
script = textwrap.dedent("""
7298+
import sys
7299+
from _datetime import timedelta
7300+
7301+
def gen():
7302+
try:
7303+
yield
7304+
finally:
7305+
assert not sys.modules
7306+
td = timedelta(days=1)
72877307
assert td.days == 1
72887308
assert not sys.modules
72897309
@@ -7301,8 +7321,9 @@ def test_remain_only_one_module(self):
73017321
ws = weakref.WeakSet()
73027322
for _ in range(3):
73037323
import _datetime
7324+
td = _datetime.timedelta
73047325
ws.add(_datetime)
7305-
del sys.modules["_datetime"]
7326+
del sys.modules['_datetime']
73067327
del _datetime
73077328
gc.collect()
73087329
assert len(ws) == 1

Modules/_datetimemodule.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,7 @@ _get_current_state(PyObject **p_mod)
165165
{
166166
PyInterpreterState *interp = PyInterpreterState_Get();
167167
PyObject *mod = get_current_module(interp, NULL);
168-
if (mod == NULL) {
169-
assert(!PyErr_Occurred());
170-
if (PyErr_Occurred()) {
171-
return NULL;
172-
}
173-
assert(!_Py_IsInterpreterFinalizing(interp));
174-
/* The static types can outlive the module,
175-
* so we must re-import the module. */
176-
mod = PyImport_ImportModule("_datetime");
177-
if (mod == NULL) {
178-
return NULL;
179-
}
180-
}
168+
assert (mod != NULL);
181169
datetime_state *st = get_module_state(mod);
182170
*p_mod = mod;
183171
return st;

0 commit comments

Comments
 (0)