Skip to content

Commit

Permalink
update tests, examples, dir.jl, and random.jl to use write(filename,...)
Browse files Browse the repository at this point in the history
in place of one-line open(filname) do io ...
  • Loading branch information
samoconnor committed Jan 13, 2016
1 parent f1e841e commit c758820
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 147 deletions.
9 changes: 2 additions & 7 deletions base/pkg/dir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ function init(meta::AbstractString=DEFAULT_META, branch::AbstractString=META_BRA
end
touch("REQUIRE")
touch("META_BRANCH")
open("META_BRANCH", "w") do io
write(io, branch)
close(io)
end
write("META_BRANCH", branch)
end
#Move TEMP to METADATA
Base.mv(joinpath(temp_dir,"METADATA"), metadata_dir)
Expand All @@ -70,9 +67,7 @@ end

function getmetabranch()
try
open(joinpath(path(),"META_BRANCH")) do io
chomp(readuntil(io, "/n"))
end
readline(joinpath(path(),"META_BRANCH"))
catch err
META_BRANCH
end
Expand Down
6 changes: 1 addition & 5 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,7 @@ function make_seed(n::Integer)
end

function make_seed(filename::AbstractString, n::Integer)
open(filename) do io
a = Array(UInt32, Int(n))
read!(io, a)
a
end
read!(filename, Array(UInt32, Int(n)))
end

## srand()
Expand Down
4 changes: 1 addition & 3 deletions examples/wordcount.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ end
function wordcount_files(result_file,inputs...)
text = ""
for file in inputs
open(file) do f
text *= readstring(f)
end
text *= readstring(file)
end
wc = parallel_wordcount(text)
open(result_file,"w") do f
Expand Down
12 changes: 5 additions & 7 deletions test/cmdlineargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ let exename = `$(joinpath(JULIA_HOME, Base.julia_exename())) --precompiled=yes`
# --load
let testfile = tempname()
try
open(testfile, "w") do io
println(io, "testvar = :test")
end
write(testfile, "testvar = :test\n")
@test split(readchomp(`$exename --load=$testfile -P "println(testvar)"`), '\n')[end] == "test"
@test split(readchomp(`$exename -P "println(testvar)" -L $testfile`), '\n')[end] == "test"
finally
Expand Down Expand Up @@ -210,10 +208,10 @@ let exename = `$(joinpath(JULIA_HOME, Base.julia_exename())) --precompiled=yes`
let testfile = tempname()
try
# write a julia source file that just prints ARGS to STDOUT and exits
open(testfile, "w") do io
println(io, "println(ARGS)")
println(io, "exit(0)")
end
write(testfile, """
println(ARGS)
exit(0)
""")
@test readchomp(`$exename $testfile foo -bar --baz`) == "UTF8String[\"foo\",\"-bar\",\"--baz\"]"
@test readchomp(`$exename $testfile -- foo -bar --baz`) == "UTF8String[\"foo\",\"-bar\",\"--baz\"]"
@test readchomp(`$exename -L $testfile -- foo -bar --baz`) == "UTF8String[\"foo\",\"-bar\",\"--baz\"]"
Expand Down
90 changes: 43 additions & 47 deletions test/compile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ Foo_module = :Foo4b3a94a1a081a8cb
try
Foo_file = joinpath(dir, "$Foo_module.jl")

open(Foo_file, "w") do f
print(f, """
__precompile__(true)
module $Foo_module
@doc "foo function" foo(x) = x + 1
include_dependency("foo.jl")
include_dependency("foo.jl")
module Bar
@doc "bar function" bar(x) = x + 2
include_dependency("bar.jl")
end
end
""")
end
write(Foo_file,
"""
__precompile__(true)
module $Foo_module
@doc "foo function" foo(x) = x + 1
include_dependency("foo.jl")
include_dependency("foo.jl")
module Bar
@doc "bar function" bar(x) = x + 2
include_dependency("bar.jl")
end
end
""")

# Issue #12623
@test __precompile__(true) === nothing
Expand Down Expand Up @@ -51,25 +50,25 @@ try
end

Baz_file = joinpath(dir, "Baz.jl")
open(Baz_file, "w") do f
print(f, """
__precompile__(false)
module Baz
end
""")
end
write(Baz_file,
"""
__precompile__(false)
module Baz
end
""")

println(STDERR, "\nNOTE: The following 'LoadError: __precompile__(false)' indicates normal operation")
@test_throws ErrorException Base.compilecache("Baz") # from __precompile__(false)

# Issue #12720
FooBar_file = joinpath(dir, "FooBar.jl")
open(FooBar_file, "w") do f
print(f, """
__precompile__(true)
module FooBar
end
""")
end
write(FooBar_file,
"""
__precompile__(true)
module FooBar
end
""")

Base.compilecache("FooBar")
sleep(2)
@test isfile(joinpath(dir, "FooBar.ji"))
Expand All @@ -82,14 +81,14 @@ try
@test Base.stale_cachefile(FooBar_file, joinpath(dir, "FooBar.ji"))
@test !Base.stale_cachefile(FooBar_file, joinpath(dir2, "FooBar.ji"))

open(FooBar_file, "w") do f
print(f, """
__precompile__(true)
module FooBar
error("break me")
end
""")
end
write(FooBar_file,
"""
__precompile__(true)
module FooBar
error("break me")
end
""")

println(STDERR, "\nNOTE: The following 'LoadError: break me' indicates normal operation")
@test_throws ErrorException Base.require(:FooBar)

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

try
open(joinpath(dir, "$Time_module.jl"), "w") do io
write(io, """
module $Time_module
__precompile__(true)
time = Base.time()
end
""")
end
write(joinpath(dir, "$Time_module.jl"),
"""
module $Time_module
__precompile__(true)
time = Base.time()
end
""")

eval(quote
insert!(LOAD_PATH, 1, $(dir))
Expand Down Expand Up @@ -151,9 +149,7 @@ let module_name = string("a",randstring())
file_name = string(module_name, ".jl")
touch(file_name)
code = """module $(module_name)\nend\n"""
open(file_name, "w") do file
write(file, code)
end
write(file_name, code)
reload(module_name)
@test typeof(eval(symbol(module_name))) == Module
deleteat!(LOAD_PATH,1)
Expand Down
4 changes: 1 addition & 3 deletions test/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
@test countlines(IOBuffer("\n \n \n \n \n \n \n \n \n \n")) == 10
@test countlines(IOBuffer("\r\n \r\n \r\n \r\n \r\n")) == 5
file = tempname()
open(file,"w") do f
write(f,"Spiffy header\nspectacular first row\neven better 2nd row\nalmost done\n")
end
write(file,"Spiffy header\nspectacular first row\neven better 2nd row\nalmost done\n")
@test countlines(file) == 4
@test countlines(file,'\r') == 0
@test countlines(file,'\n') == 4
Expand Down
42 changes: 12 additions & 30 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,7 @@ bfile = joinpath(dir, "b.txt")
cp(afile, bfile)

cfile = joinpath(dir, "c.txt")
open(cfile, "w") do cf
write(cf, "This is longer than the contents of afile")
end
write(cfile, "This is longer than the contents of afile")
cp(afile, cfile; remove_destination=true)

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

cfile = joinpath(srcdir, "c.txt")
file_txt = "This is some text with unicode - 这是一个文件"
open(cfile, "w") do cf
write(cf, file_txt)
end
write(cfile, file_txt)
hidden_cfile = joinpath(hidden_srcsubdir, "c.txt")
open(hidden_cfile, "w") do cf
write(cf, file_txt)
end
write(hidden_cfile, file_txt)

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

cfile = abspath(joinpath(maindir, "c.txt"))
open(cfile, "w") do cf
write(cf, "This is c.txt - 这是一个文件")
end
open(abspath(joinpath(targetdir, "file1.txt")), "w") do cf
write(cf, "This is file1.txt - 这是一个文件")
end
write(cfile, "This is c.txt - 这是一个文件")
write(abspath(joinpath(targetdir, "file1.txt")),
"This is file1.txt - 这是一个文件")

abs_dl = joinpath(maindir, "abs_linkto_targetdir")
symlink(targetdir, abs_dl)
Expand Down Expand Up @@ -616,12 +607,8 @@ end
srcfile_new = joinpath(tmpdir, "srcfile_new.txt")
hidden_srcfile_new = joinpath(tmpdir, ".hidden_srcfile_new.txt")
file_txt = "This is some text with unicode - 这是一个文件"
open(srcfile, "w") do f
write(f, file_txt)
end
open(hidden_srcfile, "w") do f
write(f, file_txt)
end
write(srcfile, file_txt)
write(hidden_srcfile, file_txt)
abs_filelink = joinpath(tmpdir, "abs_filelink")
symlink(abspath(srcfile), abs_filelink)
cd(tmpdir)
Expand Down Expand Up @@ -701,9 +688,7 @@ end
# Test remove the existing path first and copy an other file
otherfile = joinpath(tmpdir, "otherfile.txt")
otherfile_content = "This is otherfile.txt with unicode - 这是一个文件"
open(otherfile, "w") do f
write(f, otherfile_content)
end
write(otherfile, otherfile_content)
for d in test_new_paths1
cp(otherfile, d; remove_destination=true, follow_symlinks=false)
# Expect no link because a file is copied (follow_symlinks=false does not effect this)
Expand Down Expand Up @@ -753,12 +738,9 @@ end
mkdir(subdir1)

cfile = abspath(joinpath(maindir, "c.txt"))
open(cfile, "w") do cf
write(cf, "This is c.txt - 这是一个文件")
end
open(abspath(joinpath(targetdir, "file1.txt")), "w") do cf
write(cf, "This is file1.txt - 这是一个文件")
end
write(cfile, "This is c.txt - 这是一个文件")
write(abspath(joinpath(targetdir, "file1.txt")),
"This is file1.txt - 这是一个文件")

abs_fl = joinpath(maindir, "abs_linkto_c.txt")
symlink(cfile, abs_fl)
Expand Down
12 changes: 3 additions & 9 deletions test/mmap.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

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

s = open(file, "w") do f
write(f, "Hello World\n")
end
write(file, "Hello World\n")

s = open(file, "r")
m = Mmap.mmap(s)
Expand All @@ -114,9 +110,7 @@ close(s)
finalize(m); finalize(c); finalize(d)
m=nothing; c=nothing; d=nothing; gc()

s = open(file, "w") do f
write(f, "Hello World\n")
end
write(file, "Hello World\n")

s = open(file, "r")
@test isreadonly(s) == true
Expand Down
20 changes: 5 additions & 15 deletions test/parallel_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ d = SharedArray(Float64, (2,3))

# Mapping an existing file
fn = tempname()
open(fn, "w") do io
write(io, 1:30)
end
write(fn, 1:30)
sz = (6,5)
Atrue = reshape(1:30, sz)

Expand All @@ -267,9 +265,7 @@ end
check_pids_all(S)

filedata = similar(Atrue)
open(fn, "r") do io
read!(io, filedata)
end
read!(fn, filedata)
@test filedata == sdata(S)

# Error for write-only files
Expand All @@ -283,23 +279,17 @@ fn2 = tempname()
S = SharedArray(fn2, Int, sz, init=D->D[localindexes(D)] = myid())
@test S == filedata
filedata2 = similar(Atrue)
open(fn2, "r") do io
read!(io, filedata2)
end
read!(fn2, filedata2)
@test filedata == filedata2

# Appending to a file
fn3 = tempname()
open(fn3, "w") do io
write(io, ones(UInt8, 4))
end
write(fn3, ones(UInt8, 4))
S = SharedArray(fn3, UInt8, sz, 4, mode="a+", init=D->D[localindexes(D)]=0x02)
len = prod(sz)+4
@test filesize(fn3) == len
filedata = Array(UInt8, len)
open(fn3, "r") do io
read!(io, filedata)
end
read!(fn3, filedata)
@test all(filedata[1:4] .== 0x01)
@test all(filedata[5:end] .== 0x02)

Expand Down
4 changes: 1 addition & 3 deletions test/perf/kernel/perf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ d = randn(len)

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

open("random.csv","w") do io
writecsv(io, rand(100000,4))
end
writecsv("random.csv", rand(100000,4))

function parsecsv()
for line in EachLine(open("random.csv"))
Expand Down
Loading

0 comments on commit c758820

Please sign in to comment.