8000 Copy .NET to numpy array: python 3 breaks constructor IntPtr.__overloads__[int](np_arr.__array_interface__['data'][0]) · Issue #1116 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content
8000

Copy .NET to numpy array: python 3 breaks constructor IntPtr.__overloads__[int](np_arr.__array_interface__['data'][0]) #1116

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
greg814 opened this issue Apr 16, 2020 · 7 comments
Milestone

Comments

@greg814
Copy link
greg814 commented Apr 16, 2020

Environment

Pythonnet version: 2.4.0 (in both python versions)

Python version:

  • code working in Python 2.7.17 (v2.7.17:c2f86d86e6, Oct 19 2019, 21:01:17) [MSC v.1500 64 bit (AMD64)] on win32

  • code not working in Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32

  • Operating System: Windows 10 Professional 64-bit

Details

Trying to copy a .NET array to a numpy array using Marshal as in the example on the mailing list:

import clr
from System import Array, Double, IntPtr, Random
from System.Runtime.InteropServices import Marshal
import numpy as np

r = Random()
src = Array.CreateInstance(Double, 10000000)
for i in xrange(len(src)): src[i] = r.NextDouble()

from System.Runtime.InteropServices import Marshal
dest = np.empty(len(src))
dest_ptr = IntPtr.__overloads__[int](dest.__array_interface__['data'][0])
Marshal.Copy(src, 0, dest_ptr, len(src))

was working fine in python 2 but in 3 results in

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    dest_ptr = IntPtr.__overloads__[int](dest.__array_interface__['data'][0])
TypeError: no constructor matches given arguments

TODO
How can the changed python 3 int be used in this constructor?

lostmsu added a commit to losttech/pythonnet that referenced this issue May 14, 2020
…tails

Similar to pythonnet#900, but for constructors (reuses the same code)

Related issues: pythonnet#811, pythonnet#265, pythonnet#1116
filmor pushed a commit that referenced this issue May 14, 2020
…tails (#1143)

Similar to #900, but for constructors (reuses the same code)

Related issues: #811, #265, #1116
AlexCatarino pushed a commit to QuantConnect/pythonnet that referenced this issue Jun 29, 2020
@filmor filmor added this to the 3.0.0 milestone Jul 8, 2022
@filmor
Copy link
Member
filmor commented Jul 8, 2022

@lostmsu I'll investigate this, there seems to be no way to initialise a System.IntPtr from Python.NET right now.

@filmor filmor mentioned this issue Jul 8, 2022
23 tasks
@greg814
Copy link
Author
greg814 commented Jul 8, 2022

Did you see this parsing workaround on stackoverflow? Funny you would comment here 2 hrs after the so post...

@filmor filmor mentioned this issue Jul 8, 2022
5 tasks
@filmor
Copy link
Member
filmor commented Jul 8, 2022

It's related, @jhe-pf commented on gitter and I started to look into it :)

I have identified the problem, I just need to find a fix.

@lostmsu
Copy link
Member
lostmsu commented Jul 8, 2022

@filmor as mentioned on StackOverflow, __overloads__[Int64] should work. The overloads fail with int because int is not a .NET type.

@filmor
Copy link
Member
filmor commented Jul 8, 2022

No, check the linked PR. The issue is that primitive types are initialised differently, which in our case is implemented as a conversion, but not for the ptr types.

@lostmsu
Copy link
Member
lostmsu commented Jul 11, 2022

I have pythonnet==3.0.0a2 and in it IntPtr.__overloads__[Int64](42).ToInt64() prints 42.

EDIT. Ah, just updated to rc2 and there I indeed get "TypeError: No match found for given type params"

@filmor
Copy link
Member
filmor commented Aug 12, 2022

Fixed in #1861.

@filmor filmor closed this as completed Aug 12, 2022
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

3 participants
0