Skip to content

Commit c3c2c37

Browse files
mikewadstendpgeorge
authored andcommitted
tests/basics: Add tests for type-checking subclassed exc instances.
1 parent fe2bc92 commit c3c2c37

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# test subclassing exceptions and providing __new__
2+
3+
4+
class Dummy(BaseException):
5+
pass
6+
7+
8+
class GoodException(BaseException):
9+
def __new__(cls, *args, **kwargs):
10+
print("GoodException __new__")
11+
return Dummy(*args, **kwargs)
12+
13+
14+
class BadException(BaseException):
15+
def __new__(cls, *args, **kwargs):
16+
print("BadException __new__")
17+
return 1
18+
19+
20+
try:
21+
raise GoodException("good message")
22+
except BaseException as good:
23+
print(type(good), good.args[0])
24+
25+
try:
26+
raise BadException("bad message")
27+
except Exception as bad:
28+
# Should be TypeError 'exceptions must derive from BaseException'
29+
print(type(bad), bad.args[0])
30+
31+
try:
32+
33+
def gen():
34+
yield
35+
36+
gen().throw(BadException)
37+
except Exception as genbad:
38+
# Should be TypeError 'exceptions must derive from BaseException'
39+
print(type(genbad), genbad.args[0])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
GoodException __new__
2+
<class 'Dummy'> good message
3+
BadException __new__
4+
<class 'TypeError'> exceptions must derive from BaseException
5+
BadException __new__
6+
<class 'TypeError'> exceptions must derive from BaseException

0 commit comments

Comments
 (0)