Skip to content

Commit 6371029

Browse files
[3.12] gh-100238: Use setuptools in peg-generator and reenable tests (GH-104798) (#105135)
(cherry picked from commit afa759f) Co-authored-by: Lysandros Nikolaou <[email protected]>
1 parent 00c522a commit 6371029

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

Lib/test/support/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,15 +1868,16 @@ def missing_compiler_executable(cmd_names=[]):
18681868
missing.
18691869
18701870
"""
1871-
# TODO (PEP 632): alternate check without using distutils
1872-
from distutils import ccompiler, sysconfig, spawn, errors
1871+
from setuptools._distutils import ccompiler, sysconfig, spawn
1872+
from setuptools import errors
1873+
18731874
compiler = ccompiler.new_compiler()
18741875
sysconfig.customize_compiler(compiler)
18751876
if compiler.compiler_type == "msvc":
18761877
# MSVC has no executables, so check whether initialization succeeds
18771878
try:
18781879
compiler.initialize()
1879-
except errors.DistutilsPlatformError:
1880+
except errors.PlatformError:
18801881
return "msvc"
18811882
for name in compiler.executables:
18821883
if cmd_names and name not in cmd_names:

Lib/test/test_peg_generator/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
from test import support
44
from test.support import load_package_tests
55

6-
# TODO: gh-92584: peg_generator uses distutils which was removed in Python 3.12
7-
raise unittest.SkipTest("distutils has been removed in Python 3.12")
8-
96

107
if support.check_sanitizer(address=True, memory=True):
118
# bpo-46633: Skip the test because it is too slow when Python is built

Lib/test/test_peg_generator/test_c_parser.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import contextlib
2+
import subprocess
13
import sysconfig
24
import textwrap
35
import unittest
@@ -8,7 +10,7 @@
810

911
from test import test_tools
1012
from test import support
11-
from test.support import os_helper
13+
from test.support import os_helper, import_helper
1214
from test.support.script_helper import assert_python_ok
1315

1416
_py_cflags_nodist = sysconfig.get_config_var("PY_CFLAGS_NODIST")
@@ -88,6 +90,16 @@ def setUpClass(cls):
8890
cls.library_dir = tempfile.mkdtemp(dir=cls.tmp_base)
8991
cls.addClassCleanup(shutil.rmtree, cls.library_dir)
9092

93+
with contextlib.ExitStack() as stack:
94+
python_exe = stack.enter_context(support.setup_venv_with_pip_setuptools_wheel("venv"))
95+
sitepackages = subprocess.check_output(
96+
[python_exe, "-c", "import sysconfig; print(sysconfig.get_path('platlib'))"],
97+
text=True,
98+
).strip()
99+
stack.enter_context(import_helper.DirsOnSysPath(sitepackages))
100+
cls.addClassCleanup(stack.pop_all().close)
101+
102+
@support.requires_venv_with_pip()
91103
def setUp(self):
92104
self._backup_config_vars = dict(sysconfig._CONFIG_VARS)
93105
cmd = support.missing_compiler_executable()

Lib/test/test_peg_generator/test_pegen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ def test_soft_keyword(self) -> None:
794794
start:
795795
| "number" n=NUMBER { eval(n.string) }
796796
| "string" n=STRING { n.string }
797-
| SOFT_KEYWORD l=NAME n=(NUMBER | NAME | STRING) { f"{l.string} = {n.string}"}
797+
| SOFT_KEYWORD l=NAME n=(NUMBER | NAME | STRING) { l.string + " = " + n.string }
798798
"""
799799
parser_class = make_parser(grammar)
800800
self.assertEqual(parse_string("number 1", parser_class), 1)

Tools/peg_generator/pegen/build.py

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import itertools
2+
import os
23
import pathlib
34
import sys
45
import sysconfig
@@ -27,6 +28,46 @@ def get_extra_flags(compiler_flags: str, compiler_py_flags_nodist: str) -> List[
2728
return f"{flags} {py_flags_nodist}".split()
2829

2930

31+
def fixup_build_ext(cmd):
32+
"""Function needed to make build_ext tests pass.
33+
34+
When Python was built with --enable-shared on Unix, -L. is not enough to
35+
find libpython<blah>.so, because regrtest runs in a tempdir, not in the
36+
source directory where the .so lives.
37+
38+
When Python was built with in debug mode on Windows, build_ext commands
39+
need their debug attribute set, and it is not done automatically for
40+
some reason.
41+
42+
This function handles both of these things. Example use:
43+
44+
cmd = build_ext(dist)
45+
support.fixup_build_ext(cmd)
46+
cmd.ensure_finalized()
47+
48+
Unlike most other Unix platforms, Mac OS X embeds absolute paths
49+
to shared libraries into executables, so the fixup is not needed there.
50+
51+
Taken from distutils (was part of the CPython stdlib until Python 3.11)
52+
"""
53+
if os.name == 'nt':
54+
cmd.debug = sys.executable.endswith('_d.exe')
55+
elif sysconfig.get_config_var('Py_ENABLE_SHARED'):
56+
# To further add to the shared builds fun on Unix, we can't just add
57+
# library_dirs to the Extension() instance because that doesn't get
58+
# plumbed through to the final compiler command.
59+
runshared = sysconfig.get_config_var('RUNSHARED')
60+
if runshared is None:
61+
cmd.library_dirs = ['.']
62+
else:
63+
if sys.platform == 'darwin':
64+
cmd.library_dirs = []
65+
else:
66+
name, equals, value = runshared.partition('=')
67+
cmd.library_dirs = [d for d in value.split(os.pathsep) if d]
68+
69+
70+
3071
def compile_c_extension(
3172
generated_source_path: str,
3273
build_dir: Optional[str] = None,
@@ -49,16 +90,15 @@ def compile_c_extension(
4990
static library of the common parser sources (this is useful in case you are
5091
creating multiple extensions).
5192
"""
52-
import distutils.log
53-
from distutils.core import Distribution, Extension
54-
from distutils.tests.support import fixup_build_ext # type: ignore
93+
import setuptools.logging
5594

56-
from distutils.ccompiler import new_compiler
57-
from distutils.dep_util import newer_group
58-
from distutils.sysconfig import customize_compiler
95+
from setuptools import Extension, Distribution
96+
from setuptools._distutils.dep_util import newer_group
97+
from setuptools._distutils.ccompiler import new_compiler
98+
from setuptools._distutils.sysconfig import customize_compiler
5999

60100
if verbose:
61-
distutils.log.set_threshold(distutils.log.DEBUG)
101+
setuptools.logging.set_threshold(setuptools.logging.logging.DEBUG)
62102

63103
source_file_path = pathlib.Path(generated_source_path)
64104
extension_name = source_file_path.stem

0 commit comments

Comments
 (0)