Kaydet (Commit) 2ab5b092 authored tarafından Nick Coghlan's avatar Nick Coghlan

Close #24458: PEP 489 documentation

Patch by Petr Viktorin.
üst ccc897f8
...@@ -873,6 +873,8 @@ been created. ...@@ -873,6 +873,8 @@ been created.
instead. instead.
.. _sub-interpreter-support:
Sub-interpreter support Sub-interpreter support
======================= =======================
......
This diff is collapsed.
.. highlightlang:: c .. highlightlang:: c
.. _building: .. _building:
******************************************** *****************************
Building C and C++ Extensions with distutils Building C and C++ Extensions
******************************************** *****************************
.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de> A C extension for CPython is a shared library (e.g. a ``.so`` file on Linux,
``.pyd`` on Windows), which exports an *initialization function*.
To be importable, the shared library must be available on :envvar:`PYTHONPATH`,
and must be named after the module name, with an appropriate extension.
When using distutils, the correct filename is generated automatically.
The initialization function has the signature:
.. c:function:: PyObject* PyInit_modulename(void)
It returns either a fully-initialized module, or a :c:type:`PyModuleDef`
instance. See :ref:`initializing-modules` for details.
.. highlightlang:: python
For modules with ASCII-only names, the function must be named
``PyInit_<modulename>``, with ``<modulename>`` replaced by the name of the
module. When using :ref:`multi-phase-initialization`, non-ASCII module names
are allowed. In this case, the initialization function name is
``PyInitU_<modulename>``, with ``<modulename>`` encoded using Python's
*punycode* encoding with hyphens replaced by underscores. In Python::
Starting in Python 1.4, Python provides, on Unix, a special make file for def initfunc_name(name):
building make files for building dynamically-linked extensions and custom try:
interpreters. Starting with Python 2.0, this mechanism (known as related to suffix = b'_' + name.encode('ascii')
Makefile.pre.in, and Setup files) is no longer supported. Building custom except UnicodeEncodeError:
interpreters was rarely used, and extension modules can be built using suffix = b'U_' + name.encode('punycode').replace(b'-', b'_')
distutils. return b'PyInit' + suffix
It is possible to export multiple modules from a single shared library by
defining multiple initialization functions. However, importing them requires
using symbolic links or a custom importer, because by default only the
function corresponding to the filename is found.
See :PEP:`489#multiple-modules-in-one-library` for details.
.. highlightlang:: c
Building C and C++ Extensions with distutils
============================================
.. sectionauthor:: Martin v. Löwis <martin@v.loewis.de>
Building an extension module using distutils requires that distutils is Extension modules can be built using distutils, which is included in Python.
installed on the build machine, which is included in Python 2.x and available Since distutils also supports creation of binary packages, users don't
separately for Python 1.5. Since distutils also supports creation of binary necessarily need a compiler and distutils to install the extension.
packages, users don't necessarily need a compiler and distutils to install the
extension.
A distutils package contains a driver script, :file:`setup.py`. This is a plain A distutils package contains a driver script, :file:`setup.py`. This is a plain
Python file, which, in the most simple case, could look like this:: Python file, which, in the most simple case, could look like this::
......
...@@ -413,6 +413,13 @@ A more substantial example module is included in the Python source distribution ...@@ -413,6 +413,13 @@ A more substantial example module is included in the Python source distribution
as :file:`Modules/xxmodule.c`. This file may be used as a template or simply as :file:`Modules/xxmodule.c`. This file may be used as a template or simply
read as an example. read as an example.
.. note::
Unlike our ``spam`` example, ``xxmodule`` uses *multi-phase initialization*
(new in Python 3.5), where a PyModuleDef structure is returned from
``PyInit_spam``, and creation of the module is left to the import machinery.
For details on multi-phase initialization, see :PEP:`489`.
.. _compilation: .. _compilation:
......
...@@ -98,9 +98,8 @@ described here are distributed with the Python sources in the ...@@ -98,9 +98,8 @@ described here are distributed with the Python sources in the
it. Copy your C sources into it. Note that the module source file name does it. Copy your C sources into it. Note that the module source file name does
not necessarily have to match the module name, but the name of the not necessarily have to match the module name, but the name of the
initialization function should match the module name --- you can only import a initialization function should match the module name --- you can only import a
module :mod:`spam` if its initialization function is called :c:func:`initspam`, module :mod:`spam` if its initialization function is called :c:func:`PyInit_spam`,
and it should call :c:func:`Py_InitModule` with the string ``"spam"`` as its (see :ref:`building`, or use the minimal :file:`Modules/xxmodule.c` as a guide).
first argument (use the minimal :file:`example.c` in this directory as a guide).
By convention, it lives in a file called :file:`spam.c` or :file:`spammodule.c`. By convention, it lives in a file called :file:`spam.c` or :file:`spammodule.c`.
The output file should be called :file:`spam.pyd` (in Release mode) or The output file should be called :file:`spam.pyd` (in Release mode) or
:file:`spam_d.pyd` (in Debug mode). The extension :file:`.pyd` was chosen :file:`spam_d.pyd` (in Debug mode). The extension :file:`.pyd` was chosen
......
...@@ -283,7 +283,7 @@ two step module loading mechanism introduced by :pep:`451` in Python 3.4. ...@@ -283,7 +283,7 @@ two step module loading mechanism introduced by :pep:`451` in Python 3.4.
This change brings the import semantics of extension modules that opt-in to This change brings the import semantics of extension modules that opt-in to
using the new mechanism much closer to those of Python source and bytecode using the new mechanism much closer to those of Python source and bytecode
modules, including the ability to any valid identifier as a module name, modules, including the ability to use any valid identifier as a module name,
rather than being restricted to ASCII. rather than being restricted to ASCII.
.. seealso:: .. seealso::
...@@ -763,7 +763,7 @@ unicodedata ...@@ -763,7 +763,7 @@ unicodedata
----------- -----------
* The :mod:`unicodedata` module now uses data from `Unicode 8.0.0 * The :mod:`unicodedata` module now uses data from `Unicode 8.0.0
<http://unicode.org/versions/Unicode8.0.0/>`_. <http://unicode.org/versions/Unicode8.0.0/>`_.
wsgiref wsgiref
......
...@@ -92,6 +92,9 @@ Tests ...@@ -92,6 +92,9 @@ Tests
Documentation Documentation
------------- -------------
- Issue #24458: Update documentation to cover multi-phase initialization for
extension modules (PEP 489). Patch by Petr Viktorin.
- Issue #24351: Clarify what is meant by "identifier" in the context of - Issue #24351: Clarify what is meant by "identifier" in the context of
string.Template instances. string.Template instances.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment