Skip to content

Commit 1996512

Browse files
Add Ligo Segments (emscripten-forge#5344)
* Add Ligo Segments * update test * update test --------- Co-authored-by: Thorsten Beier <thorsten.beier@quantstack.net>
1 parent c56aa4f commit 1996512

5 files changed

Lines changed: 229 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
From c5579b37854025d662cf985832ea5763712cdbfe Mon Sep 17 00:00:00 2001
2+
From: "duncan.macleod" <duncan.macleod@ligo.org>
3+
Date: Thu, 13 Apr 2023 11:57:53 +0100
4+
Subject: [PATCH 1/5] use PEP-420 implicit namespace packages
5+
6+
---
7+
ligo/__init__.py | 1 -
8+
setup.py | 3 +--
9+
2 files changed, 1 insertion(+), 3 deletions(-)
10+
delete mode 100644 ligo/__init__.py
11+
12+
diff --git a/ligo/__init__.py b/ligo/__init__.py
13+
deleted file mode 100644
14+
index de40ea7..0000000
15+
--- a/ligo/__init__.py
16+
+++ /dev/null
17+
@@ -1 +0,0 @@
18+
-__import__('pkg_resources').declare_namespace(__name__)
19+
diff --git a/setup.py b/setup.py
20+
index 48e79f0..ce38467 100644
21+
--- a/setup.py
22+
+++ b/setup.py
23+
@@ -57,8 +57,7 @@ setup(
24+
author = 'Kipp Cannon',
25+
author_email = 'kipp.cannon@ligo.org',
26+
license = 'GPLv3',
27+
- packages = ['ligo', 'ligo.segments'],
28+
- namespace_packages = ['ligo'],
29+
+ packages = ['ligo.segments'],
30+
cmdclass = cmdclass,
31+
setup_requires = setup_requires,
32+
install_requires = ['six'],
33+
--
34+
2.39.5
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
From 4b450a63436a6e979f8ef80bd8edce52617d43ee Mon Sep 17 00:00:00 2001
2+
From: Leo Singer <leo.singer@ligo.org>
3+
Date: Tue, 16 Jul 2024 16:00:17 -0400
4+
Subject: [PATCH 4/5] Initialize PyTypeObjects with PyVarObject_HEAD_INIT
5+
MIME-Version: 1.0
6+
Content-Type: text/plain; charset=UTF-8
7+
Content-Transfer-Encoding: 8bit
8+
9+
Fixes #20.
10+
---
11+
src/infinity.c | 2 +-
12+
src/segment.c | 2 +-
13+
src/segmentlist.c | 2 +-
14+
3 files changed, 3 insertions(+), 3 deletions(-)
15+
16+
diff --git a/src/infinity.c b/src/infinity.c
17+
index d85ea13..87ceb23 100644
18+
--- a/src/infinity.c
19+
+++ b/src/infinity.c
20+
@@ -268,7 +268,7 @@ static struct PyMethodDef methods[] = {
21+
22+
23+
PyTypeObject segments_Infinity_Type = {
24+
- PyObject_HEAD_INIT(NULL)
25+
+ PyVarObject_HEAD_INIT(NULL, 0)
26+
.tp_as_number = &as_number,
27+
.tp_basicsize = sizeof(segments_Infinity),
28+
.tp_doc =
29+
diff --git a/src/segment.c b/src/segment.c
30+
index cc9a418..1f373c8 100644
31+
--- a/src/segment.c
32+
+++ b/src/segment.c
33+
@@ -480,7 +480,7 @@ static struct PyMethodDef methods[] = {
34+
35+
36+
PyTypeObject segments_Segment_Type = {
37+
- PyObject_HEAD_INIT(NULL)
38+
+ PyVarObject_HEAD_INIT(NULL, 0)
39+
.tp_as_number = &as_number,
40+
.tp_as_sequence = &as_sequence,
41+
.tp_doc =
42+
diff --git a/src/segmentlist.c b/src/segmentlist.c
43+
index 98b6b76..f666487 100644
44+
--- a/src/segmentlist.c
45+
+++ b/src/segmentlist.c
46+
@@ -1540,7 +1540,7 @@ static struct PyMethodDef methods[] = {
47+
48+
49+
PyTypeObject segments_SegmentList_Type = {
50+
- PyObject_HEAD_INIT(NULL)
51+
+ PyVarObject_HEAD_INIT(NULL, 0)
52+
.tp_as_number = &as_number,
53+
.tp_as_sequence = &as_sequence,
54+
.tp_doc =
55+
--
56+
2.39.5
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
From f211a1f6e0352ba96e720be619fb903f41d287db Mon Sep 17 00:00:00 2001
2+
From: Leo Singer <leo.singer@ligo.org>
3+
Date: Mon, 7 Oct 2024 07:53:58 -0400
4+
Subject: [PATCH 5/5] Fix build for Python 3.13
5+
6+
Use PyList_Extend for Python >= 3.13, and provide replacement
7+
using _PyList_Extend for Python < 3.13. In Python 3.13, the
8+
function _PyList_Extend was removed and the public API method
9+
PyList_Extend was added in its place.
10+
11+
Fixes #21.
12+
---
13+
src/segmentlist.c | 10 ++++++----
14+
1 file changed, 6 insertions(+), 4 deletions(-)
15+
16+
diff --git a/src/segmentlist.c b/src/segmentlist.c
17+
index f666487..233775d 100644
18+
--- a/src/segmentlist.c
19+
+++ b/src/segmentlist.c
20+
@@ -240,7 +240,8 @@ static PyObject *make_segment(PyObject *lo, PyObject *hi)
21+
}
22+
23+
24+
-static int pylist_extend(PyListObject *l, PyObject *v)
25+
+#if PY_VERSION_HEX < 0x030D0000
26+
+static int PyList_Extend(PyListObject *l, PyObject *v)
27+
{
28+
if(!PyList_Check(l)) {
29+
PyErr_SetObject(PyExc_TypeError, (PyObject *) l);
30+
@@ -252,6 +253,7 @@ static int pylist_extend(PyListObject *l, PyObject *v)
31+
Py_DECREF(result);
32+
return 0;
33+
}
34+
+#endif
35+
36+
37+
static PyListObject *segments_SegmentList_New(PyTypeObject *type, PyObject *sequence)
38+
@@ -263,7 +265,7 @@ static PyListObject *segments_SegmentList_New(PyTypeObject *type, PyObject *sequ
39+
}
40+
new = (PyListObject *) type->tp_alloc(type, 0);
41+
if(new && sequence)
42+
- if(pylist_extend(new, sequence)) {
43+
+ if(PyList_Extend(new, sequence)) {
44+
Py_DECREF(new);
45+
new = NULL;
46+
}
47+
@@ -817,7 +819,7 @@ static PyObject *__ior__(PyObject *self, PyObject *other)
48+
/* Faster algorithm when the two lists have very different sizes.
49+
* OK to not test size functions for error return values */
50+
if(PySequence_Size(other) > PyList_GET_SIZE(self) / 2) {
51+
- if(pylist_extend((PyListObject *) self, other))
52+
+ if(PyList_Extend((PyListObject *) self, other))
53+
return NULL;
54+
return PyObject_CallMethod(self, "coalesce", NULL);
55+
}
56+
@@ -988,7 +990,7 @@ static PyObject *__xor__(PyObject *self, PyObject *other)
57+
Py_XDECREF(other);
58+
return NULL;
59+
}
60+
- if(pylist_extend((PyListObject *) new, other)) {
61+
+ if(PyList_Extend((PyListObject *) new, other)) {
62+
Py_DECREF(new);
63+
Py_DECREF(other);
64+
return NULL;
65+
--
66+
2.39.5
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
context:
2+
name: ligo-segments
3+
version: 1.4.0
4+
5+
package:
6+
name: ${{ name }}
7+
version: ${{ version }}
8+
9+
source:
10+
url: https://pypi.org/packages/source/${{ name[0] }}/${{ name }}/${{ name }}-${{ version }}.tar.gz
11+
sha256: e072a844713c5b02efdcaf5bfe4c3a8cd9ef225b08cfd3202a4e185e0f71f5dc
12+
patches:
13+
- 0001-use-PEP-420-implicit-namespace-packages.patch
14+
- 0004-Initialize-PyTypeObjects-with-PyVarObject_HEAD_INIT.patch
15+
- 0005-Fix-build-for-Python-3.13.patch
16+
17+
build:
18+
number: 0
19+
script: ${{ PYTHON }} -m pip install . -vv
20+
21+
files:
22+
exclude:
23+
- '**/__pycache__/**'
24+
- '**/*.pyc'
25+
python:
26+
skip_pyc_compilation:
27+
- '**/*.py'
28+
29+
requirements:
30+
build:
31+
- ${{ compiler('c') }}
32+
- cross-python_emscripten-wasm32
33+
- python
34+
host:
35+
- pip
36+
- python
37+
- setuptools
38+
run:
39+
- python
40+
- six
41+
42+
tests:
43+
- script: pytester
44+
files:
45+
recipe:
46+
- test_ligo_segments.py
47+
requirements:
48+
build:
49+
- pytester
50+
run:
51+
- pytester-run
52+
53+
about:
54+
homepage: https://git.ligo.org/lscsoft/ligo-segments/
55+
license: GPL-3.0-or-later
56+
license_file: LICENSE
57+
summary: Representations of semi-open intervals
58+
59+
extra:
60+
recipe-maintainers:
61+
- arjxn-py
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from ligo.segments import segment
2+
3+
4+
def test_ligo_segments():
5+
a = segment(1, 2)
6+
b = segment(2, 3)
7+
c = segment(5, 6)
8+
9+
assert a.connects(b)
10+
assert a in (a + b)
11+
assert (a + b).intersects(b)
12+
assert a.disjoint(c)

0 commit comments

Comments
 (0)