Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fix AST
  • Loading branch information
youknowone committed Jul 21, 2025
commit 09c340af09fb5b648eda421b994ecea88776c1cd
2 changes: 2 additions & 0 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,8 @@ class S(str):
with assertNumDeprecated():
self.assertNotIsInstance(Constant(S("42")), Num)

# TODO: RUSTPYTHON; will be removed in Python 3.14
@unittest.expectedFailure
def test_constant_subclasses_deprecated(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "", DeprecationWarning)
Expand Down
33 changes: 31 additions & 2 deletions vm/src/stdlib/ast/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ use super::{PY_CF_OPTIMIZED_AST, PY_CF_TYPE_COMMENTS, PY_COMPILE_FLAG_AST_ONLY};
pub(crate) mod _ast {
use crate::{
AsObject, Context, PyObjectRef, PyPayload, PyResult, VirtualMachine,
builtins::{PyStrRef, PyTupleRef},
builtins::{PyStrRef, PyTupleRef, PyTypeRef},
function::FuncArgs,
types::Constructor,
};
#[pyattr]
#[pyclass(module = "_ast", name = "AST")]
#[derive(Debug, PyPayload)]
pub(crate) struct NodeAst;

#[pyclass(flags(BASETYPE, HAS_DICT))]
#[pyclass(with(Constructor), flags(BASETYPE, HAS_DICT))]
impl NodeAst {
#[pyslot]
#[pymethod]
Expand Down Expand Up @@ -52,6 +53,34 @@ pub(crate) mod _ast {
}
}

impl Constructor for NodeAst {
type Args = FuncArgs;

fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
// AST nodes accept extra arguments (unlike object.__new__)
// This matches CPython's behavior where AST has its own tp_new
let dict = if cls
.slots
.flags
.contains(crate::types::PyTypeFlags::HAS_DICT)
{
Some(vm.ctx.new_dict())
} else {
None
};
let zelf = vm.ctx.new_base_object(cls, dict);

// Initialize the instance with the provided arguments
NodeAst::__init__(zelf.clone(), args, vm)?;

Ok(zelf)
}

fn py_new(_cls: PyTypeRef, _args: Self::Args, _vm: &VirtualMachine) -> PyResult {
unreachable!("slow_new is implemented");
}
}

#[pyattr(name = "PyCF_ONLY_AST")]
use super::PY_COMPILE_FLAG_AST_ONLY;

Expand Down
Loading