]> granicus.if.org Git - python/commitdiff
Check unicode identifier directly instead of converting
authorWalter Dörwald <walter@livinglogic.de>
Mon, 11 Jun 2007 14:55:19 +0000 (14:55 +0000)
committerWalter Dörwald <walter@livinglogic.de>
Mon, 11 Jun 2007 14:55:19 +0000 (14:55 +0000)
it to an 8bit string first.

Lib/test/test_descr.py
Objects/typeobject.c

index f20f676aa243091214e34f45a191c0d31f812e60..fca006158536a30e76c5e328d80c1ff66e1cc99f 100644 (file)
@@ -1083,6 +1083,13 @@ def slots():
         pass
     else:
         raise TestFailed, "['foo\\0bar'] slots not caught"
+    try:
+        class C(object):
+            __slots__ = ["foo\u1234bar"]
+    except TypeError:
+        pass
+    else:
+        raise TestFailed, "['foo\\u1234bar'] slots not caught"
     try:
         class C(object):
             __slots__ = ["1"]
index bc5fad1c1370b9f5f41bdca57e778ba450aa0485..b826eb4be0f86db4c53a36fe520c81bd4c439aa0 100644 (file)
@@ -1561,7 +1561,7 @@ static PyGetSetDef subtype_getsets_weakref_only[] = {
 static int
 valid_identifier(PyObject *s)
 {
-       unsigned char *p;
+       Py_UNICODE *p;
        Py_ssize_t i, n;
 
        if (!PyUnicode_Check(s)) {
@@ -1570,14 +1570,14 @@ valid_identifier(PyObject *s)
                             s->ob_type->tp_name);
                return 0;
        }
-       p = (unsigned char *) PyUnicode_AsString(s);
-       n = strlen((char*)p)/*XXX PyString_GET_SIZE(s)*/;
+       p = PyUnicode_AS_UNICODE(s);
+       n = PyUnicode_GET_SIZE(s);
        /* We must reject an empty name.  As a hack, we bump the
           length to 1 so that the loop will balk on the trailing \0. */
        if (n == 0)
                n = 1;
        for (i = 0; i < n; i++, p++) {
-               if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
+               if (i > 255 || (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_')) {
                        PyErr_SetString(PyExc_TypeError,
                                        "__slots__ must be identifiers");
                        return 0;