Remove usage of py_module macro from ast.
use crate::{
builtins::{self, PyStrRef, PyTypeRef},
- function::FuncArgs,
IdProtocol, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyResult, PyValue, StaticType,
TryFromObject, TypeProtocol, VirtualMachine,
};
@@ -22,22 +21,13 @@ use rustpython_parser::parser;
mod gen;
-fn get_node_field(vm: &VirtualMachine, obj: &PyObjectRef, field: &str, typ: &str) -> PyResult {
- vm.get_attribute_opt(obj.clone(), field)?.ok_or_else(|| {
- vm.new_type_error(format!("required field \"{}\" missing from {}", field, typ))
- })
-}
-
-fn get_node_field_opt(
- vm: &VirtualMachine,
- obj: &PyObjectRef,
- field: &str,
-) -> PyResult<Option<PyObjectRef>> {
- Ok(vm
- .get_attribute_opt(obj.clone(), field)?
- .filter(|obj| !vm.is_none(obj)))
-}
-
+#[pymodule]
+mod _ast {
+ use crate::{
+ builtins::PyStrRef, function::FuncArgs, PyObjectRef, PyResult, PyValue, TypeProtocol,
+ VirtualMachine,
+ };
+ #[pyattr]
#[pyclass(module = "_ast", name = "AST")]
#[derive(Debug, PyValue)]
pub(crate) struct AstNode;
@@ -77,8 +67,25 @@ impl AstNode {
}
}
-const MODULE_NAME: &str = "_ast";
-pub const PY_COMPILE_FLAG_AST_ONLY: i32 = 0x0400;
+ #[pyattr(name = "PyCF_ONLY_AST")]
+ use super::PY_COMPILE_FLAG_AST_ONLY;
+}
+
+fn get_node_field(vm: &VirtualMachine, obj: &PyObjectRef, field: &str, typ: &str) -> PyResult {
+ vm.get_attribute_opt(obj.clone(), field)?.ok_or_else(|| {
+ vm.new_type_error(format!("required field \"{}\" missing from {}", field, typ))
+ })
+}
+
+fn get_node_field_opt(
+ vm: &VirtualMachine,
+ obj: &PyObjectRef,
+ field: &str,
+) -> PyResult<Option<PyObjectRef>> {
+ Ok(vm
+ .get_attribute_opt(obj.clone(), field)?
+ .filter(|obj| !vm.is_none(obj)))
+}
trait Node: Sized {
fn ast_to_object(self, vm: &VirtualMachine) -> PyObjectRef;
@@ -276,15 +283,13 @@ pub(crate) fn compile(
Ok(vm.new_code_object(code).into())
}
-pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
- let ctx = &vm.ctx;
+// Required crate visibility for inclusion by gen.rs
+pub(crate) use _ast::AstNode;
+// Used by builtins::compile()
+pub const PY_COMPILE_FLAG_AST_ONLY: i32 = 0x0400;
- let ast_base = AstNode::make_class(ctx);
- let module = py_module!(vm, MODULE_NAME, {
- // TODO: There's got to be a better way!
- "AST" => ast_base,
- "PyCF_ONLY_AST" => ctx.new_int(PY_COMPILE_FLAG_AST_ONLY),
- });
+pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
+ let module = _ast::make_module(vm);
gen::extend_module_nodes(vm, &module);
module
}
