Commit 8a9d2015 authored by Jeremy Reizenstein's avatar Jeremy Reizenstein Committed by Facebook GitHub Bot
Browse files

Avoid contextlib.nullcontext for Python 3.6 compatibility

Summary: As mentioned in a comment on https://github.com/facebookresearch/pytorch3d/issues/77, we can't use this function in Python 3.6. It's easy to write our own version.

Reviewed By: gkioxari

Differential Revision: D24249915

fbshipit-source-id: 4c70a3efb03daa115041d082e616511297eab8fa
parent f5383a7e
...@@ -11,6 +11,14 @@ from fvcore.common.file_io import PathManager ...@@ -11,6 +11,14 @@ from fvcore.common.file_io import PathManager
from PIL import Image from PIL import Image
@contextlib.contextmanager
def nullcontext(x):
"""
This is just like contextlib.nullcontext but also works in Python 3.6.
"""
yield x
def _open_file(f, mode="r") -> ContextManager[IO]: def _open_file(f, mode="r") -> ContextManager[IO]:
if isinstance(f, str): if isinstance(f, str):
f = open(f, mode) f = open(f, mode)
...@@ -19,7 +27,7 @@ def _open_file(f, mode="r") -> ContextManager[IO]: ...@@ -19,7 +27,7 @@ def _open_file(f, mode="r") -> ContextManager[IO]:
f = f.open(mode) f = f.open(mode)
return contextlib.closing(f) return contextlib.closing(f)
else: else:
return contextlib.nullcontext(f) return nullcontext(f)
def _make_tensor( def _make_tensor(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment