-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-46906: Add PyFloat_Pack8() to the C API #31657
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Add new functions to pack and unpack C double (serialize and deserialize): | ||
:c:func:`PyFloat_Pack2`, :c:func:`PyFloat_Pack4`, :c:func:`PyFloat_Pack8`, | ||
:c:func:`PyFloat_Unpack2`, :c:func:`PyFloat_Unpack4` and | ||
:c:func:`PyFloat_Unpack8`. Patch by Victor Stinner. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ | |
|
||
#include "pycore_bitutils.h" // _Py_bswap32() | ||
#include "pycore_call.h" // _PyObject_CallNoArgs() | ||
#include "pycore_floatobject.h" // _PyFloat_Pack8() | ||
|
||
#include <ffi.h> | ||
#include "ctypes.h" | ||
|
@@ -1009,10 +1008,10 @@ d_set_sw(void *ptr, PyObject *value, Py_ssize_t size) | |
if (x == -1 && PyErr_Occurred()) | ||
return NULL; | ||
#ifdef WORDS_BIGENDIAN | ||
if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1)) | ||
if (PyFloat_Pack8(x, ptr, 1)) | ||
return NULL; | ||
#else | ||
if (_PyFloat_Pack8(x, (unsigned char *)ptr, 0)) | ||
if (PyFloat_Pack8(x, ptr, 0)) | ||
return NULL; | ||
#endif | ||
_RET(value); | ||
|
@@ -1022,9 +1021,9 @@ static PyObject * | |
d_get_sw(void *ptr, Py_ssize_t size) | ||
{ | ||
#ifdef WORDS_BIGENDIAN | ||
return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 1)); | ||
return PyFloat_FromDouble(PyFloat_Unpack8(ptr, 1)); | ||
#else | ||
return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 0)); | ||
return PyFloat_FromDouble(PyFloat_Unpack8(ptr, 0)); | ||
#endif | ||
} | ||
|
||
|
@@ -1057,10 +1056,10 @@ f_set_sw(void *ptr, PyObject *value, Py_ssize_t size) | |
if (x == -1 && PyErr_Occurred()) | ||
return NULL; | ||
#ifdef WORDS_BIGENDIAN | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this parameter is changed, it will be way harder to provide this function in pythoncapi_compat to older Python versions: python/pythoncapi-compat#26 And it maybe become more complicated to write a single code base supporting old and new Python versions. It's easy to implement the "native" behavior by checking if your system uses little endian or not with your preferred way.
So far, I didn't see a module using the "native endian". For a serialization function, it's not convenient. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wrote #31832 to suggest using PY_BIG_ENDIAN macro to use the native endian. |
||
if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1)) | ||
if (PyFloat_Pack4(x, ptr, 1)) | ||
return NULL; | ||
#else | ||
if (_PyFloat_Pack4(x, (unsigned char *)ptr, 0)) | ||
if (PyFloat_Pack4(x, ptr, 0)) | ||
return NULL; | ||
#endif | ||
_RET(value); | ||
|
@@ -1070,9 +1069,9 @@ static PyObject * | |
f_get_sw(void *ptr, Py_ssize_t size) | ||
{ | ||
#ifdef WORDS_BIGENDIAN | ||
return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 1)); | ||
return PyFloat_FromDouble(PyFloat_Unpack4(ptr, 1)); | ||
#else | ||
return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 0)); | ||
return PyFloat_FromDouble(PyFloat_Unpack4(ptr, 0)); | ||
#endif | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably once we're assuming IEEE 754 for CPython, all this text can be dropped?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My plan is to first fix the Python 3.11 "regression" by exposing these functions as public C API functions. And then deprecate or remove support for non-IEEE 754 platforms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Require IEEE 754 support is tracked by https://bugs.python.org/issue46917