You are previewing Learning Cython Programming - Second Edition.
O'Reilly logo
Learning Cython Programming - Second Edition

Book Description

Learn the fundamentals of Cython to extend the legacy of your applications

About This Book

  • Learn how to extend C applications with pure Python code

  • Get more from Python – you’ll not only learn Cython, you’ll also unlock a greater understanding of how to harness Python

  • Packed with tips and tricks that make Cython look easy, dive into this accessible programming guide and find out what happens when you bring C and Python together!

  • Who This Book Is For

    This book is for developers who are familiar with the basics of C and Python programming and wish to learn Cython programming to extend their applications.

    What You Will Learn

  • Reuse Python logging in C

  • Make an IRC bot out of your C application

  • Extend an application so you have a web server for rest calls

  • Practice Cython against your C++ code

  • Discover tricks to work with Python ConfigParser in C

  • Create Python bindings for native libraries

  • Find out about threading and concurrency related to GIL

  • Expand Terminal Multiplexer Tmux with Cython

  • In Detail

    Cython is a hybrid programming language used to write C extensions for Python language. Combining the practicality of Python and speed and ease of the C language it’s an exciting language worth learning if you want to build fast applications with ease.

    This new edition of Learning Cython Programming shows you how to get started, taking you through the fundamentals so you can begin to experience its unique powers.

    You’ll find out how to get set up, before exploring the relationship between Python and Cython. You’ll also look at debugging Cython, before moving on to C++ constructs, Caveat on C++ usage, Python threading and GIL in Cython. Finally, you’ll learn object initialization and compile time, and gain a deeper insight into Python 3, which will help you not only become a confident Cython developer, but a much more fluent Python developer too.

    Style and approach

    This practical and a fast-paced guide gives you all the information you need to start programming using Cython.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Learning Cython Programming Second Edition
      1. Table of Contents
      2. Learning Cython Programming Second Edition
      3. Credits
      4. About the Author
      5. Acknowledgments
      6. About the Reviewer
        1. eBooks, discount offers, and more
          1. Why subscribe?
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      9. 1. Cython Won't Bite
        1. Installing Cython
          1. Emacs mode
          2. Getting the code examples
        2. Getting started – Hello World
          1. Using distutils with Cython
          2. Calling C functions from Python
          3. Type conversion in Cython
        3. Summary
      10. 2. Understanding Cython
        1. Linking models
        2. Cython keyword – cdef
          1. Structs
          2. Enums
        3. Typedef and function pointers
        4. The public keyword
        5. Keyword cpdef
        6. Logging from C/C++ into Python
        7. Using Python ConfigParser from C/C++
        8. Callbacks from Python to C/C++
        9. Cython PXD
        10. Integration with build systems
          1. Python Distutils
          2. GNU/Autotools
        11. Summary
      11. 3. Extending Applications
        1. Cython pure Python code
        2. Compiling pure Python code
          1. Avoid Makefiles – pyximport
        3. Python garbage collector
        4. Extending Tmux
          1. Tmux build system
        5. Embedding Python
        6. Cythonizing struct cmd_entry
        7. Implementing a Tmux command
        8. Hooking everything together
        9. Summary
      12. 4. Debugging Cython
        1. Using GDB with Cython
          1. Running cygdb
        2. Cython caveats
          1. Type checking
          2. Dereference operator (*)
          3. Python exceptions
          4. C/C++ iterators
          5. Boolean error
          6. Const keyword
          7. Multiple Cython inputs
          8. Struct initialization
          9. Calling into pure Python modules
        3. Summary
      13. 5. Advanced Cython
        1. Cython and C++
          1. Namespaces
          2. Classes
        2. C++ new and del keyword
          1. Exceptions
          2. Bool type
        3. Overloading
        4. Templates
        5. Static class member attribute
        6. Calling C++ functions – Caveat
          1. Namespaces – Caveat
        7. Python distutils
        8. Python threading and GIL
          1. Atomic instructions
          2. Read/write lock
        9. Cython keywords
        10. Messaging server
        11. Caveat on GIL
        12. Unit testing the native code
        13. Preventing subclassing
        14. Parsing large amounts of data
        15. Summary
      14. 6. Further Reading
        1. Overview
        2. OpenMP support
        3. Compile time preprocessor
        4. Python 3
        5. PyPy
        6. AutoPXD
        7. Pyrex and Cython
        8. SWIG and Cython
        9. Cython and NumPy
        10. Numba versus Cython
        11. Parakeet and Numba
        12. Relevant Links
        13. Summary
      15. Index