Skip to content

gh-91484: Allow memoryview cast for F-contiguous#137803

Open
jamie2779 wants to merge 3 commits into
python:mainfrom
jamie2779:gh-91484
Open

gh-91484: Allow memoryview cast for F-contiguous#137803
jamie2779 wants to merge 3 commits into
python:mainfrom
jamie2779:gh-91484

Conversation

@jamie2779

@jamie2779 jamie2779 commented Aug 15, 2025

Copy link
Copy Markdown

Allow N-D to 1-D memoryview cast for F-contiguous

@python-cla-bot

python-cla-bot Bot commented Aug 15, 2025

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Aug 15, 2025

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

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

This change needs a NEWS entry, you can do it via the web interface (comment above) or with the blurb tool locally. You also have some trailing whitespace.

@jamie2779

Copy link
Copy Markdown
Author

@corona10 Please check my Pull Request

@jamie2779

Copy link
Copy Markdown
Author

@StanFromIreland thank you! I added NEWS entry.

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

Please check lint status, looks like there is empty space in some line.

@jamie2779

Copy link
Copy Markdown
Author

Ok, I removed the trailing whitespace.

@serhiy-storchaka serhiy-storchaka self-requested a review August 15, 2025 10:06
@jamie2779 jamie2779 requested a review from corona10 August 21, 2025 05:43
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 28, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Sprint Apr 28, 2026

@serhiy-storchaka serhiy-storchaka 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.

Looks fine. I added some minor suggestions, and please update Doc/library/stdtypes.rst which says "Supported casts are 1D -> C-:term:contiguous and C-contiguous -> 1D." Mention that an F-contiguous can also be cast to 1D. Add versionchanged:: next and a What's New entry.

I just submitted a complementary PR #153663. They can be merged in arbitrary order, but the latter will need to resolve conflict in What's New.

Comment thread Objects/memoryobject.c
PyErr_SetString(PyExc_TypeError,
"memoryview: casts are restricted to C-contiguous views");
return NULL;
if(shape || self->view.ndim == 1 || !MV_F_CONTIGUOUS(self->flags)) {

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.

Missed space, and self->view.ndim == 1 looks redundant -- for 1D view C- and F-contiguity are equivalent.

Suggested change
if(shape || self->view.ndim == 1 || !MV_F_CONTIGUOUS(self->flags)) {
if (shape || !MV_F_CONTIGUOUS(self->flags)) {

Comment thread Objects/memoryobject.c
return NULL;
if(shape || self->view.ndim == 1 || !MV_F_CONTIGUOUS(self->flags)) {
PyErr_SetString(PyExc_TypeError,
"memoryview: casts are restricted to C-contiguous views");

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.

It is now slightly misleading, since F-contiguous -> 1D is allowed.

Maybe just "contiguous views"?

Suggested change
"memoryview: casts are restricted to C-contiguous views");
"memoryview: casts are restricted to contiguous views");

@@ -0,0 +1 @@
memoryview.cast() now allows casting from N-D to 1-D for F-contiguous.

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.

Suggested change
memoryview.cast() now allows casting from N-D to 1-D for F-contiguous.
:meth:`memoryview.cast` now allows casting from N-D to 1-D for F-contiguous.

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

Labels

awaiting review sprint stale Stale PR or inactive for long period of time.

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

4 participants