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: profiler invents error in nested scopes
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: jhylton Nosy List: caeshmer, gvanrossum, jhylton
Priority: normal Keywords:

Created on 2001-07-31 20:55 by caeshmer, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Messages (3)
msg5723 - (view) Author: W. G. Mitchener (caeshmer) Date: 2001-07-31 20:55
I'm trying to use the profiler in the Python 2.1 RPM on
Red Hat 7.0.

Here's the most reduced form of problem I could come up
with.  This is the file ProfilerBug.py:

from __future__ import nested_scopes

class Spam:

    def __init__(self, x, y, z):
        eggs = (self, x, y, z)
        a = 4
        b = 5
        c = 6
        def f(t):
            return a + b + c + t


def _test1():
    pr = Spam(1, 2, 3)

If I do this:

Python 2.1 (#1, Jul 13 2001, 10:27:46) 
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-85)] on linux2
Type "copyright", "credits" or "license" for more
information.
>>> import ProfilerBug
>>> ProfilerBug._test1()
>>> 

there's no error.  But here's what I get when I run the
profiler:

Python 2.1 (#1, Jul 13 2001, 10:27:46) 
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-85)] on linux2
Type "copyright", "credits" or "license" for more
information.
>>> import profile
>>> import ProfilerBug
>>> profile.run('ProfilerBug._test1()')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.1/profile.py", line 71, in run
    prof = prof.run(statement)
  File "/usr/lib/python2.1/profile.py", line 356, in run
    return self.runctx(cmd, dict, dict)
  File "/usr/lib/python2.1/profile.py", line 362, in runctx
    exec cmd in globals, locals
  File "<string>", line 1, in ?
  File "ProfilerBug.py", line 15, in _test1
    pr = Spam(1, 2, 3)
  File "ProfilerBug.py", line 6, in __init__
    eggs = (self, x, y, z)
UnboundLocalError: local variable 'self' referenced
before assignment

It has something to do with nested scopes, because if I
comment out the definition of f, the error goes away.
msg5724 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-07-31 23:31
Logged In: YES 
user_id=6380

Sounds like one for Jeremy.  Have you tried this with the
bugfix release 2.1.1 yet?
msg5725 - (view) Author: Jeremy Hylton (jhylton) (Python triager) Date: 2001-08-01 19:53
Logged In: YES 
user_id=31392

This appears to be fixed in 2.1.1.
History
Date User Action Args
2022-04-10 16:04:16adminsetgithub: 34873
2001-07-31 20:55:57caeshmercreate