regex Info creating ref cycles

Create issue
Issue #355 resolved
Carson Ip created an issue


regex version: master

Python: CPython 2.7.15

I am noticing some ref cycles created by matching regex. These objects cannot be collected by CPython’s refcount and must rely on gc module to clean up.


import regex
import gc

assert regex.match(r"a(.)", "ab").group(1) == "b"


import objgraph
objgraph.show_backrefs(gc.garbage, filename='regex.png')

Thanks in advance.

Comments (6)

  1. Matthew Barnett repo owner

    It’s less of an issue in Python 3.4+, so I’ll fix it for the next release, whenever that is.

    Python 2 has already reached its EOL, so I don’t regard it as urgent unless it’s a serious issue in Python 3.

  2. Carson Ip reporter

    Hi @Matthew Barnett , sorry for replying a few months late. The fix that you added (6ea85d9) did not actually fix the ref cycle in my MCVE.

    However, I realize we could actually do info.defined_groups = None right before returning in _compile function. This would break the ref cycle and solve the issue. No more __del__ mess is needed. Does that sound acceptable to you?

    Here’s how I do it in my fork:


  3. Matthew Barnett repo owner

    This doesn’t affect Python 3, and, after supporting it for 10 years, Python 2 is past its EOL. Frankly, I’m not bothered. Sorry.

  4. Log in to comment