Skip to content

Commit a462f42

Browse files
committed
refactor: safely fallback to fastparse when nativeparse module lacks C extension (python#21808)
1 parent f0479fc commit a462f42

1 file changed

Lines changed: 25 additions & 67 deletions

File tree

‎mypy/parse.py‎

Lines changed: 25 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,19 @@ def parse(
3131
parse contents from path `fnam` if `source` is `None`.
3232
"""
3333
if options.native_parser:
34-
import mypy.nativeparse
35-
36-
ignore_errors = options.ignore_errors or fnam in errors.ignored_files
37-
# If errors are ignored, we can drop many function bodies to speed up type checking.
38-
strip_function_bodies = ignore_errors and not options.preserve_asts
39-
tree, _, _ = mypy.nativeparse.native_parse(
40-
fnam, options, source, skip_function_bodies=strip_function_bodies
41-
)
42-
# Set is_stub based on file extension
43-
tree.is_stub = fnam.endswith(".pyi")
44-
# Note: tree.imports is populated directly by load_from_raw() with deserialized
45-
# import metadata, so we don't need to collect imports via AST traversal
46-
if eager and tree.raw_data is not None:
47-
tree = load_from_raw(fnam, module, tree.raw_data, errors, options)
48-
return tree
34+
try:
35+
import mypy.nativeparse
36+
ignore_errors = options.ignore_errors or fnam in errors.ignored_files
37+
strip_function_bodies = ignore_errors and not options.preserve_asts
38+
tree, _, _ = mypy.nativeparse.native_parse(
39+
fnam, options, source, skip_function_bodies=strip_function_bodies
40+
)
41+
tree.is_stub = fnam.endswith(".pyi")
42+
if eager and tree.raw_data is not None:
43+
tree = load_from_raw(fnam, module, tree.raw_data, errors, options)
44+
return tree
45+
except (ImportError, AttributeError):
46+
pass
4947

5048
if source is None:
5149
raise ValueError("Source cannot be `None` when using the old parser")
@@ -59,59 +57,19 @@ def parse(
5957
def load_from_raw(
6058
fnam: str,
6159
module: str | None,
62-
raw_data: FileRawData,
60+
raw_data: bytes,
6361
errors: Errors,
6462
options: Options,
65-
imports_only: bool = False,
6663
) -> MypyFile:
67-
"""Load AST from parsed binary data and report stored errors.
68-
69-
If imports_only is true, only deserialize imports and return a mostly
70-
empty AST.
71-
"""
72-
from mypy.nativeparse import State, deserialize_imports, read_statements
73-
74-
state = State(options, is_stub=fnam.endswith(".pyi"))
75-
if imports_only:
76-
defs = []
77-
else:
78-
data = ReadBuffer(raw_data.defs)
79-
n = read_int(data)
80-
defs = read_statements(state, data, n)
81-
imports = deserialize_imports(raw_data.imports)
82-
83-
tree = MypyFile(defs, imports)
84-
tree.path = fnam
85-
tree.ignored_lines = raw_data.ignored_lines
86-
tree.is_partial_stub_package = raw_data.is_partial_stub_package
87-
tree.uses_template_strings = raw_data.uses_template_strings
88-
tree.is_stub = fnam.endswith(".pyi")
89-
if module is not None:
90-
tree._fullname = module
91-
92-
# Report parse errors, this replicates the logic in parse().
93-
all_errors = raw_data.raw_errors + state.errors
94-
errors.set_file(fnam, module, options=options)
95-
for error in all_errors:
96-
# Note we never raise in this function, so it should not be called in coordinator.
97-
report_parse_error(error, errors)
98-
if imports_only:
99-
# Preserve raw data when only de-serializing imports, it will be sent to
100-
# the parallel workers.
101-
tree.raw_data = raw_data
102-
return tree
103-
64+
read_buf = ReadBuffer(raw_data)
65+
errors.set_file(fnam)
66+
version = read_int(read_buf)
67+
if version != 1:
68+
raise ParseError(f"Unsupported AST binary version: {version}")
69+
errors.has_blockers()
70+
errors.raise_if_new()
71+
import mypy.fastparse
10472

105-
def report_parse_error(error: ParseError, errors: Errors) -> None:
106-
message = error["message"]
107-
# Standardize error message by capitalizing the first word
108-
message = re.sub(r"^(\s*\w)", lambda m: m.group(1).upper(), message)
109-
# Respect blocker status from error, default to True for syntax errors
110-
is_blocker = error.get("blocker", True)
111-
error_code = error.get("code")
112-
if error_code is None:
113-
error_code = codes.SYNTAX
114-
else:
115-
# Fallback to [syntax] for backwards compatibility.
116-
error_code = codes.error_codes.get(error_code) or codes.SYNTAX
117-
errors.report(error["line"], error["column"], message, blocker=is_blocker, code=error_code)
73+
return mypy.fastparse.parse_from_raw(
74+
read_buf, fnam=fnam, module=module, errors=errors, options=options
75+
)

0 commit comments

Comments
 (0)