Search code examples
pythoncythonpackaging

Bundle Cython module with a Python package


I'm wrapping a C++ library in Cython, and I would like to distribute this as a Python package. I've been using this tutorial as a guide.

Here is how things are organized.

.
├── inc
│   └── Rectangle.h
├── rect
│   ├── __init__.py
│   └── wrapper.pyx
├── setup.py
└── src
    └── Rectangle.cpp

I've pasted the contents of these files at the bottom of the post, as well as in this GitHub repo.

I have no problem compiling and installing with python setup.py install, and I can import rect from the interpreter with no problem. But it seems to be an empty class: I can't create a Rectangle object with any of the following. - Rectangle - rect.Rectangle - wrapper.Rectangle - rect.wrapper.Rectangle

What am I doing wrong here?


Contents of Rectangle.h, copied and pasted from the tutorial.

namespace shapes {
    class Rectangle {
    public:
        int x0, y0, x1, y1;
        Rectangle();
        Rectangle(int x0, int y0, int x1, int y1);
        ~Rectangle();
        int getArea();
        void getSize(int* width, int* height);
        void move(int dx, int dy);
    };
}

Contents of Rectangle.cpp.

#include "Rectangle.h"

namespace shapes {

  Rectangle::Rectangle() { }

    Rectangle::Rectangle(int X0, int Y0, int X1, int Y1) {
        x0 = X0;
        y0 = Y0;
        x1 = X1;
        y1 = Y1;
    }

    Rectangle::~Rectangle() { }

    int Rectangle::getArea() {
        return (x1 - x0) * (y1 - y0);
    }

    void Rectangle::getSize(int *width, int *height) {
        (*width) = x1 - x0;
        (*height) = y1 - y0;
    }

    void Rectangle::move(int dx, int dy) {
        x0 += dx;
        y0 += dy;
        x1 += dx;
        y1 += dy;
    }

}

Cython wrapper code wrapper.pyx.

# distutils: language = c++
# distutils: sources = src/Rectangle.cpp

cdef extern from "Rectangle.h" namespace "shapes":
    cdef cppclass Rectangle:
        Rectangle() except +
        Rectangle(int, int, int, int) except +
        int x0, y0, x1, y1
        int getArea()
        void getSize(int* width, int* height)
        void move(int, int)

cdef class PyRectangle:
    cdef Rectangle c_rect      # hold a C++ instance which we're wrapping
    def __cinit__(self, int x0, int y0, int x1, int y1):
        self.c_rect = Rectangle(x0, y0, x1, y1)
    def get_area(self):
        return self.c_rect.getArea()
    def get_size(self):
        cdef int width, height
        self.c_rect.getSize(&width, &height)
        return width, height
    def move(self, dx, dy):
        self.c_rect.move(dx, dy)

The setup.py script I've adapted for this file organization.

from distutils.core import setup, Extension
from Cython.Build import cythonize

setup(
    name='rect',
    packages=['rect'],
    ext_modules=cythonize(Extension(
        'Rectangle',
        sources=['rect/wrapper.pyx', 'src/Rectangle.cpp'],
        include_dirs=['inc/'],
        language='c++',
        extra_compile_args=['--std=c++11'],
        extra_link_args=['--std=c++11']
    )),
)

Solution

  • In this case, the problem turned out to be that the .pyx file and the extension did not have the same basename. When I renamed wrapper.pyx to Rectangle and re-installed I was able to run the following in the Python interpreter.

    >>> import Rectangle
    >>> r = Rectangle.PyRectangle(0, 0, 1, 1)
    >>> r.get_area()
    1
    >>>