Cython dynamic array not converting to Python

I’m working on:

  • Ubuntu 24.04
  • Python 3.12
  • Cython 3.0
  • cc 13.2.

I have a C module that implements an arbitrary tree and a few constructor functions:

typedef unsigned char byte;
typedef unsigned long ulong;

// ; #region Tree
typedef struct _file {
  ulong length;
  ulong offset;
  byte *data;
} file;

typedef struct _node {
  ulong length;
  byte *value;
  ulong childrenCount;
  struct _node *children;
} node;
// ; #endregion Tree

// ; #region Functions
file read_file(char *file_name);
node process_file(char *file_name);
// ; #endregion Functions

All I have from the C side is a header file and a .so library. The tree is implemented via the children field, which is subject to a malloc. This all works perfectly well on C alone, with no leaks, no SIGSEGV, no fuss.

I’m trying to connect this to Python via Cython. I have the following files:

# cfile.pxd
cdef extern from "file.h":
    ctypedef unsigned char byte;
    ctypedef unsigned short ushort;
    ctypedef unsigned long ulong;

    ctypedef struct file:
        ulong length
        ulong offset
        byte* data

    ctypedef struct node:
        ulong  length
        byte*  value
        ulong  childrenCount
        node*  children

    file read_file(char *file_name)
    node process_file(char *file_name);

and:

# pyfile.pyx
cimport cfile
from libc.stdlib cimport malloc, free
from libc.string cimport memcpy

cdef class File:
    cdef cfile.file c_struct

    def __cinit__(self, unsigned long length = 0, unsigned long offset = 0, bytes data = None):
        self.c_struct.length = length
        self.c_struct.offset = offset
        self.c_struct.data = <unsigned char *> malloc(length)
        memcpy(self.c_struct.data, <unsigned char *> data, length)

    @property
    def length(self):
        return self.c_struct.length

    @property
    def offset(self):
        return self.c_struct.offset

    @property
    def data(self):
        return list(self.c_struct.data[:self.c_struct.length])


cdef class Node:
    cdef cfile.node c_struct

    def __cinit__(self, unsigned long length, bytearray value, unsigned long childrenCount):
        self.c_struct.length = length
        self.c_struct.value = <unsigned char *> malloc(length)
        memcpy(self.c_struct.value, <unsigned char *> value, length)
        self.c_struct.childrenCount = childrenCount
        self.c_struct.children = NULL

    @property
    def length(self):
        return self.c_struct.length

    @property
    def value(self):
        return bytearray(self.c_struct.value[:self.c_struct.length])

    @property
    def childrenCount(self):
        return self.c_struct.childrenCount

    @property
    def children(self):
        return [Node.from_c_struct(self.c_struct.children[i]) for i in range(self.c_struct.childrenCount)]

    @property
    def from_c_struct(self,):
        cdef Node node = self.__new__(self)
        node.c_struct = self.c_struct
        return node


def read_file(str file_name):
    cdef cfile.file c_data
    c_data = cfile.read_file(file_name.encode('utf-8'))
    file = File.__new__(File)
    file._c_struct = c_data
    return file


def process_file(str file_name):
    cdef cfile.node c_node
    c_node = cfile.process_file(file_name.encode('utf-8'))
    return Node.from_c_struct(c_node)

Finally, I am using the following setup:

# setup.py
from setuptools import setup
from Cython.Build import cythonize
from setuptools.extension import Extension

extensions = [
    Extension(
        name="file",
        sources=["file.pyx"],
        include_dirs=["."],
        libraries=["file"],
        library_dirs=["."],
        runtime_library_dirs=["."],
        extra_objects=["libfile.so"]
    )
]

setup(
    name="file",
    ext_modules=cythonize(extensions),
)

and running python setup.py build_ext --inplace.

ERRORS

I’m getting these:

    @property
    def children(self):
        return [Node.from_c_struct(self.c_struct.children[i]) for i in range(self.c_struct.childrenCount)]
                                                         ^
------------------------------------------------------------

file.pyx:53:57: Cannot convert 'node' to Python object

and

def process_file(str file_name):
    cdef cfile.node c_node
    c_node = cfile.process_file(file_name.encode('utf-8'))
    return Node.from_c_struct(c_node)
                              ^
------------------------------------------------------------

file.pyx:72:30: Cannot convert 'node' to Python object

I believe this is related to the children field, as removing it and all connected logic seems to work.

Question

Any ideas on how to make this work directly with Cython?

I’m looking for a solution that precludes third parties. I found many solutions based on cpython imports or numpy, or even both. I would like to avoid any of that, if possible.

I’m also looking for a solution that does not involve compiling the C code together with the Python code. In order to keep concerns separated, all I want from the C side is the .so.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật