Skip to content

Commit 9af13a6

Browse files
committed
Add subinterp tests
1 parent 0377764 commit 9af13a6

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

Lib/test/datetimetester.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7313,6 +7313,60 @@ def gen():
73137313
res = script_helper.assert_python_ok('-c', script)
73147314
self.assertFalse(res.err)
73157315

7316+
def test_static_type_at_shutdown3(self):
7317+
script = textwrap.dedent(f'''
7318+
import textwrap
7319+
from test import support
7320+
7321+
subinterp_script = textwrap.dedent(f"""
7322+
from _testcapi import get_delta_type
7323+
# Test without calling test_datetime_capi() in subinterp
7324+
timedelta = get_delta_type()
7325+
7326+
def gen():
7327+
try:
7328+
yield
7329+
finally:
7330+
timedelta(days=1)
7331+
7332+
it = gen()
7333+
next(it)
7334+
""")
7335+
7336+
import _testcapi
7337+
_testcapi.test_datetime_capi()
7338+
ret = support.run_in_subinterp(subinterp_script)
7339+
assert ret == 0
7340+
''')
7341+
7342+
res = script_helper.assert_python_ok('-c', script)
7343+
self.assertIn(b'ImportError: sys.meta_path is None', res.err)
7344+
7345+
def test_static_type_before_shutdown(self):
7346+
script = textwrap.dedent(f'''
7347+
import textwrap
7348+
from test import support
7349+
7350+
subinterp_script = textwrap.dedent(f"""
7351+
from _testcapi import get_delta_type
7352+
# Test without calling test_datetime_capi() in subinterp
7353+
7354+
import sys
7355+
assert '_datetime' not in sys.modules
7356+
timedelta = get_delta_type()
7357+
timedelta(days=1)
7358+
assert '_datetime' in sys.modules
7359+
""")
7360+
7361+
import _testcapi
7362+
_testcapi.test_datetime_capi()
7363+
ret = support.run_in_subinterp(subinterp_script)
7364+
assert ret == 0
7365+
''')
7366+
7367+
res = script_helper.assert_python_ok('-c', script)
7368+
self.assertFalse(res.err)
7369+
73167370
def test_remain_only_one_module(self):
73177371
script = textwrap.dedent("""
73187372
import sys

Modules/_datetimemodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ _get_current_state(PyObject **p_mod)
175175
mod = PyImport_ImportModule("_datetime");
176176
if (mod == NULL) {
177177
assert(_Py_IsInterpreterFinalizing(interp));
178-
/* Ask users to take care about the unlikely case
179-
* that happened after restarting the interpreter. */
178+
/* We do not take care of the unlikely case. */
180179
return NULL;
181180
}
182181
}

Modules/_testcapi/datetime.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,12 @@ test_PyDateTime_DELTA_GET(PyObject *self, PyObject *obj)
453453
return Py_BuildValue("(iii)", days, seconds, microseconds);
454454
}
455455

456+
static PyObject *
457+
get_delta_type(PyObject *self, PyObject *args)
458+
{
459+
return PyDateTimeAPI ? Py_NewRef(PyDateTimeAPI->DeltaType) : Py_None;
460+
}
461+
456462
static PyMethodDef test_methods[] = {
457463
{"PyDateTime_DATE_GET", test_PyDateTime_DATE_GET, METH_O},
458464
{"PyDateTime_DELTA_GET", test_PyDateTime_DELTA_GET, METH_O},
@@ -469,6 +475,7 @@ static PyMethodDef test_methods[] = {
469475
{"get_datetime_fromdateandtimeandfold", get_datetime_fromdateandtimeandfold, METH_VARARGS},
470476
{"get_datetime_fromtimestamp", get_datetime_fromtimestamp, METH_VARARGS},
471477
{"get_delta_fromdsu", get_delta_fromdsu, METH_VARARGS},
478+
{"get_delta_type", get_delta_type, METH_NOARGS},
472479
{"get_time_fromtime", get_time_fromtime, METH_VARARGS},
473480
{"get_time_fromtimeandfold", get_time_fromtimeandfold, METH_VARARGS},
474481
{"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS},

0 commit comments

Comments
 (0)