Skip to content

Commit 130893d

Browse files
authored
bpo-35081: Internal headers require Py_BUILD_CORE (pythonGH-10363)
* All internal header files now require Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN to be defined. * _json.c is now compiled with Py_BUILD_CORE_BUILTIN to access pycore_accu.h header. * Add an example to Modules/Setup to show how to build _json as a built-in module; it requires non trivial compiler options.
1 parent 0a18e05 commit 130893d

16 files changed

+48
-13
lines changed

Include/internal/pycore_accu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ extern "C" {
99
*** Its definition may be changed or removed at any moment.
1010
***/
1111

12+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
13+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
14+
#endif
15+
1216
/*
1317
* A two-level accumulator of unicode objects that avoids both the overhead
1418
* of keeping a huge number of small separate objects, and the quadratic

Include/internal/pycore_atomic.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
extern "C" {
55
#endif
66

7-
#ifndef Py_BUILD_CORE
8-
# error "Py_BUILD_CORE must be defined to include this header"
7+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
8+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
99
#endif
1010

1111
#include "dynamic_annotations.h"

Include/internal/pycore_ceval.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
extern "C" {
55
#endif
66

7+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
8+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
9+
#endif
10+
711
#include "pycore_atomic.h"
812
#include "pythread.h"
913

Include/internal/pycore_condvar.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef Py_INTERNAL_CONDVAR_H
22
#define Py_INTERNAL_CONDVAR_H
33

4+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
5+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
6+
#endif
7+
48
#ifndef _POSIX_THREADS
59
/* This means pthreads are not implemented in libc headers, hence the macro
610
not present in unistd.h. But they still can be implemented as an external

Include/internal/pycore_context.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#ifndef Py_INTERNAL_CONTEXT_H
22
#define Py_INTERNAL_CONTEXT_H
33

4+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
5+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
6+
#endif
47

58
#include "pycore_hamt.h"
69

7-
810
struct _pycontextobject {
911
PyObject_HEAD
1012
PyContext *ctx_prev;
@@ -37,5 +39,4 @@ struct _pycontexttokenobject {
3739
int _PyContext_Init(void);
3840
void _PyContext_Fini(void);
3941

40-
4142
#endif /* !Py_INTERNAL_CONTEXT_H */

Include/internal/pycore_getopt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef Py_INTERNAL_PYGETOPT_H
22
#define Py_INTERNAL_PYGETOPT_H
33

4+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
5+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
6+
#endif
7+
48
extern int _PyOS_opterr;
59
extern int _PyOS_optind;
610
extern wchar_t *_PyOS_optarg;

Include/internal/pycore_gil.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
extern "C" {
55
#endif
66

7+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
8+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
9+
#endif
10+
711
#include "pycore_condvar.h"
812
#include "pycore_atomic.h"
913

Include/internal/pycore_hamt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef Py_INTERNAL_HAMT_H
22
#define Py_INTERNAL_HAMT_H
33

4+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
5+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
6+
#endif
47

58
#define _Py_HAMT_MAX_TREE_DEPTH 7
69

Include/internal/pycore_hash.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef Py_INTERNAL_HASH_H
22
#define Py_INTERNAL_HASH_H
33

4+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
5+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
6+
#endif
7+
48
uint64_t _Py_KeyedHash(uint64_t, const char *, Py_ssize_t);
59

610
#endif

Include/internal/pycore_lifecycle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
extern "C" {
55
#endif
66

7-
#ifndef Py_BUILD_CORE
8-
# error "Py_BUILD_CORE must be defined to include this header"
7+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
8+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
99
#endif
1010

1111
PyAPI_FUNC(int) _Py_UnixMain(int argc, char **argv);

Include/internal/pycore_mem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
extern "C" {
55
#endif
66

7-
#ifndef Py_BUILD_CORE
8-
# error "Py_BUILD_CORE must be defined to include this header"
7+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
8+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN defined"
99
#endif
1010

1111
#include "objimpl.h"

Include/internal/pycore_pathconfig.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
extern "C" {
55
#endif
66

7-
#ifndef Py_BUILD_CORE
8-
# error "Py_BUILD_CORE must be defined to include this header"
7+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
8+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
99
#endif
1010

1111
PyAPI_FUNC(void) _Py_wstrlist_clear(

Include/internal/pycore_state.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
extern "C" {
55
#endif
66

7-
#ifndef Py_BUILD_CORE
8-
# error "Py_BUILD_CORE must be defined to include this header"
7+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
8+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
99
#endif
1010

1111
#include "pystate.h"

Include/internal/pycore_warnings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
extern "C" {
55
#endif
66

7+
#if !defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_BUILTIN)
8+
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
9+
#endif
10+
711
#include "object.h"
812

913
struct _warnings_runtime_state {

Modules/Setup

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ _symtable symtablemodule.c
180180
#_bisect _bisectmodule.c # Bisection algorithms
181181
#_heapq _heapqmodule.c # Heap queue algorithm
182182
#_asyncio _asynciomodule.c # Fast asyncio Future
183+
#_json -I$(srcdir)/Include/internal -DPy_BUILD_CORE_BUILTIN _json.c # _json speedups
183184

184185
#unicodedata unicodedata.c # static Unicode character database
185186

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,9 @@ def detect_modules(self):
678678
# atexit
679679
exts.append( Extension("atexit", ["atexitmodule.c"]) )
680680
# _json speedups
681-
exts.append( Extension("_json", ["_json.c"]) )
681+
exts.append( Extension("_json", ["_json.c"],
682+
# pycore_accu.h requires Py_BUILD_CORE_BUILTIN
683+
extra_compile_args=['-DPy_BUILD_CORE_BUILTIN']) )
682684
# Python C API test module
683685
exts.append( Extension('_testcapi', ['_testcapimodule.c'],
684686
depends=['testcapi_long.h']) )

0 commit comments

Comments
 (0)