diff options
author | Victor Stinner <vstinner@python.org> | 2022-03-11 23:10:02 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-11 23:10:02 (GMT) |
commit | 882d8096c262a5945e0cfdd706e5db3ad2b73543 (patch) | |
tree | 5e903c7e87a13203543e3e215f30b6b708df018d /Modules/_struct.c | |
parent | ecfff63e06e77e22035a7f7caa26986f033f3aea (diff) | |
download | cpython-882d8096c262a5945e0cfdd706e5db3ad2b73543.zip cpython-882d8096c262a5945e0cfdd706e5db3ad2b73543.tar.gz cpython-882d8096c262a5945e0cfdd706e5db3ad2b73543.tar.bz2 |
bpo-46906: Add PyFloat_Pack8() to the C API (GH-31657)
Add new functions to pack and unpack C double (serialize and
deserialize):
* PyFloat_Pack2(), PyFloat_Pack4(), PyFloat_Pack8()
* PyFloat_Unpack2(), PyFloat_Unpack4(), PyFloat_Unpack8()
Document these functions and add unit tests.
Rename private functions and move them from the internal C API
to the public C API:
* _PyFloat_Pack2() => PyFloat_Pack2()
* _PyFloat_Pack4() => PyFloat_Pack4()
* _PyFloat_Pack8() => PyFloat_Pack8()
* _PyFloat_Unpack2() => PyFloat_Unpack2()
* _PyFloat_Unpack4() => PyFloat_Unpack4()
* _PyFloat_Unpack8() => PyFloat_Unpack8()
Replace the "unsigned char*" type with "char*" which is more common
and easy to use.
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r-- | Modules/_struct.c | 19 |
1 files changed, 8 insertions, 11 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c index a2e14e8..7cd0ef8 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -10,7 +10,6 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" -#include "pycore_floatobject.h" // _PyFloat_Unpack2() #include "pycore_moduleobject.h" // _PyModule_GetState() #include "structmember.h" // PyMemberDef #include <ctype.h> @@ -303,9 +302,7 @@ static PyObject * unpack_halffloat(const char *p, /* start of 2-byte string */ int le) /* true for little-endian, false for big-endian */ { - double x; - - x = _PyFloat_Unpack2((unsigned char *)p, le); + double x = PyFloat_Unpack2(p, le); if (x == -1.0 && PyErr_Occurred()) { return NULL; } @@ -324,7 +321,7 @@ pack_halffloat(_structmodulestate *state, "required argument is not a float"); return -1; } - return _PyFloat_Pack2(x, (unsigned char *)p, le); + return PyFloat_Pack2(x, p, le); } static PyObject * @@ -333,7 +330,7 @@ unpack_float(const char *p, /* start of 4-byte string */ { double x; - x = _PyFloat_Unpack4((unsigned char *)p, le); + x = PyFloat_Unpack4(p, le); if (x == -1.0 && PyErr_Occurred()) return NULL; return PyFloat_FromDouble(x); @@ -345,7 +342,7 @@ unpack_double(const char *p, /* start of 8-byte string */ { double x; - x = _PyFloat_Unpack8((unsigned char *)p, le); + x = PyFloat_Unpack8(p, le); if (x == -1.0 && PyErr_Occurred()) return NULL; return PyFloat_FromDouble(x); @@ -979,7 +976,7 @@ bp_float(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) "required argument is not a float"); return -1; } - return _PyFloat_Pack4(x, (unsigned char *)p, 0); + return PyFloat_Pack4(x, p, 0); } static int @@ -991,7 +988,7 @@ bp_double(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) "required argument is not a float"); return -1; } - return _PyFloat_Pack8(x, (unsigned char *)p, 0); + return PyFloat_Pack8(x, p, 0); } static int @@ -1194,7 +1191,7 @@ lp_float(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) "required argument is not a float"); return -1; } - return _PyFloat_Pack4(x, (unsigned char *)p, 1); + return PyFloat_Pack4(x, p, 1); } static int @@ -1206,7 +1203,7 @@ lp_double(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) "required argument is not a float"); return -1; } - return _PyFloat_Pack8(x, (unsigned char *)p, 1); + return PyFloat_Pack8(x, p, 1); } static formatdef lilendian_table[] = { |