summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na92@gmail.com>2020-11-19 22:47:57 +0900
committerGitHub <noreply@github.com>2020-11-19 22:47:57 +0900
commit56e190d749fb6acee832e358c1121ece83b13346 (patch)
tree49309f502a12cfece7b30190f3f36a8ab1143d46
parentcfeb5437a8910e28726422a14a93a36584e32238 (diff)
downloadcpython-git-revert-23398-bpo-1635741-struct.tar.gz
Revert "bpo-1635741: Port _struct to multiphase initialization (GH-23398)"revert-23398-bpo-1635741-struct
This reverts commit cfeb5437a8910e28726422a14a93a36584e32238.
-rw-r--r--Misc/NEWS.d/next/C API/2020-11-19-12-06-43.bpo-1635741.KEfZpn.rst1
-rw-r--r--Modules/_struct.c498
-rw-r--r--Modules/clinic/_struct.c.h10
3 files changed, 238 insertions, 271 deletions
diff --git a/Misc/NEWS.d/next/C API/2020-11-19-12-06-43.bpo-1635741.KEfZpn.rst b/Misc/NEWS.d/next/C API/2020-11-19-12-06-43.bpo-1635741.KEfZpn.rst
deleted file mode 100644
index fc64757e00..0000000000
--- a/Misc/NEWS.d/next/C API/2020-11-19-12-06-43.bpo-1635741.KEfZpn.rst
+++ /dev/null
@@ -1 +0,0 @@
-Port _struct extension module to multiphase initialization (:pep:`489`)
diff --git a/Modules/_struct.c b/Modules/_struct.c
index 8dfcd882a4..81cdbb9b81 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -15,7 +15,6 @@ class Struct "PyStructObject *" "&PyStructType"
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9b032058a83ed7c3]*/
typedef struct {
- PyObject *cache;
PyObject *PyStructType;
PyObject *unpackiter_type;
PyObject *StructError;
@@ -31,19 +30,16 @@ get_struct_state(PyObject *module)
static struct PyModuleDef _structmodule;
-#define get_struct_state_structinst(self) \
- (get_struct_state(_PyType_GetModuleByDef(Py_TYPE(self), &_structmodule)))
-#define get_struct_state_iterinst(self) \
- (get_struct_state(PyType_GetModule(Py_TYPE(self))))
+#define _structmodulestate_global get_struct_state(PyState_FindModule(&_structmodule))
/* The translation function for each format character is table driven */
typedef struct _formatdef {
char format;
Py_ssize_t size;
Py_ssize_t alignment;
- PyObject* (*unpack)(_structmodulestate *, const char *,
+ PyObject* (*unpack)(const char *,
const struct _formatdef *);
- int (*pack)(_structmodulestate *, char *, PyObject *,
+ int (*pack)(char *, PyObject *,
const struct _formatdef *);
} formatdef;
@@ -65,7 +61,10 @@ typedef struct {
PyObject *weakreflist; /* List of weak references */
} PyStructObject;
-#define PyStruct_Check(op, state) PyObject_TypeCheck(op, (PyTypeObject *)(state)->PyStructType)
+
+#define PyStruct_Check(op) PyObject_TypeCheck(op, (PyTypeObject *)_structmodulestate_global->PyStructType)
+#define PyStruct_CheckExact(op) Py_IS_TYPE(op, (PyTypeObject *)_structmodulestate_global->PyStructType)
+
/* Define various structs to figure out the alignments of types */
@@ -103,20 +102,12 @@ class cache_struct_converter(CConverter):
converter = 'cache_struct_converter'
c_default = "NULL"
- def parse_arg(self, argname, displayname):
- return """
- if (!{converter}(module, {argname}, &{paramname})) {{{{
- goto exit;
- }}}}
- """.format(argname=argname, paramname=self.name,
- converter=self.converter)
-
def cleanup(self):
return "Py_XDECREF(%s);\n" % self.name
[python start generated code]*/
-/*[python end generated code: output=da39a3ee5e6b4b0d input=d6746621c2fb1a7d]*/
+/*[python end generated code: output=da39a3ee5e6b4b0d input=49957cca130ffb63]*/
-static int cache_struct_converter(PyObject *, PyObject *, PyStructObject **);
+static int cache_struct_converter(PyObject *, PyStructObject **);
#include "clinic/_struct.c.h"
@@ -124,7 +115,7 @@ static int cache_struct_converter(PyObject *, PyObject *, PyStructObject **);
PyLongObject if possible, otherwise fails. Caller should decref. */
static PyObject *
-get_pylong(_structmodulestate *state, PyObject *v)
+get_pylong(PyObject *v)
{
assert(v != NULL);
if (!PyLong_Check(v)) {
@@ -135,7 +126,7 @@ get_pylong(_structmodulestate *state, PyObject *v)
return NULL;
}
else {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"required argument is not an integer");
return NULL;
}
@@ -151,11 +142,11 @@ get_pylong(_structmodulestate *state, PyObject *v)
one */
static int
-get_long(_structmodulestate *state, PyObject *v, long *p)
+get_long(PyObject *v, long *p)
{
long x;
- v = get_pylong(state, v);
+ v = get_pylong(v);
if (v == NULL)
return -1;
assert(PyLong_Check(v));
@@ -163,7 +154,7 @@ get_long(_structmodulestate *state, PyObject *v, long *p)
Py_DECREF(v);
if (x == (long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"argument out of range");
return -1;
}
@@ -175,11 +166,11 @@ get_long(_structmodulestate *state, PyObject *v, long *p)
/* Same, but handling unsigned long */
static int
-get_ulong(_structmodulestate *state, PyObject *v, unsigned long *p)
+get_ulong(PyObject *v, unsigned long *p)
{
unsigned long x;
- v = get_pylong(state, v);
+ v = get_pylong(v);
if (v == NULL)
return -1;
assert(PyLong_Check(v));
@@ -187,7 +178,7 @@ get_ulong(_structmodulestate *state, PyObject *v, unsigned long *p)
Py_DECREF(v);
if (x == (unsigned long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"argument out of range");
return -1;
}
@@ -198,11 +189,11 @@ get_ulong(_structmodulestate *state, PyObject *v, unsigned long *p)
/* Same, but handling native long long. */
static int
-get_longlong(_structmodulestate *state, PyObject *v, long long *p)
+get_longlong(PyObject *v, long long *p)
{
long long x;
- v = get_pylong(state, v);
+ v = get_pylong(v);
if (v == NULL)
return -1;
assert(PyLong_Check(v));
@@ -210,7 +201,7 @@ get_longlong(_structmodulestate *state, PyObject *v, long long *p)
Py_DECREF(v);
if (x == (long long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"argument out of range");
return -1;
}
@@ -221,11 +212,11 @@ get_longlong(_structmodulestate *state, PyObject *v, long long *p)
/* Same, but handling native unsigned long long. */
static int
-get_ulonglong(_structmodulestate *state, PyObject *v, unsigned long long *p)
+get_ulonglong(PyObject *v, unsigned long long *p)
{
unsigned long long x;
- v = get_pylong(state, v);
+ v = get_pylong(v);
if (v == NULL)
return -1;
assert(PyLong_Check(v));
@@ -233,7 +224,7 @@ get_ulonglong(_structmodulestate *state, PyObject *v, unsigned long long *p)
Py_DECREF(v);
if (x == (unsigned long long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"argument out of range");
return -1;
}
@@ -244,11 +235,11 @@ get_ulonglong(_structmodulestate *state, PyObject *v, unsigned long long *p)
/* Same, but handling Py_ssize_t */
static int
-get_ssize_t(_structmodulestate *state, PyObject *v, Py_ssize_t *p)
+get_ssize_t(PyObject *v, Py_ssize_t *p)
{
Py_ssize_t x;
- v = get_pylong(state, v);
+ v = get_pylong(v);
if (v == NULL)
return -1;
assert(PyLong_Check(v));
@@ -256,7 +247,7 @@ get_ssize_t(_structmodulestate *state, PyObject *v, Py_ssize_t *p)
Py_DECREF(v);
if (x == (Py_ssize_t)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"argument out of range");
return -1;
}
@@ -267,11 +258,11 @@ get_ssize_t(_structmodulestate *state, PyObject *v, Py_ssize_t *p)
/* Same, but handling size_t */
static int
-get_size_t(_structmodulestate *state, PyObject *v, size_t *p)
+get_size_t(PyObject *v, size_t *p)
{
size_t x;
- v = get_pylong(state, v);
+ v = get_pylong(v);
if (v == NULL)
return -1;
assert(PyLong_Check(v));
@@ -279,7 +270,7 @@ get_size_t(_structmodulestate *state, PyObject *v, size_t *p)
Py_DECREF(v);
if (x == (size_t)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"argument out of range");
return -1;
}
@@ -288,7 +279,7 @@ get_size_t(_structmodulestate *state, PyObject *v, size_t *p)
}
-#define RANGE_ERROR(state, x, f, flag, mask) return _range_error(state, f, flag)
+#define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
/* Floating point helpers */
@@ -307,14 +298,13 @@ unpack_halffloat(const char *p, /* start of 2-byte string */
}
static int
-pack_halffloat(_structmodulestate *state,
- char *p, /* start of 2-byte string */
+pack_halffloat(char *p, /* start of 2-byte string */
PyObject *v, /* value to pack */
int le) /* true for little-endian, false for big-endian */
{
double x = PyFloat_AsDouble(v);
if (x == -1.0 && PyErr_Occurred()) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"required argument is not a float");
return -1;
}
@@ -347,7 +337,7 @@ unpack_double(const char *p, /* start of 8-byte string */
/* Helper to format the range error exceptions */
static int
-_range_error(_structmodulestate *state, const formatdef *f, int is_unsigned)
+_range_error(const formatdef *f, int is_unsigned)
{
/* ulargest is the largest unsigned value with f->size bytes.
* Note that the simpler:
@@ -360,13 +350,13 @@ _range_error(_structmodulestate *state, const formatdef *f, int is_unsigned)
const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
if (is_unsigned)
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"'%c' format requires 0 <= number <= %zu",
f->format,
ulargest);
else {
const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"'%c' format requires %zd <= number <= %zd",
f->format,
~ largest,
@@ -398,25 +388,25 @@ _range_error(_structmodulestate *state, const formatdef *f, int is_unsigned)
does this). */
static PyObject *
-nu_char(_structmodulestate *state, const char *p, const formatdef *f)
+nu_char(const char *p, const formatdef *f)
{
return PyBytes_FromStringAndSize(p, 1);
}
static PyObject *
-nu_byte(_structmodulestate *state, const char *p, const formatdef *f)
+nu_byte(const char *p, const formatdef *f)
{
return PyLong_FromLong((long) *(signed char *)p);
}
static PyObject *
-nu_ubyte(_structmodulestate *state, const char *p, const formatdef *f)
+nu_ubyte(const char *p, const formatdef *f)
{
return PyLong_FromLong((long) *(unsigned char *)p);
}
static PyObject *
-nu_short(_structmodulestate *state, const char *p, const formatdef *f)
+nu_short(const char *p, const formatdef *f)
{
short x;
memcpy((char *)&x, p, sizeof x);
@@ -424,7 +414,7 @@ nu_short(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_ushort(_structmodulestate *state, const char *p, const formatdef *f)
+nu_ushort(const char *p, const formatdef *f)
{
unsigned short x;
memcpy((char *)&x, p, sizeof x);
@@ -432,7 +422,7 @@ nu_ushort(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_int(_structmodulestate *state, const char *p, const formatdef *f)
+nu_int(const char *p, const formatdef *f)
{
int x;
memcpy((char *)&x, p, sizeof x);
@@ -440,7 +430,7 @@ nu_int(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_uint(_structmodulestate *state, const char *p, const formatdef *f)
+nu_uint(const char *p, const formatdef *f)
{
unsigned int x;
memcpy((char *)&x, p, sizeof x);
@@ -448,7 +438,7 @@ nu_uint(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_long(_structmodulestate *state, const char *p, const formatdef *f)
+nu_long(const char *p, const formatdef *f)
{
long x;
memcpy((char *)&x, p, sizeof x);
@@ -456,7 +446,7 @@ nu_long(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_ulong(_structmodulestate *state, const char *p, const formatdef *f)
+nu_ulong(const char *p, const formatdef *f)
{
unsigned long x;
memcpy((char *)&x, p, sizeof x);
@@ -464,7 +454,7 @@ nu_ulong(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_ssize_t(_structmodulestate *state, const char *p, const formatdef *f)
+nu_ssize_t(const char *p, const formatdef *f)
{
Py_ssize_t x;
memcpy((char *)&x, p, sizeof x);
@@ -472,7 +462,7 @@ nu_ssize_t(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_size_t(_structmodulestate *state, const char *p, const formatdef *f)
+nu_size_t(const char *p, const formatdef *f)
{
size_t x;
memcpy((char *)&x, p, sizeof x);
@@ -480,7 +470,7 @@ nu_size_t(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_longlong(_structmodulestate *state, const char *p, const formatdef *f)
+nu_longlong(const char *p, const formatdef *f)
{
long long x;
memcpy((char *)&x, p, sizeof x);
@@ -488,7 +478,7 @@ nu_longlong(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_ulonglong(_structmodulestate *state, const char *p, const formatdef *f)
+nu_ulonglong(const char *p, const formatdef *f)
{
unsigned long long x;
memcpy((char *)&x, p, sizeof x);
@@ -496,7 +486,7 @@ nu_ulonglong(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_bool(_structmodulestate *state, const char *p, const formatdef *f)
+nu_bool(const char *p, const formatdef *f)
{
_Bool x;
memcpy((char *)&x, p, sizeof x);
@@ -505,7 +495,7 @@ nu_bool(_structmodulestate *state, const char *p, const formatdef *f)
static PyObject *
-nu_halffloat(_structmodulestate *state, const char *p, const formatdef *f)
+nu_halffloat(const char *p, const formatdef *f)
{
#if PY_LITTLE_ENDIAN
return unpack_halffloat(p, 1);
@@ -515,7 +505,7 @@ nu_halffloat(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_float(_structmodulestate *state, const char *p, const formatdef *f)
+nu_float(const char *p, const formatdef *f)
{
float x;
memcpy((char *)&x, p, sizeof x);
@@ -523,7 +513,7 @@ nu_float(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_double(_structmodulestate *state, const char *p, const formatdef *f)
+nu_double(const char *p, const formatdef *f)
{
double x;
memcpy((char *)&x, p, sizeof x);
@@ -531,7 +521,7 @@ nu_double(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-nu_void_p(_structmodulestate *state, const char *p, const formatdef *f)
+nu_void_p(const char *p, const formatdef *f)
{
void *x;
memcpy((char *)&x, p, sizeof x);
@@ -539,13 +529,13 @@ nu_void_p(_structmodulestate *state, const char *p, const formatdef *f)
}
static int
-np_byte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_byte(char *p, PyObject *v, const formatdef *f)
{
long x;
- if (get_long(state, v, &x) < 0)
+ if (get_long(v, &x) < 0)
return -1;
if (x < -128 || x > 127) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"byte format requires -128 <= number <= 127");
return -1;
}
@@ -554,13 +544,13 @@ np_byte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-np_ubyte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_ubyte(char *p, PyObject *v, const formatdef *f)
{
long x;
- if (get_long(state, v, &x) < 0)
+ if (get_long(v, &x) < 0)
return -1;
if (x < 0 || x > 255) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"ubyte format requires 0 <= number <= 255");
return -1;
}
@@ -569,10 +559,10 @@ np_ubyte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-np_char(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_char(char *p, PyObject *v, const formatdef *f)
{
if (!PyBytes_Check(v) || PyBytes_Size(v) != 1) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"char format requires a bytes object of length 1");
return -1;
}
@@ -581,14 +571,14 @@ np_char(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-np_short(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_short(char *p, PyObject *v, const formatdef *f)
{
long x;
short y;
- if (get_long(state, v, &x) < 0)
+ if (get_long(v, &x) < 0)
return -1;
if (x < SHRT_MIN || x > SHRT_MAX) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"short format requires " Py_STRINGIFY(SHRT_MIN)
" <= number <= " Py_STRINGIFY(SHRT_MAX));
return -1;
@@ -599,14 +589,14 @@ np_short(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-np_ushort(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_ushort(char *p, PyObject *v, const formatdef *f)
{
long x;
unsigned short y;
- if (get_long(state, v, &x) < 0)
+ if (get_long(v, &x) < 0)
return -1;
if (x < 0 || x > USHRT_MAX) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"ushort format requires 0 <= number <= "
Py_STRINGIFY(USHRT_MAX));
return -1;
@@ -617,15 +607,15 @@ np_ushort(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-np_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_int(char *p, PyObject *v, const formatdef *f)
{
long x;
int y;
- if (get_long(state, v, &x) < 0)
+ if (get_long(v, &x) < 0)
return -1;
#if (SIZEOF_LONG > SIZEOF_INT)
if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
- RANGE_ERROR(state, x, f, 0, -1);
+ RANGE_ERROR(x, f, 0, -1);
#endif
y = (int)x;
memcpy(p, (char *)&y, sizeof y);
@@ -633,76 +623,76 @@ np_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-np_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_uint(char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
unsigned int y;
- if (get_ulong(state, v, &x) < 0)
+ if (get_ulong(v, &x) < 0)
return -1;
y = (unsigned int)x;
#if (SIZEOF_LONG > SIZEOF_INT)
if (x > ((unsigned long)UINT_MAX))
- RANGE_ERROR(state, y, f, 1, -1);
+ RANGE_ERROR(y, f, 1, -1);
#endif
memcpy(p, (char *)&y, sizeof y);
return 0;
}
static int
-np_long(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_long(char *p, PyObject *v, const formatdef *f)
{
long x;
- if (get_long(state, v, &x) < 0)
+ if (get_long(v, &x) < 0)
return -1;
memcpy(p, (char *)&x, sizeof x);
return 0;
}
static int
-np_ulong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_ulong(char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
- if (get_ulong(state, v, &x) < 0)
+ if (get_ulong(v, &x) < 0)
return -1;
memcpy(p, (char *)&x, sizeof x);
return 0;
}
static int
-np_ssize_t(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_ssize_t(char *p, PyObject *v, const formatdef *f)
{
Py_ssize_t x;
- if (get_ssize_t(state, v, &x) < 0)
+ if (get_ssize_t(v, &x) < 0)
return -1;
memcpy(p, (char *)&x, sizeof x);
return 0;
}
static int
-np_size_t(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_size_t(char *p, PyObject *v, const formatdef *f)
{
size_t x;
- if (get_size_t(state, v, &x) < 0)
+ if (get_size_t(v, &x) < 0)
return -1;
memcpy(p, (char *)&x, sizeof x);
return 0;
}
static int
-np_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_longlong(char *p, PyObject *v, const formatdef *f)
{
long long x;
- if (get_longlong(state, v, &x) < 0)
+ if (get_longlong(v, &x) < 0)
return -1;
memcpy(p, (char *)&x, sizeof x);
return 0;
}
static int
-np_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_ulonglong(char *p, PyObject *v, const formatdef *f)
{
unsigned long long x;
- if (get_ulonglong(state, v, &x) < 0)
+ if (get_ulonglong(v, &x) < 0)
return -1;
memcpy(p, (char *)&x, sizeof x);
return 0;
@@ -710,7 +700,7 @@ np_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f
static int
-np_bool(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_bool(char *p, PyObject *v, const formatdef *f)
{
int y;
_Bool x;
@@ -723,21 +713,21 @@ np_bool(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-np_halffloat(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_halffloat(char *p, PyObject *v, const formatdef *f)
{
#if PY_LITTLE_ENDIAN
- return pack_halffloat(state, p, v, 1);
+ return pack_halffloat(p, v, 1);
#else
- return pack_halffloat(statem p, v, 0);
+ return pack_halffloat(p, v, 0);
#endif
}
static int
-np_float(_structmodulestate *state, 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()) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"required argument is not a float");
return -1;
}
@@ -746,11 +736,11 @@ np_float(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-np_double(_structmodulestate *state, 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()) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"required argument is not a float");
return -1;
}
@@ -759,11 +749,11 @@ np_double(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-np_void_p(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+np_void_p(char *p, PyObject *v, const formatdef *f)
{
void *x;
- v = get_pylong(state, v);
+ v = get_pylong(v);
if (v == NULL)
return -1;
assert(PyLong_Check(v));
@@ -803,7 +793,7 @@ static const formatdef native_table[] = {
/* Big-endian routines. *****************************************************/
static PyObject *
-bu_int(_structmodulestate *state, const char *p, const formatdef *f)
+bu_int(const char *p, const formatdef *f)
{
long x = 0;
Py_ssize_t i = f->size;
@@ -818,7 +808,7 @@ bu_int(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-bu_uint(_structmodulestate *state, const char *p, const formatdef *f)
+bu_uint(const char *p, const formatdef *f)
{
unsigned long x = 0;
Py_ssize_t i = f->size;
@@ -830,7 +820,7 @@ bu_uint(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-bu_longlong(_structmodulestate *state, const char *p, const formatdef *f)
+bu_longlong(const char *p, const formatdef *f)
{
long long x = 0;
Py_ssize_t i = f->size;
@@ -845,7 +835,7 @@ bu_longlong(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-bu_ulonglong(_structmodulestate *state, const char *p, const formatdef *f)
+bu_ulonglong(const char *p, const formatdef *f)
{
unsigned long long x = 0;
Py_ssize_t i = f->size;
@@ -857,44 +847,44 @@ bu_ulonglong(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-bu_halffloat(_structmodulestate *state, const char *p, const formatdef *f)
+bu_halffloat(const char *p, const formatdef *f)
{
return unpack_halffloat(p, 0);
}
static PyObject *
-bu_float(_structmodulestate *state, const char *p, const formatdef *f)
+bu_float(const char *p, const formatdef *f)
{
return unpack_float(p, 0);
}
static PyObject *
-bu_double(_structmodulestate *state, const char *p, const formatdef *f)
+bu_double(const char *p, const formatdef *f)
{
return unpack_double(p, 0);
}
static PyObject *
-bu_bool(_structmodulestate *state, const char *p, const formatdef *f)
+bu_bool(const char *p, const formatdef *f)
{
return PyBool_FromLong(*p != 0);
}
static int
-bp_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+bp_int(char *p, PyObject *v, const formatdef *f)
{
long x;
Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
- if (get_long(state, v, &x) < 0)
+ if (get_long(v, &x) < 0)
return -1;
i = f->size;
if (i != SIZEOF_LONG) {
if ((i == 2) && (x < -32768 || x > 32767))
- RANGE_ERROR(state, x, f, 0, 0xffffL);
+ RANGE_ERROR(x, f, 0, 0xffffL);
#if (SIZEOF_LONG != 4)
else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
- RANGE_ERROR(state, x, f, 0, 0xffffffffL);
+ RANGE_ERROR(x, f, 0, 0xffffffffL);
#endif
}
do {
@@ -905,19 +895,19 @@ bp_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-bp_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+bp_uint(char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
- if (get_ulong(state, v, &x) < 0)
+ if (get_ulong(v, &x) < 0)
return -1;
i = f->size;
if (i != SIZEOF_LONG) {
unsigned long maxint = 1;
maxint <<= (unsigned long)(i * 8);
if (x >= maxint)
- RANGE_ERROR(state, x, f, 1, maxint - 1);
+ RANGE_ERROR(x, f, 1, maxint - 1);
}
do {
q[--i] = (unsigned char)(x & 0xffUL);
@@ -927,10 +917,10 @@ bp_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-bp_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+bp_longlong(char *p, PyObject *v, const formatdef *f)
{
int res;
- v = get_pylong(state, v);
+ v = get_pylong(v);
if (v == NULL)
return -1;
res = _PyLong_AsByteArray((PyLongObject *)v,
@@ -943,10 +933,10 @@ bp_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-bp_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+bp_ulonglong(char *p, PyObject *v, const formatdef *f)
{
int res;
- v = get_pylong(state, v);
+ v = get_pylong(v);
if (v == NULL)
return -1;
res = _PyLong_AsByteArray((PyLongObject *)v,
@@ -959,17 +949,17 @@ bp_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f
}
static int
-bp_halffloat(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+bp_halffloat(char *p, PyObject *v, const formatdef *f)
{
- return pack_halffloat(state, p, v, 0);
+ return pack_halffloat(p, v, 0);
}
static int
-bp_float(_structmodulestate *state, 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()) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"required argument is not a float");
return -1;
}
@@ -977,11 +967,11 @@ bp_float(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-bp_double(_structmodulestate *state, 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()) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"required argument is not a float");
return -1;
}
@@ -989,7 +979,7 @@ bp_double(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-bp_bool(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+bp_bool(char *p, PyObject *v, const formatdef *f)
{
int y;
y = PyObject_IsTrue(v);
@@ -1024,7 +1014,7 @@ static formatdef bigendian_table[] = {
/* Little-endian routines. *****************************************************/
static PyObject *
-lu_int(_structmodulestate *state, const char *p, const formatdef *f)
+lu_int(const char *p, const formatdef *f)
{
long x = 0;
Py_ssize_t i = f->size;
@@ -1039,7 +1029,7 @@ lu_int(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-lu_uint(_structmodulestate *state, const char *p, const formatdef *f)
+lu_uint(const char *p, const formatdef *f)
{
unsigned long x = 0;
Py_ssize_t i = f->size;
@@ -1051,7 +1041,7 @@ lu_uint(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-lu_longlong(_structmodulestate *state, const char *p, const formatdef *f)
+lu_longlong(const char *p, const formatdef *f)
{
long long x = 0;
Py_ssize_t i = f->size;
@@ -1066,7 +1056,7 @@ lu_longlong(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-lu_ulonglong(_structmodulestate *state, const char *p, const formatdef *f)
+lu_ulonglong(const char *p, const formatdef *f)
{
unsigned long long x = 0;
Py_ssize_t i = f->size;
@@ -1078,38 +1068,38 @@ lu_ulonglong(_structmodulestate *state, const char *p, const formatdef *f)
}
static PyObject *
-lu_halffloat(_structmodulestate *state, const char *p, const formatdef *f)
+lu_halffloat(const char *p, const formatdef *f)
{
return unpack_halffloat(p, 1);
}
static PyObject *
-lu_float(_structmodulestate *state, const char *p, const formatdef *f)
+lu_float(const char *p, const formatdef *f)
{
return unpack_float(p, 1);
}
static PyObject *
-lu_double(_structmodulestate *state, const char *p, const formatdef *f)
+lu_double(const char *p, const formatdef *f)
{
return unpack_double(p, 1);
}
static int
-lp_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+lp_int(char *p, PyObject *v, const formatdef *f)
{
long x;
Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
- if (get_long(state, v, &x) < 0)
+ if (get_long(v, &x) < 0)
return -1;
i = f->size;
if (i != SIZEOF_LONG) {
if ((i == 2) && (x < -32768 || x > 32767))
- RANGE_ERROR(state, x, f, 0, 0xffffL);
+ RANGE_ERROR(x, f, 0, 0xffffL);
#if (SIZEOF_LONG != 4)
else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
- RANGE_ERROR(state, x, f, 0, 0xffffffffL);
+ RANGE_ERROR(x, f, 0, 0xffffffffL);
#endif
}
do {
@@ -1120,19 +1110,19 @@ lp_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-lp_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+lp_uint(char *p, PyObject *v, const formatdef *f)
{
unsigned long x;
Py_ssize_t i;
unsigned char *q = (unsigned char *)p;
- if (get_ulong(state, v, &x) < 0)
+ if (get_ulong(v, &x) < 0)
return -1;
i = f->size;
if (i != SIZEOF_LONG) {
unsigned long maxint = 1;
maxint <<= (unsigned long)(i * 8);
if (x >= maxint)
- RANGE_ERROR(state, x, f, 1, maxint - 1);
+ RANGE_ERROR(x, f, 1, maxint - 1);
}
do {
*q++ = (unsigned char)(x & 0xffUL);
@@ -1142,10 +1132,10 @@ lp_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-lp_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+lp_longlong(char *p, PyObject *v, const formatdef *f)
{
int res;
- v = get_pylong(state, v);
+ v = get_pylong(v);
if (v == NULL)
return -1;
res = _PyLong_AsByteArray((PyLongObject*)v,
@@ -1158,10 +1148,10 @@ lp_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-lp_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+lp_ulonglong(char *p, PyObject *v, const formatdef *f)
{
int res;
- v = get_pylong(state, v);
+ v = get_pylong(v);
if (v == NULL)
return -1;
res = _PyLong_AsByteArray((PyLongObject*)v,
@@ -1174,17 +1164,17 @@ lp_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f
}
static int
-lp_halffloat(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
+lp_halffloat(char *p, PyObject *v, const formatdef *f)
{
- return pack_halffloat(state, p, v, 1);
+ return pack_halffloat(p, v, 1);
}
static int
-lp_float(_structmodulestate *state, 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()) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"required argument is not a float");
return -1;
}
@@ -1192,11 +1182,11 @@ lp_float(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
}
static int
-lp_double(_structmodulestate *state, 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()) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"required argument is not a float");
return -1;
}
@@ -1256,14 +1246,14 @@ whichtable(const char **pfmt)
/* Get the table entry for a format code */
static const formatdef *
-getentry(_structmodulestate *state, int c, const formatdef *f)
+getentry(int c, const formatdef *f)
{
for (; f->format != '\0'; f++) {
if (f->format == c) {
return f;
}
}
- PyErr_SetString(state->StructError, "bad char in struct format");
+ PyErr_SetString(_structmodulestate_global->StructError, "bad char in struct format");
return NULL;
}
@@ -1305,11 +1295,9 @@ prepare_s(PyStructObject *self)
Py_ssize_t size, len, num, itemsize;
size_t ncodes;
- _structmodulestate *state = get_struct_state_structinst(self);
-
fmt = PyBytes_AS_STRING(self->s_format);
if (strlen(fmt) != (size_t)PyBytes_GET_SIZE(self->s_format)) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"embedded null character");
return -1;
}
@@ -1335,7 +1323,7 @@ prepare_s(PyStructObject *self)
num = num*10 + (c - '0');
}
if (c == '\0') {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"repeat count given without format specifier");
return -1;
}
@@ -1343,7 +1331,7 @@ prepare_s(PyStructObject *self)
else
num = 1;
- e = getentry(state, c, f);
+ e = getentry(c, f);
if (e == NULL)
return -1;
@@ -1396,7 +1384,7 @@ prepare_s(PyStructObject *self)
else
num = 1;
- e = getentry(state, c, f);
+ e = getentry(c, f);
size = align(size, c, e);
if (c == 's' || c == 'p') {
@@ -1425,7 +1413,7 @@ prepare_s(PyStructObject *self)
return 0;
overflow:
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"total struct size too long");
return -1;
}
@@ -1511,8 +1499,7 @@ s_dealloc(PyStructObject *s)
}
static PyObject *
-s_unpack_internal(PyStructObject *soself, const char *startfrom,
- _structmodulestate *state) {
+s_unpack_internal(PyStructObject *soself, const char *startfrom) {
formatcode *code;
Py_ssize_t i = 0;
PyObject *result = PyTuple_New(soself->s_len);
@@ -1533,7 +1520,7 @@ s_unpack_internal(PyStructObject *soself, const char *startfrom,
n = code->size - 1;
v = PyBytes_FromStringAndSize(res + 1, n);
} else {
- v = e->unpack(state, res, e);
+ v = e->unpack(res, e);
}
if (v == NULL)
goto fail;
@@ -1567,15 +1554,14 @@ static PyObject *
Struct_unpack_impl(PyStructObject *self, Py_buffer *buffer)
/*[clinic end generated code: output=873a24faf02e848a input=3113f8e7038b2f6c]*/
{
- _structmodulestate *state = get_struct_state_structinst(self);
assert(self->s_codes != NULL);
if (buffer->len != self->s_size) {
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"unpack requires a buffer of %zd bytes",
self->s_size);
return NULL;
}
- return s_unpack_internal(self, buffer->buf, state);
+ return s_unpack_internal(self, buffer->buf);
}
/*[clinic input]
@@ -1599,12 +1585,11 @@ Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
Py_ssize_t offset)
/*[clinic end generated code: output=57fac875e0977316 input=cafd4851d473c894]*/
{
- _structmodulestate *state = get_struct_state_structinst(self);
assert(self->s_codes != NULL);
if (offset < 0) {
if (offset + self->s_size > 0) {
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"not enough data to unpack %zd bytes at offset %zd",
self->s_size,
offset);
@@ -1612,7 +1597,7 @@ Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
}
if (offset + buffer->len < 0) {
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"offset %zd out of range for %zd-byte buffer",
offset,
buffer->len);
@@ -1622,7 +1607,7 @@ Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
}
if ((buffer->len - offset) < self->s_size) {
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"unpack_from requires a buffer of at least %zu bytes for "
"unpacking %zd bytes at offset %zd "
"(actual buffer size is %zd)",
@@ -1632,7 +1617,7 @@ Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
buffer->len);
return NULL;
}
- return s_unpack_internal(self, (char*)buffer->buf + offset, state);
+ return s_unpack_internal(self, (char*)buffer->buf + offset);
}
@@ -1686,7 +1671,6 @@ static PyMethodDef unpackiter_methods[] = {
static PyObject *
unpackiter_iternext(unpackiterobject *self)
{
- _structmodulestate *state = get_struct_state_iterinst(self);
PyObject *result;
if (self->so == NULL)
return NULL;
@@ -1698,8 +1682,7 @@ unpackiter_iternext(unpackiterobject *self)
}
assert(self->index + self->so->s_size <= self->buf.len);
result = s_unpack_internal(self->so,
- (char*) self->buf.buf + self->index,
- state);
+ (char*) self->buf.buf + self->index);
self->index += self->so->s_size;
return result;
}
@@ -1746,18 +1729,17 @@ static PyObject *
Struct_iter_unpack(PyStructObject *self, PyObject *buffer)
/*[clinic end generated code: output=172d83d0cd15dbab input=6d65b3f3107dbc99]*/
{
- _structmodulestate *state = get_struct_state_structinst(self);
unpackiterobject *iter;
assert(self->s_codes != NULL);
if (self->s_size == 0) {
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"cannot iteratively unpack with a struct of length 0");
return NULL;
}
- iter = (unpackiterobject *) PyType_GenericAlloc((PyTypeObject *)state->unpackiter_type, 0);
+ iter = (unpackiterobject *) PyType_GenericAlloc((PyTypeObject *)_structmodulestate_global->unpackiter_type, 0);
if (iter == NULL)
return NULL;
@@ -1766,7 +1748,7 @@ Struct_iter_unpack(PyStructObject *self, PyObject *buffer)
return NULL;
}
if (iter->buf.len % self->s_size != 0) {
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"iterative unpacking requires a buffer of "
"a multiple of %zd bytes",
self->s_size);
@@ -1791,8 +1773,7 @@ Struct_iter_unpack(PyStructObject *self, PyObject *buffer)
*
*/
static int
-s_pack_internal(PyStructObject *soself, PyObject *const *args, int offset,
- char* buf, _structmodulestate *state)
+s_pack_internal(PyStructObject *soself, PyObject *const *args, int offset, char* buf)
{
formatcode *code;
/* XXX(nnorwitz): why does i need to be a local? can we use
@@ -1813,7 +1794,7 @@ s_pack_internal(PyStructObject *soself, PyObject *const *args, int offset,
const void *p;
isstring = PyBytes_Check(v);
if (!isstring && !PyByteArray_Check(v)) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"argument for 's' must be a bytes object");
return -1;
}
@@ -1835,7 +1816,7 @@ s_pack_internal(PyStructObject *soself, PyObject *const *args, int offset,
const void *p;
isstring = PyBytes_Check(v);
if (!isstring && !PyByteArray_Check(v)) {
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"argument for 'p' must be a bytes object");
return -1;
}
@@ -1855,9 +1836,9 @@ s_pack_internal(PyStructObject *soself, PyObject *const *args, int offset,
n = 255;
*res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
} else {
- if (e->pack(state, res, v, e) < 0) {
+ if (e->pack(res, v, e) < 0) {
if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))
- PyErr_SetString(state->StructError,
+ PyErr_SetString(_structmodulestate_global->StructError,
"int too large to convert");
return -1;
}
@@ -1883,15 +1864,14 @@ s_pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
{
char *buf;
PyStructObject *soself;
- _structmodulestate *state = get_struct_state_structinst(self);
/* Validate arguments. */
soself = (PyStructObject *)self;
- assert(PyStruct_Check(self, state));
+ assert(PyStruct_Check(self));
assert(soself->s_codes != NULL);
if (nargs != soself->s_len)
{
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"pack expected %zd items for packing (got %zd)", soself->s_len, nargs);
return NULL;
}
@@ -1906,7 +1886,7 @@ s_pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
}
/* Call the guts */
- if ( s_pack_internal(soself, args, 0, buf, state) != 0 ) {
+ if ( s_pack_internal(soself, args, 0, buf) != 0 ) {
_PyBytesWriter_Dealloc(&writer);
return NULL;
}
@@ -1928,24 +1908,23 @@ s_pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
PyStructObject *soself;
Py_buffer buffer;
Py_ssize_t offset;
- _structmodulestate *state = get_struct_state_structinst(self);
/* Validate arguments. +1 is for the first arg as buffer. */
soself = (PyStructObject *)self;
- assert(PyStruct_Check(self, state));
+ assert(PyStruct_Check(self));
assert(soself->s_codes != NULL);
if (nargs != (soself->s_len + 2))
{
if (nargs == 0) {
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"pack_into expected buffer argument");
}
else if (nargs == 1) {
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"pack_into expected offset argument");
}
else {
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"pack_into expected %zd items for packing (got %zd)",
soself->s_len, (nargs - 2));
}
@@ -1968,7 +1947,7 @@ s_pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
if (offset < 0) {
/* Check that negative offset is low enough to fit data */
if (offset + soself->s_size > 0) {
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"no space to pack %zd bytes at offset %zd",
soself->s_size,
offset);
@@ -1978,7 +1957,7 @@ s_pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
/* Check that negative offset is not crossing buffer boundary */
if (offset + buffer.len < 0) {
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"offset %zd out of range for %zd-byte buffer",
offset,
buffer.len);
@@ -1994,7 +1973,7 @@ s_pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
assert(offset >= 0);
assert(soself->s_size >= 0);
- PyErr_Format(state->StructError,
+ PyErr_Format(_structmodulestate_global->StructError,
"pack_into requires a buffer of at least %zu bytes for "
"packing %zd bytes at offset %zd "
"(actual buffer size is %zd)",
@@ -2007,7 +1986,7 @@ s_pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
}
/* Call the guts */
- if (s_pack_internal(soself, args, 2, (char*)buffer.buf + offset, state) != 0) {
+ if (s_pack_internal(soself, args, 2, (char*)buffer.buf + offset) != 0) {
PyBuffer_Release(&buffer);
return NULL;
}
@@ -2101,12 +2080,12 @@ static PyType_Spec PyStructType_spec = {
/* ---- Standalone functions ---- */
#define MAXCACHE 100
+static PyObject *cache = NULL;
static int
-cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
+cache_struct_converter(PyObject *fmt, PyStructObject **ptr)
{
PyObject * s_object;
- _structmodulestate *state = get_struct_state(module);
if (fmt == NULL) {
Py_DECREF(*ptr);
@@ -2114,13 +2093,13 @@ cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
return 1;
}
- if (state->cache == NULL) {
- state->cache = PyDict_New();
- if (state->cache == NULL)
+ if (cache == NULL) {
+ cache = PyDict_New();
+ if (cache == NULL)
return 0;
}
- s_object = PyDict_GetItemWithError(state->cache, fmt);
+ s_object = PyDict_GetItemWithError(cache, fmt);
if (s_object != NULL) {
Py_INCREF(s_object);
*ptr = (PyStructObject *)s_object;
@@ -2130,12 +2109,12 @@ cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
return 0;
}
- s_object = PyObject_CallOneArg(state->PyStructType, fmt);
+ s_object = PyObject_CallOneArg(_structmodulestate_global->PyStructType, fmt);
if (s_object != NULL) {
- if (PyDict_GET_SIZE(state->cache) >= MAXCACHE)
- PyDict_Clear(state->cache);
+ if (PyDict_GET_SIZE(cache) >= MAXCACHE)
+ PyDict_Clear(cache);
/* Attempt to cache the result */
- if (PyDict_SetItem(state->cache, fmt, s_object) == -1)
+ if (PyDict_SetItem(cache, fmt, s_object) == -1)
PyErr_Clear();
*ptr = (PyStructObject *)s_object;
return Py_CLEANUP_SUPPORTED;
@@ -2153,7 +2132,7 @@ static PyObject *
_clearcache_impl(PyObject *module)
/*[clinic end generated code: output=ce4fb8a7bf7cb523 input=463eaae04bab3211]*/
{
- Py_CLEAR(get_struct_state(module)->cache);
+ Py_CLEAR(cache);
Py_RETURN_NONE;
}
@@ -2181,7 +2160,7 @@ Return a bytes object containing the values v1, v2, ... packed according\n\
to the format string. See help(struct) for more on format strings.");
static PyObject *
-pack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
+pack(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *s_object = NULL;
PyObject *format, *result;
@@ -2192,7 +2171,7 @@ pack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
}
format = args[0];
- if (!cache_struct_converter(module, format, (PyStructObject **)&s_object)) {
+ if (!cache_struct_converter(format, (PyStructObject **)&s_object)) {
return NULL;
}
result = s_pack(s_object, args + 1, nargs - 1);
@@ -2209,7 +2188,7 @@ that the offset is a required argument. See help(struct) for more\n\
on format strings.");
static PyObject *
-pack_into(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
+pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *s_object = NULL;
PyObject *format, *result;
@@ -2220,7 +2199,7 @@ pack_into(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
}
format = args[0];
- if (!cache_struct_converter(module, format, (PyStructObject **)&s_object)) {
+ if (!cache_struct_converter(format, (PyStructObject **)&s_object)) {
return NULL;
}
result = s_pack_into(s_object, args + 1, nargs - 1);
@@ -2343,9 +2322,8 @@ The variable struct.error is an exception raised on errors.\n");
static int
_structmodule_traverse(PyObject *module, visitproc visit, void *arg)
{
- _structmodulestate *state = get_struct_state(module);
+ _structmodulestate *state = (_structmodulestate *)PyModule_GetState(module);
if (state) {
- Py_VISIT(state->cache);
Py_VISIT(state->PyStructType);
Py_VISIT(state->unpackiter_type);
Py_VISIT(state->StructError);
@@ -2356,9 +2334,8 @@ _structmodule_traverse(PyObject *module, visitproc visit, void *arg)
static int
_structmodule_clear(PyObject *module)
{
- _structmodulestate *state = get_struct_state(module);
+ _structmodulestate *state = (_structmodulestate *)PyModule_GetState(module);
if (state) {
- Py_CLEAR(state->cache);
Py_CLEAR(state->PyStructType);
Py_CLEAR(state->unpackiter_type);
Py_CLEAR(state->StructError);
@@ -2372,25 +2349,40 @@ _structmodule_free(void *module)
_structmodule_clear((PyObject *)module);
}
-static int
-_structmodule_exec(PyObject *m)
+static struct PyModuleDef _structmodule = {
+ PyModuleDef_HEAD_INIT,
+ "_struct",
+ module_doc,
+ sizeof(_structmodulestate),
+ module_functions,
+ NULL,
+ _structmodule_traverse,
+ _structmodule_clear,
+ _structmodule_free,
+};
+
+PyMODINIT_FUNC
+PyInit__struct(void)
{
- _structmodulestate *state = get_struct_state(m);
+ PyObject *m;
- state->PyStructType = PyType_FromModuleAndSpec(
- m, &PyStructType_spec, NULL);
- if (state->PyStructType == NULL) {
- return -1;
- }
- if (PyModule_AddType(m, (PyTypeObject *)state->PyStructType) < 0) {
- return -1;
+ m = PyModule_Create(&_structmodule);
+ if (m == NULL)
+ return NULL;
+
+ PyObject *PyStructType = PyType_FromSpec(&PyStructType_spec);
+ if (PyStructType == NULL) {
+ return NULL;
}
+ Py_INCREF(PyStructType);
+ PyModule_AddObject(m, "Struct", PyStructType);
+ get_struct_state(m)->PyStructType = PyStructType;
- state->unpackiter_type = PyType_FromModuleAndSpec(
- m, &unpackiter_type_spec, NULL);
- if (state->unpackiter_type == NULL) {
- return -1;
+ PyObject *unpackiter_type = PyType_FromSpec(&unpackiter_type_spec);
+ if (unpackiter_type == NULL) {
+ return NULL;
}
+ get_struct_state(m)->unpackiter_type = unpackiter_type;
/* Check endian and swap in faster functions */
{
@@ -2435,36 +2427,12 @@ _structmodule_exec(PyObject *m)
}
/* Add some symbolic constants to the module */
- state->StructError = PyErr_NewException("struct.error", NULL, NULL);
- if (state->StructError == NULL) {
- return -1;
- }
- if (PyModule_AddObjectRef(m, "error", state->StructError) < 0) {
- return -1;
- }
-
- return 0;
-}
-
-static PyModuleDef_Slot _structmodule_slots[] = {
- {Py_mod_exec, _structmodule_exec},
- {0, NULL}
-};
-
-static struct PyModuleDef _structmodule = {
- PyModuleDef_HEAD_INIT,
- .m_name = "_struct",
- .m_doc = module_doc,
- .m_size = sizeof(_structmodulestate),
- .m_methods = module_functions,
- .m_slots = _structmodule_slots,
- .m_traverse = _structmodule_traverse,
- .m_clear = _structmodule_clear,
- .m_free = _structmodule_free,
-};
+ PyObject *StructError = PyErr_NewException("struct.error", NULL, NULL);
+ if (StructError == NULL)
+ return NULL;
+ Py_INCREF(StructError);
+ PyModule_AddObject(m, "error", StructError);
+ get_struct_state(m)->StructError = StructError;
-PyMODINIT_FUNC
-PyInit__struct(void)
-{
- return PyModuleDef_Init(&_structmodule);
+ return m;
}
diff --git a/Modules/clinic/_struct.c.h b/Modules/clinic/_struct.c.h
index b0c1eb4587..1cfaef3b71 100644
--- a/Modules/clinic/_struct.c.h
+++ b/Modules/clinic/_struct.c.h
@@ -199,7 +199,7 @@ calcsize(PyObject *module, PyObject *arg)
PyStructObject *s_object = NULL;
Py_ssize_t _return_value;
- if (!cache_struct_converter(module, arg, &s_object)) {
+ if (!cache_struct_converter(arg, &s_object)) {
goto exit;
}
_return_value = calcsize_impl(module, s_object);
@@ -241,7 +241,7 @@ unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("unpack", nargs, 2, 2)) {
goto exit;
}
- if (!cache_struct_converter(module, args[0], &s_object)) {
+ if (!cache_struct_converter(args[0], &s_object)) {
goto exit;
}
if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
@@ -297,7 +297,7 @@ unpack_from(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject
if (!args) {
goto exit;
}
- if (!cache_struct_converter(module, args[0], &s_object)) {
+ if (!cache_struct_converter(args[0], &s_object)) {
goto exit;
}
if (PyObject_GetBuffer(args[1], &buffer, PyBUF_SIMPLE) != 0) {
@@ -364,7 +364,7 @@ iter_unpack(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("iter_unpack", nargs, 2, 2)) {
goto exit;
}
- if (!cache_struct_converter(module, args[0], &s_object)) {
+ if (!cache_struct_converter(args[0], &s_object)) {
goto exit;
}
buffer = args[1];
@@ -376,4 +376,4 @@ exit:
return return_value;
}
-/*[clinic end generated code: output=a3d3cd900091cb1c input=a9049054013a1b77]*/
+/*[clinic end generated code: output=8089792d8ed0c1be input=a9049054013a1b77]*/