Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,13 @@ def test_getfile_builtin_module(self):
inspect.getfile(sys)
self.assertStartsWith(str(e.exception), '<module')

def test_getfile_custom_module(self):
import types
custom_module = types.ModuleType('custom_module')
with self.assertRaises(TypeError) as e:
inspect.getfile(custom_module)
self.assertStartsWith(str(e.exception), 'Custom module')

def test_getfile_builtin_class(self):
with self.assertRaises(TypeError) as e:
inspect.getfile(int)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
the error message when when inspect getfile a custom module
Loading