Fixed #22714 -- Made contrib.gis use six-provided memoryview type

Thanks Tim Graham for the report.
This commit is contained in:
Claude Paroz 2014-05-28 09:22:53 +02:00
parent 4b57e203fe
commit e97b7a2677
9 changed files with 15 additions and 31 deletions

View File

@ -1,9 +1 @@
from django.utils import six
if six.PY3:
memoryview = memoryview
else:
memoryview = buffer
default_app_config = 'django.contrib.gis.apps.GISConfig' default_app_config = 'django.contrib.gis.apps.GISConfig'

View File

@ -5,7 +5,6 @@ corresponding to geographic model fields.
Thanks to Robert Coup for providing this functionality (see #4322). Thanks to Robert Coup for providing this functionality (see #4322).
""" """
from django.contrib.gis import memoryview
from django.utils import six from django.utils import six
@ -57,7 +56,7 @@ class GeometryProxy(object):
# Assigning the SRID to the geometry. # Assigning the SRID to the geometry.
if value.srid is None: if value.srid is None:
value.srid = self._field.srid 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 # Set with None, WKT, HEX, or WKB
pass pass
else: else:

View File

@ -1,7 +1,6 @@
from django.db import connections from django.db import connections
from django.db.models.query import QuerySet, ValuesQuerySet, ValuesListQuerySet 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 import aggregates
from django.contrib.gis.db.models.fields import get_srid_info, PointField, LineStringField 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 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 backend.geography:
if not isinstance(geo_field, PointField): if not isinstance(geo_field, PointField):
raise ValueError('Spherical distance calculation only supported on PointFields.') 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') raise ValueError('Spherical distance calculation only supported with Point Geometry parameters')
# The `function` procedure argument needs to be set differently for # The `function` procedure argument needs to be set differently for
# geodetic distance calculations. # geodetic distance calculations.

View File

@ -43,8 +43,6 @@ import sys
from binascii import a2b_hex, b2a_hex from binascii import a2b_hex, b2a_hex
from ctypes import byref, string_at, c_char_p, c_double, c_ubyte, c_void_p 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 # Getting GDAL prerequisites
from django.contrib.gis.gdal.base import GDALBase from django.contrib.gis.gdal.base import GDALBase
from django.contrib.gis.gdal.envelope import Envelope, OGREnvelope 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 HEX, unpack input to a binary buffer.
if str_instance and hex_regex.match(geom_input): 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 str_instance = False
# Constructing the geometry, # Constructing the geometry,
@ -102,7 +100,7 @@ class OGRGeometry(GDALBase):
# (e.g., 'Point', 'POLYGON'). # (e.g., 'Point', 'POLYGON').
OGRGeomType(geom_input) OGRGeomType(geom_input)
g = capi.create_geom(OGRGeomType(geom_input).num) g = capi.create_geom(OGRGeomType(geom_input).num)
elif isinstance(geom_input, memoryview): elif isinstance(geom_input, six.memoryview):
# WKB was passed in # WKB was passed in
g = capi.from_wkb(bytes(geom_input), None, byref(c_void_p()), len(geom_input)) g = capi.from_wkb(bytes(geom_input), None, byref(c_void_p()), len(geom_input))
elif isinstance(geom_input, OGRGeomType): elif isinstance(geom_input, OGRGeomType):
@ -346,7 +344,7 @@ class OGRGeometry(GDALBase):
buf = (c_ubyte * sz)() buf = (c_ubyte * sz)()
capi.to_wkb(self.ptr, byteorder, byref(buf)) capi.to_wkb(self.ptr, byteorder, byref(buf))
# Returning a buffer of the string at the pointer. # Returning a buffer of the string at the pointer.
return memoryview(string_at(buf, sz)) return six.memoryview(string_at(buf, sz))
@property @property
def wkt(self): def wkt(self):

View File

@ -1,4 +1,3 @@
from django.contrib.gis import memoryview
from django.contrib.gis.geos.geometry import GEOSGeometry, wkt_regex, hex_regex from django.contrib.gis.geos.geometry import GEOSGeometry, wkt_regex, hex_regex
from django.utils import six from django.utils import six
@ -27,7 +26,7 @@ def fromfile(file_h):
else: else:
return GEOSGeometry(buf) return GEOSGeometry(buf)
return GEOSGeometry(memoryview(buf)) return GEOSGeometry(six.memoryview(buf))
def fromstr(string, **kwargs): def fromstr(string, **kwargs):

View File

@ -7,7 +7,6 @@ from __future__ import unicode_literals
# Python, ctypes and types dependencies. # Python, ctypes and types dependencies.
from ctypes import addressof, byref, c_double from ctypes import addressof, byref, c_double
from django.contrib.gis import memoryview
# super-class for mutable list behavior # super-class for mutable list behavior
from django.contrib.gis.geos.mutable_list import ListMixin from django.contrib.gis.geos.mutable_list import ListMixin
@ -80,7 +79,7 @@ class GEOSGeometry(GEOSBase, ListMixin):
elif isinstance(geo_input, GEOM_PTR): elif isinstance(geo_input, GEOM_PTR):
# When the input is a pointer to a geometry (GEOM_PTR). # When the input is a pointer to a geometry (GEOM_PTR).
g = geo_input g = geo_input
elif isinstance(geo_input, memoryview): elif isinstance(geo_input, six.memoryview):
# When the input is a buffer (WKB). # When the input is a buffer (WKB).
g = wkb_r().read(geo_input) g = wkb_r().read(geo_input)
elif isinstance(geo_input, GEOSGeometry): elif isinstance(geo_input, GEOSGeometry):
@ -151,7 +150,7 @@ class GEOSGeometry(GEOSBase, ListMixin):
def __setstate__(self, state): def __setstate__(self, state):
# Instantiating from the tuple state that was pickled. # Instantiating from the tuple state that was pickled.
wkb, srid = state wkb, srid = state
ptr = wkb_r().read(memoryview(wkb)) ptr = wkb_r().read(six.memoryview(wkb))
if not ptr: if not ptr:
raise GEOSException('Invalid Geometry loaded from pickled state.') raise GEOSException('Invalid Geometry loaded from pickled state.')
self.ptr = ptr self.ptr = ptr

View File

@ -1,6 +1,5 @@
import threading import threading
from ctypes import byref, c_char_p, c_int, c_char, c_size_t, Structure, POINTER 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.base import GEOSBase
from django.contrib.gis.geos.libgeos import GEOM_PTR from django.contrib.gis.geos.libgeos import GEOM_PTR
from django.contrib.gis.geos.prototypes.errcheck import check_geom, check_string, check_sized_string 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): def read(self, wkb):
"Returns a _pointer_ to C GEOS Geometry object from the given 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) wkb_s = bytes(wkb)
return wkb_reader_read(self.ptr, wkb_s, len(wkb_s)) return wkb_reader_read(self.ptr, wkb_s, len(wkb_s))
elif isinstance(wkb, (bytes, six.string_types)): elif isinstance(wkb, (bytes, six.string_types)):
@ -201,7 +200,7 @@ class WKBWriter(IOBase):
def write(self, geom): def write(self, geom):
"Returns the WKB representation of the given geometry." "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): def write_hex(self, geom):
"Returns the HEXEWKB representation of the given geometry." "Returns the HEXEWKB representation of the given geometry."

