Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Bug fix for approval #1452

Closed
wants to merge 4 commits into from

4 participants

Daley Chetwynd Min RK Thomas Kluyver Fernando Perez
Daley Chetwynd

This is a fix for a simple bug that was done at the Python Sheffield user group. The bug was that running a file in debug mode in Python 3 caused a NameError to be thrown for execfile.

IPython/core/magic.py
@@ -1740,7 +1740,9 @@ def magic_run(self, parameter_s ='', runner=None,
main_mod = self.shell.new_main_mod()
prog_ns = main_mod.__dict__
prog_ns['__name__'] = name
-
+
+ prog_ns['execfile'] = 'py3compat.execfile'
Thomas Kluyver Owner

This needs to be the function itself, not the name (i.e. lose the quotation marks)

Thomas Kluyver Owner

You don't need to submit a new PR - if you commit the change on the same branch and push it, the pull request will automatically get updated ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Thomas Kluyver takluyver commented on the diff
IPython/core/magic.py
@@ -1871,6 +1873,9 @@ def magic_run(self, parameter_s ='', runner=None,
# exit.
self.shell.user_ns['__builtins__'] = builtin_mod
+ if prog_ns.has_key('execfile'):
Thomas Kluyver Owner

Oh, and it's generally good practice now to use 'execfile' in prog_ns rather than calling has_key.

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

adding a note that the bug in question that this fixes is #1421.

Once the has_key -> in change is made, I think this can go in.

Thomas Kluyver
Owner

For the sake of completeness, I should note that if you do %run -id (i.e. run with debug in the interactive namespace), and you've defined execfile within IPython, this will remove your execfile from the namespace. I don't think that's a major concern. It shouldn't affect the standard execfile in Python 2, since that's in the builtin namespace.

Thomas Kluyver
Owner

Sorry, I was a bit too hasty in proposing a fix. This doesn't actually work, because our execfile expects a second argument for the namespace. Grabbing the namespace of the calling scope is a rather horrible hack.

I've worked out what we actually need to do for this. We should build a tiny new namespace with just execfile and a reference to prog_ns:

ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns}
deb.run('execfile("%s", prog_ns)' % filename, ns)

That also avoids all the potential problems from modifying the user namespace. @dchetwynd , let us know if you want to have a go at this (it would be best as a new pull request).

Fernando Perez
Owner

@takluyver, it seems from your last comment that you see this particular PR as best being closed, and a new one started for the cleaner fix, am I right? I'm just trying to go through our entire backlog so we can start seeing 0.13 take shape... Let us know how we should proceed on this one.

Thomas Kluyver
Owner

Yep, I've made PR #1599, so I'll close this one now. Thanks @dchetwynd for getting the ball rolling, and sorry the fix I suggested didn't quite work.

Thomas Kluyver takluyver closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 28, 2012
  1. Daley Chetwynd
  2. Daley Chetwynd

    Merge pull request #1 from dchetwynd/daleyandrew

    dchetwynd authored
    Fixed bug with running a file in debug mode in Python 3
Commits on Feb 29, 2012
  1. Daley Chetwynd
  2. Daley Chetwynd

    Merge pull request #2 from dchetwynd/daleyandrew

    dchetwynd authored
    Fixed code to use function object instead of literal string function nam...
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 1 deletion.
  1. +6 −1 IPython/core/magic.py
7 IPython/core/magic.py
View
@@ -1740,7 +1740,9 @@ def magic_run(self, parameter_s ='', runner=None,
main_mod = self.shell.new_main_mod()
prog_ns = main_mod.__dict__
prog_ns['__name__'] = name
-
+
+ prog_ns['execfile'] = py3compat.execfile
+
# Since '%run foo' emulates 'python foo.py' at the cmd line, we must
# set the __file__ global in the script's namespace
prog_ns['__file__'] = filename
@@ -1871,6 +1873,9 @@ def magic_run(self, parameter_s ='', runner=None,
# exit.
self.shell.user_ns['__builtins__'] = builtin_mod
+ if prog_ns.has_key('execfile'):
Thomas Kluyver Owner

Oh, and it's generally good practice now to use 'execfile' in prog_ns rather than calling has_key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ del prog_ns['execfile']
+
# Ensure key global structures are restored
sys.argv = save_argv
if restore_main:
Something went wrong with that request. Please try again.