[Cython] char* and NULL in log statements

Johannes Wienke languitar at semipol.de
Wed Jun 4 16:57:53 CEST 2008


Hi,

I've just noticed that using NULL values in char* is a bad idea when
converting them to a python string. This results in a segfault:

cdef void doSomething(char *string):
    print string

def doIt():
    cdef char* string = NULL
    doSomething(NULL)

*Wouldn't it be a good idea to automatically convert them to None?*

For example I have a lot of debug messages that simply indicate which
function is currently called and with which arguments. At the moment
this is always 4 extra lines to check for the NULL value, which could
even be semantically correct for the rest of that function:

cdef void doSomething(char *string):
    if string == NULL:
        printString = None
    else:
        printString = string
    print printString
    # now I want to use NULL...

def doIt():
    cdef char* string = NULL
    doSomething(NULL)

Even if I use a custom function for this (which isn't easy to manage in
formatted strings), this will be or is a common cause for bugs. I don't
want to know the times that I have forgotten to check this...

- Johannes

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
Url : http://codespeak.net/pipermail/cython-dev/attachments/20080604/9a939503/attachment.pgp 


More information about the Cython-dev mailing list