SlideShare a Scribd company logo
Python Bindings OverviewRudáMoura : ruda.moura@gmail.comSébastien Tandel : sebastien.tandel@gmail.com
Python C APIPythonBrasil[5]Caxias do Sul, RS - Brazil
3Python C API : philosophyEverything is a PyObject*Public symbols with Py prefixPrivate symbols with _Py prefixObjects are stored in memory heapIt’s you job to count the referencesPy_INCREF & Py_DECREFYeah, that’s sucks!There are functions to convert C data types to Python C data types and backPyArg_ParseTuple()Py_BuildValue()
Python C APIA wrapper around two readline functions:readline - return a malloced stringadd_history - add a (const) string to the historychar *readline(const char *);intadd_history(const char *);
Python C APIreadline()#include <Python.h>#include <readline/readline.h>PyObject *fn_readline(PyObject *self, PyObject *args){  char *prompt;  char *line;PyObject *o;  if (!PyArg_ParseTuple(args, "s", &prompt))    return NULL;line = readline(prompt);o = Py_BuildValue("s", line);free(line);  return o;}
Python C APIadd_history()PyObject *fn_add_history(PyObject *self, PyObject *args){  char *line;int status;PyObject *o;  if (!PyArg_ParseTuple(args, "s", &line)) return NULL;status = add_history(line);o = Py_BuildValue("i", status);  return o;}And if I want to return None?Py_INCREF(Py_None); return Py_None;
Python C APIRegistering functions and modulesstatic PyMethodDefmethods[] = {  {"readline", fn_readline, METH_VARARGS, NULL},  {"add_history", fn_add_history, METH_VARARGS, NULL},  {NULL, NULL, 0, NULL}};PyMODINIT_FUNCinitreadline(void){  (void) Py_InitModule("readline", methods);}
Python C APICompiling...$ gcc -dynamiclib -I/usr/include/python2.5 -lpython -lreadline mod_readline.c -o readline.soOr you can use distutils / setup.py
Python C APIThe goodsStrong C/C++ and Python integrationThe best performance possibleFull Python data types supportThe badsIt’s hard work, only good for masochistsYou’re supposed to be good in CReference counts nightmare
Python C API : summary
SWIGSimplified Wrapper and Interface Generator
SWIG : philosophyEverything is defined in the interface fileModule nameLiteral #include or source codeDefine What symbols to exportThe swig compiler translates the interface file into C source code
SWIGThe interface file// readline.i%module readline%{#include <readline/readline.h>%}char *readline(const char *);intadd_history(const char *);
SWIGCompiling...$ swig -python readline.i$ gcc -creadline_wrap.c  -I/usr/include/python2.5$ gcc -dynamiclibreadline_wrap.o -lpython -lreadline -o _readline.soOr you can use distutils / setup.py
SWIGThe goodsFull bindings process automatizationStrong C++ support (class, template, exception)Not only for Python! Support for Perl, Ruby and othersIt’s mature (since 1995)The badsThe C file created is not for human consumCallbacks and double references are not easy
SWIG : summary
CTYPES
Ctypes : philosophyCtypes is a dlopen-like for PythonThe basic steps :Obtain a handle of a libraryUse this handle to access library’s functionsfrom ctypes import cdlllibc_h = cdll.LoadLibrary(“libc.so.6”)libc_h.mkdir(“python-mkdir-test”)
CtypesOK, it’s a dlopen-like, and?libc_h.mkdir(“python-mkdir-test”)It’s a Python string!Transparent conversion for these types :None, integers, long, byte strings and unicode strings
CtypesWhat if I want specific C types?c_int, c_long, c_char, c_short, c_ulong, …i = c_int(10)print i.value()10
CtypesFunctions return values?By default, assumed to be intfrom ctypes import cdll, c_char_plr = cdll.LoadLibrary("libreadline.dylib")while True:  line = lr.readline(“C:\>")c_line = c_char_p(line)  if not c_line.value: break
CtypesReturn value as input for another C function?from ctypes import cdll, c_char_plr = cdll.LoadLibrary("libreadline.dylib")while True:  line = lr.readline(“C:\>")c_line = c_char_p(line)  if not c_line.value: breaklr.add_history(c_line.value)  # orlr.add_history(c_line) 	  # orlr.add_history(line)
CtypesIs there a way to simplify a bit?from ctypes import cdll, c_char_plr = cdll.LoadLibrary("libreadline.dylib")lr.readline.restype = c_char_pwhile True:  line = lr.readline("c:>")  if not line: breaklr.add_history(line)You can define types for arguments too with .argtypes!
CtypesPossible to access structures returned by C functionsPossible to define arrays … but with a fixed length!Possible to create callback written in Python called by C lib.On windows, check the number of parameters passed to a function.Part of Python 2.5
Ctypes : summary
Cython
Cython : philosophyCython is an extension of Python languageThree simple steps :Write “python”Compile the module it with C compilerImport the package in your python code
CythonCompiling a module => setuptoolsprint “hello world!”setup.pyfrom distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_extsetup(cmdclass = {'build_ext': build_ext},ext_modules = [Extension(”hello", [”hello.pyx"])])
Cythoncdef extern from "readline/readline.h":  char* readline(char*)intadd_history(char*)def readline_loop():  while True:    line = readline("c:\>”)    if not line: breakadd_history(line)
Cythonsetup.pyfrom distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_extsetup(cmdclass = {'build_ext': build_ext},ext_modules = [Extension(‘readlineloop’, 			    [‘readline-loop.pyx’],libraries = [‘readline’])])
CythonLast details :Can access C structuresPossible to define callbacksCheck on number of argsPossible to wrap C++ classes!Handle exceptions!Can be used to optimize your codeneed to learn a bit moreCythonSome limitations :No support to yieldNo nested defNo globals(), locals()Class / func can’t be placed inside control structures
Cython : summary
Conclusions
Thank You!ruda.moura@gmail.comsebastien.tandel@gmail.com
Python Bindings Overview

More Related Content

PDF
Object-Oriented Programming in Modern C++. Borislav Stanimirov. CoreHard Spri...
PDF
Rcpp
PDF
Integrating R with C++: Rcpp, RInside and RProtoBuf
PPTX
introduction of c langauge(I unit)
PPT
Glimpses of C++0x
PPTX
basics of c++
PPTX
Python vs c++ ppt
PPT
History of c++
Object-Oriented Programming in Modern C++. Borislav Stanimirov. CoreHard Spri...
Rcpp
Integrating R with C++: Rcpp, RInside and RProtoBuf
introduction of c langauge(I unit)
Glimpses of C++0x
basics of c++
Python vs c++ ppt
History of c++

What's hot (20)

PDF
C++ vs python the best ever comparison
PDF
C++ vs python
DOCX
C tutorials
DOCX
GNU GCC - what just a compiler...?
PPT
C introduction by piyushkumar
PPTX
C++ vs C#
PPTX
Introduction to c programming language
PDF
Comparison between python and c++
PDF
علم البيانات - Data Sience
PPT
Csc1100 lecture01 ch01-pt1
PPT
Csc1100 lecture01 ch01-pt1
PPT
PPTX
Intro of C
PPT
History of c++
PDF
C fundamentals
PDF
Cs gate-2011
PPTX
R and Python, A Code Demo
PPT
The Python Programming Language and HDF5: H5Py
PPT
Lecture01
PPT
Substituting HDF5 tools with Python/H5py scripts
C++ vs python the best ever comparison
C++ vs python
C tutorials
GNU GCC - what just a compiler...?
C introduction by piyushkumar
C++ vs C#
Introduction to c programming language
Comparison between python and c++
علم البيانات - Data Sience
Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1
Intro of C
History of c++
C fundamentals
Cs gate-2011
R and Python, A Code Demo
The Python Programming Language and HDF5: H5Py
Lecture01
Substituting HDF5 tools with Python/H5py scripts
Ad

Viewers also liked (19)

PDF
Blockchain overview, use cases, implementations and challenges
PDF
ACI Mercato auto usate novembre 2012
PDF
Listino Prezzi McLaren 570GT
PPTX
Python For Large Company?
PPTX
SWIG Hello World
PDF
Using SWIG to Control, Prototype, and Debug C Programs with Python
PDF
SWIG : An Easy to Use Tool for Integrating Scripting Languages with C and C++
PDF
Interfacing C/C++ and Python with SWIG
PDF
Brussels Hyperledger Meetup - IBM Blockchain Explained
PPTX
Blockchain for Business
PPTX
Bitcoin, Blockchain, and IoT
PDF
Industria 4.0 e IoT: Panorama, Leggende e Standard
PPTX
Blockchain Perspective - Internet of Memorable Things
PPTX
Practical Applications of Block Chain Technologies
PDF
170321 cebit blockchain summit frank bolten
PPTX
State of Blockchain Q4 2016
PDF
The design thinking transformation in business
PPTX
Blockchain in IoT and Other Considerations by Dinis Guarda
PDF
The Top Skills That Can Get You Hired in 2017
Blockchain overview, use cases, implementations and challenges
ACI Mercato auto usate novembre 2012
Listino Prezzi McLaren 570GT
Python For Large Company?
SWIG Hello World
Using SWIG to Control, Prototype, and Debug C Programs with Python
SWIG : An Easy to Use Tool for Integrating Scripting Languages with C and C++
Interfacing C/C++ and Python with SWIG
Brussels Hyperledger Meetup - IBM Blockchain Explained
Blockchain for Business
Bitcoin, Blockchain, and IoT
Industria 4.0 e IoT: Panorama, Leggende e Standard
Blockchain Perspective - Internet of Memorable Things
Practical Applications of Block Chain Technologies
170321 cebit blockchain summit frank bolten
State of Blockchain Q4 2016
The design thinking transformation in business
Blockchain in IoT and Other Considerations by Dinis Guarda
The Top Skills That Can Get You Hired in 2017
Ad

Similar to Python Bindings Overview (20)

ODP
C Types - Extending Python
PDF
Extending Python - EuroPython 2014
PDF
Interfacing C++ with Python to boost your legacy apps with Python interfaces
PDF
PyHEP 2018: Tools to bind to Python
PDF
Cython compiler
PDF
Cluj.py Meetup: Extending Python in C
PDF
PyCon2022 - Building Python Extensions
PPTX
Ctypes
PDF
Extending Python with ctypes
PDF
Bind Python and C @ COSCUP 2015
PPTX
Kostiantyn Grygoriev "Wrapping C++ for Python"
ODP
Learn python
KEY
Mypy pycon-fi-2012
PDF
Cython - close to metal Python
PDF
Notes about moving from python to c++ py contw 2020
PDF
OpenSAF Symposium_Python Bindings_9.21.11
PPTX
Mixing C++ & Python II: Pybind11
ODP
Pythonpresent
PDF
Start Wrap Episode 11: A New Rope
PDF
Development_C_Extension_with_Pybind11.pdf
C Types - Extending Python
Extending Python - EuroPython 2014
Interfacing C++ with Python to boost your legacy apps with Python interfaces
PyHEP 2018: Tools to bind to Python
Cython compiler
Cluj.py Meetup: Extending Python in C
PyCon2022 - Building Python Extensions
Ctypes
Extending Python with ctypes
Bind Python and C @ COSCUP 2015
Kostiantyn Grygoriev "Wrapping C++ for Python"
Learn python
Mypy pycon-fi-2012
Cython - close to metal Python
Notes about moving from python to c++ py contw 2020
OpenSAF Symposium_Python Bindings_9.21.11
Mixing C++ & Python II: Pybind11
Pythonpresent
Start Wrap Episode 11: A New Rope
Development_C_Extension_with_Pybind11.pdf

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Empathic Computing: Creating Shared Understanding
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Advanced IT Governance
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPT
Teaching material agriculture food technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
KodekX | Application Modernization Development
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Review of recent advances in non-invasive hemoglobin estimation
Chapter 3 Spatial Domain Image Processing.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Empathic Computing: Creating Shared Understanding
Network Security Unit 5.pdf for BCA BBA.
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Advanced IT Governance
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Teaching material agriculture food technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The Rise and Fall of 3GPP – Time for a Sabbatical?
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
KodekX | Application Modernization Development
CIFDAQ's Market Insight: SEC Turns Pro Crypto
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....

Python Bindings Overview

  • 1. Python Bindings OverviewRudáMoura : ruda.moura@gmail.comSébastien Tandel : sebastien.tandel@gmail.com
  • 3. 3Python C API : philosophyEverything is a PyObject*Public symbols with Py prefixPrivate symbols with _Py prefixObjects are stored in memory heapIt’s you job to count the referencesPy_INCREF & Py_DECREFYeah, that’s sucks!There are functions to convert C data types to Python C data types and backPyArg_ParseTuple()Py_BuildValue()
  • 4. Python C APIA wrapper around two readline functions:readline - return a malloced stringadd_history - add a (const) string to the historychar *readline(const char *);intadd_history(const char *);
  • 5. Python C APIreadline()#include <Python.h>#include <readline/readline.h>PyObject *fn_readline(PyObject *self, PyObject *args){ char *prompt; char *line;PyObject *o; if (!PyArg_ParseTuple(args, "s", &prompt)) return NULL;line = readline(prompt);o = Py_BuildValue("s", line);free(line); return o;}
  • 6. Python C APIadd_history()PyObject *fn_add_history(PyObject *self, PyObject *args){ char *line;int status;PyObject *o; if (!PyArg_ParseTuple(args, "s", &line)) return NULL;status = add_history(line);o = Py_BuildValue("i", status); return o;}And if I want to return None?Py_INCREF(Py_None); return Py_None;
  • 7. Python C APIRegistering functions and modulesstatic PyMethodDefmethods[] = { {"readline", fn_readline, METH_VARARGS, NULL}, {"add_history", fn_add_history, METH_VARARGS, NULL}, {NULL, NULL, 0, NULL}};PyMODINIT_FUNCinitreadline(void){ (void) Py_InitModule("readline", methods);}
  • 8. Python C APICompiling...$ gcc -dynamiclib -I/usr/include/python2.5 -lpython -lreadline mod_readline.c -o readline.soOr you can use distutils / setup.py
  • 9. Python C APIThe goodsStrong C/C++ and Python integrationThe best performance possibleFull Python data types supportThe badsIt’s hard work, only good for masochistsYou’re supposed to be good in CReference counts nightmare
  • 10. Python C API : summary
  • 11. SWIGSimplified Wrapper and Interface Generator
  • 12. SWIG : philosophyEverything is defined in the interface fileModule nameLiteral #include or source codeDefine What symbols to exportThe swig compiler translates the interface file into C source code
  • 13. SWIGThe interface file// readline.i%module readline%{#include <readline/readline.h>%}char *readline(const char *);intadd_history(const char *);
  • 14. SWIGCompiling...$ swig -python readline.i$ gcc -creadline_wrap.c -I/usr/include/python2.5$ gcc -dynamiclibreadline_wrap.o -lpython -lreadline -o _readline.soOr you can use distutils / setup.py
  • 15. SWIGThe goodsFull bindings process automatizationStrong C++ support (class, template, exception)Not only for Python! Support for Perl, Ruby and othersIt’s mature (since 1995)The badsThe C file created is not for human consumCallbacks and double references are not easy
  • 18. Ctypes : philosophyCtypes is a dlopen-like for PythonThe basic steps :Obtain a handle of a libraryUse this handle to access library’s functionsfrom ctypes import cdlllibc_h = cdll.LoadLibrary(“libc.so.6”)libc_h.mkdir(“python-mkdir-test”)
  • 19. CtypesOK, it’s a dlopen-like, and?libc_h.mkdir(“python-mkdir-test”)It’s a Python string!Transparent conversion for these types :None, integers, long, byte strings and unicode strings
  • 20. CtypesWhat if I want specific C types?c_int, c_long, c_char, c_short, c_ulong, …i = c_int(10)print i.value()10
  • 21. CtypesFunctions return values?By default, assumed to be intfrom ctypes import cdll, c_char_plr = cdll.LoadLibrary("libreadline.dylib")while True: line = lr.readline(“C:\>")c_line = c_char_p(line) if not c_line.value: break
  • 22. CtypesReturn value as input for another C function?from ctypes import cdll, c_char_plr = cdll.LoadLibrary("libreadline.dylib")while True: line = lr.readline(“C:\>")c_line = c_char_p(line) if not c_line.value: breaklr.add_history(c_line.value) # orlr.add_history(c_line) # orlr.add_history(line)
  • 23. CtypesIs there a way to simplify a bit?from ctypes import cdll, c_char_plr = cdll.LoadLibrary("libreadline.dylib")lr.readline.restype = c_char_pwhile True: line = lr.readline("c:>") if not line: breaklr.add_history(line)You can define types for arguments too with .argtypes!
  • 24. CtypesPossible to access structures returned by C functionsPossible to define arrays … but with a fixed length!Possible to create callback written in Python called by C lib.On windows, check the number of parameters passed to a function.Part of Python 2.5
  • 27. Cython : philosophyCython is an extension of Python languageThree simple steps :Write “python”Compile the module it with C compilerImport the package in your python code
  • 28. CythonCompiling a module => setuptoolsprint “hello world!”setup.pyfrom distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_extsetup(cmdclass = {'build_ext': build_ext},ext_modules = [Extension(”hello", [”hello.pyx"])])
  • 29. Cythoncdef extern from "readline/readline.h": char* readline(char*)intadd_history(char*)def readline_loop(): while True: line = readline("c:\>”) if not line: breakadd_history(line)
  • 30. Cythonsetup.pyfrom distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_extsetup(cmdclass = {'build_ext': build_ext},ext_modules = [Extension(‘readlineloop’, [‘readline-loop.pyx’],libraries = [‘readline’])])
  • 31. CythonLast details :Can access C structuresPossible to define callbacksCheck on number of argsPossible to wrap C++ classes!Handle exceptions!Can be used to optimize your codeneed to learn a bit moreCythonSome limitations :No support to yieldNo nested defNo globals(), locals()Class / func can’t be placed inside control structures