silence mass dump of BareExcept when using unittest (#25260)

Seems better to change it to `CatchableError` instead?

(cherry picked from commit cc4c7377b2)
This commit is contained in:
Ryan McConnell
2025-11-10 01:27:50 -05:00
committed by narimiran
parent 38384d040a
commit c75c85cbf8
2 changed files with 3 additions and 11 deletions

View File

@@ -394,8 +394,9 @@ proc semTry(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil)
elif a.len == 1:
# count number of ``except: body`` blocks
inc catchAllExcepts
message(c.config, a.info, warnBareExcept,
"The bare except clause is deprecated; use `except CatchableError:` instead")
if noPanicOnExcept in c.graph.config.legacyFeatures:
message(c.config, a.info, warnBareExcept,
"The bare except clause is deprecated; use `except CatchableError:` instead")
else:
# support ``except KeyError, ValueError, ... : body``
if catchAllExcepts > 0:

View File

@@ -547,14 +547,11 @@ template test*(name, body) {.dirty.} =
for formatter in formatters:
formatter.testStarted(name)
{.push warning[BareExcept]:off.}
try:
when declared(testSetupIMPLFlag): testSetupIMPL()
when declared(testTeardownIMPLFlag):
defer: testTeardownIMPL()
{.push warning[BareExcept]:on.}
body
{.pop.}
except Exception:
let e = getCurrentException()
@@ -577,7 +574,6 @@ template test*(name, body) {.dirty.} =
)
testEnded(testResult)
checkpoints = @[]
{.pop.}
proc checkpoint*(msg: string) =
## Set a checkpoint identified by `msg`. Upon test failure all
@@ -803,11 +799,8 @@ macro expect*(exceptions: varargs[typed], body: untyped): untyped =
discard
template expectBody(errorTypes, lineInfoLit, body): NimNode {.dirty.} =
{.push warning[BareExcept]:off.}
try:
{.push warning[BareExcept]:on.}
body
{.pop.}
checkpoint(lineInfoLit & ": Expect Failed, no exception was thrown.")
fail()
except errorTypes:
@@ -816,8 +809,6 @@ macro expect*(exceptions: varargs[typed], body: untyped): untyped =
let err = getCurrentException()
checkpoint(lineInfoLit & ": Expect Failed, " & $err.name & " was thrown.")
fail()
{.pop.}
var errorTypes = newNimNode(nnkBracket)
var hasException = false
for exp in exceptions: