1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
|
/////////////// TypeConversions.proto ///////////////
//@requires: StringTools.c::IncludeStringH
/* Type Conversion Predeclarations */
#define __Pyx_uchar_cast(c) ((unsigned char)c)
#define __Pyx_long_cast(x) ((long)x)
#define __Pyx_fits_Py_ssize_t(v, type, is_signed) ( \
(sizeof(type) < sizeof(Py_ssize_t)) || \
(sizeof(type) > sizeof(Py_ssize_t) && \
likely(v < (type)PY_SSIZE_T_MAX || \
v == (type)PY_SSIZE_T_MAX) && \
(!is_signed || likely(v > (type)PY_SSIZE_T_MIN || \
v == (type)PY_SSIZE_T_MIN))) || \
(sizeof(type) == sizeof(Py_ssize_t) && \
(is_signed || likely(v < (type)PY_SSIZE_T_MAX || \
v == (type)PY_SSIZE_T_MAX))) )
static CYTHON_INLINE int __Pyx_is_valid_index(Py_ssize_t i, Py_ssize_t limit) {
// Optimisation from Section 14.2 "Bounds Checking" in
// https://www.agner.org/optimize/optimizing_cpp.pdf
// See https://bugs.python.org/issue28397
// The cast to unsigned effectively tests for "0 <= i < limit".
return (size_t) i < (size_t) limit;
}
// fast and unsafe abs(Py_ssize_t) that ignores the overflow for (-PY_SSIZE_T_MAX-1)
#if defined (__cplusplus) && __cplusplus >= 201103L
#include <cstdlib>
#define __Pyx_sst_abs(value) std::abs(value)
#elif SIZEOF_INT >= SIZEOF_SIZE_T
#define __Pyx_sst_abs(value) abs(value)
#elif SIZEOF_LONG >= SIZEOF_SIZE_T
#define __Pyx_sst_abs(value) labs(value)
#elif defined (_MSC_VER)
// abs() is defined for long, but 64-bits type on MSVC is long long.
// Use MS-specific _abs64 instead.
#define __Pyx_sst_abs(value) ((Py_ssize_t)_abs64(value))
#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
#define __Pyx_sst_abs(value) llabs(value)
#elif defined (__GNUC__)
// gcc or clang on 64 bit windows.
#define __Pyx_sst_abs(value) __builtin_llabs(value)
#else
#define __Pyx_sst_abs(value) ((value<0) ? -value : value)
#endif
static CYTHON_INLINE Py_ssize_t __Pyx_ssize_strlen(const char *s);
static CYTHON_INLINE const char* __Pyx_PyObject_AsString(PyObject*);
static CYTHON_INLINE const char* __Pyx_PyObject_AsStringAndSize(PyObject*, Py_ssize_t* length);
static CYTHON_INLINE PyObject* __Pyx_PyByteArray_FromString(const char*);
#define __Pyx_PyByteArray_FromStringAndSize(s, l) PyByteArray_FromStringAndSize((const char*)s, l)
#define __Pyx_PyBytes_FromString PyBytes_FromString
#define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize
static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char*);
#if PY_MAJOR_VERSION < 3
#define __Pyx_PyStr_FromString __Pyx_PyBytes_FromString
#define __Pyx_PyStr_FromStringAndSize __Pyx_PyBytes_FromStringAndSize
#else
#define __Pyx_PyStr_FromString __Pyx_PyUnicode_FromString
#define __Pyx_PyStr_FromStringAndSize __Pyx_PyUnicode_FromStringAndSize
#endif
#define __Pyx_PyBytes_AsWritableString(s) ((char*) PyBytes_AS_STRING(s))
#define __Pyx_PyBytes_AsWritableSString(s) ((signed char*) PyBytes_AS_STRING(s))
#define __Pyx_PyBytes_AsWritableUString(s) ((unsigned char*) PyBytes_AS_STRING(s))
#define __Pyx_PyBytes_AsString(s) ((const char*) PyBytes_AS_STRING(s))
#define __Pyx_PyBytes_AsSString(s) ((const signed char*) PyBytes_AS_STRING(s))
#define __Pyx_PyBytes_AsUString(s) ((const unsigned char*) PyBytes_AS_STRING(s))
#define __Pyx_PyObject_AsWritableString(s) ((char*)(__pyx_uintptr_t) __Pyx_PyObject_AsString(s))
#define __Pyx_PyObject_AsWritableSString(s) ((signed char*)(__pyx_uintptr_t) __Pyx_PyObject_AsString(s))
#define __Pyx_PyObject_AsWritableUString(s) ((unsigned char*)(__pyx_uintptr_t) __Pyx_PyObject_AsString(s))
#define __Pyx_PyObject_AsSString(s) ((const signed char*) __Pyx_PyObject_AsString(s))
#define __Pyx_PyObject_AsUString(s) ((const unsigned char*) __Pyx_PyObject_AsString(s))
#define __Pyx_PyObject_FromCString(s) __Pyx_PyObject_FromString((const char*)s)
#define __Pyx_PyBytes_FromCString(s) __Pyx_PyBytes_FromString((const char*)s)
#define __Pyx_PyByteArray_FromCString(s) __Pyx_PyByteArray_FromString((const char*)s)
#define __Pyx_PyStr_FromCString(s) __Pyx_PyStr_FromString((const char*)s)
#define __Pyx_PyUnicode_FromCString(s) __Pyx_PyUnicode_FromString((const char*)s)
#define __Pyx_PyUnicode_FromOrdinal(o) PyUnicode_FromOrdinal((int)o)
#define __Pyx_PyUnicode_AsUnicode PyUnicode_AsUnicode
#define __Pyx_NewRef(obj) (Py_INCREF(obj), obj)
#define __Pyx_Owned_Py_None(b) __Pyx_NewRef(Py_None)
static CYTHON_INLINE PyObject * __Pyx_PyBool_FromLong(long b);
static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*);
static CYTHON_INLINE int __Pyx_PyObject_IsTrueAndDecref(PyObject*);
static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x);
#define __Pyx_PySequence_Tuple(obj) \
(likely(PyTuple_CheckExact(obj)) ? __Pyx_NewRef(obj) : PySequence_Tuple(obj))
static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*);
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);
static CYTHON_INLINE Py_hash_t __Pyx_PyIndex_AsHash_t(PyObject*);
#if CYTHON_ASSUME_SAFE_MACROS
#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
#else
#define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x)
#endif
#define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x))
#if PY_MAJOR_VERSION >= 3
#define __Pyx_PyNumber_Int(x) (PyLong_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Long(x))
#else
#define __Pyx_PyNumber_Int(x) (PyInt_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Int(x))
#endif
// __Pyx_PyNumber_Float is now in its own section since it has dependencies (needed to make
// string conversion work the same in all circumstances).
#if CYTHON_USE_PYLONG_INTERNALS
#if PY_VERSION_HEX >= 0x030C00A7
#ifndef _PyLong_SIGN_MASK
#define _PyLong_SIGN_MASK 3
#endif
#ifndef _PyLong_NON_SIZE_BITS
#define _PyLong_NON_SIZE_BITS 3
#endif
#define __Pyx_PyLong_Sign(x) (((PyLongObject*)x)->long_value.lv_tag & _PyLong_SIGN_MASK)
#define __Pyx_PyLong_IsNeg(x) ((__Pyx_PyLong_Sign(x) & 2) != 0)
#define __Pyx_PyLong_IsNonNeg(x) (!__Pyx_PyLong_IsNeg(x))
#define __Pyx_PyLong_IsZero(x) (__Pyx_PyLong_Sign(x) & 1)
#define __Pyx_PyLong_IsPos(x) (__Pyx_PyLong_Sign(x) == 0)
#define __Pyx_PyLong_CompactValueUnsigned(x) (__Pyx_PyLong_Digits(x)[0])
#define __Pyx_PyLong_DigitCount(x) ((Py_ssize_t) (((PyLongObject*)x)->long_value.lv_tag >> _PyLong_NON_SIZE_BITS))
#define __Pyx_PyLong_SignedDigitCount(x) \
((1 - (Py_ssize_t) __Pyx_PyLong_Sign(x)) * __Pyx_PyLong_DigitCount(x))
#if defined(PyUnstable_Long_IsCompact) && defined(PyUnstable_Long_CompactValue)
#define __Pyx_PyLong_IsCompact(x) PyUnstable_Long_IsCompact((PyLongObject*) x)
#define __Pyx_PyLong_CompactValue(x) PyUnstable_Long_CompactValue((PyLongObject*) x)
#else
#define __Pyx_PyLong_IsCompact(x) (((PyLongObject*)x)->long_value.lv_tag < (2 << _PyLong_NON_SIZE_BITS))
#define __Pyx_PyLong_CompactValue(x) ((1 - (Py_ssize_t) __Pyx_PyLong_Sign(x)) * (Py_ssize_t) __Pyx_PyLong_Digits(x)[0])
#endif
// CPython 3.12 requires C99, which defines 'size_t' (but not 'ssize_t')
typedef Py_ssize_t __Pyx_compact_pylong;
typedef size_t __Pyx_compact_upylong;
#else /* Py < 3.12 */
#define __Pyx_PyLong_IsNeg(x) (Py_SIZE(x) < 0)
#define __Pyx_PyLong_IsNonNeg(x) (Py_SIZE(x) >= 0)
#define __Pyx_PyLong_IsZero(x) (Py_SIZE(x) == 0)
#define __Pyx_PyLong_IsPos(x) (Py_SIZE(x) > 0)
#define __Pyx_PyLong_CompactValueUnsigned(x) ((Py_SIZE(x) == 0) ? 0 : __Pyx_PyLong_Digits(x)[0])
#define __Pyx_PyLong_DigitCount(x) __Pyx_sst_abs(Py_SIZE(x))
#define __Pyx_PyLong_SignedDigitCount(x) Py_SIZE(x)
#define __Pyx_PyLong_IsCompact(x) (Py_SIZE(x) == 0 || Py_SIZE(x) == 1 || Py_SIZE(x) == -1)
#define __Pyx_PyLong_CompactValue(x) \
((Py_SIZE(x) == 0) ? (sdigit) 0 : ((Py_SIZE(x) < 0) ? -(sdigit)__Pyx_PyLong_Digits(x)[0] : (sdigit)__Pyx_PyLong_Digits(x)[0]))
typedef sdigit __Pyx_compact_pylong;
typedef digit __Pyx_compact_upylong;
#endif
#if PY_VERSION_HEX >= 0x030C00A5
#define __Pyx_PyLong_Digits(x) (((PyLongObject*)x)->long_value.ob_digit)
#else
#define __Pyx_PyLong_Digits(x) (((PyLongObject*)x)->ob_digit)
#endif
#endif
#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
#include <string.h>
static int __Pyx_sys_getdefaultencoding_not_ascii;
static int __Pyx_init_sys_getdefaultencoding_params(void) {
PyObject* sys;
PyObject* default_encoding = NULL;
PyObject* ascii_chars_u = NULL;
PyObject* ascii_chars_b = NULL;
const char* default_encoding_c;
sys = PyImport_ImportModule("sys");
if (!sys) goto bad;
default_encoding = PyObject_CallMethod(sys, (char*) "getdefaultencoding", NULL);
Py_DECREF(sys);
if (!default_encoding) goto bad;
default_encoding_c = PyBytes_AsString(default_encoding);
if (!default_encoding_c) goto bad;
if (strcmp(default_encoding_c, "ascii") == 0) {
__Pyx_sys_getdefaultencoding_not_ascii = 0;
} else {
char ascii_chars[128];
int c;
for (c = 0; c < 128; c++) {
ascii_chars[c] = (char) c;
}
__Pyx_sys_getdefaultencoding_not_ascii = 1;
ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL);
if (!ascii_chars_u) goto bad;
ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL);
if (!ascii_chars_b || !PyBytes_Check(ascii_chars_b) || memcmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) {
PyErr_Format(
PyExc_ValueError,
"This module compiled with c_string_encoding=ascii, but default encoding '%.200s' is not a superset of ascii.",
default_encoding_c);
goto bad;
}
Py_DECREF(ascii_chars_u);
Py_DECREF(ascii_chars_b);
}
Py_DECREF(default_encoding);
return 0;
bad:
Py_XDECREF(default_encoding);
Py_XDECREF(ascii_chars_u);
Py_XDECREF(ascii_chars_b);
return -1;
}
#endif
#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT && PY_MAJOR_VERSION >= 3
#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL)
#else
#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL)
// __PYX_DEFAULT_STRING_ENCODING is either a user provided string constant
// or we need to look it up here
#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
#include <string.h>
static char* __PYX_DEFAULT_STRING_ENCODING;
static int __Pyx_init_sys_getdefaultencoding_params(void) {
PyObject* sys;
PyObject* default_encoding = NULL;
char* default_encoding_c;
sys = PyImport_ImportModule("sys");
if (!sys) goto bad;
default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL);
Py_DECREF(sys);
if (!default_encoding) goto bad;
default_encoding_c = PyBytes_AsString(default_encoding);
if (!default_encoding_c) goto bad;
__PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c) + 1);
if (!__PYX_DEFAULT_STRING_ENCODING) goto bad;
strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c);
Py_DECREF(default_encoding);
return 0;
bad:
Py_XDECREF(default_encoding);
return -1;
}
#endif
#endif
/////////////// TypeConversions ///////////////
/* Type Conversion Functions */
#include <string.h>
static CYTHON_INLINE Py_ssize_t __Pyx_ssize_strlen(const char *s) {
size_t len = strlen(s);
if (unlikely(len > (size_t) PY_SSIZE_T_MAX)) {
PyErr_SetString(PyExc_OverflowError, "byte string is too long");
return -1;
}
return (Py_ssize_t) len;
}
static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char* c_str) {
Py_ssize_t len = __Pyx_ssize_strlen(c_str);
if (unlikely(len < 0)) return NULL;
return __Pyx_PyUnicode_FromStringAndSize(c_str, len);
}
static CYTHON_INLINE PyObject* __Pyx_PyByteArray_FromString(const char* c_str) {
Py_ssize_t len = __Pyx_ssize_strlen(c_str);
if (unlikely(len < 0)) return NULL;
return PyByteArray_FromStringAndSize(c_str, len);
}
// Py3.7 returns a "const char*" for unicode strings
static CYTHON_INLINE const char* __Pyx_PyObject_AsString(PyObject* o) {
Py_ssize_t ignore;
return __Pyx_PyObject_AsStringAndSize(o, &ignore);
}
#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
#if !CYTHON_PEP393_ENABLED
static const char* __Pyx_PyUnicode_AsStringAndSize(PyObject* o, Py_ssize_t *length) {
char* defenc_c;
// borrowed reference, cached internally in 'o' by CPython
PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL);
if (!defenc) return NULL;
defenc_c = PyBytes_AS_STRING(defenc);
#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
{
char* end = defenc_c + PyBytes_GET_SIZE(defenc);
char* c;
for (c = defenc_c; c < end; c++) {
if ((unsigned char) (*c) >= 128) {
// raise the error
PyUnicode_AsASCIIString(o);
return NULL;
}
}
}
#endif /*__PYX_DEFAULT_STRING_ENCODING_IS_ASCII*/
*length = PyBytes_GET_SIZE(defenc);
return defenc_c;
}
#else /* CYTHON_PEP393_ENABLED: */
static CYTHON_INLINE const char* __Pyx_PyUnicode_AsStringAndSize(PyObject* o, Py_ssize_t *length) {
if (unlikely(__Pyx_PyUnicode_READY(o) == -1)) return NULL;
#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
if (likely(PyUnicode_IS_ASCII(o))) {
// cached for the lifetime of the object
*length = PyUnicode_GET_LENGTH(o);
return PyUnicode_AsUTF8(o);
} else {
// raise the error
PyUnicode_AsASCIIString(o);
return NULL;
}
#else /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */
return PyUnicode_AsUTF8AndSize(o, length);
#endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */
}
#endif /* CYTHON_PEP393_ENABLED */
#endif
// Py3.7 returns a "const char*" for unicode strings
static CYTHON_INLINE const char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) {
#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT
if (
#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII
__Pyx_sys_getdefaultencoding_not_ascii &&
#endif
PyUnicode_Check(o)) {
return __Pyx_PyUnicode_AsStringAndSize(o, length);
} else
#endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT */
#if (!CYTHON_COMPILING_IN_PYPY && !CYTHON_COMPILING_IN_LIMITED_API) || (defined(PyByteArray_AS_STRING) && defined(PyByteArray_GET_SIZE))
if (PyByteArray_Check(o)) {
*length = PyByteArray_GET_SIZE(o);
return PyByteArray_AS_STRING(o);
} else
#endif
{
char* result;
int r = PyBytes_AsStringAndSize(o, &result, length);
if (unlikely(r < 0)) {
return NULL;
} else {
return result;
}
}
}
/* Note: __Pyx_PyObject_IsTrue is written to minimize branching. */
static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {
int is_true = x == Py_True;
if (is_true | (x == Py_False) | (x == Py_None)) return is_true;
else return PyObject_IsTrue(x);
}
static CYTHON_INLINE int __Pyx_PyObject_IsTrueAndDecref(PyObject* x) {
int retval;
if (unlikely(!x)) return -1;
retval = __Pyx_PyObject_IsTrue(x);
Py_DECREF(x);
return retval;
}
static PyObject* __Pyx_PyNumber_IntOrLongWrongResultType(PyObject* result, const char* type_name) {
__Pyx_TypeName result_type_name = __Pyx_PyType_GetName(Py_TYPE(result));
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check(result)) {
// CPython issue #17576: warn if 'result' not of exact type int.
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
"__int__ returned non-int (type " __Pyx_FMT_TYPENAME "). "
"The ability to return an instance of a strict subclass of int is deprecated, "
"and may be removed in a future version of Python.",
result_type_name)) {
__Pyx_DECREF_TypeName(result_type_name);
Py_DECREF(result);
return NULL;
}
__Pyx_DECREF_TypeName(result_type_name);
return result;
}
#endif
PyErr_Format(PyExc_TypeError,
"__%.4s__ returned non-%.4s (type " __Pyx_FMT_TYPENAME ")",
type_name, type_name, result_type_name);
__Pyx_DECREF_TypeName(result_type_name);
Py_DECREF(result);
return NULL;
}
static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x) {
#if CYTHON_USE_TYPE_SLOTS
PyNumberMethods *m;
#endif
const char *name = NULL;
PyObject *res = NULL;
#if PY_MAJOR_VERSION < 3
if (likely(PyInt_Check(x) || PyLong_Check(x)))
#else
if (likely(PyLong_Check(x)))
#endif
return __Pyx_NewRef(x);
#if CYTHON_USE_TYPE_SLOTS
m = Py_TYPE(x)->tp_as_number;
#if PY_MAJOR_VERSION < 3
if (m && m->nb_int) {
name = "int";
res = m->nb_int(x);
}
else if (m && m->nb_long) {
name = "long";
res = m->nb_long(x);
}
#else
if (likely(m && m->nb_int)) {
name = "int";
res = m->nb_int(x);
}
#endif
#else
if (!PyBytes_CheckExact(x) && !PyUnicode_CheckExact(x)) {
res = PyNumber_Int(x);
}
#endif
if (likely(res)) {
#if PY_MAJOR_VERSION < 3
if (unlikely(!PyInt_Check(res) && !PyLong_Check(res))) {
#else
if (unlikely(!PyLong_CheckExact(res))) {
#endif
return __Pyx_PyNumber_IntOrLongWrongResultType(res, name);
}
}
else if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,
"an integer is required");
}
return res;
}
{{py: from Cython.Utility import pylong_join }}
static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {
Py_ssize_t ival;
PyObject *x;
#if PY_MAJOR_VERSION < 3
if (likely(PyInt_CheckExact(b))) {
if (sizeof(Py_ssize_t) >= sizeof(long))
return PyInt_AS_LONG(b);
else
return PyInt_AsSsize_t(b);
}
#endif
if (likely(PyLong_CheckExact(b))) {
#if CYTHON_USE_PYLONG_INTERNALS
// handle most common case first to avoid indirect branch and optimise branch prediction
if (likely(__Pyx_PyLong_IsCompact(b))) {
return __Pyx_PyLong_CompactValue(b);
} else {
const digit* digits = __Pyx_PyLong_Digits(b);
const Py_ssize_t size = __Pyx_PyLong_SignedDigitCount(b);
switch (size) {
{{for _size in (2, 3, 4)}}
{{for _case in (_size, -_size)}}
case {{_case}}:
if (8 * sizeof(Py_ssize_t) > {{_size}} * PyLong_SHIFT) {
return {{'-' if _case < 0 else ''}}(Py_ssize_t) {{pylong_join(_size, 'digits', 'size_t')}};
}
break;
{{endfor}}
{{endfor}}
}
}
#endif
return PyLong_AsSsize_t(b);
}
x = PyNumber_Index(b);
if (!x) return -1;
ival = PyInt_AsSsize_t(x);
Py_DECREF(x);
return ival;
}
static CYTHON_INLINE Py_hash_t __Pyx_PyIndex_AsHash_t(PyObject* o) {
if (sizeof(Py_hash_t) == sizeof(Py_ssize_t)) {
return (Py_hash_t) __Pyx_PyIndex_AsSsize_t(o);
#if PY_MAJOR_VERSION < 3
} else if (likely(PyInt_CheckExact(o))) {
return PyInt_AS_LONG(o);
#endif
} else {
Py_ssize_t ival;
PyObject *x;
x = PyNumber_Index(o);
if (!x) return -1;
ival = PyInt_AsLong(x);
Py_DECREF(x);
return ival;
}
}
static CYTHON_INLINE PyObject * __Pyx_PyBool_FromLong(long b) {
return b ? __Pyx_NewRef(Py_True) : __Pyx_NewRef(Py_False);
}
static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {
return PyInt_FromSize_t(ival);
}
/////////////// pynumber_float.proto ///////////////
static CYTHON_INLINE PyObject* __Pyx__PyNumber_Float(PyObject* obj); /* proto */
#define __Pyx_PyNumber_Float(x) (PyFloat_CheckExact(x) ? __Pyx_NewRef(x) : __Pyx__PyNumber_Float(x))
/////////////// pynumber_float ///////////////
//@requires: Optimize.c::pybytes_as_double
//@requires: Optimize.c::pyunicode_as_double
static CYTHON_INLINE PyObject* __Pyx__PyNumber_Float(PyObject* obj) {
// 'obj is PyFloat' is handled in the calling macro
double val;
if (PyLong_CheckExact(obj)) {
#if CYTHON_USE_PYLONG_INTERNALS
if (likely(__Pyx_PyLong_IsCompact(obj))) {
val = (double) __Pyx_PyLong_CompactValue(obj);
goto no_error;
}
#endif
val = PyLong_AsDouble(obj);
} else if (PyUnicode_CheckExact(obj)) {
val = __Pyx_PyUnicode_AsDouble(obj);
} else if (PyBytes_CheckExact(obj)) {
val = __Pyx_PyBytes_AsDouble(obj);
} else if (PyByteArray_CheckExact(obj)) {
val = __Pyx_PyByteArray_AsDouble(obj);
} else {
return PyNumber_Float(obj);
}
if (unlikely(val == -1 && PyErr_Occurred())) {
return NULL;
}
#if CYTHON_USE_PYLONG_INTERNALS
no_error:
#endif
return PyFloat_FromDouble(val);
}
/////////////// GCCDiagnostics.proto ///////////////
// GCC diagnostic pragmas were introduced in GCC 4.6
// Used to silence conversion warnings that are ok but cannot be avoided.
#if !defined(__INTEL_COMPILER) && defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#define __Pyx_HAS_GCC_DIAGNOSTIC
#endif
/////////////// ToPyCTupleUtility.proto ///////////////
static PyObject* {{funcname}}({{struct_type_decl}});
/////////////// ToPyCTupleUtility ///////////////
static PyObject* {{funcname}}({{struct_type_decl}} value) {
PyObject* item = NULL;
PyObject* result = PyTuple_New({{size}});
if (!result) goto bad;
{{for ix, component in enumerate(components):}}
{{py:attr = "value.f%s" % ix}}
item = {{component.to_py_function}}({{attr}});
if (!item) goto bad;
PyTuple_SET_ITEM(result, {{ix}}, item);
{{endfor}}
return result;
bad:
Py_XDECREF(item);
Py_XDECREF(result);
return NULL;
}
/////////////// FromPyCTupleUtility.proto ///////////////
static CYTHON_INLINE {{struct_type_decl}} {{funcname}}(PyObject *);
/////////////// FromPyCTupleUtility ///////////////
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
static void __Pyx_tuple_{{funcname}}(PyObject * o, {{struct_type_decl}} *result) {
{{for ix, component in enumerate(components):}}
{{py:attr = "result->f%s" % ix}}
{{attr}} = {{component.from_py_function}}(PyTuple_GET_ITEM(o, {{ix}}));
if ({{component.error_condition(attr)}}) goto bad;
{{endfor}}
return;
bad:
return;
}
static void __Pyx_list_{{funcname}}(PyObject * o, {{struct_type_decl}} *result) {
{{for ix, component in enumerate(components):}}
{{py:attr = "result->f%s" % ix}}
{{attr}} = {{component.from_py_function}}(PyList_GET_ITEM(o, {{ix}}));
if ({{component.error_condition(attr)}}) goto bad;
{{endfor}}
return;
bad:
return;
}
#endif
static void __Pyx_seq_{{funcname}}(PyObject * o, {{struct_type_decl}} *result) {
if (unlikely(!PySequence_Check(o))) {
__Pyx_TypeName o_type_name = __Pyx_PyType_GetName(Py_TYPE(o));
PyErr_Format(PyExc_TypeError,
"Expected a sequence of size %zd, got " __Pyx_FMT_TYPENAME, (Py_ssize_t) {{size}}, o_type_name);
__Pyx_DECREF_TypeName(o_type_name);
goto bad;
} else if (unlikely(PySequence_Length(o) != {{size}})) {
PyErr_Format(PyExc_TypeError,
"Expected a sequence of size %zd, got size %zd", (Py_ssize_t) {{size}}, PySequence_Length(o));
goto bad;
}
{
PyObject *item;
{{for ix, component in enumerate(components):}}
{{py:attr = "result->f%s" % ix}}
item = PySequence_ITEM(o, {{ix}}); if (unlikely(!item)) goto bad;
{{attr}} = {{component.from_py_function}}(item);
Py_DECREF(item);
if ({{component.error_condition(attr)}}) goto bad;
{{endfor}}
}
return;
bad:
return;
}
static CYTHON_INLINE {{struct_type_decl}} {{funcname}}(PyObject * o) {
{{struct_type_decl}} result;
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
if (likely(PyTuple_Check(o) && PyTuple_GET_SIZE(o) == {{size}})) {
__Pyx_tuple_{{funcname}}(o, &result);
} else if (likely(PyList_Check(o) && PyList_GET_SIZE(o) == {{size}})) {
__Pyx_list_{{funcname}}(o, &result);
} else
#endif
{
__Pyx_seq_{{funcname}}(o, &result);
}
return result;
}
/////////////// UnicodeAsUCS4.proto ///////////////
static CYTHON_INLINE Py_UCS4 __Pyx_PyUnicode_AsPy_UCS4(PyObject*);
/////////////// UnicodeAsUCS4 ///////////////
static CYTHON_INLINE Py_UCS4 __Pyx_PyUnicode_AsPy_UCS4(PyObject* x) {
Py_ssize_t length;
#if CYTHON_PEP393_ENABLED
length = PyUnicode_GET_LENGTH(x);
if (likely(length == 1)) {
return PyUnicode_READ_CHAR(x, 0);
}
#else
length = PyUnicode_GET_SIZE(x);
if (likely(length == 1)) {
return PyUnicode_AS_UNICODE(x)[0];
}
#if Py_UNICODE_SIZE == 2
else if (PyUnicode_GET_SIZE(x) == 2) {
Py_UCS4 high_val = PyUnicode_AS_UNICODE(x)[0];
if (high_val >= 0xD800 && high_val <= 0xDBFF) {
Py_UCS4 low_val = PyUnicode_AS_UNICODE(x)[1];
if (low_val >= 0xDC00 && low_val <= 0xDFFF) {
return 0x10000 + (((high_val & ((1<<10)-1)) << 10) | (low_val & ((1<<10)-1)));
}
}
}
#endif
#endif
PyErr_Format(PyExc_ValueError,
"only single character unicode strings can be converted to Py_UCS4, "
"got length %" CYTHON_FORMAT_SSIZE_T "d", length);
return (Py_UCS4)-1;
}
/////////////// ObjectAsUCS4.proto ///////////////
//@requires: UnicodeAsUCS4
#define __Pyx_PyObject_AsPy_UCS4(x) \
(likely(PyUnicode_Check(x)) ? __Pyx_PyUnicode_AsPy_UCS4(x) : __Pyx__PyObject_AsPy_UCS4(x))
static Py_UCS4 __Pyx__PyObject_AsPy_UCS4(PyObject*);
/////////////// ObjectAsUCS4 ///////////////
static Py_UCS4 __Pyx__PyObject_AsPy_UCS4_raise_error(long ival) {
if (ival < 0) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_OverflowError,
"cannot convert negative value to Py_UCS4");
} else {
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to Py_UCS4");
}
return (Py_UCS4)-1;
}
static Py_UCS4 __Pyx__PyObject_AsPy_UCS4(PyObject* x) {
long ival;
ival = __Pyx_PyInt_As_long(x);
if (unlikely(!__Pyx_is_valid_index(ival, 1114111 + 1))) {
return __Pyx__PyObject_AsPy_UCS4_raise_error(ival);
}
return (Py_UCS4)ival;
}
/////////////// ObjectAsPyUnicode.proto ///////////////
static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject*);
/////////////// ObjectAsPyUnicode ///////////////
static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject* x) {
long ival;
#if CYTHON_PEP393_ENABLED
#if Py_UNICODE_SIZE > 2
const long maxval = 1114111;
#else
const long maxval = 65535;
#endif
#else
static long maxval = 0;
#endif
if (PyUnicode_Check(x)) {
if (unlikely(__Pyx_PyUnicode_GET_LENGTH(x) != 1)) {
PyErr_Format(PyExc_ValueError,
"only single character unicode strings can be converted to Py_UNICODE, "
"got length %" CYTHON_FORMAT_SSIZE_T "d", __Pyx_PyUnicode_GET_LENGTH(x));
return (Py_UNICODE)-1;
}
#if CYTHON_PEP393_ENABLED
ival = PyUnicode_READ_CHAR(x, 0);
#else
return PyUnicode_AS_UNICODE(x)[0];
#endif
} else {
#if !CYTHON_PEP393_ENABLED
if (unlikely(!maxval))
maxval = (long)PyUnicode_GetMax();
#endif
ival = __Pyx_PyInt_As_long(x);
}
if (unlikely(!__Pyx_is_valid_index(ival, maxval + 1))) {
if (ival < 0) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_OverflowError,
"cannot convert negative value to Py_UNICODE");
return (Py_UNICODE)-1;
} else {
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to Py_UNICODE");
}
return (Py_UNICODE)-1;
}
return (Py_UNICODE)ival;
}
/////////////// CIntToPy.proto ///////////////
static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value);
/////////////// CIntToPy ///////////////
//@requires: GCCDiagnostics
static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) {
#ifdef __Pyx_HAS_GCC_DIAGNOSTIC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = ({{TYPE}}) 0;
#ifdef __Pyx_HAS_GCC_DIAGNOSTIC
#pragma GCC diagnostic pop
#endif
const int is_unsigned = neg_one > const_zero;
if (is_unsigned) {
if (sizeof({{TYPE}}) < sizeof(long)) {
return PyInt_FromLong((long) value);
} else if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
return PyLong_FromUnsignedLong((unsigned long) value);
#ifdef HAVE_LONG_LONG
} else if (sizeof({{TYPE}}) <= sizeof(unsigned PY_LONG_LONG)) {
return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG) value);
#endif
}
} else {
if (sizeof({{TYPE}}) <= sizeof(long)) {
return PyInt_FromLong((long) value);
#ifdef HAVE_LONG_LONG
} else if (sizeof({{TYPE}}) <= sizeof(PY_LONG_LONG)) {
return PyLong_FromLongLong((PY_LONG_LONG) value);
#endif
}
}
{
unsigned char *bytes = (unsigned char *)&value;
#if !CYTHON_COMPILING_IN_LIMITED_API && PY_VERSION_HEX >= 0x030d00A4
if (is_unsigned) {
return PyLong_FromUnsignedNativeBytes(bytes, sizeof(value), -1);
} else {
return PyLong_FromNativeBytes(bytes, sizeof(value), -1);
}
#elif !CYTHON_COMPILING_IN_LIMITED_API && PY_VERSION_HEX < 0x030d0000
int one = 1; int little = (int)*(unsigned char *)&one;
return _PyLong_FromByteArray(bytes, sizeof({{TYPE}}),
little, !is_unsigned);
#else
// call int.from_bytes()
int one = 1; int little = (int)*(unsigned char *)&one;
PyObject *from_bytes, *result = NULL;
PyObject *py_bytes = NULL, *arg_tuple = NULL, *kwds = NULL, *order_str = NULL;
from_bytes = PyObject_GetAttrString((PyObject*)&PyLong_Type, "from_bytes");
if (!from_bytes) return NULL;
py_bytes = PyBytes_FromStringAndSize((char*)bytes, sizeof({{TYPE}}));
if (!py_bytes) goto limited_bad;
// I'm deliberately not using PYIDENT here because this code path is very unlikely
// to ever run so it seems a pessimization mostly.
order_str = PyUnicode_FromString(little ? "little" : "big");
if (!order_str) goto limited_bad;
arg_tuple = PyTuple_Pack(2, py_bytes, order_str);
if (!arg_tuple) goto limited_bad;
if (!is_unsigned) {
// default is signed=False
kwds = PyDict_New();
if (!kwds) goto limited_bad;
if (PyDict_SetItemString(kwds, "signed", __Pyx_NewRef(Py_True))) goto limited_bad;
}
result = PyObject_Call(from_bytes, arg_tuple, kwds);
limited_bad:
Py_XDECREF(kwds);
Py_XDECREF(arg_tuple);
Py_XDECREF(order_str);
Py_XDECREF(py_bytes);
Py_XDECREF(from_bytes);
return result;
#endif
}
}
/////////////// CIntToDigits ///////////////
static const char DIGIT_PAIRS_10[2*10*10+1] = {
"00010203040506070809"
"10111213141516171819"
"20212223242526272829"
"30313233343536373839"
"40414243444546474849"
"50515253545556575859"
"60616263646566676869"
"70717273747576777879"
"80818283848586878889"
"90919293949596979899"
};
static const char DIGIT_PAIRS_8[2*8*8+1] = {
"0001020304050607"
"1011121314151617"
"2021222324252627"
"3031323334353637"
"4041424344454647"
"5051525354555657"
"6061626364656667"
"7071727374757677"
};
static const char DIGITS_HEX[2*16+1] = {
"0123456789abcdef"
"0123456789ABCDEF"
};
/////////////// CIntToPyUnicode.proto ///////////////
static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t width, char padding_char, char format_char);
/////////////// CIntToPyUnicode ///////////////
//@requires: StringTools.c::IncludeStringH
//@requires: StringTools.c::BuildPyUnicode
//@requires: CIntToDigits
//@requires: GCCDiagnostics
// NOTE: inlining because most arguments are constant, which collapses lots of code below
static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t width, char padding_char, char format_char) {
// simple and conservative C string allocation on the stack: each byte gives at most 3 digits, plus sign
char digits[sizeof({{TYPE}})*3+2];
// 'dpos' points to end of digits array + 1 initially to allow for pre-decrement looping
char *dpos, *end = digits + sizeof({{TYPE}})*3+2;
const char *hex_digits = DIGITS_HEX;
Py_ssize_t length, ulength;
int prepend_sign, last_one_off;
{{TYPE}} remaining;
#ifdef __Pyx_HAS_GCC_DIAGNOSTIC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = ({{TYPE}}) 0;
#ifdef __Pyx_HAS_GCC_DIAGNOSTIC
#pragma GCC diagnostic pop
#endif
const int is_unsigned = neg_one > const_zero;
if (format_char == 'X') {
hex_digits += 16;
format_char = 'x';
}
// surprise: even trivial sprintf() calls don't get optimised in gcc (4.8)
remaining = value; /* not using abs(value) to avoid overflow problems */
last_one_off = 0;
dpos = end;
do {
int digit_pos;
switch (format_char) {
case 'o':
digit_pos = abs((int)(remaining % (8*8)));
remaining = ({{TYPE}}) (remaining / (8*8));
dpos -= 2;
memcpy(dpos, DIGIT_PAIRS_8 + digit_pos * 2, 2); /* copy 2 digits at a time, unaligned */
last_one_off = (digit_pos < 8);
break;
case 'd':
digit_pos = abs((int)(remaining % (10*10)));
remaining = ({{TYPE}}) (remaining / (10*10));
dpos -= 2;
memcpy(dpos, DIGIT_PAIRS_10 + digit_pos * 2, 2); /* copy 2 digits at a time, unaligned */
last_one_off = (digit_pos < 10);
break;
case 'x':
*(--dpos) = hex_digits[abs((int)(remaining % 16))];
remaining = ({{TYPE}}) (remaining / 16);
break;
default:
assert(0);
break;
}
} while (unlikely(remaining != 0));
// Correct dpos by 1 if we read an excess digit.
assert(!last_one_off || *dpos == '0');
dpos += last_one_off;
length = end - dpos;
ulength = length;
prepend_sign = 0;
if (!is_unsigned && value <= neg_one) {
if (padding_char == ' ' || width <= length + 1) {
*(--dpos) = '-';
++length;
} else {
prepend_sign = 1;
}
++ulength;
}
if (width > ulength) {
ulength = width;
}
// single character unicode strings are cached in CPython => use PyUnicode_FromOrdinal() for them
if (ulength == 1) {
return PyUnicode_FromOrdinal(*dpos);
}
return __Pyx_PyUnicode_BuildFromAscii(ulength, dpos, (int) length, prepend_sign, padding_char);
}
/////////////// CBIntToPyUnicode.proto ///////////////
#define {{TO_PY_FUNCTION}}(value) \
((value) ? __Pyx_NewRef({{TRUE_CONST}}) : __Pyx_NewRef({{FALSE_CONST}}))
/////////////// PyIntFromDouble.proto ///////////////
#if PY_MAJOR_VERSION < 3
static CYTHON_INLINE PyObject* __Pyx_PyInt_FromDouble(double value);
#else
#define __Pyx_PyInt_FromDouble(value) PyLong_FromDouble(value)
#endif
/////////////// PyIntFromDouble ///////////////
#if PY_MAJOR_VERSION < 3
static CYTHON_INLINE PyObject* __Pyx_PyInt_FromDouble(double value) {
if (value >= (double)LONG_MIN && value <= (double)LONG_MAX) {
return PyInt_FromLong((long)value);
}
return PyLong_FromDouble(value);
}
#endif
/////////////// CIntFromPyVerify ///////////////
// see CIntFromPy
#define __PYX_VERIFY_RETURN_INT(target_type, func_type, func_value) \
__PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, 0)
#define __PYX_VERIFY_RETURN_INT_EXC(target_type, func_type, func_value) \
__PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, 1)
#define __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, exc) \
{ \
func_type value = func_value; \
if (sizeof(target_type) < sizeof(func_type)) { \
if (unlikely(value != (func_type) (target_type) value)) { \
func_type zero = 0; \
if (exc && unlikely(value == (func_type)-1 && PyErr_Occurred())) \
return (target_type) -1; \
if (is_unsigned && unlikely(value < zero)) \
goto raise_neg_overflow; \
else \
goto raise_overflow; \
} \
} \
return (target_type) value; \
}
/////////////// CIntFromPy.proto ///////////////
static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *);
/////////////// CIntFromPy ///////////////
//@requires: CIntFromPyVerify
//@requires: GCCDiagnostics
{{py: from Cython.Utility import pylong_join }}
static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
#ifdef __Pyx_HAS_GCC_DIAGNOSTIC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = ({{TYPE}}) 0;
#ifdef __Pyx_HAS_GCC_DIAGNOSTIC
#pragma GCC diagnostic pop
#endif
const int is_unsigned = neg_one > const_zero;
#if PY_MAJOR_VERSION < 3
if (likely(PyInt_Check(x))) {
if ((sizeof({{TYPE}}) < sizeof(long))) {
__PYX_VERIFY_RETURN_INT({{TYPE}}, long, PyInt_AS_LONG(x))
} else {
long val = PyInt_AS_LONG(x);
if (is_unsigned && unlikely(val < 0)) {
goto raise_neg_overflow;
}
return ({{TYPE}}) val;
}
}
#endif
if (unlikely(!PyLong_Check(x))) {
{{TYPE}} val;
PyObject *tmp = __Pyx_PyNumber_IntOrLong(x);
if (!tmp) return ({{TYPE}}) -1;
val = {{FROM_PY_FUNCTION}}(tmp);
Py_DECREF(tmp);
return val;
}
if (is_unsigned) {
#if CYTHON_USE_PYLONG_INTERNALS
if (unlikely(__Pyx_PyLong_IsNeg(x))) {
goto raise_neg_overflow;
//} else if (__Pyx_PyLong_IsZero(x)) {
// return ({{TYPE}}) 0;
} else if (__Pyx_PyLong_IsCompact(x)) {
__PYX_VERIFY_RETURN_INT({{TYPE}}, __Pyx_compact_upylong, __Pyx_PyLong_CompactValueUnsigned(x))
} else {
const digit* digits = __Pyx_PyLong_Digits(x);
assert(__Pyx_PyLong_DigitCount(x) > 1);
switch (__Pyx_PyLong_DigitCount(x)) {
{{for _size in (2, 3, 4)}}
case {{_size}}:
if ((8 * sizeof({{TYPE}}) > {{_size-1}} * PyLong_SHIFT)) {
if ((8 * sizeof(unsigned long) > {{_size}} * PyLong_SHIFT)) {
__PYX_VERIFY_RETURN_INT({{TYPE}}, unsigned long, {{pylong_join(_size, 'digits')}})
} else if ((8 * sizeof({{TYPE}}) >= {{_size}} * PyLong_SHIFT)) {
return ({{TYPE}}) {{pylong_join(_size, 'digits', TYPE)}};
}
}
break;
{{endfor}}
}
}
#endif
#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX < 0x030C00A7
if (unlikely(Py_SIZE(x) < 0)) {
goto raise_neg_overflow;
}
#else
{
// misuse Py_False as a quick way to compare to a '0' int object in PyPy
int result = PyObject_RichCompareBool(x, Py_False, Py_LT);
if (unlikely(result < 0))
return ({{TYPE}}) -1;
if (unlikely(result == 1))
goto raise_neg_overflow;
}
#endif
if ((sizeof({{TYPE}}) <= sizeof(unsigned long))) {
__PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, unsigned long, PyLong_AsUnsignedLong(x))
#ifdef HAVE_LONG_LONG
} else if ((sizeof({{TYPE}}) <= sizeof(unsigned PY_LONG_LONG))) {
__PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x))
#endif
}
} else {
// signed
#if CYTHON_USE_PYLONG_INTERNALS
if (__Pyx_PyLong_IsCompact(x)) {
__PYX_VERIFY_RETURN_INT({{TYPE}}, __Pyx_compact_pylong, __Pyx_PyLong_CompactValue(x))
} else {
const digit* digits = __Pyx_PyLong_Digits(x);
assert(__Pyx_PyLong_DigitCount(x) > 1);
switch (__Pyx_PyLong_SignedDigitCount(x)) {
{{for _size in (2, 3, 4)}}
{{for _case in (-_size, _size)}}
case {{_case}}:
if ((8 * sizeof({{TYPE}}){{' - 1' if _case < 0 else ''}} > {{_size-1}} * PyLong_SHIFT)) {
if ((8 * sizeof(unsigned long) > {{_size}} * PyLong_SHIFT)) {
__PYX_VERIFY_RETURN_INT({{TYPE}}, {{'long' if _case < 0 else 'unsigned long'}}, {{'-(long) ' if _case < 0 else ''}}{{pylong_join(_size, 'digits')}})
} else if ((8 * sizeof({{TYPE}}) - 1 > {{_size}} * PyLong_SHIFT)) {
return ({{TYPE}}) ({{'((%s)-1)*' % TYPE if _case < 0 else ''}}{{pylong_join(_size, 'digits', TYPE)}});
}
}
break;
{{endfor}}
{{endfor}}
}
}
#endif
if ((sizeof({{TYPE}}) <= sizeof(long))) {
__PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, long, PyLong_AsLong(x))
#ifdef HAVE_LONG_LONG
} else if ((sizeof({{TYPE}}) <= sizeof(PY_LONG_LONG))) {
__PYX_VERIFY_RETURN_INT_EXC({{TYPE}}, PY_LONG_LONG, PyLong_AsLongLong(x))
#endif
}
}
// large integer type and no access to PyLong internals => allow for a more expensive conversion
{
{{TYPE}} val;
int ret = -1;
#if PY_VERSION_HEX >= 0x030d00A6 && !CYTHON_COMPILING_IN_LIMITED_API
Py_ssize_t bytes_copied = PyLong_AsNativeBytes(
x, &val, sizeof(val), Py_ASNATIVEBYTES_NATIVE_ENDIAN | (is_unsigned ? Py_ASNATIVEBYTES_UNSIGNED_BUFFER | Py_ASNATIVEBYTES_REJECT_NEGATIVE : 0));
if (unlikely(bytes_copied == -1)) {
// failed
} else if (unlikely(bytes_copied > (Py_ssize_t) sizeof(val))) {
goto raise_overflow;
} else {
ret = 0;
}
#elif PY_VERSION_HEX < 0x030d0000 && !(CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_LIMITED_API) || defined(_PyLong_AsByteArray)
int one = 1; int is_little = (int)*(unsigned char *)&one;
unsigned char *bytes = (unsigned char *)&val;
ret = _PyLong_AsByteArray((PyLongObject *)x,
bytes, sizeof(val),
is_little, !is_unsigned);
#else
{{if IS_ENUM}}
// The fallback implementation uses math operations like shifting, which do not work well with enums.
PyErr_SetString(PyExc_RuntimeError,
"_PyLong_AsByteArray() or PyLong_AsNativeBytes() not available, cannot convert large enums");
val = ({{TYPE}}) -1;
{{else}}
// Inefficient copy of bit chunks through the C-API. Probably still better than a "cannot do this" exception.
// This is substantially faster in CPython (>30%) than calling "int.to_bytes()" through the C-API.
PyObject *v;
PyObject *stepval = NULL, *mask = NULL, *shift = NULL;
int bits, remaining_bits, is_negative = 0;
int chunk_size = (sizeof(long) < 8) ? 30 : 62;
// Use exact PyLong to prevent user defined &&/<</etc. implementations (and make Py_SIZE() work below).
if (likely(PyLong_CheckExact(x))) {
v = __Pyx_NewRef(x);
} else {
v = PyNumber_Long(x);
if (unlikely(!v)) return ({{TYPE}}) -1;
assert(PyLong_CheckExact(v));
}
// Misuse Py_False as a quick way to compare to a '0' int object.
{
int result = PyObject_RichCompareBool(v, Py_False, Py_LT);
if (unlikely(result < 0)) {
Py_DECREF(v);
return ({{TYPE}}) -1;
}
is_negative = result == 1;
}
if (is_unsigned && unlikely(is_negative)) {
Py_DECREF(v);
goto raise_neg_overflow;
} else if (is_negative) {
// bit-invert to make sure we can safely convert it
stepval = PyNumber_Invert(v);
Py_DECREF(v);
if (unlikely(!stepval))
return ({{TYPE}}) -1;
} else {
stepval = v;
}
v = NULL;
// Unpack full chunks of bits.
val = ({{TYPE}}) 0;
mask = PyLong_FromLong((1L << chunk_size) - 1); if (unlikely(!mask)) goto done;
shift = PyLong_FromLong(chunk_size); if (unlikely(!shift)) goto done;
for (bits = 0; bits < (int) sizeof({{TYPE}}) * 8 - chunk_size; bits += chunk_size) {
PyObject *tmp, *digit;
long idigit;
digit = PyNumber_And(stepval, mask);
if (unlikely(!digit)) goto done;
idigit = PyLong_AsLong(digit);
Py_DECREF(digit);
if (unlikely(idigit < 0)) goto done;
val |= (({{TYPE}}) idigit) << bits;
tmp = PyNumber_Rshift(stepval, shift);
if (unlikely(!tmp)) goto done;
Py_DECREF(stepval); stepval = tmp;
}
Py_DECREF(shift); shift = NULL;
Py_DECREF(mask); mask = NULL;
// Add the last bits and detect overflow.
{
long idigit = PyLong_AsLong(stepval);
if (unlikely(idigit < 0)) goto done;
remaining_bits = ((int) sizeof({{TYPE}}) * 8) - bits - (is_unsigned ? 0 : 1);
if (unlikely(idigit >= (1L << remaining_bits)))
goto raise_overflow;
val |= (({{TYPE}}) idigit) << bits;
}
// Handle sign and overflow into sign bit.
if (!is_unsigned) {
// gcc warns about unsigned (val < 0) => test sign bit instead
if (unlikely(val & ((({{TYPE}}) 1) << (sizeof({{TYPE}}) * 8 - 1))))
goto raise_overflow;
// undo the PyNumber_Invert() above
if (is_negative)
val = ~val;
}
ret = 0;
done:
Py_XDECREF(shift);
Py_XDECREF(mask);
Py_XDECREF(stepval);
{{endif}}
#endif
if (unlikely(ret))
return ({{TYPE}}) -1;
return val;
}
raise_overflow:
PyErr_SetString(PyExc_OverflowError,
"value too large to convert to {{TYPE}}");
return ({{TYPE}}) -1;
raise_neg_overflow:
PyErr_SetString(PyExc_OverflowError,
"can't convert negative value to {{TYPE}}");
return ({{TYPE}}) -1;
}
|