Git Product home page Git Product logo

c-extension-tutorial's Introduction

How to Write and Debug C Extension Modules

The CPython interpreter allows us implement modules in C for performance critical code or to interface with external libraries while presenting users with a high level Python API. This tutorial will teach you how to leverage the power of C in your Python projects.

We will start by explaining the C representation of Python objects and how to manipulate them from within C. We will then move on to implementing functions in C for use in Python. We will discuss reference counting and correct exception handling. We will also talk about how to package and build your new extension module so that it may be shared on PyPI. (We will only be covering building extension modules on GNU/Linux and OSX, not Windows).

After the break, we will show how to implement a new type in C. This will cover how to hook into various protocols and properly support cyclic garbage collection. We will also discuss techniques for debugging C extension modules with gdb using the CPython gdb extension.

Install Steps

Prior to the tutorial, attendees need to install a git, a C89 compatible C compiler and gdb. OSX users will also need to install brew.

For the C compiler: I recommend gcc, but clang works as well. The tutorial will be using gdb specific features so gdb is reqiured. Other C debuggers like lldb will not work.

Once all of the system packages have been installed, run the following commands in a terminal:

$ git clone --recursive https://github.com/llllllllll/c-extension-tutorial.git
$ cd c-extension-tutorial
$ source etc/setup-env

The setup-env script will compile a debug version of CPython 3.6 and create a local virtual env with this new interpreter. This ensures that everyone is using the same version of CPython with the same compile time flags.

Viewing the Tutorial

The tutorial is structured as a sphinx project. This allows the tutorial to be viewed from a standard browser or hosted online.

The material can be viewed in a browser by opening tutorial/build/html/index.html, for example:

$ ${BROWSER} tutorial/build/html/index.html

c-extension-tutorial's People

Contributors

jonkiparsky avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.