SlideShare a Scribd company logo
Writing a C Python
extension in 2018
Paris.py 2018
Jean-Baptiste Aviat
CTO & Co-founder of sqreen.io
Former Hacker at Apple (Red Team)
PyMiniRacer author
jb@sqreen.io
@jbaviat
Who Am I?
Someday… we needed to
use V8 from Python.
What we ship:
• is public
• is widely used
• need to have frictionless install
$ pip install sqreen
—> it just works™!
Writing a Python C extension
PyMiniRacer: cool JS binding
github.com/sqreen/PyMiniRacer
The problem
• V8 is C++
• How do you run C++ in Python?
…We need some kind of binding
between these 2 worlds.
Many popular
packages rely
on binary
cryptography
numpy
pymongo
psycopg
simplejson
lxml
sqlalchemy…
People do it!

Let’s do it too.
What are our goals?
• minimize maintenance
• make setup easy
• make testing easy
• have great performance
• have low memory fingerprint
• dev time is a constraint
We want to: And (obviously)…
built-in pythonic
Python version
independant
open to other
languages
high
throughput
capable
CPython
✔ ✔ ✔
ctypes ✔ ✔ ✔
cffi ✔ ✔ ✔ ✔
Cython ✔ ✔
SWIG ✔ ✔
ctypes
Built into Python
Binary is Python independant:
• can be used on any Python
version
• can be used in other languages!
No tight integration to Python
• not high throughput
capable
• less Pythonic
Complex syntax (C types
wrapped in Python…)
Not for C++
👍 👎
$ python
>>> path = "./hello.so"
>>> import ctypes
>>> lib = ctypes.cdll.LoadLibrary(path)
>>> lib.hello_world()
Hello world!
C file
Python
interface
binary

object
Overview
V8 (C++ interface)C interface to V8Python interface
3rd party
binaries
import ctypes
class PyMiniRacer(object):
…
#include <v8.h>
int miniracer_init();
…
V8 library (libv8.a)
V8 headers (v8.h)
linkingctypes
C/C++
code
Python
library
How to put this together?
$ cat setup.py
from distutils.core import setup, Extension
extension = Extension('hello', ['hello.c'])
setup(name=‘hello',
version='1.0',
ext_modules=[extension])
$ python setup.py build
running build
running build_ext
building 'hello' extension
clang […] -c hello.c -o hello.o
creating build/lib.macosx-10.6-intel-2.7
clang -bundle […] hello.o -o hello.so
Writing a Python C extension
Writing a Python C extension
Ship it
Ever had trouble installing packages?
Writing a Python C extension
(at Sqreen, we are
dog friendly)
Come get your dog sticker!
Python packaging

history
sdist (source distribution)
eggs
wheels
—> manylinux wheels
(built distribution)
2004
2012
2016
Python 2.4
Python 3.3
Python 3.6 ❤
manylinux wheels
Python standard:
PEP513 / PEP571
Compatible on most
(real world) Linux
Only in pip >= 8.1
Need build for all Python versions
Binaries need to be built on
🚨🚨CentOS 5🚨🚨
👍 👎
CentOS 5??
PEP571 → bumped requirement
to CentOS 6!
Wheels vs compilation
iso builds (crash can
be reproduced)
many packages
need maintenance
one build per user
only one package
harder to install…
Best of 2 worlds
We can have:
ctypes: use a Python independant binary
wheels: ship compiled binaries
By combining these…
🥂🍾 ctypes + wheels = ship only one binary
Thanks!
Questions?
We’re

hiring!
https://sqreen.io/jobs

More Related Content

What's hot (20)

PDF
Ways to generate PDF from Python Web applications, Gaël Le Mignot
Pôle Systematic Paris-Region
 
PDF
Easy native wrappers with SWIG
Javier Arturo Rodríguez
 
PDF
How we do python
Brice Laurencin
 
PDF
Python in a real life
Vladimir Filonov
 
PDF
Reactive Programming by UniRx for Asynchronous & Event Processing
Yoshifumi Kawai
 
PDF
Enterprise python
pythonchile
 
PDF
Introduction to Tensorflow.js
Riza Fahmi
 
PDF
Python to scala
kao kuo-tung
 
PDF
Paris.py
Dimitri Merejkowsky
 
PDF
Plasmaquick Workshop - FISL 13
Daker Fernandes
 
PDF
ML in the Browser: Interactive Experiences with Tensorflow.js
C4Media
 
PPTX
How to integrate python into a scala stack
Fliptop
 
PPTX
Go. Why it goes
Sergey Pichkurov
 
ODP
Groovy and Grails intro
Miguel Pastor
 
PPTX
Calling python from r
Barry DeCicco
 
PDF
How to Write Node.js Module
Fred Chien
 
ODP
C Under Linux
mohan43u
 
PDF
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Romain Dorgueil
 
PPTX
Introduction to Python
Bogdan Sabău
 
PDF
D vs OWKN Language at LLnagoya
N Masahiro
 
Ways to generate PDF from Python Web applications, Gaël Le Mignot
Pôle Systematic Paris-Region
 
Easy native wrappers with SWIG
Javier Arturo Rodríguez
 
How we do python
Brice Laurencin
 
Python in a real life
Vladimir Filonov
 
Reactive Programming by UniRx for Asynchronous & Event Processing
Yoshifumi Kawai
 
Enterprise python
pythonchile
 
Introduction to Tensorflow.js
Riza Fahmi
 
Python to scala
kao kuo-tung
 
Plasmaquick Workshop - FISL 13
Daker Fernandes
 
ML in the Browser: Interactive Experiences with Tensorflow.js
C4Media
 
How to integrate python into a scala stack
Fliptop
 
Go. Why it goes
Sergey Pichkurov
 
Groovy and Grails intro
Miguel Pastor
 
Calling python from r
Barry DeCicco
 
How to Write Node.js Module
Fred Chien
 
C Under Linux
mohan43u
 
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Romain Dorgueil
 
Introduction to Python
Bogdan Sabău
 
D vs OWKN Language at LLnagoya
N Masahiro
 

Similar to Writing a Python C extension (20)

PDF
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
Pôle Systematic Paris-Region
 
PDF
PyCon2022 - Building Python Extensions
Henry Schreiner
 
ODP
C Types - Extending Python
Priyank Kapadia
 
PDF
Cython compiler
Tanikella Sai Abhijyan
 
PDF
Extending Python with ctypes
Anant Narayanan
 
PDF
Call a C API from Python becomes more enjoyable with CFFI, Jean-Sébastien Bev...
Pôle Systematic Paris-Region
 
PDF
Cython - close to metal Python
Taras Lyapun
 
PPTX
C pythontalk
Nicholaus Jackson
 
PPTX
Python Bindings Overview
Sébastien Tandel
 
PDF
Modern binary build systems - PyCon 2024
Henry Schreiner
 
PDF
Development_C_Extension_with_Pybind11.pdf
Takayuki Suzuki
 
PDF
Extending Python - EuroPython 2014
fcofdezc
 
PPTX
Ctypes
嘉駿 戴
 
PDF
Thinking Hybrid - Python/C++ Integration
Guy K. Kloss
 
PDF
The Python in the Apple
zeroSteiner
 
PPTX
Kostiantyn Grygoriev "Wrapping C++ for Python"
LogeekNightUkraine
 
PDF
PyHEP 2018: Tools to bind to Python
Henry Schreiner
 
PDF
Pybind11 - SciPy 2021
Henry Schreiner
 
PDF
Interfacing C++ with Python to boost your legacy apps with Python interfaces
Ovidiu Farauanu
 
PDF
Extending Python - Codemotion Milano 2014
fcofdezc
 
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
Pôle Systematic Paris-Region
 
PyCon2022 - Building Python Extensions
Henry Schreiner
 
C Types - Extending Python
Priyank Kapadia
 
Cython compiler
Tanikella Sai Abhijyan
 
Extending Python with ctypes
Anant Narayanan
 
Call a C API from Python becomes more enjoyable with CFFI, Jean-Sébastien Bev...
Pôle Systematic Paris-Region
 
Cython - close to metal Python
Taras Lyapun
 
C pythontalk
Nicholaus Jackson
 
Python Bindings Overview
Sébastien Tandel
 
Modern binary build systems - PyCon 2024
Henry Schreiner
 
Development_C_Extension_with_Pybind11.pdf
Takayuki Suzuki
 
Extending Python - EuroPython 2014
fcofdezc
 
Ctypes
嘉駿 戴
 
Thinking Hybrid - Python/C++ Integration
Guy K. Kloss
 
The Python in the Apple
zeroSteiner
 
Kostiantyn Grygoriev "Wrapping C++ for Python"
LogeekNightUkraine
 
PyHEP 2018: Tools to bind to Python
Henry Schreiner
 
Pybind11 - SciPy 2021
Henry Schreiner
 
Interfacing C++ with Python to boost your legacy apps with Python interfaces
Ovidiu Farauanu
 
