Skip to content

“TypeError: Cannot get managed object” due to getting wrong type info from .NET structs #488

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

Closed
DfM7 opened this issue Jun 12, 2017 · 8 comments

Comments

@DfM7
Copy link

DfM7 commented Jun 12, 2017

Environment

  • Pythonnet version: 2.3.0
  • Python version: Python 3.6.1 64-bit
  • Operating System: Windows 10 1703 64-bit

Details

pythonnet fails to get the right type from the struct.

C# code:

namespace PyNetFails
{
    public struct A
    {
        public int X;
        public int Y;
        public uint V;
    }
}

Then compile it as class library named PyNetFails and run the following Python code:

import clr
clr.AddReference('PyNetFails.dll')
from PyNetFails import A
a = A()
print(type(a.V))

It produces: <class 'int'>, although the type should be System.UInt32.
Then try to run afterwards the following code:

from System import UInt32
v = UInt32(42)
print(v >= a.V)
  • Traceback:
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    print(v >= a.V)
TypeError: Cannot get managed object
@den-run-ai den-run-ai added the bug label Jun 14, 2017
@Konstantin-Posudevskiy
Copy link
Contributor

@DfM7

Firstly,

Lets discuss the next snippet:

from PyNetFails import A
a = A()
print(type(a.V))

It really produces <class 'int'>, as it must be. But not the System.UInt32.
As said from pythonnet `documentation:

Type conversion under Python for .NET is fairly straightforward - most elemental Python types (string, int, long, etc.) convert automatically to compatible managed equivalents (String, Int32, etc.) and vice-versa.

You can read more information on official pythonnet documentatition. Take a look at paragraph Type conversion.

Secondly,

Lets take into account the first notice (result of a.V is converted from System.UInt32 directly to the Python type <class 'int'>) and take a look at the next Python code:

from System import UInt32
v = UInt32(42)
print(v >= a.V)

In the 3-rd line print(v >= a.V), as result of a.V is converted to the Python type, we are trying to compare CLR object v of type System.UInt32 with the Python object, which returned in a result of a.V. As a result of this comparision, we receive TypeError: Cannot get managed object.

So, this action fails, as pythonnet cannot compare Python objects with CLR and vice versa.
In this case, you can fix it, for example, by comparing CLR objects (requires add explicit cast to CLR object):

from System import UInt32
v = UInt32(42)
print(v >= UInt32(a.V))

or fix problem, by comparing Python objects:

v = 42
print(v >= a.V)

@Konstantin-Posudevskiy
Copy link
Contributor

@denfromufa I am convinced it is not a bug. It is a documented behaviour.

@den-run-ai
Copy link
Contributor

@Konstantin-Posudevskiy yes, that's what I originally suggested to @DfM7. But I'm thinking that this is annoyance and unexpected behavior.

https://stackoverflow.com/questions/44491159

@filmor
Copy link
Member

filmor commented Jun 16, 2017

IMHO, the first step doesn't qualify as a bug, like @Konstantin-Posudevskiy said, it's documented (and sensible) behaviour. Regarding the comparison, I think we could add special cases for the conversion operators that detect mapped Python types and convert them back to their .NET counterpart.

@Konstantin-Posudevskiy
Copy link
Contributor

Seems to be related with #94

@DfM7
Copy link
Author

DfM7 commented Jun 16, 2017

@Konstantin-Posudevskiy
Thank you for your clarification.

I had read all of the paragraphs in the official documentation before I've posted the question on SO.
I've just interpreted the "Type Conversion" paragraph in another way, as I got that, for example, System.Int32 and int are logically equivalent, but not System.UInt32 and int.

@den-run-ai
Copy link
Contributor

originally reported here: https://stackoverflow.com/questions/44491159

@lostmsu
Copy link
Member

lostmsu commented Sep 23, 2021

As discussed above, this is not a bug.

@lostmsu lostmsu closed this as completed Sep 23, 2021
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

5 participants