From e023fe0eefc60d0e7c01cbb38d176ec2e59d9a2a Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 30 Aug 2001 03:12:59 +0000 Subject: [PATCH] Make unicode subclassable. --- Objects/unicodeobject.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index c25c5ac9cb3..a9b4eb2fc6b 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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 */