Skip to content

Managed exception from Python code is always transformed to PythonException #1443

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tibortakacs opened this issue Apr 16, 2021 · 7 comments
Closed

Comments

@tibortakacs
Copy link

tibortakacs commented Apr 16, 2021

Environment

  • Pythonnet version: 3.0.0 preview
  • Python version: 3.8 win32
  • Operating System: Windows
  • .NET Runtime: .NET 5

Summary

If there is a callback from .NET to Python, and the Python code raises a .NET type exception, instead of the corresponding real .NET exception was rethrown on the .NET side (after marshalling), always Python.Runtime.PythonException is thrown.

Details

.NET snippet

class Foo
{
    public static void Bar(Action action)
    {
        try
        {
            action.Invoke();
            Console.WriteLine("No exception.");
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("Null reference caught.");
        }
        catch (Exception ex)
        {
             Console.WriteLine("General exception caught: " + ex.GetType().Type); 
        }
    }
}

Python snippet

# .NET imports
from System import NullReferenceException, Action
from MyProgram import Foo

# Preparation
def call():
    raise NullReferenceException()

action = Action(call)

# Action
Foo.Bar(action)

Expected output:
Null reference caught.

Actual output:
General exception caught: Python.Runtime.PythonException

@tibortakacs tibortakacs changed the title Managed exception from Python code transformed to PythonException Managed exception from Python code is always transformed to PythonException Apr 16, 2021
@lostmsu
Copy link
Member

lostmsu commented Apr 16, 2021

In 2.x this is by design.
In 3.x the work is already in progress: #1134

@tibortakacs
Copy link
Author

@lostmsu Amazing, thank you. I see great progress has been achieved there.

Is there any workaround in the meantime that you can recommend?
Can I ask whether there is an estimation of merging this back into master? (Just asking this because if it will be available in the near future, I do not redesign our code with a workaround.)

@lostmsu
Copy link
Member

lostmsu commented Apr 16, 2021

@tibortakacs you can try simply using the branch from the PR.

@tibortakacs
Copy link
Author

@lostmsu Amazing, it works properly, not only in the simplified code but also in the real application. Based on this great progress, is it fair to say that there is a chance that it will be merged back into master in the next few weeks? (I am really sorry if my question seems to be "pushy", I am just more comfortable releasing our application based on the master if possible.)

@lostmsu
Copy link
Member

lostmsu commented Apr 16, 2021

A chance, yes. But I can not guarantee you that. I'd probably be more certain about the end of May.

@tibortakacs
Copy link
Author

@lostmsu Thank you, looking forward to it. If there any way I can support your work (e.g. I am happy to test it with our application regularly), please let me know.

@lostmsu
Copy link
Member

lostmsu commented Sep 23, 2021

Corresponding PR has been merged

@lostmsu lostmsu closed this as completed Sep 23, 2021
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

No branches or pull requests

2 participants