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>

authored by Tony Jones and committed by Arnaldo Carvalho de Melo 8f2f350c 6ab3bc24

+19 -13
+19 -13
tools/perf/tests/attr.py
··· 1 1 #! /usr/bin/python 2 2 # SPDX-License-Identifier: GPL-2.0 3 3 4 + from __future__ import print_function 5 + 4 6 import os 5 7 import sys 6 8 import glob ··· 10 8 import tempfile 11 9 import logging 12 10 import shutil 13 - import ConfigParser 11 + 12 + try: 13 + import configparser 14 + except ImportError: 15 + import ConfigParser as configparser 14 16 15 17 def data_equal(a, b): 16 18 # Allow multiple values in assignment separated by '|' ··· 106 100 def equal(self, other): 107 101 for t in Event.terms: 108 102 log.debug(" [%s] %s %s" % (t, self[t], other[t])); 109 - if not self.has_key(t) or not other.has_key(t): 103 + if t not in self or t not in other: 110 104 return False 111 105 if not data_equal(self[t], other[t]): 112 106 return False 113 107 return True 114 108 115 109 def optional(self): 116 - if self.has_key('optional') and self['optional'] == '1': 110 + if 'optional' in self and self['optional'] == '1': 117 111 return True 118 112 return False 119 113 120 114 def diff(self, other): 121 115 for t in Event.terms: 122 - if not self.has_key(t) or not other.has_key(t): 116 + if t not in self or t not in other: 123 117 continue 124 118 if not data_equal(self[t], other[t]): 125 119 log.warning("expected %s=%s, got %s" % (t, self[t], other[t])) ··· 140 134 # - expected values assignments 141 135 class Test(object): 142 136 def __init__(self, path, options): 143 - parser = ConfigParser.SafeConfigParser() 137 + parser = configparser.SafeConfigParser() 144 138 parser.read(path) 145 139 146 140 log.warning("running '%s'" % path) ··· 199 193 return True 200 194 201 195 def load_events(self, path, events): 202 - parser_event = ConfigParser.SafeConfigParser() 196 + parser_event = configparser.SafeConfigParser() 203 197 parser_event.read(path) 204 198 205 199 # The event record section header contains 'event' word, ··· 213 207 # Read parent event if there's any 214 208 if (':' in section): 215 209 base = section[section.index(':') + 1:] 216 - parser_base = ConfigParser.SafeConfigParser() 210 + parser_base = configparser.SafeConfigParser() 217 211 parser_base.read(self.test_dir + '/' + base) 218 212 base_items = parser_base.items('event') 219 213 ··· 328 322 for f in glob.glob(options.test_dir + '/' + options.test): 329 323 try: 330 324 Test(f, options).run() 331 - except Unsup, obj: 325 + except Unsup as obj: 332 326 log.warning("unsupp %s" % obj.getMsg()) 333 - except Notest, obj: 327 + except Notest as obj: 334 328 log.warning("skipped %s" % obj.getMsg()) 335 329 336 330 def setup_log(verbose): ··· 369 363 parser.add_option("-p", "--perf", 370 364 action="store", type="string", dest="perf") 371 365 parser.add_option("-v", "--verbose", 372 - action="count", dest="verbose") 366 + default=0, action="count", dest="verbose") 373 367 374 368 options, args = parser.parse_args() 375 369 if args: ··· 379 373 setup_log(options.verbose) 380 374 381 375 if not options.test_dir: 382 - print 'FAILED no -d option specified' 376 + print('FAILED no -d option specified') 383 377 sys.exit(-1) 384 378 385 379 if not options.test: ··· 388 382 try: 389 383 run_tests(options) 390 384 391 - except Fail, obj: 392 - print "FAILED %s" % obj.getMsg(); 385 + except Fail as obj: 386 + print("FAILED %s" % obj.getMsg()) 393 387 sys.exit(-1) 394 388 395 389 sys.exit(0)