fix error by which utility.open might not always close file handles

This commit is contained in:
Tangent / Rose / Nebula Rosa 2024-11-09 00:48:44 -07:00
parent 33f9a123e6
commit 942a47330e

View File

@ -133,9 +133,13 @@ utility.open = function(file_name, mode, custom_error_message)
local file, err = io.open(file_name, mode) local file, err = io.open(file_name, mode)
if not file then error(custom_error_message or err) end if not file then error(custom_error_message or err) end
return function(fn) return function(fn)
local result = fn(file) local success, result_or_error = pcall(function() return fn(file) end)
file:close() file:close()
return result if success then
return result_or_error
else
error(result_or_error)
end
end end
end end