Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Explain how to add an extension module #1350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 46 commits into from
Sep 11, 2024
Merged
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
086ded3
explain how to add modules
picnixz Jul 13, 2024
96fb998
Update extension-modules.rst
picnixz Jul 13, 2024
5f8797c
Update extension-modules.rst
picnixz Jul 13, 2024
e5d41f8
Update extension-modules.rst
picnixz Jul 13, 2024
b740114
Update extension-modules.rst
picnixz Jul 13, 2024
c74df67
Update extension-modules.rst
picnixz Jul 13, 2024
e645869
Update extension-modules.rst
picnixz Jul 14, 2024
5a9a57c
Update extension-modules.rst
picnixz Jul 14, 2024
26a18eb
Update extension-modules.rst
picnixz Jul 14, 2024
1b4d73f
Update extension-modules.rst
picnixz Jul 14, 2024
94086dd
Address Hugo's feedback
picnixz Jul 14, 2024
9a78a3b
Update extension-modules.rst
picnixz Jul 14, 2024
e22c278
Update extension-modules.rst
picnixz Jul 14, 2024
1f51497
improvements
picnixz Jul 15, 2024
bdf09e5
fixup! sphinx
picnixz Jul 15, 2024
ef7cf3e
fixup! indents
picnixz Jul 15, 2024
1a405ba
fixup! warnings
picnixz Jul 15, 2024
e39cbc2
improve sections
picnixz Jul 15, 2024
35f207f
fix markup
picnixz Jul 15, 2024
316b00d
improve titles
picnixz Jul 15, 2024
523dece
improve presentation
picnixz Jul 15, 2024
d1cdd1d
fixup! markup
picnixz Jul 15, 2024
defb31e
simplify snippets
picnixz Jul 15, 2024
6213438
improvements
picnixz Jul 15, 2024
f6e5d55
improvements
picnixz Jul 15, 2024
d1a1ed5
some rewordings and cleanups
picnixz Jul 15, 2024
86e3e54
simplify wording
picnixz Jul 16, 2024
65f62e7
address Erlend's review
picnixz Jul 17, 2024
4b7c7d8
fix indents?
picnixz Jul 17, 2024
7abb6f1
add ref to clinic everywhere when needed
picnixz Jul 17, 2024
da9b58b
fix typos
picnixz Jul 17, 2024
783e6db
address encukou's review
picnixz Jul 18, 2024
8906ebd
improve the page flow
picnixz Jul 18, 2024
128c81c
use sentence case
picnixz Jul 18, 2024
7d6c8d6
add podman tip
picnixz Jul 18, 2024
01c25bc
address rest of the review
picnixz Jul 18, 2024
56910fb
address Alyssa's review
picnixz Jul 18, 2024
3ee1cea
add details
picnixz Jul 18, 2024
3d235f4
address review
picnixz Aug 4, 2024
7b0b234
Make it easier to update the required ubuntu version
picnixz Aug 4, 2024
7fa94bf
Merge remote-tracking branch 'upstream/main' into add-extensions-tuto…
picnixz Aug 4, 2024
ec48fdb
fixup!
picnixz Aug 4, 2024
1843e3d
fixup!
picnixz Aug 4, 2024
18c4d91
improve comment
picnixz Aug 14, 2024
8224bbd
use double quotes instead of single quotes
picnixz Aug 14, 2024
2f63053
Address Carol's review.
picnixz Sep 11, 2024
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
Prev Previous commit
Next Next commit
address encukou's review
  • Loading branch information
picnixz committed Jul 18, 2024
commit 783e6db82e7b758a938e38c00c114971fe7e170e
39 changes: 30 additions & 9 deletions 39 developer-workflow/extension-modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,25 @@ written in C. Ideally, we want to modify ``foo.py`` as follows:
def greet():
return "Hello World!"

Some modules in the standard library are implemented both in C and in Python,
such as :mod:`decimal` or :mod:`itertools`, and the C implementation is expected
to improve performance when available (such modules are commonly referred
to as *accelerator modules*). In our example, we need to determine:
Some modules in the standard library, such as :mod:`datetime` or :mod:`pickle`,
have identical implementations in C and Python; the C implementation, when
available, is typically expected to improve performance (such modules are
commonly referred to as *accelerator modules*).

Other modules mainly implemented in Python may import a C helper extension
providing implementation details (for instance, the :mod:`csv` module uses
the internal :mod:`!_csv` module defined in :cpy-file:`Modules/_csv.c`).

.. note::

According to :pep:`399` guidelines, *new* modules must have a working
and tested implementation in pure Python, unless a special dispensation
is given.

Please ask the :github:`Steering Council <python/steering-council>` if
such dispensation is needed.

In our example, we need to determine:

- where to place the extension module source code in the CPython project tree;
- which files to modify in order to compile the CPython project;
Expand Down Expand Up @@ -208,8 +223,12 @@ The following code snippets illustrate the possible contents of the above files:

.. tip::

Do not forget that symbols exported by ``libpython`` must start
with ``Py`` or ``_Py``, which can be verified by ``make smelly``.
Functions or data that do not need to be shared across different C source
files should be declared ``static`` to avoid exporting the symbols from
``libpython``.

If symbols need to be exported, their names must start with ``Py`` or
``_Py``. This can be verified by ``make smelly``.
picnixz marked this conversation as resolved.
Show resolved Hide resolved

One could imagine having more ``.h`` files, or no ``helper.c`` file. Here,
we wanted to illustrate a simple example without making it too trivial. If
Expand All @@ -236,9 +255,11 @@ Extension modules can be classified into the following types:
Built-in extension modules are part of the interpreter, while shared extension
modules might be supplied or overridden externally.

In particular, built-in extension modules do not need to have a pure Python
implementation but shared extension modules should have one in case the shared
library is not present on the system.
New built-in extension modules could be considered exceptions to :pep:`399`,
but please ask the Steering Council for confirmation. Nevertheless, besides
respecting :pep:`399`, shared extension modules MUST provide a working and
tested Python implementation since the corresponding shared library might
not be present on the system.

.. note::

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.