[Zodb-checkins] SVN: ZODB/branches/jim-zeo-blob/src/ZODB/Blobs/tests/consume.txt consumeFile no-longer works with open files.

Jim Fulton jim at zope.com
Fri May 18 11:42:03 EDT 2007


Log message for revision 75831:
  consumeFile no-longer works with open files. 
  
  It never worked with NamedTemporaryFiles on Windows.
  

Changed:
  U   ZODB/branches/jim-zeo-blob/src/ZODB/Blobs/tests/consume.txt

-=-
Modified: ZODB/branches/jim-zeo-blob/src/ZODB/Blobs/tests/consume.txt
===================================================================
--- ZODB/branches/jim-zeo-blob/src/ZODB/Blobs/tests/consume.txt	2007-05-18 15:22:39 UTC (rev 75830)
+++ ZODB/branches/jim-zeo-blob/src/ZODB/Blobs/tests/consume.txt	2007-05-18 15:42:03 UTC (rev 75831)
@@ -6,20 +6,28 @@
 
 Let's create a file::
 
-    >>> import tempfile
-    >>> to_import = tempfile.NamedTemporaryFile()
+    >>> to_import = open('to_import', 'wb')
     >>> to_import.write("I'm a Blob and I feel fine.")
-    >>> to_import.flush()
 
+The file *must* be closed before giving it to consumeFile:
+
+    >>> to_import.close()
+
 Now, let's consume this file in a blob by specifying it's name::
 
     >>> from ZODB.Blobs.Blob import Blob
     >>> blob = Blob()
-    >>> blob.consumeFile(to_import.name)
+    >>> blob.consumeFile('to_import')
 
+After the consumeFile operation, the original file has been removed:
+
+    >>> import os
+    >>> os.path.exists('to_import')
+    False
+
 We now can call open on the blob and read and write the data::
 
-    >>> blob_read = blob.open('rb')
+    >>> blob_read = blob.open('r')
     >>> blob_read.read()
     "I'm a Blob and I feel fine."
     >>> blob_read.close()
@@ -27,41 +35,24 @@
     >>> blob_write.write('I was changed.')
     >>> blob_write.close()
 
-Please note that the interface for the `consume` method specifies a hard-link
-as a part of the contract so your existing file and the blob file will be the
-same. If one gets changed the other will reflect those changes as well. This
-is especially a known side-effect when consuming a file and then opening the
-blob for writing before committing in between::
-
-    >>> to_import.seek(0)
-    >>> to_import.read()
-    'I was changed.'
-
-(Applications are expected that files for consumption are typically copies of
-existing data and that the imported link to the file will be removed after a
-successfull import. This can be achieved (as in this test) by using a
-NamedTempFile.)
-
 We can not consume a file when there is a reader or writer around for a blob
 already::
 
-    >>> to_import2 = tempfile.NamedTemporaryFile()
-    >>> to_import2.write('I am another blob.')
-    >>> to_import2.flush()
+    >>> open('to_import', 'wb').write('I am another blob.')
     >>> blob_read = blob.open('r')
-    >>> blob.consumeFile(to_import2.name)
+    >>> blob.consumeFile('to_import')
     Traceback (most recent call last):
     BlobError: Already opened for reading.
     >>> blob_read.close()
     >>> blob_write = blob.open('w')
-    >>> blob.consumeFile(to_import2.name)
+    >>> blob.consumeFile('to_import')
     Traceback (most recent call last):
     BlobError: Already opened for writing.
     >>> blob_write.close()
 
 Now, after closing all readers and writers we can consume files again::
 
-    >>> blob.consumeFile(to_import2.name)
+    >>> blob.consumeFile('to_import')
     >>> blob_read = blob.open('r')
     >>> blob_read.read()
     'I am another blob.'
@@ -70,14 +61,13 @@
 Edge cases
 ==========
 
-There are some edge cases what happens when the link() operation fails. We simulate this in different states:
+There are some edge cases what happens when the link() operation
+fails. We simulate this in different states:
 
 Case 1: We don't have uncommitted data, but the link operation fails. The
 exception will be re-raised and the target file will not exist::
 
-    >>> input = tempfile.NamedTemporaryFile()
-    >>> input.write('Some data')
-    >>> input.flush()
+    >>> open('to_import', 'wb').write('Some data.')
 
     >>> def failing_link(self, filename):
     ...   raise Exception("I can't link.")
@@ -88,7 +78,7 @@
     BlobError: Blob does not exist.
 
     >>> blob._os_link = failing_link
-    >>> blob.consumeFile(input.name)
+    >>> blob.consumeFile('to_import')
     Traceback (most recent call last):
     Exception: I can't link.
 
@@ -102,18 +92,13 @@
 exception will be re-raised and the target file will exist with the previous
 uncomitted data::
 
-
-    >>> input = tempfile.NamedTemporaryFile()
-    >>> input.write('Unimported data')
-    >>> input.flush()
-
     >>> blob = Blob()
     >>> blob_writing = blob.open('w')
     >>> blob_writing.write('Uncommitted data')
     >>> blob_writing.close()
 
     >>> blob._os_link = failing_link
-    >>> blob.consumeFile(input.name)
+    >>> blob.consumeFile('to_import')
     Traceback (most recent call last):
     Exception: I can't link.
 



More information about the Zodb-checkins mailing list