commit c06e7e1940fd089749cebb0d3082ff5fae57c10b Author: Mauro Carvalho Chehab Date: Tue Aug 7 15:02:11 2018 -0300 python: Make it work with modern versions of python 2 Some of the examples and test code don't work with python 2.7 and PIL. Modernize them by running 2to3 and fixing PIL manually. Signed-off-by: Mauro Carvalho Chehab diff --git a/configure.ac b/configure.ac index 141a3c2ab9f3..c808dab7e7bd 100644 --- a/configure.ac +++ b/configure.ac @@ -444,7 +444,7 @@ AC_ARG_VAR([PYGTK_CODEGEN], [full path to pygtk-codegen program]) AC_ARG_VAR([PYGTK_DEFS], [directory where PyGTK definitions may be found]) AS_IF([test "x$with_python" != "xno"], - [AM_PATH_PYTHON(2.3.5) + [AM_PATH_PYTHON(2.7.0) AS_IF([test "x$PYTHON_CFLAGS" != "x"], [], [test "x$PYTHON_CONFIG" != "x" && test -x "$PYTHON_CONFIG"], diff --git a/python/examples/processor.py b/python/examples/processor.py index 52996d16cf0d..b8f20f053dc6 100644 --- a/python/examples/processor.py +++ b/python/examples/processor.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python from sys import argv import zbar @@ -19,7 +19,7 @@ def my_handler(proc, image, closure): # extract results for symbol in image.symbols: # do something useful with results - print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data + print('decoded', symbol.type, 'symbol', '"%s"' % symbol.data) proc.set_data_handler(my_handler) @@ -31,5 +31,5 @@ proc.active = True try: # keep scanning until user provides key/mouse input proc.user_wait() -except zbar.WindowClosed, e: +except zbar.WindowClosed as e: pass diff --git a/python/examples/read_one.py b/python/examples/read_one.py index 426b9994155e..7a18c9998c0f 100644 --- a/python/examples/read_one.py +++ b/python/examples/read_one.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python from sys import argv import zbar @@ -26,4 +26,4 @@ proc.visible = False # extract results for symbol in proc.results: # do something useful with results - print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data + print('decoded', symbol.type, 'symbol', '"%s"' % symbol.data) diff --git a/python/examples/scan_image.py b/python/examples/scan_image.py index 2a80759da7b5..e26cb427064b 100644 --- a/python/examples/scan_image.py +++ b/python/examples/scan_image.py @@ -1,7 +1,7 @@ -#!/usr/bin/python +#!/usr/bin/env python from sys import argv import zbar -import Image +from PIL import Image if len(argv) < 2: exit(1) @@ -14,7 +14,7 @@ scanner.parse_config('enable') # obtain image data pil = Image.open(argv[1]).convert('L') width, height = pil.size -raw = pil.tostring() +raw = pil.tobytes() # wrap image data image = zbar.Image(width, height, 'Y800', raw) @@ -25,7 +25,7 @@ scanner.scan(image) # extract results for symbol in image: # do something useful with results - print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data + print('decoded', symbol.type, 'symbol', '"%s"' % symbol.data) # clean up del(image) diff --git a/python/test/test_zbar.py b/python/test/test_zbar.py index dbe180324378..5b63de47adb3 100755 --- a/python/test/test_zbar.py +++ b/python/test/test_zbar.py @@ -1,18 +1,18 @@ -#!/usr/bin/python +#!/usr/bin/env python import sys, os, re import unittest as ut import zbar # FIXME this needs to be conditional # would be even better to auto-select PIL or ImageMagick (or...) -import Image +from PIL import Image data = None size = (0, 0) def load_image(): image = Image.open(os.path.join(sys.path[0], "barcode.png")).convert('L') - return(image.tostring(), image.size) + return(image.tobytes(), image.size) # FIXME this could be integrated w/fixture creation (data, size) = load_image() @@ -32,17 +32,17 @@ is_identifier = re.compile(r'^[A-Z][A-Z_0-9]*$') class TestZBarFunctions(ut.TestCase): def test_version(self): ver = zbar.version() - self.assert_(isinstance(ver, tuple)) + self.assertTrue(isinstance(ver, tuple)) self.assertEqual(len(ver), 2) for v in ver: - self.assert_(isinstance(v, int)) + self.assertTrue(isinstance(v, int)) def test_verbosity(self): zbar.increase_verbosity() zbar.set_verbosity(16) def test_exceptions(self): - self.assert_(isinstance(zbar.Exception, type)) + self.assertTrue(isinstance(zbar.Exception, type)) for err in (zbar.InternalError, zbar.UnsupportedError, zbar.InvalidRequestError, @@ -53,7 +53,7 @@ class TestZBarFunctions(ut.TestCase): zbar.X11ProtocolError, zbar.WindowClosed, zbar.WinAPIError): - self.assert_(issubclass(err, zbar.Exception)) + self.assertTrue(issubclass(err, zbar.Exception)) def test_configs(self): for cfg in (zbar.Config.ENABLE, @@ -66,16 +66,16 @@ class TestZBarFunctions(ut.TestCase): zbar.Config.POSITION, zbar.Config.X_DENSITY, zbar.Config.Y_DENSITY): - self.assert_(isinstance(cfg, zbar.EnumItem)) - self.assert_(int(cfg) >= 0) - self.assert_(is_identifier.match(str(cfg))) + self.assertTrue(isinstance(cfg, zbar.EnumItem)) + self.assertTrue(int(cfg) >= 0) + self.assertTrue(is_identifier.match(str(cfg))) def test_modifiers(self): for mod in (zbar.Modifier.GS1, zbar.Modifier.AIM): - self.assert_(isinstance(mod, zbar.EnumItem)) - self.assert_(int(mod) >= 0) - self.assert_(is_identifier.match(str(mod))) + self.assertTrue(isinstance(mod, zbar.EnumItem)) + self.assertTrue(int(mod) >= 0) + self.assertTrue(is_identifier.match(str(mod))) def test_symbologies(self): for sym in (zbar.Symbol.NONE, @@ -95,9 +95,9 @@ class TestZBarFunctions(ut.TestCase): zbar.Symbol.QRCODE, zbar.Symbol.CODE93, zbar.Symbol.CODE128): - self.assert_(isinstance(sym, zbar.EnumItem)) - self.assert_(int(sym) >= 0) - self.assert_(is_identifier.match(str(sym))) + self.assertTrue(isinstance(sym, zbar.EnumItem)) + self.assertTrue(int(sym) >= 0) + self.assertTrue(is_identifier.match(str(sym))) def test_orientations(self): for orient in (zbar.Orient.UNKNOWN, @@ -105,9 +105,9 @@ class TestZBarFunctions(ut.TestCase): zbar.Orient.RIGHT, zbar.Orient.DOWN, zbar.Orient.LEFT): - self.assert_(isinstance(orient, zbar.EnumItem)) - self.assert_(-1 <= int(orient) <= 3) - self.assert_(is_identifier.match(str(orient))) + self.assertTrue(isinstance(orient, zbar.EnumItem)) + self.assertTrue(-1 <= int(orient) <= 3) + self.assertTrue(is_identifier.match(str(orient))) class TestScanner(ut.TestCase): def setUp(self): @@ -117,10 +117,10 @@ class TestScanner(ut.TestCase): del(self.scn) def test_type(self): - self.assert_(isinstance(self.scn, zbar.Scanner)) - self.assert_(callable(self.scn.reset)) - self.assert_(callable(self.scn.new_scan)) - self.assert_(callable(self.scn.scan_y)) + self.assertTrue(isinstance(self.scn, zbar.Scanner)) + self.assertTrue(callable(self.scn.reset)) + self.assertTrue(callable(self.scn.new_scan)) + self.assertTrue(callable(self.scn.scan_y)) def set_color(color): self.scn.color = color @@ -140,13 +140,13 @@ class TestDecoder(ut.TestCase): del(self.dcode) def test_type(self): - self.assert_(isinstance(self.dcode, zbar.Decoder)) - self.assert_(callable(self.dcode.set_config)) - self.assert_(callable(self.dcode.parse_config)) - self.assert_(callable(self.dcode.reset)) - self.assert_(callable(self.dcode.new_scan)) - self.assert_(callable(self.dcode.set_handler)) - self.assert_(callable(self.dcode.decode_width)) + self.assertTrue(isinstance(self.dcode, zbar.Decoder)) + self.assertTrue(callable(self.dcode.set_config)) + self.assertTrue(callable(self.dcode.parse_config)) + self.assertTrue(callable(self.dcode.reset)) + self.assertTrue(callable(self.dcode.new_scan)) + self.assertTrue(callable(self.dcode.set_handler)) + self.assertTrue(callable(self.dcode.decode_width)) def set_type(typ): self.dcode.type = typ @@ -165,25 +165,25 @@ class TestDecoder(ut.TestCase): def test_width(self): sym = self.dcode.decode_width(5) - self.assert_(sym is zbar.Symbol.NONE) - self.assert_(not sym) + self.assertTrue(sym is zbar.Symbol.NONE) + self.assertTrue(not sym) self.assertEqual(str(sym), 'NONE') typ = self.dcode.type - self.assert_(sym is typ) + self.assertTrue(sym is typ) def test_reset(self): - self.assert_(self.dcode.color is zbar.SPACE) + self.assertTrue(self.dcode.color is zbar.SPACE) sym = self.dcode.decode_width(1) - self.assert_(self.dcode.color is zbar.BAR) + self.assertTrue(self.dcode.color is zbar.BAR) self.dcode.reset() - self.assert_(self.dcode.color is zbar.SPACE) + self.assertTrue(self.dcode.color is zbar.SPACE) self.assertEqual(self.dcode.direction, 0) def test_decode(self): inline_sym = [ -1 ] def handler(dcode, closure): - self.assert_(dcode is self.dcode) + self.assertTrue(dcode is self.dcode) if dcode.type > zbar.Symbol.PARTIAL: inline_sym[0] = dcode.type closure[0] += 1 @@ -196,19 +196,19 @@ class TestDecoder(ut.TestCase): if width == ' ': continue sym = self.dcode.decode_width(int(width)) if i < len(encoded_widths) - 1: - self.assert_(sym is zbar.Symbol.NONE or + self.assertTrue(sym is zbar.Symbol.NONE or sym is zbar.Symbol.PARTIAL) else: - self.assert_(sym is zbar.Symbol.EAN13) + self.assertTrue(sym is zbar.Symbol.EAN13) self.assertEqual(self.dcode.configs, set((zbar.Config.ENABLE, zbar.Config.EMIT_CHECK))) self.assertEqual(self.dcode.modifiers, set()) self.assertEqual(self.dcode.data, '6268964977804') - self.assert_(self.dcode.color is zbar.BAR) + self.assertTrue(self.dcode.color is zbar.BAR) self.assertEqual(self.dcode.direction, 1) - self.assert_(sym is zbar.Symbol.EAN13) - self.assert_(inline_sym[0] is zbar.Symbol.EAN13) + self.assertTrue(sym is zbar.Symbol.EAN13) + self.assertTrue(inline_sym[0] is zbar.Symbol.EAN13) self.assertEqual(explicit_closure, [ 2 ]) def test_databar(self): @@ -217,15 +217,15 @@ class TestDecoder(ut.TestCase): if width == ' ': continue sym = self.dcode.decode_width(int(width)) if i < len(databar_widths) - 1: - self.assert_(sym is zbar.Symbol.NONE or + self.assertTrue(sym is zbar.Symbol.NONE or sym is zbar.Symbol.PARTIAL) - self.assert_(sym is zbar.Symbol.DATABAR) + self.assertTrue(sym is zbar.Symbol.DATABAR) self.assertEqual(self.dcode.get_configs(zbar.Symbol.EAN13), set((zbar.Config.ENABLE, zbar.Config.EMIT_CHECK))) self.assertEqual(self.dcode.modifiers, set((zbar.Modifier.GS1,))) self.assertEqual(self.dcode.data, '0124012345678905') - self.assert_(self.dcode.color is zbar.BAR) + self.assertTrue(self.dcode.color is zbar.BAR) self.assertEqual(self.dcode.direction, 1) # FIXME test exception during callback @@ -238,8 +238,8 @@ class TestImage(ut.TestCase): del(self.image) def test_type(self): - self.assert_(isinstance(self.image, zbar.Image)) - self.assert_(callable(self.image.convert)) + self.assertTrue(isinstance(self.image, zbar.Image)) + self.assertTrue(callable(self.image.convert)) def test_new(self): self.assertEqual(self.image.format, 'Y800') @@ -247,7 +247,7 @@ class TestImage(ut.TestCase): self.assertEqual(self.image.crop, (0, 0, 123, 456)) image = zbar.Image() - self.assert_(isinstance(image, zbar.Image)) + self.assertTrue(isinstance(image, zbar.Image)) self.assertEqual(image.format, '\0\0\0\0') self.assertEqual(image.size, (0, 0)) self.assertEqual(image.crop, (0, 0, 0, 0)) @@ -308,11 +308,11 @@ class TestImageScanner(ut.TestCase): del(self.scn) def test_type(self): - self.assert_(isinstance(self.scn, zbar.ImageScanner)) - self.assert_(callable(self.scn.set_config)) - self.assert_(callable(self.scn.parse_config)) - self.assert_(callable(self.scn.enable_cache)) - self.assert_(callable(self.scn.scan)) + self.assertTrue(isinstance(self.scn, zbar.ImageScanner)) + self.assertTrue(callable(self.scn.set_config)) + self.assertTrue(callable(self.scn.parse_config)) + self.assertTrue(callable(self.scn.enable_cache)) + self.assertTrue(callable(self.scn.scan)) def test_set_config(self): self.scn.set_config() @@ -339,63 +339,63 @@ class TestImageScan(ut.TestCase): self.assertEqual(n, 1) syms = self.image.symbols - self.assert_(isinstance(syms, zbar.SymbolSet)) + self.assertTrue(isinstance(syms, zbar.SymbolSet)) self.assertEqual(len(syms), 1) i = iter(self.image) j = iter(syms) - self.assert_(isinstance(i, zbar.SymbolIter)) - self.assert_(isinstance(j, zbar.SymbolIter)) - symi = i.next() - symj = j.next() - self.assertRaises(StopIteration, i.next) - self.assertRaises(StopIteration, j.next) + self.assertTrue(isinstance(i, zbar.SymbolIter)) + self.assertTrue(isinstance(j, zbar.SymbolIter)) + symi = next(i) + symj = next(j) + self.assertRaises(StopIteration, i.__next__) + self.assertRaises(StopIteration, j.__next__) # this is the only way to obtain a Symbol, # so test Symbol here for sym in (symi, symj): - self.assert_(isinstance(sym, zbar.Symbol)) - self.assert_(sym.type is zbar.Symbol.EAN13) - self.assert_(sym.type is sym.EAN13) + self.assertTrue(isinstance(sym, zbar.Symbol)) + self.assertTrue(sym.type is zbar.Symbol.EAN13) + self.assertTrue(sym.type is sym.EAN13) self.assertEqual(str(sym.type), 'EAN13') cfgs = sym.configs - self.assert_(isinstance(cfgs, set)) + self.assertTrue(isinstance(cfgs, set)) for cfg in cfgs: - self.assert_(isinstance(cfg, zbar.EnumItem)) + self.assertTrue(isinstance(cfg, zbar.EnumItem)) self.assertEqual(cfgs, set((zbar.Config.ENABLE, zbar.Config.EMIT_CHECK))) mods = sym.modifiers - self.assert_(isinstance(mods, set)) + self.assertTrue(isinstance(mods, set)) for mod in mods: - self.assert_(isinstance(mod, zbar.EnumItem)) + self.assertTrue(isinstance(mod, zbar.EnumItem)) self.assertEqual(mods, set()) - self.assert_(sym.quality > 0) + self.assertTrue(sym.quality > 0) self.assertEqual(sym.count, 0) # FIXME put a nice QR S-A in here comps = sym.components - self.assert_(isinstance(comps, zbar.SymbolSet)) + self.assertTrue(isinstance(comps, zbar.SymbolSet)) self.assertEqual(len(comps), 0) - self.assert_(not comps) - self.assert_(tuple(comps) is ()) + self.assertTrue(not comps) + self.assertTrue(tuple(comps) is ()) data = sym.data self.assertEqual(data, '9876543210128') loc = sym.location - self.assert_(len(loc) >= 4) # FIXME - self.assert_(isinstance(loc, tuple)) + self.assertTrue(len(loc) >= 4) # FIXME + self.assertTrue(isinstance(loc, tuple)) for pt in loc: - self.assert_(isinstance(pt, tuple)) + self.assertTrue(isinstance(pt, tuple)) self.assertEqual(len(pt), 2) # FIXME test values (API currently in flux) - self.assert_(sym.orientation is zbar.Orient.UP) - self.assert_(data is sym.data) - self.assert_(loc is sym.location) + self.assertTrue(sym.orientation is zbar.Orient.UP) + self.assertTrue(data is sym.data) + self.assertTrue(loc is sym.location) def set_symbols(syms): self.image.symbols = syms @@ -430,14 +430,14 @@ class TestProcessor(ut.TestCase): del(self.proc) def test_type(self): - self.assert_(isinstance(self.proc, zbar.Processor)) - self.assert_(callable(self.proc.init)) - self.assert_(callable(self.proc.set_config)) - self.assert_(callable(self.proc.parse_config)) - self.assert_(callable(self.proc.set_data_handler)) - self.assert_(callable(self.proc.user_wait)) - self.assert_(callable(self.proc.process_one)) - self.assert_(callable(self.proc.process_image)) + self.assertTrue(isinstance(self.proc, zbar.Processor)) + self.assertTrue(callable(self.proc.init)) + self.assertTrue(callable(self.proc.set_config)) + self.assertTrue(callable(self.proc.parse_config)) + self.assertTrue(callable(self.proc.set_data_handler)) + self.assertTrue(callable(self.proc.user_wait)) + self.assertTrue(callable(self.proc.process_one)) + self.assertTrue(callable(self.proc.process_image)) def test_set_config(self): self.proc.set_config() @@ -461,31 +461,31 @@ class TestProcessor(ut.TestCase): def test_processing(self): self.proc.init(VIDEO_DEVICE) - self.assert_(self.proc.visible is False) + self.assertTrue(self.proc.visible is False) self.proc.visible = 1 - self.assert_(self.proc.visible is True) + self.assertTrue(self.proc.visible is True) self.assertEqual(self.proc.user_wait(1.1), 0) self.image = zbar.Image(size[0], size[1], 'Y800', data) count = [ 0 ] def data_handler(proc, image, closure): - self.assert_(proc is self.proc) - self.assert_(image is self.image) + self.assertTrue(proc is self.proc) + self.assertTrue(image is self.image) self.assertEqual(count[0], 0) count[0] += 1 symiter = iter(image) - self.assert_(isinstance(symiter, zbar.SymbolIter)) + self.assertTrue(isinstance(symiter, zbar.SymbolIter)) syms = tuple(image) self.assertEqual(len(syms), 1) for sym in syms: - self.assert_(isinstance(sym, zbar.Symbol)) - self.assert_(sym.type is zbar.Symbol.EAN13) + self.assertTrue(isinstance(sym, zbar.Symbol)) + self.assertTrue(sym.type is zbar.Symbol.EAN13) self.assertEqual(sym.data, '9876543210128') - self.assert_(sym.quality > 0) - self.assert_(sym.orientation is zbar.Orient.UP) + self.assertTrue(sym.quality > 0) + self.assertTrue(sym.orientation is zbar.Orient.UP) closure[0] += 1 explicit_closure = [ 0 ]