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

Are executables produced with Cython really free of the source code?

I have read Making an executable in Cython and BuvinJ's answer to How to obfuscate Python code effectively? and would like to test if the source code compiled with Cython is really "no-more-there" after the compilation. It is indeed a popular…
Basj
  • 41,386
  • 99
  • 383
  • 673
22
votes
3 answers

Cython for a Django app: would it work?

Would compiling with cython work with a python 3.4 Django app, or could it be made to work without a huge amount of effort? This answer - https://stackoverflow.com/a/7347168/805141 - to a question about protecting python code prompted me to ask…
Daniel
  • 1,774
  • 2
  • 22
  • 38
22
votes
1 answer

Possible to autogenerate Cython bindings around a large, existing C library?

In otherwords: *.h/*.c --[??POSSIBLE??]--> *.pxd/*.pyx OK. I’ve done (I hope) enough digging around the Internet - but I think this is a good question so I’ll ask it straight. There are a few related questions (e.g. Generate python bindings, what…
timlukins
  • 2,694
  • 21
  • 35
22
votes
3 answers

Do Cython extension types support class attributes?

Python classes can have class attributes: class Foo(object): bar = 4 Is there an analogous construct for defining class attributes in Cython extension types? For example, when I try to compile the following cython code cdef class Foo: cdef…
Pwnosaurus
  • 2,058
  • 1
  • 19
  • 21
22
votes
1 answer

Fortran - Cython Workflow

I would like to set up a workflow to reach fortran routines from Python using Cython on a Windows Machine after some searching I found : http://www.fortran90.org/src/best-practices.html#interfacing-with-c and…
martburg
  • 490
  • 3
  • 12
22
votes
2 answers

Cython: how to make an python object as a property of cython class

I have a existing python class X and I want to do the followings: from my_python_module import X cdef class Y: cdef X test But this does not work out of the box, the cdef only accepts C type, not a Python class. Any work-around ?
w00d
  • 5,416
  • 12
  • 53
  • 85
22
votes
3 answers

Reloading a Python extension module from IPython

Using Cython, I am developing an extension module which gets build as an .so file. I then test it using IPython. During development, I frequently need to make changes and rebuild. I also need to exit the IPython shell and reenter all commands.…
clstaudt
  • 21,436
  • 45
  • 156
  • 239
22
votes
2 answers

project structure for wrapping many c++ classes in cython to a single shared object

I have found partial answers between the docs, mailing lists, and this question here, but I wanted to get a more direct answer addressing my specifics... I'm learning cython by trying to wrap small parts, little by little, of a library that I am…
jdi
  • 90,542
  • 19
  • 167
  • 203
21
votes
2 answers

Passing C++ vector to Numpy through Cython without copying and taking care of memory management automatically

Dealing with processing large matrices (NxM with 1K <= N <= 20K & 10K <= M <= 200K), I often need to pass Numpy matrices to C++ through Cython to get the job done and this works as expected & without copying. However, there are times when I need to…
NULL
  • 759
  • 9
  • 18
21
votes
1 answer

3d sliding window operation in Theano?

TL.DR. Is there a 3-dimensional friendly implementation of theano.tensor.nnet.neighbours.images2neibs? I would like to perform voxel-wise classification of a volume (NxNxN) using a neural network that takes in a nxnxn image, where N>n. To classify…
pangyuteng
  • 1,749
  • 14
  • 29
21
votes
3 answers

Optimizing a reed-solomon encoder (polynomial division)

I am trying to optimize a Reed-Solomon encoder, which is in fact simply a polynomial division operation over Galois Fields 2^8 (which simply means that values wrap-around over 255). The code is in fact very very similar to what can be found here for…
gaborous
  • 15,832
  • 10
  • 83
  • 102
21
votes
5 answers

Collapse multiple submodules to one Cython extension

This setup.py: from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize extensions = ( Extension('myext', ['myext/__init__.py', 'myext/algorithms/__init__.py', …
Reinderien
  • 11,755
  • 5
  • 49
  • 77
21
votes
2 answers

cython compilation - import vs cimport

Newbie to Cython (perhaps this is a basic question). Consider two examples both taken from this blog here: # code 1 import numpy as np def num_update(u): u[1:-1,1:-1] = ((u[2:,1:-1]+u[:-2,1:-1])*dy2 + (u[1:-1,2:] +…
uday
  • 6,453
  • 13
  • 56
  • 94
21
votes
3 answers

cython error compiling with print function parameters

when use cython to create helloworld.c from helloworld.pyx , this error occured: error compiling Cython file: ------------------------------------------------------------ ... print('hello world',end='') …
mohammad
  • 241
  • 2
  • 8
21
votes
1 answer

Using the buffer API in Cython

I'm working in with a C library that repeatedly calls a user supplied function pointer to get more data. I'd like to write a Cython wrapper in such a way that the Python implementation of that callback can return any reasonable data type like str,…
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304