aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-10-23 13:57:25 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-10-23 13:57:25 -0300
commit0a9aca56caa925c42aaa683b43560357ab736ea4 (patch)
treefccde4e8cf00c6b7e3563ef3f75c3a28a3f13c64
parentea1322ef5438aa38f16fa00d3ab377811bba5a76 (diff)
downloadlua-0a9aca56caa925c42aaa683b43560357ab736ea4.tar.gz
lua-0a9aca56caa925c42aaa683b43560357ab736ea4.tar.bz2
lua-0a9aca56caa925c42aaa683b43560357ab736ea4.zip
Added a '__close' metamethod to file handles
-rw-r--r--liolib.c1
-rw-r--r--testes/files.lua57
2 files changed, 36 insertions, 22 deletions
diff --git a/liolib.c b/liolib.c
index 21305b8c..3dc509bd 100644
--- a/liolib.c
+++ b/liolib.c
@@ -743,6 +743,7 @@ static const luaL_Reg flib[] = {
743 {"setvbuf", f_setvbuf}, 743 {"setvbuf", f_setvbuf},
744 {"write", f_write}, 744 {"write", f_write},
745 {"__gc", f_gc}, 745 {"__gc", f_gc},
746 {"__close", f_gc},
746 {"__tostring", f_tostring}, 747 {"__tostring", f_tostring},
747 {NULL, NULL} 748 {NULL, NULL}
748}; 749};
diff --git a/testes/files.lua b/testes/files.lua
index c3e42235..9aae5913 100644
--- a/testes/files.lua
+++ b/testes/files.lua
@@ -120,31 +120,45 @@ io.output(io.open(otherfile, "ab"))
120assert(io.write("\n\n\t\t ", 3450, "\n")); 120assert(io.write("\n\n\t\t ", 3450, "\n"));
121io.close() 121io.close()
122 122
123-- test writing/reading numbers 123
124f = assert(io.open(file, "w")) 124do
125f:write(maxint, '\n') 125 -- closing file by scope
126f:write(string.format("0X%x\n", maxint)) 126 local F = nil
127f:write("0xABCp-3", '\n') 127 do
128f:write(0, '\n') 128 local scoped f = assert(io.open(file, "w"))
129f:write(-maxint, '\n') 129 F = f
130f:write(string.format("0x%X\n", -maxint)) 130 end
131f:write("-0xABCp-3", '\n') 131 assert(tostring(F) == "file (closed)")
132assert(f:close()) 132end
133f = assert(io.open(file, "r")) 133assert(os.remove(file))
134assert(f:read("n") == maxint) 134
135assert(f:read("n") == maxint) 135
136assert(f:read("n") == 0xABCp-3) 136do
137assert(f:read("n") == 0) 137 -- test writing/reading numbers
138assert(f:read("*n") == -maxint) -- test old format (with '*') 138 local scoped f = assert(io.open(file, "w"))
139assert(f:read("n") == -maxint) 139 f:write(maxint, '\n')
140assert(f:read("*n") == -0xABCp-3) -- test old format (with '*') 140 f:write(string.format("0X%x\n", maxint))
141assert(f:close()) 141 f:write("0xABCp-3", '\n')
142 f:write(0, '\n')
143 f:write(-maxint, '\n')
144 f:write(string.format("0x%X\n", -maxint))
145 f:write("-0xABCp-3", '\n')
146 assert(f:close())
147 f = assert(io.open(file, "r"))
148 assert(f:read("n") == maxint)
149 assert(f:read("n") == maxint)
150 assert(f:read("n") == 0xABCp-3)
151 assert(f:read("n") == 0)
152 assert(f:read("*n") == -maxint) -- test old format (with '*')
153 assert(f:read("n") == -maxint)
154 assert(f:read("*n") == -0xABCp-3) -- test old format (with '*')
155end
142assert(os.remove(file)) 156assert(os.remove(file))
143 157
144 158
145-- testing multiple arguments to io.read 159-- testing multiple arguments to io.read
146do 160do
147 local f = assert(io.open(file, "w")) 161 local scoped f = assert(io.open(file, "w"))
148 f:write[[ 162 f:write[[
149a line 163a line
150another line 164another line
@@ -171,9 +185,8 @@ three
171 -- second item failing 185 -- second item failing
172 l1, n1, n2, dummy = f:read("l", "n", "n", "l") 186 l1, n1, n2, dummy = f:read("l", "n", "n", "l")
173 assert(l1 == "a line" and n1 == nil) 187 assert(l1 == "a line" and n1 == nil)
174 assert(f:close())
175 assert(os.remove(file))
176end 188end
189assert(os.remove(file))
177 190
178 191
179 192