From e97b7a26779054e7bb4326c688b0ed7823037dec Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 28 May 2014 09:22:53 +0200 Subject: [PATCH] Fixed #22714 -- Made contrib.gis use six-provided memoryview type Thanks Tim Graham for the report. --- django/contrib/gis/__init__.py | 8 -------- django/contrib/gis/db/models/proxy.py | 3 +-- django/contrib/gis/db/models/query.py | 3 +-- django/contrib/gis/gdal/geometries.py | 8 +++----- django/contrib/gis/geos/factory.py | 3 +-- django/contrib/gis/geos/geometry.py | 5 ++--- django/contrib/gis/geos/prototypes/io.py | 5 ++--- django/contrib/gis/geos/tests/test_geos.py | 9 ++++----- django/contrib/gis/geos/tests/test_io.py | 2 +- 9 files changed, 15 insertions(+), 31 deletions(-) diff --git a/django/contrib/gis/__init__.py b/django/contrib/gis/__init__.py index 84b3552ce7..458c90eb58 100644 --- a/django/contrib/gis/__init__.py +++ b/django/contrib/gis/__init__.py @@ -1,9 +1 @@ -from django.utils import six - -if six.PY3: - memoryview = memoryview -else: - memoryview = buffer - - default_app_config = 'django.contrib.gis.apps.GISConfig' diff --git a/django/contrib/gis/db/models/proxy.py b/django/contrib/gis/db/models/proxy.py index 91ded31dc0..8f2b04c452 100644 --- a/django/contrib/gis/db/models/proxy.py +++ b/django/contrib/gis/db/models/proxy.py @@ -5,7 +5,6 @@ corresponding to geographic model fields. Thanks to Robert Coup for providing this functionality (see #4322). """ -from django.contrib.gis import memoryview from django.utils import six @@ -57,7 +56,7 @@ class GeometryProxy(object): # Assigning the SRID to the geometry. if value.srid is None: value.srid = self._field.srid - elif value is None or isinstance(value, six.string_types + (memoryview,)): + elif value is None or isinstance(value, six.string_types + (six.memoryview,)): # Set with None, WKT, HEX, or WKB pass else: diff --git a/django/contrib/gis/db/models/query.py b/django/contrib/gis/db/models/query.py index add67890e8..4080ccb3a2 100644 --- a/django/contrib/gis/db/models/query.py +++ b/django/contrib/gis/db/models/query.py @@ -1,7 +1,6 @@ from django.db import connections from django.db.models.query import QuerySet, ValuesQuerySet, ValuesListQuerySet -from django.contrib.gis import memoryview from django.contrib.gis.db.models import aggregates from django.contrib.gis.db.models.fields import get_srid_info, PointField, LineStringField from django.contrib.gis.db.models.sql import AreaField, DistanceField, GeomField, GeoQuery @@ -690,7 +689,7 @@ class GeoQuerySet(QuerySet): if not backend.geography: if not isinstance(geo_field, PointField): raise ValueError('Spherical distance calculation only supported on PointFields.') - if not str(Geometry(memoryview(params[0].ewkb)).geom_type) == 'Point': + if not str(Geometry(six.memoryview(params[0].ewkb)).geom_type) == 'Point': raise ValueError('Spherical distance calculation only supported with Point Geometry parameters') # The `function` procedure argument needs to be set differently for # geodetic distance calculations. diff --git a/django/contrib/gis/gdal/geometries.py b/django/contrib/gis/gdal/geometries.py index 60aae9d5cb..e3f76ef162 100644 --- a/django/contrib/gis/gdal/geometries.py +++ b/django/contrib/gis/gdal/geometries.py @@ -43,8 +43,6 @@ import sys from binascii import a2b_hex, b2a_hex from ctypes import byref, string_at, c_char_p, c_double, c_ubyte, c_void_p -from django.contrib.gis import memoryview - # Getting GDAL prerequisites from django.contrib.gis.gdal.base import GDALBase from django.contrib.gis.gdal.envelope import Envelope, OGREnvelope @@ -77,7 +75,7 @@ class OGRGeometry(GDALBase): # If HEX, unpack input to a binary buffer. if str_instance and hex_regex.match(geom_input): - geom_input = memoryview(a2b_hex(geom_input.upper().encode())) + geom_input = six.memoryview(a2b_hex(geom_input.upper().encode())) str_instance = False # Constructing the geometry, @@ -102,7 +100,7 @@ class OGRGeometry(GDALBase): # (e.g., 'Point', 'POLYGON'). OGRGeomType(geom_input) g = capi.create_geom(OGRGeomType(geom_input).num) - elif isinstance(geom_input, memoryview): + elif isinstance(geom_input, six.memoryview): # WKB was passed in g = capi.from_wkb(bytes(geom_input), None, byref(c_void_p()), len(geom_input)) elif isinstance(geom_input, OGRGeomType): @@ -346,7 +344,7 @@ class OGRGeometry(GDALBase): buf = (c_ubyte * sz)() capi.to_wkb(self.ptr, byteorder, byref(buf)) # Returning a buffer of the string at the pointer. - return memoryview(string_at(buf, sz)) + return six.memoryview(string_at(buf, sz)) @property def wkt(self): diff --git a/django/contrib/gis/geos/factory.py b/django/contrib/gis/geos/factory.py index 2e5fa4f331..87ee660020 100644 --- a/django/contrib/gis/geos/factory.py +++ b/django/contrib/gis/geos/factory.py @@ -1,4 +1,3 @@ -from django.contrib.gis import memoryview from django.contrib.gis.geos.geometry import GEOSGeometry, wkt_regex, hex_regex from django.utils import six @@ -27,7 +26,7 @@ def fromfile(file_h): else: return GEOSGeometry(buf) - return GEOSGeometry(memoryview(buf)) + return GEOSGeometry(six.memoryview(buf)) def fromstr(string, **kwargs): diff --git a/django/contrib/gis/geos/geometry.py b/django/contrib/gis/geos/geometry.py index cc22bea1ec..3cee800f1c 100644 --- a/django/contrib/gis/geos/geometry.py +++ b/django/contrib/gis/geos/geometry.py @@ -7,7 +7,6 @@ from __future__ import unicode_literals # Python, ctypes and types dependencies. from ctypes import addressof, byref, c_double -from django.contrib.gis import memoryview # super-class for mutable list behavior from django.contrib.gis.geos.mutable_list import ListMixin @@ -80,7 +79,7 @@ class GEOSGeometry(GEOSBase, ListMixin): elif isinstance(geo_input, GEOM_PTR): # When the input is a pointer to a geometry (GEOM_PTR). g = geo_input - elif isinstance(geo_input, memoryview): + elif isinstance(geo_input, six.memoryview): # When the input is a buffer (WKB). g = wkb_r().read(geo_input) elif isinstance(geo_input, GEOSGeometry): @@ -151,7 +150,7 @@ class GEOSGeometry(GEOSBase, ListMixin): def __setstate__(self, state): # Instantiating from the tuple state that was pickled. wkb, srid = state - ptr = wkb_r().read(memoryview(wkb)) + ptr = wkb_r().read(six.memoryview(wkb)) if not ptr: raise GEOSException('Invalid Geometry loaded from pickled state.') self.ptr = ptr diff --git a/django/contrib/gis/geos/prototypes/io.py b/django/contrib/gis/geos/prototypes/io.py index a09e977425..34bd34886a 100644 --- a/django/contrib/gis/geos/prototypes/io.py +++ b/django/contrib/gis/geos/prototypes/io.py @@ -1,6 +1,5 @@ import threading from ctypes import byref, c_char_p, c_int, c_char, c_size_t, Structure, POINTER -from django.contrib.gis import memoryview from django.contrib.gis.geos.base import GEOSBase from django.contrib.gis.geos.libgeos import GEOM_PTR from django.contrib.gis.geos.prototypes.errcheck import check_geom, check_string, check_sized_string @@ -164,7 +163,7 @@ class _WKBReader(IOBase): def read(self, wkb): "Returns a _pointer_ to C GEOS Geometry object from the given WKB." - if isinstance(wkb, memoryview): + if isinstance(wkb, six.memoryview): wkb_s = bytes(wkb) return wkb_reader_read(self.ptr, wkb_s, len(wkb_s)) elif isinstance(wkb, (bytes, six.string_types)): @@ -201,7 +200,7 @@ class WKBWriter(IOBase): def write(self, geom): "Returns the WKB representation of the given geometry." - return memoryview(wkb_writer_write(self.ptr, geom.ptr, byref(c_size_t()))) + return six.memoryview(wkb_writer_write(self.ptr, geom.ptr, byref(c_size_t()))) def write_hex(self, geom): "Returns the HEXEWKB representation of the given geometry." diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py index ac2e65925c..d84350ab5b 100644 --- a/django/contrib/gis/geos/tests/test_geos.py +++ b/django/contrib/gis/geos/tests/test_geos.py @@ -10,7 +10,6 @@ from io import BytesIO from django.contrib.gis.gdal import HAS_GDAL -from django.contrib.gis import memoryview from django.contrib.gis.geometry.test_data import TestDataMixin from django.utils.encoding import force_bytes @@ -110,8 +109,8 @@ class GEOSTest(unittest.TestCase, TestDataMixin): self.assertEqual(True, GEOSGeometry(hexewkb_3d).hasz) # Same for EWKB. - self.assertEqual(memoryview(a2b_hex(hexewkb_2d)), pnt_2d.ewkb) - self.assertEqual(memoryview(a2b_hex(hexewkb_3d)), pnt_3d.ewkb) + self.assertEqual(six.memoryview(a2b_hex(hexewkb_2d)), pnt_2d.ewkb) + self.assertEqual(six.memoryview(a2b_hex(hexewkb_3d)), pnt_3d.ewkb) # Redundant sanity check. self.assertEqual(4326, GEOSGeometry(hexewkb_2d).srid) @@ -132,7 +131,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin): fromstr(err.wkt) # Bad WKB - self.assertRaises(GEOSException, GEOSGeometry, memoryview(b'0')) + self.assertRaises(GEOSException, GEOSGeometry, six.memoryview(b'0')) class NotAGeometry(object): pass @@ -160,7 +159,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin): def test_create_wkb(self): "Testing creation from WKB." for g in self.geometries.hex_wkt: - wkb = memoryview(a2b_hex(g.hex.encode())) + wkb = six.memoryview(a2b_hex(g.hex.encode())) geom_h = GEOSGeometry(wkb) # we need to do this so decimal places get normalized geom_t = fromstr(g.wkt) diff --git a/django/contrib/gis/geos/tests/test_io.py b/django/contrib/gis/geos/tests/test_io.py index 00dad3abc7..06ca76e33d 100644 --- a/django/contrib/gis/geos/tests/test_io.py +++ b/django/contrib/gis/geos/tests/test_io.py @@ -4,7 +4,7 @@ import binascii import unittest from unittest import skipUnless -from django.contrib.gis import memoryview +from django.utils.six import memoryview from ..import HAS_GEOS