From f2a725fba3abfef28503600e2fdaab6c30482e56 Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Tue, 21 Jan 2020 22:42:12 +0500 Subject: [PATCH] Fixed #30274 -- Prevented segmentation fault on LineString iteration. This reverts commit 138a78ec8cab5e1df0c8ba2a3ee895cb756ff1ae and adds a test for the regression. --- django/contrib/gis/geos/linestring.py | 3 ++- tests/gis_tests/geos_tests/test_geos.py | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/django/contrib/gis/geos/linestring.py b/django/contrib/gis/geos/linestring.py index 85f29319e4d..a85ecfff8c7 100644 --- a/django/contrib/gis/geos/linestring.py +++ b/django/contrib/gis/geos/linestring.py @@ -91,7 +91,8 @@ class LineString(LinearGeometryMixin, GEOSGeometry): def __iter__(self): "Allow iteration over this LineString." - return iter(self._cs) + for i in range(len(self)): + yield self[i] def __len__(self): "Return the number of points in this LineString." diff --git a/tests/gis_tests/geos_tests/test_geos.py b/tests/gis_tests/geos_tests/test_geos.py index 1f326b6774d..782280e6bac 100644 --- a/tests/gis_tests/geos_tests/test_geos.py +++ b/tests/gis_tests/geos_tests/test_geos.py @@ -1434,3 +1434,12 @@ class GEOSTest(SimpleTestCase, TestDataMixin): self.assertEqual(p.transform(2774, clone=True), Point(srid=2774)) p.transform(2774) self.assertEqual(p, Point(srid=2774)) + + def test_linestring_iter(self): + ls = LineString((0, 0), (1, 1)) + it = iter(ls) + # Step into CoordSeq iterator. + next(it) + ls[:] = [] + with self.assertRaises(IndexError): + next(it)