summaryrefslogtreecommitdiffstats
path: root/Objects/stringobject.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/stringobject.c
parent147b13c069b192a08774f86dea55d541d21c64ac (diff)
downloadcpython-ae960afb5e85bc89d0a579385ecbf1a6a87b3db7.zip
cpython-ae960afb5e85bc89d0a579385ecbf1a6a87b3db7.tar.gz
cpython-ae960afb5e85bc89d0a579385ecbf1a6a87b3db7.tar.bz2
Make str and tuple types subclassable.
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r--Objects/stringobject.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 1332881..7675f7e 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -2683,13 +2683,17 @@ string_methods[] = {
{NULL, NULL} /* sentinel */
};
+staticforward PyObject *
+str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
+
static PyObject *
string_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *x = NULL;
static char *kwlist[] = {"object", 0};
- assert(type == &PyString_Type);
+ if (type != &PyString_Type)
+ return str_subtype_new(type, args, kwds);
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x))
return NULL;
if (x == NULL)
@@ -2697,6 +2701,24 @@ string_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return PyObject_Str(x);
}
+static PyObject *
+str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ PyObject *tmp, *new;
+ int n;
+
+ assert(PyType_IsSubtype(type, &PyString_Type));
+ tmp = string_new(&PyString_Type, args, kwds);
+ if (tmp == NULL)
+ return NULL;
+ assert(PyString_Check(tmp));
+ new = type->tp_alloc(type, n = PyString_GET_SIZE(tmp));
+ if (new == NULL)
+ return NULL;
+ memcpy(PyString_AS_STRING(new), PyString_AS_STRING(tmp), n+1);
+ return new;
+}
+
static char string_doc[] =
"str(object) -> string\n\
\n\
@@ -2724,7 +2746,7 @@ PyTypeObject PyString_Type = {
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
&string_as_buffer, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT, /* tp_flags */
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
string_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */