Skip to content

Commit 416e48b

Browse files
authored
Merge pull request RustPython#134 from RustPython/objtyp
Initial attempt at ast builtin module
2 parents ba78000 + da7745c commit 416e48b

File tree

5 files changed

+365
-14
lines changed

5 files changed

+365
-14
lines changed

tests/snippets/ast_snippet.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
import ast
3+
print(ast)
4+
5+
source = """
6+
def foo():
7+
print('bar')
8+
"""
9+
n = ast.parse(source)
10+
print(n)
11+
print(n.body)
12+
print(n.body[0].name)
13+
assert n.body[0].name == 'foo'
14+
print(n.body[0].body)
15+
print(n.body[0].body[0])
16+
print(n.body[0].body[0].value.func.id)
17+
assert n.body[0].body[0].value.func.id == 'print'

vm/src/import.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::path::PathBuf;
1010

1111
use self::rustpython_parser::parser;
1212
use super::compile;
13-
use super::pyobject::{DictProtocol, PyObject, PyObjectKind, PyResult};
13+
use super::pyobject::{DictProtocol, PyObjectKind, PyResult};
1414
use super::vm::VirtualMachine;
1515

1616
fn import_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
@@ -50,28 +50,23 @@ fn import_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
5050
};
5151

5252
let builtins = vm.get_builtin_scope();
53-
let scope = vm.context().new_scope(Some(builtins));
53+
let scope = vm.ctx.new_scope(Some(builtins));
5454

5555
match vm.run_code_obj(code_obj, scope.clone()) {
5656
Ok(_) => {}
5757
Err(value) => return Err(value),
5858
}
59-
Ok(scope)
59+
let py_module = vm.ctx.new_module(module, scope);
60+
Ok(py_module)
6061
}
6162

62-
pub fn import(vm: &mut VirtualMachine, module: &String, symbol: &Option<String>) -> PyResult {
63-
let scope = import_module(vm, module)?;
63+
pub fn import(vm: &mut VirtualMachine, module_name: &String, symbol: &Option<String>) -> PyResult {
64+
let module = import_module(vm, module_name)?;
6465
// If we're importing a symbol, look it up and use it, otherwise construct a module and return
6566
// that
6667
let obj = match symbol {
67-
Some(symbol) => scope.get_item(symbol).unwrap(),
68-
None => PyObject::new(
69-
PyObjectKind::Module {
70-
name: module.clone(),
71-
dict: scope.clone(),
72-
},
73-
vm.get_type(),
74-
),
68+
Some(symbol) => module.get_item(symbol).unwrap(),
69+
None => module,
7570
};
7671
Ok(obj)
7772
}

vm/src/pyobject.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub struct PyContext {
6262
pub tuple_type: PyObjectRef,
6363
pub str_type: PyObjectRef,
6464
pub function_type: PyObjectRef,
65+
pub module_type: PyObjectRef,
6566
pub bound_method_type: PyObjectRef,
6667
pub member_descriptor_type: PyObjectRef,
6768
pub object: PyObjectRef,
@@ -111,6 +112,7 @@ impl PyContext {
111112
objobject::create_object(type_type.clone(), object_type.clone(), dict_type.clone());
112113
objdict::create_type(type_type.clone(), object_type.clone(), dict_type.clone());
113114

115+
let module_type = create_type("module", &type_type, &object_type, &dict_type);
114116
let function_type = create_type("function", &type_type, &object_type, &dict_type);
115117
let bound_method_type = create_type("method", &type_type, &object_type, &dict_type);
116118
let member_descriptor_type =
@@ -145,6 +147,7 @@ impl PyContext {
145147
str_type: str_type,
146148
object: object_type,
147149
function_type: function_type,
150+
module_type: module_type,
148151
bound_method_type: bound_method_type,
149152
member_descriptor_type: member_descriptor_type,
150153
type_type: type_type,
@@ -213,6 +216,15 @@ impl PyContext {
213216
self.object.clone()
214217
}
215218

219+
pub fn new_object(&self) -> PyObjectRef {
220+
PyObject::new(
221+
PyObjectKind::Instance {
222+
dict: self.new_dict(),
223+
},
224+
self.object(),
225+
)
226+
}
227+
216228
pub fn new_int(&self, i: i32) -> PyObjectRef {
217229
PyObject::new(PyObjectKind::Integer { value: i }, self.int_type())
218230
}
@@ -275,7 +287,7 @@ impl PyContext {
275287
name: name.clone(),
276288
dict: scope.clone(),
277289
},
278-
self.type_type(),
290+
self.module_type.clone(),
279291
)
280292
}
281293

0 commit comments

Comments
 (0)