MCPcopy Index your code
hub / github.com/RustPython/RustPython / ast_reduce

Function ast_reduce

crates/vm/src/stdlib/_ast/python.rs:89–117  ·  view source on GitHub ↗
(zelf: PyObjectRef, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

87 }
88
89 pub(crate) fn ast_reduce(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
90 let dict = zelf.as_object().dict();
91 let cls = zelf.class();
92 let type_obj: PyObjectRef = cls.to_owned().into();
93
94 let Some(dict) = dict else {
95 return Ok(vm.ctx.new_tuple(vec![type_obj]));
96 };
97
98 let fields = cls.get_attr(vm.ctx.intern_str("_fields"));
99 if let Some(fields) = fields {
100 let fields: Vec<PyStrRef> = fields.try_to_value(vm)?;
101 let mut positional: Vec<PyObjectRef> = Vec::new();
102 for field in fields {
103 if dict.get_item_opt::<Wtf8>(field.as_wtf8(), vm)?.is_some() {
104 positional.push(vm.ctx.none());
105 } else {
106 break;
107 }
108 }
109 let args: PyObjectRef = vm.ctx.new_tuple(positional).into();
110 let dict_obj: PyObjectRef = dict.into();
111 return Ok(vm.ctx.new_tuple(vec![type_obj, args, dict_obj]));
112 }
113
114 Ok(vm
115 .ctx
116 .new_tuple(vec![type_obj, vm.ctx.new_tuple(vec![]).into(), dict.into()]))
117 }
118
119 pub(crate) fn ast_replace(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
120 if !args.args.is_empty() {

Callers 3

extend_classMethod · 0.85
__reduce__Method · 0.85
module_execFunction · 0.85

Calls 12

newFunction · 0.85
intern_strMethod · 0.80
try_to_valueMethod · 0.80
noneMethod · 0.80
dictMethod · 0.45
as_objectMethod · 0.45
classMethod · 0.45
to_ownedMethod · 0.45
new_tupleMethod · 0.45
get_attrMethod · 0.45
as_wtf8Method · 0.45
pushMethod · 0.45

Tested by

no test coverage detected