Skip to content
This repository

Don't pass unicode sys.argv with %run or `ipython script.py` #898

Closed
wants to merge 1 commit into from

2 participants

Min RK Fernando Perez
Min RK
Owner

only affects Python 2.x

closes #710

Min RK don't inject unicode into sys.argv
Protects %run and `ipython script.py` from getting unicode sys.argv on Python 2

closes #710
625ff81
Fernando Perez
Owner

Looks solid, thanks! Merging with rebase to avoid recursive.

Fernando Perez fperez closed this October 18, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 18, 2011
Min RK don't inject unicode into sys.argv
Protects %run and `ipython script.py` from getting unicode sys.argv on Python 2

closes #710
625ff81
This page is out of date. Refresh to see the latest.
3  IPython/core/magic.py
@@ -1612,6 +1612,9 @@ def magic_run(self, parameter_s ='', runner=None,
1612 1612
         args = [ os.path.expanduser(a) for a in arg_lst[1:] ]
1613 1613
 
1614 1614
         sys.argv = [filename] + args  # put in the proper filename
  1615
+        # protect sys.argv from potential unicode strings on Python 2:
  1616
+        if not py3compat.PY3:
  1617
+            sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
1615 1618
 
1616 1619
         if 'i' in opts:
1617 1620
             # Run in user's interactive namespace
4  IPython/core/shellapp.py
@@ -28,6 +28,7 @@
28 28
 from IPython.config.application import boolean_flag
29 29
 from IPython.config.configurable import Configurable
30 30
 from IPython.config.loader import Config
  31
+from IPython.utils import py3compat
31 32
 from IPython.utils.path import filefind
32 33
 from IPython.utils.traitlets import Unicode, Instance, List, Bool
33 34
 
@@ -207,6 +208,9 @@ def _exec_file(self, fname):
207 208
         # were run from a system shell.
208 209
         save_argv = sys.argv
209 210
         sys.argv = [full_filename] + self.extra_args[1:]
  211
+        # protect sys.argv from potential unicode strings on Python 2:
  212
+        if not py3compat.PY3:
  213
+            sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
210 214
         try:
211 215
             if os.path.isfile(full_filename):
212 216
                 if full_filename.endswith('.ipy'):
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.