Skip to content

Commit 780e378

Browse files
fix: use os.path to avoid os errors for invalid file paths (#575)
* fix: use os.path to avoid os errors for invalid file paths * fix: add helper function using sys.version * fix: lint errors
1 parent 59ed5ce commit 780e378

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

ni_measurementlink_service/_dotenvpath.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,35 @@ def _get_caller_path() -> Optional[Path]:
4444
for frame, _ in traceback.walk_stack(inspect.currentframe()):
4545
if frame.f_code.co_filename:
4646
module_path = Path(frame.f_code.co_filename)
47-
if module_path.exists() and not _is_relative_to(module_path, nims_path):
47+
if _exists(module_path) and not _is_relative_to(module_path, nims_path):
4848
return module_path
4949

5050
return None
5151

5252

53-
def _is_relative_to(path: PurePath, other: PurePath) -> bool:
54-
if sys.version_info >= (3, 9):
53+
# Path.exists() throws OSError when the path has invalid file characters.
54+
# https://github.com/python/cpython/issues/79487
55+
if sys.version_info >= (3, 10):
56+
57+
def _exists(path: Path) -> bool:
58+
return path.exists()
59+
60+
else:
61+
62+
def _exists(path: Path) -> bool:
63+
import os
64+
65+
return os.path.exists(path)
66+
67+
68+
if sys.version_info >= (3, 9):
69+
70+
def _is_relative_to(path: PurePath, other: PurePath) -> bool:
5571
return path.is_relative_to(other)
56-
else:
72+
73+
else:
74+
75+
def _is_relative_to(path: PurePath, other: PurePath) -> bool:
5776
try:
5877
_ = path.relative_to(other)
5978
return True

0 commit comments

Comments
 (0)