summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-12-16 17:39:08 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-12-16 17:39:08 (GMT)
commit5bbd231f27f254cd92ce543dca82f65c4d90f20d (patch)
treeade2ec19f3cfa52ffe72b05ba37906e06426ba44 /Modules
parent05dadcfb28b815c9558fe2a6a74cd3ce7df62577 (diff)
downloadcpython-5bbd231f27f254cd92ce543dca82f65c4d90f20d.zip
cpython-5bbd231f27f254cd92ce543dca82f65c4d90f20d.tar.gz
cpython-5bbd231f27f254cd92ce543dca82f65c4d90f20d.tar.bz2
Issue #15513: Added a __sizeof__ implementation for pickle classes.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_pickle.c58
-rw-r--r--Modules/clinic/_pickle.c.h56
2 files changed, 112 insertions, 2 deletions
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 7faf96d..10eb513 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -375,7 +375,7 @@ static PyTypeObject Pdata_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_pickle.Pdata", /*tp_name*/
sizeof(Pdata), /*tp_basicsize*/
- 0, /*tp_itemsize*/
+ sizeof(PyObject *), /*tp_itemsize*/
(destructor)Pdata_dealloc, /*tp_dealloc*/
};
@@ -3930,9 +3930,37 @@ _pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Py_RETURN_NONE;
}
+/*[clinic input]
+
+_pickle.Pickler.__sizeof__ -> Py_ssize_t
+
+Returns size in memory, in bytes.
+[clinic start generated code]*/
+
+static Py_ssize_t
+_pickle_Pickler___sizeof___impl(PicklerObject *self)
+/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
+{
+ Py_ssize_t res, s;
+
+ res = sizeof(PicklerObject);
+ if (self->memo != NULL) {
+ res += sizeof(PyMemoTable);
+ res += self->memo->mt_allocated * sizeof(PyMemoEntry);
+ }
+ if (self->output_buffer != NULL) {
+ s = _PySys_GetSizeOf(self->output_buffer);
+ if (s == -1)
+ return -1;
+ res += s;
+ }
+ return res;
+}
+
static struct PyMethodDef Pickler_methods[] = {
_PICKLE_PICKLER_DUMP_METHODDEF
_PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
+ _PICKLE_PICKLER___SIZEOF___METHODDEF
{NULL, NULL} /* sentinel */
};
@@ -6289,9 +6317,37 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name,
return global;
}
+/*[clinic input]
+
+_pickle.Unpickler.__sizeof__ -> Py_ssize_t
+
+Returns size in memory, in bytes.
+[clinic start generated code]*/
+
+static Py_ssize_t
+_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
+/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
+{
+ Py_ssize_t res;
+
+ res = sizeof(UnpicklerObject);
+ if (self->memo != NULL)
+ res += self->memo_size * sizeof(PyObject *);
+ if (self->marks != NULL)
+ res += self->marks_size * sizeof(Py_ssize_t);
+ if (self->input_line != NULL)
+ res += strlen(self->input_line) + 1;
+ if (self->encoding != NULL)
+ res += strlen(self->encoding) + 1;
+ if (self->errors != NULL)
+ res += strlen(self->errors) + 1;
+ return res;
+}
+
static struct PyMethodDef Unpickler_methods[] = {
_PICKLE_UNPICKLER_LOAD_METHODDEF
_PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
+ _PICKLE_UNPICKLER___SIZEOF___METHODDEF
{NULL, NULL} /* sentinel */
};
diff --git a/Modules/clinic/_pickle.c.h b/Modules/clinic/_pickle.c.h
index a20a61e..975298c 100644
--- a/Modules/clinic/_pickle.c.h
+++ b/Modules/clinic/_pickle.c.h
@@ -34,6 +34,33 @@ PyDoc_STRVAR(_pickle_Pickler_dump__doc__,
#define _PICKLE_PICKLER_DUMP_METHODDEF \
{"dump", (PyCFunction)_pickle_Pickler_dump, METH_O, _pickle_Pickler_dump__doc__},
+PyDoc_STRVAR(_pickle_Pickler___sizeof____doc__,
+"__sizeof__($self, /)\n"
+"--\n"
+"\n"
+"Returns size in memory, in bytes.");
+
+#define _PICKLE_PICKLER___SIZEOF___METHODDEF \
+ {"__sizeof__", (PyCFunction)_pickle_Pickler___sizeof__, METH_NOARGS, _pickle_Pickler___sizeof____doc__},
+
+static Py_ssize_t
+_pickle_Pickler___sizeof___impl(PicklerObject *self);
+
+static PyObject *
+_pickle_Pickler___sizeof__(PicklerObject *self, PyObject *Py_UNUSED(ignored))
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t _return_value;
+
+ _return_value = _pickle_Pickler___sizeof___impl(self);
+ if ((_return_value == -1) && PyErr_Occurred())
+ goto exit;
+ return_value = PyLong_FromSsize_t(_return_value);
+
+exit:
+ return return_value;
+}
+
PyDoc_STRVAR(_pickle_Pickler___init____doc__,
"Pickler(file, protocol=None, fix_imports=True)\n"
"--\n"
@@ -191,6 +218,33 @@ exit:
return return_value;
}
+PyDoc_STRVAR(_pickle_Unpickler___sizeof____doc__,
+"__sizeof__($self, /)\n"
+"--\n"
+"\n"
+"Returns size in memory, in bytes.");
+
+#define _PICKLE_UNPICKLER___SIZEOF___METHODDEF \
+ {"__sizeof__", (PyCFunction)_pickle_Unpickler___sizeof__, METH_NOARGS, _pickle_Unpickler___sizeof____doc__},
+
+static Py_ssize_t
+_pickle_Unpickler___sizeof___impl(UnpicklerObject *self);
+
+static PyObject *
+_pickle_Unpickler___sizeof__(UnpicklerObject *self, PyObject *Py_UNUSED(ignored))
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t _return_value;
+
+ _return_value = _pickle_Unpickler___sizeof___impl(self);
+ if ((_return_value == -1) && PyErr_Occurred())
+ goto exit;
+ return_value = PyLong_FromSsize_t(_return_value);
+
+exit:
+ return return_value;
+}
+
PyDoc_STRVAR(_pickle_Unpickler___init____doc__,
"Unpickler(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
"--\n"
@@ -488,4 +542,4 @@ _pickle_loads(PyModuleDef *module, PyObject *args, PyObject *kwargs)
exit:
return return_value;
}
-/*[clinic end generated code: output=f965b6c7018c898d input=a9049054013a1b77]*/
+/*[clinic end generated code: output=3aba79576e240c62 input=a9049054013a1b77]*/