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

Improve Python <-> .NET exception integration#1134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
lostmsu merged 42 commits intopythonnet:masterfromlosttech:PR/ExceptionsImprovement
Jun 1, 2021

Conversation

lostmsu
Copy link
Member

@lostmsulostmsu commentedMay 5, 2020
edited
Loading

What does this implement/fix? Explain your changes.

  1. .NET exceptions bubbled through Python stack back into .NET are preserved (before:PythonException was thrown instead).
  2. Codecs can now encode and decode exceptions
  3. .NETInnerException property is populated from Python's__cause__ attribute.
  4. Python exception stack is added to .NET exception stack
  5. Internal: simplified memory management inPythonException class. Instead of working with raw pointers, usesPyObject instances.

Breaking

  1. Traceback,PyVal andPyType are now stored and returned asPyObjects.
  2. PythonException.Restore no longer nulls outPythonException instance.
  3. PythonException instances are no longer disposable. You can still dispose.Type,.Value and.Traceback, but it is not recommended, as they may be shared with other instances.

Does this close any currently open issues?

#893,#1098

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Add yourself toAUTHORS
  • Updated theCHANGELOG

jiajiawei reacted with thumbs up emoji
…ET interopNew method ThrowLastAsClrException should be used everywhere instead of obsolete PythonException constructor. It automatically restores .NET exceptions, and applies codecs for Python exceptions.Traceback, PyVal and PyType are now stored and returned as PyObjects.PythonException now has InnerException set from its cause (e.g. __cause__ in Python, if any).PythonException.Restore no longer clears the exception instance.All helper methods were removed from public API surface.
@lostmsulostmsuforce-pushed thePR/ExceptionsImprovement branch from4fe0548 to0961c94CompareMay 13, 2020 21:04
The new type indicates parameters of C API functions,that steal references to passed objects.
Removed private fields, apart from ones returned by `PyErr_Fetch`.Corresponding property values are now generated on demand.Added FetchCurrent*Raw for internal consumption.`PythonException.Type` is now of type `PyType`.Use C API functions `PyException_GetCause` and `PyException_GetTraceback` instead of trying to read via attributes by name.`PythonException` instances are no longer disposable. You can still dispose `.Type`, `.Value` and `.Traceback`, but it is not recommended, as they may be shared with other instances.
@lostmsu
Copy link
MemberAuthor

Damn CI failures do not reproduce locally :/

@lostmsulostmsuforce-pushed thePR/ExceptionsImprovement branch from4c76f15 to4877fe7CompareApril 11, 2021 10:33
@lostmsulostmsuforce-pushed thePR/ExceptionsImprovement branch from1512a55 to87dd9fdCompareApril 11, 2021 20:27
@lostmsulostmsu mentioned this pull requestMay 21, 2021
@lostmsulostmsu marked this pull request as ready for reviewMay 23, 2021 09:01
@lostmsulostmsu requested a review fromfilmorMay 23, 2021 09:02
@lostmsulostmsuforce-pushed thePR/ExceptionsImprovement branch 2 times, most recently fromf812ec7 to651d145CompareMay 29, 2021 22:24
@lostmsulostmsuforce-pushed thePR/ExceptionsImprovement branch from651d145 toc500a39CompareMay 29, 2021 23:47
@lostmsulostmsu merged commit7d8f754 intopythonnet:masterJun 1, 2021
@Felk
Copy link

Hello, given thatExceptions is not public anymore, what is the recommended way of catching specific python exceptions? Previously I used this:

catch(PythonExceptionex)when(ex.PyType==Exceptions.KeyError)

@lostmsu
Copy link
MemberAuthor

lostmsu commentedJun 14, 2021
edited
Loading

@Felk You can getKeyError frombuiltins module.

@Felk
Copy link

Thank you, this is how I got it to work now:

dynamicbuiltins=Py.Import("builtins");try{// ...}catch(PythonExceptionex)when(ex.Type.Equals(builtins.KeyError)){// ...}

@lostmsu
Copy link
MemberAuthor

@Felk comparing type handles will be slightly faster

@Felk
Copy link

@lostmsu can you give me an example of what you mean?

@lostmsu
Copy link
MemberAuthor

lostmsu commentedJun 14, 2021
edited
Loading

@Felkex.Type.Handle == builtins.KeyError.Handle. But in you scenario that should not matter, cause generally exception handling code won't be performance critical part.

If you really cared, you'd also cachebuiltins.KeyError into a variable of typePyObject to avoid dynamic dispatch.

Felk reacted with thumbs up emoji

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

@filmorfilmorAwaiting requested review from filmor

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@lostmsu@Felk@SDEII

[8]ページ先頭

©2009-2025 Movatter.jp