[Zope3-checkins] SVN: zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/ Moved reference count support to its own module.

Christian Theune ct at gocept.com
Sat May 3 12:04:28 EDT 2008


Log message for revision 86250:
  Moved reference count support to its own module.
  
  Cleared up imports in __init__.py
  

Changed:
  U   zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/__init__.py
  A   zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/refcount.py
  U   zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/runner.py

-=-
Modified: zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/__init__.py
===================================================================
--- zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/__init__.py	2008-05-03 15:59:42 UTC (rev 86249)
+++ zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/__init__.py	2008-05-03 16:04:27 UTC (rev 86250)
@@ -16,119 +16,13 @@
 $Id$
 """
 
-import gc
-import glob
-import logging
-import os
-import errno
-import pdb
-import re
-import cStringIO
 import sys
-import tempfile
-import threading
-import time
-import trace
-import traceback
-import types
 import unittest
 
 
-from zope.testing import doctest
-from zope.testing.testrunner.formatter import OutputFormatter, ColorfulOutputFormatter
-from zope.testing.testrunner.formatter import terminal_has_colors
-from zope.testing.testrunner.find import name_from_layer, _layer_name_cache
-from zope.testing.testrunner.runner import run, EndRun
+from zope.testing.testrunner.runner import run
 
 
-class TrackRefs(object):
-    """Object to track reference counts across test runs."""
-
-    def __init__(self):
-        self.type2count = {}
-        self.type2all = {}
-        self.delta = None
-        self.n = 0
-        self.update()
-        self.delta = None
-
-    def update(self):
-        gc.collect()
-        obs = sys.getobjects(0)
-        type2count = {}
-        type2all = {}
-        n = 0
-        for o in obs:
-            if type(o) is str and o == '<dummy key>':
-                # avoid dictionary madness
-                continue
-
-            all = sys.getrefcount(o) - 3
-            n += all
-
-            t = type(o)
-            if t is types.InstanceType:
-                t = o.__class__
-
-            if t in type2count:
-                type2count[t] += 1
-                type2all[t] += all
-            else:
-                type2count[t] = 1
-                type2all[t] = all
-
-
-        ct = [(
-               type_or_class_title(t),
-               type2count[t] - self.type2count.get(t, 0),
-               type2all[t] - self.type2all.get(t, 0),
-               )
-              for t in type2count.iterkeys()]
-        ct += [(
-                type_or_class_title(t),
-                - self.type2count[t],
-                - self.type2all[t],
-                )
-               for t in self.type2count.iterkeys()
-               if t not in type2count]
-        ct.sort()
-        self.delta = ct
-        self.type2count = type2count
-        self.type2all = type2all
-        self.n = n
-
-
-    def output(self):
-        printed = False
-        s1 = s2 = 0
-        for t, delta1, delta2 in self.delta:
-            if delta1 or delta2:
-                if not printed:
-                    print (
-                        '    Leak details, changes in instances and refcounts'
-                        ' by type/class:')
-                    print "    %-55s %6s %6s" % ('type/class', 'insts', 'refs')
-                    print "    %-55s %6s %6s" % ('-' * 55, '-----', '----')
-                    printed = True
-                print "    %-55s %6d %6d" % (t, delta1, delta2)
-                s1 += delta1
-                s2 += delta2
-
-        if printed:
-            print "    %-55s %6s %6s" % ('-' * 55, '-----', '----')
-            print "    %-55s %6s %6s" % ('total', s1, s2)
-
-
-        self.delta = None
-
-
-def type_or_class_title(t):
-    module = getattr(t, '__module__', '__builtin__')
-    if module == '__builtin__':
-        return t.__name__
-    return "%s.%s" % (module, t.__name__)
-
-
 ###############################################################################
 # Install 2.4 TestSuite __iter__ into earlier versions
 

Added: zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/refcount.py
===================================================================
--- zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/refcount.py	                        (rev 0)
+++ zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/refcount.py	2008-05-03 16:04:27 UTC (rev 86250)
@@ -0,0 +1,109 @@
+##############################################################################
+#
+# Copyright (c) 2004-2008 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Support for tracking reference counts.
+
+$Id: __init__.py 86246 2008-05-03 15:54:02Z ctheune $
+"""
+
+import gc
+import sys
+import types
+
+
+class TrackRefs(object):
+    """Object to track reference counts across test runs."""
+
+    def __init__(self):
+        self.type2count = {}
+        self.type2all = {}
+        self.delta = None
+        self.n = 0
+        self.update()
+        self.delta = None
+
+    def update(self):
+        gc.collect()
+        obs = sys.getobjects(0)
+        type2count = {}
+        type2all = {}
+        n = 0
+        for o in obs:
+            if type(o) is str and o == '<dummy key>':
+                # avoid dictionary madness
+                continue
+
+            all = sys.getrefcount(o) - 3
+            n += all
+
+            t = type(o)
+            if t is types.InstanceType:
+                t = o.__class__
+
+            if t in type2count:
+                type2count[t] += 1
+                type2all[t] += all
+            else:
+                type2count[t] = 1
+                type2all[t] = all
+
+
+        ct = [(
+               type_or_class_title(t),
+               type2count[t] - self.type2count.get(t, 0),
+               type2all[t] - self.type2all.get(t, 0),
+               )
+              for t in type2count.iterkeys()]
+        ct += [(
+                type_or_class_title(t),
+                - self.type2count[t],
+                - self.type2all[t],
+                )
+               for t in self.type2count.iterkeys()
+               if t not in type2count]
+        ct.sort()
+        self.delta = ct
+        self.type2count = type2count
+        self.type2all = type2all
+        self.n = n
+
+
+    def output(self):
+        printed = False
+        s1 = s2 = 0
+        for t, delta1, delta2 in self.delta:
+            if delta1 or delta2:
+                if not printed:
+                    print (
+                        '    Leak details, changes in instances and refcounts'
+                        ' by type/class:')
+                    print "    %-55s %6s %6s" % ('type/class', 'insts', 'refs')
+                    print "    %-55s %6s %6s" % ('-' * 55, '-----', '----')
+                    printed = True
+                print "    %-55s %6d %6d" % (t, delta1, delta2)
+                s1 += delta1
+                s2 += delta2
+
+        if printed:
+            print "    %-55s %6s %6s" % ('-' * 55, '-----', '----')
+            print "    %-55s %6s %6s" % ('total', s1, s2)
+
+
+        self.delta = None
+
+
+def type_or_class_title(t):
+    module = getattr(t, '__module__', '__builtin__')
+    if module == '__builtin__':
+        return t.__name__
+    return "%s.%s" % (module, t.__name__)


Property changes on: zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/refcount.py
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/runner.py
===================================================================
--- zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/runner.py	2008-05-03 15:59:42 UTC (rev 86249)
+++ zope.testing/branches/ctheune-cleanup/src/zope/testing/testrunner/runner.py	2008-05-03 16:04:27 UTC (rev 86250)
@@ -1,6 +1,6 @@
 ##############################################################################
 #
-# Copyright (c) 2004-2006 Zope Corporation and Contributors.
+# Copyright (c) 2004-2008 Zope Corporation and Contributors.
 # All Rights Reserved.
 #
 # This software is subject to the provisions of the Zope Public License,
@@ -35,6 +35,7 @@
 from zope.testing.testrunner.find import StartUpFailure, import_name
 from zope.testing.testrunner.find import name_from_layer, _layer_name_cache
 from zope.testing.testrunner.coverage import TestTrace
+from zope.testing.testrunner.refcount import TrackRefs
 from zope.testing.testrunner.options import get_options
 
 real_pdb_set_trace = pdb.set_trace
@@ -73,6 +74,7 @@
     sumrc = 0
     if options.report_refcounts:
         if options.verbose:
+            # XXX This code path is untested
             track = TrackRefs()
         rc = sys.gettotalrefcount()
 



More information about the Zope3-Checkins mailing list