Specify alter_sys=True when invoking runpy.run_module by guoshimin · Pull Request #211 · pex-tool/pex · GitHub
Skip to content

Specify alter_sys=True when invoking runpy.run_module - #211

Closed
guoshimin wants to merge 1 commit into
pex-tool:masterfrom
guoshimin:alter_sys
Closed

guoshimin wants to merge 1 commit into
pex-tool:masterfrom
guoshimin:alter_sys

Conversation

@guoshimin

Copy link
Copy Markdown

Specify alter_sys=True when invoking runpy.run_module so that __main__ points to the entry point module.

Fixes #210

…ain__ module is set to the entry point module
@mikekap

mikekap commented Feb 16, 2016

Copy link
Copy Markdown
Contributor

@guoshimin

Copy link
Copy Markdown
Author

Fair point about code expecting the existing behavior of sys.argv[0]. Alas the runpy.run_module function doesn't provide a way to override only sys.modules but not sys.argv[0]. So patching sys.modules by hand would amount to reinventing much of the logic in run_module.

@guoshimin

Copy link
Copy Markdown
Author

It will essentially be inlining the run_module method. Something like

def run_module_with_sys_modules_override(mod_name):
  run_name = '__main__'
  _, loader, code, fname = runpy._get_module_details(mod_name)
  pkg_name = mod_name.rpartition('.')[0]
  with runpy._TempModule(run_name) as temp_module:
      mod_globals = temp_module.module.__dict__
      runpy._run_code(code, mod_globals, None,
                      run_name, fname, loader, pkg_name)

Calling the internal methods of runpy isn't ideal.

@mikekap

mikekap commented Feb 17, 2016

Copy link
Copy Markdown
Contributor

Definitely agree that depending on runpy internals isn't great. Before we go down too much into thinking how to implement this, I'd like to make a case that leaving sys.argv[0] alone is probably the more correct behavior in general. sys.argv[0] is how the program was started. You should be able to os.exec([sys.executable] + sys.argv) for essentially restarting the program. Does that seem reasonable?

From reading https://www.python.org/dev/peps/pep-0338/ , it looks like the argument for changing sys.argv[0] is that some scripts expect it. The example given is regrtest.py, and indeed, the script would not work due to an assert: https://github.com/python/cpython/blob/2.7/Lib/test/regrtest.py#L1588 . However, I can't figure out why that assert is there at all - nothing else seems to read sys.argv[0]. It seems to be a remnant of another time where the script used sys.argv[0] instead of __file__ and slowly transitioned to __file__ (which matches the pattern in other parts of the python world).

If the above argument makes sense, and others agree with it, the right thing to do here would probably be to propose changing runpy for python 3.6+. Assuming that's accepted, we can backport the functionality with the "hack" you posted for < 3.6. I'll send an mail to python-dev to see how folks feel about this.

@guoshimin

Copy link
Copy Markdown
Author

Yeah totally agree sys.argv[0] is best left alone. Looks like I'll need to patch pex for my organization for the time being.

@ncoghlan

Copy link
Copy Markdown

@guoshimin guoshimin closed this Jun 17, 2016
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.

3 participants