Skip to content

Commit d064522

Browse files
committed
Fix python 3 compatibility issues in tests
Fixes a number of issues with python 3 compatibility issues: * `hexlify` returns bytes in python 3 * `im.view` returns bytes in python 3 * `Popen` returns bytes in python 3 * division returns float not int in python 3 With this applied python 2.7 and 3.4 give me the same results.
1 parent 37bbeed commit d064522

File tree

2 files changed

+49
-44
lines changed

2 files changed

+49
-44
lines changed

test/python_tests/multi_tile_raster_test.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,28 @@ def test_multi_tile_policy():
4646
mapnik.render(_map, im)
4747

4848
# test green chunk
49-
eq_(im.view(0, 64, 1, 1).tostring(), '\x00\xff\x00\xff')
50-
eq_(im.view(127, 64, 1, 1).tostring(), '\x00\xff\x00\xff')
51-
eq_(im.view(0, 127, 1, 1).tostring(), '\x00\xff\x00\xff')
52-
eq_(im.view(127, 127, 1, 1).tostring(), '\x00\xff\x00\xff')
49+
eq_(im.view(0, 64, 1, 1).tostring(), b'\x00\xff\x00\xff')
50+
eq_(im.view(127, 64, 1, 1).tostring(), b'\x00\xff\x00\xff')
51+
eq_(im.view(0, 127, 1, 1).tostring(), b'\x00\xff\x00\xff')
52+
eq_(im.view(127, 127, 1, 1).tostring(), b'\x00\xff\x00\xff')
5353

5454
# test blue chunk
55-
eq_(im.view(128, 64, 1, 1).tostring(), '\x00\x00\xff\xff')
56-
eq_(im.view(255, 64, 1, 1).tostring(), '\x00\x00\xff\xff')
57-
eq_(im.view(128, 127, 1, 1).tostring(), '\x00\x00\xff\xff')
58-
eq_(im.view(255, 127, 1, 1).tostring(), '\x00\x00\xff\xff')
55+
eq_(im.view(128, 64, 1, 1).tostring(), b'\x00\x00\xff\xff')
56+
eq_(im.view(255, 64, 1, 1).tostring(), b'\x00\x00\xff\xff')
57+
eq_(im.view(128, 127, 1, 1).tostring(), b'\x00\x00\xff\xff')
58+
eq_(im.view(255, 127, 1, 1).tostring(), b'\x00\x00\xff\xff')
5959

6060
# test red chunk
61-
eq_(im.view(0, 128, 1, 1).tostring(), '\xff\x00\x00\xff')
62-
eq_(im.view(127, 128, 1, 1).tostring(), '\xff\x00\x00\xff')
63-
eq_(im.view(0, 191, 1, 1).tostring(), '\xff\x00\x00\xff')
64-
eq_(im.view(127, 191, 1, 1).tostring(), '\xff\x00\x00\xff')
61+
eq_(im.view(0, 128, 1, 1).tostring(), b'\xff\x00\x00\xff')
62+
eq_(im.view(127, 128, 1, 1).tostring(), b'\xff\x00\x00\xff')
63+
eq_(im.view(0, 191, 1, 1).tostring(), b'\xff\x00\x00\xff')
64+
eq_(im.view(127, 191, 1, 1).tostring(), b'\xff\x00\x00\xff')
6565

6666
# test magenta chunk
67-
eq_(im.view(128, 128, 1, 1).tostring(), '\xff\x00\xff\xff')
68-
eq_(im.view(255, 128, 1, 1).tostring(), '\xff\x00\xff\xff')
69-
eq_(im.view(128, 191, 1, 1).tostring(), '\xff\x00\xff\xff')
70-
eq_(im.view(255, 191, 1, 1).tostring(), '\xff\x00\xff\xff')
67+
eq_(im.view(128, 128, 1, 1).tostring(), b'\xff\x00\xff\xff')
68+
eq_(im.view(255, 128, 1, 1).tostring(), b'\xff\x00\xff\xff')
69+
eq_(im.view(128, 191, 1, 1).tostring(), b'\xff\x00\xff\xff')
70+
eq_(im.view(255, 191, 1, 1).tostring(), b'\xff\x00\xff\xff')
7171

7272
if __name__ == "__main__":
7373
setup()

