bpo-28552: Fix distutils.sysconfig for empty sys.executable (GH-12875… · python/cpython@f4edd39 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -114,7 +114,7 @@ def finalize_options(self):
114 114 self.build_scripts = os.path.join(self.build_base,
115 115 'scripts-' + sys.version[0:3])
116 116
117 -if self.executable is None:
117 +if self.executable is None and sys.executable:
118 118 self.executable = os.path.normpath(sys.executable)
119 119
120 120 def run(self):
Original file line number Diff line number Diff line change
@@ -25,7 +25,12 @@
25 25 # Path to the base directory of the project. On Windows the binary may
26 26 # live in project/PCBuild9. If we're dealing with an x64 Windows build,
27 27 # 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()
29 34 if os.name == "nt" and "pcbuild" in project_base[-8:].lower():
30 35 project_base = os.path.abspath(os.path.join(project_base, os.path.pardir))
31 36 # PC/VS7.1
@@ -79,7 +84,12 @@ def get_python_inc(plat_specific=0, prefix=None):
79 84
80 85 if os.name == "posix":
81 86 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()
83 93 if plat_specific:
84 94 # python.h is located in the buildir
85 95 inc_dir = buildir
Original file line number Diff line number Diff 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.