Commit c9159338 authored by Marius Gedminas's avatar Marius Gedminas

Fix many warnings on Python 3

Many more remain.
parent 4b0ea80d
......@@ -962,9 +962,10 @@ class FileStorage(
# We're undoing a blob modification operation.
# We have to copy the blob data
tmp = ZODB.utils.mktemp(dir=self.fshelper.temp_dir)
with open(tmp, 'wb') as fp:
ZODB.utils.cp(
self.openCommittedBlobFile(h.oid, userial),
open(tmp, 'wb'))
fp)
self._blob_storeblob(h.oid, self._tid, tmp)
new = DataHeader(h.oid, self._tid, ipos, otloc, 0, len(p))
......@@ -1180,7 +1181,8 @@ class FileStorage(
handle_dir = ZODB.blob.remove_committed_dir
# Fist step: move or remove oids or revisions
for line in open(os.path.join(self.blob_dir, '.removed'), 'rb'):
fp = open(os.path.join(self.blob_dir, '.removed'), 'rb')
for line in fp:
line = binascii.unhexlify(line.strip())
if len(line) == 8:
......@@ -1194,6 +1196,7 @@ class FileStorage(
continue
if len(line) != 16:
fp.close()
raise ValueError("Bad record in ", self.blob_dir, '.removed')
oid, tid = line[:8], line[8:]
......@@ -1205,6 +1208,7 @@ class FileStorage(
assert not os.path.exists(path)
maybe_remove_empty_dir_containing(path)
fp.close()
os.remove(os.path.join(self.blob_dir, '.removed'))
if not self.pack_keep_old:
......
......@@ -347,6 +347,7 @@ class FileStoragePacker(FileStorageFormatter):
os.path.join(storage.blob_dir, '.removed'), 'wb')
else:
self.pack_blobs = False
self.blob_removed = None
path = storage._file.name
self._name = path
......@@ -412,6 +413,8 @@ class FileStoragePacker(FileStorageFormatter):
self._tfile.close()
self._file.close()
os.remove(self._name + ".pack")
if self.blob_removed is not None:
self.blob_removed.close()
return None
self._commit_lock_acquire()
self.locked = True
......@@ -446,6 +449,8 @@ class FileStoragePacker(FileStorageFormatter):
self._tfile.flush()
self._tfile.close()
self._file.close()
if self.blob_removed is not None:
self.blob_removed.close()
return pos
except:
......
......@@ -178,7 +178,8 @@ class Blob(persistent.Persistent):
self._create_uncommitted_file()
result = BlobFile(self._p_blob_uncommitted, mode, self)
if self._p_blob_committed:
utils.cp(open(self._p_blob_committed, 'rb'), result)
with open(self._p_blob_committed, 'rb') as fp:
utils.cp(fp, result)
if mode == 'r+':
result.seek(0)
else:
......@@ -356,7 +357,7 @@ class FilesystemHelper:
layout_marker.write(self.layout_name)
else:
with open(layout_marker_path, 'r') as layout_marker:
layout = open(layout_marker_path, 'r').read().strip()
layout = layout_marker.read().strip()
if layout != self.layout_name:
raise ValueError(
"Directory layout `%s` selected for blob directory %s, but "
......@@ -508,8 +509,8 @@ def auto_layout_select(path):
# use.
layout_marker = os.path.join(path, LAYOUT_MARKER)
if os.path.exists(layout_marker):
layout = open(layout_marker, 'r').read()
layout = layout.strip()
with open(layout_marker, 'r') as fp:
layout = fp.read().strip()
log('Blob directory `%s` has layout marker set. '
'Selected `%s` layout. ' % (path, layout), level=logging.DEBUG)
elif not os.path.exists(path):
......@@ -960,7 +961,8 @@ def copyTransactionsFromTo(source, destination):
fd, name = tempfile.mkstemp(
suffix='.tmp', dir=destination.fshelper.temp_dir)
os.close(fd)
utils.cp(open(blobfilename, 'rb'), open(name, 'wb'))
with open(blobfilename, 'rb') as sf, open(name, 'wb') as df:
utils.cp(sf, df)
destination.restoreBlob(record.oid, record.tid, record.data,
name, record.data_txn, trans)
else:
......
......@@ -387,6 +387,7 @@ def recover(inp, outp, verbose=0, partial=False, force=False, pack=None):
ofs.pack(pack, referencesf)
ofs.close()
f.close()
if __name__ == "__main__":
main()
......@@ -91,7 +91,8 @@ class FileStorageTests(
newindex = dict(index)
data['index'] = newindex
dump(data, open('FileStorageTests.fs.index', 'wb'), 1)
with open('FileStorageTests.fs.index', 'wb') as fp:
dump(data, fp, 1)
return index
def check_conversion_to_fsIndex(self, read_only=False):
......
......@@ -13,7 +13,6 @@
##############################################################################
"""Tests of the file storage recovery script."""
import base64
import os
import random
import sys
......@@ -22,6 +21,7 @@ import unittest
import ZODB
import ZODB.tests.util
from ZODB.FileStorage import FileStorage
from ZODB._compat import decodebytes
import ZODB.fsrecover
from persistent.mapping import PersistentMapping
......@@ -152,11 +152,11 @@ class RecoverTest(ZODB.tests.util.TestCase):
L = self.storage.undoLog()
r = L[3]
tid = base64.decodestring(r["id"] + b"\n")
tid = decodebytes(r["id"] + b"\n")
pos1 = self.storage._txn_find(tid, 0)
r = L[8]
tid = base64.decodestring(r["id"] + b"\n")
tid = decodebytes(r["id"] + b"\n")
pos2 = self.storage._txn_find(tid, 0)
self.storage.close()
......@@ -190,7 +190,7 @@ class RecoverTest(ZODB.tests.util.TestCase):
# Find a transaction near the end.
L = self.storage.undoLog()
r = L[1]
tid = base64.decodestring(r["id"] + b"\n")
tid = decodebytes(r["id"] + b"\n")
pos = self.storage._txn_find(tid, 0)
# Overwrite its status with 'c'.
......
......@@ -221,7 +221,8 @@ Note that we pass a file position, which gets saved with the index data.
If we save the data in the old format, we can still read it:
>>> from ZODB._compat import dump
>>> dump(dict(pos=42, index=index), open('old', 'wb'), 1)
>>> with open('old', 'wb') as fp:
... dump(dict(pos=42, index=index), fp, 1)
>>> info = fsIndex.load('old')
>>> info['pos']
42
......
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