diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a41353c..a75559e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,10 @@ Changelog ========= +* Fix ``SystemError`` on Python 3.13 and Windows when starting time travelling. + + Thanks to Bernát Gábor for the report in `Issue #456 `__. + 2.14.1 (2024-03-22) ------------------- diff --git a/src/_time_machine.c b/src/_time_machine.c index 55e434b..9986c87 100644 --- a/src/_time_machine.c +++ b/src/_time_machine.c @@ -423,21 +423,25 @@ _time_machine_patch_if_needed(PyObject *module, PyObject *unused) PyObject *time_module = PyImport_ImportModule("time"); - - - PyCFunctionObject *time_clock_gettime = (PyCFunctionObject *) PyObject_GetAttrString(time_module, "clock_gettime"); /* - time.clock_gettime() is not always available - e.g. on builds against old macOS = official Python.org installer + time.clock_gettime(), only available on Unix platforms. */ - if (time_clock_gettime != NULL) { + PyCFunctionObject *time_clock_gettime = (PyCFunctionObject *) PyObject_GetAttrString(time_module, "clock_gettime"); + if (time_clock_gettime == NULL) { + PyErr_Clear(); + } else { state->original_clock_gettime = time_clock_gettime->m_ml->ml_meth; time_clock_gettime->m_ml->ml_meth = _time_machine_clock_gettime; Py_DECREF(time_clock_gettime); } + /* + time.clock_gettime_ns(), only available on Unix platforms. + */ PyCFunctionObject *time_clock_gettime_ns = (PyCFunctionObject *) PyObject_GetAttrString(time_module, "clock_gettime_ns"); - if (time_clock_gettime_ns != NULL) { + if (time_clock_gettime_ns == NULL) { + PyErr_Clear(); + } else { state->original_clock_gettime_ns = time_clock_gettime_ns->m_ml->ml_meth; time_clock_gettime_ns->m_ml->ml_meth = _time_machine_clock_gettime_ns; Py_DECREF(time_clock_gettime_ns);