[ZODB-Dev] Typechecking oid in getitem

Christian Reis kiko at async.com.br
Sun May 18 15:24:54 EDT 2003


The current error message when indexing a connection incorrectly is a
bit confusing (supporting someone on #zope suggested this):

    >>> c1[1]           # Duh, it should be a string
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "/usr/local/lib/python2.1/site-packages/ZODB/Connection.py", line
    161, in __getitem__
        p, serial = self._storage.load(oid, self._version)
      File "/usr/local/lib/python2.1/site-packages/ZEO/ClientStorage.py",
    line 697, in load
        p, s, v, pv, sv = self._server.zeoLoad(oid)
      File "/usr/local/lib/python2.1/site-packages/ZEO/ServerStub.py", line
    78, in zeoLoad
        return self.rpc.call('zeoLoad', oid)
      File "/usr/local/lib/python2.1/site-packages/ZEO/zrpc/connection.py",
    line 348, in call
        raise inst # error raised by server
    TypeError: unsliceable object

The following patch catches the exception higher up and produces a
reasonable error message. Note that it is hand-hacked so it needs to be
manually applied, but comments would be nice:

diff -u -r1.91 Connection.py
--- Connection.py       23 Apr 2003 20:36:02 -0000      1.91
+++ Connection.py       18 May 2003 17:10:26 -0000
@@ -142,6 +148,14 @@
         self._root_ = None

     def __getitem__(self, oid, tt=type(())):
+
+        if type(oid) is not StringType:
+            raise TypeError, "expected packed 8-byte oid string, got
%s" % oid
+            
         obj = self._cache.get(oid, None)
         if obj is not None:
             return obj


This gets us:

>>> c[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.1/site-packages/ZODB/Connection.py", line
157, in __getitem__
    raise TypeError, "expected packed 8-byte oid string, got %s" % oid
TypeError: expected packed 8-byte oid string, got 1

Much nicer!

Take care,
--
Christian Reis, Senior Engineer, Async Open Source, Brazil.
http://async.com.br/~kiko/ | [+55 16] 261 2331 | NMFL



More information about the ZODB-Dev mailing list