openpaperwork / pyinsane

Python library to access and use image scanners (Linux/Windows/etc) (Sane/WIA) -- Moved to Gnome's Gitlab
https://gitlab.gnome.org/World/OpenPaperwork/pyinsane
63 stars 24 forks source link

Assertion error getting/setting scanner options #28

Closed peterdc closed 7 years ago

peterdc commented 7 years ago

Reading or setting any options for Canon P-215 using Python 2.7.10, MacOS Sierra yields this error:

'canon_dr:libusb:020:011' (CANON, P-215, scanner) Traceback (most recent call last): File "/Users/Birch/Desktop/untitled 5.py", line 12, in for opt in device.options.values(): File "/Users/Birch/Envs/sane/lib/python2.7/site-packages/pyinsane2/sane/abstract_proc.py", line 249, in _get_options options = remote_do("get_options", self.name) File "/Users/Birch/Envs/sane/lib/python2.7/site-packages/pyinsane2/sane/abstract_proc.py", line 70, in remote_do assert(len(result) == length) AssertionError

#!/usr/bin/env python

import pyinsane2

if __name__ == "__main__":
    pyinsane2.init()
    try:
        for device in pyinsane2.get_devices():
            print("%s" % (str(device)))

            for opt in device.options.values():
                print("  Option: %s" % (opt.name))
                print("    Title: %s" % (opt.title))
                print("    Desc: %s" % (opt.desc))
                print("    Type: %s" % (str(opt.val_type)))
                print("    Unit: %s" % (str(opt.unit)))
                print("    Size: %d" % (opt.size))
                print("    Capabilities: %s" % (str(opt.capabilities)))
                print("    Constraint type: %s" % (str(opt.constraint_type)))
                print("    Constraint: %s" % (str(opt.constraint)))
                try:
                    print("    Value: %s" % (str(opt.value)))
                except pyinsane2.PyinsaneException as exc:
                    # Some scanner allow changing a value, but not reading it.
                    # For instance Canon Lide 110 allow setting the resolution,
                    # but not reading it
                    print("    Value: Failed to get the value: %s" % str(exc))

            print("")
    finally:
        pyinsane2.exit()`
jflesch commented 7 years ago

Unfortunately, I don't have any Apple computer to test and fix this issue. And it works fine on Python2.7 + GNU/Linux.

My guess would be that that dedicated process has crashed while sending the reply. When running from a terminal, did you get any other error / exception ?

peterdc commented 7 years ago

I understand the issue of you not having the hardware. I set up a python3 env and using the following script.

These errors and are from the terminal (as was the above). I'm not familiar with python debugging of this nature, any suggestions/links for what to try would be appreciated.

import pyinsane2
pyinsane2.init()
dev = pyinsane2.get_devices()[0]
dev.options.values()

I was able to induce these errors:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Birch/Envs/sane3/lib/python3.5/site-packages/pyinsane2/sane/abstract_proc.py", line 249, in _get_options
    options = remote_do("get_options", self.name)
  File "/Users/Birch/Envs/sane3/lib/python3.5/site-packages/pyinsane2/sane/abstract_proc.py", line 69, in remote_do
    result = os.read(fifo_s2c, length)
OSError: [Errno 22] Invalid argument

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Birch/Envs/sane3/lib/python3.5/site-packages/pyinsane2/sane/abstract_proc.py", line 249, in _get_options
    options = remote_do("get_options", self.name)
  File "/Users/Birch/Envs/sane3/lib/python3.5/site-packages/pyinsane2/sane/abstract_proc.py", line 71, in remote_do
    result = pickle.loads(result)
KeyError: 33
jflesch commented 7 years ago

Unfortunately, all these errors are telling is that the communication with the dedicated process failed. Usually, on GNU/Linux, those errors are accompanied by other errors from the dedicated process.

Is there any crash indication in your dmesg ?

What you can try is to bypass the dedicated process and call the Sane library directly from your process. Just replace import pyinsane2 by import pyinsane2.sane.abstract as pyinsane2 and try again

peterdc commented 7 years ago

Unfortunately Mac OS Sierra had changed how loging occurs. I was unable to find anything potentially useful in dmesg or other systems logs I was able find. Open to other suggestions.

The workaround you suggest; however, works for me. I've not had problems with the SANE driver for this scanner in the past, so hopefully that will remain the case.

jflesch commented 7 years ago

Well .. it wasn't really intended as a workaround ... I was actually expecting it to crash, but with a more meaningful error message. Because if it works, it means the problem is strictly between the dedicated process and the program. Which means I'm probably making something in the code that is not allowed on MacOSX.

jflesch commented 7 years ago

5cdac134136a6c4d28b51665ce78e5c18233b99c