From 8a9d20151d3776972067a8e6e6f8f6c77b748693 Mon Sep 17 00:00:00 2001 From: Jeremy Reizenstein Date: Mon, 12 Oct 2020 09:46:13 -0700 Subject: [PATCH] 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 --- pytorch3d/io/utils.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pytorch3d/io/utils.py b/pytorch3d/io/utils.py index b28a864b..7cea7947 100644 --- a/pytorch3d/io/utils.py +++ b/pytorch3d/io/utils.py @@ -11,6 +11,14 @@ from fvcore.common.file_io import PathManager 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]: if isinstance(f, str): f = open(f, mode) @@ -19,7 +27,7 @@ def _open_file(f, mode="r") -> ContextManager[IO]: f = f.open(mode) return contextlib.closing(f) else: - return contextlib.nullcontext(f) + return nullcontext(f) def _make_tensor(