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
8c4e86c6
authored
Jan 05, 2012
by
GaelVaroquaux
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix: the code was actually doing a copy
parent
bd8a6922
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
2 deletions
+15
-2
cython_wrapper.pyx
+10
-1
test.py
+5
-1
No files found.
cython_wrapper.pyx
View file @
8c4e86c6
...
...
@@ -14,6 +14,7 @@ cdef extern from "c_code.c":
float *compute(int size)
from libc.stdlib cimport free
from cpython cimport PyObject, Py_INCREF
# Import the Python-level symbols of numpy
import numpy as np
...
...
@@ -70,10 +71,18 @@ def py_compute(int size):
not copy the data allocated in C.
"""
cdef float *array
cdef np.ndarray ndarray
# Call the C function
array = compute(size)
array_wrapper = ArrayWrapper()
array_wrapper.set_data(size, <void*> array)
ndarray = np.array(array_wrapper, copy=False)
# Assign our object to the 'base' of the ndarray object
ndarray.base = <PyObject*> array_wrapper
# Increment the reference count, as the above assignement was done in
# C, and Python does not know that there is this additional reference
Py_INCREF(array_wrapper)
return np.array(array_wrapper)
return ndarray
test.py
View file @
8c4e86c6
...
...
@@ -2,7 +2,11 @@
"""
# Author: Gael Varoquaux
# License: BSD
import
numpy
as
np
import
cython_wrapper
a
=
cython_wrapper
.
py_compute
(
10
)
print
a
print
'The array created is
%
s'
%
a
print
'It carries a reference to our deallocator:
%
s '
%
a
.
base
np
.
testing
.
assert_allclose
(
a
,
np
.
arange
(
10
))
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