Using numpy arrays where a sequence is expected

gwvdlindengwvdlinden Gwendolyn van der LindenOrganization: SC SolutionsProject: Bridge health monitoring systemMember
I am using IcePy 3.3.1, and noticed that I can pass numpy arrays where a sequence is expected, which is great. However, one can unwittingly get into trouble if the item type doesn't match.

I have a class function that accepts a sequence of floats (i.e. 32-bit), I can pass a numpy array:
data = numpy.array([...], dtype=numpy.float32)
myProxy.myFunction(data)
which works OK.

If I do this:
data = numpy.array([...], dtype=numpy.float64)
myProxy.myFunction(data)
it seems to cast the data to an array of float32s with twice as many elements, without reporting an error.

Shouldn't this generate an exception? Of course having Ice handle the data type conversion would be even better.

Comments

  • mesmes CaliforniaMark SpruiellOrganization: ZeroC, Inc.Project: Ice DeveloperAdministrators, ZeroC Staff ZeroC Staff
    Welcome to the forum!

    The Ice extension for Python doesn't know anything about numpy directly, but rather accesses the memory associated with a numpy array via Python's generic "buffer" API.

    Here's a quote from the manual:
    Note that the Ice run time has no way of knowing what type of elements a buffer contains, therefore it is the application’s responsibility to ensure that a buffer is compatible with the declared sequence type.
    Regards,
    Mark
  • gwvdlindengwvdlinden Gwendolyn van der LindenOrganization: SC SolutionsProject: Bridge health monitoring systemMember
    Thanks! That makes sense.

    I guess it's a little too early to consider using the new Python buffer object interface (back ported to 2.6), which gives more information about the object than the (now deprecated) buffer protocol, and would allow better error checking and type handling.
  • mesmes CaliforniaMark SpruiellOrganization: ZeroC, Inc.Project: Ice DeveloperAdministrators, ZeroC Staff ZeroC Staff
    gwvdlinden wrote: »
    I guess it's a little too early to consider using the new Python buffer object interface (back ported to 2.6), which gives more information about the object than the (now deprecated) buffer protocol, and would allow better error checking and type handling.
    It's tempting to use the shiny new APIs, but we don't always have that luxury. :)

    Regards,
    Mark
Sign In or Register to comment.