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.
This commit is contained in:
Victor Stinner 2022-03-12 00:10:02 +01:00 committed by GitHub
parent ecfff63e06
commit 882d8096c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 294 additions and 91 deletions

View file

@ -11,7 +11,6 @@
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_code.h" // _PyCode_New()
#include "pycore_floatobject.h" // _PyFloat_Pack8()
#include "pycore_hashtable.h" // _Py_hashtable_t
#include "code.h"
#include "marshal.h" // Py_MARSHAL_VERSION
@ -271,8 +270,8 @@ w_PyLong(const PyLongObject *ob, char flag, WFILE *p)
static void
w_float_bin(double v, WFILE *p)
{
unsigned char buf[8];
if (_PyFloat_Pack8(v, buf, 1) < 0) {
char buf[8];
if (PyFloat_Pack8(v, buf, 1) < 0) {
p->error = WFERR_UNMARSHALLABLE;
return;
}
@ -883,10 +882,10 @@ r_PyLong(RFILE *p)
static double
r_float_bin(RFILE *p)
{
const unsigned char *buf = (const unsigned char *) r_string(8, p);
const char *buf = r_string(8, p);
if (buf == NULL)
return -1;
return _PyFloat_Unpack8(buf, 1);
return PyFloat_Unpack8(buf, 1);
}
/* Issue #33720: Disable inlining for reducing the C stack consumption