Commit c0b7812b authored by Tres Seaver's avatar Tres Seaver

Bend over backward to avoid ResourceWarnings.

parent 25ebff76
...@@ -27,7 +27,8 @@ Aborting a blob add leaves the blob unchanged: ...@@ -27,7 +27,8 @@ Aborting a blob add leaves the blob unchanged:
>>> blob1._p_oid >>> blob1._p_oid
>>> blob1._p_jar >>> blob1._p_jar
>>> with blob1.open() as fp: fp.read() >>> with blob1.open() as fp:
... fp.read()
'this is blob 1' 'this is blob 1'
It doesn't clear the file because there is no previously committed version: It doesn't clear the file because there is no previously committed version:
...@@ -51,7 +52,8 @@ state: ...@@ -51,7 +52,8 @@ state:
>>> with blob1.open('w') as file: >>> with blob1.open('w') as file:
... _ = file.write(b'this is new blob 1') ... _ = file.write(b'this is new blob 1')
>>> with blob1.open() as fp: fp.read() >>> with blob1.open() as fp:
... fp.read()
'this is new blob 1' 'this is new blob 1'
>>> fname = blob1._p_blob_uncommitted >>> fname = blob1._p_blob_uncommitted
>>> os.path.exists(fname) >>> os.path.exists(fname)
...@@ -62,7 +64,8 @@ state: ...@@ -62,7 +64,8 @@ state:
False False
>>> blob1._p_blob_uncommitted >>> blob1._p_blob_uncommitted
>>> with blob1.open() as fp: fp.read() >>> with blob1.open() as fp:
... fp.read()
'this is blob 1' 'this is blob 1'
Opening a blob gives us a filehandle. Getting data out of the Opening a blob gives us a filehandle. Getting data out of the
...@@ -103,12 +106,12 @@ when we start):: ...@@ -103,12 +106,12 @@ when we start)::
>>> bool(blob1a._p_changed) >>> bool(blob1a._p_changed)
False False
>>> with blob1a.open('r') as fp: fp.read() >>> with blob1a.open('r') as fp:
... fp.read()
'this is blob 1' 'this is blob 1'
>>> blob1afh3 = blob1a.open('a') >>> with blob1a.open('a') as blob1afh3:
>>> bool(blob1a._p_changed) ... assert(bool(blob1a._p_changed))
True ... _ = blob1afh3.write(b'woot!')
>>> _ = blob1afh3.write(b'woot!')
>>> blob1afh3.close() >>> blob1afh3.close()
We can open more than one blob object during the course of a single We can open more than one blob object during the course of a single
...@@ -124,11 +127,14 @@ Since we committed the current transaction above, the aggregate ...@@ -124,11 +127,14 @@ Since we committed the current transaction above, the aggregate
changes we've made to blob, blob1a (these refer to the same object) and changes we've made to blob, blob1a (these refer to the same object) and
blob2 (a different object) should be evident:: blob2 (a different object) should be evident::
>>> with blob1.open('r') as fp: fp.read() >>> with blob1.open('r') as fp:
... fp.read()
'this is blob 1woot!' 'this is blob 1woot!'
>>> with blob1a.open('r') as fp: fp.read() >>> with blob1a.open('r') as fp:
... fp.read()
'this is blob 1woot!' 'this is blob 1woot!'
>>> with blob2.open('r') as fp: fp.read() >>> with blob2.open('r') as fp:
... fp.read()
'this is blob 3' 'this is blob 3'
We shouldn't be able to persist a blob filehandle at commit time We shouldn't be able to persist a blob filehandle at commit time
...@@ -136,8 +142,9 @@ We shouldn't be able to persist a blob filehandle at commit time ...@@ -136,8 +142,9 @@ We shouldn't be able to persist a blob filehandle at commit time
pickled appears to be particulary unhelpful for casual users at the pickled appears to be particulary unhelpful for casual users at the
moment):: moment)::
>>> root1['wontwork'] = blob1.open('r') >>> with blob1.open('r') as f:
>>> transaction.commit() ... root1['wontwork'] = f
... transaction.commit()
Traceback (most recent call last): Traceback (most recent call last):
... ...
TypeError: ... TypeError: ...
...@@ -160,7 +167,8 @@ connections should result in a write conflict error:: ...@@ -160,7 +167,8 @@ connections should result in a write conflict error::
>>> with blob1c4.open('a') as blob1c4fh1: >>> with blob1c4.open('a') as blob1c4fh1:
... _ = blob1c4fh1.write(b'this is from connection 4') ... _ = blob1c4fh1.write(b'this is from connection 4')
>>> tm1.commit() >>> tm1.commit()
>>> with root3['blob1'].open('r') as fp: fp.read() >>> with root3['blob1'].open('r') as fp:
... fp.read()
'this is blob 1woot!this is from connection 3' 'this is blob 1woot!this is from connection 3'
>>> tm2.commit() >>> tm2.commit()
Traceback (most recent call last): Traceback (most recent call last):
...@@ -170,10 +178,12 @@ connections should result in a write conflict error:: ...@@ -170,10 +178,12 @@ connections should result in a write conflict error::
After the conflict, the winning transaction's result is visible on both After the conflict, the winning transaction's result is visible on both
connections:: connections::
>>> with root3['blob1'].open('r') as fp: fp.read() >>> with root3['blob1'].open('r') as fp:
... fp.read()
'this is blob 1woot!this is from connection 3' 'this is blob 1woot!this is from connection 3'
>>> tm2.abort() >>> tm2.abort()
>>> with root4['blob1'].open('r') as fp: fp.read() >>> with root4['blob1'].open('r') as fp:
... fp.read()
'this is blob 1woot!this is from connection 3' 'this is blob 1woot!this is from connection 3'
You can't commit a transaction while blob files are open: You can't commit a transaction while blob files are open:
...@@ -205,21 +215,22 @@ We do support optimistic savepoints: ...@@ -205,21 +215,22 @@ We do support optimistic savepoints:
>>> connection5 = database.open() >>> connection5 = database.open()
>>> root5 = connection5.root() >>> root5 = connection5.root()
>>> blob = ZODB.blob.Blob() >>> blob = ZODB.blob.Blob()
>>> blob_fh = blob.open("w") >>> with blob.open("w") as blob_fh:
>>> _ = blob_fh.write(b"I'm a happy blob.") ... _ = blob_fh.write(b"I'm a happy blob.")
>>> blob_fh.close()
>>> root5['blob'] = blob >>> root5['blob'] = blob
>>> transaction.commit() >>> transaction.commit()
>>> with root5['blob'].open("r") as fp: fp.read() >>> with root5['blob'].open("r") as fp:
... fp.read()
"I'm a happy blob." "I'm a happy blob."
>>> blob_fh = root5['blob'].open("a") >>> with root5['blob'].open("a") as blob_fh:
>>> _ = blob_fh.write(b" And I'm singing.") ... _ = blob_fh.write(b" And I'm singing.")
>>> blob_fh.close() >>> with root5['blob'].open("r") as fp:
>>> with root5['blob'].open("r") as fp: fp.read() ... fp.read()
"I'm a happy blob. And I'm singing." "I'm a happy blob. And I'm singing."
>>> savepoint = transaction.savepoint(optimistic=True) >>> savepoint = transaction.savepoint(optimistic=True)
>>> with root5['blob'].open("r") as fp: fp.read() >>> with root5['blob'].open("r") as fp:
... fp.read()
"I'm a happy blob. And I'm singing." "I'm a happy blob. And I'm singing."
Savepoints store the blobs in temporary directories in the temporary Savepoints store the blobs in temporary directories in the temporary
...@@ -241,7 +252,8 @@ We support non-optimistic savepoints too: ...@@ -241,7 +252,8 @@ We support non-optimistic savepoints too:
>>> with root5['blob'].open("a") as file: >>> with root5['blob'].open("a") as file:
... _ = file.write(b" And I'm dancing.") ... _ = file.write(b" And I'm dancing.")
>>> with root5['blob'].open("r") as fp: fp.read() >>> with root5['blob'].open("r") as fp:
... fp.read()
"I'm a happy blob. And I'm singing. And I'm dancing." "I'm a happy blob. And I'm singing. And I'm dancing."
>>> savepoint = transaction.savepoint() >>> savepoint = transaction.savepoint()
...@@ -255,7 +267,8 @@ Again, the savepoint creates a new savepoints directory: ...@@ -255,7 +267,8 @@ Again, the savepoint creates a new savepoints directory:
... _ = file.write(b" And the weather is beautiful.") ... _ = file.write(b" And the weather is beautiful.")
>>> savepoint.rollback() >>> savepoint.rollback()
>>> with root5['blob'].open("r") as fp: fp.read() >>> with root5['blob'].open("r") as fp:
... fp.read()
"I'm a happy blob. And I'm singing. And I'm dancing." "I'm a happy blob. And I'm singing. And I'm dancing."
>>> transaction.abort() >>> transaction.abort()
...@@ -275,12 +288,12 @@ file that can be opened. ...@@ -275,12 +288,12 @@ file that can be opened.
>>> connection6 = database.open() >>> connection6 = database.open()
>>> root6 = connection6.root() >>> root6 = connection6.root()
>>> blob = ZODB.blob.Blob() >>> blob = ZODB.blob.Blob()
>>> blob_fh = blob.open("w") >>> with blob.open("w") as blob_fh:
>>> _ = blob_fh.write(b"I'm a happy blob.") ... _ = blob_fh.write(b"I'm a happy blob.")
>>> blob_fh.close()
>>> root6['blob'] = blob >>> root6['blob'] = blob
>>> transaction.commit() >>> transaction.commit()
>>> with open(blob.committed()) as fp: fp.read() >>> with open(blob.committed()) as fp:
... fp.read()
"I'm a happy blob." "I'm a happy blob."
We can also read committed data by calling open with a 'c' flag: We can also read committed data by calling open with a 'c' flag:
...@@ -344,7 +357,8 @@ uncommitted changes: ...@@ -344,7 +357,8 @@ uncommitted changes:
BlobError: Uncommitted changes BlobError: Uncommitted changes
>>> transaction.commit() >>> transaction.commit()
>>> with open(blob.committed()) as fp: fp.read() >>> with open(blob.committed()) as fp:
... fp.read()
"I'm a happy blob." "I'm a happy blob."
You can't open a committed blob file for writing: You can't open a committed blob file for writing:
...@@ -397,7 +411,8 @@ And we shouldn't be able to read the data that we saved: ...@@ -397,7 +411,8 @@ And we shouldn't be able to read the data that we saved:
Of course the old data should be unaffected: Of course the old data should be unaffected:
>>> with open(blob_storage.loadBlob(blob._p_oid, oldserial)) as fp: fp.read() >>> with open(blob_storage.loadBlob(blob._p_oid, oldserial)) as fp:
... fp.read()
"I'm a happy blob." "I'm a happy blob."
Similarly, the new object wasn't added to the storage: Similarly, the new object wasn't added to the storage:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment