Skip to content

Commit f1ae706

Browse files
authored
gh-107211: No longer export internal functions (7) (#108425)
No longer export _PyUnicode_FromId() internal C API function. Change comment style to "// comment" and add comment explaining why other functions have to be exported. Update Tools/build/generate_token.py to update Include/internal/pycore_token.h comments.
1 parent 52c6a6e commit f1ae706

10 files changed

+69
-46
lines changed

Include/internal/pycore_pylifecycle.h

+4
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,20 @@ extern int _Py_FdIsInteractive(FILE *fp, PyObject *filename);
9898
extern const char* _Py_gitidentifier(void);
9999
extern const char* _Py_gitversion(void);
100100

101+
// Export for '_asyncio' shared extension
101102
PyAPI_FUNC(int) _Py_IsInterpreterFinalizing(PyInterpreterState *interp);
102103

103104
/* Random */
104105
extern int _PyOS_URandom(void *buffer, Py_ssize_t size);
106+
105107
// Export for '_random' shared extension
106108
PyAPI_FUNC(int) _PyOS_URandomNonblock(void *buffer, Py_ssize_t size);
107109

108110
/* Legacy locale support */
109111
extern int _Py_CoerceLegacyLocale(int warn);
110112
extern int _Py_LegacyLocaleDetected(int warn);
113+
114+
// Export for 'readline' shared extension
111115
PyAPI_FUNC(char*) _Py_SetLocaleFromEnv(int category);
112116

113117
#ifdef __cplusplus

Include/internal/pycore_pystate.h

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ _Py_ThreadCanHandleSignals(PyInterpreterState *interp)
6666
#if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE)
6767
extern _Py_thread_local PyThreadState *_Py_tss_tstate;
6868
#endif
69+
70+
// Export for most shared extensions, used via _PyThreadState_GET() static
71+
// inline function.
6972
PyAPI_FUNC(PyThreadState *) _PyThreadState_GetCurrent(void);
7073

7174
/* Get the current Python thread state.

Include/internal/pycore_runtime.h

+3
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ typedef struct pyruntimestate {
272272

273273
/* other API */
274274

275+
// Export _PyRuntime for shared extensions which use it in static inline
276+
// functions for best performance, like _Py_IsMainThread() or _Py_ID().
277+
// It's also made accessible for debuggers and profilers.
275278
PyAPI_DATA(_PyRuntimeState) _PyRuntime;
276279

277280
extern PyStatus _PyRuntimeState_Init(_PyRuntimeState *runtime);

Include/internal/pycore_setobject.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
// Export for 'pickle' shared extension
11+
// Export for '_pickle' shared extension
1212
PyAPI_FUNC(int) _PySet_NextEntry(
1313
PyObject *set,
1414
Py_ssize_t *pos,
1515
PyObject **key,
1616
Py_hash_t *hash);
1717

18-
// Export for 'pickle' shared extension
18+
// Export for '_pickle' shared extension
1919
PyAPI_FUNC(int) _PySet_Update(PyObject *set, PyObject *iterable);
2020

21-
// Export _PySet_Dummy for the gdb plugin's benefit
21+
// Export for the gdb plugin's (python-gdb.py) benefit
2222
PyAPI_DATA(PyObject *) _PySet_Dummy;
2323

2424
#ifdef __cplusplus

Include/internal/pycore_sysmodule.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ extern int _PySys_Audit(
1414
const char *argFormat,
1515
...);
1616

17-
/* We want minimal exposure of this function, so use extern rather than
18-
PyAPI_FUNC() to not export the symbol. */
17+
// _PySys_ClearAuditHooks() must not be exported: use extern rather than
18+
// PyAPI_FUNC(). We want minimal exposure of this function.
1919
extern void _PySys_ClearAuditHooks(PyThreadState *tstate);
2020

2121
extern int _PySys_SetAttr(PyObject *, PyObject *);

Include/internal/pycore_token.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Auto-generated by Tools/build/generate_token.py */
1+
// Auto-generated by Tools/build/generate_token.py
22

33
/* Token types */
44
#ifndef Py_INTERNAL_TOKEN_H
@@ -94,7 +94,7 @@ extern "C" {
9494
(x) == FSTRING_MIDDLE)
9595

9696

97-
// Symbols exported for test_peg_generator
97+
// Export these 4 symbols for 'test_peg_generator'
9898
PyAPI_DATA(const char * const) _PyParser_TokenNames[]; /* Token names */
9999
PyAPI_FUNC(int) _PyToken_OneChar(int);
100100
PyAPI_FUNC(int) _PyToken_TwoChars(int, int);

Include/internal/pycore_typeobject.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ extern static_builtin_state * _PyStaticType_GetState(PyInterpreterState *, PyTyp
114114
extern void _PyStaticType_ClearWeakRefs(PyInterpreterState *, PyTypeObject *type);
115115
extern void _PyStaticType_Dealloc(PyInterpreterState *, PyTypeObject *);
116116

117-
// Export for 'math' shared extension via _PyType_IsReady() function
117+
// Export for 'math' shared extension, used via _PyType_IsReady() static inline
118+
// function
118119
PyAPI_FUNC(PyObject *) _PyType_GetDict(PyTypeObject *);
120+
119121
extern PyObject * _PyType_GetBases(PyTypeObject *type);
120122
extern PyObject * _PyType_GetMRO(PyTypeObject *type);
121123
extern PyObject* _PyType_GetSubclasses(PyTypeObject *);

Include/internal/pycore_unicodeobject.h

+45-34
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ extern int _PyUnicode_IsCased(Py_UCS4 ch);
2424

2525
/* --- Unicode API -------------------------------------------------------- */
2626

27+
// Export for '_json' shared extension
2728
PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
2829
PyObject *op,
2930
int check_content);
3031

3132
extern void _PyUnicode_ExactDealloc(PyObject *op);
3233
extern Py_ssize_t _PyUnicode_InternedSize(void);
3334

34-
/* Get a copy of a Unicode string. */
35+
// Get a copy of a Unicode string.
36+
// Export for '_datetime' shared extension.
3537
PyAPI_FUNC(PyObject*) _PyUnicode_Copy(
36-
PyObject *unicode
37-
);
38+
PyObject *unicode);
3839

