|
1 | 1 | use super::pystr::IntoPyStrRef; |
2 | 2 | use super::{PyDictRef, PyStr, PyStrRef, PyType, PyTypeRef}; |
3 | 3 | use crate::{ |
4 | | - builtins::PyStrInterned, |
| 4 | + builtins::{pystr::AsPyStr, PyStrInterned}, |
5 | 5 | class::PyClassImpl, |
6 | 6 | convert::ToPyObject, |
7 | 7 | function::FuncArgs, |
@@ -30,29 +30,24 @@ impl PyModule { |
30 | 30 | // pub(crate) fn new(d: PyDictRef) -> Self { |
31 | 31 | // PyModule { dict: d.into() } |
32 | 32 | // } |
| 33 | +} |
33 | 34 |
|
34 | | - // #[inline] |
35 | | - // pub fn dict(&self) -> PyDictRef { |
36 | | - // self.dict.get() |
37 | | - // } |
38 | | - |
39 | | - fn getattr_inner(zelf: &Py<Self>, name: PyStrRef, vm: &VirtualMachine) -> PyResult { |
40 | | - if let Some(attr) = zelf.as_object().generic_getattr_opt(&name, None, vm)? { |
| 35 | +impl Py<PyModule> { |
| 36 | + fn getattr_inner(&self, name: &Py<PyStr>, vm: &VirtualMachine) -> PyResult { |
| 37 | + if let Some(attr) = self.as_object().generic_getattr_opt(name, None, vm)? { |
41 | 38 | return Ok(attr); |
42 | 39 | } |
43 | | - if let Ok(getattr) = zelf.dict().get_item(identifier!(vm, __getattr__), vm) { |
44 | | - return getattr.call((name,), vm); |
| 40 | + if let Ok(getattr) = self.dict().get_item(identifier!(vm, __getattr__), vm) { |
| 41 | + return getattr.call((name.to_owned(),), vm); |
45 | 42 | } |
46 | | - let module_name = if let Some(name) = zelf.name(vm) { |
| 43 | + let module_name = if let Some(name) = self.name(vm) { |
47 | 44 | format!(" '{name}'") |
48 | 45 | } else { |
49 | 46 | "".to_owned() |
50 | 47 | }; |
51 | 48 | Err(vm.new_attribute_error(format!("module{module_name} has no attribute '{name}'"))) |
52 | 49 | } |
53 | | -} |
54 | 50 |
|
55 | | -impl Py<PyModule> { |
56 | 51 | fn name(&self, vm: &VirtualMachine) -> Option<PyStrRef> { |
57 | 52 | let name = self |
58 | 53 | .as_object() |
@@ -85,9 +80,10 @@ impl Py<PyModule> { |
85 | 80 | .expect("Failed to set __spec__ on module"); |
86 | 81 | } |
87 | 82 |
|
88 | | - pub fn get_attr(&self, attr_name: impl IntoPyStrRef, vm: &VirtualMachine) -> PyResult { |
89 | | - PyModule::getattr_inner(self, attr_name.into_pystr_ref(vm), vm) |
| 83 | + pub fn get_attr<'a>(&self, attr_name: impl AsPyStr<'a>, vm: &VirtualMachine) -> PyResult { |
| 84 | + self.getattr_inner(attr_name.as_pystr(&vm.ctx), vm) |
90 | 85 | } |
| 86 | + |
91 | 87 | pub fn set_attr( |
92 | 88 | &self, |
93 | 89 | attr_name: impl IntoPyStrRef, |
@@ -135,8 +131,8 @@ impl Initializer for PyModule { |
135 | 131 | } |
136 | 132 |
|
137 | 133 | impl GetAttr for PyModule { |
138 | | - fn getattro(zelf: &Py<Self>, name: PyStrRef, vm: &VirtualMachine) -> PyResult { |
139 | | - Self::getattr_inner(zelf, name, vm) |
| 134 | + fn getattro(zelf: &Py<Self>, name: &Py<PyStr>, vm: &VirtualMachine) -> PyResult { |
| 135 | + zelf.getattr_inner(name, vm) |
140 | 136 | } |
141 | 137 | } |
142 | 138 |
|
|
0 commit comments