History

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 #

Tony Jones committed 7 years ago

Commit 8f2f3501 parent 6ab3bc21 file · +19 −13Browse files

Unified · Split

tools/perf/tests/attr.py+19−13
@@ -1,6 +1,8 @@
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
@@ -8,7 +10,11 @@ import optparse · 1 unmodified line
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 @@ class Event(dict): · 85 unmodified lines
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 @@ class Event(dict): · 14 unmodified lines
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 @@ class Test(object): · 52 unmodified lines
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 @@ class Test(object): · 7 unmodified lines
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): · 108 unmodified lines
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(): · 32 unmodified lines
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(): · 3 unmodified lines
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(): · 2 unmodified lines
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)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t