diff options
author | Robert Schuppenies <okkotonushi@googlemail.com> | 2008-06-10 10:10:31 +0000 |
---|---|---|
committer | Robert Schuppenies <okkotonushi@googlemail.com> | 2008-06-10 10:10:31 +0000 |
commit | 901c997de0ea941fe60e8b0671b126fec0c58cb9 (patch) | |
tree | ebde2f646d98116493d94d8404d2ce2e7292d79a /Objects/unicodeobject.c | |
parent | 0705bc08232ca82df7d77ad39c1de2e9d8c2f071 (diff) | |
download | cpython-git-901c997de0ea941fe60e8b0671b126fec0c58cb9.tar.gz |
Issue 3048: Fixed sys.getsizeof for unicode objects.
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 873f1c4395..840efb9de3 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -7895,6 +7895,29 @@ PyDoc_STRVAR(p_format__doc__, \n\ "); +static PyObject * +unicode__sizeof__(PyUnicodeObject *v) +{ + PyObject *res = NULL, *defsize = NULL; + + res = PyInt_FromSsize_t(sizeof(PyUnicodeObject) + + sizeof(Py_UNICODE) * (v->length + 1)); + if (v->defenc) { + defsize = PyObject_CallMethod(v->defenc, "__sizeof__", NULL); + if (defsize == NULL) { + Py_DECREF(res); + return NULL; + } + res = PyNumber_Add(res, defsize); + Py_DECREF(defsize); + } + return res; +} + +PyDoc_STRVAR(sizeof__doc__, +"S.__sizeof__() -> size of S in memory, in bytes\n\ +\n\ +"); static PyObject * unicode_getnewargs(PyUnicodeObject *v) @@ -7952,6 +7975,7 @@ static PyMethodDef unicode_methods[] = { {"__format__", (PyCFunction) unicode__format__, METH_VARARGS, p_format__doc__}, {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, + {"__sizeof__", (PyCFunction) unicode__sizeof__, METH_NOARGS, sizeof__doc__}, #if 0 {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__}, #endif |