Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e50f9aa

Browse files
Issue #19697: Document cases where __main__.__spec__ is None.
1 parent 4ab532b commit e50f9aa

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

Doc/reference/import.rst

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,10 @@ the module.
519519

520520
The ``__spec__`` attribute must be set to the module spec that was
521521
used when importing the module. This is used primarily for
522-
introspection and during reloading.
522+
introspection and during reloading. Setting ``__spec__``
523+
appropriately applies equally to :ref:`modules initialized during
524+
interpreter startup <programs>`. The one exception is ``__main__``,
525+
where ``__spec__`` is :ref:`set to None in some cases <main_spec>`.
523526

524527
.. versionadded:: 3.4
525528

@@ -829,6 +832,37 @@ it is sufficient to raise :exc:`ImportError` directly from
829832
while raising an exception terminates it immediately.
830833

831834

835+
Special considerations for __main__
836+
===================================
837+
838+
The :mod:`__main__` module is a special case relative to Python's import
839+
system. As noted :ref:`elsewhere <programs>`, the ``__main__`` module
840+
is directly initialized at interpreter startup, much like :mod:`sys` and
841+
:mod:`builtins`. However, unlike those two, it doesn't strictly
842+
qualify as a built-in module. This is because the manner in which
843+
``__main__`` is initialized depends on the flags and other options with
844+
which the interpreter is invoked.
845+
846+
.. _main_spec:
847+
848+
__main__.__spec__
849+
-----------------
850+
851+
Depending on how :mod:`__main__` is initialized, ``__main__.__spec__``
852+
gets set appropriately or to ``None``.
853+
854+
When Python is started with the :option:`-m` option, ``__spec__`` is set
855+
to the module spec of the corresponding module.
856+
857+
In :ref:`the remaining cases <using-on-interface-options>`
858+
``__main__.__spec__`` is set to ``None``:
859+
860+
- interactive prompt
861+
- -c switch
862+
- running from stdin
863+
- running directly from a source or bytecode file
864+
865+
832866
Open issues
833867
===========
834868

@@ -841,6 +875,12 @@ related entries in the data model reference page?
841875
XXX runpy, pkgutil, et al in the library manual should all get "See Also"
842876
links at the top pointing to the new import system section.
843877

878+
XXX Add more explanation regarding the different ways in which
879+
``__main__`` is initialized?
880+
881+
XXX Add more info on ``__main__`` quirks/pitfalls (i.e. copy from
882+
:pep:`395`).
883+
844884

845885
References
846886
==========

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ Documentation
115115
required reference material for tool developers that isn't recorded
116116
anywhere else.
117117

118+
- Issue #19697: Document cases where __main__.__spec__ is None.
119+
118120
Tests
119121
-----
120122

0 commit comments

Comments
 (0)