Johannes, I think you are right. The char* -> str conversion should
return None if the pointer is NULL. This is even consistent with the
form Py_BuildValue() works in Python C API.

As a side note, the mapping of NULL pointers to None is what SWIG does
in almost all cases. I would not follow that approach in Cython for
all cases, but the char* -> str is special enough.

Stefan, are you completelly sure that the performance implications of
checking for NULL pointers is this case are noticeable enough as to do
not follow the safe path? IMHO, I would avoid the chances for
segfaults.


On 6/4/08, Johannes Wienke <[EMAIL PROTECTED]> wrote:
> 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
>
>
> _______________________________________________
>  Cython-dev mailing list
>  [email protected]
>  http://codespeak.net/mailman/listinfo/cython-dev
>
>
>


-- 
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to