Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 46874ad

Browse files
committed
Issue #15364: Fix sysconfig.get_config_var('srcdir') to be an absolute path.
1 parent a61b459 commit 46874ad

5 files changed

Lines changed: 92 additions & 22 deletions

File tree

Lib/distutils/sysconfig.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,23 @@ def get_config_vars(*args):
533533
_config_vars['prefix'] = PREFIX
534534
_config_vars['exec_prefix'] = EXEC_PREFIX
535535

536+
# Always convert srcdir to an absolute path
537+
srcdir = _config_vars.get('srcdir', project_base)
538+
if os.name == 'posix':
539+
if python_build:
540+
# If srcdir is a relative path (typically '.' or '..')
541+
# then it should be interpreted relative to the directory
542+
# containing Makefile.
543+
base = os.path.dirname(get_makefile_filename())
544+
srcdir = os.path.join(base, srcdir)
545+
else:
546+
# srcdir is not meaningful since the installation is
547+
# spread about the filesystem. We choose the
548+
# directory containing the Makefile since we know it
549+
# exists.
550+
srcdir = os.path.dirname(get_makefile_filename())
551+
_config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
552+
536553
# Convert srcdir into an absolute path if it appears necessary.
537554
# Normally it is relative to the build directory. However, during
538555
# testing, for example, we might be running a non-installed python

Lib/distutils/tests/test_sysconfig.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ def test_get_config_vars(self):
5353
self.assertTrue(isinstance(cvars, dict))
5454
self.assertTrue(cvars)
5555

56+
def test_srcdir(self):
57+
# See Issues #15322, #15364.
58+
srcdir = sysconfig.get_config_var('srcdir')
59+
60+
self.assertTrue(os.path.isabs(srcdir), srcdir)
61+
self.assertTrue(os.path.isdir(srcdir), srcdir)
62+
63+
if sysconfig.python_build:
64+
# The python executable has not been installed so srcdir
65+
# should be a full source checkout.
66+
Python_h = os.path.join(srcdir, 'Include', 'Python.h')
67+
self.assertTrue(os.path.exists(Python_h), Python_h)
68+
self.assertTrue(sysconfig._is_python_source_dir(srcdir))
69+
elif os.name == 'posix':
70+
self.assertEqual(sysconfig.get_makefile_filename(), srcdir)
71+
72+
def test_srcdir_independent_of_cwd(self):
73+
# srcdir should be independent of the current working directory
74+
# See Issues #15322, #15364.
75+
srcdir = sysconfig.get_config_var('srcdir')
76+
cwd = os.getcwd()
77+
try:
78+
os.chdir('..')
79+
srcdir2 = sysconfig.get_config_var('srcdir')
80+
finally:
81+
os.chdir(cwd)
82+
self.assertEqual(srcdir, srcdir2)
83+
5684
def test_customize_compiler(self):
5785

5886
# not testing if default compiler is not unix

Lib/sysconfig.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -533,28 +533,22 @@ def get_config_vars(*args):
533533
# the init-function.
534534
_CONFIG_VARS['userbase'] = _getuserbase()
535535

536-
if 'srcdir' not in _CONFIG_VARS:
537-
_CONFIG_VARS['srcdir'] = _PROJECT_BASE
538-
else:
539-
_CONFIG_VARS['srcdir'] = _safe_realpath(_CONFIG_VARS['srcdir'])
540-
541-
# Convert srcdir into an absolute path if it appears necessary.
542-
# Normally it is relative to the build directory. However, during
543-
# testing, for example, we might be running a non-installed python
544-
# from a different directory.
545-
if _PYTHON_BUILD and os.name == "posix":
546-
base = _PROJECT_BASE
547-
try:
548-
cwd = os.getcwd()
549-
except OSError:
550-
cwd = None
551-
if (not os.path.isabs(_CONFIG_VARS['srcdir']) and
552-
base != cwd):
553-
# srcdir is relative and we are not in the same directory
554-
# as the executable. Assume executable is in the build
555-
# directory and make srcdir absolute.
556-
srcdir = os.path.join(base, _CONFIG_VARS['srcdir'])
557-
_CONFIG_VARS['srcdir'] = os.path.normpath(srcdir)
536+
# Always convert srcdir to an absolute path
537+
srcdir = _CONFIG_VARS.get('srcdir', _PROJECT_BASE)
538+
if os.name == 'posix':
539+
if _PYTHON_BUILD:
540+
# If srcdir is a relative path (typically '.' or '..')
541+
# then it should be interpreted relative to the directory
542+
# containing Makefile.
543+
base = os.path.dirname(get_makefile_filename())
544+
srcdir = os.path.join(base, srcdir)
545+
else:
546+
# srcdir is not meaningful since the installation is
547+
# spread about the filesystem. We choose the
548+
# directory containing the Makefile since we know it
549+
# exists.
550+
srcdir = os.path.dirname(get_makefile_filename())
551+
_CONFIG_VARS['srcdir'] = _safe_realpath(srcdir)
558552

559553
# OS X platforms require special customization to handle
560554
# multi-architecture, multi-os-version installers

Lib/test/test_sysconfig.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,34 @@ def test_platform_in_subprocess(self):
340340
self.assertEqual(status, 0)
341341
self.assertEqual(my_platform, test_platform)
342342

343+
def test_srcdir(self):
344+
# See Issues #15322, #15364.
345+
srcdir = sysconfig.get_config_var('srcdir')
346+
347+
self.assertTrue(os.path.isabs(srcdir), srcdir)
348+
self.assertTrue(os.path.isdir(srcdir), srcdir)
349+
350+
if sysconfig._PYTHON_BUILD:
351+
# The python executable has not been installed so srcdir
352+
# should be a full source checkout.
353+
Python_h = os.path.join(srcdir, 'Include', 'Python.h')
354+
self.assertTrue(os.path.exists(Python_h), Python_h)
355+
self.assertTrue(sysconfig._is_python_source_dir(srcdir))
356+
elif os.name == 'posix':
357+
self.assertEqual(sysconfig.get_makefile_filename(), srcdir)
358+
359+
def test_srcdir_independent_of_cwd(self):
360+
# srcdir should be independent of the current working directory
361+
# See Issues #15322, #15364.
362+
srcdir = sysconfig.get_config_var('srcdir')
363+
cwd = os.getcwd()
364+
try:
365+
os.chdir('..')
366+
srcdir2 = sysconfig.get_config_var('srcdir')
367+
finally:
368+
os.chdir(cwd)
369+
self.assertEqual(srcdir, srcdir2)
370+
343371

344372
class MakefileTests(unittest.TestCase):
345373

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ Core and Builtins
5555
Library
5656
-------
5757

58+
- Issue #15364: Fix sysconfig.get_config_var('srcdir') to be an
59+
absolute path.
60+
5861
- Issue #15041: update "see also" list in tkinter documentation.
5962

6063
- Issue #15413: os.times() had disappeared under Windows.

0 commit comments

Comments
 (0)