summaryrefslogtreecommitdiffstatshomepage
path: root/tests
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-12-24 12:47:39 +0000
committerDamien George <damien.p.george@gmail.com>2015-12-24 12:47:39 +0000
commit8047340d7532ec32bc9f2d603bffc0bc9544297f (patch)
tree2a1eced095349199a049e82f0c9a080de13fb9b3 /tests
parent117158fcd560a84318a0b9cb5332c5599acdb84b (diff)
downloadmicropython-8047340d7532ec32bc9f2d603bffc0bc9544297f.tar.gz
micropython-8047340d7532ec32bc9f2d603bffc0bc9544297f.zip
py: Handle case of return within the finally block of try-finally.
Addresses issue #1636.
Diffstat (limited to 'tests')
-rw-r--r--tests/basics/try_finally_return2.py104
1 files changed, 104 insertions, 0 deletions
diff --git a/tests/basics/try_finally_return2.py b/tests/basics/try_finally_return2.py
new file mode 100644
index 0000000000..e3ea5f1eb8
--- /dev/null
+++ b/tests/basics/try_finally_return2.py
@@ -0,0 +1,104 @@
+# test 'return' within the finally block
+# it should swallow the exception
+
+# simple case
+def f():
+ try:
+ raise ValueError()
+ finally:
+ print('finally')
+ return 0
+ print('got here')
+print(f())
+
+# nested, return in outer
+def f():
+ try:
+ try:
+ raise ValueError
+ finally:
+ print('finally 1')
+ print('got here')
+ finally:
+ print('finally 2')
+ return 2
+ print('got here')
+print(f())
+
+# nested, return in inner
+def f():
+ try:
+ try:
+ raise ValueError
+ finally:
+ print('finally 1')
+ return 1
+ print('got here')
+ finally:
+ print('finally 2')
+ print('got here')
+print(f())
+
+# nested, return in inner and outer
+def f():
+ try:
+ try:
+ raise ValueError
+ finally:
+ print('finally 1')
+ return 1
+ print('got here')
+ finally:
+ print('finally 2')
+ return 2
+ print('got here')
+print(f())
+
+# nested with reraise
+def f():
+ try:
+ try:
+ raise ValueError
+ except:
+ raise
+ print('got here')
+ finally:
+ print('finally')
+ return 0
+ print('got here')
+print(f())
+
+# triple nesting with reraise
+def f():
+ try:
+ try:
+ try:
+ raise ValueError
+ except:
+ raise
+ except:
+ raise
+ finally:
+ print('finally')
+ return 0
+print(f())
+
+# exception when matching exception
+def f():
+ try:
+ raise ValueError
+ except NonExistingError:
+ pass
+ finally:
+ print('finally')
+ return 0
+print(f())
+
+# raising exception class, not instance
+def f():
+ try:
+ raise ValueError
+ finally:
+ print('finally')
+ return 0
+print(f())