Visitar URL original
Python exception raised on conversion failure by macaronikazoo · Pull Request #2741 · pythonnet/pythonnet · GitHub
Skip to content

Python exception raised on conversion failure - #2741

Merged
filmor merged 1 commit into
pythonnet:masterfrom
macaronikazoo:exception-handling
Sep 25, 2026
Merged

filmor merged 1 commit into
pythonnet:masterfrom
macaronikazoo:exception-handling

Conversation

@macaronikazoo

@macaronikazoo macaronikazoo commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

What does this implement/fix? Explain your changes.

Converting a CLR value to Python can throw after the call has succeeded, eg if an encoder's TryEncode throws, or if calling enumerator.Current throws. Previously the exception was never converted to a Python exception making it uncatchable even instead, so Python code could not catch it, not even using except BaseException

  • MethodBinder.Invoke guarded only the invocation, and converted the result and out parameters after its try/catch
  • Iterator.tp_iternext guarded MoveNext but not Current or the conversion of the item
  • ArrayObject indexing converted items with no guard at all

Each now reports the failure through Exceptions.SetError, as the existing catch already does for the call itself. Property and field getters already convert inside their try and are unchanged.

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • [n/a] If an enhancement PR, please create docs and at best an example
  • Ensure you have signed the .NET Foundation CLA

Converting a CLR value to Python can throw after the call has
succeeded, eg if an encoder's TryEncode throws, or if calling
enumerator.Current throws. Previously the exception was never
converted to a Python exception making it uncatchable even
instead, so Python code could not catch it, not even using
except BaseException

- MethodBinder.Invoke guarded only the invocation, and converted the
  result and out parameters after its try/catch
- Iterator.tp_iternext guarded MoveNext but not Current or the
  conversion of the item
- ArrayObject indexing converted items with no guard at all

Each now reports the failure through Exceptions.SetError, as the
existing catch already does for the call itself. Property and field
getters already convert inside their try and are unchanged.
@filmor

filmor commented Sep 24, 2026

Copy link
Copy Markdown
Member

Thanks a lot. If all is well, I will merge this and release an rc2 today.

@macaronikazoo

Copy link
Copy Markdown
Contributor Author

ok no worries - I have another one that I'll create shortly... related to disallowing certain .net types to be handed over to python

@macaronikazoo

Copy link
Copy Markdown
Contributor Author

however, the other change I want to submit builds off this one - but this one seemed useful in an of itself, so I submitted it separately. should I wait for this one to be accepted before submitting the other PR?

@filmor

filmor commented Sep 24, 2026

Copy link
Copy Markdown
Member

Just go ahead. I won't have time before afternoon for a proper review.

@filmor
filmor merged commit a7af7a8 into pythonnet:master Sep 25, 2026
36 checks passed
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.

2 participants