Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug report
Bug description:
python 3.13 raises anRecursionError when I try todeepcopy the ast in the following example.
importastimportcopycode="""('',)while i < n: if ch == '': ch = format[i] if ch == '': if freplace is None: '' % getattr(object) elif ch == '': if zreplace is None: if hasattr: offset = object.utcoffset() if offset is not None: if offset.days < 0: offset = -offset h = divmod(timedelta(hours=0)) if u: zreplace = '' % (sign,) elif s: zreplace = '' % (sign,) else: zreplace = '' % (sign,) elif ch == '': if Zreplace is None: Zreplace = '' if hasattr(object): s = object.tzname() if s is not None: Zreplace = s.replace('') newformat.append(Zreplace) else: push('') else: push(ch)"""tree=ast.parse(code)# add a back reference to the parent nodefornodeinast.walk(tree):forchildinast.iter_child_nodes(node):child.parent=nodetree2=copy.deepcopy(tree)
output (Python 3.13.0b1+):
Traceback (mostrecentcalllast):File"/home/frank/projects/cpython/../cpython_bugs/deep_copy_ast_with_parents.py",line50,in<module>tree2=copy.deepcopy(tree)File"/home/frank/projects/cpython/Lib/copy.py",line162,indeepcopyy=_reconstruct(x,memo,*rv)File"/home/frank/projects/cpython/Lib/copy.py",line253,in_reconstructy=func(*args)File"/home/frank/projects/cpython/Lib/copy.py",line252,in<genexpr>args= (deepcopy(arg,memo)forarginargs)~~~~~~~~^^^^^^^^^^^File"/home/frank/projects/cpython/Lib/copy.py",line136,indeepcopyy=copier(x,memo)File"/home/frank/projects/cpython/Lib/copy.py",line196,in_deepcopy_listappend(deepcopy(a,memo))~~~~~~~~^^^^^^^^^...File"/home/frank/projects/cpython/Lib/copy.py",line162,indeepcopyy=_reconstruct(x,memo,*rv)File"/home/frank/projects/cpython/Lib/copy.py",line259,in_reconstructstate=deepcopy(state,memo)File"/home/frank/projects/cpython/Lib/copy.py",line136,indeepcopyy=copier(x,memo)File"/home/frank/projects/cpython/Lib/copy.py",line221,in_deepcopy_dicty[deepcopy(key,memo)]=deepcopy(value,memo)~~~~~~~~^^^^^^^^^^^^^File"/home/frank/projects/cpython/Lib/copy.py",line162,indeepcopyy=_reconstruct(x,memo,*rv)File"/home/frank/projects/cpython/Lib/copy.py",line253,in_reconstructy=func(*args)File"/home/frank/projects/cpython/Lib/copy.py",line252,in<genexpr>args= (deepcopy(arg,memo)forarginargs)~~~~~~~~^^^^^^^^^^^File"/home/frank/projects/cpython/Lib/copy.py",line136,indeepcopyy=copier(x,memo)RecursionError:maximumrecursiondepthexceeded
The problem can be reproduced on the current main (5c02ea8)
I was able to bisect the problem down toed4dfd8 (@JelleZijlstra may be you know more here)
The code in the example looks big but is already minimized.
I hope that this example is simple enough to find and fix the bug, I can try to find a smaller one if it is needed.
CPython versions tested on:
3.13, CPython main branch
Operating systems tested on:
No response