Skip to content

BUG, ENH: Access PyArrayMultiIterObject fields using macros. - #24658

Merged
seberg merged 12 commits into
numpy:mainfrom
zoj613:multiiter-macros
Oct 18, 2023
Merged

seberg merged 12 commits into
numpy:mainfrom
zoj613:multiiter-macros

Conversation

@zoj613

@zoj613 zoj613 commented Sep 6, 2023 •

Copy link
Copy Markdown
Contributor

Adds macros to the C-API for accessing PyArrayMultiIterObject fields. At the Cython level, these macros can be used to access fields of a np.broadcast type. This is to restore Cython 0.29.X functionality that was lost in #16986 where the fields of np.broadcast could no longer be accessed directly.

closes #24649

Cc @seberg, Is there a testing framework for the C-API somewhere? I searched and could not see how one would test the added macros.

Comment thread numpy/core/include/numpy/ndarraytypes.h Outdated
Comment thread numpy/__init__.cython-30.pxd
@seberg

seberg commented Sep 7, 2023

Copy link
Copy Markdown
Member

Thanks! Sorry, about testing, this is mainly a Cython problem right now. There are some Cython tests in core/test/test_cython.py which use files from core/test/examples/cython. Adding such a test would make sense I think.
Then we can test the attributes access working directly (which will pass after adding the property code).

@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Sep 7, 2023
@seberg

seberg commented Sep 7, 2023

Copy link
Copy Markdown
Member

It is a bit weird to add new macros in a backport for cython, but I think we need to backport this to allow projects using this (for better or worse) to transition to cython 3.

@zoj613

zoj613 commented Sep 8, 2023

Copy link
Copy Markdown
Contributor Author

Could you take another look when you can @seberg. I addressed the above comments and added tests as suggested.

Comment thread numpy/__init__.pxd Outdated
@mattip

mattip commented Sep 10, 2023

Copy link
Copy Markdown
Member

Is your code using these constructs public? I wonder if there is a better way to use the MultiIter than to expose these internals.

@zoj613

zoj613 commented Sep 10, 2023 •

Copy link
Copy Markdown
Contributor Author

Is your code using these constructs public? I wonder if there is a better way to use the MultiIter than to expose these internals.

Yes, it is used at several places in this file. An example is this snippet

I thought the addition of macros to access information would be considered better than direct access to the struct fields? Also, is there a reason why the Array Iterator API from here is not part of the public API? I would have preferred that over using PyArray_MultiIter_* since its newer, but it doesn't appear to be accessible via Cython (at least it wasn't at the time I wrote the above code).

Comment thread numpy/core/include/numpy/ndarraytypes.h Outdated
@mattip

mattip commented Sep 10, 2023

Copy link
Copy Markdown
Member

I thought the addition of macros to access information would be considered better than direct access to the struct fields?

Macros are indeed better than direct access, but even better for flexibility are getter/setter functions, even if they are statically defined. They at least provide some typing information.

Also, is there a reason ...

I don't think there was ever a demand for it.

I am not sure I understand how we "removed np.broadcast from the 0.29.x cython pxd file in #16986", as the motivation for this PR. All I see is the removal of extern from the declaration. I think this PR should do the minimum to restore the property access via broadcast.numiter and friends for cython 3.0+

@zoj613

zoj613 commented Sep 10, 2023 •

Copy link
Copy Markdown
Contributor Author

I am not sure I understand how we "removed np.broadcast from the 0.29.x cython pxd file in #16986", as the motivation for this PR. All I see is the removal of extern from the declaration. I think this PR should do the minimum to restore the property access via broadcast.numiter and friends for cython 3.0+

In #16986 (comment) I mentioned that the ability to access the fields (either directly or indirectly) of a np.broadcast object is lost in the cython3 init file, compared to the functionality provided by the init file of cython 0.29.X. If you look at https://github.com/scoder/numpy/blob/df769ef591f8a21f8e39ee30c80e09d1b7e0a705/numpy/__init__.cython-30.pxd#L230-L232, you will notice that the fields no longer exist for np.broadcast. That is the motivation for this PR: To restore that functionality by either direct access (via @Property decorator) and/or using the macros/functions defined in https://github.com/numpy/numpy/pull/24658/files/6ab7f384e927fb3d7afea0fa69b2ee88c9d353ee#diff-1e081d27e4d07ed1eaa8fe4af755d81de8c1dc062c919559b9678788dfc691f3 .

@mattip mattip added the triage review Issue/PR to be discussed at the next triage meeting label Sep 11, 2023
@seberg

seberg commented Sep 11, 2023

Copy link
Copy Markdown
Member

You can use the new API and we can add it (not backport). I would have guess that hte few times someone wanted it, they added it to their own file because they didn't want to wait for upstream to have it anyway (which doesn't mean you can't add it here, too).

@zoj613

zoj613 commented Sep 11, 2023

Copy link
Copy Markdown
Contributor Author

You can use the new API and we can add it (not backport). I would have guess that hte few times someone wanted it, they added it to their own file because they didn't want to wait for upstream to have it anyway (which doesn't mean you can't add it here, too).

Skimming through https://numpy.org/doc/stable/reference/c-api/iterator.html, it looks like this would be great as a separate PR so as to not block this one. I am happy to open one that adds the new iterator API to Cython.

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

I thought we had already put this in. For some reason force pushing a rebase to fix the merge conflict isn't working for me, though?!

@zoj613 could you maybe rebase? Sorry about that. Otherwise, this should just go in IMO @charris.

@zoj613

zoj613 commented Oct 18, 2023

Copy link
Copy Markdown
Contributor Author

I thought we had already put this in. For some reason force pushing a rebase to fix the merge conflict isn't working for me, though?!

@zoj613 could you maybe rebase? Sorry about that. Otherwise, this should just go in IMO @charris.

Could you try again? I think I had to toggle the "Allow edits and access to secrets by maintainers" option

@seberg

seberg commented Oct 18, 2023

Copy link
Copy Markdown
Member

Could you try again? I think I had to toggle the "Allow edits and access to secrets by maintainers" option

Thanks, worked.

@seberg
seberg merged commit 1aae0a2 into numpy:main Oct 18, 2023
lysnikolaou pushed a commit to lysnikolaou/numpy that referenced this pull request Oct 19, 2023
…#24658)

Adds static inline functions to the C-API for accessing `PyArrayMultiIterObject` fields.
At the Cython level, these macros can be used to access fields of a
`np.broadcast` type.
@charris

charris commented Oct 19, 2023 •

Copy link
Copy Markdown
Member

This doesn't backport easily. There are failing tests, the names of have changed (_build) making comparisons difficult. I can probably fix things given time, but it would be easier to forego the backport.

EDIT: I'm tempted to tag a commit prior to the change to _build just for differencing.

@charris charris added this to the 1.26.3 release milestone Nov 12, 2023
seberg pushed a commit that referenced this pull request Nov 16, 2023
This is a followup to gh-24658, and adds the new Iterator API to the Cython 3.X init.
@charris charris removed this from the 1.26.3 release milestone Jan 9, 2024
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jan 9, 2024
@charris

charris commented Jan 9, 2024

Copy link
Copy Markdown
Member

Took this off the backport list.

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

Labels

00 - Bug triage review Issue/PR to be discussed at the next triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Cython 3 pyd file doesn't allow access to multiterobject fields

4 participants