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

gh-146151: memoryview supports 'F' and 'D' format types (complex)#146241
vstinner merged 5 commits into
python:mainfrom
skirpichev:support-complex-memoryview/146151

Conversation

@skirpichev

@skirpichevskirpichev commented Mar 21, 2026

Copy link
Copy Markdown
Member

Comment threadDoc/whatsnew/3.15.rst Outdated
Comment threadLib/test/test_memoryview.py

@vstinnervstinner 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.

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:

>>>importstruct, 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
MemberAuthor

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:mainMar 26, 2026
51 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 freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@skirpichev@vstinner