summaryrefslogtreecommitdiffstats
path: root/Objects/unicodeobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-08-30 03:12:59 (GMT)
committerGuido van Rossum <guido@python.org>2001-08-30 03:12:59 (GMT)
commite023fe0eefc60d0e7c01cbb38d176ec2e59d9a2a (patch)
treea6ed6edc0ff9bed84a166f92ac93e9c4361b41a2 /Objects/unicodeobject.c
parentae960afb5e85bc89d0a579385ecbf1a6a87b3db7 (diff)
downloadcpython-e023fe0eefc60d0e7c01cbb38d176ec2e59d9a2a.zip
cpython-e023fe0eefc60d0e7c01cbb38d176ec2e59d9a2a.tar.gz
cpython-e023fe0eefc60d0e7c01cbb38d176ec2e59d9a2a.tar.bz2
Make unicode subclassable.
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r--Objects/unicodeobject.c34
1 files changed, 32 insertions, 2 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index c25c5ac..a9b4eb2 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 */