summaryrefslogtreecommitdiffstats
path: root/Objects/tupleobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-08-30 03:11:59 (GMT)
committerGuido van Rossum <guido@python.org>2001-08-30 03:11:59 (GMT)
commitae960afb5e85bc89d0a579385ecbf1a6a87b3db7 (patch)
tree82805cbb67e244e9e74c921399252a67b6b60c08 /Objects/tupleobject.c
parent147b13c069b192a08774f86dea55d541d21c64ac (diff)
downloadcpython-ae960afb5e85bc89d0a579385ecbf1a6a87b3db7.zip
cpython-ae960afb5e85bc89d0a579385ecbf1a6a87b3db7.tar.gz
cpython-ae960afb5e85bc89d0a579385ecbf1a6a87b3db7.tar.bz2
Make str and tuple types subclassable.
Diffstat (limited to 'Objects/tupleobject.c')
-rw-r--r--Objects/tupleobject.c32
1 files changed, 30 insertions, 2 deletions
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 5d01a99..9ae732f 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -475,13 +475,17 @@ tuplerichcompare(PyObject *v, PyObject *w, int op)
return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
}
+staticforward PyObject *
+tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
+
static PyObject *
tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *arg = NULL;
static char *kwlist[] = {"sequence", 0};
- assert(type == &PyTuple_Type);
+ if (type != &PyTuple_Type)
+ return tuple_subtype_new(type, args, kwds);
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
return NULL;
@@ -491,6 +495,29 @@ tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return PySequence_Tuple(arg);
}
+static PyObject *
+tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ PyObject *tmp, *new, *item;
+ int i, n;
+
+ assert(PyType_IsSubtype(type, &PyTuple_Type));
+ tmp = tuple_new(&PyTuple_Type, args, kwds);
+ if (tmp == NULL)
+ return NULL;
+ assert(PyTuple_Check(tmp));
+ new = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
+ if (new == NULL)
+ return NULL;
+ for (i = 0; i < n; i++) {
+ item = PyTuple_GET_ITEM(tmp, i);
+ Py_INCREF(item);
+ PyTuple_SET_ITEM(new, i, item);
+ }
+ Py_DECREF(tmp);
+ return new;
+}
+
static char tuple_doc[] =
"tuple(sequence) -> list\n\
\n\
@@ -529,7 +556,8 @@ PyTypeObject PyTuple_Type = {
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
+ Py_TPFLAGS_BASETYPE, /* tp_flags */
tuple_doc, /* tp_doc */
(traverseproc)tupletraverse, /* tp_traverse */
0, /* tp_clear */