| 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() { |