Repository navigation
Closed
Labels
3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
Description
| BPO | 16353 |
|---|---|
| Nosy | @gpshead, @ncoghlan, @pitrou, @vstinner, @tiran, @ned-deily, @ezio-melotti, @merwok, @bitdancer, @asvetlov, @cjerdonek, @4kir4, @xdegaye, @Fak3, @serhiy-storchaka, @eryksun, @aixtools, @csabella |
| Files |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = <Date 2019-10-22.23:47:05.614>
created_at = <Date 2012-10-29.01:19:20.218>
labels = ['3.8', 'type-feature', 'library']
title = 'add function to os module for getting path to default shell'
updated_at = <Date 2019-10-22.23:47:05.613>
user = 'https://github.com/cjerdonek'bugs.python.org fields:
activity = <Date 2019-10-22.23:47:05.613>
actor = 'vstinner'
assignee = 'none'
closed = True
closed_date = <Date 2019-10-22.23:47:05.614>
closer = 'vstinner'
components = ['Library (Lib)']
creation = <Date 2012-10-29.01:19:20.218>
creator = 'chris.jerdonek'
dependencies = []
files = ['27849', '27855', '27897', '27898', '36196', '37140', '42953']
hgrepos = []
issue_num = 16353
keywords = ['patch']
message_count = 52.0
messages = ['174093', '174569', '174572', '174574', '174576', '174578', '174580', '174582', '174583', '174586', '174607', '174619', '174621', '174623', '174624', '174628', '174629', '174631', '174646', '174652', '174689', '174690', '174885', '174886', '174929', '174930', '174932', '174933', '174935', '174962', '175002', '175003', '175006', '203160', '224477', '224508', '224509', '224512', '224514', '230711', '230713', '230720', '230754', '230755', '230761', '266135', '270099', '271418', '271439', '335137', '335138', '355182']
nosy_count = 21.0
nosy_names = ['gregory.p.smith', 'ncoghlan', 'pitrou', 'vstinner', 'christian.heimes', 'ned.deily', 'ezio.melotti', 'eric.araujo', 'r.david.murray', 'asvetlov', 'chris.jerdonek', 'neologix', 'akira', 'xdegaye', 'Roman.Evstifeev', 'serhiy.storchaka', 'eryksun', 'lyapun', 'WanderingLogic', 'Michael.Felt', 'cheryl.sabella']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue16353'
versions = ['Python 3.8']Metadata
Metadata
Assignees
Labels
3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement
Activity
cjerdonek commentedon Oct 29, 2012
This issue is to add a function to the os module for getting the path to the default shell (e.g. os.getdefaultshell()).
In bpo-16255, it was reported that on Android, the path to the default shell is "/system/bin/sh" rather than "/bin/sh" which it is on other Unix systems.
Such a function in the os module would implement the necessary detection logic which could then be used, for example, by the subprocess module when invoking Popen() with shell=True (e.g. for bpo-16255).
pitrou commentedon Nov 2, 2012
Is it the "system default shell" or the user's default shell that we want here?
That is, shouldn't we look up
pwd.getpwuid(os.getuid()).pw_shell?(but only when os.getuid() == os.geteuid()?)
cjerdonek commentedon Nov 2, 2012
The system default shell is what I had in mind when filing this issue (i.e. what Popen() uses by default or, in the case of Android, what Popen() should use).
pitrou commentedon Nov 2, 2012
Well, the question then becomes whether Popen() shouldn't use the user's
default shell instead? :)
It probably doesn't make much of a difference in most cases, though.
Also, there's the problem that some special users may have a void shell
entry (or /bin/false or /sbin/nologin), to prevent a human from logging
in as them.
asvetlov commentedon Nov 2, 2012
I guess to return sh if supported, cmd.exe for Windows.
The reason is: other shells can have different calling agreements (I mean rules to process command line).
subprocess intended to use by library writers, so we need solid well known basis.
There are another option — provide two functions: first for shell in terms of subprocess requirements and second will return actual user shell.
I like it.
tjguk commentedon Nov 2, 2012
On 02/11/2012 21:00, Andrew Svetlov wrote:
FWIW the canonical approach on Windows is to return whatever %COMSPEC%
points to.
cjerdonek commentedon Nov 2, 2012
That's a good question, too. :) I was thinking just in terms of supporting the status quo. Maybe two functions would be useful? (as suggested also by Andrew)
I do think the question of changing Popen()'s behavior should be decided independently of this issue though. In other words, even if we change Popen() in the future, I think exposing the system default shell would still be worthwhile.
gpshead commentedon Nov 2, 2012
No, you can't use the users shell from the pwd module. That can be
any crazy program. Not a functional /bin/sh for use in making
commands when shell=True in subprocess which is where this feature
request came from.
*system* default, not the user's. subprocess MUST use /bin/sh or
equivalent, ignoring any per-user setting.
bitdancer commentedon Nov 2, 2012
The "system default shell" (which should always be a /bin/sh workalike, I think) should always be the default. Any other shell should be something that has to be specified explicitly. At least, that's the way most posix programs work, I think. As Chris said, different shells can use different syntax, so if you want to write portable code you want to be able to expect /bin/sh by default.
So at a minimum we want a function for the system default shell. Anything else is a bonus :)
asvetlov commentedon Nov 2, 2012
BTW, according to PEP-11 (http://www.python.org/dev/peps/pep-0011/)
Python 3.4 will remove code for "Windows systems where COMSPEC points to command.com". There are only winner: cmd.exe as well known shell good as default, command.com is gone.
cmd.exe can be assumed as default shell as well as sh for posix platforms.
About subprocess: I like to add something to point str (or pathlib path when accepted) as shell, but it's definitely another issue.
lyapun commentedon Nov 3, 2012
Diff with implementation, test and doc.
asvetlov commentedon Nov 3, 2012
I would to prefer *get_default_shell* as function name (it's a bit shorter).
Also please add ".. versionadded:: 3.4" tag to docs.
lyapun commentedon Nov 3, 2012
Hm... I'm not sure, because someone can imagine that function returns some object or something like this... 'path' indicates that function returns something like string.
Oh, sorry, added.
29 remaining items
4kir4 commentedon Nov 6, 2014
os.defpath is supposed to be ':'+CS_PATH, e.g., look at glibc (C library
used on Linux) sysdeps/posix/spawni.c I don't know whether it is
possible to change CS_PATH without recompiling every statically linked
executable on a system, sysdeps/unix/confstr.h:
Though this issue is about the path to the standard shell sh/cmd.exe.
It is not about os.defpath.
The patch [1] has tests. Have you tried to run them?
The tests *pass* at least on one system ;)
[1] http://bugs.python.org/review/16353/#ps12569
To run the tests, download the patch into your python checkout:
and apply it:
$ patch -p1< os.get_shell_executable.patchto run only test_os.py:
os.get_shell_executable() does not use cwd. Neither the documentation
nor the code in the patch suggest that.
os.defpath is the default search path used by exec*p* and spawn*p*
i.e., if os.defpath is incorrect for these system then os.exec*p* and
os.spawn*p* functions could also be broken.
I expect that Windows, OS X, Linux work as is. If the tests fail on
Solaris, AIX then os.defpath should be tweaked on these systems if it
hasn't been already. Note: os.defpath is a very conservative value: it
should be set at python's installation time at the very
latest. Henceforth it should remain the same.
Please, look at the patch. Neither the documentation nor the code
suggest that os.defpath is used on Windows.
Note I'm the author of http://bugs.python.org/issue16353#msg224514
message that references the POSIX recommendation about
getconf PATHand explicitly mentions os.confstr('CS_PATH').
I don't remember exactly the motivation to use os.defpath instead of
os.confstr('CS_PATH').
A guess: the result is the same (a lone `:` is ignored in the patch) but
os.defpath is easier to modify for uncommon systems where os.confstr
might not be available.
I expect that the tests in the patch will pass on all stable buildbots
[2] successfully without any modifications (with os.defpath as is).
[2] http://buildbot.python.org/all/waterfall?category=3.x.stable
Other systems should probably configure os.defpath appropriately (it
should include the path to the standard shell).
The standard shell should not depend on PATH envvar. The tests show that
os.get_shell_executable() works even with an empty environment. The
motivation is the same as why
getconf PATHexists in the first place.The documentation for os.get_shell_executable() (from the patch):
Return a path to the standard system shell executable --
shprogram on POSIX (default:
'/bin/sh') or%ComSpec%commandprocessor on Windows (default:
%SystemRoot%\system32\cmd.exe).Availability: Unix, Windows
The intent is that os.get_shell_executable() returns the same *shell
path* as used by system(3) on POSIX i.e., (ignoring signals,
etc) os.system(command) could be implemented:
(presumably subprocess can use get_shell_executable() internally instead
of /bin/sh but it is another issue)
WanderingLogic commentedon Nov 6, 2014
In msg174930 Christian Heimes (christian.heimes) wrote:
In msg230713 Ned Deily(ned.deily) wrote:
These two messages have convinced me that the correct approach is to kick the can down the road. I will file a new issue and patch to fix os.defpath (by initializing os.defpath by calling confstr("CS_PATH")). Then I will file a new issue and patch that will define a reasonable os.confstr() for platforms where HAVE_CONFSTR is not defined.
WanderingLogic commentedon Nov 6, 2014
In msg230720 Akira Li (akira) wrote:
I understand this issue is about the path to the standard shell.
My argument is that os.defpath is the wrong tool for finding that
path, and os.confstr('CS_PATH') is the correct tool, as you originally
suggested in msg224514.
I ran the tests (and the entire Python test suite) before I wrote my
first message. But you didn't test what happens when there is a bogus
'sh' somewhere along the path. You also have a bug in your path
searching loop that interprets an empty element on the path as "/".
Here's the current failure mode:
Go to root on your Posix system. With "su" or "sudo" create an
executable file named sh. (for example "cd /; sudo touch sh; sudo
chmod +x sh"). Then go back to your python interpreter (with the
patch applied) and run "os.get_shell_executable()". The returned
result will be '/sh'.
The loop _should_ be treating empty path elements as current working
directory. (In the glibc sysdeps/posix/spawni.c file you pointed to
look around line 281, by the comment that says "/* Two adjacent
colons, or a colon at the beginning or the end of 'PATH' means to
search the current directory.*/")
But if you fix the loop to iterate over the path correctly (including
'cwd') then you will find that putting an executable named 'sh' in the
current working directory will make os.get_shell_executable() return
the "sh" in the current working directory (because os.defpath says it
should).
Note also, that glibc's spawni() uses two different "default paths".
One is for if the user didn't specify their PATH variable (that's the
one where they use ":/bin:/usr/bin") and a completely different (built
in path is used in the spawn*p* version) if it turns out that the file
is a script that needs to run under the system sh. (On line 290 they
call maybe_script_execute(), which calls script_execute(), which uses
_PATH_BSHELL.)
I'll look into it.
The motivation was in msg224514. You wrote:
But os.defpath doesn't work on Android either (because it is hardcoded
to ':/bin:/usr/bin').
4kir4 commentedon Nov 6, 2014
I'll repeat os.defpath definition that I've cited in the same message
msg224514:
The default search path used by exec*p* and spawn*p* if the
environment doesn’t have a 'PATH' key. Also available via os.path.
os.defpath should work (contain the path to the standard shell)
everywhere where os.confstr("CS_PATH") works.
And it might be easier to customize e.g., on systems where there is
no os.confstr or where changing CS_PATH involves recompiling crucial
system processes.
If these two statements are not true then os.defpath could be dropped.
Comment in the patch explicitly says that '' is interpreted as '/'. The
intent is to exclude the current working directory, thinking that /sh
executable can create only root. And root can do anything to the machine
anyway.
I agree. It is a misfeature. I've uploaded a new patch that excludes ''
completely. The previous code tried to be too cute.
yes. Empty path element stands for the current working directory e.g.,
run ./python on my system.
The current working directory is intentionally excluded (in the original
and in the current patches) otherwise os.get_exec_path(env={}) would be
used.
glibc hardcodes the path (like subprocess module does currently):
os.get_shell_executable() is an enhancement that allows
(Unix) systems to configure the path via os.defpath.
As I've mentioned in msg224514 before posting my original patch:
i.e., yes. Default os.defpath doesn't work there and it should be
configured on Android to include the path to the standard shell.
xdegaye commentedon May 23, 2016
Following Serhiy suggestion at msg224477, this tentative patch adds the is_android attribute to the posix module and the default_shell attribute to the os module. No documentation for the moment. No test case is needed I think, as the changes in bpo-16255 will test any solution found here.
An alternative to posix.is_android is sys.platform set to 'linux-android' on Android. These are the locations in the standard library that do not test for "if sys.platform.startswith('linux')":
Lib/distutils/tests/test_dist.py|386 col 16| if sys.platform in ('linux', 'darwin'):
Lib/test/test_logging.py|527 col 12| if sys.platform in ('linux', 'darwin'):
Lib/test/test_resource.py|135 col 26| @unittest.skipUnless(sys.platform == 'linux', 'test requires Linux')
Lib/test/test_socket.py|898 col 16| or sys.platform in ('linux', 'darwin')):
Lib/test/test_socket.py|2256 col 23| @skipWithClientIf(sys.platform not in {"linux"},
Lib/test/test_socket.py|4508 col 22| @unittest.skipUnless(sys.platform == 'linux', 'Linux specific test')
Lib/test/test_sysconfig.py|388 col 26| @unittest.skipUnless(sys.platform == 'linux', 'Linux-specific test')
xdegaye commentedon Jul 10, 2016
bpo-27472 adds test.support.unix_shell as the path to the default shell.
aixtools commentedon Jul 26, 2016
An interesting read, but I am lost in what the goal is.
e.g., on AIX, which I know well, the system default is /bin/ksh (aka /usr/bin/ksh). However, /bin/sh (/usr/bin/sh) is available as well.
My expectation is that on Linux the default shell is /bin/bash, and like AIX /bin/sh is also available.
/bin/sh, /bin/ksh, /bin/bash are all (potentially) default shells. However, something every posix system is "sh" aka borne shell.
Aside: windows is different, and I expect has a different syntax.
So, if the goal is to GET the pathname of the so-called "default" shell -again, interesting - BUT - what does that buy me?
For Popen or now subprocess I would want a consistent shell syntax, i.e., borne shell on posix, cmd.exe on windows, and whatever is correct on platforms that do not fit in these two.
Hence, on posix platforms where /bin/sh does not exist (hard)code the correct path for that platform.
FYI: CS_PATH does not exist on AIX
IMHO: (as others have stated) having . in PATH is bad for security, being dependent on PATH opens other security concerns as well.
IMHO2: KISS principle. After several years there is still no consenus, so make it simple - the popen and subprocess shell is one of /bin/sh, cmd.exe, or some other "hard-coded" shell.
(Although I thought that subprocess called "program" directly. Python is still new, so if not applicable for subprocess - please ignore
rereading, direct call would be when shell=False I expect.
Anyway, my two cents.
xdegaye commentedon Jul 27, 2016
I agree, the last patch that is being reviewed in bpo-16255 attempts to do that.
csabella commentedon Feb 9, 2019
3.7 added
sys.getandroidapilevel()under bpo-28740. As Xavier mentioned in msg271439, that was used for subprocess.Popen in bpo-16255 and also merged as part of 3.7.I'm not sure if there is still interest in adding a
default_shellto the os module usingsys.androidapilevel()and the last batch that Xavier uploaded in 2016.pitrou commentedon Feb 9, 2019
I think the functionality of getting either the system-wide default shell or the current user's default shell can be useful regardless of whether the specific subprocess issue was fixed.
vstinner commentedon Oct 22, 2019
This issue has been fixed. The subprocess module now uses '/system/bin/sh' by default on Android thanks to Xavier de Gaye!