Skip to content

Commit edadec0

Browse files
author
bob.ippolito
committed
Remove the range checking and int usage #defines from _struct and strip out the now-dead code
git-svn-id: http://svn.python.org/projects/python/trunk@46450 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 32789b0 commit edadec0

1 file changed

Lines changed: 8 additions & 63 deletions

File tree

Modules/_struct.c

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,6 @@ static PyTypeObject PyStructType;
1717
typedef int Py_ssize_t;
1818
#endif
1919

20-
21-
/* PY_USE_INT_WHEN_POSSIBLE is a flag that changes the
22-
struct API to return int instead of long when possible. This is
23-
often a significant performance improvement. */
24-
#define PY_USE_INT_WHEN_POSSIBLE 1
25-
26-
/* PY_STRUCT_RANGE_CHECKING performs range checking on all arguments
27-
to be packed. This will break some incorrect code that happened
28-
to accidentally do the right thing anyway (such as binhex). */
29-
#define PY_STRUCT_RANGE_CHECKING 1
30-
3120
/* The translation function for each format character is table driven */
3221
typedef struct _formatdef {
3322
char format;
@@ -232,7 +221,6 @@ unpack_double(const char *p, /* start of 8-byte string */
232221
return PyFloat_FromDouble(x);
233222
}
234223

235-
#ifdef PY_STRUCT_RANGE_CHECKING
236224
/* Helper to format the range error exceptions */
237225
static int
238226
_range_error(char format, Py_ssize_t size, int is_unsigned)
@@ -261,7 +249,6 @@ _range_error(char format, Py_ssize_t size, int is_unsigned)
261249
}
262250
return -1;
263251
}
264-
#endif
265252

266253

267254

@@ -331,10 +318,8 @@ nu_uint(const char *p, const formatdef *f)
331318
{
332319
unsigned int x;
333320
memcpy((char *)&x, p, sizeof x);
334-
#ifdef PY_USE_INT_WHEN_POSSIBLE
335321
if (x <= LONG_MAX)
336322
return PyInt_FromLong((long)x);
337-
#endif
338323
return PyLong_FromUnsignedLong((unsigned long)x);
339324
}
340325

@@ -351,10 +336,8 @@ nu_ulong(const char *p, const formatdef *f)
351336
{
352337
unsigned long x;
353338
memcpy((char *)&x, p, sizeof x);
354-
#ifdef PY_USE_INT_WHEN_POSSIBLE
355339
if (x <= LONG_MAX)
356340
return PyInt_FromLong((long)x);
357-
#endif
358341
return PyLong_FromUnsignedLong(x);
359342
}
360343

@@ -368,10 +351,8 @@ nu_longlong(const char *p, const formatdef *f)
368351
{
369352
PY_LONG_LONG x;
370353
memcpy((char *)&x, p, sizeof x);
371-
#ifdef PY_USE_INT_WHEN_POSSIBLE
372354
if (x >= LONG_MIN && x <= LONG_MAX)
373355
return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
374-
#endif
375356
return PyLong_FromLongLong(x);
376357
}
377358

@@ -380,10 +361,8 @@ nu_ulonglong(const char *p, const formatdef *f)
380361
{
381362
unsigned PY_LONG_LONG x;
382363
memcpy((char *)&x, p, sizeof x);
383-
#ifdef PY_USE_INT_WHEN_POSSIBLE
384364
if (x <= LONG_MAX)
385365
return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
386-
#endif
387366
return PyLong_FromUnsignedLongLong(x);
388367
}
389368

@@ -497,7 +476,7 @@ np_int(char *p, PyObject *v, const formatdef *f)
497476
int y;
498477
if (get_long(v, &x) < 0)
499478
return -1;
500-
#if defined(PY_STRUCT_RANGE_CHECKING) && (SIZEOF_LONG > SIZEOF_INT)
479+
#if (SIZEOF_LONG > SIZEOF_INT)
501480
if (x < INT_MIN || x > INT_MAX)
502481
return _range_error(f->format, sizeof(y), 0);
503482
#endif
@@ -512,13 +491,9 @@ np_uint(char *p, PyObject *v, const formatdef *f)
512491
unsigned long x;
513492
unsigned int y;
514493
if (get_ulong(v, &x) < 0)
515-
#ifdef PY_STRUCT_RANGE_CHECKING
516494
return _range_error(f->format, sizeof(y), 1);
517-
#else
518-
return -1;
519-
#endif
520495
y = (unsigned int)x;
521-
#if defined(PY_STRUCT_RANGE_CHECKING) && (SIZEOF_LONG > SIZEOF_INT)
496+
#if (SIZEOF_LONG > SIZEOF_INT)
522497
if (x > UINT_MAX)
523498
return _range_error(f->format, sizeof(y), 1);
524499
#endif
@@ -541,11 +516,7 @@ np_ulong(char *p, PyObject *v, const formatdef *f)
541516
{
542517
unsigned long x;
543518
if (get_ulong(v, &x) < 0)
544-
#ifdef PY_STRUCT_RANGE_CHECKING
545519
return _range_error(f->format, sizeof(x), 1);
546-
#else
547-
return -1;
548-
#endif
549520
memcpy(p, (char *)&x, sizeof x);
550521
return 0;
551522
}
@@ -663,20 +634,15 @@ bu_uint(const char *p, const formatdef *f)
663634
do {
664635
x = (x<<8) | (*p++ & 0xFF);
665636
} while (--i > 0);
666-
#ifdef PY_USE_INT_WHEN_POSSIBLE
667637
if (x <= LONG_MAX)
668638
return PyInt_FromLong((long)x);
669-
#else
670-
if (SIZEOF_LONG > f->size)
671-
return PyInt_FromLong((long)x);
672-
#endif
673639
return PyLong_FromUnsignedLong(x);
674640
}
675641

676642
static PyObject *
677643
bu_longlong(const char *p, const formatdef *f)
678644
{
679-
#if HAVE_LONG_LONG
645+
#ifdef HAVE_LONG_LONG
680646
PY_LONG_LONG x = 0;
681647
Py_ssize_t i = f->size;
682648
do {
@@ -685,10 +651,8 @@ bu_longlong(const char *p, const formatdef *f)
685651
/* Extend the sign bit. */
686652
if (SIZEOF_LONG_LONG > f->size)
687653
x |= -(x & (1L << (8 * f->size - 1)));
688-
#ifdef PY_USE_INT_WHEN_POSSIBLE
689654
if (x >= LONG_MIN && x <= LONG_MAX)
690655
return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
691-
#endif
692656
return PyLong_FromLongLong(x);
693657
#else
694658
return _PyLong_FromByteArray((const unsigned char *)p,
@@ -701,16 +665,14 @@ bu_longlong(const char *p, const formatdef *f)
701665
static PyObject *
702666
bu_ulonglong(const char *p, const formatdef *f)
703667
{
704-
#if HAVE_LONG_LONG
668+
#ifdef HAVE_LONG_LONG
705669
unsigned PY_LONG_LONG x = 0;
706670
Py_ssize_t i = f->size;
707671
do {
708672
x = (x<<8) | (*p++ & 0xFF);
709673
} while (--i > 0);
710-
#ifdef PY_USE_INT_WHEN_POSSIBLE
711674
if (x <= LONG_MAX)
712675
return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
713-
#endif
714676
return PyLong_FromUnsignedLongLong(x);
715677
#else
716678
return _PyLong_FromByteArray((const unsigned char *)p,
@@ -740,15 +702,13 @@ bp_int(char *p, PyObject *v, const formatdef *f)
740702
if (get_long(v, &x) < 0)
741703
return -1;
742704
i = f->size;
743-
#ifdef PY_STRUCT_RANGE_CHECKING
744705
if (i != SIZEOF_LONG && (
745706
(i == 2 && (x < -32768 || x > 32767))
746-
#if SIZEOF_LONG != 4
707+
#if (SIZEOF_LONG != 4)
747708
|| (i == 4) && (x < -2147483648L || x > -2147483647L)
748709
#endif
749710
))
750711
return _range_error(f->format, i, 0);
751-
#endif
752712
do {
753713
p[--i] = (char)x;
754714
x >>= 8;
@@ -764,10 +724,8 @@ bp_uint(char *p, PyObject *v, const formatdef *f)
764724
if (get_ulong(v, &x) < 0)
765725
return -1;
766726
i = f->size;
767-
#ifdef PY_STRUCT_RANGE_CHECKING
768727
if (i != SIZEOF_LONG && x >= (1U << (((unsigned int)i) * 8)))
769728
return _range_error(f->format, f->size, 1);
770-
#endif
771729
do {
772730
p[--i] = (char)x;
773731
x >>= 8;
@@ -875,20 +833,15 @@ lu_uint(const char *p, const formatdef *f)
875833
do {
876834
x = (x<<8) | (p[--i] & 0xFF);
877835
} while (i > 0);
878-
#ifdef PY_USE_INT_WHEN_POSSIBLE
879836
if (x <= LONG_MAX)
880837
return PyInt_FromLong((long)x);
881-
#else
882-
if (SIZEOF_LONG > f->size)
883-
return PyInt_FromLong((long)x);
884-
#endif
885838
return PyLong_FromUnsignedLong((long)x);
886839
}
887840

888841
static PyObject *
889842
lu_longlong(const char *p, const formatdef *f)
890843
{
891-
#if HAVE_LONG_LONG
844+
#ifdef HAVE_LONG_LONG
892845
PY_LONG_LONG x = 0;
893846
Py_ssize_t i = f->size;
894847
do {
@@ -897,10 +850,8 @@ lu_longlong(const char *p, const formatdef *f)
897850
/* Extend the sign bit. */
898851
if (SIZEOF_LONG_LONG > f->size)
899852
x |= -(x & (1L << (8 * f->size - 1)));
900-
#ifdef PY_USE_INT_WHEN_POSSIBLE
901853
if (x >= LONG_MIN && x <= LONG_MAX)
902854
return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
903-
#endif
904855
return PyLong_FromLongLong(x);
905856
#else
906857
return _PyLong_FromByteArray((const unsigned char *)p,
@@ -913,16 +864,14 @@ lu_longlong(const char *p, const formatdef *f)
913864
static PyObject *
914865
lu_ulonglong(const char *p, const formatdef *f)
915866
{
916-
#if HAVE_LONG_LONG
867+
#ifdef HAVE_LONG_LONG
917868
unsigned PY_LONG_LONG x = 0;
918869
Py_ssize_t i = f->size;
919870
do {
920871
x = (x<<8) | (p[--i] & 0xFF);
921872
} while (i > 0);
922-
#ifdef PY_USE_INT_WHEN_POSSIBLE
923873
if (x <= LONG_MAX)
924874
return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
925-
#endif
926875
return PyLong_FromUnsignedLongLong(x);
927876
#else
928877
return _PyLong_FromByteArray((const unsigned char *)p,
@@ -952,15 +901,13 @@ lp_int(char *p, PyObject *v, const formatdef *f)
952901
if (get_long(v, &x) < 0)
953902
return -1;
954903
i = f->size;
955-
#ifdef PY_STRUCT_RANGE_CHECKING
956904
if (i != SIZEOF_LONG && (
957905
(i == 2 && (x < -32768 || x > 32767))
958-
#if SIZEOF_LONG != 4
906+
#if (SIZEOF_LONG != 4)
959907
|| (i == 4) && (x < -2147483648L || x > -2147483647L)
960908
#endif
961909
))
962910
return _range_error(f->format, i, 0);
963-
#endif
964911
do {
965912
*p++ = (char)x;
966913
x >>= 8;
@@ -976,10 +923,8 @@ lp_uint(char *p, PyObject *v, const formatdef *f)
976923
if (get_ulong(v, &x) < 0)
977924
return -1;
978925
i = f->size;
979-
#ifdef PY_STRUCT_RANGE_CHECKING
980926
if (i != SIZEOF_LONG && x >= (1U << (((unsigned int)i) * 8)))
981927
return _range_error(f->format, f->size, 1);
982-
#endif
983928
do {
984929
*p++ = (char)x;
985930
x >>= 8;

0 commit comments

Comments
 (0)