strtok and return value of PyString_AsString()
Les Schaffer
godzilla at netmeg.net
Wed Mar 29 20:47:36 EST 2000
i am finishing up a C extension in which i feed a list of strings (a
long list), and do a strtok on each item of the list.
i use PyString_AsString() to grab a char * pointer to the "string".
i then do a strtok(pointer ...), strtok(NULL, ...), etc etc
in the docs for Python/C API, it says for PyString_AsString :
Returns a null-terminated representation of the contents of
string. The pointer refers to the internal buffer of string, not a
copy. The data must not be modified in any way. It must not be
de-allocated.
the question is, why am i getting away with murder? i thought strtok
ate its first argument for breakfast? i checked the strings in the
list back in python after the call to extension function, and the
strings were intact. so i am confused.
[i was previously using strdup to copy the string (as per glibc docs),
but didnt like all the (implied) malloc's and explicit free()'s i
needed.]
only thing i can think of is that PyString_AsString __is__ malloc'ing
some memoery and passing a copy of strobject's data on each call. but
then there's the malloc/free i was trying to avoid.
thanks for any insight.
les schaffer
p.s. i don't understand stringobject.h definition of a string object:
typedef struct {
PyObject_VAR_HEAD
#ifdef CACHE_HASH
long ob_shash;
#endif
#ifdef INTERN_STRINGS
PyObject *ob_sinterned;
#endif
char ob_sval[1];
} PyStringObject;
why is ob_sval only one character long? where's da string????
guesstomate: ob_sval is the first letter of the string, which is
interned (whatever that means ;-) and when you go to grab the string,
its looked up in some tree somewhere starting with the first
letter?????
or sumpin like that?????????????
but then how does this return a string if ob_sval is a 1 element
character array? :
#define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
signed 'dazed and confused'
More information about the Python-list
mailing list