mirror of
https://github.com/python/cpython.git
synced 2025-07-19 09:15:34 +00:00
ANSI-fication
This commit is contained in:
parent
416d413527
commit
41c36ffe83
4 changed files with 70 additions and 213 deletions
|
@ -84,9 +84,7 @@ typedef struct { char c; void *x; } s_void_p;
|
|||
if it isn't one */
|
||||
|
||||
static int
|
||||
get_long(v, p)
|
||||
PyObject *v;
|
||||
long *p;
|
||||
get_long(PyObject *v, long *p)
|
||||
{
|
||||
long x = PyInt_AsLong(v);
|
||||
if (x == -1 && PyErr_Occurred()) {
|
||||
|
@ -103,9 +101,7 @@ get_long(v, p)
|
|||
/* Same, but handling unsigned long */
|
||||
|
||||
static int
|
||||
get_ulong(v, p)
|
||||
PyObject *v;
|
||||
unsigned long *p;
|
||||
get_ulong(PyObject *v, unsigned long *p)
|
||||
{
|
||||
if (PyLong_Check(v)) {
|
||||
unsigned long x = PyLong_AsUnsignedLong(v);
|
||||
|
@ -129,10 +125,9 @@ get_ulong(v, p)
|
|||
/* XXX Inf/NaN are not handled quite right (but underflow is!) */
|
||||
|
||||
static int
|
||||
pack_float(x, p, incr)
|
||||
double x; /* The number to pack */
|
||||
char *p; /* Where to pack the high order byte */
|
||||
int incr; /* 1 for big-endian; -1 for little-endian */
|
||||
pack_float(double x, /* The number to pack */
|
||||
char *p, /* Where to pack the high order byte */
|
||||
int incr) /* 1 for big-endian; -1 for little-endian */
|
||||
{
|
||||
int s;
|
||||
int e;
|
||||
|
@ -201,10 +196,9 @@ pack_float(x, p, incr)
|
|||
}
|
||||
|
||||
static int
|
||||
pack_double(x, p, incr)
|
||||
double x; /* The number to pack */
|
||||
char *p; /* Where to pack the high order byte */
|
||||
int incr; /* 1 for big-endian; -1 for little-endian */
|
||||
pack_double(double x, /* The number to pack */
|
||||
char *p, /* Where to pack the high order byte */
|
||||
int incr) /* 1 for big-endian; -1 for little-endian */
|
||||
{
|
||||
int s;
|
||||
int e;
|
||||
|
@ -294,9 +288,8 @@ pack_double(x, p, incr)
|
|||
}
|
||||
|
||||
static PyObject *
|
||||
unpack_float(p, incr)
|
||||
char *p; /* Where the high order byte is */
|
||||
int incr; /* 1 for big-endian; -1 for little-endian */
|
||||
unpack_float(const char *p, /* Where the high order byte is */
|
||||
int incr) /* 1 for big-endian; -1 for little-endian */
|
||||
{
|
||||
int s;
|
||||
int e;
|
||||
|
@ -338,9 +331,8 @@ unpack_float(p, incr)
|
|||
}
|
||||
|
||||
static PyObject *
|
||||
unpack_double(p, incr)
|
||||
char *p; /* Where the high order byte is */
|
||||
int incr; /* 1 for big-endian; -1 for little-endian */
|
||||
unpack_double(const char *p, /* Where the high order byte is */
|
||||
int incr) /* 1 for big-endian; -1 for little-endian */
|
||||
{
|
||||
int s;
|
||||
int e;
|
||||
|
@ -413,82 +405,62 @@ typedef struct _formatdef {
|
|||
} formatdef;
|
||||
|
||||
static PyObject *
|
||||
nu_char(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
nu_char(const char *p, const formatdef *f)
|
||||
{
|
||||
return PyString_FromStringAndSize(p, 1);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
nu_byte(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
nu_byte(const char *p, const formatdef *f)
|
||||
{
|
||||
return PyInt_FromLong((long) *(signed char *)p);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
nu_ubyte(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
nu_ubyte(const char *p, const formatdef *f)
|
||||
{
|
||||
return PyInt_FromLong((long) *(unsigned char *)p);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
nu_short(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
nu_short(const char *p, const formatdef *f)
|
||||
{
|
||||
return PyInt_FromLong((long) *(short *)p);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
nu_ushort(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
nu_ushort(const char *p, const formatdef *f)
|
||||
{
|
||||
return PyInt_FromLong((long) *(unsigned short *)p);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
nu_int(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
nu_int(const char *p, const formatdef *f)
|
||||
{
|
||||
return PyInt_FromLong((long) *(int *)p);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
nu_uint(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
nu_uint(const char *p, const formatdef *f)
|
||||
{
|
||||
unsigned int x = *(unsigned int *)p;
|
||||
return PyLong_FromUnsignedLong((unsigned long)x);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
nu_long(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
nu_long(const char *p, const formatdef *f)
|
||||
{
|
||||
return PyInt_FromLong(*(long *)p);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
nu_ulong(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
nu_ulong(const char *p, const formatdef *f)
|
||||
{
|
||||
return PyLong_FromUnsignedLong(*(unsigned long *)p);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
nu_float(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
nu_float(const char *p, const formatdef *f)
|
||||
{
|
||||
float x;
|
||||
memcpy((char *)&x, p, sizeof(float));
|
||||
|
@ -496,9 +468,7 @@ nu_float(p, f)
|
|||
}
|
||||
|
||||
static PyObject *
|
||||
nu_double(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
nu_double(const char *p, const formatdef *f)
|
||||
{
|
||||
double x;
|
||||
memcpy((char *)&x, p, sizeof(double));
|
||||
|
@ -506,18 +476,13 @@ nu_double(p, f)
|
|||
}
|
||||
|
||||
static PyObject *
|
||||
nu_void_p(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
nu_void_p(const char *p, const formatdef *f)
|
||||
{
|
||||
return PyLong_FromVoidPtr(*(void **)p);
|
||||
}
|
||||
|
||||
static int
|
||||
np_byte(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
np_byte(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
long x;
|
||||
if (get_long(v, &x) < 0)
|
||||
|
@ -527,10 +492,7 @@ np_byte(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
np_char(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
np_char(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
if (!PyString_Check(v) || PyString_Size(v) != 1) {
|
||||
PyErr_SetString(StructError,
|
||||
|
@ -542,10 +504,7 @@ np_char(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
np_short(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
np_short(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
long x;
|
||||
if (get_long(v, &x) < 0)
|
||||
|
@ -555,10 +514,7 @@ np_short(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
np_int(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
np_int(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
long x;
|
||||
if (get_long(v, &x) < 0)
|
||||
|
@ -568,10 +524,7 @@ np_int(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
np_uint(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
np_uint(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
unsigned long x;
|
||||
if (get_ulong(v, &x) < 0)
|
||||
|
@ -581,10 +534,7 @@ np_uint(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
np_long(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
np_long(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
long x;
|
||||
if (get_long(v, &x) < 0)
|
||||
|
@ -594,10 +544,7 @@ np_long(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
np_ulong(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
np_ulong(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
unsigned long x;
|
||||
if (get_ulong(v, &x) < 0)
|
||||
|
@ -607,10 +554,7 @@ np_ulong(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
np_float(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
np_float(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
float x = (float)PyFloat_AsDouble(v);
|
||||
if (x == -1 && PyErr_Occurred()) {
|
||||
|
@ -623,10 +567,7 @@ np_float(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
np_double(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
np_double(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
double x = PyFloat_AsDouble(v);
|
||||
if (x == -1 && PyErr_Occurred()) {
|
||||
|
@ -639,10 +580,7 @@ np_double(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
np_void_p(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
np_void_p(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
void *x = PyLong_AsVoidPtr(v);
|
||||
if (x == NULL && PyErr_Occurred()) {
|
||||
|
@ -676,9 +614,7 @@ static formatdef native_table[] = {
|
|||
};
|
||||
|
||||
static PyObject *
|
||||
bu_int(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
bu_int(const char *p, const formatdef *f)
|
||||
{
|
||||
long x = 0;
|
||||
int i = f->size;
|
||||
|
@ -694,9 +630,7 @@ bu_int(p, f)
|
|||
}
|
||||
|
||||
static PyObject *
|
||||
bu_uint(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
bu_uint(const char *p, const formatdef *f)
|
||||
{
|
||||
unsigned long x = 0;
|
||||
int i = f->size;
|
||||
|
@ -710,26 +644,19 @@ bu_uint(p, f)
|
|||
}
|
||||
|
||||
static PyObject *
|
||||
bu_float(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
bu_float(const char *p, const formatdef *f)
|
||||
{
|
||||
return unpack_float(p, 1);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bu_double(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
bu_double(const char *p, const formatdef *f)
|
||||
{
|
||||
return unpack_double(p, 1);
|
||||
}
|
||||
|
||||
static int
|
||||
bp_int(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
bp_int(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
long x;
|
||||
int i;
|
||||
|
@ -744,10 +671,7 @@ bp_int(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
bp_uint(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
bp_uint(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
unsigned long x;
|
||||
int i;
|
||||
|
@ -762,10 +686,7 @@ bp_uint(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
bp_float(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
bp_float(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
double x = PyFloat_AsDouble(v);
|
||||
if (x == -1 && PyErr_Occurred()) {
|
||||
|
@ -777,10 +698,7 @@ bp_float(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
bp_double(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
bp_double(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
double x = PyFloat_AsDouble(v);
|
||||
if (x == -1 && PyErr_Occurred()) {
|
||||
|
@ -810,9 +728,7 @@ static formatdef bigendian_table[] = {
|
|||
};
|
||||
|
||||
static PyObject *
|
||||
lu_int(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
lu_int(const char *p, const formatdef *f)
|
||||
{
|
||||
long x = 0;
|
||||
int i = f->size;
|
||||
|
@ -828,9 +744,7 @@ lu_int(p, f)
|
|||
}
|
||||
|
||||
static PyObject *
|
||||
lu_uint(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
lu_uint(const char *p, const formatdef *f)
|
||||
{
|
||||
unsigned long x = 0;
|
||||
int i = f->size;
|
||||
|
@ -844,26 +758,19 @@ lu_uint(p, f)
|
|||
}
|
||||
|
||||
static PyObject *
|
||||
lu_float(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
lu_float(const char *p, const formatdef *f)
|
||||
{
|
||||
return unpack_float(p+3, -1);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
lu_double(p, f)
|
||||
const char *p;
|
||||
const formatdef *f;
|
||||
lu_double(const char *p, const formatdef *f)
|
||||
{
|
||||
return unpack_double(p+7, -1);
|
||||
}
|
||||
|
||||
static int
|
||||
lp_int(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
lp_int(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
long x;
|
||||
int i;
|
||||
|
@ -878,10 +785,7 @@ lp_int(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
lp_uint(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
lp_uint(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
unsigned long x;
|
||||
int i;
|
||||
|
@ -896,10 +800,7 @@ lp_uint(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
lp_float(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
lp_float(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
double x = PyFloat_AsDouble(v);
|
||||
if (x == -1 && PyErr_Occurred()) {
|
||||
|
@ -911,10 +812,7 @@ lp_float(p, v, f)
|
|||
}
|
||||
|
||||
static int
|
||||
lp_double(p, v, f)
|
||||
char *p;
|
||||
PyObject *v;
|
||||
const formatdef *f;
|
||||
lp_double(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
double x = PyFloat_AsDouble(v);
|
||||
if (x == -1 && PyErr_Occurred()) {
|
||||
|
@ -945,8 +843,7 @@ static formatdef lilendian_table[] = {
|
|||
|
||||
|
||||
static const formatdef *
|
||||
whichtable(pfmt)
|
||||
const char **pfmt;
|
||||
whichtable(char **pfmt)
|
||||
{
|
||||
const char *fmt = (*pfmt)++; /* May be backed out of later */
|
||||
switch (*fmt) {
|
||||
|
@ -975,9 +872,7 @@ whichtable(pfmt)
|
|||
/* Get the table entry for a format code */
|
||||
|
||||
static const formatdef *
|
||||
getentry(c, f)
|
||||
int c;
|
||||
const formatdef *f;
|
||||
getentry(int c, const formatdef *f)
|
||||
{
|
||||
for (; f->format != '\0'; f++) {
|
||||
if (f->format == c) {
|
||||
|
@ -992,10 +887,7 @@ getentry(c, f)
|
|||
/* Align a size according to a format code */
|
||||
|
||||
static int
|
||||
align(size, c, e)
|
||||
int size;
|
||||
int c;
|
||||
const formatdef *e;
|
||||
align(int size, int c, const formatdef *e)
|
||||
{
|
||||
if (e->format == c) {
|
||||
if (e->alignment) {
|
||||
|
@ -1011,9 +903,7 @@ align(size, c, e)
|
|||
/* calculate the size of a format string */
|
||||
|
||||
static int
|
||||
calcsize(fmt, f)
|
||||
const char *fmt;
|
||||
const formatdef *f;
|
||||
calcsize(const char *fmt, const formatdef *f)
|
||||
{
|
||||
const formatdef *e;
|
||||
const char *s;
|
||||
|
@ -1067,9 +957,7 @@ Return size of C struct described by format string fmt.\n\
|
|||
See struct.__doc__ for more on format strings.";
|
||||
|
||||
static PyObject *
|
||||
struct_calcsize(self, args)
|
||||
PyObject *self; /* Not used */
|
||||
PyObject *args;
|
||||
struct_calcsize(PyObject *self, PyObject *args)
|
||||
{
|
||||
char *fmt;
|
||||
const formatdef *f;
|
||||
|
@ -1091,9 +979,7 @@ Return string containing values v1, v2, ... packed according to fmt.\n\
|
|||
See struct.__doc__ for more on format strings.";
|
||||
|
||||
static PyObject *
|
||||
struct_pack(self, args)
|
||||
PyObject *self; /* Not used */
|
||||
PyObject *args;
|
||||
struct_pack(PyObject *self, PyObject *args)
|
||||
{
|
||||
const formatdef *f, *e;
|
||||
PyObject *format, *result, *v;
|
||||
|
@ -1231,9 +1117,7 @@ to fmt. Requires len(string)==calcsize(fmt).\n\
|
|||
See struct.__doc__ for more on format strings.";
|
||||
|
||||
static PyObject *
|
||||
struct_unpack(self, args)
|
||||
PyObject *self; /* Not used */
|
||||
PyObject *args;
|
||||
struct_unpack(PyObject *self, PyObject *args)
|
||||
{
|
||||
const formatdef *f, *e;
|
||||
char *str, *start, *fmt, *s;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue