Skip to content

Commit

Permalink
perf script python: Add Python3 support to tests/attr.py
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Tony Jones authored and Arnaldo Carvalho de Melo committed Feb 5, 2019
1 parent 6ab3bc2 commit 8f2f350
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions tools/perf/tests/attr.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#! /usr/bin/python
# SPDX-License-Identifier: GPL-2.0

from __future__ import print_function

import os
import sys
import glob
import optparse
import tempfile
import logging
import shutil
import ConfigParser

try:
import configparser
except ImportError:
import ConfigParser as configparser

def data_equal(a, b):
# Allow multiple values in assignment separated by '|'
Expand Down Expand Up @@ -100,20 +106,20 @@ def __init__(self, name, data, base):
def equal(self, other):
for t in Event.terms:
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
if not data_equal(self[t], other[t]):
return False
return True

def optional(self):
if self.has_key('optional') and self['optional'] == '1':
if 'optional' in self and self['optional'] == '1':
return True
return False

def diff(self, other):
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
if not data_equal(self[t], other[t]):
log.warning("expected %s=%s, got %s" % (t, self[t], other[t]))
Expand All @@ -134,7 +140,7 @@ def diff(self, other):
# - expected values assignments
class Test(object):
def __init__(self, path, options):
parser = ConfigParser.SafeConfigParser()
parser = configparser.SafeConfigParser()
parser.read(path)

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

def load_events(self, path, events):
parser_event = ConfigParser.SafeConfigParser()
parser_event = configparser.SafeConfigParser()
parser_event.read(path)

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

Expand Down Expand Up @@ -322,9 +328,9 @@ def run_tests(options):
for f in glob.glob(options.test_dir + '/' + options.test):
try:
Test(f, options).run()
except Unsup, obj:
except Unsup as obj:
log.warning("unsupp %s" % obj.getMsg())
except Notest, obj:
except Notest as obj:
log.warning("skipped %s" % obj.getMsg())

def setup_log(verbose):
Expand Down Expand Up @@ -363,7 +369,7 @@ def main():
parser.add_option("-p", "--perf",
action="store", type="string", dest="perf")
parser.add_option("-v", "--verbose",
action="count", dest="verbose")
default=0, action="count", dest="verbose")

options, args = parser.parse_args()
if args:
Expand All @@ -373,7 +379,7 @@ def main():
setup_log(options.verbose)

if not options.test_dir:
print 'FAILED no -d option specified'
print('FAILED no -d option specified')
sys.exit(-1)

if not options.test:
Expand All @@ -382,8 +388,8 @@ def main():
try:
run_tests(options)

except Fail, obj:
print "FAILED %s" % obj.getMsg();
except Fail as obj:
print("FAILED %s" % obj.getMsg())
sys.exit(-1)

sys.exit(0)
Expand Down

0 comments on commit 8f2f350

Please sign in to comment.