Repository navigation
.replace functions in datetime do not call __new__ #89039
Description
Activity
I've come here while investigating a segfault when datetime.replace(...) was called with a Pandas Timestamp object:
pandas-dev/pandas#42305The Python implementation of datetime.replace
Line 1823 in 03e5647
def replace(self, year=None, month=None, day=None, hour=None,
is polymorphic, in the sense that if a custom object like pd.Timestamp is passed in, a pd.Timestamp will come out rather than a datetime. This works just fine (copying and using Python code makes this segfault disappear).The C implementation is also polymorphic
cpython/Modules/_datetimemodule.c
Line 5845 in 03e5647
datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
but I think that something in the C implementation is wrong: eventually tp_alloc gets called for the passed type, but never tp_new, and then afterwards the custom type is treated just like a regular datetime. In the pd.Timestamp case, there are some extra fields that only get set in the __new__ method, leading to a segfault later when they're accessed.I'm not familiar enough with the C/CPython interface (especially object setup and initialisation) to tell where a fix for this should go, I would assume that the line
cpython/Modules/_datetimemodule.c
Line 5872 in 03e5647
clone = datetime_new(Py_TYPE(self), tuple, NULL);
should be replaced by a call to PyObject_new(Py_TYPE(self), ...), or something similar.This also affects date.replace and time.replace.
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Aug 9, 2021 - added3.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13only security fixesonly security fixes
on Feb 12, 2024 Can this be closed? It doesn't seem its going to be back ported any time soon.
I think it's better not to backport it now. 3.11 is security-only and 3.12 is nearing its last bugfix and since the ABI seemed to have changed, and we needed the RM approval, it's better to be on the safe side.
Reacted by Stan UlbrychThere was a small discussion about backporting in the PR. It seems there were no serious objections.
I created a backport to 3.12. It does not change the ABI. Previous complains were likely caused by using Argument Clinic in the backport.
In any case, I leave this on the release manager, @Yhg1s.
- added a commit that references this issue
on Apr 2, 2025 - added a commit that references this issue
on Sep 9, 2025 - added a commit that references this issue
on Sep 21, 2025 - added a commit that references this issue
on Oct 5, 2025 - added a commit that references this issue
on Oct 17, 2025
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
Rationale for not backporting: #89039 (comment).