Skip to content

Commit 64b04d0

Browse files
committed
Move get_attributes to objtype file
1 parent 9fa10c7 commit 64b04d0

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

vm/src/builtins.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::collections::HashMap;
44
use std::io::{self, Write};
55

66
use super::compile;
7-
use super::obj::objdict;
87
use super::obj::objstr;
98
use super::obj::objtype;
109
use super::objbool;
@@ -35,32 +34,8 @@ fn dir_locals(vm: &mut VirtualMachine) -> PyObjectRef {
3534

3635
fn dir_object(vm: &mut VirtualMachine, obj: &PyObjectRef) -> PyObjectRef {
3736
// Gather all members here:
38-
let mut members: Vec<String> = vec![];
39-
40-
// Get class attributes:
41-
let mut base_classes = objtype::base_classes(obj);
42-
base_classes.reverse();
43-
for bc in base_classes {
44-
if let PyObjectKind::Class {
45-
name: _,
46-
dict,
47-
mro: _,
48-
} = &bc.borrow().kind
49-
{
50-
let elements = objdict::get_elements(dict);
51-
for (name, _value) in elements {
52-
members.push(name.to_string());
53-
}
54-
}
55-
}
56-
57-
// Get instance attributes:
58-
if let PyObjectKind::Instance { dict } = &obj.borrow().kind {
59-
let elements = objdict::get_elements(dict);
60-
for (name, _value) in elements {
61-
members.push(name.to_string());
62-
}
63-
}
37+
let attributes = objtype::get_attributes(obj);
38+
let mut members: Vec<String> = attributes.into_iter().map(|(n, _o)| n).collect();
6439

6540
// Sort members:
6641
members.sort();

vm/src/obj/objtype.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::super::pyobject::{
55
use super::super::vm::VirtualMachine;
66
use super::objdict;
77
use super::objtype; // Required for arg_check! to use isinstance
8+
use std::collections::HashMap;
89

910
/*
1011
* The magical type type
@@ -165,6 +166,37 @@ pub fn get_attribute(vm: &mut VirtualMachine, obj: PyObjectRef, name: &str) -> P
165166
}
166167
}
167168

169+
pub fn get_attributes(obj: &PyObjectRef) -> HashMap<String, PyObjectRef> {
170+
// Gather all members here:
171+
let mut attributes: HashMap<String, PyObjectRef> = HashMap::new();
172+
173+
// Get class attributes:
174+
let mut base_classes = objtype::base_classes(obj);
175+
base_classes.reverse();
176+
for bc in base_classes {
177+
if let PyObjectKind::Class {
178+
name: _,
179+
dict,
180+
mro: _,
181+
} = &bc.borrow().kind
182+
{
183+
let elements = objdict::get_elements(dict);
184+
for (name, value) in elements {
185+
attributes.insert(name.to_string(), value.clone());
186+
}
187+
}
188+
}
189+
190+
// Get instance attributes:
191+
if let PyObjectKind::Instance { dict } = &obj.borrow().kind {
192+
let elements = objdict::get_elements(dict);
193+
for (name, value) in elements {
194+
attributes.insert(name.to_string(), value.clone());
195+
}
196+
}
197+
attributes
198+
}
199+
168200
fn take_next_base(
169201
mut bases: Vec<Vec<PyObjectRef>>,
170202
) -> Option<(PyObjectRef, Vec<Vec<PyObjectRef>>)> {

0 commit comments

Comments
 (0)