summaryrefslogtreecommitdiff
path: root/devel/py-optree/files/patch-i386
blob: 3bb59348376e45908af28cfb7ff6147a44d861dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
Obtained from:	https://github.com/metaopt/optree/commit/9a613a705ee18e18784ba3a16b2786f56f9c74d2 (based on)

--- include/treespec.h.orig	2024-03-25 18:01:43 UTC
+++ include/treespec.h
@@ -19,6 +19,7 @@ limitations under the License.
 
 #include <pybind11/pybind11.h>
 
+#include <algorithm>      // std::min
 #include <memory>         // std::unique_ptr
 #include <optional>       // std::optional, std::nullopt
 #include <string>         // std::string
@@ -38,7 +39,10 @@ using ssize_t = py::ssize_t;
 using ssize_t = py::ssize_t;
 
 // The maximum depth of a pytree.
-constexpr ssize_t MAX_RECURSION_DEPTH = 1000;
+#ifndef Py_C_RECURSION_LIMIT
+#define Py_C_RECURSION_LIMIT 1000
+#endif
+constexpr ssize_t MAX_RECURSION_DEPTH = std::min(1000, Py_C_RECURSION_LIMIT);
 
 // Test whether the given object is a leaf node.
 bool IsLeaf(const py::object &object,
--- include/utils.h.orig	2024-03-25 18:01:43 UTC
+++ include/utils.h
@@ -208,6 +208,9 @@ inline ssize_t GET_SIZE<py::list>(const py::handle& si
 inline ssize_t GET_SIZE<py::list>(const py::handle& sized) {
     return PyList_GET_SIZE(sized.ptr());
 }
+#ifndef PyDict_GET_SIZE
+#define PyDict_GET_SIZE PyDict_GetSize
+#endif
 template <>
 inline ssize_t GET_SIZE<py::dict>(const py::handle& sized) {
     return PyDict_GET_SIZE(sized.ptr());
@@ -222,91 +225,35 @@ template <>
     return PyTuple_GET_ITEM(container.ptr(), item);
 }
 template <>
-inline py::handle GET_ITEM_HANDLE<py::tuple>(const py::handle& container, const py::size_t& item) {
-    return PyTuple_GET_ITEM(container.ptr(), py::ssize_t_cast(item));
-}
-template <>
-inline py::handle GET_ITEM_HANDLE<py::tuple>(const py::handle& container, const int& item) {
-    return PyTuple_GET_ITEM(container.ptr(), py::ssize_t_cast(item));
-}
-template <>
 inline py::handle GET_ITEM_HANDLE<py::list>(const py::handle& container, const py::ssize_t& item) {
     return PyList_GET_ITEM(container.ptr(), item);
 }
-template <>
-inline py::handle GET_ITEM_HANDLE<py::list>(const py::handle& container, const py::size_t& item) {
-    return PyList_GET_ITEM(container.ptr(), py::ssize_t_cast(item));
-}
-template <>
-inline py::handle GET_ITEM_HANDLE<py::list>(const py::handle& container, const int& item) {
-    return PyList_GET_ITEM(container.ptr(), py::ssize_t_cast(item));
-}
 
 template <typename Container, typename Item>
 inline py::object GET_ITEM_BORROW(const py::handle& container, const Item& item) {
-    return container[item];
+    return py::reinterpret_borrow<py::object>(container[item]);
 }
 template <>
 inline py::object GET_ITEM_BORROW<py::tuple>(const py::handle& container, const py::ssize_t& item) {
     return py::reinterpret_borrow<py::object>(PyTuple_GET_ITEM(container.ptr(), item));
 }
 template <>
-inline py::object GET_ITEM_BORROW<py::tuple>(const py::handle& container, const py::size_t& item) {
-    return py::reinterpret_borrow<py::object>(
-        PyTuple_GET_ITEM(container.ptr(), py::ssize_t_cast(item)));
-}
-template <>
-inline py::object GET_ITEM_BORROW<py::tuple>(const py::handle& container, const int& item) {
-    return py::reinterpret_borrow<py::object>(
-        PyTuple_GET_ITEM(container.ptr(), py::ssize_t_cast(item)));
-}
-template <>
 inline py::object GET_ITEM_BORROW<py::list>(const py::handle& container, const py::ssize_t& item) {
     return py::reinterpret_borrow<py::object>(PyList_GET_ITEM(container.ptr(), item));
 }
-template <>
-inline py::object GET_ITEM_BORROW<py::list>(const py::handle& container, const py::size_t& item) {
-    return py::reinterpret_borrow<py::object>(
-        PyList_GET_ITEM(container.ptr(), py::ssize_t_cast(item)));
-}
-template <>
-inline py::object GET_ITEM_BORROW<py::list>(const py::handle& container, const int& item) {
-    return py::reinterpret_borrow<py::object>(
-        PyList_GET_ITEM(container.ptr(), py::ssize_t_cast(item)));
-}
 
 template <typename Container, typename Item>
 inline py::object GET_ITEM_STEAL(const py::handle& container, const Item& item) {
-    return container[item];
+    return py::reinterpret_steal<py::object>(container[item]);
 }
 template <>
 inline py::object GET_ITEM_STEAL<py::tuple>(const py::handle& container, const py::ssize_t& item) {
     return py::reinterpret_steal<py::object>(PyTuple_GET_ITEM(container.ptr(), item));
 }
 template <>
-inline py::object GET_ITEM_STEAL<py::tuple>(const py::handle& container, const py::size_t& item) {
-    return py::reinterpret_steal<py::object>(
-        PyTuple_GET_ITEM(container.ptr(), py::ssize_t_cast(item)));
-}
-template <>
-inline py::object GET_ITEM_STEAL<py::tuple>(const py::handle& container, const int& item) {
-    return py::reinterpret_steal<py::object>(
-        PyTuple_GET_ITEM(container.ptr(), py::ssize_t_cast(item)));
-}
-template <>
 inline py::object GET_ITEM_STEAL<py::list>(const py::handle& container, const py::ssize_t& item) {
     return py::reinterpret_steal<py::object>(PyList_GET_ITEM(container.ptr(), item));
 }
-template <>
-inline py::object GET_ITEM_STEAL<py::list>(const py::handle& container, const py::size_t& item) {
-    return py::reinterpret_steal<py::object>(
-        PyList_GET_ITEM(container.ptr(), py::ssize_t_cast(item)));
-}
-template <>
-inline py::object GET_ITEM_STEAL<py::list>(const py::handle& container, const int& item) {
-    return py::reinterpret_steal<py::object>(
-        PyList_GET_ITEM(container.ptr(), py::ssize_t_cast(item)));
-}
 
 template <typename Container, typename Item>
 inline void SET_ITEM(const py::handle& container, const Item& item, const py::handle& value) {
@@ -314,39 +261,15 @@ inline void SET_ITEM<py::tuple>(const py::handle& cont
 }
 template <>
 inline void SET_ITEM<py::tuple>(const py::handle& container,
-                                const ssize_t& item,
+                                const py::ssize_t& item,
                                 const py::handle& value) {
     PyTuple_SET_ITEM(container.ptr(), item, value.inc_ref().ptr());
 }
 template <>
-inline void SET_ITEM<py::tuple>(const py::handle& container,
-                                const py::size_t& item,
-                                const py::handle& value) {
-    PyTuple_SET_ITEM(container.ptr(), py::ssize_t_cast(item), value.inc_ref().ptr());
-}
-template <>
-inline void SET_ITEM<py::tuple>(const py::handle& container,
-                                const int& item,
-                                const py::handle& value) {
-    PyTuple_SET_ITEM(container.ptr(), py::ssize_t_cast(item), value.inc_ref().ptr());
-}
-template <>
 inline void SET_ITEM<py::list>(const py::handle& container,
-                               const ssize_t& item,
+                               const py::ssize_t& item,
                                const py::handle& value) {
     PyList_SET_ITEM(container.ptr(), item, value.inc_ref().ptr());
-}
-template <>
-inline void SET_ITEM<py::list>(const py::handle& container,
-                               const py::size_t& item,
-                               const py::handle& value) {
-    PyList_SET_ITEM(container.ptr(), py::ssize_t_cast(item), value.inc_ref().ptr());
-}
-template <>
-inline void SET_ITEM<py::list>(const py::handle& container,
-                               const int& item,
-                               const py::handle& value) {
-    PyList_SET_ITEM(container.ptr(), py::ssize_t_cast(item), value.inc_ref().ptr());
 }
 
 inline std::string PyRepr(const py::handle& object) {
--- src/treespec/constructor.cpp.orig	2024-03-25 18:01:43 UTC
+++ src/treespec/constructor.cpp
@@ -78,7 +78,7 @@ template <bool NoneIsLeaf>
                                             std::vector<PyTreeSpec>& treespecs,
                                             std::string& register_namespace) {
         for (const py::object& child : children) {
-            if (!py::isinstance<PyTreeSpec>(child)) {
+            if (!py::isinstance<PyTreeSpec>(child)) [[unlikely]] {
                 std::ostringstream oss{};
                 oss << "Expected a(n) " << NodeKindToString(node) << " of PyTreeSpec(s), got "
                     << PyRepr(handle) << ".";
@@ -213,15 +213,15 @@ template <bool NoneIsLeaf>
                 throw std::runtime_error(oss.str());
             }
             node.arity = 0;
-            node.node_data = GET_ITEM_BORROW<py::tuple>(out, 1);
+            node.node_data = GET_ITEM_BORROW<py::tuple>(out, ssize_t(1));
             for (const py::handle& child :
-                 py::cast<py::iterable>(GET_ITEM_BORROW<py::tuple>(out, 0))) {
+                 py::cast<py::iterable>(GET_ITEM_BORROW<py::tuple>(out, ssize_t(0)))) {
                 ++node.arity;
                 children.emplace_back(py::reinterpret_borrow<py::object>(child));
             }
             verify_children(children, treespecs, registry_namespace);
             if (num_out == 3) [[likely]] {
-                py::object node_entries = GET_ITEM_BORROW<py::tuple>(out, 2);
+                py::object node_entries = GET_ITEM_BORROW<py::tuple>(out, ssize_t(2));
                 if (!node_entries.is_none()) [[likely]] {
                     node.node_entries = py::cast<py::tuple>(std::move(node_entries));
                     const ssize_t num_entries = GET_SIZE<py::tuple>(node.node_entries);
--- src/treespec/flatten.cpp.orig	2024-03-25 18:01:43 UTC
+++ src/treespec/flatten.cpp
@@ -144,14 +144,14 @@ bool PyTreeSpec::FlattenIntoImpl(const py::handle& han
                     throw std::runtime_error(oss.str());
                 }
                 node.arity = 0;
-                node.node_data = GET_ITEM_BORROW<py::tuple>(out, 1);
+                node.node_data = GET_ITEM_BORROW<py::tuple>(out, ssize_t(1));
                 for (const py::handle& child :
-                     py::cast<py::iterable>(GET_ITEM_BORROW<py::tuple>(out, 0))) {
+                     py::cast<py::iterable>(GET_ITEM_BORROW<py::tuple>(out, ssize_t(0)))) {
                     ++node.arity;
                     recurse(child);
                 }
                 if (num_out == 3) [[likely]] {
-                    py::object node_entries = GET_ITEM_BORROW<py::tuple>(out, 2);
+                    py::object node_entries = GET_ITEM_BORROW<py::tuple>(out, ssize_t(2));
                     if (!node_entries.is_none()) [[likely]] {
                         node.node_entries = py::cast<py::tuple>(std::move(node_entries));
                         const ssize_t num_entries = GET_SIZE<py::tuple>(node.node_entries);
@@ -338,16 +338,16 @@ bool PyTreeSpec::FlattenIntoWithPathImpl(const py::han
                     throw std::runtime_error(oss.str());
                 }
                 node.arity = 0;
-                node.node_data = GET_ITEM_BORROW<py::tuple>(out, 1);
+                node.node_data = GET_ITEM_BORROW<py::tuple>(out, ssize_t(1));
                 py::object node_entries;
                 if (num_out == 3) [[likely]] {
-                    node_entries = GET_ITEM_BORROW<py::tuple>(out, 2);
+                    node_entries = GET_ITEM_BORROW<py::tuple>(out, ssize_t(2));
                 } else [[unlikely]] {
                     node_entries = py::none();
                 }
                 if (node_entries.is_none()) [[unlikely]] {
                     for (const py::handle& child :
-                         py::cast<py::iterable>(GET_ITEM_BORROW<py::tuple>(out, 0))) {
+                         py::cast<py::iterable>(GET_ITEM_BORROW<py::tuple>(out, ssize_t(0)))) {
                         recurse(child, py::int_(node.arity++));
                     }
                 } else [[likely]] {
@@ -355,7 +355,7 @@ bool PyTreeSpec::FlattenIntoWithPathImpl(const py::han
                     node.arity = GET_SIZE<py::tuple>(node.node_entries);
                     ssize_t num_children = 0;
                     for (const py::handle& child :
-                         py::cast<py::iterable>(GET_ITEM_BORROW<py::tuple>(out, 0))) {
+                         py::cast<py::iterable>(GET_ITEM_BORROW<py::tuple>(out, ssize_t(0)))) {
                         if (num_children >= node.arity) [[unlikely]] {
                             throw std::runtime_error(
                                 "PyTree custom flatten function for type " +
@@ -500,9 +500,10 @@ py::list PyTreeSpec::FlattenUpTo(const py::object& ful
             case PyTreeKind::DefaultDict: {
                 AssertExactStandardDict(object);
                 auto dict = py::reinterpret_borrow<py::dict>(object);
-                py::list expected_keys = (node.kind != PyTreeKind::DefaultDict
-                                              ? node.node_data
-                                              : GET_ITEM_BORROW<py::tuple>(node.node_data, 1));
+                py::list expected_keys =
+                    (node.kind != PyTreeKind::DefaultDict
+                         ? node.node_data
+                         : GET_ITEM_BORROW<py::tuple>(node.node_data, ssize_t(1)));
                 if (!DictKeysEqual(expected_keys, dict)) [[unlikely]] {
                     py::list keys = SortedDictKeys(dict);
                     auto [missing_keys, extra_keys] = DictKeysDifference(expected_keys, dict);
@@ -619,7 +620,7 @@ py::list PyTreeSpec::FlattenUpTo(const py::object& ful
                         << " should return a 2- or 3-tuple, got " << num_out << ".";
                     throw std::runtime_error(oss.str());
                 }
-                py::object node_data = GET_ITEM_BORROW<py::tuple>(out, 1);
+                py::object node_data = GET_ITEM_BORROW<py::tuple>(out, ssize_t(1));
                 if (node.node_data.not_equal(node_data)) [[unlikely]] {
                     std::ostringstream oss{};
                     oss << "Mismatch custom node data; expected: " << PyRepr(node.node_data)
@@ -628,7 +629,7 @@ py::list PyTreeSpec::FlattenUpTo(const py::object& ful
                 }
                 ssize_t arity = 0;
                 for (const py::handle& child :
-                     py::cast<py::iterable>(GET_ITEM_BORROW<py::tuple>(out, 0))) {
+                     py::cast<py::iterable>(GET_ITEM_BORROW<py::tuple>(out, ssize_t(0)))) {
                     ++arity;
                     agenda.emplace_back(py::reinterpret_borrow<py::object>(child));
                 }
--- src/treespec/traversal.cpp.orig	2024-03-25 18:01:43 UTC
+++ src/treespec/traversal.cpp
@@ -123,10 +123,10 @@ py::object PyTreeIter::NextImpl() {
                         << " should return a 2- or 3-tuple, got " << num_out << ".";
                     throw std::runtime_error(oss.str());
                 }
-                auto children = py::cast<py::tuple>(GET_ITEM_BORROW<py::tuple>(out, 0));
+                auto children = py::cast<py::tuple>(GET_ITEM_BORROW<py::tuple>(out, ssize_t(0)));
                 ssize_t arity = GET_SIZE<py::tuple>(children);
                 if (num_out == 3) [[likely]] {
-                    py::object node_entries = GET_ITEM_BORROW<py::tuple>(out, 2);
+                    py::object node_entries = GET_ITEM_BORROW<py::tuple>(out, ssize_t(2));
                     if (!node_entries.is_none()) [[likely]] {
                         const ssize_t num_entries =
                             GET_SIZE<py::tuple>(py::cast<py::tuple>(std::move(node_entries)));
--- src/treespec/treespec.cpp.orig	2024-03-25 18:01:43 UTC
+++ src/treespec/treespec.cpp
@@ -138,8 +138,8 @@ namespace optree {
 
         case PyTreeKind::DefaultDict: {
             py::dict dict{};
-            py::object default_factory = GET_ITEM_BORROW<py::tuple>(node.node_data, 0);
-            py::list keys = GET_ITEM_BORROW<py::tuple>(node.node_data, 1);
+            py::object default_factory = GET_ITEM_BORROW<py::tuple>(node.node_data, ssize_t(0));
+            py::list keys = GET_ITEM_BORROW<py::tuple>(node.node_data, ssize_t(1));
             if (node.original_keys) [[unlikely]] {
                 for (ssize_t i = 0; i < node.arity; ++i) {
                     dict[GET_ITEM_HANDLE<py::list>(node.original_keys, i)] = py::none();
@@ -254,11 +254,11 @@ namespace optree {
             auto expected_keys = py::reinterpret_borrow<py::list>(
                 root.kind != PyTreeKind::DefaultDict
                     ? root.node_data
-                    : GET_ITEM_BORROW<py::tuple>(root.node_data, 1));
+                    : GET_ITEM_BORROW<py::tuple>(root.node_data, ssize_t(1)));
             auto other_keys = py::reinterpret_borrow<py::list>(
                 other_root.kind != PyTreeKind::DefaultDict
                     ? other_root.node_data
-                    : GET_ITEM_BORROW<py::tuple>(other_root.node_data, 1));
+                    : GET_ITEM_BORROW<py::tuple>(other_root.node_data, ssize_t(1)));
             py::dict dict{};
             for (ssize_t i = 0; i < other_root.arity; ++i) {
                 dict[GET_ITEM_HANDLE<py::list>(other_keys, i)] = py::int_(i);
@@ -530,7 +530,7 @@ ssize_t PyTreeSpec::PathsImpl(Span& paths,
                 auto keys = py::reinterpret_borrow<py::list>(
                     root.kind != PyTreeKind::DefaultDict
                         ? root.node_data
-                        : GET_ITEM_BORROW<py::tuple>(root.node_data, 1));
+                        : GET_ITEM_BORROW<py::tuple>(root.node_data, ssize_t(1)));
                 for (ssize_t i = root.arity - 1; i >= 0; --i) {
                     cur -= recurse(cur, GET_ITEM_HANDLE<py::list>(keys, i));
                 }
@@ -595,7 +595,8 @@ py::list PyTreeSpec::Entries() const {
             return py::getattr(root.node_data, Py_Get_ID(copy))();
         }
         case PyTreeKind::DefaultDict: {
-            return py::getattr(GET_ITEM_BORROW<py::tuple>(root.node_data, 1), Py_Get_ID(copy))();
+            return py::getattr(GET_ITEM_BORROW<py::tuple>(root.node_data, ssize_t(1)),
+                               Py_Get_ID(copy))();
         }
 
         default:
@@ -631,7 +632,8 @@ py::object PyTreeSpec::Entry(ssize_t index) const {
             return GET_ITEM_BORROW<py::list>(root.node_data, index);
         }
         case PyTreeKind::DefaultDict: {
-            return GET_ITEM_BORROW<py::list>(GET_ITEM_BORROW<py::tuple>(root.node_data, 1), index);
+            return GET_ITEM_BORROW<py::list>(GET_ITEM_BORROW<py::tuple>(root.node_data, ssize_t(1)),
+                                             index);
         }
 
         case PyTreeKind::None:
@@ -804,11 +806,11 @@ bool PyTreeSpec::IsPrefix(const PyTreeSpec& other, con
                 auto expected_keys = py::reinterpret_borrow<py::list>(
                     a->kind != PyTreeKind::DefaultDict
                         ? a->node_data
-                        : GET_ITEM_BORROW<py::tuple>(a->node_data, 1));
+                        : GET_ITEM_BORROW<py::tuple>(a->node_data, ssize_t(1)));
                 auto other_keys = py::reinterpret_borrow<py::list>(
                     b->kind != PyTreeKind::DefaultDict
                         ? b->node_data
-                        : GET_ITEM_BORROW<py::tuple>(b->node_data, 1));
+                        : GET_ITEM_BORROW<py::tuple>(b->node_data, ssize_t(1)));
                 py::dict dict{};
                 for (ssize_t i = 0; i < b->arity; ++i) {
                     dict[GET_ITEM_HANDLE<py::list>(other_keys, i)] = py::int_(i);
@@ -1023,9 +1025,9 @@ std::string PyTreeSpec::ToStringImpl() const {
             case PyTreeKind::DefaultDict: {
                 EXPECT_EQ(
                     GET_SIZE<py::tuple>(node.node_data), 2, "Number of auxiliary data mismatch.");
-                py::object default_factory = GET_ITEM_BORROW<py::tuple>(node.node_data, 0);
-                auto keys =
-                    py::reinterpret_borrow<py::list>(GET_ITEM_BORROW<py::tuple>(node.node_data, 1));
+                py::object default_factory = GET_ITEM_BORROW<py::tuple>(node.node_data, ssize_t(0));
+                auto keys = py::reinterpret_borrow<py::list>(
+                    GET_ITEM_BORROW<py::tuple>(node.node_data, ssize_t(1)));
                 EXPECT_EQ(GET_SIZE<py::list>(keys),
                           node.arity,
                           "Number of keys and entries does not match.");
@@ -1146,13 +1148,13 @@ std::string PyTreeSpec::ToString() const {
             if (node.kind == PyTreeKind::DefaultDict) [[unlikely]] {
                 EXPECT_EQ(
                     GET_SIZE<py::tuple>(node.node_data), 2, "Number of auxiliary data mismatch.");
-                py::object default_factory = GET_ITEM_BORROW<py::tuple>(node.node_data, 0);
+                py::object default_factory = GET_ITEM_BORROW<py::tuple>(node.node_data, ssize_t(0));
                 data_hash = py::hash(default_factory);
             }
             auto keys = py::reinterpret_borrow<py::list>(
                 node.kind != PyTreeKind::DefaultDict
                     ? node.node_data
-                    : GET_ITEM_BORROW<py::tuple>(node.node_data, 1));
+                    : GET_ITEM_BORROW<py::tuple>(node.node_data, ssize_t(1)));
             EXPECT_EQ(
                 GET_SIZE<py::list>(keys), node.arity, "Number of keys and entries does not match.");
             for (const py::handle& key : keys) {