Created
March 26, 2020 11:25
-
-
Save no1xsyzy/29cefc675b764b6cbf871c9b0d0800e6 to your computer and use it in GitHub Desktop.
Just tired with seemingly infinite try ... finally or with ... as.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# WTFPL | |
import functools | |
class Defers: | |
def __init__(self): | |
self._defereds = [] | |
def add_defer(self, defer_func): | |
self._defereds.append(defer_func) | |
def aswith(self, enterable, *, fallback=None): | |
if fallback is None: | |
f = enterable.__enter__() | |
else: | |
try: | |
f = enterable.__enter__() | |
# pylint: disable=broad-except | |
except Exception: | |
f = fallback() | |
self.add_defer(enterable.__exit__) | |
return f | |
def call_all_defers(self): | |
for defered in self._defereds: | |
defered() | |
def __enter__(self): | |
return self | |
def __exit__(self, type_, value, tb): | |
self.call_all_defers() | |
def open(self, *args, **kwargs): | |
return self.aswith(open(*args, **kwargs)) | |
def defers(self, func): | |
self.add_defer(func) | |
return func |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with Defers() as d: | |
f1 = open("file1") | |
@d.defers | |
def finally_block(): | |
f1.close() | |
f2 = d.aswith(open("file2")) | |
f3 = d.open("file3") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment