Skip to content

Commit

Permalink
Kinda fix the C API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed May 22, 2024
1 parent 7211eb1 commit 863ecd7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
6 changes: 5 additions & 1 deletion Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -6384,14 +6384,18 @@ class IranTest(ZoneInfoTest):

@unittest.skipIf(_testcapi is None, 'need _testcapi module')
class CapiTest(unittest.TestCase):

def setUp(self):
# Since the C API is not present in the _Pure tests, skip all tests
if self.__class__.__name__.endswith('Pure'):
self.skipTest('Not relevant in pure Python')

# This *must* be called, and it must be called first, so until either
# restriction is loosened, we'll call it as part of test setup
_testcapi.test_datetime_capi()
_testcapi.setup_capi()

def tearDown(self):
_testcapi.teardown_capi()

def test_utc_capi(self):
for use_macro in (True, False):
Expand Down
34 changes: 15 additions & 19 deletions Modules/_testcapi/datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,21 @@
static int test_run_counter = 0;

static PyObject *
test_datetime_capi(PyObject *self, PyObject *args)
setup_capi(PyObject *self, PyObject *args)
{
if (PyDateTimeAPI) {
if (test_run_counter) {
/* Probably regrtest.py -R */
Py_RETURN_NONE;
}
else {
PyErr_SetString(PyExc_AssertionError,
"PyDateTime_CAPI somehow initialized");
return NULL;
}
}
test_run_counter++;
PyDateTime_IMPORT;

if (PyDateTimeAPI) {
Py_RETURN_NONE;
if (!test_run_counter++) {
PyDateTime_IMPORT;
}
return NULL;
assert(PyDateTimeAPI);
Py_RETURN_NONE;
}

static PyObject *
teardown_capi(PyObject *self, PyObject *args)
{
test_run_counter--;
assert(test_run_counter >= 0);
Py_RETURN_NONE;
}

/* Functions exposing the C API type checking for testing */
Expand Down Expand Up @@ -467,7 +462,8 @@ static PyMethodDef test_methods[] = {
{"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS},
{"get_timezones_offset_zero", get_timezones_offset_zero, METH_NOARGS},
{"make_timezones_capi", make_timezones_capi, METH_NOARGS},
{"test_datetime_capi", test_datetime_capi, METH_NOARGS},
{"setup_capi", setup_capi, METH_NOARGS},
{"teardown_capi", teardown_capi, METH_NOARGS},
{NULL},
};

Expand Down

0 comments on commit 863ecd7

Please sign in to comment.