Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PyErr_SetObject() behavior is strange and not as documented. #101578

Closed
Labels
3.10only security fixes3.11only security fixes3.12only security fixes3.7 (EOL)end of life3.8 (EOL)end of life3.9 (EOL)end of lifetopic-C-APItype-bugAn unexpected behavior, bug, or error
@markshannon

Description

@markshannon

Briefly:
PyErr_SetObject(exc_type, exc_val) does not create a new exception iffisinstance(exc_val, BaseException), but usesexc_val instead.

Callers ofPyErr_SetObject() need various workarounds to handle this.

The long version:

Internally CPython handles exceptions as a triple(type, value, traceback), but the language treats exceptions as a single value.

This a legacy of the olden days before proper exceptions.
To handle adding proper exceptions to Python, various error handling functions, specifically_PyErr_SetObject still treat exceptions as triples, with the convention that if the value is an exception, then the exception is already normalized.

One other oddity is that ifexc_val is a tuple, it is treated as the* arguments toexc_type when calling it. So, ifisinstance(exc_val, BaseException) the desired behavior can be achieved by wrappingexc_val in a one-tuple.

As a consequence, both_PyErr_SetKeyError and_PyGen_SetStopIterationValue are a lot more complex than they should be to workaround this behavior.

We could makePyErr_SetObject act as documented, but that is likely to break C extensions, given how old this behavior is, and that it is relied on throughout CPython.

Code that does the following is common:

exc=new_foo_exception();PyErr_SetObject(&PyFooException_Type,exc);

We could just document the current behavior, but the current behavior is strange.
What I suggest is this:

  • Create a new API function, PyErr_SetException(exc)` that takes a single exception object.
  • DocumentPyErr_SetObject() accurately
  • Deprecate the old function

This is an old bug going back to the 2 series.

Linked PRs

Also relevant:

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10only security fixes3.11only security fixes3.12only security fixes3.7 (EOL)end of life3.8 (EOL)end of life3.9 (EOL)end of lifetopic-C-APItype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp