This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: .replace functions in datetime do not call __new__
Type: behavior Stage:
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: joelgibson
Priority: normal Keywords:

Created on 2021-08-09 22:58 by joelgibson, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg399295 - (view) Author: Joel Gibson (joelgibson) Date: 2021-08-09 22:58
I've come here while investigating a segfault when datetime.replace(...) was called with a Pandas Timestamp object:
https://github.com/pandas-dev/pandas/issues/42305

The Python implementation of datetime.replace
https://github.com/python/cpython/blob/03e5647ab07c7d2a05094fc3b5ed6eba6fc01349/Lib/datetime.py#L1823
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
https://github.com/python/cpython/blob/03e5647ab07c7d2a05094fc3b5ed6eba6fc01349/Modules/_datetimemodule.c#L5845
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
https://github.com/python/cpython/blob/03e5647ab07c7d2a05094fc3b5ed6eba6fc01349/Modules/_datetimemodule.c#L5872
should be replaced by a call to PyObject_new(Py_TYPE(self), ...), or something similar.

This also affects date.replace and time.replace.
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89039
2021-08-09 22:58:31joelgibsoncreate