|
2 | 2 |
|
3 | 3 | use super::*; |
4 | 4 | use crate::common::ascii; |
| 5 | +use crate::function::FuncArgs; |
| 6 | +use crate::types::Initializer; |
5 | 7 |
|
6 | 8 | macro_rules! impl_node { |
7 | 9 | ( |
@@ -462,12 +464,54 @@ impl_node!( |
462 | 464 | attributes: ["lineno", "col_offset", "end_lineno", "end_col_offset"], |
463 | 465 | ); |
464 | 466 |
|
465 | | -impl_node!( |
466 | | - #[pyclass(module = "_ast", name = "Constant", base = NodeExpr)] |
467 | | - pub(crate) struct NodeExprConstant, |
468 | | - fields: ["value", "kind"], |
469 | | - attributes: ["lineno", "col_offset", "end_lineno", "end_col_offset"], |
470 | | -); |
| 467 | +// NodeExprConstant needs custom Initializer to default kind to None |
| 468 | +#[pyclass(module = "_ast", name = "Constant", base = NodeExpr)] |
| 469 | +#[repr(transparent)] |
| 470 | +pub(crate) struct NodeExprConstant(NodeExpr); |
| 471 | + |
| 472 | +#[pyclass(flags(HAS_DICT, BASETYPE), with(Initializer))] |
| 473 | +impl NodeExprConstant { |
| 474 | + #[extend_class] |
| 475 | + fn extend_class_with_fields(ctx: &Context, class: &'static Py<PyType>) { |
| 476 | + class.set_attr( |
| 477 | + identifier!(ctx, _fields), |
| 478 | + ctx.new_tuple(vec![ |
| 479 | + ctx.new_str(ascii!("value")).into(), |
| 480 | + ctx.new_str(ascii!("kind")).into(), |
| 481 | + ]) |
| 482 | + .into(), |
| 483 | + ); |
| 484 | + |
| 485 | + class.set_attr( |
| 486 | + identifier!(ctx, _attributes), |
| 487 | + ctx.new_list(vec![ |
| 488 | + ctx.new_str(ascii!("lineno")).into(), |
| 489 | + ctx.new_str(ascii!("col_offset")).into(), |
| 490 | + ctx.new_str(ascii!("end_lineno")).into(), |
| 491 | + ctx.new_str(ascii!("end_col_offset")).into(), |
| 492 | + ]) |
| 493 | + .into(), |
| 494 | + ); |
| 495 | + } |
| 496 | +} |
| 497 | + |
| 498 | +impl Initializer for NodeExprConstant { |
| 499 | + type Args = FuncArgs; |
| 500 | + |
| 501 | + fn slot_init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> { |
| 502 | + <NodeAst as Initializer>::slot_init(zelf.clone(), args, vm)?; |
| 503 | + // kind defaults to None if not provided |
| 504 | + let dict = zelf.as_object().dict().unwrap(); |
| 505 | + if !dict.contains_key("kind", vm) { |
| 506 | + dict.set_item("kind", vm.ctx.none(), vm)?; |
| 507 | + } |
| 508 | + Ok(()) |
| 509 | + } |
| 510 | + |
| 511 | + fn init(_zelf: PyRef<Self>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<()> { |
| 512 | + unreachable!("slot_init is defined") |
| 513 | + } |
| 514 | +} |
471 | 515 |
|
472 | 516 | impl_node!( |
473 | 517 | #[pyclass(module = "_ast", name = "Attribute", base = NodeExpr)] |
|
0 commit comments