Skip to content

Commit 976afed

Browse files
Add helper for obtaining class from module
1 parent fa9e48a commit 976afed

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

vm/src/stdlib/re.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct PyMatch {
112112

113113
impl PyValue for PyMatch {
114114
fn class(vm: &mut VirtualMachine) -> PyObjectRef {
115-
vm.import("re").unwrap().get_attr("Match").unwrap()
115+
vm.class("re", "Match")
116116
}
117117
}
118118

vm/src/vm.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ impl VirtualMachine {
103103
&frame.scope
104104
}
105105

106+
pub fn class(&mut self, module: &str, class: &str) -> PyObjectRef {
107+
self.import(module)
108+
.unwrap_or_else(|_| panic!("unable to import {}", module))
109+
.get_attr(class)
110+
.unwrap_or_else(|| panic!("module {} has no class {}", module, class))
111+
}
112+
106113
/// Create a new python string object.
107114
pub fn new_str(&self, s: String) -> PyObjectRef {
108115
self.ctx.new_str(s)

0 commit comments

Comments
 (0)