From 99aaaf7dba06d469b92f3507124cd47fcc9f010a Mon Sep 17 00:00:00 2001 From: Kangzhi Shi Date: Sat, 2 Jan 2021 08:02:00 +0200 Subject: [PATCH] impl Match.expand --- vm/src/stdlib/sre.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/vm/src/stdlib/sre.rs b/vm/src/stdlib/sre.rs index 0e875340ad..078ac1019a 100644 --- a/vm/src/stdlib/sre.rs +++ b/vm/src/stdlib/sre.rs @@ -12,7 +12,7 @@ mod _sre { use super::constants::SreFlag; use super::interp::{self, lower_ascii, lower_unicode, upper_unicode, State}; use crate::builtins::tuple::PyTupleRef; - use crate::builtins::{PyStrRef, PyTypeRef}; + use crate::builtins::{PyDictRef, PyStrRef, PyTypeRef}; use crate::function::{Args, OptionalArg}; use crate::pyobject::{ Either, IntoPyObject, PyCallable, PyObjectRef, PyRef, PyResult, PyValue, StaticType, @@ -84,7 +84,7 @@ mod _sre { flags: u16, code: PyObjectRef, groups: usize, - groupindex: HashMap, + groupindex: PyDictRef, indexgroup: PyObjectRef, vm: &VirtualMachine, ) -> PyResult { @@ -126,7 +126,7 @@ mod _sre { pub flags: SreFlag, pub code: Vec, pub groups: usize, - pub groupindex: HashMap, + pub groupindex: PyDictRef, pub indexgroup: Vec>, } @@ -207,6 +207,10 @@ mod _sre { fn flags(&self) -> u16 { self.flags.bits() } + #[pyproperty] + fn groupindex(&self) -> PyDictRef { + self.groupindex.clone() + } fn subx(&self, sub_args: SubArgs, vm: &VirtualMachine) -> PyResult { Err(vm.new_not_implemented_error("".to_owned())) @@ -310,6 +314,13 @@ mod _sre { self.get_index(group.unwrap_or(0), vm).map(|x| self.regs[x]) } + #[pymethod] + fn expand(zelf: PyRef, template: PyStrRef, vm: &VirtualMachine) -> PyResult { + let re = vm.import("re", &[], 0)?; + let func = vm.get_attribute(re, "_expand")?; + vm.invoke(&func, (zelf.pattern.clone(), zelf, template)) + } + #[pymethod] fn group(&self, args: Args, vm: &VirtualMachine) -> PyResult { let mut args = args.into_vec();