Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release GIL for is_geometry, is_missing and is_valid_input #207

Merged
merged 4 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Version 0.9 (unreleased)

**Highlights of this release**

* Release the GIL for ``is_geometry()``, ``is_missing()``, and
``is_valid_input()`` (#207)
* Addition of a ``is_ccw()`` function for GEOS >= 3.7 (#201)
* Added support for pickling to ``Geometry`` objects (#190)
* Limited the length of geometry repr to 80 characters (#189)
Expand Down
6 changes: 3 additions & 3 deletions pygeos/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def is_empty(geometry, **kwargs):
"""
return lib.is_empty(geometry, **kwargs)


@multithreading_enabled
def is_geometry(geometry, **kwargs):
"""Returns True if the object is a geometry

Expand All @@ -159,7 +159,7 @@ def is_geometry(geometry, **kwargs):
"""
return lib.is_geometry(geometry, **kwargs)


@multithreading_enabled
def is_missing(geometry, **kwargs):
"""Returns True if the object is not a geometry (None)

Expand All @@ -186,7 +186,7 @@ def is_missing(geometry, **kwargs):
"""
return lib.is_missing(geometry, **kwargs)


@multithreading_enabled
def is_valid_input(geometry, **kwargs):
"""Returns True if the object is a geometry or None

Expand Down
8 changes: 8 additions & 0 deletions pygeos/test/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,11 @@ def test_subclasses(with_point_in_registry):
assert isinstance(point, Point)
assert pygeos.get_type_id(point) == pygeos.GeometryType.POINT
assert point.x == 1


def test_subclass_is_geometry(with_point_in_registry):
assert pygeos.is_geometry(Point("POINT (1 1)"))


def test_subclass_is_valid_input(with_point_in_registry):
assert pygeos.is_valid_input(Point("POINT (1 1)"))
19 changes: 14 additions & 5 deletions src/ufuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,24 +165,33 @@ static PyUFuncGenericFunction Y_b_funcs[1] = {&Y_b_func};

/* Define the object -> bool functions (O_b) which do not raise on non-geom objects*/
static char IsMissing(void* context, PyObject* obj) {
return ((PyObject*)obj == Py_None);
GEOSGeometry* g = NULL;
if (!get_geom((GeometryObject *) obj, &g)) {
return 0;
};
return g == NULL; // get_geom sets g to NULL for None input
}
static void* is_missing_data[1] = {IsMissing};
static char IsGeometry(void* context, PyObject* obj) {
return (PyObject_IsInstance(obj, (PyObject*)&GeometryType));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the reason it worked before as well is that IsInstance should work for subclasses?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, indeed, this PyObject_IsInstance recurses through the MRO.

GEOSGeometry* g = NULL;
if (!get_geom((GeometryObject *) obj, &g)) {
return 0;
}
return g != NULL;
}
static void* is_geometry_data[1] = {IsGeometry};
static char IsValidInput(void* context, PyObject* obj) {
return (IsGeometry(context, obj) | IsMissing(context, obj));
GEOSGeometry* g = NULL;
return get_geom((GeometryObject *) obj, &g);
}
static void* is_valid_input_data[1] = {IsValidInput};
typedef char FuncGEOS_O_b(void* context, PyObject* obj);
static char O_b_dtypes[2] = {NPY_OBJECT, NPY_BOOL};
static void O_b_func(char** args, npy_intp* dimensions, npy_intp* steps, void* data) {
FuncGEOS_O_b* func = (FuncGEOS_O_b*)data;
GEOS_INIT;
GEOS_INIT_THREADS;
UNARY_LOOP { *(npy_bool*)op1 = func(ctx, *(PyObject**)ip1); }
GEOS_FINISH;
GEOS_FINISH_THREADS;
}
static PyUFuncGenericFunction O_b_funcs[1] = {&O_b_func};

Expand Down