Skip to content

Commit 863ecd7

Browse files
Kinda fix the C API tests
1 parent 7211eb1 commit 863ecd7

2 files changed

Lines changed: 20 additions & 20 deletions

File tree

Lib/test/datetimetester.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6384,14 +6384,18 @@ class IranTest(ZoneInfoTest):
63846384

63856385
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
63866386
class CapiTest(unittest.TestCase):
6387+
63876388
def setUp(self):
63886389
# Since the C API is not present in the _Pure tests, skip all tests
63896390
if self.__class__.__name__.endswith('Pure'):
63906391
self.skipTest('Not relevant in pure Python')
63916392

63926393
# This *must* be called, and it must be called first, so until either
63936394
# restriction is loosened, we'll call it as part of test setup
6394-
_testcapi.test_datetime_capi()
6395+
_testcapi.setup_capi()
6396+
6397+
def tearDown(self):
6398+
_testcapi.teardown_capi()
63956399

63966400
def test_utc_capi(self):
63976401
for use_macro in (True, False):

Modules/_testcapi/datetime.c

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,21 @@
66
static int test_run_counter = 0;
77

88
static PyObject *
9-
test_datetime_capi(PyObject *self, PyObject *args)
9+
setup_capi(PyObject *self, PyObject *args)
1010
{
11-
if (PyDateTimeAPI) {
12-
if (test_run_counter) {
13-
/* Probably regrtest.py -R */
14-
Py_RETURN_NONE;
15-
}
16-
else {
17-
PyErr_SetString(PyExc_AssertionError,
18-
"PyDateTime_CAPI somehow initialized");
19-
return NULL;
20-
}
21-
}
22-
test_run_counter++;
23-
PyDateTime_IMPORT;
24-
25-
if (PyDateTimeAPI) {
26-
Py_RETURN_NONE;
11+
if (!test_run_counter++) {
12+
PyDateTime_IMPORT;
2713
}
28-
return NULL;
14+
assert(PyDateTimeAPI);
15+
Py_RETURN_NONE;
16+
}
17+
18+
static PyObject *
19+
teardown_capi(PyObject *self, PyObject *args)
20+
{
21+
test_run_counter--;
22+
assert(test_run_counter >= 0);
23+
Py_RETURN_NONE;
2924
}
3025

3126
/* Functions exposing the C API type checking for testing */
@@ -467,7 +462,8 @@ static PyMethodDef test_methods[] = {
467462
{"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS},
468463
{"get_timezones_offset_zero", get_timezones_offset_zero, METH_NOARGS},
469464
{"make_timezones_capi", make_timezones_capi, METH_NOARGS},
470-
{"test_datetime_capi", test_datetime_capi, METH_NOARGS},
465+
{"setup_capi", setup_capi, METH_NOARGS},
466+
{"teardown_capi", teardown_capi, METH_NOARGS},
471467
{NULL},
472468
};
473469

0 commit comments

Comments
 (0)