Movatterモバイル変換


[0]ホーム

URL:



Pyrex FAQ

Contents


How do I call Python/C API routines?

Declare them as C functions inside acdef extern from block.Use the type nameobject for any parameters and return types which are Python object references. Don't use the wordconst anywhere. Here is an example which defines and uses thePyString_FromStringAndSize routine:
cdef extern from "Python.h":
    object PyString_FromStringAndSize(char *, int)

cdef char buf[42]
my_string = PyString_FromStringAndSize(buf, 42)

How do I convert a C string containing nullbytes to a Python string?

Put in a declaration for thePyString_FromStringAndSize API routine and use that. SeeHow do I call Python/C API routines?

How do I access the data inside a Numeric array object?

Use acdef extern from block to include the Numeric header file and declare the array object as an external extension type. The following code illustrates how to do this:
cdef extern from "Numeric/arrayobject.h":

    struct PyArray_Descr:
        int type_num, elsize
        char type

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

For more information about external extension types, see the"External Extension Types"section of the"Extension Types" documentationpage.

Pyrex says my extension type object has no attribute'rhubarb', but I know it does. What gives?

You're probably trying to access it through a reference which Pyrex thinksis a generic Python object. You need to tell Pyrex that it's a referenceto your extension type by means of a declaration. For example,
cdef class Vegetables:
    cdef int rhubarb

...
cdef Vegetables veg
veg.rhubarb = 42
Also see the"Attributes"section of the"ExtensionTypes" documentation page.

Python says my extension type has no method called 'quack', but I know it does. What gives?

You may have declared the method usingcdef instead ofdef. Only functions and methods declared withdef are callable from Python code.

---
[8]ページ先頭

©2009-2025 Movatter.jp