Skip to content

Commit b7f00c5

Browse files
Disallow _Py_IDENTIFIER in core.
1 parent dee574e commit b7f00c5

File tree

7 files changed

+75
-50
lines changed

7 files changed

+75
-50
lines changed

Include/cpython/abstract.h

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ PyAPI_FUNC(PyObject *) _PyObject_CallMethod(PyObject *obj,
120120
PyObject *name,
121121
const char *format, ...);
122122

123+
#ifdef _Py_IDENTIFIER
124+
123125
/* Like PyObject_CallMethod(), but expect a _Py_Identifier*
124126
as the method name. */
125127
PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
@@ -165,6 +167,8 @@ _PyObject_CallMethodIdOneArg(PyObject *self, _Py_Identifier *name, PyObject *arg
165167
2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
166168
}
167169

170+
#endif /* _Py_IDENTIFIER */
171+
168172
PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
169173

170174
/* Guess the size of object 'o' using len(o) or o.__length_hint__().

Include/cpython/ceval.h

+2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ PyAPI_FUNC(PyObject *) _PyEval_GetAsyncGenFinalizer(void);
1616

1717
/* Helper to look up a builtin object */
1818
PyAPI_FUNC(PyObject *) _PyEval_GetBuiltin(PyObject *);
19+
#ifdef _Py_IDENTIFIER
1920
PyAPI_FUNC(PyObject *) _PyEval_GetBuiltinId(_Py_Identifier *);
21+
#endif
2022
/* Look at the current frame's (if any) code's co_flags, and turn on
2123
the corresponding compiler flags in cf->cf_flags. Return 1 if any
2224
flag was set, else return 0. */

Include/cpython/dictobject.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ typedef struct {
3131
PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
3232
Py_hash_t hash);
3333
PyAPI_FUNC(PyObject *) _PyDict_GetItemWithError(PyObject *dp, PyObject *key);
34-
PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp,
35-
struct _Py_Identifier *key);
3634
PyAPI_FUNC(PyObject *) _PyDict_GetItemStringWithError(PyObject *, const char *);
3735
PyAPI_FUNC(PyObject *) PyDict_SetDefault(
3836
PyObject *mp, PyObject *key, PyObject *defaultobj);
@@ -49,7 +47,6 @@ PyAPI_FUNC(int) _PyDict_Next(
4947
/* Get the number of items of a dictionary. */
5048
#define PyDict_GET_SIZE(mp) (assert(PyDict_Check(mp)),((PyDictObject *)mp)->ma_used)
5149
PyAPI_FUNC(int) _PyDict_Contains_KnownHash(PyObject *, PyObject *, Py_hash_t);
52-
PyAPI_FUNC(int) _PyDict_ContainsId(PyObject *, struct _Py_Identifier *);
5350
PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
5451
PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp);
5552
PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp);
@@ -66,15 +63,21 @@ PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
6663
argument is raised.
6764
*/
6865
PyAPI_FUNC(int) _PyDict_MergeEx(PyObject *mp, PyObject *other, int override);
69-
PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, struct _Py_Identifier *key, PyObject *item);
7066

71-
PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, struct _Py_Identifier *key);
7267
PyAPI_FUNC(void) _PyDict_DebugMallocStats(FILE *out);
7368

7469
int _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr, PyObject *name, PyObject *value);
7570
PyObject *_PyDict_LoadGlobal(PyDictObject *, PyDictObject *, PyObject *);
7671
Py_ssize_t _PyDict_GetItemHint(PyDictObject *, PyObject *, Py_ssize_t, PyObject **);
7772

73+
#ifdef _Py_IDENTIFIER
74+
PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp,
75+
struct _Py_Identifier *key);
76+
PyAPI_FUNC(int) _PyDict_ContainsId(PyObject *, struct _Py_Identifier *);
77+
PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, struct _Py_Identifier *key, PyObject *item);
78+
PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, struct _Py_Identifier *key);
79+
#endif
80+
7881
/* _PyDictView */
7982

8083
typedef struct {

Include/cpython/import.h

+2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ PyMODINIT_FUNC PyInit__imp(void);
66

77
PyAPI_FUNC(int) _PyImport_IsInitialized(PyInterpreterState *);
88

9+
#ifdef _Py_IDENTIFIER
910
PyAPI_FUNC(PyObject *) _PyImport_GetModuleId(struct _Py_Identifier *name);
11+
#endif
1012
PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);
1113
PyAPI_FUNC(int) _PyImport_SetModuleString(const char *name, PyObject* module);
1214

Include/cpython/object.h

+44-37
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,6 @@ PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
1414
#endif
1515

1616

17-
/********************* String Literals ****************************************/
18-
/* This structure helps managing static strings. The basic usage goes like this:
19-
Instead of doing
20-
21-
r = PyObject_CallMethod(o, "foo", "args", ...);
22-
23-
do
24-
25-
_Py_IDENTIFIER(foo);
26-
...
27-
r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...);
28-
29-
PyId_foo is a static variable, either on block level or file level. On first
30-
usage, the string "foo" is interned, and the structures are linked. On interpreter
31-
shutdown, all strings are released.
32-
33-
Alternatively, _Py_static_string allows choosing the variable name.
34-
_PyUnicode_FromId returns a borrowed reference to the interned string.
35-
_PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
36-
*/
37-
typedef struct _Py_Identifier {
38-
const char* string;
39-
// Index in PyInterpreterState.unicode.ids.array. It is process-wide
40-
// unique and must be initialized to -1.
41-
Py_ssize_t index;
42-
} _Py_Identifier;
43-
44-
#define _Py_static_string_init(value) { .string = value, .index = -1 }
45-
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
46-
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
47-
4817
/* buffer interface */
4918
typedef struct bufferinfo {
5019
void *buf;
@@ -299,24 +268,19 @@ typedef struct _heaptypeobject {
299268

300269
PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
301270
PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
302-
PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
303271
PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, PyObject *);
304-
PyAPI_FUNC(PyObject *) _PyObject_LookupSpecialId(PyObject *, _Py_Identifier *);
305272
PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
306273
PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
307274
PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
308275
struct PyModuleDef;
309276
PyAPI_FUNC(PyObject *) _PyType_GetModuleByDef(PyTypeObject *, struct PyModuleDef *);
310277

311-
struct _Py_Identifier;
312278
PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
313279
PyAPI_FUNC(void) _Py_BreakPoint(void);
314280
PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
315281
PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
316282

317283
PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
318-
PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *);
319-
PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *);
320284
/* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which
321285
don't raise AttributeError.
322286
@@ -327,7 +291,6 @@ PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObjec
327291
is raised.
328292
*/
329293
PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **);
330-
PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, struct _Py_Identifier *, PyObject **);
331294

332295
PyAPI_FUNC(int) _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method);
333296

@@ -346,6 +309,50 @@ _PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
346309

347310
PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *);
348311

312+
/********************* String Literals ****************************************/
313+
/* This structure helps managing static strings. The basic usage goes like this:
314+
Instead of doing
315+
316+
r = PyObject_CallMethod(o, "foo", "args", ...);
317+
318+
do
319+
320+
_Py_IDENTIFIER(foo);
321+
...
322+
r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...);
323+
324+
PyId_foo is a static variable, either on block level or file level. On first
325+
usage, the string "foo" is interned, and the structures are linked. On interpreter
326+
shutdown, all strings are released.
327+
328+
Alternatively, _Py_static_string allows choosing the variable name.
329+
_PyUnicode_FromId returns a borrowed reference to the interned string.
330+
_PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
331+
*/
332+
typedef struct _Py_Identifier {
333+
const char* string;
334+
// Index in PyInterpreterState.unicode.ids.array. It is process-wide
335+
// unique and must be initialized to -1.
336+
Py_ssize_t index;
337+
} _Py_Identifier;
338+
339+
#if !defined(Py_BUILD_CORE) || defined(Py_BUILD_CORE_MODULE)
340+
// For now we are keeping _Py_IDENTIFIER for continued use
341+
// in non-builtin extensions (and naughty PyPI modules).
342+
343+
#define _Py_static_string_init(value) { .string = value, .index = -1 }
344+
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
345+
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
346+
347+
#endif /* ! Py_BUILD_CORE */
348+
349+
PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
350+
PyAPI_FUNC(PyObject *) _PyObject_LookupSpecialId(PyObject *, _Py_Identifier *);
351+
struct _Py_Identifier;
352+
PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *);
353+
PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *);
354+
PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, struct _Py_Identifier *, PyObject **);
355+
349356
/* Safely decref `op` and set `op` to `op2`.
350357
*
351358
* As in case of Py_CLEAR "the obvious" code can be deadly:

Include/cpython/sysmodule.h

+3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
PyAPI_FUNC(PyObject *) _PySys_GetAttr(PyThreadState *tstate,
66
PyObject *name);
7+
8+
#ifdef _Py_IDENTIFIER
79
PyAPI_FUNC(PyObject *) _PySys_GetObjectId(_Py_Identifier *key);
810
PyAPI_FUNC(int) _PySys_SetObjectId(_Py_Identifier *key, PyObject *);
11+
#endif
912

1013
PyAPI_FUNC(size_t) _PySys_GetSizeOf(PyObject *);
1114

Include/cpython/unicodeobject.h

+12-8
Original file line numberDiff line numberDiff line change
@@ -861,14 +861,6 @@ PyAPI_FUNC(PyObject *) _PyUnicode_JoinArray(
861861
Py_ssize_t seqlen
862862
);
863863

864-
/* Test whether a unicode is equal to ASCII identifier. Return 1 if true,
865-
0 otherwise. The right argument must be ASCII identifier.
866-
Any error occurs inside will be cleared before return. */
867-
PyAPI_FUNC(int) _PyUnicode_EqualToASCIIId(
868-
PyObject *left, /* Left string */
869-
_Py_Identifier *right /* Right identifier */
870-
);
871-
872864
/* Test whether a unicode is equal to ASCII string. Return 1 if true,
873865
0 otherwise. The right argument must be ASCII-encoded string.
874866
Any error occurs inside will be cleared before return. */
@@ -1013,9 +1005,21 @@ PyAPI_FUNC(int) _PyUnicode_IsAlpha(
10131005

10141006
PyAPI_FUNC(PyObject*) _PyUnicode_FormatLong(PyObject *, int, int, int);
10151007

1008+
#ifdef _Py_IDENTIFIER
1009+
1010+
/* Test whether a unicode is equal to ASCII identifier. Return 1 if true,
1011+
0 otherwise. The right argument must be ASCII identifier.
1012+
Any error occurs inside will be cleared before return. */
1013+
PyAPI_FUNC(int) _PyUnicode_EqualToASCIIId(
1014+
PyObject *left, /* Left string */
1015+
_Py_Identifier *right /* Right identifier */
1016+
);
1017+
10161018
/* Return an interned Unicode object for an Identifier; may fail if there is no memory.*/
10171019
PyAPI_FUNC(PyObject*) _PyUnicode_FromId(_Py_Identifier*);
10181020

1021+
#endif /* _Py_IDENTIFIER */
1022+
10191023
/* Fast equality check when the inputs are known to be exact unicode types
10201024
and where the hash values are equal (i.e. a very probable match) */
10211025
PyAPI_FUNC(int) _PyUnicode_EQ(PyObject *, PyObject *);

0 commit comments

Comments
 (0)