[Pyrex] getting effect of PyArg_Parse...(...,"s#",...)

sjmachin at lexicon.net sjmachin at lexicon.net
Sun Jan 30 01:04:29 CET 2005


Now that Yann has shown me how to get my kludge working properly (thanks, Yann!!):

> cdef extern from "Python.h":
>    int PyString_AsStringAndSize(object obj, char **buff, int *length) except -1
>
>     PyString_AsStringAndSize(s, &ucptr, &slen)

Let's get back to kludge avoidance:

Suggested syntax:

def myfunc(string s):

# Pyrex compiler generates code something like the kludge:
# cdef unsigned char * s_pointer
# cdef int s_length
# PyString_AsStringAndSize(s, &s_pointer, &s_length)

and one can then do what one likes

   for i from 0 <= i < s_length:
    peek_dont_poke(s_pointer[i])

The objectives being:
(a) 8-bit clean 
(b) efficient -- no need to call strlen to get the length
(c) non-string arg gets an exception raised w/o any further programming effort

Thinking further about the last point, perhaps it should be string-only; 
PyString_AsStringAndSize permits Unicode as well.





More information about the Pyrex mailing list