Skip to content

Commit

Permalink
Merge pull request #10230 from dbaston/python-subgeom-parent-ref
Browse files Browse the repository at this point in the history
Python bindings: Avoid crash when using orphaned subgeometry
  • Loading branch information
rouault committed Jun 18, 2024
2 parents 5481008 + 981d223 commit 9bd1a27
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions autotest/ogr/ogr_geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -4519,3 +4519,13 @@ def test_ogr_geom_buffer_with_args():

with pytest.raises(Exception, match="Unsupported buffer option"):
geom.Buffer(1, {"QUALITY": "HIGH"})


def test_ogr_subgeom_use_after_parent_free():

g = ogr.CreateGeometryFromWkt("POLYGON ((0 0, 1 0, 1 1, 0 0))")

exterior_ring = g.GetGeometryRef(0)
del g

assert exterior_ring.GetPointCount() > 0 # does not crash
5 changes: 5 additions & 0 deletions swig/include/python/ogr_python.i
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,13 @@ def _WarnIfUserHasNotSpecifiedIfUsingExceptions():
def __iter__(self):
for i in range(self.GetGeometryCount()):
yield self.GetGeometryRef(i)
%}

%feature("pythonappend") GetGeometryRef %{
if val is not None:
val._parent_geom = self
%}

}


Expand Down

0 comments on commit 9bd1a27

Please sign in to comment.