Skip to content

Commit a37701d

Browse files
committed
Monkey-patch os.path to support gs:// URIs
This monkey-patches the private `os.path._get_sep` function used by `os.path.join` to handle paths that start with "gs://". If a path starts with "gs://", we ignore the OS separator and return "/" as the separator to use.
1 parent 319c4a4 commit a37701d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

learned_optimization/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414
# limitations under the License.
1515

1616
"""learned_optimizer module."""
17+
18+
from .py_utils import patch_os_path_get_sep
19+
20+
_OLD_OS_PATH_GET_SEP = patch_os_path_get_sep()

learned_optimization/py_utils.py

+24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# limitations under the License.
1515

1616
"""Common python utilities."""
17+
import os
1718
from concurrent import futures
1819
from typing import Any, Callable, Sequence
1920
import tqdm
@@ -26,3 +27,26 @@ def threaded_tqdm_map(threads: int, func: Callable[[Any], Any],
2627
for l in tqdm.tqdm(data):
2728
future_list.append(executor.submit(func, l))
2829
return [x.result() for x in tqdm.tqdm(future_list)]
30+
31+
32+
def patch_os_path_get_sep():
33+
old_get_sep = os.path._get_sep
34+
35+
def new_get_sep(path):
36+
"""Return the OS separator for the given path.
37+
38+
If `path` starts with "gs://", "/" is used as the separator.
39+
"""
40+
if isinstance(path, bytes):
41+
gs_prefix = b'gs://'
42+
sep = b'/'
43+
else:
44+
gs_prefix = 'gs://'
45+
sep = '/'
46+
47+
if not path.startswith(gs_prefix):
48+
sep = old_get_sep(path)
49+
return sep
50+
51+
os.path._get_sep = new_get_sep
52+
return old_get_sep

0 commit comments

Comments
 (0)