Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad727ff

Browse files
authored
Avoid break string inside unicode char when formatting (#767)
1 parent 2395e2c commit ad727ff

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/python_data_access.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ where
409409
let value = copy_string(addr as *const I::StringObject, process)?
410410
.replace('\'', "\\\"")
411411
.replace('\n', "\\n");
412-
if value.len() as isize >= max_length - 5 {
413-
format!("\"{}...\"", &value[..(max_length - 5) as usize])
412+
if let Some((offset, _)) = value.char_indices().nth((max_length - 5) as usize) {
413+
format!("\"{}...\"", &value[..offset])
414414
} else {
415415
format!("\"{}\"", value)
416416
}

tests/integration_test.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fn test_local_vars() {
280280
let frame = &trace.frames[0];
281281
let locals = frame.locals.as_ref().unwrap();
282282

283-
assert_eq!(locals.len(), 27);
283+
assert_eq!(locals.len(), 28);
284284

285285
let arg1 = &locals[0];
286286
assert_eq!(arg1.name, "arg1");
@@ -415,6 +415,13 @@ fn test_local_vars() {
415415
assert_eq!(local24.name, "local24");
416416
test_repr_prefix(local24, "<numpy.clongdouble at");
417417

418+
// https://github.com/benfred/py-spy/issues/766
419+
let local25 = &locals[27];
420+
assert_eq!(local25.name, "local25");
421+
let unicode_val = local25.repr.as_ref().unwrap();
422+
let end = unicode_val.char_indices().map(|(i, _)| i).nth(4).unwrap();
423+
assert_eq!(unicode_val[0..end], *"\"测试1");
424+
418425
// we only support dictionary lookup on python 3.6+ right now
419426
if runner.spy.version.major == 3 && runner.spy.version.minor >= 6 {
420427
assert_eq!(

tests/scripts/local_vars.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def local_variable_lookup(arg1="foo", arg2=None, arg3=True):
3939
local23 = np.complex128(0.3+5j)
4040
local24 = np.clongdouble(0.3+5j)
4141

42+
# https://github.com/benfred/py-spy/issues/766
43+
local25 = "测试1" * 500
4244

4345
time.sleep(100000)
4446

0 commit comments

Comments
 (0)