diff --git a/.gitignore b/.gitignore index 975664a..08a6d86 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ __pycache__/ *$py.class #var is weird -test.py +test*.py .vscode/ # C extensions diff --git a/Github/objects.py b/Github/objects.py index 4a83018..d298ca4 100644 --- a/Github/objects.py +++ b/Github/objects.py @@ -8,6 +8,8 @@ if TYPE_CHECKING: from datetime import datetime import io +import os +from contextlib import nullcontext __all__ = ( 'APIObject', @@ -220,11 +222,17 @@ class File: def read(self) -> str: if isinstance(self.fp, str): - with open(self.fp) as fp: - data = fp.read() - return data + if os.path.exists(self.fp): + with open(self.fp) as fp: + data = fp.read() + return data + return self.fp + elif isinstance(self.fp, io.BytesIO): + return self.fp.read().decode('utf-8') + elif isinstance(self.fp, io.StringIO): + return self.fp.getvalue() else: - return self.fp.read() + raise TypeError(f'Expected str, io.StringIO, or io.BytesIO, got {type(self.fp)}') class Gist(APIObject): __slots__ = (