Skip to content

Commit

Permalink
[CI] Add pre-commit hook ClangFormat; format C files with style `Go…
Browse files Browse the repository at this point in the history
  • Loading branch information
jbampton committed Oct 13, 2024
1 parent c6d7969 commit b821e4b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 47 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ repos:
hooks:
- id: isort
name: isort (python)
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v19.1.1
hooks:
- id: clang-format
args: [--style=Google]
types_or: [c]
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
hooks:
Expand Down
52 changes: 28 additions & 24 deletions python/src/geomserde_speedup_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,33 +192,37 @@ 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; // Argument parsing failed; error already set by PyArg_ParseTuple
}
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) {
// 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;
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) {
Expand Down
55 changes: 32 additions & 23 deletions python/src/pygeos/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,28 @@
#define _PYGEOS_API_H

#include <Python.h>

#include "../geos_c_dyn.h"

/* PyObject* PyGEOS_CreateGeometry(GEOSGeometry *ptr, GEOSContextHandle_t ctx) */
/* PyObject* PyGEOS_CreateGeometry(GEOSGeometry *ptr, GEOSContextHandle_t ctx)
*/
#define PyGEOS_CreateGeometry_NUM 0
#define PyGEOS_CreateGeometry_RETURN PyObject *
#define PyGEOS_CreateGeometry_PROTO (GEOSGeometry * ptr, GEOSContextHandle_t ctx)
#define PyGEOS_CreateGeometry_PROTO \
(GEOSGeometry * ptr, GEOSContextHandle_t ctx)

/* char PyGEOS_GetGEOSGeometry(GeometryObject *obj, GEOSGeometry **out) */
#define PyGEOS_GetGEOSGeometry_NUM 1
#define PyGEOS_GetGEOSGeometry_RETURN char
#define PyGEOS_GetGEOSGeometry_PROTO (PyObject * obj, GEOSGeometry * *out)

/* GEOSCoordSequence* PyGEOS_CoordSeq_FromBuffer(GEOSContextHandle_t ctx, const double* buf,
unsigned int size, unsigned int dims, char ring_closure)*/
/* GEOSCoordSequence* PyGEOS_CoordSeq_FromBuffer(GEOSContextHandle_t ctx, const
double* buf, unsigned int size, unsigned int dims, char ring_closure)*/
#define PyGEOS_CoordSeq_FromBuffer_NUM 2
#define PyGEOS_CoordSeq_FromBuffer_RETURN GEOSCoordSequence*
#define PyGEOS_CoordSeq_FromBuffer_PROTO (GEOSContextHandle_t ctx, const double* buf, unsigned int size, unsigned int dims, char ring_closure)
#define PyGEOS_CoordSeq_FromBuffer_RETURN GEOSCoordSequence *
#define PyGEOS_CoordSeq_FromBuffer_PROTO \
(GEOSContextHandle_t ctx, const double *buf, unsigned int size, \
unsigned int dims, char ring_closure)

/* Total number of C API pointers */
#define PyGEOS_API_num_pointers 3
Expand All @@ -64,39 +69,43 @@
* Each API function needs to provide a corresponding *_PROTO here.
*/

extern PyGEOS_CreateGeometry_RETURN PyGEOS_CreateGeometry PyGEOS_CreateGeometry_PROTO;
extern PyGEOS_GetGEOSGeometry_RETURN PyGEOS_GetGEOSGeometry PyGEOS_GetGEOSGeometry_PROTO;
extern PyGEOS_CoordSeq_FromBuffer_RETURN PyGEOS_CoordSeq_FromBuffer PyGEOS_CoordSeq_FromBuffer_PROTO;
extern PyGEOS_CreateGeometry_RETURN PyGEOS_CreateGeometry
PyGEOS_CreateGeometry_PROTO;
extern PyGEOS_GetGEOSGeometry_RETURN PyGEOS_GetGEOSGeometry
PyGEOS_GetGEOSGeometry_PROTO;
extern PyGEOS_CoordSeq_FromBuffer_RETURN PyGEOS_CoordSeq_FromBuffer
PyGEOS_CoordSeq_FromBuffer_PROTO;

#else
/* This section is used in modules that use the PyGEOS C API
* Each API function needs to provide the lookup into PyGEOS_API as a
* define statement.
*/
*/

static void **PyGEOS_API;

#define PyGEOS_CreateGeometry \
(*(PyGEOS_CreateGeometry_RETURN(*) PyGEOS_CreateGeometry_PROTO)PyGEOS_API[PyGEOS_CreateGeometry_NUM])
#define PyGEOS_CreateGeometry \
(*(PyGEOS_CreateGeometry_RETURN(*) \
PyGEOS_CreateGeometry_PROTO)PyGEOS_API[PyGEOS_CreateGeometry_NUM])

#define PyGEOS_GetGEOSGeometry \
(*(PyGEOS_GetGEOSGeometry_RETURN(*) PyGEOS_GetGEOSGeometry_PROTO)PyGEOS_API[PyGEOS_GetGEOSGeometry_NUM])
#define PyGEOS_GetGEOSGeometry \
(*(PyGEOS_GetGEOSGeometry_RETURN(*) \
PyGEOS_GetGEOSGeometry_PROTO)PyGEOS_API[PyGEOS_GetGEOSGeometry_NUM])

#define PyGEOS_CoordSeq_FromBuffer \
(*(PyGEOS_CoordSeq_FromBuffer_RETURN(*) PyGEOS_CoordSeq_FromBuffer_PROTO)PyGEOS_API[PyGEOS_CoordSeq_FromBuffer_NUM])
#define PyGEOS_CoordSeq_FromBuffer \
(*(PyGEOS_CoordSeq_FromBuffer_RETURN(*) PyGEOS_CoordSeq_FromBuffer_PROTO) \
PyGEOS_API[PyGEOS_CoordSeq_FromBuffer_NUM])

/* Dynamically load C API from PyCapsule.
* This MUST be called prior to using C API functions in other modules; otherwise
* segfaults will occur when the PyGEOS C API functions are called.
* This MUST be called prior to using C API functions in other modules;
* otherwise segfaults will occur when the PyGEOS C API functions are called.
*
* Returns 0 on success, -1 if error.
* PyCapsule_Import will set an exception on error.
*/
static int
import_shapely_c_api(void)
{
PyGEOS_API = (void **)PyCapsule_Import("shapely.lib._C_API", 0);
return (PyGEOS_API == NULL) ? -1 : 0;
static int import_shapely_c_api(void) {
PyGEOS_API = (void **)PyCapsule_Import("shapely.lib._C_API", 0);
return (PyGEOS_API == NULL) ? -1 : 0;
}

#endif
Expand Down

0 comments on commit b821e4b

Please sign in to comment.