[Python-checkins] gh-108220: Internal header files require Py_BUILD_CORE to be defined (#108221)

vstinner webhook-mailer at python.org
Mon Aug 21 13:15:56 EDT 2023


https://github.com/python/cpython/commit/21c0844742cf15db8e56e8848ecbb2e25f314aed
commit: 21c0844742cf15db8e56e8848ecbb2e25f314aed
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2023-08-21T19:15:52+02:00
summary:

gh-108220: Internal header files require Py_BUILD_CORE to be defined (#108221)

* pycore_intrinsics.h does nothing if included twice
  (add #ifndef and #define).
* Update Tools/cases_generator/generate_cases.py to generate the
  Py_BUILD_CORE test.
* _bz2, _lzma, _opcode and zlib extensions now define the
  Py_BUILD_CORE_MODULE macro to use internal headers
  (pycore_code.h, pycore_intrinsics.h and pycore_blocks_output_buffer.h).

files:
M Include/internal/pycore_blocks_output_buffer.h
M Include/internal/pycore_code.h
M Include/internal/pycore_codecs.h
M Include/internal/pycore_emscripten_signal.h
M Include/internal/pycore_fileutils.h
M Include/internal/pycore_fileutils_windows.h
M Include/internal/pycore_frame.h
M Include/internal/pycore_import.h
M Include/internal/pycore_instruments.h
M Include/internal/pycore_intrinsics.h
M Include/internal/pycore_opcode_metadata.h
M Modules/_bz2module.c
M Modules/_lzmamodule.c
M Modules/_opcode.c
M Modules/zlibmodule.c
M Tools/cases_generator/generate_cases.py

diff --git a/Include/internal/pycore_blocks_output_buffer.h b/Include/internal/pycore_blocks_output_buffer.h
index 28cf6fba4eeba..573e10359b7bd 100644
--- a/Include/internal/pycore_blocks_output_buffer.h
+++ b/Include/internal/pycore_blocks_output_buffer.h
@@ -40,6 +40,10 @@ extern "C" {
 
 #include "Python.h"
 
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
+
 typedef struct {
     // List of bytes objects
     PyObject *list;
@@ -314,4 +318,4 @@ _BlocksOutputBuffer_OnError(_BlocksOutputBuffer *buffer)
 #ifdef __cplusplus
 }
 #endif
-#endif /* Py_INTERNAL_BLOCKS_OUTPUT_BUFFER_H */
\ No newline at end of file
+#endif /* Py_INTERNAL_BLOCKS_OUTPUT_BUFFER_H */
diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h
index 00099376635e9..f5127a8114435 100644
--- a/Include/internal/pycore_code.h
+++ b/Include/internal/pycore_code.h
@@ -4,6 +4,10 @@
 extern "C" {
 #endif
 
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
+
 #define CODE_MAX_WATCHERS 8
 
 /* PEP 659
diff --git a/Include/internal/pycore_codecs.h b/Include/internal/pycore_codecs.h
index a2465192eacd5..a2a7151d50ade 100644
--- a/Include/internal/pycore_codecs.h
+++ b/Include/internal/pycore_codecs.h
@@ -4,6 +4,10 @@
 extern "C" {
 #endif
 
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
+
 extern PyObject* _PyCodec_Lookup(const char *encoding);
 
 /* Text codec specific encoding and decoding API.
diff --git a/Include/internal/pycore_emscripten_signal.h b/Include/internal/pycore_emscripten_signal.h
index 8b3287d85da4b..d1bcb9a92c772 100644
--- a/Include/internal/pycore_emscripten_signal.h
+++ b/Include/internal/pycore_emscripten_signal.h
@@ -3,6 +3,10 @@
 
 #if defined(__EMSCRIPTEN__)
 
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
+
 void
 _Py_CheckEmscriptenSignals(void);
 
diff --git a/Include/internal/pycore_fileutils.h b/Include/internal/pycore_fileutils.h
index 0ed139f79b142..25b383b9780ad 100644
--- a/Include/internal/pycore_fileutils.h
+++ b/Include/internal/pycore_fileutils.h
@@ -5,7 +5,7 @@ extern "C" {
 #endif
 
 #ifndef Py_BUILD_CORE
-#  error "Py_BUILD_CORE must be defined to include this header"
+#  error "this header requires Py_BUILD_CORE define"
 #endif
 
 #include <locale.h>   /* struct lconv */
diff --git a/Include/internal/pycore_fileutils_windows.h b/Include/internal/pycore_fileutils_windows.h
index e804d385e7670..b79aa9fb46537 100644
--- a/Include/internal/pycore_fileutils_windows.h
+++ b/Include/internal/pycore_fileutils_windows.h
@@ -5,7 +5,7 @@ extern "C" {
 #endif
 
 #ifndef Py_BUILD_CORE
-#  error "Py_BUILD_CORE must be defined to include this header"
+#  error "this header requires Py_BUILD_CORE define"
 #endif
 
 #ifdef MS_WINDOWS
diff --git a/Include/internal/pycore_frame.h b/Include/internal/pycore_frame.h
index 0dc2a1814cb1d..ae77367f6a3c9 100644
--- a/Include/internal/pycore_frame.h
+++ b/Include/internal/pycore_frame.h
@@ -4,6 +4,10 @@
 extern "C" {
 #endif
 
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
+
 #include <stdbool.h>
 #include <stddef.h>               // offsetof()
 #include "pycore_code.h"          // STATS
diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h
index 077508e6c58f6..34f572bd59296 100644
--- a/Include/internal/pycore_import.h
+++ b/Include/internal/pycore_import.h
@@ -5,6 +5,10 @@
 extern "C" {
 #endif
 
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
+
 #include "pycore_hashtable.h"     // _Py_hashtable_t
 #include "pycore_time.h"          // _PyTime_t
 
diff --git a/Include/internal/pycore_instruments.h b/Include/internal/pycore_instruments.h
index 56de9f8717148..e15447acec2c0 100644
--- a/Include/internal/pycore_instruments.h
+++ b/Include/internal/pycore_instruments.h
@@ -1,7 +1,9 @@
-
 #ifndef Py_INTERNAL_INSTRUMENT_H
 #define Py_INTERNAL_INSTRUMENT_H
 
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
 
 #include "pycore_bitutils.h"      // _Py_popcount32
 #include "pycore_frame.h"
diff --git a/Include/internal/pycore_intrinsics.h b/Include/internal/pycore_intrinsics.h
index 37d4efc12bb77..3a8dd95cff8e5 100644
--- a/Include/internal/pycore_intrinsics.h
+++ b/Include/internal/pycore_intrinsics.h
@@ -1,3 +1,9 @@
+#ifndef Py_INTERNAL_INTRINSIC_H
+#define Py_INTERNAL_INTRINSIC_H
+
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
 
 /* Unary Functions: */
 #define INTRINSIC_1_INVALID                      0
@@ -40,3 +46,5 @@ typedef struct {
 
 extern const intrinsic_func1_info _PyIntrinsics_UnaryFunctions[];
 extern const intrinsic_func2_info _PyIntrinsics_BinaryFunctions[];
+
+#endif  // !Py_INTERNAL_INTRINSIC_H
diff --git a/Include/internal/pycore_opcode_metadata.h b/Include/internal/pycore_opcode_metadata.h
index 396d194ed2734..fab91e611b617 100644
--- a/Include/internal/pycore_opcode_metadata.h
+++ b/Include/internal/pycore_opcode_metadata.h
@@ -3,6 +3,10 @@
 //   Python/bytecodes.c
 // Do not edit!
 
+#ifndef Py_BUILD_CORE
+#  error "this header requires Py_BUILD_CORE define"
+#endif
+
 #include <stdbool.h>
 
 
diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c
index 0a84f25ca4cbe..3d0d4ee5e79c2 100644
--- a/Modules/_bz2module.c
+++ b/Modules/_bz2module.c
@@ -1,5 +1,9 @@
 /* _bz2 - Low-level Python interface to libbzip2. */
 
+#ifndef Py_BUILD_CORE_BUILTIN
+#  define Py_BUILD_CORE_MODULE 1
+#endif
+
 #include "Python.h"
 
 #include <bzlib.h>
diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c
index c548f8fa3839e..eb90c308d16d1 100644
--- a/Modules/_lzmamodule.c
+++ b/Modules/_lzmamodule.c
@@ -5,6 +5,10 @@
 
 */
 
+#ifndef Py_BUILD_CORE_BUILTIN
+#  define Py_BUILD_CORE_MODULE 1
+#endif
+
 #include "Python.h"
 
 
diff --git a/Modules/_opcode.c b/Modules/_opcode.c
index 4f85e7ee26de2..3e13dbb6edc26 100644
--- a/Modules/_opcode.c
+++ b/Modules/_opcode.c
@@ -1,3 +1,7 @@
+#ifndef Py_BUILD_CORE_BUILTIN
+#  define Py_BUILD_CORE_MODULE 1
+#endif
+
 #include "Python.h"
 #include "compile.h"
 #include "opcode.h"
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index a98a37adadcff..9b76afa0e56f7 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -3,6 +3,10 @@
 
 /* Windows users:  read Python's PCbuild\readme.txt */
 
+#ifndef Py_BUILD_CORE_BUILTIN
+#  define Py_BUILD_CORE_MODULE 1
+#endif
+
 #include "Python.h"
 
 #include "zlib.h"
diff --git a/Tools/cases_generator/generate_cases.py b/Tools/cases_generator/generate_cases.py
index 7b1880b98a8fe..c3b729df44a6a 100644
--- a/Tools/cases_generator/generate_cases.py
+++ b/Tools/cases_generator/generate_cases.py
@@ -9,6 +9,7 @@
 import os
 import posixpath
 import sys
+import textwrap
 import typing
 from collections.abc import Iterator
 
@@ -403,6 +404,12 @@ def write_metadata(self, metadata_filename: str, pymetadata_filename: str) -> No
 
             self.write_provenance_header()
 
+            self.out.emit("\n" + textwrap.dedent("""
+                #ifndef Py_BUILD_CORE
+                #  error "this header requires Py_BUILD_CORE define"
+                #endif
+            """).strip())
+
             self.out.emit("\n#include <stdbool.h>")
 
             self.write_pseudo_instrs()



More information about the Python-checkins mailing list