Make tests not rely on 'exception' being an implicit name in 'except' blocks

This commit is contained in:
K. Lange 2023-11-25 11:11:56 +09:00
parent dd9b84258b
commit 01d377555a
13 changed files with 28 additions and 28 deletions

View File

@ -26,5 +26,5 @@ last(1,2,7,3)
last(1,2,test="thing")
try:
last(1,2,'c','d',7,8,extra='foo')
except:
except as exception:
print(exception.arg)

View File

@ -8,7 +8,7 @@ class Foo():
# This doesn't work because amethod is unbound and needs an instance.
try:
Foo.amethod()
except:
except as exception:
print(exception.arg)
# This works
Foo.amethod(Foo())

View File

@ -9,5 +9,5 @@ print("{a} {b} {c}".format(**{'a': 1, 'b': "apples", 'c': object}))
try:
print("{a} {b} {c}".format(a=True,**{'a': 1, 'b': "apples", 'c': object}))
except:
except as exception:
print(exception.arg)

View File

@ -120,7 +120,7 @@ def aMethod(with_,args=None):
try:
aMethod()
except:
except as exception:
print(exception.arg)
aMethod("just the first")
@ -128,11 +128,11 @@ aMethod("the first","and the second")
aMethod(args="hello",with_="world")
try:
aMethod(foo="hello",with_="bar")
except:
except as exception:
print(exception.arg) # unrecognized keyword arg, from inner method
aMethod(*[1,2])
try:
aMethod(*[1,2,3])
except:
except as exception:
print(exception.arg) # too many arguments

View File

@ -20,7 +20,7 @@ del l[3]
print(l) # [1, 3, 4]
try:
del l[3]
except:
except as exception:
print(exception.arg) # List index out of range
let o = object()
@ -36,13 +36,13 @@ print(dir(o.foo.bar))
print(o.foo.bar.qux)
try:
print(o.foo.bar.baz)
except:
except as exception:
print(exception.arg) # AttributeError
del o.foo.bar
print(dir(o.foo))
try:
print(o.foo.bar)
except:
except as exception:
print(exception.arg) # AttributeError
del o.foo
del o

View File

@ -2,7 +2,7 @@ def doTheThing(excp):
try:
try:
raise excp
except (TypeError, ValueError):
except (TypeError, ValueError) as exception:
print("Caught a", repr(exception))
for i in exception.traceback:
let func, instr = i

View File

@ -4,7 +4,7 @@ class Bar(object):
try:
let b = Bar()
except:
except as exception:
print(exception.arg)
class Foo():
@ -13,6 +13,6 @@ class Foo():
try:
let f = Foo()
except:
except as exception:
print(exception.arg)

View File

@ -7,10 +7,10 @@ if True:
try:
let a, b = range(3)
except:
except as exception:
print(repr(exception))
try:
let a, b, c, d = range(3)
except:
except as exception:
print(repr(exception))

View File

@ -9,22 +9,22 @@ function(1,2,keyword2=5)
try:
function(1,keyword2=5)
except:
except as exception:
print(exception.arg)
try:
function(1,2,positional1=4)
except:
except as exception:
print(exception.arg)
try:
function(1,2,keyword2=None,keyword2=5)
except:
except as exception:
print(exception.arg)
function(1,keyword2=4,positional2="abc")
try:
function(1,keyword2=4,keyword1=5,positional1="nope")
except:
except as exception:
print(exception.arg)

View File

@ -8,7 +8,7 @@ def testTop():
printPackage(foo)
try:
print(foo.bar, "Fail")
except:
except as exception:
print(repr(exception)) # AttributeError
def testCaching():
@ -33,7 +33,7 @@ def testFromImport():
print(baz.qux)
try:
print(foo, "Fail")
except:
except as exception:
print(repr(exception))
def testRenames():
@ -42,17 +42,17 @@ def testRenames():
print(blah.qux)
try:
print(foo, "Fail")
except:
except as exception:
print(repr(exception))
from foo.bar.baz import qux as thing
print(thing)
try:
print(qux, "Fail")
except:
except as exception:
print(repr(exception))
try:
print(foo.bar, "Fail")
except:
except as exception:
print(repr(exception))
if __name__ == '__main__':

View File

@ -2,7 +2,7 @@ import time
try:
time.sleep()
except:
except as exception:
print("oh no! " + exception.arg)
print("Back from try/except")

View File

@ -21,9 +21,9 @@ try:
try:
print("This is the inner level")
raise "This is the exception"
except:
except as exception:
print("This is the inner handler.")
raise exception
except:
except as exception:
print("This is the outer handler.")
print(exception)

View File

@ -7,7 +7,7 @@ for k,v in [(1,2),(3,4),(5,6)]:
try:
for k,v,z in [(1,2,7),(3,4,8),(5,6)]:
print(k,v,z)
except:
except as exception:
print(exception.__class__.__name__)
# 1 2 7
# 3 4 8
@ -16,7 +16,7 @@ except:
try:
for k,v in [1,2,3]:
print("type error")
except:
except as exception:
print(exception.__class__.__name__)
# TypeError