Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 15 additions & 1 deletion src/_time_machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,21 @@ _time_machine_original_now(
return NULL; // Propagate AttributeError
}

PyObject *result = state->original_now(datetime_class, args, nargs, kwnames);
PyObject *result;
if (state->original_now == NULL) {
PyCFunctionObject *datetime_datetime_now =
(PyCFunctionObject *)PyObject_GetAttrString(datetime_class, "now");
if (datetime_datetime_now == NULL) {
Py_DECREF(datetime_class);
Py_DECREF(datetime_module);
return NULL; // Propagate AttributeError
}
result = _PyObject_Vectorcall((PyObject *)datetime_datetime_now, args, nargs, kwnames);
Py_DECREF(datetime_datetime_now);
}
else {
result = state->original_now(datetime_class, args, nargs, kwnames);
}

Py_DECREF(datetime_class);
Py_DECREF(datetime_module);
Expand Down
3 changes: 3 additions & 0 deletions tests/test_time_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,9 @@ def test_datetime_now(self):
eh_now = time_machine.escape_hatch.datetime.datetime.now()
assert eh_now >= real_now

eh_now2 = time_machine.escape_hatch.datetime.datetime.now()
assert eh_now2 >= eh_now

def test_datetime_now_tz(self):
real_now = dt.datetime.now(tz=dt.timezone.utc)

Expand Down