mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00

jruby-head (which will be JRuby 9.4.0.0) can now properly process the keywords to Kernel#warn. I cannot think of any capability based test for this so I constrained it using a version guard. Only JRuby will ever hit the version guard. https://github.com/rubygems/rubygems/commit/cd468c7e0f
54 lines
1.3 KiB
Ruby
54 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# `uplevel` keyword argument of Kernel#warn is available since ruby 2.5.
|
|
if RUBY_VERSION >= "2.5" && !Gem::KERNEL_WARN_IGNORES_INTERNAL_ENTRIES
|
|
|
|
module Kernel
|
|
rubygems_path = "#{__dir__}/" # Frames to be skipped start with this path.
|
|
|
|
original_warn = instance_method(:warn)
|
|
|
|
remove_method :warn
|
|
|
|
class << self
|
|
remove_method :warn
|
|
end
|
|
|
|
module_function define_method(:warn) {|*messages, **kw|
|
|
unless uplevel = kw[:uplevel]
|
|
if Gem.java_platform? && RUBY_VERSION < "3.1"
|
|
return original_warn.bind(self).call(*messages)
|
|
else
|
|
return original_warn.bind(self).call(*messages, **kw)
|
|
end
|
|
end
|
|
|
|
# Ensure `uplevel` fits a `long`
|
|
uplevel, = [uplevel].pack("l!").unpack("l!")
|
|
|
|
if uplevel >= 0
|
|
start = 0
|
|
while uplevel >= 0
|
|
loc, = caller_locations(start, 1)
|
|
unless loc
|
|
# No more backtrace
|
|
start += uplevel
|
|
break
|
|
end
|
|
|
|
start += 1
|
|
|
|
if path = loc.path
|
|
unless path.start_with?(rubygems_path) || path.start_with?("<internal:")
|
|
# Non-rubygems frames
|
|
uplevel -= 1
|
|
end
|
|
end
|
|
end
|
|
kw[:uplevel] = start
|
|
end
|
|
|
|
original_warn.bind(self).call(*messages, **kw)
|
|
}
|
|
end
|
|
end
|