From ca13be9af784b7288d3a07d9b5bccb329086e885 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Aug 2019 09:51:54 -0300 Subject: Supressed errors in '__close' generate warnings --- testes/all.lua | 4 +- testes/coroutine.lua | 5 +- testes/locals.lua | 152 ++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 133 insertions(+), 28 deletions(-) (limited to 'testes') diff --git a/testes/all.lua b/testes/all.lua index 5d698d4b..42809b9a 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -209,12 +209,12 @@ if #msgs > 0 then warn("#tests not performed:\n ", m, "\n") end +print("(there should be two warnings now)") +warn("#This is ", "an expected", " warning") warn("@off") warn("******** THIS WARNING SHOULD NOT APPEAR **********") warn("******** THIS WARNING ALSO SHOULD NOT APPEAR **********") warn("@on") -print("(there should be two warnings now)") -warn("#This is ", "an expected", " warning") warn("#This is", " another one") -- no test module should define 'debug' diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 457374ca..79c72a9d 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -168,7 +168,7 @@ do local y = func2close(function (self,err) if (err ~= 111) then os.exit(false) end -- should not happen x = 200 - error(200) + error("200") end) local x = func2close(function (self, err) assert(err == nil); error(111) @@ -177,7 +177,10 @@ do end) coroutine.resume(co) assert(x == 0) + _WARN = nil; warn("@off"); warn("@store") local st, msg = coroutine.close(co) + warn("@on"); warn("@normal") + assert(_WARN == nil or string.find(_WARN, "200")) assert(st == false and coroutine.status(co) == "dead" and msg == 111) assert(x == 200) diff --git a/testes/locals.lua b/testes/locals.lua index 99fa79cd..595e107a 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -286,57 +286,149 @@ do end -do -- errors in __close - local log = {} - local function foo (err) +-- auxiliary functions for testing warnings in '__close' +local function prepwarn () + warn("@off") -- do not show (lots of) warnings + if not T then + _WARN = "OFF" -- signal that warnings are not being captured + else + warn("@store") -- to test the warnings + end +end + + +local function endwarn () + assert(T or _WARN == "OFF") + warn("@on") -- back to normal + warn("@normal") + _WARN = nil +end + + +local function checkwarn (msg) + assert(_WARN == "OFF" or string.find(_WARN, msg)) +end + + +do print("testing errors in __close") + + prepwarn() + + -- original error is in __close + local function foo () + local x = - func2close(function (self, msg) log[#log + 1] = msg; error(1) end) + func2close(function (self, msg) + assert(string.find(msg, "@z")) + error("@x") + end) + local x1 = - func2close(function (self, msg) log[#log + 1] = msg; end) + func2close(function (self, msg) + checkwarn("@y") + assert(string.find(msg, "@z")) + end) + local gc = func2close(function () collectgarbage() end) + local y = - func2close(function (self, msg) log[#log + 1] = msg; error(2) end) + func2close(function (self, msg) + assert(string.find(msg, "@z")) -- error in 'z' + error("@y") + end) + + local first = true local z = + -- 'z' close is called twice func2close(function (self, msg) - log[#log + 1] = (msg or 10) + 1; - error(3) + if first then + assert(msg == nil) + first = false + else + assert(string.find(msg, "@z")) -- own error + end + error("@z") end) - if err then error(4) end + + return 200 end + local stat, msg = pcall(foo, false) - assert(msg == 3) - -- 'z' close is called twice - assert(log[1] == 11 and log[2] == 4 and log[3] == 3 and log[4] == 3 - and log[5] == 3 and #log == 5) + assert(string.find(msg, "@z")) + checkwarn("@x") + + + -- original error not in __close + local function foo () + + local x = + func2close(function (self, msg) + assert(msg == 4) + end) + + local x1 = + func2close(function (self, msg) + checkwarn("@y") + assert(msg == 4) + error("@x1") + end) + + local gc = func2close(function () collectgarbage() end) + + local y = + func2close(function (self, msg) + assert(msg == 4) -- error in body + error("@y") + end) + + local first = true + local z = + func2close(function (self, msg) + checkwarn("@z") + -- 'z' close is called once + assert(first and msg == 4) + first = false + error("@z") + end) + + error(4) -- original error + end - log = {} local stat, msg = pcall(foo, true) assert(msg == 4) - -- 'z' close is called once - assert(log[1] == 5 and log[2] == 4 and log[3] == 4 and log[4] == 4 - and #log == 4) + checkwarn("@x1") -- last error -- error leaving a block local function foo (...) do - local x1 = func2close(function () error("Y") end) - local x123 = func2close(function () error("X") end) + local x1 = + func2close(function () + checkwarn("@X") + error("@Y") + end) + + local x123 = + func2close(function () + error("@X") + end) end + os.exit(false) -- should not run end local st, msg = xpcall(foo, debug.traceback) - assert(string.match(msg, "^[^ ]* X")) + assert(string.match(msg, "^[^ ]* @X")) assert(string.find(msg, "in metamethod 'close'")) -- error in toclose in vararg function local function foo (...) - local x123 = func2close(function () error("X") end) + local x123 = func2close(function () error("@X") end) end local st, msg = xpcall(foo, debug.traceback) - assert(string.match(msg, "^[^ ]* X")) + assert(string.match(msg, "^[^ ]* @X")) assert(string.find(msg, "in metamethod 'close'")) + endwarn() end @@ -361,6 +453,8 @@ end if rawget(_G, "T") then + warn("@off") + -- memory error inside closing function local function foo () local y = func2close(function () T.alloccount() end) @@ -437,7 +531,7 @@ if rawget(_G, "T") then local s = string.rep("a", lim) - -- concat this table needs two buffer resizes (one for each 's') + -- concat this table needs two buffer resizes (one for each 's') local a = {s, s} collectgarbage() @@ -472,6 +566,8 @@ if rawget(_G, "T") then print'+' end + + warn("@on") end @@ -501,17 +597,20 @@ end do + prepwarn() + -- error in a wrapped coroutine raising errors when closing a variable local x = 0 local co = coroutine.wrap(function () - local xx = func2close(function () x = x + 1; error("YYY") end) - local xv = func2close(function () x = x + 1; error("XXX") end) + local xx = func2close(function () x = x + 1; error("@YYY") end) + local xv = func2close(function () x = x + 1; error("@XXX") end) coroutine.yield(100) error(200) end) assert(co() == 100); assert(x == 0) local st, msg = pcall(co); assert(x == 2) assert(not st and msg == 200) -- should get first error raised + checkwarn("@YYY") local x = 0 local y = 0 @@ -526,6 +625,9 @@ do assert(x == 2 and y == 1) -- first close is called twice -- should get first error raised assert(not st and string.find(msg, "%w+%.%w+:%d+: XXX")) + checkwarn("YYY") + + endwarn() end -- cgit v1.2.3-55-g6feb