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
Cython/Utility/Optimize.c CHANGED
@@ -26,56 +26,108 @@ static CYTHON_INLINE int __Pyx_PyObject_Append(PyObject* L, PyObject* x) {
26
26
  return 0;
27
27
  }
28
28
 
29
+
30
+ /////////////// ListAppendAndDecrefInternal ///////////////
31
+
32
+ #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS && CYTHON_ASSUME_SAFE_SIZE
33
+ static CYTHON_INLINE void __Pyx__ListComp_AppendAndDecref(PyObject* list, Py_ssize_t len, PyObject* x) {
34
+ #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030d0000
35
+ // In Py3.13a1, PyList_SET_ITEM() checks that the end index is lower than the current size.
36
+ // However, extending the size *before* setting the value would not be correct,
37
+ // so we cannot call PyList_SET_ITEM().
38
+ PyListObject* L = (PyListObject*) list;
39
+ L->ob_item[len] = x;
40
+ #else
41
+ PyList_SET_ITEM(list, len, x);
42
+ #endif
43
+ Py_SET_SIZE(list, len + 1);
44
+ }
45
+ #endif
46
+
47
+
29
48
  /////////////// ListAppend.proto ///////////////
30
49
 
31
- #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS
50
+ #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS && CYTHON_ASSUME_SAFE_SIZE
51
+ static CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x); /*proto*/
52
+ #else
53
+ #define __Pyx_PyList_Append(L,x) PyList_Append(L,x)
54
+ #endif
55
+
56
+ /////////////// ListAppend ///////////////
57
+ //@requires: ListAppendAndDecrefInternal
58
+
59
+ #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS && CYTHON_ASSUME_SAFE_SIZE
32
60
  static CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x) {
33
61
  PyListObject* L = (PyListObject*) list;
34
62
  Py_ssize_t len = Py_SIZE(list);
63
+
35
64
  if (likely(L->allocated > len) & likely(len > (L->allocated >> 1))) {
36
65
  Py_INCREF(x);
37
- #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030d0000
38
- // In Py3.13a1, PyList_SET_ITEM() checks that the end index is lower than the current size.
39
- // However, extending the size *before* setting the value would not be correct,
40
- // so we cannot call PyList_SET_ITEM().
41
- L->ob_item[len] = x;
42
- #else
43
- PyList_SET_ITEM(list, len, x);
44
- #endif
45
- __Pyx_SET_SIZE(list, len + 1);
66
+ __Pyx__ListComp_AppendAndDecref(list, len, x);
46
67
  return 0;
47
68
  }
48
69
  return PyList_Append(list, x);
49
70
  }
50
- #else
51
- #define __Pyx_PyList_Append(L,x) PyList_Append(L,x)
52
71
  #endif
53
72
 
73
+
54
74
  /////////////// ListCompAppend.proto ///////////////
55
75
 
56
- #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS
76
+ #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS && CYTHON_ASSUME_SAFE_SIZE
77
+ static CYTHON_INLINE int __Pyx_ListComp_Append(PyObject* list, PyObject* x); /*proto*/
78
+ #else
79
+ #define __Pyx_ListComp_Append(L,x) PyList_Append(L,x)
80
+ #endif
81
+
82
+ /////////////// ListCompAppend ///////////////
83
+ //@requires: ListAppendAndDecrefInternal
84
+
85
+ #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS && CYTHON_ASSUME_SAFE_SIZE
57
86
  static CYTHON_INLINE int __Pyx_ListComp_Append(PyObject* list, PyObject* x) {
58
87
  PyListObject* L = (PyListObject*) list;
59
88
  Py_ssize_t len = Py_SIZE(list);
89
+
60
90
  if (likely(L->allocated > len)) {
61
91
  Py_INCREF(x);
62
- #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030d0000
63
- // In Py3.13a1, PyList_SET_ITEM() checks that the end index is lower than the current size.
64
- // However, extending the size *before* setting the value would not be correct,
65
- // so we cannot call PyList_SET_ITEM().
66
- L->ob_item[len] = x;
67
- #else
68
- PyList_SET_ITEM(list, len, x);
69
- #endif
70
- __Pyx_SET_SIZE(list, len + 1);
92
+ __Pyx__ListComp_AppendAndDecref(list, len, x);
71
93
  return 0;
72
94
  }
73
95
  return PyList_Append(list, x);
74
96
  }
97
+ #endif
98
+
99
+
100
+ /////////////// ListCompAppendAndDecref.proto ///////////////
101
+
102
+ static CYTHON_INLINE int __Pyx_ListComp_AppendAndDecref(PyObject* list, PyObject* x); /*proto*/
103
+
104
+ /////////////// ListCompAppendAndDecref ///////////////
105
+ //@requires: ListAppendAndDecrefInternal
106
+
107
+ #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS && CYTHON_ASSUME_SAFE_SIZE
108
+ static CYTHON_INLINE int __Pyx_ListComp_AppendAndDecref(PyObject* list, PyObject* x) {
109
+ PyListObject* L = (PyListObject*) list;
110
+ Py_ssize_t len = Py_SIZE(list);
111
+
112
+ if (likely(L->allocated > len)) {
113
+ __Pyx__ListComp_AppendAndDecref(list, len, x);
114
+ return 0;
115
+ }
116
+
117
+ int result = PyList_Append(list, x);
118
+ Py_DECREF(x);
119
+ return result;
120
+ }
121
+
75
122
  #else
76
- #define __Pyx_ListComp_Append(L,x) PyList_Append(L,x)
123
+ static CYTHON_INLINE int __Pyx_ListComp_AppendAndDecref(PyObject* list, PyObject* x) {
124
+ int result = PyList_Append(list, x);
125
+ Py_DECREF(x);
126
+ return result;
127
+ }
77
128
  #endif
78
129
 
130
+
79
131
  //////////////////// ListExtend.proto ////////////////////
80
132
 
81
133
  static CYTHON_INLINE int __Pyx_PyList_Extend(PyObject* L, PyObject* v) {
@@ -110,7 +162,7 @@ static CYTHON_INLINE PyObject* __Pyx_PyList_Pop(PyObject* L); /*proto*/
110
162
  //@requires: ObjectHandling.c::PyObjectCallMethod0
111
163
 
112
164
  static CYTHON_INLINE PyObject* __Pyx__PyObject_Pop(PyObject* L) {
113
- if (__Pyx_IS_TYPE(L, &PySet_Type)) {
165
+ if (Py_IS_TYPE(L, &PySet_Type)) {
114
166
  return PySet_Pop(L);
115
167
  }
116
168
  return __Pyx_PyObject_CallMethod0(L, PYIDENT("pop"));
@@ -120,7 +172,7 @@ static CYTHON_INLINE PyObject* __Pyx__PyObject_Pop(PyObject* L) {
120
172
  static CYTHON_INLINE PyObject* __Pyx_PyList_Pop(PyObject* L) {
121
173
  /* Check that both the size is positive and no reallocation shrinking needs to be done. */
122
174
  if (likely(PyList_GET_SIZE(L) > (((PyListObject*)L)->allocated >> 1))) {
123
- __Pyx_SET_SIZE(L, Py_SIZE(L) - 1);
175
+ Py_SET_SIZE(L, Py_SIZE(L) - 1);
124
176
  return PyList_GET_ITEM(L, PyList_GET_SIZE(L));
125
177
  }
126
178
  return CALL_UNBOUND_METHOD(PyList_Type, "pop", L);
@@ -183,7 +235,7 @@ static PyObject* __Pyx__PyList_PopIndex(PyObject* L, PyObject* py_ix, Py_ssize_t
183
235
  }
184
236
  if (likely(__Pyx_is_valid_index(cix, size))) {
185
237
  PyObject* v = PyList_GET_ITEM(L, cix);
186
- __Pyx_SET_SIZE(L, Py_SIZE(L) - 1);
238
+ Py_SET_SIZE(L, Py_SIZE(L) - 1);
187
239
  size -= 1;
188
240
  memmove(&PyList_GET_ITEM(L, cix), &PyList_GET_ITEM(L, cix+1), (size_t)(size-cix)*sizeof(PyObject*));
189
241
  return v;
@@ -302,70 +354,31 @@ static CYTHON_INLINE int __Pyx_PyDict_Pop_ignore(PyObject *d, PyObject *key, PyO
302
354
  }
303
355
 
304
356
 
305
- /////////////// dict_iter.proto ///////////////
306
-
307
- static CYTHON_INLINE PyObject* __Pyx_dict_iterator(PyObject* dict, int is_dict, PyObject* method_name,
308
- Py_ssize_t* p_orig_length, int* p_is_dict);
357
+ /////////////// dict_iter_common.proto ///////////////
358
+ static PyObject *__Pyx_dict_call_to_get_iterable(PyObject* iterable, PyObject* method_name); /* proto */
309
359
  static CYTHON_INLINE int __Pyx_dict_iter_next(PyObject* dict_or_iter, Py_ssize_t orig_length, Py_ssize_t* ppos,
310
360
  PyObject** pkey, PyObject** pvalue, PyObject** pitem, int is_dict);
311
361
 
312
- /////////////// dict_iter ///////////////
362
+ /////////////// dict_iter_common ///////////////
313
363
  //@requires: ObjectHandling.c::UnpackTuple2
314
364
  //@requires: ObjectHandling.c::IterFinish
315
365
  //@requires: ObjectHandling.c::PyObjectCallMethod0
316
366
 
317
- #if CYTHON_AVOID_BORROWED_REFS
318
- #include <string.h>
319
- #endif
367
+ static PyObject *__Pyx_dict_call_to_get_iterable(PyObject* iterable, PyObject* method_name) {
320
368
 
321
- static CYTHON_INLINE PyObject* __Pyx_dict_iterator(PyObject* iterable, int is_dict, PyObject* method_name,
322
- Py_ssize_t* p_orig_length, int* p_source_is_dict) {
323
- is_dict = is_dict || likely(PyDict_CheckExact(iterable));
324
- *p_source_is_dict = is_dict;
325
- if (is_dict) {
369
+ PyObject* iter;
370
+ iterable = __Pyx_PyObject_CallMethod0(iterable, method_name);
371
+ if (!iterable)
372
+ return NULL;
326
373
  #if !CYTHON_AVOID_BORROWED_REFS
327
- *p_orig_length = PyDict_Size(iterable);
328
- Py_INCREF(iterable);
374
+ if (PyTuple_CheckExact(iterable) || PyList_CheckExact(iterable))
329
375
  return iterable;
330
- #else
331
- // On PyPy3/GraalPy, we need to translate manually a few method names.
332
- // This logic is not needed on CPython thanks to the fast case above.
333
- static PyObject *py_items = NULL, *py_keys = NULL, *py_values = NULL;
334
- PyObject **pp = NULL;
335
- if (method_name) {
336
- const char *name = PyUnicode_AsUTF8(method_name);
337
- if (strcmp(name, "iteritems") == 0) pp = &py_items;
338
- else if (strcmp(name, "iterkeys") == 0) pp = &py_keys;
339
- else if (strcmp(name, "itervalues") == 0) pp = &py_values;
340
- if (pp) {
341
- if (!*pp) {
342
- *pp = PyUnicode_FromString(name + 4);
343
- if (!*pp)
344
- return NULL;
345
- }
346
- method_name = *pp;
347
- }
348
- }
349
- #endif
350
- }
351
- *p_orig_length = 0;
352
- if (method_name) {
353
- PyObject* iter;
354
- iterable = __Pyx_PyObject_CallMethod0(iterable, method_name);
355
- if (!iterable)
356
- return NULL;
357
- #if !CYTHON_AVOID_BORROWED_REFS
358
- if (PyTuple_CheckExact(iterable) || PyList_CheckExact(iterable))
359
- return iterable;
360
376
  #endif
361
- iter = PyObject_GetIter(iterable);
362
- Py_DECREF(iterable);
363
- return iter;
364
- }
365
- return PyObject_GetIter(iterable);
377
+ iter = PyObject_GetIter(iterable);
378
+ Py_DECREF(iterable);
379
+ return iter;
366
380
  }
367
381
 
368
-
369
382
  #if !CYTHON_AVOID_BORROWED_REFS
370
383
  static CYTHON_INLINE int __Pyx_dict_iter_next_source_is_dict(
371
384
  PyObject* iter_obj, CYTHON_NCP_UNUSED Py_ssize_t orig_length, CYTHON_NCP_UNUSED Py_ssize_t* ppos,
@@ -477,6 +490,81 @@ static CYTHON_INLINE int __Pyx_dict_iter_next(
477
490
  return 1;
478
491
  }
479
492
 
493
+ /////////////// dict_iter_legacy.proto //////////////
494
+
495
+ // "legacy" handles old-style iter* methods
496
+ static CYTHON_INLINE PyObject* __Pyx_dict_iterator_legacy(PyObject* dict, int is_dict, PyObject* method_name,
497
+ Py_ssize_t* p_orig_length, int* p_is_dict);
498
+
499
+ /////////////// dict_iter_legacy //////////////////
500
+ //@requires: dict_iter_common
501
+
502
+ #if CYTHON_AVOID_BORROWED_REFS
503
+ #include <string.h>
504
+ #endif
505
+
506
+ static CYTHON_INLINE PyObject* __Pyx_dict_iterator_legacy(PyObject* iterable, int is_dict, PyObject* method_name,
507
+ Py_ssize_t* p_orig_length, int* p_source_is_dict) {
508
+ int owned_method_name = 0;
509
+ // Don't include frozendict.
510
+ is_dict = is_dict || likely(PyDict_CheckExact(iterable));
511
+ *p_source_is_dict = is_dict;
512
+ if (is_dict) {
513
+ #if !CYTHON_AVOID_BORROWED_REFS
514
+ *p_orig_length = PyDict_Size(iterable);
515
+ Py_INCREF(iterable);
516
+ return iterable;
517
+ #else
518
+ // On PyPy3/GraalPy, we need to translate manually the method name.
519
+ // This logic is not needed on CPython thanks to the fast case above.
520
+ if (method_name) {
521
+ method_name = PyUnicode_Substring(method_name, 4, 10); // longest is "itervalues" (len=10)
522
+ if (unlikely(!method_name))
523
+ return NULL;
524
+ owned_method_name = 1;
525
+ }
526
+ #endif
527
+ }
528
+ *p_orig_length = 0;
529
+ if (method_name) {
530
+ iterable = __Pyx_dict_call_to_get_iterable(iterable, method_name);
531
+ if (owned_method_name) {
532
+ Py_DECREF(method_name);
533
+ }
534
+ return iterable;
535
+ } else {
536
+ return PyObject_GetIter(iterable);
537
+ }
538
+ }
539
+
540
+ /////////////// dict_iter.proto ///////////////
541
+
542
+ static CYTHON_INLINE PyObject* __Pyx_dict_iterator(PyObject* dict, int is_dict, PyObject* method_name,
543
+ Py_ssize_t* p_orig_length, int* p_is_dict);
544
+
545
+ /////////////// dict_iter ///////////////
546
+ //@requires: Builtins.c::PyFrozenDict
547
+ //@requires: dict_iter_common
548
+
549
+ static CYTHON_INLINE PyObject* __Pyx_dict_iterator(PyObject* iterable, int is_dict, PyObject* method_name,
550
+ Py_ssize_t* p_orig_length, int* p_source_is_dict) {
551
+ is_dict = is_dict || likely(__Pyx_PyAnyDict_CheckExact(iterable));
552
+ *p_source_is_dict = is_dict;
553
+ #if !CYTHON_AVOID_BORROWED_REFS
554
+ if (is_dict) {
555
+ *p_orig_length = PyDict_Size(iterable);
556
+ Py_INCREF(iterable);
557
+ return iterable;
558
+ }
559
+ #endif
560
+ *p_orig_length = 0;
561
+ if (method_name) {
562
+ return __Pyx_dict_call_to_get_iterable(iterable, method_name);
563
+ } else {
564
+ return PyObject_GetIter(iterable);
565
+ }
566
+ }
567
+
480
568
 
481
569
  /////////////// set_iter.proto ///////////////
482
570
 
@@ -658,6 +746,7 @@ static double __Pyx__PyObject_AsDouble(PyObject* obj); /* proto */
658
746
  //@requires: pybytes_as_double
659
747
  //@requires: pyunicode_as_double
660
748
  //@requires: ObjectHandling.c::PyObjectCallOneArg
749
+ //@requires: ObjectHandling.c::RaiseErrorWithObjectType
661
750
 
662
751
  static double __Pyx__PyObject_AsDouble(PyObject* obj) {
663
752
  if (PyUnicode_CheckExact(obj)) {
@@ -677,11 +766,9 @@ static double __Pyx__PyObject_AsDouble(PyObject* obj) {
677
766
  if (likely(nb) && likely(nb->nb_float)) {
678
767
  float_value = nb->nb_float(obj);
679
768
  if (likely(float_value) && unlikely(!PyFloat_Check(float_value))) {
680
- __Pyx_TypeName float_value_type_name = __Pyx_PyType_GetFullyQualifiedName(Py_TYPE(float_value));
681
- PyErr_Format(PyExc_TypeError,
769
+ __Pyx_RaiseTypeErrorWithObjectType(
682
770
  "__float__ returned non-float (type " __Pyx_FMT_TYPENAME ")",
683
- float_value_type_name);
684
- __Pyx_DECREF_TypeName(float_value_type_name);
771
+ float_value);
685
772
  Py_DECREF(float_value);
686
773
  goto bad;
687
774
  }
@@ -787,6 +874,7 @@ static double __Pyx_PyUnicode_AsDouble_WithSpaces(PyObject *obj) {
787
874
  double value;
788
875
  const char *last;
789
876
  char *end;
877
+ int valid_parse;
790
878
  Py_ssize_t start, length = PyUnicode_GET_LENGTH(obj);
791
879
  const int kind = PyUnicode_KIND(obj);
792
880
  const void* data = PyUnicode_DATA(obj);
@@ -802,16 +890,19 @@ static double __Pyx_PyUnicode_AsDouble_WithSpaces(PyObject *obj) {
802
890
 
803
891
  // parse NaN / inf
804
892
  value = __Pyx__PyUnicode_AsDouble_inf_nan(data, kind, start, length);
805
- if (unlikely(value == -1.0)) goto fallback;
806
- if (value != 0.0) return value;
893
+ if (value != 0.0) {
894
+ if (unlikely(value == -1.0)) goto fallback;
895
+ return value;
896
+ }
807
897
 
808
898
  if (length < 40) {
809
899
  char number[40];
810
900
  last = __Pyx__PyUnicode_AsDouble_Copy(data, kind, number, start, start + length);
811
901
  if (unlikely(!last)) goto fallback;
812
902
  value = PyOS_string_to_double(number, &end, NULL);
903
+ valid_parse = (end == last);
813
904
  } else {
814
- char *number = (char*) PyMem_Malloc((length + 1) * sizeof(char));
905
+ char *number = (char*) PyMem_Malloc(((size_t) length + 1) * sizeof(char));
815
906
  if (unlikely(!number)) goto fallback;
816
907
  last = __Pyx__PyUnicode_AsDouble_Copy(data, kind, number, start, start + length);
817
908
  if (unlikely(!last)) {
@@ -819,9 +910,10 @@ static double __Pyx_PyUnicode_AsDouble_WithSpaces(PyObject *obj) {
819
910
  goto fallback;
820
911
  }
821
912
  value = PyOS_string_to_double(number, &end, NULL);
913
+ valid_parse = (end == last);
822
914
  PyMem_Free(number);
823
915
  }
824
- if (likely(end == last) || (value == (double)-1 && PyErr_Occurred())) {
916
+ if (likely(valid_parse) || (value == (double)-1 && PyErr_Occurred())) {
825
917
  return value;
826
918
  }
827
919
  fallback:
@@ -967,6 +1059,7 @@ CYTHON_UNUSED static double __Pyx__PyBytes_AsDouble(PyObject *obj, const char* s
967
1059
  Py_ssize_t i, digits;
968
1060
  const char *last = start + length;
969
1061
  char *end;
1062
+ int valid_parse;
970
1063
 
971
1064
  // strip spaces at start and end
972
1065
  while (__Pyx__PyBytes_AsDouble_IsSpace(*start))
@@ -978,8 +1071,10 @@ CYTHON_UNUSED static double __Pyx__PyBytes_AsDouble(PyObject *obj, const char* s
978
1071
 
979
1072
  // parse NaN / inf
980
1073
  value = __Pyx__PyBytes_AsDouble_inf_nan(start, length);
981
- if (unlikely(value == -1.0)) goto fallback;
982
- if (value != 0.0) return value;
1074
+ if (value != 0.0) {
1075
+ if (unlikely(value == -1.0)) goto fallback;
1076
+ return value;
1077
+ }
983
1078
 
984
1079
  // look for underscores
985
1080
  digits = 0;
@@ -987,13 +1082,15 @@ CYTHON_UNUSED static double __Pyx__PyBytes_AsDouble(PyObject *obj, const char* s
987
1082
 
988
1083
  if (likely(digits == length)) {
989
1084
  value = PyOS_string_to_double(start, &end, NULL);
1085
+ valid_parse = (end == last);
990
1086
  } else if (digits < 40) {
991
1087
  char number[40];
992
1088
  last = __Pyx__PyBytes_AsDouble_Copy(start, number, length);
993
1089
  if (unlikely(!last)) goto fallback;
994
1090
  value = PyOS_string_to_double(number, &end, NULL);
1091
+ valid_parse = (end == last);
995
1092
  } else {
996
- char *number = (char*) PyMem_Malloc((digits + 1) * sizeof(char));
1093
+ char *number = (char*) PyMem_Malloc(((size_t) digits + 1) * sizeof(char));
997
1094
  if (unlikely(!number)) goto fallback;
998
1095
  last = __Pyx__PyBytes_AsDouble_Copy(start, number, length);
999
1096
  if (unlikely(!last)) {
@@ -1001,9 +1098,10 @@ CYTHON_UNUSED static double __Pyx__PyBytes_AsDouble(PyObject *obj, const char* s
1001
1098
  goto fallback;
1002
1099
  }
1003
1100
  value = PyOS_string_to_double(number, &end, NULL);
1101
+ valid_parse = (end == last);
1004
1102
  PyMem_Free(number);
1005
1103
  }
1006
- if (likely(end == last) || (value == (double)-1 && PyErr_Occurred())) {
1104
+ if (likely(valid_parse) || (value == (double)-1 && PyErr_Occurred())) {
1007
1105
  return value;
1008
1106
  }
1009
1107
  fallback:
@@ -1019,6 +1117,7 @@ fallback:
1019
1117
  static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject *none, int inplace); /*proto*/
1020
1118
 
1021
1119
  /////////////// PyNumberPow2 ///////////////
1120
+ //@requires: Exceptions.c::IgnoreException
1022
1121
 
1023
1122
  static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject *none, int inplace) {
1024
1123
  // in CPython, 1<<N is substantially faster than 2**N
@@ -1056,14 +1155,875 @@ static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject
1056
1155
  Py_DECREF(one);
1057
1156
  return result;
1058
1157
  }
1059
- } else if (shiftby == -1 && PyErr_Occurred()) {
1060
- PyErr_Clear();
1158
+ } else if (shiftby == -1) {
1159
+ PyObject *err = PyErr_Occurred();
1160
+ if (err && !__Pyx_IgnoreGivenException(err, PyExc_Exception)) {
1161
+ return NULL; // BaseException
1162
+ }
1061
1163
  }
1062
1164
  fallback:
1063
1165
  #endif
1064
1166
  return (inplace ? PyNumber_InPlacePower : PyNumber_Power)(two, exp, none);
1065
1167
  }
1066
1168
 
1169
+ /////////////// PyNumberBinop.proto ///////////////
1170
+
1171
+ #if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_GRAAL || CYTHON_COMPILING_IN_LIMITED_API
1172
+
1173
+ #define __Pyx_{{op_name}}_{{type1}}_{{type2}}(op1, op2) {{op_name}}(op1, op2)
1174
+ #define __Pyx_{{inplace_op_name}}_{{type1}}_{{type2}}(op1, op2) {{inplace_op_name}}(op1, op2)
1175
+
1176
+ #else
1177
+
1178
+ #define __Pyx_{{op_name}}_{{type1}}_{{type2}}(op1, op2) __Pyx__{{op_name}}_{{type1}}_{{type2}}(op1, op2, 0)
1179
+ #define __Pyx_{{inplace_op_name}}_{{type1}}_{{type2}}(op1, op2) __Pyx__{{op_name}}_{{type1}}_{{type2}}(op1, op2, 1)
1180
+
1181
+ static CYTHON_INLINE PyObject* __Pyx__{{op_name}}_{{type1}}_{{type2}}(PyObject *op1, PyObject *op2, int inplace); /*proto*/
1182
+ #endif
1183
+
1184
+ /////////////// PyNumberBinop ///////////////
1185
+ //@requires: ObjectHandling.c::RaiseErrorWithObjectTypes
1186
+
1187
+ #if !(CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_GRAAL || CYTHON_COMPILING_IN_LIMITED_API)
1188
+
1189
+ {{py: assert type1 in ('object', 'int', 'float'), type1 }}
1190
+ {{py: assert type2 in ('object', 'int', 'float'), type2 }}
1191
+ {{py: slot_name = {'+': 'add', '-': 'subtract', '*': 'multiply', '^': 'xor', '&': 'and', '|': 'or'}[c_op] }}
1192
+ {{py:
1193
+ def is_type(operand, expected, type1=type1, type2=type2):
1194
+ assert operand in ('op1', 'op2'), operand
1195
+ assert expected in ('int', 'float'), type
1196
+ type = type1 if operand == 'op1' else type2
1197
+ if type == expected:
1198
+ check = f"likely({operand} != Py_None)"
1199
+ else:
1200
+ function = "PyFloat_CheckExact" if expected == 'float' else 'PyLong_CheckExact'
1201
+ check = f"{function}({operand})"
1202
+ other_type = type2 if operand == 'op1' else type1
1203
+ if other_type == expected:
1204
+ check = f"likely({check})"
1205
+ return check
1206
+ }}
1207
+
1208
+ {{if c_op in '+-*' or type1 != 'float'}}
1209
+
1210
+ #if CYTHON_USE_TYPE_SLOTS || __PYX_LIMITED_VERSION_HEX >= 0x030A0000
1211
+ #ifndef __Pyx_DEFINED_BinopTypeError
1212
+ #define __Pyx_DEFINED_BinopTypeError
1213
+
1214
+ static void __Pyx_BinopTypeError(PyObject *op1, PyObject *op2, const char* op, int inplace) {
1215
+ // op1 is either 'int' or 'float', op2 is unknown.
1216
+ // op has either 1 or 2 characters, ending with NUL.
1217
+ char opname[4] = {op[0], op[1], 0, 0};
1218
+ if (inplace) {
1219
+ opname[op[1] ? 2 : 1] = '=';
1220
+ }
1221
+ __Pyx_RaiseErrorWithObjectTypes1(
1222
+ PyExc_TypeError,
1223
+ "unsupported operand type(s) for %.3s: '" __Pyx_FMT_TYPENAME "' and '" __Pyx_FMT_TYPENAME "'",
1224
+ opname, op1, op2);
1225
+ }
1226
+ #endif
1227
+ #endif
1228
+
1229
+ {{endif}}
1230
+
1231
+ {{if c_op in '+-*'}}
1232
+
1233
+ {{if type1 in ('object', 'float')}}
1234
+ #ifndef __Pyx_DEFINED_{{op_name}}_xfloat_{{type2}}
1235
+ #define __Pyx_DEFINED_{{op_name}}_xfloat_{{type2}}
1236
+
1237
+ static PyObject* __Pyx_{{op_name}}_xfloat_{{type2}}(PyObject *op1, PyObject *op2, int inplace) {
1238
+ {{if type2 in ('object', 'int')}}
1239
+ if ({{is_type('op2', 'int')}}) {
1240
+ double int_op2;
1241
+ #if CYTHON_USE_PYLONG_INTERNALS
1242
+ if (__Pyx_PyLong_IsCompact(op2)) {
1243
+ Py_ssize_t compact_op2 = __Pyx_PyLong_CompactValue(op2);
1244
+ {{if c_op in '+-'}}
1245
+ if (compact_op2 == 0) return __Pyx_NewRef(op1);
1246
+ {{endif}}
1247
+ int_op2 = (double) compact_op2;
1248
+ } else
1249
+ #endif
1250
+ {
1251
+ int_op2 = PyLong_AsDouble(op2);
1252
+ if (unlikely((int_op2 == -1.) && PyErr_Occurred())) return NULL;
1253
+
1254
+ {{if c_op in '+-'}}
1255
+ #if !CYTHON_USE_PYLONG_INTERNALS
1256
+ if (int_op2 == 0.) return __Pyx_NewRef(op1);
1257
+ #endif
1258
+ {{endif}}
1259
+ }
1260
+
1261
+ double float_op1 = __Pyx_PyFloat_AS_DOUBLE(op1);
1262
+ #if !CYTHON_ASSUME_SAFE_MACROS
1263
+ if (unlikely((float_op1 == -1.) && PyErr_Occurred())) return NULL;
1264
+ #endif
1265
+
1266
+ {{if c_op == '*'}}
1267
+ // "0 * op2" could be handled before extracting op2, but that has the potential
1268
+ // to fail for very large numbers in op2 and we'd like to keep that error.
1269
+ if (float_op1 == 0.) return __Pyx_NewRef(op1);
1270
+ {{endif}}
1271
+
1272
+ return PyFloat_FromDouble(float_op1 {{c_op}} int_op2);
1273
+ }
1274
+ {{endif}}
1275
+
1276
+ {{if type2 == 'object'}}
1277
+ if (PyLong_Check(op2)) {
1278
+ // Pass PyLong subclasses to PyFloat-op1.
1279
+ binaryfunc slot_func = __Pyx_PyType_GetSubSlot(&PyFloat_Type, tp_as_number, nb_{{slot_name}}, binaryfunc);
1280
+ if (likely(slot_func)) {
1281
+ return slot_func(op1, op2);
1282
+ }
1283
+ }
1284
+ {{endif}}
1285
+
1286
+ // PyFloat-op1 only handles exact PyFloat/PyLong as op2, everything else is left to op2.
1287
+ // PyLong-op2 is handled above, so we can always pass on to op2 here.
1288
+
1289
+ // Avoid running into non-heap-type problems in Py<3.10 Limited API.
1290
+ #if CYTHON_USE_TYPE_SLOTS || __PYX_LIMITED_VERSION_HEX >= 0x030A0000
1291
+ {
1292
+ PyTypeObject *type_op2 = Py_TYPE(op2);
1293
+ // Reverse operation => ignore 'InPlace' operator since it applies to op1, not op2.
1294
+ binaryfunc slot_func = __Pyx_PyType_GetSubSlot(type_op2, tp_as_number, nb_{{slot_name}}, binaryfunc);
1295
+ if (likely(slot_func)) {
1296
+ PyObject *result = slot_func(op1, op2);
1297
+ if (likely(result != Py_NotImplemented)) {
1298
+ return result;
1299
+ }
1300
+ Py_DECREF(result);
1301
+ }
1302
+ // No "sq_concat" since Python doesn't try it on op2 and 'float' definitely doesn't have it.
1303
+
1304
+ __Pyx_BinopTypeError(op1, op2, "{{c_op}}", inplace);
1305
+ return NULL;
1306
+
1307
+ }
1308
+ #else
1309
+ return (inplace) ? {{inplace_op_name}}(op1, op2) : {{op_name}}(op1, op2);
1310
+ #endif
1311
+ }
1312
+ #endif
1313
+ {{endif}}
1314
+
1315
+ // c_op in '+-*'
1316
+ {{endif}}
1317
+
1318
+ {{if type1 in ('object', 'int')}}
1319
+ #ifndef __Pyx_DEFINED_{{op_name}}_xint_{{type2}}
1320
+ #define __Pyx_DEFINED_{{op_name}}_xint_{{type2}}
1321
+
1322
+ static PyObject* __Pyx_{{op_name}}_xint_{{type2}}(PyObject *op1, PyObject *op2, int inplace) {
1323
+ {{if type2 in ('object', 'float') and c_op in '+-*'}}
1324
+ if ({{is_type('op2', 'float')}}) {
1325
+ double int_op1;
1326
+ #if CYTHON_USE_PYLONG_INTERNALS
1327
+ if (__Pyx_PyLong_IsCompact(op1)) {
1328
+ Py_ssize_t compact_op1 = __Pyx_PyLong_CompactValue(op1);
1329
+ {{if c_op == '+'}}
1330
+ if (compact_op1 == 0) return __Pyx_NewRef(op2);
1331
+ {{endif}}
1332
+ int_op1 = (double) compact_op1;
1333
+ } else
1334
+ #endif
1335
+ {
1336
+ int_op1 = PyLong_AsDouble(op1);
1337
+ if (unlikely((int_op1 == -1.) && PyErr_Occurred())) return NULL;
1338
+
1339
+ {{if c_op == '+'}}
1340
+ #if !CYTHON_USE_PYLONG_INTERNALS
1341
+ if (int_op1 == 0.) return __Pyx_NewRef(op2);
1342
+ #endif
1343
+ {{endif}}
1344
+ }
1345
+
1346
+ double float_op2 = __Pyx_PyFloat_AS_DOUBLE(op2);
1347
+ #if !CYTHON_ASSUME_SAFE_MACROS
1348
+ if (unlikely((float_op2 == -1.) && PyErr_Occurred())) return NULL;
1349
+ #endif
1350
+
1351
+ return PyFloat_FromDouble(int_op1 {{c_op}} float_op2);
1352
+ }
1353
+ {{endif}}
1354
+
1355
+ // PyLong-op1 only handles exact PyLong as op2, everything else is left to op2.
1356
+ // Thus, we can always pass on to op2 here.
1357
+
1358
+ // Avoid running into non-heap-type problems in Py<3.10 Limited API.
1359
+ #if CYTHON_USE_TYPE_SLOTS || __PYX_LIMITED_VERSION_HEX >= 0x030A0000
1360
+ {
1361
+ PyTypeObject *type_op2 = Py_TYPE(op2);
1362
+ // Reverse operation => ignore 'InPlace' operator since it applies to op1, not op2.
1363
+ binaryfunc slot_func = __Pyx_PyType_GetSubSlot(type_op2, tp_as_number, nb_{{slot_name}}, binaryfunc);
1364
+ if (likely(slot_func)) {
1365
+ PyObject *result = slot_func(op1, op2);
1366
+ if (likely(result != Py_NotImplemented)) {
1367
+ return result;
1368
+ }
1369
+ Py_DECREF(result);
1370
+ }
1371
+ // No "sq_concat" since Python doesn't try it on op2 and 'int' definitely doesn't have it.
1372
+ {{if c_op == '*'}}
1373
+ ssizeargfunc repeat_func = __Pyx_PyType_GetSubSlot(type_op2, tp_as_sequence, sq_repeat, ssizeargfunc);
1374
+ if (likely(repeat_func)) {
1375
+ Py_ssize_t count;
1376
+ #if CYTHON_USE_PYLONG_INTERNALS
1377
+ if (__Pyx_PyLong_IsCompact(op1)) {
1378
+ count = __Pyx_PyLong_CompactValue(op1);
1379
+ } else
1380
+ #endif
1381
+ {
1382
+ count = PyLong_AsSsize_t(op1);
1383
+ if (unlikely((count == -1) && PyErr_Occurred())) return NULL;
1384
+ }
1385
+ return repeat_func(op2, count);
1386
+ }
1387
+ {{endif}}
1388
+
1389
+ __Pyx_BinopTypeError(op1, op2, "{{c_op}}", inplace);
1390
+ return NULL;
1391
+
1392
+ }
1393
+ #else
1394
+ return (inplace) ? {{inplace_op_name}}(op1, op2) : {{op_name}}(op1, op2);
1395
+ #endif
1396
+ }
1397
+ #endif
1398
+ {{endif}}
1399
+
1400
+ static CYTHON_INLINE PyObject* __Pyx__{{op_name}}_{{type1}}_{{type2}}(PyObject *op1, PyObject *op2, int inplace) {
1401
+ {{if type1 in ('object', 'float') and c_op in '+-*'}}
1402
+ if ({{is_type('op1', 'float')}}) {
1403
+ {{if type2 in ('object', 'float')}}
1404
+ if ({{is_type('op2', 'float')}}) {
1405
+ double float_op2 = __Pyx_PyFloat_AS_DOUBLE(op2);
1406
+ #if !CYTHON_ASSUME_SAFE_MACROS
1407
+ if (unlikely((float_op2 == -1.) && PyErr_Occurred())) return NULL;
1408
+ #endif
1409
+ double float_op1 = __Pyx_PyFloat_AS_DOUBLE(op1);
1410
+ #if !CYTHON_ASSUME_SAFE_MACROS
1411
+ if (unlikely((float_op1 == -1.) && PyErr_Occurred())) return NULL;
1412
+ #endif
1413
+
1414
+ return PyFloat_FromDouble(float_op1 {{c_op}} float_op2);
1415
+ }
1416
+ {{endif}}
1417
+ return __Pyx_{{op_name}}_xfloat_{{type2}}(op1, op2, inplace);
1418
+ }
1419
+ {{endif}}
1420
+
1421
+ {{if type1 in ('object', 'int')}}
1422
+ if ({{is_type('op1', 'int')}}) {
1423
+ {{if type2 in ('object', 'int')}}
1424
+ if ({{is_type('op2', 'int')}}) {
1425
+ #if CYTHON_USE_PYLONG_INTERNALS
1426
+ {{if c_op == '*'}}
1427
+
1428
+ if (__Pyx_PyLong_IsCompact(op1)) {
1429
+ long long int_op1 = (long long) __Pyx_PyLong_CompactValue(op1);
1430
+ if (int_op1 == 0) return __Pyx_NewRef(op1);
1431
+
1432
+ if (__Pyx_PyLong_IsCompact(op2)) {
1433
+ long long int_op2 = (long long) __Pyx_PyLong_CompactValue(op2);
1434
+ if (int_op2 == 0) return __Pyx_NewRef(op2);
1435
+
1436
+ return PyLong_FromLongLong(int_op1 {{c_op}} int_op2);
1437
+ }
1438
+ }
1439
+
1440
+ {{else}}
1441
+
1442
+ if (__Pyx_PyLong_IsCompact(op1)) {
1443
+ Py_ssize_t int_op1 = __Pyx_PyLong_CompactValue(op1);
1444
+ {{if c_op in '+|^'}}
1445
+ if (int_op1 == 0) return __Pyx_NewRef(op2);
1446
+ {{elif c_op == '&'}}
1447
+ if (int_op1 == 0) return __Pyx_NewRef(op1);
1448
+ {{endif}}
1449
+
1450
+ if (__Pyx_PyLong_IsCompact(op2)) {
1451
+ Py_ssize_t int_op2 = __Pyx_PyLong_CompactValue(op2);
1452
+ {{if c_op in '+-|^'}}
1453
+ if (int_op2 == 0) return __Pyx_NewRef(op1);
1454
+ {{elif c_op == '&'}}
1455
+ if (int_op2 == 0) return __Pyx_NewRef(op2);
1456
+ {{endif}}
1457
+
1458
+ return PyLong_FromSsize_t(int_op1 {{c_op}} int_op2);
1459
+ }
1460
+ }
1461
+ {{endif}}
1462
+
1463
+ // op1 is not compact, but op2 might still hit the special case for '0':
1464
+ // identity for '+-|^' -> reuse op1
1465
+ // zero for '*&' -> reuse op2
1466
+ else if (__Pyx_PyLong_IsZero(op2)) return __Pyx_NewRef({{if c_op in '+-|^'}}op1{{else}}op2{{endif}});
1467
+
1468
+ #endif
1469
+
1470
+ binaryfunc slot_func = __Pyx_PyType_GetSubSlot(&PyLong_Type, tp_as_number, nb_{{slot_name}}, binaryfunc);
1471
+ if (likely(slot_func)) {
1472
+ return slot_func(op1, op2);
1473
+ }
1474
+ }
1475
+ {{endif}}
1476
+
1477
+ return __Pyx_{{op_name}}_xint_{{type2}}(op1, op2, inplace);
1478
+ }
1479
+ {{endif}}
1480
+
1481
+ return (inplace) ? {{inplace_op_name}}(op1, op2) : {{op_name}}(op1, op2);
1482
+ }
1483
+ // !(PyPy/Graal/LimitedAPI)
1484
+ #endif
1485
+
1486
+
1487
+ /////////////// UnicodeEquals.proto ///////////////
1488
+ //@requires: PyObjectCompare{"return_obj": 0, "op": "Eq", "c_op": "==", "type1": "str", "type2": "str"}
1489
+
1490
+ #define __Pyx_PyUnicode_Equals(s1, s2) __Pyx_PyObject_CompareBoolEq_str_str(s1, s2, Py_EQ)
1491
+
1492
+
1493
+ /////////////// PyObjectCompare.proto ///////////////
1494
+
1495
+ {{py: c_ret_type = 'PyObject*' if return_obj else 'int'}}
1496
+ static CYTHON_INLINE {{c_ret_type}} __Pyx_PyObject_Compare{{'' if return_obj else 'Bool'}}{{op}}_{{type1}}_{{type2}}(PyObject *op1, PyObject *op2, int pyop); /*proto*/
1497
+
1498
+ /////////////// PyObjectCompare ///////////////
1499
+ //@requires: StringTools.c::IncludeStringH
1500
+
1501
+ {{py: c_ret_type = 'PyObject*' if return_obj else 'int'}}
1502
+ {{py: func_suffix = f"{'' if return_obj else 'Bool'}{op}"}}
1503
+ {{py: return_true = 'goto __pyx_return_true'}}
1504
+ {{py: return_false = 'goto __pyx_return_false'}}
1505
+ {{py: return_error = "return NULL" if return_obj else "return -1"}}
1506
+ {{py: c_op_reversed = {'==': '==', '!=': '!=', '<': '>=', '<=': '>', '>=': '<', '>': '<='}[c_op] }}
1507
+ {{py:
1508
+ check_functions = {
1509
+ 'float': "PyFloat_CheckExact",
1510
+ 'int': 'PyLong_CheckExact',
1511
+ 'str': 'PyUnicode_CheckExact',
1512
+ 'bytes': 'PyBytes_CheckExact',
1513
+ 'bytearray': 'PyByteArray_CheckExact',
1514
+ }
1515
+ }}
1516
+ {{py:
1517
+ def is_type(operand, expected, type1=type1, type2=type2, check_functions=check_functions):
1518
+ assert operand in ('op1', 'op2'), operand
1519
+ assert expected in check_functions, expected
1520
+ type = type1 if operand == 'op1' else type2
1521
+ if type == expected:
1522
+ check = f"likely({operand} != Py_None)"
1523
+ else:
1524
+ function = check_functions[expected]
1525
+ check = f"{function}({operand})"
1526
+ other_type = type2 if operand == 'op1' else type1
1527
+ if other_type == expected:
1528
+ check = f"likely({check})"
1529
+ return check
1530
+ }}
1531
+
1532
+ // str comparisons
1533
+
1534
+ {{if type1 in ('object', 'str') and type2 in ('object', 'str')}}
1535
+ #ifndef __Pyx_DEFINED_PyObject_CompareStrStr{{func_suffix}}
1536
+ #define __Pyx_DEFINED_PyObject_CompareStrStr{{func_suffix}}
1537
+ static CYTHON_INLINE {{c_ret_type}} __Pyx_PyObject_CompareStrStr{{func_suffix}}(PyObject* s1, PyObject* s2) {
1538
+ {{if op in 'EqNe'}}
1539
+ #if __PYX_LIMITED_VERSION_HEX >= 0x030e0000
1540
+ int result = PyUnicode_Equal(s1, s2);
1541
+ #if !CYTHON_COMPILING_IN_CPYTHON
1542
+ // Cannot fail in CPython, but might in others.
1543
+ if (unlikely(result == -1)) {{return_error}};
1544
+ #endif
1545
+ if (result {{c_op}} 0) {{return_false}}; else {{return_true}};
1546
+ #else
1547
+ {{endif}}
1548
+ int result = PyUnicode_Compare(s1, s2);
1549
+ if (unlikely((result == -1) && PyErr_Occurred())) {{return_error}};
1550
+ if (result {{c_op}} 0) {{return_true}}; else {{return_false}};
1551
+ {{if op in 'EqNe'}}
1552
+ #endif
1553
+ {{endif}}
1554
+
1555
+ __pyx_return_true:
1556
+ {{'Py_RETURN_TRUE' if return_obj else 'return 1'}};
1557
+ __pyx_return_false:
1558
+ {{'Py_RETURN_FALSE' if return_obj else 'return 0'}};
1559
+ }
1560
+ #endif
1561
+ // end of str comparisons
1562
+ {{endif}}
1563
+
1564
+ // bytes/bytearray comparisons
1565
+
1566
+ {{for s1_type in ('bytes', 'bytearray')}}
1567
+ {{for s2_type in ('bytes', 'bytearray')}}
1568
+
1569
+ {{if type1 in ('object', s1_type) and type2 in ('object', s2_type)}}
1570
+
1571
+ {{py: s1_prefix = "PyBytes" if s1_type == 'bytes' else "PyByteArray"}}
1572
+ {{py: s2_prefix = "PyBytes" if s2_type == 'bytes' else "PyByteArray"}}
1573
+
1574
+ #if !(CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_GRAAL)
1575
+
1576
+ #ifndef __Pyx_DEFINED_PyObject_Compare{{s1_prefix}}{{s2_prefix}}{{func_suffix}}
1577
+ #define __Pyx_DEFINED_PyObject_Compare{{s1_prefix}}{{s2_prefix}}{{func_suffix}}
1578
+
1579
+ {{if op in 'EqNe'}}
1580
+ static CYTHON_INLINE {{c_ret_type}} __Pyx_PyObject_Compare{{s1_prefix}}{{s2_prefix}}{{func_suffix}}(PyObject* s1, PyObject* s2) {
1581
+ #if CYTHON_ASSUME_SAFE_SIZE && CYTHON_ASSUME_SAFE_MACROS
1582
+ const char *ps1, *ps2;
1583
+ Py_ssize_t length = {{s1_prefix}}_GET_SIZE(s1);
1584
+ if (length != {{s2_prefix}}_GET_SIZE(s2)) {{return_false if op == 'Eq' else return_true}};
1585
+
1586
+ ps1 = {{s1_prefix}}_AS_STRING(s1);
1587
+ ps2 = {{s2_prefix}}_AS_STRING(s2);
1588
+
1589
+ #else
1590
+
1591
+ char *ps1, *ps2;
1592
+ Py_ssize_t length, length2;
1593
+
1594
+ {{if s1_type == 'bytes'}}
1595
+ if (unlikely(PyBytes_AsStringAndSize(s1, &ps1, &length) == -1)) {{return_error}};
1596
+ {{else}}
1597
+ ps1 = __Pyx_PyByteArray_AsString(s1); if (unlikely(!ps1)) {{return_error}};
1598
+ length = __Pyx_PyByteArray_GET_SIZE(s1); if (unlikely(length == -1)) {{return_error}};
1599
+ {{endif}}
1600
+
1601
+ {{if s2_type == 'bytes'}}
1602
+ if (unlikely(PyBytes_AsStringAndSize(s2, &ps2, &length2) == -1)) {{return_error}};
1603
+ {{else}}
1604
+ ps2 = __Pyx_PyByteArray_AsString(s2); if (unlikely(!ps2)) {{return_error}};
1605
+ length2 = __Pyx_PyByteArray_GET_SIZE(s2); if (unlikely(length2 == -1)) {{return_error}};
1606
+ {{endif}}
1607
+
1608
+ if (length != length2) {{return_false if op == 'Eq' else return_true}};
1609
+
1610
+ #endif
1611
+
1612
+ // len(s1) == len(s2)
1613
+ {{if s1_type == 'bytearray'}}
1614
+ if (length == 0) {{return_true if op == 'Eq' else return_false}};
1615
+ {{else}}
1616
+ // bytes: length >= 1 (empty bytes is singleton, and "s1 is not s2")
1617
+ {{endif}}
1618
+
1619
+ if (ps1[0] != ps2[0]) {{return_false if op == 'Eq' else return_true}};
1620
+ if (length == 1) {{return_true if op == 'Eq' else return_false}};
1621
+
1622
+ {
1623
+ int cmp;
1624
+ {{if s1_type == 'bytes' and s2_type == 'bytes'}}
1625
+ #if CYTHON_USE_UNICODE_INTERNALS && (PY_VERSION_HEX < 0x030B0000)
1626
+ Py_hash_t hash1 = ((PyBytesObject*)s1)->ob_shash;
1627
+ Py_hash_t hash2 = ((PyBytesObject*)s2)->ob_shash;
1628
+ if (hash1 != hash2 && hash1 != -1 && hash2 != -1) {{return_false if op == 'Eq' else return_true}};
1629
+ #endif
1630
+ {{endif}}
1631
+ cmp = memcmp(ps1, ps2, (size_t)length);
1632
+ if (cmp {{c_op}} 0) {{return_true}}; else {{return_false}};
1633
+ }
1634
+
1635
+ __pyx_return_true:
1636
+ {{'Py_RETURN_TRUE' if return_obj else 'return 1'}};
1637
+ __pyx_return_false:
1638
+ {{'Py_RETURN_FALSE' if return_obj else 'return 0'}};
1639
+ }
1640
+
1641
+ {{else}}
1642
+ // !EqNe
1643
+
1644
+ static CYTHON_INLINE {{c_ret_type}} __Pyx_PyObject_Compare{{s1_prefix}}{{s2_prefix}}{{func_suffix}}(PyObject* s1, PyObject* s2) {
1645
+ Py_ssize_t cmp;
1646
+ Py_ssize_t length1, length2, short_length;
1647
+
1648
+ #if CYTHON_ASSUME_SAFE_SIZE && CYTHON_ASSUME_SAFE_MACROS
1649
+ const char *ps1, *ps2;
1650
+ length1 = __Pyx_{{s1_prefix}}_GET_SIZE(s1);
1651
+ length2 = __Pyx_{{s2_prefix}}_GET_SIZE(s2);
1652
+
1653
+ short_length = (length1 < length2) ? length1 : length2;
1654
+ if (short_length == 0) {
1655
+ if (length1 == 0) {{return_true if op in 'LtLe' else return_false}}; else {{return_false if op in 'LtLe' else return_true}};
1656
+ }
1657
+
1658
+ ps1 = {{s1_prefix}}_AS_STRING(s1);
1659
+ ps2 = {{s2_prefix}}_AS_STRING(s2);
1660
+
1661
+ #else
1662
+ char *ps1, *ps2;
1663
+
1664
+ {{if s1_type == 'bytes'}}
1665
+ if (unlikely(PyBytes_AsStringAndSize(s1, &ps1, &length1) == -1)) {{return_error}};
1666
+ {{else}}
1667
+ ps1 = __Pyx_PyByteArray_AsString(s1); if (unlikely(!ps1)) {{return_error}};
1668
+ length1 = __Pyx_PyByteArray_GET_SIZE(s1); if (unlikely(length1 == -1)) {{return_error}};
1669
+ {{endif}}
1670
+
1671
+ {{if s2_type == 'bytes'}}
1672
+ if (unlikely(PyBytes_AsStringAndSize(s2, &ps2, &length2) == -1)) {{return_error}};
1673
+ {{else}}
1674
+ ps2 = __Pyx_PyByteArray_AsString(s2); if (unlikely(!ps2)) {{return_error}};
1675
+ length2 = __Pyx_PyByteArray_GET_SIZE(s2); if (unlikely(length2 == -1)) {{return_error}};
1676
+ {{endif}}
1677
+
1678
+ short_length = (length1 < length2) ? length1 : length2;
1679
+ if (short_length == 0) {
1680
+ if (length1 == 0) {{return_true if op in 'LtLe' else return_false}}; else {{return_false if op in 'LtLe' else return_true}};
1681
+ }
1682
+ #endif
1683
+
1684
+ cmp = (Py_ssize_t) ((const unsigned char*) ps1)[0] - (Py_ssize_t) ((const unsigned char*) ps2)[0];
1685
+ if (cmp == 0 && short_length > 1) {
1686
+ cmp = memcmp(ps1, ps2, (size_t)short_length);
1687
+ }
1688
+ if (cmp == 0) cmp = (length1 - length2);
1689
+ if (cmp {{c_op}} 0) {{return_true}}; else {{return_false}};
1690
+
1691
+ __pyx_return_true:
1692
+ {{'Py_RETURN_TRUE' if return_obj else 'return 1'}};
1693
+ __pyx_return_false:
1694
+ {{'Py_RETURN_FALSE' if return_obj else 'return 0'}};
1695
+ }
1696
+ {{endif}}
1697
+
1698
+ // End of bytes/bytearray comparisons.
1699
+ #endif
1700
+ #endif
1701
+ {{endif}}
1702
+
1703
+ {{endfor}}
1704
+ {{endfor}}
1705
+
1706
+ // float/int comparisons
1707
+
1708
+ {{if type1 in ('object', 'float') and type2 in ('object', 'int')}}
1709
+ // Less likely, non-inlined comparison of float and int.
1710
+ #ifndef __Pyx_DEFINED_PyObject_CompareFloatInt{{func_suffix}}
1711
+ #define __Pyx_DEFINED_PyObject_CompareFloatInt{{func_suffix}}
1712
+ static {{c_ret_type}} __Pyx_PyObject_CompareFloatInt{{func_suffix}}(PyObject *op1, PyObject *op2) {
1713
+ double float_op1 = __Pyx_PyFloat_AS_DOUBLE(op1);
1714
+ #if !CYTHON_ASSUME_SAFE_MACROS
1715
+ if (unlikely(float_op1 == -1. && PyErr_Occurred())) {{return_error}};
1716
+ #endif
1717
+
1718
+ #if CYTHON_USE_PYLONG_INTERNALS
1719
+ if (__Pyx_PyLong_IsCompact(op2)) {
1720
+ Py_ssize_t iop2 = __Pyx_PyLong_CompactValue(op2);
1721
+ if (float_op1 {{c_op}} ((double)iop2)) {{return_true}}; else {{return_false}};
1722
+ }
1723
+ if (unlikely(!isfinite(float_op1))) {
1724
+ // CPython just compares inf/nan to 0.0
1725
+ if (float_op1 {{c_op}} 0.0) {{return_true}}; else {{return_false}};
1726
+ } else {
1727
+ // op2 != 0 (which would be compact)
1728
+ int sign2 = __Pyx_PyLong_Sign(op2);
1729
+ if (float_op1 >= 0.) {
1730
+ if (sign2 < 0) {{return_true if op in 'NeGeGt' else return_false}};
1731
+ // same sign - is float value compact while PyLong is not?
1732
+ if (float_op1 < (double) (1L << PyLong_SHIFT)) {{return_true if op in 'NeLeLt' else return_false}};
1733
+ } else {
1734
+ if (sign2 > 0) {{return_true if op in 'NeLeLt' else return_false}};
1735
+ // same sign - is float value compact while PyLong is not?
1736
+ if (float_op1 > -(double) (1L << PyLong_SHIFT)) {{return_false if op in 'EqLeLt' else return_true}};
1737
+ }
1738
+ }
1739
+ #else
1740
+ if (unlikely(!isfinite(float_op1))) {
1741
+ // CPython just compares inf/nan to 0.0
1742
+ if (float_op1 {{c_op}} 0.0) {{return_true}}; else {{return_false}};
1743
+ } else {
1744
+ int overflow2;
1745
+ // We know that we have an exact PyLong value, so we assume no exceptions.
1746
+ long iop2 = PyLong_AsLongAndOverflow(op2, &overflow2);
1747
+ if (likely(!overflow2)) {
1748
+ if ((long long) iop2 >= (1LL << 53)) {
1749
+ overflow2 = 1;
1750
+ } else if ((long long) iop2 <= - (1LL << 53)) {
1751
+ overflow2 = -1;
1752
+ } else {
1753
+ if (float_op1 {{c_op}} ((double) iop2)) {{return_true}}; else {{return_false}};
1754
+ }
1755
+ }
1756
+ if (overflow2 > 0) {
1757
+ if (float_op1 < ((double) (1LL << 53))) {{return_true if op in 'NeLeLt' else return_false}};
1758
+ } else {
1759
+ if (float_op1 > - ((double) (1LL << 53))) {{return_true if op in 'NeGeGt' else return_false}};
1760
+ }
1761
+ }
1762
+ #endif
1763
+
1764
+ return {{'PyObject_RichCompare' if return_obj else '__Pyx_PyObject_RichCompareBool'}}(op1, op2, Py_{{op.upper()}});
1765
+
1766
+ __pyx_return_true:
1767
+ {{'Py_RETURN_TRUE' if return_obj else 'return 1'}};
1768
+ __pyx_return_false:
1769
+ {{'Py_RETURN_FALSE' if return_obj else 'return 0'}};
1770
+ }
1771
+ #endif
1772
+ {{endif}}
1773
+
1774
+ {{if type1 in ('object', 'int') and type2 in ('object', 'float')}}
1775
+ // Less likely, non-inlined comparison of int and float.
1776
+ #ifndef __Pyx_DEFINED_PyObject_CompareIntFloat{{func_suffix}}
1777
+ #define __Pyx_DEFINED_PyObject_CompareIntFloat{{func_suffix}}
1778
+ static {{c_ret_type}} __Pyx_PyObject_CompareIntFloat{{func_suffix}}(PyObject *op1, PyObject *op2) {
1779
+ double float_op2 = __Pyx_PyFloat_AS_DOUBLE(op2);
1780
+ #if !CYTHON_ASSUME_SAFE_MACROS
1781
+ if (unlikely(float_op2 == -1. && PyErr_Occurred())) {{return_error}};
1782
+ #endif
1783
+
1784
+ #if CYTHON_USE_PYLONG_INTERNALS
1785
+ if (__Pyx_PyLong_IsCompact(op1)) {
1786
+ Py_ssize_t iop1 = __Pyx_PyLong_CompactValue(op1);
1787
+ if (((double)iop1) {{c_op}} float_op2) {{return_true}}; else {{return_false}};
1788
+ }
1789
+ if (unlikely(!isfinite(float_op2))) {
1790
+ // CPython just compares inf/nan to 0.0
1791
+ if (0.0 {{c_op}} float_op2) {{return_true}}; else {{return_false}};
1792
+ } else {
1793
+ // op1 != 0 (which would be compact)
1794
+ int sign1 = __Pyx_PyLong_Sign(op1);
1795
+ if (float_op2 >= 0.) {
1796
+ if (sign1 < 0) {{return_true if op in 'NeLeLt' else return_false}};
1797
+ // same sign - is float value compact while PyLong is not?
1798
+ if (float_op2 < (double) (1L << PyLong_SHIFT)) {{return_true if op in 'NeGeGt' else return_false}};
1799
+ } else {
1800
+ if (sign1 > 0) {{return_true if op in 'NeGeGt' else return_false}};
1801
+ // same sign - is float value compact while PyLong is not?
1802
+ if (float_op2 > -(double) (1L << PyLong_SHIFT)) {{return_false if op in 'EqGeGt' else return_true}};
1803
+ }
1804
+ }
1805
+ #else
1806
+ if (unlikely(!isfinite(float_op2))) {
1807
+ // CPython just compares inf/nan to 0.0
1808
+ if (0.0 {{c_op}} float_op2) {{return_true}}; else {{return_false}};
1809
+ } else {
1810
+ int overflow1;
1811
+ // We know that we have an exact PyLong value, so we assume no exceptions.
1812
+ long iop1 = PyLong_AsLongAndOverflow(op1, &overflow1);
1813
+ if (likely(!overflow1)) {
1814
+ if ((long long) iop1 >= (1LL << 53)) {
1815
+ overflow1 = 1;
1816
+ } else if ((long long) iop1 <= - (1LL << 53)) {
1817
+ overflow1 = -1;
1818
+ } else {
1819
+ if (((double) iop1) {{c_op}} float_op2) {{return_true}}; else {{return_false}};
1820
+ }
1821
+ }
1822
+ if (overflow1 < 0) {
1823
+ if (float_op2 > ((double) (1LL << 53))) {{return_true if op in 'NeLeLt' else return_false}};
1824
+ } else {
1825
+ if (float_op2 < - ((double) (1LL << 53))) {{return_true if op in 'NeGeGt' else return_false}};
1826
+ }
1827
+ }
1828
+ #endif
1829
+
1830
+ return {{'PyObject_RichCompare' if return_obj else '__Pyx_PyObject_RichCompareBool'}}(op1, op2, Py_{{op.upper()}});
1831
+
1832
+ __pyx_return_true:
1833
+ {{'Py_RETURN_TRUE' if return_obj else 'return 1'}};
1834
+ __pyx_return_false:
1835
+ {{'Py_RETURN_FALSE' if return_obj else 'return 0'}};
1836
+ }
1837
+ #endif
1838
+ {{endif}}
1839
+
1840
+ {{if type1 in ('object', 'int') and type2 in ('object', 'int')}}
1841
+ {{py: from Cython.Utility import pylong_join }}
1842
+
1843
+ #ifndef __Pyx_DEFINED_PyObject_CompareIntInt{{func_suffix}}
1844
+ #define __Pyx_DEFINED_PyObject_CompareIntInt{{func_suffix}}
1845
+ static {{c_ret_type}} __Pyx_PyObject_CompareIntInt{{func_suffix}}(PyObject *op1, PyObject *op2) {
1846
+ #if CYTHON_USE_PYLONG_INTERNALS
1847
+ Py_ssize_t cmp = __Pyx_PyLong_CompareSignAndSize(op1, op2);
1848
+ if (cmp == 0) {
1849
+ Py_ssize_t size = __Pyx_PyLong_DigitCount(op1);
1850
+ if (size > 0) {
1851
+ const digit* digits1 = __Pyx_PyLong_Digits(op1);
1852
+ const digit* digits2 = __Pyx_PyLong_Digits(op2);
1853
+ if (size == 1) {
1854
+ cmp = (Py_ssize_t) digits1[0] - (Py_ssize_t) digits2[0];
1855
+ } else if ((size == 2) && (8 * sizeof(Py_ssize_t) >= 2 * PyLong_SHIFT)) {
1856
+ cmp = (Py_ssize_t) {{pylong_join(2, 'digits1', 'size_t')}} - (Py_ssize_t) {{pylong_join(2, 'digits2', 'size_t')}};
1857
+ } else {
1858
+ for (Py_ssize_t i=size-1; i >= 0 && !cmp; --i) {
1859
+ cmp = (Py_ssize_t) digits1[i] - (Py_ssize_t) digits2[i];
1860
+ }
1861
+ }
1862
+ }
1863
+ if (cmp == 0) {{return_true if op in 'EqLeGe' else return_false}};
1864
+ if (__Pyx_PyLong_IsNeg(op1)) cmp = -cmp;
1865
+ }
1866
+
1867
+ {{if op == 'Eq'}}
1868
+ {{return_false}};
1869
+ {{elif op == 'Ne'}}
1870
+ {{return_true}};
1871
+ {{else}}
1872
+ if (cmp < 0) {{return_true if op in 'LeLt' else return_false}}; else {{return_false if op in 'LeLt' else return_true}};
1873
+ {{endif}}
1874
+
1875
+ #else
1876
+ int overflow1, overflow2;
1877
+ // We know that we have two exact PyLong values, so we assume no exceptions.
1878
+ long long iop1 = PyLong_AsLongLongAndOverflow(op1, &overflow1);
1879
+ long long iop2 = PyLong_AsLongLongAndOverflow(op2, &overflow2);
1880
+ if (likely(!(overflow1 | overflow2))) {
1881
+ if (iop1 {{c_op}} iop2) {{return_true}}; else {{return_false}};
1882
+ } else if (overflow1 != overflow2) {
1883
+ if (overflow1 {{c_op}} overflow2) {{return_true}}; else {{return_false}};
1884
+ } else {
1885
+ return {{'PyObject_RichCompare' if return_obj else '__Pyx_PyObject_RichCompareBool'}}(op1, op2, Py_{{op.upper()}});
1886
+ }
1887
+ #endif
1888
+
1889
+ __pyx_return_true:
1890
+ {{'Py_RETURN_TRUE' if return_obj else 'return 1'}};
1891
+ __pyx_return_false:
1892
+ {{'Py_RETURN_FALSE' if return_obj else 'return 0'}};
1893
+ }
1894
+ #endif
1895
+
1896
+ // end of float/int comparisons
1897
+ {{endif}}
1898
+
1899
+ // main comparison function
1900
+
1901
+ static CYTHON_INLINE {{c_ret_type}} __Pyx_PyObject_Compare{{func_suffix}}_{{type1}}_{{type2}}(PyObject *op1, PyObject *op2, int pyop) {
1902
+ CYTHON_UNUSED_VAR(pyop);
1903
+
1904
+ {{if type1 != 'object'}}
1905
+ // For concrete types, the whole rest simplifies a lot if we handle None up front.
1906
+ if (unlikely(op1 == Py_None)) {
1907
+ {{if op == 'Eq'}}
1908
+ if (op2 == Py_None) {{return_true}}; else {{if type2 != 'object'}}{{return_false}}{{else}}goto __pyx_richcmp{{endif}};
1909
+ {{elif op == 'Ne'}}
1910
+ if (op2 == Py_None) {{return_false}}; else {{if type2 != 'object'}}{{return_true}}{{else}}goto __pyx_richcmp{{endif}};
1911
+ {{else}}
1912
+ goto __pyx_richcmp;
1913
+ {{endif}}
1914
+ }
1915
+ {{endif}}
1916
+ {{if type2 != 'object'}}
1917
+ if (unlikely(op2 == Py_None)) {
1918
+ {{if op == 'Eq'}}
1919
+ if (op1 == Py_None) {{return_true}}; else {{if type1 != 'object'}}{{return_false}}{{else}}goto __pyx_richcmp{{endif}};
1920
+ {{elif op == 'Ne'}}
1921
+ if (op1 == Py_None) {{return_false}}; else {{if type1 != 'object'}}{{return_true}}{{else}}goto __pyx_richcmp{{endif}};
1922
+ {{else}}
1923
+ goto __pyx_richcmp;
1924
+ {{endif}}
1925
+ }
1926
+ {{endif}}
1927
+
1928
+ {{if (type1 == 'int' or type2 == 'int') and (type1 != 'float' and type2 != 'float')}}
1929
+ if (op1 == op2) {{return_true if op in 'EqLeGe' else return_false}};
1930
+ {{endif}}
1931
+
1932
+ {{if type1 in ('object', 'float')}}
1933
+ if ({{is_type('op1', 'float')}}) {
1934
+ {{if type2 in ('object', 'float')}}
1935
+ if ({{is_type('op2', 'float')}}) {
1936
+ double float_op1 = __Pyx_PyFloat_AS_DOUBLE(op1);
1937
+ #if !CYTHON_ASSUME_SAFE_MACROS
1938
+ if (unlikely(float_op1 == -1. && PyErr_Occurred())) {{return_error}};
1939
+ #endif
1940
+ double float_op2 = __Pyx_PyFloat_AS_DOUBLE(op2);
1941
+ #if !CYTHON_ASSUME_SAFE_MACROS
1942
+ if (unlikely(float_op2 == -1. && PyErr_Occurred())) {{return_error}};
1943
+ #endif
1944
+ if (float_op1 {{c_op}} float_op2) {{return_true}}; else {{return_false}};
1945
+ }
1946
+ {{endif}}
1947
+
1948
+ {{if type2 in ('object', 'int')}}
1949
+ if ({{is_type('op2', 'int')}}) {
1950
+ return __Pyx_PyObject_CompareFloatInt{{func_suffix}}(op1, op2);
1951
+ }
1952
+ {{endif}}
1953
+
1954
+ goto __pyx_richcmp;
1955
+ }
1956
+ {{endif}}
1957
+
1958
+ {{if type1 in ('object', 'int')}}
1959
+ if ({{is_type('op1', 'int')}}) {
1960
+ if (op1 == op2) {{return_true if op in 'EqLeGe' else return_false}};
1961
+
1962
+ {{if type2 in ('object', 'int')}}
1963
+ if ({{is_type('op2', 'int')}}) {
1964
+ return __Pyx_PyObject_CompareIntInt{{func_suffix}}(op1, op2);
1965
+ }
1966
+ {{endif}}
1967
+
1968
+ {{if type2 in ('object', 'float')}}
1969
+ if ({{is_type('op2', 'float')}}) {
1970
+ return __Pyx_PyObject_CompareIntFloat{{func_suffix}}(op1, op2);
1971
+ }
1972
+ {{endif}}
1973
+
1974
+ goto __pyx_richcmp;
1975
+ }
1976
+ {{endif}}
1977
+
1978
+ {{for string_type in ('str', 'bytes', 'bytearray')}}
1979
+ {{if type1 in ('object', string_type) and type2 in ('object', string_type)}}
1980
+
1981
+ {{if string_type != 'str'}}#if !(CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_GRAAL){{endif}}
1982
+ if ({{is_type('op1', string_type)}}) {
1983
+ // Catch interned and identical strings as well as the empty string.
1984
+ if (op1 == op2) {{return_true if op in 'EqLeGe' else return_false}};
1985
+
1986
+ if ({{is_type('op2', string_type)}}) {
1987
+ {{if string_type == 'bytes'}}
1988
+ return __Pyx_PyObject_ComparePyBytesPyBytes{{func_suffix}}(op1, op2);
1989
+ {{elif string_type == 'bytearray'}}
1990
+ return __Pyx_PyObject_ComparePyByteArrayPyByteArray{{func_suffix}}(op1, op2);
1991
+ {{else}}
1992
+ return __Pyx_PyObject_CompareStrStr{{func_suffix}}(op1, op2);
1993
+ {{endif}}
1994
+ }
1995
+
1996
+ {{if string_type == 'bytes' and type2 in ('object', 'bytearray')}}
1997
+ if ({{is_type('op2', 'bytearray')}}) {
1998
+ return __Pyx_PyObject_ComparePyBytesPyByteArray{{func_suffix}}(op1, op2);
1999
+ }
2000
+ {{elif string_type == 'bytearray' and type2 in ('object', 'bytes')}}
2001
+ if ({{is_type('op2', 'bytes')}}) {
2002
+ return __Pyx_PyObject_ComparePyByteArrayPyBytes{{func_suffix}}(op1, op2);
2003
+ }
2004
+ {{endif}}
2005
+
2006
+ goto __pyx_richcmp;
2007
+ }
2008
+ {{if string_type != 'str'}}#endif{{endif}}
2009
+
2010
+ {{endif}}
2011
+ {{endfor}}
2012
+
2013
+ // avoid unused labels
2014
+ if ((0)) goto __pyx_richcmp;
2015
+ if ((0)) {{return_true}};
2016
+ if ((0)) {{return_false}};
2017
+
2018
+ __pyx_richcmp:
2019
+ return {{'PyObject_RichCompare' if return_obj else '__Pyx_PyObject_RichCompareBool'}}(op1, op2, Py_{{op.upper()}});
2020
+
2021
+ __pyx_return_true:
2022
+ {{'Py_RETURN_TRUE' if return_obj else 'return 1'}};
2023
+ __pyx_return_false:
2024
+ {{'Py_RETURN_FALSE' if return_obj else 'return 0'}};
2025
+ }
2026
+
1067
2027
 
1068
2028
  /////////////// PyLongCompare.proto ///////////////
1069
2029
 
@@ -1136,8 +2096,7 @@ static CYTHON_INLINE {{c_ret_type}} __Pyx_PyLong_{{'' if ret_type.is_pyobject el
1136
2096
  {{return_compare('(double)a', '(double)b', c_op)}}
1137
2097
  }
1138
2098
 
1139
- return {{'' if ret_type.is_pyobject else '__Pyx_PyObject_IsTrueAndDecref'}}(
1140
- PyObject_RichCompare(op1, op2, Py_{{op.upper()}}));
2099
+ return {{'PyObject_RichCompare' if ret_type.is_pyobject else '__Pyx_PyObject_RichCompareBool'}}(op1, op2, Py_{{op.upper()}});
1141
2100
  }
1142
2101
 
1143
2102
 
@@ -1148,7 +2107,7 @@ static CYTHON_INLINE {{c_ret_type}} __Pyx_PyLong_{{'' if ret_type.is_pyobject el
1148
2107
  static CYTHON_INLINE {{c_ret_type}} __Pyx_PyLong_{{'' if ret_type.is_pyobject else 'Bool'}}{{op}}{{order}}(PyObject *op1, PyObject *op2, long intval, int inplace, int zerodivision_check); /*proto*/
1149
2108
  #else
1150
2109
  #define __Pyx_PyLong_{{'' if ret_type.is_pyobject else 'Bool'}}{{op}}{{order}}(op1, op2, intval, inplace, zerodivision_check) \
1151
- {{if op in ('Eq', 'Ne')}}{{'' if ret_type.is_pyobject else '__Pyx_PyObject_IsTrueAndDecref'}}(PyObject_RichCompare(op1, op2, Py_{{op.upper()}}))
2110
+ {{if op in ('Eq', 'Ne')}}{{'PyObject_RichCompare' if ret_type.is_pyobject else 'PyObject_RichCompareBool'}}(op1, op2, Py_{{op.upper()}})
1152
2111
  {{else}}(inplace ? PyNumber_InPlace{{op}}(op1, op2) : PyNumber_{{op}}(op1, op2))
1153
2112
  {{endif}}
1154
2113
  #endif
@@ -1183,8 +2142,7 @@ def zerodiv_check(operand, optype='integer', _is_mod=op == 'Remainder', _needs_c
1183
2142
  static {{c_ret_type}} __Pyx_Fallback_{{cfunc_name}}(PyObject *op1, PyObject *op2, int inplace) {
1184
2143
  {{if op in ('Eq', 'Ne')}}
1185
2144
  CYTHON_UNUSED_VAR(inplace);
1186
- return {{'' if ret_type.is_pyobject else '__Pyx_PyObject_IsTrueAndDecref'}}(
1187
- PyObject_RichCompare(op1, op2, Py_{{op.upper()}}));
2145
+ return {{'PyObject_RichCompare' if ret_type.is_pyobject else '__Pyx_PyObject_RichCompareBool'}}(op1, op2, Py_{{op.upper()}});
1188
2146
  {{else}}
1189
2147
  return (inplace ? PyNumber_InPlace{{op}} : PyNumber_{{op}})(op1, op2);
1190
2148
  {{endif}}
@@ -1266,24 +2224,21 @@ static {{c_ret_type}} __Pyx_Unpacked_{{cfunc_name}}(PyObject *op1, PyObject *op2
1266
2224
  {{ival}} = (long) digits[0];
1267
2225
  if (!is_positive) {{ival}} *= -1;
1268
2226
  } else {
1269
- switch (size) {
1270
- {{for _size in range(2, 5)}}
1271
- case {{_size}}:
1272
- if (8 * sizeof(long) - 1 > {{_size}} * PyLong_SHIFT{{if c_op == '*'}}+30{{endif}}{{if op == 'TrueDivide'}} && {{_size-1}} * PyLong_SHIFT < 53{{endif}}) {
1273
- {{ival}} = (long) {{pylong_join(_size, 'digits')}};
1274
- if (!is_positive) {{ival}} *= -1;
1275
- goto calculate_long;
1276
- {{if op != 'TrueDivide'}}
1277
- } else if (8 * sizeof(PY_LONG_LONG) - 1 > {{_size}} * PyLong_SHIFT{{if c_op == '*'}}+30{{endif}}) {
1278
- ll{{ival}} = (PY_LONG_LONG) {{pylong_join(_size, 'digits', 'unsigned PY_LONG_LONG')}};
1279
- if (!is_positive) ll{{ival}} *= -1;
1280
- goto calculate_long_long;
1281
- {{endif}}
1282
- }
1283
- // size doesn't fit into a long or PY_LONG_LONG any more
1284
- break;
1285
- {{endfor}}
1286
- }
2227
+ {{for _size in range(2, 5)}}
2228
+ if (size == {{_size}} && 8 * sizeof(long) - 1 > {{_size}} * PyLong_SHIFT{{if c_op == '*'}}+30{{endif}}{{if op == 'TrueDivide'}} && {{_size-1}} * PyLong_SHIFT < 53{{endif}}) {
2229
+ {{ival}} = (long) {{pylong_join(_size, 'digits')}};
2230
+ if (!is_positive) {{ival}} *= -1;
2231
+ goto calculate_long;
2232
+ {{if op != 'TrueDivide'}}
2233
+ } else if (size == {{_size}} && 8 * sizeof(PY_LONG_LONG) - 1 > {{_size}} * PyLong_SHIFT{{if c_op == '*'}}+30{{endif}}) {
2234
+ ll{{ival}} = (PY_LONG_LONG) {{pylong_join(_size, 'digits', 'unsigned PY_LONG_LONG')}};
2235
+ if (!is_positive) ll{{ival}} *= -1;
2236
+ goto calculate_long_long;
2237
+ {{endif}}
2238
+ } else
2239
+ // size doesn't fit into a long or PY_LONG_LONG any more
2240
+ {{endfor}}
2241
+ {}
1287
2242
 
1288
2243
  {{if op in ('Eq', 'Ne')}}
1289
2244
  #if PyLong_SHIFT < 30 && PyLong_SHIFT != 15
@@ -1470,7 +2425,7 @@ static CYTHON_INLINE {{c_ret_type}} {{cfunc_name}}(PyObject *op1, PyObject *op2,
1470
2425
  static {{c_ret_type}} __Pyx_PyFloat_{{'' if ret_type.is_pyobject else 'Bool'}}{{op}}{{order}}(PyObject *op1, PyObject *op2, double floatval, int inplace, int zerodivision_check); /*proto*/
1471
2426
  #else
1472
2427
  #define __Pyx_PyFloat_{{'' if ret_type.is_pyobject else 'Bool'}}{{op}}{{order}}(op1, op2, floatval, inplace, zerodivision_check) \
1473
- {{if op in ('Eq', 'Ne')}}{{'' if ret_type.is_pyobject else '__Pyx_PyObject_IsTrueAndDecref'}}(PyObject_RichCompare(op1, op2, Py_{{op.upper()}}))
2428
+ {{if op in ('Eq', 'Ne')}}{{'PyObject_RichCompare' if ret_type.is_pyobject else 'PyObject_RichCompareBool'}}(op1, op2, Py_{{op.upper()}})
1474
2429
  {{elif op == 'Divide'}}((inplace ? __Pyx_PyNumber_InPlaceDivide(op1, op2) : __Pyx_PyNumber_Divide(op1, op2)))
1475
2430
  {{else}}(inplace ? PyNumber_InPlace{{op}}(op1, op2) : PyNumber_{{op}}(op1, op2))
1476
2431
  {{endif}}
@@ -1503,7 +2458,7 @@ def zerodiv_check(operand, _is_mod=op == 'Remainder', _needs_check=(order == 'CO
1503
2458
 
1504
2459
  static {{c_ret_type}} {{cfunc_name}}(PyObject *op1, PyObject *op2, double floatval, int inplace, int zerodivision_check) {
1505
2460
  const double {{'a' if order == 'CObj' else 'b'}} = floatval;
1506
- double {{fval}}{{if op not in ('Eq', 'Ne')}}, result{{endif}};
2461
+ double {{fval}};
1507
2462
  CYTHON_UNUSED_VAR(inplace);
1508
2463
  CYTHON_UNUSED_VAR(zerodivision_check);
1509
2464
 
@@ -1523,64 +2478,57 @@ static {{c_ret_type}} {{cfunc_name}}(PyObject *op1, PyObject *op2, double floatv
1523
2478
  if (__Pyx_PyLong_IsZero({{pyval}})) {
1524
2479
  {{fval}} = 0.0;
1525
2480
  {{zerodiv_check(fval)}}
2481
+ goto digits_done;
1526
2482
  } else if (__Pyx_PyLong_IsCompact({{pyval}})) {
1527
2483
  {{fval}} = (double) __Pyx_PyLong_CompactValue({{pyval}});
2484
+ goto digits_done;
1528
2485
  } else {
1529
2486
  const digit* digits = __Pyx_PyLong_Digits({{pyval}});
1530
- const Py_ssize_t size = __Pyx_PyLong_SignedDigitCount({{pyval}});
1531
- switch (size) {
1532
- {{for _size in (2, 3, 4)}}
1533
- case -{{_size}}:
1534
- case {{_size}}:
1535
- if (8 * sizeof(unsigned long) > {{_size}} * PyLong_SHIFT && ((8 * sizeof(unsigned long) < 53) || ({{_size-1}} * PyLong_SHIFT < 53))) {
1536
- {{fval}} = (double) {{pylong_join(_size, 'digits')}};
1537
- // let CPython do its own float rounding from 2**53 on (max. consecutive integer in double float)
1538
- if ((8 * sizeof(unsigned long) < 53) || ({{_size}} * PyLong_SHIFT < 53) || ({{fval}} < (double) ((PY_LONG_LONG)1 << 53))) {
1539
- if (size == {{-_size}})
1540
- {{fval}} = -{{fval}};
1541
- break;
1542
- }
1543
- }
1544
- // Fall through if size doesn't fit safely into a double anymore.
1545
- // It may not be obvious that this is a safe fall-through given the "fval < 2**53"
1546
- // check above. However, the number of digits that CPython uses for a given PyLong
1547
- // value is minimal, and together with the "(size-1) * SHIFT < 53" check above,
1548
- // this should make it safe.
1549
- CYTHON_FALLTHROUGH;
1550
- {{endfor}}
1551
- default:
2487
+ const Py_ssize_t size = __Pyx_PyLong_DigitCount({{pyval}});
2488
+ {{for _size in (2, 3, 4)}}
2489
+ if (size <= {{_size}} && (8 * sizeof(unsigned long) > {{_size}} * PyLong_SHIFT && ((8 * sizeof(unsigned long) < 53) || ({{_size-1}} * PyLong_SHIFT < 53)))) {
2490
+ {{fval}} = (double) {{pylong_join(_size, 'digits')}};
2491
+ // let CPython do its own float rounding from 2**53 on (max. consecutive integer in double float)
2492
+ if ((8 * sizeof(unsigned long) < 53) || ({{_size}} * PyLong_SHIFT < 53) || ({{fval}} < (double) ((PY_LONG_LONG)1 << 53))) {
2493
+ if (__Pyx_PyLong_IsNeg({{pyval}})) {{fval}} = -{{fval}};
2494
+ goto digits_done;
2495
+ }
2496
+ }
2497
+ // Fall through if size doesn't fit safely into a double anymore.
2498
+ // It may not be obvious that this is a safe fall-through given the "fval < 2**53"
2499
+ // check above. However, the number of digits that CPython uses for a given PyLong
2500
+ // value is minimal, and together with the "(size-1) * SHIFT < 53" check above,
2501
+ // this should make it safe.
2502
+ {{endfor}}
2503
+ }
1552
2504
  #endif
2505
+
1553
2506
  {{if op in ('Eq', 'Ne')}}
1554
- {
1555
- PyObject *res =
1556
- #if CYTHON_USE_TYPE_SLOTS || __PYX_LIMITED_VERSION_HEX >= 0x030A0000
1557
- // PyType_GetSlot only works on non-heap types from Python 3.10
1558
- __Pyx_PyType_GetSlot((&PyFloat_Type), tp_richcompare, richcmpfunc)
1559
- #else
1560
- PyObject_RichCompare
1561
- #endif
1562
- ({{'op1, op2' if order == 'CObj' else 'op2, op1'}},
1563
- Py_{{op.upper()}});
1564
- return {{'' if ret_type.is_pyobject else '__Pyx_PyObject_IsTrueAndDecref'}}(
1565
- res);
1566
- }
2507
+ {
2508
+ PyObject *res =
2509
+ #if CYTHON_USE_TYPE_SLOTS || __PYX_LIMITED_VERSION_HEX >= 0x030A0000
2510
+ // PyType_GetSlot only works on non-heap types from Python 3.10
2511
+ __Pyx_PyType_GetSlot((&PyFloat_Type), tp_richcompare, richcmpfunc)
2512
+ #else
2513
+ PyObject_RichCompare
2514
+ #endif
2515
+ ({{'op1, op2' if order == 'CObj' else 'op2, op1'}},
2516
+ Py_{{op.upper()}});
2517
+
2518
+ return {{if ret_type.is_pyobject}}res{{else}}__Pyx_PyObject_IsTrueAndDecref(res){{endif}};
2519
+ }
1567
2520
  {{else}}
1568
- {{fval}} = PyLong_AsDouble({{pyval}});
1569
- if (unlikely({{fval}} == -1.0 && PyErr_Occurred())) return NULL;
1570
- {{if zerodiv_check(fval)}}
1571
- #if !CYTHON_USE_PYLONG_INTERNALS
1572
- {{zerodiv_check(fval)}}
1573
- #endif
1574
- {{endif}}
2521
+ {{fval}} = PyLong_AsDouble({{pyval}});
2522
+ if (unlikely({{fval}} == -1.0 && PyErr_Occurred())) return NULL;
2523
+ {{if zerodiv_check(fval)}}
2524
+ #if !CYTHON_USE_PYLONG_INTERNALS
2525
+ {{zerodiv_check(fval)}}
2526
+ #endif
2527
+ {{endif}}
1575
2528
  {{endif}}
1576
- #if CYTHON_USE_PYLONG_INTERNALS
1577
- }
1578
- }
1579
- #endif
1580
2529
  } else {
1581
2530
  {{if op in ('Eq', 'Ne')}}
1582
- return {{'' if ret_type.is_pyobject else '__Pyx_PyObject_IsTrueAndDecref'}}(
1583
- PyObject_RichCompare(op1, op2, Py_{{op.upper()}}));
2531
+ return {{'PyObject_RichCompare' if ret_type.is_pyobject else '__Pyx_PyObject_RichCompareBool'}}(op1, op2, Py_{{op.upper()}});
1584
2532
  {{elif op == 'Divide'}}
1585
2533
  return (inplace ? __Pyx_PyNumber_InPlaceDivide(op1, op2) : __Pyx_PyNumber_Divide(op1, op2));
1586
2534
  {{else}}
@@ -1588,6 +2536,10 @@ static {{c_ret_type}} {{cfunc_name}}(PyObject *op1, PyObject *op2, double floatv
1588
2536
  {{endif}}
1589
2537
  }
1590
2538
 
2539
+ #if CYTHON_USE_PYLONG_INTERNALS
2540
+ digits_done:;
2541
+ #endif
2542
+
1591
2543
  {{if op in ('Eq', 'Ne')}}
1592
2544
  if (a {{c_op}} b) {
1593
2545
  {{return_true}};
@@ -1595,6 +2547,7 @@ static {{c_ret_type}} {{cfunc_name}}(PyObject *op1, PyObject *op2, double floatv
1595
2547
  {{return_false}};
1596
2548
  }
1597
2549
  {{else}}
2550
+ double result;
1598
2551
  {{if c_op == '%'}}
1599
2552
  result = fmod(a, b);
1600
2553
  if (result)