Skip to content

Commit f4edd39

Browse files
authored
bpo-28552: Fix distutils.sysconfig for empty sys.executable (GH-12875) (GH-12949)
bpo-28552, bpo-7774: Fix distutils.sysconfig if sys.executable is None or an empty string: use os.getcwd() to initialize project_base. Fix also the distutils build command: don't use sys.executable if it's evaluated as false (None or empty string).
1 parent 84efbae commit f4edd39

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

Lib/distutils/command/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def finalize_options(self):
114114
self.build_scripts = os.path.join(self.build_base,
115115
'scripts-' + sys.version[0:3])
116116

117-
if self.executable is None:
117+
if self.executable is None and sys.executable:
118118
self.executable = os.path.normpath(sys.executable)
119119

120120
def run(self):

Lib/distutils/sysconfig.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
# Path to the base directory of the project. On Windows the binary may
2626
# live in project/PCBuild9. If we're dealing with an x64 Windows build,
2727
# it'll live in project/PCbuild/amd64.
28-
project_base = os.path.dirname(os.path.abspath(sys.executable))
28+
if sys.executable:
29+
project_base = os.path.dirname(os.path.abspath(sys.executable))
30+
else:
31+
# sys.executable can be empty if argv[0] has been changed and Python is
32+
# unable to retrieve the real program name
33+
project_base = os.getcwd()
2934
if os.name == "nt" and "pcbuild" in project_base[-8:].lower():
3035
project_base = os.path.abspath(os.path.join(project_base, os.path.pardir))
3136
# PC/VS7.1
@@ -79,7 +84,12 @@ def get_python_inc(plat_specific=0, prefix=None):
7984

8085
if os.name == "posix":
8186
if python_build:
82-
buildir = os.path.dirname(sys.executable)
87+
if sys.executable:
88+
buildir = os.path.dirname(sys.executable)
89+
else:
90+
# sys.executable can be empty if argv[0] has been changed
91+
# and Python is unable to retrieve the real program name
92+
buildir = os.getcwd()
8393
if plat_specific:
8494
# python.h is located in the buildir
8595
inc_dir = buildir
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix :mod:`distutils.sysconfig` if :data:`sys.executable` is ``None`` or an
2+
empty string: use :func:`os.getcwd` to initialize ``project_base``. Fix
3+
also the distutils build command: don't use :data:`sys.executable` if it is
4+
``None`` or an empty string.

0 commit comments

Comments
 (0)