[Python-checkins] r63748 - python/branches/okkoto-sizeof/Objects/stringobject.c

robert.schuppenies python-checkins at python.org
Tue May 27 18:27:14 CEST 2008


Author: robert.schuppenies
Date: Tue May 27 18:27:13 2008
New Revision: 63748

Log:
added magic __sizeof__ method for string


Modified:
   python/branches/okkoto-sizeof/Objects/stringobject.c

Modified: python/branches/okkoto-sizeof/Objects/stringobject.c
==============================================================================
--- python/branches/okkoto-sizeof/Objects/stringobject.c	(original)
+++ python/branches/okkoto-sizeof/Objects/stringobject.c	Tue May 27 18:27:13 2008
@@ -3920,6 +3920,17 @@
     return NULL;
 }
 
+PyDoc_STRVAR(sizeof__doc__,
+"S.__sizeof__() -> size of S in bytes");
+
+static PyObject *
+string_sizeof(PyStringObject *v)
+{
+	Py_ssize_t res;
+	res = sizeof(PyStringObject) + v->ob_size * v->ob_type->tp_itemsize;
+	return PyInt_FromSsize_t(res);
+}
+
 #undef SPLIT_APPEND
 #undef SPLIT_ADD
 #undef MAX_PREALLOC
@@ -3997,7 +4008,9 @@
 	{"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS,
 	 expandtabs__doc__},
 	{"splitlines", (PyCFunction)string_splitlines, METH_VARARGS,
-	 splitlines__doc__},
+	 sizeof__doc__},
+	{"__sizeof__", (PyCFunction)string_sizeof, METH_NOARGS,
+ 	 splitlines__doc__},
 	{"__getnewargs__",	(PyCFunction)string_getnewargs,	METH_NOARGS},
 	{NULL,     NULL}		     /* sentinel */
 };


More information about the Python-checkins mailing list