Skip to content

Add miscellaneous non-limited PyError API functions#5989

Open
clin1234 wants to merge 9 commits intoPyO3:mainfrom
clin1234:patch-9
Open

Add miscellaneous non-limited PyError API functions#5989
clin1234 wants to merge 9 commits intoPyO3:mainfrom
clin1234:patch-9

Conversation

@clin1234
Copy link
Copy Markdown
Contributor

No description provided.

Comment thread pyo3-ffi/src/cpython/pyerrors.rs
// skipped PyAttributeErrorObject
#[repr(C)]
#[derive(Debug)]
pub struct PyNameErrorObject {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These two structs are not used by any public function. Is it useful to expose them? They are not documented on python.org

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These two are exposed in public headers: https://github.com/python/cpython/blob/ad7d3616c6cc21c5ec032a726e4c5e819628aa6e/Include/cpython/pyerrors.h#L76-L85

Since they aren't prefixed with _, better to expose them

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.

Why is it better to expose them? If there's not an actual use case, it's just extra code to maintain for no benefit.

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 if we'd exposed none of the other structs, I would support not adding. But I think now that the ship has sailed it somewhat doesn't hurt to add these for completeness; otherwise some user might eventually start doing something with the raw objects and run into a wall with these ones unexpectedly.

One alternative is to remove the others and support the structures for none of them, but probably too late for now. (Maybe CPython removes the structures in the future.)

@clin1234 clin1234 changed the title Add PyNameErrorObject and PyAttributeErrorObject structs, and miscellaneous API functions Add miscellaneous non-limited API functions Apr 19, 2026
@clin1234 clin1234 changed the title Add miscellaneous non-limited API functions Add miscellaneous non-limited PyError API functions Apr 19, 2026
@@ -188,3 +229,18 @@ pub struct PyStopIterationObject {
// skipped _Py_FatalErrorFunc
// skipped _Py_FatalErrorFormat
// skipped Py_FatalError
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.

There are a ton of skipped comments above which are now either implemented or no longer in the cpython headers. Would be great to synchronize them.

end_col_offset: c_int,
);
pub fn PyErr_ProgramTextObject(filename: *mut PyObject, lineno: c_int) -> *mut PyObject;
pub static PyExc_PythonFinalizationError: *mut PyObject;
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 we could implement Py_FatalError as an inline function calling _Py_FatalErrorFunc privately.

// skipped PyAttributeErrorObject
#[repr(C)]
#[derive(Debug)]
pub struct PyNameErrorObject {
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 if we'd exposed none of the other structs, I would support not adding. But I think now that the ship has sailed it somewhat doesn't hurt to add these for completeness; otherwise some user might eventually start doing something with the raw objects and run into a wall with these ones unexpectedly.

One alternative is to remove the others and support the structures for none of them, but probably too late for now. (Maybe CPython removes the structures in the future.)

Comment thread newsfragments/5989.added.md Outdated
@@ -0,0 +1 @@
Add PyNameErrorObject and PyAttributeErrorObject structs, and miscellaneous API functions
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 list the full set of public API structs / functions added.

Removed several skipped error handling functions from pyerrors.rs.
Added new structs and several API functions related to error handling.
@clin1234
Copy link
Copy Markdown
Contributor Author

_Py_FatalErrorFunc is wrapped as a call to fatal_error:

void _Py_NO_RETURN
_Py_FatalErrorFunc(const char *func, const char *msg)
{
    fatal_error(fileno(stderr), 1, func, msg, -1);
}

and the latter's implementation is this: https://github.com/python/cpython/blob/main/Python/pylifecycle.c#L3621-L3701

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 23, 2026

Merging this PR will not alter performance

✅ 105 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing clin1234:patch-9 (d730b90) with main (c24a951)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports.

Sign up for free to 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.

4 participants