3940
/* Unsafe version of PyUnicode_Fill(): don't check arguments and so may crash
4041
if parameters are invalid (e.g. if length is longer than the string). */
@@ -93,11 +94,13 @@ typedef struct {
9394
unsigned char readonly;
9495
} _PyUnicodeWriter ;
9596

96-
/* Initialize a Unicode writer.
97-
*
98-
* By default, the minimum buffer size is 0 character and overallocation is
99-
* disabled. Set min_length, min_char and overallocate attributes to control
100-
* the allocation of the buffer. */
97+
// Initialize a Unicode writer.
98+
//
99+
// By default, the minimum buffer size is 0 character and overallocation is
100+
// disabled. Set min_length, min_char and overallocate attributes to control
101+
// the allocation of the buffer.
102+
//
103+
// Export the _PyUnicodeWriter API for '_multibytecodec' shared extension.
101104
PyAPI_FUNC(void)
102105
_PyUnicodeWriter_Init(_PyUnicodeWriter *writer);
103106

@@ -204,33 +207,36 @@ extern PyObject* _PyUnicode_EncodeUTF7(
204207

205208
/* --- UTF-8 Codecs ------------------------------------------------------- */
206209

210+
// Export for '_tkinter' shared extension.
207211
PyAPI_FUNC(PyObject*) _PyUnicode_AsUTF8String(
208212
PyObject *unicode,
209213
const char *errors);
210214

211215
/* --- UTF-32 Codecs ------------------------------------------------------ */
212216

217+
// Export for '_tkinter' shared extension
213218
PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF32(
214219
PyObject *object, /* Unicode object */
215220
const char *errors, /* error handling */
216221
int byteorder); /* byteorder to use 0=BOM+native;-1=LE,1=BE */
217222

218223
/* --- UTF-16 Codecs ------------------------------------------------------ */
219224

220-
/* Returns a Python string object holding the UTF-16 encoded value of
221-
the Unicode data.
222-
223-
If byteorder is not 0, output is written according to the following
224-
byte order:
225-
226-
byteorder == -1: little endian
227-
byteorder == 0: native byte order (writes a BOM mark)
228-
byteorder == 1: big endian
229-
230-
If byteorder is 0, the output string will always start with the
231-
Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
232-
prepended.
233-
*/
225+
// Returns a Python string object holding the UTF-16 encoded value of
226+
// the Unicode data.
227+
//
228+
// If byteorder is not 0, output is written according to the following
229+
// byte order:
230+
//
231+
// byteorder == -1: little endian
232+
// byteorder == 0: native byte order (writes a BOM mark)
233+
// byteorder == 1: big endian
234+
//
235+
// If byteorder is 0, the output string will always start with the
236+
// Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
237+
// prepended.
238+
//
239+
// Export for '_tkinter' shared extension
234240
PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF16(
235241
PyObject* unicode, /* Unicode object */
236242
const char *errors, /* error handling */
@@ -297,13 +303,14 @@ extern PyObject* _PyUnicode_EncodeCharmap(
297303

298304
/* --- Decimal Encoder ---------------------------------------------------- */
299305

300-
/* Coverts a Unicode object holding a decimal value to an ASCII string
301-
for using in int, float and complex parsers.
302-
Transforms code points that have decimal digit property to the
303-
corresponding ASCII digit code points. Transforms spaces to ASCII.
304-
Transforms code points starting from the first non-ASCII code point that
305-
is neither a decimal digit nor a space to the end into '?'. */
306-
306+
// Coverts a Unicode object holding a decimal value to an ASCII string
307+
// for using in int, float and complex parsers.
308+
// Transforms code points that have decimal digit property to the
309+
// corresponding ASCII digit code points. Transforms spaces to ASCII.
310+
// Transforms code points starting from the first non-ASCII code point that
311+
// is neither a decimal digit nor a space to the end into '?'.
312+
//
313+
// Export for '_testinternalcapi' shared extension.
307314
PyAPI_FUNC(PyObject*) _PyUnicode_TransformDecimalAndSpaceToASCII(
308315
PyObject *unicode); /* Unicode object */
309316

@@ -323,9 +330,10 @@ extern int _PyUnicode_EqualToASCIIId(
323330
_Py_Identifier *right /* Right identifier */
324331
);
325332

326-
/* Test whether a unicode is equal to ASCII string. Return 1 if true,
327-
0 otherwise. The right argument must be ASCII-encoded string.
328-
Any error occurs inside will be cleared before return. */
333+
// Test whether a unicode is equal to ASCII string. Return 1 if true,
334+
// 0 otherwise. The right argument must be ASCII-encoded string.
335+
// Any error occurs inside will be cleared before return.
336+
// Export for '_ctypes' shared extension
329337
PyAPI_FUNC(int) _PyUnicode_EqualToASCIIString(
330338
PyObject *left,
331339
const char *right /* ASCII-encoded string */
@@ -357,14 +365,17 @@ extern Py_ssize_t _PyUnicode_InsertThousandsGrouping(
357365

358366
extern PyObject* _PyUnicode_FormatLong(PyObject *, int, int, int);
359367

360-
/* Return an interned Unicode object for an Identifier; may fail if there is no memory.*/
368+
// Return an interned Unicode object for an Identifier; may fail if there is no
369+
// memory.
370+
// Export for '_testembed' program.
361371
PyAPI_FUNC(PyObject*) _PyUnicode_FromId(_Py_Identifier*);
362372

363373
/* Fast equality check when the inputs are known to be exact unicode types
364374
and where the hash values are equal (i.e. a very probable match) */
365375
extern int _PyUnicode_EQ(PyObject *, PyObject *);
366376

367-
/* Equality check. */
377+
// Equality check.
378+
// Export for '_pickle' shared extension.
368379
PyAPI_FUNC(int) _PyUnicode_Equal(PyObject *, PyObject *);
369380

370381
extern int _PyUnicode_WideCharString_Converter(PyObject *, void *);

Modules/_testinternalcapi.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
#include "pycore_bitutils.h" // _Py_bswap32()
1515
#include "pycore_bytesobject.h" // _PyBytes_Find()
1616
#include "pycore_compile.h" // _PyCompile_CodeGen, _PyCompile_OptimizeCfg, _PyCompile_Assemble, _PyCompile_CleanDoc
17-
#include "pycore_ceval.h" // _PyEval_AddPendingCall
17+
#include "pycore_ceval.h" // _PyEval_AddPendingCall()
1818
#include "pycore_dict.h" // _PyDictOrValues_GetValues()
19-
#include "pycore_fileutils.h" // _Py_normpath
19+
#include "pycore_fileutils.h" // _Py_normpath()
2020
#include "pycore_frame.h" // _PyInterpreterFrame
2121
#include "pycore_gc.h" // PyGC_Head
2222
#include "pycore_hashtable.h" // _Py_hashtable_new()

Tools/build/generate_token.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def update_file(file, content):
5050

5151

5252
token_h_template = f"""\
53-
/* {AUTO_GENERATED_BY_SCRIPT} */
53+
// {AUTO_GENERATED_BY_SCRIPT}
5454
"""
5555
token_h_template += """\
5656
@@ -84,7 +84,7 @@ def update_file(file, content):
8484
(x) == FSTRING_MIDDLE)
8585
8686
87-
// Symbols exported for test_peg_generator
87+
// Export these 4 symbols for 'test_peg_generator'
8888
PyAPI_DATA(const char * const) _PyParser_TokenNames[]; /* Token names */
8989
PyAPI_FUNC(int) _PyToken_OneChar(int);
9090
PyAPI_FUNC(int) _PyToken_TwoChars(int, int);

0 commit comments

Comments
 (0)