diff options
author | Andreas Sandberg <andreas.sandberg@arm.com> | 2019-01-25 11:32:25 +0000 |
---|---|---|
committer | Andreas Sandberg <andreas.sandberg@arm.com> | 2019-02-12 09:38:12 +0000 |
commit | fa21127a646b8d2a61fe412728762250ca38ecd2 (patch) | |
tree | 8f0c21351c0e7c7e30b1c03a81c3d4f122f566c5 /src/python/m5/util | |
parent | 6ba4545b1f9553e68e992305c92cf46246a79dae (diff) | |
download | gem5-fa21127a646b8d2a61fe412728762250ca38ecd2.tar.xz |
python: Make exception handling Python 3 safe
Change-Id: I9c2cdfad20deb1ddfa224320cf93f2105d126652
Reviewed-on: https://gem5-review.googlesource.com/c/15980
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
Reviewed-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
Diffstat (limited to 'src/python/m5/util')
-rw-r--r-- | src/python/m5/util/__init__.py | 6 | ||||
-rw-r--r-- | src/python/m5/util/code_formatter.py | 2 | ||||
-rw-r--r-- | src/python/m5/util/convert.py | 36 | ||||
-rw-r--r-- | src/python/m5/util/grammar.py | 19 | ||||
-rw-r--r-- | src/python/m5/util/jobfile.py | 37 | ||||
-rw-r--r-- | src/python/m5/util/multidict.py | 10 |
6 files changed, 54 insertions, 56 deletions
diff --git a/src/python/m5/util/__init__.py b/src/python/m5/util/__init__.py index 2ad9c5627..02dece69b 100644 --- a/src/python/m5/util/__init__.py +++ b/src/python/m5/util/__init__.py @@ -128,7 +128,7 @@ def compareVersions(v1, v2): elif isinstance(v, str): return map(lambda x: int(re.match('\d+', x).group()), v.split('.')) else: - raise TypeError + raise TypeError() v1 = make_version_list(v1) v2 = make_version_list(v2) @@ -194,7 +194,7 @@ def readCommand(cmd, **kwargs): kwargs.setdefault('close_fds', True) try: subp = Popen(cmd, **kwargs) - except Exception, e: + except Exception as e: if no_exception: return exception raise @@ -206,7 +206,7 @@ def makeDir(path): ensure that it is a directory""" if os.path.exists(path): if not os.path.isdir(path): - raise AttributeError, "%s exists but is not directory" % path + raise AttributeError("%s exists but is not directory" % path) else: os.mkdir(path) diff --git a/src/python/m5/util/code_formatter.py b/src/python/m5/util/code_formatter.py index a11c9d3d0..d48c59b26 100644 --- a/src/python/m5/util/code_formatter.py +++ b/src/python/m5/util/code_formatter.py @@ -74,7 +74,7 @@ class lookup(object): return self.args[item] except ValueError: pass - raise IndexError, "Could not find '%s'" % item + raise IndexError("Could not find '%s'" % item) class code_formatter_meta(type): pattern = r""" diff --git a/src/python/m5/util/convert.py b/src/python/m5/util/convert.py index 5ae31216b..7b9cb3812 100644 --- a/src/python/m5/util/convert.py +++ b/src/python/m5/util/convert.py @@ -89,7 +89,7 @@ binary_prefixes = { def assertStr(value): if not isinstance(value, str): - raise TypeError, "wrong type '%s' should be str" % type(value) + raise TypeError("wrong type '%s' should be str" % type(value)) # memory size configuration stuff @@ -102,8 +102,8 @@ def toFloat(value, target_type='float', units=None, prefixes=[]): try: return float(value) except ValueError: - raise ValueError, "cannot convert '%s' to %s" % \ - (value, target_type) + raise ValueError("cannot convert '%s' to %s" % \ + (value, target_type)) value = value[:-len(units)] @@ -124,8 +124,8 @@ def toInteger(value, target_type='integer', units=None, prefixes=[]): value = toFloat(value, target_type, units, prefixes) result = long(value) if value != result: - raise ValueError, "cannot convert '%s' to integer %s" % \ - (value, target_type) + raise ValueError("cannot convert '%s' to integer %s" % \ + (value, target_type)) return result @@ -155,7 +155,7 @@ def anyToLatency(value): """result is a clock period""" try: return 1 / toFrequency(value) - except ValueError, ZeroDivisionError: + except (ValueError, ZeroDivisionError): pass try: @@ -163,7 +163,7 @@ def anyToLatency(value): except ValueError: pass - raise ValueError, "cannot convert '%s' to clock period" % value + raise ValueError("cannot convert '%s' to clock period" % value) def anyToFrequency(value): """result is a clock period""" @@ -174,10 +174,10 @@ def anyToFrequency(value): try: return 1 / toLatency(value) - except ValueError, ZeroDivisionError: + except ValueError as ZeroDivisionError: pass - raise ValueError, "cannot convert '%s' to clock period" % value + raise ValueError("cannot convert '%s' to clock period" % value) def toNetworkBandwidth(value): return toMetricFloat(value, 'network bandwidth', 'bps') @@ -190,29 +190,29 @@ def toMemorySize(value): def toIpAddress(value): if not isinstance(value, str): - raise TypeError, "wrong type '%s' should be str" % type(value) + raise TypeError("wrong type '%s' should be str" % type(value)) bytes = value.split('.') if len(bytes) != 4: - raise ValueError, 'invalid ip address %s' % value + raise ValueError('invalid ip address %s' % value) for byte in bytes: if not 0 <= int(byte) <= 0xff: - raise ValueError, 'invalid ip address %s' % value + raise ValueError('invalid ip address %s' % value) return (int(bytes[0]) << 24) | (int(bytes[1]) << 16) | \ (int(bytes[2]) << 8) | (int(bytes[3]) << 0) def toIpNetmask(value): if not isinstance(value, str): - raise TypeError, "wrong type '%s' should be str" % type(value) + raise TypeError("wrong type '%s' should be str" % type(value)) (ip, netmask) = value.split('/') ip = toIpAddress(ip) netmaskParts = netmask.split('.') if len(netmaskParts) == 1: if not 0 <= int(netmask) <= 32: - raise ValueError, 'invalid netmask %s' % netmask + raise ValueError('invalid netmask %s' % netmask) return (ip, int(netmask)) elif len(netmaskParts) == 4: netmaskNum = toIpAddress(netmask) @@ -223,18 +223,18 @@ def toIpNetmask(value): testVal |= (1 << (31 - i)) if testVal == netmaskNum: return (ip, i + 1) - raise ValueError, 'invalid netmask %s' % netmask + raise ValueError('invalid netmask %s' % netmask) else: - raise ValueError, 'invalid netmask %s' % netmask + raise ValueError('invalid netmask %s' % netmask) def toIpWithPort(value): if not isinstance(value, str): - raise TypeError, "wrong type '%s' should be str" % type(value) + raise TypeError("wrong type '%s' should be str" % type(value)) (ip, port) = value.split(':') ip = toIpAddress(ip) if not 0 <= int(port) <= 0xffff: - raise ValueError, 'invalid port %s' % port + raise ValueError('invalid port %s' % port) return (ip, int(port)) def toVoltage(value): diff --git a/src/python/m5/util/grammar.py b/src/python/m5/util/grammar.py index 07702cfad..bb3429866 100644 --- a/src/python/m5/util/grammar.py +++ b/src/python/m5/util/grammar.py @@ -37,18 +37,17 @@ class ParseError(Exception): class Grammar(object): def setupLexerFactory(self, **kwargs): if 'module' in kwargs: - raise AttributeError, "module is an illegal attribute" + raise AttributeError("module is an illegal attribute") self.lex_kwargs = kwargs def setupParserFactory(self, **kwargs): if 'module' in kwargs: - raise AttributeError, "module is an illegal attribute" + raise AttributeError("module is an illegal attribute") if 'output' in kwargs: dir,tab = os.path.split(output) if not tab.endswith('.py'): - raise AttributeError, \ - 'The output file must end with .py' + raise AttributeError('The output file must end with .py') kwargs['outputdir'] = dir kwargs['tabmodule'] = tab[:-3] @@ -90,13 +89,13 @@ class Grammar(object): return -1 return self.current_lexer.lineno - raise AttributeError, \ - "'%s' object has no attribute '%s'" % (type(self), attr) + raise AttributeError( + "'%s' object has no attribute '%s'" % (type(self), attr)) def parse_string(self, data, source='<string>', debug=None, tracking=0): if not isinstance(data, basestring): - raise AttributeError, \ - "argument must be a string, was '%s'" % type(f) + raise AttributeError( + "argument must be a string, was '%s'" % type(f)) import new lexer = self.lex.clone() @@ -120,8 +119,8 @@ class Grammar(object): elif isinstance(f, file): source = f.name else: - raise AttributeError, \ - "argument must be either a string or file, was '%s'" % type(f) + raise AttributeError( + "argument must be either a string or file, was '%s'" % type(f)) return self.parse_string(f.read(), source, **kwargs) diff --git a/src/python/m5/util/jobfile.py b/src/python/m5/util/jobfile.py index d8c09afd4..ad5b5ff5c 100644 --- a/src/python/m5/util/jobfile.py +++ b/src/python/m5/util/jobfile.py @@ -38,7 +38,7 @@ class Data(object): def update(self, obj): if not isinstance(obj, Data): - raise AttributeError, "can only update from Data object" + raise AttributeError("can only update from Data object") for key,val in obj.__dict__.iteritems(): if key.startswith('_') or key in ('name', 'desc'): @@ -52,22 +52,22 @@ class Data(object): if self.__dict__[key] == val: continue - raise AttributeError, \ - "%s specified more than once old: %s new: %s" % \ - (key, self.__dict__[key], val) + raise AttributeError( + "%s specified more than once old: %s new: %s" % \ + (key, self.__dict__[key], val)) d = self.__dict__[key] for k,v in val.iteritems(): if k in d: - raise AttributeError, \ - "%s specified more than once in %s" % (k, key) + raise AttributeError( + "%s specified more than once in %s" % (k, key)) d[k] = v if hasattr(self, 'system') and hasattr(obj, 'system'): if self.system != obj.system: - raise AttributeError, \ - "conflicting values for system: '%s'/'%s'" % \ - (self.system, obj.system) + raise AttributeError( + "conflicting values for system: '%s'/'%s'" % \ + (self.system, obj.system)) def printinfo(self): if self.name: @@ -96,7 +96,7 @@ class Data(object): def __getitem__(self, key): if key.startswith('_'): - raise KeyError, "Key '%s' not found" % attr + raise KeyError("Key '%s' not found" % attr) return self.__dict__[key] def __iter__(self): @@ -131,8 +131,8 @@ class Job(Data): config = options[0]._config for opt in options: if opt._config != config: - raise AttributeError, \ - "All options are not from the same Configuration" + raise AttributeError( + "All options are not from the same Configuration") self._config = config self._groups = [ opt._group for opt in options ] @@ -309,7 +309,7 @@ class Configuration(Data): def checkchildren(self, kids): for kid in kids: if kid._config != self: - raise AttributeError, "child from the wrong configuration" + raise AttributeError("child from the wrong configuration") def sortgroups(self, groups): groups = [ (grp._number, grp) for grp in groups ] @@ -387,7 +387,7 @@ class Configuration(Data): if job.name == jobname: return job else: - raise AttributeError, "job '%s' not found" % jobname + raise AttributeError("job '%s' not found" % jobname) def job(self, options): self.checkchildren(options) @@ -414,13 +414,12 @@ def JobFile(jobfile): filename = testname break else: - raise AttributeError, \ - "Could not find file '%s'" % jobfile + raise AttributeError("Could not find file '%s'" % jobfile) data = {} execfile(filename, data) if 'conf' not in data: - raise ImportError, 'cannot import name conf from %s' % jobfile + raise ImportError('cannot import name conf from %s' % jobfile) return data['conf'] def main(conf=None): @@ -448,11 +447,11 @@ def main(conf=None): if conf is None: if len(args) != 1: - raise AttributeError, usage + raise AttributeError(usage) conf = JobFile(args[0]) else: if len(args) != 0: - raise AttributeError, usage + raise AttributeError(usage) if both: jobs = conf.alljobs() diff --git a/src/python/m5/util/multidict.py b/src/python/m5/util/multidict.py index d22b1cbbc..28090a251 100644 --- a/src/python/m5/util/multidict.py +++ b/src/python/m5/util/multidict.py @@ -48,11 +48,11 @@ class multidict(object): def __delitem__(self, key): try: del self.local[key] - except KeyError, e: + except KeyError as e: if key in self.parent: self.deleted[key] = True else: - raise KeyError, e + raise KeyError(e) def __setitem__(self, key, value): self.deleted.pop(key, False) @@ -61,11 +61,11 @@ class multidict(object): def __getitem__(self, key): try: return self.local[key] - except KeyError, e: + except KeyError as e: if not self.deleted.get(key, False) and key in self.parent: return self.parent[key] else: - raise KeyError, e + raise KeyError(e) def __len__(self): return len(self.local) + len(self.parent) @@ -106,7 +106,7 @@ class multidict(object): def get(self, key, default=None): try: return self[key] - except KeyError, e: + except KeyError as e: return default def setdefault(self, key, default): |