Skip to content
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

Generic virtual method in baseclass causes InvalidProgramException #1776

Closed
rmadsen-ks opened this issue Apr 27, 2022 · 0 comments · Fixed by #2026 · May be fixed by #2019
Closed

Generic virtual method in baseclass causes InvalidProgramException #1776

rmadsen-ks opened this issue Apr 27, 2022 · 0 comments · Fixed by #2026 · May be fixed by #2019
Labels

Comments

@rmadsen-ks
Copy link
Contributor

rmadsen-ks commented Apr 27, 2022

Environment

  • Pythonnet version: 3.0.0-preview2022-04-11
  • Python version: 3.10
  • Operating System: MacOS and Ubuntu 2021.4
  • .NET Runtime: .net 6

Details

If I create a python class which inherits from a C# class that has a virtual generic method and exception is thrown while the class is being defined. Note, not during object instanciation time.

To reproduce, take the following C# class:

    public abstract class GenericVirtualMethodTest
    {
        public virtual Q VirtMethod<Q>(Q arg1)
        {
            return arg1;
        }
    }

Then inherit from it as in this test:

def test_virtual_generic_method():
    class OverloadingSubclass(GenericVirtualMethodTest):
        __namespace__ = "test_virtual_generic_method_cls"
    obj = OverloadingSubclass()
    assert obj.VirtMethod[int](5) == 5

The result is the following exception (Actually a C# exception inside a python exception inside a C# exception):

Python.Runtime.PythonException : An attempt was made to load a program with an incorrect format.
 (0x8007000B)
  File "/Users/bruger/code/work/pythonnet/src/../tests/test_subclass.py", line 268, in test_virtual_generic_method
    class OverloadingSubclass(GenericVirtualMethodTest):
  File "<string>", line 19, in <module>
   at Python.Runtime.PythonException.ThrowLastAsClrException() in /Users/bruger/code/work/pythonnet/src/runtime/PythonException.cs:line 53
   at Python.Runtime.PythonException.ThrowIfIsNull(NewReference& ob) in /Users/bruger/code/work/pythonnet/src/runtime/PythonException.cs:line 455
   at Python.Runtime.PythonEngine.RunString(String code, BorrowedReference globals, BorrowedReference locals, RunFlagType flag) in /Users/bruger/code/work/pythonnet/src/runtime/PythonEngine.cs:line 625
   at Python.Runtime.PythonEngine.Exec(String code, PyDict globals, PyObject locals) in /Users/bruger/code/work/pythonnet/src/runtime/PythonEngine.cs:line 530
   at Python.PythonTestsRunner.PythonTestRunner.RunPythonTest(String testFile, String testName) in /Users/bruger/code/work/pythonnet/src/python_tests_runner/PythonTestRunner.cs:line 64
-----

One or more child tests had errors
  Exception doesn't have a stacktrace

Proposed Fix

I understand that it is probably a bit complicated to actually override a virtual generic method in python code and maybe it is not even necesary. So maybe the best solution is to just ignore virtual methods in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants