Skip to content

Commit e45959a

Browse files
120EE0980ThomasWaldmann
authored andcommitted
replace assert by if <condition>: raise SomeException
Signed-off-by: Nish_ <120EE0980@nitrkl.ac.in>
1 parent dd67bf3 commit e45959a

4 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/borg/hashindex.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class ChunkIndex(HTProxyMixin, MutableMapping):
8080
flags = self.F_USED
8181
else:
8282
flags = v.flags | self.F_USED
83-
assert v.size == 0 or v.size == size
83+
if v.size != 0 and v.size != size:
84+
raise ValueError(f"Invalid size: expected 0 or {size}, got {v.size}")
8485
self[key] = ChunkIndexEntry(
8586
flags=flags, size=size, pack_id=UNKNOWN_BYTES32, obj_offset=UNKNOWN_INT32, obj_size=UNKNOWN_INT32
8687
)

src/borg/legacy/hashindex.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ def _read_fd(self, fd):
8888
magic, entries, buckets, ksize, vsize = struct.unpack(self.HEADER_FMT, header_bytes)
8989
if magic != self.MAGIC:
9090
raise ValueError(f"Invalid file: magic {self.MAGIC.decode()} not found.")
91-
assert ksize == self.KEY_SIZE, "invalid key size"
92-
assert vsize == self.VALUE_SIZE, "invalid value size"
91+
if ksize != self.KEY_SIZE:
92+
raise ValueError("Invalid key size")
93+
if vsize != self.VALUE_SIZE:
94+
raise ValueError("Invalid value size")
9395
buckets_size = buckets * (ksize + vsize)
9496
current_pos = fd.tell()
9597
end_of_file = fd.seek(0, os.SEEK_END)
@@ -105,4 +107,5 @@ def _read_fd(self, fd):
105107
continue
106108
self.ht._set_raw(key, value)
107109
pos = fd.tell()
108-
assert pos == end_of_file
110+
if pos != end_of_file:
111+
raise ValueError(f"Expected pos ({pos}) to be at end_of_file ({end_of_file})")

src/borg/platform/posix.pyx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ def process_alive(host, pid, thread):
2020
from . import local_pid_alive
2121
from . import hostid
2222

23-
assert isinstance(host, str)
24-
assert isinstance(hostid, str)
25-
assert isinstance(pid, int)
26-
assert isinstance(thread, int)
23+
if not isinstance(host, str):
24+
raise TypeError("host must be a string")
25+
if not isinstance(hostid, str):
26+
raise TypeError("hostid must be a string")
27+
if not isinstance(pid, int):
28+
raise TypeError("pid must be an integer")
29+
if not isinstance(thread, int):
30+
raise TypeError("thread must be an integer")
2731

2832
if host != hostid:
2933
return True
@@ -55,7 +59,8 @@ def local_pid_alive(pid):
5559

5660
def posix_acl_use_stored_uid_gid(acl):
5761
"""Replace the user/group field with the stored uid/gid."""
58-
assert isinstance(acl, bytes)
62+
if not isinstance(acl, bytes):
63+
raise TypeError("acl must be of type bytes")
5964
from ..helpers import safe_decode, safe_encode
6065
entries = []
6166
for entry in safe_decode(acl).split('\n'):

src/borg/testsuite/hashindex_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_chunkindex_add():
3232
assert chunks[x] == ChunkIndexEntry(
3333
flags=ChunkIndex.F_USED, size=2, pack_id=UNKNOWN_BYTES32, obj_offset=UNKNOWN_INT32, obj_size=UNKNOWN_INT32
3434
)
35-
with pytest.raises(AssertionError):
35+
with pytest.raises(ValueError):
3636
chunks.add(x, 3) # inconsistent size (we already have a different size)
3737

3838

0 commit comments

Comments
 (0)