Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r59093:5a4ef2893376
Date: 2012-11-27 10:30 -0800
http://bitbucket.org/pypy/pypy/changeset/5a4ef2893376/
Log: TypeError when catching non BaseExceptions
diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -18,6 +18,9 @@
from pypy.tool.stdlib_opcode import (bytecode_spec,
unrolling_all_opcode_descs)
+CANNOT_CATCH_MSG = ("catching classes that don't inherit from BaseException "
+ "is not allowed in 3.x")
+
def unaryoperation(operationname):
"""NOT_RPYTHON"""
def opimpl(self, *ignored):
@@ -746,17 +749,16 @@
@jit.unroll_safe
def cmp_exc_match(self, w_1, w_2):
- if self.space.is_true(self.space.isinstance(w_2, self.space.w_tuple)):
- for w_t in self.space.fixedview(w_2):
- if self.space.is_true(self.space.isinstance(w_t,
- self.space.w_str)):
- self.space.warn("catching of string exceptions is "
- "deprecated",
- self.space.w_DeprecationWarning)
- elif self.space.is_true(self.space.isinstance(w_2, self.space.w_str)):
- self.space.warn("catching of string exceptions is deprecated",
- self.space.w_DeprecationWarning)
- return self.space.newbool(self.space.exception_match(w_1, w_2))
+ space = self.space
+ if space.isinstance_w(w_2, space.w_tuple):
+ for w_type in space.fixedview(w_2):
+ if not space.exception_is_valid_class_w(w_type):
+ raise OperationError(space.w_TypeError,
+ space.wrap(CANNOT_CATCH_MSG))
+ elif not space.exception_is_valid_class_w(w_2):
+ raise OperationError(space.w_TypeError,
+ space.wrap(CANNOT_CATCH_MSG))
+ return space.newbool(space.exception_match(w_1, w_2))
def COMPARE_OP(self, testnum, next_instr):
w_2 = self.popvalue()
diff --git a/pypy/interpreter/test/test_raise.py
b/pypy/interpreter/test/test_raise.py
--- a/pypy/interpreter/test/test_raise.py
+++ b/pypy/interpreter/test/test_raise.py
@@ -227,6 +227,22 @@
raise A(42)
raises(TypeError, f)
+ def test_userclass_catch(self):
+ # classes can't be caught unless they inherit from BaseException
+ class A(object):
+ pass
+
+ for exc in A, (ZeroDivisionError, A):
+ try:
+ try:
+ 1 / 0
+ except exc:
+ pass
+ except TypeError:
+ pass
+ else:
+ fail('Expected TypeError')
+
def test_it(self):
class C:
pass
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit