Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

C API/GC: datetime.datetime doesn't havePy_TPFLAGS_HAVE_GC, potentially causing leaks intzinfo cycles #132705

Open
Labels
@ariebovenberg

Description

@ariebovenberg

Bug report

Bug description:

TheGC docs say (emphasis mine)

Python’s support for detecting and collecting garbage which involves circular referencesrequires support from object types which are “containers” for other objects which may also be containers. Types which do not store references to other objects, or which only store references to atomic types (such as numbers or strings), do not need to provide any explicit support for garbage collection.

However indatetime,PyDateTime_DateTimeType is a container (oftzinfo), butdoesn’t have the GC flags set.

Withhelp on Python's discuss forums, I was able to get to this minimal example that appears to show a possible leak.

fromdatetimeimportdatetime,tzinfoimportgc,sysclassMyTzinfo(tzinfo):passtz=MyTzinfo()dt=datetime(2000,1,1,tzinfo=tz)tz.foo=dt# circular ref introduced hereassertsys.getrefcount(tz)==3# 1 from dt, 1 variable, 1extra from passing it inassertsys.getrefcount(tz)==3# 1 from tz, 1 variable, 1extra from passing it inassertlen([objforobjingc.get_objects()ifisinstance(obj,MyTzinfo)])==1deltzassertlen([objforobjingc.get_objects()ifisinstance(obj,MyTzinfo)])==1deldtgc.collect()# At this point, dt and tz should be goneassertlen([objforobjingc.get_objects()ifisinstance(obj,MyTzinfo)])==0

If datetime is replaced with a dummy class, the above program works:

classdatetime:def__init__(self,*args,**kwargs):self.args=argsself.kwargs=kwargs

I'm happy to submit a PR if needed.

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp