Python module loading documentation tweaks

This commit is contained in:
William S Fulton 2016-06-11 00:59:00 +01:00
commit d18b6e2c8d
2 changed files with 53 additions and 33 deletions

View file

@ -1595,6 +1595,12 @@
<li><a href="Python.html#Python_absimport">Enforcing absolute import semantics</a> <li><a href="Python.html#Python_absimport">Enforcing absolute import semantics</a>
<li><a href="Python.html#Python_importfrominit">Importing from __init__.py</a> <li><a href="Python.html#Python_importfrominit">Importing from __init__.py</a>
<li><a href="Python.html#Python_implicit_namespace_packages">Implicit Namespace Packages</a> <li><a href="Python.html#Python_implicit_namespace_packages">Implicit Namespace Packages</a>
<li><a href="Python.html#Python_package_search">Searching for the wrapper module</a>
<ul>
<li><a href="Python.html#Python_package_search_both_package_modules">Both modules in the same package</a>
<li><a href="Python.html#Python_package_search_wrapper_split">Split modules</a>
<li><a href="Python.html#Python_package_search_both_global_modules">Both modules are global</a>
</ul>
</ul> </ul>
<li><a href="Python.html#Python_python3support">Python 3 Support</a> <li><a href="Python.html#Python_python3support">Python 3 Support</a>
<ul> <ul>

View file

@ -117,7 +117,12 @@
<li><a href="#Python_absimport">Enforcing absolute import semantics</a> <li><a href="#Python_absimport">Enforcing absolute import semantics</a>
<li><a href="#Python_importfrominit">Importing from __init__.py</a> <li><a href="#Python_importfrominit">Importing from __init__.py</a>
<li><a href="#Python_implicit_namespace_packages">Implicit Namespace Packages</a> <li><a href="#Python_implicit_namespace_packages">Implicit Namespace Packages</a>
<li><a href="#Python_package_search">Searching for the wrapper module</a></li> <li><a href="#Python_package_search">Searching for the wrapper module</a>
<ul>
<li><a href="#Python_package_search_both_package_modules">Both modules in the same package</a>
<li><a href="#Python_package_search_wrapper_split">Split modules</a>
<li><a href="#Python_package_search_both_global_modules">Both modules are global</a>
</ul>
</ul> </ul>
<li><a href="#Python_python3support">Python 3 Support</a> <li><a href="#Python_python3support">Python 3 Support</a>
<ul> <ul>
@ -5525,18 +5530,18 @@ directories in order to obtain a desirable package/module hierarchy.
<p> <p>
Python3 adds another option for packages with Python3 adds another option for packages with
<a href="https://www.python.org/dev/peps/pep-0420/">PEP 0420</a> (implicit <a href="https://www.python.org/dev/peps/pep-0420/">PEP 0420</a> (implicit
namespace packages). These new type of python packages no longer use namespace packages). Implicit namespace packages no longer use
__init__.py files. Swig generated python modules support implicit __init__.py files. SWIG generated Python modules support implicit
namespace packages. See namespace packages. See
<a href="#Python_implicit_namespace_packages">36.11.5 Implicit Namespace <a href="#Python_implicit_namespace_packages">36.11.5 Implicit Namespace
Packages</a> for more information. Packages</a> for more information.
</p> </p>
<p> <p>
If you place a swig generated module into a python package then there If you place a SWIG generated module into a Python package then there
are details concerning the way swig are details concerning the way SWIG
<a href="#Python_package_search">searches for the wrapper module</a> <a href="#Python_package_search">searches for the wrapper module</a>
you may want to familiarize yourself with. that you may want to familiarize yourself with.
</p> </p>
<p>The way Python defines its modules and packages impacts SWIG users. Some <p>The way Python defines its modules and packages impacts SWIG users. Some
@ -5957,28 +5962,30 @@ zipimporter requires python-3.5.1 or newer to work with subpackages.
<b>Compatibility Note:</b> Support for implicit namespace packages was added in SWIG-3.0.9. <b>Compatibility Note:</b> Support for implicit namespace packages was added in SWIG-3.0.9.
</p> </p>
<H3><a name="Python_package_search">36.11.6 Searching for the wrapper module</a>
</H3> <H3><a name="Python_package_search">36.11.6 Searching for the wrapper module</a></H3>
<p> <p>
When swig creates wrappers from the interface file foo.i two python modules are When SWIG creates wrappers from an interface file, say foo.i, two Python modules are
created. There is a pure python module module (foo.py) and C code which is created. There is a pure Python module module (foo.py) and C/C++ code which is
built and linked into a dynamically (or statically) loaded python module _foo built and linked into a dynamically (or statically) loaded module _foo
(see <a href="#Python_nn3">section 36.2</a> for details). So, the interface (see the <a href="Python.html#Python_nn3">Preliminaries section</a> for details). So, the interface
file really defines two python modules. How these two modules are loaded is file really defines two Python modules. How these two modules are loaded is
covered here. covered next.
</p> </p>
<p> <p>
The pure python module needs to load the companion module in order to link The pure Python module needs to load the C/C++ module in order to link
the python to the wrapped C methods. To do this it must make some assumptions to the wrapped C/C++ methods. To do this it must make some assumptions
about what package the companion module may be located in. The method the about what package the C/C++ module may be located in. The approach the
python shadow file uses to find the other half is as follows: pure Python module uses to find the C/C++ module is as follows:
</p> </p>
<ol> <ol>
<li><p>foo.py tries to load _foo from the same package foo.py is <li><p>The pure Python module, foo.py, tries to load the C/C++ module, _foo, from the same package foo.py is
located in. The package name is determined from the __name__ located in. The package name is determined from the <tt>__name__</tt>
attribute given to foo.py by the python loader that imported attribute given to foo.py by the Python loader that imported
foo.py. If foo.py is not in a package then _foo is loaded foo.py. If foo.py is not in a package then _foo is loaded
as a global module.</p> as a global module.</p>
</li> </li>
@ -5988,21 +5995,20 @@ python shadow file uses to find the other half is as follows:
</li> </li>
</ol> </ol>
<p>
Here foo.py is the pure python module and _foo is the dynamically or statically
loaded C module.
</p>
<p> <p>
As an example suppose foo.i is compiled into foo.py and _foo.so. Assuming As an example suppose foo.i is compiled into foo.py and _foo.so. Assuming
/dir is on PYTHONPATH, then the two modules can be installed and used in the /dir is on PYTHONPATH, then the two modules can be installed and used in the
following ways: following ways:
</p> </p>
<h4>Both halves in the same package</h4>
<H4><a name="Python_package_search_both_package_modules">36.11.6.1 Both modules in the same package</a></H4>
<p>Both modules are in one package:</p>
<div class="diagram"> <div class="diagram">
<pre> <pre>
/dir/pakage/foo.py /dir/package/foo.py
/dir/package/__init__.py /dir/package/__init__.py
/dir/package/_foo.so /dir/package/_foo.so
</pre> </pre>
@ -6014,10 +6020,14 @@ from package import foo
</pre> </pre>
</div> </div>
<h4>Wrapper module is global</h4>
<H4><a name="Python_package_search_wrapper_split">36.11.6.2 Split modules</a></H4>
<p>The pure python module is in a package and the C/C++ module is global:</p>
<div class="diagram"> <div class="diagram">
<pre> <pre>
/dir/pakage/foo.py /dir/package/foo.py
/dir/package/__init__.py /dir/package/__init__.py
/dir/_foo.so /dir/_foo.so
</pre> </pre>
@ -6029,7 +6039,11 @@ from package import foo
</pre> </pre>
</div> </div>
<h4>Both modules are global</h4>
<H4><a name="Python_package_search_both_global_modules">36.11.6.3 Both modules are global</a></H4>
<p>Both modules are global:</p>
<div class="diagram"> <div class="diagram">
<pre> <pre>
/dir/foo.py /dir/foo.py
@ -6044,8 +6058,8 @@ import foo
</div> </div>
<p> <p>
If _foo is statically linked into an embedded python interpreter, then it may or If _foo is statically linked into an embedded Python interpreter, then it may or
may not be in a python package. This depends in the exact way the module was may not be in a Python package. This depends in the exact way the module was
loaded statically. The above search order will still be used for statically loaded statically. The above search order will still be used for statically
loaded modules. So, one may place the module either globally or in a package loaded modules. So, one may place the module either globally or in a package
as desired. as desired.