Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use os.path to avoid os errors for invalid file paths #575

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions ni_measurementlink_service/_dotenvpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,35 @@ def _get_caller_path() -> Optional[Path]:
for frame, _ in traceback.walk_stack(inspect.currentframe()):
if frame.f_code.co_filename:
module_path = Path(frame.f_code.co_filename)
if module_path.exists() and not _is_relative_to(module_path, nims_path):
if _exists(module_path) and not _is_relative_to(module_path, nims_path):
return module_path

return None


def _is_relative_to(path: PurePath, other: PurePath) -> bool:
if sys.version_info >= (3, 9):
# Path.exists() throws OSError when the path has invalid file characters.
# https://github.com/python/cpython/issues/79487
if sys.version_info >= (3, 10):

def _exists(path: Path) -> bool:
return path.exists()

else:

def _exists(path: Path) -> bool:
import os

return os.path.exists(path)


if sys.version_info >= (3, 9):

def _is_relative_to(path: PurePath, other: PurePath) -> bool:
return path.is_relative_to(other)
else:

else:

def _is_relative_to(path: PurePath, other: PurePath) -> bool:
try:
_ = path.relative_to(other)
return True
Expand Down