Skip to content

Commit 80ed0db

Browse files
committed
[LNT] Python 3 support: fix several bytes/str inconsistencies
Fix Profile.render() to return str. It's expected that .render() methods return str but in py3 it started returning bytes. Also fix reading/writing profile as string when it should be bytes. Differential Revision: https://reviews.llvm.org/D94418
1 parent 042938b commit 80ed0db

File tree

9 files changed

+69
-5
lines changed

9 files changed

+69
-5
lines changed

lnt/testing/profile/profile.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def saveFromRendered(s, filename=None, profileDir=None, prefix=''):
8080
return tf.name
8181

8282
else:
83-
open(filename, 'w').write(s)
83+
with open(filename, 'wb') as f:
84+
f.write(s)
8485
return filename
8586

8687
def save(self, filename=None, profileDir=None, prefix=''):
@@ -115,7 +116,7 @@ def render(self):
115116
116117
Implementation note: the string is base64 encoded.
117118
"""
118-
return base64.b64encode(self.impl.serialize())
119+
return base64.b64encode(self.impl.serialize()).decode('ascii')
119120

120121
def upgrade(self):
121122
"""

lnt/testing/profile/profilev1impl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def serialize(self, fname=None):
6060
if fname is None:
6161
return bytes(compressed_obj)
6262
else:
63-
with open(fname, 'w') as fd:
63+
with open(fname, 'wb') as fd:
6464
fd.write(compressed_obj)
6565

6666
def getVersion(self):

tests/runtest/Inputs/test-suite-cmake/fake-cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ else
5858
$CMAKE_SRC_DIR/fake-results-fail-compile.json \
5959
$CMAKE_SRC_DIR/fake-results-fail-exec.json \
6060
$CMAKE_SRC_DIR/fake-results-profile.json \
61+
$CMAKE_SRC_DIR/fake-results-profile-import.json \
62+
$CMAKE_SRC_DIR/fake-results.perf_data \
6163
.
6264
echo "Dummy" > CMakeCache.txt
6365
echo CMAKE_C_COMPILER:FILEPATH=$CMAKE_C_COMPILER >> CMakeCache.txt
@@ -72,6 +74,8 @@ else
7274
$CMAKE_SRC_DIR/fake-results-fail-compile.json \
7375
$CMAKE_SRC_DIR/fake-results-fail-exec.json \
7476
$CMAKE_SRC_DIR/fake-results-profile.json \
77+
$CMAKE_SRC_DIR/fake-results-profile-import.json \
78+
$CMAKE_SRC_DIR/fake-results.perf_data \
7579
subtest
7680
fi
7781
exit 0
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/python
2+
3+
import argparse, shutil
4+
parser = argparse.ArgumentParser(description='dummy lit')
5+
parser.add_argument('-o')
6+
parser.add_argument('-j', type=int)
7+
parser.add_argument('bar')
8+
args, _ = parser.parse_known_args()
9+
10+
shutil.copyfile(args.bar + '/fake-results-profile-import.json', args.o)
11+
with open(args.o, 'r') as f:
12+
report_tmp = f.read()
13+
report = report_tmp.replace('${PATH_TO_PROFILE}', args.bar + '/fake-results.perf_data')
14+
15+
with open(args.o, 'w') as f:
16+
f.write(report)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"tests": [
3+
{
4+
"name": "test-suite :: bar",
5+
"code": "PASS",
6+
"elapsed": "1.0",
7+
"metrics": {
8+
"compile_time": 1.3,
9+
"exec_time": 1.4,
10+
"score": 1.5,
11+
"hash": "xyz",
12+
"profile": "${PATH_TO_PROFILE}",
13+
"unknown": "unknown"
14+
}
15+
}
16+
]
17+
}
Binary file not shown.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Check importing test-suite profiles into db
2+
# RUN: rm -rf %t.SANDBOX
3+
# RUN: lnt runtest test-suite \
4+
# RUN: --sandbox %t.SANDBOX \
5+
# RUN: --no-timestamp \
6+
# RUN: --test-suite %S/Inputs/test-suite-cmake \
7+
# RUN: --cc %{shared_inputs}/FakeCompilers/clang-r154331 \
8+
# RUN: --use-cmake %S/Inputs/test-suite-cmake/fake-cmake \
9+
# RUN: --use-make %S/Inputs/test-suite-cmake/fake-make \
10+
# RUN: --use-lit %S/Inputs/test-suite-cmake/fake-lit-profile-import \
11+
# RUN: --use-perf=all \
12+
# RUN: -j2 \
13+
# RUN: --verbose \
14+
# RUN: > %t.log 2> %t.err
15+
# RUN: rm -rf %t.DB
16+
# RUN: lnt create %t.DB >> %t.log 2>> %t.err
17+
# RUN: lnt import %t.DB %t.SANDBOX/build/report.json \
18+
# RUN: --show-sample-count >> %t.log 2>> %t.err
19+
# RUN: python %s %t.DB
20+
21+
import sys
22+
import glob
23+
import glob
24+
from lnt.testing.profile.profilev2impl import ProfileV2
25+
26+
profile = glob.glob('%s/data/profiles/*.lntprof' % sys.argv[1])[0]
27+
assert ProfileV2.checkFile(profile)

tests/runtest/test_suite-profile.shtest

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# RUN: -j2 \
1313
# RUN: --exec-multisample=2 \
1414
# RUN: --verbose \
15-
# RUN: --commit 1 \
1615
# RUN: > %t.log 2> %t.err
1716
# RUN: FileCheck --check-prefix CHECK-USE-PERF-ALL < %t.err %s
1817
# CHECK-USE-PERF-ALL: Configuring with {

tests/testing/profilev1impl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_saveFromRendered(self):
4747

4848
with tempfile.NamedTemporaryFile() as f:
4949
Profile.saveFromRendered(s, filename=f.name)
50-
p2 = ProfileV1.deserialize(open(f.name))
50+
p2 = ProfileV1.deserialize(open(f.name, "rb"))
5151

5252
self.assertEqual(p2.data, self.test_data)
5353

0 commit comments

Comments
 (0)