python-pillow / Sane

Python interface to the SANE scanner and frame grabber
Other
55 stars 19 forks source link

Segfault if `progress` function passed into `SaneDev.snap()` has one argument #81

Open mhirsch opened 1 year ago

mhirsch commented 1 year ago

I couldn't find anything in the docs about how to use the progress callback function available in SaneDev.snap(). My first try was to pass in a function with only one argument, and this caused a segmentation fault in the python interpreter. (For anyone looking, you need to pass in a function that accepts two arguments. The first will be the number of lines scanned so far, and the second will be the total number of lines).

I'm not super familiar with CPython but it looks like passing two arguments into a python callable that only accepts one causes a segfault. I couldn't find a way in the docs to check the number of arguments in CPython (that must exist right?) so I don't know how to fix it in _sane.c. However one simple fix within sane.py would be to check the type of the progress object passed in to snap(), and wrap it in a function that accepts two arguments.

mhirsch commented 1 year ago

Repro:

import sane
def p(a):
  print(a)

sane.init()
devices = sane.get_devices()
dev = sane.open(devices[0][0])
dev.start()
dev.snap(progress=p)