Assigning generator expressions to ctype arrays

Terry Reedy tjreedy at udel.edu
Thu Oct 27 23:23:04 EDT 2011


On 10/27/2011 8:09 PM, Patrick Maupin wrote:

 > x[:] = (x for x in xrange(32))

This translates to

s.__setitem__(slice(None,None), generator_object)

where 'generator_object' is completely opaque, except that it will yield 
0 to infinity objects in response to next() before raising StopIteration.

Given that a cytpe_array is a *fixed-length* array, *unlike* Python's 
extensible lists and arrays, failure is a possibility due to mis-matched 
lengths. So ctype_array can either look first, as it does, by calling 
len(value_object), or leap first and create a temporary array, see if it 
fills up exactly right, and if it does, copy it over.

> I know how to work around the issue.  I'm not sure I should have to.

I do not think everyone else should suffer substantial increase in space 
and run time to avoid surprising you.

> It violates the principle of least surprise

for ctypes to do what is most efficient in 99.9% of uses?

> for the ctypes array to
> not be able to interoperate with the iterator protocol in this
> fashion.

It could, but at some cost. Remember, people use ctypes for efficiency, 
so the temp array path would have to be conditional. When you have a 
patch, open a feature request on the tracker.

-- 
Terry Jan Reedy




More information about the Python-list mailing list