Skip to content

Commit e45cb02

Browse files
committed
gh-106320: Create pycore_modsupport.h header file (#106355)
Remove the following functions from the C API, move them to the internal C API: add a new pycore_modsupport.h internal header file: * PyModule_CreateInitialized() * _PyArg_NoKwnames() * _Py_VaBuildStack() No longer export these functions.
1 parent 5ccbbe5 commit e45cb02

28 files changed

+117
-62
lines changed

Include/cpython/modsupport.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ PyAPI_FUNC(int) _PyArg_UnpackStack(
1111
...);
1212

1313
PyAPI_FUNC(int) _PyArg_NoKeywords(const char *funcname, PyObject *kwargs);
14-
PyAPI_FUNC(int) _PyArg_NoKwnames(const char *funcname, PyObject *kwnames);
1514
PyAPI_FUNC(int) _PyArg_NoPositional(const char *funcname, PyObject *args);
1615
#define _PyArg_NoKeywords(funcname, kwargs) \
1716
((kwargs) == NULL || _PyArg_NoKeywords((funcname), (kwargs)))
18-
#define _PyArg_NoKwnames(funcname, kwnames) \
19-
((kwnames) == NULL || _PyArg_NoKwnames((funcname), (kwnames)))
2017
#define _PyArg_NoPositional(funcname, args) \
2118
((args) == NULL || _PyArg_NoPositional((funcname), (args)))
2219

@@ -29,13 +26,6 @@ PyAPI_FUNC(int) _PyArg_CheckPositional(const char *, Py_ssize_t,
2926
((!_Py_ANY_VARARGS(max) && (min) <= (nargs) && (nargs) <= (max)) \
3027
|| _PyArg_CheckPositional((funcname), (nargs), (min), (max)))
3128

32-
PyAPI_FUNC(PyObject **) _Py_VaBuildStack(
33-
PyObject **small_stack,
34-
Py_ssize_t small_stack_len,
35-
const char *format,
36-
va_list va,
37-
Py_ssize_t *p_nargs);
38-
3929
typedef struct _PyArg_Parser {
4030
int initialized;
4131
const char *format;
@@ -83,5 +73,3 @@ PyAPI_FUNC(PyObject * const *) _PyArg_UnpackKeywordsWithVararg(
8373
(minpos) <= (nargs) && (nargs) <= (maxpos) && (args) != NULL) ? (args) : \
8474
_PyArg_UnpackKeywords((args), (nargs), (kwargs), (kwnames), (parser), \
8575
(minpos), (maxpos), (minkw), (buf)))
86-
87-
PyAPI_FUNC(PyObject *) _PyModule_CreateInitialized(PyModuleDef*, int apiver);

Include/cpython/pyerrors.h

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -91,39 +91,21 @@ typedef PyOSErrorObject PyWindowsErrorObject;
9191
/* Error handling definitions */
9292

9393
PyAPI_FUNC(void) _PyErr_SetKeyError(PyObject *);
94-
PyAPI_FUNC(_PyErr_StackItem*) _PyErr_GetTopmostException(PyThreadState *tstate);
95-
PyAPI_FUNC(PyObject*) _PyErr_GetHandledException(PyThreadState *);
96-
PyAPI_FUNC(void) _PyErr_SetHandledException(PyThreadState *, PyObject *);
97-
PyAPI_FUNC(void) _PyErr_GetExcInfo(PyThreadState *, PyObject **, PyObject **, PyObject **);
9894

9995
/* Context manipulation (PEP 3134) */
10096

10197
Py_DEPRECATED(3.12) PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject *);
10298
PyAPI_FUNC(void) _PyErr_ChainExceptions1(PyObject *);
10399

104-
/* Like PyErr_Format(), but saves current exception as __context__ and
105-
__cause__.
106-
*/
107-
PyAPI_FUNC(PyObject *) _PyErr_FormatFromCause(
108-
PyObject *exception,
109-
const char *format, /* ASCII-encoded string */
110-
...
111-
);
112-
113100
/* In exceptions.c */
114101

115-
PyAPI_FUNC(int) _PyException_AddNote(
116-
PyObject *exc,
117-
PyObject *note);
118-
119102
PyAPI_FUNC(PyObject*) PyUnstable_Exc_PrepReraiseStar(
120103
PyObject *orig,
121104
PyObject *excs);
122105

123106
/* In signalmodule.c */
124107

125108
int PySignal_SetWakeupFd(int fd);
126-
PyAPI_FUNC(int) _PyErr_CheckSignals(void);
127109

128110
/* Support for adding program text to SyntaxErrors */
129111

@@ -143,18 +125,6 @@ PyAPI_FUNC(PyObject *) PyErr_ProgramTextObject(
143125
PyObject *filename,
144126
int lineno);
145127

146-
PyAPI_FUNC(PyObject *) _PyErr_ProgramDecodedTextObject(
147-
PyObject *filename,
148-
int lineno,
149-
const char* encoding);
150-
151-
PyAPI_FUNC(PyObject *) _PyUnicodeTranslateError_Create(
152-
PyObject *object,
153-
Py_ssize_t start,
154-
Py_ssize_t end,
155-
const char *reason /* UTF-8 encoded string */
156-
);
157-
158128
PyAPI_FUNC(void) _PyErr_WriteUnraisableMsg(
159129
const char *err_msg,
160130
PyObject *obj);
@@ -163,16 +133,4 @@ PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalErrorFunc(
163133
const char *func,
164134
const char *message);
165135

166-
PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalErrorFormat(
167-
const char *func,
168-
const char *format,
169-
...);
170-
171-
extern PyObject *_PyErr_SetImportErrorWithNameFrom(
172-
PyObject *,
173-
PyObject *,
174-
PyObject *,
175-
PyObject *);
176-
177-
178136
#define Py_FatalError(message) _Py_FatalErrorFunc(__func__, (message))

Include/internal/pycore_modsupport.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef Py_INTERNAL_MODSUPPORT_H
2+
#define Py_INTERNAL_MODSUPPORT_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_BUILD_CORE
8+
# error "this header requires Py_BUILD_CORE define"
9+
#endif
10+
11+
12+
extern int _PyArg_NoKwnames(const char *funcname, PyObject *kwnames);
13+
#define _PyArg_NoKwnames(funcname, kwnames) \
14+
((kwnames) == NULL || _PyArg_NoKwnames((funcname), (kwnames)))
15+
16+
extern PyObject ** _Py_VaBuildStack(
17+
PyObject **small_stack,
18+
Py_ssize_t small_stack_len,
19+
const char *format,
20+
va_list va,
21+
Py_ssize_t *p_nargs);
22+
23+
extern PyObject* _PyModule_CreateInitialized(PyModuleDef*, int apiver);
24+
25+
#ifdef __cplusplus
26+
}
27+
#endif
28+
#endif // !Py_INTERNAL_MODSUPPORT_H
29+

Include/internal/pycore_pyerrors.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,54 @@ extern "C" {
99
#endif
1010

1111

12+
/* Error handling definitions */
13+
14+
PyAPI_FUNC(_PyErr_StackItem*) _PyErr_GetTopmostException(PyThreadState *tstate);
15+
PyAPI_FUNC(PyObject*) _PyErr_GetHandledException(PyThreadState *);
16+
PyAPI_FUNC(void) _PyErr_SetHandledException(PyThreadState *, PyObject *);
17+
PyAPI_FUNC(void) _PyErr_GetExcInfo(PyThreadState *, PyObject **, PyObject **, PyObject **);
18+
19+
/* Like PyErr_Format(), but saves current exception as __context__ and
20+
__cause__.
21+
*/
22+
PyAPI_FUNC(PyObject *) _PyErr_FormatFromCause(
23+
PyObject *exception,
24+
const char *format, /* ASCII-encoded string */
25+
...
26+
);
27+
28+
PyAPI_FUNC(int) _PyException_AddNote(
29+
PyObject *exc,
30+
PyObject *note);
31+
32+
PyAPI_FUNC(int) _PyErr_CheckSignals(void);
33+
34+
/* Support for adding program text to SyntaxErrors */
35+
36+
PyAPI_FUNC(PyObject *) _PyErr_ProgramDecodedTextObject(
37+
PyObject *filename,
38+
int lineno,
39+
const char* encoding);
40+
41+
PyAPI_FUNC(PyObject *) _PyUnicodeTranslateError_Create(
42+
PyObject *object,
43+
Py_ssize_t start,
44+
Py_ssize_t end,
45+
const char *reason /* UTF-8 encoded string */
46+
);
47+
48+
PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalErrorFormat(
49+
const char *func,
50+
const char *format,
51+
...);
52+
53+
extern PyObject *_PyErr_SetImportErrorWithNameFrom(
54+
PyObject *,
55+
PyObject *,
56+
PyObject *,
57+
PyObject *);
58+
59+
1260
/* runtime lifecycle */
1361

1462
extern PyStatus _PyErr_InitTypes(PyInterpreterState *);

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,7 @@ PYTHON_HEADERS= \
17611761
$(srcdir)/Include/internal/pycore_intrinsics.h \
17621762
$(srcdir)/Include/internal/pycore_list.h \
17631763
$(srcdir)/Include/internal/pycore_long.h \
1764+
$(srcdir)/Include/internal/pycore_modsupport.h \
17641765
$(srcdir)/Include/internal/pycore_moduleobject.h \
17651766
$(srcdir)/Include/internal/pycore_namespace.h \
17661767
$(srcdir)/Include/internal/pycore_object.h \

Modules/_io/bufferedio.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "Python.h"
1111
#include "pycore_call.h" // _PyObject_CallNoArgs()
1212
#include "pycore_object.h"
13+
#include "pycore_pyerrors.h" // _Py_FatalErrorFormat()
1314
#include "structmember.h" // PyMemberDef
1415
#include "_iomodule.h"
1516

Modules/_operator.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "Python.h"
2+
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
23
#include "pycore_moduleobject.h" // _PyModule_GetState()
3-
#include "structmember.h" // PyMemberDef
44
#include "pycore_runtime.h" // _Py_ID()
5+
6+
#include "structmember.h" // PyMemberDef
57
#include "clinic/_operator.c.h"
68

79
typedef struct {

Modules/_sqlite/cursor.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@
2121
* 3. This notice may not be removed or altered from any source distribution.
2222
*/
2323

24+
#ifndef Py_BUILD_CORE_BUILTIN
25+
# define Py_BUILD_CORE_MODULE 1
26+
#endif
27+
2428
#include "cursor.h"
2529
#include "microprotocols.h"
2630
#include "module.h"
2731
#include "util.h"
2832

33+
#include "pycore_pyerrors.h" // _PyErr_FormatFromCause()
34+
2935
typedef enum {
3036
TYPE_LONG,
3137
TYPE_FLOAT,

Objects/boolobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* Boolean type, a subtype of int */
22

33
#include "Python.h"
4-
#include "pycore_object.h" // _Py_FatalRefcountError()
5-
#include "pycore_long.h" // FALSE_TAG TRUE_TAG
4+
#include "pycore_long.h" // FALSE_TAG TRUE_TAG
5+
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
6+
#include "pycore_object.h" // _Py_FatalRefcountError()
67
#include "pycore_runtime.h" // _Py_ID()
78

89
#include <stddef.h>

Objects/call.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "pycore_call.h" // _PyObject_CallNoArgsTstate()
33
#include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate()
44
#include "pycore_dict.h" // _PyDict_FromItems()
5+
#include "pycore_modsupport.h" // _Py_VaBuildStack()
56
#include "pycore_object.h" // _PyCFunctionWithKeywords_TrampolineCall()
67
#include "pycore_pyerrors.h" // _PyErr_Occurred()
78
#include "pycore_pystate.h" // _PyThreadState_GET()

0 commit comments

Comments
 (0)