Questions tagged [cython]

Cython is a superset of the Python language for quickly generating Python C extensions.

Cython is a superset of the Python language for quickly generating Python C/C++ extensions. Cython is a pidgin language of Python and C/C++. Unlike pure Python, Cython code is not directly interpreted by the Python interpreter, but is instead used to generate C/C++ code. The generated C/C++ code can then be compiled into a C/C++-extension, which then can be imported by Python code.

Cython syntax was originally based on Python 2, with added type declarations à la C/C++, however, its syntax now supports both Python 2 and 3 language features. Additionally, Cython is capable of generating C/C++ extension code compatible with either Python 2 or Python 3. Its syntax now allows the use of advanced C++ constructs such as template and stl container. Finally, thanks to static typing, Cython code generally executes much faster than Python code.

Cython is freely available under the open source Apache License.

The latest release of Cython is 3.0 alpha 5 (released 2020-05-19). Cython is available from the PyPI package index repository.

5220 questions
3
votes
0 answers

Cython import external module

I am pretty naive in cython and just beginning to explore this. So I have a very basic question. I have a python code that I am trying to cythonize. I managed to create the c source and header files from the original pyx file that has my…
srsci
  • 239
  • 5
  • 10
3
votes
1 answer

How can I use an external C library wrapped with Cython in a separate Python process via multiprocessing?

I have wrapped a small C library in Cython, and can successfully call it from Python. Here's a simplified example representing how this is as of now: # wrapper_module.pyx cdef extern from "my_c_library.h": void function1() int function2(int…
benson
  • 423
  • 3
  • 11
3
votes
1 answer

Call cdef function by name in Cython

I have a bunch of cdef functions in Cython, that are called by a def function in a pyx file, e.g.: cdef inline void myfunc_c(...): (...) return def wrapper(...): myfunc_c(...) return This works well. But to simplify not having to…
tiago
  • 22,602
  • 12
  • 72
  • 88
3
votes
2 answers

How to use a Cython cdef class member method in a native callback

I have a C++ library that is heavily callback driven. The callbacks are registered as std::function instances. A callback registration function might look something like: void register(std::function callback); I can register plain cdef…
Stephen
  • 2,613
  • 1
  • 24
  • 42
3
votes
0 answers

undefined symbols in Cython C++ wrapper

I have a working cpp project which builds fine with cmake. Now I need to create a Python wrapper for the same. So I chose cython to bridge the gap between C++ and Python. Instead of writing the CMakeLists.txt logic in pyx file, I tried compiling…
ZdaR
  • 22,343
  • 7
  • 66
  • 87
3
votes
1 answer

Cython: Cannot Convert pointer to Python Object after module split

The Problem: Error compiling Cython file: ------------------------------------------------------------ ... cpdef Py_GetRemoteDevice(PyChannel Chan): ret_val = mod_one.PyRemoteDevice() cdef core.RemoteDevice * tmp with nogil: tmp…
rahvin74
  • 35
  • 7
3
votes
1 answer

Cython with numpy how to get rid of fancy indexing (no call to Python)

I want to release the GIL inside a for loop on a 3-dimensional numpy array cdef np.ndarray[DTYPE_t,ndim=3] array=np.ones((10000000,4,2)) cdef np.ndarray[DTYPE_t,ndim=2] sliced_array cdef int i cdef int N=array.shape[0] for i in range(N): …
jeandut
  • 2,471
  • 4
  • 29
  • 56
3
votes
0 answers

Buiding with distutils gives TypeError: expected str, bytes or os.PathLike object, not list

I am trying to build the sklearn.tree subpackage, which includes some Cython code. I use Python 3.6.1 on Windows with Numpy 1.12.1. The file setup.py (can be downloaded here) contains distutils configuration with code such as…
David Dale
  • 10,958
  • 44
  • 73
3
votes
1 answer

Cython undefined symbol with c wrapper

I am trying to expose c code to cython and am running into "undefined symbol" errors when trying to use functions defined in my c file from another cython module. Functions defined in my h files and functions using a manual wrapper work without a…
SleepProgger
  • 364
  • 6
  • 20
3
votes
1 answer

R6034 An application has made an attempt to load the C runtime library incorrectly

I am running a python 2.7 code (containing GPy and GPyOpt, python implementation of gaussian process and Bayesian optimization) from Matlab on Anaconda 64bit on Windows 10 and I am facing with the following error: warning in stationary: failed to…
Rayan
  • 31
  • 3
3
votes
1 answer

Cython: control cythonize to stop placing compiled `.c` file from `.pyx` file in install directory

I have a small helper function test containing sensitive code. To mask this code, I have written the function in hello.pyx and using it in my package mypackage. I am able to build and use it by modifying the setup.py for the package to something…
goofd
  • 2,028
  • 2
  • 21
  • 33
3
votes
2 answers

Why is this loop in Cython as slow as its Python equivalent?

The following code rearranges bits in an RGBA4 texture data array. cpdef bytes toDDSrgba4(bytearray data): cdef bytes new_data = b'' cdef u32 i, pixel, new_pixel cdef u32 red, green, blue, alpha for i in range(0, len(data), 2): …
AboodXD
  • 148
  • 9
3
votes
1 answer

Using Python asyncio interfaces with Cython libraries

I've got an external library in C++ that has been wrapped by Cython. This C++ library itself I cannot change. I would like to combine the library to be used as part of Python application that uses asyncio as its primary process control. The Cython…
0x00
  • 141
  • 2
  • 10
3
votes
2 answers

Direct way to access Numpy RandomState object

Is there are more direct way to access the RandomState object created on import other than np.random..__self__? Both np.random._rand and getattr(np.random, "_rand") raise AttributeError. The former works fine but doesn't seem very…
deasmhumnha
  • 456
  • 4
  • 12
3
votes
2 answers

How to use @cython decorator to declare np.ndarray type

I'm having trouble finding any documentation anywhere that specifically addresses this: The following code: cdef int foo( double data ): # ... return int( data ) can be written…
MB.
  • 1,303
  • 12
  • 19