[Zodb-checkins] CVS: StandaloneZODB/ZEO/zrpc - log.py:1.1.2.2

Jeremy Hylton jeremy@zope.com
Wed, 16 Jan 2002 20:34:48 -0500


Update of /cvs-repository/StandaloneZODB/ZEO/zrpc
In directory cvs.zope.org:/tmp/cvs-serv12012/zrpc

Modified Files:
      Tag: Standby-branch
	log.py 
Log Message:
Add a complicated short_repr() function.

This makes the same sort of change that was made late in the beta
release for ZEO 1.0.  We were seeing memory problems exacerbated by
the cost of building reprs of large strings and tuples that were going
to be truncated anyway.


=== StandaloneZODB/ZEO/zrpc/log.py 1.1.2.1 => 1.1.2.2 ===
+import types
 import zLOG
 
 _label = "zrpc:%s" % os.getpid()
@@ -9,3 +10,25 @@
 
 def log(message, level=zLOG.BLATHER, label=None, error=None):
     zLOG.LOG(label or _label, level, message, error=error)
+
+REPR_LIMIT = 40
+
+def short_repr(obj):
+    "Return an object repr limited to REPR_LIMIT bytes."
+    # Some of the objects being repr'd are large strings.  It's wastes
+    # a lot of memory to repr them and then truncate, so special case
+    # them in this function.
+    # Also handle short repr of a tuple containing a long string.
+    if isinstance(obj, types.StringType):
+        obj = obj[:REPR_LIMIT]
+    elif isinstance(obj, types.TupleType):
+        elts = []
+        size = 0
+        for elt in obj:
+            r = repr(elt)
+            elts.append(r)
+            size += len(r)
+            if size > REPR_LIMIT:
+                break
+        obj = tuple(elts)
+    return repr(obj)[:REPR_LIMIT]