Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions pep-0499.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Title: ``python -m foo`` should bind ``sys.modules['foo']`` in addition to ``sys
Version: $Revision$
Last-Modified: $Date$
Author: Cameron Simpson <cs@cskk.id.au>, Chris Angelico <rosuav@gmail.com>, Joseph Jevnik <joejev@gmail.com>
BDFL-Delegate: Nick Coghlan
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Expand Down Expand Up @@ -152,6 +153,51 @@ where ``__name__`` is expected to be "__main__".
This PEP explicitly preserves that semantic.


Reference Implementation
========================

`BPO 36375 <https://bugs.python.org/issue36375>`_ is the issue tracker entry
for the PEP's reference implementation, with the current draft PR being
available `on GitHub <https://github.com/python/cpython/pull/12490>`_.


Open Questions
==============

This proposal does raise some backwards compatibility concerns, and these will
need to be well understood, and either a deprecation process designed, or clear
porting guidelines provided.

Pickle compatibility
--------------------

If no changes are made to the pickle module, then pickles that were previously
being written with the correct module name (due to a dual import) may start
being written with `__main__` as their module name instead, and hence fail to be
loaded correctly by other projects.

Scenarios to be checked:

* `python script.py` writing, `python -m script` reading
* `python -m script` writing, `python script.py` reading
* `python -m script` writing, `python some_other_app.py` reading
* `old_python -m script` writing, `new_python -m script` reading
* `new_python -m script` writing, `old_python -m script` reading


Projects that special-case `__main__`
-------------------------------------

In order to get the regression test suite to pass, the current reference
implementation had to patch `pdb` to avoid destroying its own global namespace.

This suggests there may be a broader compatibility issue where some scripts are
relying on direct execution and import giving different namespaces (just as
package execution keeps the two separate by executing the `__main__` submodule
in the `__main__` namespace, while the package name references the `__init__`
file as usual.


Background
==========

Expand Down