Skip to content

Commit

Permalink
Merge pull request #153 from pythonspeed/150-svg-sorting-error
Browse files Browse the repository at this point in the history
Fix bug where reverse SVG wasn't generated.
  • Loading branch information
itamarst authored Apr 8, 2021
2 parents 881d299 + 7193095 commit f1c95d7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 17 deletions.
1 change: 1 addition & 0 deletions .changelog/150.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed bug where reverse SVG sometimes was generated empty, e.g. if source code used tabs.
7 changes: 4 additions & 3 deletions filprofiler/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ def get_allocations(
prof_file="peak-memory.prof",
):
"""Parses peak-memory.prof, returns mapping from callstack to size in KiB."""
assert sorted(os.listdir(glob(str(output_directory / "*"))[0])) == sorted(
expected_files
)
subdir = glob(str(output_directory / "*"))[0]
assert sorted(os.listdir(subdir)) == sorted(expected_files)
for expected_file in expected_files:
assert (Path(subdir) / expected_file).stat().st_size > 0
result = {}
with open(glob(str(output_directory / "*" / prof_file))[0]) as f:
for line in f:
Expand Down
19 changes: 6 additions & 13 deletions memapi/src/memorytracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,16 @@ impl Callstack {
// input format, so need to replace them with some other
// character. We use "full-width semicolon", and then
// replace it back in post-processing.
let mut code = code.replace(";", "\u{ff1b}");
// Make sure we don't have empty lines; we'll get rid of
// this in post-processing.
if &code.trim_end() == &"" {
code = "\u{2800}".to_string();
}
let code_suffix = if code.len() > 0 {
";".to_string() + &code.trim_end()
} else {
"".to_string()
};
let code = code.replace(";", "\u{ff1b}");
// The \u{2800} is to ensure we don't have empty lines,
// and that whitespace doesn't get trimmed from start;
// we'll get rid of this in post-processing.
format!(
"{filename}:{line} ({function}){code_suffix}",
"{filename}:{line} ({function});\u{2800}{code}",
filename = filename,
line = id.line_number,
function = function,
code_suffix = code_suffix,
code = &code.trim_end(),
)
} else {
format!(
Expand Down
19 changes: 19 additions & 0 deletions tests/test-scripts/tabs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import numpy as np


def make_big_array():
return np.zeros((1024, 1024, 50))


def make_two_arrays():
arr2 = np.ones((1024, 1024, 10))
arr1 = np.zeros((1024, 1024, 10))
return arr1, arr2


def main():
arr1, arr2 = make_two_arrays()
another_arr = make_big_array()


main()
23 changes: 22 additions & 1 deletion tests/test_endtoend.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,29 @@ def test_source_rendering():
in svg
)

# Prefix spaces are turned into non-break spaces:
# Spaces are turned into non-break spaces:
assert "> c = np.ones((1024, 1024))".replace(" ", "\u00a0") in svg

# It's valid XML:
ElementTree.fromstring(svg)


def test_tabs():
"""
Source code with tabs doesn't break SVG generation.
"""
script = TEST_SCRIPTS / "tabs.py"
output_dir = profile(script)
get_allocations(output_dir) # <- smoke test

for svg in ["peak-memory.svg", "peak-memory-reversed.svg"]:
svg_path = glob(str(output_dir / "*" / svg))[0]

with open(svg_path) as f:
svg = f.read()

# Tabs are still there:
assert ">\tarr1, arr2 = make_".replace(" ", "\u00a0") in svg

# It's valid XML:
ElementTree.fromstring(svg)

0 comments on commit f1c95d7

Please sign in to comment.