Make identifiers str (not str8) objects throughout.
This affects the parser, various object implementations,
and all places that put identifiers into C string literals.
In testing, a number of crashes occurred as code would
fail when the recursion limit was reached (such as the
Unicode interning dictionary having key/value pairs where
key is not value). To solve these, I added an overflowed
flag, which allows for 50 more recursions after the
limit was reached and the exception was raised, and
a recursion_critical flag, which indicates that recursion
absolutely must be allowed, i.e. that a certain call
must not cause a stack overflow exception.
There are still some places where both str and str8 are
accepted as identifiers; these should eventually be
removed.
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 97b2c5e..d4c8a74 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -48,7 +48,7 @@
}
func = PyTuple_GET_ITEM(args, 0); /* Better be callable */
name = PyTuple_GET_ITEM(args, 1);
- if (!PyString_Check(name)) {
+ if ((!PyString_Check(name) && !PyUnicode_Check(name))) {
PyErr_SetString(PyExc_TypeError,
"__build_class__: name is not a string");
return NULL;
@@ -835,20 +835,23 @@
static PyObject *
builtin_getattr(PyObject *self, PyObject *args)
{
- PyObject *v, *result, *dflt = NULL;
+ PyObject *v, *result, *dflt = NULL, *release = NULL;
PyObject *name;
if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
return NULL;
- if (PyUnicode_Check(name)) {
- name = _PyUnicode_AsDefaultEncodedString(name, NULL);
- if (name == NULL)
+
+ if (PyString_Check(name)) {
+ release = PyString_AsDecodedObject(name, NULL, NULL);
+ if (!release)
return NULL;
+ name = release;
}
- if (!PyString_Check(name)) {
+ if (!PyUnicode_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"getattr(): attribute name must be string");
+ Py_XDECREF(release);
return NULL;
}
result = PyObject_GetAttr(v, name);
@@ -859,6 +862,7 @@
Py_INCREF(dflt);
result = dflt;
}
+ Py_XDECREF(release);
return result;
}
@@ -894,13 +898,7 @@
if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
return NULL;
- if (PyUnicode_Check(name)) {
- name = _PyUnicode_AsDefaultEncodedString(name, NULL);
- if (name == NULL)
- return NULL;
- }
-
- if (!PyString_Check(name)) {
+ if (!PyUnicode_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"hasattr(): attribute name must be string");
return NULL;