Hi,

Johannes Wienke wrote:
> Am 06/04/2008 07:47 PM schrieb Stefan Behnel:
> 
> 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.

Then why None and not ''? And why None and not a ValueError?


> None is automatically converted to Python strings, so you can use it in
> every string or print statement without troubles.

Not on my side of the cable:

    Python 2.5.1 (r251:54863, Mar  7 2008, 04:10:12)
    [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> "abc" + None
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: cannot concatenate 'str' and 'NoneType' objects

And the following will still crash, even under your proposal:

    cdef char* s = NULL
    py_s = s
    print PyString_GET_SIZE(py_s)

And would you also want this to work:

    cdef char* s = None


> don't want to waste my time with ugly C memory management stuff.

This is not about memory management at all. This is about making sure your
code handles corner cases correctly. Where does the NULL pointer come from
anyway? Is it maybe an error return of a function that you didn't catch? You
wouldn't want Cython to find your off-by-one errors also, would you?


>> 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.

Why? Isn't it good practice to guard your code against anticipated invalid
data? Don't you ask yourself from time to time when you write Python code:
"can this variable be None?" So why not ask "can this pointer be NULL" in 
Cython?

I think it's an advantage that you get a straight crash when your code
contains the obvious bug that you forgot to test a NULL pointer for a NULL
value, instead of an automatic coercing to None, which might propagate through
your application and induce hard to track down bugs elsewhere in your program.
Usually, C value handling happens within a limited scope (e.g. a function),
while Python values tend to cross API boundaries into user code.

Stefan

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

Reply via email to