From 4a7e0c858c624eee49b9b1a977c887f5b49d0fd4 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 11 Mar 2010 12:34:39 +0000 Subject: [PATCH] Issue #7774: Set sys.executable to an empty string if argv[0] has been set to an non existent program name and Python is unable to retrieve the real program name. Fix also sysconfig: if sys.executable is an empty string, use the current working directory. --- Lib/sysconfig.py | 7 ++++++- Lib/test/test_sys.py | 11 +++++++++++ Misc/NEWS | 4 ++++ Modules/getpath.c | 2 +- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py index 69264d2e48..a11a412475 100644 --- a/Lib/sysconfig.py +++ b/Lib/sysconfig.py @@ -84,7 +84,12 @@ _PREFIX = os.path.normpath(sys.prefix) _EXEC_PREFIX = os.path.normpath(sys.exec_prefix) _CONFIG_VARS = None _USER_BASE = None -_PROJECT_BASE = os.path.dirname(realpath(sys.executable)) +if sys.executable: + _PROJECT_BASE = os.path.dirname(realpath(sys.executable)) +else: + # sys.executable can be empty if argv[0] has been changed and Python is + # unable to retrieve the real program name + _PROJECT_BASE = realpath(os.getcwd()) if os.name == "nt" and "pcbuild" in _PROJECT_BASE[-8:].lower(): _PROJECT_BASE = realpath(os.path.join(_PROJECT_BASE, pardir)) diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 15b88ceb39..e41814bfd6 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -437,6 +437,17 @@ class SysModuleTest(unittest.TestCase): self.assertEqual(sys.call_tracing(str, (2,)), "2") self.assertRaises(TypeError, sys.call_tracing, str, 2) + def test_executable(self): + # Issue #7774: Ensure that sys.executable is an empty string if argv[0] + # has been set to an non existent program name and Python is unable to + # retrieve the real program name + import subprocess + p = subprocess.Popen( + ["nonexistent", "-c", 'import sys; print "executable=%r" % sys.executable'], + executable=sys.executable, stdout=subprocess.PIPE) + executable = p.communicate()[0].strip() + p.wait() + self.assertEqual(executable, "executable=''") class SizeofTest(unittest.TestCase): diff --git a/Misc/NEWS b/Misc/NEWS index 8f450a9fd5..a64bd16fca 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,10 @@ What's New in Python 2.7 beta 1? Core and Builtins ----------------- +- Issue #7774: Set sys.executable to an empty string if argv[0] has been + set to an non existent program name and Python is unable to retrieve the real + program name + - Issue #3137: Don't ignore errors at startup, especially a keyboard interrupt (SIGINT). If an error occurs while importing the site module, the error is printed and Python exits. Initialize the GIL before importing the site diff --git a/Modules/getpath.c b/Modules/getpath.c index 09fbe1017b..682ad3ed65 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -441,7 +441,7 @@ calculate_path(void) } else progpath[0] = '\0'; - if (progpath[0] != SEP) + if (progpath[0] != SEP && progpath[0] != '\0') absolutize(progpath); strncpy(argv0_path, progpath, MAXPATHLEN); argv0_path[MAXPATHLEN] = '\0'; -- 2.40.0