Skip to content

Commit 349b0b7

Browse files
committed
feat: add result.__enter__ and result.__exit__ dunder methods
1 parent ca7dde2 commit 349b0b7

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

ResultContainer/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,25 @@ def __hash__(self) -> int:
13961396
return hash(self._val) # Hash the value stored in ok
13971397
return hash(self.str())
13981398

1399+
def __enter__(self): # Called when entering a with block.
1400+
if not self._success:
1401+
return self
1402+
self._val = self._val.__enter__()
1403+
return self
1404+
1405+
def __exit__(self, exc_type, exc_value, tb): # Called when exiting a with block.
1406+
if self._val.__exit__(exc_type, exc_value, traceback):
1407+
# tb = traceback.format_tb(tb)
1408+
# tb = [line for line in tb if not any(exclude in line for exclude in TRACEBACK_EXCLUDE_FILES)]
1409+
# tb.pop()
1410+
self.add_Err_msg(f"with block {exc_type} Exception.", add_traceback=False)
1411+
# self._val.traceback_info.pop()
1412+
# self._val.traceback_info.append(tb)
1413+
raise self._val from exc_value
1414+
return False
1415+
1416+
self._val.__exit__(exc_type, exc_value, traceback)
1417+
13991418
def __contains__(self, value):
14001419
if isinstance(value, Result):
14011420
value = value.unwrap()

0 commit comments

Comments
 (0)