a43d18
diff -up Python-2.7.2/Include/dictobject.h.add-debug-malloc-stats Python-2.7.2/Include/dictobject.h
044ee6
--- Python-2.7.2/Include/dictobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
044ee6
+++ Python-2.7.2/Include/dictobject.h	2011-09-16 19:03:25.105821625 -0400
a43d18
@@ -150,6 +150,8 @@ PyAPI_FUNC(PyObject *) PyDict_GetItemStr
a43d18
 PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item);
a43d18
 PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key);
a43d18
 
a43d18
+PyAPI_FUNC(void) _PyDict_DebugMallocStats(FILE *out);
a43d18
+
a43d18
 #ifdef __cplusplus
a43d18
 }
a43d18
 #endif
a43d18
diff -up Python-2.7.2/Include/floatobject.h.add-debug-malloc-stats Python-2.7.2/Include/floatobject.h
a43d18
--- Python-2.7.2/Include/floatobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
044ee6
+++ Python-2.7.2/Include/floatobject.h	2011-09-16 19:03:25.106821625 -0400
a43d18
@@ -132,6 +132,7 @@ PyAPI_FUNC(PyObject *) _PyFloat_FormatAd
a43d18
    failure.  Used in builtin_round in bltinmodule.c. */
a43d18
 PyAPI_FUNC(PyObject *) _Py_double_round(double x, int ndigits);
a43d18
 
a43d18
+PyAPI_FUNC(void) _PyFloat_DebugMallocStats(FILE* out);
a43d18
 
a43d18
 
a43d18
 #ifdef __cplusplus
a43d18
diff -up Python-2.7.2/Include/frameobject.h.add-debug-malloc-stats Python-2.7.2/Include/frameobject.h
044ee6
--- Python-2.7.2/Include/frameobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
044ee6
+++ Python-2.7.2/Include/frameobject.h	2011-09-16 19:03:25.107821625 -0400
a43d18
@@ -80,6 +80,8 @@ PyAPI_FUNC(void) PyFrame_FastToLocals(Py
a43d18
 
a43d18
 PyAPI_FUNC(int) PyFrame_ClearFreeList(void);
a43d18
 
a43d18
+PyAPI_FUNC(void) _PyFrame_DebugMallocStats(FILE *out);
a43d18
+
a43d18
 /* Return the line of code the frame is currently executing. */
a43d18
 PyAPI_FUNC(int) PyFrame_GetLineNumber(PyFrameObject *);
a43d18
 
a43d18
diff -up Python-2.7.2/Include/intobject.h.add-debug-malloc-stats Python-2.7.2/Include/intobject.h
a43d18
--- Python-2.7.2/Include/intobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
044ee6
+++ Python-2.7.2/Include/intobject.h	2011-09-16 19:03:25.107821625 -0400
a43d18
@@ -74,6 +74,8 @@ PyAPI_FUNC(PyObject *) _PyInt_FormatAdva
a43d18
 					     char *format_spec,
a43d18
 					     Py_ssize_t format_spec_len);
a43d18
 
a43d18
+PyAPI_FUNC(void) _PyInt_DebugMallocStats(FILE *out);
a43d18
+
a43d18
 #ifdef __cplusplus
a43d18
 }
a43d18
 #endif
a43d18
diff -up Python-2.7.2/Include/listobject.h.add-debug-malloc-stats Python-2.7.2/Include/listobject.h
044ee6
--- Python-2.7.2/Include/listobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
044ee6
+++ Python-2.7.2/Include/listobject.h	2011-09-16 19:03:25.107821625 -0400
a43d18
@@ -62,6 +62,8 @@ PyAPI_FUNC(PyObject *) _PyList_Extend(Py
a43d18
 #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
a43d18
 #define PyList_GET_SIZE(op)    Py_SIZE(op)
a43d18
 
a43d18
+PyAPI_FUNC(void) _PyList_DebugMallocStats(FILE *out);
a43d18
+
a43d18
 #ifdef __cplusplus
a43d18
 }
a43d18
 #endif
a43d18
diff -up Python-2.7.2/Include/methodobject.h.add-debug-malloc-stats Python-2.7.2/Include/methodobject.h
044ee6
--- Python-2.7.2/Include/methodobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
044ee6
+++ Python-2.7.2/Include/methodobject.h	2011-09-16 19:03:25.108821625 -0400
a43d18
@@ -87,6 +87,10 @@ typedef struct {
a43d18
 
a43d18
 PyAPI_FUNC(int) PyCFunction_ClearFreeList(void);
a43d18
 
a43d18
+PyAPI_FUNC(void) _PyCFunction_DebugMallocStats(FILE *out);
a43d18
+PyAPI_FUNC(void) _PyMethod_DebugMallocStats(FILE *out);
a43d18
+
a43d18
+
a43d18
 #ifdef __cplusplus
a43d18
 }
a43d18
 #endif
a43d18
diff -up Python-2.7.2/Include/object.h.add-debug-malloc-stats Python-2.7.2/Include/object.h
a43d18
--- Python-2.7.2/Include/object.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
044ee6
+++ Python-2.7.2/Include/object.h	2011-09-16 19:03:25.108821625 -0400
a43d18
@@ -980,6 +980,13 @@ PyAPI_DATA(PyObject *) _PyTrash_delete_l
a43d18
     else \
a43d18
         _PyTrash_deposit_object((PyObject*)op);
a43d18
 
a43d18
+PyAPI_FUNC(void)
a43d18
+_PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks,
a43d18
+		       size_t sizeof_block);
a43d18
+
a43d18
+PyAPI_FUNC(void)
a43d18
+_PyObject_DebugTypeStats(FILE *out);
a43d18
+
a43d18
 #ifdef __cplusplus
a43d18
 }
a43d18
 #endif
a43d18
diff -up Python-2.7.2/Include/objimpl.h.add-debug-malloc-stats Python-2.7.2/Include/objimpl.h
a43d18
--- Python-2.7.2/Include/objimpl.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
044ee6
+++ Python-2.7.2/Include/objimpl.h	2011-09-16 19:03:25.108821625 -0400
a43d18
@@ -101,13 +101,13 @@ PyAPI_FUNC(void) PyObject_Free(void *);
a43d18
 
a43d18
 /* Macros */
a43d18
 #ifdef WITH_PYMALLOC
a43d18
+PyAPI_FUNC(void) _PyObject_DebugMallocStats(FILE *out);
a43d18
 #ifdef PYMALLOC_DEBUG   /* WITH_PYMALLOC && PYMALLOC_DEBUG */
a43d18
 PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes);
a43d18
 PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes);
a43d18
 PyAPI_FUNC(void) _PyObject_DebugFree(void *p);
a43d18
 PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p);
a43d18
 PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p);
a43d18
-PyAPI_FUNC(void) _PyObject_DebugMallocStats(void);
a43d18
 PyAPI_FUNC(void *) _PyObject_DebugMallocApi(char api, size_t nbytes);
a43d18
 PyAPI_FUNC(void *) _PyObject_DebugReallocApi(char api, void *p, size_t nbytes);
a43d18
 PyAPI_FUNC(void) _PyObject_DebugFreeApi(char api, void *p);
a43d18
diff -up Python-2.7.2/Include/stringobject.h.add-debug-malloc-stats Python-2.7.2/Include/stringobject.h
a43d18
--- Python-2.7.2/Include/stringobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
044ee6
+++ Python-2.7.2/Include/stringobject.h	2011-09-16 19:03:25.109821625 -0400
a43d18
@@ -204,6 +204,8 @@ PyAPI_FUNC(PyObject *) _PyBytes_FormatAd
a43d18
 					       char *format_spec,
a43d18
 					       Py_ssize_t format_spec_len);
a43d18
 
a43d18
+PyAPI_FUNC(void) _PyString_DebugMallocStats(FILE *out);
a43d18
+
a43d18
 #ifdef __cplusplus
a43d18
 }
a43d18
 #endif
a43d18
diff -up Python-2.7.2/Include/unicodeobject.h.add-debug-malloc-stats Python-2.7.2/Include/unicodeobject.h
a43d18
--- Python-2.7.2/Include/unicodeobject.h.add-debug-malloc-stats	2011-06-11 11:46:23.000000000 -0400
044ee6
+++ Python-2.7.2/Include/unicodeobject.h	2011-09-16 19:03:25.109821625 -0400
a43d18
@@ -1406,6 +1406,8 @@ PyAPI_FUNC(int) _PyUnicode_IsAlpha(
a43d18
     Py_UNICODE ch       /* Unicode character */
a43d18
     );
a43d18
 
a43d18
+PyAPI_FUNC(void) _PyUnicode_DebugMallocStats(FILE *out);
a43d18
+
a43d18
 #ifdef __cplusplus
a43d18
 }
a43d18
 #endif
a43d18
diff -up Python-2.7.2/Lib/test/test_sys.py.add-debug-malloc-stats Python-2.7.2/Lib/test/test_sys.py
044ee6
--- Python-2.7.2/Lib/test/test_sys.py.add-debug-malloc-stats	2011-09-16 19:03:25.048821626 -0400
044ee6
+++ Python-2.7.2/Lib/test/test_sys.py	2011-09-16 19:03:25.110821625 -0400
a43d18
@@ -473,6 +473,32 @@ class SysModuleTest(unittest.TestCase):
a43d18
         p.wait()
a43d18
         self.assertIn(executable, ["''", repr(sys.executable)])
a43d18
 
a43d18
+    def test_debugmallocstats(self):
a43d18
+        # Test sys._debugmallocstats()
a43d18
+
a43d18
+        import subprocess
a43d18
+
a43d18
+        # Verify the default of writing to stderr:
a43d18
+        p = subprocess.Popen([sys.executable,
a43d18
+                              '-c', 'import sys; sys._debugmallocstats()'],
a43d18
+                             stderr=subprocess.PIPE)
a43d18
+        out, err = p.communicate()
a43d18
+        p.wait()
a43d18
+        self.assertIn("arenas allocated current", err)
a43d18
+                                     
a43d18
+        # Verify that we can redirect the output to a file (not a file-like
a43d18
+        # object, though):
a43d18
+        with open('mallocstats.txt', 'w') as out:
a43d18
+            sys._debugmallocstats(out)
a43d18
+        result = open('mallocstats.txt').read()
a43d18
+        self.assertIn("arenas allocated current", result)
a43d18
+        os.unlink('mallocstats.txt')
a43d18
+
a43d18
+        # Verify that the destination must be a file:
a43d18
+        with self.assertRaises(TypeError):
a43d18
+            sys._debugmallocstats(42)
a43d18
+        
a43d18
+
a43d18
 class SizeofTest(unittest.TestCase):
a43d18
 
a43d18
     TPFLAGS_HAVE_GC = 1<<14
a43d18
diff -up Python-2.7.2/Objects/classobject.c.add-debug-malloc-stats Python-2.7.2/Objects/classobject.c
044ee6
--- Python-2.7.2/Objects/classobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/classobject.c	2011-09-16 19:03:25.110821625 -0400
a43d18
@@ -2670,3 +2670,12 @@ PyMethod_Fini(void)
a43d18
 {
a43d18
     (void)PyMethod_ClearFreeList();
a43d18
 }
a43d18
+
a43d18
+/* Print summary info about the state of the optimized allocator */
a43d18
+void
a43d18
+_PyMethod_DebugMallocStats(FILE *out)
a43d18
+{
a43d18
+    _PyDebugAllocatorStats(out,
a43d18
+                           "free PyMethodObject",
a43d18
+                           numfree, sizeof(PyMethodObject));
a43d18
+}
a43d18
diff -up Python-2.7.2/Objects/dictobject.c.add-debug-malloc-stats Python-2.7.2/Objects/dictobject.c
044ee6
--- Python-2.7.2/Objects/dictobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/dictobject.c	2011-09-16 19:03:25.111821625 -0400
a43d18
@@ -225,6 +225,15 @@ show_track(void)
a43d18
 static PyDictObject *free_list[PyDict_MAXFREELIST];
a43d18
 static int numfree = 0;
a43d18
 
a43d18
+/* Print summary info about the state of the optimized allocator */
a43d18
+void
a43d18
+_PyDict_DebugMallocStats(FILE *out)
a43d18
+{
a43d18
+    _PyDebugAllocatorStats(out,
a43d18
+                           "free PyDictObject", numfree, sizeof(PyDictObject));
a43d18
+}
a43d18
+
a43d18
+
a43d18
 void
a43d18
 PyDict_Fini(void)
a43d18
 {
a43d18
diff -up Python-2.7.2/Objects/floatobject.c.add-debug-malloc-stats Python-2.7.2/Objects/floatobject.c
a43d18
--- Python-2.7.2/Objects/floatobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/floatobject.c	2011-09-16 19:03:25.111821625 -0400
a43d18
@@ -35,6 +35,22 @@ typedef struct _floatblock PyFloatBlock;
a43d18
 static PyFloatBlock *block_list = NULL;
a43d18
 static PyFloatObject *free_list = NULL;
a43d18
 
a43d18
+/* Print summary info about the state of the optimized allocator */
a43d18
+void
a43d18
+_PyFloat_DebugMallocStats(FILE *out)
a43d18
+{
a43d18
+  int num_blocks = 0;
a43d18
+  PyFloatBlock *block;
a43d18
+
a43d18
+  /* Walk the block list, counting */
a43d18
+  for (block = block_list; block ; block = block->next) {
a43d18
+      num_blocks++;
a43d18
+  }
a43d18
+
a43d18
+  _PyDebugAllocatorStats(out,
a43d18
+                         "PyFloatBlock", num_blocks, sizeof(PyFloatBlock));
a43d18
+}
a43d18
+
a43d18
 static PyFloatObject *
a43d18
 fill_free_list(void)
a43d18
 {
a43d18
diff -up Python-2.7.2/Objects/frameobject.c.add-debug-malloc-stats Python-2.7.2/Objects/frameobject.c
044ee6
--- Python-2.7.2/Objects/frameobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/frameobject.c	2011-09-16 19:03:25.112821625 -0400
a43d18
@@ -980,3 +980,13 @@ PyFrame_Fini(void)
a43d18
     Py_XDECREF(builtin_object);
a43d18
     builtin_object = NULL;
a43d18
 }
a43d18
+
a43d18
+/* Print summary info about the state of the optimized allocator */
a43d18
+void
a43d18
+_PyFrame_DebugMallocStats(FILE *out)
a43d18
+{
a43d18
+    _PyDebugAllocatorStats(out,
a43d18
+                           "free PyFrameObject",
a43d18
+                           numfree, sizeof(PyFrameObject));
a43d18
+}
a43d18
+
a43d18
diff -up Python-2.7.2/Objects/intobject.c.add-debug-malloc-stats Python-2.7.2/Objects/intobject.c
a43d18
--- Python-2.7.2/Objects/intobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/intobject.c	2011-09-16 19:03:25.112821625 -0400
a43d18
@@ -44,6 +44,23 @@ typedef struct _intblock PyIntBlock;
a43d18
 static PyIntBlock *block_list = NULL;
a43d18
 static PyIntObject *free_list = NULL;
a43d18
 
a43d18
+
a43d18
+/* Print summary info about the state of the optimized allocator */
a43d18
+void
a43d18
+_PyInt_DebugMallocStats(FILE *out)
a43d18
+{
a43d18
+    int num_blocks = 0;
a43d18
+    PyIntBlock *block;
a43d18
+
a43d18
+    /* Walk the block list, counting */
a43d18
+    for (block = block_list; block ; block = block->next) {
a43d18
+        num_blocks++;
a43d18
+    }
a43d18
+
a43d18
+    _PyDebugAllocatorStats(out,
a43d18
+                           "PyIntBlock", num_blocks, sizeof(PyIntBlock));
a43d18
+}
a43d18
+
a43d18
 static PyIntObject *
a43d18
 fill_free_list(void)
a43d18
 {
a43d18
diff -up Python-2.7.2/Objects/listobject.c.add-debug-malloc-stats Python-2.7.2/Objects/listobject.c
044ee6
--- Python-2.7.2/Objects/listobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/listobject.c	2011-09-16 19:03:25.113821625 -0400
a43d18
@@ -109,6 +109,15 @@ PyList_Fini(void)
a43d18
     }
a43d18
 }
a43d18
 
a43d18
+/* Print summary info about the state of the optimized allocator */
a43d18
+void
a43d18
+_PyList_DebugMallocStats(FILE *out)
a43d18
+{
a43d18
+    _PyDebugAllocatorStats(out,
a43d18
+                           "free PyListObject",
a43d18
+                           numfree, sizeof(PyListObject));
a43d18
+}
a43d18
+
a43d18
 PyObject *
a43d18
 PyList_New(Py_ssize_t size)
a43d18
 {
a43d18
diff -up Python-2.7.2/Objects/methodobject.c.add-debug-malloc-stats Python-2.7.2/Objects/methodobject.c
044ee6
--- Python-2.7.2/Objects/methodobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/methodobject.c	2011-09-16 19:03:25.113821625 -0400
a43d18
@@ -412,6 +412,15 @@ PyCFunction_Fini(void)
a43d18
     (void)PyCFunction_ClearFreeList();
a43d18
 }
a43d18
 
a43d18
+/* Print summary info about the state of the optimized allocator */
a43d18
+void
a43d18
+_PyCFunction_DebugMallocStats(FILE *out)
a43d18
+{
a43d18
+    _PyDebugAllocatorStats(out,
a43d18
+                           "free PyCFunction",
a43d18
+                           numfree, sizeof(PyCFunction));
a43d18
+}
a43d18
+
a43d18
 /* PyCFunction_New() is now just a macro that calls PyCFunction_NewEx(),
a43d18
    but it's part of the API so we need to keep a function around that
a43d18
    existing C extensions can call.
a43d18
diff -up Python-2.7.2/Objects/object.c.add-debug-malloc-stats Python-2.7.2/Objects/object.c
a43d18
--- Python-2.7.2/Objects/object.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/object.c	2011-09-16 19:04:46.463820849 -0400
a43d18
@@ -2334,6 +2334,23 @@ PyMem_Free(void *p)
a43d18
     PyMem_FREE(p);
a43d18
 }
a43d18
 
a43d18
+void
a43d18
+_PyObject_DebugTypeStats(FILE *out)
a43d18
+{
044ee6
+    _PyString_DebugMallocStats(out);
044ee6
+    _PyCFunction_DebugMallocStats(out);
044ee6
+    _PyDict_DebugMallocStats(out);
044ee6
+    _PyFloat_DebugMallocStats(out);
044ee6
+    _PyFrame_DebugMallocStats(out);
044ee6
+    _PyInt_DebugMallocStats(out);
044ee6
+    _PyList_DebugMallocStats(out);
044ee6
+    _PyMethod_DebugMallocStats(out);
044ee6
+    _PySet_DebugMallocStats(out);
044ee6
+    _PyTuple_DebugMallocStats(out);
a43d18
+#if Py_USING_UNICODE
044ee6
+    _PyUnicode_DebugMallocStats(out);
a43d18
+#endif
a43d18
+}
a43d18
 
a43d18
 /* These methods are used to control infinite recursion in repr, str, print,
a43d18
    etc.  Container objects that may recursively contain themselves,
a43d18
diff -up Python-2.7.2/Objects/obmalloc.c.add-debug-malloc-stats Python-2.7.2/Objects/obmalloc.c
a43d18
--- Python-2.7.2/Objects/obmalloc.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/obmalloc.c	2011-09-16 19:03:25.114821625 -0400
a43d18
@@ -508,12 +508,10 @@ static struct arena_object* usable_arena
a43d18
 /* Number of arenas allocated that haven't been free()'d. */
