Skip to content

Commit 09c340a

Browse files
committed
Fix AST
1 parent 607212e commit 09c340a

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,8 @@ class S(str):
706706
with assertNumDeprecated():
707707
self.assertNotIsInstance(Constant(S("42")), Num)
708708

709+
# TODO: RUSTPYTHON; will be removed in Python 3.14
710+
@unittest.expectedFailure
709711
def test_constant_subclasses_deprecated(self):
710712
with warnings.catch_warnings():
711713
warnings.filterwarnings("ignore", "", DeprecationWarning)

vm/src/stdlib/ast/python.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ use super::{PY_CF_OPTIMIZED_AST, PY_CF_TYPE_COMMENTS, PY_COMPILE_FLAG_AST_ONLY};
44
pub(crate) mod _ast {
55
use crate::{
66
AsObject, Context, PyObjectRef, PyPayload, PyResult, VirtualMachine,
7-
builtins::{PyStrRef, PyTupleRef},
7+
builtins::{PyStrRef, PyTupleRef, PyTypeRef},
88
function::FuncArgs,
9+
types::Constructor,
910
};
1011
#[pyattr]
1112
#[pyclass(module = "_ast", name = "AST")]
1213
#[derive(Debug, PyPayload)]
1314
pub(crate) struct NodeAst;
1415

15-
#[pyclass(flags(BASETYPE, HAS_DICT))]
16+
#[pyclass(with(Constructor), flags(BASETYPE, HAS_DICT))]
1617
impl NodeAst {
1718
#[pyslot]
1819
#[pymethod]
@@ -52,6 +53,34 @@ pub(crate) mod _ast {
5253
}
5354
}
5455

56+
impl Constructor for NodeAst {
57+
type Args = FuncArgs;
58+
59+
fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
60+
// AST nodes accept extra arguments (unlike object.__new__)
61+
// This matches CPython's behavior where AST has its own tp_new
62+
let dict = if cls
63+
.slots
64+
.flags
65+
.contains(crate::types::PyTypeFlags::HAS_DICT)
66+
{
67+
Some(vm.ctx.new_dict())
68+
} else {
69+
None
70+
};
71+
let zelf = vm.ctx.new_base_object(cls, dict);
72+
73+
// Initialize the instance with the provided arguments
74+
NodeAst::__init__(zelf.clone(), args, vm)?;
75+
76+
Ok(zelf)
77+
}
78+
79+
fn py_new(_cls: PyTypeRef, _args: Self::Args, _vm: &VirtualMachine) -> PyResult {
80+
unreachable!("slow_new is implemented");
81+
}
82+
}
83+
5584
#[pyattr(name = "PyCF_ONLY_AST")]
5685
use super::PY_COMPILE_FLAG_AST_ONLY;
5786

0 commit comments

Comments
 (0)