This is an automated email from the ASF dual-hosted git repository.

jiayu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sedona.git


The following commit(s) were added to refs/heads/master by this push:
     new 9b2ebac24 [SEDONA-512] Geometry_serde.serialize should report the 
incorrect type (#1271)
9b2ebac24 is described below

commit 9b2ebac2453cefbaf7ceea0e978fcf2e64b138a7
Author: Jia Yu <[email protected]>
AuthorDate: Wed Mar 6 23:11:50 2024 -0800

    [SEDONA-512] Geometry_serde.serialize should report the incorrect type 
(#1271)
    
    Co-authored-by: Ryan Avery <[email protected]>
---
 python/src/geomserde_speedup_module.c | 38 +++++++++++++++++++++++------------
 1 file changed, 25 insertions(+), 13 deletions(-)

diff --git a/python/src/geomserde_speedup_module.c 
b/python/src/geomserde_speedup_module.c
index e49efb4f2..2edf4b7b3 100644
--- a/python/src/geomserde_speedup_module.c
+++ b/python/src/geomserde_speedup_module.c
@@ -192,21 +192,33 @@ static GEOSGeometry *do_deserialize(PyObject *args,
 /* serialize/deserialize functions for Shapely 2.x */
 
 static PyObject *serialize(PyObject *self, PyObject *args) {
-  PyObject *pygeos_geom = NULL;
-  if (!PyArg_ParseTuple(args, "O", &pygeos_geom)) {
-    return NULL;
-  }
+    PyObject *pygeos_geom = NULL;
+    if (!PyArg_ParseTuple(args, "O", &pygeos_geom)) {
+        return NULL;  // Argument parsing failed; error already set by 
PyArg_ParseTuple
+    }
 
-  GEOSGeometry *geos_geom = NULL;
-  char success = PyGEOS_GetGEOSGeometry(pygeos_geom, &geos_geom);
-  if (success == 0) {
-    PyErr_SetString(
-        PyExc_TypeError,
-        "Argument is of incorrect type. Please provide only Geometry 
objects.");
-    return NULL;
-  }
+    GEOSGeometry *geos_geom = NULL;
+    char success = PyGEOS_GetGEOSGeometry(pygeos_geom, &geos_geom);
+    if (success == 0) {
+        // Retrieve the type of the supplied object
+        PyObject *type = (PyObject *)Py_TYPE(pygeos_geom);
+        PyObject *type_name = PyObject_GetAttrString(type, "__name__");
+        if (type_name == NULL) {
+            // Fallback error if we can't get the type name
+            PyErr_SetString(PyExc_TypeError, "Argument is of incorrect type.");
+        } else {
+            // Construct the error message with the type name
+            const char *type_str = PyUnicode_AsUTF8(type_name);
+            char error_msg[256];
+            snprintf(error_msg, sizeof(error_msg), "Argument is of incorrect 
type: '%s'. Please provide only Geometry objects.", type_str);
+
+            PyErr_SetString(PyExc_TypeError, error_msg);
+            Py_DECREF(type_name);  // Cleanup the reference to type_name
+        }
+        return NULL;
+    }
 
-  return do_serialize(geos_geom);
+    return do_serialize(geos_geom);
 }
 
 static PyObject *deserialize(PyObject *self, PyObject *args) {

Reply via email to