Skip to content

Commit

Permalink
Merge pull request #225 from benfred/fix_dump_locals
Browse files Browse the repository at this point in the history
Fix dump --locals
  • Loading branch information
benfred authored Feb 16, 2020
2 parents dec23b9 + 90f90a5 commit 26a20fc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/python_data_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,14 @@ const PY_TPFLAGS_STRING_SUBCLASS: usize = 1 << 28;
const PY_TPFLAGS_DICT_SUBCLASS: usize = 1 << 29;

/// Converts a python variable in the other process to a human readable string
/// similar to stringify_pyobject - but has knows the type of the python interpreter
pub fn format_variable<I>(process: &remoteprocess::Process, version: &Version, addr: usize, max_length: isize)
-> Result<String, Error> where I: InterpreterState {
// We need at least 5 characters remaining for all this code to work, replace with an ellipsis if
// we're out of space
if max_length <= 5 {
return Ok("...".to_owned());
}

let value: I::Object = process.copy_struct(addr)?;
let value_type = process.copy_pointer(value.ob_type())?;

Expand Down Expand Up @@ -229,13 +234,13 @@ pub fn format_variable<I>(process: &remoteprocess::Process, version: &Version, a
let mut values = Vec::new();
let mut remaining = max_length - 2;
for i in 0..object.size() {
let value_addr: *mut I::Object = process.copy_struct(object.address(addr, i))?;
let value = format_variable::<I>(process, version, value_addr as usize, remaining)?;
remaining -= value.len() as isize + 2;
if remaining <= 5 {
values.push("...".to_owned());
break;
}
let value_addr: *mut I::Object = process.copy_struct(object.address(addr, i))?;
let value = format_variable::<I>(process, version, value_addr as usize, remaining)?;
remaining -= value.len() as isize + 2;
values.push(value);
}
format!("({})", values.join(", "))
Expand Down
2 changes: 1 addition & 1 deletion src/stack_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct LocalVariable {
}

/// Given an InterpreterState, this function returns a vector of stack traces for each thread
pub fn get_stack_traces<I>(interpreter: &I, process: &Process) -> Result<(Vec<StackTrace>), Error>
pub fn get_stack_traces<I>(interpreter: &I, process: &Process) -> Result<Vec<StackTrace>, Error>
where I: InterpreterState {
// TODO: deprecate this method
let mut ret = Vec::new();
Expand Down
6 changes: 5 additions & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ fn test_local_vars() {
let frame = &trace.frames[0];
let locals = frame.locals.as_ref().unwrap();

assert_eq!(locals.len(), 8);
assert_eq!(locals.len(), 9);

let arg1 = &locals[0];
assert_eq!(arg1.name, "arg1");
Expand Down Expand Up @@ -235,6 +235,10 @@ fn test_local_vars() {
assert_eq!(local5.name, "local5");
assert!(!local5.arg);

let local6 = &locals[8];
assert_eq!(local6.name, "local6");
assert!(!local6.arg);

// we only support dictionary lookup on python 3.6+ right now
if runner.spy.version.major == 3 && runner.spy.version.minor >= 6 {
assert_eq!(local5.repr, Some("{\"a\": False, \"b\": (1, 2, 3)}".to_owned()));
Expand Down
3 changes: 3 additions & 0 deletions tests/scripts/local_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ def local_variable_lookup(arg1="foo", arg2=None, arg3=True):
local3 = 123456789123456789
local4 = 3.1415
local5 = {"a": False, "b": (1, 2, 3)}
# https://github.com/benfred/py-spy/issues/224
local6 = ("-" * 115, {"key": {"key": {"key": "value"}}})
time.sleep(100000)


if __name__ == "__main__":
local_variable_lookup()

0 comments on commit 26a20fc

Please sign in to comment.