/* Helper to format the range error exceptions */
static int
-_range_error(char format, Py_ssize_t size, int is_unsigned)
+_range_error(formatdef *f, int is_unsigned)
{
if (is_unsigned == 0) {
long smallest = 0, largest = 0;
- Py_ssize_t i = size * 8;
+ Py_ssize_t i = f->size * 8;
while (--i > 0) {
smallest = (smallest * 2) - 1;
largest = (largest * 2) + 1;
}
PyErr_Format(StructError,
"'%c' format requires %ld <= number <= %ld",
- format,
+ f->format,
smallest,
largest);
} else {
unsigned long largest = 0;
- Py_ssize_t i = size * 8;
+ Py_ssize_t i = f->size * 8;
while (--i >= 0)
largest = (largest * 2) + 1;
PyErr_Format(StructError,
"'%c' format requires 0 <= number <= %lu",
- format,
+ f->format,
largest);
}
return -1;
return -1;
#if (SIZEOF_LONG > SIZEOF_INT)
if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
- return _range_error(f->format, sizeof(y), 0);
+ return _range_error(f, 0);
#endif
y = (int)x;
memcpy(p, (char *)&y, sizeof y);
unsigned long x;
unsigned int y;
if (get_ulong(v, &x) < 0)
- return _range_error(f->format, sizeof(y), 1);
+ return _range_error(f, 1);
y = (unsigned int)x;
#if (SIZEOF_LONG > SIZEOF_INT)
if (x > ((unsigned long)UINT_MAX))
- return _range_error(f->format, sizeof(y), 1);
+ return _range_error(f, 1);
#endif
memcpy(p, (char *)&y, sizeof y);
return 0;
{
unsigned long x;
if (get_ulong(v, &x) < 0)
- return _range_error(f->format, sizeof(x), 1);
+ return _range_error(f, 1);
memcpy(p, (char *)&x, sizeof x);
return 0;
}
{
long x = 0;
Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
do {
- x = (x<<8) | (*p++ & 0xFF);
+ x = (x<<8) | *bytes++;
} while (--i > 0);
/* Extend the sign bit. */
if (SIZEOF_LONG > f->size)
{
unsigned long x = 0;
Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
do {
- x = (x<<8) | (*p++ & 0xFF);
+ x = (x<<8) | *bytes++;
} while (--i > 0);
if (x <= LONG_MAX)
return PyInt_FromLong((long)x);
#ifdef HAVE_LONG_LONG
PY_LONG_LONG x = 0;
Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
do {
- x = (x<<8) | (*p++ & 0xFF);
+ x = (x<<8) | *bytes++;
} while (--i > 0);
/* Extend the sign bit. */
if (SIZEOF_LONG_LONG > f->size)
#ifdef HAVE_LONG_LONG
unsigned PY_LONG_LONG x = 0;
Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
do {
- x = (x<<8) | (*p++ & 0xFF);
+ x = (x<<8) | *bytes++;
} while (--i > 0);
if (x <= LONG_MAX)
return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
i = f->size;
if (i != SIZEOF_LONG) {
if ((i == 2) && (x < -32768 || x > 32767))
- return _range_error(f->format, i, 0);
+ return _range_error(f, 0);
#if (SIZEOF_LONG != 4)
else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
- return _range_error(f->format, i, 0);
+ return _range_error(f, 0);
#endif
}
do {
unsigned long maxint = 1;
maxint <<= (unsigned long)(i * 8);
if (x >= maxint)
- return _range_error(f->format, f->size, 1);
+ return _range_error(f, 1);
}
do {
p[--i] = (char)x;
{
long x = 0;
Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
do {
- x = (x<<8) | (p[--i] & 0xFF);
+ x = (x<<8) | bytes[--i];
} while (i > 0);
/* Extend the sign bit. */
if (SIZEOF_LONG > f->size)
{
unsigned long x = 0;
Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
do {
- x = (x<<8) | (p[--i] & 0xFF);
+ x = (x<<8) | bytes[--i];
} while (i > 0);
if (x <= LONG_MAX)
return PyInt_FromLong((long)x);
#ifdef HAVE_LONG_LONG
PY_LONG_LONG x = 0;
Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
do {
- x = (x<<8) | (p[--i] & 0xFF);
+ x = (x<<8) | bytes[--i];
} while (i > 0);
/* Extend the sign bit. */
if (SIZEOF_LONG_LONG > f->size)
#ifdef HAVE_LONG_LONG
unsigned PY_LONG_LONG x = 0;
Py_ssize_t i = f->size;
+ const unsigned char *bytes = (const unsigned char *)p;
do {
- x = (x<<8) | (p[--i] & 0xFF);
+ x = (x<<8) | bytes[--i];
} while (i > 0);
if (x <= LONG_MAX)
return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
i = f->size;
if (i != SIZEOF_LONG) {
if ((i == 2) && (x < -32768 || x > 32767))
- return _range_error(f->format, i, 0);
+ return _range_error(f, 0);
#if (SIZEOF_LONG != 4)
else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
- return _range_error(f->format, i, 0);
+ return _range_error(f, 0);
#endif
}
do {
unsigned long maxint = 1;
maxint <<= (unsigned long)(i * 8);
if (x >= maxint)
- return _range_error(f->format, f->size, 1);
+ return _range_error(f, 1);
}
do {
*p++ = (char)x;