Skip to content

Commit 6e3cc72

Browse files
GH-90699: disallow _Py_IDENTIFIER in core code (GH-99210)
1 parent c03e05c commit 6e3cc72

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Include/cpython/object.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ typedef struct _Py_Identifier {
4141
Py_ssize_t index;
4242
} _Py_Identifier;
4343

44-
#if defined(NEEDS_PY_IDENTIFIER) || !defined(Py_BUILD_CORE)
44+
#ifndef Py_BUILD_CORE
4545
// For now we are keeping _Py_IDENTIFIER for continued use
4646
// in non-builtin extensions (and naughty PyPI modules).
4747

4848
#define _Py_static_string_init(value) { .string = (value), .index = -1 }
4949
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
5050
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
5151

52-
#endif /* NEEDS_PY_IDENTIFIER */
52+
#endif /* !Py_BUILD_CORE */
5353

5454
typedef struct {
5555
/* Number implementations must check *both*

Programs/_testembed.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef Py_BUILD_CORE_MODULE
22
# define Py_BUILD_CORE_MODULE
33
#endif
4-
#define NEEDS_PY_IDENTIFIER
54

65
/* Always enable assertion (even in release mode) */
76
#undef NDEBUG
@@ -1891,7 +1890,14 @@ static int test_unicode_id_init(void)
18911890
{
18921891
// bpo-42882: Test that _PyUnicode_FromId() works
18931892
// when Python is initialized multiples times.
1894-
_Py_IDENTIFIER(test_unicode_id_init);
1893+
1894+
// This is equivalent to `_Py_IDENTIFIER(test_unicode_id_init)`
1895+
// but since `_Py_IDENTIFIER` is disabled when `Py_BUILD_CORE`
1896+
// is defined, it is manually expanded here.
1897+
static _Py_Identifier PyId_test_unicode_id_init = {
1898+
.string = "test_unicode_id_init",
1899+
.index = -1,
1900+
};
18951901

18961902
// Initialize Python once without using the identifier
18971903
_testembed_Py_InitializeFromConfig();

0 commit comments

Comments
 (0)