diff --git a/oracletrace/tracer.py b/oracletrace/tracer.py index bf948cc..11ef97e 100644 --- a/oracletrace/tracer.py +++ b/oracletrace/tracer.py @@ -127,31 +127,21 @@ def _is_user_code(self, filename: str) -> bool: filename = os.path.realpath(filename) root_path = os.path.realpath(self._root_path) - # Check project root (commonpath raises ValueError on Windows when - # the paths sit on different drives — a different drive is outside) - try: - if os.path.commonpath([root_path, filename]) != root_path: - return False - except ValueError: + # Check project root + if os.path.commonpath([root_path, filename]) != root_path: return False - # Exclude Python stdlib (a different drive cannot be the stdlib) + # Exclude Python stdlib stdlib_path = os.path.realpath(sysconfig.get_path("stdlib")) - try: - if os.path.commonpath([stdlib_path, filename]) == stdlib_path: - return False - except ValueError: - pass + if os.path.commonpath([stdlib_path, filename]) == stdlib_path: + return False # Exclude all site-packages site_paths = site.getsitepackages() + [site.getusersitepackages()] normalized_site_paths = [os.path.realpath(p) for p in site_paths] for sp in normalized_site_paths: - try: - if os.path.commonpath([sp, filename]) == sp: - return False - except ValueError: - continue + if os.path.commonpath([sp, filename]) == sp: + return False # Exclude venv-like directories that may be inside the project root venv_markers = {"venv", ".venv", "env", ".env", "virtualenv"} diff --git a/tests/test_tracer.py b/tests/test_tracer.py index a0a2c32..07e1889 100644 --- a/tests/test_tracer.py +++ b/tests/test_tracer.py @@ -110,42 +110,3 @@ def test_root_path_with_trailing_slash(tracer): user_file = os.path.join(tracer._root_path, "module.py") Path(user_file).touch() assert tracer._is_user_code(user_file) - - -def test_file_on_another_drive_is_ignored_not_fatal(tracer, monkeypatch): - # On Windows, os.path.commonpath raises ValueError when the two paths - # sit on different drives; a file on another drive is simply not part - # of the project. Simulated so the guard is exercised on every OS. - real_commonpath = os.path.commonpath - - def commonpath(paths): - if any("OTHER_DRIVE" in str(p) for p in paths): - raise ValueError("Paths don't have the same drive") - return real_commonpath(paths) - - monkeypatch.setattr(os.path, "commonpath", commonpath) - assert not tracer._is_user_code(os.path.join("OTHER_DRIVE", "script.py")) - - -def test_stdlib_and_site_on_another_drive_do_not_crash(tracer, monkeypatch): - # Interpreter installed on C:, project on F: — the stdlib/site-packages - # comparisons raise ValueError for every project file, which used to - # kill tracing entirely. A different drive cannot be the stdlib. - import site - import sysconfig - - real_commonpath = os.path.commonpath - - def commonpath(paths): - if any("OTHER_DRIVE" in str(p) for p in paths): - raise ValueError("Paths don't have the same drive") - return real_commonpath(paths) - - monkeypatch.setattr(os.path, "commonpath", commonpath) - monkeypatch.setattr(sysconfig, "get_path", lambda name: os.path.join("OTHER_DRIVE", "stdlib")) - monkeypatch.setattr(site, "getsitepackages", lambda: [os.path.join("OTHER_DRIVE", "site-packages")]) - monkeypatch.setattr(site, "getusersitepackages", lambda: os.path.join("OTHER_DRIVE", "user-site")) - - user_file = os.path.join(tracer._root_path, "module.py") - Path(user_file).touch() - assert tracer._is_user_code(user_file)