test/python_tests/pgraster_test.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def setup():
3333
def call(cmd, silent=False):
3434
stdin, stderr = Popen(cmd, shell=True, stdout=PIPE,
3535
stderr=PIPE).communicate()
36+
stdin = stdin.decode()
37+
stderr = stderr.decode()
3638
if not stderr:
3739
return stdin.strip()
3840
elif not silent and 'error' in stderr.lower() \
@@ -212,18 +214,18 @@ def _test_dataraster_16bsi_rendering(lbl, overview, rescale, clip):
212214
lap = time.time() - t0
213215
log('T ' + str(lap) + ' -- ' + lbl + ' E:full')
214216
# no data
215-
eq_(im.view(1, 1, 1, 1).tostring(), '\x00\x00\x00\x00')
216-
eq_(im.view(255, 255, 1, 1).tostring(), '\x00\x00\x00\x00')
217-
eq_(im.view(195, 116, 1, 1).tostring(), '\x00\x00\x00\x00')
217+
eq_(im.view(1, 1, 1, 1).tostring(), b'\x00\x00\x00\x00')
218+
eq_(im.view(255, 255, 1, 1).tostring(), b'\x00\x00\x00\x00')
219+
eq_(im.view(195, 116, 1, 1).tostring(), b'\x00\x00\x00\x00')
218220
# A0A0A0
219-
eq_(im.view(100, 120, 1, 1).tostring(), '\xa0\xa0\xa0\xff')
220-
eq_(im.view(75, 80, 1, 1).tostring(), '\xa0\xa0\xa0\xff')
221+
eq_(im.view(100, 120, 1, 1).tostring(), b'\xa0\xa0\xa0\xff')
222+
eq_(im.view(75, 80, 1, 1).tostring(), b'\xa0\xa0\xa0\xff')
221223
# 808080
222-
eq_(im.view(74, 170, 1, 1).tostring(), '\x80\x80\x80\xff')
223-
eq_(im.view(30, 50, 1, 1).tostring(), '\x80\x80\x80\xff')
224+
eq_(im.view(74, 170, 1, 1).tostring(), b'\x80\x80\x80\xff')
225+
eq_(im.view(30, 50, 1, 1).tostring(), b'\x80\x80\x80\xff')
224226
# 404040
225-
eq_(im.view(190, 70, 1, 1).tostring(), '\x40\x40\x40\xff')
226-
eq_(im.view(140, 170, 1, 1).tostring(), '\x40\x40\x40\xff')
227+
eq_(im.view(190, 70, 1, 1).tostring(), b'\x40\x40\x40\xff')
228+
eq_(im.view(140, 170, 1, 1).tostring(), b'\x40\x40\x40\xff')
227229

228230
# Now zoom over a portion of the env (1/10)
229231
newenv = mapnik.Box2d(273663, 4024478, 330738, 4072303)
@@ -233,16 +235,16 @@ def _test_dataraster_16bsi_rendering(lbl, overview, rescale, clip):
233235
lap = time.time() - t0
234236
log('T ' + str(lap) + ' -- ' + lbl + ' E:1/10')
235237
# nodata
236-
eq_(hexlify(im.view(255, 255, 1, 1).tostring()), '00000000')
237-
eq_(hexlify(im.view(200, 254, 1, 1).tostring()), '00000000')
238+
eq_(hexlify(im.view(255, 255, 1, 1).tostring()), b'00000000')
239+
eq_(hexlify(im.view(200, 254, 1, 1).tostring()), b'00000000')
238240
# A0A0A0
239-
eq_(hexlify(im.view(90, 232, 1, 1).tostring()), 'a0a0a0ff')
240-
eq_(hexlify(im.view(96, 245, 1, 1).tostring()), 'a0a0a0ff')
241+
eq_(hexlify(im.view(90, 232, 1, 1).tostring()), b'a0a0a0ff')
242+
eq_(hexlify(im.view(96, 245, 1, 1).tostring()), b'a0a0a0ff')
241243
# 808080
242-
eq_(hexlify(im.view(1, 1, 1, 1).tostring()), '808080ff')
243-
eq_(hexlify(im.view(128, 128, 1, 1).tostring()), '808080ff')
244+
eq_(hexlify(im.view(1, 1, 1, 1).tostring()), b'808080ff')
245+
eq_(hexlify(im.view(128, 128, 1, 1).tostring()), b'808080ff')
244246
# 404040
245-
eq_(hexlify(im.view(255, 0, 1, 1).tostring()), '404040ff')
247+
eq_(hexlify(im.view(255, 0, 1, 1).tostring()), b'404040ff')
246248

