Mailing List Archive

Problem with profiler module
I have problem with profiler module. Below is listing of python
interpreter session. I can run profiler from command line with now
problem. What is the problem?

Python 1.5.2 (#1, Apr 30 1999, 22:36:07) [GCC 2.7.2] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> def f(x):
x.append(1)


>>> import profile
>>> a=[]
>>> profile.run('f(a)')
Traceback (innermost last):
File "<pyshell#6>", line 1, in ?
profile.run("f(a)")
File "/usr/local/lib/python1.5/profile.py", line 62, in run
prof = prof.run(statement)
File "/usr/local/lib/python1.5/profile.py", line 348, in run
return self.runctx(cmd, dict, dict)
File "/usr/local/lib/python1.5/profile.py", line 354, in runctx
exec cmd in globals, locals
File "<string>", line 1, in ?
NameError: f
Problem with profiler module [ In reply to ]
Tomaz Ficko:
|I have problem with profiler module. Below is listing of python
|interpreter session. I can run profiler from command line with now
|problem. What is the problem?
|
|Python 1.5.2 (#1, Apr 30 1999, 22:36:07) [GCC 2.7.2] on linux2
|Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
|>>> def f(x):
| x.append(1)
|
|
|>>> import profile
|>>> a=[]
|>>> profile.run('f(a)')
|Traceback (innermost last):
| File "<pyshell#6>", line 1, in ?
| profile.run("f(a)")
| File "/usr/local/lib/python1.5/profile.py", line 62, in run
| prof = prof.run(statement)
| File "/usr/local/lib/python1.5/profile.py", line 348, in run
| return self.runctx(cmd, dict, dict)
| File "/usr/local/lib/python1.5/profile.py", line 354, in runctx
| exec cmd in globals, locals
| File "<string>", line 1, in ?
|NameError: f

Having gone through this myself recently (albeit not in idle), you
might try:

import profile
a=[]
prof = profile.Profile()
prof.runctx( 'f(a)', globals(), locals() )
prof.print_stats()

If that works, you can just define:

import profile
def runctx(statement, globals, locals, *args):
prof = profile.Profile()
try:
prof = prof.runctx( statement, globals, locals )
except SystemExit:
pass
if args:
prof.dump_stats(args[0])
else:
return prof.print_stats()

and then subsequently use:

a=[]
runctx( 'f(a)', globals(), locals() )

Hopefully this convenience function will be added to profile.py in the next
version.

Randall