View File

@ -10,7 +10,6 @@ from io import BytesIO
from django.contrib.gis.gdal import HAS_GDAL 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.contrib.gis.geometry.test_data import TestDataMixin
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes
@ -110,8 +109,8 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
self.assertEqual(True, GEOSGeometry(hexewkb_3d).hasz) self.assertEqual(True, GEOSGeometry(hexewkb_3d).hasz)
# Same for EWKB. # Same for EWKB.
self.assertEqual(memoryview(a2b_hex(hexewkb_2d)), pnt_2d.ewkb) self.assertEqual(six.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_3d)), pnt_3d.ewkb)
# Redundant sanity check. # Redundant sanity check.
self.assertEqual(4326, GEOSGeometry(hexewkb_2d).srid) self.assertEqual(4326, GEOSGeometry(hexewkb_2d).srid)
@ -132,7 +131,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
fromstr(err.wkt) fromstr(err.wkt)
# Bad WKB # Bad WKB
self.assertRaises(GEOSException, GEOSGeometry, memoryview(b'0')) self.assertRaises(GEOSException, GEOSGeometry, six.memoryview(b'0'))
class NotAGeometry(object): class NotAGeometry(object):
pass pass
@ -160,7 +159,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
def test_create_wkb(self): def test_create_wkb(self):
"Testing creation from WKB." "Testing creation from WKB."
for g in self.geometries.hex_wkt: 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) geom_h = GEOSGeometry(wkb)
# we need to do this so decimal places get normalized # we need to do this so decimal places get normalized
geom_t = fromstr(g.wkt) geom_t = fromstr(g.wkt)

View File

@ -4,7 +4,7 @@ import binascii
import unittest import unittest
from unittest import skipUnless from unittest import skipUnless
from django.contrib.gis import memoryview from django.utils.six import memoryview
from ..import HAS_GEOS from ..import HAS_GEOS