]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Implemented two new functions in sys:
authorSjoerd Mullender <sjoerd@acm.org>
Tue, 29 Aug 1995 09:18:14 +0000 (09:18 +0000)
committerSjoerd Mullender <sjoerd@acm.org>
Tue, 29 Aug 1995 09:18:14 +0000 (09:18 +0000)
getcounts() returns a list of counts of allocations and
deallocations for all different object types.
getobjects(n [, type ]) returns a list of recently allocated
and not-yet-freed objects of the given type (all
objects if no type given).  Only the n most recent
(all if n==0) objects are returned.
getcounts is only available if compiled with -DCOUNT_ALLOCS,
getobjects is only available if compiled with -DTRACE_REFS.  Note that
everything must be compiled with these options!

Objects/object.c
Python/sysmodule.c

index 1643ec6fc1c8968269f474d2ae7da5c13cd27a7a..4899ef18fcddcda7928942cce3753cb5bdf8d977 100644 (file)
@@ -56,6 +56,33 @@ dump_counts()
                null_strings, one_strings);
 }
 
+PyObject *
+get_counts()
+{
+       PyTypeObject *tp;
+       PyObject *result;
+       PyObject *v;
+
+       result = PyList_New(0);
+       if (result == NULL)
+               return NULL;
+       for (tp = type_list; tp; tp = tp->tp_next) {
+               v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
+                                 tp->tp_free, tp->tp_maxalloc);
+               if (v == NULL) {
+                       Py_DECREF(result);
+                       return NULL;
+               }
+               if (PyList_Append(result, v) < 0) {
+                       Py_DECREF(v);
+                       Py_DECREF(result);
+                       return NULL;
+               }
+               Py_DECREF(v);
+       }
+       return result;
+}
+
 void
 inc_count(tp)
        typeobject *tp;
@@ -513,4 +540,35 @@ printrefs(fp)
        }
 }
 
+PyObject *
+getobjects(self, args)
+       PyObject *self;
+       PyObject *args;
+{
+       int i, n;
+       PyObject *t = NULL;
+       PyObject *res, *op;
+
+       if (!PyArg_ParseTuple(args, "i|O", &n, &t))
+               return NULL;
+       op = refchain._ob_next;
+       res = PyList_New(0);
+       if (res == NULL)
+               return NULL;
+       for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
+               while (op == self || op == args || op == res || op == t ||
+                      t != NULL && op->ob_type != (PyTypeObject *) t) {
+                       op = op->_ob_next;
+                       if (op == &refchain)
+                               return res;
+               }
+               if (PyList_Append(res, op) < 0) {
+                       Py_DECREF(res);
+                       return NULL;
+               }
+               op = op->_ob_next;
+       }
+       return res;
+}
+
 #endif
index 6e153e45946a5bd947887fa32633cf877c1b9bf7..5fc098d527bf3510c6d511c321d2f8a729ed8034 100644 (file)
@@ -154,8 +154,42 @@ sys_mdebug(self, args)
 }
 #endif /* USE_MALLOPT */
 
+static object *
+sys_getrefcount(self, args)
+       object *self;
+       object *args;
+{
+       object *arg;
+       if (!getargs(args, "O", &arg))
+               return NULL;
+       return newintobject((long) arg->ob_refcnt);
+}
+
+#ifdef COUNT_ALLOCS
+static PyObject *
+sys_getcounts(self, args)
+       PyObject *self, *args;
+{
+       extern PyObject *get_counts Py_PROTO((void));
+
+       if (!PyArg_Parse(args, ""))
+               return NULL;
+       return get_counts();
+}
+#endif
+
+#ifdef TRACE_REFS
+extern PyObject *getobjects Py_PROTO((PyObject *, PyObject *));
+#endif
 static struct methodlist sys_methods[] = {
        {"exit",        sys_exit, 0},
+       {"getrefcount", sys_getrefcount, 0},
+#ifdef COUNT_ALLOCS
+       {"getcounts",   sys_getcounts, 0},
+#endif
+#ifdef TRACE_REFS
+       {"getobjects",  getobjects, 1},
+#endif
 #ifdef USE_MALLOPT
        {"mdebug",      sys_mdebug, 0},
 #endif