From: Hyungjoo Andrew Cho Date: Fri, 29 Mar 2019 03:49:43 +0000 (-0700) Subject: Serialization supports pathlib.Path object for the input argument (#18562) X-Git-Tag: accepted/tizen/6.5/unified/20211028.231830~570 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=37529161326a001e8398bc5b4e75791db94c04b0;p=platform%2Fupstream%2Fpytorch.git Serialization supports pathlib.Path object for the input argument (#18562) Summary: This will allow pathlib.Path object to the torch.load as an input argument. Fixes #16607 Pull Request resolved: https://github.com/pytorch/pytorch/pull/18562 Differential Revision: D14668255 Pulled By: soumith fbshipit-source-id: 0ae4f7c210918582912f2d1ef2a98f1ab288c540 --- diff --git a/torch/serialization.py b/torch/serialization.py index ee92d8d..aabdf72 100644 --- a/torch/serialization.py +++ b/torch/serialization.py @@ -377,10 +377,12 @@ def load(f, map_location=None, pickle_module=pickle, **pickle_load_args): """ new_fd = False if isinstance(f, str) or \ - (sys.version_info[0] == 2 and isinstance(f, unicode)) or \ - (sys.version_info[0] == 3 and isinstance(f, pathlib.Path)): + (sys.version_info[0] == 2 and isinstance(f, unicode)): new_fd = True f = open(f, 'rb') + elif (sys.version_info[0] == 3 and isinstance(f, pathlib.Path)): + new_fd = True + f = f.open('rb') try: return _load(f, map_location, pickle_module, **pickle_load_args) finally: