Skip to content
Snippets Groups Projects
Commit 67fe08b1 authored by scott snyder's avatar scott snyder Committed by scott snyder
Browse files

PyUtils: Initial py3 porting.

Not yet intended to be complete --- only what's needed by basic Athena
and configuration jobs.
parent 9cf0b1ce
No related branches found
No related tags found
No related merge requests found
# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
# @author: Sebastien Binet <binet@cern.ch> # @author: Sebastien Binet <binet@cern.ch>
# @date: March 2008 # @date: March 2008
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
# from here: # from here:
# http://www.phyast.pitt.edu/~micheles/python/documentation.html # http://www.phyast.pitt.edu/~micheles/python/documentation.html
# #
from __future__ import with_statement from __future__ import with_statement, print_function
__version__ = "$Revision$" __version__ = "$Revision$"
__author__ = "Sebastien Binet <binet@cern.ch>" __author__ = "Sebastien Binet <binet@cern.ch>"
...@@ -14,7 +14,7 @@ __author__ = "Sebastien Binet <binet@cern.ch>" ...@@ -14,7 +14,7 @@ __author__ = "Sebastien Binet <binet@cern.ch>"
__all__ = [ __all__ = [
'memoize', 'memoize',
'forking', 'forking',
'async', 'async_decor',
] ]
import sys import sys
...@@ -45,7 +45,6 @@ def memoize(func, *args): ...@@ -45,7 +45,6 @@ def memoize(func, *args):
# FIXME: does not work... func is an instance of FunctionMaker which cannot # FIXME: does not work... func is an instance of FunctionMaker which cannot
# be pickled... # be pickled...
import __builtin__
@decorator @decorator
def mp_forking(func, *args, **kwargs): def mp_forking(func, *args, **kwargs):
import multiprocessing as mp import multiprocessing as mp
...@@ -59,7 +58,7 @@ def mp_forking(func, *args, **kwargs): ...@@ -59,7 +58,7 @@ def mp_forking(func, *args, **kwargs):
try: try:
res = func(*args, **kwargs) res = func(*args, **kwargs)
# catch *everything* and 're-raise' # catch *everything* and 're-raise'
except BaseException,err: except BaseException as err:
#import traceback; traceback.print_exc() #import traceback; traceback.print_exc()
res = err res = err
q.put(res) q.put(res)
...@@ -82,7 +81,7 @@ def reraise_exception(new_exc, exc_info=None): ...@@ -82,7 +81,7 @@ def reraise_exception(new_exc, exc_info=None):
if exc_info is None: if exc_info is None:
exc_info = sys.exc_info() exc_info = sys.exc_info()
_exc_class, _exc, tb = exc_info _exc_class, _exc, tb = exc_info
raise new_exc.__class__, new_exc, tb raise new_exc.__class__ (new_exc, tb)
@decorator @decorator
def forking(func, *args, **kwargs): def forking(func, *args, **kwargs):
...@@ -122,17 +121,17 @@ def forking(func, *args, **kwargs): ...@@ -122,17 +121,17 @@ def forking(func, *args, **kwargs):
try: try:
result = func(*args, **kwargs) result = func(*args, **kwargs)
status = 0 status = 0
except (Exception, KeyboardInterrupt), exc: except (Exception, KeyboardInterrupt) as exc:
import traceback import traceback
exc_string = traceback.format_exc(limit=10) exc_string = traceback.format_exc(limit=10)
for l in exc_string.splitlines(): for l in exc_string.splitlines():
print "[%d]"%os.getpid(),l.rstrip() print ("[%d]"%os.getpid(),l.rstrip())
result = exc, exc_string result = exc, exc_string
status = 1 status = 1
with os.fdopen(pwrite, 'wb') as f: with os.fdopen(pwrite, 'wb') as f:
try: try:
pickle.dump((status,result), f, pickle.HIGHEST_PROTOCOL) pickle.dump((status,result), f, pickle.HIGHEST_PROTOCOL)
except pickle.PicklingError, exc: except pickle.PicklingError as exc:
pickle.dump((2,exc), f, pickle.HIGHEST_PROTOCOL) pickle.dump((2,exc), f, pickle.HIGHEST_PROTOCOL)
os._exit(0) os._exit(0)
pass # forking pass # forking
...@@ -147,7 +146,7 @@ def _async_on_success(result): # default implementation ...@@ -147,7 +146,7 @@ def _async_on_success(result): # default implementation
def _async_on_failure(exc_info): # default implementation def _async_on_failure(exc_info): # default implementation
"Called if the function fails" "Called if the function fails"
_exc_class, _exc, tb = exc_info _exc_class, _exc, tb = exc_info
raise _exc_class, _exc, tb raise _exc_class (_exc, tb)
pass pass
def _async_on_closing(): # default implementation def _async_on_closing(): # default implementation
...@@ -193,7 +192,7 @@ class Async(object): ...@@ -193,7 +192,7 @@ class Async(object):
return thread return thread
# default async decorator: using processes # default async decorator: using processes
def async(async_type='mp'): def async_decor(async_type='mp'):
if async_type in ("mp", "multiprocessing"): if async_type in ("mp", "multiprocessing"):
from multiprocessing import Process from multiprocessing import Process
factory = Process factory = Process
......
# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
# @author: Sebastien Binet <binet@cern.ch> # @author: Sebastien Binet <binet@cern.ch>
# @date: March 2007 # @date: March 2007
# #
# #
from __future__ import with_statement from __future__ import with_statement, print_function
__version__ = "$Revision$" __version__ = "$Revision$"
__author__ = "Sebastien Binet <binet@cern.ch>" __author__ = "Sebastien Binet <binet@cern.ch>"
import sys import sys
import os import os
import six
from AthenaCommon.Logging import log from AthenaCommon.Logging import log
def ROOT6Setup(): def ROOT6Setup():
log.info('executing ROOT6Setup') log.info('executing ROOT6Setup')
import __builtin__ if six.PY3:
oldimporthook = __builtin__.__import__ import builtins as builtin_mod
else:
import __builtin__ as builtin_mod
oldimporthook = builtin_mod.__import__
autoload_var_name = 'ROOT6_NamespaceAutoloadHook' autoload_var_name = 'ROOT6_NamespaceAutoloadHook'
def root6_importhook(name, globals={}, locals={}, fromlist=[], level=-1): def root6_importhook(name, globals={}, locals={}, fromlist=[], level=-1):
if six.PY3 and level < 0: level = 0
m = oldimporthook(name, globals, locals, fromlist, level) m = oldimporthook(name, globals, locals, fromlist, level)
if m and (m.__name__== 'ROOT' or name[0:4]=='ROOT'): if m and (m.__name__== 'ROOT' or name[0:4]=='ROOT'):
log.debug('Python import module=%s fromlist=%s'%(name, str(fromlist))) log.debug('Python import module=%s fromlist=%s'%(name, str(fromlist)))
...@@ -40,7 +45,7 @@ def ROOT6Setup(): ...@@ -40,7 +45,7 @@ def ROOT6Setup():
pass pass
return m return m
__builtin__.__import__ = root6_importhook builtin_mod.__import__ = root6_importhook
...@@ -83,6 +88,11 @@ class ShutUp(object): ...@@ -83,6 +88,11 @@ class ShutUp(object):
os.dup2( sys.stderr.fileno(), self.save_err.fileno() ) os.dup2( sys.stderr.fileno(), self.save_err.fileno() )
os.dup2( sys.stdout.fileno(), self.save_out.fileno() ) os.dup2( sys.stdout.fileno(), self.save_out.fileno() )
return return
def __del__ (self):
self.save_err.close()
self.save_out.close()
return
def mute(self): def mute(self):
if not self._dummy: if not self._dummy:
...@@ -108,7 +118,7 @@ class ShutUp(object): ...@@ -108,7 +118,7 @@ class ShutUp(object):
if re.match(filter, l): if re.match(filter, l):
printOut = False printOut = False
if printOut: if printOut:
print "PyRoot:",l.replace("\n","") print ("PyRoot:",l.replace("\n",""))
pass pass
return return
......
# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
# @file PyUtils.RootUtils # @file PyUtils.RootUtils
# @author Sebastien Binet # @author Sebastien Binet
# @purpose a few utils to ease the day-to-day work with ROOT # @purpose a few utils to ease the day-to-day work with ROOT
# @date November 2009 # @date November 2009
from __future__ import with_statement from __future__ import with_statement, print_function
__doc__ = "a few utils to ease the day-to-day work with ROOT" __doc__ = "a few utils to ease the day-to-day work with ROOT"
__version__ = "$Revision: 739816 $" __version__ = "$Revision: 739816 $"
...@@ -81,7 +81,7 @@ def root_compile(src=None, fname=None, batch=True): ...@@ -81,7 +81,7 @@ def root_compile(src=None, fname=None, batch=True):
import tempfile import tempfile
src_file = tempfile.NamedTemporaryFile(prefix='root_aclic_', src_file = tempfile.NamedTemporaryFile(prefix='root_aclic_',
suffix='.cxx') suffix='.cxx')
src_file.write(textwrap.dedent(src)) src_file.write(textwrap.dedent(src).encode())
src_file.flush() src_file.flush()
src_file.seek(0) src_file.seek(0)
fname = src_file.name fname = src_file.name
...@@ -146,15 +146,15 @@ def _pythonize_tfile(): ...@@ -146,15 +146,15 @@ def _pythonize_tfile():
FIXME: probably doesn't follow python file-like conventions... FIXME: probably doesn't follow python file-like conventions...
""" """
SZ = 4096 SZ = 4096
if size>=0: if size>=0:
#size = _adjust_sz(size) #size = _adjust_sz(size)
#print "-->0",self.tell(),size #print ("-->0",self.tell(),size)
c_buf = read_root_file(self, size) c_buf = read_root_file(self, size)
if c_buf and c_buf.sz: if c_buf and c_buf.sz:
#print "-->1",self.tell(),c_buf.sz #print ("-->1",self.tell(),c_buf.sz)
#self.seek(c_buf.sz+self.tell()) #self.seek(c_buf.sz+self.tell())
#print "-->2",self.tell() #print ("-->2",self.tell())
buf = c_buf.buffer() buf = c_buf.buffer()
_set_byte_size (buf, c_buf.sz) _set_byte_size (buf, c_buf.sz)
return str(buf[:]) return str(buf[:])
...@@ -272,32 +272,32 @@ class RootFileDumper(object): ...@@ -272,32 +272,32 @@ class RootFileDumper(object):
_n = int(itr_entries) _n = int(itr_entries)
itr_entries = xrange(_n) itr_entries = xrange(_n)
except ValueError: except ValueError:
print "** err ** invalid 'itr_entries' argument. will iterate over all entries." print ("** err ** invalid 'itr_entries' argument. will iterate over all entries.")
itr_entries = xrange(nentries) itr_entries = xrange(nentries)
else: else:
itr_entries = xrange(itr_entries) itr_entries = xrange(itr_entries)
for ientry in itr_entries: for ientry in itr_entries:
hdr = ":: entry [%05i]..." % (ientry,) hdr = ":: entry [%05i]..." % (ientry,)
#print hdr #print (hdr)
#print >> self.fout, hdr #print (hdr, file=self.fout)
err = tree.LoadTree(ientry) err = tree.LoadTree(ientry)
if err < 0: if err < 0:
print "**err** loading tree for entry",ientry print ("**err** loading tree for entry",ientry)
self.allgood = False self.allgood = False
break break
nbytes = tree.GetEntry(ientry) nbytes = tree.GetEntry(ientry)
if nbytes <= 0: if nbytes <= 0:
print "**err** reading entry [%s] of tree [%s]" % (ientry, tree_name) print ("**err** reading entry [%s] of tree [%s]" % (ientry, tree_name))
hdr = ":: entry [%05i]... [ERR]" % (ientry,) hdr = ":: entry [%05i]... [ERR]" % (ientry,)
print hdr print (hdr)
self.allgood = False self.allgood = False
continue continue
for br_name in leaves: for br_name in leaves:
hdr = ":: branch [%s]..." % (br_name,) hdr = ":: branch [%s]..." % (br_name,)
#print hdr #print (hdr)
#tree.GetBranch(br_name).GetEntry(ientry) #tree.GetBranch(br_name).GetEntry(ientry)
py_name = [br_name] py_name = [br_name]
...@@ -313,15 +313,15 @@ class RootFileDumper(object): ...@@ -313,15 +313,15 @@ class RootFileDumper(object):
else: else:
val = tuple(vals) val = tuple(vals)
if not (val is None): if not (val is None):
#print "-->",val,br_name #print ("-->",val,br_name)
try: try:
vals = _pythonize(val, py_name, True) vals = _pythonize(val, py_name, True)
except Exception, err: except Exception as err:
print "**err** for branch [%s] val=%s (type=%s)" % ( print ("**err** for branch [%s] val=%s (type=%s)" % (
br_name, val, type(val), br_name, val, type(val),
) ))
self.allgood = False self.allgood = False
print err print (err)
for o in vals: for o in vals:
n = map(str, o[0]) n = map(str, o[0])
v = o[1] v = o[1]
...@@ -336,9 +336,11 @@ def _test_main(): ...@@ -336,9 +336,11 @@ def _test_main():
root = import_root() root = import_root()
def no_raise(msg, fct, *args, **kwds): def no_raise(msg, fct, *args, **kwds):
caught = False caught = False
err = None
try: try:
fct(*args, **kwds) fct(*args, **kwds)
except Exception, err: except Exception as xerr:
err = xerr
caught = True caught = True
assert not caught, "%s:\n%s\nERROR" % (msg, err,) assert not caught, "%s:\n%s\nERROR" % (msg, err,)
...@@ -351,12 +353,12 @@ def _test_main(): ...@@ -351,12 +353,12 @@ def _test_main():
# PvG workaround for ROOT-7059 # PvG workaround for ROOT-7059
dummy = tempfile.NamedTemporaryFile(prefix="foo_",suffix=".cxx") dummy = tempfile.NamedTemporaryFile(prefix="foo_",suffix=".cxx")
with tempfile.NamedTemporaryFile(prefix="foo_",suffix=".cxx") as tmp: with tempfile.NamedTemporaryFile(prefix="foo_",suffix=".cxx") as tmp:
print >> tmp, "void foo2() { return ; }" tmp.write (b"void foo2() { return ; }\n")
tmp.flush() tmp.flush()
no_raise("problem compiling a file", no_raise("problem compiling a file",
fct=root_compile, fname=tmp.name) fct=root_compile, fname=tmp.name)
print "OK" print ("OK")
return True return True
if __name__ == "__main__": if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment