pythonnet icon indicating copy to clipboard operation
pythonnet copied to clipboard

It is illegal to reflect on the custom attributes of a Type loaded via ReflectionOnlyGetType

Open fizxmike opened this issue 4 years ago • 6 comments

Environment

  • Pythonnet version: 2.5.1
  • Python version: 3.8 (anaconda)
  • Operating System: Windows 10

Details

  • Describe what you were trying to get done.

    View C# object members in jupyter notebook (via dot tab). C# app crashes.

  • What commands did you run to trigger this issue?

    csharpObjectWithCustomAttributes.  # +tab in jupyter notebook should list members, but instead crashes

The c# library I am using is not publicly available. I assume it could be reproduced with some effort (creating an example C# library having an object with custom attributes).

However, I have found a related stack overflow issue (and solution): https://stackoverflow.com/a/42540881

And I see the potential "illegal use" in the code base here: https://github.com/pythonnet/pythonnet/search?q=GetCustomAttributes&unscoped_q=GetCustomAttributes

The fix might be to convert calls to GetCustomAttributes to what is mentioned in the stack overflow answer.

  • If there was a crash, please include the traceback here.

Traceback happens inside loaded DLL and application crashes. All I get is this: image TBH, that is one of the best error messages I've seen in a long time.

fizxmike avatar Aug 07 '20 18:08 fizxmike

@fizxmike is this a regression from a previous version?

lostmsu avatar Aug 08 '20 04:08 lostmsu

@lostmsu I'm not sure what you mean. Yes, I have had previous versions of PythonNet work with the C# library I'm using. However, the library has also been updated...

fizxmike avatar Aug 10 '20 18:08 fizxmike

@fizxmike did this code work on 2.4.0 and/or below, and you used it, or is this something you started doing just now?

lostmsu avatar Aug 10 '20 19:08 lostmsu

Basically, I think, Python.NET always assumes, that loaded assemblies are loaded in the full context, meaning reflection only is not currently supported.

BTW, it would be more helpful to have full stack trace.

lostmsu avatar Aug 10 '20 19:08 lostmsu

Ah, so must be library I'm using is loading things "reflection only" ... I don't get stack trace with library I'm using. App crashes and I get that error message. So It's not clear which call in your code is the culprit; there are only 8 or so calls to GetFunctionAttributes(...). But I should confirm that the stack overflow solution is indeed the fix to my issue.

fizxmike avatar Aug 11 '20 16:08 fizxmike

I can confirm this is not a regression. Something changed in the C# library I'm accessing I believe. Bump support for reflection only!

fizxmike avatar Aug 25 '20 18:08 fizxmike