FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Error with Enum GCHandleType.Pinned · Issue #174 · pythonnet/pythonnet · GitHub

Repository navigation

Error with Enum GCHandleType.Pinned #174

Closed

Description

Hello,

I try to use the following snippet from this page to turn a byte[] into a numpy array:

from ctypes import string_at
clr.AddReference("System.Runtime.InteropServices")
from System.Runtime.InteropServices import GCHandle, GCHandleType

handler = GCHandle.Alloc(CSArray, GCHandleType.Pinned)
try:
    pointer = handler.AddrOfPinnedObject().ToInt32()
    dest = np.fromstring(string_at(pointer, len(CSArray))) 
finally:
    if handler.IsAllocated: handler.Free()

On this line:

handler = GCHandle.Alloc(CSArray, GCHandleType.Pinned)

I get the following error: TypeError: No method matches given arguments

Executing this code:

handler = GCHandle.Alloc(CSArray)

brings no error, so I assume that the issue must be from GCHandleType.Pinned.

I'm a bit troubled by:

print(type(GCHandleType.Pinned))
>>>int
print(GCHandleType.Pinned)
>>>3

Is it normal that enumare modeled by int?

Anyway, I can't get to have my code working...

Activity

  1. filmor commented on Mar 3, 2016

    Member

    Can you show exactly what CSArray is and how you create it? If I initialise CSArray = System.Array.CreateInstance(System.Byte, 1000) this works fine for me.

    And yes, C# enums are integers.

  2. aPere3 commented on Mar 3, 2016

    Author

    I have the following:

    print(type(CSArray))
    >>><class 'System.Byte[]'>

    Using your definition gives the same error on my computer.

    On my side, for test purpose, I create CSArray using a static C# method:

    public static byte[] Test_Image()
    {
        Image image = Image.FromFile("C:\\users\\alex\\logo.png");
        ImageConverter converter = new ImageConverter();
        byte[] byteArray = (byte[]) converter.ConvertTo(image, typeof(byte[]));
        return byteArray;
    }
  3. filmor commented on Mar 3, 2016

    Member

    What versions of Python, Python.NET and .NET are you using?

  4. aPere3 commented on Mar 3, 2016

    Author

    Sorry I forgot:
    Python 3.5
    Python.Net 2.1 cloned yesterday from here
    Windows 8.1 .NET4.5

  5. den-run-ai commented on Mar 3, 2016

    Contributor

    @aPere3 @filmor this problem is exactly the same as described here by @mahibra:

    #170

    the type int reported for GCHandleType.Pinned is 32-bit in c# because it is enum, but int in python 3 is pointing to int64.

    so try explicitly passing GCHandleType(GCHandleType.Pinned) - not sure if it works though.

  6. aPere3 commented on Mar 3, 2016

    Author
    from System import Int32
    handler = GCHandle.Alloc(CSArray, Int32(GCHandleType.Pinned))

    Gives me the same error as before.... What a pity!

  7. den-run-ai commented on Mar 3, 2016

    Contributor

    @aPere3 try this branch, although Travis CI and appveyor tests not done yet: #175

  8. den-run-ai commented on Mar 3, 2016

    Contributor

    @aPere3 all tests have been passed, so please try this updated version!

  9. aPere3 commented on Mar 3, 2016

    Author

    @denfromufa , I cloned an build from the master branch since you merged it a few hours ago...
    Anyway, no changes, still that same error....

  10. den-run-ai commented on Mar 6, 2016

    Contributor

    @aPere3 this used to work before, because overloading did not really check for types, which was giving wrong results like Math.Abs(5.3)=5 due to wrong selection of overloaded method.

    In your case what happens is that .NET enum GCHandleType.Pinned is converted to Python int before it is passed to GCHandle.Alloc() method, which is not what C# and IronPython do.

    So the temporary fix would be one of the following:

    • special case enums in overloading method selection to allow inter-operability with integer types.
    • implement context manager with clr.ignore_overloading_types, where method overloading logic is ignored only within this context
    • special case conversion logic to prevent conversion of c# enums to python integers in .ToPython() method
    • if only one overloaded signature matches by number of arguments, then ignore types and try to return results with warning

    But the bigger problem is that why .ToPython() has to be called on .NET types and deviate from IronPython?!

    Let me know what you think @tonyroberts @filmor ?

  11. aPere3 commented on Mar 7, 2016

    Author

    @denfromufa was your last post dedicated to me or developers of pythonNet? Because I'm quite new to python and I didn't get the details of your temporary fix recipe.

    Anyway, is a downgrading to another version of python likely to help me with this bug ?

  12. den-run-ai commented on Mar 7, 2016

    Contributor

    @aPere3 I fixed in this branch and tested it on my machine:

    #176

  13. den-run-ai commented on Mar 11, 2016

    Contributor

    @aPere3 did my patch work for you? if yes, then can you close this issue?

  14. aPere3 commented on Mar 11, 2016

    Author

    @denfromufa Sorry for the late reply...
    I cloned the last develop branch and still no changes, I have the same error...
    But I found that when I compile your code with:

    $ python setup.py bdist_wheel
    

    an error occurs. I didn't notice it because the files are built anyway but I have:

    C:\Users\alex\Downloads\pythonnet\packages\UnmanagedExports.1.2.6\tools\RGiesecke.DllExport.targets(42,5): err
    or : Microsoft.Build.Utilities.ToolLocationHelper could not find ildasm.exe.
    

    Is it important? Why are the files built anyway?
    Still the import of clr and my first import work without particular issues...

  15. den-run-ai commented on Mar 12, 2016

    Contributor

    @aPere3

    As you can see from pull request #176 it has not been merged yet, so you need to
    try my branch pointing at this pull request:

    https://github.com/denfromufa/pythonnet/tree/patch-8

    Not sure about ildasm.exe, which .net versions do you have installed?
    Anyway you can also try to build from visual studio, just select proper
    configuration and set python version symbol in Python.Runtime and clrmodule
    projects.

    On Friday, March 11, 2016, Alex, Péré <notifications@github.com
    javascript:_e(%7B%7D,'cvml','notifications@github.com');> wrote:

    @denfromufa https://github.com/denfromufa Sorry for the late reply...
    I cloned the last develop branch and still no changes, I have the same
    error...
    But I found that when I compile your code with:

    $ python setup.py bdist_wheel

    an error occurs. I didn't notice it because the files are built anyway but
    I have:

    C:\Users\alex\Downloads\pythonnet\packages\UnmanagedExports.1.2.6\tools\RGiesecke.DllExport.targets(42,5): err
    or : Microsoft.Build.Utilities.ToolLocationHelper could not find ildasm.exe.

    Is it important? Why are the files built anyway?
    Still the import of clr and my first import work without particular
    issues...

    —
    Reply to this email directly or view it on GitHub
    #174 (comment)
    .

  16. tonyroberts commented on Mar 15, 2016

    Contributor

    @denfromufa re your question about IronPython, PythonNet has nothing to do with IronPython - they are no way related. Any similarity is purely because they are both related to Python, not with each other. IronPython is a Python language implementation for the CLR, whereas PythonNet is a set of libraries built using the CPython API. Questions about why one has diverged from the other are irrelevant as neither is derived from the other.

  17. aPere3 commented on Mar 16, 2016

    Author

    @denfromufa Sorry for (very) late reply. I tried with your patch branch, and no error occurred on the line. This seems to have worked! Thank you very much!

    Anyway my code still do not work, but it is not related to this bug anymore, haha!

  18. den-run-ai commented on Mar 16, 2016

    Contributor

    @tonyroberts I thought ironpython compatibility is one of goals in pythonnet, because some people switch from ironpython due to obvious reasons, unless pythonnet breaks their existing ironpython code without migration path.

    @aPere3 glad to know that this works, open another issue if your new issue is related to pythonnet.

  19. aPere3 commented on Mar 17, 2016

    Author

    Thank you I close!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL