Unable to get return value from with statement?

66 Views Asked by At

I am trying to access a network Scanner to scan documents through this code. This is the library and code I am using it

import sane
sane.init()

class saneScanner(object):
    def __init__(self, URI):
        self.URI = URI
    def __enter__(self):
        self.dev = sane.open(self.URI)
        return self.dev
    def __exit__(self, exception_type, exception_value, traceback):
        self.dev.close()
        print(exception_type, exception_value, traceback)

def t():
    with saneScanner("airscan:HP OfficeJet Pro 8020 series [973B68]") as k:
        return k.opt

print(t())

I am ending up with this error

Traceback (most recent call last):
  File "with.py", line 23, in <module>
    print(t())
  File "/usr/local/lib/python3.7/dist-packages/sane.py", line 86, in __repr__
    curValue = repr(getattr(self.scanDev, self.py_name))
  File "/usr/local/lib/python3.7/dist-packages/sane.py", line 232, in __getattr__
    return d['dev'].get_option(opt.index)
_sane.error: SaneDev object is closed

If remove the return Statement from function t(), there is no error thrown. Can you please let me know where I am going wrong

Thanks in advance

0

There are 0 best solutions below