MCPcopy Index your code
hub / github.com/python/cpython / generate_parser_c_extension

Function generate_parser_c_extension

Tools/peg_generator/pegen/testutil.py:85–113  ·  view source on GitHub ↗

Generate a parser c extension for the given grammar in the given path Returns a module object with a parse_string() method. TODO: express that using a Protocol.

(
    grammar: Grammar,
    path: pathlib.PurePath,
    debug: bool = False,
    library_dir: str | None = None,
)

Source from the content-addressed store, hash-verified

83
84
85def generate_parser_c_extension(
86 grammar: Grammar,
87 path: pathlib.PurePath,
88 debug: bool = False,
89 library_dir: str | None = None,
90) -> Any:
91 """Generate a parser c extension for the given grammar in the given path
92
93 Returns a module object with a parse_string() method.
94 TODO: express that using a Protocol.
95 """
96 # Make sure that the working directory is empty: reusing non-empty temporary
97 # directories when generating extensions can lead to segmentation faults.
98 # Check issue #95 (https://github.com/gvanrossum/pegen/issues/95) for more
99 # context.
100 assert not os.listdir(path)
101 source = path / "parse.c"
102 with open(source, "w", encoding="utf-8") as file:
103 genr = CParserGenerator(
104 grammar, ALL_TOKENS, EXACT_TOKENS, NON_EXACT_TOKENS, file, debug=debug
105 )
106 genr.generate("parse.c")
107 compile_c_extension(
108 str(source),
109 build_dir=str(path),
110 # Significant test_peg_generator speedups
111 disable_optimization=True,
112 library_dir=library_dir,
113 )
114
115
116def print_memstats() -> bool:

Callers 1

build_extensionMethod · 0.90

Calls 6

generateMethod · 0.95
CParserGeneratorClass · 0.90
compile_c_extensionFunction · 0.90
strFunction · 0.85
listdirMethod · 0.80
openFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…