Am 06/04/2008 07:47 PM schrieb Stefan Behnel:
> Johannes Wienke wrote:
>> I've just noticed that using NULL values in char* is a bad idea when
>> converting them to a python string.
> 
> Every pointer can potentially be NULL. If Cython added NULL checks to
> everything that might be NULL, you'd get pretty inefficient code with loads of
> expensive conditionals. You should take care to write robust code yourself.

To my mind only char pointers would need this extra behavior as they
have a somewhat special role in C because of the absence a string type.

>> *Wouldn't it be a good idea to automatically convert them to None?*
> 
> What would be gained from having to check for None instead of having to check
> for NULL? Checking for NULL, referencing None and then checking for None is
> definitely more expensive than a straight check for NULL in your code.

None is automatically converted to Python strings, so you can use it in
every string or print statement without troubles. This conversion should
also only happen if the char* needs to be converted to a python string.
Than it is in your own control to do this. In all cases where the string
is not NULL this would only cost one little comparison with NULL.

Moreover I think it's more desirable to have code that is less error
prone (a program dying because of segfault is a serious bug) than the
tiny speed up by wasting _one_ comparison with NULL. As someone else on
this list said before: cython is all about writing C without writing
C... And if I am not writing C, or at least try to minimize the part, I
don't want to waste my time with ugly C memory management stuff.

> If you *really* want None, then you can use something like this:
> 
>     cdef inline stringOrNone(char* value):
>          if value is NULL: return None
>          return value

That's exactly what I'm doing now but that's error-prone as you have to
do this manually and can forget it.

- Johannes

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to