1 of 8

Porting Open source CPython Encryption Lib to Python 3 - Lessons Learnt

By Ashu Dagar

2 of 8

OVERVIEW OF THE TALK

  • Introduction to CPython
  • Need to convert the CPython extensions
  • The difference between Python 2.7 and Python3 with CPython
  • Guidelines to follow for migration of any library
  • Q&A

3 of 8

CPython

CPython is the reference implementation of the Python programming language.

Written in C and Python, CPython is the default and most widely-used implementation of the language.

CPython can be defined as both an interpreter and a compiler as it compiles Python code into bytecode before interpreting it.

4 of 8

Why CPython Extensions

You might be wondering... Python is a fantastic high level language capable of just about anything, why would I want to deal with messy C code?

And I would have to agree with the general premise of that argument. However, there are two common use cases I have found where this is likely to come up:

  1. to speed up a particular slow piece of Python code and,
  2. you are forced to include a program already written in C into an establish Python program and you don't want to rewrite the C code in Python.

The latter happened to me recently and I wanted to share what I've learned with you.

5 of 8

Differences in Python 2 and 3 with CPython

  • 2to3
  • Unicode
  • Exception Handling
  • xrange
  • print function

Unicode is the factor which produces the different outputs in CPython.

6 of 8

Guidelines for Migrating C Extensions

  1. Obtain or write C code
  2. Write Python C API wrapper function
  3. Define function(s) table
  4. Define module
  5. Write initialization function
  6. Package and build the extension

7 of 8

References

8 of 8

Q&A