Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,11 @@ jobs:

- run: ruff format --check

- name: run update_lib tests
run: cargo run -- -m unittest discover -s scripts/update_lib/tests -v
env:
PYTHONPATH: scripts

- name: install prettier
run: yarn global add prettier && echo "$(yarn global bin)" >>$GITHUB_PATH

Expand Down
56 changes: 50 additions & 6 deletions crates/vm/src/stdlib/ast/pyast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use super::*;
use crate::common::ascii;
use crate::function::FuncArgs;
use crate::types::Initializer;

macro_rules! impl_node {
(
Expand Down Expand Up @@ -462,12 +464,54 @@ impl_node!(
attributes: ["lineno", "col_offset", "end_lineno", "end_col_offset"],
);

impl_node!(
#[pyclass(module = "_ast", name = "Constant", base = NodeExpr)]
pub(crate) struct NodeExprConstant,
fields: ["value", "kind"],
attributes: ["lineno", "col_offset", "end_lineno", "end_col_offset"],
);
// NodeExprConstant needs custom Initializer to default kind to None
#[pyclass(module = "_ast", name = "Constant", base = NodeExpr)]
#[repr(transparent)]
pub(crate) struct NodeExprConstant(NodeExpr);

#[pyclass(flags(HAS_DICT, BASETYPE), with(Initializer))]
impl NodeExprConstant {
#[extend_class]
fn extend_class_with_fields(ctx: &Context, class: &'static Py<PyType>) {
class.set_attr(
identifier!(ctx, _fields),
ctx.new_tuple(vec![
ctx.new_str(ascii!("value")).into(),
ctx.new_str(ascii!("kind")).into(),
])
.into(),
);

class.set_attr(
identifier!(ctx, _attributes),
ctx.new_list(vec![
ctx.new_str(ascii!("lineno")).into(),
ctx.new_str(ascii!("col_offset")).into(),
ctx.new_str(ascii!("end_lineno")).into(),
ctx.new_str(ascii!("end_col_offset")).into(),
])
.into(),
);
}
}

impl Initializer for NodeExprConstant {
type Args = FuncArgs;

fn slot_init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
<NodeAst as Initializer>::slot_init(zelf.clone(), args, vm)?;
// kind defaults to None if not provided
let dict = zelf.as_object().dict().unwrap();
if !dict.contains_key("kind", vm) {
dict.set_item("kind", vm.ctx.none(), vm)?;
}
Ok(())
}

fn init(_zelf: PyRef<Self>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<()> {
unreachable!("slot_init is defined")
}
}

impl_node!(
#[pyclass(module = "_ast", name = "Attribute", base = NodeExpr)]
Expand Down
1 change: 1 addition & 0 deletions crates/vm/src/stdlib/ast/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub(crate) mod _ast {
}
zelf.set_attr(vm.ctx.intern_str(key), value, vm)?;
}

Ok(())
}

Expand Down
Loading