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 sys.path manipulations for isolated mode #552

Conversation

godlygeek
Copy link
Contributor

Closes #551

I struggled to write good tests for this, and while working on it I realized that at least one of the existing tests wasn't actually proving what it set out to prove, so I restructured the existing tests as well.

As these were previously written, they didn't actually prove that the
original entry added by Python when memray was run had been removed,
which is especially problematic since:

    python -m memray run -m somemodule

would remove an entry added by Python and re-add that same entry. We had
no proof this was actually running the expected code and making the
expected changes.

Use `python -c ...` when running `memray run -m` to exercise this
better, since we can check that the (different) path that would be added
by the interpreter has been replaced.

Continue using `python -m memray` when running `memray run -c cmd` and
`memray run script.py`, but have the script that gets run print the
entirety of `sys.path` so we can make assertions about what isn't there
as well as what is.

Signed-off-by: Matt Wozniski <[email protected]>
@godlygeek godlygeek added the bug Something isn't working label Feb 22, 2024
@godlygeek godlygeek self-assigned this Feb 22, 2024
`memray run` tries to match the updates to `sys.path` that are performed
by the interpreter itself when it starts. When those path updates are
suppressed (by the `-I` or `-P` command line flags or their
corresponding environment variables), Memray's emulation winds up
overwriting paths in `sys.path` that needed to be left alone.

Signed-off-by: Matt Wozniski <[email protected]>
These flags disable Python's manipulation of `sys.path`, and Memray
shouldn't modify `sys.path` either if one of them has been passed.

Signed-off-by: Matt Wozniski <[email protected]>
@codecov-commenter
Copy link

codecov-commenter commented Feb 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (41248ed) 92.55% compared to head (9bea3f5) 92.86%.
Report is 17 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #552      +/-   ##
==========================================
+ Coverage   92.55%   92.86%   +0.31%     
==========================================
  Files          91       91              
  Lines       11304    11158     -146     
  Branches     1581     2034     +453     
==========================================
- Hits        10462    10362     -100     
+ Misses        837      796      -41     
+ Partials        5        0       -5     
Flag Coverage Δ
cpp 92.86% <100.00%> (+6.92%) ⬆️
python_and_cython 92.86% <100.00%> (-2.86%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@godlygeek godlygeek force-pushed the fix_sys_path_manipulations_for_isolated_mode branch from 5d2ea4f to 9bea3f5 Compare February 22, 2024 00:37
@pablogsal pablogsal merged commit 67ee066 into bloomberg:main Feb 22, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

memray run overwrites sys.argv[0] even when -I or -P is used
3 participants