Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c160a0

Browse files
committed
Patch #517521: Consider byte strings before Unicode strings
in PyObject_Get/SetAttr.
1 parent e5363b7 commit 0c160a0

1 file changed

Lines changed: 60 additions & 52 deletions

File tree

Objects/object.c

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,21 +1085,23 @@ PyObject_GetAttr(PyObject *v, PyObject *name)
10851085
{
10861086
PyTypeObject *tp = v->ob_type;
10871087

1088+
if (!PyString_Check(name)) {
10881089
#ifdef Py_USING_UNICODE
1089-
/* The Unicode to string conversion is done here because the
1090-
existing tp_getattro slots expect a string object as name
1091-
and we wouldn't want to break those. */
1092-
if (PyUnicode_Check(name)) {
1093-
name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1094-
if (name == NULL)
1095-
return NULL;
1096-
}
1097-
else
1090+
/* The Unicode to string conversion is done here because the
1091+
existing tp_getattro slots expect a string object as name
1092+
and we wouldn't want to break those. */
1093+
if (PyUnicode_Check(name)) {
1094+
name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1095+
if (name == NULL)
1096+
return NULL;
1097+
}
1098+
else
10981099
#endif
1099-
if (!PyString_Check(name)) {
1100-
PyErr_SetString(PyExc_TypeError,
1101-
"attribute name must be string");
1102-
return NULL;
1100+
{
1101+
PyErr_SetString(PyExc_TypeError,
1102+
"attribute name must be string");
1103+
return NULL;
1104+
}
11031105
}
11041106
if (tp->tp_getattro != NULL)
11051107
return (*tp->tp_getattro)(v, name);
@@ -1129,21 +1131,23 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
11291131
PyTypeObject *tp = v->ob_type;
11301132
int err;
11311133

1134+
if (!PyString_Check(name)){
11321135
#ifdef Py_USING_UNICODE
1133-
/* The Unicode to string conversion is done here because the
1134-
existing tp_setattro slots expect a string object as name
1135-
and we wouldn't want to break those. */
1136-
if (PyUnicode_Check(name)) {
1137-
name = PyUnicode_AsEncodedString(name, NULL, NULL);
1138-
if (name == NULL)
1139-
return -1;
1140-
}
1141-
else
1136+
/* The Unicode to string conversion is done here because the
1137+
existing tp_setattro slots expect a string object as name
1138+
and we wouldn't want to break those. */
1139+
if (PyUnicode_Check(name)) {
1140+
name = PyUnicode_AsEncodedString(name, NULL, NULL);
1141+
if (name == NULL)
1142+
return -1;
1143+
}
1144+
else
11421145
#endif
1143-
if (!PyString_Check(name)){
1144-
PyErr_SetString(PyExc_TypeError,
1145-
"attribute name must be string");
1146-
return -1;
1146+
{
1147+
PyErr_SetString(PyExc_TypeError,
1148+
"attribute name must be string");
1149+
return -1;
1150+
}
11471151
}
11481152
else
11491153
Py_INCREF(name);
@@ -1217,21 +1221,23 @@ PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
12171221
descrgetfunc f;
12181222
PyObject **dictptr;
12191223

1224+
if (!PyString_Check(name)){
12201225
#ifdef Py_USING_UNICODE
1221-
/* The Unicode to string conversion is done here because the
1222-
existing tp_setattro slots expect a string object as name
1223-
and we wouldn't want to break those. */
1224-
if (PyUnicode_Check(name)) {
1225-
name = PyUnicode_AsEncodedString(name, NULL, NULL);
1226-
if (name == NULL)
1227-
return NULL;
1228-
}
1229-
else
1226+
/* The Unicode to string conversion is done here because the
1227+
existing tp_setattro slots expect a string object as name
1228+
and we wouldn't want to break those. */
1229+
if (PyUnicode_Check(name)) {
1230+
name = PyUnicode_AsEncodedString(name, NULL, NULL);
1231+
if (name == NULL)
1232+
return NULL;
1233+
}
1234+
else
12301235
#endif
1231-
if (!PyString_Check(name)){
1232-
PyErr_SetString(PyExc_TypeError,
1233-
"attribute name must be string");
1234-
return NULL;
1236+
{
1237+
PyErr_SetString(PyExc_TypeError,
1238+
"attribute name must be string");
1239+
return NULL;
1240+
}
12351241
}
12361242
else
12371243
Py_INCREF(name);
@@ -1291,21 +1297,23 @@ PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
12911297
PyObject **dictptr;
12921298
int res = -1;
12931299

1300+
if (!PyString_Check(name)){
12941301
#ifdef Py_USING_UNICODE
1295-
/* The Unicode to string conversion is done here because the
1296-
existing tp_setattro slots expect a string object as name
1297-
and we wouldn't want to break those. */
1298-
if (PyUnicode_Check(name)) {
1299-
name = PyUnicode_AsEncodedString(name, NULL, NULL);
1300-
if (name == NULL)
1301-
return -1;
1302-
}
1303-
else
1302+
/* The Unicode to string conversion is done here because the
1303+
existing tp_setattro slots expect a string object as name
1304+
and we wouldn't want to break those. */
1305+
if (PyUnicode_Check(name)) {
1306+
name = PyUnicode_AsEncodedString(name, NULL, NULL);
1307+
if (name == NULL)
1308+
return -1;
1309+
}
1310+
else
13041311
#endif
1305-
if (!PyString_Check(name)){
1306-
PyErr_SetString(PyExc_TypeError,
1307-
"attribute name must be string");
1308-
return -1;
1312+
{
1313+
PyErr_SetString(PyExc_TypeError,
1314+
"attribute name must be string");
1315+
return -1;
1316+
}
13091317
}
13101318
else
13111319
Py_INCREF(name);

0 commit comments

Comments
 (0)