Skip to content

Update sys.path when running memray run#86

Merged
pablogsal merged 2 commits into
bloomberg:mainfrom
tal66:edit-sys-path
May 16, 2022
Merged

Update sys.path when running memray run#86
pablogsal merged 2 commits into
bloomberg:mainfrom
tal66:edit-sys-path

Conversation

@tal66
Copy link
Copy Markdown
Contributor

@tal66 tal66 commented May 8, 2022

Hi, this closes #76.

  • refactor suggestion to test_cli.py::TestRunSubCommand (class level mocks limited testing possibilities)
  • added test that fails when not editing sys.path
  • added method edit_sys that edits sys.path (also moved existing sys edits to it).
    sys.path[0] will be the directory containing args.script, or '.' when run -c (like python does)

godlygeek and others added 2 commits May 13, 2022 19:49
In order to match the behavior of `python foo.py`, `python -m foo`, and
`python -c "foo"`, `memray run` manipulates `sys.path` and `sys.argv[0]`
to match the modifications that the interpreter itself would do.

Add tests validating that the modifications work as expected.

Signed-off-by: Matt Wozniski <mwozniski@bloomberg.net>
This enables finding local modules. Also, ensure that `sys.argv[0]`
matches what it would if the user had run this command without Memray,
to minimize the observeable differences our runner forces on users'
scripts.

Signed-off-by: tal66 <77445020+tal66@users.noreply.github.com>
Signed-off-by: Matt Wozniski <mwozniski@bloomberg.net>
@godlygeek
Copy link
Copy Markdown
Contributor

Thanks @tal66 - I picked this up, but ran in a bit of a different way with it. In particular, the reason you were struggling with mocks in the tests was that you were putting the new tests into the wrong layer - the tests in the unit/ directory are supposed to have no external dependencies, which is why so many things were being mocked out.

The tests that we need in order to validate that this is working properly fit better into the integration/ directory, where we can do a full run of the memray run command on a file or module that we control and observe how the actual command behaves when run as a subprocess, so I've updated to do that.

@pablogsal Since I've made some pretty extensive changes here, I'd rather not land this myself - would you mind giving this a review?

@godlygeek godlygeek changed the title Edit sys path Update sys.path when running memray run May 16, 2022
@pablogsal
Copy link
Copy Markdown
Collaborator

Thanks for adding tests for all three cases :)

@pablogsal pablogsal merged commit 348b74f into bloomberg:main May 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memray seems to modify sys.path such that local module imports fail

3 participants