New submission from Joel Gibson <j...@jgibson.id.au>:
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. ---------- components: Library (Lib) messages: 399295 nosy: joelgibson priority: normal severity: normal status: open title: .replace functions in datetime do not call __new__ type: behavior _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue44876> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com