summaryrefslogtreecommitdiffstatshomepage
path: root/tests
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-03-29 14:00:03 +0000
committerDamien George <damien.p.george@gmail.com>2014-03-29 14:00:03 +0000
commit21a07dc50f764b72e6cb1d109160b17545db70ba (patch)
tree2e61ff9a1d869ff75431a5858a7bafb5095e4789 /tests
parentb04be056fe7694cdfe2df4c5103546a39d4fd8b2 (diff)
parente7286ef2c7db77879178f45f6055d8af2fc68281 (diff)
downloadmicropython-21a07dc50f764b72e6cb1d109160b17545db70ba.tar.gz
micropython-21a07dc50f764b72e6cb1d109160b17545db70ba.zip
Merge pull request #389 from pfalcon/with-statement
With statement implementation
Diffstat (limited to 'tests')
-rw-r--r--tests/basics/with-break.py14
-rw-r--r--tests/basics/with-continue.py14
-rw-r--r--tests/basics/with-return.py14
-rw-r--r--tests/basics/with1.py71
4 files changed, 113 insertions, 0 deletions
diff --git a/tests/basics/with-break.py b/tests/basics/with-break.py
new file mode 100644
index 0000000000..f1063d5826
--- /dev/null
+++ b/tests/basics/with-break.py
@@ -0,0 +1,14 @@
+class CtxMgr:
+
+ def __enter__(self):
+ print("__enter__")
+ return self
+
+ def __exit__(self, a, b, c):
+ print("__exit__", repr(a), repr(b))
+
+for i in range(5):
+ print(i)
+ with CtxMgr():
+ if i == 3:
+ break
diff --git a/tests/basics/with-continue.py b/tests/basics/with-continue.py
new file mode 100644
index 0000000000..fc2b24bd4b
--- /dev/null
+++ b/tests/basics/with-continue.py
@@ -0,0 +1,14 @@
+class CtxMgr:
+
+ def __enter__(self):
+ print("__enter__")
+ return self
+
+ def __exit__(self, a, b, c):
+ print("__exit__", repr(a), repr(b))
+
+for i in range(5):
+ print(i)
+ with CtxMgr():
+ if i == 3:
+ continue
diff --git a/tests/basics/with-return.py b/tests/basics/with-return.py
new file mode 100644
index 0000000000..cb0135c8b3
--- /dev/null
+++ b/tests/basics/with-return.py
@@ -0,0 +1,14 @@
+class CtxMgr:
+
+ def __enter__(self):
+ print("__enter__")
+ return self
+
+ def __exit__(self, a, b, c):
+ print("__exit__", repr(a), repr(b))
+
+def foo():
+ with CtxMgr():
+ return 4
+
+print(foo())
diff --git a/tests/basics/with1.py b/tests/basics/with1.py
new file mode 100644
index 0000000000..3db1d380d4
--- /dev/null
+++ b/tests/basics/with1.py
@@ -0,0 +1,71 @@
+class CtxMgr:
+
+ def __enter__(self):
+ print("__enter__")
+ return self
+
+ def __exit__(self, a, b, c):
+ print("__exit__", repr(a), repr(b))
+
+
+with CtxMgr() as a:
+ print(isinstance(a, CtxMgr))
+
+try:
+ with CtxMgr() as a:
+ raise ValueError
+except ValueError:
+ print("ValueError")
+
+
+class CtxMgr2:
+
+ def __enter__(self):
+ print("__enter__")
+ return self
+
+ def __exit__(self, a, b, c):
+ print("__exit__", repr(a), repr(b))
+ return True
+
+try:
+ with CtxMgr2() as a:
+ raise ValueError
+ print("No ValueError2")
+except ValueError:
+ print("ValueError2")
+
+
+# These recursive try-finally tests are attempt to get some interpretation
+# of last phrase in http://docs.python.org/3.4/library/dis.html#opcode-WITH_CLEANUP
+# "If the stack represents an exception, and the function call returns a ‘true’
+# value, this information is “zapped” and replaced with a single WHY_SILENCED
+# to prevent END_FINALLY from re-raising the exception. (But non-local gotos
+# will still be resumed.)"
+print("===")
+with CtxMgr2() as a:
+ try:
+ try:
+ raise ValueError
+ print("No ValueError3")
+ finally:
+ print("finally1")
+ finally:
+ print("finally2")
+
+print("===")
+try:
+ try:
+ with CtxMgr2() as a:
+ try:
+ try:
+ raise ValueError
+ print("No ValueError3")
+ finally:
+ print("finally1")
+ finally:
+ print("finally2")
+ finally:
+ print("finally3")
+finally:
+ print("finally4")