247249
def _test_dataraster_16bsi(lbl, tilesize, constraint, overview):
248250
import_raster(
@@ -319,12 +321,12 @@ def _test_rgba_8bui_rendering(lbl, overview, rescale, clip):
319321
lyr.name, lbl, overview, clip)
320322
compare_images(expected, im)
321323
# no data
322-
eq_(hexlify(im.view(3, 3, 1, 1).tostring()), '00000000')
323-
eq_(hexlify(im.view(250, 250, 1, 1).tostring()), '00000000')
324+
eq_(hexlify(im.view(3, 3, 1, 1).tostring()), b'00000000')
325+
eq_(hexlify(im.view(250, 250, 1, 1).tostring()), b'00000000')
324326
# full opaque river color
325-
eq_(hexlify(im.view(175, 118, 1, 1).tostring()), 'b9d8f8ff')
327+
eq_(hexlify(im.view(175, 118, 1, 1).tostring()), b'b9d8f8ff')
326328
# half-transparent pixel
327-
pxstr = hexlify(im.view(122, 138, 1, 1).tostring())
329+
pxstr = hexlify(im.view(122, 138, 1, 1).tostring()).decode()
328330
apat = ".*(..)$"
329331
match = re.match(apat, pxstr)
330332
assert match, 'pixel ' + pxstr + ' does not match pattern ' + apat
@@ -344,12 +346,12 @@ def _test_rgba_8bui_rendering(lbl, overview, rescale, clip):
344346
lyr.name, lbl, overview, clip)
345347
compare_images(expected, im)
346348
# no data
347-
eq_(hexlify(im.view(255, 255, 1, 1).tostring()), '00000000')
348-
eq_(hexlify(im.view(200, 40, 1, 1).tostring()), '00000000')
349+
eq_(hexlify(im.view(255, 255, 1, 1).tostring()), b'00000000')
350+
eq_(hexlify(im.view(200, 40, 1, 1).tostring()), b'00000000')
349351
# full opaque river color
350-
eq_(hexlify(im.view(100, 168, 1, 1).tostring()), 'b9d8f8ff')
352+
eq_(hexlify(im.view(100, 168, 1, 1).tostring()), b'b9d8f8ff')
351353
# half-transparent pixel
352-
pxstr = hexlify(im.view(122, 138, 1, 1).tostring())
354+
pxstr = hexlify(im.view(122, 138, 1, 1).tostring()).decode()
353355
apat = ".*(..)$"
354356
match = re.match(apat, pxstr)
355357
assert match, 'pixel ' + pxstr + ' does not match pattern ' + apat
@@ -502,7 +504,7 @@ def _test_grayscale_subquery(lbl, pixtype, value):
502504
# 13 | v | b | v |
503505
# +---+---+---+
504506
#
505-
val_a = value / 3
507+
val_a = int(value / 3)
506508
val_b = val_a * 2
507509
sql = "(select 3 as i, " \
508510
" ST_SetValues(" \
@@ -556,10 +558,13 @@ def _test_grayscale_subquery(lbl, pixtype, value):
556558
compare_images(expected, im)
557559
h = format(value, '02x')
558560
hex_v = h + h + h + 'ff'
561+
hex_v = hex_v.encode()
559562
h = format(val_a, '02x')
560563
hex_a = h + h + h + 'ff'
564+
hex_a = hex_a.encode()
561565
h = format(val_b, '02x')
562566
hex_b = h + h + h + 'ff'
567+
hex_b = hex_b.encode()
563568
eq_(hexlify(im.view(3, 3, 1, 1).tostring()), hex_v)
564569
eq_(hexlify(im.view(8, 3, 1, 1).tostring()), hex_v)
565570
eq_(hexlify(im.view(13, 3, 1, 1).tostring()), hex_v)
@@ -788,9 +793,9 @@ def _test_rgba_subquery(lbl, pixtype, r, g, b, a, g1, b1):
788793
expected = 'images/support/pgraster/%s-%s-%s-%s-%s-%s-%s-%s-%s.png' % (
789794
lyr.name, lbl, pixtype, r, g, b, a, g1, b1)
790795
compare_images(expected, im)
791-
hex_v = format(r << 24 | g << 16 | b << 8 | a, '08x')
792-
hex_a = format(r << 24 | g1 << 16 | b << 8 | a, '08x')
793-
hex_b = format(r << 24 | g << 16 | b1 << 8 | a, '08x')
796+
hex_v = format(r << 24 | g << 16 | b << 8 | a, '08x').encode()
797+
hex_a = format(r << 24 | g1 << 16 | b << 8 | a, '08x').encode()
798+
hex_b = format(r << 24 | g << 16 | b1 << 8 | a, '08x').encode()
794799
eq_(hexlify(im.view(3, 3, 1, 1).tostring()), hex_v)
795800
eq_(hexlify(im.view(8, 3, 1, 1).tostring()), hex_v)
796801
eq_(hexlify(im.view(13, 3, 1, 1).tostring()), hex_v)

0 commit comments

Comments
 (0)