Skip to content

Commit 213ffd9

Browse files
committed
move dir in builtins to vm
Signed-off-by: snowapril <[email protected]>
1 parent 367b258 commit 213ffd9

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

vm/src/stdlib/builtins.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,7 @@ mod builtins {
191191

192192
#[pyfunction]
193193
fn dir(obj: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult<PyList> {
194-
let seq = match obj {
195-
OptionalArg::Present(obj) => vm
196-
.get_special_method(obj, "__dir__")?
197-
.map_err(|_obj| vm.new_type_error("object does not provide __dir__".to_owned()))?
198-
.invoke((), vm)?,
199-
OptionalArg::Missing => vm.call_method(vm.current_locals()?.as_object(), "keys", ())?,
200-
};
201-
let sorted = sorted(seq, Default::default(), vm)?;
202-
Ok(sorted)
194+
vm.dir(obj.into_option())
203195
}
204196

205197
#[pyfunction]

vm/src/vm.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,20 @@ impl VirtualMachine {
11741174
.invoke(args, self)
11751175
}
11761176

1177+
pub fn dir(&self, obj: Option<PyObjectRef>) -> PyResult<PyList> {
1178+
let seq = match obj {
1179+
Some(obj) => self
1180+
.get_special_method(obj, "__dir__")?
1181+
.map_err(|_obj| self.new_type_error("object does not provide __dir__".to_owned()))?
1182+
.invoke((), self)?,
1183+
None => self.call_method(self.current_locals()?.as_object(), "keys", ())?,
1184+
};
1185+
let items = self.extract_elements(&seq)?;
1186+
let lst = PyList::from(items);
1187+
lst.sort(Default::default(), self)?;
1188+
Ok(lst)
1189+
}
1190+
11771191
#[inline]
11781192
pub(crate) fn get_special_method(
11791193
&self,

0 commit comments

Comments
 (0)