From 6928ad184e19795705790d536a7e7d7774da307d Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Sun, 12 Jun 2016 07:03:22 +0600 Subject: [PATCH] Fixed #26736 -- Fixed crashes in SpatialReference with non-ASCII characters. --- django/contrib/gis/gdal/prototypes/srs.py | 8 ++++---- django/contrib/gis/gdal/srs.py | 7 ++----- tests/gis_tests/gdal_tests/test_srs.py | 25 +++++++++++++++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/django/contrib/gis/gdal/prototypes/srs.py b/django/contrib/gis/gdal/prototypes/srs.py index ccd24c5f96..1ea8a77701 100644 --- a/django/contrib/gis/gdal/prototypes/srs.py +++ b/django/contrib/gis/gdal/prototypes/srs.py @@ -54,18 +54,18 @@ linear_units = units_func(lgdal.OSRGetLinearUnits) angular_units = units_func(lgdal.OSRGetAngularUnits) # For exporting to WKT, PROJ.4, "Pretty" WKT, and XML. -to_wkt = string_output(std_call('OSRExportToWkt'), [c_void_p, POINTER(c_char_p)], decoding='ascii') +to_wkt = string_output(std_call('OSRExportToWkt'), [c_void_p, POINTER(c_char_p)], decoding='utf-8') to_proj = string_output(std_call('OSRExportToProj4'), [c_void_p, POINTER(c_char_p)], decoding='ascii') to_pretty_wkt = string_output( std_call('OSRExportToPrettyWkt'), - [c_void_p, POINTER(c_char_p), c_int], offset=-2, decoding='ascii' + [c_void_p, POINTER(c_char_p), c_int], offset=-2, decoding='utf-8' ) # Memory leak fixed in GDAL 1.5; still exists in 1.4. -to_xml = string_output(lgdal.OSRExportToXML, [c_void_p, POINTER(c_char_p), c_char_p], offset=-2, decoding='ascii') +to_xml = string_output(lgdal.OSRExportToXML, [c_void_p, POINTER(c_char_p), c_char_p], offset=-2, decoding='utf-8') # String attribute retrival routines. -get_attr_value = const_string_output(std_call('OSRGetAttrValue'), [c_void_p, c_char_p, c_int], decoding='ascii') +get_attr_value = const_string_output(std_call('OSRGetAttrValue'), [c_void_p, c_char_p, c_int], decoding='utf-8') get_auth_name = const_string_output(lgdal.OSRGetAuthorityName, [c_void_p, c_char_p], decoding='ascii') get_auth_code = const_string_output(lgdal.OSRGetAuthorityCode, [c_void_p, c_char_p], decoding='ascii') diff --git a/django/contrib/gis/gdal/srs.py b/django/contrib/gis/gdal/srs.py index ddb3ca061b..13e47132cd 100644 --- a/django/contrib/gis/gdal/srs.py +++ b/django/contrib/gis/gdal/srs.py @@ -55,9 +55,6 @@ class SpatialReference(GDALBase): self.import_wkt(srs_input) return elif isinstance(srs_input, six.string_types): - # Encoding to ASCII if unicode passed in. - if isinstance(srs_input, six.text_type): - srs_input = srs_input.encode('ascii') try: # If SRID is a string, e.g., '4326', then make acceptable # as user input. @@ -297,7 +294,7 @@ class SpatialReference(GDALBase): def import_wkt(self, wkt): "Imports the Spatial Reference from OGC WKT (string)" - capi.from_wkt(self.ptr, byref(c_char_p(wkt))) + capi.from_wkt(self.ptr, byref(c_char_p(force_bytes(wkt)))) def import_xml(self, xml): "Imports the Spatial Reference from an XML string." @@ -327,7 +324,7 @@ class SpatialReference(GDALBase): @property def xml(self, dialect=''): "Returns the XML representation of this Spatial Reference." - return capi.to_xml(self.ptr, byref(c_char_p()), dialect) + return capi.to_xml(self.ptr, byref(c_char_p()), force_bytes(dialect)) class CoordTransform(GDALBase): diff --git a/tests/gis_tests/gdal_tests/test_srs.py b/tests/gis_tests/gdal_tests/test_srs.py index c52832041a..9c7f5c39a4 100644 --- a/tests/gis_tests/gdal_tests/test_srs.py +++ b/tests/gis_tests/gdal_tests/test_srs.py @@ -1,3 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + import unittest from unittest import skipUnless @@ -259,3 +262,25 @@ class SpatialRefTest(unittest.TestCase): self.assertEqual('EPSG', s1['AUTHORITY']) self.assertEqual(4326, int(s1['AUTHORITY', 1])) self.assertIsNone(s1['FOOBAR']) + + def test_unicode(self): + wkt = ( + 'PROJCS["DHDN / Soldner 39 Langschoß",' + 'GEOGCS["DHDN",DATUM["Deutsches_Hauptdreiecksnetz",' + 'SPHEROID["Bessel 1841",6377397.155,299.1528128,AUTHORITY["EPSG","7004"]],AUTHORITY["EPSG","6314"]],' + 'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],' + 'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],' + 'AUTHORITY["EPSG","4314"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],' + 'PROJECTION["Cassini_Soldner"],PARAMETER["latitude_of_origin",50.66738711],' + 'PARAMETER["central_meridian",6.28935703],PARAMETER["false_easting",0],' + 'PARAMETER["false_northing",0],AUTHORITY["mj10777.de","187939"],AXIS["x",NORTH],AXIS["y",EAST]]' + ) + srs = SpatialReference(wkt) + srs_list = [srs, srs.clone()] + srs.import_wkt(wkt) + + for srs in srs_list: + self.assertEqual(srs.name, 'DHDN / Soldner 39 Langschoß') + self.assertEqual(srs.wkt, wkt) + self.assertIn('Langschoß', srs.pretty_wkt) + self.assertIn('Langschoß', srs.xml)