[Python-checkins] bpo-20443: No longer make sys.argv[0] absolute for script (GH-17534)

Victor Stinner webhook-mailer at python.org
Mon Dec 9 11:34:07 EST 2019


https://github.com/python/cpython/commit/a1a99b4bb7cbe2dbc55a1d92c3c509b4466d3c3b
commit: a1a99b4bb7cbe2dbc55a1d92c3c509b4466d3c3b
branch: master
author: Victor Stinner <vstinner at python.org>
committer: GitHub <noreply at github.com>
date: 2019-12-09T17:34:02+01:00
summary:

bpo-20443: No longer make sys.argv[0] absolute for script (GH-17534)

In Python 3.9.0a1, sys.argv[0] was made an asolute path if a filename
was specified on the command line. Revert this change, since most
users expect sys.argv to be unmodified.

files:
A Misc/NEWS.d/next/Core and Builtins/2019-12-09-17-05-53.bpo-20443.8OyT5P.rst
M Lib/test/test_cmd_line_script.py
M Lib/test/test_embed.py
M Python/initconfig.c

diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
index 2ac926deac485..adfb8ce5ed245 100644
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -223,12 +223,13 @@ def test_basic_script(self):
 
     def test_script_abspath(self):
         # pass the script using the relative path, expect the absolute path
-        # in __file__ and sys.argv[0]
+        # in __file__
         with support.temp_cwd() as script_dir:
             self.assertTrue(os.path.isabs(script_dir), script_dir)
 
             script_name = _make_test_script(script_dir, 'script')
-            self._check_script(os.path.basename(script_name), script_name, script_name,
+            relative_name = os.path.basename(script_name)
+            self._check_script(relative_name, script_name, relative_name,
                                script_dir, None,
                                importlib.machinery.SourceFileLoader)
 
diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py
index b87863a372a6c..60f7f7a93ea1a 100644
--- a/Lib/test/test_embed.py
+++ b/Lib/test/test_embed.py
@@ -858,10 +858,9 @@ def test_preinit_parse_argv(self):
         preconfig = {
             'allocator': PYMEM_ALLOCATOR_DEBUG,
         }
-        script_abspath = os.path.abspath('script.py')
         config = {
-            'argv': [script_abspath],
-            'run_filename': script_abspath,
+            'argv': ['script.py'],
+            'run_filename': os.path.abspath('script.py'),
             'dev_mode': 1,
             'faulthandler': 1,
             'warnoptions': ['default'],
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-12-09-17-05-53.bpo-20443.8OyT5P.rst b/Misc/NEWS.d/next/Core and Builtins/2019-12-09-17-05-53.bpo-20443.8OyT5P.rst
new file mode 100644
index 0000000000000..d3855f293b9f0
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-12-09-17-05-53.bpo-20443.8OyT5P.rst	
@@ -0,0 +1,3 @@
+In Python 3.9.0a1, sys.argv[0] was made an asolute path if a filename was
+specified on the command line. Revert this change, since most users expect
+sys.argv to be unmodified.
diff --git a/Python/initconfig.c b/Python/initconfig.c
index caa9bf5f5689e..74c9ca007ed70 100644
--- a/Python/initconfig.c
+++ b/Python/initconfig.c
@@ -2198,10 +2198,6 @@ config_update_argv(PyConfig *config, Py_ssize_t opt_index)
         /* Force sys.argv[0] = '-m'*/
         arg0 = L"-m";
     }
-    else if (config->run_filename != NULL) {
-        /* run_filename is converted to an absolute path: update argv */
-        arg0 = config->run_filename;
-    }
 
     if (arg0 != NULL) {
         arg0 = _PyMem_RawWcsdup(arg0);



More information about the Python-checkins mailing list