Skip to content

Commit 96f5028

Browse files
authored
bpo-30224: remove outdated checks in struct (#1374)
1 parent 12b1c18 commit 96f5028

File tree

1 file changed

+15
-47
lines changed

1 file changed

+15
-47
lines changed

Modules/_struct.c

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -423,13 +423,7 @@ nu_uint(const char *p, const formatdef *f)
423423
{
424424
unsigned int x;
425425
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);
431426
return PyLong_FromUnsignedLong((unsigned long)x);
432-
#endif
433427
}
434428

435429
static PyObject *
@@ -445,8 +439,6 @@ nu_ulong(const char *p, const formatdef *f)
445439
{
446440
unsigned long x;
447441
memcpy((char *)&x, p, sizeof x);
448-
if (x <= LONG_MAX)
449-
return PyLong_FromLong((long)x);
450442
return PyLong_FromUnsignedLong(x);
451443
}
452444

@@ -466,17 +458,11 @@ nu_size_t(const char *p, const formatdef *f)
466458
return PyLong_FromSize_t(x);
467459
}
468460

469-
470-
/* Native mode doesn't support q or Q unless the platform C supports
471-
long long (or, on Windows, __int64). */
472-
473461
static PyObject *
474462
nu_longlong(const char *p, const formatdef *f)
475463
{
476464
long long x;
477465
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));
480466
return PyLong_FromLongLong(x);
481467
}
482468

@@ -485,8 +471,6 @@ nu_ulonglong(const char *p, const formatdef *f)
485471
{
486472
unsigned long long x;
487473
memcpy((char *)&x, p, sizeof x);
488-
if (x <= LONG_MAX)
489-
return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned long long, long));
490474
return PyLong_FromUnsignedLongLong(x);
491475
}
492476

@@ -539,7 +523,7 @@ np_byte(char *p, PyObject *v, const formatdef *f)
539523
long x;
540524
if (get_long(v, &x) < 0)
541525
return -1;
542-
if (x < -128 || x > 127){
526+
if (x < -128 || x > 127) {
543527
PyErr_SetString(StructError,
544528
"byte format requires -128 <= number <= 127");
545529
return -1;
@@ -554,7 +538,7 @@ np_ubyte(char *p, PyObject *v, const formatdef *f)
554538
long x;
555539
if (get_long(v, &x) < 0)
556540
return -1;
557-
if (x < 0 || x > 255){
541+
if (x < 0 || x > 255) {
558542
PyErr_SetString(StructError,
559543
"ubyte format requires 0 <= number <= 255");
560544
return -1;
@@ -566,12 +550,12 @@ np_ubyte(char *p, PyObject *v, const formatdef *f)
566550
static int
567551
np_char(char *p, PyObject *v, const formatdef *f)
568552
{
569-
if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
553+
if (!PyBytes_Check(v) || PyBytes_GET_SIZE(v) != 1) {
570554
PyErr_SetString(StructError,
571555
"char format requires a bytes object of length 1");
572556
return -1;
573557
}
574-
*p = *PyBytes_AsString(v);
558+
*p = *PyBytes_AS_STRING(v);
575559
return 0;
576560
}
577561

@@ -582,7 +566,7 @@ np_short(char *p, PyObject *v, const formatdef *f)
582566
short y;
583567
if (get_long(v, &x) < 0)
584568
return -1;
585-
if (x < SHRT_MIN || x > SHRT_MAX){
569+
if (x < SHRT_MIN || x > SHRT_MAX) {
586570
PyErr_SetString(StructError,
587571
"short format requires " Py_STRINGIFY(SHRT_MIN)
588572
" <= number <= " Py_STRINGIFY(SHRT_MAX));
@@ -600,7 +584,7 @@ np_ushort(char *p, PyObject *v, const formatdef *f)
600584
unsigned short y;
601585
if (get_long(v, &x) < 0)
602586
return -1;
603-
if (x < 0 || x > USHRT_MAX){
587+
if (x < 0 || x > USHRT_MAX) {
604588
PyErr_SetString(StructError,
605589
"ushort format requires 0 <= number <= "
606590
Py_STRINGIFY(USHRT_MAX));
@@ -821,8 +805,6 @@ bu_uint(const char *p, const formatdef *f)
821805
do {
822806
x = (x<<8) | *bytes++;
823807
} while (--i > 0);
824-
if (x <= LONG_MAX)
825-
return PyLong_FromLong((long)x);
826808
return PyLong_FromUnsignedLong(x);
827809
}
828810

@@ -838,8 +820,6 @@ bu_longlong(const char *p, const formatdef *f)
838820
/* Extend the sign bit. */
839821
if (SIZEOF_LONG_LONG > f->size)
840822
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));
843823
return PyLong_FromLongLong(x);
844824
}
845825

@@ -852,8 +832,6 @@ bu_ulonglong(const char *p, const formatdef *f)
852832
do {
853833
x = (x<<8) | *bytes++;
854834
} while (--i > 0);
855-
if (x <= LONG_MAX)
856-
return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned long long, long));
857835
return PyLong_FromUnsignedLongLong(x);
858836
}
859837

@@ -878,9 +856,7 @@ bu_double(const char *p, const formatdef *f)
878856
static PyObject *
879857
bu_bool(const char *p, const formatdef *f)
880858
{
881-
char x;
882-
memcpy((char *)&x, p, sizeof x);
883-
return PyBool_FromLong(x != 0);
859+
return PyBool_FromLong(*p != 0);
884860
}
885861

886862
static int
@@ -938,7 +914,7 @@ bp_longlong(char *p, PyObject *v, const formatdef *f)
938914
(unsigned char *)p,
939915
8,
940916
0, /* little_endian */
941-
1 /* signed */);
917+
1 /* signed */);
942918
Py_DECREF(v);
943919
return res;
944920
}
@@ -954,7 +930,7 @@ bp_ulonglong(char *p, PyObject *v, const formatdef *f)
954930
(unsigned char *)p,
955931
8,
956932
0, /* little_endian */
957-
0 /* signed */);
933+
0 /* signed */);
958934
Py_DECREF(v);
959935
return res;
960936
}
@@ -1048,9 +1024,7 @@ lu_uint(const char *p, const formatdef *f)
10481024
do {
10491025
x = (x<<8) | bytes[--i];
10501026
} 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);
10541028
}
10551029

10561030
static PyObject *
@@ -1065,8 +1039,6 @@ lu_longlong(const char *p, const formatdef *f)
10651039
/* Extend the sign bit. */
10661040
if (SIZEOF_LONG_LONG > f->size)
10671041
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));
10701042
return PyLong_FromLongLong(x);
10711043
}
10721044

@@ -1079,8 +1051,6 @@ lu_ulonglong(const char *p, const formatdef *f)
10791051
do {
10801052
x = (x<<8) | bytes[--i];
10811053
} while (i > 0);
1082-
if (x <= LONG_MAX)
1083-
return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned long long, long));
10841054
return PyLong_FromUnsignedLongLong(x);
10851055
}
10861056

@@ -1157,7 +1127,7 @@ lp_longlong(char *p, PyObject *v, const formatdef *f)
11571127
(unsigned char *)p,
11581128
8,
11591129
1, /* little_endian */
1160-
1 /* signed */);
1130+
1 /* signed */);
11611131
Py_DECREF(v);
11621132
return res;
11631133
}
@@ -1173,7 +1143,7 @@ lp_ulonglong(char *p, PyObject *v, const formatdef *f)
11731143
(unsigned char *)p,
11741144
8,
11751145
1, /* little_endian */
1176-
0 /* signed */);
1146+
0 /* signed */);
11771147
Py_DECREF(v);
11781148
return res;
11791149
}
@@ -1390,8 +1360,6 @@ prepare_s(PyStructObject *self)
13901360
num = c - '0';
13911361
while ('0' <= (c = *s++) && c <= '9')
13921362
num = num*10 + (c - '0');
1393-
if (c == '\0')
1394-
break;
13951363
}
13961364
else
13971365
num = 1;
@@ -1486,7 +1454,7 @@ Struct___init___impl(PyStructObject *self, PyObject *format)
14861454
return -1;
14871455
}
14881456

1489-
Py_XSETREF(self->s_format, format);
1457+
Py_SETREF(self->s_format, format);
14901458

14911459
ret = prepare_s(self);
14921460
return ret;
@@ -1500,7 +1468,7 @@ s_dealloc(PyStructObject *s)
15001468
if (s->s_codes != NULL) {
15011469
PyMem_FREE(s->s_codes);
15021470
}
1503-
Py_XDECREF(s->s_format);
1471+
Py_DECREF(s->s_format);
15041472
Py_TYPE(s)->tp_free((PyObject *)s);
15051473
}
15061474

@@ -1864,7 +1832,7 @@ s_pack(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
18641832
return NULL;
18651833
}
18661834

1867-
/* Allocate a new string */
1835+
/* Allocate a new buffer */
18681836
result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
18691837
if (result == NULL)
18701838
return NULL;

0 commit comments

Comments
 (0)