Repository: spyder-ide/spyder

Issue:
runfile arguments with spaces
_From [oth...@gmail.com](https://code.google.com/u/104424612751893720965/) on 2014-10-16T17:12:04Z_

What steps will reproduce the problem?
1. I have a program that I run that takes a path as a command line option, but the path contains spaces, the Spyder runfile (atleast I think runfile is from spyder as I cannot find it in other python packages) see to not allow for this and my argparser sees the path as multiple args no matter how I escape it

eg:
runfile('temp.py', args='\"./path to files/\"', ...
...error: unrecognized arguments: to files/...
(the same happens if I escape the args or not)

After looking here and on google I cannot find a similar bug report

_Original issue: http://code.google.com/p/spyderlib/issues/detail?id=2010_

runfile arguments with spaces
_From [oth...@gmail.com](https://code.google.com/u/104424612751893720965/) on 2014-10-16T17:12:04Z_

What steps will reproduce the problem?
1. I have a program that I run that takes a path as a command line option, but the path contains spaces, the Spyder runfile (atleast I think runfile is from spyder as I cannot find it in other python packages) see to not allow for this and my argparser sees the path as multiple args no matter how I escape it

eg:
runfile('temp.py', args='\"./path to files/\"', ...
...error: unrecognized arguments: to files/...
(the same happens if I escape the args or not)

After looking here and on google I cannot find a similar bug report

_Original issue: http://code.google.com/p/spyderlib/issues/detail?id=2010_



Discussion:
_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2014-11-18T15:02:26Z_

Thanks for reporting.

**Labels:** Cat-Console MS-v2.4  

I just ran into the same issue. I am attempting to run a script that takes in a list of arguments parsed by argparse.ArgumentParser. Some of the arguments take an additional file path parameter. Using runfile() results in an error for any paths with spaces because each space in the path is considered to represent a separate number. This is regardless of the presence of slashes or quotes.

A little debugging indicates that the argument list in sys.argv shows each space in the path is presented as a separate argument:

``` python
temp.py:
    import sys
    import argparse

    print sys.argv

    parser=argparse.ArgumentParser(description="")
    parser.add_argument('-p','--path',help="This argument accepts a path with spaces")
    args=parser.parse_args()
    print args.path
```

Executing this lists the arguments provided by sys.argv, and the file name has been split blindly:

``` python
In [1]: runfile('temp.py', args='-p "Name with spaces.txt"')
[u'temp.py', '-p', '"Name', 'with', 'spaces.txt"']
usage: temp.py [-h] [-p PATH]
temp.py: error: unrecognized arguments: with spaces.txt"
To exit: use 'exit', 'quit', or Ctrl-D.
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2
```

This is a quick and dirty fix, but I worked around it by editing the spyderlib sitecustomize module to import shlex and use shlex.split(). Modify the sitecustomize.py as below:

``` python
...
import shlex
...
def runfile(filename, args=None, wdir=None, namespace=None):
    ...
    if args is not None:
        for arg in shlex.split(args):
#        for arg in args.split():
            sys.argv.append(arg)
```

Someone will probably have a very good reason not to import shlex inside sitecustomize, but it works for me (after creating a new kernel):

``` python
In [1]: runfile('temp.py', args='-p "Name with spaces.txt"')
[u'temp.py', '-p', 'Name with spaces.txt']
Name with spaces.txt
```

Is there any reason not to import shlex in sitecustomize

No problem with using `shlex`. This is recommended in python's documentation: https://docs.python.org/2/library/subprocess.html#popen-constructor
Would you mind creating a PR to solve this issue ?

_From [ccordoba12](https://code.google.com/u/ccordoba12/) on 2014-11-18T15:02:26Z_

Thanks for reporting.

**Labels:** Cat-Console MS-v2.4  

I just ran into the same issue. I am attempting to run a script that takes in a list of arguments parsed by argparse.ArgumentParser. Some of the arguments take an additional file path parameter. Using runfile() results in an error for any paths with spaces because each space in the path is considered to represent a separate number. This is regardless of the presence of slashes or quotes.

A little debugging indicates that the argument list in sys.argv shows each space in the path is presented as a separate argument:

``` python
temp.py:
    import sys
    import argparse

    print sys.argv

    parser=argparse.ArgumentParser(description="")
    parser.add_argument('-p','--path',help="This argument accepts a path with spaces")
    args=parser.parse_args()
    print args.path
```

Executing this lists the arguments provided by sys.argv, and the file name has been split blindly:

``` python
In [1]: runfile('temp.py', args='-p "Name with spaces.txt"')
[u'temp.py', '-p', '"Name', 'with', 'spaces.txt"']
usage: temp.py [-h] [-p PATH]
temp.py: error: unrecognized arguments: with spaces.txt"
To exit: use 'exit', 'quit', or Ctrl-D.
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2
```

This is a quick and dirty fix, but I worked around it by editing the spyderlib sitecustomize module to import shlex and use shlex.split(). Modify the sitecustomize.py as below:

``` python
...
import shlex
...
def runfile(filename, args=None, wdir=None, namespace=None):
    ...
    if args is not None:
        for arg in shlex.split(args):
#        for arg in args.split():
            sys.argv.append(arg)
```

Someone will probably have a very good reason not to import shlex inside sitecustomize, but it works for me (after creating a new kernel):

``` python
In [1]: runfile('temp.py', args='-p "Name with spaces.txt"')
[u'temp.py', '-p', 'Name with spaces.txt']
Name with spaces.txt
```

Is there any reason not to import shlex in sitecustomize

No problem with using `shlex`. This is recommended in python's documentation: https://docs.python.org/2/library/subprocess.html#popen-constructor
Would you mind creating a PR to solve this issue ?


Resolving patch:
diff --git a/spyderlib/widgets/externalshell/sitecustomize.py b/spyderlib/widgets/externalshell/sitecustomize.py
--- a/spyderlib/widgets/externalshell/sitecustomize.py
+++ b/spyderlib/widgets/externalshell/sitecustomize.py
@@ -16,6 +16,7 @@
 import bdb
 import time
 import traceback
+import shlex
 
 
 #==============================================================================
@@ -775,7 +776,7 @@ def runfile(filename, args=None, wdir=None, namespace=None, post_mortem=False):
     namespace['__file__'] = filename
     sys.argv = [filename]
     if args is not None:
-        for arg in args.split():
+        for arg in shlex.split(args):
             sys.argv.append(arg)
     if wdir is not None:
         try:


Test patch: