GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cpython/Modules/_struct.c at 3.14 · python/cpython Expand file tree/ Copy path_struct.cMore file actions 2848 lines (2506 loc) · 78.7 KB / Copy path_struct.cFile metadata and controls2848 lines (2506 loc) · 78.7 KB 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 /* struct module -- pack values into and (out of) bytes objects */ /* New version supporting byte order, alignment and size options, byte strings, and unsigned numbers */ #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif #include "Python.h" #include "pycore_bytesobject.h" // _PyBytesWriter #include "pycore_lock.h" // _PyOnceFlag_CallOnce() #include "pycore_long.h" // _PyLong_AsByteArray() #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() #include <stddef.h> // offsetof() /*[clinic input] class Struct "PyStructObject *" "&PyStructType" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9b032058a83ed7c3]*/ typedef struct { PyObject *cache; PyObject *PyStructType; PyObject *unpackiter_type; PyObject *StructError; } _structmodulestate; static inline _structmodulestate* get_struct_state(PyObject *module) { void *state = _PyModule_GetState(module); assert(state != NULL); return (_structmodulestate *)state; } static struct PyModuleDef _structmodule; #define get_struct_state_structinst(self) \ (get_struct_state(PyType_GetModuleByDef(Py_TYPE(self), &_structmodule))) #define get_struct_state_iterinst(self) \ (get_struct_state(PyType_GetModule(Py_TYPE(self)))) /* The translation function for each format character is table driven */ typedef struct _formatdef { char format; Py_ssize_t size; Py_ssize_t alignment; PyObject* (*unpack)(_structmodulestate *, const char *, const struct _formatdef *); int (*pack)(_structmodulestate *, char *, PyObject *, const struct _formatdef *); } formatdef; typedef struct _formatcode { const struct _formatdef *fmtdef; Py_ssize_t offset; Py_ssize_t size; Py_ssize_t repeat; } formatcode; /* Struct object interface */ typedef struct { PyObject_HEAD Py_ssize_t s_size; Py_ssize_t s_len; formatcode *s_codes; PyObject *s_format; PyObject *weakreflist; /* List of weak references */ } PyStructObject; #define PyStructObject_CAST(op) ((PyStructObject *)(op)) #define PyStruct_Check(op, state) PyObject_TypeCheck(op, (PyTypeObject *)(state)->PyStructType) #ifdef __powerc #pragma options align=reset #endif /*[python input] class cache_struct_converter(CConverter): type = 'PyStructObject *' converter = 'cache_struct_converter' c_default = "NULL" broken_limited_capi = True def parse_arg(self, argname, displayname, *, limited_capi): assert not limited_capi return self.format_code(""" if (!{converter}(module, {argname}, &{paramname})) {{{{ goto exit; }}}} """, argname=argname, converter=self.converter) def cleanup(self): return "Py_XDECREF(%s);\n" % self.name [python start generated code]*/ /*[python end generated code: output=da39a3ee5e6b4b0d input=c33b27d6b06006c6]*/ static int cache_struct_converter(PyObject *, PyObject *, PyStructObject **); #include "clinic/_struct.c.h" /* Helper for integer format codes: converts an arbitrary Python object to a PyLongObject if possible, otherwise fails. Caller should decref. */ static PyObject * get_pylong(_structmodulestate *state, PyObject *v) { assert(v != NULL); if (!PyLong_Check(v)) { /* Not an integer; try to use __index__ to convert. */ if (PyIndex_Check(v)) { v = _PyNumber_Index(v); if (v == NULL) return NULL; } else { PyErr_SetString(state->StructError, "required argument is not an integer"); return NULL; } } else Py_INCREF(v); assert(PyLong_Check(v)); return v; } /* Helper routine to get a C long and raise the appropriate error if it isn't one */ static int get_long(_structmodulestate *state, PyObject *v, long *p) { long x; v = get_pylong(state, v); if (v == NULL) return -1; assert(PyLong_Check(v)); x = PyLong_AsLong(v); Py_DECREF(v); if (x == (long)-1 && PyErr_Occurred()) { return -1; } *p = x; return 0; } /* Same, but handling unsigned long */ static int get_ulong(_structmodulestate *state, PyObject *v, unsigned long *p) { unsigned long x; v = get_pylong(state, v); if (v == NULL) return -1; assert(PyLong_Check(v)); x = PyLong_AsUnsignedLong(v); Py_DECREF(v); if (x == (unsigned long)-1 && PyErr_Occurred()) { return -1; } *p = x; return 0; } /* Same, but handling native long long. */ static int get_longlong(_structmodulestate *state, PyObject *v, long long *p) { long long x; v = get_pylong(state, v); if (v == NULL) return -1; assert(PyLong_Check(v)); x = PyLong_AsLongLong(v); Py_DECREF(v); if (x == (long long)-1 && PyErr_Occurred()) { return -1; } *p = x; return 0; } /* Same, but handling native unsigned long long. */ static int get_ulonglong(_structmodulestate *state, PyObject *v, unsigned long long *p) { unsigned long long x; v = get_pylong(state, v); if (v == NULL) return -1; assert(PyLong_Check(v)); x = PyLong_AsUnsignedLongLong(v); Py_DECREF(v); if (x == (unsigned long long)-1 && PyErr_Occurred()) { return -1; } *p = x; return 0; } /* Same, but handling Py_ssize_t */ static int get_ssize_t(_structmodulestate *state, PyObject *v, Py_ssize_t *p) { Py_ssize_t x; v = get_pylong(state, v); if (v == NULL) return -1; assert(PyLong_Check(v)); x = PyLong_AsSsize_t(v); Py_DECREF(v); if (x == (Py_ssize_t)-1 && PyErr_Occurred()) { return -1; } *p = x; return 0; } /* Same, but handling size_t */ static int get_size_t(_structmodulestate *state, PyObject *v, size_t *p) { size_t x; v = get_pylong(state, v); if (v == NULL) return -1; assert(PyLong_Check(v)); x = PyLong_AsSize_t(v); Py_DECREF(v); if (x == (size_t)-1 && PyErr_Occurred()) { return -1; } *p = x; return 0; } #define RANGE_ERROR(state, f, flag) return _range_error(state, f, flag) /* Floating-point helpers */ static PyObject * unpack_halffloat(const char *p, /* start of 2-byte string */ int le) /* true for little-endian, false for big-endian */ { double x = PyFloat_Unpack2(p, le); if (x == -1.0 && PyErr_Occurred()) { return NULL; } return PyFloat_FromDouble(x); } static int pack_halffloat(_structmodulestate *state, char *p, /* start of 2-byte string */ PyObject *v, /* value to pack */ int le) /* true for little-endian, false for big-endian */ { double x = PyFloat_AsDouble(v); if (x == -1.0 && PyErr_Occurred()) { PyErr_SetString(state->StructError, "required argument is not a float"); return -1; } return PyFloat_Pack2(x, p, le); } static PyObject * unpack_float(const char *p, /* start of 4-byte string */ int le) /* true for little-endian, false for big-endian */ { double x; x = PyFloat_Unpack4(p, le); if (x == -1.0 && PyErr_Occurred()) return NULL; return PyFloat_FromDouble(x); } static PyObject * unpack_double(const char *p, /* start of 8-byte string */ int le) /* true for little-endian, false for big-endian */ { double x; x = PyFloat_Unpack8(p, le); if (x == -1.0 && PyErr_Occurred()) return NULL; return PyFloat_FromDouble(x); } /* Helper to format the range error exceptions */ static int _range_error(_structmodulestate *state, const formatdef *f, int is_unsigned) { /* ulargest is the largest unsigned value with f->size bytes. * Note that the simpler: * ((size_t)1 << (f->size * 8)) - 1 * doesn't work when f->size == sizeof(size_t) because C doesn't * define what happens when a left shift count is >= the number of * bits in the integer being shifted; e.g., on some boxes it doesn't * shift at all when they're equal. */ const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8); assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T); if (is_unsigned) PyErr_Format(state->StructError, "'%c' format requires 0 <= number <= %zu", f->format, ulargest); else { const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1); PyErr_Format(state->StructError, "'%c' format requires %zd <= number <= %zd", f->format, ~ largest, largest); } return -1; } /* A large number of small routines follow, with names of the form [bln][up]_TYPE [bln] distinguishes among big-endian, little-endian and native. [pu] distinguishes between pack (to struct) and unpack (from struct). TYPE is one of char, byte, ubyte, etc. */ /* Native mode routines. ****************************************************/ /* NOTE: In all n[up]_<type> routines handling types larger than 1 byte, there is *no* guarantee that the p pointer is properly aligned for each type, therefore memcpy is called. An intermediate variable is used to compensate for big-endian architectures. Normally both the intermediate variable and the memcpy call will be skipped by C optimisation in little-endian architectures (gcc >= 2.91 does this). */ static PyObject * nu_char(_structmodulestate *state, const char *p, const formatdef *f) { return PyBytes_FromStringAndSize(p, 1); } static PyObject * nu_byte(_structmodulestate *state, const char *p, const formatdef *f) { return PyLong_FromLong((long) *(signed char *)p); } static PyObject * nu_ubyte(_structmodulestate *state, const char *p, const formatdef *f) { return PyLong_FromLong((long) *(unsigned char *)p); } static PyObject * nu_short(_structmodulestate *state, const char *p, const formatdef *f) { short x; memcpy(&x, p, sizeof x); return PyLong_FromLong((long)x); } static PyObject * nu_ushort(_structmodulestate *state, const char *p, const formatdef *f) { unsigned short x; memcpy(&x, p, sizeof x); return PyLong_FromLong((long)x); } static PyObject * nu_int(_structmodulestate *state, const char *p, const formatdef *f) { int x; memcpy(&x, p, sizeof x); return PyLong_FromLong((long)x); } static PyObject * nu_uint(_structmodulestate *state, const char *p, const formatdef *f) { unsigned int x; memcpy(&x, p, sizeof x); return PyLong_FromUnsignedLong((unsigned long)x); } static PyObject * nu_long(_structmodulestate *state, const char *p, const formatdef *f) { long x; memcpy(&x, p, sizeof x); return PyLong_FromLong(x); } static PyObject * nu_ulong(_structmodulestate *state, const char *p, const formatdef *f) { unsigned long x; memcpy(&x, p, sizeof x); return PyLong_FromUnsignedLong(x); } static PyObject * nu_ssize_t(_structmodulestate *state, const char *p, const formatdef *f) { Py_ssize_t x; memcpy(&x, p, sizeof x); return PyLong_FromSsize_t(x); } static PyObject * nu_size_t(_structmodulestate *state, const char *p, const formatdef *f) { size_t x; memcpy(&x, p, sizeof x); return PyLong_FromSize_t(x); } static PyObject * nu_longlong(_structmodulestate *state, const char *p, const formatdef *f) { long long x; memcpy(&x, p, sizeof x); return PyLong_FromLongLong(x); } static PyObject * nu_ulonglong(_structmodulestate *state, const char *p, const formatdef *f) { unsigned long long x; memcpy(&x, p, sizeof x); return PyLong_FromUnsignedLongLong(x); } static PyObject * nu_bool(_structmodulestate *state, const char *p, const formatdef *f) { const _Bool bool_false = 0; return PyBool_FromLong(memcmp(p, &bool_false, sizeof(_Bool))); } static PyObject * nu_halffloat(_structmodulestate *state, const char *p, const formatdef *f) { #if PY_LITTLE_ENDIAN return unpack_halffloat(p, 1); #else return unpack_halffloat(p, 0); #endif } static PyObject * nu_float(_structmodulestate *state, const char *p, const formatdef *f) { float x; memcpy(&x, p, sizeof x); return PyFloat_FromDouble((double)x); } static PyObject * nu_double(_structmodulestate *state, const char *p, const formatdef *f) { double x; memcpy(&x, p, sizeof x); return PyFloat_FromDouble(x); } static PyObject * nu_float_complex(_structmodulestate *state, const char *p, const formatdef *f) { float x[2]; memcpy(&x, p, sizeof(x)); return PyComplex_FromDoubles(x[0], x[1]); } static PyObject * nu_double_complex(_structmodulestate *state, const char *p, const formatdef *f) { double x[2]; memcpy(&x, p, sizeof(x)); return PyComplex_FromDoubles(x[0], x[1]); } static PyObject * nu_void_p(_structmodulestate *state, const char *p, const formatdef *f) { void *x; memcpy(&x, p, sizeof x); return PyLong_FromVoidPtr(x); } static int np_byte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { long x; if (get_long(state, v, &x) < 0) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { RANGE_ERROR(state, f, 0); } return -1; } if (x < -128 || x > 127) { RANGE_ERROR(state, f, 0); } *p = (char)x; return 0; } static int np_ubyte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { long x; if (get_long(state, v, &x) < 0) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { RANGE_ERROR(state, f, 1); } return -1; } if (x < 0 || x > 255) { RANGE_ERROR(state, f, 1); } *(unsigned char *)p = (unsigned char)x; return 0; } static int np_char(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) { PyErr_SetString(state->StructError, "char format requires a bytes object of length 1"); return -1; } *p = *PyBytes_AS_STRING(v); return 0; } static int np_short(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { long x; short y; if (get_long(state, v, &x) < 0) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { RANGE_ERROR(state, f, 0); } return -1; } if (x < SHRT_MIN || x > SHRT_MAX) { RANGE_ERROR(state, f, 0); } y = (short)x; memcpy(p, &y, sizeof y); return 0; } static int np_ushort(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { long x; unsigned short y; if (get_long(state, v, &x) < 0) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { RANGE_ERROR(state, f, 1); } return -1; } if (x < 0 || x > USHRT_MAX) { RANGE_ERROR(state, f, 1); } y = (unsigned short)x; memcpy(p, &y, sizeof y); return 0; } static int np_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { long x; int y; if (get_long(state, v, &x) < 0) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { RANGE_ERROR(state, f, 0); } return -1; } #if (SIZEOF_LONG > SIZEOF_INT) if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX))) RANGE_ERROR(state, f, 0); #endif y = (int)x; memcpy(p, &y, sizeof y); return 0; } static int np_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { unsigned long x; unsigned int y; if (get_ulong(state, v, &x) < 0) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { RANGE_ERROR(state, f, 1); } return -1; } y = (unsigned int)x; #if (SIZEOF_LONG > SIZEOF_INT) if (x > ((unsigned long)UINT_MAX)) RANGE_ERROR(state, f, 1); #endif memcpy(p, &y, sizeof y); return 0; } static int np_long(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { long x; if (get_long(state, v, &x) < 0) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { RANGE_ERROR(state, f, 0); } return -1; } memcpy(p, &x, sizeof x); return 0; } static int np_ulong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { unsigned long x; if (get_ulong(state, v, &x) < 0) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { RANGE_ERROR(state, f, 1); } return -1; } memcpy(p, &x, sizeof x); return 0; } static int np_ssize_t(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { Py_ssize_t x; if (get_ssize_t(state, v, &x) < 0) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { RANGE_ERROR(state, f, 0); } return -1; } memcpy(p, &x, sizeof x); return 0; } static int np_size_t(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { size_t x; if (get_size_t(state, v, &x) < 0) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { RANGE_ERROR(state, f, 1); } return -1; } memcpy(p, &x, sizeof x); return 0; } static int np_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { long long x; if (get_longlong(state, v, &x) < 0) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { PyErr_Format(state->StructError, "'%c' format requires %lld <= number <= %lld", f->format, LLONG_MIN, LLONG_MAX); } return -1; } memcpy(p, &x, sizeof x); return 0; } static int np_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { unsigned long long x; if (get_ulonglong(state, v, &x) < 0) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { PyErr_Format(state->StructError, "'%c' format requires 0 <= number <= %llu", f->format, ULLONG_MAX); } return -1; } memcpy(p, &x, sizeof x); return 0; } static int np_bool(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { int y; _Bool x; y = PyObject_IsTrue(v); if (y < 0) return -1; x = y; memcpy(p, &x, sizeof x); return 0; } static int np_halffloat(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { #if PY_LITTLE_ENDIAN return pack_halffloat(state, p, v, 1); #else return pack_halffloat(state, p, v, 0); #endif } static int np_float(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { double x = PyFloat_AsDouble(v); if (x == -1 && PyErr_Occurred()) { PyErr_SetString(state->StructError, "required argument is not a float"); return -1; } return PyFloat_Pack4(x, p, PY_LITTLE_ENDIAN); } static int np_double(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { double x = PyFloat_AsDouble(v); if (x == -1 && PyErr_Occurred()) { PyErr_SetString(state->StructError, "required argument is not a float"); return -1; } memcpy(p, &x, sizeof(double)); return 0; } static int np_float_complex(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { Py_complex c = PyComplex_AsCComplex(v); float x[2] = {(float)c.real, (float)c.imag}; if (c.real == -1 && PyErr_Occurred()) { PyErr_SetString(state->StructError, "required argument is not a complex"); return -1; } memcpy(p, &x, sizeof(x)); return 0; } static int np_double_complex(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { Py_complex c = PyComplex_AsCComplex(v); double x[2] = {c.real, c.imag}; if (c.real == -1 && PyErr_Occurred()) { PyErr_SetString(state->StructError, "required argument is not a complex"); return -1; } memcpy(p, &x, sizeof(x)); return 0; } static int np_void_p(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { void *x; v = get_pylong(state, v); if (v == NULL) return -1; assert(PyLong_Check(v)); x = PyLong_AsVoidPtr(v); Py_DECREF(v); if (x == NULL && PyErr_Occurred()) return -1; memcpy(p, &x, sizeof x); return 0; } static const formatdef native_table[] = { {'x', sizeof(char), 0, NULL}, {'b', sizeof(char), 0, nu_byte, np_byte}, {'B', sizeof(char), 0, nu_ubyte, np_ubyte}, {'c', sizeof(char), 0, nu_char, np_char}, {'s', sizeof(char), 0, NULL}, {'p', sizeof(char), 0, NULL}, {'h', sizeof(short), _Alignof(short), nu_short, np_short}, {'H', sizeof(short), _Alignof(short), nu_ushort, np_ushort}, {'i', sizeof(int), _Alignof(int), nu_int, np_int}, {'I', sizeof(int), _Alignof(int), nu_uint, np_uint}, {'l', sizeof(long), _Alignof(long), nu_long, np_long}, {'L', sizeof(long), _Alignof(long), nu_ulong, np_ulong}, {'n', sizeof(size_t), _Alignof(size_t), nu_ssize_t, np_ssize_t}, {'N', sizeof(size_t), _Alignof(size_t), nu_size_t, np_size_t}, {'q', sizeof(long long), _Alignof(long long), nu_longlong, np_longlong}, {'Q', sizeof(long long), _Alignof(long long), nu_ulonglong,np_ulonglong}, {'?', sizeof(_Bool), _Alignof(_Bool), nu_bool, np_bool}, {'e', sizeof(short), _Alignof(short), nu_halffloat, np_halffloat}, {'f', sizeof(float), _Alignof(float), nu_float, np_float}, {'d', sizeof(double), _Alignof(double), nu_double, np_double}, {'F', 2*sizeof(float), _Alignof(float), nu_float_complex, np_float_complex}, {'D', 2*sizeof(double), _Alignof(double), nu_double_complex, np_double_complex}, {'P', sizeof(void *), _Alignof(void *), nu_void_p, np_void_p}, {0} }; /* Big-endian routines. *****************************************************/ static PyObject * bu_short(_structmodulestate *state, const char *p, const formatdef *f) { unsigned long x = 0; /* This function is only ever used in the case f->size == 2. */ assert(f->size == 2); Py_ssize_t i = 2; const unsigned char *bytes = (const unsigned char *)p; do { x = (x<<8) | *bytes++; } while (--i > 0); /* Extend sign, avoiding implementation-defined or undefined behaviour. */ x = (x ^ 0x8000U) - 0x8000U; return PyLong_FromLong(x & 0x8000U ? -1 - (long)(~x) : (long)x); } static PyObject * bu_int(_structmodulestate *state, const char *p, const formatdef *f) { unsigned long x = 0; /* This function is only ever used in the case f->size == 4. */ assert(f->size == 4); Py_ssize_t i = 4; const unsigned char *bytes = (const unsigned char *)p; do { x = (x<<8) | *bytes++; } while (--i > 0); /* Extend sign, avoiding implementation-defined or undefined behaviour. */ x = (x ^ 0x80000000U) - 0x80000000U; return PyLong_FromLong(x & 0x80000000U ? -1 - (long)(~x) : (long)x); } static PyObject * bu_uint(_structmodulestate *state, const char *p, const formatdef *f) { unsigned long x = 0; Py_ssize_t i = f->size; const unsigned char *bytes = (const unsigned char *)p; do { x = (x<<8) | *bytes++; } while (--i > 0); return PyLong_FromUnsignedLong(x); } static PyObject * bu_longlong(_structmodulestate *state, const char *p, const formatdef *f) { unsigned long long x = 0; /* This function is only ever used in the case f->size == 8. */ assert(f->size == 8); Py_ssize_t i = 8; const unsigned char *bytes = (const unsigned char *)p; do { x = (x<<8) | *bytes++; } while (--i > 0); /* Extend sign, avoiding implementation-defined or undefined behaviour. */ x = (x ^ 0x8000000000000000U) - 0x8000000000000000U; return PyLong_FromLongLong( x & 0x8000000000000000U ? -1 - (long long)(~x) : (long long)x); } static PyObject * bu_ulonglong(_structmodulestate *state, const char *p, const formatdef *f) { unsigned long long x = 0; Py_ssize_t i = f->size; const unsigned char *bytes = (const unsigned char *)p; do { x = (x<<8) | *bytes++; } while (--i > 0); return PyLong_FromUnsignedLongLong(x); } static PyObject * bu_halffloat(_structmodulestate *state, const char *p, const formatdef *f) { return unpack_halffloat(p, 0); } static PyObject * bu_float(_structmodulestate *state, const char *p, const formatdef *f) { return unpack_float(p, 0); } static PyObject * bu_double(_structmodulestate *state, const char *p, const formatdef *f) { return unpack_double(p, 0); } static PyObject * bu_float_complex(_structmodulestate *state, const char *p, const formatdef *f) { double x = PyFloat_Unpack4(p, 0); if (x == -1.0 && PyErr_Occurred()) { return NULL; } double y = PyFloat_Unpack4(p + 4, 0); if (y == -1.0 && PyErr_Occurred()) { return NULL; } return PyComplex_FromDoubles(x, y); } static PyObject * bu_double_complex(_structmodulestate *state, const char *p, const formatdef *f) { double x, y; x = PyFloat_Unpack8(p, 0); if (x == -1.0 && PyErr_Occurred()) { return NULL; } y = PyFloat_Unpack8(p + 8, 0); if (y == -1.0 && PyErr_Occurred()) { return NULL; } return PyComplex_FromDoubles(x, y); } static PyObject * bu_bool(_structmodulestate *state, const char *p, const formatdef *f) { return PyBool_FromLong(*p != 0); } static int bp_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) { long x; Py_ssize_t i; unsigned char *q = (unsigned char *)p; |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |