[Pyrex] Handling bytes

Stefan Behnel stefan_ml at behnel.de
Fri May 9 10:31:59 CEST 2008


Hi,

Brad Schick wrote:
> On 5/8/2008 12:05 AM, Stefan Behnel wrote:
>>> It just seems
>>> so ugly in what is otherwise a fairly clean language. Particularly when
>>> the data sometimes starts life as a list. How do you go about stuffing
>>> 'bytes' into a string? With arrays as below? Or with cStringIO? Yuck and
>>> yuck ;)
>>>     
>> Sure, why not? You can also create an empty string and fill it yourself:
>>
>>    cdef int i
>>    cdef char* s
>>    py_s = PyString_FromStringAndSize(NULL, len(data))
>>    s = py_s
>>    for i in range(len(data)):
>>        s[i] = l[i]
>>   
> 
> I meant in python code. In other words, the code calling into the pyrex
> module that will need to cram everything into strings. That's fine if it
> the data starts in a string or file, but its pretty ugly and inefficient
> if it starts in some other sequence like a list.

But that's just an API design question. At the API leve, you can accept byte
strings and iterables that return byte numbers, and just write a Pyrex
function that detects what you actually got and return a normalised internal
representation (and maybe raise an exception for everything else). Then just
call it at the beginning of every API function and you're done thinking about
this issue.

Stefan



More information about the Pyrex mailing list