Skip to content

Commit 1a1d5e5

Browse files
authored
Merge pull request #179 from davidanthoff/gen2-names
Add support for fileio_* names
2 parents b81b089 + adc8577 commit 1a1d5e5

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ Consequently, **packages should define "private" `load` and `save` methods (also
172172
`loadstreaming` and `savestreaming` if you implement them), and not extend
173173
(import) FileIO's**.
174174

175+
If you run into a naming conflict with the `load` and `save` functions
176+
(for example, you already have another function in your package that has
177+
one of these names), you can instead name your loaders `fileio_load`,
178+
`fileio_save` etc. Note that you cannot mix and match these styles: either
179+
all your loaders have to be named `load`, or all of them should be called
180+
`fileio_load`, but you cannot use both conventions in one module.
181+
175182
`load(::File)` and `save(::File)` should close any streams
176183
they open. (If you use the `do` syntax, this happens for you
177184
automatically even if the code inside the `do` scope throws an error.)

src/loadsave.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ for fn in (:load, :loadstreaming, :metadata)
173173
for library in libraries
174174
try
175175
Library = checked_import(library)
176+
gen2_func_name = Symbol("fileio_" * $(string(fn)))
177+
if isdefined(Library, gen2_func_name)
178+
return eval(Library, :($gen2_func_name($q, $args...; $options...)))
179+
end
176180
if !has_method_from(methods(Library.$fn), Library)
177181
throw(LoaderError(string(library), "$($fn) not defined"))
178182
end
@@ -193,6 +197,10 @@ for fn in (:save, :savestreaming)
193197
for library in libraries
194198
try
195199
Library = checked_import(library)
200+
gen2_func_name = Symbol("fileio_" * $(string(fn)))
201+
if isdefined(Library, gen2_func_name)
202+
return eval(Library, :($gen2_func_name($q, $data...; $options...)))
203+
end
196204
if !has_method_from(methods(Library.$fn), Library)
197205
throw(WriterError(string(library), "$($fn) not defined"))
198206
end

test/loadsave.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ module TestLoadSave
77
import FileIO: File, @format_str
88
load(file::File{format"PBMText"}) = "PBMText"
99
load(file::File{format"PBMBinary"}) = "PBMBinary"
10-
load(file::File{format"HDF5"}) = "HDF5"
1110
load(file::File{format"JLD"}) = "JLD"
1211
load(file::File{format"GZIP"}) = "GZIP"
1312
end
13+
module TestLoadSave2
14+
import FileIO: File, @format_str
15+
fileio_load(file::File{format"HDF5"}) = "HDF5"
16+
end
1417

1518
sym2loader = copy(FileIO.sym2loader)
1619
sym2saver = copy(FileIO.sym2saver)
@@ -23,7 +26,7 @@ try
2326

2427
add_loader(format"PBMText", :TestLoadSave)
2528
add_loader(format"PBMBinary", :TestLoadSave)
26-
add_loader(format"HDF5", :TestLoadSave)
29+
add_loader(format"HDF5", :TestLoadSave2)
2730
add_loader(format"JLD", :TestLoadSave)
2831
add_loader(format"GZIP", :TestLoadSave)
2932

@@ -285,12 +288,12 @@ load(f::File{format"AmbigExt2"}) = open(f) do io
285288
read(stream(io), String)
286289
end
287290

288-
save(f::File{format"AmbigExt1"}, testdata) = open(f, "w") do io
291+
fileio_save(f::File{format"AmbigExt1"}, testdata) = open(f, "w") do io
289292
s = stream(io)
290293
print(s, "ambigext1")
291294
print(s, testdata)
292295
end
293-
save(f::File{format"AmbigExt2"}, testdata) = open(f, "w") do io
296+
fileio_save(f::File{format"AmbigExt2"}, testdata) = open(f, "w") do io
294297
s = stream(io)
295298
print(s, "ambigext2")
296299
print(s, testdata)

0 commit comments

Comments
 (0)