Skip to content

Commit 40cd3c7

Browse files
committed
Fix deprecated PyUnicode_FromUnicode (NOTE: shouldn't assume wchar is 4 bytes!) + Unit tests (shapefile, render_grid)
1 parent d6e8d1b commit 40cd3c7

3 files changed

Lines changed: 152 additions & 209 deletions

File tree

src/python_grid_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void grid2utf(T const& grid_type,
105105
}
106106
l.append(boost::python::object(
107107
boost::python::handle<>(
108-
PyUnicode_FromUnicode(line.get(), array_size))));
108+
PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, line.get(), array_size))));
109109
}
110110
}
111111

@@ -168,7 +168,7 @@ void grid2utf(T const& grid_type,
168168
}
169169
l.append(boost::python::object(
170170
boost::python::handle<>(
171-
PyUnicode_FromUnicode(line.get(), array_size))));
171+
PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, line.get(), array_size))));
172172
}
173173
}
174174

test/python_tests/render_grid_test.py

Lines changed: 74 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
1-
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-
3-
4-
import os
5-
6-
from nose.tools import eq_, raises
7-
81
import mapnik
9-
10-
from .utilities import execution_path, run_all
11-
12-
try:
13-
import json
14-
except ImportError:
15-
import simplejson as json
16-
17-
18-
def setup():
19-
# All of the paths used are relative, if we run the tests
20-
# from another directory we need to chdir()
21-
os.chdir(execution_path('.'))
2+
import json
3+
import pytest
224

235
if mapnik.has_grid_renderer():
246
def show_grids(name, g1, g2):
@@ -384,27 +366,26 @@ def test_render_grid():
384366
grid = mapnik.Grid(m.width, m.height, key='Name')
385367
mapnik.render_layer(m, grid, layer=0, fields=['Name'])
386368
utf1 = grid.encode('utf', resolution=4)
387-
eq_(utf1, grid_correct_new3, show_grids(
388-
'new-markers', utf1, grid_correct_new3))
369+
assert utf1 == grid_correct_new3, show_grids('new-markers', utf1, grid_correct_new3)
389370

390371
# check a full view is the same as a full image
391372
grid_view = grid.view(0, 0, width, height)
392373
# for kicks check at full res too
393374
utf3 = grid.encode('utf', resolution=1)
394375
utf4 = grid_view.encode('utf', resolution=1)
395-
eq_(utf3['grid'], utf4['grid'])
396-
eq_(utf3['keys'], utf4['keys'])
397-
eq_(utf3['data'], utf4['data'])
376+
assert utf3['grid'] == utf4['grid']
377+
assert utf3['keys'] == utf4['keys']
378+
assert utf3['data'] == utf4['data']
398379

399-
eq_(resolve(utf4, 0, 0), None)
380+
assert resolve(utf4, 0, 0) == None
400381

401382
# resolve some center points in the
402383
# resampled view
403384
utf5 = grid_view.encode('utf', resolution=4)
404-
eq_(resolve(utf5, 25, 10), {"Name": "North West"})
405-
eq_(resolve(utf5, 25, 46), {"Name": "North East"})
406-
eq_(resolve(utf5, 38, 10), {"Name": "South West"})
407-
eq_(resolve(utf5, 38, 46), {"Name": "South East"})
385+
assert resolve(utf5, 25, 10) == {"Name": "North West"}
386+
assert resolve(utf5, 25, 46) == {"Name": "North East"}
387+
assert resolve(utf5, 38, 10) == {"Name": "South West"}
388+
assert resolve(utf5, 38, 46) == {"Name": "South East"}
408389

409390
grid_feat_id = {
410391
'keys': [
@@ -670,25 +651,25 @@ def test_render_grid3():
670651
grid = mapnik.Grid(m.width, m.height, key='__id__')
671652
mapnik.render_layer(m, grid, layer=0, fields=['__id__', 'Name'])
672653
utf1 = grid.encode('utf', resolution=4)
673-
eq_(utf1, grid_feat_id3, show_grids('id-markers', utf1, grid_feat_id3))
654+
assert utf1 == grid_feat_id3, show_grids('id-markers', utf1 == grid_feat_id3)
674655
# check a full view is the same as a full image
675656
grid_view = grid.view(0, 0, width, height)
676657
# for kicks check at full res too
677658
utf3 = grid.encode('utf', resolution=1)
678659
utf4 = grid_view.encode('utf', resolution=1)
679-
eq_(utf3['grid'], utf4['grid'])
680-
eq_(utf3['keys'], utf4['keys'])
681-
eq_(utf3['data'], utf4['data'])
660+
assert utf3['grid'] == utf4['grid']
661+
assert utf3['keys'] == utf4['keys']
662+
assert utf3['data'] == utf4['data']
682663

683-
eq_(resolve(utf4, 0, 0), None)
664+
assert resolve(utf4, 0, 0) == None
684665

685666
# resolve some center points in the
686667
# resampled view
687668
utf5 = grid_view.encode('utf', resolution=4)
688-
eq_(resolve(utf5, 25, 10), {"Name": "North West", "__id__": 3})
689-
eq_(resolve(utf5, 25, 46), {"Name": "North East", "__id__": 4})
690-
eq_(resolve(utf5, 38, 10), {"Name": "South West", "__id__": 2})
691-
eq_(resolve(utf5, 38, 46), {"Name": "South East", "__id__": 1})
669+
assert resolve(utf5, 25, 10) == {"Name": "North West", "__id__": 3}
670+
assert resolve(utf5, 25, 46) == {"Name": "North East", "__id__": 4}
671+
assert resolve(utf5, 38, 10) == {"Name": "South West", "__id__": 2}
672+
assert resolve(utf5, 38, 46) == {"Name": "South East", "__id__": 1}
692673

693674
def gen_grid_for_id(pixel_key):
694675
ds = mapnik.MemoryDatasource()
@@ -718,39 +699,39 @@ def gen_grid_for_id(pixel_key):
718699

719700
def test_negative_id():
720701
grid = gen_grid_for_id(-1)
721-
eq_(grid.get_pixel(128, 128), -1)
702+
assert grid.get_pixel(128, 128) == -1
722703
utf1 = grid.encode('utf', resolution=4)
723-
eq_(utf1['keys'], ['-1'])
704+
assert utf1['keys'] == ['-1']
724705

725706
def test_32bit_int_id():
726707
int32 = 2147483647
727708
grid = gen_grid_for_id(int32)
728-
eq_(grid.get_pixel(128, 128), int32)
709+
assert grid.get_pixel(128, 128) == int32
729710
utf1 = grid.encode('utf', resolution=4)
730-
eq_(utf1['keys'], [str(int32)])
711+
assert utf1['keys'] == [str(int32)]
731712
max_neg = -(int32)
732713
grid = gen_grid_for_id(max_neg)
733-
eq_(grid.get_pixel(128, 128), max_neg)
714+
assert grid.get_pixel(128, 128) == max_neg
734715
utf1 = grid.encode('utf', resolution=4)
735-
eq_(utf1['keys'], [str(max_neg)])
716+
assert utf1['keys'] == [str(max_neg)]
736717

737718
def test_64bit_int_id():
738719
int64 = 0x7FFFFFFFFFFFFFFF
739720
grid = gen_grid_for_id(int64)
740-
eq_(grid.get_pixel(128, 128), int64)
721+
assert grid.get_pixel(128, 128) == int64
741722
utf1 = grid.encode('utf', resolution=4)
742-
eq_(utf1['keys'], [str(int64)])
723+
assert utf1['keys'] == [str(int64)]
743724
max_neg = -(int64)
744725
grid = gen_grid_for_id(max_neg)
745-
eq_(grid.get_pixel(128, 128), max_neg)
726+
assert grid.get_pixel(128, 128) == max_neg
746727
utf1 = grid.encode('utf', resolution=4)
747-
eq_(utf1['keys'], [str(max_neg)])
728+
assert utf1['keys'] == [str(max_neg)]
748729

749730
def test_id_zero():
750731
grid = gen_grid_for_id(0)
751-
eq_(grid.get_pixel(128, 128), 0)
732+
assert grid.get_pixel(128, 128) == 0
752733
utf1 = grid.encode('utf', resolution=4)
753-
eq_(utf1['keys'], ['0'])
734+
assert utf1['keys'] == ['0']
754735

755736
line_expected = {
756737
"keys": [
@@ -852,7 +833,7 @@ def test_line_rendering():
852833
grid = mapnik.Grid(m.width, m.height, key='__id__')
853834
mapnik.render_layer(m, grid, layer=0, fields=['Name'])
854835
utf1 = grid.encode()
855-
eq_(utf1, line_expected, show_grids('line', utf1, line_expected))
836+
assert utf1 == line_expected, show_grids('line', utf1, line_expected)
856837

857838
point_expected = {
858839
"data": {
@@ -939,61 +920,57 @@ def test_line_rendering():
939920
def test_point_symbolizer_grid():
940921
width, height = 256, 256
941922
sym = mapnik.PointSymbolizer()
942-
sym.file = '../data/images/dummy.png'
923+
sym.file = './test/data/images/dummy.png'
943924
m = create_grid_map(width, height, sym)
944925
ul_lonlat = mapnik.Coord(142.30, -38.20)
945926
lr_lonlat = mapnik.Coord(143.40, -38.80)
946927
m.zoom_to_box(mapnik.Box2d(ul_lonlat, lr_lonlat))
947928
grid = mapnik.Grid(m.width, m.height)
948929
mapnik.render_layer(m, grid, layer=0, fields=['Name'])
949930
utf1 = grid.encode()
950-
eq_(utf1, point_expected, show_grids('point-sym', utf1, point_expected))
931+
assert utf1 == point_expected, show_grids('point-sym', utf1, point_expected)
951932

952933
test_point_symbolizer_grid.requires_data = True
953934

954935
# should throw because this is a mis-usage
955936
# https://github.com/mapnik/mapnik/issues/1325
956-
@raises(RuntimeError)
957937
def test_render_to_grid_multiple_times():
958-
# create map with two layers
959-
m = mapnik.Map(256, 256)
960-
s = mapnik.Style()
961-
r = mapnik.Rule()
962-
sym = mapnik.MarkersSymbolizer()
963-
sym.allow_overlap = True
964-
r.symbols.append(sym)
965-
s.rules.append(r)
966-
m.append_style('points', s)
967-
968-
# NOTE: we use a csv datasource here
969-
# because the memorydatasource fails silently for
970-
# queries requesting fields that do not exist in the datasource
971-
ds1 = mapnik.Datasource(**{"type": "csv", "inline": '''
972-
wkt,Name
973-
"POINT (143.10 -38.60)",South East'''})
974-
lyr1 = mapnik.Layer('One')
975-
lyr1.datasource = ds1
976-
lyr1.styles.append('points')
977-
m.layers.append(lyr1)
978-
979-
ds2 = mapnik.Datasource(**{"type": "csv", "inline": '''
980-
wkt,Value
981-
"POINT (142.48 -38.60)",South West'''})
982-
lyr2 = mapnik.Layer('Two')
983-
lyr2.datasource = ds2
984-
lyr2.styles.append('points')
985-
m.layers.append(lyr2)
986-
987-
ul_lonlat = mapnik.Coord(142.30, -38.20)
988-
lr_lonlat = mapnik.Coord(143.40, -38.80)
989-
m.zoom_to_box(mapnik.Box2d(ul_lonlat, lr_lonlat))
990-
grid = mapnik.Grid(m.width, m.height)
991-
mapnik.render_layer(m, grid, layer=0, fields=['Name'])
992-
# should throw right here since Name will be a property now on the `grid` object
993-
# and it is not found on the second layer
994-
mapnik.render_layer(m, grid, layer=1, fields=['Value'])
995-
grid.encode()
996-
997-
if __name__ == "__main__":
998-
setup()
999-
exit(run_all(eval(x) for x in dir() if x.startswith("test_")))
938+
with pytest.raises(RuntimeError):
939+
# create map with two layers
940+
m = mapnik.Map(256, 256)
941+
s = mapnik.Style()
942+
r = mapnik.Rule()
943+
sym = mapnik.MarkersSymbolizer()
944+
sym.allow_overlap = True
945+
r.symbols.append(sym)
946+
s.rules.append(r)
947+
m.append_style('points', s)
948+
949+
# NOTE: we use a csv datasource here
950+
# because the memorydatasource fails silently for
951+
# queries requesting fields that do not exist in the datasource
952+
ds1 = mapnik.Datasource(**{"type": "csv", "inline": '''
953+
wkt,Name
954+
"POINT (143.10 -38.60)",South East'''})
955+
lyr1 = mapnik.Layer('One')
956+
lyr1.datasource = ds1
957+
lyr1.styles.append('points')
958+
m.layers.append(lyr1)
959+
960+
ds2 = mapnik.Datasource(**{"type": "csv", "inline": '''
961+
wkt,Value
962+
"POINT (142.48 -38.60)",South West'''})
963+
lyr2 = mapnik.Layer('Two')
964+
lyr2.datasource = ds2
965+
lyr2.styles.append('points')
966+
m.layers.append(lyr2)
967+
968+
ul_lonlat = mapnik.Coord(142.30, -38.20)
969+
lr_lonlat = mapnik.Coord(143.40, -38.80)
970+
m.zoom_to_box(mapnik.Box2d(ul_lonlat, lr_lonlat))
971+
grid = mapnik.Grid(m.width, m.height)
972+
mapnik.render_layer(m, grid, layer=0, fields=['Name'])
973+
# should throw right here since Name will be a property now on the `grid` object
974+
# and it is not found on the second layer
975+
mapnik.render_layer(m, grid, layer=1, fields=['Value'])
976+
grid.encode()

0 commit comments

Comments
 (0)