This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: array module "minimum size in bytes" table is wrong for int/long
Type: Stage:
Components: Documentation Versions: Python 3.6, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Jonathan Booth, docs@python, georg.brandl, random832, serhiy.storchaka
Priority: normal Keywords:

Created on 2016-04-21 19:07 by Jonathan Booth, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg263931 - (view) Author: Jonathan Booth (Jonathan Booth) Date: 2016-04-21 19:07
https://docs.python.org/3.5/library/array.html describes the 'I' and 'i' typecodes as being minimum-size in bytes of 2. The interpreter disagrees:

>>> import array
>>> a = array.array('i')
>>> a.itemsize
4

There is also a bug with the 'L' and 'l' long typecodes, which document as min-size of 4 bytes but are 8 bytes in the interpreter. That could be a bug in cPython itself though, as if 'L' should be 8 bytes, that disagrees with the type code sizing from the struct module, where it is 4 bytes, just like integers.

I checked documentation for all versions of python and it matches -- I did not check all python interpreters to see they match, but 2.7 and 3.5 did.
msg263935 - (view) Author: (random832) Date: 2016-04-21 20:36
It says *minimum* size for a reason. The *actual* sizes of the types used in array are platform-dependent. 2 is the smallest that an int can be (it is probably not that size on any currently supported platforms, but would have been in DOS), and 4 is the smallest size a long can be (any 32-bit python build, and I think also any build on Windows, will have it be this size).
msg263938 - (view) Author: Jonathan Booth (Jonathan Booth) Date: 2016-04-21 22:25
Ugly -- if I know I'm dealing with 4-byte data, I can't just specify 'I' or 'L' because it'll be wrong on some platform? Maybe the bug is really the module's design. Seems I need to look elsewhere for other reasons (array seems to want to copy memory, rather than sharing it, so I can't get two arrays one int, one byte with the same backing memory where changes to one effect the other), but that's all the more argument for me to switch off array anyway.

In any case, take it as the documentation wasn't particularly clear.
msg263957 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2016-04-22 05:08
Indeed, I don't think the `array` module is much used anymore.

If you're looking to serialize or deserialize fixed-size formats, you'll probably want the `struct` module.  It has both native and fixed-size modes.

For anything else involving arrays (mostly, but not exclusively, of numbers), just use numpy.  It has a much more developed system of data types for its arrays, and supports views.
msg263958 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-04-22 05:22
> I can't get two arrays one int, one byte with the same backing memory where changes to one effect the other

For this case you can use an array or bytearray + memoryview.

I think this issue can be closed.
msg263960 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2016-04-22 05:27
Agreed.
History
Date User Action Args
2022-04-11 14:58:29adminsetgithub: 71008
2016-04-22 05:27:21georg.brandlsetstatus: open -> closed
resolution: not a bug
messages: + msg263960
2016-04-22 05:22:21serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg263958
2016-04-22 05:08:56georg.brandlsetnosy: + georg.brandl
messages: + msg263957
2016-04-21 22:25:02Jonathan Boothsetmessages: + msg263938
2016-04-21 20:36:08random832setnosy: + random832
messages: + msg263935
2016-04-21 19:07:41Jonathan Boothcreate