Skip to main content

Python C Extension Patterns.

Project description

If you need to write C extension for Python then this is the place for you.

The full documentation is on Read the Docs.

Code examples and documentation source are right here on GitHub.

Videos

I have presented some of this, well mostly the chapter “PyObjects and Reference Counting”, at Python conferences so if you prefer videos they are here:

Subjects Covered

  • Introduction

  • A Simple Example

  • PyObjects and Reference Counting

  • Exception Raising

  • A Pythonic Coding Pattern for C Functions

  • Parsing Python Arguments

  • Creating New Types

  • Setting and Getting Module Globals

  • Logging

  • File Paths and Files

  • Subclassing and Using super()

  • Capsules

  • Iterators and Generators

  • Pickling C Extension Types

  • Setting Compiler Flags

  • Debugging

  • Memory Leaks

  • Thread Safety

  • Source Code Layout

  • Using C++ With CPython Code

  • Miscellaneous

  • Further Reading

Installation

This is primarily a documentation project hosted on Read the Docs. However all the example code is buildable and testable so if you want to examine that then here is how to get the project.

From PyPi

pip install cPyExtPatt

From Source

Choose a directory of your choice, in this case: ~/dev/tmp.

mkdir -p ~/dev/tmp
cd ~/dev/tmp
git clone https://github.com/paulross/PythonExtensionPatterns.git
cd PythonExtensionPatterns

Virtual Environment

Create a Python environment in the directory of your choice, in this case: ~/dev/tmp/PythonExtensionPatterns/venv_3.11 and activate it:

python3.11 -m venv venv_3.11
source venv_3.11/bin/activate

Install the Dependencies

pip install -r requirements.txt

Running the Tests

You now should be able to run the following commands successfully in ~/dev/tmp/PythonExtensionPatterns with your environment activated:

pytest tests/

Building the Documentation

This will build the html and PDF documentation (requires a latex installation):

cd doc/sphinx
make html latexpdf

History

0.2.1 (2024-07-29)

  • Python versions supported: 3.9, 3.10, 3.11, 3.12, 3.13 (possibly backwards compatible with Python 3.6, 3.7, 3.8)

  • Almost all example code is built and tested against these Python versions.

  • Added a chapter on managing file paths and files between Python and C.

  • Added a chapter on subclassing from your classes or builtin classes.

  • Added a chapter on pickling from C.

  • Added a chapter on Capsules.

  • Added a chapter on Iterators and Generators.

  • Added a chapter on memory leaks and how to detect them.

  • Added a chapter on thread safety.

  • Update “Homogeneous Python Containers and C++” to refer to https://github.com/paulross/PyCppContainers

  • All the documentation has been extensively reviewed and corrected where necessary.

  • Development Status :: 5 - Production/Stable

Contributors

Many thanks!

Pull Requests
Issues

0.1.0 (2014-09-09)

  • First release.

  • Originally “Examples of reliable coding of Python ‘C’ extensions by Paul Ross.”.

  • Development Status :: 3 - Alpha

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cpyextpatt-0.2.1.tar.gz (38.4 kB view details)

Uploaded Source

Built Distributions

cPyExtPatt-0.2.1-cp313-cp313-macosx_10_13_universal2.whl (225.5 kB view details)

Uploaded CPython 3.13 macOS 10.13+ universal2 (ARM64, x86-64)

cPyExtPatt-0.2.1-cp312-cp312-macosx_10_9_universal2.whl (227.9 kB view details)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

cPyExtPatt-0.2.1-cp311-cp311-macosx_10_9_universal2.whl (225.4 kB view details)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

cPyExtPatt-0.2.1-cp310-cp310-macosx_10_9_universal2.whl (223.1 kB view details)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

cPyExtPatt-0.2.1-cp39-cp39-macosx_10_9_universal2.whl (222.8 kB view details)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file cpyextpatt-0.2.1.tar.gz.

File metadata

  • Download URL: cpyextpatt-0.2.1.tar.gz
  • Upload date:
  • Size: 38.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.6

File hashes

Hashes for cpyextpatt-0.2.1.tar.gz
Algorithm Hash digest
SHA256 d2d22dc6ec17f3cb1128fb2d18d4d4f06d608ae5a2ef67f40266c3f433bfbe10
MD5 75974ca9975f7a6bd43260a1a3f2877c
BLAKE2b-256 333a7f519fb0afbac3a0a8d1bc232f0b3b730b02d8b1c989b19e4b2cdd194546

See more details on using hashes here.

File details

Details for the file cPyExtPatt-0.2.1-cp313-cp313-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for cPyExtPatt-0.2.1-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 b8b0f7655e0c840293a317de3ef18563fca6bbe729db18e9eebb137357639449
MD5 71da1ba8e913c6a495827f51ac017e6f
BLAKE2b-256 ed6fd780212450e07493d6fc7e9d22795420e464667453da9d4e61ee940a8cf8

See more details on using hashes here.

File details

Details for the file cPyExtPatt-0.2.1-cp312-cp312-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for cPyExtPatt-0.2.1-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 671e5b30b5174fed8645aa10cec4d96ff18a4cf4d7f39c6bed867b321f8c2873
MD5 325a1a497f8cbb57b393d90b138f85a6
BLAKE2b-256 a56296d4de6e9a44b1b0ec19a50174e90afa28368f32561a40d31fca477f9707

See more details on using hashes here.

File details

Details for the file cPyExtPatt-0.2.1-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for cPyExtPatt-0.2.1-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 ec915f2428133a304cfe5b881658a8544dcb9634307c14af675e4515dea5c050
MD5 9c75d33645bbcac83b819a01cd35cec4
BLAKE2b-256 7e11dbb5f5dbaed94ba5c4a936ab85f0c6fb3deab45b0444f9e1a59b92bfcea6

See more details on using hashes here.

File details

Details for the file cPyExtPatt-0.2.1-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for cPyExtPatt-0.2.1-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 f7b400790e4ad8551d7588526b31216000893e6d80c708fc2839d6d8f2a0b0c3
MD5 1834dd3643ad00904e7a0f66e2fd029d
BLAKE2b-256 0fb9aba4f662fb916735fd1630c05ab883a4c42ab79887a2fe7b3f311fb8dd9e

See more details on using hashes here.

File details

Details for the file cPyExtPatt-0.2.1-cp39-cp39-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for cPyExtPatt-0.2.1-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 c2191be9fff67e7001542068f6dcf9c50bb95493e8f84e3503b4f9dedf93c49e
MD5 e01a5bf1733477b15f03137b40171642
BLAKE2b-256 2640dd478a5b14daba76edc0838329845355aae6db2d0afe97d130dce62c60f1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page