Skip to content

Commit 04038ec

Browse files
committed
feat: allow context mngr interface for File obj
1 parent 2239f58 commit 04038ec

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

interactions/models/discord/file.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def open_file(self) -> BinaryIO:
4545
else:
4646
return open(str(self.file), "rb")
4747

48+
def __enter__(self) -> "File":
49+
return self
50+
51+
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
52+
if isinstance(self.file, (IOBase, BinaryIO)):
53+
self.file.close()
4854

4955
UPLOADABLE_TYPE = Union[File, IOBase, BinaryIO, Path, str]
5056

0 commit comments

Comments
 (0)