|
20 | 20 | import subprocess |
21 | 21 | import sys |
22 | 22 | import tempfile |
| 23 | +import test.support |
23 | 24 | import unittest |
24 | 25 |
|
25 | 26 | # Local imports |
@@ -589,25 +590,31 @@ class TestParserIdempotency(support.TestCase): |
589 | 590 |
|
590 | 591 | """A cut-down version of pytree_idempotency.py.""" |
591 | 592 |
|
| 593 | + def parse_file(self, filepath): |
| 594 | + if test.support.verbose: |
| 595 | + print(f"Parse file: {filepath}") |
| 596 | + with open(filepath, "rb") as fp: |
| 597 | + encoding = tokenize.detect_encoding(fp.readline)[0] |
| 598 | + self.assertIsNotNone(encoding, |
| 599 | + "can't detect encoding for %s" % filepath) |
| 600 | + with open(filepath, "r", encoding=encoding) as fp: |
| 601 | + source = fp.read() |
| 602 | + try: |
| 603 | + tree = driver.parse_string(source) |
| 604 | + except ParseError: |
| 605 | + try: |
| 606 | + tree = driver_no_print_statement.parse_string(source) |
| 607 | + except ParseError as err: |
| 608 | + self.fail('ParseError on file %s (%s)' % (filepath, err)) |
| 609 | + new = str(tree) |
| 610 | + if new != source: |
| 611 | + print(diff_texts(source, new, filepath)) |
| 612 | + self.fail("Idempotency failed: %s" % filepath) |
| 613 | + |
592 | 614 | def test_all_project_files(self): |
593 | 615 | for filepath in support.all_project_files(): |
594 | | - with open(filepath, "rb") as fp: |
595 | | - encoding = tokenize.detect_encoding(fp.readline)[0] |
596 | | - self.assertIsNotNone(encoding, |
597 | | - "can't detect encoding for %s" % filepath) |
598 | | - with open(filepath, "r", encoding=encoding) as fp: |
599 | | - source = fp.read() |
600 | | - try: |
601 | | - tree = driver.parse_string(source) |
602 | | - except ParseError: |
603 | | - try: |
604 | | - tree = driver_no_print_statement.parse_string(source) |
605 | | - except ParseError as err: |
606 | | - self.fail('ParseError on file %s (%s)' % (filepath, err)) |
607 | | - new = str(tree) |
608 | | - if new != source: |
609 | | - print(diff_texts(source, new, filepath)) |
610 | | - self.fail("Idempotency failed: %s" % filepath) |
| 616 | + with self.subTest(filepath=filepath): |
| 617 | + self.parse_file(filepath) |
611 | 618 |
|
612 | 619 | def test_extended_unpacking(self): |
613 | 620 | driver.parse_string("a, *b, c = x\n") |
|
0 commit comments