diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-10-23 13:57:25 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-10-23 13:57:25 -0300 |
commit | 0a9aca56caa925c42aaa683b43560357ab736ea4 (patch) | |
tree | fccde4e8cf00c6b7e3563ef3f75c3a28a3f13c64 | |
parent | ea1322ef5438aa38f16fa00d3ab377811bba5a76 (diff) | |
download | lua-0a9aca56caa925c42aaa683b43560357ab736ea4.tar.gz lua-0a9aca56caa925c42aaa683b43560357ab736ea4.tar.bz2 lua-0a9aca56caa925c42aaa683b43560357ab736ea4.zip |
Added a '__close' metamethod to file handles
-rw-r--r-- | liolib.c | 1 | ||||
-rw-r--r-- | testes/files.lua | 57 |
2 files changed, 36 insertions, 22 deletions
@@ -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")) | |||
120 | assert(io.write("\n\n\t\t ", 3450, "\n")); | 120 | assert(io.write("\n\n\t\t ", 3450, "\n")); |
121 | io.close() | 121 | io.close() |
122 | 122 | ||
123 | -- test writing/reading numbers | 123 | |
124 | f = assert(io.open(file, "w")) | 124 | do |
125 | f:write(maxint, '\n') | 125 | -- closing file by scope |
126 | f:write(string.format("0X%x\n", maxint)) | 126 | local F = nil |
127 | f:write("0xABCp-3", '\n') | 127 | do |
128 | f:write(0, '\n') | 128 | local scoped f = assert(io.open(file, "w")) |
129 | f:write(-maxint, '\n') | 129 | F = f |
130 | f:write(string.format("0x%X\n", -maxint)) | 130 | end |
131 | f:write("-0xABCp-3", '\n') | 131 | assert(tostring(F) == "file (closed)") |
132 | assert(f:close()) | 132 | end |
133 | f = assert(io.open(file, "r")) | 133 | assert(os.remove(file)) |
134 | assert(f:read("n") == maxint) | 134 | |
135 | assert(f:read("n") == maxint) | 135 | |
136 | assert(f:read("n") == 0xABCp-3) | 136 | do |
137 | assert(f:read("n") == 0) | 137 | -- test writing/reading numbers |
138 | assert(f:read("*n") == -maxint) -- test old format (with '*') | 138 | local scoped f = assert(io.open(file, "w")) |
139 | assert(f:read("n") == -maxint) | 139 | f:write(maxint, '\n') |
140 | assert(f:read("*n") == -0xABCp-3) -- test old format (with '*') | 140 | f:write(string.format("0X%x\n", maxint)) |
141 | assert(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 '*') | ||
155 | end | ||
142 | assert(os.remove(file)) | 156 | assert(os.remove(file)) |
143 | 157 | ||
144 | 158 | ||
145 | -- testing multiple arguments to io.read | 159 | -- testing multiple arguments to io.read |
146 | do | 160 | do |
147 | local f = assert(io.open(file, "w")) | 161 | local scoped f = assert(io.open(file, "w")) |
148 | f:write[[ | 162 | f:write[[ |
149 | a line | 163 | a line |
150 | another line | 164 | another 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)) | ||
176 | end | 188 | end |
189 | assert(os.remove(file)) | ||
177 | 190 | ||
178 | 191 | ||
179 | 192 | ||