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>
This commit is contained in:
Tony Jones 2019-01-23 16:52:29 -08:00 committed by Arnaldo Carvalho de Melo
parent a38352de44
commit 35ea7e4bbb

View File

@ -1,5 +1,7 @@
# SPDX-License-Identifier: GPL-2.0 # SPDX-License-Identifier: GPL-2.0
from __future__ import print_function
import os import os
import sys import sys
import glob import glob
@ -7,7 +9,11 @@ import optparse
import tempfile import tempfile
import logging import logging
import shutil import shutil
import ConfigParser
try:
import configparser
except ImportError:
import ConfigParser as configparser
def data_equal(a, b): def data_equal(a, b):
# Allow multiple values in assignment separated by '|' # Allow multiple values in assignment separated by '|'
@ -99,20 +105,20 @@ class Event(dict):
def equal(self, other): def equal(self, other):
for t in Event.terms: for t in Event.terms:
log.debug(" [%s] %s %s" % (t, self[t], other[t])); log.debug(" [%s] %s %s" % (t, self[t], other[t]));
if not self.has_key(t) or not other.has_key(t): if t not in self or t not in other:
return False return False
if not data_equal(self[t], other[t]): if not data_equal(self[t], other[t]):
return False return False
return True return True
def optional(self): def optional(self):
if self.has_key('optional') and self['optional'] == '1': if 'optional' in self and self['optional'] == '1':
return True return True
return False return False
def diff(self, other): def diff(self, other):
for t in Event.terms: for t in Event.terms:
if not self.has_key(t) or not other.has_key(t): if t not in self or t not in other:
continue continue
if not data_equal(self[t], other[t]): if not data_equal(self[t], other[t]):
log.warning("expected %s=%s, got %s" % (t, self[t], other[t])) log.warning("expected %s=%s, got %s" % (t, self[t], other[t]))
@ -133,7 +139,7 @@ class Event(dict):
# - expected values assignments # - expected values assignments
class Test(object): class Test(object):
def __init__(self, path, options): def __init__(self, path, options):
parser = ConfigParser.SafeConfigParser() parser = configparser.SafeConfigParser()
parser.read(path) parser.read(path)
log.warning("running '%s'" % path) log.warning("running '%s'" % path)
@ -192,7 +198,7 @@ class Test(object):
return True return True
def load_events(self, path, events): def load_events(self, path, events):
parser_event = ConfigParser.SafeConfigParser() parser_event = configparser.SafeConfigParser()
parser_event.read(path) parser_event.read(path)
# The event record section header contains 'event' word, # The event record section header contains 'event' word,
@ -206,7 +212,7 @@ class Test(object):
# Read parent event if there's any # Read parent event if there's any
if (':' in section): if (':' in section):
base = section[section.index(':') + 1:] base = section[section.index(':') + 1:]
parser_base = ConfigParser.SafeConfigParser() parser_base = configparser.SafeConfigParser()
parser_base.read(self.test_dir + '/' + base) parser_base.read(self.test_dir + '/' + base)
base_items = parser_base.items('event') base_items = parser_base.items('event')
@ -321,9 +327,9 @@ def run_tests(options):
for f in glob.glob(options.test_dir + '/' + options.test): for f in glob.glob(options.test_dir + '/' + options.test):
try: try:
Test(f, options).run() Test(f, options).run()
except Unsup, obj: except Unsup as obj:
log.warning("unsupp %s" % obj.getMsg()) log.warning("unsupp %s" % obj.getMsg())
except Notest, obj: except Notest as obj:
log.warning("skipped %s" % obj.getMsg()) log.warning("skipped %s" % obj.getMsg())
def setup_log(verbose): def setup_log(verbose):
@ -362,7 +368,7 @@ def main():
parser.add_option("-p", "--perf", parser.add_option("-p", "--perf",
action="store", type="string", dest="perf") action="store", type="string", dest="perf")
parser.add_option("-v", "--verbose", parser.add_option("-v", "--verbose",
action="count", dest="verbose") default=0, action="count", dest="verbose")
options, args = parser.parse_args() options, args = parser.parse_args()
if args: if args:
@ -372,7 +378,7 @@ def main():
setup_log(options.verbose) setup_log(options.verbose)
if not options.test_dir: if not options.test_dir:
print 'FAILED no -d option specified' print('FAILED no -d option specified')
sys.exit(-1) sys.exit(-1)
if not options.test: if not options.test:
@ -381,8 +387,8 @@ def main():
try: try:
run_tests(options) run_tests(options)
except Fail, obj: except Fail as obj:
print "FAILED %s" % obj.getMsg(); print("FAILED %s" % obj.getMsg())
sys.exit(-1) sys.exit(-1)
sys.exit(0) sys.exit(0)