Skip to content

Commit 38d0ae2

Browse files
committed
replace assert by if <condition>: raise SomeException
Signed-off-by: Nish_ <120EE0980@nitrkl.ac.in>
1 parent 4eef617 commit 38d0ae2

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/borg/hashindex.pyx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class ChunkIndex(HTProxyMixin, MutableMapping):
7878
flags = self.F_USED
7979
else:
8080
flags = v.flags | self.F_USED
81-
assert v.size == 0 or v.size == size
81+
if v.size != 0 and v.size != size:
82+
raise ValueError(f"Invalid size: expected 0 or {size}, got {v.size}")
8283
self[key] = ChunkIndexEntry(flags=flags, size=size)
8384

8485
def __getitem__(self, key):
@@ -217,8 +218,10 @@ class NSIndex1(HTProxyMixin, MutableMapping):
217218
magic, entries, buckets, ksize, vsize = struct.unpack(self.HEADER_FMT, header_bytes)
218219
if magic != self.MAGIC:
219220
raise ValueError(f"Invalid file, magic {self.MAGIC.decode()} not found.")
220-
assert ksize == self.KEY_SIZE, "invalid key size"
221-
assert vsize == self.VALUE_SIZE, "invalid value size"
221+
if ksize != self.KEY_SIZE:
222+
raise ValueError("Invalid key size")
223+
if vsize != self.VALUE_SIZE:
224+
raise ValueError("Invalid value size")
222225
buckets_size = buckets * (ksize + vsize)
223226
current_pos = fd.tell()
224227
end_of_file = fd.seek(0, os.SEEK_END)
@@ -230,4 +233,5 @@ class NSIndex1(HTProxyMixin, MutableMapping):
230233
value = fd.read(vsize)
231234
self.ht._set_raw(key, value)
232235
pos = fd.tell()
233-
assert pos == end_of_file
236+
if pos != end_of_file:
237+
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
@@ -44,10 +44,14 @@ def process_alive(host, pid, thread):
4444
from . import local_pid_alive
4545
from . import hostid
4646

47-
assert isinstance(host, str)
48-
assert isinstance(hostid, str)
49-
assert isinstance(pid, int)
50-
assert isinstance(thread, int)
47+
if not isinstance(host, str):
48+
raise TypeError("host must be a string")
49+
if not isinstance(hostid, str):
50+
raise TypeError("hostid must be a string")
51+
if not isinstance(pid, int):
52+
raise TypeError("pid must be an integer")
53+
if not isinstance(thread, int):
54+
raise TypeError("thread must be an integer")
5155

5256
if host != hostid:
5357
return True
@@ -116,7 +120,8 @@ def group2gid(group, default=None):
116120
def posix_acl_use_stored_uid_gid(acl):
117121
"""Replace the user/group field with the stored uid/gid
118122
"""
119-
assert isinstance(acl, bytes)
123+
if not isinstance(acl, bytes):
124+
raise TypeError("acl must be of type bytes")
120125
from ..helpers import safe_decode, safe_encode
121126
entries = []
122127
for entry in safe_decode(acl).split('\n'):

0 commit comments

Comments
 (0)