1--- python/google/protobuf/internal/test_util.py 2+++ python/google/protobuf/internal/test_util.py 3@@ -39,6 +39,7 @@ __author__ = '[email protected] (Will Robinson)' 4 import numbers 5 import operator 6 import os.path 7+import pathlib 8 9 from google.protobuf import unittest_import_pb2 10 from google.protobuf import unittest_pb2 11@@ -617,17 +618,22 @@ def ExpectAllFieldsSet(test_case, message): 12 message.default_import_enum) 13 14 15+def _SearchUp(path, filename): 16+ path = pathlib.Path(path).resolve() 17+ for parent in [path] + list(path.parents): 18+ file_path = parent / ('google/protobuf/testdata/' + filename) 19+ if file_path.exists(): 20+ # Found it. Load the golden file from the testdata directory. 21+ return file_path.open('rb') 22+ return None 23+ 24 def GoldenFile(filename): 25 """Finds the given golden file and returns a file object representing it.""" 26 27 # Search up the directory tree looking for the C++ protobuf source code. 28- path = '.' 29- while os.path.exists(path): 30- if os.path.exists(os.path.join(path, 'src/google/protobuf')): 31- # Found it. Load the golden file from the testdata directory. 32- full_path = os.path.join(path, 'src/google/protobuf/testdata', filename) 33- return open(full_path, 'rb') 34- path = os.path.join(path, '..') 35+ f = _SearchUp('.', filename) or _SearchUp(__file__, filename) 36+ if f: 37+ return f 38 39 # Search internally. 40 path = '.'