mirror of
https://github.com/python/cpython.git
synced 2025-11-25 21:11:09 +00:00
bpo-46541: Replace core use of _Py_IDENTIFIER() with statically initialized global objects. (gh-30928)
We're no longer using _Py_IDENTIFIER() (or _Py_static_string()) in any core CPython code. It is still used in a number of non-builtin stdlib modules. The replacement is: PyUnicodeObject (not pointer) fields under _PyRuntimeState, statically initialized as part of _PyRuntime. A new _Py_GET_GLOBAL_IDENTIFIER() macro facilitates lookup of the fields (along with _Py_GET_GLOBAL_STRING() for non-identifier strings). https://bugs.python.org/issue46541#msg411799 explains the rationale for this change. The core of the change is in: * (new) Include/internal/pycore_global_strings.h - the declarations for the global strings, along with the macros * Include/internal/pycore_runtime_init.h - added the static initializers for the global strings * Include/internal/pycore_global_objects.h - where the struct in pycore_global_strings.h is hooked into _PyRuntimeState * Tools/scripts/generate_global_objects.py - added generation of the global string declarations and static initializers I've also added a --check flag to generate_global_objects.py (along with make check-global-objects) to check for unused global strings. That check is added to the PR CI config. The remainder of this change updates the core code to use _Py_GET_GLOBAL_IDENTIFIER() instead of _Py_IDENTIFIER() and the related _Py*Id functions (likewise for _Py_GET_GLOBAL_STRING() instead of _Py_static_string()). This includes adding a few functions where there wasn't already an alternative to _Py*Id(), replacing the _Py_Identifier * parameter with PyObject *. The following are not changed (yet): * stop using _Py_IDENTIFIER() in the stdlib modules * (maybe) get rid of _Py_IDENTIFIER(), etc. entirely -- this may not be doable as at least one package on PyPI using this (private) API * (maybe) intern the strings during runtime init https://bugs.python.org/issue46541
This commit is contained in:
parent
c018d3037b
commit
81c72044a1
108 changed files with 2282 additions and 1573 deletions
|
|
@ -25,21 +25,6 @@ class _io.BufferedRandom "buffered *" "&PyBufferedRandom_Type"
|
|||
[clinic start generated code]*/
|
||||
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=59460b9c5639984d]*/
|
||||
|
||||
_Py_IDENTIFIER(close);
|
||||
_Py_IDENTIFIER(_dealloc_warn);
|
||||
_Py_IDENTIFIER(flush);
|
||||
_Py_IDENTIFIER(isatty);
|
||||
_Py_IDENTIFIER(mode);
|
||||
_Py_IDENTIFIER(name);
|
||||
_Py_IDENTIFIER(peek);
|
||||
_Py_IDENTIFIER(read);
|
||||
_Py_IDENTIFIER(read1);
|
||||
_Py_IDENTIFIER(readable);
|
||||
_Py_IDENTIFIER(readinto);
|
||||
_Py_IDENTIFIER(readinto1);
|
||||
_Py_IDENTIFIER(writable);
|
||||
_Py_IDENTIFIER(write);
|
||||
|
||||
/*
|
||||
* BufferedIOBase class, inherits from IOBase.
|
||||
*/
|
||||
|
|
@ -65,9 +50,10 @@ _bufferediobase_readinto_generic(PyObject *self, Py_buffer *buffer, char readint
|
|||
Py_ssize_t len;
|
||||
PyObject *data;
|
||||
|
||||
data = _PyObject_CallMethodId(self,
|
||||
readinto1 ? &PyId_read1 : &PyId_read,
|
||||
"n", buffer->len);
|
||||
PyObject *attr = readinto1
|
||||
? &_Py_ID(read1)
|
||||
: &_Py_ID(read);
|
||||
data = _PyObject_CallMethod(self, attr, "n", buffer->len);
|
||||
if (data == NULL)
|
||||
return NULL;
|
||||
|
||||
|
|
@ -436,8 +422,7 @@ buffered_dealloc_warn(buffered *self, PyObject *source)
|
|||
{
|
||||
if (self->ok && self->raw) {
|
||||
PyObject *r;
|
||||
r = _PyObject_CallMethodIdOneArg(self->raw, &PyId__dealloc_warn,
|
||||
source);
|
||||
r = PyObject_CallMethodOneArg(self->raw, &_Py_ID(_dealloc_warn), source);
|
||||
if (r)
|
||||
Py_DECREF(r);
|
||||
else
|
||||
|
|
@ -583,14 +568,14 @@ static PyObject *
|
|||
buffered_name_get(buffered *self, void *context)
|
||||
{
|
||||
CHECK_INITIALIZED(self)
|
||||
return _PyObject_GetAttrId(self->raw, &PyId_name);
|
||||
return PyObject_GetAttr(self->raw, &_Py_ID(name));
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
buffered_mode_get(buffered *self, void *context)
|
||||
{
|
||||
CHECK_INITIALIZED(self)
|
||||
return _PyObject_GetAttrId(self->raw, &PyId_mode);
|
||||
return PyObject_GetAttr(self->raw, &_Py_ID(mode));
|
||||
}
|
||||
|
||||
/* Lower-level APIs */
|
||||
|
|
@ -1381,7 +1366,7 @@ buffered_repr(buffered *self)
|
|||
{
|
||||
PyObject *nameobj, *res;
|
||||
|
||||
if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) {
|
||||
if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
|
||||
if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -2153,7 +2138,7 @@ bufferedrwpair_dealloc(rwpair *self)
|
|||
}
|
||||
|
||||
static PyObject *
|
||||
_forward_call(buffered *self, _Py_Identifier *name, PyObject *args)
|
||||
_forward_call(buffered *self, PyObject *name, PyObject *args)
|
||||
{
|
||||
PyObject *func, *ret;
|
||||
if (self == NULL) {
|
||||
|
|
@ -2162,9 +2147,9 @@ _forward_call(buffered *self, _Py_Identifier *name, PyObject *args)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
func = _PyObject_GetAttrId((PyObject *)self, name);
|
||||
func = PyObject_GetAttr((PyObject *)self, name);
|
||||
if (func == NULL) {
|
||||
PyErr_SetString(PyExc_AttributeError, name->string);
|
||||
PyErr_SetObject(PyExc_AttributeError, name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
@ -2176,67 +2161,67 @@ _forward_call(buffered *self, _Py_Identifier *name, PyObject *args)
|
|||
static PyObject *
|
||||
bufferedrwpair_read(rwpair *self, PyObject *args)
|
||||
{
|
||||
return _forward_call(self->reader, &PyId_read, args);
|
||||
return _forward_call(self->reader, &_Py_ID(read), args);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bufferedrwpair_peek(rwpair *self, PyObject *args)
|
||||
{
|
||||
return _forward_call(self->reader, &PyId_peek, args);
|
||||
return _forward_call(self->reader, &_Py_ID(peek), args);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bufferedrwpair_read1(rwpair *self, PyObject *args)
|
||||
{
|
||||
return _forward_call(self->reader, &PyId_read1, args);
|
||||
return _forward_call(self->reader, &_Py_ID(read1), args);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bufferedrwpair_readinto(rwpair *self, PyObject *args)
|
||||
{
|
||||
return _forward_call(self->reader, &PyId_readinto, args);
|
||||
return _forward_call(self->reader, &_Py_ID(readinto), args);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bufferedrwpair_readinto1(rwpair *self, PyObject *args)
|
||||
{
|
||||
return _forward_call(self->reader, &PyId_readinto1, args);
|
||||
return _forward_call(self->reader, &_Py_ID(readinto1), args);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bufferedrwpair_write(rwpair *self, PyObject *args)
|
||||
{
|
||||
return _forward_call(self->writer, &PyId_write, args);
|
||||
return _forward_call(self->writer, &_Py_ID(write), args);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bufferedrwpair_flush(rwpair *self, PyObject *Py_UNUSED(ignored))
|
||||
{
|
||||
return _forward_call(self->writer, &PyId_flush, NULL);
|
||||
return _forward_call(self->writer, &_Py_ID(flush), NULL);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bufferedrwpair_readable(rwpair *self, PyObject *Py_UNUSED(ignored))
|
||||
{
|
||||
return _forward_call(self->reader, &PyId_readable, NULL);
|
||||
return _forward_call(self->reader, &_Py_ID(readable), NULL);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bufferedrwpair_writable(rwpair *self, PyObject *Py_UNUSED(ignored))
|
||||
{
|
||||
return _forward_call(self->writer, &PyId_writable, NULL);
|
||||
return _forward_call(self->writer, &_Py_ID(writable), NULL);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
bufferedrwpair_close(rwpair *self, PyObject *Py_UNUSED(ignored))
|
||||
{
|
||||
PyObject *exc = NULL, *val, *tb;
|
||||
PyObject *ret = _forward_call(self->writer, &PyId_close, NULL);
|
||||
PyObject *ret = _forward_call(self->writer, &_Py_ID(close), NULL);
|
||||
if (ret == NULL)
|
||||
PyErr_Fetch(&exc, &val, &tb);
|
||||
else
|
||||
Py_DECREF(ret);
|
||||
ret = _forward_call(self->reader, &PyId_close, NULL);
|
||||
ret = _forward_call(self->reader, &_Py_ID(close), NULL);
|
||||
if (exc != NULL) {
|
||||
_PyErr_ChainExceptions(exc, val, tb);
|
||||
Py_CLEAR(ret);
|
||||
|
|
@ -2247,7 +2232,7 @@ bufferedrwpair_close(rwpair *self, PyObject *Py_UNUSED(ignored))
|
|||
static PyObject *
|
||||
bufferedrwpair_isatty(rwpair *self, PyObject *Py_UNUSED(ignored))
|
||||
{
|
||||
PyObject *ret = _forward_call(self->writer, &PyId_isatty, NULL);
|
||||
PyObject *ret = _forward_call(self->writer, &_Py_ID(isatty), NULL);
|
||||
|
||||
if (ret != Py_False) {
|
||||
/* either True or exception */
|
||||
|
|
@ -2255,7 +2240,7 @@ bufferedrwpair_isatty(rwpair *self, PyObject *Py_UNUSED(ignored))
|
|||
}
|
||||
Py_DECREF(ret);
|
||||
|
||||
return _forward_call(self->reader, &PyId_isatty, NULL);
|
||||
return _forward_call(self->reader, &_Py_ID(isatty), NULL);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue