[email protected] wrote:
> Stefan Behnel wrote:
>> [email protected] wrote:
>>> I my case I have a c function like:
>>>
>>> void foo(char *)
>>>
>>> cdef c_foo "foo"(char *)
>>>
>>>
>>> cdef class A:
>>>    def __init__(self,arg0):
>>>      c_foo(arg0)
>> Unless you are dealing with binary data, you should not do this.
> 
> foo(char *) is expected to get an zero terminated string.
> 
> What exactly

This:

    def __init__(self,arg0):
        c_foo(arg0)

because it leaks implementation details into your Python-level API. If your
users are required to pass byte strings (e.g. binary data), this is
perfectly fine. But if the idea is to pass some kind of textual data (i.e.
unicode strings), then you have to do some kind of conversion, usually by
encoding the string to a byte sequence. The specific encoding depends on
what your C function can handle and should be hidden from user code.

It's ok to strictly require unicode input here in Py3, where unicode
strings and byte strings are distinct types. You may or may not want to
find a suitable way to deal with byte string input in Py2, but that is
application specific. A helper function that does the conversion for any
input value you get from Python space (and raises an appropriate exception
on errors) will likely prove useful here.

I think I'll actually write this up as a FAQ entry.

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

Reply via email to