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