[Python-bugs-list] [ python-Bugs-735301 ] timeit.py doesn't see module globals

SourceForge.net noreply@sourceforge.net
Thu, 15 May 2003 18:32:01 -0700


Bugs item #735301, was opened at 2003-05-09 11:47
Message generated for change (Comment added) made by gvanrossum
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=735301&group_id=5470

Category: Python Library
Group: Python 2.3
Status: Closed
Resolution: Wont Fix
Priority: 5
Submitted By: Paul Moore (pmoore)
Assigned to: Nobody/Anonymous (nobody)
Summary: timeit.py doesn't see module globals

Initial Comment:
Simple test file (save as test.py)

def test():
    "Stupid test function"
    L = []
    for i in range(100):
        L.append(i)

if __name__=='__main__':
    from timeit import Timer
    t = Timer("test()")
    print t.timeit()

Try to run this, and the following traceback occurs:

>test.py
Traceback (most recent call last):
  File "C:\Data\graph\test.py", line 10, in ?
    print t.timeit()
  File "C:\Apps\Python\lib\timeit.py", line 159, in timeit
    return self.inner(it, self.timer)
  File "<timeit-src>", line 6, in inner
NameError: global name 'test' is not defined



----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2003-05-15 21:32

Message:
Logged In: YES 
user_id=6380

Yes, I know it's easy to provide the feature, but I'm
against the feature.

----------------------------------------------------------------------

Comment By: Paul Moore (pmoore)
Date: 2003-05-12 05:28

Message:
Logged In: YES 
user_id=113328

If "Wont Fix" means just that, then that's OK. But for what it's 
worth, this change to timeit.py seems to do what I'd expect.

--- timeit.py.orig	2003-05-12 10:23:55.000000000 +0100
+++ timeit.py	2003-05-12 10:24:06.000000000 +0100
@@ -115,7 +115,8 @@
         self.src = src # Save for traceback display
         code = compile(src, dummy_src_name, "exec")
         ns = {}
-        exec code in globals(), ns
+        gbl = sys._getframe(1).f_globals
+        exec code in gbl, ns
         self.inner = ns["inner"]
 
     def print_exc(self, file=None):


----------------------------------------------------------------------

Comment By: Skip Montanaro (montanaro)
Date: 2003-05-09 14:22

Message:
Logged In: YES 
user_id=44345

I added an example of this to the module documentation.


----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2003-05-09 14:06

Message:
Logged In: YES 
user_id=6380

You must import the test function from __main__ in the setup
code fragment, e.g.

t = Timer("test()", "from __main__ import test")



----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=735301&group_id=5470