-
-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zipstore delitems via override #1184
Changes from all commits
96d1183
a83bff2
41017c9
0144f95
609186d
16fb300
bf18397
d2d49de
60f9f61
6366a2c
a65d383
a41af48
ab52106
425a744
8544da2
57ce061
15c783c
040da5f
5f8b815
489390a
736e27e
458d31d
dcce2c3
d305642
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1831,7 +1831,12 @@ def __exit__(self, *args): | |||||
def __getitem__(self, key): | ||||||
with self.mutex: | ||||||
with self.zf.open(key) as f: # will raise KeyError | ||||||
return f.read() | ||||||
data = f.read() | ||||||
|
||||||
if data: | ||||||
return data | ||||||
else: | ||||||
raise KeyError("Key not found") | ||||||
|
||||||
def __setitem__(self, key, value): | ||||||
if self.mode == "r": | ||||||
|
@@ -1852,7 +1857,23 @@ def __setitem__(self, key, value): | |||||
self.zf.writestr(keyinfo, value) | ||||||
|
||||||
def __delitem__(self, key): | ||||||
raise NotImplementedError | ||||||
with self.mutex: | ||||||
try: | ||||||
self.zf.getinfo(key) | ||||||
jakirkham marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
except KeyError: | ||||||
raise KeyError("Cannot delete a non-existent key") | ||||||
keyinfo = zipfile.ZipInfo( | ||||||
filename=key, | ||||||
date_time=time.localtime(time.time())[:6] | ||||||
) | ||||||
keyinfo.compress_type = self.compression | ||||||
if keyinfo.filename[-1] == os.sep: | ||||||
keyinfo.external_attr = 0o40775 << 16 # drwxrwxr-x | ||||||
keyinfo.external_attr |= 0x10 # MS-DOS directory flag | ||||||
else: | ||||||
keyinfo.external_attr = 0o644 << 16 # ?rw-r--r-- | ||||||
|
||||||
self.zf.writestr(keyinfo, b"") | ||||||
|
||||||
def __eq__(self, other): | ||||||
return ( | ||||||
|
@@ -1864,7 +1885,8 @@ def __eq__(self, other): | |||||
|
||||||
def keylist(self): | ||||||
with self.mutex: | ||||||
return sorted(self.zf.namelist()) | ||||||
namelist = [key for key in self.zf.namelist() if self.zf.getinfo(key) != b""] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would suggest passing Also with Otherwise this seems reasonable.
Suggested change
|
||||||
return sorted(namelist) | ||||||
|
||||||
def keys(self): | ||||||
yield from self.keylist() | ||||||
|
@@ -1878,7 +1900,11 @@ def __len__(self): | |||||
def __contains__(self, key): | ||||||
try: | ||||||
with self.mutex: | ||||||
self.zf.getinfo(key) | ||||||
value = self.zf.getinfo(key) | ||||||
|
||||||
if value == b"": | ||||||
raise KeyError | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
except KeyError: | ||||||
return False | ||||||
else: | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,6 +217,21 @@ def test_pop(self): | |
|
||
store.close() | ||
|
||
def test_delitem(self): | ||
store = self.create_store() | ||
foo = self.root + 'foo' | ||
store[foo] = b'bar' | ||
|
||
del store[foo] | ||
|
||
with pytest.raises(KeyError): | ||
store[foo] | ||
# if foo in store: | ||
# if isinstance(store, ZipStore): | ||
# assert store[foo] == b'' | ||
# else: | ||
# assert foo not in store | ||
|
||
def test_popitem(self): | ||
store = self.create_store() | ||
store[self.root + "foo"] = b"bar" | ||
|
@@ -1803,8 +1818,7 @@ def test_flush(self): | |
store.flush() | ||
assert store[self.root + "foo"] == b"bar" | ||
store.close() | ||
|
||
store = self.ZipStoreClass("data/store.zip", mode="r") | ||
store = self.ZipStoreClass('data/store.zip', mode='r') | ||
store.flush() # no-op | ||
|
||
def test_context_manager(self): | ||
|
@@ -1816,9 +1830,9 @@ def test_context_manager(self): | |
def test_pop(self): | ||
# override because not implemented | ||
store = self.create_store() | ||
store[self.root + "foo"] = b"bar" | ||
with pytest.raises(NotImplementedError): | ||
store.pop(self.root + "foo") | ||
store[self.root + 'foo'] = b'bar' | ||
store.pop(self.root + 'foo') | ||
assert store[self.root + 'foo'] == b"" | ||
|
||
def test_popitem(self): | ||
# override because not implemented | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would also update this test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.