-
Notifications
You must be signed in to change notification settings - Fork 200
Check against sys.executable when determining if we should instrument subprocess #2065
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -300,6 +300,42 @@ def is_python(path) -> bool: | |
| if filename.find(name) != -1: | ||
| return True | ||
|
|
||
| # Fallback: check if the path refers to the same executable as sys.executable. | ||
| # This handles cases where the binary name doesn't contain "python" (e.g. | ||
| # custom builds, embedded distributions). We use a platform-aware | ||
| # comparison that covers symlinks, relative paths, and Windows case | ||
| # differences. | ||
| try: | ||
| path_str = path.decode(sys.getfilesystemencoding()) if isinstance(path, bytes) else path | ||
| sys_exec_str = sys.executable | ||
|
|
||
| # Fast path: exact string match | ||
| if path_str == sys_exec_str: | ||
| return True | ||
|
|
||
| # If both exist, samefile is the most reliable (handles symlinks, hardlinks) | ||
| try: | ||
| if os.path.exists(path_str) and os.path.exists(sys_exec_str): | ||
| if os.path.samefile(path_str, sys_exec_str): | ||
| return True | ||
| except (OSError, AttributeError, NotImplementedError, ValueError): | ||
| pass | ||
|
|
||
| # Fallback: compare normalized realpaths | ||
| # - realpath resolves symlinks | ||
| # - abspath is implied by realpath, but we ensure it | ||
| # - normpath collapses redundant separators | ||
| # - normcase normalizes case and separators on Windows | ||
| try: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
A bare command such as |
||
| norm_path = os.path.normcase(os.path.normpath(os.path.realpath(path_str))) | ||
| norm_sys = os.path.normcase(os.path.normpath(os.path.realpath(sys_exec_str))) | ||
| if norm_path == norm_sys: | ||
| return True | ||
| except Exception: | ||
| pass | ||
| except Exception: | ||
| pass | ||
|
|
||
| return False | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strict decoding with
sys.getfilesystemencoding()rejects valid POSIX byte paths containing undecodable bytes, causing the outer handler to classify the executable as non-Python. Useos.fsdecode(path)so surrogate-escaped filesystem paths can still be compared withsys.executable.