Skip to content

.replace functions in datetime do not call __new__ #89039

Description

@joelgibson
mannequin
BPO 44876
Nosy @joelgibson

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 = None
closed_at = None
created_at = <Date 2021-08-09.22:58:31.112>
labels = ['type-bug', 'library']
title = '.replace functions in datetime do not call __new__'
updated_at = <Date 2021-08-09.22:58:31.112>
user = 'https://github.com/joelgibson'

bugs.python.org fields:

activity = <Date 2021-08-09.22:58:31.112>
actor = 'joelgibson'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2021-08-09.22:58:31.112>
creator = 'joelgibson'
dependencies = []
files = []
hgrepos = []
issue_num = 44876
keywords = []
message_count = 1.0
messages = ['399295']
nosy_count = 1.0
nosy_names = ['joelgibson']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue44876'
versions = []

Linked PRs

Rationale for not backporting: #89039 (comment).

Activity

  1. joelgibson commented on Aug 9, 2021

    joelgibsonmannequin
    MannequinAuthor

    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

    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

    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

    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.

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on Aug 9, 2021
  3. transferred this issue fromon Apr 10, 2022
  4. added 2 commits that reference this issue on Jan 31, 2024
  5. added a commit that references this issue on Feb 12, 2024
  6. added a commit that references this issue on Feb 14, 2024
  7. StanFromIreland commented on Mar 12, 2025

    @StanFromIreland
    Member

    Can this be closed? It doesn't seem its going to be back ported any time soon.

  8. picnixz commented on Mar 13, 2025

    @picnixz
    Member

    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.

  9. added a commit that references this issue on Mar 14, 2025
  10. serhiy-storchaka commented on Mar 14, 2025

    @serhiy-storchaka
    Member

    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.

  11. added a commit that references this issue on Apr 2, 2025
  12. added a commit that references this issue on Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions