[Cython] char* and NULL in log statements
Dag Sverre Seljebotn
dagss at student.matnat.uio.no
Wed Jun 4 23:36:00 CEST 2008
Johannes Wienke wrote:
> Hi,
>
> Am 06/04/2008 09:54 PM schrieb Stefan Behnel:
>> 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?
>
> Because None has for python the same meaning as NULL in C. ValueError
> would be another possibility. Nevertheless if NULL can be a legal value
> for the rest of that function, this would be as awkward to handle as the
> explicit check for NULL if I only want a safe print statement.
I have to agree with Stefan (not for performance reasons, but for not
bogging down the language with too much magic). In Cython you do have to
care about whether you are dealing with a C pointer or a Python object.
Blurring it in this specific case doesn't seem like a good idea, to me
it seems to simply encourage bad habits. (For instance, what kind of
behaviour are you assuming for non-ASCII values in that automatic char*
to str conversion?)
char* is usually used to call into legacy C code, if you need to print
them I'd argue that in most cases you are converting to char* one step
too early. But if you really need to print char* directly,
print "%s ... %s" % (cb2str(a), cb2str(b))
is *a lot* better than % (a, b) because
a) it is explicit what's going on (conversion of a byte buffer to a
string is *not* trivial and should not be transparent anyway)
b) you can change cb2str to use the charset you are assuming
c) your code can be made to work in Python 3
d) A function taking a char* may very well be taking unprintable
characters, perhaps cb2str should really hex-encode the data instead
and so on.
Raising ValueError (or similar) as a generic feature for "any C pointer
which coerces automatically to Python objects" (which currently is char*
only?) seems like an good feature (though depending on a decision being
reached on the char* coercion business then that might be deprecated
anyway, and then it may not be worth the effort).
However, Cython cannot be Java because C is not Java. It will always be
possible to do (<int*>0)[0] and segfault; that's part of the deal when
writing C.
Cython can very well work safely; simply use Python strings!; avoid all
pointers, etc. Java doesn't have a char* either. The Cython equivalent
to java.lang.String is a Python str, not a char*!
--
Dag Sverre
More information about the Cython-dev
mailing list