mirror of
https://github.com/ruby/ruby.git
synced 2025-08-23 21:14:23 +02:00

* See https://github.com/oracle/truffleruby/issues/2046
* `<internal:` is a common prefix also used by core Ruby files in CRuby.
* test_no_kernel_require_in_*warn_with_uplevel already test this.
* Unfortunately just skipping `<internal:` in the Ruby implementation
is not enough, because RubyGems' #warn would not skip the
`<internal:` require (TruffleRuby defines #require in Ruby),
and the Ruby implementation's #warn would not skip
RubyGems's #require. The #caller_locations(0) look like this:
warnee.rb:1:in `<top (required)>' # where #warn is called
<internal:core> core/kernel.rb:234:in `gem_original_require' # not skipped by RubyGems' warn, skipped by the Ruby impl
rubygems/core_ext/kernel_require.rb:54:in `require' # not skipped by the Ruby impl's warn, what would be shown without this fix
warn.rb:1:in `<main>' # what would be correct
warn.rb is
require "warnee"
warnee.rb is
puts caller_locations(0), nil
warn "oops", uplevel: 1
7c838f7419
54 lines
1.2 KiB
Ruby
54 lines
1.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# `uplevel` keyword argument of Kernel#warn is available since ruby 2.5.
|
|
if RUBY_VERSION >= "2.5"
|
|
|
|
module Kernel
|
|
rubygems_path = "#{__dir__}/" # Frames to be skipped start with this path.
|
|
|
|
original_warn = 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?
|
|
return original_warn.call(*messages)
|
|
else
|
|
return original_warn.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
|
|
|
|
path = loc.path
|
|
unless path.start_with?(rubygems_path) or path.start_with?('<internal:')
|
|
# Non-rubygems frames
|
|
uplevel -= 1
|
|
end
|
|
end
|
|
uplevel = start
|
|
end
|
|
|
|
kw[:uplevel] = uplevel
|
|
original_warn.call(*messages, **kw)
|
|
}
|
|
end
|
|
end
|