Commit 7ed47ec3 authored by Armin Rigo's avatar Armin Rigo
Browse files

Issue #3109

Improve the error message we get for 'class X(random_object)'
parent 4748c02be810
......@@ -310,6 +310,9 @@ class OperationError(Exception):
return space.w_None
return tb
def got_any_traceback(self):
return self._application_traceback is not None
def set_traceback(self, traceback):
"""Set the current traceback."""
self._application_traceback = traceback
......
......@@ -832,8 +832,20 @@ class __extend__(pyframe.PyFrame):
w_metaclass = find_metaclass(self.space, w_bases,
w_methodsdict, self.get_w_globals(),
self.get_builtin())
w_newclass = self.space.call_function(w_metaclass, w_name,
w_bases, w_methodsdict)
try:
w_newclass = self.space.call_function(w_metaclass, w_name,
w_bases, w_methodsdict)
except OperationError as e:
# give a more comprehensible error message for TypeErrors
if e.got_any_traceback():
raise
if not e.match(self.space, self.space.w_TypeError):
raise
raise oefmt(self.space.w_TypeError,
"metaclass found to be '%N', but calling %R "
"with args (%R, %R, dict) raised %R",
w_metaclass, w_metaclass, w_name, w_bases,
e.get_w_value(self.space))
self.pushvalue(w_newclass)
def STORE_NAME(self, varindex, next_instr):
......
......@@ -131,3 +131,19 @@ class AppTestClass:
c = C()
assert c.one == "two"
raises(AttributeError, getattr, c, "two")
def test_nonsensical_base_error_message(self):
with raises(TypeError) as exc:
class Foo('base'):
pass
assert str(exc.value).startswith(
"metaclass found to be 'str', but calling <type 'str'> "
"with args ('Foo', ('base',), dict) raised ")
#
def foo_func(): pass
with raises(TypeError) as exc:
class Foo(foo_func):
pass
assert str(exc.value).startswith(
"metaclass found to be 'function', but calling <type 'function'> "
"with args ('Foo', (<function foo_func at ")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment