#6905: use better exception messages in inspect when the argument is of the wrong type.

This commit is contained in:
Georg Brandl 2009-09-18 09:14:52 +00:00
parent 4d94d31b6d
commit 097f70881a
1 changed files with 7 additions and 7 deletions

View File

@ -402,12 +402,12 @@ def getfile(object):
if ismodule(object): if ismodule(object):
if hasattr(object, '__file__'): if hasattr(object, '__file__'):
return object.__file__ return object.__file__
raise TypeError('arg is a built-in module') raise TypeError('%r is a built-in module' % object)
if isclass(object): if isclass(object):
object = sys.modules.get(object.__module__) object = sys.modules.get(object.__module__)
if hasattr(object, '__file__'): if hasattr(object, '__file__'):
return object.__file__ return object.__file__
raise TypeError('arg is a built-in class') raise TypeError('%r is a built-in class' % object)
if ismethod(object): if ismethod(object):
object = object.im_func object = object.im_func
if isfunction(object): if isfunction(object):
@ -418,8 +418,8 @@ def getfile(object):
object = object.f_code object = object.f_code
if iscode(object): if iscode(object):
return object.co_filename return object.co_filename
raise TypeError('arg is not a module, class, method, ' raise TypeError('%r is not a module, class, method, '
'function, traceback, frame, or code object') 'function, traceback, frame, or code object' % object)
ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type') ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
@ -741,7 +741,7 @@ def getargs(co):
'varargs' and 'varkw' are the names of the * and ** arguments or None.""" 'varargs' and 'varkw' are the names of the * and ** arguments or None."""
if not iscode(co): if not iscode(co):
raise TypeError('arg is not a code object') raise TypeError('%r is not a code object' % co)
nargs = co.co_argcount nargs = co.co_argcount
names = co.co_varnames names = co.co_varnames
@ -805,7 +805,7 @@ def getargspec(func):
if ismethod(func): if ismethod(func):
func = func.im_func func = func.im_func
if not isfunction(func): if not isfunction(func):
raise TypeError('arg is not a Python function') raise TypeError('%r is not a Python function' % func)
args, varargs, varkw = getargs(func.func_code) args, varargs, varkw = getargs(func.func_code)
return ArgSpec(args, varargs, varkw, func.func_defaults) return ArgSpec(args, varargs, varkw, func.func_defaults)
@ -902,7 +902,7 @@ def getframeinfo(frame, context=1):
else: else:
lineno = frame.f_lineno lineno = frame.f_lineno
if not isframe(frame): if not isframe(frame):
raise TypeError('arg is not a frame or traceback object') raise TypeError('%r is not a frame or traceback object' % frame)
filename = getsourcefile(frame) or getfile(frame) filename = getsourcefile(frame) or getfile(frame)
if context > 0: if context > 0: