# HG changeset patch # User RĂ©mi Cardona # Date 1449766675 -3600 # Node ID 8909593f46d4a840bb0b0c23ad031903fac6ed1c # Parent b1ba18016151b75adf63580f29f191f0ffd39eeb [toolsutils] Small fixes to fill_templated_file() * Use context managers to close _both_ files * Use io.open to enforce text mode on both python 2 and 3 diff -r b1ba18016151 -r 8909593f46d4 toolsutils.py --- a/toolsutils.py Thu Dec 10 18:00:41 2015 +0100 +++ b/toolsutils.py Thu Dec 10 17:57:55 2015 +0100 @@ -22,6 +22,7 @@ # XXX move most of this in logilab.common (shellutils ?) +import io import os, sys import subprocess from os import listdir, makedirs, environ, chmod, walk, remove @@ -153,10 +154,10 @@ shutil.copyfile(fpath, tfpath) def fill_templated_file(fpath, tfpath, context): - fobj = open(tfpath, 'w') - templated = open(fpath).read() - fobj.write(templated % context) - fobj.close() + with io.open(fpath, encoding='ascii') as fobj: + template = fobj.read() + with io.open(tfpath, 'w', encoding='ascii') as fobj: + fobj.write(template % context) def restrict_perms_to_user(filepath, log=None): """set -rw------- permission on the given file"""