mabl / PyPylon

An experimental python wrapper around the Basler Pylon 5 library
BSD 3-Clause "New" or "Revised" License
53 stars 34 forks source link

No bound checking in place for setting camera properties, causing crash #12

Closed duncanmcbryde closed 8 years ago

duncanmcbryde commented 8 years ago

Pylon Library version: 5.0.0.build_6150 OS: Windows 7 64 bit Python: 3.5 Camera: Basler acA640-750um

PyPylon does not check to see if the parameters passed to pylon are within acceptable bounds, causing Python to crash rather than throwing an exception.

Quick example, start by opening the camera:

import pypylon
available_cameras = pypylon.factory.find_devices()
cam = pypylon.factory.create_device(available_cameras[-1])
cam.open()

This works correctly:

# set exposure time to 100 microseconds works correctly,
cam.properties['ExposureTime'] = 100

However, if we set the exposure time to something beyond the range of values that can be provided with Pylon:

# Pylon only supports a minimum exposure time of 59 microseconds,
# Setting the time lower than this causes python to crash.
cam.properties['ExposureTime'] = 0

Python crashes rather than returning an exception. Crashes occur for setting other variables below or above other camera properties.

duncanmcbryde commented 8 years ago

Could this be due to the macros in hacks.h?

# EVIL HACK: We cannot dereference officially with the -> operator. So we use ugly macros...
cdef extern from 'hacks.h':
    bool ACCESS_CGrabResultPtr_GrabSucceeded(CGrabResultPtr ptr)
    String_t ACCESS_CGrabResultPtr_GetErrorDescription(CGrabResultPtr ptr)
    uint32_t ACCESS_CGrabResultPtr_GetErrorCode(CGrabResultPtr ptr)
mabl commented 8 years ago

Fixed in master. The hacks you found is caused by Basler requiring -> dereferencing of smart pointers, and Cython not having a syntax for this... At least last I looked.

duncanmcbryde commented 8 years ago

Thanks for the fixes, they work nicely.

cam.open()
cam.properties['ExposureTime']
>>>10000.0
cam.properties['ExposureTime'] = 100
>>> 100.0
cam.properties['ExposureTime'] = 100
cam.properties['ExposureTime'] = 58
Traceback (most recent call last):

  File "<ipython-input-23-5117c8c5935e>", line 1, in <module>
    cam.properties['ExposureTime'] = 58

  File "PyPylon\cython\factory.pyx", line 143, in pypylon.cython.factory._PropertyMap.__setitem__ (cython\factory.cpp:3588)

ValueError: Parameter value for ExposureTime not inside valid range [59.0, 1000000.0], was 58```