diff options
Diffstat (limited to 'lib/rubygems/core_ext')
-rw-r--r-- | lib/rubygems/core_ext/kernel_gem.rb | 13 | ||||
-rw-r--r-- | lib/rubygems/core_ext/kernel_require.rb | 202 | ||||
-rw-r--r-- | lib/rubygems/core_ext/kernel_warn.rb | 73 | ||||
-rw-r--r-- | lib/rubygems/core_ext/tcpsocket_init.rb | 6 |
4 files changed, 129 insertions, 165 deletions
diff --git a/lib/rubygems/core_ext/kernel_gem.rb b/lib/rubygems/core_ext/kernel_gem.rb index e722225739..4e09b95c44 100644 --- a/lib/rubygems/core_ext/kernel_gem.rb +++ b/lib/rubygems/core_ext/kernel_gem.rb @@ -1,12 +1,6 @@ # frozen_string_literal: true -## -# RubyGems adds the #gem method to allow activation of specific gem versions -# and overrides the #require method on Kernel to make gems appear as if they -# live on the <code>$LOAD_PATH</code>. See the documentation of these methods -# for further detail. module Kernel - ## # Use Kernel#gem to activate a specific version of +gem_name+. # @@ -39,12 +33,12 @@ module Kernel # GEM_SKIP=libA:libB ruby -I../libA -I../libB ./mycode.rb def gem(gem_name, *requirements) # :doc: - skip_list = (ENV['GEM_SKIP'] || "").split(/:/) + skip_list = (ENV["GEM_SKIP"] || "").split(/:/) raise Gem::LoadError, "skipping #{gem_name}" if skip_list.include? gem_name - if gem_name.kind_of? Gem::Dependency + if gem_name.is_a? Gem::Dependency unless Gem::Deprecate.skip - warn "#{Gem.location_of_caller.join ':'}:Warning: Kernel.gem no longer "\ + warn "#{Gem.location_of_caller.join ":"}:Warning: Kernel.gem no longer "\ "accepts a Gem::Dependency object, please pass the name "\ "and requirements directly" end @@ -71,5 +65,4 @@ module Kernel end private :gem - end diff --git a/lib/rubygems/core_ext/kernel_require.rb b/lib/rubygems/core_ext/kernel_require.rb index 4b867c55e9..073966b696 100644 --- a/lib/rubygems/core_ext/kernel_require.rb +++ b/lib/rubygems/core_ext/kernel_require.rb @@ -1,24 +1,24 @@ # frozen_string_literal: true + #-- # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others. # All rights reserved. # See LICENSE.txt for permissions. #++ -require 'monitor' +require "monitor" module Kernel - RUBYGEMS_ACTIVATION_MONITOR = Monitor.new # :nodoc: # Make sure we have a reference to Ruby's original Kernel#require unless defined?(gem_original_require) - alias gem_original_require require + # :stopdoc: + alias_method :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 ## # When RubyGems is required, Kernel#require is replaced with our own which # is capable of loading gems on demand. @@ -33,143 +33,117 @@ module Kernel # The normal <tt>require</tt> functionality of returning false if # that file has already been loaded is preserved. - def require(path) - if RUBYGEMS_ACTIVATION_MONITOR.respond_to?(:mon_owned?) - monitor_owned = RUBYGEMS_ACTIVATION_MONITOR.mon_owned? - end - RUBYGEMS_ACTIVATION_MONITOR.enter - - path = path.to_path if path.respond_to? :to_path - - if spec = Gem.find_unresolved_default_spec(path) - # Ensure -I beats a default gem - resolved_path = begin - rp = nil - load_path_check_index = Gem.load_path_insert_index - Gem.activated_gem_paths - Gem.suffixes.each do |s| - $LOAD_PATH[0...load_path_check_index].each do |lp| - safe_lp = lp.dup.tap(&Gem::UNTAINT) - begin - if File.symlink? safe_lp # for backward compatibility + def require(path) # :doc: + return gem_original_require(path) unless Gem.discover_gems_on_require + + RUBYGEMS_ACTIVATION_MONITOR.synchronize do + path = File.path(path) + + # If +path+ belongs to a default gem, we activate it and then go straight + # to normal require + + if spec = Gem.find_default_spec(path) + name = spec.name + + next if Gem.loaded_specs[name] + + # Ensure -I beats a default gem + resolved_path = begin + rp = nil + load_path_check_index = Gem.load_path_insert_index - Gem.activated_gem_paths + Gem.suffixes.find do |s| + $LOAD_PATH[0...load_path_check_index].find do |lp| + if File.symlink? lp # for backward compatibility next end - rescue SecurityError - RUBYGEMS_ACTIVATION_MONITOR.exit - raise - end - full_path = File.expand_path(File.join(safe_lp, "#{path}#{s}")) - if File.file?(full_path) - rp = full_path - break + full_path = File.expand_path(File.join(lp, "#{path}#{s}")) + rp = full_path if File.file?(full_path) end end - break if rp + rp end - rp - end - begin - Kernel.send(:gem, spec.name, Gem::Requirement.default_prerelease) - rescue Exception - RUBYGEMS_ACTIVATION_MONITOR.exit - raise - end unless resolved_path - end + Kernel.send(:gem, name, Gem::Requirement.default_prerelease) unless + resolved_path - # If there are no unresolved deps, then we can use just try - # normal require handle loading a gem from the rescue below. + next + end - if Gem::Specification.unresolved_deps.empty? - RUBYGEMS_ACTIVATION_MONITOR.exit - return gem_original_require(path) - end + # If there are no unresolved deps, then we can use just try + # normal require handle loading a gem from the rescue below. - # If +path+ is for a gem that has already been loaded, don't - # bother trying to find it in an unresolved gem, just go straight - # to normal require. - #-- - # TODO request access to the C implementation of this to speed up RubyGems + if Gem::Specification.unresolved_deps.empty? + next + end - if Gem::Specification.find_active_stub_by_path(path) - RUBYGEMS_ACTIVATION_MONITOR.exit - return gem_original_require(path) - end + # If +path+ is for a gem that has already been loaded, don't + # bother trying to find it in an unresolved gem, just go straight + # to normal require. + #-- + # TODO request access to the C implementation of this to speed up RubyGems - # Attempt to find +path+ in any unresolved gems... - - found_specs = Gem::Specification.find_in_unresolved path - - # If there are no directly unresolved gems, then try and find +path+ - # in any gems that are available via the currently unresolved gems. - # For example, given: - # - # a => b => c => d - # - # If a and b are currently active with c being unresolved and d.rb is - # requested, then find_in_unresolved_tree will find d.rb in d because - # it's a dependency of c. - # - if found_specs.empty? - found_specs = Gem::Specification.find_in_unresolved_tree path - - found_specs.each do |found_spec| - found_spec.activate + if Gem::Specification.find_active_stub_by_path(path) + next end - # We found +path+ directly in an unresolved gem. Now we figure out, of - # the possible found specs, which one we should activate. - else + # Attempt to find +path+ in any unresolved gems... - # Check that all the found specs are just different - # versions of the same gem - names = found_specs.map(&:name).uniq + found_specs = Gem::Specification.find_in_unresolved path - if names.size > 1 - RUBYGEMS_ACTIVATION_MONITOR.exit - raise Gem::LoadError, "#{path} found in multiple gems: #{names.join ', '}" - end + # If there are no directly unresolved gems, then try and find +path+ + # in any gems that are available via the currently unresolved gems. + # For example, given: + # + # a => b => c => d + # + # If a and b are currently active with c being unresolved and d.rb is + # requested, then find_in_unresolved_tree will find d.rb in d because + # it's a dependency of c. + # + if found_specs.empty? + found_specs = Gem::Specification.find_in_unresolved_tree path - # Ok, now find a gem that has no conflicts, starting - # at the highest version. - valid = found_specs.find {|s| !s.has_conflicts? } + found_specs.each(&:activate) - unless valid - le = Gem::LoadError.new "unable to find a version of '#{names.first}' to activate" - le.name = names.first - RUBYGEMS_ACTIVATION_MONITOR.exit - raise le - end + # We found +path+ directly in an unresolved gem. Now we figure out, of + # the possible found specs, which one we should activate. + else - valid.activate - end + # Check that all the found specs are just different + # versions of the same gem + names = found_specs.map(&:name).uniq - RUBYGEMS_ACTIVATION_MONITOR.exit - return gem_original_require(path) - rescue LoadError => load_error - RUBYGEMS_ACTIVATION_MONITOR.enter + if names.size > 1 + raise Gem::LoadError, "#{path} found in multiple gems: #{names.join ", "}" + end - begin - if load_error.path == path and Gem.try_activate(path) - require_again = true + # Ok, now find a gem that has no conflicts, starting + # at the highest version. + valid = found_specs.find {|s| !s.has_conflicts? } + + unless valid + le = Gem::LoadError.new "unable to find a version of '#{names.first}' to activate" + le.name = names.first + raise le + end + + valid.activate end - ensure - RUBYGEMS_ACTIVATION_MONITOR.exit end - return gem_original_require(path) if require_again + begin + gem_original_require(path) + rescue LoadError => load_error + if load_error.path == path && + RUBYGEMS_ACTIVATION_MONITOR.synchronize { Gem.try_activate(path) } - raise load_error - ensure - if RUBYGEMS_ACTIVATION_MONITOR.respond_to?(:mon_owned?) - if monitor_owned != (ow = RUBYGEMS_ACTIVATION_MONITOR.mon_owned?) - STDERR.puts [$$, Thread.current, $!, $!.backtrace].inspect if $! - raise "CRITICAL: RUBYGEMS_ACTIVATION_MONITOR.owned?: before #{monitor_owned} -> after #{ow}" + return gem_original_require(path) end + + raise load_error end end -RUBY private :require - end diff --git a/lib/rubygems/core_ext/kernel_warn.rb b/lib/rubygems/core_ext/kernel_warn.rb index 3373cfdd3b..9dc9f2218c 100644 --- a/lib/rubygems/core_ext/kernel_warn.rb +++ b/lib/rubygems/core_ext/kernel_warn.rb @@ -1,54 +1,49 @@ # 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. - 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? - 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 - - if path = loc.path - unless path.start_with?(rubygems_path) or path.start_with?('<internal:') - # Non-rubygems frames - uplevel -= 1 - end - end + start += 1 + + next unless path = loc.path + unless path.start_with?(rubygems_path, "<internal:") + # Non-rubygems frames + uplevel -= 1 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 diff --git a/lib/rubygems/core_ext/tcpsocket_init.rb b/lib/rubygems/core_ext/tcpsocket_init.rb index 2a79b63bd6..018c49dbeb 100644 --- a/lib/rubygems/core_ext/tcpsocket_init.rb +++ b/lib/rubygems/core_ext/tcpsocket_init.rb @@ -1,4 +1,6 @@ -require 'socket' +# frozen_string_literal: true + +require "socket" module CoreExtensions module TCPSocketExt @@ -17,7 +19,7 @@ module CoreExtensions cond_var = Thread::ConditionVariable.new Addrinfo.foreach(host, serv, nil, :STREAM) do |addr| - Thread.report_on_exception = false if defined? Thread.report_on_exception = () + Thread.report_on_exception = false threads << Thread.new(addr) do # give head start to ipv6 addresses |