Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
gists
/
cython-cwrapper
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Registry
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ff802218
authored
Sep 28, 2011
by
Gael varoquaux
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add all files to have a working example
parent
e1b6fce3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
100 additions
and
0 deletions
+100
-0
c_code.c
+20
-0
cython_wrapper.pyx
+43
-0
setup.py
+37
-0
No files found.
c_code.c
0 → 100644
View file @
ff802218
/* Small C file creating an array to demo C -> Python data passing
*
* Author: Gael Varoquaux
* License: BSD
*/
#include <stdlib.h>
float
*
compute
(
int
size
)
{
int
*
array
;
array
=
malloc
(
sizeof
(
int
)
*
size
);
int
i
;
for
(
i
=
0
;
i
<
size
;
i
++
)
{
array
[
i
]
=
i
;
}
return
array
;
}
cython_wrapper.pyx
0 → 100644
View file @
ff802218
""" Small Cython file to demonstrate the use of PyArray_SimpleNewFromData
in Cython to create an array from already allocated memory.
Cython enables mixing C-level calls and Python-level calls in the same
file with a Python-like syntax and easy type cohersion. See
http://cython.org for more information
"""
# Author: Gael Varoquaux
# License: BSD
# Declare the prototype of the C function we are interested in calling
cdef extern from "c_code.c":
float *compute(int size)
# Import the Python-level symbols of numpy
import numpy as np
# Import the C-level symbols of numpy
cimport numpy as np
# Numpy must be initialized. When using numpy from C or Cython you must
# _always_ do that, or you will have segfaults
np.import_array()
def py_compute(int size):
""" Python binding of the 'compute' function in 'c_code.c' that does
not copy the data allocated in C.
"""
cdef float *array
# Call the C function
array = compute(size)
# Create a C array to describe the shape of the ndarray
cdef np.npy_intp shape[1]
shape[0] = <np.npy_intp> size
# Use the PyArray_SimpleNewFromData function from numpy to create a
# new Python object pointing to the existing data
ndarray = np.PyArray_SimpleNewFromData(1, shape,
np.NPY_INT, <void *> array)
return ndarray
setup.py
0 → 100644
View file @
ff802218
""" Example of building a module with a Cython file. See the distutils
and numpy distutils documentations for more info:
http://docs.scipy.org/doc/numpy/reference/distutils.html
"""
# Author: Gael Varoquaux
# License: BSD
import
numpy
from
Cython.Distutils
import
build_ext
def
configuration
(
parent_package
=
''
,
top_path
=
None
):
""" Function used to build our configuration.
"""
from
numpy.distutils.misc_util
import
Configuration
# The configuration object that hold information on all the files
# to be built.
config
=
Configuration
(
''
,
parent_package
,
top_path
)
config
.
add_extension
(
'cython_wrapper'
,
sources
=
[
'cython_wrapper.pyx'
],
# libraries=['m'],
depends
=
[
'c_code.c'
],
include_dirs
=
[
numpy
.
get_include
()])
return
config
if
__name__
==
'__main__'
:
# Retrieve the parameters of our local configuration
params
=
configuration
(
top_path
=
''
)
.
todict
()
# Override the C-extension building so that it knows about '.pyx'
# Cython files
params
[
'cmdclass'
]
=
dict
(
build_ext
=
build_ext
)
# Call the actual building/packaging function (see distutils docs)
from
numpy.distutils.core
import
setup
setup
(
**
params
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment