Skip to content

gh-146151: memoryview supports 'F' and 'D' format types (complex) - #146241

Merged
vstinner merged 5 commits into
python:mainfrom
skirpichev:support-complex-memoryview/146151
Mar 26, 2026
Merged

vstinner merged 5 commits into
python:mainfrom
skirpichev:support-complex-memoryview/146151

Conversation

@skirpichev

@skirpichev skirpichev commented Mar 21, 2026 •

Copy link
Copy Markdown
Member
Comment thread Doc/whatsnew/3.15.rst Resolved
Comment thread Lib/test/test_memoryview.py Resolved
@skirpichev
skirpichev requested a review from vstinner March 26, 2026 00:11

@vstinner vstinner left a comment

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.

LGTM. The implementation looks complete and well tested.

Comment thread Objects/memoryobject.c
memcpy(ptr, &x, sizeof(x));
}
else {
float x[2] = {(float)c.real, (float)c.imag};

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 wasn't sure of the behavior on underflow or overflow of the double to float cast, so I tested:

>>> import struct, sys
>>> data=struct.pack('F', 0.0) * 4
>>> m=memoryview(bytearray(data)).cast('F')
>>> m[0]=math.nextafter(0, 1)
>>> m[1]=sys.float_info.min
>>> m[2]=sys.float_info.max
>>> m[3]=float("nan")
>>> m.tolist()
[0j, 0j, (inf+0j), (nan+0j)]

I got the values that I expected. I suppose that these conversions are well specified by IEEE 754.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The behavior here is same as for 'f' format, just component-wise, see PACK_SINGLE macro.

…e-146151.4-lhim.rst

Co-authored-by: Victor Stinner <vstinner@python.org>
@vstinner
vstinner merged commit c68a194 into python:main Mar 26, 2026
47 checks passed
@vstinner

Copy link
Copy Markdown
Member

Merged, thanks for your contribution.

@skirpichev
skirpichev deleted the support-complex-memoryview/146151 branch March 26, 2026 18:34
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Apr 16, 2026
…x) (python#146241)

Co-authored-by: Victor Stinner <vstinner@python.org>
skirpichev added a commit to skirpichev/cpython that referenced this pull request Apr 17, 2026
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
…x) (python#146241)

Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants