|
15 | 15 | from __future__ import annotations |
16 | 16 |
|
17 | 17 | import argparse |
18 | | -import ast |
19 | 18 | import functools |
20 | 19 | import keyword |
21 | 20 | import logging |
22 | 21 | import os |
| 22 | +import py_compile |
23 | 23 | import re |
24 | 24 | import shutil |
25 | 25 | import tempfile |
@@ -323,59 +323,54 @@ def wrap_text(text): |
323 | 323 | f.write(stub_file) |
324 | 324 |
|
325 | 325 |
|
326 | | -def check_remove_syntax_error(filename, limit=1000): |
| 326 | +def check_remove_syntax_error(filename: str, limit: int = 10000): |
327 | 327 | """ |
328 | 328 | Args: |
329 | 329 | filename: xxx.pyi |
330 | | - limit: check limit, or raise error |
| 330 | + limit: the max times try to check syntax error |
331 | 331 | """ |
332 | 332 | pattern_check = re.compile( |
333 | 333 | rf"File.*{re.escape(filename)}.*line (?P<lineno>\d+)" |
334 | 334 | ) |
335 | 335 |
|
336 | | - while limit: |
| 336 | + while limit > 0: |
| 337 | + |
337 | 338 | limit -= 1 |
338 | 339 |
|
339 | | - # read file and check syntax error |
| 340 | + # check syntax error |
340 | 341 | err = "" |
341 | | - source = "" |
342 | | - source_lines = [] |
343 | | - with open(filename, "r", encoding="utf-8") as f: |
344 | | - source_lines = f.readlines() |
345 | | - source = "".join(source_lines) |
346 | | - |
347 | | - is_valid = True |
348 | | - |
349 | | - try: |
350 | | - ast.parse(source, filename) |
351 | | - except SyntaxError: |
352 | | - is_valid = False |
353 | | - err = traceback.format_exc() |
354 | 342 |
|
355 | | - if is_valid: |
356 | | - break |
357 | | - else: |
358 | | - if limit <= 0: |
359 | | - print(f">>> Syntax error detected in file: {filename}") |
| 343 | + try: |
| 344 | + py_compile.compile(filename, doraise=True) |
| 345 | + break |
| 346 | + except py_compile.PyCompileError as e: |
| 347 | + err = traceback.format_exc() |
360 | 348 |
|
361 | 349 | print(f">>> Syntax error: find syntax error in file: {filename}") |
362 | 350 |
|
363 | 351 | # find the line with syntax error |
364 | 352 | match_obj = pattern_check.search(err) |
365 | 353 | if match_obj is not None: |
366 | 354 | line_no = int(match_obj.group("lineno")) |
| 355 | + |
| 356 | + # read file |
| 357 | + source_lines = [] |
| 358 | + with open(filename, "r", encoding="utf-8") as f: |
| 359 | + source_lines = f.readlines() |
| 360 | + |
| 361 | + del source_lines[line_no - 1] |
| 362 | + |
| 363 | + # write new lines |
| 364 | + with open(filename, "w", encoding="utf-8") as f: |
| 365 | + f.writelines(source_lines) |
| 366 | + |
367 | 367 | print( |
368 | 368 | f">>> Syntax error: remove the error line {line_no}, and continue to check ..." |
369 | 369 | ) |
370 | | - del source_lines[line_no - 1] |
371 | 370 | else: |
372 | 371 | print(">>> Syntax error: no match obj, just continue ...") |
373 | 372 | break |
374 | 373 |
|
375 | | - # write new lines |
376 | | - with open(filename, "w", encoding="utf-8") as f: |
377 | | - f.writelines(source_lines) |
378 | | - |
379 | 374 |
|
380 | 375 | def post_process(output_dir: str): |
381 | 376 | """ |
|
0 commit comments