Skip to content

Commit a8dbe2c

Browse files
committed
Address review: interactive print nesting, locals_to_fast flag, trace arg
- Skip interactive print for expression statements inside compound blocks (if/for/while/with/try) by checking in_conditional_block - Move locals_dirty flag clear to after sync loop succeeds - Pass callable object as arg to c_call/c_return/c_exception trace events
1 parent d7a616d commit a8dbe2c

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

crates/codegen/src/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,7 @@ impl Compiler {
21562156
ast::Stmt::Expr(ast::StmtExpr { value, .. }) => {
21572157
self.compile_expression(value)?;
21582158

2159-
if self.interactive && !self.ctx.in_func() && !self.ctx.in_class {
2159+
if self.interactive && !self.ctx.in_func() && !self.ctx.in_class && self.current_code_info().in_conditional_block == 0 {
21602160
emit!(
21612161
self,
21622162
Instruction::CallIntrinsic1 {

crates/vm/src/frame.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ impl Frame {
264264
if !self.locals_dirty.load(atomic::Ordering::Acquire) {
265265
return Ok(());
266266
}
267-
self.locals_dirty.store(false, atomic::Ordering::Release);
268267
let code = &**self.code;
269268
let mut fastlocals = self.fastlocals.lock();
270269
for (i, &varname) in code.varnames.iter().enumerate() {
@@ -277,6 +276,7 @@ impl Frame {
277276
Err(e) => return Err(e),
278277
}
279278
}
279+
self.locals_dirty.store(false, atomic::Ordering::Release);
280280
Ok(())
281281
}
282282

@@ -475,7 +475,7 @@ impl ExecutingFrame<'_> {
475475
&& loc.line.get() != prev_line
476476
{
477477
prev_line = loc.line.get();
478-
vm.trace_event(crate::protocol::TraceEvent::Line)?;
478+
vm.trace_event(crate::protocol::TraceEvent::Line, None)?;
479479
}
480480
self.update_lasti(|i| *i += 1);
481481
let bytecode::CodeUnit { op, arg } = instructions[idx];

crates/vm/src/protocol/callable.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
builtins::{PyBoundMethod, PyFunction},
33
function::{FuncArgs, IntoFuncArgs},
44
types::GenericMethod,
5-
{AsObject, PyObject, PyResult, VirtualMachine},
5+
{AsObject, PyObject, PyObjectRef, PyResult, VirtualMachine},
66
};
77

88
impl PyObject {
@@ -57,12 +57,13 @@ impl<'a> PyCallable<'a> {
5757
if is_python_callable {
5858
(self.call)(self.obj, args, vm)
5959
} else {
60-
vm.trace_event(TraceEvent::CCall)?;
60+
let callable = self.obj.to_owned();
61+
vm.trace_event(TraceEvent::CCall, Some(callable.clone()))?;
6162
let result = (self.call)(self.obj, args, vm);
6263
if result.is_ok() {
63-
vm.trace_event(TraceEvent::CReturn)?;
64+
vm.trace_event(TraceEvent::CReturn, Some(callable))?;
6465
} else {
65-
let _ = vm.trace_event(TraceEvent::CException);
66+
let _ = vm.trace_event(TraceEvent::CException, Some(callable));
6667
}
6768
result
6869
}
@@ -96,14 +97,18 @@ impl core::fmt::Display for TraceEvent {
9697
impl VirtualMachine {
9798
/// Call registered trace function.
9899
#[inline]
99-
pub(crate) fn trace_event(&self, event: TraceEvent) -> PyResult<()> {
100+
pub(crate) fn trace_event(
101+
&self,
102+
event: TraceEvent,
103+
arg: Option<PyObjectRef>,
104+
) -> PyResult<()> {
100105
if self.use_tracing.get() {
101-
self._trace_event_inner(event)
106+
self._trace_event_inner(event, arg)
102107
} else {
103108
Ok(())
104109
}
105110
}
106-
fn _trace_event_inner(&self, event: TraceEvent) -> PyResult<()> {
111+
fn _trace_event_inner(&self, event: TraceEvent, arg: Option<PyObjectRef>) -> PyResult<()> {
107112
let trace_func = self.trace_func.borrow().to_owned();
108113
let profile_func = self.profile_func.borrow().to_owned();
109114
if self.is_none(&trace_func) && self.is_none(&profile_func) {
@@ -117,7 +122,7 @@ impl VirtualMachine {
117122

118123
let frame = frame_ref.unwrap().as_object().to_owned();
119124
let event = self.ctx.new_str(event.to_string()).into();
120-
let args = vec![frame, event, self.ctx.none()];
125+
let args = vec![frame, event, arg.unwrap_or_else(|| self.ctx.none())];
121126

122127
// temporarily disable tracing, during the call to the
123128
// tracing function itself.

crates/vm/src/vm/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ impl VirtualMachine {
10211021
use crate::protocol::TraceEvent;
10221022
// Fire 'call' trace event after pushing frame
10231023
// (current_frame() now returns the callee's frame)
1024-
let result = match self.trace_event(TraceEvent::Call) {
1024+
let result = match self.trace_event(TraceEvent::Call, None) {
10251025
Ok(()) => {
10261026
// Set per-frame trace function so line events fire for this frame.
10271027
// Frames entered before sys.settrace() keep trace=None and skip line events.
@@ -1034,7 +1034,7 @@ impl VirtualMachine {
10341034
let result = f(frame);
10351035
// Fire 'return' trace event on success
10361036
if result.is_ok() {
1037-
let _ = self.trace_event(TraceEvent::Return);
1037+
let _ = self.trace_event(TraceEvent::Return, None);
10381038
}
10391039
result
10401040
}

0 commit comments

Comments
 (0)