Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'subject:Windows': 0.02; 'scripts': 0.03; 'value,': 0.04; 'root': 0.05; '(python': 0.07; 'context': 0.07; 'correct.': 0.07; 'explicit': 0.07; 'linux,': 0.07; 'python3': 0.07; 'subject:PEP': 0.07; 'subject:support': 0.07; '__init__': 0.09; 'bash': 0.09; 'credentials,': 0.09; 'exec': 0.09; 'executable': 0.09; 'feature,': 0.09; 'here?': 0.09; 'main()': 0.09; 'oh,': 0.09; 'stating': 0.09; 'subject:using': 0.09; 'windows,': 0.09; 'api': 0.11; 'cc:addr:python-list': 0.11; 'python': 0.11; 'bug': 0.12; 'assume': 0.14; 'windows': 0.15; '"run': 0.16; '"running': 0.16; 'behaviour.': 0.16; 'command,': 0.16; 'errread,': 0.16; 'errwrite)': 0.16; 'expects': 0.16; 'failed!': 0.16; 'fork': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'governed': 0.16; 'grep': 0.16; 'guessing': 0.16; 'ha!': 0.16; 'os;': 0.16; 'ought': 0.16; 'reasonable.': 0.16; 'relates': 0.16; 'relevant,': 0.16; 'scripts.': 0.16; 'subject: \n ': 0.16; 'subject:API': 0.16; 'underlying': 0.16; 'windows?': 0.16; 'world!': 0.16; 'worst': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'variable': 0.18; 'trying': 0.19; 'possible,': 0.19; "skip:' 30": 0.19; 'thu,': 0.19; 'starts': 0.20; '>>>': 0.22; 'putting': 0.22; 'shell': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; "aren't": 0.24; 'instance,': 0.24; 'replace': 0.24; 'specify': 0.24; 'subject: .': 0.24; 'earlier': 0.24; 'versions': 0.24; '(or': 0.24; 'environment': 0.24; 'cc:2**0': 0.24; 'script': 0.25; 'equivalent': 0.26; 'order.': 0.26; 'possibly': 0.26; 'skip:" 40': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'function': 0.29; 'chris': 0.29; '0);': 0.29; 'am,': 0.29; 'generally': 0.29; 'possibility': 0.29; 'raise': 0.29; 'thus': 0.29; 'unix': 0.29; "doesn't": 0.30; 'forgot': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; '"",': 0.31; "d'aprano": 0.31; 'directory,': 0.31; 'steven': 0.31; 'file': 0.32; 'know.': 0.32; 'stuff': 0.32; "we're": 0.32; 'run': 0.32; 'linux': 0.33; 'running': 0.33; '(most': 0.33; 'alone': 0.33; 'becomes': 0.33; 'entirely': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; "i'd": 0.34; 'subject:from': 0.34; 'problem': 0.35; "can't": 0.35; 'problem.': 0.35; 'skip:s 30': 0.35; 'needing': 0.65; 'talking': 0.65; 'between': 0.67; 'advantages': 0.68; 'risk': 0.72; 'apart': 0.72; 'family': 0.73; '2015': 0.84; 'commands.': 0.84; 'confusing': 0.84; 'difference.': 0.84; 'etc,': 0.84; 'execution.': 0.84; 'forks': 0.84; 'fortunately': 0.84; 'hmm...': 0.84; 'limits,': 0.84; 'pike': 0.84; 'subject:skip:F 10': 0.84; 'yours': 0.88; 'angel': 0.91; 'mistake': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=jLqnJCSuEztfAxhXmFD1vQjEmlU67Mc3nq/rYnqCEvw=; b=CgsDHKga7fqmhyXqlRsni5QkBgPmJ7d1+gtGDFkiPceTfLUwLGkAFgnEShDhp7kzQc M8+fJu9XNZhq5SUJM77dCML6xztwqAsC4LE/TbVkj59iwGMOBbJQ5qU3pkpeoLnehTkM 69O/Mbk2CyaqcJdCxn+puVqm7Z7plz7KzQ7tObZuTyuMEziuuCZJ+B4lrhzCNuwHqruP YUk3UP5ux0+AQR8gtAXYdmwmJ8r2djIH8MHQwpkvHJR9swX4icAyY7SVk+L/zI4NsSiU SQS4hvJKvLABhfiZL8/FUkwIT6OXoDl4sAQSH/o4/8O84iSHfe72/JIyWU41oP6DZusl aNJg== MIME-Version: 1.0 X-Received: by 10.107.160.202 with SMTP id j193mr2359494ioe.43.1430971027192; Wed, 06 May 2015 20:57:07 -0700 (PDT) In-Reply-To: <554adcf8$0$11103$c3e8da3@news.astraweb.com> References: <554AB8A5.708@davea.name> <554adcf8$0$11103$c3e8da3@news.astraweb.com> Date: Thu, 7 May 2015 13:57:07 +1000 Subject: Re: PEP idea: On Windows, subprocess should implicitly support .bat and .cmd scripts by using FindExecutable from win32 API From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 147 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430971035 news.xs4all.nl 2954 [2001:888:2000:d::a6]:53940 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90088 On Thu, May 7, 2015 at 1:33 PM, Steven D'Aprano wrote: > On Thursday 07 May 2015 12:19, Chris Angelico wrote: > >> On Thu, May 7, 2015 at 10:58 AM, Dave Angel wrote: >>> There's nothing Windows-specific about that behaviour. In Linux, there >>> are >>> bash commands that can only be run by using shell=True. Fortunately >>> Popen didn't make the mistake of pretending it's a shell. >> >> But bash commands aren't the same as shell scripts. For instance, if >> you want to enumerate bash aliases, you can't exec() to the 'alias' >> command, because there isn't one. But shell scripts *can* be exec'd: > > Um, are we still talking about Python here? exec("alias") fails with > NameError on all the versions of Python I know. *semi-wink* > > I'm guessing you're taking about some other exec, it might be a good idea > that on a Python mailing list you don't assume that we're all going to > understand the context :-) Fair point. I'm talking about the underlying exec* family of functions, which are used on Unix-like systems to do normal process execution. More on that later. >> $ grep $ exec_demo.* >> exec_demo.c:#include >> exec_demo.c:#include >> exec_demo.c:int main() >> exec_demo.c:{ >> exec_demo.c: printf("This part is coming from C code.\n"); >> exec_demo.c: int err=execl("./exec_demo.sh", 0); >> exec_demo.c: printf("exec() failed! %d\n",err); >> exec_demo.c:} >> exec_demo.sh:#!/bin/sh >> exec_demo.sh:echo This part ran from the shell. >> exec_demo.sh:echo Hello, world! >> $ ./a.out >> This part is coming from C code. >> This part ran from the shell. >> Hello, world! > >> $ pike -e 'Process.exec("./exec_demo.sh");' >> This part ran from the shell. >> Hello, world! > > Okay, so C code can call the shell. So can Pike. > > >> $ python -c 'import subprocess; subprocess.call(["./exec_demo.sh"])' >> This part ran from the shell. >> Hello, world! > > And so can Python. I'm not entirely sure what point you are trying to make > here, or how it relates to the OP's problem that when he calls > > subprocess.Popen(['foo']) > > he expects it to run any of foo.exe, foo.cmd, foo.bat (and possibly any > other number of executable files). Are you agreeing with him or disagreeing? I'm stating that this works on Unix with shell=False. Consequently, the OP's request that it work thusly on Windows ought to be possible, and ought to be reasonable. > Apart from any other number of problems, surely having "foo" alone run > foo.exe, foo.bat etc. is at best confusing and at worst a security risk? > What if you have *both* foo.exe and foo.bat in the same directory? There's a specific search order. Back in the days of DOS, it was simply "com, then exe, then bat", but on modern Windowses, I think it's governed by an environment variable (similarly to PATH for the directories to search in). It's identical security risk to the possibility of putting something earlier in $PATH; if you insist on running a specific executable, you put the entire path, and then there's no problem. (Actually, I'd consider this to be a feature, not a bug - it's the equivalent of shadowing built-ins in a Python module. When you want it, it's really useful.) >> (Python doesn't seem to have any way to 'exec', but a subprocess comes >> to the same thing.) > > According to `man exec` on my Linux system, I don't think that is correct. > The exec* family of functions "replaces the current process image with a new > process image", they don't run in a subprocess. > > I think the Python equivalent of Unix exec* commands are the various > os.exec* functions. Oh, I forgot about os.exec. That's a better equivalent: $ python3 -c 'import os; os.execl("./exec_demo.sh","exec_demo.sh")' This part ran from the shell. Hello, world! When you call subprocess.* to run something, what happens is generally that Python forks and exec's to the new process. The parent of the fork keeps running (or maybe waits on the child immediately), the child uses one of the exec family of functions to replace itself with the new process. This is different from Windows, where there's a standard API function for "start this program in a new process". Advantages of the fork/exec model include that you can do stuff in between the two steps - for instance, you fork, then you change your user credentials, root directory, current directory, nice value, usage limits, etc, etc, etc, etc, prior to exec'ing. Unix systems don't need a way to say "run this process with this root directory", because it can all be built up from primitives. So yes, in terms of the ability to locate other executables, it makes no difference whether you fork first or not - the exec call is the same. > Hmmm. I'm not sure if this is relevant, or if I'm going off on a tangent, > but if I write a short bash script and set the execute permission: > > steve@runes:~$ chmod u+x test.sh > steve@runes:~$ cat test.sh > echo "Running shell script" > > subprocess.call fails unless I set shell=True: > > py> p = subprocess.Popen('./test.sh', shell=True) > py> Running shell script > > py> p = subprocess.Popen('./test.sh', shell=False) > Traceback (most recent call last): > File "", line 1, in > File "/usr/local/lib/python2.7/subprocess.py", line 711, in __init__ > errread, errwrite) > File "/usr/local/lib/python2.7/subprocess.py", line 1308, in > _execute_child > raise child_exception > OSError: [Errno 8] Exec format error > > > How is this any different from needing to specify shell=True for .bat and > .cmd files under Windows? This is not a rhetorical question, I actually want > to know. Hmm... hm... Ha! Found the difference. I had an explicit shebang on my script; yours just starts out with shell commands. That means that your shell script wasn't truly executable, and thus requires a shell to execute it. Try adding "#!/bin/sh" to the top and rerun that - at that point, it becomes kernel-executable instead of just shell-executable. ChrisA