Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Revert "Kinda fix the C API tests"
This reverts commit 863ecd7.
  • Loading branch information
erlend-aasland committed May 22, 2024
commit 8de63177e7a503cbaa44c328707d938d2ae690f3
6 changes: 1 addition & 5 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -6384,18 +6384,14 @@ 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.setup_capi()

def tearDown(self):
_testcapi.teardown_capi()
_testcapi.test_datetime_capi()

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

static PyObject *
setup_capi(PyObject *self, PyObject *args)
test_datetime_capi(PyObject *self, PyObject *args)
{
if (!test_run_counter++) {
PyDateTime_IMPORT;
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;
}
assert(PyDateTimeAPI);
Py_RETURN_NONE;
}

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

/* Functions exposing the C API type checking for testing */
Expand Down Expand Up @@ -462,8 +467,7 @@ 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},
{"setup_capi", setup_capi, METH_NOARGS},
{"teardown_capi", teardown_capi, METH_NOARGS},
{"test_datetime_capi", test_datetime_capi, METH_NOARGS},
{NULL},
};

Expand Down