]> granicus.if.org Git - python/commitdiff
Make unicode subclassable.
authorGuido van Rossum <guido@python.org>
Thu, 30 Aug 2001 03:12:59 +0000 (03:12 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 30 Aug 2001 03:12:59 +0000 (03:12 +0000)
Objects/unicodeobject.c

index c25c5ac9cb3e104886a65f0c39371cbd386e74fd..a9b4eb2fc6b20daf5753f9f4f19b45f68e13f2f1 100644 (file)
@@ -5298,6 +5298,9 @@ static PyBufferProcs unicode_as_buffer = {
     (getcharbufferproc) unicode_buffer_getcharbuf,
 };
 
+staticforward PyObject *
+unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
+
 static PyObject *
 unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
@@ -5306,7 +5309,8 @@ unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
        char *encoding = NULL;
        char *errors = NULL;
 
-       assert(type == &PyUnicode_Type);
+       if (type != &PyUnicode_Type)
+               return unicode_subtype_new(type, args, kwds);
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode",
                                          kwlist, &x, &encoding, &errors))
            return NULL;
@@ -5315,6 +5319,32 @@ unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
        return PyUnicode_FromEncodedObject(x, encoding, errors);
 }
 
+static PyObject *
+unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+       PyUnicodeObject *tmp, *new;
+       int n;
+
+       assert(PyType_IsSubtype(type, &PyUnicode_Type));
+       tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds);
+       if (tmp == NULL)
+               return NULL;
+       assert(PyUnicode_Check(tmp));
+       new = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length);
+       if (new == NULL)
+               return NULL;
+       new->str = PyMem_NEW(Py_UNICODE, n+1);
+       if (new->str == NULL) {
+               _Py_ForgetReference((PyObject *)new);
+               PyObject_DEL(new);
+               return NULL;
+       }
+       Py_UNICODE_COPY(new->str, tmp->str, n+1);
+       new->length = n;
+       Py_DECREF(tmp);
+       return (PyObject *)new;
+}
+
 static char unicode_doc[] =
 "unicode(string [, encoding[, errors]]) -> object\n\
 \n\
@@ -5344,7 +5374,7 @@ PyTypeObject PyUnicode_Type = {
     PyObject_GenericGetAttr,           /* tp_getattro */
     0,                                 /* tp_setattro */
     &unicode_as_buffer,                        /* tp_as_buffer */
-    Py_TPFLAGS_DEFAULT,                        /* tp_flags */
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
     unicode_doc,                       /* tp_doc */
     0,                                 /* tp_traverse */
     0,                                 /* tp_clear */