Interactively defined functions can't access user namespace #387

Closed
fperez opened this Issue Apr 15, 2011 · 7 comments

Comments

Projects
None yet
3 participants
Owner

fperez commented Apr 15, 2011

Simple example:

In [1]: a = 10

In [2]: def f(x):
   ...:     return x+a
   ...: 

In [3]: f(1)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/etc/cups/ in ()
----> 1 f(1)

/etc/cups/ in f(x)
      1 def f(x):
----> 2     return x+a

NameError: global name 'a' is not defined

This used to work in 0.10.x, I just tested it. I haven't had time to bisect it and have to run now, but this one is absolutely a blocker, unfortunately.

Anyone who can take a stab at it, would be awesome. The first thing to do is to add a failing test for this, I can't believe we didn't have any already...

Owner

minrk commented Apr 15, 2011

Are you working in Thomas' pickle fixing branch?

Because this bug does not exist in master for me (just tested Linux, Windows, and OSX).

Owner

fperez commented Apr 15, 2011

On Fri, Apr 15, 2011 at 3:59 PM, minrk
reply@reply.github.com
wrote:

Are you working in Thomas' pickle fixing branch?

Oh, wow, yes! But by accident, I had just finished testing his branch
and left my checkout there, and started doing something else without
realizing that.

Because this bug does not exist in master for me (just tested Linux, Windows, and OSX).

OK, thanks. That's great to hear. @takluyver, that means that a test
of this kind should be added to your pickle branch, so that a) it gets
fixed there b) we have a safety in place for this in the future.

Owner

minrk commented Apr 15, 2011

I just checked, and this is definitely not in master (yet), it's introduced by Thomas' pickle fixing in #384.

Owner

takluyver commented Apr 16, 2011

Ouch, yes. I don't know quite where that's coming from, but I'll look into it.

takluyver was assigned Apr 16, 2011

Owner

takluyver commented Apr 16, 2011

I think that in master, user_ns and user_global_ns point to the same thing when you start the shell. I've broken that link in linking user_ns to user_ns_mod.__dict__. It's going to be tricky to test, though, because in the test environment, user_ns and user_global_ns are already separate objects. In fact, I'm not quite sure why we need two user namespaces - can anyone enlighten me?

Owner

fperez commented Apr 16, 2011

Sorry, I'll be offline all day... The reason we have two is to be
able to map to the locals and globals in the exec statement:

exec code in glob, loc

Without both, we can't provide the same semantics, which is important
in cases like embedded ipython shells.

f

Owner

takluyver commented Apr 20, 2011

This is fixed in the branch it affects. Closing for now, reopen if you can replicate it.

takluyver closed this Apr 20, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment