This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: UnboundLocalError in cgitb.py
Type: Stage:
Components: Library (Lib) Versions: Python 2.3
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: skip.montanaro Nosy List: jemfinch, nnorwitz, skip.montanaro
Priority: normal Keywords:

Created on 2003-12-16 23:09 by jemfinch, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
lookup.py skip.montanaro, 2003-12-18 17:31
Messages (8)
msg19427 - (view) Author: Jeremy Fincher (jemfinch) Date: 2003-12-16 23:09
Here's the exception: 
 
Exception 
    return cgitb.text((E, e, tb)).rstrip('\r\n') 
  File "/usr/lib/python2.3/cgitb.py", line 197, in text 
    vars = scanvars(reader, frame, locals) 
  File "/usr/lib/python2.3/cgitb.py", line 76, in scanvars 
    parent = value 
UnboundLocalError: local variable 'value' referenced 
before assignment 
 
And here's the code: 
 
            if lasttoken == '.': 
                if parent is not __UNDEF__: 
                    value = getattr(parent, token, __UNDEF__) 
                    vars.append((prefix + token, prefix, value)) 
            else: 
                where, value = lookup(token, frame, locals) 
                vars.append((token, where, value)) 
        elif token == '.': 
            prefix += lasttoken + '.' 
            parent = value 
 
If lasttoken is '.' and parent is __UNDEF__, value doesn't 
get set.  I'd offer a patch, but I really have no idea what this 
code is doing and so don't know what to set value to. 
msg19428 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-12-18 15:02
Logged In: YES 
user_id=33168

Jeremy, can you attach a complete test case?

Skip, I realize you didn't write the scanvars code (Ping
did), but you did add text.  So maybe you have an idea. 
From a brief scan, it looks like if value is initialized to
None, the code might work.  
msg19429 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2003-12-18 16:13
Logged In: YES 
user_id=44345

Sorry, I have no idea what it does either.  I'll look into it a bit,
but don't expect a miracle. :-(
msg19430 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2003-12-18 17:31
Logged In: YES 
user_id=44345

I pulled scanvars() and lookup() out into a separate file and 
instrumented things a little.  From staring at the code I thought
that perhaps it didn't handle the case where the line starts with 
a dot separating an object and its attribute.  I can't provoke a
failure in that situation though.  See attached lookup.py.

Jeremy, can you fiddle it to make it fail?
msg19431 - (view) Author: Jeremy Fincher (jemfinch) Date: 2003-12-18 22:56
Logged In: YES 
user_id=99508

To be honest, it's just something I ran into in my own test suite 
when my exception-handling logging code caught an exception I 
didn't expect to get raised.  Since I have no idea what the code in 
cgitb that's raising the exception is actually doing, I really don't 
know how to begin to try and narrow it down to a test case. 
msg19432 - (view) Author: Jeremy Fincher (jemfinch) Date: 2003-12-18 23:02
Logged In: YES 
user_id=99508

The code that caused the raised exception (which subsequently 
triggered the exception in cgitb) was this: 
 
        outputstr = '%d %s match \'%s\' (%s):' % (len(bugs), \ 
        utils.pluralize(len(bugs), 'bug'), 
        searchstr, utils.commaAndify(keywords, And='AND')) 
 
That's actually from the diff where I changed it to be a bit more 
idiomatic :)  It wasn't my code, I promise! 
msg19433 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-12-18 23:32
Logged In: YES 
user_id=33168

Jeremy, that's still not enough to reproduce the bug.  What
exception was thrown by the code below and from where?  What
were the values of bugs, searchstr, and keywords?  What are
the definitions for utils.pluralize() and utils.commaAndify()?
msg19434 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2005-05-20 03:26
Logged In: YES 
user_id=44345

Looks like this was fixed at some point.  The local variable value is
clearly initialized in scanvars() at this point.
History
Date User Action Args
2022-04-11 14:56:01adminsetgithub: 39713
2003-12-16 23:09:19jemfinchcreate