Skip to content

API: Fix cython exception handling for exported extern C functions#22997

Merged
charris merged 3 commits intonumpy:mainfrom
seberg:issue-19291
Jan 17, 2023
Merged

API: Fix cython exception handling for exported extern C functions#22997
charris merged 3 commits intonumpy:mainfrom
seberg:issue-19291

Conversation

@seberg
Copy link
Copy Markdown
Member

@seberg seberg commented Jan 11, 2023

This hopefully fixes them for all the functions currently in the .pyd files. A surprising amount of them look like scary thing I wouldn't mind to just delete :).

Anyway, had looked at some Cython code today and remembered this.

Closes gh-19291

This hopefully fixes them for all the functions currently in the `.pyd`
files.  A surprising amount of them look like scary thing I wouldn't
mind to just delete :).

Anyway, had looked at some Cython code today and remembered this.

Closes numpygh-19291
int PyArray_CastTo (ndarray, ndarray) except -1
int PyArray_CastAnyTo (ndarray, ndarray) except -1
int PyArray_CanCastSafely (int, int) # writes errors
npy_bool PyArray_CanCastTo (dtype, dtype) # writes errors
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 think all these "writes errors" should either be except * (if you can't say anything about the return value), or except? 0 (if errors always produce zero, but not all zeros are errors).

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.

Oh, "writes errors" here means it uses PyErr_WriteUnraiseable. So I think for cython that should be as good as nothing.

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.

Oh, I thought this meant "sets the exception but doesn't indicate it in the return value".

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.

PyErr_WriteUnraiseable -> PyErr_WriteUnraisable

Just to help searching for the documentation :)

npy_intp PyArray_PyIntAsIntp (object)
int PyArray_Broadcast (broadcast)
int PyArray_Broadcast (broadcast) except -1
void PyArray_FillObjectArray (ndarray, object)
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.

Should this one (and probably a handful of other void functions) be except *? I assume this doesn't swallow errors.

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.

Arg, I didn't scan the void ones carefully, I guess. This isn't designed to error, but clearly can... (one of those bad functions). (the xincref/decref family is also weird, I think they tend to clear, but probably not always... and they shouldn't be used anway, especially in cython)

The incref/decref function shouldn't be able to fail (especially the
decref).  But right now they can, this will be fixed when we redo
clearing (see numpygh-22924)
@seberg
Copy link
Copy Markdown
Member Author

seberg commented Jan 13, 2023

As confirmed by Stefan Behnel in the issue, I think this should be rather safe to do. Cython will insert error handling and any manual error handling simply becomes obsolete.

@charris
Copy link
Copy Markdown
Member

charris commented Jan 17, 2023

Let's give this a shot. Thanks Sebastian.

@charris charris merged commit 2e6b85d into numpy:main Jan 17, 2023
@seberg seberg deleted the issue-19291 branch January 17, 2023 20:54
@zoj613
Copy link
Copy Markdown
Contributor

zoj613 commented Jan 17, 2023

Thanks for closing #19291, @seberg

@seberg
Copy link
Copy Markdown
Member Author

seberg commented Jan 17, 2023

@scoder is it still relevant to push such changes to cython (in which case I can make a PR)?

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG,ENH: cython stubs should use except (0|-1?) etc. for integer returning functions

4 participants