- Notifications
You must be signed in to change notification settings - Fork749
Description
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 aRawProxyEncoder
withPyObjectConversions.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 inissue 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.
importsysfrompathlibimportPathfromclr_loaderimportget_coreclrfrompythonnetimportset_runtimeapi_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)importclrimportSystemfromPython.RuntimeimportPyObjectConversionsfromPython.Runtime.CodecsimportRawProxyEncoderclassListAsRawEncoder(RawProxyEncoder):__namespace__='Dummy'defCanEncode(self,clr_type):returnclr_type.Name=='IList`1'andclr_type.Namespace=='System.Collections.Generic'if__name__=='__main__':list_encoder=ListAsRawEncoder()PyObjectConversions.RegisterEncoder(list_encoder)domain=System.AppDomain.CurrentDomainprint("Found",domain.GetAssemblies().Length,"assemblies in domain")
The output is:
Stack overflow
Commenting out the call to RegisterEncoder prevents the crash.