Added a '__close' metamethod to file handles
This commit is contained in:
1
liolib.c
1
liolib.c
@@ -743,6 +743,7 @@ static const luaL_Reg flib[] = {
|
|||||||
{"setvbuf", f_setvbuf},
|
{"setvbuf", f_setvbuf},
|
||||||
{"write", f_write},
|
{"write", f_write},
|
||||||
{"__gc", f_gc},
|
{"__gc", f_gc},
|
||||||
|
{"__close", f_gc},
|
||||||
{"__tostring", f_tostring},
|
{"__tostring", f_tostring},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -120,31 +120,45 @@ io.output(io.open(otherfile, "ab"))
|
|||||||
assert(io.write("\n\n\t\t ", 3450, "\n"));
|
assert(io.write("\n\n\t\t ", 3450, "\n"));
|
||||||
io.close()
|
io.close()
|
||||||
|
|
||||||
-- test writing/reading numbers
|
|
||||||
f = assert(io.open(file, "w"))
|
do
|
||||||
f:write(maxint, '\n')
|
-- closing file by scope
|
||||||
f:write(string.format("0X%x\n", maxint))
|
local F = nil
|
||||||
f:write("0xABCp-3", '\n')
|
do
|
||||||
f:write(0, '\n')
|
local scoped f = assert(io.open(file, "w"))
|
||||||
f:write(-maxint, '\n')
|
F = f
|
||||||
f:write(string.format("0x%X\n", -maxint))
|
end
|
||||||
f:write("-0xABCp-3", '\n')
|
assert(tostring(F) == "file (closed)")
|
||||||
assert(f:close())
|
end
|
||||||
f = assert(io.open(file, "r"))
|
assert(os.remove(file))
|
||||||
assert(f:read("n") == maxint)
|
|
||||||
assert(f:read("n") == maxint)
|
|
||||||
assert(f:read("n") == 0xABCp-3)
|
do
|
||||||
assert(f:read("n") == 0)
|
-- test writing/reading numbers
|
||||||
assert(f:read("*n") == -maxint) -- test old format (with '*')
|
local scoped f = assert(io.open(file, "w"))
|
||||||
assert(f:read("n") == -maxint)
|
f:write(maxint, '\n')
|
||||||
assert(f:read("*n") == -0xABCp-3) -- test old format (with '*')
|
f:write(string.format("0X%x\n", maxint))
|
||||||
assert(f:close())
|
f:write("0xABCp-3", '\n')
|
||||||
|
f:write(0, '\n')
|
||||||
|
f:write(-maxint, '\n')
|
||||||
|
f:write(string.format("0x%X\n", -maxint))
|
||||||
|
f:write("-0xABCp-3", '\n')
|
||||||
|
assert(f:close())
|
||||||
|
f = assert(io.open(file, "r"))
|
||||||
|
assert(f:read("n") == maxint)
|
||||||
|
assert(f:read("n") == maxint)
|
||||||
|
assert(f:read("n") == 0xABCp-3)
|
||||||
|
assert(f:read("n") == 0)
|
||||||
|
assert(f:read("*n") == -maxint) -- test old format (with '*')
|
||||||
|
assert(f:read("n") == -maxint)
|
||||||
|
assert(f:read("*n") == -0xABCp-3) -- test old format (with '*')
|
||||||
|
end
|
||||||
assert(os.remove(file))
|
assert(os.remove(file))
|
||||||
|
|
||||||
|
|
||||||
-- testing multiple arguments to io.read
|
-- testing multiple arguments to io.read
|
||||||
do
|
do
|
||||||
local f = assert(io.open(file, "w"))
|
local scoped f = assert(io.open(file, "w"))
|
||||||
f:write[[
|
f:write[[
|
||||||
a line
|
a line
|
||||||
another line
|
another line
|
||||||
@@ -171,9 +185,8 @@ three
|
|||||||
-- second item failing
|
-- second item failing
|
||||||
l1, n1, n2, dummy = f:read("l", "n", "n", "l")
|
l1, n1, n2, dummy = f:read("l", "n", "n", "l")
|
||||||
assert(l1 == "a line" and n1 == nil)
|
assert(l1 == "a line" and n1 == nil)
|
||||||
assert(f:close())
|
|
||||||
assert(os.remove(file))
|
|
||||||
end
|
end
|
||||||
|
assert(os.remove(file))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user