Skip to content

Commit 8f2f350

Browse files
tonyj-suseacmel
authored andcommitted
perf script python: Add Python3 support to tests/attr.py
Support both Python 2 and Python 3 in tests/attr.py The use of "except as" syntax implies the minimum supported Python2 version is now v2.6 Committer testing: $ make -C tools/perf PYTHON3=python install-bin Before: # perf test attr 16: Setup struct perf_event_attr : FAILED! 48: Synthesize attr update : Ok [root@quaco ~]# perf test -v attr 16: Setup struct perf_event_attr : --- start --- test child forked, pid 3121 File "/home/acme/libexec/perf-core/tests/attr.py", line 324 except Unsup, obj: ^ SyntaxError: invalid syntax test child finished with -1 ---- end ---- Setup struct perf_event_attr: FAILED! 48: Synthesize attr update : --- start --- test child forked, pid 3124 test child finished with 0 ---- end ---- Synthesize attr update: Ok # After: # perf test attr 16: Setup struct perf_event_attr : Ok 48: Synthesize attr update : Ok # Signed-off-by: Tony Jones <tonyj@suse.de> Acked-by: Jiri Olsa <jolsa@kernel.org> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Ravi Bangoria <ravi.bangoria@linux.ibm.com> Cc: Seeteena Thoufeek <s1seetee@linux.vnet.ibm.com> Link: http://lkml.kernel.org/r/20190124005229.16146-7-tonyj@suse.de Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 6ab3bc2 commit 8f2f350

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

tools/perf/tests/attr.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#! /usr/bin/python
22
# SPDX-License-Identifier: GPL-2.0
33

4+
from __future__ import print_function
5+
46
import os
57
import sys
68
import glob
79
import optparse
810
import tempfile
911
import logging
1012
import shutil
11-
import ConfigParser
13+
14+
try:
15+
import configparser
16+
except ImportError:
17+
import ConfigParser as configparser
1218

1319
def data_equal(a, b):
1420
# Allow multiple values in assignment separated by '|'
@@ -100,20 +106,20 @@ def __init__(self, name, data, base):
100106
def equal(self, other):
101107
for t in Event.terms:
102108
log.debug(" [%s] %s %s" % (t, self[t], other[t]));
103-
if not self.has_key(t) or not other.has_key(t):
109+
if t not in self or t not in other:
104110
return False
105111
if not data_equal(self[t], other[t]):
106112
return False
107113
return True
108114

109115
def optional(self):
110-
if self.has_key('optional') and self['optional'] == '1':
116+
if 'optional' in self and self['optional'] == '1':
111117
return True
112118
return False
113119

114120
def diff(self, other):
115121
for t in Event.terms:
116-
if not self.has_key(t) or not other.has_key(t):
122+
if t not in self or t not in other:
117123
continue
118124
if not data_equal(self[t], other[t]):
119125
log.warning("expected %s=%s, got %s" % (t, self[t], other[t]))
@@ -134,7 +140,7 @@ def diff(self, other):
134140
# - expected values assignments
135141
class Test(object):
136142
def __init__(self, path, options):
137-
parser = ConfigParser.SafeConfigParser()
143+
parser = configparser.SafeConfigParser()
138144
parser.read(path)
139145

140146
log.warning("running '%s'" % path)
@@ -193,7 +199,7 @@ def skip_test(self, myarch):
193199
return True
194200

195201
def load_events(self, path, events):
196-
parser_event = ConfigParser.SafeConfigParser()
202+
parser_event = configparser.SafeConfigParser()
197203
parser_event.read(path)
198204

199205
# The event record section header contains 'event' word,
@@ -207,7 +213,7 @@ def load_events(self, path, events):
207213
# Read parent event if there's any
208214
if (':' in section):
209215
base = section[section.index(':') + 1:]
210-
parser_base = ConfigParser.SafeConfigParser()
216+
parser_base = configparser.SafeConfigParser()
211217
parser_base.read(self.test_dir + '/' + base)
212218
base_items = parser_base.items('event')
213219

@@ -322,9 +328,9 @@ def run_tests(options):
322328
for f in glob.glob(options.test_dir + '/' + options.test):
323329
try:
324330
Test(f, options).run()
325-
except Unsup, obj:
331+
except Unsup as obj:
326332
log.warning("unsupp %s" % obj.getMsg())
327-
except Notest, obj:
333+
except Notest as obj:
328334
log.warning("skipped %s" % obj.getMsg())
329335

330336
def setup_log(verbose):
@@ -363,7 +369,7 @@ def main():
363369
parser.add_option("-p", "--perf",
364370
action="store", type="string", dest="perf")
365371
parser.add_option("-v", "--verbose",
366-
action="count", dest="verbose")
372+
default=0, action="count", dest="verbose")
367373

368374
options, args = parser.parse_args()
369375
if args:
@@ -373,7 +379,7 @@ def main():
373379
setup_log(options.verbose)
374380

375381
if not options.test_dir:
376-
print 'FAILED no -d option specified'
382+
print('FAILED no -d option specified')
377383
sys.exit(-1)
378384

379385
if not options.test:
@@ -382,8 +388,8 @@ def main():
382388
try:
383389
run_tests(options)
384390

385-
except Fail, obj:
386-
print "FAILED %s" % obj.getMsg();
391+
except Fail as obj:
392+
print("FAILED %s" % obj.getMsg())
387393
sys.exit(-1)
388394

389395
sys.exit(0)

0 commit comments

Comments
 (0)