Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47717d1

Browse files
authored
bpo-45434: Cleanup Python.h header file (GH-28883)
* Move limits.h include and UCHAR_MAX checks to pyport.h. * Move sanitizers macros to pyport.h. * Remove comment about <assert.h>: C extensions are built with NDEBUG automatically by Python.
1 parent 2f92e2a commit 47717d1

2 files changed

Lines changed: 39 additions & 52 deletions

File tree

Include/Python.h

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,50 @@
1+
// Entry point of the Python C API.
2+
// C extensions should only #include <Python.h>, and not include directly
3+
// the other Python header files included by <Python.h>.
4+
15
#ifndef Py_PYTHON_H
26
#define Py_PYTHON_H
3-
/* Since this is a "meta-include" file, no #ifdef __cplusplus / extern "C" { */
47

5-
/* Include nearly all Python header files */
8+
// Since this is a "meta-include" file, no #ifdef __cplusplus / extern "C" {
69

10+
// Include Python header files
711
#include "patchlevel.h"
812
#include "pyconfig.h"
913
#include "pymacconfig.h"
1014

11-
#include <limits.h>
12-
13-
#ifndef UCHAR_MAX
14-
#error "Something's broken. UCHAR_MAX should be defined in limits.h."
15-
#endif
16-
17-
#if UCHAR_MAX != 255
18-
#error "Python's source code assumes C's unsigned char is an 8-bit type."
19-
#endif
20-
2115
#if defined(__sgi) && !defined(_SGI_MP_SOURCE)
22-
#define _SGI_MP_SOURCE
16+
# define _SGI_MP_SOURCE
2317
#endif
2418

25-
#include <stdio.h>
19+
#include <stdio.h> // NULL, FILE*
2620
#ifndef NULL
2721
# error "Python.h requires that stdio.h define NULL."
2822
#endif
2923

30-
#include <string.h>
24+
#include <string.h> // memcpy()
3125
#ifdef HAVE_ERRNO_H
32-
#include <errno.h>
26+
# include <errno.h> // errno
3327
#endif
3428
#include <stdlib.h>
3529
#ifndef MS_WINDOWS
36-
#include <unistd.h>
30+
# include <unistd.h>
3731
#endif
38-
39-
/* For size_t? */
4032
#ifdef HAVE_STDDEF_H
41-
#include <stddef.h>
33+
// For size_t
34+
# include <stddef.h>
4235
#endif
4336

44-
/* CAUTION: Build setups should ensure that NDEBUG is defined on the
45-
* compiler command line when building Python in release mode; else
46-
* assert() calls won't be removed.
47-
*/
4837
#include <assert.h>
4938

5039
#include "pyport.h"
5140
#include "pymacro.h"
52-
53-
/* A convenient way for code to know if sanitizers are enabled. */
54-
#if defined(__has_feature)
55-
# if __has_feature(memory_sanitizer)
56-
# if !defined(_Py_MEMORY_SANITIZER)
57-
# define _Py_MEMORY_SANITIZER
58-
# endif
59-
# endif
60-
# if __has_feature(address_sanitizer)
61-
# if !defined(_Py_ADDRESS_SANITIZER)
62-
# define _Py_ADDRESS_SANITIZER
63-
# endif
64-
# endif
65-
#elif defined(__GNUC__)
66-
# if defined(__SANITIZE_ADDRESS__)
67-
# define _Py_ADDRESS_SANITIZER
68-
# endif
69-
#endif
70-
7141
#include "pymath.h"
7242
#include "pymem.h"
73-
7443
#include "object.h"
7544
#include "objimpl.h"
7645
#include "typeslots.h"
7746
#include "pyhash.h"
78-
7947
#include "cpython/pydebug.h"
80-
8148
#include "bytearrayobject.h"
8249
#include "bytesobject.h"
8350
#include "unicodeobject.h"
@@ -115,15 +82,12 @@
11582
#include "namespaceobject.h"
11683
#include "cpython/picklebufobject.h"
11784
#include "cpython/pytime.h"
118-
11985
#include "codecs.h"
12086
#include "pyerrors.h"
121-
12287
#include "cpython/initconfig.h"
12388
#include "pythread.h"
12489
#include "pystate.h"
12590
#include "context.h"
126-
12791
#include "modsupport.h"
12892
#include "compile.h"
12993
#include "pythonrun.h"
@@ -133,12 +97,9 @@
13397
#include "osmodule.h"
13498
#include "intrcheck.h"
13599
#include "import.h"
136-
137100
#include "abstract.h"
138101
#include "bltinmodule.h"
139-
140102
#include "eval.h"
141-
142103
#include "cpython/pyctype.h"
143104
#include "pystrtod.h"
144105
#include "pystrcmp.h"

Include/pyport.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55

66
#include <inttypes.h>
77

8+
#include <limits.h>
9+
#ifndef UCHAR_MAX
10+
# error "limits.h must define UCHAR_MAX"
11+
#endif
12+
#if UCHAR_MAX != 255
13+
# error "Python's source code assumes C's unsigned char is an 8-bit type"
14+
#endif
15+
816

917
/* Defines to build Python and its standard library:
1018
*
@@ -851,4 +859,22 @@ extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
851859
#endif
852860

853861

862+
/* A convenient way for code to know if sanitizers are enabled. */
863+
#if defined(__has_feature)
864+
# if __has_feature(memory_sanitizer)
865+
# if !defined(_Py_MEMORY_SANITIZER)
866+
# define _Py_MEMORY_SANITIZER
867+
# endif
868+
# endif
869+
# if __has_feature(address_sanitizer)
870+
# if !defined(_Py_ADDRESS_SANITIZER)
871+
# define _Py_ADDRESS_SANITIZER
872+
# endif
873+
# endif
874+
#elif defined(__GNUC__)
875+
# if defined(__SANITIZE_ADDRESS__)
876+
# define _Py_ADDRESS_SANITIZER
877+
# endif
878+
#endif
879+
854880
#endif /* Py_PYPORT_H */

0 commit comments

Comments
 (0)