osdir.com
mailing list archive

Subject: getting data pointer to numpy object - msg#00026

List: python.pyrex

Date: Prev Next Index Thread: Prev Next Index
Hello,

Is there an easy way to get the data pointer to a numpy object which is *not* passed as a parameter? For example, I know I can do:

#---------------
cdef extern from "numpy/arrayobject.h":

struct PyArray_Descr:
int type_num, elsize
char type

ctypedef class numpy.ArrayType [object PyArrayObject]:
cdef char *data
cdef int nd
cdef int *dimensions, *strides
cdef object base
cdef PyArray_Descr *descr
cdef int flags

def test1(ArrayType w): # pass a numpy object

cdef double *w_p

# get the pointers
w_p=<double *>w.data
#---------------

...but I would like to do something like...

#-------------------------------
def test2(d): # pass a python dict

w=d['w'] # get the numpy object

cdef double *w_p

# get the pointers
w_p=<double *>w.data
#-------------------------------


Is there a way to do this?



thanks,


Brian Blais


--
-----------------

bblais@xxxxxxxxxx
http://web.bryant.edu/~bblais


Was this page helpful?
Yes No
Thread at a glance:

Previous Message by Date: click to view message preview

Pyrex generates broken code when calling a function immediately on the result of a C function call

Hi, Pyrex 0.9.3.1 generates broken code for the following statement: ----------------------------- return _parseMemoryDocument(text, __DEFAULT_HTML_PARSER).getroot() ----------------------------- _parseMemoryDocument is a C function, the type of getroot seems to be unimportant (C or Python). The resulting C code is ----------------------------- __pyx_1 = ((PyObject *)__pyx_f_5etree__parseMemoryDocument(__pyx_v_text,((PyObject *)__pyx_v_5etree___DEFAULT_HTML_PARSER))); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1203; goto __pyx_L1;} __pyx_2 = ((struct __pyx_vtabstruct_5etree__Document *)__pyx_f_5etree__parseMemoryDocument(__pyx_v_text,((PyObject *)__pyx_v_5etree___DEFAULT_HTML_PARSER))->__pyx_vtab)->getroot(__pyx_f_5etree__parseMemoryDocument(__pyx_v_text,((PyObject *)__pyx_v_5etree___DEFAULT_HTML_PARSER))); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1203; goto __pyx_L1;} ----------------------------- As this shows, the C function is called three times. The work-around is to separate the two function calls and to store the intermediate result in a variable. This generates the following (expected) code: ----------------------------- __pyx_1 = ((PyObject *)__pyx_f_5etree__parseMemoryDocument(__pyx_v_text,((PyObject *)__pyx_v_5etree___DEFAULT_HTML_PARSER))); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1203; goto __pyx_L1;} Py_DECREF(((PyObject *)__pyx_v_doc)); __pyx_v_doc = __pyx_1; __pyx_1 = 0; __pyx_1 = ((struct __pyx_vtabstruct_5etree__Document *)__pyx_v_doc->__pyx_vtab)->getroot(__pyx_v_doc); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; goto __pyx_L1;} __pyx_r = __pyx_1; ----------------------------- The fix would be to replace the second and third call by __pyx_1, which is correctly calculated in the first line. Stefan

Next Message by Date: click to view message preview

conditional compilation

I noticed something about conditional compilation in the recent patch from Stefan Behnel... maybe it's time to pipe up about this change I've been sitting on. The attached patch extends the Pyrex syntax to support three forms of 'directive', which allows some compile-time changes to the output. Now, Greg Ewing isn't a big fan of this - and I agree with him that conditionals are bad in many ways. I don't blame him in the least for not wanting this wart in his language. 8^) However, we desperately need this functionality for our work at IronPort... and the patch isn't all that large. We're currently using it for debugging, instrumentation, assertions, platform differentiation, etc... I hope others find it useful. The new syntax is: %define <name> [<expr>] %if <expr>: <suite-to-be-merged> [%else: <suite-to-be-merged>] %import <module-name> There's a single global 'compile-time' namespace. For <expr> you can put just about anything you like, it's very flexible. [One sample I tried grepped through an include file - how's that for ugly!] The patch is against 0.9.3.1. A demo file with a setup.py is also included. Comments/feedback appreciated. -Sam pyrex_conditional.tar.gz Description: application/compressed-tar signature.asc Description: This is a digitally signed message part _______________________________________________ Pyrex mailing list Pyrex@xxxxxxxxxxxxxxxxx http://lists.copyleft.no/mailman/listinfo/pyrex

Previous Message by Thread: click to view message preview

Pyrex generates broken code when calling a function immediately on the result of a C function call

Hi, Pyrex 0.9.3.1 generates broken code for the following statement: ----------------------------- return _parseMemoryDocument(text, __DEFAULT_HTML_PARSER).getroot() ----------------------------- _parseMemoryDocument is a C function, the type of getroot seems to be unimportant (C or Python). The resulting C code is ----------------------------- __pyx_1 = ((PyObject *)__pyx_f_5etree__parseMemoryDocument(__pyx_v_text,((PyObject *)__pyx_v_5etree___DEFAULT_HTML_PARSER))); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1203; goto __pyx_L1;} __pyx_2 = ((struct __pyx_vtabstruct_5etree__Document *)__pyx_f_5etree__parseMemoryDocument(__pyx_v_text,((PyObject *)__pyx_v_5etree___DEFAULT_HTML_PARSER))->__pyx_vtab)->getroot(__pyx_f_5etree__parseMemoryDocument(__pyx_v_text,((PyObject *)__pyx_v_5etree___DEFAULT_HTML_PARSER))); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1203; goto __pyx_L1;} ----------------------------- As this shows, the C function is called three times. The work-around is to separate the two function calls and to store the intermediate result in a variable. This generates the following (expected) code: ----------------------------- __pyx_1 = ((PyObject *)__pyx_f_5etree__parseMemoryDocument(__pyx_v_text,((PyObject *)__pyx_v_5etree___DEFAULT_HTML_PARSER))); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1203; goto __pyx_L1;} Py_DECREF(((PyObject *)__pyx_v_doc)); __pyx_v_doc = __pyx_1; __pyx_1 = 0; __pyx_1 = ((struct __pyx_vtabstruct_5etree__Document *)__pyx_v_doc->__pyx_vtab)->getroot(__pyx_v_doc); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1204; goto __pyx_L1;} __pyx_r = __pyx_1; ----------------------------- The fix would be to replace the second and third call by __pyx_1, which is correctly calculated in the first line. Stefan

Next Message by Thread: click to view message preview

Re: getting data pointer to numpy object

Brian Blais wrote: ...but I would like to do something like... #------------------------------- def test2(d): # pass a python dict w=d['w'] # get the numpy object cdef double *w_p # get the pointers w_p=<double *>w.data #------------------------------- You can declare any variable as an ArrayType, not just parameters. So def test2(d): cdef ArrayType w cdef double *w_p w = d['w'] w_p = <double *>w.data -- Greg
Sign up for updates to this mailing list. email:
Loading Comments...
Home | News | Patents | Sitemap | FAQ | advertise

Advertising by