From 06cf15cc6dcee0e490755e5d17a8eccd9bc0ea42 Mon Sep 17 00:00:00 2001 From: Marian Beermann Date: Thu, 25 May 2017 13:43:04 +0200 Subject: [PATCH] hashindex: read/write: accept file-like objects for path --- src/borg/hashindex.pyx | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/borg/hashindex.pyx b/src/borg/hashindex.pyx index fba8c7a38..2409836fe 100644 --- a/src/borg/hashindex.pyx +++ b/src/borg/hashindex.pyx @@ -67,8 +67,11 @@ cdef class IndexBase: def __cinit__(self, capacity=0, path=None, key_size=32): self.key_size = key_size if path: - with open(path, 'rb') as fd: - self.index = hashindex_read(fd) + if isinstance(path, (str, bytes)): + with open(path, 'rb') as fd: + self.index = hashindex_read(fd) + else: + self.index = hashindex_read(path) assert self.index, 'hashindex_read() returned NULL with no exception set' else: self.index = hashindex_init(capacity, self.key_size, self.value_size) @@ -84,8 +87,11 @@ cdef class IndexBase: return cls(path=path) def write(self, path): - with open(path, 'wb') as fd: - hashindex_write(self.index, fd) + if isinstance(path, (str, bytes)): + with open(path, 'wb') as fd: + hashindex_write(self.index, fd) + else: + hashindex_write(self.index, path) def clear(self): hashindex_free(self.index)