@@ -423,13 +423,7 @@ nu_uint(const char *p, const formatdef *f)
423
423
{
424
424
unsigned int x ;
425
425
memcpy ((char * )& x , p , sizeof x );
426
- #if (SIZEOF_LONG > SIZEOF_INT )
427
- return PyLong_FromLong ((long )x );
428
- #else
429
- if (x <= ((unsigned int )LONG_MAX ))
430
- return PyLong_FromLong ((long )x );
431
426
return PyLong_FromUnsignedLong ((unsigned long )x );
432
- #endif
433
427
}
434
428
435
429
static PyObject *
@@ -445,8 +439,6 @@ nu_ulong(const char *p, const formatdef *f)
445
439
{
446
440
unsigned long x ;
447
441
memcpy ((char * )& x , p , sizeof x );
448
- if (x <= LONG_MAX )
449
- return PyLong_FromLong ((long )x );
450
442
return PyLong_FromUnsignedLong (x );
451
443
}
452
444
@@ -466,17 +458,11 @@ nu_size_t(const char *p, const formatdef *f)
466
458
return PyLong_FromSize_t (x );
467
459
}
468
460
469
-
470
- /* Native mode doesn't support q or Q unless the platform C supports
471
- long long (or, on Windows, __int64). */
472
-
473
461
static PyObject *
474
462
nu_longlong (const char * p , const formatdef * f )
475
463
{
476
464
long long x ;
477
465
memcpy ((char * )& x , p , sizeof x );
478
- if (x >= LONG_MIN && x <= LONG_MAX )
479
- return PyLong_FromLong (Py_SAFE_DOWNCAST (x , long long , long ));
480
466
return PyLong_FromLongLong (x );
481
467
}
482
468
@@ -485,8 +471,6 @@ nu_ulonglong(const char *p, const formatdef *f)
485
471
{
486
472
unsigned long long x ;
487
473
memcpy ((char * )& x , p , sizeof x );
488
- if (x <= LONG_MAX )
489
- return PyLong_FromLong (Py_SAFE_DOWNCAST (x , unsigned long long, long ));
490
474
return PyLong_FromUnsignedLongLong (x );
491
475
}
492
476
@@ -539,7 +523,7 @@ np_byte(char *p, PyObject *v, const formatdef *f)
539
523
long x ;
540
524
if (get_long (v , & x ) < 0 )
541
525
return -1 ;
542
- if (x < -128 || x > 127 ){
526
+ if (x < -128 || x > 127 ) {
543
527
PyErr_SetString (StructError ,
544
528
"byte format requires -128 <= number <= 127" );
545
529
return -1 ;
@@ -554,7 +538,7 @@ np_ubyte(char *p, PyObject *v, const formatdef *f)
554
538
long x ;
555
539
if (get_long (v , & x ) < 0 )
556
540
return -1 ;
557
- if (x < 0 || x > 255 ){
541
+ if (x < 0 || x > 255 ) {
558
542
PyErr_SetString (StructError ,
559
543
"ubyte format requires 0 <= number <= 255" );
560
544
return -1 ;
@@ -566,12 +550,12 @@ np_ubyte(char *p, PyObject *v, const formatdef *f)
566
550
static int
567
551
np_char (char * p , PyObject * v , const formatdef * f )
568
552
{
569
- if (!PyBytes_Check (v ) || PyBytes_Size (v ) != 1 ) {
553
+ if (!PyBytes_Check (v ) || PyBytes_GET_SIZE (v ) != 1 ) {
570
554
PyErr_SetString (StructError ,
571
555
"char format requires a bytes object of length 1" );
572
556
return -1 ;
573
557
}
574
- * p = * PyBytes_AsString (v );
558
+ * p = * PyBytes_AS_STRING (v );
575
559
return 0 ;
576
560
}
577
561
@@ -582,7 +566,7 @@ np_short(char *p, PyObject *v, const formatdef *f)
582
566
short y ;
583
567
if (get_long (v , & x ) < 0 )
584
568
return -1 ;
585
- if (x < SHRT_MIN || x > SHRT_MAX ){
569
+ if (x < SHRT_MIN || x > SHRT_MAX ) {
586
570
PyErr_SetString (StructError ,
587
571
"short format requires " Py_STRINGIFY (SHRT_MIN )
588
572
" <= number <= " Py_STRINGIFY (SHRT_MAX ));
@@ -600,7 +584,7 @@ np_ushort(char *p, PyObject *v, const formatdef *f)
600
584
unsigned short y ;
601
585
if (get_long (v , & x ) < 0 )
602
586
return -1 ;
603
- if (x < 0 || x > USHRT_MAX ){
587
+ if (x < 0 || x > USHRT_MAX ) {
604
588
PyErr_SetString (StructError ,
605
589
"ushort format requires 0 <= number <= "
606
590
Py_STRINGIFY (USHRT_MAX ));
@@ -821,8 +805,6 @@ bu_uint(const char *p, const formatdef *f)
821
805
do {
822
806
x = (x <<8 ) | * bytes ++ ;
823
807
} while (-- i > 0 );
824
- if (x <= LONG_MAX )
825
- return PyLong_FromLong ((long )x );
826
808
return PyLong_FromUnsignedLong (x );
827
809
}
828
810
@@ -838,8 +820,6 @@ bu_longlong(const char *p, const formatdef *f)
838
820
/* Extend the sign bit. */
839
821
if (SIZEOF_LONG_LONG > f -> size )
840
822
x |= - (x & ((long long )1 << ((8 * f -> size ) - 1 )));
841
- if (x >= LONG_MIN && x <= LONG_MAX )
842
- return PyLong_FromLong (Py_SAFE_DOWNCAST (x , long long , long ));
843
823
return PyLong_FromLongLong (x );
844
824
}
845
825
@@ -852,8 +832,6 @@ bu_ulonglong(const char *p, const formatdef *f)
852
832
do {
853
833
x = (x <<8 ) | * bytes ++ ;
854
834
} while (-- i > 0 );
855
- if (x <= LONG_MAX )
856
- return PyLong_FromLong (Py_SAFE_DOWNCAST (x , unsigned long long, long ));
857
835
return PyLong_FromUnsignedLongLong (x );
858
836
}
859
837
@@ -878,9 +856,7 @@ bu_double(const char *p, const formatdef *f)
878
856
static PyObject *
879
857
bu_bool (const char * p , const formatdef * f )
880
858
{
881
- char x ;
882
- memcpy ((char * )& x , p , sizeof x );
883
- return PyBool_FromLong (x != 0 );
859
+ return PyBool_FromLong (* p != 0 );
884
860
}
885
861
886
862
static int
@@ -938,7 +914,7 @@ bp_longlong(char *p, PyObject *v, const formatdef *f)
938
914
(unsigned char * )p ,
939
915
8 ,
940
916
0 , /* little_endian */
941
- 1 /* signed */ );
917
+ 1 /* signed */ );
942
918
Py_DECREF (v );
943
919
return res ;
944
920
}
@@ -954,7 +930,7 @@ bp_ulonglong(char *p, PyObject *v, const formatdef *f)
954
930
(unsigned char * )p ,
955
931
8 ,
956
932
0 , /* little_endian */
957
- 0 /* signed */ );
933
+ 0 /* signed */ );
958
934
Py_DECREF (v );
959
935
return res ;
960
936
}
@@ -1048,9 +1024,7 @@ lu_uint(const char *p, const formatdef *f)
1048
1024
do {
1049
1025
x = (x <<8 ) | bytes [-- i ];
1050
1026
} while (i > 0 );
1051
- if (x <= LONG_MAX )
1052
- return PyLong_FromLong ((long )x );
1053
- return PyLong_FromUnsignedLong ((long )x );
1027
+ return PyLong_FromUnsignedLong (x );
1054
1028
}
1055
1029
1056
1030
static PyObject *
@@ -1065,8 +1039,6 @@ lu_longlong(const char *p, const formatdef *f)
1065
1039
/* Extend the sign bit. */
1066
1040
if (SIZEOF_LONG_LONG > f -> size )
1067
1041
x |= - (x & ((long long )1 << ((8 * f -> size ) - 1 )));
1068
- if (x >= LONG_MIN && x <= LONG_MAX )
1069
- return PyLong_FromLong (Py_SAFE_DOWNCAST (x , long long , long ));
1070
1042
return PyLong_FromLongLong (x );
1071
1043
}
1072
1044
@@ -1079,8 +1051,6 @@ lu_ulonglong(const char *p, const formatdef *f)
1079
1051
do {
1080
1052
x = (x <<8 ) | bytes [-- i ];
1081
1053
} while (i > 0 );
1082
- if (x <= LONG_MAX )
1083
- return PyLong_FromLong (Py_SAFE_DOWNCAST (x , unsigned long long, long ));
1084
1054
return PyLong_FromUnsignedLongLong (x );
1085
1055
}
1086
1056
@@ -1157,7 +1127,7 @@ lp_longlong(char *p, PyObject *v, const formatdef *f)
1157
1127
(unsigned char * )p ,
1158
1128
8 ,
1159
1129
1 , /* little_endian */
1160
- 1 /* signed */ );
1130
+ 1 /* signed */ );
1161
1131
Py_DECREF (v );
1162
1132
return res ;
1163
1133
}
@@ -1173,7 +1143,7 @@ lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1173
1143
(unsigned char * )p ,
1174
1144
8 ,
1175
1145
1 , /* little_endian */
1176
- 0 /* signed */ );
1146
+ 0 /* signed */ );
1177
1147
Py_DECREF (v );
1178
1148
return res ;
1179
1149
}
@@ -1390,8 +1360,6 @@ prepare_s(PyStructObject *self)
1390
1360
num = c - '0' ;
1391
1361
while ('0' <= (c = * s ++ ) && c <= '9' )
1392
1362
num = num * 10 + (c - '0' );
1393
- if (c == '\0' )
1394
- break ;
1395
1363
}
1396
1364
else
1397
1365
num = 1 ;
@@ -1486,7 +1454,7 @@ Struct___init___impl(PyStructObject *self, PyObject *format)
1486
1454
return -1 ;
1487
1455
}
1488
1456
1489
- Py_XSETREF (self -> s_format , format );
1457
+ Py_SETREF (self -> s_format , format );
1490
1458
1491
1459
ret = prepare_s (self );
1492
1460
return ret ;
@@ -1500,7 +1468,7 @@ s_dealloc(PyStructObject *s)
1500
1468
if (s -> s_codes != NULL ) {
1501
1469
PyMem_FREE (s -> s_codes );
1502
1470
}
1503
- Py_XDECREF (s -> s_format );
1471
+ Py_DECREF (s -> s_format );
1504
1472
Py_TYPE (s )-> tp_free ((PyObject * )s );
1505
1473
}
1506
1474
@@ -1864,7 +1832,7 @@ s_pack(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
1864
1832
return NULL ;
1865
1833
}
1866
1834
1867
- /* Allocate a new string */
1835
+ /* Allocate a new buffer */
1868
1836
result = PyBytes_FromStringAndSize ((char * )NULL , soself -> s_size );
1869
1837
if (result == NULL )
1870
1838
return NULL ;
0 commit comments