summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_peg_generator
diff options
context:
space:
mode:
authorJeremy Kloth <jeremy.kloth@gmail.com>2022-04-06 21:55:58 (GMT)
committerGitHub <noreply@github.com>2022-04-06 21:55:58 (GMT)
commit612e422c6ea9df60d3382ed1491d8254c283e93f (patch)
treef817d0a53e5119e317fffd17ba14ef55a2fb0b87 /Lib/test/test_peg_generator
parent1ba82d4419010994ddf68b9b4851509acc4d45e1 (diff)
downloadcpython-612e422c6ea9df60d3382ed1491d8254c283e93f.zip
cpython-612e422c6ea9df60d3382ed1491d8254c283e93f.tar.gz
cpython-612e422c6ea9df60d3382ed1491d8254c283e93f.tar.bz2
bpo-46576: Speed up test_peg_generator by using a static library for shared sources (GH-32338)
Speed up test_peg_generator by using a static library for shared sources to avoid recompiling as much code.
Diffstat (limited to 'Lib/test/test_peg_generator')
-rw-r--r--Lib/test/test_peg_generator/test_c_parser.py24
1 files changed, 22 insertions, 2 deletions
diff --git a/Lib/test/test_peg_generator/test_c_parser.py b/Lib/test/test_peg_generator/test_c_parser.py
index 51a4f7d..13b83a9 100644
--- a/Lib/test/test_peg_generator/test_c_parser.py
+++ b/Lib/test/test_peg_generator/test_c_parser.py
@@ -72,13 +72,30 @@ unittest.main()
@support.requires_subprocess()
class TestCParser(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ # When running under regtest, a seperate tempdir is used
+ # as the current directory and watched for left-overs.
+ # Reusing that as the base for temporary directories
+ # ensures everything is cleaned up properly and
+ # cleans up afterwards if not (with warnings).
+ cls.tmp_base = os.getcwd()
+ if os.path.samefile(cls.tmp_base, os_helper.SAVEDCWD):
+ cls.tmp_base = None
+ # Create a directory for the reuseable static library part of
+ # the pegen extension build process. This greatly reduces the
+ # runtime overhead of spawning compiler processes.
+ cls.library_dir = tempfile.mkdtemp(dir=cls.tmp_base)
+ cls.addClassCleanup(shutil.rmtree, cls.library_dir)
+
def setUp(self):
self._backup_config_vars = dict(sysconfig._CONFIG_VARS)
cmd = support.missing_compiler_executable()
if cmd is not None:
self.skipTest("The %r command is not found" % cmd)
self.old_cwd = os.getcwd()
- self.tmp_path = tempfile.mkdtemp()
+ self.tmp_path = tempfile.mkdtemp(dir=self.tmp_base)
change_cwd = os_helper.change_cwd(self.tmp_path)
change_cwd.__enter__()
self.addCleanup(change_cwd.__exit__, None, None, None)
@@ -91,7 +108,10 @@ class TestCParser(unittest.TestCase):
def build_extension(self, grammar_source):
grammar = parse_string(grammar_source, GrammarParser)
- generate_parser_c_extension(grammar, Path(self.tmp_path))
+ # Because setUp() already changes the current directory to the
+ # temporary path, use a relative path here to prevent excessive
+ # path lengths when compiling.
+ generate_parser_c_extension(grammar, Path('.'), library_dir=self.library_dir)
def run_test(self, grammar_source, test_source):
self.build_extension(grammar_source)