a43d18
 static size_t narenas_currently_allocated = 0;
a43d18
 
a43d18
-#ifdef PYMALLOC_DEBUG
a43d18
 /* Total number of times malloc() called to allocate an arena. */
a43d18
 static size_t ntimes_arena_allocated = 0;
a43d18
 /* High water mark (max value ever seen) for narenas_currently_allocated. */
a43d18
 static size_t narenas_highwater = 0;
a43d18
-#endif
a43d18
 
a43d18
 /* Allocate a new arena.  If we run out of memory, return NULL.  Else
a43d18
  * allocate a new arena, and return the address of an arena_object
a43d18
@@ -528,7 +526,7 @@ new_arena(void)
a43d18
 
a43d18
 #ifdef PYMALLOC_DEBUG
a43d18
     if (Py_GETENV("PYTHONMALLOCSTATS"))
a43d18
-        _PyObject_DebugMallocStats();
a43d18
+        _PyObject_DebugMallocStats(stderr);
a43d18
 #endif
a43d18
     if (unused_arena_objects == NULL) {
a43d18
         uint i;
a43d18
@@ -588,11 +586,9 @@ new_arena(void)
a43d18
     }
a43d18
 
a43d18
     ++narenas_currently_allocated;
a43d18
-#ifdef PYMALLOC_DEBUG
a43d18
     ++ntimes_arena_allocated;
a43d18
     if (narenas_currently_allocated > narenas_highwater)
a43d18
         narenas_highwater = narenas_currently_allocated;
a43d18
-#endif
a43d18
     arenaobj->freepools = NULL;
a43d18
     /* pool_address <- first pool-aligned address in the arena
a43d18
        nfreepools <- number of whole pools that fit after alignment */
a43d18
@@ -1694,17 +1690,19 @@ _PyObject_DebugDumpAddress(const void *p
a43d18
     }
a43d18
 }
a43d18
 
a43d18
+#endif  /* PYMALLOC_DEBUG */
a43d18
+
a43d18
 static size_t
a43d18
-printone(const char* msg, size_t value)
a43d18
+printone(FILE *out, const char* msg, size_t value)
a43d18
 {
a43d18
     int i, k;
a43d18
     char buf[100];
a43d18
     size_t origvalue = value;
a43d18
 
a43d18
-    fputs(msg, stderr);
a43d18
+    fputs(msg, out);
a43d18
     for (i = (int)strlen(msg); i < 35; ++i)
a43d18
-        fputc(' ', stderr);
a43d18
-    fputc('=', stderr);
a43d18
+        fputc(' ', out);
a43d18
+    fputc('=', out);
a43d18
 
a43d18
     /* Write the value with commas. */
a43d18
     i = 22;
a43d18
@@ -1725,17 +1723,32 @@ printone(const char* msg, size_t value)
a43d18
 
a43d18
     while (i >= 0)
a43d18
         buf[i--] = ' ';
a43d18
-    fputs(buf, stderr);
a43d18
+    fputs(buf, out);
a43d18
 
a43d18
     return origvalue;
a43d18
 }
a43d18
 
a43d18
-/* Print summary info to stderr about the state of pymalloc's structures.
a43d18
+void
a43d18
+_PyDebugAllocatorStats(FILE *out,
a43d18
+                       const char *block_name, int num_blocks, size_t sizeof_block)
a43d18
+{
a43d18
+       char buf1[128];
a43d18
+       char buf2[128];
a43d18
+       PyOS_snprintf(buf1, sizeof(buf1),
a43d18
+                     "%d %ss * %zd bytes each",
a43d18
+                     num_blocks, block_name, sizeof_block);
a43d18
+       PyOS_snprintf(buf2, sizeof(buf2),
a43d18
+                     "%48s ", buf1);
a43d18
+      (void)printone(out, buf2, num_blocks * sizeof_block);
a43d18
+}
a43d18
+
a43d18
+
a43d18
+/* Print summary info to "out" about the state of pymalloc's structures.
a43d18
  * In Py_DEBUG mode, also perform some expensive internal consistency
a43d18
  * checks.
a43d18
  */
a43d18
 void
a43d18
-_PyObject_DebugMallocStats(void)
a43d18
+_PyObject_DebugMallocStats(FILE *out)
a43d18
 {
a43d18
     uint i;
a43d18
     const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT;
a43d18
@@ -1764,7 +1777,7 @@ _PyObject_DebugMallocStats(void)
a43d18
     size_t total;
a43d18
     char buf[128];
a43d18
 
a43d18
-    fprintf(stderr, "Small block threshold = %d, in %u size classes.\n",
a43d18
+    fprintf(out, "Small block threshold = %d, in %u size classes.\n",
a43d18
             SMALL_REQUEST_THRESHOLD, numclasses);
a43d18
 
a43d18
     for (i = 0; i < numclasses; ++i)
a43d18
@@ -1818,10 +1831,10 @@ _PyObject_DebugMallocStats(void)
a43d18
     }
a43d18
     assert(narenas == narenas_currently_allocated);
a43d18
 
a43d18
-    fputc('\n', stderr);
a43d18
+    fputc('\n', out);
a43d18
     fputs("class   size   num pools   blocks in use  avail blocks\n"
a43d18
           "-----   ----   ---------   -------------  ------------\n",
a43d18
-          stderr);
a43d18
+          out);
a43d18
 
a43d18
     for (i = 0; i < numclasses; ++i) {
a43d18
         size_t p = numpools[i];
a43d18
@@ -1832,7 +1845,7 @@ _PyObject_DebugMallocStats(void)
a43d18
             assert(b == 0 && f == 0);
a43d18
             continue;
a43d18
         }
a43d18
-        fprintf(stderr, "%5u %6u "
a43d18
+        fprintf(out, "%5u %6u "
a43d18
                         "%11" PY_FORMAT_SIZE_T "u "
a43d18
                         "%15" PY_FORMAT_SIZE_T "u "
a43d18
                         "%13" PY_FORMAT_SIZE_T "u\n",
a43d18
@@ -1842,36 +1855,35 @@ _PyObject_DebugMallocStats(void)
a43d18
         pool_header_bytes += p * POOL_OVERHEAD;
a43d18
         quantization += p * ((POOL_SIZE - POOL_OVERHEAD) % size);
a43d18
     }
a43d18
-    fputc('\n', stderr);
a43d18
-    (void)printone("# times object malloc called", serialno);
a43d18
-
a43d18
-    (void)printone("# arenas allocated total", ntimes_arena_allocated);
a43d18
-    (void)printone("# arenas reclaimed", ntimes_arena_allocated - narenas);
a43d18
-    (void)printone("# arenas highwater mark", narenas_highwater);
a43d18
-    (void)printone("# arenas allocated current", narenas);
a43d18
+    fputc('\n', out);
a43d18
+#ifdef PYMALLOC_DEBUG
a43d18
+    (void)printone(out, "# times object malloc called", serialno);
a43d18
+#endif
a43d18
+    (void)printone(out, "# arenas allocated total", ntimes_arena_allocated);
a43d18
+    (void)printone(out, "# arenas reclaimed", ntimes_arena_allocated - narenas);
a43d18
+    (void)printone(out, "# arenas highwater mark", narenas_highwater);
a43d18
+    (void)printone(out, "# arenas allocated current", narenas);
a43d18
 
a43d18
     PyOS_snprintf(buf, sizeof(buf),
a43d18
         "%" PY_FORMAT_SIZE_T "u arenas * %d bytes/arena",
a43d18
         narenas, ARENA_SIZE);
a43d18
-    (void)printone(buf, narenas * ARENA_SIZE);
a43d18
+    (void)printone(out, buf, narenas * ARENA_SIZE);
a43d18
 
a43d18
-    fputc('\n', stderr);
a43d18
+    fputc('\n', out);
a43d18
 
a43d18
-    total = printone("# bytes in allocated blocks", allocated_bytes);
a43d18
-    total += printone("# bytes in available blocks", available_bytes);
a43d18
+    total = printone(out, "# bytes in allocated blocks", allocated_bytes);
a43d18
+    total += printone(out, "# bytes in available blocks", available_bytes);
a43d18
 
a43d18
     PyOS_snprintf(buf, sizeof(buf),
a43d18
         "%u unused pools * %d bytes", numfreepools, POOL_SIZE);
a43d18
-    total += printone(buf, (size_t)numfreepools * POOL_SIZE);
a43d18
+    total += printone(out, buf, (size_t)numfreepools * POOL_SIZE);
a43d18
 
a43d18
-    total += printone("# bytes lost to pool headers", pool_header_bytes);
a43d18
-    total += printone("# bytes lost to quantization", quantization);
a43d18
-    total += printone("# bytes lost to arena alignment", arena_alignment);
a43d18
-    (void)printone("Total", total);
a43d18
+    total += printone(out, "# bytes lost to pool headers", pool_header_bytes);
a43d18
+    total += printone(out, "# bytes lost to quantization", quantization);
a43d18
+    total += printone(out, "# bytes lost to arena alignment", arena_alignment);
a43d18
+    (void)printone(out, "Total", total);
a43d18
 }
a43d18
 
a43d18
-#endif  /* PYMALLOC_DEBUG */
a43d18
-
a43d18
 #ifdef Py_USING_MEMORY_DEBUGGER
a43d18
 /* Make this function last so gcc won't inline it since the definition is
a43d18
  * after the reference.
a43d18
diff -up Python-2.7.2/Objects/setobject.c.add-debug-malloc-stats Python-2.7.2/Objects/setobject.c
044ee6
--- Python-2.7.2/Objects/setobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/setobject.c	2011-09-16 19:03:25.115821625 -0400
a43d18
@@ -1088,6 +1088,16 @@ PySet_Fini(void)
a43d18
     Py_CLEAR(emptyfrozenset);
a43d18
 }
a43d18
 
a43d18
+/* Print summary info about the state of the optimized allocator */
a43d18
+void
a43d18
+_PySet_DebugMallocStats(FILE *out)
a43d18
+{
a43d18
+    _PyDebugAllocatorStats(out,
a43d18
+                           "free PySetObject",
a43d18
+                           numfree, sizeof(PySetObject));
a43d18
+}
a43d18
+
a43d18
+
a43d18
 static PyObject *
a43d18
 set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
a43d18
 {
a43d18
diff -up Python-2.7.2/Objects/stringobject.c.add-debug-malloc-stats Python-2.7.2/Objects/stringobject.c
a43d18
--- Python-2.7.2/Objects/stringobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/stringobject.c	2011-09-16 19:03:25.116821625 -0400
a43d18
@@ -4822,3 +4822,43 @@ void _Py_ReleaseInternedStrings(void)
a43d18
     Py_DECREF(interned);
a43d18
     interned = NULL;
a43d18
 }
a43d18
+
a43d18
+void _PyString_DebugMallocStats(FILE *out)
a43d18
+{
a43d18
+    ssize_t i;
a43d18
+    int num_immortal = 0, num_mortal = 0;
a43d18
+    ssize_t immortal_size = 0, mortal_size = 0;
a43d18
+
a43d18
+    if (interned == NULL || !PyDict_Check(interned))
a43d18
+        return;
a43d18
+
a43d18
+    for (i = 0; i <= ((PyDictObject*)interned)->ma_mask; i++) {
a43d18
+        PyDictEntry *ep = ((PyDictObject*)interned)->ma_table + i;
a43d18
+        PyObject *pvalue = ep->me_value;
a43d18
+        if (pvalue != NULL) {
a43d18
+            PyStringObject *s = (PyStringObject *)ep->me_key;
a43d18
+
a43d18
+            switch (s->ob_sstate) {
a43d18
+            case SSTATE_NOT_INTERNED:
a43d18
+                /* XXX Shouldn't happen */
a43d18
+                break;
a43d18
+            case SSTATE_INTERNED_IMMORTAL:
a43d18
+                num_immortal ++;
a43d18
+                immortal_size += s->ob_size;
a43d18
+                break;
a43d18
+            case SSTATE_INTERNED_MORTAL:
a43d18
+                num_mortal ++;
a43d18
+                mortal_size += s->ob_size;
a43d18
+                break;
a43d18
+            default:
a43d18
+                Py_FatalError("Inconsistent interned string state.");
a43d18
+            }
a43d18
+        }
a43d18
+    }
a43d18
+
a43d18
+    fprintf(out, "%d mortal interned strings\n", num_mortal);
a43d18
+    fprintf(out, "%d immortal interned strings\n", num_immortal);
a43d18
+    fprintf(out, "total size of all interned strings: "
a43d18
+            "%zi/%zi "
a43d18
+            "mortal/immortal\n", mortal_size, immortal_size);
a43d18
+}
a43d18
diff -up Python-2.7.2/Objects/tupleobject.c.add-debug-malloc-stats Python-2.7.2/Objects/tupleobject.c
044ee6
--- Python-2.7.2/Objects/tupleobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/tupleobject.c	2011-09-16 19:03:25.116821625 -0400
a43d18
@@ -44,6 +44,22 @@ show_track(void)
a43d18
 }
a43d18
 #endif
a43d18
 
a43d18
+/* Print summary info about the state of the optimized allocator */
a43d18
+void
a43d18
+_PyTuple_DebugMallocStats(FILE *out)
a43d18
+{
a43d18
+#if PyTuple_MAXSAVESIZE > 0
a43d18
+    int i;
a43d18
+    char buf[128];
a43d18
+    for (i = 1; i < PyTuple_MAXSAVESIZE; i++) {
a43d18
+        PyOS_snprintf(buf, sizeof(buf),
a43d18
+                      "free %d-sized PyTupleObject", i);
a43d18
+        _PyDebugAllocatorStats(out,
a43d18
+                               buf,
a43d18
+                               numfree[i], _PyObject_VAR_SIZE(&PyTuple_Type, i));
a43d18
+    }
a43d18
+#endif
a43d18
+}
a43d18
 
a43d18
 PyObject *
a43d18
 PyTuple_New(register Py_ssize_t size)
a43d18
diff -up Python-2.7.2/Objects/unicodeobject.c.add-debug-malloc-stats Python-2.7.2/Objects/unicodeobject.c
a43d18
--- Python-2.7.2/Objects/unicodeobject.c.add-debug-malloc-stats	2011-06-11 11:46:27.000000000 -0400
044ee6
+++ Python-2.7.2/Objects/unicodeobject.c	2011-09-16 19:03:25.118821625 -0400
a43d18
@@ -8883,6 +8883,12 @@ _PyUnicode_Fini(void)
a43d18
     (void)PyUnicode_ClearFreeList();
a43d18
 }
a43d18
 
a43d18
+void _PyUnicode_DebugMallocStats(FILE *out)
a43d18
+{
a43d18
+    _PyDebugAllocatorStats(out, "free PyUnicodeObject", numfree,
a43d18
+                           sizeof(PyUnicodeObject));
a43d18
+}
a43d18
+
a43d18
 #ifdef __cplusplus
a43d18
 }
a43d18
 #endif
044ee6
diff -up Python-2.7.2/Python/pythonrun.c.add-debug-malloc-stats Python-2.7.2/Python/pythonrun.c
044ee6
--- Python-2.7.2/Python/pythonrun.c.add-debug-malloc-stats	2011-09-16 19:03:25.025821626 -0400
044ee6
+++ Python-2.7.2/Python/pythonrun.c	2011-09-16 19:03:25.118821625 -0400
044ee6
@@ -549,7 +549,7 @@ Py_Finalize(void)
044ee6
 #endif /* Py_TRACE_REFS */
044ee6
 #ifdef PYMALLOC_DEBUG
044ee6
     if (Py_GETENV("PYTHONMALLOCSTATS"))
044ee6
-        _PyObject_DebugMallocStats();
044ee6
+        _PyObject_DebugMallocStats(stderr);
044ee6
 #endif
044ee6
 
044ee6
     call_ll_exitfuncs();
a43d18
diff -up Python-2.7.2/Python/sysmodule.c.add-debug-malloc-stats Python-2.7.2/Python/sysmodule.c
044ee6
--- Python-2.7.2/Python/sysmodule.c.add-debug-malloc-stats	2011-09-16 19:03:25.007821626 -0400
044ee6
+++ Python-2.7.2/Python/sysmodule.c	2011-09-16 19:03:25.119821625 -0400
a43d18
@@ -872,6 +872,57 @@ a 11-tuple where the entries in the tupl
a43d18
 extern "C" {
a43d18
 #endif
a43d18
 
a43d18
+static PyObject *
a43d18
+sys_debugmallocstats(PyObject *self, PyObject *args)
a43d18
+{
a43d18
+    PyObject *file = NULL;
a43d18
+    FILE *fp;
a43d18
+
a43d18
+    if (!PyArg_ParseTuple(args, "|O!",
a43d18
+                          &PyFile_Type, &file)) {
a43d18
+      return NULL;
a43d18
+    }
a43d18
+    if (!file) {
a43d18
+        /* Default to sys.stderr: */
a43d18
+      file = PySys_GetObject("stderr");
a43d18
+      if (!file) {
a43d18
+          PyErr_SetString(PyExc_ValueError, "sys.stderr not set");
a43d18
+          return NULL;
a43d18
+      }
a43d18
+      if (!PyFile_Check(file)) {
a43d18
+          PyErr_SetString(PyExc_TypeError, "sys.stderr is not a file");
a43d18
+          return NULL;
a43d18
+      }
a43d18
+    }
a43d18
+
a43d18
+    Py_INCREF(file);
a43d18
+    /* OK, we now own a ref on non-NULL "file" */
a43d18
+
a43d18
+    fp = PyFile_AsFile(file);
a43d18
+    if (!fp) {
a43d18
+        PyErr_SetString(PyExc_ValueError, "file is closed");
a43d18
+        Py_DECREF(file);
a43d18
+        return NULL;	
a43d18
+    }
a43d18
+
a43d18
+    _PyObject_DebugMallocStats(fp);
a43d18
+    fputc('\n', fp);
a43d18
+    _PyObject_DebugTypeStats(fp);
a43d18
+
a43d18
+    Py_DECREF(file);
a43d18
+
a43d18
+    Py_RETURN_NONE;
a43d18
+}
a43d18
+PyDoc_STRVAR(debugmallocstats_doc,
a43d18
+"_debugmallocstats([file])\n\
a43d18
+\n\
a43d18
+Print summary info to the given file (or sys.stderr) about the state of\n\
a43d18
+pymalloc's structures.\n\
a43d18
+\n\
a43d18
+In Py_DEBUG mode, also perform some expensive internal consistency\n\
a43d18
+checks.\n\
a43d18
+");
a43d18
+
a43d18
 #ifdef Py_TRACE_REFS
a43d18
 /* Defined in objects.c because it uses static globals if that file */
a43d18
 extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
a43d18
@@ -970,6 +1021,8 @@ static PyMethodDef sys_methods[] = {
a43d18
     {"settrace",        sys_settrace, METH_O, settrace_doc},
a43d18
     {"gettrace",        sys_gettrace, METH_NOARGS, gettrace_doc},
a43d18
     {"call_tracing", sys_call_tracing, METH_VARARGS, call_tracing_doc},
a43d18
+    {"_debugmallocstats", sys_debugmallocstats, METH_VARARGS,
a43d18
+     debugmallocstats_doc},
a43d18
     {NULL,              NULL}           /* sentinel */
a43d18
 };
a43d18