SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
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™!
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
Ship it
Ever had trouble installing packages?
(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

Contenu connexe

Tendances

Plasmaquick Workshop - FISL 13
Plasmaquick Workshop - FISL 13Plasmaquick Workshop - FISL 13
Plasmaquick Workshop - FISL 13
Daker Fernandes
 
How to integrate python into a scala stack
How to integrate python into a scala stackHow to integrate python into a scala stack
How to integrate python into a scala stack
Fliptop
 

Tendances (20)

Ways to generate PDF from Python Web applications, Gaël Le Mignot
Ways to generate PDF from Python Web applications, Gaël Le MignotWays to generate PDF from Python Web applications, Gaël Le Mignot
Ways to generate PDF from Python Web applications, Gaël Le Mignot
 
Easy native wrappers with SWIG
Easy native wrappers with SWIGEasy native wrappers with SWIG
Easy native wrappers with SWIG
 
How we do python
How we do pythonHow we do python
How we do python
 
Python in a real life
Python in a real lifePython in a real life
Python in a real life
 
Reactive Programming by UniRx for Asynchronous & Event Processing
Reactive Programming by UniRx for Asynchronous & Event ProcessingReactive Programming by UniRx for Asynchronous & Event Processing
Reactive Programming by UniRx for Asynchronous & Event Processing
 
Enterprise python
Enterprise pythonEnterprise python
Enterprise python
 
Introduction to Tensorflow.js
Introduction to Tensorflow.jsIntroduction to Tensorflow.js
Introduction to Tensorflow.js
 
Python to scala
Python to scalaPython to scala
Python to scala
 
Paris.py
Paris.pyParis.py
Paris.py
 
Plasmaquick Workshop - FISL 13
Plasmaquick Workshop - FISL 13Plasmaquick Workshop - FISL 13
Plasmaquick Workshop - FISL 13
 
ML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.jsML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.js
 
How to integrate python into a scala stack
How to integrate python into a scala stackHow to integrate python into a scala stack
How to integrate python into a scala stack
 
Go. Why it goes
Go. Why it goesGo. Why it goes
Go. Why it goes
 
Groovy and Grails intro
Groovy and Grails introGroovy and Grails intro
Groovy and Grails intro
 
Calling python from r
Calling python from rCalling python from r
Calling python from r
 
How to Write Node.js Module
How to Write Node.js ModuleHow to Write Node.js Module
How to Write Node.js Module
 
C Under Linux
C Under LinuxC Under Linux
C Under Linux
 
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoya
 

Similaire à Writing a Python C extension

Similaire à Writing a Python C extension (20)

PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
 
Python on a chip
Python on a chipPython on a chip
Python on a chip
 
PySide
PySidePySide
PySide
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
 
First python project
First python projectFirst python project
First python project
 
Cython - close to metal Python
Cython - close to metal PythonCython - close to metal Python
Cython - close to metal Python
 
05 python.pdf
05 python.pdf05 python.pdf
05 python.pdf
 
Software Quality Assurance Tooling - Wintersession 2024
Software Quality Assurance Tooling - Wintersession 2024Software Quality Assurance Tooling - Wintersession 2024
Software Quality Assurance Tooling - Wintersession 2024
 
Pythonpresent
PythonpresentPythonpresent
Pythonpresent
 
Qt for Python
Qt for PythonQt for Python
Qt for Python
 
Princeton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance ToolingPrinceton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance Tooling
 
osakapy 2014.10 LT (CI for Python Project)
osakapy 2014.10 LT (CI for Python Project)osakapy 2014.10 LT (CI for Python Project)
osakapy 2014.10 LT (CI for Python Project)
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
 
Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"
Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"
Kyrylo Cherneha "C++ & Python Interaction in Automotive Industry"
 
Digital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meetingDigital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meeting
 
PyCourse - Self driving python course
PyCourse - Self driving python coursePyCourse - Self driving python course
PyCourse - Self driving python course
 
Interfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIGInterfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIG
 
Software Quality Assurance Tooling 2023
Software Quality Assurance Tooling 2023Software Quality Assurance Tooling 2023
Software Quality Assurance Tooling 2023
 
Why "Hello World" is a Massive Operation - From Python code to Stack Virtual ...
Why "Hello World" is a Massive Operation - From Python code to Stack Virtual ...Why "Hello World" is a Massive Operation - From Python code to Stack Virtual ...
Why "Hello World" is a Massive Operation - From Python code to Stack Virtual ...
 
C pythontalk
C pythontalkC pythontalk
C pythontalk
 

Plus de Sqreen

Plus de Sqreen (8)

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

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

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 jb@sqreen.io @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™!
  • 4.
  • 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
  • 15.
  • 16.
  • 18. Ever had trouble installing packages?
  • 19.
  • 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