From 0516ac3d28a7ac8b756ada0e6f89473c7a6e0474 Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Sat, 10 Sep 2011 00:29:34 +0000 Subject: [PATCH] Fixed #13670 -- Comparisons with the spatial adapter won't blow up in some corner cases. Thanks, milosu for the bug report and jpaulett for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16757 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/gis/db/backends/adapter.py | 2 ++ django/contrib/gis/db/backends/postgis/adapter.py | 2 ++ django/contrib/gis/tests/geoapp/test_regress.py | 12 +++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/django/contrib/gis/db/backends/adapter.py b/django/contrib/gis/db/backends/adapter.py index 9766ef08c9..ca77124351 100644 --- a/django/contrib/gis/db/backends/adapter.py +++ b/django/contrib/gis/db/backends/adapter.py @@ -8,6 +8,8 @@ class WKTAdapter(object): self.srid = geom.srid def __eq__(self, other): + if not isinstance(other, WKTAdapter): + return False return self.wkt == other.wkt and self.srid == other.srid def __str__(self): diff --git a/django/contrib/gis/db/backends/postgis/adapter.py b/django/contrib/gis/db/backends/postgis/adapter.py index 3f8603e50a..c9b9841d55 100644 --- a/django/contrib/gis/db/backends/postgis/adapter.py +++ b/django/contrib/gis/db/backends/postgis/adapter.py @@ -21,6 +21,8 @@ class PostGISAdapter(object): raise Exception('Error implementing psycopg2 protocol. Is psycopg2 installed?') def __eq__(self, other): + if not isinstance(other, PostGISAdapter): + return False return (self.ewkb == other.ewkb) and (self.srid == other.srid) def __str__(self): diff --git a/django/contrib/gis/tests/geoapp/test_regress.py b/django/contrib/gis/tests/geoapp/test_regress.py index 02b1ff08eb..45070b0c93 100644 --- a/django/contrib/gis/tests/geoapp/test_regress.py +++ b/django/contrib/gis/tests/geoapp/test_regress.py @@ -2,7 +2,7 @@ from datetime import datetime from django.contrib.gis.tests.utils import no_mysql, no_spatialite from django.contrib.gis.shortcuts import render_to_kmz from django.test import TestCase -from models import City, PennsylvaniaCity +from models import City, PennsylvaniaCity, State class GeoRegressionTests(TestCase): @@ -43,3 +43,13 @@ class GeoRegressionTests(TestCase): mansfield = PennsylvaniaCity.objects.create(name='Mansfield', county='Tioga', point='POINT(-77.071445 41.823881)', founded=founded) self.assertEqual(founded, PennsylvaniaCity.objects.dates('founded', 'day')[0]) + + def test05_empty_count(self): + "Testing that PostGISAdapter.__eq__ does check empty strings, see #13670" + # contrived example, but need a geo lookup paired with an id__in lookup + pueblo = City.objects.get(name='Pueblo') + state = State.objects.filter(poly__contains=pueblo.point) + cities_within_state = City.objects.filter(id__in=state) + + # .count() should not throw TypeError in __eq__ + self.assertEqual(cities_within_state.count(), 1)