|
13 | 13 | import contextlib |
14 | 14 |
|
15 | 15 | from test import support |
16 | | -from test.support.script_helper import assert_python_ok |
| 16 | +from test.support.script_helper import assert_python_ok, assert_python_failure |
17 | 17 |
|
18 | 18 | try: |
19 | 19 | import _testcapi |
@@ -617,6 +617,14 @@ def test_dunder_lazy_import_invalid_arguments(self): |
617 | 617 | with self.assertRaises(TypeError): |
618 | 618 | __lazy_import__("sys", globals=1) |
619 | 619 |
|
| 620 | + code = textwrap.dedent(""" |
| 621 | + __lazy_import__("sys", fromlist=(1, 2, 3)) |
| 622 | + """) |
| 623 | + result = assert_python_failure("-c", code, NO_COLOR='y') |
| 624 | + self.assertIn( |
| 625 | + b"TypeError: Item in ``from list'' must be str, not int", |
| 626 | + result.err) |
| 627 | + |
620 | 628 | def test_dunder_lazy_import_builtins(self): |
621 | 629 | """__lazy_import__ should use module's __builtins__ for __import__.""" |
622 | 630 | from test.test_lazy_import.data import dunder_lazy_import_builtins |
@@ -766,6 +774,28 @@ def test_missing_lazy_from_import_shows_chained_traceback(self): |
766 | 774 | """) |
767 | 775 | assert_python_ok("-c", code) |
768 | 776 |
|
| 777 | + @support.subTests('name', ( |
| 778 | + 'test.test_lazy_import.data.broken_module_chained_cause', |
| 779 | + 'test.test_lazy_import.data.broken_module_chained_context', |
| 780 | + 'test.test_lazy_import.data.broken_module_chained_suppressed', |
| 781 | + )) |
| 782 | + def test_chained_exception_import_shows_notes(self, name): |
| 783 | + """Accessing missing attribute from lazy from-import should chain errors.""" |
| 784 | + code = textwrap.dedent(f""" |
| 785 | + lazy import {name} |
| 786 | +
|
| 787 | + try: |
| 788 | + _ = test |
| 789 | + except ValueError as e: |
| 790 | + assert any( |
| 791 | + note.startswith("lazy import of '{name}' declared in ") |
| 792 | + for note in e.__notes__ |
| 793 | + ), e.__notes__ |
| 794 | + else: |
| 795 | + raise AssertionError("ImportError was not raised") |
| 796 | + """) |
| 797 | + assert_python_ok("-c", code) |
| 798 | + |
769 | 799 | def test_reification_retries_on_failure(self): |
770 | 800 | """Failed reification should allow retry on subsequent access. |
771 | 801 |
|
|
0 commit comments