morefigs / pymba

Python wrapper for Allied Vision's Vimba C API
MIT License
105 stars 84 forks source link

Type error reading string features #57

Closed seahawk67 closed 5 years ago

seahawk67 commented 7 years ago

I am working on an embedded board (iMX6) with Python 3.4 and get a type error reading string features like that:

    with pymba.Vimba() as vimba:
        camera = vimba.getCamera(cameraSerialNumber)
        camera.openCamera()
        currentCameraModelName = camera.DeviceModelName

It looks like the call to ctypes create_string_buffer in vimbafeature.py is wrong:

    def _getStringFeature(self):
        """
        Get the value of a string feature.

        :returns: string -- value of the specified feature.
        """

        # create args
        bufferSize = 256
        valueToGet = create_string_buffer('\000' * bufferSize)
        sizeFilled = c_uint32()

        errorCode = VimbaDLL.featureStringGet(self._handle,
                                              self._name,
                                              valueToGet,
                                              bufferSize,
                                              byref(sizeFilled))
        if errorCode != 0:
            raise VimbaException(errorCode)
        return valueToGet.value.decode()

The function is called with a string which raises a TypeError in cytpes/__init__.py:

def create_string_buffer(init, size=None):
    """create_string_buffer(aBytes) -> character array
    create_string_buffer(anInteger) -> character array
    create_string_buffer(aString, anInteger) -> character array
    """
    if isinstance(init, bytes):
        if size is None:
            size = len(init)+1
        buftype = c_char * size
        buf = buftype()
        buf.value = init
        return buf
    elif isinstance(init, int):
        buftype = c_char * init
        buf = buftype()
        return buf
    raise TypeError(init)

I was able to resolve the issue by changing the call to create_string_buffer with a bytearray as argument: valueToGet = create_string_buffer(('\000' * bufferSize).encode())

But: as reading string features is quite common, I wonder why no one else had this issue before. Am I missing something?

morefigs commented 5 years ago

Fixed in v0.2, thanks.