Kaydet (Commit) cc9e4297 authored tarafından Claude Paroz's avatar Claude Paroz

Fixed #27557 -- Casted GEOSGeometry only when necessary

Thanks Pete Flugstad for the report, and Tim Graham for the review.
üst 4464b9b9
...@@ -95,13 +95,15 @@ class GEOSGeometry(GEOSBase, ListMixin): ...@@ -95,13 +95,15 @@ class GEOSGeometry(GEOSBase, ListMixin):
self.srid = srid self.srid = srid
# Setting the class type (e.g., Point, Polygon, etc.) # Setting the class type (e.g., Point, Polygon, etc.)
if type(self) == GEOSGeometry:
if GEOSGeometry._GEOS_CLASSES is None: if GEOSGeometry._GEOS_CLASSES is None:
# Lazy-loaded variable to avoid import conflicts with GEOSGeometry. # Lazy-loaded variable to avoid import conflicts with GEOSGeometry.
from .linestring import LineString, LinearRing from .linestring import LineString, LinearRing
from .point import Point from .point import Point
from .polygon import Polygon from .polygon import Polygon
from .collections import ( from .collections import (
GeometryCollection, MultiPoint, MultiLineString, MultiPolygon) GeometryCollection, MultiPoint, MultiLineString, MultiPolygon,
)
GEOSGeometry._GEOS_CLASSES = { GEOSGeometry._GEOS_CLASSES = {
0: Point, 0: Point,
1: LineString, 1: LineString,
......
...@@ -1308,6 +1308,25 @@ class GEOSTest(SimpleTestCase, TestDataMixin): ...@@ -1308,6 +1308,25 @@ class GEOSTest(SimpleTestCase, TestDataMixin):
self.assertEqual(args, (Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly)) self.assertEqual(args, (Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly))
self.assertEqual(kwargs, {}) self.assertEqual(kwargs, {})
def test_subclassing(self):
"""
GEOSGeometry subclass may itself be subclassed without being forced-cast
to the parent class during `__init__`.
"""
class ExtendedPolygon(Polygon):
def __init__(self, *args, **kwargs):
data = kwargs.pop('data', 0)
super(ExtendedPolygon, self).__init__(*args, **kwargs)
self._data = data
def __str__(self):
return "EXT_POLYGON - data: %d - %s" % (self._data, self.wkt)
ext_poly = ExtendedPolygon(((0, 0), (0, 1), (1, 1), (0, 0)), data=3)
self.assertEqual(type(ext_poly), ExtendedPolygon)
# ExtendedPolygon.__str__ should be called (instead of Polygon.__str__).
self.assertEqual(str(ext_poly), "EXT_POLYGON - data: 3 - POLYGON ((0 0, 0 1, 1 1, 0 0))")
def test_geos_version(self): def test_geos_version(self):
"""Testing the GEOS version regular expression.""" """Testing the GEOS version regular expression."""
from django.contrib.gis.geos.libgeos import version_regex from django.contrib.gis.geos.libgeos import version_regex
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment