Tim, check this out:

>>> import re, gc
>>> x = re.compile("x")
>>> gc.get_referents(x.__class__)[-1]
<module '_sre' (built-in)>

That seems due to:

https://github.com/python/cpython/blob/e90e0422182f4ca7faefd19c629f84aebb34e2ee/Objects/typeobject.c#L4241


On Thu, 27 May 2021 at 20:15, Tim Peters <tim.pet...@gmail.com> wrote:

> [Tim]
> >> And if a type pointer is the only thing being visited, then there's no
> point
> >> unless the object can itself be reachable from the type object.
>
> {Pablo]
> > But that could happen easily for heap types as they are mutable by
> > default. For instance, you set the instance in a global:
> >
> > type -> module -> globals -> instance -> type
>
> Sorry, but this is apparently a rat's nest and that's too sketchy for me
> ;-)
>
> Can you flesh this out for what stumbled into being my running
> example? That is, how could a regexp pattern object be part of a
> cycle?
>
> >>> import re
> >>> p = re.compile("ab*c")
> >>> type(p)
> <class 're.Pattern'>
> >>> type(p).__module__
> 're'
> >>> type(type(p).__module__)
> <class 'str'>
>
> That is, its __module__ attribute is a string, not a module object.
>
> And, in general, I can't add attributes to p, or change their bindings:
>
> >>> p.flags
> 32
> >>> p.flags = re
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: readonly attribute
> >>> p.newattr = re
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 're.Pattern' object has no attribute 'newattr'
>
> I just don't see a way to trick it into being part of a cycle.
>
> Not denying that safe is better than sorry, though.
>
_______________________________________________
python-committers mailing list -- python-committers@python.org
To unsubscribe send an email to python-committers-le...@python.org
https://mail.python.org/mailman3/lists/python-committers.python.org/
Message archived at 
https://mail.python.org/archives/list/python-committers@python.org/message/UO6K3E6MBE6YJJWDSU6VVEUYFS2ONWOT/
Code of Conduct: https://www.python.org/psf/codeofconduct/

Reply via email to