Commit 7c2963dc authored by Marco Clemencic's avatar Marco Clemencic
Browse files

Python 2 & 3 compatibility, part 1 (!832)

parents a6047069 aeada68b
...@@ -447,7 +447,7 @@ class gaudimain(object): ...@@ -447,7 +447,7 @@ class gaudimain(object):
# It may not be 100% correct, but usually it means a segfault in C++ # It may not be 100% correct, but usually it means a segfault in C++
self.ip.setProperty('ReturnCode', str(128 + 11)) self.ip.setProperty('ReturnCode', str(128 + 11))
statuscode = False statuscode = False
except Exception, x: except Exception as x:
print 'Exception:', x print 'Exception:', x
# for other exceptions, just set a generic error code # for other exceptions, just set a generic error code
self.ip.setProperty('ReturnCode', '1') self.ip.setProperty('ReturnCode', '1')
......
...@@ -67,18 +67,18 @@ if '__main__' == __name__: ...@@ -67,18 +67,18 @@ if '__main__' == __name__:
try: try:
xp2.SVector5 = [1, 2, 3, 4, 5, 6] xp2.SVector5 = [1, 2, 3, 4, 5, 6]
except Exception, e: except Exception as e:
print ' Exception: ', e print ' Exception: ', e
try: try:
xp2.Point3D = (1, 2, 3, 4) xp2.Point3D = (1, 2, 3, 4)
except Exception, e: except Exception as e:
print ' Exception: ', e print ' Exception: ', e
if not isWin: if not isWin:
try: try:
xp2.Vector4D = (1, 2, 3) xp2.Vector4D = (1, 2, 3)
except Exception, e: except Exception as e:
print ' Exception: ', e print ' Exception: ', e
xp2.Vectors3D = [(1, 2, 3), (4, 5, 6), [7, 8, 9]] xp2.Vectors3D = [(1, 2, 3), (4, 5, 6), [7, 8, 9]]
......
...@@ -52,7 +52,7 @@ def test(): ...@@ -52,7 +52,7 @@ def test():
if __name__ == '__main__': if __name__ == '__main__':
try: try:
test() test()
except AssertionError, a: except AssertionError as a:
print "FAILURE:", a print "FAILURE:", a
sys.exit(1) sys.exit(1)
print "SUCCESS" print "SUCCESS"
...@@ -514,7 +514,7 @@ class Configurable(object): ...@@ -514,7 +514,7 @@ class Configurable(object):
if hasattr(cc, 'setParent') and parent: if hasattr(cc, 'setParent') and parent:
try: try:
cc.setParent(parent) cc.setParent(parent)
except RuntimeError, e: except RuntimeError as e:
# temporary backdoor resolution for compatibility # temporary backdoor resolution for compatibility
log.error(str(e) + '%s', error_explanation) log.error(str(e) + '%s', error_explanation)
ccbd = cc.configurables[cc.getJobOptName()] ccbd = cc.configurables[cc.getJobOptName()]
......
...@@ -138,7 +138,7 @@ def loadConfigurableDb(): ...@@ -138,7 +138,7 @@ def loadConfigurableDb():
log.debug("\t-loading [%s]...", confDb) log.debug("\t-loading [%s]...", confDb)
try: try:
cfgDb._loadModule(confDb) cfgDb._loadModule(confDb)
except Exception, err: except Exception as err:
log.warning("Could not load file [%s] !", confDb) log.warning("Could not load file [%s] !", confDb)
log.warning("Reason: %s", err) log.warning("Reason: %s", err)
nFiles += 1 nFiles += 1
......
...@@ -146,7 +146,7 @@ class PropertyProxy(object): ...@@ -146,7 +146,7 @@ class PropertyProxy(object):
except AttributeError: except AttributeError:
# value not yet set # value not yet set
pass pass
except ValueError, e: except ValueError as e:
if allowcompat: if allowcompat:
log.error('inconsistent value types for %s.%s (%s)' % log.error('inconsistent value types for %s.%s (%s)' %
(obj.getName(), self.descr.__name__, str(e))) (obj.getName(), self.descr.__name__, str(e)))
...@@ -199,7 +199,7 @@ class GaudiHandlePropertyProxyBase(PropertyProxy): ...@@ -199,7 +199,7 @@ class GaudiHandlePropertyProxyBase(PropertyProxy):
default = self.convertDefaultToBeSet(obj, default) default = self.convertDefaultToBeSet(obj, default)
if default: if default:
self.__set__(obj, default) self.__set__(obj, default)
except AttributeError, e: except AttributeError as e:
# change type of exception to avoid false error message # change type of exception to avoid false error message
raise RuntimeError(*e.args) raise RuntimeError(*e.args)
...@@ -266,7 +266,7 @@ class GaudiHandlePropertyProxyBase(PropertyProxy): ...@@ -266,7 +266,7 @@ class GaudiHandlePropertyProxyBase(PropertyProxy):
# print self.fullPropertyName(obj) + ": Setting default private configurable (from default handle): %r" % conf # print self.fullPropertyName(obj) + ": Setting default private configurable (from default handle): %r" % conf
except AttributeError, e: except AttributeError as e:
# change type of exception to avoid false error message # change type of exception to avoid false error message
raise RuntimeError(*e.args) raise RuntimeError(*e.args)
if conf is None: if conf is None:
...@@ -389,7 +389,7 @@ class DataObjectHandleBasePropertyProxy(PropertyProxy): ...@@ -389,7 +389,7 @@ class DataObjectHandleBasePropertyProxy(PropertyProxy):
default = self.convertValueToBeSet(obj, default) default = self.convertValueToBeSet(obj, default)
if default: if default:
self.__set__(obj, default) self.__set__(obj, default)
except AttributeError, e: except AttributeError as e:
# change type of exception to avoid false error message # change type of exception to avoid false error message
raise RuntimeError(*e.args) raise RuntimeError(*e.args)
......
...@@ -17,7 +17,7 @@ from GaudiKernel.ConfigurableDb import cfgDb ...@@ -17,7 +17,7 @@ from GaudiKernel.ConfigurableDb import cfgDb
logging.VERBOSE = (logging.INFO + logging.DEBUG) / 2 logging.VERBOSE = (logging.INFO + logging.DEBUG) / 2
logging.addLevelName(logging.VERBOSE, "VERBOSE") logging.addLevelName(logging.VERBOSE, "VERBOSE")
logging.verbose = lambda msg, *args, **kwargs: \ logging.verbose = lambda msg, *args, **kwargs: \
apply(logging.log, (logging.VERBOSE, msg) + args, kwargs) logging.log(logging.VERBOSE, msg, *args, **kwargs)
def _inheritsfrom(derived, basenames): def _inheritsfrom(derived, basenames):
...@@ -68,7 +68,7 @@ def loadConfigurableDb(): ...@@ -68,7 +68,7 @@ def loadConfigurableDb():
log.debug("\t-loading [%s]..." % confDb) log.debug("\t-loading [%s]..." % confDb)
try: try:
cfgDb._loadModule(confDb) cfgDb._loadModule(confDb)
except Exception, err: except Exception as err:
# It may happen that the file is found but not completely # It may happen that the file is found but not completely
# written, usually during parallel builds, but we do not care. # written, usually during parallel builds, but we do not care.
log.warning("Could not load file [%s] !", confDb) log.warning("Could not load file [%s] !", confDb)
...@@ -278,7 +278,7 @@ def main(): ...@@ -278,7 +278,7 @@ def main():
try: try:
logging.info("Creating directory %r", output_dir) logging.info("Creating directory %r", output_dir)
os.makedirs(output_dir, 0755) os.makedirs(output_dir, 0755)
except OSError, err: except OSError as err:
import errno import errno
if err.errno == errno.EEXIST: if err.errno == errno.EEXIST:
# somebody already - perhaps concurrently - created that dir. # somebody already - perhaps concurrently - created that dir.
......
...@@ -16,13 +16,13 @@ def command(cmd, *args, **kwargs): ...@@ -16,13 +16,13 @@ def command(cmd, *args, **kwargs):
d.update(kwargs) d.update(kwargs)
cmd = [cmd] + list(args) cmd = [cmd] + list(args)
logging.debug("Execute command: %r %r", " ".join(cmd), kwargs) logging.debug("Execute command: %r %r", " ".join(cmd), kwargs)
proc = apply(Popen, (cmd, ), d) proc = Popen(cmd, **d)
return proc.communicate() return proc.communicate()
cmt = lambda *args, **kwargs: apply(command, ("cmt", ) + args, kwargs) cmt = lambda *args, **kwargs: command("cmt", *args, **kwargs)
cvs = lambda *args, **kwargs: apply(command, ("cvs", ) + args, kwargs) cvs = lambda *args, **kwargs: command("cvs", *args, **kwargs)
svn = lambda *args, **kwargs: apply(command, ("svn", ) + args, kwargs) svn = lambda *args, **kwargs: command("svn", *args, **kwargs)
def broadcast_packages(): def broadcast_packages():
......
...@@ -53,7 +53,7 @@ def test_invalid_value(): ...@@ -53,7 +53,7 @@ def test_invalid_value():
raise raise
except ValueError: except ValueError:
pass pass
except Exception, x: except Exception as x:
assert False, 'ValueError exception expected, got %s' % type( assert False, 'ValueError exception expected, got %s' % type(
x).__name__ x).__name__
...@@ -64,7 +64,7 @@ def test_invalid_value(): ...@@ -64,7 +64,7 @@ def test_invalid_value():
raise raise
except ValueError: except ValueError:
pass pass
except Exception, x: except Exception as x:
assert False, 'ValueError exception expected, got %s' % type( assert False, 'ValueError exception expected, got %s' % type(
x).__name__ x).__name__
...@@ -89,7 +89,7 @@ def test_invalid_key(): ...@@ -89,7 +89,7 @@ def test_invalid_key():
raise raise
except AttributeError: except AttributeError:
pass pass
except Exception, x: except Exception as x:
assert False, 'AttributeError exception expected, got %s' % type( assert False, 'AttributeError exception expected, got %s' % type(
x).__name__ x).__name__
......
...@@ -55,7 +55,7 @@ def kill_tree(ppid, sig): ...@@ -55,7 +55,7 @@ def kill_tree(ppid, sig):
try: try:
log.debug('killing process %d', ppid) log.debug('killing process %d', ppid)
os.kill(ppid, sig) os.kill(ppid, sig)
except OSError, err: except OSError as err:
if err.errno != 3: # No such process if err.errno != 3: # No such process
raise raise
log.debug('no such process %d', ppid) log.debug('no such process %d', ppid)
......
...@@ -77,7 +77,7 @@ class QMTTest(BaseTest): ...@@ -77,7 +77,7 @@ class QMTTest(BaseTest):
# positional or keyword # positional or keyword
if a not in positional and a not in kwargs: if a not in positional and a not in kwargs:
kwargs[a] = self.extra_args[a] kwargs[a] = self.extra_args[a]
return apply(self.callable, args, kwargs) return self.callable(*args, **kwargs)
# local names to be exposed in the script # local names to be exposed in the script
stdout_ref = self._expandReferenceFileName(self.reference) stdout_ref = self._expandReferenceFileName(self.reference)
......
...@@ -64,7 +64,7 @@ if sys.platform == "win32": ...@@ -64,7 +64,7 @@ if sys.platform == "win32":
import win32pipe import win32pipe
import win32process import win32process
else: else:
import cPickle from six.moves import cPickle
import fcntl import fcntl
import select import select
import qm.sigmask import qm.sigmask
...@@ -1540,7 +1540,7 @@ class GaudiExeTest(ExecTestBase): ...@@ -1540,7 +1540,7 @@ class GaudiExeTest(ExecTestBase):
# positional or keyword # positional or keyword
if a not in positional and a not in kwargs: if a not in positional and a not in kwargs:
kwargs[a] = self.extra_args[a] kwargs[a] = self.extra_args[a]
return apply(self.callable, args, kwargs) return self.callable(*args, **kwargs)
# local names to be exposed in the script # local names to be exposed in the script
exported_symbols = { exported_symbols = {
......
...@@ -178,7 +178,7 @@ def zipdir(directory, no_pyc=False): ...@@ -178,7 +178,7 @@ def zipdir(directory, no_pyc=False):
log.info("File '%s' closed", filename) log.info("File '%s' closed", filename)
else: else:
log.info("Nothing to do on '%s'", filename) log.info("Nothing to do on '%s'", filename)
except UnicodeDecodeError, x: except UnicodeDecodeError as x:
log.error("Wrong encoding in file '%s':", src) log.error("Wrong encoding in file '%s':", src)
log.error(" %s", x) log.error(" %s", x)
log.error("Probably you forgot the line '# -*- coding: utf-8 -*-'") log.error("Probably you forgot the line '# -*- coding: utf-8 -*-'")
......
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import os import os
import sys import sys
import re import re
...@@ -21,7 +21,7 @@ def main(): ...@@ -21,7 +21,7 @@ def main():
project, version, outputfile = args project, version, outputfile = args
if not opts.quiet: if not opts.quiet:
print "Creating %s for %s %s" % (outputfile, project, version) print("Creating %s for %s %s" % (outputfile, project, version))
for style in [lhcb_ver_style, atlas_ver_style, plain_ver_style]: for style in [lhcb_ver_style, atlas_ver_style, plain_ver_style]:
m = re.match(style, version) m = re.match(style, version)
...@@ -38,7 +38,7 @@ def main(): ...@@ -38,7 +38,7 @@ def main():
outdir = os.path.dirname(outputfile) outdir = os.path.dirname(outputfile)
if not os.path.exists(outdir): if not os.path.exists(outdir):
if not opts.quiet: if not opts.quiet:
print "Creating directory", outdir print("Creating directory", outdir)
os.makedirs(outdir) os.makedirs(outdir)
# Prepare data to be written # Prepare data to be written
......
...@@ -111,7 +111,7 @@ class InterfaceCast(object): ...@@ -111,7 +111,7 @@ class InterfaceCast(object):
return ip return ip
else: else:
print "ERROR: queryInterface failed for", obj, "interface:", self.type print "ERROR: queryInterface failed for", obj, "interface:", self.type
except Exception, e: except Exception as e:
print "ERROR: exception", e, "caught when retrieving interface", self.type, "for object", obj print "ERROR: exception", e, "caught when retrieving interface", self.type, "for object", obj
import traceback import traceback
traceback.print_stack() traceback.print_stack()
...@@ -358,7 +358,7 @@ class iProperty(object): ...@@ -358,7 +358,7 @@ class iProperty(object):
for p in props: for p in props:
try: try:
dct[p.name()] = PropertyEntry(p) dct[p.name()] = PropertyEntry(p)
except (ValueError, TypeError), e: except (ValueError, TypeError) as e:
raise ValueError, "gaudimodule.iProperty.properties(): %s%s processing property %s.%s = %s" % \ raise ValueError, "gaudimodule.iProperty.properties(): %s%s processing property %s.%s = %s" % \
(e.__class__.__name__, e.args, (e.__class__.__name__, e.args,
propsFrom, p.name(), p.value()) propsFrom, p.name(), p.value())
...@@ -720,7 +720,7 @@ class iHistogramSvc(iDataSvc): ...@@ -720,7 +720,7 @@ class iHistogramSvc(iDataSvc):
>>> svc = ... >>> svc = ...
>>> histo = svc.book( .... ) >>> histo = svc.book( .... )
""" """
return apply(self._ihs.book, args) return self._ihs.book(*args)
def bookProf(self, *args): def bookProf(self, *args):
""" """
...@@ -728,7 +728,7 @@ class iHistogramSvc(iDataSvc): ...@@ -728,7 +728,7 @@ class iHistogramSvc(iDataSvc):
>>> svc = ... >>> svc = ...
>>> histo = svc.bookProf( .... ) >>> histo = svc.bookProf( .... )
""" """
return apply(self._ihs.bookProf, args) return self._ihs.bookProf(*args)
def __getitem__(self, path): def __getitem__(self, path):
""" """
...@@ -774,7 +774,7 @@ class iNTupleSvc(iDataSvc): ...@@ -774,7 +774,7 @@ class iNTupleSvc(iDataSvc):
iDataSvc.__init__(self, name, ints) iDataSvc.__init__(self, name, ints)
def book(self, *args): def book(self, *args):
return apply(self._ints.book, args) return self._ints.book(*args)
def defineOutput(self, files, typ="Gaudi::RootCnvSvc"): def defineOutput(self, files, typ="Gaudi::RootCnvSvc"):
""" Defines the mapping between logical names and the output file """ Defines the mapping between logical names and the output file
...@@ -1443,7 +1443,7 @@ def getComponentProperties(name): ...@@ -1443,7 +1443,7 @@ def getComponentProperties(name):
obj = factory.instantiate(dummysvc) obj = factory.instantiate(dummysvc)
else: else:
obj = factory.instantiate(svcloc) obj = factory.instantiate(svcloc)
except RuntimeError, text: except RuntimeError as text:
print 'Error instantiating', cname, ' from ', name print 'Error instantiating', cname, ' from ', name
print text print text
continue continue
......
...@@ -18,7 +18,7 @@ from ConfigParser import ConfigParser ...@@ -18,7 +18,7 @@ from ConfigParser import ConfigParser
def svn(*args, **kwargs): def svn(*args, **kwargs):
print "> svn", " ".join(args) print "> svn", " ".join(args)
return apply(Popen, (["svn"] + list(args), ), kwargs) return Popen(["svn"] + list(args), **kwargs)
def svn_ls(url): def svn_ls(url):
...@@ -42,7 +42,7 @@ def svn_exists(url): ...@@ -42,7 +42,7 @@ def svn_exists(url):
def checkout_structure(url, proj, branch): def checkout_structure(url, proj, branch):
def checkout_level(base): def checkout_level(base):
dirs = ["%s/%s" % (base, d) for d in svn_ls(base) if d.endswith("/")] dirs = ["%s/%s" % (base, d) for d in svn_ls(base) if d.endswith("/")]
apply(svn, ["up", "-N"] + dirs).wait() svn("up", "-N", *args).wait()
return dirs return dirs
root = basename(url) root = basename(url)
......
...@@ -5,12 +5,14 @@ Gaudi cmake process (See Instrument.cmake) and create an other file ...@@ -5,12 +5,14 @@ Gaudi cmake process (See Instrument.cmake) and create an other file
understandable by kcachegrind for easy visualization of where we spend time in understandable by kcachegrind for easy visualization of where we spend time in
cmake. cmake.
''' '''
from __future__ import print_function
import sys import sys
def usage(): def usage():
print "Invalid arguments\nProper syntax is :\n %s <log file> <callgrind file>" % sys.argv[ print(
0] "Invalid arguments\nProper syntax is :\n %s <log file> <callgrind file>"
% sys.argv[0])
if len(sys.argv) != 3: if len(sys.argv) != 3:
...@@ -45,7 +47,7 @@ for line in open(sys.argv[1]).readlines(): ...@@ -45,7 +47,7 @@ for line in open(sys.argv[1]).readlines():
elif key == 'ENDTIME': elif key == 'ENDTIME':
sfunc, stime = callStack.pop() sfunc, stime = callStack.pop()
if sfunc != func: if sfunc != func:
print 'Mismatch START/END for %s/%s' % (sfunc, func) print('Mismatch START/END for %s/%s' % (sfunc, func))
sys.exit() sys.exit()
deltatime = int(time) - stime deltatime = int(time) - stime
# add time spent to this function # add time spent to this function
......
...@@ -11,6 +11,7 @@ import platform ...@@ -11,6 +11,7 @@ import platform
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import collections import collections
import re import re
import six
def qmt_filename_to_name(path): def qmt_filename_to_name(path):
...@@ -111,11 +112,11 @@ def analyze_suites(pkg, rootdir): ...@@ -111,11 +112,11 @@ def analyze_suites(pkg, rootdir):
# transpose the dictionary of lists # transpose the dictionary of lists
test_labels = collections.defaultdict(set) test_labels = collections.defaultdict(set)
for label, tests in labels.iteritems(): for label, tests in six.iteritems(labels):
for test in tests: for test in tests:
test_labels[test].add(label) test_labels[test].add(label)
for test, labels in test_labels.iteritems(): for test, labels in six.iteritems(test_labels):
print('set_property(TEST {0} APPEND PROPERTY LABELS {1})'.format( print('set_property(TEST {0} APPEND PROPERTY LABELS {1})'.format(
test, ' '.join(labels))) test, ' '.join(labels)))
......
#!/usr/bin/env python #!/usr/bin/env python
import re import re
from itertools import imap, ifilter import six
from os.path import join, exists, isabs from os.path import join, exists, isabs
...@@ -16,8 +16,10 @@ def find_file(filename, searchpath): ...@@ -16,8 +16,10 @@ def find_file(filename, searchpath):
if isabs(filename): if isabs(filename):
return filename if exists(filename) else None return filename if exists(filename) else None
try: try:
return ifilter(exists, imap(lambda x: join(x, filename), return six.next(
searchpath)).next() six.moves.filter(
exists, six.moves.map(lambda x: join(x, filename),
searchpath)))
except StopIteration: except StopIteration:
return None return None
...@@ -37,11 +39,11 @@ def find_deps(filename, searchpath, deps=None): ...@@ -37,11 +39,11 @@ def find_deps(filename, searchpath, deps=None):
# Look for all "#include" lines in the file, then consider each of the # Look for all "#include" lines in the file, then consider each of the
# included files, ignoring those already included in the recursion # included files, ignoring those already included in the recursion
for included in ifilter( for included in six.moves.filter(
lambda f: f and f not in deps, lambda f: f and f not in deps,
imap( six.moves.map(
lambda m: m and find_file(m.group(1), searchpath),