Skip to content

Commit 8279ec9

Browse files
committed
update tests, examples, dir.jl, and random.jl to use write(filename,...)
in place of one-line open(filname) do io ...
1 parent 041af9f commit 8279ec9

File tree

12 files changed

+89
-147
lines changed

12 files changed

+89
-147
lines changed

base/pkg/dir.jl

+2-7
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ function init(meta::AbstractString=DEFAULT_META, branch::AbstractString=META_BRA
5151
end
5252
touch("REQUIRE")
5353
touch("META_BRANCH")
54-
open("META_BRANCH", "w") do io
55-
write(io, branch)
56-
close(io)
57-
end
54+
write("META_BRANCH", branch)
5855
end
5956
#Move TEMP to METADATA
6057
Base.mv(joinpath(temp_dir,"METADATA"), metadata_dir)
@@ -70,9 +67,7 @@ end
7067

7168
function getmetabranch()
7269
try
73-
open(joinpath(path(),"META_BRANCH")) do io
74-
chomp(readuntil(io, "/n"))
75-
end
70+
readline(joinpath(path(),"META_BRANCH"))
7671
catch err
7772
META_BRANCH
7873
end

base/random.jl

+1-5
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,7 @@ function make_seed(n::Integer)
168168
end
169169

170170
function make_seed(filename::AbstractString, n::Integer)
171-
open(filename) do io
172-
a = Array(UInt32, Int(n))
173-
read!(io, a)
174-
a
175-
end
171+
read!(filename, Array(UInt32, Int(n)))
176172
end
177173

178174
## srand()

examples/wordcount.jl

+1-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ end
7575
function wordcount_files(result_file,inputs...)
7676
text = ""
7777
for file in inputs
78-
open(file) do f
79-
text *= readstring(f)
80-
end
78+
text *= readstring(file)
8179
end
8280
wc = parallel_wordcount(text)
8381
open(result_file,"w") do f

test/cmdlineargs.jl

+5-7
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ let exename = `$(joinpath(JULIA_HOME, Base.julia_exename())) --precompiled=yes`
4545
# --load
4646
let testfile = tempname()
4747
try
48-
open(testfile, "w") do io
49-
println(io, "testvar = :test")
50-
end
48+
write(testfile, "testvar = :test\n")
5149
@test split(readchomp(`$exename --load=$testfile -P "println(testvar)"`), '\n')[end] == "test"
5250
@test split(readchomp(`$exename -P "println(testvar)" -L $testfile`), '\n')[end] == "test"
5351
finally
@@ -210,10 +208,10 @@ let exename = `$(joinpath(JULIA_HOME, Base.julia_exename())) --precompiled=yes`
210208
let testfile = tempname()
211209
try
212210
# write a julia source file that just prints ARGS to STDOUT and exits
213-
open(testfile, "w") do io
214-
println(io, "println(ARGS)")
215-
println(io, "exit(0)")
216-
end
211+
write(testfile, """
212+
println(ARGS)
213+
exit(0)
214+
""")
217215
@test readchomp(`$exename $testfile foo -bar --baz`) == "UTF8String[\"foo\",\"-bar\",\"--baz\"]"
218216
@test readchomp(`$exename $testfile -- foo -bar --baz`) == "UTF8String[\"foo\",\"-bar\",\"--baz\"]"
219217
@test readchomp(`$exename -L $testfile -- foo -bar --baz`) == "UTF8String[\"foo\",\"-bar\",\"--baz\"]"

test/compile.jl

+43-47
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,19 @@ Foo_module = :Foo4b3a94a1a081a8cb
1010
try
1111
Foo_file = joinpath(dir, "$Foo_module.jl")
1212

13-
open(Foo_file, "w") do f
14-
print(f, """
15-
__precompile__(true)
16-
module $Foo_module
17-
@doc "foo function" foo(x) = x + 1
18-
include_dependency("foo.jl")
19-
include_dependency("foo.jl")
20-
module Bar
21-
@doc "bar function" bar(x) = x + 2
22-
include_dependency("bar.jl")
23-
end
24-
end
25-
""")
26-
end
13+
write(Foo_file,
14+
"""
15+
__precompile__(true)
16+
module $Foo_module
17+
@doc "foo function" foo(x) = x + 1
18+
include_dependency("foo.jl")
19+
include_dependency("foo.jl")
20+
module Bar
21+
@doc "bar function" bar(x) = x + 2
22+
include_dependency("bar.jl")
23+
end
24+
end
25+
""")
2726

2827
# Issue #12623
2928
@test __precompile__(true) === nothing
@@ -51,25 +50,25 @@ try
5150
end
5251

5352
Baz_file = joinpath(dir, "Baz.jl")
54-
open(Baz_file, "w") do f
55-
print(f, """
56-
__precompile__(false)
57-
module Baz
58-
end
59-
""")
60-
end
53+
write(Baz_file,
54+
"""
55+
__precompile__(false)
56+
module Baz
57+
end
58+
""")
59+
6160
println(STDERR, "\nNOTE: The following 'LoadError: __precompile__(false)' indicates normal operation")
6261
@test_throws ErrorException Base.compilecache("Baz") # from __precompile__(false)
6362

6463
# Issue #12720
6564
FooBar_file = joinpath(dir, "FooBar.jl")
66-
open(FooBar_file, "w") do f
67-
print(f, """
68-
__precompile__(true)
69-
module FooBar
70-
end
71-
""")
72-
end
65+
write(FooBar_file,
66+
"""
67+
__precompile__(true)
68+
module FooBar
69+
end
70+
""")
71+
7372
Base.compilecache("FooBar")
7473
sleep(2)
7574
@test isfile(joinpath(dir, "FooBar.ji"))
@@ -82,14 +81,14 @@ try
8281
@test Base.stale_cachefile(FooBar_file, joinpath(dir, "FooBar.ji"))
8382
@test !Base.stale_cachefile(FooBar_file, joinpath(dir2, "FooBar.ji"))
8483

85-
open(FooBar_file, "w") do f
86-
print(f, """
87-
__precompile__(true)
88-
module FooBar
89-
error("break me")
90-
end
91-
""")
92-
end
84+
write(FooBar_file,
85+
"""
86+
__precompile__(true)
87+
module FooBar
88+
error("break me")
89+
end
90+
""")
91+
9392
println(STDERR, "\nNOTE: The following 'LoadError: break me' indicates normal operation")
9493
@test_throws ErrorException Base.require(:FooBar)
9594

@@ -106,14 +105,13 @@ let dir = mktempdir(),
106105
Time_module = :Time4b3a94a1a081a8cb
107106

108107
try
109-
open(joinpath(dir, "$Time_module.jl"), "w") do io
110-
write(io, """
111-
module $Time_module
112-
__precompile__(true)
113-
time = Base.time()
114-
end
115-
""")
116-
end
108+
write(joinpath(dir, "$Time_module.jl"),
109+
"""
110+
module $Time_module
111+
__precompile__(true)
112+
time = Base.time()
113+
end
114+
""")
117115

118116
eval(quote
119117
insert!(LOAD_PATH, 1, $(dir))
@@ -151,9 +149,7 @@ let module_name = string("a",randstring())
151149
file_name = string(module_name, ".jl")
152150
touch(file_name)
153151
code = """module $(module_name)\nend\n"""
154-
open(file_name, "w") do file
155-
write(file, code)
156-
end
152+
write(file_name, code)
157153
reload(module_name)
158154
@test typeof(eval(symbol(module_name))) == Module
159155
deleteat!(LOAD_PATH,1)

test/datafmt.jl

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
@test countlines(IOBuffer("\n \n \n \n \n \n \n \n \n \n")) == 10
88
@test countlines(IOBuffer("\r\n \r\n \r\n \r\n \r\n")) == 5
99
file = tempname()
10-
open(file,"w") do f
11-
write(f,"Spiffy header\nspectacular first row\neven better 2nd row\nalmost done\n")
12-
end
10+
write(file,"Spiffy header\nspectacular first row\neven better 2nd row\nalmost done\n")
1311
@test countlines(file) == 4
1412
@test countlines(file,'\r') == 0
1513
@test countlines(file,'\n') == 4

test/file.jl

+12-30
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,7 @@ bfile = joinpath(dir, "b.txt")
337337
cp(afile, bfile)
338338

339339
cfile = joinpath(dir, "c.txt")
340-
open(cfile, "w") do cf
341-
write(cf, "This is longer than the contents of afile")
342-
end
340+
write(cfile, "This is longer than the contents of afile")
343341
cp(afile, cfile; remove_destination=true)
344342

345343
a_stat = stat(afile)
@@ -410,13 +408,9 @@ if @unix? true : (Base.windows_version() >= Base.WINDOWS_VISTA_VER)
410408

411409
cfile = joinpath(srcdir, "c.txt")
412410
file_txt = "This is some text with unicode - 这是一个文件"
413-
open(cfile, "w") do cf
414-
write(cf, file_txt)
415-
end
411+
write(cfile, file_txt)
416412
hidden_cfile = joinpath(hidden_srcsubdir, "c.txt")
417-
open(hidden_cfile, "w") do cf
418-
write(cf, file_txt)
419-
end
413+
write(hidden_cfile, file_txt)
420414

421415
abs_dirlink_cp = joinpath(tmpdir, "abs_dirlink_cp")
422416
hidden_srcsubdir_cp = joinpath(tmpdir, ".hidden_srcsubdir_cp")
@@ -543,12 +537,9 @@ if @unix? true : (Base.windows_version() >= Base.WINDOWS_VISTA_VER)
543537
mkdir(subdir1)
544538

545539
cfile = abspath(joinpath(maindir, "c.txt"))
546-
open(cfile, "w") do cf
547-
write(cf, "This is c.txt - 这是一个文件")
548-
end
549-
open(abspath(joinpath(targetdir, "file1.txt")), "w") do cf
550-
write(cf, "This is file1.txt - 这是一个文件")
551-
end
540+
write(cfile, "This is c.txt - 这是一个文件")
541+
write(abspath(joinpath(targetdir, "file1.txt")),
542+
"This is file1.txt - 这是一个文件")
552543

553544
abs_dl = joinpath(maindir, "abs_linkto_targetdir")
554545
symlink(targetdir, abs_dl)
@@ -616,12 +607,8 @@ end
616607
srcfile_new = joinpath(tmpdir, "srcfile_new.txt")
617608
hidden_srcfile_new = joinpath(tmpdir, ".hidden_srcfile_new.txt")
618609
file_txt = "This is some text with unicode - 这是一个文件"
619-
open(srcfile, "w") do f
620-
write(f, file_txt)
621-
end
622-
open(hidden_srcfile, "w") do f
623-
write(f, file_txt)
624-
end
610+
write(srcfile, file_txt)
611+
write(hidden_srcfile, file_txt)
625612
abs_filelink = joinpath(tmpdir, "abs_filelink")
626613
symlink(abspath(srcfile), abs_filelink)
627614
cd(tmpdir)
@@ -701,9 +688,7 @@ end
701688
# Test remove the existing path first and copy an other file
702689
otherfile = joinpath(tmpdir, "otherfile.txt")
703690
otherfile_content = "This is otherfile.txt with unicode - 这是一个文件"
704-
open(otherfile, "w") do f
705-
write(f, otherfile_content)
706-
end
691+
write(otherfile, otherfile_content)
707692
for d in test_new_paths1
708693
cp(otherfile, d; remove_destination=true, follow_symlinks=false)
709694
# Expect no link because a file is copied (follow_symlinks=false does not effect this)
@@ -753,12 +738,9 @@ end
753738
mkdir(subdir1)
754739

755740
cfile = abspath(joinpath(maindir, "c.txt"))
756-
open(cfile, "w") do cf
757-
write(cf, "This is c.txt - 这是一个文件")
758-
end
759-
open(abspath(joinpath(targetdir, "file1.txt")), "w") do cf
760-
write(cf, "This is file1.txt - 这是一个文件")
761-
end
741+
write(cfile, "This is c.txt - 这是一个文件")
742+
write(abspath(joinpath(targetdir, "file1.txt")),
743+
"This is file1.txt - 这是一个文件")
762744

763745
abs_fl = joinpath(maindir, "abs_linkto_c.txt")
764746
symlink(cfile, abs_fl)

test/mmap.jl

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# This file is a part of Julia. License is MIT: http://julialang.org/license
22

33
file = tempname()
4-
s = open(file, "w") do f
5-
write(f, "Hello World\n")
6-
end
4+
write(file, "Hello World\n")
75
t = "Hello World".data
86
@test Mmap.mmap(file, Array{UInt8,3}, (11,1,1)) == reshape(t,(11,1,1))
97
gc(); gc()
@@ -94,9 +92,7 @@ m = Mmap.mmap(s)
9492
@test_throws ReadOnlyMemoryError m[5] = UInt8('x') # tries to setindex! on read-only array
9593
finalize(m); m=nothing; gc()
9694

97-
s = open(file, "w") do f
98-
write(f, "Hello World\n")
99-
end
95+
write(file, "Hello World\n")
10096

10197
s = open(file, "r")
10298
m = Mmap.mmap(s)
@@ -114,9 +110,7 @@ close(s)
114110
finalize(m); finalize(c); finalize(d)
115111
m=nothing; c=nothing; d=nothing; gc()
116112

117-
s = open(file, "w") do f
118-
write(f, "Hello World\n")
119-
end
113+
write(file, "Hello World\n")
120114

121115
s = open(file, "r")
122116
@test isreadonly(s) == true

test/parallel_exec.jl

+5-15
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,7 @@ d = SharedArray(Float64, (2,3))
248248

249249
# Mapping an existing file
250250
fn = tempname()
251-
open(fn, "w") do io
252-
write(io, 1:30)
253-
end
251+
write(fn, 1:30)
254252
sz = (6,5)
255253
Atrue = reshape(1:30, sz)
256254

@@ -267,9 +265,7 @@ end
267265
check_pids_all(S)
268266

269267
filedata = similar(Atrue)
270-
open(fn, "r") do io
271-
read!(io, filedata)
272-
end
268+
read!(fn, filedata)
273269
@test filedata == sdata(S)
274270

275271
# Error for write-only files
@@ -283,23 +279,17 @@ fn2 = tempname()
283279
S = SharedArray(fn2, Int, sz, init=D->D[localindexes(D)] = myid())
284280
@test S == filedata
285281
filedata2 = similar(Atrue)
286-
open(fn2, "r") do io
287-
read!(io, filedata2)
288-
end
282+
read!(fn2, filedata2)
289283
@test filedata == filedata2
290284

291285
# Appending to a file
292286
fn3 = tempname()
293-
open(fn3, "w") do io
294-
write(io, ones(UInt8, 4))
295-
end
287+
write(fn3, ones(UInt8, 4))
296288
S = SharedArray(fn3, UInt8, sz, 4, mode="a+", init=D->D[localindexes(D)]=0x02)
297289
len = prod(sz)+4
298290
@test filesize(fn3) == len
299291
filedata = Array(UInt8, len)
300-
open(fn3, "r") do io
301-
read!(io, filedata)
302-
end
292+
read!(fn3, filedata)
303293
@test all(filedata[1:4] .== 0x01)
304294
@test all(filedata[5:end] .== 0x02)
305295

test/perf/kernel/perf.jl

+1-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ d = randn(len)
102102

103103
@timeit (for n in 1:10; a = arith_vectorized(b,c,d); end) "vectorize" "Vectorized arithmetic"
104104

105-
open("random.csv","w") do io
106-
writecsv(io, rand(100000,4))
107-
end
105+
writecsv("random.csv", rand(100000,4))
108106

109107
function parsecsv()
110108
for line in EachLine(open("random.csv"))

0 commit comments

Comments
 (0)