]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45434: Remove pystrhex.h header file (GH-28923)
authorVictor Stinner <vstinner@python.org>
Wed, 13 Oct 2021 13:22:35 +0000 (15:22 +0200)
committerGitHub <noreply@github.com>
Wed, 13 Oct 2021 13:22:35 +0000 (15:22 +0200)
Move Include/pystrhex.h to Include/internal/pycore_strhex.h.
The header file only contains private functions.

The following C extensions are now built with Py_BUILD_CORE_MODULE
macro defined to get access to the internal C API:

* _blake2
* _hashopenssl
* _md5
* _sha1
* _sha3
* _ssl
* binascii

22 files changed:
Doc/whatsnew/3.11.rst
Include/internal/pycore_strhex.h [new file with mode: 0644]
Include/pystrhex.h [deleted file]
Makefile.pre.in
Misc/NEWS.d/next/C API/2021-10-13-14-42-46.bpo-45434.INNEEt.rst [new file with mode: 0644]
Modules/Setup
Modules/_blake2/blake2b_impl.c
Modules/_blake2/blake2s_impl.c
Modules/_hashopenssl.c
Modules/_sha3/sha3module.c
Modules/binascii.c
Modules/md5module.c
Modules/sha1module.c
Modules/sha256module.c
Modules/sha512module.c
Objects/bytearrayobject.c
Objects/bytesobject.c
Objects/memoryobject.c
PCbuild/pythoncore.vcxproj
PCbuild/pythoncore.vcxproj.filters
Python/pystrhex.c
setup.py

index 21a46b4b72133a5b13df2ad9182bcd634dfb380c..21a0e1ab9c693f58f799ca35c9a4a58a98eb0146 100644 (file)
@@ -598,3 +598,7 @@ Removed
   since Python 3.3. Use ``PyUnicode_CopyCharacters()`` or ``memcpy()``
   (``wchar_t*`` string), and ``PyUnicode_Fill()`` functions instead.
   (Contributed by Victor Stinner in :issue:`41123`.)
+
+* Remove the ``pystrhex.h`` header file. It only contains private functions.
+  C extensions should only include the main ``<Python.h>`` header file.
+  (Contributed by Victor Stinner in :issue:`45434`.)
diff --git a/Include/internal/pycore_strhex.h b/Include/internal/pycore_strhex.h
new file mode 100644 (file)
index 0000000..1633671
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef Py_INTERNAL_STRHEX_H
+#define Py_INTERNAL_STRHEX_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
+
+// Returns a str() containing the hex representation of argbuf.
+PyAPI_FUNC(PyObject*) _Py_strhex(const
+    char* argbuf,
+    const Py_ssize_t arglen);
+
+// Returns a bytes() containing the ASCII hex representation of argbuf.
+PyAPI_FUNC(PyObject*) _Py_strhex_bytes(
+    const char* argbuf,
+    const Py_ssize_t arglen);
+
+// These variants include support for a separator between every N bytes:
+PyAPI_FUNC(PyObject*) _Py_strhex_with_sep(
+    const char* argbuf,
+    const Py_ssize_t arglen,
+    const PyObject* sep,
+    const int bytes_per_group);
+PyAPI_FUNC(PyObject*) _Py_strhex_bytes_with_sep(
+    const char* argbuf,
+    const Py_ssize_t arglen,
+    const PyObject* sep,
+    const int bytes_per_group);
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_INTERNAL_STRHEX_H */
diff --git a/Include/pystrhex.h b/Include/pystrhex.h
deleted file mode 100644 (file)
index a4f3630..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-#ifndef Py_STRHEX_H
-#define Py_STRHEX_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#ifndef Py_LIMITED_API
-/* Returns a str() containing the hex representation of argbuf. */
-PyAPI_FUNC(PyObject*) _Py_strhex(const char* argbuf, const Py_ssize_t arglen);
-/* Returns a bytes() containing the ASCII hex representation of argbuf. */
-PyAPI_FUNC(PyObject*) _Py_strhex_bytes(const char* argbuf, const Py_ssize_t arglen);
-/* These variants include support for a separator between every N bytes: */
-PyAPI_FUNC(PyObject*) _Py_strhex_with_sep(const char* argbuf, const Py_ssize_t arglen, const PyObject* sep, const int bytes_per_group);
-PyAPI_FUNC(PyObject*) _Py_strhex_bytes_with_sep(const char* argbuf, const Py_ssize_t arglen, const PyObject* sep, const int bytes_per_group);
-#endif /* !Py_LIMITED_API */
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* !Py_STRHEX_H */
index 4d4076f388260924e808a6fe86938a1e8b9bad68..041bc63e2fb8ed5b29dcbbd9505983b71ff63640 100644 (file)
@@ -1179,7 +1179,6 @@ PYTHON_HEADERS= \
                $(srcdir)/Include/pyport.h \
                $(srcdir)/Include/pystate.h \
                $(srcdir)/Include/pystrcmp.h \
-               $(srcdir)/Include/pystrhex.h \
                $(srcdir)/Include/pystrtod.h \
                $(srcdir)/Include/pythonrun.h \
                $(srcdir)/Include/pythread.h \
@@ -1271,6 +1270,7 @@ PYTHON_HEADERS= \
                $(srcdir)/Include/internal/pycore_pymem.h \
                $(srcdir)/Include/internal/pycore_pystate.h \
                $(srcdir)/Include/internal/pycore_runtime.h \
+               $(srcdir)/Include/internal/pycore_strhex.h \
                $(srcdir)/Include/internal/pycore_structseq.h \
                $(srcdir)/Include/internal/pycore_symtable.h \
                $(srcdir)/Include/internal/pycore_sysmodule.h \
diff --git a/Misc/NEWS.d/next/C API/2021-10-13-14-42-46.bpo-45434.INNEEt.rst b/Misc/NEWS.d/next/C API/2021-10-13-14-42-46.bpo-45434.INNEEt.rst
new file mode 100644 (file)
index 0000000..933f806
--- /dev/null
@@ -0,0 +1,3 @@
+Remove the ``pystrhex.h`` header file. It only contains private functions. C
+extensions should only include the main ``<Python.h>`` header file. Patch by
+Victor Stinner.
index 5e26472446677a277c03d361497854d2eb3ead84..ba8c154ee45868e401c010ec79f516f272dba745 100644 (file)
@@ -212,19 +212,23 @@ _symtable symtablemodule.c
 # OPENSSL=/path/to/openssl/directory
 # _ssl _ssl.c \
 #     -I$(OPENSSL)/include -L$(OPENSSL)/lib \
-#     -lssl -lcrypto
+#     -lssl -lcrypto \
+#     -DPy_BUILD_CORE_BUILTIN
 #_hashlib _hashopenssl.c \
 #     -I$(OPENSSL)/include -L$(OPENSSL)/lib \
-#     -lcrypto
+#     -lcrypto \
+#     -DPy_BUILD_CORE_BUILTIN
 
 # To statically link OpenSSL:
 # _ssl _ssl.c \
 #     -I$(OPENSSL)/include -L$(OPENSSL)/lib \
 #     -l:libssl.a -Wl,--exclude-libs,libssl.a \
-#     -l:libcrypto.a -Wl,--exclude-libs,libcrypto.a
+#     -l:libcrypto.a -Wl,--exclude-libs,libcrypto.a \
+#     -DPy_BUILD_CORE_BUILTIN
 #_hashlib _hashopenssl.c \
 #     -I$(OPENSSL)/include -L$(OPENSSL)/lib \
-#     -l:libcrypto.a -Wl,--exclude-libs,libcrypto.a
+#     -l:libcrypto.a -Wl,--exclude-libs,libcrypto.a \
+#     -DPy_BUILD_CORE_BUILTIN
 
 # The crypt module is now disabled by default because it breaks builds
 # on many systems (where -lcrypt is needed), e.g. Linux (I believe).
@@ -255,18 +259,18 @@ _symtable symtablemodule.c
 # The _md5 module implements the RSA Data Security, Inc. MD5
 # Message-Digest Algorithm, described in RFC 1321.
 
-#_md5 md5module.c
+#_md5 md5module.c -DPy_BUILD_CORE_BUILTIN
 
 
 # The _sha module implements the SHA checksum algorithms.
 # (NIST's Secure Hash Algorithms.)
-#_sha1 sha1module.c
+#_sha1 sha1module.c -DPy_BUILD_CORE_BUILTIN
 #_sha256 sha256module.c -DPy_BUILD_CORE_BUILTIN
 #_sha512 sha512module.c -DPy_BUILD_CORE_BUILTIN
-#_sha3 _sha3/sha3module.c
+#_sha3 _sha3/sha3module.c -DPy_BUILD_CORE_BUILTIN
 
 # _blake module
-#_blake2 _blake2/blake2module.c _blake2/blake2b_impl.c _blake2/blake2s_impl.c
+#_blake2 _blake2/blake2module.c _blake2/blake2b_impl.c _blake2/blake2s_impl.c -DPy_BUILD_CORE_BUILTIN
 
 # The _tkinter module.
 #
@@ -339,7 +343,7 @@ _symtable symtablemodule.c
 
 
 # Helper module for various ascii-encoders
-#binascii binascii.c
+#binascii binascii.c -DPy_BUILD_CORE_MODULE
 
 # Andrew Kuchling's zlib module.
 # This require zlib 1.1.3 (or later).
index 8b0d60d02ac7c8e5991b12d475ea569dde252e13..b16324e8f7aeb8e1f48a88f76004548582693d6a 100644 (file)
@@ -14,7 +14,7 @@
  */
 
 #include "Python.h"
-#include "pystrhex.h"
+#include "pycore_strhex.h"       // _Py_strhex()
 
 #include "../hashlib.h"
 #include "blake2ns.h"
index 7ab3917aa1a65598ee221ddf0cce2d9e2ef2fe8b..6b31a363ea27b92eefe28d00824840626f2111d7 100644 (file)
@@ -14,7 +14,7 @@
  */
 
 #include "Python.h"
-#include "pystrhex.h"
+#include "pycore_strhex.h"        // _Py_strhex()
 
 #include "../hashlib.h"
 #include "blake2ns.h"
index b9e68c05c3edbeec2d1d75f1f19333a2ffcf4f54..b4ba60baaac93d79ce7e0a3ba8152b4745e91ed0 100644 (file)
@@ -22,7 +22,7 @@
 
 #include "Python.h"
 #include "hashlib.h"
-#include "pystrhex.h"
+#include "pycore_strhex.h"        // _Py_strhex()
 
 /* EVP is the preferred interface to hashing in OpenSSL */
 #include <openssl/evp.h>
index 3974e0b6b47faac9946cbad3206910ae138e6954..a033c4e4525456a74bba1ef1d9ece60f08215fb3 100644 (file)
@@ -16,7 +16,7 @@
  */
 
 #include "Python.h"
-#include "pystrhex.h"
+#include "pycore_strhex.h"        // _Py_strhex()
 #include "../hashlib.h"
 
 /* **************************************************************************
index db960dcdcb0b60ba19c6ae9d0bea9914244fd110..ef209881494ac586f6cbd3f71ab30fd80ee559c0 100644 (file)
@@ -56,9 +56,9 @@
 #define PY_SSIZE_T_CLEAN
 
 #include "Python.h"
-#include "pystrhex.h"
+#include "pycore_strhex.h"        // _Py_strhex_bytes_with_sep()
 #ifdef USE_ZLIB_CRC32
-#include "zlib.h"
+#  include "zlib.h"
 #endif
 
 typedef struct binascii_state {
index b50d2e5b0bdb71039e91fc8eb0b22bdd6e3a2eb8..4d03f6b844b33206740ba264227e8a091cfee7e0 100644 (file)
@@ -18,7 +18,7 @@
 
 #include "Python.h"
 #include "hashlib.h"
-#include "pystrhex.h"
+#include "pycore_strhex.h"        // _Py_strhex()
 
 /*[clinic input]
 module _md5
index d186aa460a07b1986a088650c26f5edcb53cef13..153bc12a8bd743ef4e6f78aeb810f99037027239 100644 (file)
@@ -18,7 +18,7 @@
 
 #include "Python.h"
 #include "hashlib.h"
-#include "pystrhex.h"
+#include "pycore_strhex.h"        // _Py_strhex()
 
 /*[clinic input]
 module _sha1
index fac7073d951d2df931e6b214e1f0fe3e78afbb4c..5858071db4e13334e2beb8c855f353624da08eae 100644 (file)
@@ -18,9 +18,9 @@
 
 #include "Python.h"
 #include "pycore_bitutils.h"      // _Py_bswap32()
+#include "pycore_strhex.h"        // _Py_strhex()
 #include "structmember.h"         // PyMemberDef
 #include "hashlib.h"
-#include "pystrhex.h"
 
 /*[clinic input]
 module _sha256
index 4ea2fc16de05266a516f4414074969ed0d1acb57..e50b69be4617d9b11f1dde6b32ea8e2eed239df8 100644 (file)
@@ -18,9 +18,9 @@
 
 #include "Python.h"
 #include "pycore_bitutils.h"      // _Py_bswap64()
+#include "pycore_strhex.h"        // _Py_strhex()
 #include "structmember.h"         // PyMemberDef
 #include "hashlib.h"
-#include "pystrhex.h"
 
 /*[clinic input]
 module _sha512
index 1ab9621b1f2656a150e6ebb60ae54da01336dff7..a6009854221ff555f6473869eca8093fcdabcd04 100644 (file)
@@ -4,9 +4,9 @@
 #include "Python.h"
 #include "pycore_abstract.h"      // _PyIndex_Check()
 #include "pycore_bytes_methods.h"
-#include "pycore_object.h"
+#include "pycore_object.h"        // _PyObject_GC_UNTRACK()
+#include "pycore_strhex.h"        // _Py_strhex_with_sep()
 #include "bytesobject.h"
-#include "pystrhex.h"
 
 /*[clinic input]
 class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
index bc0b075cf405b301ff0cb1e922c810f87e86424f..1163cf00034b2820179e95407a4661314e5e87da 100644 (file)
@@ -10,8 +10,8 @@
 #include "pycore_initconfig.h"    // _PyStatus_OK()
 #include "pycore_object.h"        // _PyObject_GC_TRACK
 #include "pycore_pymem.h"         // PYMEM_CLEANBYTE
+#include "pycore_strhex.h"        // _Py_strhex_with_sep()
 
-#include "pystrhex.h"
 #include <stddef.h>
 
 /*[clinic input]
index 913d358062219bb052114b71ca1621fd55ebe154..6257455d34715f8bea34a82ff3b3e917909eaf12 100644 (file)
  */
 
 #include "Python.h"
-#include "pycore_abstract.h"   // _PyIndex_Check()
-#include "pycore_object.h"
-#include "pystrhex.h"
-#include <stddef.h>
+#include "pycore_abstract.h"      // _PyIndex_Check()
+#include "pycore_object.h"        // _PyObject_GC_UNTRACK()
+#include "pycore_strhex.h"        // _Py_strhex_with_sep()
+#include <stddef.h>               // offsetof()
 
 /*[clinic input]
 class memoryview "PyMemoryViewObject *" "&PyMemoryView_Type"
index b8cadf469355f4f0835cdc3909a65d97f3e0ff8a..d0e1b52a4927c1f789a6a6997c2176ca49b44f57 100644 (file)
     <ClInclude Include="..\Include\internal\pycore_pymem.h" />
     <ClInclude Include="..\Include\internal\pycore_pystate.h" />
     <ClInclude Include="..\Include\internal\pycore_runtime.h" />
+    <ClInclude Include="..\Include\internal\pycore_strhex.h" />
     <ClInclude Include="..\Include\internal\pycore_structseq.h" />
     <ClInclude Include="..\Include\internal\pycore_sysmodule.h" />
     <ClInclude Include="..\Include\internal\pycore_symtable.h" />
     <ClInclude Include="..\Include\pyport.h" />
     <ClInclude Include="..\Include\pystate.h" />
     <ClInclude Include="..\Include\pystrcmp.h" />
-    <ClInclude Include="..\Include\pystrhex.h" />
     <ClInclude Include="..\Include\pystrtod.h" />
     <ClInclude Include="..\Include\pythonrun.h" />
     <ClInclude Include="..\Include\pythread.h" />
index 4eccf4f43d1ad2afdf1d968c55343c606418e303..9370f405132c36bd2ba97682fcf14062eaa08643 100644 (file)
     <ClInclude Include="..\Include\pystrtod.h">
       <Filter>Include</Filter>
     </ClInclude>
-    <ClInclude Include="..\Include\pystrhex.h">
-      <Filter>Include</Filter>
-    </ClInclude>
     <ClInclude Include="..\Include\Python.h">
       <Filter>Include</Filter>
     </ClInclude>
     <ClInclude Include="..\Include\internal\pycore_runtime.h">
       <Filter>Include\internal</Filter>
     </ClInclude>
+    <ClInclude Include="..\Include\internal\pycore_strhex.h">
+      <Filter>Include\internal</Filter>
+    </ClInclude>
     <ClInclude Include="..\Include\internal\pycore_sysmodule.h">
       <Filter>Include\internal</Filter>
     </ClInclude>
index b74e57ad913b11109f32f7a183e932836a784696..f7fe3b6eb85d0604787d1d60b0b1daef23339590 100644 (file)
@@ -1,8 +1,8 @@
-/* bytes to hex implementation */
+/* Format bytes as hexadecimal */
 
 #include "Python.h"
