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

Registering an encoder implemented in Python breaks the constructing of other Python-implemented encoders #2368

Open
TrianecT-Simon opened this issue May 2, 2024 · 0 comments

Comments

@TrianecT-Simon
Copy link

Environment

  • Pythonnet version: 3.0.3
  • Python version: 3.12.2
  • Operating System: Windows 11 (23H2)
  • .NET Runtime: .NET Framework 4.8.9232.0 (also .NET 8.0.4, but I am unable to provide a minimal example)

Details

  • Describe what you were trying to get done.

I am working on a Python project that interacts with a .NET library and am trying to write an IPyObjectEncoder implementation in Python to make dealing with certain types a bit easier.
However I am running into an issue, as for some reason registering a Python-implemented encoder will cause the next Python-implemented encoder to throw an exception when constructing it.

Placing or moving around seemingly random things sometimes 'fixes' the crash. I've had cases where even removing any of the 3 imports in from System.Collections.Generic import IList, List, Dictionary (even though none of them were used in the Python code) would suddenly work around the crash. But I am unable to reproduce this again.

import clr
from Python.Runtime import PyObjectConversions, IPyObjectEncoder
from Python.Runtime.Codecs import RawProxyEncoder
import System

class DummyEncoder1(IPyObjectEncoder):
    __namespace__ = "CustomPyCodecs"

    def CanEncode(self, clr_type):
        return False

    def TryEncode(self, clr_object):
        return None

class DummyEncoder2(IPyObjectEncoder):
    __namespace__ = "CustomPyCodecs"

    def CanEncode(self, clr_type):
        return False

    def TryEncode(self, clr_object):
        return None

PyObjectConversions.RegisterEncoder(DummyEncoder1())
# System.Activator.CreateInstance(System.Int32)  # Uncomment me to somehow fix DummyEncoder2()
encoder_2 = DummyEncoder2()
  • If there was a crash, please include the traceback here.
TypeError: Invoked a non-static method with an invalid instance

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "F:\repos\pyclr\minimal_issue_framework.py", line 25, in <module>
    encoder_2 = DummyEncoder2()
                ^^^^^^^^^^^^^^^
TypeError: No method matches given arguments for DummyEncoder2..ctor: ()
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

1 participant