Author tobytobkin
Recipients bar.harel, bobjalex, eryksun, paul.moore, r.david.murray, steve.dower, tim.golden, tobytobkin, zach.ware
Date 2015-12-14.08:29:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1450081768.45.0.367815105968.issue24505@psf.upfronthosting.co.za>
In-reply-to
Content
Hopefully this isn't too much of an amateur question, but I ran into a semantics issue: should which be imitating the semantics of the Windows shell or of CreateProcess[3]? The current implementation of shutil.which implies Windows shell, but Python uses CreateProcess in subprocess for executing commands, not cmd.exe.

In order to correctly emulate the behavior of the Windows command search sequence[1] (e.g. for cmd.exe or Powershell), one needs to check whether or not a given command matches one of the internal Windows shell commands. For instance, if we have an executable at C:\xyz\chdir.exe, the following outputs should be observed from which if our current directory is C:\xyz:

>>> which('chdir')
(none)

>>> which('.\\chdir')
'C:\\xyz\\chdir.exe'

On the other hand, CreateProcess[3] would work this way:

CreateProcess(NULL, "chdir", ...) --> executes C:\xyz\chdir.exe
CreateProcess(NULL, "chdir.exe", ...) --> executes C:\xyz\chdir.exe

There are other semantic differences as well. For example, CreateProcess will not do path extension of e.g. "abc" to "abc.bat", but Powershell and cmd will.

Which semantics do I follow? Powershell/cmd or CreateProcess?

[1] Subsection "Command Search Sequence" of https://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120
[2] Subsection "Internal and External Commands" of https://technet.microsoft.com/en-us/library/cc723564.aspx#XSLTsection127121120120
[3] https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
History
Date User Action Args
2015-12-14 08:29:28tobytobkinsetrecipients: + tobytobkin, paul.moore, tim.golden, r.david.murray, zach.ware, eryksun, steve.dower, bobjalex, bar.harel
2015-12-14 08:29:28tobytobkinsetmessageid: <1450081768.45.0.367815105968.issue24505@psf.upfronthosting.co.za>
2015-12-14 08:29:28tobytobkinlinkissue24505 messages
2015-12-14 08:29:27tobytobkincreate