8000 gh-106320: Remove private _Py_Identifier API · python/cpython@7ccbf0b · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ccbf0b

Browse files
committed
gh-106320: Remove private _Py_Identifier API
Remove the private _Py_Identifier type and related private functions from the public C API: * _PyObject_GetAttrId() * _PyObject_LookupSpecialId() * _PyObject_SetAttrId() * _PyType_LookupId() * _Py_IDENTIFIER() * _Py_static_string() * _Py_static_string_init() Move them to the internal C API: add a new pycore_identifier.h header file. No longer export these functions.
1 parent 5c68cba commit 7ccbf0b

8 files changed

+66
-46
lines changed

Include/cpython/object.h

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,6 @@ PyAPI_FUNC(Py_ssize_t) _PyInterpreterState_GetRefTotal(PyInterpreterState *);
1919
#endif
2020

2121

22-
/********************* String Literals ****************************************/
23-
/* This structure helps managing static strings. The basic usage goes like this:
24-
Instead of doing
25-
26-
r = PyObject_CallMethod(o, "foo", "args", ...);
27-
28-
do
29-
30-
_Py_IDENTIFIER(foo);
31-
...
32-
r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...);
33-
34-
PyId_foo is a static variable, either on block level or file level. On first
35-
usage, the string "foo" is interned, and the structures are linked. On interpreter
36-
shutdown, all strings are released.
37-
38-
Alternatively, _Py_static_string allows choosing the variable name.
39-
_PyUnicode_FromId returns a borrowed reference to the interned string.
40-
_PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
41-
*/
42-
typedef struct _Py_Identifier {
43-
const char* string;
44-
// Index in PyInterpreterState.unicode.ids.array. It is process-wide
45-
// unique and must be initialized to -1.
46-
Py_ssize_t index;
47-
} _Py_Identifier;
48-
49-
#ifndef Py_BUILD_CORE
50-
// For now we are keeping _Py_IDENTIFIER for continued use
51-
// in non-builtin extensions (and naughty PyPI modules).
52-
53-
#define _Py_static_string_init(value) { .string = (value), .index = -1 }
54-
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
55-
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
56-
57-
#endif /* !Py_BUILD_CORE */
58-
5922
typedef struct {
6023
/* Number implementations must check *both*
6124
arguments for proper type and implement the necessary conversions
@@ -273,18 +236,13 @@ typedef struct _heaptypeobject {
273236

274237
PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
275238
PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
276-
PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
277-
PyAPI_FUNC(PyObject *) _PyObject_LookupSpecialId(PyObject *, _Py_Identifier *);
278239
PyAPI_FUNC(PyObject *) PyType_GetModuleByDef(PyTypeObject *, PyModuleDef *);
279240
PyAPI_FUNC(PyObject *) PyType_GetDict(PyTypeObject *);
280241

281242
PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
282243
PyAPI_FUNC(void) _Py_BreakPoint(void);
283244
PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
284245

285-
PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, _Py_Identifier *);
286-
PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, _Py_Identifier *, PyObject *);
287-
288246
PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
289247
PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
290248
PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);

Include/internal/pycore_call.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_identifier.h" // _Py_Identifier
1112
#include "pycore_pystate.h" // _PyThreadState_GET()
1213

1314
/* Suggested size (number of positional arguments) for arrays of PyObject*

Include/internal/pycore_dict.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" {
99
# error "this header requires Py_BUILD_CORE define"
1010
#endif
1111

12+
#include "pycore_identifier.h" // _Py_Identifier
1213
#include "pycore_object.h" // PyDictOrValues
1314

1415
// Unsafe flavor of PyDict_GetItemWithError(): no error checking

Include/internal/pycore_identifier.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* String Literals: _Py_Identifier API */
2+
3+
#ifndef Py_INTERNAL_IDENTIFIER_H
4+
#define Py_INTERNAL_IDENTIFIER_H
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
#ifndef Py_BUILD_CORE
10+
# error "this header requires Py_BUILD_CORE define"
11+
#endif
12+
13+
/* This structure helps managing static strings. The basic usage goes like this:
14+
Instead of doing
15+
16+
r = PyObject_CallMethod(o, "foo", "args", ...);
17+
18+
do
19+
20+
_Py_IDENTIFIER(foo);
21+
...
22+
r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...);
23+
24+
PyId_foo is a static variable, either on block level or file level. On first
25+
usage, the string "foo" is interned, and the structures are linked. On interpreter
26+
shutdown, all strings are released.
27+
28+
Alternatively, _Py_static_string allows choosing the variable name.
29+
_PyUnicode_FromId returns a borrowed reference to the interned string.
30+
_PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
31+
*/
32+
typedef struct _Py_Identifier {
33+
const char* string;
34+
// Index in PyInterpreterState.unicode.ids.array. It is process-wide
35+
// unique and must be initialized to -1.
36+
Py_ssize_t index;
37+
} _Py_Identifier;
38+
39+
// For now we are keeping _Py_IDENTIFIER for continued use
40+
// in non-builtin extensions (and naughty PyPI modules).
41+
42+
#define _Py_static_string_init(value) { .string = (value), .index = -1 }
43+
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
44+
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
45+
46+
extern PyObject* _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
47+
extern PyObject* _PyObject_LookupSpecialId(PyObject *, _Py_Identifier *);
48+
extern PyObject* _PyObject_GetAttrId(PyObject *, _Py_Identifier *);
49+
extern int _PyObject_SetAttrId(PyObject *, _Py_Identifier *, PyObject *);
50+
51+
#ifdef __cplusplus
52+
}
53+
#endif
54+
#endif // !Py_INTERNAL_IDENTIFIER_H

Include/internal/pycore_unicodeobject.h

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

1111
#include "pycore_fileutils.h" // _Py_error_handler
12+
#include "pycore_identifier.h" // _Py_Identifier
1213
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
1314

1415
/* --- Characters Type APIs ----------------------------------------------- */

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,7 @@ PYTHON_HEADERS= \
17691769
$(srcdir)/Include/internal/pycore_global_objects_fini_generated.h \
17701770
$(srcdir)/Include/internal/pycore_hamt.h \
17711771
$(srcdir)/Include/internal/pycore_hashtable.h \
1772+
$(srcdir)/Include/internal/pycore_identifier.h \
17721773
$(srcdir)/Include/internal/pycore_import.h \
17731774
$(srcdir)/Include/internal/pycore_initconfig.h \
17741775
$(srcdir)/Include/internal/pycore_interp.h \

PCbuild/pythoncore.vcxproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
<ClInclude Include="..\Include\internal\pycore_global_objects_fini_generated.h" />
236236
<ClInclude Include="..\Include\internal\pycore_hamt.h" />
237237
<ClInclude Include="..\Include\internal\pycore_hashtable.h" />
238+
<ClInclude Include="..\Include\internal\pycore_identifier.h" />
238239
<ClInclude Include="..\Include\internal\pycore_import.h" />
239240
<ClInclude Include="..\Include\internal\pycore_initconfig.h" />
240241
<ClInclude Include="..\Include\internal\pycore_interp.h" />
@@ -248,7 +249,7 @@
248249
<ClInclude Include="..\Include\internal\pycore_object_state.h" />
249250
<ClInclude Include="..\Include\internal\pycore_obmalloc.h" />
250251
<ClInclude Include="..\Include\internal\pycore_obmalloc_init.h" />
251-
<ClInclude Include="..\Include\internal\pycore_optimizer.h" />
252+
<ClInclude Include="..\Include\internal\pycore_optimizer.h" />
252253
<ClInclude Include="..\Include\internal\pycore_pathconfig.h" />
253254
<ClInclude Include="..\Include\internal\pycore_pyarena.h" />
254255
<ClInclude Include="..\Include\internal\pycore_pyerrors.h" />
@@ -280,7 +281,7 @@
280281
<ClInclude Include="..\Include\internal\pycore_unionobject.h" />
281282
<ClInclude Include="..\Include\internal\pycore_unicodeobject.h" />
282283
<ClInclude Include="..\Include\internal\pycore_unicodeobject_generated.h" />
283-
<ClInclude Include="..\Include\internal\pycore_uops.h" />
284+
<ClInclude Include="..\Include\internal\pycore_uops.h" />
284285
<ClInclude Include="..\Include\internal\pycore_warnings.h" />
285286
<ClInclude Include="..\Include\internal\pycore_weakref.h" />
286287
<ClInclude Include="..\Include\interpreteridobject.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@
609609
<ClInclude Include="..\Include\internal\pycore_hashtable.h">
610610
<Filter>Include\internal</Filter>
611611
</ClInclude>
612+
<ClInclude Include="..\Include\internal\pycore_identifier.h">
613+
<Filter>Include\internal</Filter>
614+
</ClInclude>
612615
<ClInclude Include="..\Include\internal\pycore_import.h">
613616
<Filter>Include\internal</Filter>
614617
</ClInclude>
@@ -650,7 +653,7 @@
650653
</ClInclude>
651654
<ClInclude Include="..\Include\internal\pycore_optimizer.h">
652655
<Filter>Include\internal</Filter>
653-
</ClInclude>
656+
</ClInclude>
654657
<ClInclude Include="..\Include\internal\pycore_pathconfig.h">
655658
<Filter>Include\internal</Filter>
656659
</ClInclude>
@@ -737,7 +740,7 @@
737740
</ClInclude>
738741
<ClInclude Include="..\Include\internal\pycore_uops.h">
739742
<Filter>Include\internal</Filter>
740-
</ClInclude>
743+
</ClInclude>
741744
<ClInclude Include="$(zlibDir)\crc32.h">
742745
<Filter>Modules\zlib</Filter>
743746
</ClInclude>

0 commit comments

Comments
 (0)
0