Commit 153ac11a authored by Kevin Modzelewski's avatar Kevin Modzelewski

Some crazy exception tests

parent 5b407e21
# expected: fail
# - descriptors not implemented yet
class D(object):
def __get__(self, instance, owner):
print "get", instance, owner
return 1
def f1():
class D(object):
def __get__(self, instance, owner):
print "get", instance, owner
return 1
def __set__(self, instance, value):
print "set", instance, value
def __set__(self, instance, value):
print "set", instance, value
class C(object):
d = D()
class C(object):
d = D()
print C.d
print C().d
print C.d
print C().d
c = C()
c.d = 2
print c.d
c = C()
c.d = 2
print c.d
f1()
def f2():
class MaybeDescriptorMeta(type):
def __getattribute__(self, attr):
print "meta __getattribute__", attr
return 2
class MaybeDescriptor(object):
__metaclass__ = MaybeDescriptorMeta
def __getattribute__(self, attr):
print "__getattribute__", attr
return 1
class HasDescriptor(object):
x = MaybeDescriptor()
hd = HasDescriptor()
# Getting hd.x will look up type(hd.__dict__[x]).__get__
# and not go through __getattribute__
print hd.x
print hd.x.__get__
print type(hd.x).__get__
# But we can still set it here:
def get(*args):
print "get", args
return 3
hd.x.__get__ = get
print hd.x
type(hd.x).__get__ = get
print hd.x
f2()
# expected: fail
# - exceptions
def throw(x):
try:
raise x
except Exception, e:
print type(e)
# Both print "Exception"
throw(Exception)
throw(Exception())
# expected: fail
# - exceptions
# Different ways of nesting exceptions
import sys
def f1():
print
print "f1"
# First make sure that xrange iterator really does raise StopIteration:
try:
iter(xrange(0)).next()
assert 0
except StopIteration:
print sys.exc_info()[0].__name__
f1()
def f2():
print
print "f2"
try:
raise Exception
except Exception:
print sys.exc_info()[0].__name__ # "Exception"
for i in xrange(5):
pass
print sys.exc_info()[0].__name__ # "Exception", despite the implicit StopIteration that got raised
def f():
try:
raise StopIteration()
except:
pass
f()
print sys.exc_info()[0].__name__ # still "Exception"
def f2():
raise StopIteration()
def f3():
try:
f2()
except StopIteration:
pass
try:
f3()
except:
pass
print sys.exc_info()[0].__name__ # still "Exception", since the exception didn't get back up to this frame
try:
f2()
except:
pass
print sys.exc_info()[0].__name__ # "StopIteration"
f2()
def f2_2():
try:
raise Exception
except Exception:
# These two look similar, but they have different
# exception-setting behavior:
for n in xrange(5):
print n, sys.exc_info()[0].__name__
it = iter(xrange(5))
while True:
try:
n = it.next()
except StopIteration:
break
print n, sys.exc_info()[0].__name__
print "done", n, sys.exc_info()[0].__name__
f2_2()
def f3():
print
print "f3"
def f():
print "getting the exc handler type"
raise AssertionError()
try:
print "in the first try"
# f() won't get evaluated until the exception is actually thrown:
try:
print "in the second try"
raise Exception()
except f():
print "In the inner exception block??"
finally:
# This will get called even though there was an exception in
# evaluating the exception-handler type:
print "inner finally"
except Exception:
# This will print "AssertionError", from the f() call, *not* the Exception
# that was thrown in the inner try block.
print "In the outer exception block:", sys.exc_info()[0].__name__
finally:
print "outer finally"
f3()
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