mirror of
https://github.com/python/cpython.git
synced 2025-12-04 08:34:25 +00:00
bpo-30224: remove outdated checks in struct (#1374)
This commit is contained in:
parent
12b1c18098
commit
96f5028567
1 changed files with 15 additions and 47 deletions
|
|
@ -423,13 +423,7 @@ nu_uint(const char *p, const formatdef *f)
|
|||
{
|
||||
unsigned int x;
|
||||
memcpy((char *)&x, p, sizeof x);
|
||||
#if (SIZEOF_LONG > SIZEOF_INT)
|
||||
return PyLong_FromLong((long)x);
|
||||
#else
|
||||
if (x <= ((unsigned int)LONG_MAX))
|
||||
return PyLong_FromLong((long)x);
|
||||
return PyLong_FromUnsignedLong((unsigned long)x);
|
||||
#endif
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
|
|
@ -445,8 +439,6 @@ nu_ulong(const char *p, const formatdef *f)
|
|||
{
|
||||
unsigned long x;
|
||||
memcpy((char *)&x, p, sizeof x);
|
||||
if (x <= LONG_MAX)
|
||||
return PyLong_FromLong((long)x);
|
||||
return PyLong_FromUnsignedLong(x);
|
||||
}
|
||||
|
||||
|
|
@ -466,17 +458,11 @@ nu_size_t(const char *p, const formatdef *f)
|
|||
return PyLong_FromSize_t(x);
|
||||
}
|
||||
|
||||
|
||||
/* Native mode doesn't support q or Q unless the platform C supports
|
||||
long long (or, on Windows, __int64). */
|
||||
|
||||
static PyObject *
|
||||
nu_longlong(const char *p, const formatdef *f)
|
||||
{
|
||||
long long x;
|
||||
memcpy((char *)&x, p, sizeof x);
|
||||
if (x >= LONG_MIN && x <= LONG_MAX)
|
||||
return PyLong_FromLong(Py_SAFE_DOWNCAST(x, long long, long));
|
||||
return PyLong_FromLongLong(x);
|
||||
}
|
||||
|
||||
|
|
@ -485,8 +471,6 @@ nu_ulonglong(const char *p, const formatdef *f)
|
|||
{
|
||||
unsigned long long x;
|
||||
memcpy((char *)&x, p, sizeof x);
|
||||
if (x <= LONG_MAX)
|
||||
return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned long long, long));
|
||||
return PyLong_FromUnsignedLongLong(x);
|
||||
}
|
||||
|
||||
|
|
@ -566,12 +550,12 @@ np_ubyte(char *p, PyObject *v, const formatdef *f)
|
|||
static int
|
||||
np_char(char *p, PyObject *v, const formatdef *f)
|
||||
{
|
||||
if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
|
||||
if (!PyBytes_Check(v) || PyBytes_GET_SIZE(v) != 1) {
|
||||
PyErr_SetString(StructError,
|
||||
"char format requires a bytes object of length 1");
|
||||
return -1;
|
||||
}
|
||||
*p = *PyBytes_AsString(v);
|
||||
*p = *PyBytes_AS_STRING(v);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -821,8 +805,6 @@ bu_uint(const char *p, const formatdef *f)
|
|||
do {
|
||||
x = (x<<8) | *bytes++;
|
||||
} while (--i > 0);
|
||||
if (x <= LONG_MAX)
|
||||
return PyLong_FromLong((long)x);
|
||||
return PyLong_FromUnsignedLong(x);
|
||||
}
|
||||
|
||||
|
|
@ -838,8 +820,6 @@ bu_longlong(const char *p, const formatdef *f)
|
|||
/* Extend the sign bit. */
|
||||
if (SIZEOF_LONG_LONG > f->size)
|
||||
x |= -(x & ((long long)1 << ((8 * f->size) - 1)));
|
||||
if (x >= LONG_MIN && x <= LONG_MAX)
|
||||
return PyLong_FromLong(Py_SAFE_DOWNCAST(x, long long, long));
|
||||
return PyLong_FromLongLong(x);
|
||||
}
|
||||
|
||||
|
|
@ -852,8 +832,6 @@ bu_ulonglong(const char *p, const formatdef *f)
|
|||
do {
|
||||
x = (x<<8) | *bytes++;
|
||||
} while (--i > 0);
|
||||
if (x <= LONG_MAX)
|
||||
return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned long long, long));
|
||||
return PyLong_FromUnsignedLongLong(x);
|
||||
}
|
||||
|
||||
|
|
@ -878,9 +856,7 @@ bu_double(const char *p, const formatdef *f)
|
|||
static PyObject *
|
||||
bu_bool(const char *p, const formatdef *f)
|
||||
{
|
||||
char x;
|
||||
memcpy((char *)&x, p, sizeof x);
|
||||
return PyBool_FromLong(x != 0);
|
||||
return PyBool_FromLong(*p != 0);
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
@ -1048,9 +1024,7 @@ lu_uint(const char *p, const formatdef *f)
|
|||
do {
|
||||
x = (x<<8) | bytes[--i];
|
||||
} while (i > 0);
|
||||
if (x <= LONG_MAX)
|
||||
return PyLong_FromLong((long)x);
|
||||
return PyLong_FromUnsignedLong((long)x);
|
||||
return PyLong_FromUnsignedLong(x);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
|
|
@ -1065,8 +1039,6 @@ lu_longlong(const char *p, const formatdef *f)
|
|||
/* Extend the sign bit. */
|
||||
if (SIZEOF_LONG_LONG > f->size)
|
||||
x |= -(x & ((long long)1 << ((8 * f->size) - 1)));
|
||||
if (x >= LONG_MIN && x <= LONG_MAX)
|
||||
return PyLong_FromLong(Py_SAFE_DOWNCAST(x, long long, long));
|
||||
return PyLong_FromLongLong(x);
|
||||
}
|
||||
|
||||
|
|
@ -1079,8 +1051,6 @@ lu_ulonglong(const char *p, const formatdef *f)
|
|||
do {
|
||||
x = (x<<8) | bytes[--i];
|
||||
} while (i > 0);
|
||||
if (x <= LONG_MAX)
|
||||
return PyLong_FromLong(Py_SAFE_DOWNCAST(x, unsigned long long, long));
|
||||
return PyLong_FromUnsignedLongLong(x);
|
||||
}
|
||||
|
||||
|
|
@ -1390,8 +1360,6 @@ prepare_s(PyStructObject *self)
|
|||
num = c - '0';
|
||||
while ('0' <= (c = *s++) && c <= '9')
|
||||
num = num*10 + (c - '0');
|
||||
if (c == '\0')
|
||||
break;
|
||||
}
|
||||
else
|
||||
num = 1;
|
||||
|
|
@ -1486,7 +1454,7 @@ Struct___init___impl(PyStructObject *self, PyObject *format)
|
|||
return -1;
|
||||
}
|
||||
|
||||
Py_XSETREF(self->s_format, format);
|
||||
Py_SETREF(self->s_format, format);
|
||||
|
||||
ret = prepare_s(self);
|
||||
return ret;
|
||||
|
|
@ -1500,7 +1468,7 @@ s_dealloc(PyStructObject *s)
|
|||
if (s->s_codes != NULL) {
|
||||
PyMem_FREE(s->s_codes);
|
||||
}
|
||||
Py_XDECREF(s->s_format);
|
||||
Py_DECREF(s->s_format);
|
||||
Py_TYPE(s)->tp_free((PyObject *)s);
|
||||
}
|
||||
|
||||
|
|
@ -1864,7 +1832,7 @@ s_pack(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* Allocate a new string */
|
||||
/* Allocate a new buffer */
|
||||
result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size);
|
||||
if (result == NULL)
|
||||
return NULL;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue