mirror of
https://github.com/python/cpython.git
synced 2025-07-29 22:24:49 +00:00
enable all of the struct tests, use ssize_t, fix some whitespace
This commit is contained in:
parent
5132407868
commit
aa70a17e13
2 changed files with 68 additions and 59 deletions
|
@ -323,7 +323,7 @@ class IntTester:
|
|||
|
||||
else:
|
||||
# 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:
|
||||
print "Skipping buggy range check for code", code
|
||||
else:
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
/* New version supporting byte order, alignment and size options,
|
||||
character strings, and unsigned numbers */
|
||||
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
|
||||
#include "Python.h"
|
||||
#include "structseq.h"
|
||||
#include "structmember.h"
|
||||
|
@ -29,8 +31,8 @@ typedef int Py_ssize_t;
|
|||
/* The translation function for each format character is table driven */
|
||||
typedef struct _formatdef {
|
||||
char format;
|
||||
int size;
|
||||
int alignment;
|
||||
Py_ssize_t size;
|
||||
Py_ssize_t alignment;
|
||||
PyObject* (*unpack)(const char *,
|
||||
const struct _formatdef *);
|
||||
int (*pack)(char *, PyObject *,
|
||||
|
@ -39,16 +41,16 @@ typedef struct _formatdef {
|
|||
|
||||
typedef struct _formatcode {
|
||||
const struct _formatdef *fmtdef;
|
||||
int offset;
|
||||
int size;
|
||||
Py_ssize_t offset;
|
||||
Py_ssize_t size;
|
||||
} formatcode;
|
||||
|
||||
/* Struct object interface */
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
int s_size;
|
||||
int s_len;
|
||||
Py_ssize_t s_size;
|
||||
Py_ssize_t s_len;
|
||||
formatcode *s_codes;
|
||||
PyObject *s_format;
|
||||
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
|
||||
/* Helper to format the range error exceptions */
|
||||
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) {
|
||||
long smallest = 0, largest = 0;
|
||||
int i = size * 8;
|
||||
Py_ssize_t i = size * 8;
|
||||
while (--i > 0) {
|
||||
smallest = (smallest * 2) - 1;
|
||||
largest = (largest * 2) + 1;
|
||||
|
@ -249,7 +251,7 @@ _range_error(char format, int size, int is_unsigned)
|
|||
largest);
|
||||
} else {
|
||||
unsigned long largest = 0;
|
||||
int i = size * 8;
|
||||
Py_ssize_t i = size * 8;
|
||||
while (--i >= 0)
|
||||
largest = (largest * 2) + 1;
|
||||
PyErr_Format(StructError,
|
||||
|
@ -643,7 +645,7 @@ static PyObject *
|
|||
bu_int(const char *p, const formatdef *f)
|
||||
{
|
||||
long x = 0;
|
||||
int i = f->size;
|
||||
Py_ssize_t i = f->size;
|
||||
do {
|
||||
x = (x<<8) | (*p++ & 0xFF);
|
||||
} while (--i > 0);
|
||||
|
@ -657,7 +659,7 @@ static PyObject *
|
|||
bu_uint(const char *p, const formatdef *f)
|
||||
{
|
||||
unsigned long x = 0;
|
||||
int i = f->size;
|
||||
Py_ssize_t i = f->size;
|
||||
do {
|
||||
x = (x<<8) | (*p++ & 0xFF);
|
||||
} while (--i > 0);
|
||||
|
@ -676,7 +678,7 @@ bu_longlong(const char *p, const formatdef *f)
|
|||
{
|
||||
#if HAVE_LONG_LONG
|
||||
PY_LONG_LONG x = 0;
|
||||
int i = f->size;
|
||||
Py_ssize_t i = f->size;
|
||||
do {
|
||||
x = (x<<8) | (*p++ & 0xFF);
|
||||
} while (--i > 0);
|
||||
|
@ -701,7 +703,7 @@ bu_ulonglong(const char *p, const formatdef *f)
|
|||
{
|
||||
#if HAVE_LONG_LONG
|
||||
unsigned PY_LONG_LONG x = 0;
|
||||
int i = f->size;
|
||||
Py_ssize_t i = f->size;
|
||||
do {
|
||||
x = (x<<8) | (*p++ & 0xFF);
|
||||
} while (--i > 0);
|
||||
|
@ -734,7 +736,7 @@ static int
|
|||
bp_int(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
long x;
|
||||
int i;
|
||||
Py_ssize_t i;
|
||||
if (get_long(v, &x) < 0)
|
||||
return -1;
|
||||
i = f->size;
|
||||
|
@ -758,7 +760,7 @@ static int
|
|||
bp_uint(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
unsigned long x;
|
||||
int i;
|
||||
Py_ssize_t i;
|
||||
if (get_ulong(v, &x) < 0)
|
||||
return -1;
|
||||
i = f->size;
|
||||
|
@ -855,7 +857,7 @@ static PyObject *
|
|||
lu_int(const char *p, const formatdef *f)
|
||||
{
|
||||
long x = 0;
|
||||
int i = f->size;
|
||||
Py_ssize_t i = f->size;
|
||||
do {
|
||||
x = (x<<8) | (p[--i] & 0xFF);
|
||||
} while (i > 0);
|
||||
|
@ -869,7 +871,7 @@ static PyObject *
|
|||
lu_uint(const char *p, const formatdef *f)
|
||||
{
|
||||
unsigned long x = 0;
|
||||
int i = f->size;
|
||||
Py_ssize_t i = f->size;
|
||||
do {
|
||||
x = (x<<8) | (p[--i] & 0xFF);
|
||||
} while (i > 0);
|
||||
|
@ -888,7 +890,7 @@ lu_longlong(const char *p, const formatdef *f)
|
|||
{
|
||||
#if HAVE_LONG_LONG
|
||||
PY_LONG_LONG x = 0;
|
||||
int i = f->size;
|
||||
Py_ssize_t i = f->size;
|
||||
do {
|
||||
x = (x<<8) | (p[--i] & 0xFF);
|
||||
} while (i > 0);
|
||||
|
@ -913,7 +915,7 @@ lu_ulonglong(const char *p, const formatdef *f)
|
|||
{
|
||||
#if HAVE_LONG_LONG
|
||||
unsigned PY_LONG_LONG x = 0;
|
||||
int i = f->size;
|
||||
Py_ssize_t i = f->size;
|
||||
do {
|
||||
x = (x<<8) | (p[--i] & 0xFF);
|
||||
} while (i > 0);
|
||||
|
@ -946,7 +948,7 @@ static int
|
|||
lp_int(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
long x;
|
||||
int i;
|
||||
Py_ssize_t i;
|
||||
if (get_long(v, &x) < 0)
|
||||
return -1;
|
||||
i = f->size;
|
||||
|
@ -970,7 +972,7 @@ static int
|
|||
lp_uint(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
unsigned long x;
|
||||
int i;
|
||||
Py_ssize_t i;
|
||||
if (get_ulong(v, &x) < 0)
|
||||
return -1;
|
||||
i = f->size;
|
||||
|
@ -1107,7 +1109,7 @@ getentry(int c, const formatdef *f)
|
|||
/* Align a size according to a format code */
|
||||
|
||||
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->alignment) {
|
||||
|
@ -1132,7 +1134,7 @@ prepare_s(PyStructObject *self)
|
|||
const char *s;
|
||||
const char *fmt;
|
||||
char c;
|
||||
int size, len, num, itemsize, x;
|
||||
Py_ssize_t size, len, num, itemsize, x;
|
||||
|
||||
fmt = PyString_AS_STRING(self->s_format);
|
||||
|
||||
|
@ -1345,7 +1347,7 @@ s_unpack(PyObject *self, PyObject *inputstr)
|
|||
if (inputstr == NULL || !PyString_Check(inputstr) ||
|
||||
PyString_GET_SIZE(inputstr) != soself->s_size) {
|
||||
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 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) {
|
||||
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);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1479,7 +1481,7 @@ s_pack(PyObject *self, PyObject *args)
|
|||
PyTuple_GET_SIZE(args) != soself->s_len)
|
||||
{
|
||||
PyErr_Format(StructError,
|
||||
"pack requires exactly %d arguments", soself->s_len);
|
||||
"pack requires exactly %zd arguments", soself->s_len);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -1520,7 +1522,7 @@ s_pack_to(PyObject *self, PyObject *args)
|
|||
PyTuple_GET_SIZE(args) != (soself->s_len + 2))
|
||||
{
|
||||
PyErr_Format(StructError,
|
||||
"pack_to requires exactly %d arguments",
|
||||
"pack_to requires exactly %zd arguments",
|
||||
(soself->s_len + 2));
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1542,7 +1544,7 @@ s_pack_to(PyObject *self, PyObject *args)
|
|||
/* Check boundaries */
|
||||
if (offset < 0 || (buffer_len - offset) < soself->s_size) {
|
||||
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);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1555,6 +1557,18 @@ s_pack_to(PyObject *self, PyObject *args)
|
|||
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 */
|
||||
|
||||
|
@ -1570,17 +1584,12 @@ PyDoc_STRVAR(s__doc__, "Compiled struct object");
|
|||
|
||||
#define OFF(x) offsetof(PyStructObject, x)
|
||||
|
||||
static PyMemberDef s_memberlist[] = {
|
||||
{"format", T_OBJECT, OFF(s_format), RO,
|
||||
"struct format string"},
|
||||
{"size", T_INT, OFF(s_size), RO,
|
||||
"struct size in bytes"},
|
||||
{"_len", T_INT, OFF(s_len), RO,
|
||||
"number of items expected in tuple"},
|
||||
{NULL} /* Sentinel */
|
||||
static PyGetSetDef s_getsetlist[] = {
|
||||
{"format", (getter)s_get_format, (setter)NULL, "buffer's capacity", NULL},
|
||||
{"size", (getter)s_get_size, (setter)NULL, "buffer's position", NULL},
|
||||
{NULL} /* sentinel */
|
||||
};
|
||||
|
||||
|
||||
static
|
||||
PyTypeObject PyStructType = {
|
||||
PyObject_HEAD_INIT(NULL)
|
||||
|
@ -1612,8 +1621,8 @@ PyTypeObject PyStructType = {
|
|||
0, /* tp_iter */
|
||||
0, /* tp_iternext */
|
||||
s_methods, /* tp_methods */
|
||||
s_memberlist, /* tp_members */
|
||||
0, /* tp_getset */
|
||||
NULL, /* tp_members */
|
||||
s_getsetlist, /* tp_getset */
|
||||
0, /* tp_base */
|
||||
0, /* tp_dict */
|
||||
0, /* tp_descr_get */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue