Skip to content

Commit 638af16

Browse files
committed
Process review comments
1 parent 7b005c7 commit 638af16

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

tests/snippets/with.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,24 @@ def __str__(self):
1515
ls.append(3)
1616
return "c'est moi!"
1717

18-
1918
with ContextManager() as c:
2019
print(c)
2120

2221
assert ls == [1, 3, 2]
22+
23+
ls = []
24+
class ContextManager2:
25+
def __enter__(self):
26+
print('Entrada')
27+
ls.append(1)
28+
return ls
29+
30+
def __exit__(self, exc_type, exc_val, exc_tb):
31+
ls.append(2)
32+
print('Wiedersehen')
33+
34+
with ContextManager2() as c:
35+
print(c)
36+
assert c == [1]
37+
38+
assert ls == [1, 2]

vm/src/vm.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,7 @@ impl VirtualMachine {
179179
// Assume top of stack is __exit__ method:
180180
// TODO: do we want to put the exit call on the stack?
181181
let exit_method = self.pop_value();
182-
let args = PyFuncArgs {
183-
args: vec![],
184-
kwargs: vec![],
185-
};
182+
let args = PyFuncArgs::default();
186183
// TODO: what happens when we got an error during handling exception?
187184
self.invoke(exit_method, args).unwrap();
188185
}
@@ -842,15 +839,15 @@ impl VirtualMachine {
842839
None
843840
}
844841
bytecode::Instruction::SetupWith { end } => {
845-
let obj = self.pop_value();
842+
let context_manager = self.pop_value();
846843
// Call enter:
847-
match self.call_method(obj, "__enter__", vec![]) {
848-
Ok(manager) => {
844+
match self.call_method(context_manager.clone(), "__enter__", vec![]) {
845+
Ok(obj) => {
849846
self.push_block(Block::With {
850847
end: *end,
851-
context_manager: manager.clone(),
848+
context_manager: context_manager.clone(),
852849
});
853-
self.push_value(manager);
850+
self.push_value(obj);
854851
None
855852
}
856853
Err(err) => Some(Err(err)),
@@ -866,6 +863,7 @@ impl VirtualMachine {
866863
assert!(end1 == end2);
867864

868865
// call exit now:
866+
// TODO: improve exception handling in context manager.
869867
let exc_type = self.ctx.none();
870868
let exc_val = self.ctx.none();
871869
let exc_tb = self.ctx.none();

0 commit comments

Comments
 (0)