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

Unable to Create Scanner Object using name Parameter in Python 3.x #15

Closed trashgordon-zz closed 10 years ago

trashgordon-zz commented 10 years ago

I am using Python 3.4.0 with the following code (I have not tested with other minor versions):

import pyinsane.abstract as pyinsane
device = pyinsane.Scanner(name='epkowa:net:192.168.1.8')

This works in Python 2.7.6. However, I get the following error in Python 3.4.0:

AttributeError: 'str' object has no attribute 'decode'

I believe the str.decode() method was dropped in Python 3, which is why the error is getting thrown due to this line in the Scanner constructor:

self.name = name.decode('utf-8')

Since a string in Python 3 is already Unicode, perhaps there could just be a check on the parameters to see if they are already Unicode before attempting to decode them?

trashgordon-zz commented 10 years ago

This is an ugly fix, but it works for now:

def __init__(self, name, vendor="Unknown", model="Unknown",
             dev_type="Unknown"):
    if sys.version_info >= (3,0):
        self.name = name
        self.vendor = vendor
        self.model = model
        self.dev_type = dev_type
    else:
        self.name = name.decode('utf-8')
        self.vendor = vendor.decode('utf-8')
        self.model = model.decode('utf-8')
        self.dev_type = dev_type.decode('utf-8')
    self.__options = None  # { "name" : ScannerOption }
jflesch commented 10 years ago

IMHO, I think a safer bet would be to use if hasattr(string, 'decode').

jflesch commented 10 years ago

Fixed by b1bb7e433210768f49f805a4d691e4b300ac6811

jflesch commented 10 years ago

I'll do a new release with this change in one day or two

trashgordon-zz commented 10 years ago

I agree, using hasattr() is much more elegant. Thanks for the fix!

jflesch commented 10 years ago

v1.3.6 released with this fix. (sorry for the delay)