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

Crash when using nested classes with inheritance, which are instantiated from C# #1414

Closed
Assignees
filmor
Labels
Milestone
@koliyo

Description

@koliyo

Environment

  • Pythonnet version: 2.5.1
  • Python version: Python 3.9.2
  • Operating System: MacOS
  • .NET Runtime: 5.0.200

Details

  • Describe what you were trying to get done.

I am accessing a member of a nested C# class which inherits from the enclosing class.

When doing this, the Pythonnet runtime crashes!

Here is a minimal example:

namespacepythonnet_testing{publicclassBar{publicclassHej:Bar{}}publicclassFoo{publicBarBar;publicstaticFooCreate(){Foof=new();f.Bar=newBar.Hej();returnf;}}classProgram{staticvoidMain(string[]args){varpyCode=@"import pythonnet_testingf = pythonnet_testing.Foo.Create()print('Trying to access member of nested subclass')print(f'Bar: {f.Bar}')      ";PythonEngine.Initialize();using(Py.GIL()){PythonEngine.Exec(pyCode);}Console.WriteLine("EXIT");}}}

This seem to be specifically for nested classes, where the object instance is created from the C# environment, and the class has not been explicitly referenced/used from python.

I have found a way to prevent the crash, by first explicitly using the nested class in the python code.

Eg instead of this:

importpythonnet_testingf=pythonnet_testing.Foo.Create()print('Trying to access member of nested subclass')print(f'Bar:{f.Bar}')

I insert a reference to the nestedBar.Hej class

importpythonnet_testingf=pythonnet_testing.Foo.Create()print('Trying to access member of nested subclass')_=pythonnet_testing.Bar.Hej# Explicitly referencing the nested classprint(f'Bar:{f.Bar}')

If I do this, then the Pythonnet runtime no longer crashes. It is even enough to reference the enclosing baseclassBar

_=pythonnet_testing.Bar# Explicitly referencing the enclosing classprint(f'Bar:{f.Bar}')

When I do this, the python code executes properly:

Trying to access member of nested subclassBar: pythonnet_testing.Bar+Hej

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp