Skip to content

Commit 2ccb576

Browse files
authored
Merge pull request #248 from bnavigator/subcommand-argv0
set proper sys.argv[0] for subcommand
2 parents 1778a44 + e84f3b1 commit 2ccb576

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

jupyter_core/command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def main():
303303
sys.exit(e)
304304

305305
try:
306-
_execvp(command, sys.argv[1:])
306+
_execvp(command, [command] + sys.argv[2:])
307307
except OSError as e:
308308
sys.exit("Error executing Jupyter command %r: %s" % (subcommand, e))
309309

jupyter_core/tests/test_command.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,27 @@ def test_path_priority(tmpdir):
201201
env[str('PATHEXT')] = '.EXE'
202202
out = check_output([sys.executable, str(jupyter), 'witness'], env=env)
203203
assert b'WITNESS A' in out
204+
205+
def test_argv0(tmpdir):
206+
a = tmpdir.mkdir("a")
207+
jupyter = a.join('jupyter')
208+
jupyter.write(
209+
'from jupyter_core import command; command.main()'
210+
)
211+
jupyter.chmod(0o700)
212+
witness_a = a.join('jupyter-witness')
213+
witness_a_src = f'''#!{sys.executable}
214+
import sys
215+
print(sys.argv[0])
216+
'''
217+
write_executable(witness_a, witness_a_src)
218+
219+
env = {}
220+
if 'SYSTEMROOT' in os.environ: # Windows http://bugs.python.org/issue20614
221+
env[str('SYSTEMROOT')] = os.environ['SYSTEMROOT']
222+
if sys.platform == 'win32':
223+
env[str('PATHEXT')] = '.EXE'
224+
out = check_output([sys.executable, str(jupyter), 'witness'], env=env)
225+
226+
# Make sure the first argv is the full path to the executing script
227+
assert f'{jupyter}-witness'.encode() in out

0 commit comments

Comments
 (0)