Skip to content

Commit

Permalink
Merge pull request #3305 from mwichmann/pypy-opens
Browse files Browse the repository at this point in the history
Clean up some tests: use context managers
  • Loading branch information
bdbaddog authored Feb 15, 2019
2 parents 62584f1 + 887f4a1 commit 3931a68
Show file tree
Hide file tree
Showing 21 changed files with 119 additions and 58 deletions.
25 changes: 14 additions & 11 deletions src/engine/SCons/ActionTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,28 @@ def __call__(self):

test.write('act.py', """\
import os, string, sys
f = open(sys.argv[1], 'w')
f.write("act.py: '" + "' '".join(sys.argv[2:]) + "'\\n")
try:
if sys.argv[3]:
f.write("act.py: '" + os.environ[sys.argv[3]] + "'\\n")
except:
pass
f.close()
with open(sys.argv[1], 'w') as f:
f.write("act.py: '" + "' '".join(sys.argv[2:]) + "'\\n")
try:
if sys.argv[3]:
f.write("act.py: '" + os.environ[sys.argv[3]] + "'\\n")
except:
pass
if 'ACTPY_PIPE' in os.environ:
if 'PIPE_STDOUT_FILE' in os.environ:
stdout_msg = open(os.environ['PIPE_STDOUT_FILE'], 'r').read()
with open(os.environ['PIPE_STDOUT_FILE'], 'r') as f:
stdout_msg = f.read()
else:
stdout_msg = "act.py: stdout: executed act.py %s\\n" % ' '.join(sys.argv[1:])
sys.stdout.write( stdout_msg )
if 'PIPE_STDERR_FILE' in os.environ:
stderr_msg = open(os.environ['PIPE_STDERR_FILE'], 'r').read()
with open(os.environ['PIPE_STDERR_FILE'], 'r') as f:
stderr_msg = f.read()
else:
stderr_msg = "act.py: stderr: executed act.py %s\\n" % ' '.join(sys.argv[1:])
sys.stderr.write( stderr_msg )
sys.stderr.write(stderr_msg)
sys.exit(0)
""")

Expand Down
14 changes: 9 additions & 5 deletions src/engine/SCons/BuilderTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ def my_emit(env, sources):
def test_single_source(self):
"""Test Builder with single_source flag set"""
def func(target, source, env):
open(str(target[0]), "w")
open(str(target[0]), "w") # TODO: this just a throwaway?
if (len(source) == 1 and len(target) == 1):
env['CNT'][0] = env['CNT'][0] + 1

Expand Down Expand Up @@ -736,10 +736,12 @@ def test_lists(self):
"""Testing handling lists of targets and source"""
def function2(target, source, env, tlist = [outfile, outfile2], **kw):
for t in target:
open(str(t), 'w').write("function2\n")
with open(str(t), 'w') as f:
f.write("function2\n")
for t in tlist:
if not t in list(map(str, target)):
open(t, 'w').write("function2\n")
with open(t, 'w') as f:
f.write("function2\n")
return 1

env = Environment()
Expand All @@ -765,10 +767,12 @@ def function2(target, source, env, tlist = [outfile, outfile2], **kw):

def function3(target, source, env, tlist = [sub1_out, sub2_out]):
for t in target:
open(str(t), 'w').write("function3\n")
with open(str(t), 'w') as f:
f.write("function3\n")
for t in tlist:
if not t in list(map(str, target)):
open(t, 'w').write("function3\n")
with open(t, 'w') as f:
f.write("function3\n")
return 1

builder = SCons.Builder.Builder(action = function3)
Expand Down
6 changes: 4 additions & 2 deletions src/engine/SCons/SConfTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,14 @@ def test_TryAction(self):
"""Test SConf.TryAction
"""
def actionOK(target, source, env):
open(str(target[0]), "w").write("RUN OK\n")
with open(str(target[0]), "w") as f:
f.write("RUN OK\n")
return None
def actionFAIL(target, source, env):
return 1
def actionUnicode(target, source, env):
open(str(target[0]), "wb").write('2\302\242\n')
with open(str(target[0]), "wb") as f:
f.write('2\302\242\n')
return None


