Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
Description
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