From b536ad09cadc087b7559db2f8cca50f06c6e8f34 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Tue, 24 Dec 2013 15:19:25 +0100 Subject: [PATCH] [1.6.x] Fixed #21662 -- Kept parent reference in prepared geometry Thanks Robert Scott for the report. Backport of 542198c1d0 from master. --- django/contrib/gis/geos/prepared.py | 7 ++++++- django/contrib/gis/geos/tests/test_geos.py | 4 ++++ docs/releases/1.6.2.txt | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/django/contrib/gis/geos/prepared.py b/django/contrib/gis/geos/prepared.py index 68b812df8aa..ae2548ff983 100644 --- a/django/contrib/gis/geos/prepared.py +++ b/django/contrib/gis/geos/prepared.py @@ -11,7 +11,12 @@ class PreparedGeometry(GEOSBase): ptr_type = capi.PREPGEOM_PTR def __init__(self, geom): - if not isinstance(geom, GEOSGeometry): raise TypeError + # Keeping a reference to the original geometry object to prevent it + # from being garbage collected which could then crash the prepared one + # See #21662 + self._base_geom = geom + if not isinstance(geom, GEOSGeometry): + raise TypeError self.ptr = capi.geos_prepare(geom.ptr) def __del__(self): diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py index c7fe5b23213..c8e064bb956 100644 --- a/django/contrib/gis/geos/tests/test_geos.py +++ b/django/contrib/gis/geos/tests/test_geos.py @@ -1043,6 +1043,10 @@ class GEOSTest(unittest.TestCase, TestDataMixin): self.assertEqual(mpoly.intersects(pnt), prep.intersects(pnt)) self.assertEqual(c, prep.covers(pnt)) + # Original geometry deletion should not crash the prepared one (#21662) + del mpoly + self.assertTrue(prep.covers(Point(5, 5))) + def test_line_merge(self): "Testing line merge support" ref_geoms = (fromstr('LINESTRING(1 1, 1 1, 3 3)'), diff --git a/docs/releases/1.6.2.txt b/docs/releases/1.6.2.txt index cae18b86fa4..fa8bd5bc938 100644 --- a/docs/releases/1.6.2.txt +++ b/docs/releases/1.6.2.txt @@ -9,4 +9,5 @@ This is Django 1.6.2, a bugfix release for Django 1.6. Bug fixes ========= -... +* Prevented the base geometry object of a prepared geometry to be garbage + collected, which could lead to crash Django (#21662).