[Zodb-checkins] SVN: ZODB/trunk/src/ZODB/Blobs/ Only support the documented modes, 'r', 'w', 'a', and 'r+' and open

Jim Fulton jim at zope.com
Fri May 18 14:02:18 EDT 2007


Log message for revision 75842:
  Only support the documented modes, 'r', 'w', 'a', and 'r+' and open
  all OS files in binary mode.  Binary mode for blobs is now implicit.
  

Changed:
  U   ZODB/trunk/src/ZODB/Blobs/Blob.py
  U   ZODB/trunk/src/ZODB/Blobs/tests/basic.txt
  U   ZODB/trunk/src/ZODB/Blobs/tests/transaction.txt

-=-
Modified: ZODB/trunk/src/ZODB/Blobs/Blob.py
===================================================================
--- ZODB/trunk/src/ZODB/Blobs/Blob.py	2007-05-18 18:02:14 UTC (rev 75841)
+++ ZODB/trunk/src/ZODB/Blobs/Blob.py	2007-05-18 18:02:17 UTC (rev 75842)
@@ -31,23 +31,16 @@
 import transaction.interfaces
 from persistent import Persistent
 
-if sys.platform == 'win32':
-    import win32file
-
 BLOB_SUFFIX = ".blob"
 
+valid_modes = 'r', 'w', 'r+', 'a'
 
 class Blob(Persistent):
     """A BLOB supports efficient handling of large data within ZODB."""
 
     zope.interface.implements(IBlob)
 
-    # Binding this to an attribute allows overriding it in the unit tests
-    if sys.platform == 'win32':
-        _os_link = lambda self, src, dst: win32file.CreateHardLink(dst, src,
-                                                                   None)
-    else:
-        _os_link = os.link
+    _os_link = os.rename
 
     _p_blob_readers = 0
     _p_blob_writers = 0
@@ -68,8 +61,11 @@
     def open(self, mode="r"):
         """Returns a file(-like) object representing blob data."""
         result = None
+            
+        if mode not in valid_modes:
+            raise ValueError("invalid mode", mode)
 
-        if (mode.startswith("r") or mode=="U"):
+        if mode == 'r':
             if self._current_filename() is None:
                 raise BlobError("Blob does not exist.")
 
@@ -79,7 +75,7 @@
             self._p_blob_readers += 1
             result = BlobFile(self._current_filename(), mode, self)
 
-        elif mode.startswith("w"):
+        elif mode == 'w':
             if self._p_blob_readers != 0:
                 raise BlobError("Already opened for reading.")
 
@@ -88,7 +84,7 @@
                 self._create_uncommitted_file()
             result = BlobFile(self._p_blob_uncommitted, mode, self)
 
-        elif mode.startswith("a"):
+        elif mode in ('a', 'r+'):
             if self._p_blob_readers != 0:
                 raise BlobError("Already opened for reading.")
 
@@ -233,12 +229,11 @@
         self._p_blob_writers = 0
 
     def _p_blob_decref(self, mode):
-        if mode.startswith('r') or mode == 'U':
+        if mode == 'r':
             self._p_blob_readers = max(0, self._p_blob_readers - 1)
-        elif mode.startswith('w') or mode.startswith('a'):
-            self._p_blob_writers = max(0, self._p_blob_writers - 1)
         else:
-            raise AssertionError('Unknown mode %s' % mode)
+            assert mode in valid_modes, "Invalid mode %r" % mode
+            self._p_blob_writers = max(0, self._p_blob_writers - 1)
 
     def _p_blob_refcounts(self):
         # used by unit tests
@@ -345,7 +340,7 @@
     # the storage later puts them to avoid copying them ...
 
     def __init__(self, name, mode, blob):
-        super(BlobFile, self).__init__(name, mode)
+        super(BlobFile, self).__init__(name, mode+'b')
         self.blob = blob
         self.close_called = False
 
@@ -364,7 +359,7 @@
     def close(self):
         # we don't want to decref twice
         if not self.close_called:
-            self.blob._p_blob_decref(self.mode)
+            self.blob._p_blob_decref(self.mode[:-1])
             self.close_called = True
             super(BlobFile, self).close()
 

Modified: ZODB/trunk/src/ZODB/Blobs/tests/basic.txt
===================================================================
--- ZODB/trunk/src/ZODB/Blobs/tests/basic.txt	2007-05-18 18:02:14 UTC (rev 75841)
+++ ZODB/trunk/src/ZODB/Blobs/tests/basic.txt	2007-05-18 18:02:17 UTC (rev 75842)
@@ -138,9 +138,9 @@
     ''
     >>> f8.close()
 
-We can explicitly open Blobs in the different modified modes:
+Blobs are always opened in binary mode:
 
-    >>> f9 = myblob.open("rb")
+    >>> f9 = myblob.open("r")
     >>> f9.mode
     'rb'
     >>> f9.close()

Modified: ZODB/trunk/src/ZODB/Blobs/tests/transaction.txt
===================================================================
--- ZODB/trunk/src/ZODB/Blobs/tests/transaction.txt	2007-05-18 18:02:14 UTC (rev 75841)
+++ ZODB/trunk/src/ZODB/Blobs/tests/transaction.txt	2007-05-18 18:02:17 UTC (rev 75842)
@@ -206,20 +206,20 @@
     >>> connection5 = database.open()
     >>> root5 = connection5.root()
     >>> blob = Blob()
-    >>> blob_fh = blob.open("wb")
+    >>> blob_fh = blob.open("w")
     >>> blob_fh.write("I'm a happy blob.")
     >>> blob_fh.close()
     >>> root5['blob'] = blob
     >>> transaction.commit()
-    >>> root5['blob'].open("rb").read()
+    >>> root5['blob'].open("r").read()
     "I'm a happy blob."
     >>> blob_fh = root5['blob'].open("a")
     >>> blob_fh.write(" And I'm singing.")
     >>> blob_fh.close()
-    >>> root5['blob'].open("rb").read()
+    >>> root5['blob'].open("r").read()
     "I'm a happy blob. And I'm singing."
     >>> savepoint = transaction.savepoint(optimistic=True)
-    >>> root5['blob'].open("rb").read()
+    >>> root5['blob'].open("r").read()
     "I'm a happy blob. And I'm singing."
     >>> transaction.get().commit()
 
@@ -228,7 +228,7 @@
     >>> blob_fh = root5['blob'].open("a")
     >>> blob_fh.write(" And the weather is beautiful.")
     >>> blob_fh.close()
-    >>> root5['blob'].open("rb").read()
+    >>> root5['blob'].open("r").read()
     "I'm a happy blob. And I'm singing. And the weather is beautiful."
     >>> savepoint = transaction.savepoint()             # doctest: +ELLIPSIS
     Traceback (most recent call last):
@@ -245,7 +245,7 @@
     >>> connection6 = database.open()
     >>> root6 = connection6.root()
     >>> blob = Blob()
-    >>> blob_fh = blob.open("wb")
+    >>> blob_fh = blob.open("w")
     >>> blob_fh.write("I'm a happy blob.")
     >>> blob_fh.close()
     >>> root6['blob'] = blob
@@ -264,7 +264,7 @@
 nor when the Blob is already opened for writing::
 
     >>> blob = Blob()
-    >>> blob_fh = blob.open("wb")
+    >>> blob_fh = blob.open("w")
     >>> blob.openDetached()
     Traceback (most recent call last):
         ...
@@ -290,13 +290,13 @@
 It does work when the transaction was aborted, though::
 
     >>> blob = Blob()
-    >>> blob_fh = blob.open("wb")
+    >>> blob_fh = blob.open("w")
     >>> blob_fh.write("I'm a happy blob.")
     >>> blob_fh.close()
     >>> root6['blob'] = blob
     >>> transaction.commit()
 
-    >>> blob_fh = blob.open("wb")
+    >>> blob_fh = blob.open("w")
     >>> blob_fh.write("And I'm singing.")
     >>> blob_fh.close()
     >>> transaction.abort()



More information about the Zodb-checkins mailing list