Comparison

util/envload.lua @ 7930:5dec27760ecd

util.envload: Close file handle after reading data
author Kim Alvefur <zash@zash.se>
date Wed, 01 Mar 2017 17:48:15 +0100
parent 7924:8487fe9fc335
child 8416:bc9cb23b604a
comparison
equal deleted inserted replaced
7929:4017ea6d2599 7930:5dec27760ecd
20 20
21 function envloadfile(file, env) 21 function envloadfile(file, env)
22 local fh, err, errno = io_open(file); 22 local fh, err, errno = io_open(file);
23 if not fh then return fh, err, errno; end 23 if not fh then return fh, err, errno; end
24 local f, err = load(function () return fh:read(2048); end, "@"..file); 24 local f, err = load(function () return fh:read(2048); end, "@"..file);
25 fh:close();
25 if f and env then setfenv(f, env); end 26 if f and env then setfenv(f, env); end
26 return f, err; 27 return f, err;
27 end 28 end
28 else 29 else
29 function envload(code, source, env) 30 function envload(code, source, env)
31 end 32 end
32 33
33 function envloadfile(file, env) 34 function envloadfile(file, env)
34 local fh, err, errno = io_open(file); 35 local fh, err, errno = io_open(file);
35 if not fh then return fh, err, errno; end 36 if not fh then return fh, err, errno; end
36 return load(fh:lines(2048), "@"..file, nil, env); 37 local f, err = load(fh:lines(2048), "@"..file, nil, env);
38 fh:close();
39 return f, err;
37 end 40 end
38 end 41 end
39 42
40 return { envload = envload, envloadfile = envloadfile }; 43 return { envload = envload, envloadfile = envloadfile };