Use the 'with' syntax whenever possible

This commit is contained in:
Thomas Nagy 2017-09-12 19:36:43 +02:00
parent 0a0b786259
commit ad5dd18408
No known key found for this signature in database
GPG Key ID: 49B4C67C05277AAA
6 changed files with 43 additions and 101 deletions

View File

@ -4,6 +4,8 @@
"Module called for configuring, compiling and installing targets"
from __future__ import with_statement
import os, shlex, shutil, traceback, errno, sys, stat
from waflib import Utils, Configure, Logs, Options, ConfigSet, Context, Errors, Build, Node
@ -406,11 +408,8 @@ class Dist(Context.Context):
tinfo.gname = 'root'
if os.path.isfile(p):
fu = open(p, 'rb')
try:
tar.addfile(tinfo, fileobj=fu)
finally:
fu.close()
with open(p, 'rb') as f:
tar.addfile(tinfo, fileobj=f)
else:
tar.addfile(tinfo)
@ -532,12 +531,9 @@ class DistCheck(Dist):
"""
import tempfile, tarfile
try:
t = tarfile.open(self.get_arch_name())
with tarfile.open(self.get_arch_name()) as t:
for x in t:
t.extract(x)
finally:
t.close()
instdir = tempfile.mkdtemp('.inst', self.get_base_name())
cmd = self.make_distcheck_cmd(instdir)

View File

@ -27,6 +27,8 @@ Usage::
Usage of the :py:mod:`waflib.Tools.gnu_dirs` is recommended, but not obligatory.
"""
from __future__ import with_statement
import os, re
from waflib import Context, Task, Utils, Logs
import waflib.Tools.ccroot
@ -157,13 +159,12 @@ def apply_intltool_po(self):
linguas = self.path.find_node(os.path.join(podir, 'LINGUAS'))
if linguas:
# scan LINGUAS file for locales to process
file = open(linguas.abspath())
langs = []
for line in file.readlines():
# ignore lines containing comments
if not line.startswith('#'):
langs += line.split()
file.close()
with open(linguas.abspath()) as f:
langs = []
for line in f.readlines():
# ignore lines containing comments
if not line.startswith('#'):
langs += line.split()
re_linguas = re.compile('[-a-zA-Z_@.]+')
for lang in langs:
# Make sure that we only process lines which contain locales

View File

@ -61,6 +61,8 @@ The detection uses pkg-config on Linux by default. To force static library detec
QT5_XCOMPILE=1 QT5_FORCE_STATIC=1 waf configure
"""
from __future__ import with_statement
try:
from xml.sax import make_parser
from xml.sax.handler import ContentHandler
@ -366,11 +368,8 @@ class rcc(Task.Task):
parser = make_parser()
curHandler = XMLHandler()
parser.setContentHandler(curHandler)
fi = open(self.inputs[0].abspath(), 'r')
try:
parser.parse(fi)
finally:
fi.close()
with open(self.inputs[0].abspath(), 'r') as f:
parser.parse(f)
nodes = []
names = []

View File

@ -9,6 +9,8 @@ The portability fixes try to provide a consistent behavior of the Waf API
through Python versions 2.5 to 3.X and across different platforms (win32, linux, etc)
"""
from __future__ import with_statement
import atexit, os, sys, errno, inspect, re, datetime, platform, base64, signal, functools, time
try:
@ -209,21 +211,15 @@ def readf(fname, m='r', encoding='latin-1'):
if sys.hexversion > 0x3000000 and not 'b' in m:
m += 'b'
f = open(fname, m)
try:
with open(fname, m) as f:
txt = f.read()
finally:
f.close()
if encoding:
txt = txt.decode(encoding)
else:
txt = txt.decode()
else:
f = open(fname, m)
try:
with open(fname, m) as f:
txt = f.read()
finally:
f.close()
return txt
def writef(fname, data, m='w', encoding='latin-1'):
@ -248,11 +244,8 @@ def writef(fname, data, m='w', encoding='latin-1'):
if sys.hexversion > 0x3000000 and not 'b' in m:
data = data.encode(encoding)
m += 'b'
f = open(fname, m)
try:
with open(fname, m) as f:
f.write(data)
finally:
f.close()
def h_file(fname):
"""
@ -264,14 +257,11 @@ def h_file(fname):
:return: hash of the file contents
:rtype: string or bytes
"""
f = open(fname, 'rb')
m = md5()
try:
with open(fname, 'rb') as f:
while fname:
fname = f.read(200000)
m.update(fname)
finally:
f.close()
return m.digest()
def readf_win32(f, m='r', encoding='latin-1'):
@ -287,21 +277,15 @@ def readf_win32(f, m='r', encoding='latin-1'):
if sys.hexversion > 0x3000000 and not 'b' in m:
m += 'b'
f = os.fdopen(fd, m)
try:
with os.fdopen(fd, m) as f:
txt = f.read()
finally:
f.close()
if encoding:
txt = txt.decode(encoding)
else:
txt = txt.decode()
else:
f = os.fdopen(fd, m)
try:
with os.fdopen(fd, m) as f:
txt = f.read()
finally:
f.close()
return txt
def writef_win32(f, data, m='w', encoding='latin-1'):
@ -317,25 +301,19 @@ def writef_win32(f, data, m='w', encoding='latin-1'):
fd = os.open(f, flags)
except OSError:
raise OSError('Cannot write to %r' % f)
f = os.fdopen(fd, m)
try:
with os.fdopen(fd, m) as f:
f.write(data)
finally:
f.close()
def h_file_win32(fname):
try:
fd = os.open(fname, os.O_BINARY | os.O_RDONLY | os.O_NOINHERIT)
except OSError:
raise OSError('Cannot read from %r' % fname)
f = os.fdopen(fd, 'rb')
m = md5()
try:
with os.fdopen(fd, 'rb') as f:
while fname:
fname = f.read(200000)
m.update(fname)
finally:
f.close()
return m.digest()
# always save these

View File

@ -2,6 +2,8 @@
# encoding: utf-8
# Thomas Nagy, 2010-2017 (ita)
from __future__ import with_statement
import os
all_modifs = {}
@ -26,19 +28,13 @@ def modif(dir, name, fun):
return
filename = os.path.join(dir, name)
f = open(filename, 'r')
try:
with open(filename, 'r') as f:
txt = f.read()
finally:
f.close()
txt = fun(txt)
f = open(filename, 'w')
try:
with open(filename, 'w') as f:
f.write(txt)
finally:
f.close()
def subst(*k):
"""register a substitution function"""

52
wscript
View File

@ -9,6 +9,8 @@ To add a tool that does not exist in the folder compat15, pass an absolute path:
./waf-light --tools=compat15,/comp/waf/aba.py --prelude=$'\tfrom waflib.extras import aba\n\taba.foo()'
"""
from __future__ import with_statement
VERSION="2.0.0"
APPNAME='waf'
REVISION=''
@ -28,21 +30,15 @@ from waflib import Configure
Configure.autoconfig = 1
def sub_file(fname, lst):
f = open(fname, 'rU')
try:
with open(fname, 'rU') as f:
txt = f.read()
finally:
f.close()
for (key, val) in lst:
re_pat = re.compile(key, re.M)
txt = re_pat.sub(val, txt)
f = open(fname, 'w')
try:
with open(fname, 'w') as f:
f.write(txt)
finally:
f.close()
def to_bytes(x):
if sys.hexversion>0x300000f:
@ -189,25 +185,16 @@ def sfilter(path):
if path.endswith('.py') :
if Options.options.strip_comments:
if sys.version_info[0] >= 3:
f = open(path, 'rb')
try:
with open(path, 'rb') as f:
tk = tokenize.tokenize(f.readline)
next(tk) # the first one is always tokenize.ENCODING for Python 3, ignore it
cnt = process_tokens(tk)
finally:
f.close()
else:
f = open(path, 'r')
try:
with open(path, 'r') as f:
cnt = process_tokens(tokenize.generate_tokens(f.readline))
finally:
f.close()
else:
f = open(path, 'r')
try:
with open(path, 'r') as f:
cnt = f.read()
finally:
f.close()
# WARNING: since python >= 2.5 is required, decorators are not processed anymore
# uncomment the following to enable decorator replacement:
#cnt = process_decorators(cnt)
@ -216,11 +203,8 @@ def sfilter(path):
cnt = '#! /usr/bin/env python\n# encoding: utf-8\n# WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file\n\n' + cnt
else:
f = open(path, 'r')
try:
with open(path, 'r') as f:
cnt = f.read()
finally:
f.close()
if sys.hexversion > 0x030000f0:
return (io.BytesIO(cnt.encode('utf-8')), len(cnt.encode('utf-8')), cnt)
@ -331,11 +315,8 @@ def create_waf(self, *k, **kw):
tar.close()
z.close()
f = open('waf-light', 'rU')
try:
with open('waf-light', 'rU') as f:
code1 = f.read()
finally:
f.close()
# now store the revision unique number in waf
code1 = code1.replace("if sys.hexversion<0x206000f:\n\traise ImportError('Python >= 2.6 is required to create the waf file')\n", '')
@ -363,11 +344,8 @@ def create_waf(self, *k, **kw):
elif zipType == 'xz':
code1 = code1.replace('bunzip2', 'xz -d')
f = open('%s.tar.%s' % (mw, zipType), 'rb')
try:
with open('%s.tar.%s' % (mw, zipType), 'rb') as f:
cnt = f.read()
finally:
f.close()
# the REVISION value is the md5 sum of the compressed data (facilitate audits)
m = md5()
@ -396,8 +374,7 @@ def create_waf(self, *k, **kw):
if getattr(Options.options, 'interpreter', None):
ccc = ccc.replace('#!/usr/bin/env python', Options.options.interpreter)
f = open('waf', 'wb')
try:
with open('waf', 'wb') as f:
f.write(ccc.encode())
f.write(to_bytes('#==>\n#'))
f.write(cnt)
@ -419,16 +396,11 @@ def create_waf(self, *k, **kw):
f.write(sig)
f.write('\n')
os.remove('waf.asc')
finally:
f.close()
if sys.platform == 'win32' or Options.options.make_batch:
f = open('waf.bat', 'w')
try:
with open('waf.bat', 'w') as f:
f.write('@setlocal\n@set PYEXE=python\n@where %PYEXE% 1>NUL 2>NUL\n@if %ERRORLEVEL% neq 0 set PYEXE=py\n@%PYEXE% -x "%~dp0waf" %*\n@exit /b %ERRORLEVEL%\n')
finally:
f.close()
if sys.platform != 'win32':
os.chmod('waf', Utils.O755)