Skip to content

gh-146238: add missing tests for 'e', 'Zf' and 'Zd' array type codes in test_buffer.py#149345

Open
skirpichev wants to merge 6 commits intopython:mainfrom
skirpichev:amend-e-format
Open

gh-146238: add missing tests for 'e', 'Zf' and 'Zd' array type codes in test_buffer.py#149345
skirpichev wants to merge 6 commits intopython:mainfrom
skirpichev:amend-e-format

Conversation

@skirpichev
Copy link
Copy Markdown
Member

@skirpichev skirpichev commented May 4, 2026

This amends e79fd60.  I'll not fix this for 'F'/'D' complex types as
they might be removed.
@bedevere-app bedevere-app Bot added the tests Tests in the Lib/test dir label May 4, 2026
@skirpichev skirpichev closed this May 4, 2026
@skirpichev skirpichev deleted the amend-e-format branch May 4, 2026 15:25
@skirpichev skirpichev restored the amend-e-format branch May 5, 2026 02:01
@skirpichev skirpichev reopened this May 5, 2026
@skirpichev skirpichev changed the title gh-146238: add missing tests for 'e' type code in test_buffer.py gh-146238: add missing tests for 'e', 'Zf' and 'Zd' array type codes in test_buffer.py May 5, 2026
@skirpichev skirpichev requested a review from vstinner May 5, 2026 02:27
@skirpichev
Copy link
Copy Markdown
Member Author

CC @vstinner

@skirpichev

This comment was marked as resolved.

@read-the-docs-community

This comment was marked as spam.

Comment thread Lib/test/test_buffer.py
for case in cases:
case.append(0)
fmt = case.typecode
if struct.calcsize(fmt) <= MAX_ALIGN:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You already skipped formats larger than MAX_ALIGN in cases = [array.array(fmt) for fmt in ARRAY if struct.calcsize(fmt) <= MAX_ALIGN], no?

Comment thread Lib/test/test_buffer.py
Comment on lines +4500 to +4503
align = max(struct.calcsize(fmt) for fmt in ARRAY
if struct.calcsize(fmt) <= MAX_ALIGN)
cases = [array.array(fmt) for fmt in ARRAY
if struct.calcsize(fmt) <= MAX_ALIGN]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding a comment explaining why we skip some formats:

Suggested change
align = max(struct.calcsize(fmt) for fmt in ARRAY
if struct.calcsize(fmt) <= MAX_ALIGN)
cases = [array.array(fmt) for fmt in ARRAY
if struct.calcsize(fmt) <= MAX_ALIGN]
# Skip formats larger than the normal alignment of 'max_align_t',
# in bytes.
formats = [fmt for fmt in ARRAY if struct.calcsize(fmt) <= MAX_ALIGN]
align = max(struct.calcsize(fmt) for fmt in formats)
cases = [array.array(fmt) for fmt in formats]

Comment thread Lib/test/test_buffer.py
Comment on lines 144 to 148
# Format codes supported by array.array
ARRAY = NATIVE.copy()
for k in NATIVE:
if not k in "bBhHiIlLfd":
if not k in list("bBhHiIlLefd") + ['Zf', 'Zd']:
del ARRAY[k]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest only building the list once, replace not k in with k not in, replace k with fmt and add missing array formats (uw and qQ):

# Format codes supported by array.array
ARRAY = NATIVE.copy()
_ARRAY_FORMATS = list("bBuwhHiIlLqQefd") + ['Zf', 'Zd']
for fmt in NATIVE:
    if fmt not in _ARRAY_FORMATS:
        del ARRAY[fmt]

@vstinner
Copy link
Copy Markdown
Member

vstinner commented May 5, 2026

test_buffer fails on WASI:

FAIL: test_array_alignment (test.test_buffer.TestBufferProtocol.test_array_alignment)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Lib/test/test_buffer.py", line 4514, in test_array_alignment
    self.assertEqual(
    ~~~~~~~~~~~~~~~~^
        [_testcapi.buffer_pointer_as_int(case) % align for case in cases],
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        [0] * len(cases),
        ^^^^^^^^^^^^^^^^^
    )
    ^
AssertionError: Lists differ: [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8] != [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

First differing element 0:
8
0

- [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8]
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

@vstinner
Copy link
Copy Markdown
Member

vstinner commented May 5, 2026

On WASI, ALIGNOF_MAX_ALIGN_T is 16 bytes and struct.calcsize("Zd") is 16 bytes. WASI is a 32-bit platform (sys.maxsize is 2147483647). Maybe we should only test formats up to 4 bytes?

Would it make sense to use MAX_ALIGN = struct.calcsize('P')?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review skip news tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants