Skip to content

Commit f91b477

Browse files
committed
feat: add the ability to use WTFPythonMonkey as a Python context manager (with-statement)
1 parent c8075f2 commit f91b477

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

python/pythonmonkey/lib/wtfpm.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,27 @@ def printTimersDebugInfo():
1212
console.log(getDebugInfo())
1313
console.log(new Date())
1414
}""")(pm.createRequire(__file__))
15+
16+
class WTF:
17+
"""
18+
WTFPythonMonkey to use as a Python context manager (`with`-statement)
19+
20+
Usage:
21+
```py
22+
from pythonmonkey.lib.wtfpm import WTF
23+
24+
with WTF():
25+
# the main entry point for the program utilizes PythonMonkey event-loop
26+
asyncio.run(pythonmonkey_main())
27+
```
28+
"""
29+
def __enter__(self):
30+
pass
31+
def __exit__(self, errType, errValue, traceback):
32+
if errType is None: # no exception
33+
return
34+
elif issubclass(errType, KeyboardInterrupt): # except KeyboardInterrupt:
35+
printTimersDebugInfo()
36+
return True # exception suppressed
37+
else: # other exceptions
38+
return False

0 commit comments

Comments
 (0)