Skip to content

Registering RawProxyEncoder Causes Stack Overflow #1427

Closed
@richard-gemmell-42

Description

@richard-gemmell-42

Environment

  • Pythonnet version: installed from master branch on 30th March 2021
  • Python version: 3.9.2
  • Operating System: Windows 10
  • .NET Runtime: Microsoft.NETCore.App 5.0.4, Microsoft.WindowsDesktop.App 3.1.13

Summary

Registering a RawProxyEncoder with PyObjectConversions.RegisterEncoder() causes the script to fail with a stack overflow.

Thanks

First of all, I'd like to say thanks to everyone who's contributed to this project. It's incredibly useful for us to write one set of code to serve both .NET and Python developers. We've had very few problems with Pythonnet. Thanks very much.

Details

I ran into a problem with Pythonnet converting System.Collections.Generic.List into a native python list as described in issue 1153.

lostmsu recommended using a RawProxyEncoder to disable the automatic conversion. Unfortunately, if I register an encoder and then call a .NET method the system crashes with a stack overflow.

import sys
from pathlib import Path
from clr_loader import get_coreclr
from pythonnet import set_runtime
api_path = Path('..\\PythonStubs\\bin\\Debug\\netcoreapp3.1')
sys.path.append(str(api_path))
rt = get_coreclr(runtime_config=str(api_path / f'PythonStubs.runtimeconfig.json'))
set_runtime(rt)
import clr
import System
from Python.Runtime import PyObjectConversions
from Python.Runtime.Codecs import RawProxyEncoder


class ListAsRawEncoder(RawProxyEncoder):
    __namespace__ = 'Dummy'

    def CanEncode(self, clr_type):
        return clr_type.Name == 'IList`1' and clr_type.Namespace == 'System.Collections.Generic'


if __name__ == '__main__':
    list_encoder = ListAsRawEncoder()
    PyObjectConversions.RegisterEncoder(list_encoder)

    domain = System.AppDomain.CurrentDomain
    print("Found", domain.GetAssemblies().Length, "assemblies in domain")

The output is:

    Stack overflow

Commenting out the call to RegisterEncoder prevents the crash.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions