Skip to content

Commit

Permalink
UP031 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
twizmwazin committed Jun 19, 2024
1 parent badd85f commit 13b00f5
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 67 deletions.
2 changes: 1 addition & 1 deletion claripy/ast/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ def _op_repr(op, args, inner, length, details, inner_infix_use_par):
extras.append("stride=%s" % (fmt % args[3]))
if args[4] is True:
extras.append("UNINITIALIZED")
return "{}{}".format(args[0], "{%s}" % ", ".join(extras) if extras else "")
return "{}{}".format(args[0], "{{{}}}".format(", ".join(extras)) if extras else "")

elif op == "BoolV":
return str(args[0])
Expand Down
4 changes: 2 additions & 2 deletions claripy/ast/bv.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def BVS(
if isinstance(name, bytes):
name = name.decode()
if not isinstance(name, str):
raise TypeError("Name value for BVS must be a str, got %r" % type(name))
raise TypeError(f"Name value for BVS must be a str, got {type(name)!r}")

n = _make_name(name, size, False if explicit_name is None else explicit_name)
encoded_name = n.encode()
Expand Down Expand Up @@ -366,7 +366,7 @@ def ValueSet(bits, region=None, region_base_addr=None, value=None, name=None, va
min_v, max_v = v.lower_bound, v.upper_bound
stride = v.stride
else:
raise ClaripyValueError("ValueSet() does not take `value` of type %s" % type(value))
raise ClaripyValueError(f"ValueSet() does not take `value` of type {type(value)}")

if name is None:
name = "ValueSet"
Expand Down
14 changes: 7 additions & 7 deletions claripy/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def _abstract(self, e): # pylint:disable=W0613,R0201
:param e: The backend object.
:return: An AST.
"""
raise BackendError("backend %s doesn't implement abstract()" % self.__class__.__name__)
raise BackendError(f"backend {self.__class__.__name__} doesn't implement abstract()")

#
# These functions simplify expressions.
Expand All @@ -300,7 +300,7 @@ def simplify(self, e):
return o

def _simplify(self, e): # pylint:disable=R0201,unused-argument
raise BackendError("backend %s can't simplify" % self.__class__.__name__)
raise BackendError(f"backend {self.__class__.__name__} can't simplify")

#
# Some other helpers
Expand Down Expand Up @@ -525,7 +525,7 @@ def eval(self, expr, n, extra_constraints=(), solver=None, model_callback=None):
:return: A sequence of up to n results (backend objects)
"""
if self._solver_required and solver is None:
raise BackendError("%s requires a solver for evaluation" % self.__class__.__name__)
raise BackendError(f"{self.__class__.__name__} requires a solver for evaluation")

return self._eval(
self.convert(expr),
Expand Down Expand Up @@ -563,7 +563,7 @@ def batch_eval(self, exprs, n, extra_constraints=(), solver=None, model_callback
:return: A list of up to n tuples, where each tuple is a solution for all expressions.
"""
if self._solver_required and solver is None:
raise BackendError("%s requires a solver for batch evaluation" % self.__class__.__name__)
raise BackendError(f"{self.__class__.__name__} requires a solver for batch evaluation")

converted_exprs = [self.convert(ex) for ex in exprs]

Expand Down Expand Up @@ -604,7 +604,7 @@ def min(self, expr, extra_constraints=(), signed=False, solver=None, model_callb
:return: the minimum possible value of expr (backend object)
"""
if self._solver_required and solver is None:
raise BackendError("%s requires a solver for evaluation" % self.__class__.__name__)
raise BackendError(f"{self.__class__.__name__} requires a solver for evaluation")

return self._min(
self.convert(expr),
Expand Down Expand Up @@ -643,7 +643,7 @@ def max(self, expr, extra_constraints=(), signed=False, solver=None, model_callb
:return: the maximum possible value of expr (backend object)
"""
if self._solver_required and solver is None:
raise BackendError("%s requires a solver for evaluation" % self.__class__.__name__)
raise BackendError(f"{self.__class__.__name__} requires a solver for evaluation")

return self._max(
self.convert(expr),
Expand Down Expand Up @@ -736,7 +736,7 @@ def solution(self, expr, v, extra_constraints=(), solver=None, model_callback=No
:return: True if `v` is a solution of `expr`, False otherwise
"""
if self._solver_required and solver is None:
raise BackendError("%s requires a solver for evaluation" % self.__class__.__name__)
raise BackendError(f"{self.__class__.__name__} requires a solver for evaluation")

return self._solution(
self.convert(expr),
Expand Down
2 changes: 1 addition & 1 deletion claripy/backends/backend_concrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _convert(self, a):
return a
if isinstance(a, (numbers.Number, bv.BVV, fp.FPV, fp.RM, fp.FSort, strings.StringV)):
return a
raise BackendError("can't handle AST of type %s" % type(a))
raise BackendError(f"can't handle AST of type {type(a)}")

def _simplify(self, e):
return e
Expand Down
2 changes: 1 addition & 1 deletion claripy/backends/backend_smtlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _expr_to_smtlib(e, daggify=True):
if e.is_symbol():
return f"(declare-fun {e.symbol_name()} {e.symbol_type().as_smtlib()})"
else:
return "(assert %s)" % e.to_smtlib(daggify=daggify)
return f"(assert {e.to_smtlib(daggify=daggify)})"


def _exprs_to_smtlib(*exprs, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion claripy/backends/backend_smtlib_solvers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def eval(self, expr, n, extra_constraints=(), solver=None, model_callback=None):
:return: A sequence of up to n results (backend objects)
"""
if self._solver_required and solver is None:
raise BackendError("%s requires a solver for evaluation" % self.__class__.__name__)
raise BackendError(f"{self.__class__.__name__} requires a solver for evaluation")

results = self._eval(
self.convert(expr),
Expand Down
20 changes: 10 additions & 10 deletions claripy/backends/backend_vsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def arg_filter(f):
@functools.wraps(f)
def filter(*args): # pylint:disable=redefined-builtin
if isinstance(args[0], numbers.Number): # pylint:disable=unidiomatic-typecheck
raise BackendError("Unsupported argument type %s" % type(args[0]))
raise BackendError(f"Unsupported argument type {type(args[0])}")
return f(*args)

return filter
Expand Down Expand Up @@ -149,7 +149,7 @@ def _eval(self, expr, n, extra_constraints=(), solver=None, model_callback=None)
elif isinstance(expr, BoolResult):
return expr.value
else:
raise BackendError("Unsupported type %s" % type(expr))
raise BackendError(f"Unsupported type {type(expr)}")

def _min(self, expr, extra_constraints=(), signed=False, solver=None, model_callback=None):
# TODO: signed min
Expand All @@ -164,7 +164,7 @@ def _min(self, expr, extra_constraints=(), signed=False, solver=None, model_call
return expr.min

else:
raise BackendError("Unsupported expr type %s" % type(expr))
raise BackendError(f"Unsupported expr type {type(expr)}")

def _max(self, expr, extra_constraints=(), signed=False, solver=None, model_callback=None):
# TODO: signed max
Expand All @@ -179,7 +179,7 @@ def _max(self, expr, extra_constraints=(), signed=False, solver=None, model_call
return expr.max

else:
raise BackendError("Unsupported expr type %s" % type(expr))
raise BackendError(f"Unsupported expr type {type(expr)}")

def _solution(self, obj, v, extra_constraints=(), solver=None, model_callback=None):
if isinstance(obj, BoolResult):
Expand Down Expand Up @@ -221,7 +221,7 @@ def _unique(self, obj): # pylint:disable=unused-argument,no-self-use
if isinstance(obj, (StridedInterval, ValueSet)):
return obj.unique
else:
raise BackendError("Not supported type of operand %s" % type(obj))
raise BackendError(f"Not supported type of operand {type(obj)}")

def _cardinality(self, a): # pylint:disable=unused-argument,no-self-use
return a.cardinality
Expand Down Expand Up @@ -365,7 +365,7 @@ def Concat(*args):
DiscreteStridedIntervalSet,
ValueSet,
}: # pylint:disable=unidiomatic-typecheck
raise BackendError("Unsupported expr type %s" % type(expr))
raise BackendError(f"Unsupported expr type {type(expr)}")

ret = ret.concat(expr) if ret is not None else expr

Expand All @@ -382,7 +382,7 @@ def Extract(*args):
DiscreteStridedIntervalSet,
ValueSet,
}: # pylint:disable=unidiomatic-typecheck
raise BackendError("Unsupported expr type %s" % type(expr))
raise BackendError(f"Unsupported expr type {type(expr)}")

ret = expr.extract(high_bit, low_bit)

Expand All @@ -394,7 +394,7 @@ def SignExt(*args):
expr = args[1]

if type(expr) not in {StridedInterval, DiscreteStridedIntervalSet}: # pylint:disable=unidiomatic-typecheck
raise BackendError("Unsupported expr type %s" % type(expr))
raise BackendError(f"Unsupported expr type {type(expr)}")

return expr.sign_extend(new_bits + expr.bits)

Expand All @@ -404,7 +404,7 @@ def ZeroExt(*args):
expr = args[1]

if type(expr) not in {StridedInterval, DiscreteStridedIntervalSet}: # pylint:disable=unidiomatic-typecheck
raise BackendError("Unsupported expr type %s" % type(expr))
raise BackendError(f"Unsupported expr type {type(expr)}")

return expr.zero_extend(new_bits + expr.bits)

Expand All @@ -415,7 +415,7 @@ def Reverse(arg):
DiscreteStridedIntervalSet,
ValueSet,
}: # pylint:disable=unidiomatic-typecheck
raise BackendError("Unsupported expr type %s" % type(arg))
raise BackendError(f"Unsupported expr type {type(arg)}")

return arg.reverse()

Expand Down
9 changes: 4 additions & 5 deletions claripy/backends/backend_z3.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def _convert(self, obj): # pylint:disable=arguments-renamed
return obj
else:
l.debug("BackendZ3 encountered unexpected type %s", type(obj))
raise BackendError("unexpected type %s encountered in BackendZ3" % type(obj))
raise BackendError(f"unexpected type {type(obj)} encountered in BackendZ3")

def call(self, *args, **kwargs): # pylint;disable=arguments-renamed
return Backend.call(self, *args, **kwargs)
Expand Down Expand Up @@ -450,7 +450,7 @@ def _abstract_internal(self, ctx, ast, split_on=None):
if decl_num not in z3_op_nums:
raise ClaripyError("unknown decl kind %d" % decl_num)
if z3_op_nums[decl_num] not in op_map:
raise ClaripyError("unknown decl op %s" % z3_op_nums[decl_num])
raise ClaripyError(f"unknown decl op {z3_op_nums[decl_num]}")
op_name = op_map[z3_op_nums[decl_num]]

num_args = z3.Z3_get_app_num_args(ctx, ast)
Expand Down Expand Up @@ -547,8 +547,7 @@ def _abstract_internal(self, ctx, ast, split_on=None):

if isinstance(result_ty, str):
err = (
"Unknown Z3 error in abstraction (result_ty == '%s'). Update your version of Z3, and, if the problem persists, open a claripy issue."
% result_ty
f"Unknown Z3 error in abstraction (result_ty == '{result_ty}'). Update your version of Z3, and, if the problem persists, open a claripy issue."
)
l.error(err)
raise BackendError(err)
Expand Down Expand Up @@ -579,7 +578,7 @@ def _abstract_to_primitive(self, ctx, ast):
if decl_num not in z3_op_nums:
raise ClaripyError("unknown decl kind %d" % decl_num)
if z3_op_nums[decl_num] not in op_map:
raise ClaripyError("unknown decl op %s" % z3_op_nums[decl_num])
raise ClaripyError(f"unknown decl op {z3_op_nums[decl_num]}")
op_name = op_map[z3_op_nums[decl_num]]

if op_name == "BitVecVal":
Expand Down
10 changes: 5 additions & 5 deletions claripy/balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,18 @@ def _reverse_comparison(a):
try:
new_op = opposites[a.op]
except KeyError:
raise ClaripyBalancerError("unable to reverse comparison %s (missing from 'opposites')" % a.op)
raise ClaripyBalancerError(f"unable to reverse comparison {a.op} (missing from 'opposites')")

try:
op = getattr(operator, new_op) if new_op.startswith("__") else getattr(_all_operations, new_op)
except AttributeError:
raise ClaripyBalancerError("unable to reverse comparison %s (AttributeError)" % a.op)
raise ClaripyBalancerError(f"unable to reverse comparison {a.op} (AttributeError)")

try:
return op(*a.args[::-1])
except ClaripyOperationError:
# TODO: copy trace
raise ClaripyBalancerError("unable to reverse comparison %s (ClaripyOperationError)" % a.op)
raise ClaripyBalancerError(f"unable to reverse comparison {a.op} (ClaripyOperationError)")

def _align_bv(self, a):
if a.op in commutative_operations:
Expand Down Expand Up @@ -393,7 +393,7 @@ def _balance(self, truism):
return truism

try:
balancer = getattr(self, "_balance_%s" % inner_aligned.args[0].op)
balancer = getattr(self, f"_balance_{inner_aligned.args[0].op}")
except AttributeError:
l.debug("Balance handler %s is not found in balancer. Consider implementing.", truism.args[0].op)
return truism
Expand Down Expand Up @@ -615,7 +615,7 @@ def _handle(self, truism):
return

try:
handler = getattr(self, "_handle_%s" % truism.op)
handler = getattr(self, f"_handle_{truism.op}")
except AttributeError:
l.debug("No handler for operation %s", truism.op)
return
Expand Down
2 changes: 1 addition & 1 deletion claripy/frontend_mixins/debug_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def debugged(*args, **kwargs):
try:
r = f(*args, **kwargs)
except Exception as e: # pylint:disable=broad-except
r = "EXCEPTION: %s" % str(e)
r = f"EXCEPTION: {e!s}"
raise
finally:
call_depth -= 1
Expand Down
2 changes: 1 addition & 1 deletion claripy/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def __init__(self, value):
self.value = value

def __repr__(self):
return "StringV(%s)" % (self.value)
return f"StringV({self.value})"


def StrConcat(*args):
Expand Down
10 changes: 5 additions & 5 deletions claripy/vsa/discrete_strided_interval_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def operator(self, o=None):
return ret.normalize()

else:
raise ClaripyVSAOperationError("Unsupported operand type %s" % (type(o)))
raise ClaripyVSAOperationError(f"Unsupported operand type {type(o)}")

return operator

Expand Down Expand Up @@ -457,7 +457,7 @@ def union(self, b):
return b.union(self)

else:
raise ClaripyVSAOperationError("Unsupported operand type %s for operation union." % type(b))
raise ClaripyVSAOperationError(f"Unsupported operand type {type(b)} for operation union.")

def intersection(self, b):
if isinstance(b, DiscreteStridedIntervalSet):
Expand All @@ -467,7 +467,7 @@ def intersection(self, b):
return self._intersection_with_si(b)

else:
raise ClaripyVSAOperationError("Unsupported operand type %s for operation intersection." % type(b))
raise ClaripyVSAOperationError(f"Unsupported operand type {type(b)} for operation intersection.")

# Other operations

Expand Down Expand Up @@ -601,7 +601,7 @@ def _intersection_with_dsis(self, dsis):

def _update_bounds(self, val):
if not isinstance(val, StridedInterval):
raise ClaripyVSAOperationError("Unsupported operand type %s." % type(val))
raise ClaripyVSAOperationError(f"Unsupported operand type {type(val)}.")

if val._lower_bound is not None and (self._lower_bound is None or val.lower_bound < self._lower_bound):
self._lower_bound = val.lower_bound
Expand All @@ -611,6 +611,6 @@ def _update_bounds(self, val):

def _update_bits(self, val):
if not isinstance(val, StridedInterval):
raise ClaripyVSAOperationError("Unsupported operand type %s." % type(val))
raise ClaripyVSAOperationError(f"Unsupported operand type {type(val)}.")

self._bits = val.bits
10 changes: 5 additions & 5 deletions claripy/vsa/strided_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,10 @@ def __init__(
self._upper_bound = upper_bound if upper_bound is not None else (2**bits - 1)

if lower_bound is not None and not isinstance(lower_bound, numbers.Number):
raise ClaripyVSAError("'lower_bound' must be an int. %s is not supported." % type(lower_bound))
raise ClaripyVSAError(f"'lower_bound' must be an int. {type(lower_bound)} is not supported.")

if upper_bound is not None and not isinstance(upper_bound, numbers.Number):
raise ClaripyVSAError("'upper_bound' must be an int. %s is not supported." % type(upper_bound))
raise ClaripyVSAError(f"'upper_bound' must be an int. {type(upper_bound)} is not supported.")

self._reversed = False

Expand Down Expand Up @@ -1154,8 +1154,8 @@ def __repr__(self):
if self.is_empty:
s = "<%d>[EmptySI]" % (self._bits)
else:
lower_bound = self._lower_bound if isinstance(self._lower_bound, str) else "%#x" % self._lower_bound
upper_bound = self._upper_bound if isinstance(self._upper_bound, str) else "%#x" % self._upper_bound
lower_bound = self._lower_bound if isinstance(self._lower_bound, str) else f"{self._lower_bound:#x}"
upper_bound = self._upper_bound if isinstance(self._upper_bound, str) else f"{self._upper_bound:#x}"
s = "<%d>0x%x[%s, %s]%s" % (
self._bits,
self._stride,
Expand Down Expand Up @@ -3573,7 +3573,7 @@ def CreateStridedInterval(
return to_conv

if not isinstance(to_conv, (numbers.Number, BVV)):
raise ClaripyOperationError("Unsupported to_conv type %s" % type(to_conv))
raise ClaripyOperationError(f"Unsupported to_conv type {type(to_conv)}")

if stride is not None or lower_bound is not None or upper_bound is not None:
raise ClaripyOperationError("You cannot specify both to_conv and other parameters at the same time.")
Expand Down
Loading

0 comments on commit 13b00f5

Please sign in to comment.