Skip to content

Commit 8053b6f

Browse files
committed
Change walkdir implementation to match Julia 0.6 and don't export it
1 parent 40bc120 commit 8053b6f

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Currently, the `@compat` macro supports the following syntaxes:
8181

8282
* `foreach`, similar to `map` but when the return value is not needed ([#13744])
8383

84-
* `walkdir`, returns an iterator that walks the directory tree of a directory ([#13707])
84+
* `walkdir`/`Compat.walkdir`, returns an iterator that walks the directory tree of a directory. For compatibility with Julia 0.6- `Compat.walkdir` should be used and `walkdir` should be used for compatibility with the Julia 0.5. ([#13707])
8585

8686
* `allunique`, checks whether all elements in an iterable appear only once ([#15914])
8787

src/Compat.jl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,21 @@ elseif VERSION < v"0.4.0-dev+6987"
152152
export pipeline
153153
end
154154

155-
if VERSION < v"0.5.0-dev+961"
156-
export walkdir
157-
155+
if VERSION < v"0.6.0-dev.2043"
158156
function walkdir(root; topdown=true, follow_symlinks=false, onerror=throw)
159157
content = nothing
160158
try
161159
content = readdir(root)
162160
catch err
163161
isa(err, SystemError) || throw(err)
164162
onerror(err)
165-
#Need to return an empty task to skip the current root folder
166-
return Task(()->())
163+
# Need to return an empty closed channel to skip the current root folder
164+
chnl = Channel(0)
165+
close(chnl)
166+
return chnl
167167
end
168-
dirs = Array(eltype(content), 0)
169-
files = Array(eltype(content), 0)
168+
dirs = Vector{eltype(content)}(0)
169+
files = Vector{eltype(content)}(0)
170170
for name in content
171171
if isdir(joinpath(root, name))
172172
push!(dirs, name)
@@ -175,24 +175,27 @@ if VERSION < v"0.5.0-dev+961"
175175
end
176176
end
177177

178-
function _it()
178+
function _it(chnl)
179179
if topdown
180-
produce(root, dirs, files)
180+
put!(chnl, (root, dirs, files))
181181
end
182182
for dir in dirs
183183
path = joinpath(root,dir)
184184
if follow_symlinks || !islink(path)
185185
for (root_l, dirs_l, files_l) in walkdir(path, topdown=topdown, follow_symlinks=follow_symlinks, onerror=onerror)
186-
produce(root_l, dirs_l, files_l)
186+
put!(chnl, (root_l, dirs_l, files_l))
187187
end
188188
end
189189
end
190190
if !topdown
191-
produce(root, dirs, files)
191+
put!(chnl, (root, dirs, files))
192192
end
193193
end
194-
Task(_it)
194+
195+
return Channel(_it)
195196
end
197+
else
198+
import Base.walkdir
196199
end
197200

198201
function rewrite_show(ex)

test/runtests.jl

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -983,29 +983,29 @@ cd(dirwalk) do
983983
follow_symlink_vec = has_symlinks ? [true, false] : [false]
984984
has_symlinks && symlink(abspath("sub_dir2"), joinpath("sub_dir1", "link"))
985985
for follow_symlinks in follow_symlink_vec
986-
task = walkdir(".", follow_symlinks=follow_symlinks)
987-
root, dirs, files = consume(task)
986+
chnl = Compat.walkdir(".", follow_symlinks=follow_symlinks)
987+
root, dirs, files = take!(chnl)
988988
@test root == "."
989989
@test dirs == ["sub_dir1", "sub_dir2"]
990990
@test files == ["file1", "file2"]
991991

992-
root, dirs, files = consume(task)
992+
root, dirs, files = take!(chnl)
993993
@test root == joinpath(".", "sub_dir1")
994994
@test dirs == (has_symlinks ? ["link", "subsub_dir1", "subsub_dir2"] : ["subsub_dir1", "subsub_dir2"])
995995
@test files == ["file1", "file2"]
996996

997-
root, dirs, files = consume(task)
997+
root, dirs, files = take!(chnl)
998998
if follow_symlinks
999999
@test root == joinpath(".", "sub_dir1", "link")
10001000
@test dirs == []
10011001
@test files == ["file_dir2"]
1002-
root, dirs, files = consume(task)
1002+
root, dirs, files = take!(chnl)
10031003
end
10041004
for i=1:2
10051005
@test root == joinpath(".", "sub_dir1", "subsub_dir$i")
10061006
@test dirs == []
10071007
@test files == []
1008-
root, dirs, files = consume(task)
1008+
root, dirs, files = take!(chnl)
10091009
end
10101010

10111011
@test root == joinpath(".", "sub_dir2")
@@ -1014,51 +1014,51 @@ cd(dirwalk) do
10141014
end
10151015

10161016
for follow_symlinks in follow_symlink_vec
1017-
task = walkdir(".", follow_symlinks=follow_symlinks, topdown=false)
1018-
root, dirs, files = consume(task)
1017+
chnl = Compat.walkdir(".", follow_symlinks=follow_symlinks, topdown=false)
1018+
root, dirs, files = take!(chnl)
10191019
if follow_symlinks
10201020
@test root == joinpath(".", "sub_dir1", "link")
10211021
@test dirs == []
10221022
@test files == ["file_dir2"]
1023-
root, dirs, files = consume(task)
1023+
root, dirs, files = take!(chnl)
10241024
end
10251025
for i=1:2
10261026
@test root == joinpath(".", "sub_dir1", "subsub_dir$i")
10271027
@test dirs == []
10281028
@test files == []
1029-
root, dirs, files = consume(task)
1029+
root, dirs, files = take!(chnl)
10301030
end
10311031
@test root == joinpath(".", "sub_dir1")
10321032
@test dirs == (has_symlinks ? ["link", "subsub_dir1", "subsub_dir2"] : ["subsub_dir1", "subsub_dir2"])
10331033
@test files == ["file1", "file2"]
10341034

1035-
root, dirs, files = consume(task)
1035+
root, dirs, files = take!(chnl)
10361036
@test root == joinpath(".", "sub_dir2")
10371037
@test dirs == []
10381038
@test files == ["file_dir2"]
10391039

1040-
root, dirs, files = consume(task)
1040+
root, dirs, files = take!(chnl)
10411041
@test root == "."
10421042
@test dirs == ["sub_dir1", "sub_dir2"]
10431043
@test files == ["file1", "file2"]
10441044
end
10451045
#test of error handling
1046-
task_error = walkdir(".")
1047-
task_noerror = walkdir(".", onerror=x->x)
1048-
root, dirs, files = consume(task_error)
1046+
chnl_error = Compat.walkdir(".")
1047+
chnl_noerror = Compat.walkdir(".", onerror=x->x)
1048+
root, dirs, files = take!(chnl_error)
10491049
@test root == "."
10501050
@test dirs == ["sub_dir1", "sub_dir2"]
10511051
@test files == ["file1", "file2"]
10521052

10531053
rm(joinpath("sub_dir1"), recursive=true)
1054-
@test_throws SystemError consume(task_error) # throws an error because sub_dir1 do not exist
1054+
@test_throws SystemError take!(chnl_error) # throws an error because sub_dir1 do not exist
10551055

1056-
root, dirs, files = consume(task_noerror)
1056+
root, dirs, files = take!(chnl_noerror)
10571057
@test root == "."
10581058
@test dirs == ["sub_dir1", "sub_dir2"]
10591059
@test files == ["file1", "file2"]
10601060

1061-
root, dirs, files = consume(task_noerror) # skips sub_dir1 as it no longer exist
1061+
root, dirs, files = take!(chnl_noerror) # skips sub_dir1 as it no longer exist
10621062
@test root == joinpath(".", "sub_dir2")
10631063
@test dirs == []
10641064
@test files == ["file_dir2"]
@@ -1192,7 +1192,7 @@ let x = rand(3), y = rand(3)
11921192
@test @compat(sin.(cos.(x))) == map(x -> sin(cos(x)), x)
11931193
@test @compat(atan2.(sin.(y),x)) == broadcast(atan2,map(sin,y),x)
11941194
end
1195-
let x0 = Array(Float64), v, v0
1195+
let x0 = Array{Float64}(), v, v0
11961196
x0[1] = rand()
11971197
v0 = @compat sin.(x0)
11981198
@test isa(v0, Array{Float64,0})

0 commit comments

Comments
 (0)