Johannes Wienke wrote:
> Hi,
>
> Am 06/04/2008 11:36 PM schrieb Dag Sverre Seljebotn:
>> 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.
> >
>> 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,
>
> Well but for my purposes the legacy code calls into cython.
>
>> print "%s ... %s" % (cb2str(a), cb2str(b))
>
> What is that function? I have never seen that before?
I just renamed what Stefan already provided you (I think you may have
missed his point though, go reread his first post). Here, I'll
reimplement it for you:
cdef inline object cb2str(char* bytebuf):
if bytebuf == NULL:
return "None"
else:
return bytebuf
Then, for Python 3 compatability and nice logging of any binary data you
are passed, you can switch to
cdef inline object cb2str(char* bytebuf):
if bytebuf == NULL:
return u"None"
else:
try:
return unicode(bytebuf, "iso-8859-1")
except:
# have some code to return a hex-formatted string instead,
# as the char* doesn't contain latin data...
And so on. The point is: Outputting a char* to a logfile is a
potentially complex task with associated business logic. Stick it in a
function.
--
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev