Wrapping a pre-initialized pointer in a cython class

Instead of using __init__/__cinit__ (which always expects Python objects as arguments), you can use a custom @staticmethod cdef to create instances: cdef class Tree: cdef glp_tree* ptr def __init__(self, *args): raise TypeError(‘Cannot create instance from Python’) @staticmethod cdef Tree create(glp_tree* ptr): obj = <Tree>Tree.__new__(Tree) # create instance without calling __init__ obj.ptr = ptr return obj

Interfacing C++11 array with Cython

As discussed in the comments, the issue you’re having is because Cython doesn’t really support non-type template arguments. A workround (hacky and probably fragile) is to trick Cython into thinking it’s providing a type template argument: cdef extern from “<array>” namespace “std” nogil : cdef cppclass two “2”: pass cdef cppclass array[T, U]: T& operator[](size_t) … Read more

Can Cython code be compiled to a dll so C++ application can call it?

Using cython-module in a dll is not unlike using a cython-module in an embeded python interpreter. The first step would be to mark cdef-function which should be used from external C-code with public, for example: #cyfun.pyx: #doesn’t need python interpreter cdef public int double_me(int me): return 2*me; #needs initialized python interpreter cdef public void print_me(int … Read more

setup_requires with Cython?

Starting from 18.0 release of setuptools (released on 2015-06-23) it is possible to specify Cython in setup_requires and pass *.pyx modules sources for regular setuptools.Extension: from setuptools import setup, Extension setup( # … setup_requires=[ # Setuptools 18.0 properly handles Cython extensions. ‘setuptools>=18.0’, ‘cython’, ], ext_modules=[ Extension( ‘mylib’, sources=[‘src/mylib.pyx’], ), ], )

Difference between np.int, np.int_, int, and np.int_t in cython?

It’s a bit complicated because the names have different meanings depending on the context. int In Python The int is normally just a Python type, it’s of arbitrary precision, meaning that you can store any conceivable integer inside it (as long as you have enough memory). >>> int(10**50) 100000000000000000000000000000000000000000000000000 However, when you use it as … Read more

Fortran – Cython Workflow

Here’s a minimum working example. I used gfortran and wrote the compile commands directly into the setup file. gfunc.f90 module gfunc_module implicit none contains subroutine gfunc(x, n, m, a, b, c) double precision, intent(in) :: x integer, intent(in) :: n, m double precision, dimension(n), intent(in) :: a double precision, dimension(m), intent(in) :: b double precision, … Read more

Use generated header file from Cython

I think the problem is your misconception, that distutils will build the executable for you. That is not the case. Out goal is to use some python functionality from a C-program. Let’s do the necessary steps by ourselves: Using cython to produce hello.h and hello.c from the given pyx-file. Using the created hello.h for importing … Read more