Skip to content

Commit

Permalink
[rubygems/rubygems] Let RDoc parse the doc of Kernel#require
Browse files Browse the repository at this point in the history
Since RDoc does not parse string literals as documents, `eval` the
entire file instead of embedding in a here-document.
On the contrary, as `gem_original_require` alias is an implementation
detail but not for users, it should not be documented.

rubygems/rubygems@cad4cf16cf
  • Loading branch information
nobu authored and matzbot committed Jan 8, 2023
1 parent fd98169 commit 1a1b653
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 46 deletions.
17 changes: 11 additions & 6 deletions lib/rubygems.rb
Expand Up @@ -119,10 +119,6 @@ module Gem
# to avoid deprecation warnings in Ruby 2.7.
UNTAINT = RUBY_VERSION < "2.7" ? :untaint.to_sym : proc {}

# When https://bugs.ruby-lang.org/issues/17259 is available, there is no need to override Kernel#warn
KERNEL_WARN_IGNORES_INTERNAL_ENTRIES = RUBY_ENGINE == "truffleruby" ||
(RUBY_ENGINE == "ruby" && RUBY_VERSION >= "3.0")

##
# An Array of Regexps that match windows Ruby platforms.

Expand Down Expand Up @@ -1348,7 +1344,16 @@ def default_gem_load_paths
Gem::Specification.load_defaults

require_relative "rubygems/core_ext/kernel_gem"
require_relative "rubygems/core_ext/kernel_require"
require_relative "rubygems/core_ext/kernel_warn"

path = File.join(__dir__, "rubygems/core_ext/kernel_require.rb")
# When https://bugs.ruby-lang.org/issues/17259 is available, there is no need to override Kernel#warn
if RUBY_ENGINE == "truffleruby" ||
(RUBY_ENGINE == "ruby" && RUBY_VERSION >= "3.0")
file = "<internal:#{path}>"
else
require_relative "rubygems/core_ext/kernel_warn"
file = path
end
eval File.read(path), nil, file

require ENV["BUNDLER_SETUP"] if ENV["BUNDLER_SETUP"] && !defined?(Bundler)
7 changes: 3 additions & 4 deletions lib/rubygems/core_ext/kernel_require.rb
Expand Up @@ -13,12 +13,12 @@ module Kernel

# Make sure we have a reference to Ruby's original Kernel#require
unless defined?(gem_original_require)
# :stopdoc:
alias gem_original_require require
private :gem_original_require
# :startdoc:
end

file = Gem::KERNEL_WARN_IGNORES_INTERNAL_ENTRIES ? "<internal:#{__FILE__}>" : __FILE__
module_eval <<'RUBY', file, __LINE__ + 1 # rubocop:disable Style/EvalWithLocation
##
# When RubyGems is required, Kernel#require is replaced with our own which
# is capable of loading gems on demand.
Expand All @@ -33,7 +33,7 @@ module Kernel
# The normal <tt>require</tt> functionality of returning false if
# that file has already been loaded is preserved.

def require(path)
def require(path) # :doc:
if RUBYGEMS_ACTIVATION_MONITOR.respond_to?(:mon_owned?)
monitor_owned = RUBYGEMS_ACTIVATION_MONITOR.mon_owned?
end
Expand Down Expand Up @@ -168,7 +168,6 @@ def require(path)
end
end
end
RUBY

private :require

Expand Down
69 changes: 33 additions & 36 deletions lib/rubygems/core_ext/kernel_warn.rb
@@ -1,53 +1,50 @@
# frozen_string_literal: true

if !Gem::KERNEL_WARN_IGNORES_INTERNAL_ENTRIES
module Kernel
rubygems_path = "#{__dir__}/" # Frames to be skipped start with this path.

module Kernel
rubygems_path = "#{__dir__}/" # Frames to be skipped start with this path.
original_warn = instance_method(:warn)

original_warn = instance_method(:warn)
remove_method :warn

class << self
remove_method :warn
end

class << self
remove_method :warn
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

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)
# 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
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

start += 1

if path = loc.path
unless path.start_with?(rubygems_path) || path.start_with?("<internal:")
# Non-rubygems frames
uplevel -= 1
end
if path = loc.path
unless path.start_with?(rubygems_path) || path.start_with?("<internal:")
# Non-rubygems frames
uplevel -= 1
end
end
kw[:uplevel] = start
end
kw[:uplevel] = start
end

original_warn.bind(self).call(*messages, **kw)
}
end
original_warn.bind(self).call(*messages, **kw)
}
end

0 comments on commit 1a1b653

Please sign in to comment.