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
1 answer

Potential memory leak when converting wide char to python string

I have the following code in in cython in the pyx file, which converts wchar_t* to python string (unicode) // All code below is python 2.7.4 cdef wc_to_pystr(wchar_t *buf): if buf == NULL: return None cdef size_t buflen buflen =…
bitflood
  • 441
  • 5
  • 15
3
votes
2 answers

Fast iteration over vectors in a multidimensional numpy array

I'm writing some python + numpy + cython code, and am trying to find the most elegant and efficient way of doing the following kind of iteration over an array: Let's say I have a function f(x, y) that takes a vector x of shape (3,) and a vector y of…
John von N.
  • 195
  • 1
  • 1
  • 6
3
votes
1 answer

Allocate memory in C++ then deallocate it in Cython?

I have a C++ function that allocates an array and returns it. I want to use this array within Cython and then delete it when finished, but Cython doesn't seem to be happy with the way I'm trying to do this. The most trivial example: foo.hpp: float *…
David Pfau
  • 793
  • 9
  • 23
3
votes
1 answer

cython: both const and except in C++ method declaration

My code is: cdef extern from "mylib.h": cdef cppclass MyClass: MyClass(const char *data) except + # and I also need except + for myMethod int myMethod(size_t len, char *data) const I need both const and except + for my…
monoid
  • 1,661
  • 11
  • 16
3
votes
1 answer

Compiling and distributing Cython extensions

I have a python package (python 2.7) that includes some optimized functions written in cython. My setup.py handles the pure-python files and the installed package uses the un-optimized pure python functions. I'm trying to understand what is the best…
user2304916
  • 7,882
  • 5
  • 39
  • 53
3
votes
1 answer

Cython Partial Derivative

I have a python script where, as part of an evolutionary optimization algorithm, I'm evaluating partial derivatives many thousands of times. I've done a line by line profile, and this partial derivative calculation is taking up the majority of the…
hobscrk777
  • 2,347
  • 4
  • 23
  • 29
3
votes
0 answers

"Access violation reading location" when wrapping c++ api with Cython

I'm working on a wrapper module in Python for an api that's written in c++. The api itself is a wrapper for a c library/api for a Windows (7) application that's installed locally on my computer. I have access to the c++ api sourcecode but not the c…
e9wikner
  • 51
  • 5
3
votes
1 answer

Adding the __add__ operator to a Cython wrapper class?

The following C++ code goes off the standard Cython rectangle example, with an arbitrary added "+" operator: #include "Rectangle.h" using namespace shapes; Rectangle::Rectangle(int X0, int Y0, int X1, int Y1) { x0 = X0; y0 = Y0; x1 =…
user1313527
  • 141
  • 5
3
votes
1 answer

Python-for-Android: Error compiling Cython file

After git cloning python-for-android I tried creating a distribution: ./distribute.sh -m "kivy" And got few errors of this kind: Error compiling Cython file: ------------------------------------------------------------ ... …
Michael
  • 3,206
  • 5
  • 26
  • 44
3
votes
2 answers

General functions to iterate over n-D arrays

With Cython, is there a way to write fast general functions, which work for arrays with different dimensions? For example for this simple case of dealiasing functions: import numpy as np cimport numpy as np ctypedef np.uint8_t DTYPEb_t ctypedef…
paugier
  • 1,838
  • 2
  • 20
  • 39
3
votes
1 answer

Cython calling lapack, error: `Cannot take address of Python variable'

My problem is very similar to that posed here, and so I am trying to implement what was proposed in the answer there, based on this example of calling blas bundled with python. My code is only a slightly modified version of that example.pyx, here's…
newling
  • 624
  • 6
  • 10
3
votes
0 answers

how to build egg package with cython and c codes?

I have a setup.py as follows (other source files are available too) from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext ext_modules = [ Extension("fastsim", …
Jerry Feng
  • 187
  • 1
  • 2
  • 8
3
votes
1 answer

Using a pointer array within a struct in Cython

I am trying to write a Cython wrapper around a C library. I am very new to Cython, so my apologies in advance if the problem is obvious. In a file wrapper.pxd, I define a struct (reduced example): cdef extern from "thiscouldbeyourlibrary.h": …
3
votes
1 answer

Cython/Numpy - cimport numpy as np fails

I am trying to use cython to code critical part of my code but I have trouble doing to import numpy : Setup.py from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext import…
MiKL
  • 73
  • 10
3
votes
1 answer

cython lambda1 vs.

I have found out that on my PC, a certain method is represented as at 0x06DD02A0>, while on a CentOS server, it's . I believe this is the cause for a very obscure downstream error with a…
Korem
  • 11,383
  • 7
  • 55
  • 72
1 2 3
99
100