PyCon JP 2024

Your locale preferences have been saved. We like to think that we have excellent support for English in pretalx, but if you encounter issues or errors, please contact us!

Sleuthing in Cython: Wrapping and Debugging Legacy C Libraries for Python
2024-09-28 , 4F Track4

Cython, an optimizing static compiler for Python, provides an accessible way to expose legacy C/C++ programs to Python users with minimal background knowledge. However, once initial setup is complete, debugging issues in both the build and runtime can be challenging. The underlying C library might obscure problems in memory management, and the lack of "training wheels" compared to Python can result in system errors, like segfaults or obscure error codes that are difficult to debug. This session explores the tools and workflows essential for working with Cython, identifying common errors, and presenting lessons learned from wrapping a geometric triangulation C library for using with Python via Cython.


In Sleuthing in Cython, we'll go over the benefits and challenges of utilizing Cython as a way to utilize legacy C/C++ programs and libraries within Python applications. Cython's capabilities permit users to use Python-like syntax to integrate with existing C code and build their own library and program interfaces accessible from both C and Python. This talk will cover essential tools and workflows for building with Cython and what the interface looks like for integrating with legacy C code, and how to build and package a Cython extension with Poetry.

However, utilizing the full powers of Cython is not without its own set of challenges. The presentation will also provide practical insights into common errors encountered during build and runtime phases while developing in Cython. Attendees will learn how to navigate these issues, using a detailed account of the specific obstacles faced when wrapping a legacy C library for Python via Cython. We will discuss different memory management and debugging tools, with a focus on Valgrind, and how to utilize these tools during development in Cython.


Why did you choose this topic?

I chose this topic because Cython is a powerful tool in the Python ecosystem that can bridge the gap between high-level Python code and low-level C performance. While I've previously built a Cython library from scratch, when I had the opportunity to wrap an existing C library, I stumbled upon an entirely different set of issues in development. I'd like to share how I approached these challenges and what tools I found to be most effective.

Knowledges and know-how the audience can get from your talk
  • How to wrap C libraries with Cython
  • How to package a Cython library with Poetry
  • Writing effective test suites for a Cython library
  • Debugging strategies for Cython libraries
  • Developer tooling for working with Cython
Prior knowledges speakers assume the audience to have
  • Intermediate/Advanced Knowledge of Python
  • Interest in memory management
  • Interest in performance optimization and C
  • Some familiarity with debugging practices
Audience experiment

Intermediate

Language of presentation

English

Language of presentation material

English

See also: Presentation slides (9.1 MB)