[Python-checkins] CVS: python/dist/src/Objects unicodeobject.c,2.108,2.109
Guido van Rossum
gvanrossum@users.sourceforge.net
Wed, 29 Aug 2001 20:13:01 -0700
Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv27362
Modified Files:
unicodeobject.c
Log Message:
Make unicode subclassable.
Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.108
retrieving revision 2.109
diff -C2 -d -r2.108 -r2.109
*** unicodeobject.c 2001/08/16 13:15:00 2.108
--- unicodeobject.c 2001/08/30 03:12:59 2.109
***************
*** 5299,5302 ****
--- 5299,5305 ----
};
+ staticforward PyObject *
+ unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
+
static PyObject *
unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
***************
*** 5307,5311 ****
char *errors = NULL;
! assert(type == &PyUnicode_Type);
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode",
kwlist, &x, &encoding, &errors))
--- 5310,5315 ----
char *errors = NULL;
! if (type != &PyUnicode_Type)
! return unicode_subtype_new(type, args, kwds);
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:unicode",
kwlist, &x, &encoding, &errors))
***************
*** 5316,5319 ****
--- 5320,5349 ----
}
+ 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\
***************
*** 5345,5349 ****
0, /* tp_setattro */
&unicode_as_buffer, /* tp_as_buffer */
! Py_TPFLAGS_DEFAULT, /* tp_flags */
unicode_doc, /* tp_doc */
0, /* tp_traverse */
--- 5375,5379 ----
0, /* tp_setattro */
&unicode_as_buffer, /* tp_as_buffer */
! Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
unicode_doc, /* tp_doc */
0, /* tp_traverse */