From d349fce10c6be61c51a0bc0d2ca2ccbb68cfa060 Mon Sep 17 00:00:00 2001 From: Ben Greiner Date: Sat, 30 Oct 2021 13:16:52 +0200 Subject: [PATCH 1/3] set proper sys.argv[0] for subcommand --- jupyter_core/command.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jupyter_core/command.py b/jupyter_core/command.py index 2b59eff..29ca71c 100644 --- a/jupyter_core/command.py +++ b/jupyter_core/command.py @@ -303,7 +303,7 @@ def main(): sys.exit(e) try: - _execvp(command, sys.argv[1:]) + _execvp(command, [command] + sys.argv[2:]) except OSError as e: sys.exit("Error executing Jupyter command %r: %s" % (subcommand, e)) From ed57a79f19275c807d34d39620883c16312bbd22 Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Wed, 3 Nov 2021 21:18:16 -0700 Subject: [PATCH 2/3] Add test for argv0 being the absolute path to the command. --- jupyter_core/tests/test_command.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/jupyter_core/tests/test_command.py b/jupyter_core/tests/test_command.py index 4aec65c..6262fa0 100644 --- a/jupyter_core/tests/test_command.py +++ b/jupyter_core/tests/test_command.py @@ -201,3 +201,27 @@ def test_path_priority(tmpdir): env[str('PATHEXT')] = '.EXE' out = check_output([sys.executable, str(jupyter), 'witness'], env=env) assert b'WITNESS A' in out + +def test_argv0(tmpdir): + a = tmpdir.mkdir("a") + jupyter = a.join('jupyter') + jupyter.write( + 'from jupyter_core import command; command.main()' + ) + jupyter.chmod(0o700) + witness_a = a.join('jupyter-witness') + witness_a_src = f'''#!{sys.executable} +import sys +print(sys.argv[0]) +''' + write_executable(witness_a, witness_a_src) + + env = {} + if 'SYSTEMROOT' in os.environ: # Windows http://bugs.python.org/issue20614 + env[str('SYSTEMROOT')] = os.environ['SYSTEMROOT'] + if sys.platform == 'win32': + env[str('PATHEXT')] = '.EXE' + out = check_output([sys.executable, str(jupyter), 'witness'], env=env) + + # Make sure the first argv is the full path to the executing script + assert f'{jupyter}-witness'.encode() in out \ No newline at end of file From e84f3b16d66b31fee31e647edccf02bc5621a935 Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Wed, 3 Nov 2021 21:20:27 -0700 Subject: [PATCH 3/3] Add line ending to last line of file --- jupyter_core/tests/test_command.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jupyter_core/tests/test_command.py b/jupyter_core/tests/test_command.py index 6262fa0..0b2b1d2 100644 --- a/jupyter_core/tests/test_command.py +++ b/jupyter_core/tests/test_command.py @@ -224,4 +224,4 @@ def test_argv0(tmpdir): out = check_output([sys.executable, str(jupyter), 'witness'], env=env) # Make sure the first argv is the full path to the executing script - assert f'{jupyter}-witness'.encode() in out \ No newline at end of file + assert f'{jupyter}-witness'.encode() in out