From 13f8c0dcfa3fad8548973f66e19b94efd7bc2e3c Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Fri, 5 Sep 2025 23:19:26 +0800 Subject: [PATCH 1/8] fix: error message is wrong when inspect a custom module Signed-off-by: yihong0618 --- Lib/inspect.py | 6 +++++- Lib/test/test_inspect/test_inspect.py | 7 +++++++ .../Library/2025-09-05-23-18-51.gh-issue-138542.hQ9Q04.rst | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-09-05-23-18-51.gh-issue-138542.hQ9Q04.rst diff --git a/Lib/inspect.py b/Lib/inspect.py index 5a46987b78b437..baecbd2778bddb 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -819,7 +819,11 @@ def getfile(object): if ismodule(object): if getattr(object, '__file__', None): return object.__file__ - raise TypeError('{!r} is a built-in module'.format(object)) + if object.__spec__ is not None: + raise TypeError('{!r} is a built-in module'.format(object)) + else: + raise TypeError('Custom module: {!r} is can not get source' + .format(object)) if isclass(object): if hasattr(object, '__module__'): module = sys.modules.get(object.__module__) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 88fa4b7460c412..6df996e1ae4ff3 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -811,6 +811,13 @@ def test_getfile_builtin_module(self): inspect.getfile(sys) self.assertStartsWith(str(e.exception), ' Date: Fri, 5 Sep 2025 23:52:25 +0800 Subject: [PATCH 2/8] Update Lib/inspect.py Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- Lib/inspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index baecbd2778bddb..4e5c8fa51b6a8d 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -822,7 +822,7 @@ def getfile(object): if object.__spec__ is not None: raise TypeError('{!r} is a built-in module'.format(object)) else: - raise TypeError('Custom module: {!r} is can not get source' + raise TypeError('Custom module: {!r} cannot get source' .format(object)) if isclass(object): if hasattr(object, '__module__'): From a1402b9f59f3689bc2605f02e0353023bc27f7ae Mon Sep 17 00:00:00 2001 From: yihong Date: Sun, 7 Sep 2025 07:38:00 +0800 Subject: [PATCH 3/8] Apply suggestions from code review Co-authored-by: sobolevn --- Lib/inspect.py | 6 ++---- Lib/test/test_inspect/test_inspect.py | 8 +++++--- .../2025-09-05-23-18-51.gh-issue-138542.hQ9Q04.rst | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 4e5c8fa51b6a8d..8a67b48d2f64f6 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -820,10 +820,8 @@ def getfile(object): if getattr(object, '__file__', None): return object.__file__ if object.__spec__ is not None: - raise TypeError('{!r} is a built-in module'.format(object)) - else: - raise TypeError('Custom module: {!r} cannot get source' - .format(object)) + raise TypeError(f'{object!r} is a built-in module') + raise TypeError(f'Custom module: {object!r} cannot get source') if isclass(object): if hasattr(object, '__module__'): module = sys.modules.get(object.__module__) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 6df996e1ae4ff3..4887089306d695 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -812,11 +812,13 @@ def test_getfile_builtin_module(self): self.assertStartsWith(str(e.exception), ' Date: Sun, 7 Sep 2025 18:04:14 +0800 Subject: [PATCH 4/8] Update Lib/inspect.py Co-authored-by: sobolevn --- Lib/inspect.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 8a67b48d2f64f6..84795ab9bf396c 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -819,9 +819,9 @@ def getfile(object): if ismodule(object): if getattr(object, '__file__', None): return object.__file__ - if object.__spec__ is not None: + if getattr(object, '__spec__', None) is not None: raise TypeError(f'{object!r} is a built-in module') - raise TypeError(f'Custom module: {object!r} cannot get source') + raise TypeError(f'Cannot get source from {object!r}') if isclass(object): if hasattr(object, '__module__'): module = sys.modules.get(object.__module__) From a9bdc589b0dd0a7d75f29416ade6326b761675fe Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Sun, 7 Sep 2025 18:29:46 +0800 Subject: [PATCH 5/8] fix: tests Signed-off-by: yihong0618 --- Lib/test/test_inspect/test_inspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 4887089306d695..211a6a34f2603b 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -815,7 +815,7 @@ def test_getfile_custom_module(self): import re custom_module = types.ModuleType('custom_module') msg = re.escape( - f'Custom module: {custom_module!r} cannot get source' + f'Cannot get source from {custom_module!r}' ) with self.assertRaisesRegex(TypeError, msg) as e: inspect.getfile(custom_module) From ccd79d6983dad6fc5390b900a8040000e096ef6b Mon Sep 17 00:00:00 2001 From: yihong Date: Sun, 7 Sep 2025 18:57:30 +0800 Subject: [PATCH 6/8] Update Lib/inspect.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/inspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 84795ab9bf396c..4bd59d512c0159 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -821,7 +821,7 @@ def getfile(object): return object.__file__ if getattr(object, '__spec__', None) is not None: raise TypeError(f'{object!r} is a built-in module') - raise TypeError(f'Cannot get source from {object!r}') + raise TypeError(f'cannot get source from {object!r}') if isclass(object): if hasattr(object, '__module__'): module = sys.modules.get(object.__module__) From a8af4644bb76348c7fd91679b07764ebbdf30242 Mon Sep 17 00:00:00 2001 From: yihong Date: Sun, 7 Sep 2025 19:05:57 +0800 Subject: [PATCH 7/8] Update Lib/test/test_inspect/test_inspect.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_inspect/test_inspect.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 211a6a34f2603b..94f5431a3dd7f8 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -812,13 +812,10 @@ def test_getfile_builtin_module(self): self.assertStartsWith(str(e.exception), ' Date: Sun, 7 Sep 2025 19:11:35 +0800 Subject: [PATCH 8/8] fix: move re to the top Signed-off-by: yihong0618 --- Lib/test/test_inspect/test_inspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 94f5431a3dd7f8..8e1efe60a0ecab 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -15,6 +15,7 @@ from os.path import normcase import _pickle import pickle +import re import shutil import stat import sys @@ -6160,7 +6161,6 @@ def test_pwd_module_has_signatures(self): self._test_module_has_signatures(pwd) def test_re_module_has_signatures(self): - import re methods_no_signature = {'Match': {'group'}} self._test_module_has_signatures(re, methods_no_signature=methods_no_signature,