Expand Down
1 change: 1 addition & 0 deletions src/engine/SCons/TaskmasterTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ def test_exception(self):
exception_values = [
"integer division or modulo",
"integer division or modulo by zero",
"integer division by zero", # PyPy2
]
assert str(exc_value) in exception_values, exc_value

Expand Down
3 changes: 2 additions & 1 deletion src/engine/SCons/cppTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,8 @@ def strip_spaces(l, spaces=spaces):
return '\n'.join(map(strip_spaces, lines))

def write(self, file, contents):
open(file, 'w').write(self.strip_initial_spaces(contents))
with open(file, 'w') as f:
f.write(self.strip_initial_spaces(contents))

def test_basic(self):
"""Test basic file inclusion"""
Expand Down
10 changes: 6 additions & 4 deletions test/Batch/generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
test.write('SConstruct', """
def batch_build(target, source, env):
for t, s in zip(target, source):
fp = open(str(t), 'wb')
if str(t) == 'f3.out':
fp.write(open('f3.include', 'rb').read())
fp.write(open(str(s), 'rb').read())
with open(str(t), 'wb') as fp:
if str(t) == 'f3.out':
with open('f3.include', 'rb') as f:
fp.write(f.read())
with open(str(s), 'rb') as f:
fp.write(f.read())
env = Environment()
bb = Action(batch_build, batch_key=True)
env['BUILDERS']['Batch'] = Builder(action=bb)
Expand Down
3 changes: 2 additions & 1 deletion test/CacheDir/NoCache.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
g = '%s'
def ActionWithUndeclaredInputs(target,source,env):
open(target[0].get_abspath(),'w').write(g)
with open(target[0].get_abspath(),'w') as f:
f.write(g)
Command('foo_cached', [], ActionWithUndeclaredInputs)
NoCache(Command('foo_notcached', [], ActionWithUndeclaredInputs))
Expand Down
3 changes: 2 additions & 1 deletion test/Deprecated/TargetSignatures/build-content.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
env = Environment()
def copy1(env, source, target):
open(str(target[0]), 'wb').write(open(str(source[0]), 'rb').read())
with open(str(target[0]), 'wb') as fo, open(str(source[0]), 'rb') as fi:
fo.write(fi.read())
def copy2(env, source, target):
%s
Expand Down
6 changes: 4 additions & 2 deletions test/Fortran/link-with-cxx.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
outfile = open(sys.argv[1][5:], 'wb')
infiles = sys.argv[2:]
for infile in infiles:
outfile.write(open(infile, 'rb').read())
with open(infile, 'rb') as f:
outfile.write(f.read())
outfile.close()
sys.exit(0)
""")
Expand All @@ -69,7 +70,8 @@
def copier(target, source, env):
s = str(source[0])
t = str(target[0])
open(t, 'wb').write(open(s, 'rb').read())
with open(t, 'wb') as fo, open(s, 'rb') as fi:
fo.write(fi.read())
env = Environment(CXX = r'%(_python_)s test_linker.py',
CXXCOM = Action(copier),
SMARTLINK = SCons.Tool.link.smart_link,
Expand Down
8 changes: 4 additions & 4 deletions test/MSVC/generate-rc.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@

test.write(fake_rc, """\
import sys
contents = open(sys.argv[2], 'r').read()
open(sys.argv[1], 'w').write("fake_rc.py\\n" + contents)
with open(sys.argv[1], 'w') as fo, open(sys.argv[2], 'r') as fi:
fo.write("fake_rc.py\\n" + fi.read())
""")

test.write('SConstruct', """
def generate_rc(target, source, env):
t = str(target[0])
s = str(source[0])
tfp = open(t, 'w')
tfp.write('generate_rc\\n' + open(s, 'r').read())
with open(t, 'w') as fo, open(s, 'r') as fi:
fo.write('generate_rc\\n' + fi.read())
env = Environment(tools=['msvc'],
RCCOM=r'%(_python_)s %(fake_rc)s $TARGET $SOURCE')
Expand Down
3 changes: 2 additions & 1 deletion test/NodeOps.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
Import('*')
def mycopy(env, source, target):
open(str(target[0]),'w').write(open(str(source[0]),'r').read())
with open(str(target[0]), 'wt') as fo, open(str(source[0]), 'rt') as fi:
fo.write(fi.read())
def exists_test(node):
before = os.path.exists(str(node)) # doesn't exist yet in VariantDir
Expand Down
6 changes: 4 additions & 2 deletions test/Repository/Local.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def copy(env, source, target):
source = str(source[0])
target = str(target[0])
print('copy() < %s > %s' % (source, target))
open(target, "w").write(open(source, "r").read())
with open(target, 'w') as fo, open(source, 'r') as fi:
fo.write(fi.read())
Build = Builder(action=copy)
env = Environment(BUILDERS={'Build':Build}, BBB='bbb')
Expand All @@ -66,7 +67,8 @@ def copy(env, source, target):
def bbb_copy(env, source, target):
target = str(target[0])
print('bbb_copy()')
open(target, "w").write(open('build/bbb.1', "r").read())
with open(target, 'w') as fo, open('build/bbb.1', 'r') as fi:
fo.write(fi.read())
Import("env")
env.Build('bbb.1', 'bbb.0')
Expand Down
3 changes: 2 additions & 1 deletion test/Repository/option-c.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def copy(env, source, target):
source = str(source[0])
target = str(target[0])
print('copy() < %s > %s' % (source, target))
open(target, "w").write(open(source, "r").read())
with open(target, 'w') as fo, open(source, 'r') as fi:
fo.write(fi.read())
Build = Builder(action=copy)
env = Environment(BUILDERS={'Build':Build})
Expand Down
10 changes: 6 additions & 4 deletions test/Requires/eval-order.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@

test.write('SConstruct', """
def copy_and_create_func(target, source, env):
fp = open(str(target[0]), 'w')
for s in source:
fp.write(open(str(s), 'r').read())
fp.close()
with open(str(target[0]), 'w') as fp:
for s in source:
with open(str(s), 'r') as f:
fp.write(f.read())
open('file.in', 'w').write("file.in 1\\n")
with open('file.in', 'w') as f:
f.write("file.in 1\\n")
return None
copy_and_create = Action(copy_and_create_func)
env = Environment()
Expand Down
10 changes: 7 additions & 3 deletions test/Value.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def __str__(self): return "C=" + str(self.value)
C = Custom(P)
def create(target, source, env):
open(str(target[0]), 'wb').write(source[0].get_contents())
with open(str(target[0]), 'wb') as f:
f.write(source[0].get_contents())
env = Environment()
env['BUILDERS']['B'] = Builder(action = create)
Expand All @@ -62,7 +63,9 @@ def create_value (target, source, env):
target[0].write(source[0].get_contents())
def create_value_file (target, source, env):
open(str(target[0]), 'wb').write(source[0].read())
#open(str(target[0]), 'wb').write(source[0].read())
with open(str(target[0]), 'wb') as f:
f.write(source[0].read())
env['BUILDERS']['B2'] = Builder(action = create_value)
env['BUILDERS']['B3'] = Builder(action = create_value_file)
Expand All @@ -75,7 +78,8 @@ def create_value_file (target, source, env):
test.write('put.py', """\
import os
import sys
open(sys.argv[-1],'w').write(" ".join(sys.argv[1:-2]))
with open(sys.argv[-1],'w') as f:
f.write(" ".join(sys.argv[1:-2]))
""")

# Run all of the tests with both types of source signature
Expand Down
6 changes: 4 additions & 2 deletions test/VariantDir/File-create.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@
test.write(['src', 'SConscript'], """\
#f1_in = File('f1.in')
#Command('f1.out', f1_in, Copy('$TARGET', '$SOURCE'))
#open('f1.in', 'w').write("f1.in\\n")
#with open('f1.in', 'w') as f:
# f.write("f1.in\\n")
f2_in = File('f2.in')
str(f2_in)
Command('f2.out', f2_in, Copy('$TARGET', '$SOURCE'))
open('f2.in', 'w').write("f2.in\\n")
with open('f2.in', 'w') as f:
f.write("f2.in\\n")
""")

test.run(arguments = '--tree=all .')
Expand Down
6 changes: 4 additions & 2 deletions test/chained-build.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@

SConstruct1_contents = """\
def build(env, target, source):
open(str(target[0]), 'wt').write(open(str(source[0]), 'rt').read())
with open(str(target[0]), 'wt') as fo, open(str(source[0]), 'rt') as fi:
fo.write(fi.read())
env=Environment(BUILDERS={'B' : Builder(action=build)})
env.B('foo.mid', 'foo.in')
"""

SConstruct2_contents = """\
def build(env, target, source):
open(str(target[0]), 'wt').write(open(str(source[0]), 'rt').read())
with open(str(target[0]), 'wt') as fo, open(str(source[0]), 'rt') as fi:
fo.write(fi.read())
env=Environment(BUILDERS={'B' : Builder(action=build)})
env.B('foo.out', 'foo.mid')
Expand Down
3 changes: 2 additions & 1 deletion test/implicit-cache/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
SConscript('variant/SConscript', "env")
def copy(target, source, env):
open(str(target[0]), 'wt').write(open(str(source[0]), 'rt').read())
with open(str(target[0]), 'wt') as fo, open(str(source[0]), 'rt') as fi:
fo.write(fi.read())
nodep = env.Command('nodeps.c', 'nodeps.in', action=copy)
env.Program('nodeps', 'nodeps.c')
Expand Down
12 changes: 8 additions & 4 deletions test/sconsign/nonwritable.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@

SConstruct_contents = """\
def build1(target, source, env):
open(str(target[0]), 'wb').write(open(str(source[0]), 'rb').read())
with open(str(target[0]), 'wb') as fo, open(str(source[0]), 'rb') as fi:
fo.write(fi.read())
return None
def build2(target, source, env):
import os
import os.path
open(str(target[0]), 'wb').write(open(str(source[0]), 'rb').read())
with open(str(target[0]), 'wb') as fo, open(str(source[0]), 'rb') as fi:
fo.write(fi.read())
dir, file = os.path.split(str(target[0]))
os.chmod(dir, 0o555)
return None
Expand Down Expand Up @@ -92,8 +94,10 @@ def build2(target, source, env):

test.write(['work2', 'foo.in'], "work2/foo.in\n")

pickle.dump({}, open(work2_sub1__sconsign, 'wb'), 1)
pickle.dump({}, open(work2_sub2__sconsign, 'wb'), 1)
with open(work2_sub1__sconsign, 'wb') as p:
pickle.dump({}, p, 1)
with open(work2_sub2__sconsign, 'wb') as p:
pickle.dump({}, p, 1)

os.chmod(work2_sub1__sconsign, 0o444)

Expand Down
3 changes: 2 additions & 1 deletion testing/framework/TestSCons.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,8 @@ def Qt_dummy_installation(self, dir='qt'):
else: opt_string = opt_string + ' ' + opt
output.write("/* mymoc.py%s */\\n" % opt_string)
for a in args:
contents = open(a, 'r').read()
with open(a, 'r') as f:
contents = f.read()
a = a.replace('\\\\', '\\\\\\\\')
subst = r'{ my_qt_symbol( "' + a + '\\\\n" ); }'
if impl:
Expand Down
Loading

0 comments on commit 3931a68

Please sign in to comment.