+#include "pycore_strhex.h"       // _Py_strhex_with_sep()
 
-#include "pystrhex.h"
 
 static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
                                  const PyObject* sep, int bytes_per_sep_group,
index 039c96b0e4d7b606eb77cba1beea710f2ecd6f04..56c06cbddaf21d6c790829643e06d17b6c3f80ab 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -1709,12 +1709,12 @@ class PyBuildExt(build_ext):
 
         # Helper module for various ascii-encoders.  Uses zlib for an optimized
         # crc32 if we have it.  Otherwise binascii uses its own.
+        extra_compile_args = ['-DPy_BUILD_CORE_MODULE']
         if have_zlib:
-            extra_compile_args = ['-DUSE_ZLIB_CRC32']
+            extra_compile_args.append('-DUSE_ZLIB_CRC32')
             libraries = ['z']
             extra_link_args = zlib_extra_link_args
         else:
-            extra_compile_args = []
             libraries = []
             extra_link_args = []
         self.add(Extension('binascii', ['binascii.c'],
@@ -2469,6 +2469,7 @@ class PyBuildExt(build_ext):
             library_dirs=openssl_libdirs,
             libraries=openssl_libs,
             runtime_library_dirs=runtime_library_dirs,
+            extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
         )
 
         # This static linking is NOT OFFICIALLY SUPPORTED.
@@ -2530,27 +2531,29 @@ class PyBuildExt(build_ext):
         if "sha256" in configured:
             self.add(Extension(
                 '_sha256', ['sha256module.c'],
+                depends=['hashlib.h'],
                 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
-                depends=['hashlib.h']
             ))
 
         if "sha512" in configured:
             self.add(Extension(
                 '_sha512', ['sha512module.c'],
+                depends=['hashlib.h'],
                 extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
-                depends=['hashlib.h']
             ))
 
         if "md5" in configured:
             self.add(Extension(
                 '_md5', ['md5module.c'],
-                depends=['hashlib.h']
+                depends=['hashlib.h'],
+                extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
             ))
 
         if "sha1" in configured:
             self.add(Extension(
                 '_sha1', ['sha1module.c'],
-                depends=['hashlib.h']
+                depends=['hashlib.h'],
+                extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
             ))
 
         if "blake2" in configured:
@@ -2565,7 +2568,8 @@ class PyBuildExt(build_ext):
                     '_blake2/blake2b_impl.c',
                     '_blake2/blake2s_impl.c'
                 ],
-                depends=blake2_deps
+                depends=blake2_deps,
+                extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
             ))
 
         if "sha3" in configured:
@@ -2576,7 +2580,8 @@ class PyBuildExt(build_ext):
             self.add(Extension(
                 '_sha3',
                 ['_sha3/sha3module.c'],
-                depends=sha3_deps
+                depends=sha3_deps,
+                extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
             ))
 
     def detect_nis(self):