Skip to content

Commit a7f185c

Browse files
committed
Use Bound wherever possible
1 parent cccdcd6 commit a7f185c

File tree

2 files changed

+58
-45
lines changed

2 files changed

+58
-45
lines changed

claripy/ast/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def __new__(cls, op, args, add_variables=None, hash=None, **kwargs): # pylint:d
243243
cls,
244244
op,
245245
tuple(args),
246-
kwargs.pop("length", None),
246+
kwargs.pop("length", 1),
247247
frozenset(kwargs.pop("variables")),
248248
kwargs.pop("symbolic"),
249249
# annotations,

src/ast/base.rs

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ pub struct ASTCacheKey {
1717
#[pymethods]
1818
impl ASTCacheKey {
1919
#[new]
20-
pub fn new(py: Python, ast: PyObject) -> PyResult<Self> {
20+
pub fn new(ast: Bound<PyAny>) -> PyResult<Self> {
2121
Ok(ASTCacheKey {
22-
hash: ast.as_any().bind(py).hash()?,
23-
ast,
22+
hash: ast.as_any().hash()?,
23+
ast: ast.into(),
2424
})
2525
}
2626

@@ -45,7 +45,7 @@ pub struct Base {
4545
#[pyo3(get, set)]
4646
args: Py<PyTuple>,
4747
#[pyo3(get, set)]
48-
length: PyObject,
48+
length: usize,
4949
#[pyo3(get, set)]
5050
variables: PyObject, // TODO: This should be a HashSet, leave opaque for now
5151
#[pyo3(get, set)]
@@ -86,33 +86,48 @@ pub struct Base {
8686
#[pymethods]
8787
impl Base {
8888
#[new]
89-
#[pyo3(signature = (op, args, length, variables, symbolic, annotations=None, simplified=None, errored=None, eager_backends=None, uninitialized=None, uc_alloc_depth=None, encoded_name=None, depth=None, uneliminatable_annotations=None, relocatable_annotations=None))]
89+
#[pyo3(signature = (
90+
op,
91+
args,
92+
length,
93+
variables,
94+
symbolic,
95+
annotations=None,
96+
simplified=None,
97+
errored=None,
98+
eager_backends=None,
99+
uninitialized=None,
100+
uc_alloc_depth=None,
101+
encoded_name=None,
102+
depth=None,
103+
uneliminatable_annotations=None,
104+
relocatable_annotations=None
105+
))]
90106
fn new(
91107
py: Python,
92108
op: String,
93-
args: Py<PyTuple>,
94-
length: PyObject,
95-
variables: PyObject,
109+
args: Bound<PyTuple>,
110+
length: usize,
111+
variables: Bound<PyAny>,
96112
symbolic: bool,
97-
annotations: Option<Py<PyTuple>>,
113+
annotations: Option<Bound<PyTuple>>,
98114
// New stuff
99-
simplified: Option<PyObject>,
100-
errored: Option<Py<PySet>>,
101-
eager_backends: Option<PyObject>,
102-
uninitialized: Option<PyObject>,
103-
uc_alloc_depth: Option<PyObject>,
104-
encoded_name: Option<PyObject>,
115+
simplified: Option<Bound<PyAny>>,
116+
errored: Option<Bound<PySet>>,
117+
eager_backends: Option<Bound<PyAny>>,
118+
uninitialized: Option<Bound<PyAny>>,
119+
uc_alloc_depth: Option<Bound<PyAny>>,
120+
encoded_name: Option<Bound<PyAny>>,
105121
depth: Option<usize>,
106-
uneliminatable_annotations: Option<PyObject>,
107-
relocatable_annotations: Option<PyObject>,
122+
uneliminatable_annotations: Option<Bound<PyAny>>,
123+
relocatable_annotations: Option<Bound<PyAny>>,
108124
) -> PyResult<Self> {
109-
if args.bind(py).len() == 0 {
125+
if args.len() == 0 {
110126
return Err(PyValueError::new_err("AST with no arguments!")); // TODO: This should be a custom error
111127
}
112128

113129
let depth = depth.unwrap_or(
114130
*args
115-
.bind(py)
116131
.iter()
117132
.map(|arg| {
118133
arg.getattr("depth")
@@ -122,43 +137,41 @@ impl Base {
122137
.collect::<Result<Vec<usize>, PyErr>>()?
123138
.iter()
124139
.max()
125-
.unwrap_or(&0) + 1
140+
.unwrap_or(&0)
141+
+ 1,
126142
);
127143

128144
Ok(Base {
129145
op,
130-
args,
146+
args: args.into(),
131147
length,
132-
variables,
148+
variables: variables.into(),
133149
symbolic,
134-
annotations: annotations.unwrap_or_else(|| PyTuple::empty_bound(py).unbind()),
150+
annotations: annotations
151+
.unwrap_or_else(|| PyTuple::empty_bound(py))
152+
.into(),
135153

136154
depth,
137155

138156
_hash: None,
139-
_simplified: simplified,
157+
_simplified: simplified.map(|s| s.into()),
140158
_cache_key: None,
141-
_cached_encoded_name: encoded_name,
142-
_errored: errored.unwrap_or(
143-
// TODO: Is there really not an easier way to make a set?
144-
py.eval_bound("set()", None, None)?
145-
.downcast_into()?
146-
.unbind(),
147-
),
148-
_eager_backends: eager_backends,
159+
_cached_encoded_name: encoded_name.map(|s| s.into()),
160+
_errored: errored.unwrap_or(PySet::empty_bound(py)?).into(),
161+
_eager_backends: eager_backends.map(|s| s.into()),
149162
_excavated: None,
150163
_burrowed: None,
151-
_uninitialized: uninitialized,
152-
_uc_alloc_depth: uc_alloc_depth,
153-
_uneliminatable_annotations: uneliminatable_annotations,
154-
_relocatable_annotations: relocatable_annotations,
164+
_uninitialized: uninitialized.map(|s| s.into()),
165+
_uc_alloc_depth: uc_alloc_depth.map(|s| s.into()),
166+
_uneliminatable_annotations: uneliminatable_annotations.map(|s| s.into()),
167+
_relocatable_annotations: relocatable_annotations.map(|s| s.into()),
155168
})
156169
}
157170

158171
#[staticmethod]
159172
fn _arg_serialize<'py>(
160173
py: Python<'py>,
161-
arg: &Bound<'_, PyAny>,
174+
arg: Bound<'_, PyAny>,
162175
) -> PyResult<Option<Cow<'py, [u8]>>> {
163176
if arg.is_none() {
164177
return Ok(Some(Cow::from(vec![b'\x0f'])));
@@ -208,7 +221,7 @@ impl Base {
208221
if arg.is_instance(&py.get_type_bound::<PyTuple>())? {
209222
let mut result = Vec::new();
210223
for item in arg.downcast::<PyTuple>()?.iter() {
211-
if let Some(sub_result) = Self::_arg_serialize(py, &item)? {
224+
if let Some(sub_result) = Self::_arg_serialize(py, item)? {
212225
result.extend(sub_result.iter());
213226
} else {
214227
return Ok(None); // Do we really want to return None here?
@@ -223,16 +236,16 @@ impl Base {
223236
fn _ast_serialize<'py>(
224237
py: Python<'py>,
225238
op: String,
226-
args_tuple: &Bound<'_, PyTuple>,
227-
keywords: &Bound<'_, PyDict>, // TODO: This should be a struct or seperate args
239+
args_tuple: Bound<'_, PyTuple>,
240+
keywords: Bound<'_, PyDict>, // TODO: This should be a struct or seperate args
228241
) -> PyResult<Option<Cow<'py, [u8]>>> {
229-
let serailized_args = match Base::_arg_serialize(py, args_tuple)? {
242+
let serailized_args = match Base::_arg_serialize(py, args_tuple.into_any())? {
230243
Some(args) => args,
231244
None => return Ok(None),
232245
};
233246

234247
let length = match keywords.contains("length")? {
235-
true => match Base::_arg_serialize(py, &keywords.get_item("length")?.unwrap())? {
248+
true => match Base::_arg_serialize(py, keywords.get_item("length")?.unwrap())? {
236249
Some(length) => length,
237250
None => return Ok(None),
238251
},
@@ -268,7 +281,7 @@ impl Base {
268281
fn _calc_hash<'py>(
269282
py: Python<'py>,
270283
op: String,
271-
args: &Bound<PyTuple>,
284+
args: Bound<PyTuple>,
272285
keywords: Bound<PyDict>,
273286
) -> PyResult<isize> {
274287
let mut args_tuple = Vec::new();
@@ -299,7 +312,7 @@ impl Base {
299312
}
300313
}
301314

302-
let to_hash = match Base::_ast_serialize(py, op.clone(), &args, &keywords)? {
315+
let to_hash = match Base::_ast_serialize(py, op.clone(), args, keywords.clone())? {
303316
Some(to_hash) => to_hash,
304317
None => {
305318
let hash_tuple: Bound<PyTuple> = PyTuple::new_bound(

0 commit comments

Comments
 (0)