Skip to content

Commit 2c8657c

Browse files
committed
Add load_cell to NameProtocol
1 parent 84e89d3 commit 2c8657c

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

vm/src/frame.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,6 @@ impl Scope {
106106
}
107107
}
108108

109-
pub fn get(&self, name: &str) -> Option<PyObjectRef> {
110-
for dict in self.locals.iter() {
111-
if let Some(value) = dict.get_item(name) {
112-
return Some(value);
113-
}
114-
}
115-
116-
if let Some(value) = self.globals.get_item(name) {
117-
return Some(value);
118-
}
119-
120-
None
121-
}
122-
123109
pub fn get_only_locals(&self) -> Option<PyObjectRef> {
124110
self.locals.iter().next().cloned()
125111
}
@@ -140,15 +126,31 @@ pub trait NameProtocol {
140126
fn load_name(&self, vm: &VirtualMachine, name: &str) -> Option<PyObjectRef>;
141127
fn store_name(&self, vm: &VirtualMachine, name: &str, value: PyObjectRef);
142128
fn delete_name(&self, vm: &VirtualMachine, name: &str);
129+
fn load_cell(&self, vm: &VirtualMachine, name: &str) -> Option<PyObjectRef>;
143130
}
144131

145132
impl NameProtocol for Scope {
146133
fn load_name(&self, vm: &VirtualMachine, name: &str) -> Option<PyObjectRef> {
147-
if let Some(value) = self.get(name) {
148-
Some(value)
149-
} else {
150-
vm.builtins.get_item(name)
134+
for dict in self.locals.iter() {
135+
if let Some(value) = dict.get_item(name) {
136+
return Some(value);
137+
}
138+
}
139+
140+
if let Some(value) = self.globals.get_item(name) {
141+
return Some(value);
151142
}
143+
144+
vm.builtins.get_item(name)
145+
}
146+
147+
fn load_cell(&self, _vm: &VirtualMachine, name: &str) -> Option<PyObjectRef> {
148+
for dict in self.locals.iter().skip(1) {
149+
if let Some(value) = dict.get_item(name) {
150+
return Some(value);
151+
}
152+
}
153+
None
152154
}
153155

154156
fn store_name(&self, vm: &VirtualMachine, key: &str, value: PyObjectRef) {

vm/src/obj/objsuper.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ https://github.com/python/cpython/blob/50b48572d9a90c5bb36e2bef6179548ea927a35a/
66
77
*/
88

9+
use crate::frame::NameProtocol;
910
use crate::function::PyFuncArgs;
1011
use crate::obj::objstr;
1112
use crate::obj::objtype::PyClass;
@@ -105,7 +106,7 @@ fn super_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
105106
let py_type = if let Some(ty) = py_type {
106107
ty.clone()
107108
} else {
108-
match vm.current_scope().get("__class__") {
109+
match vm.current_scope().load_cell(vm, "__class__") {
109110
Some(obj) => obj.clone(),
110111
_ => {
111112
return Err(vm.new_type_error(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy