Skip to content

Commit f8186c8

Browse files
committed
Turn some asserts back into exceptions
1 parent 6f5fd10 commit f8186c8

6 files changed

Lines changed: 142 additions & 74 deletions

File tree

python/basicblock.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ def index(self) -> int:
223223

224224
def _make_edges(self, edges, count:int, direction:bool) -> List[BasicBlockEdge]:
225225
assert edges is not None, "Got empty edges list from core"
226-
assert self.view is not None, "Attempting to get BasicBlock edges when BinaryView is None"
226+
if self.view is None:
227+
raise ValueError("Attempting to get BasicBlock edges when BinaryView is None")
227228
result:List[BasicBlockEdge] = []
228229
try:
229230
for i in range(0, count):
@@ -371,9 +372,10 @@ def post_dominance_frontier(self) -> List['BasicBlock']:
371372
@property
372373
def annotations(self) -> List[List['_function.InstructionTextToken']]:
373374
"""List of automatic annotations for the start of this block (read-only)"""
374-
assert self.arch is not None, "attempting to get annotation from BasicBlock without architecture"
375+
if self.arch is None:
376+
raise ValueError("attempting to get annotation from BasicBlock without architecture")
375377
if self.function is None:
376-
raise Exception("Attempting to call BasicBlock.annotations when BinaryView is None")
378+
raise ValueError("Attempting to call BasicBlock.annotations when Function is None")
377379

378380
return self.function.get_block_annotations(self.start, self.arch)
379381

python/binaryview.py

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4717,7 +4717,8 @@ def create_tag(self, tag_type:'TagType', data:str, user:bool=True) -> 'Tag':
47174717
>>> bv.add_user_data_tag(here, tag)
47184718
>>>
47194719
"""
4720-
assert isinstance(tag_type, TagType), f"type is not a TagType instead got {type(tag_type)} : {repr(tag_type)}"
4720+
if not isinstance(tag_type, TagType):
4721+
raise ValueError(f"type is not a TagType instead got {type(tag_type)} : {repr(tag_type)}")
47214722
tag_handle = core.BNCreateTag(tag_type.handle, data)
47224723
assert tag_handle is not None, "core.BNCreateTag returned None"
47234724
tag = Tag(tag_handle)
@@ -6226,7 +6227,8 @@ def define_user_type(self, name:Optional['_types.QualifiedNameType'], type_obj:S
62266227
(type_obj, new_name) = self.parse_type_string(type_obj)
62276228
if name is None:
62286229
name = new_name
6229-
assert name is not None, "name can only be None if named type is derived from string passed to type_obj"
6230+
if name is None:
6231+
raise ValueError("name can only be None if named type is derived from string passed to type_obj")
62306232
_name = _types.QualifiedName(name)._to_core_struct()
62316233
core.BNDefineUserAnalysisType(self.handle, _name, type_obj.handle)
62326234

@@ -6351,14 +6353,15 @@ def export_type_to_library(self, lib:typelibrary.TypeLibrary, name:Optional[str]
63516353
if name is not None:
63526354
_name = _types.QualifiedName(name)
63536355
if not isinstance(lib, typelibrary.TypeLibrary):
6354-
raise ValueError("lib must be a TypeLibrary object")
6356+
raise TypeError("lib must be a TypeLibrary object")
63556357
if isinstance(type_obj, str):
63566358
(type_obj, new_name) = self.parse_type_string(type_obj)
63576359
if name is None:
63586360
_name = new_name
63596361
if not isinstance(type_obj, (_types.Type, _types.TypeBuilder)):
6360-
raise ValueError("type_obj must be a Type object")
6361-
assert _name is not None, "name can only be None if named type is derived from string passed to type_obj"
6362+
raise TypeError("type_obj must be a Type object")
6363+
if _name is None:
6364+
raise ValueError("name can only be None if named type is derived from string passed to type_obj")
63626365
core.BNBinaryViewExportTypeToTypeLibrary(self.handle, lib.handle, _name._to_core_struct(), type_obj.handle)
63636366

63646367
def export_object_to_library(self, lib:typelibrary.TypeLibrary, name:Optional[str], type_obj:StringOrType) -> None:
@@ -6378,14 +6381,15 @@ def export_object_to_library(self, lib:typelibrary.TypeLibrary, name:Optional[st
63786381
if name is not None:
63796382
_name = _types.QualifiedName(name)
63806383
if not isinstance(lib, typelibrary.TypeLibrary):
6381-
raise ValueError("lib must be a TypeLibrary object")
6384+
raise TypeError("lib must be a TypeLibrary object")
63826385
if isinstance(type_obj, str):
63836386
(type_obj, new_name) = self.parse_type_string(type_obj)
63846387
if name is None:
63856388
_name = new_name
63866389
if not isinstance(type_obj, (_types.Type, _types.TypeBuilder)):
6387-
raise ValueError("type_obj must be a Type object")
6388-
assert _name is not None, "name can only be None if named type is derived from string passed to type_obj"
6390+
raise TypeError("type_obj must be a Type object")
6391+
if _name is None:
6392+
raise ValueError("name can only be None if named type is derived from string passed to type_obj")
63896393
core.BNBinaryViewExportObjectToTypeLibrary(self.handle, lib.handle, _name._to_core_struct(), type_obj.handle)
63906394

63916395
def register_platform_types(self, platform:'_platform.Platform') -> None:
@@ -7026,13 +7030,13 @@ def debug_info(self) -> "debuginfo.DebugInfo":
70267030
def debug_info(self, value: "debuginfo.DebugInfo") -> None:
70277031
"""Sets the debug info for the current binary view"""
70287032
if not isinstance(value, debuginfo.DebugInfo):
7029-
assert False, "Attempting to set debug_info to something which isn't and instance of 'DebugInfo'"
7033+
raise ValueError("Attempting to set debug_info to something which isn't and instance of 'DebugInfo'")
70307034
core.BNSetDebugInfo(self.handle, value.handle)
70317035

70327036
def apply_debug_info(self, value: "debuginfo.DebugInfo") -> None:
70337037
"""Sets the debug info and applies its contents to the current binary view"""
70347038
if not isinstance(value, debuginfo.DebugInfo):
7035-
assert False, "Attempting to apply_debug_info with something which isn't and instance of 'DebugInfo'"
7039+
raise ValueError("Attempting to apply_debug_info with something which isn't and instance of 'DebugInfo'")
70367040
core.BNApplyDebugInfo(self.handle, value.handle)
70377041

70387042
def query_metadata(self, key:str) -> 'metadata.MetadataValueType':
@@ -7962,8 +7966,10 @@ def __init__(self, bv:'BinaryView', structure_name:'_types.QualifiedNameType', a
79627966
s = self._bv.get_type_by_name(self._structure_name)
79637967
if isinstance(s, _types.NamedTypeReferenceType):
79647968
s = s.target(self._bv)
7965-
assert s is not None, f"Failed to find type: {structure_name}"
7966-
assert isinstance(s, _types.StructureType), f"{self._structure_name} is not a StructureTypeClass, got: {type(s)}"
7969+
if s is None:
7970+
raise ValueError(f"Failed to find type: {structure_name}")
7971+
if not isinstance(s, _types.StructureType):
7972+
raise ValueError(f"{self._structure_name} is not a StructureTypeClass, got: {type(s)}")
79677973
self._structure = s
79687974

79697975
for m in self._structure.members:
@@ -8027,7 +8033,8 @@ class TypedDataAccessor:
80278033
endian:Endianness
80288034

80298035
def __post_init__(self):
8030-
assert isinstance(self.type, _types.Type), "Attempting to create TypedDataAccessor with TypeBuilder"
8036+
if not isinstance(self.type, _types.Type):
8037+
raise TypeError("Attempting to create TypedDataAccessor with TypeBuilder")
80318038

80328039
def __bytes__(self):
80338040
return self.view.read(self.address, len(self))
@@ -8039,7 +8046,8 @@ def __len__(self):
80398046
_type = self.type
80408047
if isinstance(_type, _types.NamedTypeReferenceType):
80418048
_type = _type.target(self.view)
8042-
assert _type is not None
8049+
if _type is None:
8050+
raise ValueError(f"Couldn't get target of type {_type}")
80438051
return len(_type)
80448052

80458053
def __int__(self):
@@ -8057,12 +8065,16 @@ def __getitem__(self, key:Union[str, int]) -> 'TypedDataAccessor':
80578065
if isinstance(_type, _types.NamedTypeReferenceType):
80588066
_type = _type.target(self.view)
80598067
if isinstance(_type, _types.ArrayType) and isinstance(key, int):
8060-
assert key < _type.count, f"Index {key} out of bounds array has {_type.count} elements"
8068+
if key >= _type.count:
8069+
raise ValueError(f"Index {key} out of bounds array has {_type.count} elements")
80618070
return TypedDataAccessor(_type.element_type, key * len(_type.element_type), self.view, self.endian)
8062-
assert isinstance(_type, _types.StructureType), "Can't get member of non-structure"
8063-
assert isinstance(key, str), "Must use string to get member of structure"
8071+
if not isinstance(_type, _types.StructureType):
8072+
raise ValueError("Can't get member of non-structure")
8073+
if not isinstance(key, str):
8074+
raise ValueError("Must use string to get member of structure")
80648075
m = _type[key]
8065-
assert m is not None, f"Member {key} doesn't exist in structure"
8076+
if m is None:
8077+
raise ValueError(f"Member {key} doesn't exist in structure")
80668078
return TypedDataAccessor(m.type.immutable_copy(), self.address + m.offset, self.view, self.endian)
80678079

80688080
@staticmethod
@@ -8100,7 +8112,8 @@ def value(self, data:Union[bytes, int]) -> None:
81008112
_types.WideCharType, _types.WideCharBuilder,
81018113
_types.PointerType, _types.PointerBuilder,
81028114
_types.EnumerationType, _types.EnumerationBuilder)
8103-
assert isinstance(self.type, integral_types), f"Can't set the value of type {type(self.type)} to int value"
8115+
if not isinstance(self.type, integral_types):
8116+
raise TypeError(f"Can't set the value of type {type(self.type)} to int value")
81048117
to_write = data.to_bytes(len(self), TypedDataAccessor.byte_order(self.endian)) # type: ignore
81058118
elif isinstance(data, float) and isinstance(self.type, (_types.FloatType, _types.FloatBuilder)):
81068119
endian = "<" if self.endian == Endianness.LittleEndian else ">"
@@ -8119,10 +8132,12 @@ def value(self, data:Union[bytes, int]) -> None:
81198132
assert count == len(to_write), "Unable to write all bytes to the location, segment might not have file backing"
81208133

81218134
def _value_helper(self, _type:'_types.Type', data:bytes) -> Any:
8122-
assert isinstance(_type, _types.Type), f"Attempting to get value of TypeBuilder of type {type(_type)}"
8135+
if not isinstance(_type, _types.Type):
8136+
raise TypeError(f"Attempting to get value of TypeBuilder of type {type(_type)}")
81238137
if isinstance(_type, _types.NamedTypeReferenceType):
81248138
target = _type.target(self.view)
8125-
assert target is not None
8139+
if target is None:
8140+
raise ValueError("Couldn't find target for type")
81268141
_type = target
81278142

81288143
if isinstance(_type, (_types.VoidType, _types.FunctionType)): #, _types.VarArgsType, _types.ValueType)):
@@ -8156,7 +8171,7 @@ def _value_helper(self, _type:'_types.Type', data:bytes) -> Any:
81568171
result.append(TypedDataAccessor(_type.element_type, self.address + offset, self.view, self.endian).value)
81578172
return result
81588173
else:
8159-
assert False, f"Unhandled `Type` {type(_type)}"
8174+
raise TypeError(f"Unhandled `Type` {type(_type)}")
81608175

81618176

81628177
# for backward compatibility

python/databuffer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def __init__(self, contents:Union[str, bytes, 'DataBuffer', int]=b"", handle=Non
3636
elif isinstance(contents, str):
3737
self.handle = core.BNCreateDataBuffer(contents.encode("utf-8"), len(contents.encode("utf-8")))
3838
else:
39-
assert isinstance(contents, bytes)
39+
if not isinstance(contents, bytes):
40+
raise TypeError(f"type {type(contents)} not convertable to DataBuffer")
4041
self.handle = core.BNCreateDataBuffer(contents, len(contents))
4142

4243
def __del__(self):

0 commit comments

Comments
 (0)