You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Possibly related to #86. When passing a numpy float64 to a dll library using PythonNet, the object type changes (not sure to what type).
I have tried to reproduce the error on the dll directly, by passing the same value with different types and it works as expected. Any ideas?
Just saw the C# examples for numpy. But I am going the other way around. I am trying to pass the numpy float64 object as an argument to the clr package. To give some context, the package is a customized dbapi wrapper.
import clr
import numpy as np
clr.AddReference('Custom.DB.API')
from Custom.DB.API import Query()
q = Query('select :num from dual')
q.parameter('num',np.float(1)) #works
q.parameter('num',np.long(1)) #works
q.parameter('num',np.double(1)) #breaks
But the same example using Win32Com instead works without any issues.
import clr
from System import Array, Object
import numpy as np
arr3=Array[Object]([np.float(1),np.long(1)])
list(arr3) # returns [1.0, 1]
But:
arr4=Array[Object]([np.float(1),np.double(1)])
returns:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-53-fee0ccf03f95> in <module>()
----> 1 arr4=Array[Object]([np.float(1),np.double(1)])
TypeError: value cannot be converted to Object
@denfromufa just search for the error in the code (it's in converter.cs) and it might be fairly clear what's going on just from looking at the code immediately before the error. Otherwise, set a breakpoint where the error is being set, attach the visual studio debugger to your python process and run the python code and work back from there.
In this case I imagine that the C# code is checking for an exact type match with 'float' which is failing for np.double. If that is the case, changing it to an 'isinstance' style check instead would fix it I would think.
Pardon my low level of understanding. I surveyed the code, and from what I think I understand, on runtime.cs you are creating c-type objects from the python library that correspond to Python Objects. Then the code compares incoming objects against this reference object to determine if its of the same type to then convert it?
The numpy double isn't identifying itself as a python standard object unless I explicitly cast it to change itself into one. I'm browsing through the numpy documentation to see what type of C object it identifies itself as.
dont know if this helps maybe.
to make numpy floats and integerswork with pythonnet, it is necessary to useisinstance()withnumber.Realandnumber.Integralrespectively. simply trying to compare withpython float and intdoes not work fornumpy` objects.
That sounds logical. I tried to look at how pywin32 handles the conversion, but their source isn't as navigation friendly. Have you tested to see if any of the other numpy objects break as well?
Activity
vmuriart commentedon Aug 19, 2015
Executing the same code using Win32COM does not produce an error either.
den-run-ai commentedon Aug 19, 2015
can you show minimal example and how you figured out that type is changed?
do examples on this page work for you?
https://github.com/pythonnet/pythonnet/tree/develop
vmuriart commentedon Aug 20, 2015
Just saw the
C#examples fornumpy. But I am going the other way around. I am trying to pass thenumpy float64object as an argument to the clr package. To give some context, the package is a customized dbapi wrapper.But the same example using
Win32Cominstead works without any issues.den-run-ai commentedon Aug 25, 2015
@vmuriart I could replicate your problem:
But:
returns:
den-run-ai commentedon Aug 25, 2015
@tonyroberts how can this issue be debugged? is this failing in the weird IL code?
tonyroberts commentedon Aug 25, 2015
@denfromufa just search for the error in the code (it's in converter.cs) and it might be fairly clear what's going on just from looking at the code immediately before the error. Otherwise, set a breakpoint where the error is being set, attach the visual studio debugger to your python process and run the python code and work back from there.
In this case I imagine that the C# code is checking for an exact type match with 'float' which is failing for np.double. If that is the case, changing it to an 'isinstance' style check instead would fix it I would think.
vmuriart commentedon Aug 30, 2015
Pardon my low level of understanding. I surveyed the code, and from what I think I understand, on
runtime.csyou are creating c-type objects from the python library that correspond to Python Objects. Then the code comparesincoming objectsagainst this reference object to determine if its of the same type to then convert it?The numpy double isn't identifying itself as a python standard object unless I explicitly cast it to change itself into one. I'm browsing through the numpy documentation to see what type of C object it identifies itself as.
dont know if this helps maybe.
den-run-ai commentedon Aug 31, 2015
to be more precise numpy double fails here:
call stack
locals
auto
den-run-ai commentedon Aug 31, 2015
to make
numpyfloats and integerswork with pythonnet, it is necessary to useisinstance()withnumber.Realandnumber.Integralrespectively. simply trying to compare withpython float and intdoes not work fornumpy` objects.vmuriart commentedon Sep 1, 2015
That sounds logical. I tried to look at how pywin32 handles the conversion, but their source isn't as navigation friendly. Have you tested to see if any of the other numpy objects break as well?
den-run-ai commentedon Sep 1, 2015
Yep, bunch of them break,
np.floatdid not break because it maps tofloatin Python.den-run-ai commentedon Sep 3, 2015
I did not find how pywin32 works, but this document shows mapping between
com typesandnumpy typesincomtypespackage:http://pythonhosted.org/comtypes/#numpy-interop
filmor commentedon Aug 6, 2021
This seems to be fixed.