Cython 3.2.6__py3-none-any.whl → 3.3.0a2.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of Cython might be problematic. Click here for more details.

Files changed (112) hide show
  1. Cython/Build/BuildExecutable.py +1 -14
  2. Cython/Build/Dependencies.py +6 -44
  3. Cython/Build/Inline.py +7 -3
  4. Cython/Build/IpythonMagic.py +0 -1
  5. Cython/Build/SharedModule.py +1 -2
  6. Cython/Build/Tests/TestCythonizeArgsParser.py +6 -6
  7. Cython/Build/Tests/TestDependencies.py +3 -3
  8. Cython/Build/Tests/TestInline.py +3 -3
  9. Cython/Build/Tests/TestIpythonMagic.py +35 -19
  10. Cython/Build/Tests/TestStripLiterals.py +2 -2
  11. Cython/Compiler/Buffer.py +5 -5
  12. Cython/Compiler/Builtin.py +57 -46
  13. Cython/Compiler/Code.pxd +12 -9
  14. Cython/Compiler/Code.py +220 -94
  15. Cython/Compiler/Dataclass.py +78 -52
  16. Cython/Compiler/Errors.py +21 -8
  17. Cython/Compiler/ExprNodes.py +871 -459
  18. Cython/Compiler/FlowControl.pxd +8 -6
  19. Cython/Compiler/FlowControl.py +111 -8
  20. Cython/Compiler/FusedNode.py +17 -12
  21. Cython/Compiler/Lexicon.py +123 -122
  22. Cython/Compiler/Main.py +3 -4
  23. Cython/Compiler/MatchCaseNodes.py +1948 -10
  24. Cython/Compiler/MemoryView.py +62 -33
  25. Cython/Compiler/ModuleNode.py +559 -283
  26. Cython/Compiler/Naming.py +4 -0
  27. Cython/Compiler/Nodes.py +333 -197
  28. Cython/Compiler/Optimize.py +268 -112
  29. Cython/Compiler/Options.py +0 -5
  30. Cython/Compiler/ParseTreeTransforms.pxd +2 -1
  31. Cython/Compiler/ParseTreeTransforms.py +180 -76
  32. Cython/Compiler/Parsing.py +55 -38
  33. Cython/Compiler/PyrexTypes.py +364 -128
  34. Cython/Compiler/Scanning.pxd +27 -5
  35. Cython/Compiler/Scanning.py +24 -14
  36. Cython/Compiler/StringEncoding.py +1 -2
  37. Cython/Compiler/Symtab.py +27 -19
  38. Cython/Compiler/Tests/TestBuiltin.py +41 -6
  39. Cython/Compiler/Tests/TestCmdLine.py +3 -2
  40. Cython/Compiler/Tests/TestCode.py +4 -3
  41. Cython/Compiler/Tests/TestFlowControl.py +2 -2
  42. Cython/Compiler/Tests/TestParseTreeTransforms.py +2 -2
  43. Cython/Compiler/Tests/TestScanning.py +2 -4
  44. Cython/Compiler/Tests/TestSignatureMatching.py +3 -3
  45. Cython/Compiler/Tests/TestStringEncoding.py +2 -3
  46. Cython/Compiler/Tests/TestTypes.py +46 -3
  47. Cython/Compiler/Tests/TestUtilityLoad.py +4 -4
  48. Cython/Compiler/TypeInference.py +20 -0
  49. Cython/Compiler/TypeSlots.py +171 -16
  50. Cython/Compiler/UtilNodes.py +24 -0
  51. Cython/Compiler/UtilityCode.py +1 -0
  52. Cython/Compiler/Visitor.pxd +1 -1
  53. Cython/Compiler/Visitor.py +3 -2
  54. Cython/Debugger/Tests/TestLibCython.py +7 -2
  55. Cython/Debugger/Tests/test_libcython_in_gdb.py +4 -2
  56. Cython/Debugger/Tests/test_libpython_in_gdb.py +1 -1
  57. Cython/Includes/cpython/array.pxd +5 -5
  58. Cython/Includes/cpython/dict.pxd +1 -6
  59. Cython/Includes/cpython/float.pxd +19 -10
  60. Cython/Includes/cpython/frozendict.pxd +37 -0
  61. Cython/Includes/cpython/long.pxd +31 -0
  62. Cython/Includes/cpython/object.pxd +9 -12
  63. Cython/Includes/cpython/sentinel.pxd +17 -0
  64. Cython/Includes/cpython/type.pxd +93 -0
  65. Cython/Includes/libcpp/exception.pxd +90 -0
  66. Cython/LZSS.py +170 -0
  67. Cython/Shadow.py +550 -73
  68. Cython/StringIOTree.py +1 -2
  69. Cython/Tempita/_tempita.py +45 -46
  70. Cython/TestUtils.py +50 -8
  71. Cython/Tests/TestCythonUtils.py +2 -2
  72. Cython/Tests/TestJediTyper.py +1 -1
  73. Cython/Tests/TestShadow.py +7 -5
  74. Cython/Tests/TestStringIOTree.py +4 -3
  75. Cython/Tests/TestTestUtils.py +2 -3
  76. Cython/Utility/AsyncGen.c +59 -19
  77. Cython/Utility/Buffer.c +4 -3
  78. Cython/Utility/Builtins.c +139 -17
  79. Cython/Utility/CConvert.pyx +27 -12
  80. Cython/Utility/CommonStructures.c +55 -37
  81. Cython/Utility/Coroutine.c +112 -68
  82. Cython/Utility/CythonFunction.c +607 -330
  83. Cython/Utility/Embed.c +8 -0
  84. Cython/Utility/Exceptions.c +20 -3
  85. Cython/Utility/ExtensionTypes.c +414 -224
  86. Cython/Utility/FunctionArguments.c +83 -81
  87. Cython/Utility/ImportExport.c +72 -61
  88. Cython/Utility/MatchCase.c +979 -0
  89. Cython/Utility/MatchCase_Cy.pyx +12 -0
  90. Cython/Utility/MemoryView.pxd +9 -88
  91. Cython/Utility/MemoryView.pyx +173 -61
  92. Cython/Utility/MemoryView_C.c +3 -3
  93. Cython/Utility/ModuleSetupCode.c +306 -157
  94. Cython/Utility/ObjectHandling.c +483 -404
  95. Cython/Utility/Optimize.c +1117 -164
  96. Cython/Utility/Overflow.c +3 -9
  97. Cython/Utility/Profile.c +4 -0
  98. Cython/Utility/StringTools.c +187 -149
  99. Cython/Utility/Synchronization.c +39 -0
  100. Cython/Utility/TString.c +24 -13
  101. Cython/Utility/TypeConversion.c +405 -250
  102. Cython/Utility/arrayarray.h +24 -22
  103. Cython/Utils.py +11 -23
  104. Cython/__init__.py +3 -1
  105. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/METADATA +14 -33
  106. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/RECORD +109 -107
  107. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/WHEEL +1 -1
  108. Cython/Shadow.pyi +0 -521
  109. Cython/Utility/Printing.c +0 -86
  110. Cython/__init__.pyi +0 -7
  111. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/entry_points.txt +0 -0
  112. {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,979 @@
1
+ ///////////////////////////// ABCCheck //////////////////////////////
2
+ //@requires: Builtins.c::PyFrozenDict
3
+
4
+ #if PY_VERSION_HEX < 0x030A0000 || CYTHON_COMPILING_IN_LIMITED_API || CYTHON_COMPILING_IN_PYPY
5
+ static CYTHON_INLINE int __Pyx_MatchCase_IsExactSequence(PyObject *o) {
6
+ // Is one of the small list of builtin types known to be a sequence.
7
+ if (PyList_CheckExact(o) || PyTuple_CheckExact(o) ||
8
+ Py_IS_TYPE(o, &PyRange_Type) || Py_IS_TYPE(o, &PyMemoryView_Type)) {
9
+ // Use exact type match for these checks. In in the event of inheritance we need to make sure
10
+ // that it isn't a mapping too
11
+ return 1;
12
+ }
13
+ return 0;
14
+ }
15
+
16
+ static CYTHON_INLINE int __Pyx_MatchCase_IsExactMapping(PyObject *o) {
17
+ // dict and frozendict are the only regularly used mapping type.
18
+ // "types.MappingProxyType" also exists but is correctly covered by
19
+ // the isinstance(o, Mapping) check.
20
+ return __Pyx_PyAnyDict_CheckExact(o);
21
+ }
22
+
23
+ static int __Pyx_MatchCase_IsExactNeitherSequenceNorMapping(PyObject *o) {
24
+ if (PyByteArray_Check(o) || PyBytes_Check(o) || PyUnicode_Check(o)) {
25
+ // These types are deliberately excluded from the sequence test
26
+ // even though they look like sequences for most other purposes.
27
+ // Leaving them as inexact checks since they do pass
28
+ // "isinstance(o, collections.abc.Sequence)" so it's very hard to
29
+ // reason about their subclasses.
30
+ return 1;
31
+ }
32
+ // No-exhaustive list of other common builtin types as an optimization.
33
+ if (o == Py_None || PyLong_CheckExact(o) || PyFloat_CheckExact(o) ||
34
+ Py_TYPE(o) == &PyBool_Type || Py_TYPE(o) == &PySlice_Type ||
35
+ PyAnySet_CheckExact(o) || PyComplex_CheckExact(o)) {
36
+ return 1;
37
+ }
38
+
39
+ return 0;
40
+ }
41
+
42
+ // sequence_mapping_temp: For Python 3.10 testing sequences and mappings are
43
+ // really quick and this is ignored. For lower versions of Python they're
44
+ // slow, especially in the "fail" case.
45
+ // Therefore, we store an int temp to avoid duplicating tests.
46
+ // The bits of it in order are:
47
+ // 0. definitely a sequence
48
+ // 1. definitely a mapping
49
+ // - note that both of the above can be true when
50
+ // the type is registered with both abc types (not via inheritance)
51
+ // and in this case we return true for both IsSequence or IsMapping
52
+ // (which seems like the best handling of an ambiguous situation)
53
+ // 2. definitely not a sequence
54
+ // 3. definitely not a mapping
55
+
56
+ #define __PYX_DEFINITELY_SEQUENCE_FLAG 1U
57
+ #define __PYX_DEFINITELY_MAPPING_FLAG (1U<<1)
58
+ #define __PYX_DEFINITELY_NOT_SEQUENCE_FLAG (1U<<2)
59
+ #define __PYX_DEFINITELY_NOT_MAPPING_FLAG (1U<<3)
60
+ #define __PYX_SEQUENCE_MAPPING_ERROR (1U<<4) // only used by the ABCCheck function
61
+
62
+ static int __Pyx_MatchCase_InitAbcType(PyObject *abc_module, PyObject **abc_type, PyObject *name) {
63
+ if (*abc_type) return 0;
64
+ *abc_type = PyObject_GetAttr(abc_module, name);
65
+ return *abc_type ? 0 : -1;
66
+ }
67
+
68
+ // The result is defined using the specification for sequence_mapping_temp
69
+ // (detailed in "is_sequence").
70
+ static unsigned int __Pyx_MatchCase_ABCCheck(PyObject *o, int sequence_first, int definitely_not_sequence, int definitely_not_mapping) {
71
+ // In Python 3.10+, objects can have their sequence bit set or their mapping bit set
72
+ // but not both. Practically, this translates to "which type is registered first".
73
+ // In Python < 3.10 we can only determine this if they're direct bases (by looking
74
+ // at the MRO order). If they're registered manually then we can't tell.
75
+
76
+ PyObject *abc_module=NULL, *sequence_type=NULL, *mapping_type=NULL;
77
+ PyObject *mro;
78
+ int sequence_result=0, mapping_result=0;
79
+ unsigned int result = 0;
80
+
81
+ if (sequence_first && definitely_not_sequence) {
82
+ return __PYX_DEFINITELY_NOT_SEQUENCE_FLAG;
83
+ }
84
+ if (!sequence_first && definitely_not_mapping) {
85
+ return __PYX_DEFINITELY_NOT_MAPPING_FLAG;
86
+ }
87
+
88
+ abc_module = PyImport_ImportModule("collections.abc");
89
+ if (!abc_module) {
90
+ return __PYX_SEQUENCE_MAPPING_ERROR;
91
+ }
92
+ if (sequence_first) {
93
+ if (unlikely(__Pyx_MatchCase_InitAbcType(abc_module, &sequence_type, PYIDENT("Sequence")) == -1)) {
94
+ result = __PYX_SEQUENCE_MAPPING_ERROR;
95
+ goto end;
96
+ }
97
+ sequence_result = PyObject_IsInstance(o, sequence_type);
98
+ if (sequence_result <= 0) {
99
+ result = sequence_result < 0 ? __PYX_SEQUENCE_MAPPING_ERROR : __PYX_DEFINITELY_NOT_SEQUENCE_FLAG;
100
+ goto end;
101
+ }
102
+ // else sequence_result==1 but wait to see what mapping is
103
+ }
104
+ if (!definitely_not_mapping) {
105
+ if (unlikely(__Pyx_MatchCase_InitAbcType(abc_module, &mapping_type, PYIDENT("Mapping")) == -1)) {
106
+ result = __PYX_SEQUENCE_MAPPING_ERROR;
107
+ goto end;
108
+ }
109
+ mapping_result = PyObject_IsInstance(o, mapping_type);
110
+ if (unlikely(mapping_result < 0)) {
111
+ result = __PYX_SEQUENCE_MAPPING_ERROR;
112
+ goto end;
113
+ } else if (mapping_result == 0 && !sequence_first) {
114
+ result = __PYX_DEFINITELY_NOT_MAPPING_FLAG;
115
+ goto end;
116
+ } // else mapping_result == 1
117
+ }
118
+ if (!sequence_first && !definitely_not_sequence) {
119
+ if (unlikely(__Pyx_MatchCase_InitAbcType(abc_module, &sequence_type, PYIDENT("Sequence")) == -1)) {
120
+ result = __PYX_SEQUENCE_MAPPING_ERROR;
121
+ goto end;
122
+ }
123
+ sequence_result = PyObject_IsInstance(o, sequence_type);
124
+ if (unlikely(sequence_result < 0)) {
125
+ result = __PYX_SEQUENCE_MAPPING_ERROR;
126
+ goto end;
127
+ }
128
+ }
129
+ result |= (sequence_result ? __PYX_DEFINITELY_SEQUENCE_FLAG : __PYX_DEFINITELY_NOT_SEQUENCE_FLAG);
130
+ result |= (mapping_result ? __PYX_DEFINITELY_MAPPING_FLAG : __PYX_DEFINITELY_NOT_MAPPING_FLAG);
131
+ if (result != (__PYX_DEFINITELY_SEQUENCE_FLAG | __PYX_DEFINITELY_MAPPING_FLAG)) {
132
+ goto end;
133
+ }
134
+
135
+ // It's an instance of both types. Look up the MRO order.
136
+ // In event of failure treat it as "could be either"
137
+ mro = PyObject_GetAttrString((PyObject*)Py_TYPE(o), "__mro__");
138
+ Py_ssize_t i, mro_size;
139
+ if (!mro) {
140
+ PyErr_Clear();
141
+ goto end;
142
+ }
143
+ if (!PyTuple_Check(mro)) {
144
+ Py_DECREF(mro);
145
+ goto end;
146
+ }
147
+ mro_size = __Pyx_PyTuple_GET_SIZE(mro);
148
+ #if !CYTHON_ASSUME_SAFE_SIZE
149
+ if (unlikely(mro_size == -1)) {
150
+ goto loop_error;
151
+ }
152
+ #endif
153
+ for (i=1; i < mro_size; ++i) {
154
+ int is_subclass_sequence, is_subclass_mapping;
155
+ PyObject *mro_item = __Pyx_PyTuple_GET_ITEM(mro, i);
156
+ #if !CYTHON_ASSUME_SAFE_MACROS
157
+ if (unlikely(!mro_item)) goto loop_error;
158
+ #endif
159
+ is_subclass_sequence = PyObject_IsSubclass(mro_item, sequence_type);
160
+ if (is_subclass_sequence < 0) goto loop_error;
161
+ is_subclass_mapping = PyObject_IsSubclass(mro_item, mapping_type);
162
+ if (is_subclass_mapping < 0) goto loop_error;
163
+ if (is_subclass_sequence && !is_subclass_mapping) {
164
+ result = (__PYX_DEFINITELY_SEQUENCE_FLAG | __PYX_DEFINITELY_NOT_MAPPING_FLAG);
165
+ break;
166
+ } else if (is_subclass_mapping && !is_subclass_sequence) {
167
+ result = (__PYX_DEFINITELY_NOT_SEQUENCE_FLAG | __PYX_DEFINITELY_MAPPING_FLAG);
168
+ break;
169
+ }
170
+ }
171
+ // If we get to the end of the loop without breaking then neither type is in
172
+ // the MRO, so they've both been registered manually. We don't know which was
173
+ // registered first so accept the object as either as a compromise.
174
+ loop_error_recovery:
175
+ Py_DECREF(mro);
176
+
177
+ end:
178
+ Py_XDECREF(abc_module);
179
+ Py_XDECREF(sequence_type);
180
+ Py_XDECREF(mapping_type);
181
+ return result;
182
+
183
+ loop_error:
184
+ PyErr_Clear();
185
+ goto loop_error_recovery;
186
+ }
187
+ #endif
188
+
189
+ ///////////////////////////// IsSequence.proto //////////////////////
190
+
191
+ static int __Pyx_MatchCase_IsSequence(PyObject *o, unsigned int *sequence_mapping_temp); /* proto */
192
+
193
+ //////////////////////////// IsSequence /////////////////////////
194
+ //@requires: ABCCheck
195
+
196
+ static int __Pyx_MatchCase_IsSequence(PyObject *o, unsigned int *sequence_mapping_temp) {
197
+ #if PY_VERSION_HEX >= 0x030A0000 && !(CYTHON_COMPILING_IN_LIMITED_API || CYTHON_COMPILING_IN_PYPY)
198
+ return __Pyx_PyType_HasFeature(Py_TYPE(o), Py_TPFLAGS_SEQUENCE);
199
+ #else
200
+ #if CYTHON_COMPILING_IN_LIMITED_API
201
+ // In the Limited API we have runtime access to Py_TPFLAGS_SEQUENCE
202
+ // by looking it up on module init so it's still worth attempting
203
+ // the fast path.
204
+ if (__Pyx_Runtime_TPFLAGS_SEQUENCE) {
205
+ return __Pyx_PyType_HasFeature(Py_TYPE(o), __Pyx_Runtime_TPFLAGS_SEQUENCE);
206
+ }
207
+ #elif defined(Py_TPFLAGS_SEQUENCE)
208
+ // Elsewhere *we* define Py_TPFLAGS_SEQUENCE but that doesn't necessarily
209
+ // mean other types use it, so only success is meaningful.
210
+ if (__Pyx_PyType_HasFeature(Py_TYPE(o), Py_TPFLAGS_SEQUENCE)) {
211
+ return 1;
212
+ }
213
+ #endif
214
+ // Py_TPFLAGS_SEQUENCE doesn't exit.
215
+ PyObject *o_module_name;
216
+ unsigned int abc_result, dummy=0;
217
+
218
+ if (sequence_mapping_temp) {
219
+ // maybe we already know the answer
220
+ if (*sequence_mapping_temp & __PYX_DEFINITELY_SEQUENCE_FLAG) {
221
+ return 1;
222
+ }
223
+ if (*sequence_mapping_temp & __PYX_DEFINITELY_NOT_SEQUENCE_FLAG) {
224
+ return 0;
225
+ }
226
+ } else {
227
+ // Probably quicker to just assign it and not check from here.
228
+ sequence_mapping_temp = &dummy;
229
+ }
230
+
231
+ // Start by checking a known list of types.
232
+ if (__Pyx_MatchCase_IsExactSequence(o)) {
233
+ *sequence_mapping_temp |= (__PYX_DEFINITELY_SEQUENCE_FLAG | __PYX_DEFINITELY_NOT_MAPPING_FLAG);
234
+ return 1;
235
+ }
236
+ if (__Pyx_MatchCase_IsExactMapping(o)) {
237
+ *sequence_mapping_temp |= (__PYX_DEFINITELY_MAPPING_FLAG | __PYX_DEFINITELY_NOT_SEQUENCE_FLAG);
238
+ return 0;
239
+ }
240
+ if (__Pyx_MatchCase_IsExactNeitherSequenceNorMapping(o)) {
241
+ *sequence_mapping_temp |= (__PYX_DEFINITELY_NOT_SEQUENCE_FLAG | __PYX_DEFINITELY_NOT_MAPPING_FLAG);
242
+ return 0;
243
+ }
244
+
245
+ abc_result = __Pyx_MatchCase_ABCCheck(
246
+ o, 1,
247
+ *sequence_mapping_temp & __PYX_DEFINITELY_NOT_SEQUENCE_FLAG,
248
+ *sequence_mapping_temp & __PYX_DEFINITELY_NOT_MAPPING_FLAG
249
+ );
250
+ if (abc_result & __PYX_SEQUENCE_MAPPING_ERROR) {
251
+ return -1;
252
+ }
253
+ *sequence_mapping_temp = abc_result;
254
+ if (*sequence_mapping_temp & __PYX_DEFINITELY_SEQUENCE_FLAG) {
255
+ return 1;
256
+ }
257
+
258
+ // array.array is a more complicated check (and unfortunately isn't covered by
259
+ // collections.abc.Sequence on Python <3.10).
260
+ // Do the test by checking the module name, and then importing/testing the class.
261
+ // It also doesn't give perfect results for classes that inherit from both array.array
262
+ // and a mapping.
263
+ #if !CYTHON_COMPILING_IN_LIMITED_API || __PYX_LIMITED_VERSION_HEX < 0x030A0000
264
+ #if CYTHON_COMPILING_IN_LIMITED_API
265
+ if (__Pyx_get_runtime_version() < 0x030A0000)
266
+ #endif
267
+ {
268
+ o_module_name = PyObject_GetAttrString((PyObject*)Py_TYPE(o), "__module__");
269
+ if (!o_module_name) {
270
+ return -1;
271
+ }
272
+ if (PyUnicode_Check(o_module_name) && PyUnicode_CompareWithASCIIString(o_module_name, "array") == 0)
273
+ {
274
+ int is_array;
275
+ PyObject *array_module, *array_object;
276
+ Py_DECREF(o_module_name);
277
+ array_module = PyImport_ImportModule("array");
278
+ if (!array_module) {
279
+ PyErr_Clear();
280
+ return 0; // treat these tests as "soft" and don't cause an exception
281
+ }
282
+ array_object = PyObject_GetAttrString(array_module, "array");
283
+ Py_DECREF(array_module);
284
+ if (!array_object) {
285
+ PyErr_Clear();
286
+ return 0;
287
+ }
288
+ is_array = PyObject_IsInstance(o, array_object);
289
+ Py_DECREF(array_object);
290
+ if (is_array) {
291
+ *sequence_mapping_temp |= __PYX_DEFINITELY_SEQUENCE_FLAG;
292
+ return 1;
293
+ }
294
+ PyErr_Clear();
295
+ } else {
296
+ Py_DECREF(o_module_name);
297
+ }
298
+ }
299
+ #else
300
+ CYTHON_UNUSED_VAR(o_module_name);
301
+ #endif
302
+ *sequence_mapping_temp |= __PYX_DEFINITELY_NOT_SEQUENCE_FLAG;
303
+ return 0;
304
+ #endif
305
+ }
306
+
307
+ ////////////////////// OtherSequenceSliceToList.proto //////////////////////
308
+
309
+ static PyObject *__Pyx_MatchCase_OtherSequenceSliceToList(PyObject *x, Py_ssize_t start, Py_ssize_t end); /* proto */
310
+
311
+ ////////////////////// OtherSequenceSliceToList //////////////////////////
312
+
313
+ // This is substantially based off ceval unpack_iterable.
314
+ // It's also pretty similar to itertools.islice.
315
+ // Indices must be positive - there's no wraparound or boundschecking.
316
+
317
+ static PyObject *__Pyx_MatchCase_OtherSequenceSliceToList(PyObject *x, Py_ssize_t start, Py_ssize_t end) {
318
+ Py_ssize_t total = end-start;
319
+ Py_ssize_t i;
320
+ PyObject *list;
321
+ ssizeargfunc slot;
322
+
323
+ list = PyList_New(total);
324
+ if (!list) {
325
+ return NULL;
326
+ }
327
+
328
+ slot = __Pyx_PyObject_TryGetSubSlot(x, tp_as_sequence, sq_item, ssizeargfunc);
329
+ if (!slot) {
330
+ #if !CYTHON_COMPILING_IN_LIMITED_API && !defined(PySequence_ITEM)
331
+ // PyPy (and maybe others?) implements PySequence_ITEM as a function. In this case.
332
+ // it's slightly more efficient than using PySequence_GetItem since it skips negative indices.
333
+ slot = PySequence_ITEM;
334
+ #else
335
+ slot = PySequence_GetItem;
336
+ #endif
337
+ }
338
+
339
+ for (i=start; i<end; ++i) {
340
+ PyObject *obj = slot(x, i);
341
+ if (unlikely(!obj) || unlikely(__Pyx_PyList_SET_ITEM(list, i-start, obj) == -1)) {
342
+ Py_DECREF(list);
343
+ return NULL;
344
+ }
345
+ }
346
+ return list;
347
+ }
348
+
349
+ ////////////////////// TupleSliceToList.proto //////////////////////
350
+
351
+ static PyObject *__Pyx_MatchCase_TupleSliceToList(PyObject *x, Py_ssize_t start, Py_ssize_t end); /* proto */
352
+
353
+ ////////////////////// TupleSliceToList //////////////////////////
354
+ //@requires: OtherSequenceSliceToList
355
+ //@requires: ObjectHandling.c::ListFromArray
356
+
357
+ // Indices must be positive - there's no wraparound or boundschecking.
358
+
359
+ static PyObject *__Pyx_MatchCase_TupleSliceToList(PyObject *x, Py_ssize_t start, Py_ssize_t end) {
360
+ #if !CYTHON_COMPILING_IN_CPYTHON
361
+ return __Pyx_MatchCase_OtherSequenceSliceToList(x, start, end);
362
+ #else
363
+ PyObject **array;
364
+
365
+ (void)__Pyx_MatchCase_OtherSequenceSliceToList; // clear unused warning
366
+
367
+ array = &PyTuple_GET_ITEM(x, 0);
368
+ return __Pyx_PyList_FromArray(array+start, end-start);
369
+ #endif
370
+ }
371
+
372
+ ////////////////////////// UnknownTypeSliceToList.proto //////////////////////
373
+
374
+ static PyObject *__Pyx_MatchCase_UnknownTypeSliceToList(PyObject *x, Py_ssize_t start, Py_ssize_t end); /* proto */
375
+
376
+ ////////////////////////// UnknownTypeSliceToList.proto //////////////////////
377
+ //@requires: TupleSliceToList
378
+ //@requires: OtherSequenceSliceToList
379
+
380
+ static PyObject *__Pyx_MatchCase_UnknownTypeSliceToList(PyObject *x, Py_ssize_t start, Py_ssize_t end) {
381
+ if (PyList_CheckExact(x)) {
382
+ return PyList_GetSlice(x, start, end);
383
+ }
384
+ #if !CYTHON_COMPILING_IN_CPYTHON
385
+ // since __Pyx_MatchCase_TupleToList only does anything special in CPython, skip the check otherwise
386
+ if (PyTuple_CheckExact(x)) {
387
+ return __Pyx_MatchCase_TupleSliceToList(x, start, end);
388
+ }
389
+ #else
390
+ (void)__Pyx_MatchCase_TupleSliceToList;
391
+ #endif
392
+ return __Pyx_MatchCase_OtherSequenceSliceToList(x, start, end);
393
+ }
394
+
395
+ ///////////////////////////// IsMapping.proto //////////////////////
396
+
397
+ static int __Pyx_MatchCase_IsMapping(PyObject *o, unsigned int *sequence_mapping_temp); /* proto */
398
+
399
+ //////////////////////////// IsMapping /////////////////////////
400
+ //@requires: ABCCheck
401
+
402
+ static int __Pyx_MatchCase_IsMapping(PyObject *o, unsigned int *sequence_mapping_temp) {
403
+ #if PY_VERSION_HEX >= 0x030A0000 && !(CYTHON_COMPILING_IN_LIMITED_API || CYTHON_COMPILING_IN_PYPY)
404
+ return __Pyx_PyType_HasFeature(Py_TYPE(o), Py_TPFLAGS_MAPPING);
405
+ #else
406
+ #if CYTHON_COMPILING_IN_LIMITED_API
407
+ // In the Limited API we have runtime access to Py_TPFLAGS_MAPPING
408
+ // by looking it up on module init so it's still worth attempting
409
+ // the fast path.
410
+ if (__Pyx_Runtime_TPFLAGS_MAPPING) {
411
+ return __Pyx_PyType_HasFeature(Py_TYPE(o), __Pyx_Runtime_TPFLAGS_MAPPING);
412
+ }
413
+ #elif defined(Py_TPFLAGS_MAPPING)
414
+ // Elsewhere *we* define Py_TPFLAGS_SEQUENCE but that doesn't necessarily
415
+ // mean other types use it, so only success is meaningful.
416
+ if (__Pyx_PyType_HasFeature(Py_TYPE(o), Py_TPFLAGS_MAPPING)) {
417
+ return 1;
418
+ }
419
+ #endif
420
+ unsigned int abc_result, dummy=0;
421
+ if (sequence_mapping_temp) {
422
+ // do we already know the answer?
423
+ if (*sequence_mapping_temp & __PYX_DEFINITELY_MAPPING_FLAG) {
424
+ return 1;
425
+ } else if (*sequence_mapping_temp & __PYX_DEFINITELY_NOT_MAPPING_FLAG) {
426
+ return 0;
427
+ }
428
+ } else {
429
+ sequence_mapping_temp = &dummy; // just so we can assign freely without checking
430
+ }
431
+
432
+ if (__Pyx_MatchCase_IsExactMapping(o)) {
433
+ *sequence_mapping_temp |= (__PYX_DEFINITELY_MAPPING_FLAG | __PYX_DEFINITELY_NOT_SEQUENCE_FLAG);
434
+ return 1;
435
+ }
436
+ if (__Pyx_MatchCase_IsExactSequence(o)) {
437
+ *sequence_mapping_temp |= (__PYX_DEFINITELY_SEQUENCE_FLAG | __PYX_DEFINITELY_NOT_MAPPING_FLAG);
438
+ return 0;
439
+ }
440
+ if (__Pyx_MatchCase_IsExactNeitherSequenceNorMapping(o)) {
441
+ *sequence_mapping_temp |= (__PYX_DEFINITELY_NOT_SEQUENCE_FLAG | __PYX_DEFINITELY_NOT_MAPPING_FLAG);
442
+ return 0;
443
+ }
444
+
445
+ // otherwise check against collections.abc.Mapping
446
+ abc_result = __Pyx_MatchCase_ABCCheck(
447
+ o, 0,
448
+ *sequence_mapping_temp & __PYX_DEFINITELY_NOT_SEQUENCE_FLAG,
449
+ *sequence_mapping_temp & __PYX_DEFINITELY_NOT_MAPPING_FLAG
450
+ );
451
+ if (abc_result & __PYX_SEQUENCE_MAPPING_ERROR) {
452
+ return -1;
453
+ }
454
+ *sequence_mapping_temp = abc_result;
455
+ return *sequence_mapping_temp & __PYX_DEFINITELY_MAPPING_FLAG;
456
+ #endif
457
+ }
458
+
459
+ //////////////////////// MappingKeyCheck.proto /////////////////////////
460
+
461
+ static int __Pyx_MatchCase_CheckMappingDuplicateKeys(PyObject *keys[], Py_ssize_t nFixedKeys, Py_ssize_t nKeys);
462
+
463
+ //////////////////////// MappingKeyCheck ///////////////////////////////
464
+
465
+ static int __Pyx_MatchCase_CheckMappingDuplicateKeys(PyObject *keys[], Py_ssize_t nFixedKeys, Py_ssize_t nKeys) {
466
+ // Inputs are arrays, and typically fairly small. It may be more efficient to
467
+ // loop over the array than create a set.
468
+
469
+ // The CPython implementation (match_keys in ceval.c) does this concurrently with
470
+ // taking the keys out of the dictionary. I'm choosing to do it separately since the
471
+ // majority of the time the keys will be known at compile-time so Cython can skip
472
+ // this step completely.
473
+ // The step is also skipped when there's only a single key.
474
+
475
+ PyObject *var_keys_set;
476
+ PyObject *key;
477
+ Py_ssize_t n;
478
+ int contains;
479
+
480
+ var_keys_set = PySet_New(NULL);
481
+ if (!var_keys_set) return -1;
482
+
483
+ for (n=nFixedKeys; n < nKeys; ++n) {
484
+ key = keys[n];
485
+ contains = PySet_Contains(var_keys_set, key);
486
+ if (contains < 0) {
487
+ goto bad;
488
+ } else if (contains == 1) {
489
+ goto raise_error;
490
+ } else {
491
+ if (PySet_Add(var_keys_set, key)) {
492
+ goto bad;
493
+ }
494
+ }
495
+ }
496
+ for (n=0; n < nFixedKeys; ++n) {
497
+ key = keys[n];
498
+ contains = PySet_Contains(var_keys_set, key);
499
+ if (contains < 0) {
500
+ goto bad;
501
+ } else if (contains == 1) {
502
+ goto raise_error;
503
+ }
504
+ }
505
+ Py_DECREF(var_keys_set);
506
+ return 0;
507
+
508
+ raise_error:
509
+ PyErr_Format(PyExc_ValueError,
510
+ "mapping pattern checks duplicate key (%R)", key);
511
+ bad:
512
+ Py_DECREF(var_keys_set);
513
+ return -1;
514
+ }
515
+
516
+ /////////////////////////// ExtractExactDict.proto ////////////////
517
+
518
+ // The subjects array is a list of PyObject** to subjects to be filled. They may be NULL
519
+ // in which case they're ignored.
520
+ //
521
+ // This is a specialized version for when we have an exact (frozen)dict (which is likely to be pretty common)
522
+
523
+ #if CYTHON_REFNANNY
524
+ #define __Pyx_MatchCase_Mapping_ExtractDict(...) __Pyx__MatchCase_Mapping_ExtractDict(__pyx_refnanny, __VA_ARGS__)
525
+ #else
526
+ #define __Pyx_MatchCase_Mapping_ExtractDict(...) __Pyx__MatchCase_Mapping_ExtractDict(NULL, __VA_ARGS__)
527
+ #endif
528
+ static CYTHON_INLINE int __Pyx__MatchCase_Mapping_ExtractDict(void *__pyx_refnanny, PyObject *dict, PyObject *keys[], Py_ssize_t nKeys, PyObject **subjects[]); /* proto */
529
+
530
+ /////////////////////////// ExtractExactDict ////////////////
531
+
532
+ static CYTHON_INLINE int __Pyx__MatchCase_Mapping_ExtractDict(void *__pyx_refnanny, PyObject *dict, PyObject *keys[], Py_ssize_t nKeys, PyObject **subjects[]) {
533
+ #if !CYTHON_REFNANNY
534
+ CYTHON_UNUSED_VAR(__pyx_refnanny);
535
+ #endif
536
+
537
+ Py_ssize_t i;
538
+ Py_ssize_t size;
539
+ size = PyDict_Size(dict);
540
+ if (size < nKeys) {
541
+ return size == -1 ? -1: 0;
542
+ }
543
+
544
+ for (i=0; i<nKeys; ++i) {
545
+ PyObject *key = keys[i];
546
+ PyObject **subject = subjects[i];
547
+ if (!subject) {
548
+ int contains = PyDict_Contains(dict, key);
549
+ if (contains <= 0) {
550
+ return contains; // any subjects that were already set will be cleaned up externally
551
+ }
552
+ } else {
553
+ PyObject *value;
554
+ int getref_result = __Pyx_PyDict_GetItemRef(dict, key, &value);
555
+ if (getref_result != 1) {
556
+ return getref_result; // any subjects that were already set will be cleaned up externally
557
+ }
558
+ __Pyx_GOTREF(value);
559
+ __Pyx_XDECREF_SET(*subject, value);
560
+ }
561
+ }
562
+ return 1; // success
563
+ }
564
+
565
+ ///////////////////////// ExtractNonDict.proto ////////////////////////////////
566
+
567
+ // The subjects array is a list of PyObject** to subjects to be filled. They may be NULL
568
+ // in which case they're ignored.
569
+ //
570
+ // This is a specialized version for the rarer case when the type isn't an exact dict.
571
+
572
+ #if CYTHON_REFNANNY
573
+ #define __Pyx_MatchCase_Mapping_ExtractNonDict(...) __Pyx__MatchCase_Mapping_ExtractNonDict(__pyx_refnanny, __VA_ARGS__)
574
+ #else
575
+ #define __Pyx_MatchCase_Mapping_ExtractNonDict(...) __Pyx__MatchCase_Mapping_ExtractNonDict(NULL, __VA_ARGS__)
576
+ #endif
577
+ static CYTHON_INLINE int __Pyx__MatchCase_Mapping_ExtractNonDict(void *__pyx_refnanny, PyObject *mapping, PyObject *keys[], Py_ssize_t nKeys, PyObject **subjects[]); /* proto */
578
+
579
+ ///////////////////////// ExtractNonDict //////////////////////////////////////
580
+ //@requires: ObjectHandling.c::PyObjectCall2Args
581
+
582
+ // largely adapted from match_keys in CPython ceval.c
583
+
584
+ static int __Pyx__MatchCase_Mapping_ExtractNonDict(void *__pyx_refnanny, PyObject *mapping, PyObject *keys[], Py_ssize_t nKeys, PyObject **subjects[]) {
585
+ PyObject *dummy=NULL, *get=NULL;
586
+ Py_ssize_t i, size;
587
+ int result = 0;
588
+ #if CYTHON_UNPACK_METHODS && CYTHON_VECTORCALL
589
+ PyObject *get_method = NULL, *get_self = NULL;
590
+ #endif
591
+
592
+ #if !CYTHON_REFNANNY
593
+ CYTHON_UNUSED_VAR(__pyx_refnanny);
594
+ #endif
595
+
596
+ // Length check is undocumented but does take place in CPython and is probably worthwhile.
597
+ size = PyObject_Length(mapping);
598
+ if (size < nKeys) {
599
+ return size == -1 ? -1: 0;
600
+ }
601
+
602
+ dummy = PyObject_CallObject((PyObject *)&PyBaseObject_Type, NULL);
603
+ if (!dummy) {
604
+ return -1;
605
+ }
606
+ get = PyObject_GetAttr(mapping, PYIDENT("get"));
607
+ if (!get) {
608
+ result = -1;
609
+ goto end;
610
+ }
611
+ #if CYTHON_UNPACK_METHODS && CYTHON_VECTORCALL
612
+ if (likely(PyMethod_Check(get))) {
613
+ // both of these are borrowed
614
+ get_method = PyMethod_GET_FUNCTION(get);
615
+ get_self = PyMethod_GET_SELF(get);
616
+ }
617
+ #endif
618
+
619
+ for (i=0; i<nKeys; ++i) {
620
+ PyObject **subject;
621
+ PyObject *value = NULL;
622
+ PyObject *key = keys[i];
623
+
624
+ // TODO - there's an optimization here (although it deviates from the strict definition of pattern matching).
625
+ // If we don't need the values then we can call PyObject_Contains instead of "get". If we don't need *any*
626
+ // of the values then we can skip initialization "get" and "dummy"
627
+ #if CYTHON_UNPACK_METHODS && CYTHON_VECTORCALL
628
+ if (likely(get_method)) {
629
+ PyObject *args[] = { get_self, key, dummy };
630
+ value = PyObject_Vectorcall(get_method, args, 3, NULL);
631
+ }
632
+ else
633
+ #endif
634
+ {
635
+ value = __Pyx_PyObject_Call2Args(get, key, dummy);
636
+ }
637
+ if (!value) {
638
+ result = -1;
639
+ goto end;
640
+ } else if (value == dummy) {
641
+ Py_DECREF(value);
642
+ goto end; // failed to match.
643
+ } else {
644
+ subject = subjects[i];
645
+ if (subject) {
646
+ __Pyx_XDECREF_SET(*subject, value);
647
+ __Pyx_GOTREF(*subject);
648
+ } else {
649
+ Py_DECREF(value);
650
+ }
651
+ }
652
+ }
653
+ result = 1;
654
+
655
+ end:
656
+ Py_XDECREF(dummy);
657
+ Py_XDECREF(get);
658
+ return result;
659
+ }
660
+
661
+ ///////////////////////// ExtractGeneric.proto ////////////////////////////////
662
+
663
+ #if CYTHON_REFNANNY
664
+ #define __Pyx_MatchCase_Mapping_Extract(...) __Pyx__MatchCase_Mapping_Extract(__pyx_refnanny, __VA_ARGS__)
665
+ #else
666
+ #define __Pyx_MatchCase_Mapping_Extract(...) __Pyx__MatchCase_Mapping_Extract(NULL, __VA_ARGS__)
667
+ #endif
668
+ static CYTHON_INLINE int __Pyx__MatchCase_Mapping_Extract(void *__pyx_refnanny, PyObject *mapping, PyObject *keys[], Py_ssize_t nKeys, PyObject **subjects[]); /* proto */
669
+
670
+ ////////////////////// ExtractGeneric //////////////////////////////////////
671
+ //@requires: ExtractExactDict
672
+ //@requires: ExtractNonDict
673
+ //@requires: Builtins.c::PyFrozenDict
674
+
675
+ static CYTHON_INLINE int __Pyx__MatchCase_Mapping_Extract(void *__pyx_refnanny, PyObject *mapping, PyObject *keys[], Py_ssize_t nKeys, PyObject **subjects[]) {
676
+ if (__Pyx_PyAnyDict_CheckExact(mapping)) {
677
+ return __Pyx__MatchCase_Mapping_ExtractDict(__pyx_refnanny, mapping, keys, nKeys, subjects);
678
+ } else {
679
+ return __Pyx__MatchCase_Mapping_ExtractNonDict(__pyx_refnanny, mapping, keys, nKeys, subjects);
680
+ }
681
+ }
682
+
683
+ ///////////////////////////// DoubleStarCapture.proto //////////////////////
684
+
685
+ static PyObject* __Pyx_MatchCase_DoubleStarCapture{{tag}}(PyObject *mapping, PyObject *keys[], Py_ssize_t nKeys); /* proto */
686
+
687
+ //////////////////////////// DoubleStarCapture //////////////////////////////
688
+ //@requires: Builtins.c::PyFrozenDict
689
+
690
+ // The implementation is largely copied from the original COPY_DICT_WITHOUT_KEYS opcode
691
+ // implementation of CPython
692
+ // https://github.com/python/cpython/blob/145bf269df3530176f6ebeab1324890ef7070bf8/Python/ceval.c#L3977
693
+ // (now removed in favour of building the same thing from a combination of opcodes)
694
+ // The differences are:
695
+ // 1. We use an array of keys rather than a tuple of keys
696
+ // 2. We add a shortcut for when there will be no left over keys (because I guess it's pretty common)
697
+ //
698
+ // Tempita variable 'tag' can be "NonDict", "ExactDict", "ExactFrozenDict" or empty
699
+
700
+ static PyObject* __Pyx_MatchCase_DoubleStarCapture{{tag}}(PyObject *mapping, PyObject *keys[], Py_ssize_t nKeys) {
701
+ PyObject *dict_out;
702
+ Py_ssize_t i, size;
703
+
704
+ // shortcut for when there are no left-over keys
705
+ {{if tag=="ExactDict" or tag=="ExactFrozenDict"}}
706
+ if ((1))
707
+ {{else}}
708
+ if (__Pyx_PyAnyDict_CheckExact(mapping))
709
+ {{endif}}
710
+ {
711
+ size = PyDict_Size(mapping);
712
+ } else {
713
+ size = PyObject_Length(mapping);
714
+ }
715
+ if (unlikely(size == -1)) return NULL;
716
+ if (size == nKeys) {
717
+ return PyDict_New();
718
+ }
719
+
720
+ {{if tag=="ExactDict"}}
721
+ dict_out = PyDict_Copy(mapping);
722
+ {{else}}
723
+ dict_out = PyDict_New();
724
+ {{endif}}
725
+ if (!dict_out) {
726
+ return NULL;
727
+ }
728
+ {{if tag!="ExactDict"}}
729
+ if (PyDict_Update(dict_out, mapping)) {
730
+ Py_DECREF(dict_out);
731
+ return NULL;
732
+ }
733
+ {{endif}}
734
+
735
+ for (i=0; i<nKeys; ++i) {
736
+ if (PyDict_DelItem(dict_out, keys[i])) {
737
+ Py_DECREF(dict_out);
738
+ return NULL;
739
+ }
740
+ }
741
+ return dict_out;
742
+ }
743
+
744
+ ////////////////////////////// ClassPositionalPatterns.proto ////////////////////////
745
+
746
+ #if CYTHON_REFNANNY
747
+ #define __Pyx_MatchCase_ClassPositional(...) __Pyx__MatchCase_ClassPositional(__pyx_refnanny, __VA_ARGS__)
748
+ #else
749
+ #define __Pyx_MatchCase_ClassPositional(...) __Pyx__MatchCase_ClassPositional(NULL, __VA_ARGS__)
750
+ #endif
751
+ static int __Pyx__MatchCase_ClassPositional(void *__pyx_refnanny, PyObject *subject, PyObject *type, PyObject *fixed_names[], Py_ssize_t n_fixed, int match_self, PyObject **subjects[], Py_ssize_t n_subjects); /* proto */
752
+
753
+ /////////////////////////// ClassPositionalPatterns //////////////////////////////
754
+ //@requires: ObjectHandling.c::RaiseErrorWithObjectTypes
755
+ //@requires: ObjectHandling.c::RaiseErrorWithObjectType
756
+ //@requires: ObjectHandling.c::RaiseErrorWithTypeAndVarargs
757
+ //@requires: Builtins.c::PyFrozenDict
758
+
759
+ static int __Pyx_MatchCase_ClassCheckDuplicateAttrs(PyTypeObject *type, PyObject *fixed_names[], Py_ssize_t n_fixed, PyObject *match_args, Py_ssize_t num_args) {
760
+ // a lot of the basic logic of this is shared with __Pyx_MatchCase_CheckMappingDuplicateKeys
761
+ // but they take different input types so it isn't easy to actually share the code.
762
+
763
+ // Inputs are tuples, and typically fairly small. It may be more efficient to
764
+ // loop over the tuple than create a set.
765
+
766
+ PyObject *attrs_set;
767
+ PyObject *attr = NULL;
768
+ Py_ssize_t n, match_args_size;
769
+ int contains;
770
+
771
+ match_args_size = __Pyx_PyTuple_GET_SIZE(match_args);
772
+ if (n_fixed == 0 && match_args_size == 1) {
773
+ // Trivial cases where there can be no duplicates.
774
+ // (In the match_args_size == 0 trivial case, this function is never called).
775
+ return 0;
776
+ }
777
+ #if !CYTHON_ASSUME_SAFE_SIZE
778
+ if (unlikely(match_args_size < 0)) return -1;
779
+ #endif
780
+
781
+ attrs_set = PySet_New(NULL);
782
+ if (unlikely(!attrs_set)) return -1;
783
+
784
+ num_args = match_args_size < num_args ? match_args_size : num_args;
785
+ for (n=0; n < num_args; ++n) {
786
+ attr = __Pyx_PyTuple_GET_ITEM(match_args, n);
787
+ #if !CYTHON_ASSUME_SAFE_MACROS
788
+ if (unlikely(!attr)) goto bad;
789
+ #endif
790
+ contains = PySet_Contains(attrs_set, attr);
791
+ if (contains < 0) {
792
+ goto bad;
793
+ } else if (contains == 1) {
794
+ goto raise_error;
795
+ } else {
796
+ if (PySet_Add(attrs_set, attr)) {
797
+ goto bad;
798
+ }
799
+ }
800
+ }
801
+ for (n=0; n < n_fixed; ++n) {
802
+ attr = fixed_names[n];
803
+ contains = PySet_Contains(attrs_set, attr);
804
+ if (contains < 0) {
805
+ goto bad;
806
+ } else if (contains == 1) {
807
+ goto raise_error;
808
+ }
809
+ }
810
+ Py_DECREF(attrs_set);
811
+ return 0;
812
+
813
+ raise_error:
814
+ __Pyx_RaiseErrorWithTypeAndVarargs(
815
+ PyExc_TypeError,
816
+ __Pyx_FMT_TYPENAME "() got multiple sub-patterns for attribute %R",
817
+ type, attr
818
+ );
819
+ bad:
820
+ Py_DECREF(attrs_set);
821
+ return -1;
822
+ }
823
+
824
+ // Adapted from ceval.c "match_class" in CPython
825
+ //
826
+ // The argument match_self can equal 1 for "known to be true"
827
+ // 0 for "known to be false"
828
+ // -1 for "unknown", runtime test
829
+ // n_subjects is >= 0 otherwise this function will be skipped
830
+ static int __Pyx__MatchCase_ClassPositional(void *__pyx_refnanny, PyObject *subject, PyObject *type_o, PyObject *fixed_names[], Py_ssize_t n_fixed, int match_self, PyObject **subjects[], Py_ssize_t n_subjects)
831
+ {
832
+ PyObject *match_args = NULL;
833
+ Py_ssize_t allowed, i;
834
+ int result;
835
+
836
+ assert(PyType_Check(type_o));
837
+
838
+ PyTypeObject *type = (PyTypeObject*)type_o;
839
+
840
+ #if !CYTHON_REFNANNY
841
+ CYTHON_UNUSED_VAR(__pyx_refnanny);
842
+ #endif
843
+
844
+ if (match_self != 1) {
845
+ #if __PYX_LIMITED_VERSION_HEX >= 0x030d0000
846
+ if (PyObject_GetOptionalAttr(type_o, PYIDENT("__match_args__"), &match_args) == -1) {
847
+ return -1;
848
+ }
849
+ #else
850
+ match_args = PyObject_GetAttr(type_o, PYIDENT("__match_args__"));
851
+ if (!match_args) {
852
+ if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
853
+ PyErr_Clear();
854
+ } else {
855
+ return -1;
856
+ }
857
+ }
858
+ #endif
859
+ }
860
+ if (match_args) {
861
+ match_self = 0;
862
+ if (!PyTuple_CheckExact(match_args)) {
863
+ __Pyx_RaiseTypeErrorWithTypes(
864
+ __Pyx_FMT_TYPENAME ".__match_args__ must be a tuple (got " __Pyx_FMT_TYPENAME ")",
865
+ type, Py_TYPE(match_args));
866
+ Py_DECREF(match_args);
867
+ return -1;
868
+ }
869
+ } else if (!match_args && match_self == -1) {
870
+ // Mysteriously, this private flag seems to have ended up defined in the Limited API
871
+ #if defined(_Py_TPFLAGS_MATCH_SELF) && !CYTHON_COMPILING_IN_PYPY && !(CYTHON_COMPILING_IN_LIMITED_API && __PYX_LIMITED_VERSION_HEX < 0x030A0000)
872
+ match_self = PyType_HasFeature(type, _Py_TPFLAGS_MATCH_SELF);
873
+ #else
874
+ // probably an earlier version of Python. Go off the known list in the specification
875
+ match_self = ((PyType_GetFlags(type) &
876
+ // long should capture bool too
877
+ (Py_TPFLAGS_LONG_SUBCLASS | Py_TPFLAGS_LIST_SUBCLASS | Py_TPFLAGS_TUPLE_SUBCLASS |
878
+ Py_TPFLAGS_BYTES_SUBCLASS | Py_TPFLAGS_UNICODE_SUBCLASS | Py_TPFLAGS_DICT_SUBCLASS
879
+ )) ||
880
+ PyType_IsSubtype(type, &PyByteArray_Type) ||
881
+ PyType_IsSubtype(type, &PyFloat_Type) ||
882
+ PyType_IsSubtype(type, &PyFrozenSet_Type)
883
+ #if CYTHON_COMPILING_IN_LIMITED_API || PY_VERSION_HEX >= 0x030F0000
884
+ || PyType_IsSubtype(type, __Pyx_PyFrozenDict_TypePtr)
885
+ #endif
886
+ );
887
+ #endif
888
+ }
889
+
890
+ if (match_self) {
891
+ allowed = 1;
892
+ } else if (match_args) {
893
+ allowed = __Pyx_PyTuple_GET_SIZE(match_args);
894
+ #if !CYTHON_ASSUME_SAFE_SIZE
895
+ if (unlikely(allowed < 0)) goto end;
896
+ #endif
897
+ } else {
898
+ allowed = 0;
899
+ }
900
+ if (unlikely(allowed < n_subjects)) {
901
+ const char *plural = (allowed == 1) ? "" : "s";
902
+ __Pyx_RaiseErrorWithTypeAndVarargs(
903
+ PyExc_TypeError,
904
+ __Pyx_FMT_TYPENAME "() accepts %d positional sub-pattern%s (%d given)",
905
+ type, allowed, plural, n_subjects
906
+ );
907
+ Py_XDECREF(match_args);
908
+ return -1;
909
+ }
910
+ if (match_self) {
911
+ PyObject **self_subject = subjects[0];
912
+ if (self_subject) {
913
+ // Easy. Copy the subject itself, and move on to kwargs.
914
+ __Pyx_XDECREF_SET(*self_subject, subject);
915
+ __Pyx_INCREF(*self_subject);
916
+ }
917
+ result = 1;
918
+ goto end_match_self;
919
+ }
920
+ // next stage is to check for duplicate attributes.
921
+ if (__Pyx_MatchCase_ClassCheckDuplicateAttrs(type, fixed_names, n_fixed, match_args, n_subjects)) {
922
+ result = -1;
923
+ goto end;
924
+ }
925
+
926
+ for (i = 0; i < n_subjects; i++) {
927
+ PyObject *attr;
928
+ PyObject **subject_i;
929
+ PyObject *name = __Pyx_PyTuple_GET_ITEM(match_args, i);
930
+ #if !CYTHON_ASSUME_SAFE_MACROS
931
+ if (unlikely(!name)) {
932
+ result = -1;
933
+ goto end;
934
+ }
935
+ #endif
936
+ if (!PyUnicode_CheckExact(name)) {
937
+ __Pyx_RaiseTypeErrorWithObjectType(
938
+ "__match_args__ elements must be strings (got " __Pyx_FMT_TYPENAME ")",
939
+ name);
940
+ result = -1;
941
+ goto end;
942
+ }
943
+
944
+ attr = PyObject_GetAttr(subject, name);
945
+ if (attr == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
946
+ PyErr_Clear();
947
+ result = 0;
948
+ goto end;
949
+ }
950
+ subject_i = subjects[i];
951
+ if (subject_i) {
952
+ __Pyx_XDECREF_SET(*subject_i, attr);
953
+ __Pyx_GOTREF(attr);
954
+ } else {
955
+ Py_DECREF(attr);
956
+ }
957
+ }
958
+ result = 1;
959
+
960
+ end:
961
+ Py_DECREF(match_args);
962
+ end_match_self: // because match_args isn't set
963
+ return result;
964
+ }
965
+
966
+ //////////////////////// MatchClassTypeGuard.proto /////////////////////////////
967
+
968
+ static PyObject* __Pyx_MatchCase_TypeGuard(PyObject* type); /* proto */
969
+
970
+ //////////////////////// MatchClassTypeGuard /////////////////////////////
971
+
972
+ static PyObject* __Pyx_MatchCase_TypeGuard(PyObject* type) {
973
+ if (!PyType_Check(type)) {
974
+ PyErr_Format(PyExc_TypeError, "called match pattern must be a type");
975
+ return NULL;
976
+ }
977
+ Py_INCREF(type);
978
+ return type;
979
+ }