Dag Sverre Seljebotn wrote:
> 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.

To expand on that: For your purposes, outputting "None" is probably 
wrong anyway. What happens if the client library passes a char* 
containing the string "None"? How do you distinguish it?

I.e. something like this is probably better (cb means "charbuf" btw):

cdef inline object cbrepr(char* bytebuf):
     if bytebuf == NULL:
         return u'None'
     else:
         try:
             return u'"%s"' % unicode(bytebuf, "iso-8859-1")
         except:
             return u'binary data:%s' % your_hex_dump_func(bytebuf)


(But now we're straying rather far from Cython and into general 
programming...)

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

Reply via email to