[issue32782] ctypes: memoryview gives incorrect PEP3118 itemsize for array of length zero

2019-04-14 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

This issue is about the itemsize attribute of instances of the built-in 
memoryview class.  Ctypes in only involved in providing format information.  
Hence the nosy additions.

On Win10 with 3.8, ctypes has no uint attributes.  Using 'c_int32' instead, I 
see the same behavior (itemsize 0 for empty structure).

About itemsize, https://www.python.org/dev/peps/pep-3118/ says

"This is a storage for the itemsize (in bytes) of each element of the shared 
memory. It is technically un-necessary as it can be obtained using 
PyBuffer_SizeFromFormat, however an exporter may know this information without 
parsing the format string and it is necessary to know the itemsize for proper 
interpretation of striding. Therefore, storing it is more convenient and 
faster."

The first line could be seen as implying that itemsize is undefined if there 
are no items (and as justifying numbytes/numitems otherwise).  The 0 return 
could be seen as equivalent to a None return from a python-coded function.  If 
so, it is not a bug, and there might be code that would break if it is changed.

On the other hand, the next lines imply that itemsize is *usually*, though not 
necessarily, a cache for PyBuffer_SizeFromFormat.  This could be seen as 
implying that in the absence of other information, the itemsize should be 
calculated from the format, making 0 a bug.

--
components: +Interpreter Core -ctypes
nosy: +skrah, teoliphant, terry.reedy
versions: +Python 3.8 -Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32782] ctypes: memoryview gives incorrect PEP3118 itemsize for array of length zero

2018-09-06 Thread Eric Wieser


Eric Wieser  added the comment:

Pinging again, for lack of a clearer path forward

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32782] ctypes: memoryview gives incorrect PEP3118 itemsize for array of length zero

2018-07-08 Thread Eric Wieser


Eric Wieser  added the comment:

Pinging, as recommended by https://devguide.python.org/pullrequest/#reviewing. 
Ideally this and https://bugs.python.org/issue32780 would make the same patch 
release.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32782] ctypes: memoryview gives incorrect PEP3118 itemsize for array of length zero

2018-02-09 Thread Terry J. Reedy

Change by Terry J. Reedy :


--
nosy: +amaury.forgeotdarc, belopolsky, meador.inge

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32782] ctypes: memoryview gives incorrect PEP3118 itemsize for array of length zero

2018-02-06 Thread Eric Wieser

Change by Eric Wieser :


--
keywords: +patch
pull_requests: +5394
stage:  -> patch review

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32782] ctypes: memoryview gives incorrect PEP3118 itemsize for array of length zero

2018-02-06 Thread Eric Wieser

New submission from Eric Wieser :

Take the following simple structure:

class Foo(ctypes.Structure):
_fields_ = [('f', ctypes.uint32_t)]

And construct some arrays with it:

def get_array_view(N):
return memoryview((Foo * N)())

In most cases, this works as expected, returning the size of one item:

>>> get_array_view(10).itemsize
4
>>> get_array_view(1).itemsize
4

But when N=0, it returns the wrong result

>>> get_array_view(0).itemsize
0

Which contradicts its `.format`, which still describes a 4-byte struct

>>> get_array_view(0).format
'T{>I:one:}'

This causes a downstream problem in numpy:

>>> np.array(get_array_view(0))
RuntimeWarning: Item size computed from the PEP 3118 buffer format string 
does not match the actual item size.

--
components: ctypes
messages: 311740
nosy: Eric.Wieser
priority: normal
severity: normal
status: open
title: ctypes: memoryview gives incorrect PEP3118 itemsize for array of length 
zero
type: behavior
versions: Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com