Skip to content

Commit b0240e5

Browse files
author
bob.ippolito
committed
simplify the struct code a bit (no functional changes)
git-svn-id: http://svn.python.org/projects/python/trunk@46527 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 2161382 commit b0240e5

1 file changed

Lines changed: 31 additions & 23 deletions

File tree

Modules/_struct.c

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -223,28 +223,28 @@ unpack_double(const char *p, /* start of 8-byte string */
223223

224224
/* Helper to format the range error exceptions */
225225
static int
226-
_range_error(char format, Py_ssize_t size, int is_unsigned)
226+
_range_error(formatdef *f, int is_unsigned)
227227
{
228228
if (is_unsigned == 0) {
229229
long smallest = 0, largest = 0;
230-
Py_ssize_t i = size * 8;
230+
Py_ssize_t i = f->size * 8;
231231
while (--i > 0) {
232232
smallest = (smallest * 2) - 1;
233233
largest = (largest * 2) + 1;
234234
}
235235
PyErr_Format(StructError,
236236
"'%c' format requires %ld <= number <= %ld",
237-
format,
237+
f->format,
238238
smallest,
239239
largest);
240240
} else {
241241
unsigned long largest = 0;
242-
Py_ssize_t i = size * 8;
242+
Py_ssize_t i = f->size * 8;
243243
while (--i >= 0)
244244
largest = (largest * 2) + 1;
245245
PyErr_Format(StructError,
246246
"'%c' format requires 0 <= number <= %lu",
247-
format,
247+
f->format,
248248
largest);
249249
}
250250
return -1;
@@ -482,7 +482,7 @@ np_int(char *p, PyObject *v, const formatdef *f)
482482
return -1;
483483
#if (SIZEOF_LONG > SIZEOF_INT)
484484
if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
485-
return _range_error(f->format, sizeof(y), 0);
485+
return _range_error(f, 0);
486486
#endif
487487
y = (int)x;
488488
memcpy(p, (char *)&y, sizeof y);
@@ -495,11 +495,11 @@ np_uint(char *p, PyObject *v, const formatdef *f)
495495
unsigned long x;
496496
unsigned int y;
497497
if (get_ulong(v, &x) < 0)
498-
return _range_error(f->format, sizeof(y), 1);
498+
return _range_error(f, 1);
499499
y = (unsigned int)x;
500500
#if (SIZEOF_LONG > SIZEOF_INT)
501501
if (x > ((unsigned long)UINT_MAX))
502-
return _range_error(f->format, sizeof(y), 1);
502+
return _range_error(f, 1);
503503
#endif
504504
memcpy(p, (char *)&y, sizeof y);
505505
return 0;
@@ -520,7 +520,7 @@ np_ulong(char *p, PyObject *v, const formatdef *f)
520520
{
521521
unsigned long x;
522522
if (get_ulong(v, &x) < 0)
523-
return _range_error(f->format, sizeof(x), 1);
523+
return _range_error(f, 1);
524524
memcpy(p, (char *)&x, sizeof x);
525525
return 0;
526526
}
@@ -621,8 +621,9 @@ bu_int(const char *p, const formatdef *f)
621621
{
622622
long x = 0;
623623
Py_ssize_t i = f->size;
624+
const unsigned char *bytes = (const unsigned char *)p;
624625
do {
625-
x = (x<<8) | (*p++ & 0xFF);
626+
x = (x<<8) | *bytes++;
626627
} while (--i > 0);
627628
/* Extend the sign bit. */
628629
if (SIZEOF_LONG > f->size)
@@ -635,8 +636,9 @@ bu_uint(const char *p, const formatdef *f)
635636
{
636637
unsigned long x = 0;
637638
Py_ssize_t i = f->size;
639+
const unsigned char *bytes = (const unsigned char *)p;
638640
do {
639-
x = (x<<8) | (*p++ & 0xFF);
641+
x = (x<<8) | *bytes++;
640642
} while (--i > 0);
641643
if (x <= LONG_MAX)
642644
return PyInt_FromLong((long)x);
@@ -649,8 +651,9 @@ bu_longlong(const char *p, const formatdef *f)
649651
#ifdef HAVE_LONG_LONG
650652
PY_LONG_LONG x = 0;
651653
Py_ssize_t i = f->size;
654+
const unsigned char *bytes = (const unsigned char *)p;
652655
do {
653-
x = (x<<8) | (*p++ & 0xFF);
656+
x = (x<<8) | *bytes++;
654657
} while (--i > 0);
655658
/* Extend the sign bit. */
656659
if (SIZEOF_LONG_LONG > f->size)
@@ -672,8 +675,9 @@ bu_ulonglong(const char *p, const formatdef *f)
672675
#ifdef HAVE_LONG_LONG
673676
unsigned PY_LONG_LONG x = 0;
674677
Py_ssize_t i = f->size;
678+
const unsigned char *bytes = (const unsigned char *)p;
675679
do {
676-
x = (x<<8) | (*p++ & 0xFF);
680+
x = (x<<8) | *bytes++;
677681
} while (--i > 0);
678682
if (x <= LONG_MAX)
679683
return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
@@ -708,10 +712,10 @@ bp_int(char *p, PyObject *v, const formatdef *f)
708712
i = f->size;
709713
if (i != SIZEOF_LONG) {
710714
if ((i == 2) && (x < -32768 || x > 32767))
711-
return _range_error(f->format, i, 0);
715+
return _range_error(f, 0);
712716
#if (SIZEOF_LONG != 4)
713717
else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
714-
return _range_error(f->format, i, 0);
718+
return _range_error(f, 0);
715719
#endif
716720
}
717721
do {
@@ -733,7 +737,7 @@ bp_uint(char *p, PyObject *v, const formatdef *f)
733737
unsigned long maxint = 1;
734738
maxint <<= (unsigned long)(i * 8);
735739
if (x >= maxint)
736-
return _range_error(f->format, f->size, 1);
740+
return _range_error(f, 1);
737741
}
738742
do {
739743
p[--i] = (char)x;
@@ -825,8 +829,9 @@ lu_int(const char *p, const formatdef *f)
825829
{
826830
long x = 0;
827831
Py_ssize_t i = f->size;
832+
const unsigned char *bytes = (const unsigned char *)p;
828833
do {
829-
x = (x<<8) | (p[--i] & 0xFF);
834+
x = (x<<8) | bytes[--i];
830835
} while (i > 0);
831836
/* Extend the sign bit. */
832837
if (SIZEOF_LONG > f->size)
@@ -839,8 +844,9 @@ lu_uint(const char *p, const formatdef *f)
839844
{
840845
unsigned long x = 0;
841846
Py_ssize_t i = f->size;
847+
const unsigned char *bytes = (const unsigned char *)p;
842848
do {
843-
x = (x<<8) | (p[--i] & 0xFF);
849+
x = (x<<8) | bytes[--i];
844850
} while (i > 0);
845851
if (x <= LONG_MAX)
846852
return PyInt_FromLong((long)x);
@@ -853,8 +859,9 @@ lu_longlong(const char *p, const formatdef *f)
853859
#ifdef HAVE_LONG_LONG
854860
PY_LONG_LONG x = 0;
855861
Py_ssize_t i = f->size;
862+
const unsigned char *bytes = (const unsigned char *)p;
856863
do {
857-
x = (x<<8) | (p[--i] & 0xFF);
864+
x = (x<<8) | bytes[--i];
858865
} while (i > 0);
859866
/* Extend the sign bit. */
860867
if (SIZEOF_LONG_LONG > f->size)
@@ -876,8 +883,9 @@ lu_ulonglong(const char *p, const formatdef *f)
876883
#ifdef HAVE_LONG_LONG
877884
unsigned PY_LONG_LONG x = 0;
878885
Py_ssize_t i = f->size;
886+
const unsigned char *bytes = (const unsigned char *)p;
879887
do {
880-
x = (x<<8) | (p[--i] & 0xFF);
888+
x = (x<<8) | bytes[--i];
881889
} while (i > 0);
882890
if (x <= LONG_MAX)
883891
return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
@@ -912,10 +920,10 @@ lp_int(char *p, PyObject *v, const formatdef *f)
912920
i = f->size;
913921
if (i != SIZEOF_LONG) {
914922
if ((i == 2) && (x < -32768 || x > 32767))
915-
return _range_error(f->format, i, 0);
923+
return _range_error(f, 0);
916924
#if (SIZEOF_LONG != 4)
917925
else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
918-
return _range_error(f->format, i, 0);
926+
return _range_error(f, 0);
919927
#endif
920928
}
921929
do {
@@ -937,7 +945,7 @@ lp_uint(char *p, PyObject *v, const formatdef *f)
937945
unsigned long maxint = 1;
938946
maxint <<= (unsigned long)(i * 8);
939947
if (x >= maxint)
940-
return _range_error(f->format, f->size, 1);
948+
return _range_error(f, 1);
941949
}
942950
do {
943951
*p++ = (char)x;

0 commit comments

Comments
 (0)