python-pillow / Sane

Python interface to the SANE scanner and frame grabber

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

mhirsch opened this issue · comments

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.

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)