From 38e9815fd60a85bc5781fce75c407f7950b4fc47 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 11 Dec 2022 14:32:31 -0800 Subject: [PATCH 1/3] stubtest: catch BaseException on imports This came up in https://github.com/python/typeshed/pull/9347 --- mypy/stubtest.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 5e39b996076b..50858cf65e15 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -205,7 +205,7 @@ def test_module(module_name: str) -> Iterator[Error]: try: runtime = silent_import_module(module_name) - except Exception as e: + except BaseException as e: yield Error([module_name], f"failed to import, {type(e).__name__}: {e}", stub, MISSING) return @@ -1495,13 +1495,14 @@ def build_stubs(modules: list[str], options: Options, find_submodules: bool = Fa # find submodules via pkgutil try: runtime = silent_import_module(module) + except BaseException: + pass + else: all_modules.extend( m.name for m in pkgutil.walk_packages(runtime.__path__, runtime.__name__ + ".") if m.name not in all_modules ) - except Exception: - pass if sources: try: From b0fed126cc673564741c1c33cc7e9c4e91419804 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 11 Dec 2022 14:46:27 -0800 Subject: [PATCH 2/3] raise on keyboardinterrupt --- mypy/stubtest.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 50858cf65e15..acecc871c480 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -205,6 +205,8 @@ def test_module(module_name: str) -> Iterator[Error]: try: runtime = silent_import_module(module_name) + except KeyboardInterrupt: + raise except BaseException as e: yield Error([module_name], f"failed to import, {type(e).__name__}: {e}", stub, MISSING) return @@ -1495,6 +1497,8 @@ def build_stubs(modules: list[str], options: Options, find_submodules: bool = Fa # find submodules via pkgutil try: runtime = silent_import_module(module) + except KeyboardInterrupt: + raise except BaseException: pass else: From c1eb4c9d6c9ff66ee1ae4a27eed5b2023c0f081d Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 11 Dec 2022 15:30:18 -0800 Subject: [PATCH 3/3] fix tests --- mypy/stubtest.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index acecc871c480..0f8df607858f 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -1497,16 +1497,15 @@ def build_stubs(modules: list[str], options: Options, find_submodules: bool = Fa # find submodules via pkgutil try: runtime = silent_import_module(module) - except KeyboardInterrupt: - raise - except BaseException: - pass - else: all_modules.extend( m.name for m in pkgutil.walk_packages(runtime.__path__, runtime.__name__ + ".") if m.name not in all_modules ) + except KeyboardInterrupt: + raise + except BaseException: + pass if sources: try: