]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-35134: Add Include/cpython/floatobject.h (GH-28957)
authorVictor Stinner <vstinner@python.org>
Thu, 14 Oct 2021 21:41:06 +0000 (23:41 +0200)
committerGitHub <noreply@github.com>
Thu, 14 Oct 2021 21:41:06 +0000 (23:41 +0200)
Split Include/floatobject.h into sub-files: add
Include/cpython/floatobject.h and
Include/internal/pycore_floatobject.h.

14 files changed:
Include/cpython/floatobject.h [new file with mode: 0644]
Include/floatobject.h
Include/internal/pycore_floatobject.h [new file with mode: 0644]
Makefile.pre.in
Modules/_ctypes/cfield.c
Modules/_pickle.c
Modules/_struct.c
Modules/arraymodule.c
Objects/floatobject.c
Objects/object.c
Objects/stringlib/unicode_format.h
PCbuild/pythoncore.vcxproj
PCbuild/pythoncore.vcxproj.filters
Python/marshal.c

diff --git a/Include/cpython/floatobject.h b/Include/cpython/floatobject.h
new file mode 100644 (file)
index 0000000..fffd468
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef Py_CPYTHON_FLOATOBJECT_H
+#  error "this header file must not be included directly"
+#endif
+
+typedef struct {
+    PyObject_HEAD
+    double ob_fval;
+} PyFloatObject;
+
+// Macro version of PyFloat_AsDouble() trading safety for speed.
+// It doesn't check if op is a double object.
+#define PyFloat_AS_DOUBLE(op) (((PyFloatObject *)(op))->ob_fval)
index e994aa8f29da484a1e4660c28d782d2324b93145..3b6ca478eaef201047234ae63a30b30ea76eb92d 100644 (file)
@@ -11,106 +11,44 @@ PyFloatObject represents a (double precision) floating point number.
 extern "C" {
 #endif
 
-#ifndef Py_LIMITED_API
-typedef struct {
-    PyObject_HEAD
-    double ob_fval;
-} PyFloatObject;
-#endif
-
 PyAPI_DATA(PyTypeObject) PyFloat_Type;
 
 #define PyFloat_Check(op) PyObject_TypeCheck(op, &PyFloat_Type)
 #define PyFloat_CheckExact(op) Py_IS_TYPE(op, &PyFloat_Type)
 
 #ifdef Py_NAN
-#define Py_RETURN_NAN return PyFloat_FromDouble(Py_NAN)
+#  define Py_RETURN_NAN return PyFloat_FromDouble(Py_NAN)
 #endif
 
-#define Py_RETURN_INF(sign) do                     \
-    if (copysign(1., sign) == 1.) {                \
-        return PyFloat_FromDouble(Py_HUGE_VAL);    \
-    } else {                        \
-        return PyFloat_FromDouble(-Py_HUGE_VAL);   \
+#define Py_RETURN_INF(sign)                          \
+    do {                                             \
+        if (copysign(1., sign) == 1.) {              \
+            return PyFloat_FromDouble(Py_HUGE_VAL);  \
+        }                                            \
+        else {                                       \
+            return PyFloat_FromDouble(-Py_HUGE_VAL); \
+        }                                            \
     } while(0)
 
 PyAPI_FUNC(double) PyFloat_GetMax(void);
 PyAPI_FUNC(double) PyFloat_GetMin(void);
-PyAPI_FUNC(PyObject *) PyFloat_GetInfo(void);
+PyAPI_FUNC(PyObject*) PyFloat_GetInfo(void);
 
 /* Return Python float from string PyObject. */
-PyAPI_FUNC(PyObject *) PyFloat_FromString(PyObject*);
+PyAPI_FUNC(PyObject*) PyFloat_FromString(PyObject*);
 
 /* Return Python float from C double. */
-PyAPI_FUNC(PyObject *) PyFloat_FromDouble(double);
+PyAPI_FUNC(PyObject*) PyFloat_FromDouble(double);
 
 /* Extract C double from Python float.  The macro version trades safety for
    speed. */
-PyAPI_FUNC(double) PyFloat_AsDouble(PyObject *);
-#ifndef Py_LIMITED_API
-#define PyFloat_AS_DOUBLE(op) (((PyFloatObject *)(op))->ob_fval)
-#endif
+PyAPI_FUNC(double) PyFloat_AsDouble(PyObject*);
 
 #ifndef Py_LIMITED_API
-/* _PyFloat_{Pack,Unpack}{4,8}
- *
- * The struct and pickle (at least) modules need an efficient platform-
- * independent way to store floating-point values as byte strings.
- * The Pack routines produce a string from a C double, and the Unpack
- * routines produce a C double from such a string.  The suffix (4 or 8)
- * specifies the number of bytes in the string.
- *
- * On platforms that appear to use (see _PyFloat_Init()) IEEE-754 formats
- * these functions work by copying bits.  On other platforms, the formats the
- * 4- byte format is identical to the IEEE-754 single precision format, and
- * the 8-byte format to the IEEE-754 double precision format, although the
- * packing of INFs and NaNs (if such things exist on the platform) isn't
- * handled correctly, and attempting to unpack a string containing an IEEE
- * INF or NaN will raise an exception.
- *
- * On non-IEEE platforms with more precision, or larger dynamic range, than
- * 754 supports, not all values can be packed; on non-IEEE platforms with less
- * precision, or smaller dynamic range, not all values can be unpacked.  What
- * happens in such cases is partly accidental (alas).
- */
-
-/* The pack routines write 2, 4 or 8 bytes, starting at p.  le is a bool
- * argument, true if you want the string in little-endian format (exponent
- * last, at p+1, p+3 or p+7), false if you want big-endian format (exponent
- * first, at p).
- * Return value:  0 if all is OK, -1 if error (and an exception is
- * set, most likely OverflowError).
- * There are two problems on non-IEEE platforms:
- * 1):  What this does is undefined if x is a NaN or infinity.
- * 2):  -0.0 and +0.0 produce the same string.
- */
-PyAPI_FUNC(int) _PyFloat_Pack2(double x, unsigned char *p, int le);
-PyAPI_FUNC(int) _PyFloat_Pack4(double x, unsigned char *p, int le);
-PyAPI_FUNC(int) _PyFloat_Pack8(double x, unsigned char *p, int le);
-
-/* The unpack routines read 2, 4 or 8 bytes, starting at p.  le is a bool
- * argument, true if the string is in little-endian format (exponent
- * last, at p+1, p+3 or p+7), false if big-endian (exponent first, at p).
- * Return value:  The unpacked double.  On error, this is -1.0 and
- * PyErr_Occurred() is true (and an exception is set, most likely
- * OverflowError).  Note that on a non-IEEE platform this will refuse
- * to unpack a string that represents a NaN or infinity.
- */
-PyAPI_FUNC(double) _PyFloat_Unpack2(const unsigned char *p, int le);
-PyAPI_FUNC(double) _PyFloat_Unpack4(const unsigned char *p, int le);
-PyAPI_FUNC(double) _PyFloat_Unpack8(const unsigned char *p, int le);
-
-PyAPI_FUNC(void) _PyFloat_DebugMallocStats(FILE* out);
-
-/* Format the object based on the format_spec, as defined in PEP 3101
-   (Advanced String Formatting). */
-PyAPI_FUNC(int) _PyFloat_FormatAdvancedWriter(
-    _PyUnicodeWriter *writer,
-    PyObject *obj,
-    PyObject *format_spec,
-    Py_ssize_t start,
-    Py_ssize_t end);
-#endif /* Py_LIMITED_API */
+#  define Py_CPYTHON_FLOATOBJECT_H
+#  include "cpython/floatobject.h"
+#  undef Py_CPYTHON_FLOATOBJECT_H
+#endif
 
 #ifdef __cplusplus
 }
diff --git a/Include/internal/pycore_floatobject.h b/Include/internal/pycore_floatobject.h
new file mode 100644 (file)
index 0000000..18227c9
--- /dev/null
@@ -0,0 +1,75 @@
+#ifndef Py_INTERNAL_FLOATOBJECT_H
+#define Py_INTERNAL_FLOATOBJECT_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
+
+/* _PyFloat_{Pack,Unpack}{4,8}
+ *
+ * The struct and pickle (at least) modules need an efficient platform-
+ * independent way to store floating-point values as byte strings.
+ * The Pack routines produce a string from a C double, and the Unpack
+ * routines produce a C double from such a string.  The suffix (4 or 8)
+ * specifies the number of bytes in the string.
+ *
+ * On platforms that appear to use (see _PyFloat_Init()) IEEE-754 formats
+ * these functions work by copying bits.  On other platforms, the formats the
+ * 4- byte format is identical to the IEEE-754 single precision format, and
+ * the 8-byte format to the IEEE-754 double precision format, although the
+ * packing of INFs and NaNs (if such things exist on the platform) isn't
+ * handled correctly, and attempting to unpack a string containing an IEEE
+ * INF or NaN will raise an exception.
+ *
+ * On non-IEEE platforms with more precision, or larger dynamic range, than
+ * 754 supports, not all values can be packed; on non-IEEE platforms with less
+ * precision, or smaller dynamic range, not all values can be unpacked.  What
+ * happens in such cases is partly accidental (alas).
+ */
+
+/* The pack routines write 2, 4 or 8 bytes, starting at p.  le is a bool
+ * argument, true if you want the string in little-endian format (exponent
+ * last, at p+1, p+3 or p+7), false if you want big-endian format (exponent
+ * first, at p).
+ * Return value:  0 if all is OK, -1 if error (and an exception is
+ * set, most likely OverflowError).
+ * There are two problems on non-IEEE platforms:
+ * 1):  What this does is undefined if x is a NaN or infinity.
+ * 2):  -0.0 and +0.0 produce the same string.
+ */
+PyAPI_FUNC(int) _PyFloat_Pack2(double x, unsigned char *p, int le);
+PyAPI_FUNC(int) _PyFloat_Pack4(double x, unsigned char *p, int le);
+PyAPI_FUNC(int) _PyFloat_Pack8(double x, unsigned char *p, int le);
+
+/* The unpack routines read 2, 4 or 8 bytes, starting at p.  le is a bool
+ * argument, true if the string is in little-endian format (exponent
+ * last, at p+1, p+3 or p+7), false if big-endian (exponent first, at p).
+ * Return value:  The unpacked double.  On error, this is -1.0 and
+ * PyErr_Occurred() is true (and an exception is set, most likely
+ * OverflowError).  Note that on a non-IEEE platform this will refuse
+ * to unpack a string that represents a NaN or infinity.
+ */
+PyAPI_FUNC(double) _PyFloat_Unpack2(const unsigned char *p, int le);
+PyAPI_FUNC(double) _PyFloat_Unpack4(const unsigned char *p, int le);
+PyAPI_FUNC(double) _PyFloat_Unpack8(const unsigned char *p, int le);
+
+
+PyAPI_FUNC(void) _PyFloat_DebugMallocStats(FILE* out);
+
+
+/* Format the object based on the format_spec, as defined in PEP 3101
+   (Advanced String Formatting). */
+PyAPI_FUNC(int) _PyFloat_FormatAdvancedWriter(
+    _PyUnicodeWriter *writer,
+    PyObject *obj,
+    PyObject *format_spec,
+    Py_ssize_t start,
+    Py_ssize_t end);
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_INTERNAL_FLOATOBJECT_H */
index 041bc63e2fb8ed5b29dcbbd9505983b71ff63640..30b025e7efb83965bb7a0c7682eaf559a2af02e9 100644 (file)
@@ -1208,6 +1208,7 @@ PYTHON_HEADERS= \
                $(srcdir)/Include/cpython/dictobject.h \
                $(srcdir)/Include/cpython/fileobject.h \
                $(srcdir)/Include/cpython/fileutils.h \
+               $(srcdir)/Include/cpython/floatobject.h \
                $(srcdir)/Include/cpython/frameobject.h \
                $(srcdir)/Include/cpython/import.h \
                $(srcdir)/Include/cpython/initconfig.h \
@@ -1250,6 +1251,7 @@ PYTHON_HEADERS= \
                $(srcdir)/Include/internal/pycore_dict.h \
                $(srcdir)/Include/internal/pycore_dtoa.h \
                $(srcdir)/Include/internal/pycore_fileutils.h \
+               $(srcdir)/Include/internal/pycore_floatobject.h \
                $(srcdir)/Include/internal/pycore_format.h \
                $(srcdir)/Include/internal/pycore_getopt.h \
                $(srcdir)/Include/internal/pycore_gil.h \
index b7585bc8a9fffbeea672b972d1796539e5db1090..2cfd657028aca06ae7f5568f043af69e689accdf 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "pycore_bitutils.h"      // _Py_bswap32()
 #include "pycore_call.h"          // _PyObject_CallNoArgs()
+#include "pycore_floatobject.h"   // _PyFloat_Pack8()
 
 #include <ffi.h>
 #include "ctypes.h"
index b5131696981dc561a1f09588a1593d9e565662d5..53ab57111967ce22ab6b2bed4ada6cad0f54a6d2 100644 (file)
@@ -9,6 +9,7 @@
 #endif
 
 #include "Python.h"
+#include "pycore_floatobject.h"   // _PyFloat_Pack8()
 #include "pycore_moduleobject.h"  // _PyModule_GetState()
 #include "structmember.h"         // PyMemberDef
 
index 69de080f4388cd5526e47a9f0cf01c03066b3b50..a8003a90b2682c42acdd66e0e06d8db7580d3433 100644 (file)
@@ -6,6 +6,7 @@
 #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>
index c9b6a92c2274918d85e6c48bb0c8287810cf19c8..5e57fe116059de151aa7ec01eaa79902ec7a2573 100644 (file)
@@ -5,6 +5,7 @@
 
 #define PY_SSIZE_T_CLEAN
 #include "Python.h"
+#include "pycore_floatobject.h"   // _PyFloat_Unpack4()
 #include "pycore_moduleobject.h"  // _PyModule_GetState()
 #include "structmember.h"         // PyMemberDef
 #include <stddef.h>               // offsetof()
index 5a8113eca8bd043101bbaa7c42b513397778f44c..1be31e38d4935e8e97e23db0531ebbc17cbd5cb9 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "Python.h"
 #include "pycore_dtoa.h"          // _Py_dg_dtoa()
+#include "pycore_floatobject.h"   // _PyFloat_FormatAdvancedWriter()
 #include "pycore_interp.h"        // _PyInterpreterState.float_state
 #include "pycore_long.h"          // _PyLong_GetOne()
 #include "pycore_object.h"        // _PyObject_Init()
index 14c85c233df11a41024fb780c1404adec8e025fe..589dd6647318dafae5dd14e8a7e4855bdb33ebcf 100644 (file)
@@ -6,6 +6,7 @@
 #include "pycore_ceval.h"         // _Py_EnterRecursiveCall()
 #include "pycore_context.h"
 #include "pycore_dict.h"
+#include "pycore_floatobject.h"   // _PyFloat_DebugMallocStats()
 #include "pycore_initconfig.h"    // _PyStatus_EXCEPTION()
 #include "pycore_object.h"        // _PyType_CheckConsistency()
 #include "pycore_pyerrors.h"      // _PyErr_Occurred()
index 7152ec6ebe7128ada0b742a79c9e589ac16592c8..a4eea7b91988b9ba3aa6a170690fdb0aed38c9ba 100644 (file)
@@ -2,6 +2,8 @@
     unicode_format.h -- implementation of str.format().
 */
 
+#include "pycore_floatobject.h"   // _PyFloat_FormatAdvancedWriter()
+
 /************************************************************************/
 /***********   Global data structures and forward declarations  *********/
 /************************************************************************/
index d0e1b52a4927c1f789a6a6997c2176ca49b44f57..f688e8a32324515342f8a794e66cf20e65fa7681 100644 (file)
     <ClInclude Include="..\Include\cpython\dictobject.h" />
     <ClInclude Include="..\Include\cpython\fileobject.h" />
     <ClInclude Include="..\Include\cpython\fileutils.h" />
+    <ClInclude Include="..\Include\cpython\floatobject.h" />
     <ClInclude Include="..\Include\cpython\frameobject.h" />
     <ClInclude Include="..\Include\cpython\import.h" />
     <ClInclude Include="..\Include\cpython\initconfig.h" />
     <ClInclude Include="..\Include\internal\pycore_context.h" />
     <ClInclude Include="..\Include\internal\pycore_dtoa.h" />
     <ClInclude Include="..\Include\internal\pycore_fileutils.h" />
+    <ClInclude Include="..\Include\internal\pycore_floatobject.h" />
     <ClInclude Include="..\Include\internal\pycore_format.h" />
     <ClInclude Include="..\Include\internal\pycore_gc.h" />
     <ClInclude Include="..\Include\internal\pycore_getopt.h" />
index 9370f405132c36bd2ba97682fcf14062eaa08643..eb72c38a7b2038d86776674d20a53201ae4a5b1a 100644 (file)
     <ClInclude Include="..\Include\cpython\fileutils.h">
       <Filter>Include\cpython</Filter>
     </ClInclude>
+    <ClInclude Include="..\Include\cpython\floatobject.h">
+      <Filter>Include\cpython</Filter>
+    </ClInclude>
     <ClInclude Include="..\Include\cpython\import.h">
       <Filter>Include\cpython</Filter>
     </ClInclude>
     <ClInclude Include="..\Include\internal\pycore_fileutils.h">
       <Filter>Include\internal</Filter>
     </ClInclude>
+    <ClInclude Include="..\Include\internal\pycore_floatobject.h">
+      <Filter>Include\internal</Filter>
+    </ClInclude>
     <ClInclude Include="..\Include\internal\pycore_format.h">
       <Filter>Include\internal</Filter>
     </ClInclude>
index c8a48a55dee0f314e5def8d911daed30a56ea8bc..e9ad566b71b107dcf55103d69776893404cbba5e 100644 (file)
@@ -11,6 +11,7 @@
 #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 "longintrepr.h"
 #include "code.h"