[issue6921] recursion wierdness ... related to variable scoping?
David Hanney
report at bugs.python.org
Wed Sep 16 17:38:44 CEST 2009
New submission from David Hanney <spam2008 at nney.com>:
I was playing about with ideas behind b-plus-trees
and found i python bug
the important bit of code is:
for p, k in enumerate(self.keys):
self.ptrs[p].dump(indent+1, kmin=lk, kmax=k)
print sindent + "(" + str(k) + ")"
lk = k
#
# python bug?
#
show_python_bug = len(sys.argv)>1
if show_python_bug:
#
# why not this?
assert p == len(self.keys)-1
else:
#
# why do I need this instead?
p = len(self.keys)-1
i'm expecting k to still be in scope and equal len(self.keys)-1
sometimes it is and sometimes it isn't (depending on recursion depth)
you can try it for yourselves as I attach the full program:
./btree.py
runs fine
./btree.py show_python_bug
eventually breaks like this:
Traceback (most recent call last):
File "./btree.py", line 235, in <module>
page.dump()
File "./btree.py", line 223, in dump
self.ptrs[p+1].dump(indent+1, kmin=lk)
File "./btree.py", line 223, in dump
self.ptrs[p+1].dump(indent+1, kmin=lk)
File "./btree.py", line 223, in dump
self.ptrs[p+1].dump(indent+1, kmin=lk)
File "./btree.py", line 217, in dump
assert p == len(self.keys)-1
UnboundLocalError: local variable 'p' referenced before assignment
... despite executing that code many times successfully before this
happens ... strange!
I hope you can figure it out and that this report proves helpful.
----------
components: Interpreter Core
files: btree.py
messages: 92691
nosy: mrdiskodave
severity: normal
status: open
title: recursion wierdness ... related to variable scoping?
type: behavior
versions: Python 2.6
Added file: http://bugs.python.org/file14897/btree.py
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6921>
_______________________________________
More information about the Python-bugs-list
mailing list