You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.replace functions in datetime do not call __new__ #89039
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:
assignee=Noneclosed_at=Nonecreated_at=<Date2021-08-09.22:58:31.112>labels= ['type-bug', 'library']
title='.replace functions in datetime do not call __new__'updated_at=<Date2021-08-09.22:58:31.112>user='https://github.com/joelgibson'
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).
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
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.
Activity
joelgibson commentedon Aug 9, 2021
I've come here while investigating a segfault when datetime.replace(...) was called with a Pandas Timestamp object:
pandas-dev/pandas#42305
The Python implementation of datetime.replace
cpython/Lib/datetime.py
Line 1823 in 03e5647
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
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
should be replaced by a call to PyObject_new(Py_TYPE(self), ...), or something similar.
This also affects date.replace and time.replace.
pythongh-89039: call subclass constructors in datetime.*.replace
pythongh-89039: call subclass constructors in datetime.*.replace
gh-89039: Call subclass constructors in datetime.*.replace (GH-114780)
pythongh-89039: Call subclass constructors in datetime.*.replace (pyt…
StanFromIreland commentedon Mar 12, 2025
Can this be closed? It doesn't seem its going to be back ported any time soon.
picnixz commentedon Mar 13, 2025
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.
[3.12] pythongh-89039: Call subclass constructors in datetime.*.repla…
serhiy-storchaka commentedon Mar 14, 2025
There 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.
[3.12] gh-89039: Call subclass constructors in datetime.*.replace (GH…
Python: fix Python 3.13 incompatibility
Python: Fix datetime.replace(tzinfo=None) with Python 3.13
Fix datetime.replace(tzinfo=None) with Python 3.13
Fix datetime.replace(tzinfo=None) with Python 3.13