Fixed #36246 -- Caught GDALException in BaseGeometryWidget.deserialize.

Signed-off-by: JaeHyuck Sa <wogur981208@gmail.com>
This commit is contained in:
JaeHyuck Sa
2026-01-18 02:39:05 +09:00
committed by Jacob Walls
parent 6380e3f01e
commit 7c54fee776
3 changed files with 16 additions and 6 deletions

View File

@@ -1,5 +1,4 @@
from django import forms
from django.contrib.gis.gdal import GDALException
from django.contrib.gis.geos import GEOSException, GEOSGeometry
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
@@ -41,10 +40,7 @@ class GeometryField(forms.Field):
if not isinstance(value, GEOSGeometry):
if hasattr(self.widget, "deserialize"):
try:
value = self.widget.deserialize(value)
except GDALException:
value = None
value = self.widget.deserialize(value)
else:
try:
value = GEOSGeometry(value)

View File

@@ -1,6 +1,7 @@
import logging
from django.contrib.gis import gdal
from django.contrib.gis.gdal import GDALException
from django.contrib.gis.geometry import json_regex
from django.contrib.gis.geos import GEOSException, GEOSGeometry
from django.forms.widgets import Widget
@@ -36,7 +37,7 @@ class BaseGeometryWidget(Widget):
def deserialize(self, value):
try:
return GEOSGeometry(value)
except (GEOSException, ValueError, TypeError) as err:
except (GEOSException, GDALException, ValueError, TypeError) as err:
logger.error("Error creating geometry from value '%s' (%s)", value, err)
return None

View File

@@ -494,6 +494,19 @@ class GeometryWidgetTests(SimpleTestCase):
context = widget.get_context("geometry", None, None)
self.assertEqual(context["widget"]["attrs"]["geom_name"], "Geometry")
def test_invalid_values(self):
bad_inputs = [
"POINT(5)",
"MULTI POLYGON(((0 0, 0 1, 1 1, 1 0, 0 0)))",
"BLAH(0 0, 1 1)",
'{"type": "FeatureCollection", "features": ['
'{"geometry": {"type": "Point", "coordinates": [508375, 148905]}, '
'"type": "Feature"}]}',
]
for input in bad_inputs:
with self.subTest(input=input):
self.assertIsNone(BaseGeometryWidget().deserialize(input))
def test_subwidgets(self):
widget = forms.BaseGeometryWidget()
self.assertEqual(