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