summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2002-10-14 21:07:28 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2002-10-14 21:07:28 (GMT)
commitd919a59ab58be7dec14ea78c182b8d4545559adb (patch)
tree842191da76b404bc2e659c5bb8a1ed7dae95d2fa /Objects
parentff9284bc2e3b702e75fe27ab05c7ae4e5b080743 (diff)
downloadcpython-d919a59ab58be7dec14ea78c182b8d4545559adb.zip
cpython-d919a59ab58be7dec14ea78c182b8d4545559adb.tar.gz
cpython-d919a59ab58be7dec14ea78c182b8d4545559adb.tar.bz2
Allow Unicode strings in __slots__, converting them to byte strings.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/typeobject.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index a12e7df..cfd5409 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -1003,6 +1003,38 @@ valid_identifier(PyObject *s)
return 1;
}
+#ifdef Py_USING_UNICODE
+/* Replace Unicode objects in slots. */
+
+static PyObject *
+_unicode_to_string(PyObject *slots, int nslots)
+{
+ PyObject *tmp = slots;
+ PyObject *o, *o1;
+ int i;
+ intintargfunc copy = slots->ob_type->tp_as_sequence->sq_slice;
+ for (i = 0; i < nslots; i++) {
+ if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) {
+ if (tmp == slots) {
+ tmp = copy(slots, 0, PyTuple_GET_SIZE(slots));
+ if (tmp == NULL)
+ return NULL;
+ }
+ o1 = _PyUnicode_AsDefaultEncodedString
+ (o, NULL);
+ if (o1 == NULL) {
+ Py_DECREF(tmp);
+ return 0;
+ }
+ Py_INCREF(o1);
+ Py_DECREF(o);
+ PyTuple_SET_ITEM(tmp, i, o1);
+ }
+ }
+ return tmp;
+}
+#endif
+
static PyObject *
type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
{
@@ -1135,6 +1167,13 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
return NULL;
}
+#ifdef Py_USING_UNICODE
+ tmp = _unicode_to_string(slots, nslots);
+ Py_DECREF(slots);
+ slots = tmp;
+ if (!tmp)
+ return NULL;
+#endif
/* Check for valid slot names and two special cases */
for (i = 0; i < nslots; i++) {
PyObject *tmp = PyTuple_GET_ITEM(slots, i);