Skip to content

Commit 13f8c0d

Browse files
committed
fix: error message is wrong when inspect a custom module
Signed-off-by: yihong0618 <[email protected]>
1 parent 0c74fc8 commit 13f8c0d

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Lib/inspect.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,11 @@ def getfile(object):
819819
if ismodule(object):
820820
if getattr(object, '__file__', None):
821821
return object.__file__
822-
raise TypeError('{!r} is a built-in module'.format(object))
822+
if object.__spec__ is not None:
823+
raise TypeError('{!r} is a built-in module'.format(object))
824+
else:
825+
raise TypeError('Custom module: {!r} is can not get source'
826+
.format(object))
823827
if isclass(object):
824828
if hasattr(object, '__module__'):
825829
module = sys.modules.get(object.__module__)

Lib/test/test_inspect/test_inspect.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,13 @@ def test_getfile_builtin_module(self):
811811
inspect.getfile(sys)
812812
self.assertStartsWith(str(e.exception), '<module')
813813

814+
def test_getfile_custom_module(self):
815+
import types
816+
custom_module = types.ModuleType('custom_module')
817+
with self.assertRaises(TypeError) as e:
818+
inspect.getfile(custom_module)
819+
self.assertStartsWith(str(e.exception), 'Custom module')
820+
814821
def test_getfile_builtin_class(self):
815822
with self.assertRaises(TypeError) as e:
816823
inspect.getfile(int)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
the error message when when inspect getfile a custom module

0 commit comments

Comments
 (0)