Skip to content

Commit 8939221

Browse files
vstinnerzooba
andauthored
[3.12] gh-109615: Fix support test_copy_python_src_ignore() (#109958) (#110340)
* gh-109615: Fix support test_copy_python_src_ignore() (#109958) Fix the test when run on an installed Python: use "abs_srcdir" of sysconfig, and skip the test if the Python source code cannot be found. * Tools/patchcheck/patchcheck.py, Tools/freeze/test/freeze.py and Lib/test/libregrtest/utils.py now first try to get "abs_srcdir" from sysconfig, before getting "srcdir" from sysconfig. * test.pythoninfo logs sysconfig "abs_srcdir". (cherry picked from commit b89ed9d) * gh-109615: Fix support test_copy_python_src_ignore() on WASM (#109970) Not only check if src_dir exists, but look also for Lib/os.py landmark. (cherry picked from commit cc54bcf) * gh-109615: Look for 'Modules' as landmark for test_copy_python_src_ignore (GH-110108) (cherry picked from commit 20bc5f7) * gh-109748: Fix again venv test_zippath_from_non_installed_posix() (#110149) Call also copy_python_src_ignore() on listdir() names. shutil.copytree(): replace set() with an empty tuple. An empty tuple becomes a constant in the compiler and checking if an item is in an empty tuple is cheap. (cherry picked from commit 0def8c7) --------- Co-authored-by: Steve Dower <[email protected]>
1 parent e7a61d3 commit 8939221

File tree

7 files changed

+49
-12
lines changed

7 files changed

+49
-12
lines changed

Lib/shutil.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def _copytree(entries, src, dst, symlinks, ignore, copy_function,
481481
if ignore is not None:
482482
ignored_names = ignore(os.fspath(src), [x.name for x in entries])
483483
else:
484-
ignored_names = set()
484+
ignored_names = ()
485485

486486
os.makedirs(dst, exist_ok=dirs_exist_ok)
487487
errors = []

Lib/test/libregrtest/main.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -747,11 +747,13 @@ def set_temp_dir(self):
747747
if sysconfig.is_python_build():
748748
self.tmp_dir = sysconfig.get_config_var('abs_builddir')
749749
if self.tmp_dir is None:
750-
# gh-74470: On Windows, only srcdir is available. Using
751-
# abs_builddir mostly matters on UNIX when building Python
752-
# out of the source tree, especially when the source tree
753-
# is read only.
754-
self.tmp_dir = sysconfig.get_config_var('srcdir')
750+
self.tmp_dir = sysconfig.get_config_var('abs_srcdir')
751+
if not self.tmp_dir:
752+
# gh-74470: On Windows, only srcdir is available. Using
753+
# abs_builddir mostly matters on UNIX when building
754+
# Python out of the source tree, especially when the
755+
# source tree is read only.
756+
self.tmp_dir = sysconfig.get_config_var('srcdir')
755757
self.tmp_dir = os.path.join(self.tmp_dir, 'build')
756758
else:
757759
self.tmp_dir = tempfile.gettempdir()

Lib/test/pythoninfo.py

+2
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ def collect_sysconfig(info_add):
516516
'Py_ENABLE_SHARED',
517517
'SHELL',
518518
'SOABI',
519+
'abs_builddir',
520+
'abs_srcdir',
519521
'prefix',
520522
):
521523
value = sysconfig.get_config_var(name)

Lib/test/test_support.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,27 @@ def recursive_function(depth):
767767
#self.assertEqual(available, 2)
768768

769769
def test_copy_python_src_ignore(self):
770-
src_dir = sysconfig.get_config_var('srcdir')
770+
# Get source directory
771+
src_dir = sysconfig.get_config_var('abs_srcdir')
772+
if not src_dir:
773+
src_dir = sysconfig.get_config_var('srcdir')
771774
src_dir = os.path.abspath(src_dir)
772775

773-
ignored = {'.git', '__pycache__'}
776+
# Check that the source code is available
777+
if not os.path.exists(src_dir):
778+
self.skipTest(f"cannot access Python source code directory:"
779+
f" {src_dir!r}")
780+
# Check that the landmark copy_python_src_ignore() expects is available
781+
# (Previously we looked for 'Lib\os.py', which is always present on Windows.)
782+
landmark = os.path.join(src_dir, 'Modules')
783+
if not os.path.exists(landmark):
784+
self.skipTest(f"cannot access Python source code directory:"
785+
f" {landmark!r} landmark is missing")
786+
787+
# Test support.copy_python_src_ignore()
774788

775789
# Source code directory
790+
ignored = {'.git', '__pycache__'}
776791
names = os.listdir(src_dir)
777792
self.assertEqual(support.copy_python_src_ignore(src_dir, names),
778793
ignored | {'build'})
@@ -782,7 +797,7 @@ def test_copy_python_src_ignore(self):
782797
self.assertEqual(support.copy_python_src_ignore(path, os.listdir(path)),
783798
ignored | {'build', 'venv'})
784799

785-
# An other directory
800+
# Another directory
786801
path = os.path.join(src_dir, 'Objects')
787802
self.assertEqual(support.copy_python_src_ignore(path, os.listdir(path)),
788803
ignored)

Lib/test/test_venv.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,11 @@ def test_zippath_from_non_installed_posix(self):
571571
eachpath,
572572
os.path.join(non_installed_dir, platlibdir))
573573
elif os.path.isfile(os.path.join(eachpath, "os.py")):
574-
for name in os.listdir(eachpath):
574+
names = os.listdir(eachpath)
575+
ignored_names = copy_python_src_ignore(eachpath, names)
576+
for name in names:
577+
if name in ignored_names:
578+
continue
575579
if name == "site-packages":
576580
continue
577581
fn = os.path.join(eachpath, name)

Tools/freeze/test/freeze.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@
77
from test import support
88

99

10+
def get_python_source_dir():
11+
src_dir = sysconfig.get_config_var('abs_srcdir')
12+
if not src_dir:
13+
src_dir = sysconfig.get_config_var('srcdir')
14+
return os.path.abspath(src_dir)
15+
16+
1017
TESTS_DIR = os.path.dirname(__file__)
1118
TOOL_ROOT = os.path.dirname(TESTS_DIR)
12-
SRCDIR = os.path.abspath(sysconfig.get_config_var('srcdir'))
19+
SRCDIR = get_python_source_dir()
1320

1421
MAKE = shutil.which('make')
1522
FREEZE = os.path.join(TOOL_ROOT, 'freeze.py')

Tools/patchcheck/patchcheck.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@
1111
import untabify
1212

1313

14+
def get_python_source_dir():
15+
src_dir = sysconfig.get_config_var('abs_srcdir')
16+
if not src_dir:
17+
src_dir = sysconfig.get_config_var('srcdir')
18+
return os.path.abspath(src_dir)
19+
20+
1421
# Excluded directories which are copies of external libraries:
1522
# don't check their coding style
1623
EXCLUDE_DIRS = [
1724
os.path.join('Modules', '_decimal', 'libmpdec'),
1825
os.path.join('Modules', 'expat'),
1926
os.path.join('Modules', 'zlib'),
2027
]
21-
SRCDIR = sysconfig.get_config_var('srcdir')
28+
SRCDIR = get_python_source_dir()
2229

2330

2431
def n_files_str(count):

0 commit comments

Comments
 (0)