gh-108512: Add and use new replacements for PySys_GetObject() (GH-111035)

Add functions PySys_GetAttr(), PySys_GetAttrString(),
PySys_GetOptionalAttr() and PySys_GetOptionalAttrString().
This commit is contained in:
Serhiy Storchaka 2025-05-28 20:11:09 +03:00 committed by GitHub
parent b265a7ddeb
commit bac3fcba5b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 287 additions and 93 deletions

View file

@ -76,12 +76,12 @@ module sys
PyObject *
_PySys_GetRequiredAttr(PyObject *name)
PySys_GetAttr(PyObject *name)
{
if (!PyUnicode_Check(name)) {
PyErr_Format(PyExc_TypeError,
"attribute name must be string, not '%.200s'",
Py_TYPE(name)->tp_name);
"attribute name must be string, not '%T'",
name);
return NULL;
}
PyThreadState *tstate = _PyThreadState_GET();
@ -98,7 +98,7 @@ _PySys_GetRequiredAttr(PyObject *name)
}
PyObject *
_PySys_GetRequiredAttrString(const char *name)
PySys_GetAttrString(const char *name)
{
PyThreadState *tstate = _PyThreadState_GET();
PyObject *sysdict = tstate->interp->sysdict;
@ -114,12 +114,12 @@ _PySys_GetRequiredAttrString(const char *name)
}
int
_PySys_GetOptionalAttr(PyObject *name, PyObject **value)
PySys_GetOptionalAttr(PyObject *name, PyObject **value)
{
if (!PyUnicode_Check(name)) {
PyErr_Format(PyExc_TypeError,
"attribute name must be string, not '%.200s'",
Py_TYPE(name)->tp_name);
"attribute name must be string, not '%T'",
name);
*value = NULL;
return -1;
}
@ -133,7 +133,7 @@ _PySys_GetOptionalAttr(PyObject *name, PyObject **value)
}
int
_PySys_GetOptionalAttrString(const char *name, PyObject **value)
PySys_GetOptionalAttrString(const char *name, PyObject **value)
{
PyThreadState *tstate = _PyThreadState_GET();
PyObject *sysdict = tstate->interp->sysdict;
@ -773,7 +773,7 @@ sys_displayhook(PyObject *module, PyObject *o)
}
if (PyObject_SetAttr(builtins, _Py_LATIN1_CHR('_'), Py_None) != 0)
return NULL;
outf = _PySys_GetRequiredAttr(&_Py_ID(stdout));
outf = PySys_GetAttr(&_Py_ID(stdout));
if (outf == NULL) {
return NULL;
}
@ -3005,7 +3005,7 @@ static PyObject *
get_warnoptions(PyThreadState *tstate)
{
PyObject *warnoptions;
if (_PySys_GetOptionalAttr(&_Py_ID(warnoptions), &warnoptions) < 0) {
if (PySys_GetOptionalAttr(&_Py_ID(warnoptions), &warnoptions) < 0) {
return NULL;
}
if (warnoptions == NULL || !PyList_Check(warnoptions)) {
@ -3042,7 +3042,7 @@ PySys_ResetWarnOptions(void)
}
PyObject *warnoptions;
if (_PySys_GetOptionalAttr(&_Py_ID(warnoptions), &warnoptions) < 0) {
if (PySys_GetOptionalAttr(&_Py_ID(warnoptions), &warnoptions) < 0) {
PyErr_Clear();
return;
}
@ -3106,7 +3106,7 @@ PyAPI_FUNC(int)
PySys_HasWarnOptions(void)
{
PyObject *warnoptions;
if (_PySys_GetOptionalAttr(&_Py_ID(warnoptions), &warnoptions) < 0) {
if (PySys_GetOptionalAttr(&_Py_ID(warnoptions), &warnoptions) < 0) {
PyErr_Clear();
return 0;
}
@ -3120,7 +3120,7 @@ static PyObject *
get_xoptions(PyThreadState *tstate)
{
PyObject *xoptions;
if (_PySys_GetOptionalAttr(&_Py_ID(_xoptions), &xoptions) < 0) {
if (PySys_GetOptionalAttr(&_Py_ID(_xoptions), &xoptions) < 0) {
return NULL;
}
if (xoptions == NULL || !PyDict_Check(xoptions)) {
@ -3373,7 +3373,7 @@ sys_set_flag(PyObject *flags, Py_ssize_t pos, PyObject *value)
int
_PySys_SetFlagObj(Py_ssize_t pos, PyObject *value)
{
PyObject *flags = _PySys_GetRequiredAttrString("flags");
PyObject *flags = PySys_GetAttrString("flags");
if (flags == NULL) {
return -1;
}
@ -3935,7 +3935,7 @@ _PySys_UpdateConfig(PyThreadState *tstate)
#undef COPY_WSTR
// sys.flags
PyObject *flags = _PySys_GetRequiredAttrString("flags");
PyObject *flags = PySys_GetAttrString("flags");
if (flags == NULL) {
return -1;
}
@ -4251,7 +4251,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
}
PyObject *sys_path;
if (_PySys_GetOptionalAttr(&_Py_ID(path), &sys_path) < 0) {
if (PySys_GetOptionalAttr(&_Py_ID(path), &sys_path) < 0) {
Py_FatalError("can't get sys.path");
}
else if (sys_path != NULL) {
@ -4347,7 +4347,7 @@ sys_write(PyObject *key, FILE *fp, const char *format, va_list va)
PyObject *exc = _PyErr_GetRaisedException(tstate);
written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
file = _PySys_GetRequiredAttr(key);
file = PySys_GetAttr(key);
if (sys_pyfile_write(buffer, file) != 0) {
_PyErr_Clear(tstate);
fputs(buffer, fp);
@ -4391,7 +4391,7 @@ sys_format(PyObject *key, FILE *fp, const char *format, va_list va)
PyObject *exc = _PyErr_GetRaisedException(tstate);
message = PyUnicode_FromFormatV(format, va);
if (message != NULL) {
file = _PySys_GetRequiredAttr(key);
file = PySys_GetAttr(key);
if (sys_pyfile_write_unicode(message, file) != 0) {
_PyErr_Clear(tstate);
utf8 = PyUnicode_AsUTF8(message);