(original) (raw)


On Tue, Feb 16, 2016 at 9:00 PM Mike Kaplinskiy <mike.kaplinskiy@gmail.com> wrote:
Hey folks,

I hope this is the right list for this sort of thing (python-ideas seemed more far-fetched).

For some context: there is currently a issue with pex that causes sys.modules lookups to stop working for \_\_main\_\_. In turns this makes unittest.run() & pkg\_resources.resource\_\* fail. The root cause is that pex uses runpy.run\_module with alter\_sys=False. The fix should be to just pass alter\_sys=True, but that changes sys.argv\[0\] and various existing pex files depend on that being the pex file. You can read more at https://github.com/pantsbuild/pex/pull/211 .

Conservatively, I'd like to propose adding an argument to disable this behavior. The current behavior breaks a somewhat reasonable invariant that you can restart your program via \`os.execv(\[sys.executable\] + sys.argv)\`.

I don't know enough about pex to really dig into what it is trying to do so this is tangential to answering your question but:

sys.executable may be None. ex: If you're an embedded Python interpreter there is no Python executable. It cannot be blindly used re-execute the current process.

sys.argv represents the C main() argv array. Your inclination (in the linked to bug above) to leave sys.argv\[0\] alone is a good one.

-gps

Moreover it might be user-friendly to add a \`argv=sys.argv\[1:\]\` argument to set & restore the full arguments to the module, where \`argv=None\` disables argv\[0\] switching.

What do you think?

Mike.

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/greg%40krypto.org