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
@@ -109,6 +109,8 @@ static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*);
109
109
  static CYTHON_INLINE int __Pyx_PyObject_IsTrueAndDecref(PyObject*);
110
110
  static CYTHON_INLINE PyObject* __Pyx_PyNumber_Long(PyObject* x);
111
111
 
112
+ #define __Pyx_PyObject_RichCompareBool(a,b,cmp) __Pyx_PyObject_IsTrueAndDecref(PyObject_RichCompare((a),(b),(cmp)))
113
+
112
114
  #define __Pyx_PySequence_Tuple(obj) \
113
115
  (likely(PyTuple_CheckExact(obj)) ? __Pyx_NewRef(obj) : PySequence_Tuple(obj))
114
116
 
@@ -119,9 +121,11 @@ static CYTHON_INLINE Py_hash_t __Pyx_PyIndex_AsHash_t(PyObject*);
119
121
  #if CYTHON_ASSUME_SAFE_MACROS
120
122
  #define __Pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
121
123
  #define __Pyx_PyFloat_AS_DOUBLE(x) PyFloat_AS_DOUBLE(x)
124
+ #define __Pyx_PyFloat_IsNonZero(x) (PyFloat_AS_DOUBLE(x) != 0.0)
122
125
  #else
123
126
  #define __Pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x)
124
127
  #define __Pyx_PyFloat_AS_DOUBLE(x) PyFloat_AsDouble(x)
128
+ #define __Pyx_PyFloat_IsNonZero(x) PyObject_IsTrue(x)
125
129
  #endif
126
130
  #define __Pyx_PyFloat_AsFloat(x) ((float) __Pyx_PyFloat_AsDouble(x))
127
131
 
@@ -138,29 +142,45 @@ static CYTHON_INLINE Py_hash_t __Pyx_PyIndex_AsHash_t(PyObject*);
138
142
  #ifndef _PyLong_NON_SIZE_BITS
139
143
  #define _PyLong_NON_SIZE_BITS 3
140
144
  #endif
141
- #define __Pyx_PyLong_Sign(x) (((PyLongObject*)x)->long_value.lv_tag & _PyLong_SIGN_MASK)
142
- #define __Pyx_PyLong_IsNeg(x) ((__Pyx_PyLong_Sign(x) & 2) != 0)
145
+ #define __Pyx_PyLong_SignBits(x) ((int) (((PyLongObject*)x)->long_value.lv_tag & _PyLong_SIGN_MASK))
146
+ #define __Pyx_PyLong_Sign(x) (1 - __Pyx_PyLong_SignBits(x))
147
+ #define __Pyx_PyLong_IsNeg(x) ((__Pyx_PyLong_SignBits(x) & 2) != 0)
143
148
  #define __Pyx_PyLong_IsNonNeg(x) (!__Pyx_PyLong_IsNeg(x))
144
- #define __Pyx_PyLong_IsZero(x) (__Pyx_PyLong_Sign(x) & 1)
145
- #define __Pyx_PyLong_IsPos(x) (__Pyx_PyLong_Sign(x) == 0)
149
+ #define __Pyx_PyLong_IsZero(x) (__Pyx_PyLong_SignBits(x) & 1)
150
+ #define __Pyx_PyLong_IsPos(x) (__Pyx_PyLong_SignBits(x) == 0)
146
151
  #define __Pyx_PyLong_CompactValueUnsigned(x) (__Pyx_PyLong_Digits(x)[0])
147
152
  #define __Pyx_PyLong_DigitCount(x) ((Py_ssize_t) (((PyLongObject*)x)->long_value.lv_tag >> _PyLong_NON_SIZE_BITS))
148
153
  #define __Pyx_PyLong_SignedDigitCount(x) \
149
- ((1 - (Py_ssize_t) __Pyx_PyLong_Sign(x)) * __Pyx_PyLong_DigitCount(x))
154
+ (((Py_ssize_t) __Pyx_PyLong_Sign(x)) * __Pyx_PyLong_DigitCount(x))
150
155
 
151
156
  #if defined(PyUnstable_Long_IsCompact) && defined(PyUnstable_Long_CompactValue)
152
157
  #define __Pyx_PyLong_IsCompact(x) PyUnstable_Long_IsCompact((PyLongObject*) x)
153
158
  #define __Pyx_PyLong_CompactValue(x) PyUnstable_Long_CompactValue((PyLongObject*) x)
154
159
  #else
155
160
  #define __Pyx_PyLong_IsCompact(x) (((PyLongObject*)x)->long_value.lv_tag < (2 << _PyLong_NON_SIZE_BITS))
156
- #define __Pyx_PyLong_CompactValue(x) ((1 - (Py_ssize_t) __Pyx_PyLong_Sign(x)) * (Py_ssize_t) __Pyx_PyLong_Digits(x)[0])
161
+ #define __Pyx_PyLong_CompactValue(x) (((Py_ssize_t) __Pyx_PyLong_Sign(x)) * (Py_ssize_t) __Pyx_PyLong_Digits(x)[0])
157
162
  #endif
158
163
 
164
+ static CYTHON_INLINE Py_ssize_t __Pyx_PyLong_CompareSignAndSize(PyObject *a, PyObject *b) {
165
+ uintptr_t tag_a = ((PyLongObject*)a)->long_value.lv_tag;
166
+ uintptr_t tag_b = ((PyLongObject*)b)->long_value.lv_tag;
167
+ if (tag_a == tag_b) return 0;
168
+ int sign_a = (int) (tag_a & _PyLong_SIGN_MASK);
169
+ int sign_b = (int) (tag_b & _PyLong_SIGN_MASK);
170
+ // 0: positive, 1: zero, 2: negative
171
+ if (sign_a > sign_b) return -1;
172
+ if (sign_a < sign_b) return 1;
173
+ Py_ssize_t size_a = (Py_ssize_t) (tag_a >> _PyLong_NON_SIZE_BITS);
174
+ Py_ssize_t size_b = (Py_ssize_t) (tag_b >> _PyLong_NON_SIZE_BITS);
175
+ return (1 - sign_a) * (size_a - size_b);
176
+ }
177
+
159
178
  // CPython 3.12 requires C99, which defines 'size_t' (but not 'ssize_t')
160
179
  typedef Py_ssize_t __Pyx_compact_pylong;
161
180
  typedef size_t __Pyx_compact_upylong;
162
181
 
163
182
  #else /* Py < 3.12 */
183
+ #define __Pyx_PyLong_Sign(x) ((int) ((Py_SIZE(x) == 0) ? 0 : (Py_SIZE(x) < 0) ? -1 : 1))
164
184
  #define __Pyx_PyLong_IsNeg(x) (Py_SIZE(x) < 0)
165
185
  #define __Pyx_PyLong_IsNonNeg(x) (Py_SIZE(x) >= 0)
166
186
  #define __Pyx_PyLong_IsZero(x) (Py_SIZE(x) == 0)
@@ -173,6 +193,8 @@ static CYTHON_INLINE Py_hash_t __Pyx_PyIndex_AsHash_t(PyObject*);
173
193
  #define __Pyx_PyLong_CompactValue(x) \
174
194
  ((Py_SIZE(x) == 0) ? (sdigit) 0 : ((Py_SIZE(x) < 0) ? -(sdigit)__Pyx_PyLong_Digits(x)[0] : (sdigit)__Pyx_PyLong_Digits(x)[0]))
175
195
 
196
+ #define __Pyx_PyLong_CompareSignAndSize(a, b) (Py_SIZE(a) - Py_SIZE(b))
197
+
176
198
  typedef sdigit __Pyx_compact_pylong;
177
199
  typedef digit __Pyx_compact_upylong;
178
200
  #endif
@@ -182,6 +204,11 @@ static CYTHON_INLINE Py_hash_t __Pyx_PyIndex_AsHash_t(PyObject*);
182
204
  #else
183
205
  #define __Pyx_PyLong_Digits(x) (((PyLongObject*)x)->ob_digit)
184
206
  #endif
207
+
208
+ // Functions/macros that must be generally available, e.g. for truth testing.
209
+ #define __Pyx_PyLong_IsNonZero(x) (!__Pyx_PyLong_IsZero(x))
210
+ #else
211
+ #define __Pyx_PyLong_IsNonZero(x) PyObject_IsTrue(x)
185
212
  #endif
186
213
 
187
214
  #if __PYX_DEFAULT_STRING_ENCODING_IS_UTF8
@@ -319,6 +346,10 @@ static CYTHON_INLINE int __Pyx_PyObject_IsTrueAndDecref(PyObject* x) {
319
346
 
320
347
  static PyObject* __Pyx_PyNumber_LongWrongResultType(PyObject* result) {
321
348
  __Pyx_TypeName result_type_name = __Pyx_PyType_GetFullyQualifiedName(Py_TYPE(result));
349
+ #if CYTHON_COMPILING_IN_LIMITED_API && __PYX_LIMITED_VERSION_HEX < 0x030d0000
350
+ if (unlikely(!result_type_name)) goto bad;
351
+ #endif
352
+
322
353
  if (PyLong_Check(result)) {
323
354
  // CPython issue #17576: warn if 'result' not of exact type int.
324
355
  if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
@@ -327,16 +358,19 @@ static PyObject* __Pyx_PyNumber_LongWrongResultType(PyObject* result) {
327
358
  "and may be removed in a future version of Python.",
328
359
  result_type_name)) {
329
360
  __Pyx_DECREF_TypeName(result_type_name);
330
- Py_DECREF(result);
331
- return NULL;
361
+ goto bad;
332
362
  }
333
363
  __Pyx_DECREF_TypeName(result_type_name);
334
364
  return result;
335
365
  }
366
+
336
367
  PyErr_Format(PyExc_TypeError,
337
368
  "__int__ returned non-int (type " __Pyx_FMT_TYPENAME ")",
338
369
  result_type_name);
370
+
339
371
  __Pyx_DECREF_TypeName(result_type_name);
372
+
373
+ bad:
340
374
  Py_DECREF(result);
341
375
  return NULL;
342
376
  }
@@ -372,37 +406,39 @@ static CYTHON_INLINE PyObject* __Pyx_PyNumber_Long(PyObject* x) {
372
406
 
373
407
  {{py: from Cython.Utility import pylong_join }}
374
408
 
375
- static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
376
- Py_ssize_t ival;
377
- PyObject *x;
378
- if (likely(PyLong_CheckExact(b))) {
409
+ static CYTHON_INLINE Py_ssize_t __Pyx_PyLong_AsSsize_t(PyObject* b) {
379
410
  #if CYTHON_USE_PYLONG_INTERNALS
380
- // handle most common case first to avoid indirect branch and optimise branch prediction
381
- if (likely(__Pyx_PyLong_IsCompact(b))) {
382
- return __Pyx_PyLong_CompactValue(b);
383
- } else {
384
- const digit* digits = __Pyx_PyLong_Digits(b);
385
- const Py_ssize_t size = __Pyx_PyLong_SignedDigitCount(b);
386
- switch (size) {
387
- {{for _size in (2, 3, 4)}}
388
- {{for _case in (_size, -_size)}}
389
- case {{_case}}:
390
- if (8 * sizeof(Py_ssize_t) > {{_size}} * PyLong_SHIFT) {
391
- return {{'-' if _case < 0 else ''}}(Py_ssize_t) {{pylong_join(_size, 'digits', 'size_t')}};
392
- }
393
- break;
394
- {{endfor}}
395
- {{endfor}}
396
- }
397
- }
411
+ Py_ssize_t ival;
412
+ const Py_ssize_t size = __Pyx_PyLong_DigitCount(b);
413
+ const int is_neg = __Pyx_PyLong_IsNeg(b);
414
+ const digit* digits = __Pyx_PyLong_Digits(b);
415
+ if (size == 0) return 0;
416
+ {{for _size in (1, 2, 3, 4)}}
417
+ #if SIZEOF_SIZE_T * 8 > {{_size}} * PyLong_SHIFT
418
+ if (size == {{_size}}) {
419
+ ival = (Py_ssize_t) {{pylong_join(_size, 'digits', 'size_t')}};
420
+ } else
398
421
  #endif
422
+ {{endfor}}
423
+ return PyLong_AsSsize_t(b);
424
+ return is_neg ? -ival : ival;
425
+
426
+ #else
399
427
  return PyLong_AsSsize_t(b);
400
- }
401
- x = PyNumber_Index(b);
402
- if (!x) return -1;
403
- ival = PyLong_AsSsize_t(x);
404
- Py_DECREF(x);
405
- return ival;
428
+ #endif
429
+ }
430
+
431
+ static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
432
+ if (likely(PyLong_Check(b))) {
433
+ return __Pyx_PyLong_AsSsize_t(b);
434
+ } else {
435
+ Py_ssize_t ival;
436
+ PyObject *x = PyNumber_Index(b);
437
+ if (unlikely(!x)) return -1;
438
+ ival = __Pyx_PyLong_AsSsize_t(x);
439
+ Py_DECREF(x);
440
+ return ival;
441
+ }
406
442
  }
407
443
 
408
444
 
@@ -435,19 +471,26 @@ static CYTHON_INLINE PyObject * __Pyx_PyLong_FromSize_t(size_t ival) {
435
471
  }
436
472
 
437
473
 
474
+ /////////////// pybuiltin_invalid.export ///////////////
475
+
476
+ static void __Pyx_PyBuiltin_Invalid(PyObject *obj, const char *builtin_type_name, const char *argname); /*proto*/
477
+
438
478
  /////////////// pybuiltin_invalid ///////////////
439
479
 
440
- static void __Pyx_PyBuiltin_Invalid(PyObject *obj, const char *type_name, const char *argname) {
480
+ static void __Pyx_PyBuiltin_Invalid(PyObject *obj, const char *builtin_type_name, const char *argname) {
441
481
  __Pyx_TypeName obj_type_name = __Pyx_PyType_GetFullyQualifiedName(Py_TYPE(obj));
482
+ #if CYTHON_COMPILING_IN_LIMITED_API && __PYX_LIMITED_VERSION_HEX < 0x030d0000
483
+ if (unlikely(!obj_type_name)) return;
484
+ #endif
442
485
  if (argname) {
443
486
  PyErr_Format(PyExc_TypeError,
444
- "Argument '%.200s' has incorrect type (expected %.200s, got " __Pyx_FMT_TYPENAME ")",
445
- argname, type_name, obj_type_name
487
+ "Argument '%.200s' has incorrect type (expected %.5s, got " __Pyx_FMT_TYPENAME ")",
488
+ argname, builtin_type_name, obj_type_name
446
489
  );
447
490
  } else {
448
491
  PyErr_Format(PyExc_TypeError,
449
- "Expected %.200s, got " __Pyx_FMT_TYPENAME,
450
- type_name, obj_type_name
492
+ "Expected %.5s, got " __Pyx_FMT_TYPENAME,
493
+ builtin_type_name, obj_type_name
451
494
  );
452
495
  }
453
496
  __Pyx_DECREF_TypeName(obj_type_name);
@@ -660,6 +703,9 @@ bad:
660
703
  static void __Pyx_seq_{{funcname}}(PyObject * o, {{struct_type_decl}} *result) {
661
704
  if (unlikely(!PySequence_Check(o))) {
662
705
  __Pyx_TypeName o_type_name = __Pyx_PyType_GetFullyQualifiedName(Py_TYPE(o));
706
+ #if CYTHON_COMPILING_IN_LIMITED_API && __PYX_LIMITED_VERSION_HEX < 0x030d0000
707
+ if (unlikely(!o_type_name)) goto bad;
708
+ #endif
663
709
  PyErr_Format(PyExc_TypeError,
664
710
  "Expected a sequence of size %zd, got " __Pyx_FMT_TYPENAME, (Py_ssize_t) {{size}}, o_type_name);
665
711
  __Pyx_DECREF_TypeName(o_type_name);
@@ -812,7 +858,7 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value);
812
858
 
813
859
  /////////////// CIntToPy ///////////////
814
860
  //@requires: GCCDiagnostics
815
- //@requires: ObjectHandling.c::PyObjectVectorCallKwBuilder
861
+ //@requires: ObjectHandling.c::PyObjectVectorcallMethodKwds
816
862
 
817
863
  static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) {
818
864
  #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
@@ -858,31 +904,41 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) {
858
904
  #else
859
905
  // call int.from_bytes()
860
906
  int one = 1; int little = (int)*(unsigned char *)&one;
861
- PyObject *from_bytes, *result = NULL, *kwds = NULL;
862
- PyObject *py_bytes = NULL, *order_str = NULL;
863
- from_bytes = PyObject_GetAttrString((PyObject*)&PyLong_Type, "from_bytes");
864
- if (!from_bytes) return NULL;
907
+ PyObject *result = NULL, *kwds = NULL;
908
+ PyObject *py_bytes = NULL, *order_str = NULL, *from_bytes_str = NULL;;
865
909
  py_bytes = PyBytes_FromStringAndSize((char*)bytes, sizeof({{TYPE}}));
866
910
  if (!py_bytes) goto limited_bad;
867
911
  // I'm deliberately not using PYIDENT here because this code path is very unlikely
868
912
  // to ever run so it seems a pessimization mostly.
913
+ from_bytes_str = PyUnicode_FromStringAndSize("from_bytes", 10);
914
+ if (!from_bytes_str) goto limited_bad;
869
915
  order_str = PyUnicode_FromString(little ? "little" : "big");
870
916
  if (!order_str) goto limited_bad;
871
917
  {
872
- PyObject *args[3+(CYTHON_VECTORCALL ? 1 : 0)] = { NULL, py_bytes, order_str };
918
+ PyObject *args[] = { (PyObject*)&PyLong_Type, py_bytes, order_str, Py_True };
873
919
  if (!is_unsigned) {
874
- kwds = __Pyx_MakeVectorcallBuilderKwds(1);
875
- if (!kwds) goto limited_bad;
876
- if (__Pyx_VectorcallBuilder_AddArgStr("signed", __Pyx_NewRef(Py_True), kwds, args+3, 0) < 0) goto limited_bad;
920
+ PyObject *signed_str = PyUnicode_FromStringAndSize("signed", 6);
921
+ if (!signed_str) goto limited_bad;
922
+ #if CYTHON_VECTORCALL
923
+ kwds = PyTuple_Pack(1, signed_str);
924
+ #else
925
+ {
926
+ PyObject *keys[] = {signed_str};
927
+ PyObject *values[] = {Py_True};
928
+ kwds = __Pyx_MakeKwargDict(keys, values, 1);
929
+ }
930
+ #endif
931
+ Py_DECREF(signed_str);
932
+ if (unlikely(!kwds)) goto limited_bad;
877
933
  }
878
- result = __Pyx_Object_Vectorcall_CallFromBuilder(from_bytes, args+1, 2 | __Pyx_PY_VECTORCALL_ARGUMENTS_OFFSET, kwds);
934
+ result = __Pyx_Object_VectorcallMethodKwds(from_bytes_str, args, 3 | __Pyx_PY_VECTORCALL_ARGUMENTS_OFFSET, kwds);
879
935
  }
880
936
 
881
937
  limited_bad:
882
938
  Py_XDECREF(kwds);
883
939
  Py_XDECREF(order_str);
884
940
  Py_XDECREF(py_bytes);
885
- Py_XDECREF(from_bytes);
941
+ Py_XDECREF(from_bytes_str);
886
942
  return result;
887
943
  #endif
888
944
  }
@@ -1169,233 +1225,332 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *);
1169
1225
 
1170
1226
  {{py: from Cython.Utility import pylong_join }}
1171
1227
 
1172
- static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
1173
- #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
1174
- #pragma GCC diagnostic push
1175
- #pragma GCC diagnostic ignored "-Wconversion"
1176
- #endif
1177
- const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = ({{TYPE}}) 0;
1178
- #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
1179
- #pragma GCC diagnostic pop
1180
- #endif
1181
- const int is_unsigned = neg_one > const_zero;
1228
+ static {{TYPE}} __Pyx_LargePyLong_{{FROM_PY_FUNCTION}}(PyObject *x); /*proto*/
1182
1229
 
1183
- if (unlikely(!PyLong_Check(x))) {
1184
- {{TYPE}} val;
1185
- PyObject *tmp = __Pyx_PyNumber_Long(x);
1186
- if (!tmp) return ({{TYPE}}) -1;
1187
- val = {{FROM_PY_FUNCTION}}(tmp);
1188
- Py_DECREF(tmp);
1189
- return val;
1190
- }
1230
+ static {{TYPE}} __Pyx_raise_neg_overflow_{{FROM_PY_FUNCTION}}(void) {
1231
+ {{if len(TYPE) <= 200}}
1232
+ const char* type_name = "{{TYPE}}";
1233
+ PyErr_Format(PyExc_OverflowError,
1234
+ "can't convert negative value to %.200s", type_name);
1235
+ {{else}}
1236
+ PyErr_SetString(PyExc_OverflowError,
1237
+ "can't convert negative value to {{TYPE}}");
1238
+ {{endif}}
1239
+ return ({{TYPE}}) -1;
1240
+ }
1191
1241
 
1192
- if (is_unsigned) {
1242
+ static {{TYPE}} __Pyx_raise_overflow_{{FROM_PY_FUNCTION}}(void) {
1243
+ {{if len(TYPE) <= 200}}
1244
+ const char* type_name = "{{TYPE}}";
1245
+ PyErr_Format(PyExc_OverflowError,
1246
+ "value too large to convert to %.200s", type_name);
1247
+ {{else}}
1248
+ PyErr_SetString(PyExc_OverflowError,
1249
+ "value too large to convert to {{TYPE}}");
1250
+ {{endif}}
1251
+ return ({{TYPE}}) -1;
1252
+ }
1253
+
1254
+ static CYTHON_INLINE {{TYPE}} __Pyx_PyULong_{{FROM_PY_FUNCTION}}(PyObject *x) {
1255
+ const int is_unsigned = 1;
1193
1256
  #if CYTHON_USE_PYLONG_INTERNALS
1194
- if (unlikely(__Pyx_PyLong_IsNeg(x))) {
1195
- goto raise_neg_overflow;
1196
- //} else if (__Pyx_PyLong_IsZero(x)) {
1197
- // return ({{TYPE}}) 0;
1198
- } else if (__Pyx_PyLong_IsCompact(x)) {
1199
- __PYX_VERIFY_RETURN_INT({{TYPE}}, __Pyx_compact_upylong, __Pyx_PyLong_CompactValueUnsigned(x))
1200
- } else {
1201
- const digit* digits = __Pyx_PyLong_Digits(x);
1202
- assert(__Pyx_PyLong_DigitCount(x) > 1);
1203
- switch (__Pyx_PyLong_DigitCount(x)) {
1204
- {{for _size in (2, 3, 4)}}
1205
- case {{_size}}:
1206
- if ((8 * sizeof({{TYPE}}) > {{_size-1}} * PyLong_SHIFT)) {
1207
- if ((8 * sizeof(unsigned long) > {{_size}} * PyLong_SHIFT)) {
1208
- __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long, {{pylong_join(_size, 'digits')}})
1209
- } else if ((8 * sizeof({{TYPE}}) >= {{_size}} * PyLong_SHIFT)) {
1210
- return ({{TYPE}}) {{pylong_join(_size, 'digits', TYPE)}};
1211
- }
1212
- }
1213
- break;
1214
- {{endfor}}
1257
+ // IsNeg() and IsCompact() were already checked in "__Pyx_PyLong_...()",
1258
+ // so 'x' is positive and at least two digits.
1259
+ {
1260
+ const digit* digits = __Pyx_PyLong_Digits(x);
1261
+ const Py_ssize_t size = __Pyx_PyLong_DigitCount(x);
1262
+ {{for _size in (2, 3, 4)}}
1263
+ if (size == {{_size}} && (8 * sizeof({{TYPE}}) > {{_size-1}} * PyLong_SHIFT)) {
1264
+ if ((8 * sizeof(unsigned long) > {{_size}} * PyLong_SHIFT)) {
1265
+ __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long, {{pylong_join(_size, 'digits')}})
1266
+ } else if ((8 * sizeof({{TYPE}}) >= {{_size}} * PyLong_SHIFT)) {
1267
+ return ({{TYPE}}) {{pylong_join(_size, 'digits', TYPE)}};
1215
1268
  }
1216
- }
1217
- #endif
1218
- #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX < 0x030C00A7
1219
- if (unlikely(Py_SIZE(x) < 0)) {
1220
- goto raise_neg_overflow;
1221
- }
1269
+ } else
1270
+ {{endfor}}
1271
+ {}
1272
+ }
1273
+ #elif CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX < 0x030C00A7
1274
+ if (unlikely(Py_SIZE(x) < 0)) {
1275
+ goto raise_neg_overflow;
1276
+ }
1222
1277
  #else
1223
- {
1224
- // misuse Py_False as a quick way to compare to a '0' int object in PyPy
1225
- int result = PyObject_RichCompareBool(x, Py_False, Py_LT);
1226
- if (unlikely(result < 0))
1227
- return ({{TYPE}}) -1;
1228
- if (unlikely(result == 1))
1229
- goto raise_neg_overflow;
1230
- }
1278
+ {
1279
+ // misuse Py_False as a quick way to compare to a '0' int object in PyPy
1280
+ int result = PyObject_RichCompareBool(x, Py_False, Py_LT);
1281
+ if (unlikely(result < 0))
1282
+ return ({{TYPE}}) -1;
1283
+ if (unlikely(result == 1))
1284
+ goto raise_neg_overflow;
1285
+ }
1231
1286
  #endif
1232
- if ((sizeof({{TYPE}}) <= sizeof(unsigned long))) {
1233
- __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, unsigned long, PyLong_AsUnsignedLong(x))
1234
- } else if ((sizeof({{TYPE}}) <= sizeof(unsigned PY_LONG_LONG))) {
1235
- __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x))
1236
- }
1287
+ if ((sizeof({{TYPE}}) <= sizeof(unsigned long))) {
1288
+ __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, unsigned long, PyLong_AsUnsignedLong(x))
1289
+ } else if ((sizeof({{TYPE}}) <= sizeof(unsigned PY_LONG_LONG))) {
1290
+ __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x))
1291
+ }
1237
1292
 
1238
- } else {
1239
- // signed
1293
+ return __Pyx_LargePyLong_{{FROM_PY_FUNCTION}}(x);
1294
+
1295
+ raise_neg_overflow:
1296
+ return __Pyx_raise_neg_overflow_{{FROM_PY_FUNCTION}}();
1297
+
1298
+ raise_overflow:
1299
+ return __Pyx_raise_overflow_{{FROM_PY_FUNCTION}}();
1300
+ }
1301
+
1302
+ static CYTHON_INLINE {{TYPE}} __Pyx_PySLong_{{FROM_PY_FUNCTION}}(PyObject *x) {
1303
+ const int is_unsigned = 0;
1240
1304
  #if CYTHON_USE_PYLONG_INTERNALS
1241
- if (__Pyx_PyLong_IsCompact(x)) {
1242
- __PYX_VERIFY_RETURN_INT({{TYPE}}, __Pyx_compact_pylong, __Pyx_PyLong_CompactValue(x))
1243
- } else {
1244
- const digit* digits = __Pyx_PyLong_Digits(x);
1245
- assert(__Pyx_PyLong_DigitCount(x) > 1);
1246
- switch (__Pyx_PyLong_SignedDigitCount(x)) {
1247
- {{for _size in (2, 3, 4)}}
1248
- {{for _case in (-_size, _size)}}
1249
- case {{_case}}:
1250
- if ((8 * sizeof({{TYPE}}){{' - 1' if _case < 0 else ''}} > {{_size-1}} * PyLong_SHIFT)) {
1251
- if ((8 * sizeof(unsigned long) > {{_size}} * PyLong_SHIFT)) {
1252
- __PYX_VERIFY_RETURN_INT({{TYPE}}, {{'long' if _case < 0 else 'unsigned long'}}, {{'-(long) ' if _case < 0 else ''}}{{pylong_join(_size, 'digits')}})
1253
- } else if ((8 * sizeof({{TYPE}}) - 1 > {{_size}} * PyLong_SHIFT)) {
1254
- return ({{TYPE}}) ({{'((%s)-1)*' % TYPE if _case < 0 else ''}}{{pylong_join(_size, 'digits', TYPE)}});
1255
- }
1256
- }
1257
- break;
1258
- {{endfor}}
1259
- {{endfor}}
1305
+ // IsCompact() was already checked in "__Pyx_PyLong_...()",
1306
+ // so 'x' is non-zero and at least two digits.
1307
+ if (__Pyx_PyLong_IsNeg(x)) {
1308
+ const Py_ssize_t size = __Pyx_PyLong_DigitCount(x);
1309
+ const digit* digits = __Pyx_PyLong_Digits(x);
1310
+ {{for _size in (2, 3, 4)}}
1311
+ if (size == {{_size}} && (8 * sizeof({{TYPE}}) > {{_size-1}} * PyLong_SHIFT)) {
1312
+ if ((8 * sizeof(long) > {{_size}} * PyLong_SHIFT)) {
1313
+ long ival = - (long) {{pylong_join(_size, 'digits')}};
1314
+ __PYX_VERIFY_RETURN_INT({{TYPE}}, long, ival)
1315
+ } else if ((8 * sizeof({{TYPE}}) - 1 > {{_size}} * PyLong_SHIFT)) {
1316
+ return ({{TYPE}}) ((({{TYPE}}) -1) * {{pylong_join(_size, 'digits', TYPE)}});
1260
1317
  }
1261
- }
1318
+ } else
1319
+ {{endfor}}
1320
+ {}
1321
+ } else {
1322
+ const Py_ssize_t size = __Pyx_PyLong_DigitCount(x);
1323
+ const digit* digits = __Pyx_PyLong_Digits(x);
1324
+ {{for _size in (2, 3, 4)}}
1325
+ if (size == {{_size}} && (8 * sizeof({{TYPE}}) > {{_size-1}} * PyLong_SHIFT)) {
1326
+ if ((8 * sizeof(long) > {{_size}} * PyLong_SHIFT)) {
1327
+ __PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long, {{pylong_join(_size, 'digits')}})
1328
+ } else if ((8 * sizeof({{TYPE}}) - 1 > {{_size}} * PyLong_SHIFT)) {
1329
+ return ({{TYPE}}) {{pylong_join(_size, 'digits', TYPE)}};
1330
+ }
1331
+ } else
1332
+ {{endfor}}
1333
+ {}
1334
+ }
1262
1335
  #endif
1263
- if ((sizeof({{TYPE}}) <= sizeof(long))) {
1264
- __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, long, PyLong_AsLong(x))
1265
- } else if ((sizeof({{TYPE}}) <= sizeof(PY_LONG_LONG))) {
1266
- __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, PY_LONG_LONG, PyLong_AsLongLong(x))
1267
- }
1336
+
1337
+ #if __PYX_LIMITED_VERSION_HEX >= 0x030d0000
1338
+ if ((sizeof({{TYPE}}) <= sizeof(int)) && (sizeof(int) < sizeof(long))) {
1339
+ __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, int, PyLong_AsInt(x))
1340
+ } else
1341
+ #endif
1342
+ if ((sizeof({{TYPE}}) <= sizeof(long))) {
1343
+ __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, long, PyLong_AsLong(x))
1344
+ } else if ((sizeof({{TYPE}}) <= sizeof(PY_LONG_LONG))) {
1345
+ __PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, PY_LONG_LONG, PyLong_AsLongLong(x))
1268
1346
  }
1269
1347
 
1270
- // large integer type and no access to PyLong internals => allow for a more expensive conversion
1271
- {
1272
- {{TYPE}} val;
1273
- int ret = -1;
1348
+ return __Pyx_LargePyLong_{{FROM_PY_FUNCTION}}(x);
1349
+
1350
+ raise_neg_overflow:
1351
+ return __Pyx_raise_neg_overflow_{{FROM_PY_FUNCTION}}();
1352
+
1353
+ raise_overflow:
1354
+ return __Pyx_raise_overflow_{{FROM_PY_FUNCTION}}();
1355
+ }
1356
+
1357
+ static {{TYPE}} __Pyx_LargePyLong_{{FROM_PY_FUNCTION}}(PyObject *x) {
1358
+ // large integer type and no access to PyLong internals => allow for a more expensive conversion
1359
+ #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
1360
+ #pragma GCC diagnostic push
1361
+ #pragma GCC diagnostic ignored "-Wconversion"
1362
+ #endif
1363
+ const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = ({{TYPE}}) 0;
1364
+ #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
1365
+ #pragma GCC diagnostic pop
1366
+ #endif
1367
+ const int is_unsigned = neg_one > const_zero;
1368
+
1369
+ {{TYPE}} val;
1370
+ int ret = -1;
1274
1371
  #if PY_VERSION_HEX >= 0x030d00A6 && !CYTHON_COMPILING_IN_LIMITED_API
1275
- Py_ssize_t bytes_copied = PyLong_AsNativeBytes(
1276
- x, &val, sizeof(val), Py_ASNATIVEBYTES_NATIVE_ENDIAN | (is_unsigned ? Py_ASNATIVEBYTES_UNSIGNED_BUFFER | Py_ASNATIVEBYTES_REJECT_NEGATIVE : 0));
1277
- if (unlikely(bytes_copied == -1)) {
1278
- // failed
1279
- } else if (unlikely(bytes_copied > (Py_ssize_t) sizeof(val))) {
1280
- goto raise_overflow;
1281
- } else {
1282
- ret = 0;
1283
- }
1372
+ Py_ssize_t bytes_copied = PyLong_AsNativeBytes(
1373
+ x, &val, sizeof(val), Py_ASNATIVEBYTES_NATIVE_ENDIAN | (is_unsigned ? Py_ASNATIVEBYTES_UNSIGNED_BUFFER | Py_ASNATIVEBYTES_REJECT_NEGATIVE : 0));
1374
+ if (unlikely(bytes_copied == -1)) {
1375
+ // failed
1376
+ } else if (unlikely(bytes_copied > (Py_ssize_t) sizeof(val))) {
1377
+ goto raise_overflow;
1378
+ } else {
1379
+ ret = 0;
1380
+ }
1284
1381
  #elif PY_VERSION_HEX < 0x030d0000 && !(CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_LIMITED_API) || defined(_PyLong_AsByteArray)
1285
- int one = 1; int is_little = (int)*(unsigned char *)&one;
1286
- unsigned char *bytes = (unsigned char *)&val;
1287
- ret = _PyLong_AsByteArray((PyLongObject *)x,
1288
- bytes, sizeof(val),
1289
- is_little, !is_unsigned);
1382
+ int one = 1; int is_little = (int)*(unsigned char *)&one;
1383
+ unsigned char *bytes = (unsigned char *)&val;
1384
+ ret = _PyLong_AsByteArray((PyLongObject *)x,
1385
+ bytes, sizeof(val),
1386
+ is_little, !is_unsigned);
1387
+ // Avoid unused label warning.
1388
+ if ((0)) goto raise_overflow;
1290
1389
  #else
1291
1390
  {{if IS_ENUM}}
1292
- // The fallback implementation uses math operations like shifting, which do not work well with enums.
1293
- PyErr_SetString(PyExc_RuntimeError,
1294
- "_PyLong_AsByteArray() or PyLong_AsNativeBytes() not available, cannot convert large enums");
1295
- val = ({{TYPE}}) -1;
1391
+ // The fallback implementation uses math operations like shifting, which do not work well with enums.
1392
+ PyErr_SetString(PyExc_RuntimeError,
1393
+ "_PyLong_AsByteArray() or PyLong_AsNativeBytes() not available, cannot convert large enums");
1394
+ val = ({{TYPE}}) -1;
1395
+ // Avoid unused label warning.
1396
+ if ((0)) goto raise_overflow;
1296
1397
  {{else}}
1297
1398
  // Inefficient copy of bit chunks through the C-API. Probably still better than a "cannot do this" exception.
1298
1399
  // This is substantially faster in CPython (>30%) than calling "int.to_bytes()" through the C-API.
1299
- PyObject *v;
1300
- PyObject *stepval = NULL, *mask = NULL, *shift = NULL;
1301
- int bits, remaining_bits, is_negative = 0;
1302
- int chunk_size = (sizeof(long) < 8) ? 30 : 62;
1303
-
1304
- // Use exact PyLong to prevent user defined &&/<</etc. implementations (and make Py_SIZE() work below).
1305
- if (likely(PyLong_CheckExact(x))) {
1306
- v = __Pyx_NewRef(x);
1307
- } else {
1308
- v = PyNumber_Long(x);
1309
- if (unlikely(!v)) return ({{TYPE}}) -1;
1310
- assert(PyLong_CheckExact(v));
1311
- }
1312
-
1313
- // Misuse Py_False as a quick way to compare to a '0' int object.
1314
- {
1315
- int result = PyObject_RichCompareBool(v, Py_False, Py_LT);
1316
- if (unlikely(result < 0)) {
1317
- Py_DECREF(v);
1318
- return ({{TYPE}}) -1;
1319
- }
1320
- is_negative = result == 1;
1321
- }
1400
+ PyObject *v;
1401
+ PyObject *stepval = NULL, *mask = NULL, *shift = NULL;
1402
+ int bits, remaining_bits, is_negative = 0;
1403
+ int chunk_size = (sizeof(long) < 8) ? 30 : 62;
1404
+
1405
+ // Use exact PyLong to prevent user defined &&/<</etc. implementations.
1406
+ if (likely(PyLong_CheckExact(x))) {
1407
+ v = __Pyx_NewRef(x);
1408
+ } else {
1409
+ v = PyNumber_Long(x);
1410
+ if (unlikely(!v)) return ({{TYPE}}) -1;
1411
+ assert(PyLong_CheckExact(v));
1412
+ }
1322
1413
 
1323
- if (is_unsigned && unlikely(is_negative)) {
1324
- Py_DECREF(v);
1325
- goto raise_neg_overflow;
1326
- } else if (is_negative) {
1327
- // bit-invert to make sure we can safely convert it
1328
- stepval = PyNumber_Invert(v);
1414
+ // Misuse Py_False as a quick way to compare to a '0' int object.
1415
+ {
1416
+ int result = PyObject_RichCompareBool(v, Py_False, Py_LT);
1417
+ if (unlikely(result < 0)) {
1329
1418
  Py_DECREF(v);
1330
- if (unlikely(!stepval))
1331
- return ({{TYPE}}) -1;
1332
- } else {
1333
- stepval = v;
1334
- }
1335
- v = NULL;
1336
-
1337
- // Unpack full chunks of bits.
1338
- val = ({{TYPE}}) 0;
1339
- mask = PyLong_FromLong((1L << chunk_size) - 1); if (unlikely(!mask)) goto done;
1340
- shift = PyLong_FromLong(chunk_size); if (unlikely(!shift)) goto done;
1341
- for (bits = 0; bits < (int) sizeof({{TYPE}}) * 8 - chunk_size; bits += chunk_size) {
1342
- PyObject *tmp, *digit;
1343
- long idigit;
1344
-
1345
- digit = PyNumber_And(stepval, mask);
1346
- if (unlikely(!digit)) goto done;
1347
-
1348
- idigit = PyLong_AsLong(digit);
1349
- Py_DECREF(digit);
1350
- if (unlikely(idigit < 0)) goto done;
1351
- val |= (({{TYPE}}) idigit) << bits;
1352
-
1353
- tmp = PyNumber_Rshift(stepval, shift);
1354
- if (unlikely(!tmp)) goto done;
1355
- Py_DECREF(stepval); stepval = tmp;
1419
+ return ({{TYPE}}) -1;
1356
1420
  }
1421
+ is_negative = result == 1;
1422
+ }
1423
+
1424
+ if (is_unsigned && unlikely(is_negative)) {
1425
+ Py_DECREF(v);
1426
+ PyErr_SetString(PyExc_OverflowError,
1427
+ "can't convert negative value to {{TYPE}}");
1428
+ return ({{TYPE}}) -1;
1429
+ } else if (is_negative) {
1430
+ // bit-invert to make sure we can safely convert it
1431
+ stepval = PyNumber_Invert(v);
1432
+ Py_DECREF(v);
1433
+ if (unlikely(!stepval))
1434
+ return ({{TYPE}}) -1;
1435
+ } else {
1436
+ stepval = v;
1437
+ }
1438
+ v = NULL;
1439
+
1440
+ // Unpack full chunks of bits.
1441
+ val = ({{TYPE}}) 0;
1442
+ mask = PyLong_FromLong((1L << chunk_size) - 1); if (unlikely(!mask)) goto done;
1443
+ shift = PyLong_FromLong(chunk_size); if (unlikely(!shift)) goto done;
1444
+ for (bits = 0; bits < (int) sizeof({{TYPE}}) * 8 - chunk_size; bits += chunk_size) {
1445
+ PyObject *tmp, *digit;
1446
+ long idigit;
1447
+
1448
+ digit = PyNumber_And(stepval, mask);
1449
+ if (unlikely(!digit)) goto done;
1450
+
1451
+ idigit = PyLong_AsLong(digit);
1452
+ Py_DECREF(digit);
1453
+ if (unlikely(idigit < 0)) goto done;
1454
+ val |= (({{TYPE}}) idigit) << bits;
1455
+
1456
+ tmp = PyNumber_Rshift(stepval, shift);
1457
+ if (unlikely(!tmp)) goto done;
1458
+ Py_DECREF(stepval); stepval = tmp;
1459
+ }
1357
1460
 
1358
- Py_DECREF(shift); shift = NULL;
1359
- Py_DECREF(mask); mask = NULL;
1461
+ Py_DECREF(shift); shift = NULL;
1462
+ Py_DECREF(mask); mask = NULL;
1360
1463
 
1361
- // Add the last bits and detect overflow.
1362
- {
1363
- long idigit = PyLong_AsLong(stepval);
1364
- if (unlikely(idigit < 0)) goto done;
1365
- remaining_bits = ((int) sizeof({{TYPE}}) * 8) - bits - (is_unsigned ? 0 : 1);
1366
- if (unlikely(idigit >= (1L << remaining_bits)))
1367
- goto raise_overflow;
1368
- val |= (({{TYPE}}) idigit) << bits;
1369
- }
1464
+ // Add the last bits and detect overflow.
1465
+ {
1466
+ long idigit = PyLong_AsLong(stepval);
1467
+ if (unlikely(idigit < 0)) goto done;
1468
+ remaining_bits = ((int) sizeof({{TYPE}}) * 8) - bits - (is_unsigned ? 0 : 1);
1469
+ if (unlikely(idigit >= (1L << remaining_bits)))
1470
+ goto raise_overflow;
1471
+ val |= (({{TYPE}}) idigit) << bits;
1472
+ }
1370
1473
 
1371
- // Handle sign and overflow into sign bit.
1372
- if (!is_unsigned) {
1373
- // gcc warns about unsigned (val < 0) => test sign bit instead
1374
- if (unlikely(val & ((({{TYPE}}) 1) << (sizeof({{TYPE}}) * 8 - 1))))
1375
- goto raise_overflow;
1376
- // undo the PyNumber_Invert() above
1377
- if (is_negative)
1378
- val = ~val;
1379
- }
1380
- ret = 0;
1381
- done:
1382
- Py_XDECREF(shift);
1383
- Py_XDECREF(mask);
1384
- Py_XDECREF(stepval);
1474
+ // Handle sign and overflow into sign bit.
1475
+ if (!is_unsigned) {
1476
+ // gcc warns about unsigned (val < 0) => test sign bit instead
1477
+ if (unlikely(val & ((({{TYPE}}) 1) << (sizeof({{TYPE}}) * 8 - 1))))
1478
+ goto raise_overflow;
1479
+ // undo the PyNumber_Invert() above
1480
+ if (is_negative)
1481
+ val = ~val;
1482
+ }
1483
+ ret = 0;
1484
+ done:
1485
+ Py_XDECREF(shift);
1486
+ Py_XDECREF(mask);
1487
+ Py_XDECREF(stepval);
1385
1488
  {{endif}}
1386
1489
  #endif
1387
- if (unlikely(ret))
1388
- return ({{TYPE}}) -1;
1389
- return val;
1390
- }
1490
+ if (unlikely(ret))
1491
+ return ({{TYPE}}) -1;
1492
+ return val;
1391
1493
 
1392
1494
  raise_overflow:
1393
- PyErr_SetString(PyExc_OverflowError,
1394
- "value too large to convert to {{TYPE}}");
1395
- return ({{TYPE}}) -1;
1495
+ return __Pyx_raise_overflow_{{FROM_PY_FUNCTION}}();
1496
+ }
1396
1497
 
1498
+ static CYTHON_INLINE {{TYPE}} __Pyx_PyLong_{{FROM_PY_FUNCTION}}(PyObject *x) {
1499
+ #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
1500
+ #pragma GCC diagnostic push
1501
+ #pragma GCC diagnostic ignored "-Wconversion"
1502
+ #endif
1503
+ const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = ({{TYPE}}) 0;
1504
+ #ifdef __Pyx_HAS_GCC_DIAGNOSTIC
1505
+ #pragma GCC diagnostic pop
1506
+ #endif
1507
+ const int is_unsigned = neg_one > const_zero;
1508
+
1509
+ if (is_unsigned) {
1510
+ #if CYTHON_USE_PYLONG_INTERNALS
1511
+ // IsNeg() check is required here and not done later!
1512
+ if (unlikely(__Pyx_PyLong_IsNeg(x))) {
1513
+ goto raise_neg_overflow;
1514
+ } else if (__Pyx_PyLong_IsCompact(x)) {
1515
+ __PYX_VERIFY_RETURN_INT({{TYPE}}, __Pyx_compact_upylong, __Pyx_PyLong_CompactValueUnsigned(x))
1516
+ } else
1517
+ #endif
1518
+ {
1519
+ return __Pyx_PyULong_{{FROM_PY_FUNCTION}}(x);
1520
+ }
1521
+ } else {
1522
+ #if CYTHON_USE_PYLONG_INTERNALS
1523
+ if (__Pyx_PyLong_IsCompact(x)) {
1524
+ __PYX_VERIFY_RETURN_INT({{TYPE}}, __Pyx_compact_pylong, __Pyx_PyLong_CompactValue(x))
1525
+ } else
1526
+ #endif
1527
+ {
1528
+ return __Pyx_PySLong_{{FROM_PY_FUNCTION}}(x);
1529
+ }
1530
+ }
1531
+
1532
+ #if CYTHON_USE_PYLONG_INTERNALS
1397
1533
  raise_neg_overflow:
1398
- PyErr_SetString(PyExc_OverflowError,
1399
- "can't convert negative value to {{TYPE}}");
1400
- return ({{TYPE}}) -1;
1534
+ return __Pyx_raise_neg_overflow_{{FROM_PY_FUNCTION}}();
1535
+
1536
+ raise_overflow:
1537
+ return __Pyx_raise_overflow_{{FROM_PY_FUNCTION}}();
1538
+ #endif
1539
+ }
1540
+
1541
+ static {{TYPE}} __Pyx_NonPyLong_{{FROM_PY_FUNCTION}}(PyObject *x) {
1542
+ {{TYPE}} val;
1543
+ PyObject *tmp = __Pyx_PyNumber_Long(x);
1544
+ if (!tmp) return ({{TYPE}}) -1;
1545
+ val = {{FROM_PY_FUNCTION}}(tmp);
1546
+ Py_DECREF(tmp);
1547
+ return val;
1548
+ }
1549
+
1550
+ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
1551
+ if (likely(PyLong_Check(x))) {
1552
+ return __Pyx_PyLong_{{FROM_PY_FUNCTION}}(x);
1553
+ } else {
1554
+ return __Pyx_NonPyLong_{{FROM_PY_FUNCTION}}(x);
1555
+ }
1401
1556
  }