Skip to content

Commit 795e83c

Browse files
committed
Separate filename and display filename.
1 parent cbc1950 commit 795e83c

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

memapi/src/memorytracking.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub trait WriteFunctionLocations {
5757
}
5858

5959
pub trait ReadFunctionLocations {
60-
fn get_function_and_filename(&self, id: FunctionId) -> (&str, &str);
60+
fn get_function_and_filename_and_display_filename(&self, id: FunctionId) -> (&str, &str, &str);
6161
}
6262

6363
/// Stores FunctionLocations, returns a FunctionId
@@ -88,12 +88,17 @@ impl VecFunctionLocations {
8888

8989
impl ReadFunctionLocations for VecFunctionLocations {
9090
/// Get the function name and filename.
91-
fn get_function_and_filename(&self, id: FunctionId) -> (&str, &str) {
91+
fn get_function_and_filename_and_display_filename(&self, id: FunctionId) -> (&str, &str, &str) {
9292
if id == FunctionId::UNKNOWN {
93-
return ("UNKNOWN", "UNKNOWN DUE TO BUG");
93+
return ("UNKNOWN", "UNKNOWN", "UNKNOWN DUE TO BUG");
9494
}
9595
let location = &self.functions[id.0 as usize];
96-
(&location.function_name, &location.filename)
96+
(
97+
&location.function_name,
98+
&location.filename,
99+
// TODO on Jupyter you might want to make display filename different...
100+
&location.filename,
101+
)
97102
}
98103
}
99104

@@ -225,10 +230,15 @@ impl Callstack {
225230
if self.calls.is_empty() {
226231
return "[No Python stack]".to_string();
227232
}
228-
let calls: Vec<(CallSiteId, (&str, &str))> = self
233+
let calls: Vec<(CallSiteId, (&str, &str, &str))> = self
229234
.calls
230235
.iter()
231-
.map(|id| (*id, functions.get_function_and_filename(id.function)))
236+
.map(|id| {
237+
(
238+
*id,
239+
functions.get_function_and_filename_and_display_filename(id.function),
240+
)
241+
})
232242
.collect();
233243
let skip_prefix = if cfg!(feature = "fil4prod") {
234244
0
@@ -240,7 +250,7 @@ impl Callstack {
240250
calls
241251
.into_iter()
242252
.skip(skip_prefix)
243-
.map(|(id, (function, filename))| {
253+
.map(|(id, (function, filename, display_filename))| {
244254
if to_be_post_processed {
245255
// Get Python code.
246256
let code = linecache
@@ -268,8 +278,8 @@ impl Callstack {
268278
// and that whitespace doesn't get trimmed from start;
269279
// we'll get rid of this in post-processing.
270280
format!(
271-
"{filename}:{line} ({function});\u{2800}{code}",
272-
filename = filename,
281+
"{display_filename}:{line} ({function});\u{2800}{code}",
282+
display_filename = display_filename,
273283
line = id.line_number.get_line_number(),
274284
function = function,
275285
code = &code.trim_end(),
@@ -287,10 +297,10 @@ impl Callstack {
287297
}
288298
}
289299

290-
fn runpy_prefix_length(calls: std::slice::Iter<(CallSiteId, (&str, &str))>) -> usize {
300+
fn runpy_prefix_length(calls: std::slice::Iter<(CallSiteId, (&str, &str, &str))>) -> usize {
291301
let mut length = 0;
292302
let runpy_path = get_runpy_path();
293-
for (_, (_, filename)) in calls {
303+
for (_, (_, filename, _)) in calls {
294304
// On Python 3.11 it uses <frozen runpy> for some reason.
295305
if *filename == runpy_path || *filename == "<frozen runpy>" {
296306
length += 1;
@@ -1229,8 +1239,10 @@ mod tests {
12291239
#[test]
12301240
fn test_unknown_function_id() {
12311241
let func_locations = VecFunctionLocations::new().to_reader();
1232-
let (function, filename) = func_locations.get_function_and_filename(FunctionId::UNKNOWN);
1233-
assert_eq!(filename, "UNKNOWN DUE TO BUG");
1242+
let (function, filename, display_filename) =
1243+
func_locations.get_function_and_filename_and_display_filename(FunctionId::UNKNOWN);
1244+
assert_eq!(display_filename, "UNKNOWN DUE TO BUG");
1245+
assert_eq!(filename, "UNKNOWN");
12341246
assert_eq!(function, "UNKNOWN");
12351247
}
12361248

0 commit comments

Comments
 (0)