Skip to content

Commit

Permalink
BUG: move weakref-able implementation to base C extension type (fix P…
Browse files Browse the repository at this point in the history
…yPy) (#1577)
  • Loading branch information
jorisvandenbossche committed Oct 29, 2022
1 parent da27cdc commit 24f80f2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Expand Up @@ -58,6 +58,11 @@ jobs:
python: 3.9
geos: 3.10.3
numpy: 1.19.5
# pypy
- os: ubuntu-latest
python: "pypy3.8"
geos: 3.11.0
numpy: 1.23.3

env:
GEOS_VERSION: ${{ matrix.geos }}
Expand Down
2 changes: 1 addition & 1 deletion shapely/geometry/base.py
Expand Up @@ -81,7 +81,7 @@ class BaseGeometry(shapely.Geometry):
"""

__slots__ = ["__weakref__"]
__slots__ = []

def __new__(self):
warn(
Expand Down
5 changes: 5 additions & 0 deletions shapely/tests/geometry/test_geometry_base.py
@@ -1,3 +1,4 @@
import platform
import weakref

import numpy as np
Expand Down Expand Up @@ -45,6 +46,10 @@ def test_geometry_collection():
]


@pytest.mark.skipif(
platform.python_implementation() == "PyPy",
reason="Setting custom attributes doesn't fail on PyPy",
)
@pytest.mark.parametrize("geom", geometries_all_types)
def test_setattr_disallowed(geom):
with pytest.raises(AttributeError):
Expand Down
5 changes: 5 additions & 0 deletions src/pygeom.c
Expand Up @@ -38,11 +38,15 @@ PyObject* GeometryObject_FromGEOS(GEOSGeometry* ptr, GEOSContextHandle_t ctx) {
} else {
self->ptr = ptr;
self->ptr_prepared = NULL;
self->weakreflist = (PyObject *)NULL;
return (PyObject*)self;
}
}

static void GeometryObject_dealloc(GeometryObject* self) {
if (self->weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *)self);
}
if (self->ptr != NULL) {
// not using GEOS_INIT, but using global context instead
GEOSContextHandle_t ctx = geos_context[0];
Expand Down Expand Up @@ -396,6 +400,7 @@ PyTypeObject GeometryType = {
.tp_repr = (reprfunc)GeometryObject_repr,
.tp_hash = (hashfunc)GeometryObject_hash,
.tp_richcompare = (richcmpfunc)GeometryObject_richcompare,
.tp_weaklistoffset = offsetof(GeometryObject, weakreflist),
.tp_str = (reprfunc)GeometryObject_str,
};

Expand Down
5 changes: 4 additions & 1 deletion src/pygeom.h
Expand Up @@ -6,8 +6,11 @@
#include "geos.h"

typedef struct {
PyObject_HEAD void* ptr;
PyObject_HEAD
void* ptr;
void* ptr_prepared;
/* For weak references */
PyObject *weakreflist;
} GeometryObject;

extern PyTypeObject GeometryType;
Expand Down

0 comments on commit 24f80f2

Please sign in to comment.