Skip to content

Commit f9e2d99

Browse files
Merge pull request JuliaLang#25158 from c42f/depwarn-caller-fix
Depwarn caller fix
2 parents b57a592 + 16bdf88 commit f9e2d99

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

base/deprecated.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function firstcaller(bt::Vector, funcsyms)
9696
for frame in bt
9797
lkups = StackTraces.lookup(frame)
9898
for outer lkup in lkups
99-
if lkup == StackTraces.UNKNOWN
99+
if lkup == StackTraces.UNKNOWN || lkup.from_c
100100
continue
101101
end
102102
if found

base/logging.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,14 @@ exists for the current task.
413413
414414
global_logger(logger)
415415
416-
Set the global logger to `logger`.
416+
Set the global logger to `logger`, and return the previous global logger.
417417
"""
418418
global_logger() = _global_logstate.logger
419419

420420
function global_logger(logger::AbstractLogger)
421+
prev = _global_logstate.logger
421422
global _global_logstate = LogState(logger)
422-
logger
423+
prev
423424
end
424425

425426
"""

test/deprecation_exec.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Test
2+
using Logging
23

34
module DeprecationTests # to test @deprecate
45
f() = true
@@ -81,3 +82,20 @@ depwarn24658() = Base.firstcaller(backtrace(), :_func_not_found_)
8182
# issue #24658
8283
@test eval(:(if true; f24658(); end)) == (Ptr{Cvoid}(0),StackTraces.UNKNOWN)
8384
end
85+
86+
# issue #25130
87+
f25130() = Base.depwarn("f25130 message", :f25130)
88+
# The following test is for the depwarn behavior of expressions evaluated at
89+
# top-level, so we can't use the usual `collect_test_logs()` / `with_logger()`
90+
testlogger = Test.TestLogger()
91+
prev_logger = global_logger(testlogger)
92+
# Each call at top level should be distinct. This won't be true if they're
93+
# attributed to internal C frames (including generic dispatch machinery)
94+
f25130()
95+
f25130()
96+
testlogs = testlogger.logs
97+
@test length(testlogs) == 2
98+
@test testlogs[1].id != testlogs[2].id
99+
@test testlogs[1].kwargs.caller.func == Symbol("top-level scope")
100+
@test all(l.message == "f25130 message" for l in testlogs)
101+
global_logger(prev_logger)

test/logging.jl

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,6 @@ end
121121
end
122122
end
123123

124-
@testset "Log filtering, global logger" begin
125-
old_logger = global_logger()
126-
logs = let
127-
logger = TestLogger(min_level=Warn)
128-
global_logger(logger)
129-
@info "b"
130-
@warn "c"
131-
logger.logs
132-
end
133-
global_logger(old_logger)
134-
135-
@test length(logs) == 1
136-
@test ismatch((Warn , "c"), logs[1])
137-
end
138-
139124
@testset "Log level filtering - global flag" begin
140125
# Test utility: Log once at each standard level
141126
function log_each_level()
@@ -191,6 +176,19 @@ end
191176
end
192177

193178

179+
#-------------------------------------------------------------------------------
180+
@testset "Logger installation and access" begin
181+
@testset "Global logger" begin
182+
logger1 = global_logger()
183+
logger2 = TestLogger()
184+
# global_logger() returns the previously installed logger
185+
@test logger1 === global_logger(logger2)
186+
# current logger looks up global logger by default.
187+
@test current_logger() === logger2
188+
global_logger(logger1) # Restore global logger
189+
end
190+
end
191+
194192
#-------------------------------------------------------------------------------
195193

196194
# Custom log levels

0 commit comments

Comments
 (0)