Skip to content

Commit

Permalink
prefer caller_locations and Thread::Backtrace::Location methods
Browse files Browse the repository at this point in the history
Mechanize has still been using `Kernel#caller` whose string
representation has changed enough in Ruby 3.4.0.dev to break how
deprecation messages were being handrolled.

Replace all uses of `caller` with `caller_locations` which is both
faster and returns an object with some nice methods we can use to
isolate the method, the file, and the line number.

This should get us green again on ruby-head.
  • Loading branch information
flavorjones committed Feb 21, 2024
1 parent 9de5c1d commit 73b8a7e
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/mechanize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,10 @@ class << self
# authentication.

def auth user, password, domain = nil
caller.first =~ /(.*?):(\d+).*?$/
c = caller_locations(1,1).first

warn <<-WARNING
At #{$1} line #{$2}
At #{c.absolute_path} line #{c.lineno}
Use of #auth and #basic_auth are deprecated due to a security vulnerability.
Expand Down
4 changes: 2 additions & 2 deletions lib/mechanize/cookie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Mechanize
module CookieDeprecated
def __deprecated__(to = nil)
$VERBOSE or return
method = caller[0][/([^`]+)(?='$)/]
method = caller_locations(1,1).first.base_label
to ||= method
case self
when Class
Expand All @@ -21,7 +21,7 @@ def __deprecated__(to = nil)
this = '%s#%s' % [klass, method]
that = 'HTTP::%s#%s' % [lname, to]
end
warn '%s: The call of %s needs to be fixed to follow the new API (%s).' % [caller[1], this, that]
warn '%s: The call of %s needs to be fixed to follow the new API (%s).' % [caller_locations(2,1).first, this, that]
end
private :__deprecated__
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_mechanize_http_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def auth_realm uri, scheme, type

def skip_if_jruby_zlib
if RUBY_ENGINE == 'jruby'
meth = caller[0][/`(\w+)/, 1]
meth = caller_locations(1,1).first.base_label
skip "#{meth}: skipped because how Zlib handles error is different in JRuby"
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_mechanize_page_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def util_page body = @body, res = @res

def skip_if_nkf_dependency
if RUBY_ENGINE == 'jruby'
meth = caller[0][/`(\w+)/, 1]
meth = caller_locations(1,1).first.base_label
skip "#{meth}: skipped because this feature currently depends on NKF"
end
end
Expand Down

0 comments on commit 73b8a7e

Please sign in to comment.