From 01d377555a7bf4acdd0592da9a5aabf4b5d07090 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sat, 25 Nov 2023 11:11:56 +0900 Subject: [PATCH] Make tests not rely on 'exception' being an implicit name in 'except' blocks --- test/testArgsKwargs.krk | 2 +- test/testClassMethod.krk | 2 +- test/testComplexArgsNative.krk | 2 +- test/testDecorators.krk | 6 +++--- test/testDel.krk | 6 +++--- test/testExceptionTracebacks.krk | 2 +- test/testImplicitObjectBase.krk | 4 ++-- test/testIteratorUnpack.krk | 4 ++-- test/testKeywordArgs.krk | 8 ++++---- test/testPackageImports.krk | 10 +++++----- test/testRuntimeException.krk | 2 +- test/testTryExcept.krk | 4 ++-- test/testUnpackIter.krk | 4 ++-- 13 files changed, 28 insertions(+), 28 deletions(-) diff --git a/test/testArgsKwargs.krk b/test/testArgsKwargs.krk index 466da66..edd6cb7 100644 --- a/test/testArgsKwargs.krk +++ b/test/testArgsKwargs.krk @@ -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) diff --git a/test/testClassMethod.krk b/test/testClassMethod.krk index 82fb2a7..190a7f3 100644 --- a/test/testClassMethod.krk +++ b/test/testClassMethod.krk @@ -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()) diff --git a/test/testComplexArgsNative.krk b/test/testComplexArgsNative.krk index dc65a21..f01cc9a 100644 --- a/test/testComplexArgsNative.krk +++ b/test/testComplexArgsNative.krk @@ -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) diff --git a/test/testDecorators.krk b/test/testDecorators.krk index 3e1cb68..aef416a 100644 --- a/test/testDecorators.krk +++ b/test/testDecorators.krk @@ -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 diff --git a/test/testDel.krk b/test/testDel.krk index 117954b..3f8b3d2 100644 --- a/test/testDel.krk +++ b/test/testDel.krk @@ -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 diff --git a/test/testExceptionTracebacks.krk b/test/testExceptionTracebacks.krk index b87a017..9da2499 100644 --- a/test/testExceptionTracebacks.krk +++ b/test/testExceptionTracebacks.krk @@ -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 diff --git a/test/testImplicitObjectBase.krk b/test/testImplicitObjectBase.krk index 97dd314..9eb60b6 100644 --- a/test/testImplicitObjectBase.krk +++ b/test/testImplicitObjectBase.krk @@ -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) diff --git a/test/testIteratorUnpack.krk b/test/testIteratorUnpack.krk index 8470afd..2852fb6 100644 --- a/test/testIteratorUnpack.krk +++ b/test/testIteratorUnpack.krk @@ -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)) diff --git a/test/testKeywordArgs.krk b/test/testKeywordArgs.krk index d0af6ab..7ec2f1f 100644 --- a/test/testKeywordArgs.krk +++ b/test/testKeywordArgs.krk @@ -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) diff --git a/test/testPackageImports.krk b/test/testPackageImports.krk index be428b3..b74c841 100644 --- a/test/testPackageImports.krk +++ b/test/testPackageImports.krk @@ -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__': diff --git a/test/testRuntimeException.krk b/test/testRuntimeException.krk index 8a70148..dc85c13 100644 --- a/test/testRuntimeException.krk +++ b/test/testRuntimeException.krk @@ -2,7 +2,7 @@ import time try: time.sleep() -except: +except as exception: print("oh no! " + exception.arg) print("Back from try/except") diff --git a/test/testTryExcept.krk b/test/testTryExcept.krk index 020ffe0..726b1c4 100644 --- a/test/testTryExcept.krk +++ b/test/testTryExcept.krk @@ -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) diff --git a/test/testUnpackIter.krk b/test/testUnpackIter.krk index 789669d..0e18ffa 100644 --- a/test/testUnpackIter.krk +++ b/test/testUnpackIter.krk @@ -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