[Cython] char* and NULL in log statements
Martin C. Martin
martin at martincmartin.com
Wed Jun 4 21:38:22 CEST 2008
Stefan Behnel wrote:
> Hi,
>
> 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.
Actually, one of the lessons of JVM optimizations is that NULL checks
are only a single instruction when they're not non-null. You can tell
the compiler to predict that the "non-null" will be taken, and when it
is, there's no branch penalty.
However, I agree that it's against the spirit of C/C++, and hence
Cython, to automatically check all pointers for null.
>> *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.
>
> 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
>
> and wrap all your char*->byte string conversions explicitly in a call to this
> function. Remember, explicit is better than implicit.
>
> Stefan
>
> _______________________________________________
> Cython-dev mailing list
> Cython-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/cython-dev
More information about the Cython-dev
mailing list