You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
platform.py: _syscmd_file() can't handle target path with space or special shell character #47969
If you install python in a location which has a space
or shell character in the path then platform.platform()
will generate an error and/or fail to get all platform
information.
For example
$ pwd
/disk0/tmp/foobar(2)/python2.4/bin
$ ./python
Python 2.4.4 (#8, Apr 11 2008, 11:42:39) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> print platform.platform()
sh: syntax error at line 1: `(' unexpected
SunOS-5.10-sun4u-sparc-32bit
Note the error from 'sh' was well as the fact that "ELF"
is missing from the platform string. If you are in a path
with a space then it silently fails to identify "ELF".
The problem is in platform.py: _syscmd_file(target,default='')
in particular this line:
f=os.popen('file %s 2> /dev/null'%target)
This should be:
f=os.popen('file "%s" 2> /dev/null'%target)
Note the double quotes to protect the path from the shell.
I've examined the 2.5, 2.6 and 3.0 source and they all
have this same problem.
Is adding the double-quotes enough to solve the problem ?
ocean-city commented on Sep 2, 2008
ocean-citymannequin
Mannequin
More actions
Can you try this patch?
used "file -b" option to eliminate file path. Otherwide, re.split
won't work because file path will contain space like this. (I hope -b
option can be used anywhere "file" command exists)
a b/python.exe: MS-DOS executable PE for MS Windows (console) Intel
80386 32-bit
used subprocess module instead of popen. subprocess is useful when
argument contains space or quote like this issue.
ocean-city commented on Sep 2, 2008
ocean-citymannequin
Mannequin
More actions
[off topic]
I used cygwin to create this patch, but platform.architecture()[1]
becomes empty string because sys.executable in cygwin points to
directory. (at least before installing)
Python 2.6b3+ (trunk:66142M, Sep 2 2008, 17:09:46)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'/home/WhiteRabbit/python-dev/trunk/python'
I workarounded this by inserting "target = './python.exe'" in
_syscmd_file, but sorry if this patch won't work.
Using subprocess is only possible conditionally, ie. if available.
Please note that the module is intended to be usable with multiple
Python versions and must at least support Python 2.1.
We could consider an updated patch for 2.7 and 3.1, but not for 2.6/3.0,
since it needs more testing.
I think for 2.6/3.0, adding the extra quotes is enough to get things
working.
Please open a new patch ticket for your patch and update it to work with
popen if subprocess is not available. The patch should also not remove
the lines:
On 2008-09-02 12:55, Hirokazu Yamamoto wrote:
> Hirokazu Yamamoto <[email protected]> added the comment:
>
> Well, if python was installed into directory named
> "foo ELF boo", python will say wrong architecture.
>
>>>> import platform
>>>> platform.architecture()
> ('32bit', 'ELF')
>
> Is it not good to use "file -b" option?
Well, I'd say that install path is rather unlikely to occur in real
life ;-)
I'm not sure how wide-spread support for the -b option is, so we'd
have to check during the next release cycle.
changed the title [-]platform.py: _syscmd_file() can't handle target path with space or special shell character[/-][+]platform.py: _syscmd_file() can't handle target path with space or special shell character[/+]on Sep 2, 2008
Activity
jfdp commentedon Aug 29, 2008
If you install python in a location which has a space
or shell character in the path then platform.platform()
will generate an error and/or fail to get all platform
information.
For example
Note the error from 'sh' was well as the fact that "ELF"
is missing from the platform string. If you are in a path
with a space then it silently fails to identify "ELF".
The problem is in platform.py: _syscmd_file(target,default='')
in particular this line:
This should be:
Note the double quotes to protect the path from the shell.
I've examined the 2.5, 2.6 and 3.0 source and they all
have this same problem.
malemburg commentedon Sep 1, 2008
Is adding the double-quotes enough to solve the problem ?
ocean-city commentedon Sep 2, 2008
Can you try this patch?
won't work because file path will contain space like this. (I hope -b
option can be used anywhere "file" command exists)
a b/python.exe: MS-DOS executable PE for MS Windows (console) Intel
80386 32-bit
argument contains space or quote like this issue.
ocean-city commentedon Sep 2, 2008
[off topic]
I used cygwin to create this patch, but platform.architecture()[1]
becomes empty string because sys.executable in cygwin points to
directory. (at least before installing)
Python 2.6b3+ (trunk:66142M, Sep 2 2008, 17:09:46) [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.executable '/home/WhiteRabbit/python-dev/trunk/python'I workarounded this by inserting "target = './python.exe'" in
_syscmd_file, but sorry if this patch won't work.
malemburg commentedon Sep 2, 2008
Using subprocess is only possible conditionally, ie. if available.
Please note that the module is intended to be usable with multiple
Python versions and must at least support Python 2.1.
We could consider an updated patch for 2.7 and 3.1, but not for 2.6/3.0,
since it needs more testing.
I think for 2.6/3.0, adding the extra quotes is enough to get things
working.
Please open a new patch ticket for your patch and update it to work with
popen if subprocess is not available. The patch should also not remove
the lines:
from _syscmd_file().
I'll checkin the extra quotes.
malemburg commentedon Sep 2, 2008
Updated the Python versions.
ocean-city commentedon Sep 2, 2008
Well, if python was installed into directory named
"foo ELF boo", python will say wrong architecture.
>>> import platform >>> platform.architecture() ('32bit', 'ELF')Is it not good to use "file -b" option?
malemburg commentedon Sep 2, 2008
Well, I'd say that install path is rather unlikely to occur in real
life ;-)
I'm not sure how wide-spread support for the -b option is, so we'd
have to check during the next release cycle.
[-]platform.py: _syscmd_file() can't handle target path with space or special shell character[/-][+]platform.py: _syscmd_file() can't handle target path with space or special shell character[/+]malemburg commentedon Sep 2, 2008
Checked in as r66145 and r66146.
ocean-city commentedon Sep 2, 2008
OK, I understand. And I must apologizes about previous
my patch.
should not be deleted. It was should be changed to
Thank you.
jfdp commentedon Sep 2, 2008
To respond to a couple questions:
Adding the double quotes fixed the issue I had -- but I did very
limited testing.
The "-b" option is not support by 'file' on Solaris.