[Zodb-checkins] SVN: ZODB/trunk/src/ The Blob open method now supports a new mode, 'c', to open committed

Jim Fulton jim at zope.com
Mon Dec 1 15:07:24 EST 2008


Log message for revision 93518:
  The Blob open method now supports a new mode, 'c', to open committed
  data for reading as an ordinary file, rather than as a blob file.
  The ordinary file may be used outside the current transaction and
  even after the blob's database connection has been closed.
  

Changed:
  U   ZODB/trunk/src/CHANGES.txt
  U   ZODB/trunk/src/ZODB/blob.py
  U   ZODB/trunk/src/ZODB/interfaces.py
  U   ZODB/trunk/src/ZODB/tests/blob_transaction.txt

-=-
Modified: ZODB/trunk/src/CHANGES.txt
===================================================================
--- ZODB/trunk/src/CHANGES.txt	2008-12-01 18:44:35 UTC (rev 93517)
+++ ZODB/trunk/src/CHANGES.txt	2008-12-01 20:07:24 UTC (rev 93518)
@@ -22,12 +22,23 @@
   XXX There are known issues with this implementation that need to be
   sorted out before it is "released".
 
-3.9.0a6 (2008-11-??)
+3.9.0a6 (2008-12-??)
 ====================
 
 New Features
 ------------
 
+- The Blob open method now supports a new mode, 'c', to open committed
+  data for reading as an ordinary file, rather than as a blob file.
+  The ordinary file may be used outside the current transaction and
+  even after the blob's database connection has been closed.
+
+3.9.0a6 (2008-11-30)
+====================
+
+New Features
+------------
+
 - ZODB 3.9 ZEO clients can connect to ZODB 3.8 servers.
 
 Bug Fixes

Modified: ZODB/trunk/src/ZODB/blob.py
===================================================================
--- ZODB/trunk/src/ZODB/blob.py	2008-12-01 18:44:35 UTC (rev 93517)
+++ ZODB/trunk/src/ZODB/blob.py	2008-12-01 20:07:24 UTC (rev 93518)
@@ -45,7 +45,7 @@
 LAYOUT_MARKER = '.layout'
 LAYOUTS = {}
 
-valid_modes = 'r', 'w', 'r+', 'a'
+valid_modes = 'r', 'w', 'r+', 'a', 'c'
 
 # Threading issues:
 # We want to support closing blob files when they are destroyed.
@@ -119,6 +119,9 @@
         if mode not in valid_modes:
             raise ValueError("invalid mode", mode)
 
+        if mode == 'c':
+            return open(self.committed(), 'rb')
+
         if self.writers:
             raise BlobError("Already opened for writing.")
 

Modified: ZODB/trunk/src/ZODB/interfaces.py
===================================================================
--- ZODB/trunk/src/ZODB/interfaces.py	2008-12-01 18:44:35 UTC (rev 93517)
+++ ZODB/trunk/src/ZODB/interfaces.py	2008-12-01 20:07:24 UTC (rev 93518)
@@ -978,7 +978,12 @@
 
         Returns a file(-like) object for handling the blob data.
 
-        mode: Mode to open the file with. Possible values: r,w,r+,a
+        mode: Mode to open the file with. Possible values: r,w,r+,a,c
+
+        The mode 'c' is similar to 'r', except that an orinary file
+        object is returned and may be used in a separate transaction
+        and after the blob's database connection has been closed.
+        
         """
 
     def committed():

Modified: ZODB/trunk/src/ZODB/tests/blob_transaction.txt
===================================================================
--- ZODB/trunk/src/ZODB/tests/blob_transaction.txt	2008-12-01 18:44:35 UTC (rev 93517)
+++ ZODB/trunk/src/ZODB/tests/blob_transaction.txt	2008-12-01 20:07:24 UTC (rev 93518)
@@ -306,6 +306,27 @@
     >>> open(blob.committed()).read()
     "I'm a happy blob."
 
+We can also read committed data by calling open with a 'c' flag:
+
+    >>> f = blob.open('c')
+
+This just returns a regular file object:
+
+    >>> type(f)
+    <type 'file'>
+
+and doesn't prevent us from opening the blob for writing:
+
+    >>> blob.open('w').write('x')
+    >>> blob.open().read()
+    'x'
+
+    >>> f.read()
+    "I'm a happy blob."
+
+    >>> f.close()
+    >>> transaction.abort()
+
 An exception is raised if we call committed on a blob that has
 uncommitted changes:
 
@@ -315,6 +336,11 @@
     ...
     BlobError: Uncommitted changes
 
+    >>> blob.open('c')
+    Traceback (most recent call last):
+    ...
+    BlobError: Uncommitted changes
+
     >>> blob.open('w').write("I'm a happy blob.")
     >>> root6['blob6'] = blob
     >>> blob.committed()
@@ -322,12 +348,22 @@
     ...
     BlobError: Uncommitted changes
 
+    >>> blob.open('c')
+    Traceback (most recent call last):
+    ...
+    BlobError: Uncommitted changes
+
     >>> s = transaction.savepoint()
     >>> blob.committed()
     Traceback (most recent call last):
     ...
     BlobError: Uncommitted changes
 
+    >>> blob.open('c')
+    Traceback (most recent call last):
+    ...
+    BlobError: Uncommitted changes
+
     >>> transaction.commit()
     >>> open(blob.committed()).read()
     "I'm a happy blob."



More information about the Zodb-checkins mailing list