[Zodb-checkins] CVS: ZODB3/ZEO/zrpc - log.py:1.5

Jeremy Hylton jeremy@zope.com
Mon, 16 Sep 2002 23:40:34 -0400


Update of /cvs-repository/ZODB3/ZEO/zrpc
In directory cvs.zope.org:/tmp/cvs-serv3173

Modified Files:
	log.py 
Log Message:
Revert to old short_repr() and fix its double-repr problem.

It was calling repr() twice on the elements of a tuple.  Fix by
calling repr() explicitly on each branch of the type-test code.

Add comment about how short_repr() is used.


=== ZODB3/ZEO/zrpc/log.py 1.4 => 1.5 ===
--- ZODB3/ZEO/zrpc/log.py:1.4	Fri Sep 13 16:53:20 2002
+++ ZODB3/ZEO/zrpc/log.py	Mon Sep 16 23:40:34 2002
@@ -24,5 +24,36 @@
 def log(message, level=zLOG.BLATHER, label=None, error=None):
     zLOG.LOG(label or _label, level, message, error=error)
 
-# There's a nice "short_repr" function in the repr module:
-from repr import repr as short_repr
+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.
+
+    # This strategy works well for arguments to StorageServer methods.
+    # The oid is usually first and will get included in its entirety.
+    # The pickle is near the beginning, too, and you can often fit the
+    # module name in the pickle.
+
+    if isinstance(obj, types.StringType):
+        r = `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
+        r = "(%s)" % (", ".join(elts))
+    else:
+        r = repr(obj)
+    if len(r) > REPR_LIMIT:
+        return r[:REPR_LIMIT] + '...'
+    else:
+        return r