enable all of the struct tests, use ssize_t, fix some whitespace

This commit is contained in:
Bob Ippolito 2006-05-26 20:25:23 +00:00
parent 5132407868
commit aa70a17e13
2 changed files with 68 additions and 59 deletions

View file

@ -323,7 +323,7 @@ class IntTester:
else: else:
# x is out of range -- verify pack realizes that. # x is out of range -- verify pack realizes that.
if code in self.BUGGY_RANGE_CHECK: if not PY_STRUCT_RANGE_CHECKING and code in self.BUGGY_RANGE_CHECK:
if verbose: if verbose:
print "Skipping buggy range check for code", code print "Skipping buggy range check for code", code
else: else:

View file

@ -3,6 +3,8 @@
/* New version supporting byte order, alignment and size options, /* New version supporting byte order, alignment and size options,
character strings, and unsigned numbers */ character strings, and unsigned numbers */
#define PY_SSIZE_T_CLEAN
#include "Python.h" #include "Python.h"
#include "structseq.h" #include "structseq.h"
#include "structmember.h" #include "structmember.h"
@ -29,8 +31,8 @@ typedef int Py_ssize_t;
/* The translation function for each format character is table driven */ /* The translation function for each format character is table driven */
typedef struct _formatdef { typedef struct _formatdef {
char format; char format;
int size; Py_ssize_t size;
int alignment; Py_ssize_t alignment;
PyObject* (*unpack)(const char *, PyObject* (*unpack)(const char *,
const struct _formatdef *); const struct _formatdef *);
int (*pack)(char *, PyObject *, int (*pack)(char *, PyObject *,
@ -39,16 +41,16 @@ typedef struct _formatdef {
typedef struct _formatcode { typedef struct _formatcode {
const struct _formatdef *fmtdef; const struct _formatdef *fmtdef;
int offset; Py_ssize_t offset;
int size; Py_ssize_t size;
} formatcode; } formatcode;
/* Struct object interface */ /* Struct object interface */
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
int s_size; Py_ssize_t s_size;
int s_len; Py_ssize_t s_len;
formatcode *s_codes; formatcode *s_codes;
PyObject *s_format; PyObject *s_format;
PyObject *weakreflist; /* List of weak references */ PyObject *weakreflist; /* List of weak references */
@ -233,11 +235,11 @@ unpack_double(const char *p, /* start of 8-byte string */
#ifdef PY_STRUCT_RANGE_CHECKING #ifdef PY_STRUCT_RANGE_CHECKING
/* Helper to format the range error exceptions */ /* Helper to format the range error exceptions */
static int static int
_range_error(char format, int size, int is_unsigned) _range_error(char format, Py_ssize_t size, int is_unsigned)
{ {
if (is_unsigned == 0) { if (is_unsigned == 0) {
long smallest = 0, largest = 0; long smallest = 0, largest = 0;
int i = size * 8; Py_ssize_t i = size * 8;
while (--i > 0) { while (--i > 0) {
smallest = (smallest * 2) - 1; smallest = (smallest * 2) - 1;
largest = (largest * 2) + 1; largest = (largest * 2) + 1;
@ -249,7 +251,7 @@ _range_error(char format, int size, int is_unsigned)
largest); largest);
} else { } else {
unsigned long largest = 0; unsigned long largest = 0;
int i = size * 8; Py_ssize_t i = size * 8;
while (--i >= 0) while (--i >= 0)
largest = (largest * 2) + 1; largest = (largest * 2) + 1;
PyErr_Format(StructError, PyErr_Format(StructError,
@ -643,7 +645,7 @@ static PyObject *
bu_int(const char *p, const formatdef *f) bu_int(const char *p, const formatdef *f)
{ {
long x = 0; long x = 0;
int i = f->size; Py_ssize_t i = f->size;
do { do {
x = (x<<8) | (*p++ & 0xFF); x = (x<<8) | (*p++ & 0xFF);
} while (--i > 0); } while (--i > 0);
@ -657,7 +659,7 @@ static PyObject *
bu_uint(const char *p, const formatdef *f) bu_uint(const char *p, const formatdef *f)
{ {
unsigned long x = 0; unsigned long x = 0;
int i = f->size; Py_ssize_t i = f->size;
do { do {
x = (x<<8) | (*p++ & 0xFF); x = (x<<8) | (*p++ & 0xFF);
} while (--i > 0); } while (--i > 0);
@ -676,7 +678,7 @@ bu_longlong(const char *p, const formatdef *f)
{ {
#if HAVE_LONG_LONG #if HAVE_LONG_LONG
PY_LONG_LONG x = 0; PY_LONG_LONG x = 0;
int i = f->size; Py_ssize_t i = f->size;
do { do {
x = (x<<8) | (*p++ & 0xFF); x = (x<<8) | (*p++ & 0xFF);
} while (--i > 0); } while (--i > 0);
@ -701,7 +703,7 @@ bu_ulonglong(const char *p, const formatdef *f)
{ {
#if HAVE_LONG_LONG #if HAVE_LONG_LONG
unsigned PY_LONG_LONG x = 0; unsigned PY_LONG_LONG x = 0;
int i = f->size; Py_ssize_t i = f->size;
do { do {
x = (x<<8) | (*p++ & 0xFF); x = (x<<8) | (*p++ & 0xFF);
} while (--i > 0); } while (--i > 0);
@ -734,7 +736,7 @@ static int
bp_int(char *p, PyObject *v, const formatdef *f) bp_int(char *p, PyObject *v, const formatdef *f)
{ {
long x; long x;
int i; Py_ssize_t i;
if (get_long(v, &x) < 0) if (get_long(v, &x) < 0)
return -1; return -1;
i = f->size; i = f->size;
@ -758,7 +760,7 @@ static int
bp_uint(char *p, PyObject *v, const formatdef *f) bp_uint(char *p, PyObject *v, const formatdef *f)
{ {
unsigned long x; unsigned long x;
int i; Py_ssize_t i;
if (get_ulong(v, &x) < 0) if (get_ulong(v, &x) < 0)
return -1; return -1;
i = f->size; i = f->size;
@ -855,7 +857,7 @@ static PyObject *
lu_int(const char *p, const formatdef *f) lu_int(const char *p, const formatdef *f)
{ {
long x = 0; long x = 0;
int i = f->size; Py_ssize_t i = f->size;
do { do {
x = (x<<8) | (p[--i] & 0xFF); x = (x<<8) | (p[--i] & 0xFF);
} while (i > 0); } while (i > 0);
@ -869,7 +871,7 @@ static PyObject *
lu_uint(const char *p, const formatdef *f) lu_uint(const char *p, const formatdef *f)
{ {
unsigned long x = 0; unsigned long x = 0;
int i = f->size; Py_ssize_t i = f->size;
do { do {
x = (x<<8) | (p[--i] & 0xFF); x = (x<<8) | (p[--i] & 0xFF);
} while (i > 0); } while (i > 0);
@ -888,7 +890,7 @@ lu_longlong(const char *p, const formatdef *f)
{ {
#if HAVE_LONG_LONG #if HAVE_LONG_LONG
PY_LONG_LONG x = 0; PY_LONG_LONG x = 0;
int i = f->size; Py_ssize_t i = f->size;
do { do {
x = (x<<8) | (p[--i] & 0xFF); x = (x<<8) | (p[--i] & 0xFF);
} while (i > 0); } while (i > 0);
@ -913,7 +915,7 @@ lu_ulonglong(const char *p, const formatdef *f)
{ {
#if HAVE_LONG_LONG #if HAVE_LONG_LONG
unsigned PY_LONG_LONG x = 0; unsigned PY_LONG_LONG x = 0;
int i = f->size; Py_ssize_t i = f->size;
do { do {
x = (x<<8) | (p[--i] & 0xFF); x = (x<<8) | (p[--i] & 0xFF);
} while (i > 0); } while (i > 0);
@ -946,7 +948,7 @@ static int
lp_int(char *p, PyObject *v, const formatdef *f) lp_int(char *p, PyObject *v, const formatdef *f)
{ {
long x; long x;
int i; Py_ssize_t i;
if (get_long(v, &x) < 0) if (get_long(v, &x) < 0)
return -1; return -1;
i = f->size; i = f->size;
@ -970,7 +972,7 @@ static int
lp_uint(char *p, PyObject *v, const formatdef *f) lp_uint(char *p, PyObject *v, const formatdef *f)
{ {
unsigned long x; unsigned long x;
int i; Py_ssize_t i;
if (get_ulong(v, &x) < 0) if (get_ulong(v, &x) < 0)
return -1; return -1;
i = f->size; i = f->size;
@ -1107,7 +1109,7 @@ getentry(int c, const formatdef *f)
/* Align a size according to a format code */ /* Align a size according to a format code */
static int static int
align(int size, int c, const formatdef *e) align(Py_ssize_t size, char c, const formatdef *e)
{ {
if (e->format == c) { if (e->format == c) {
if (e->alignment) { if (e->alignment) {
@ -1132,7 +1134,7 @@ prepare_s(PyStructObject *self)
const char *s; const char *s;
const char *fmt; const char *fmt;
char c; char c;
int size, len, num, itemsize, x; Py_ssize_t size, len, num, itemsize, x;
fmt = PyString_AS_STRING(self->s_format); fmt = PyString_AS_STRING(self->s_format);
@ -1345,7 +1347,7 @@ s_unpack(PyObject *self, PyObject *inputstr)
if (inputstr == NULL || !PyString_Check(inputstr) || if (inputstr == NULL || !PyString_Check(inputstr) ||
PyString_GET_SIZE(inputstr) != soself->s_size) { PyString_GET_SIZE(inputstr) != soself->s_size) {
PyErr_Format(StructError, PyErr_Format(StructError,
"unpack requires a string argument of length %d", soself->s_size); "unpack requires a string argument of length %zd", soself->s_size);
return NULL; return NULL;
} }
return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
@ -1389,7 +1391,7 @@ s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
if (offset < 0 || (buffer_len - offset) < soself->s_size) { if (offset < 0 || (buffer_len - offset) < soself->s_size) {
PyErr_Format(StructError, PyErr_Format(StructError,
"unpack_from requires a buffer of at least %d bytes", "unpack_from requires a buffer of at least %zd bytes",
soself->s_size); soself->s_size);
return NULL; return NULL;
} }
@ -1479,7 +1481,7 @@ s_pack(PyObject *self, PyObject *args)
PyTuple_GET_SIZE(args) != soself->s_len) PyTuple_GET_SIZE(args) != soself->s_len)
{ {
PyErr_Format(StructError, PyErr_Format(StructError,
"pack requires exactly %d arguments", soself->s_len); "pack requires exactly %zd arguments", soself->s_len);
return NULL; return NULL;
} }
@ -1520,7 +1522,7 @@ s_pack_to(PyObject *self, PyObject *args)
PyTuple_GET_SIZE(args) != (soself->s_len + 2)) PyTuple_GET_SIZE(args) != (soself->s_len + 2))
{ {
PyErr_Format(StructError, PyErr_Format(StructError,
"pack_to requires exactly %d arguments", "pack_to requires exactly %zd arguments",
(soself->s_len + 2)); (soself->s_len + 2));
return NULL; return NULL;
} }
@ -1542,7 +1544,7 @@ s_pack_to(PyObject *self, PyObject *args)
/* Check boundaries */ /* Check boundaries */
if (offset < 0 || (buffer_len - offset) < soself->s_size) { if (offset < 0 || (buffer_len - offset) < soself->s_size) {
PyErr_Format(StructError, PyErr_Format(StructError,
"pack_to requires a buffer of at least %d bytes", "pack_to requires a buffer of at least %zd bytes",
soself->s_size); soself->s_size);
return NULL; return NULL;
} }
@ -1555,6 +1557,18 @@ s_pack_to(PyObject *self, PyObject *args)
return Py_None; return Py_None;
} }
static PyObject *
s_get_format(PyStructObject *self, void *unused)
{
Py_INCREF(self->s_format);
return self->s_format;
}
static PyObject *
s_get_size(PyStructObject *self, void *unused)
{
return PyInt_FromSsize_t(self->s_size);
}
/* List of functions */ /* List of functions */
@ -1570,17 +1584,12 @@ PyDoc_STRVAR(s__doc__, "Compiled struct object");
#define OFF(x) offsetof(PyStructObject, x) #define OFF(x) offsetof(PyStructObject, x)
static PyMemberDef s_memberlist[] = { static PyGetSetDef s_getsetlist[] = {
{"format", T_OBJECT, OFF(s_format), RO, {"format", (getter)s_get_format, (setter)NULL, "buffer's capacity", NULL},
"struct format string"}, {"size", (getter)s_get_size, (setter)NULL, "buffer's position", NULL},
{"size", T_INT, OFF(s_size), RO, {NULL} /* sentinel */
"struct size in bytes"},
{"_len", T_INT, OFF(s_len), RO,
"number of items expected in tuple"},
{NULL} /* Sentinel */
}; };
static static
PyTypeObject PyStructType = { PyTypeObject PyStructType = {
PyObject_HEAD_INIT(NULL) PyObject_HEAD_INIT(NULL)
@ -1612,8 +1621,8 @@ PyTypeObject PyStructType = {
0, /* tp_iter */ 0, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */
s_methods, /* tp_methods */ s_methods, /* tp_methods */
s_memberlist, /* tp_members */ NULL, /* tp_members */
0, /* tp_getset */ s_getsetlist, /* tp_getset */
0, /* tp_base */ 0, /* tp_base */
0, /* tp_dict */ 0, /* tp_dict */
0, /* tp_descr_get */ 0, /* tp_descr_get */