Skip to content

GH-90699: disallow _Py_IDENTIFIER in core code #99210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ typedef struct _Py_Identifier {
Py_ssize_t index;
} _Py_Identifier;

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

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

#endif /* NEEDS_PY_IDENTIFIER */
#endif /* !Py_BUILD_CORE */

typedef struct {
/* Number implementations must check *both*
Expand Down
10 changes: 8 additions & 2 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef Py_BUILD_CORE_MODULE
# define Py_BUILD_CORE_MODULE
#endif
#define NEEDS_PY_IDENTIFIER

/* Always enable assertion (even in release mode) */
#undef NDEBUG
Expand Down Expand Up @@ -1891,7 +1890,14 @@ static int test_unicode_id_init(void)
{
// bpo-42882: Test that _PyUnicode_FromId() works
// when Python is initialized multiples times.
_Py_IDENTIFIER(test_unicode_id_init);

// This is equivalent to `_Py_IDENTIFIER(test_unicode_id_init)`
// but since `_Py_IDENTIFIER` is disabled when `Py_BUILD_CORE`
// is defined, it is manually expanded here.
static _Py_Identifier PyId_test_unicode_id_init = {
.string = "test_unicode_id_init",
.index = -1,
};

// Initialize Python once without using the identifier
_testembed_Py_InitializeFromConfig();
Expand Down