Extending Python - Codemotion Milano 2014
fcofdezc
 
Ad

More from Sqreen (8)

PDF
Protecting against injections at scale
Sqreen
 
PDF
Serverless security - how to protect what you don't see?
Sqreen
 
PDF
Api days 2018 - API Security by Sqreen
Sqreen
 
PDF
NoSQL Injections in Node.js - The case of MongoDB
Sqreen
 
PDF
Application Security from the Inside - OWASP
Sqreen
 
PDF
Tune your App Perf (and get fit for summer)
Sqreen
 
PDF
Instrument Rack to visualize
 Rails requests processing
Sqreen
 
PDF
Ruby on Rails security in your Continuous Integration
Sqreen
 
Protecting against injections at scale
Sqreen
 
Serverless security - how to protect what you don't see?
Sqreen
 
Api days 2018 - API Security by Sqreen
Sqreen
 
NoSQL Injections in Node.js - The case of MongoDB
Sqreen
 
Application Security from the Inside - OWASP
Sqreen
 
Tune your App Perf (and get fit for summer)
Sqreen
 
Instrument Rack to visualize
 Rails requests processing
Sqreen
 
Ruby on Rails security in your Continuous Integration
Sqreen
 
Ad

Recently uploaded (20)

PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 

Writing a Python C extension

  • 1. Writing a C Python extension in 2018 Paris.py 2018
  • 2. Jean-Baptiste Aviat CTO & Co-founder of sqreen.io Former Hacker at Apple (Red Team) PyMiniRacer author [email protected] @jbaviat Who Am I?
  • 3. Someday… we needed to use V8 from Python. What we ship: • is public • is widely used • need to have frictionless install $ pip install sqreen —> it just works™!
  • 5. PyMiniRacer: cool JS binding github.com/sqreen/PyMiniRacer
  • 6. The problem • V8 is C++ • How do you run C++ in Python? …We need some kind of binding between these 2 worlds.
  • 7. Many popular packages rely on binary cryptography numpy pymongo psycopg simplejson lxml sqlalchemy…
  • 9. What are our goals? • minimize maintenance • make setup easy • make testing easy • have great performance • have low memory fingerprint • dev time is a constraint We want to: And (obviously)…
  • 10. built-in pythonic Python version independant open to other languages high throughput capable CPython ✔ ✔ ✔ ctypes ✔ ✔ ✔ cffi ✔ ✔ ✔ ✔ Cython ✔ ✔ SWIG ✔ ✔
  • 11. ctypes Built into Python Binary is Python independant: • can be used on any Python version • can be used in other languages! No tight integration to Python • not high throughput capable • less Pythonic Complex syntax (C types wrapped in Python…) Not for C++ 👍 👎
  • 12. $ python >>> path = "./hello.so" >>> import ctypes >>> lib = ctypes.cdll.LoadLibrary(path) >>> lib.hello_world() Hello world! C file Python interface binary
 object
  • 13. Overview V8 (C++ interface)C interface to V8Python interface 3rd party binaries import ctypes class PyMiniRacer(object): … #include <v8.h> int miniracer_init(); … V8 library (libv8.a) V8 headers (v8.h) linkingctypes C/C++ code Python library
  • 14. How to put this together? $ cat setup.py from distutils.core import setup, Extension extension = Extension('hello', ['hello.c']) setup(name=‘hello', version='1.0', ext_modules=[extension]) $ python setup.py build running build running build_ext building 'hello' extension clang […] -c hello.c -o hello.o creating build/lib.macosx-10.6-intel-2.7 clang -bundle […] hello.o -o hello.so
  • 18. Ever had trouble installing packages?
  • 20. (at Sqreen, we are dog friendly) Come get your dog sticker!
  • 21. Python packaging
 history sdist (source distribution) eggs wheels —> manylinux wheels (built distribution) 2004 2012 2016 Python 2.4 Python 3.3 Python 3.6 ❤
  • 22. manylinux wheels Python standard: PEP513 / PEP571 Compatible on most (real world) Linux Only in pip >= 8.1 Need build for all Python versions Binaries need to be built on 🚨🚨CentOS 5🚨🚨 👍 👎
  • 23. CentOS 5?? PEP571 → bumped requirement to CentOS 6!
  • 24. Wheels vs compilation iso builds (crash can be reproduced) many packages need maintenance one build per user only one package harder to install…
  • 25. Best of 2 worlds We can have: ctypes: use a Python independant binary wheels: ship compiled binaries By combining these… 🥂🍾 ctypes + wheels = ship only one binary