Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global variables not seen by cprofile.run() #131

Closed
fperez opened this issue Jul 2, 2010 · 5 comments
Closed

Global variables not seen by cprofile.run() #131

fperez opened this issue Jul 2, 2010 · 5 comments
Labels
Milestone

Comments

@fperez
Copy link
Member

fperez commented Jul 2, 2010

The core problem, as reported by Benjamin Root, is that this code doesn't work in IPython trunk:

import cProfile
import math

x = 25
cProfile.run("y = math.sqrt(x)")

This throws an exception NameError: name 'math' is not defined

In plain Python it runs OK.

For further details:
http://mail.scipy.org/pipermail/ipython-dev/2010-July/006193.html

@dwf
Copy link
Contributor

dwf commented Apr 9, 2011

The problem is that cProfile (and also regular old profile) imports __main__ and examines uses its __dict__ for both globals and locals in "exec cmd in globals, locals". __main__ being a FakeModule instance without any of the IPython prompt namespace available is what makes it fail.

I know the reason that FakeModule exists has something to do with the namespace created for %run executions, but I'm not really sure if there's a way to override its...blankness, when running in interactive mode, without having all hell break loose. I should note also that putting that in a file and %running it works fine, but that's not really a surprise.

I was looking for some low-hanging fruit like the other pull requests I've sent but I think this one is out of my league... hopefully my detective work will let someone pick this up more quickly.

@fperez
Copy link
Member Author

fperez commented Apr 9, 2011

Thanks, David! I've bumped the priority for this one and made FakeModule label so we track all bugs related to this. It's true that this is not low-hanging fruit, but there are ways to help already: writing a set of tests that encapsulate all the known problems in test form would be a huge help. Pickling failures, this profile thing, etc. It would make the job of hunting the actual problem far far easier, right now part of the reason nobody has even tried is precisely that lacking any tests to know what 'fixing' means, we don't even start.

@dwf
Copy link
Contributor

dwf commented Apr 9, 2011

Good point, I will have a look at adding tests for this and the pickle bug (marked as KnownFail, I guess?). I should probably get back to studying for part of today, so probably tomorrow.

@fperez
Copy link
Member Author

fperez commented Apr 9, 2011

On Sat, Apr 9, 2011 at 1:42 PM, dwf
reply@reply.github.com
wrote:

Good point, I will have a look at adding tests for this and the pickle bug (marked as KnownFail, I guess?). I should probably get back to studying for part of today, so probably tomorrow.

Yes, there's a knownfailure decorator recently added by Paul Ivanov.
Thanks a lot!

But please do focus on your exams for now, this can always wait.

@fperez
Copy link
Member Author

fperez commented Nov 30, 2011

Fixed in master:

In [1]: import cProfile

In [2]: import math

In [3]: 

In [3]: x = 25

In [4]: cProfile.run("y = math.sqrt(x)")
         3 function calls in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {math.sqrt}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Closing.

@fperez fperez closed this as completed Nov 30, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants