From f2a91397fd7f9ca5bb3d296ec6df2de6f9cfc7cb Mon Sep 17 00:00:00 2001 From: shyouhei Date: Tue, 12 Dec 2017 11:56:25 +0000 Subject: [PATCH] Add uplevel keyword to Kernel#warn and use it If uplevel keyword is given, the warning message is prepended with caller file and line information and the string "warning: ". The use of the uplevel keyword makes Kernel#warn format output similar to how rb_warn formats output. This patch modifies net/ftp and net/imap to use Kernel#warn instead of $stderr.puts or $stderr.printf, since they are used for printing warnings. This makes lib/cgi/core and tempfile use $stderr.puts instead of warn for debug logging, since they are used for debug printing and not for warning. This does not modify bundler, rubygems, or rdoc, as those are maintained outside of ruby and probably wish to remain backwards compatible with older ruby versions. rb_warn_m code is originally from nobu, but I've changed it so that it only includes the path and lineno from uplevel (not the method), and also prepends the string "warning: ", to make it more similar to rb_warn. From: Jeremy Evans code@jeremyevans.net Signed-off-by: Urabe Shyouhei shyouhei@ruby-lang.org git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- error.c | 35 ++++++++++++++++++++++++++++++--- lib/cgi/core.rb | 2 +- lib/cmath.rb | 2 +- lib/delegate.rb | 2 +- lib/drb/ssl.rb | 2 +- lib/forwardable.rb | 2 +- lib/ipaddr.rb | 4 ++-- lib/irb/init.rb | 2 +- lib/irb/locale.rb | 2 +- lib/matrix.rb | 16 +++++++-------- lib/net/ftp.rb | 4 ++-- lib/net/http.rb | 2 +- lib/net/http/generic_request.rb | 4 ++-- lib/net/http/header.rb | 4 ++-- lib/net/http/response.rb | 6 +++--- lib/net/imap.rb | 12 ++++------- lib/ostruct.rb | 4 ++-- lib/rexml/cdata.rb | 2 +- lib/rexml/comment.rb | 2 +- lib/rexml/element.rb | 2 +- lib/rexml/instruction.rb | 2 +- lib/rexml/node.rb | 2 +- lib/rexml/text.rb | 2 +- lib/rss/rss.rb | 5 ++--- lib/tempfile.rb | 6 +++--- lib/timeout.rb | 2 +- lib/uri/common.rb | 8 ++++---- lib/uri/generic.rb | 2 +- lib/webrick/server.rb | 2 +- lib/webrick/utils.rb | 2 +- lib/yaml.rb | 6 +++--- test/ruby/test_exception.rb | 5 +++++ 32 files changed, 92 insertions(+), 63 deletions(-) diff --git a/error.c b/error.c index 4b921f99ff..075cb1dfb1 100644 --- a/error.c +++ b/error.c @@ -327,10 +327,39 @@ warning_write(int argc, VALUE *argv, VALUE buf) static VALUE rb_warn_m(int argc, VALUE *argv, VALUE exc) { - if (!NIL_P(ruby_verbose) && argc > 0) { + VALUE opts, uplevel = Qnil; + + if (!NIL_P(ruby_verbose) && argc > 0 && + (argc = rb_scan_args(argc, argv, "*:", NULL, &opts)) > 0) { VALUE str = argv[0]; - if (argc > 1 || !end_with_asciichar(str, '\n')) { - str = rb_str_tmp_new(0); + if (!NIL_P(opts)) { + static ID kwds[1]; + if (!kwds[0]) { + CONST_ID(kwds[0], "uplevel"); + } + rb_get_kwargs(opts, kwds, 0, 1, &uplevel); + if (uplevel == Qundef) { + uplevel = Qnil; + } + else if (!NIL_P(uplevel)) { + uplevel = LONG2NUM((long)NUM2ULONG(uplevel) + 1); + uplevel = rb_vm_thread_backtrace_locations(1, &uplevel, GET_THREAD()->self); + if (!NIL_P(uplevel)) { + uplevel = rb_ary_entry(uplevel, 0); + } + } + } + if (argc > 1 || !NIL_P(uplevel) || !end_with_asciichar(str, '\n')) { + if (NIL_P(uplevel)) { + str = rb_str_tmp_new(0); + } + else { + VALUE path; + path = rb_funcall(uplevel, rb_intern("path"), 0); + str = rb_sprintf("%s:%li: warning: ", + rb_string_value_ptr(&path), + NUM2LONG(rb_funcall(uplevel, rb_intern("lineno"), 0))); + } RBASIC_SET_CLASS(str, rb_cWarningBuffer); rb_io_puts(argc, argv, str); RBASIC_SET_CLASS(str, rb_cString); diff --git a/lib/cgi/core.rb b/lib/cgi/core.rb index 1b5f7ed390..d510b80628 100644 --- a/lib/cgi/core.rb +++ b/lib/cgi/core.rb @@ -267,7 +267,7 @@ class CGI def _header_for_modruby(buf) #:nodoc: request = Apache::request buf.scan(/([^:]+): (.+)#{EOL}/o) do |name, value| - warn sprintf("name:%s value:%s\n", name, value) if $DEBUG + $stderr.puts sprintf("name:%s value:%s\n", name, value) if $DEBUG case name when 'Set-Cookie' request.headers_out.add(name, value) diff --git a/lib/cmath.rb b/lib/cmath.rb index 4f327fd188..7dbd65e799 100644 --- a/lib/cmath.rb +++ b/lib/cmath.rb @@ -50,7 +50,7 @@ module CMath atanh ].each do |meth| define_method(meth + '!') do |*args, &block| - warn("CMath##{meth}! is deprecated; use CMath##{meth} or Math##{meth}") if $VERBOSE + warn("CMath##{meth}! is deprecated; use CMath##{meth} or Math##{meth}", uplevel: 1) if $VERBOSE RealMath.send(meth, *args, &block) end end diff --git a/lib/delegate.rb b/lib/delegate.rb index 71aff40f7f..37819a28f4 100644 --- a/lib/delegate.rb +++ b/lib/delegate.rb @@ -97,7 +97,7 @@ class Delegator < BasicObject target = self.__getobj__ {r = false} r &&= target.respond_to?(m, include_private) if r && include_private && !target.respond_to?(m, false) - warn "#{caller(3, 1)[0]}: delegator does not forward private method \##{m}" + warn "delegator does not forward private method \##{m}", uplevel: 3 return false end r diff --git a/lib/drb/ssl.rb b/lib/drb/ssl.rb index 8d2724e736..45fe4f1f74 100644 --- a/lib/drb/ssl.rb +++ b/lib/drb/ssl.rb @@ -336,7 +336,7 @@ module DRb end self.class.new(uri, ssl, @config, true) rescue OpenSSL::SSL::SSLError - warn("#{__FILE__}:#{__LINE__}: warning: #{$!.message} (#{$!.class})") if @config[:verbose] + warn("#{$!.message} (#{$!.class})", uplevel: 0) if @config[:verbose] retry end end diff --git a/lib/forwardable.rb b/lib/forwardable.rb index 3662f56011..a8e5aa1d5a 100644 --- a/lib/forwardable.rb +++ b/lib/forwardable.rb @@ -206,7 +206,7 @@ module Forwardable method_call = "#{<<-"begin;"}\n#{<<-"end;".chomp}" begin; unless defined? _.#{method} - ::Kernel.warn "\#{caller_locations(1)[0]}: "#{mesg.dump}"\#{_.class}"'##{method}' + ::Kernel.warn #{mesg.dump}"\#{_.class}"'##{method}', uplevel: 1 _#{method_call} else _.#{method}(*args, &block) diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb index 0f3a5e2878..60f102d4e6 100644 --- a/lib/ipaddr.rb +++ b/lib/ipaddr.rb @@ -310,7 +310,7 @@ class IPAddr # Returns true if the ipaddr is an IPv4-compatible IPv6 address. def ipv4_compat? - warn "#{caller(1, 1)[0]}: warning: IPAddr\##{__callee__} is obsolete" if $VERBOSE + warn "IPAddr\##{__callee__} is obsolete", uplevel: 1 if $VERBOSE _ipv4_compat? end @@ -336,7 +336,7 @@ class IPAddr # Returns a new ipaddr built by converting the native IPv4 address # into an IPv4-compatible IPv6 address. def ipv4_compat - warn "#{caller(1, 1)[0]}: warning: IPAddr\##{__callee__} is obsolete" if $VERBOSE + warn "IPAddr\##{__callee__} is obsolete", uplevel: 1 if $VERBOSE if !ipv4? raise InvalidAddressError, "not an IPv4 address" end diff --git a/lib/irb/init.rb b/lib/irb/init.rb index db03340a69..c50e3421ec 100644 --- a/lib/irb/init.rb +++ b/lib/irb/init.rb @@ -277,7 +277,7 @@ module IRB # :nodoc: begin require m rescue LoadError => err - warn err.backtrace[0] << ":#{err.class}: #{err}" + warn ":#{err.class}: #{err}", uplevel: 0 end end end diff --git a/lib/irb/locale.rb b/lib/irb/locale.rb index df540c8cbb..b713f50e76 100644 --- a/lib/irb/locale.rb +++ b/lib/irb/locale.rb @@ -31,7 +31,7 @@ module IRB # :nodoc: if @encoding_name begin load 'irb/encoding_aliases.rb'; rescue LoadError; end if @encoding = @@legacy_encoding_alias_map[@encoding_name] - warn "%s is obsolete. use %s" % ["#{@lang}_#{@territory}.#{@encoding_name}", "#{@lang}_#{@territory}.#{@encoding.name}"] + warn(("%s is obsolete. use %s" % ["#{@lang}_#{@territory}.#{@encoding_name}", "#{@lang}_#{@territory}.#{@encoding.name}"]), uplevel: 1) end @encoding = Encoding.find(@encoding_name) rescue nil end diff --git a/lib/matrix.rb b/lib/matrix.rb index ca4424f4b1..923e716b35 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -1184,7 +1184,7 @@ class Matrix # deprecated; use Matrix#determinant # def determinant_e - warn "#{caller(1, 1)[0]}: warning: Matrix#determinant_e is deprecated; use #determinant" + warn "Matrix#determinant_e is deprecated; use #determinant", uplevel: 1 determinant end alias det_e determinant_e @@ -1242,7 +1242,7 @@ class Matrix # deprecated; use Matrix#rank # def rank_e - warn "#{caller(1, 1)[0]}: warning: Matrix#rank_e is deprecated; use #rank" + warn "Matrix#rank_e is deprecated; use #rank", uplevel: 1 rank end @@ -1435,17 +1435,17 @@ class Matrix end def elements_to_f - warn "#{caller(1, 1)[0]}: warning: Matrix#elements_to_f is deprecated, use map(&:to_f)" + warn "Matrix#elements_to_f is deprecated, use map(&:to_f)", uplevel: 1 map(&:to_f) end def elements_to_i - warn "#{caller(1, 1)[0]}: warning: Matrix#elements_to_i is deprecated, use map(&:to_i)" + warn "Matrix#elements_to_i is deprecated, use map(&:to_i)", uplevel: 1 map(&:to_i) end def elements_to_r - warn "#{caller(1, 1)[0]}: warning: Matrix#elements_to_r is deprecated, use map(&:to_r)" + warn "Matrix#elements_to_r is deprecated, use map(&:to_r)", uplevel: 1 map(&:to_r) end @@ -2098,17 +2098,17 @@ class Vector end def elements_to_f - warn "#{caller(1, 1)[0]}: warning: Vector#elements_to_f is deprecated" + warn "Vector#elements_to_f is deprecated", uplevel: 1 map(&:to_f) end def elements_to_i - warn "#{caller(1, 1)[0]}: warning: Vector#elements_to_i is deprecated" + warn "Vector#elements_to_i is deprecated", uplevel: 1 map(&:to_i) end def elements_to_r - warn "#{caller(1, 1)[0]}: warning: Vector#elements_to_r is deprecated" + warn "Vector#elements_to_r is deprecated", uplevel: 1 map(&:to_r) end diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb index cfd72ee4e0..a7915189bd 100644 --- a/lib/net/ftp.rb +++ b/lib/net/ftp.rb @@ -310,13 +310,13 @@ module Net # Obsolete def return_code # :nodoc: - $stderr.puts("warning: Net::FTP#return_code is obsolete and do nothing") + warn("Net::FTP#return_code is obsolete and do nothing", uplevel: 1) return "\n" end # Obsolete def return_code=(s) # :nodoc: - $stderr.puts("warning: Net::FTP#return_code= is obsolete and do nothing") + warn("Net::FTP#return_code= is obsolete and do nothing", uplevel: 1) end # Constructs a socket with +host+ and +port+. diff --git a/lib/net/http.rb b/lib/net/http.rb index 6aa3839011..c2d33018a2 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -708,7 +708,7 @@ module Net #:nodoc: # http.start { .... } # def set_debug_output(output) - warn 'Net::HTTP#set_debug_output called after HTTP started' if started? + warn 'Net::HTTP#set_debug_output called after HTTP started', uplevel: 1 if started? @debug_output = output end diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb index 6c5ceafe61..526cc333fc 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -82,7 +82,7 @@ class Net::HTTPGenericRequest end def body_exist? - warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?" if $VERBOSE + warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?", uplevel: 1 if $VERBOSE response_body_permitted? end @@ -299,7 +299,7 @@ class Net::HTTPGenericRequest def supply_default_content_type return if content_type() - warn 'net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded' if $VERBOSE + warn 'net/http: Content-Type did not set; using application/x-www-form-urlencoded', uplevel: 1 if $VERBOSE set_content_type 'application/x-www-form-urlencoded' end diff --git a/lib/net/http/header.rb b/lib/net/http/header.rb index 3484b5b681..5c543e769d 100644 --- a/lib/net/http/header.rb +++ b/lib/net/http/header.rb @@ -14,9 +14,9 @@ module Net::HTTPHeader @header = {} return unless initheader initheader.each do |key, value| - warn "net/http: warning: duplicated HTTP header: #{key}" if key?(key) and $VERBOSE + warn "net/http: duplicated HTTP header: #{key}", uplevel: 1 if key?(key) and $VERBOSE if value.nil? - warn "net/http: warning: nil HTTP header: #{key}" if $VERBOSE + warn "net/http: nil HTTP header: #{key}", uplevel: 1 if $VERBOSE else @header[key.downcase] = [value.strip] end diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index ee02f4be4a..6a78272ac8 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -140,17 +140,17 @@ class Net::HTTPResponse # def response #:nodoc: - warn "#{caller(1, 1)[0]}: warning: Net::HTTPResponse#response is obsolete" if $VERBOSE + warn "Net::HTTPResponse#response is obsolete", uplevel: 1 if $VERBOSE self end def header #:nodoc: - warn "#{caller(1, 1)[0]}: warning: Net::HTTPResponse#header is obsolete" if $VERBOSE + warn "Net::HTTPResponse#header is obsolete", uplevel: 1 if $VERBOSE self end def read_header #:nodoc: - warn "#{caller(1, 1)[0]}: warning: Net::HTTPResponse#read_header is obsolete" if $VERBOSE + warn "Net::HTTPResponse#read_header is obsolete", uplevel: 1 if $VERBOSE self end diff --git a/lib/net/imap.rb b/lib/net/imap.rb index bb6754c732..3d33b94fdd 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -2032,8 +2032,7 @@ module Net # generate a warning message to +stderr+, then return # the value of +subtype+. def media_subtype - $stderr.printf("warning: media_subtype is obsolete.\n") - $stderr.printf(" use subtype instead.\n") + warn("media_subtype is obsolete, use subtype instead.\n", uplevel: 1) return subtype end end @@ -2060,8 +2059,7 @@ module Net # generate a warning message to +stderr+, then return # the value of +subtype+. def media_subtype - $stderr.printf("warning: media_subtype is obsolete.\n") - $stderr.printf(" use subtype instead.\n") + warn("media_subtype is obsolete, use subtype instead.\n", uplevel: 1) return subtype end end @@ -2090,8 +2088,7 @@ module Net # generate a warning message to +stderr+, then return # the value of +subtype+. def media_subtype - $stderr.printf("warning: media_subtype is obsolete.\n") - $stderr.printf(" use subtype instead.\n") + warn("media_subtype is obsolete, use subtype instead.\n", uplevel: 1) return subtype end end @@ -2151,8 +2148,7 @@ module Net # generate a warning message to +stderr+, then return # the value of +subtype+. def media_subtype - $stderr.printf("warning: media_subtype is obsolete.\n") - $stderr.printf(" use subtype instead.\n") + warn("media_subtype is obsolete, use subtype instead.\n", uplevel: 1) return subtype end end diff --git a/lib/ostruct.rb b/lib/ostruct.rb index 6cf0ecefc3..28890304e4 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -162,7 +162,7 @@ class OpenStruct end private :modifiable? - # ::Kernel.warn("#{caller(1, 1)[0]}: do not use OpenStruct#modifiable") + # ::Kernel.warn("do not use OpenStruct#modifiable", uplevel: 1) alias modifiable modifiable? # :nodoc: protected :modifiable @@ -181,7 +181,7 @@ class OpenStruct end private :new_ostruct_member! - # ::Kernel.warn("#{caller(1, 1)[0]}: do not use OpenStruct#new_ostruct_member") + # ::Kernel.warn("do not use OpenStruct#new_ostruct_member", uplevel: 1) alias new_ostruct_member new_ostruct_member! # :nodoc: protected :new_ostruct_member diff --git a/lib/rexml/cdata.rb b/lib/rexml/cdata.rb index fe9b49b5f7..2238446dc4 100644 --- a/lib/rexml/cdata.rb +++ b/lib/rexml/cdata.rb @@ -58,7 +58,7 @@ module REXML # c = CData.new( " Some text " ) # c.write( $stdout ) #-> def write( output=$stdout, indent=-1, transitive=false, ie_hack=false ) - Kernel.warn( "#{self.class.name}.write is deprecated" ) + Kernel.warn( "#{self.class.name}.write is deprecated", uplevel: 1) indent( output, indent ) output << START output << @string diff --git a/lib/rexml/comment.rb b/lib/rexml/comment.rb index 746af77296..822fe0d586 100644 --- a/lib/rexml/comment.rb +++ b/lib/rexml/comment.rb @@ -48,7 +48,7 @@ module REXML # ie_hack:: # Needed for conformity to the child API, but not used by this class. def write( output, indent=-1, transitive=false, ie_hack=false ) - Kernel.warn("Comment.write is deprecated. See REXML::Formatters") + Kernel.warn("Comment.write is deprecated. See REXML::Formatters", uplevel: 1) indent( output, indent ) output << START output << @string diff --git a/lib/rexml/element.rb b/lib/rexml/element.rb index a9811bcba3..ac9b10872c 100644 --- a/lib/rexml/element.rb +++ b/lib/rexml/element.rb @@ -710,7 +710,7 @@ module REXML # doc.write( out ) #-> doc is written to the string 'out' # doc.write( $stdout ) #-> doc written to the console def write(output=$stdout, indent=-1, transitive=false, ie_hack=false) - Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters") + Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters", uplevel: 1) formatter = if indent > -1 if transitive require "rexml/formatters/transitive" diff --git a/lib/rexml/instruction.rb b/lib/rexml/instruction.rb index 576939ca2b..c4f65eefc1 100644 --- a/lib/rexml/instruction.rb +++ b/lib/rexml/instruction.rb @@ -43,7 +43,7 @@ module REXML # See the rexml/formatters package # def write writer, indent=-1, transitive=false, ie_hack=false - Kernel.warn( "#{self.class.name}.write is deprecated" ) + Kernel.warn( "#{self.class.name}.write is deprecated", uplevel: 1) indent(writer, indent) writer << START.sub(/\\/u, '') writer << @target diff --git a/lib/rexml/node.rb b/lib/rexml/node.rb index c7a3936799..52337ade44 100644 --- a/lib/rexml/node.rb +++ b/lib/rexml/node.rb @@ -26,7 +26,7 @@ module REXML # REXML::Formatters package for changing the output style. def to_s indent=nil unless indent.nil? - Kernel.warn( "#{self.class.name}.to_s(indent) parameter is deprecated" ) + Kernel.warn( "#{self.class.name}.to_s(indent) parameter is deprecated", uplevel: 1) f = REXML::Formatters::Pretty.new( indent ) f.write( self, rv = "" ) else diff --git a/lib/rexml/text.rb b/lib/rexml/text.rb index 9ea8ba9df3..86269dea1e 100644 --- a/lib/rexml/text.rb +++ b/lib/rexml/text.rb @@ -293,7 +293,7 @@ module REXML # See REXML::Formatters # def write( writer, indent=-1, transitive=false, ie_hack=false ) - Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters") + Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters", uplevel: 1) formatter = if indent > -1 REXML::Formatters::Pretty.new( indent ) else diff --git a/lib/rss/rss.rb b/lib/rss/rss.rb index f88bb3c1ba..db87e11ad5 100644 --- a/lib/rss/rss.rb +++ b/lib/rss/rss.rb @@ -596,11 +596,10 @@ EOC def #{accessor_name}=(*args) receiver = self.class.name - warn("Warning:\#{caller.first.sub(/:in `.*'\z/, '')}: " \ - "Don't use `\#{receiver}\##{accessor_name} = XXX'/" \ + warn("Don't use `\#{receiver}\##{accessor_name} = XXX'/" \ "`\#{receiver}\#set_#{accessor_name}(XXX)'. " \ "Those APIs are not sense of Ruby. " \ - "Use `\#{receiver}\##{plural_name} << XXX' instead of them.") + "Use `\#{receiver}\##{plural_name} << XXX' instead of them.", uplevel: 1) if args.size == 1 @#{accessor_name}.push(args[0]) else diff --git a/lib/tempfile.rb b/lib/tempfile.rb index 5489ac044f..671ac7b83b 100644 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -124,7 +124,7 @@ class Tempfile < DelegateClass(File) # If Tempfile.new cannot find a unique filename within a limited # number of tries, then it will raise an exception. def initialize(basename="", tmpdir=nil, mode: 0, **options) - warn "Tempfile.new doesn't call the given block." if block_given? + warn "Tempfile.new doesn't call the given block.", uplevel: 1 if block_given? @unlinked = false @mode = mode|File::RDWR|File::CREAT|File::EXCL @@ -250,7 +250,7 @@ class Tempfile < DelegateClass(File) def call(*args) return if @pid != Process.pid - warn "removing #{@tmpfile.path}..." if $DEBUG + $stderr.puts "removing #{@tmpfile.path}..." if $DEBUG @tmpfile.close begin @@ -258,7 +258,7 @@ class Tempfile < DelegateClass(File) rescue Errno::ENOENT end - warn "done" if $DEBUG + $stderr.puts "done" if $DEBUG end end diff --git a/lib/timeout.rb b/lib/timeout.rb index 057dd7fe68..a33bb4ce65 100644 --- a/lib/timeout.rb +++ b/lib/timeout.rb @@ -118,7 +118,7 @@ module Timeout end def timeout(*args, &block) - warn "#{caller_locations(1, 1)[0]}: Object##{__method__} is deprecated, use Timeout.timeout instead." + warn "Object##{__method__} is deprecated, use Timeout.timeout instead.", uplevel: 1 Timeout.timeout(*args, &block) end diff --git a/lib/uri/common.rb b/lib/uri/common.rb index a082c2a918..764f89d810 100644 --- a/lib/uri/common.rb +++ b/lib/uri/common.rb @@ -100,7 +100,7 @@ module URI # # => "@%3F@%21" # def escape(*arg) - warn "#{caller(1, 1)[0]}: warning: URI.escape is obsolete" if $VERBOSE + warn "URI.escape is obsolete", uplevel: 1 if $VERBOSE DEFAULT_PARSER.escape(*arg) end alias encode escape @@ -132,7 +132,7 @@ module URI # # => "http://example.com/?a=\t\r" # def unescape(*arg) - warn "#{caller(1, 1)[0]}: warning: URI.unescape is obsolete" if $VERBOSE + warn "URI.unescape is obsolete", uplevel: 1 if $VERBOSE DEFAULT_PARSER.unescape(*arg) end alias decode unescape @@ -300,7 +300,7 @@ module URI # # => ["http://foo.example.com/bla", "mailto:test@example.com"] # def self.extract(str, schemes = nil, &block) - warn "#{caller(1, 1)[0]}: warning: URI.extract is obsolete" if $VERBOSE + warn "URI.extract is obsolete", uplevel: 1 if $VERBOSE DEFAULT_PARSER.extract(str, schemes, &block) end @@ -336,7 +336,7 @@ module URI # end # def self.regexp(schemes = nil) - warn "#{caller(1, 1)[0]}: warning: URI.regexp is obsolete" if $VERBOSE + warn "URI.regexp is obsolete", uplevel: 1 if $VERBOSE DEFAULT_PARSER.make_regexp(schemes) end diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index 044d408f50..dc4f9c4c30 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -1517,7 +1517,7 @@ module URI elsif name == 'http_proxy' unless proxy_uri = env[name] if proxy_uri = env[name.upcase] - warn 'The environment variable HTTP_PROXY is discouraged. Use http_proxy.' + warn 'The environment variable HTTP_PROXY is discouraged. Use http_proxy.', uplevel: 1 end end else diff --git a/lib/webrick/server.rb b/lib/webrick/server.rb index 06b7f9fde3..88e160d981 100644 --- a/lib/webrick/server.rb +++ b/lib/webrick/server.rb @@ -103,7 +103,7 @@ module WEBrick @shutdown_pipe = nil unless @config[:DoNotListen] if @config[:Listen] - warn(":Listen option is deprecated; use GenericServer#listen") + warn(":Listen option is deprecated; use GenericServer#listen", uplevel: 1) end listen(@config[:BindAddress], @config[:Port]) if @config[:Port] == 0 diff --git a/lib/webrick/utils.rb b/lib/webrick/utils.rb index 68833af96e..07044876b9 100644 --- a/lib/webrick/utils.rb +++ b/lib/webrick/utils.rb @@ -37,7 +37,7 @@ module WEBrick Process::Sys::setgid(pw.gid) Process::Sys::setuid(pw.uid) else - warn("WEBrick::Utils::su doesn't work on this platform") + warn("WEBrick::Utils::su doesn't work on this platform", uplevel: 1) end end module_function :su diff --git a/lib/yaml.rb b/lib/yaml.rb index 0c33305e1d..aa0a1eb470 100644 --- a/lib/yaml.rb +++ b/lib/yaml.rb @@ -5,9 +5,9 @@ begin require 'psych' rescue LoadError - warn "#{caller[0]}:" - warn "It seems your ruby installation is missing psych (for YAML output)." - warn "To eliminate this warning, please install libyaml and reinstall your ruby." + warn "It seems your ruby installation is missing psych (for YAML output).\n" \ + "To eliminate this warning, please install libyaml and reinstall your ruby.\n", + uplevel: 1 raise end diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb index da1cbd37cf..ce3e98f2c8 100644 --- a/test/ruby/test_exception.rb +++ b/test/ruby/test_exception.rb @@ -1003,6 +1003,11 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| assert_equal(["\n"], capture_warning_warn {warn ""}) end + def test_kernel_warn_uplevel + warning = capture_warning_warn {warn("test warning", uplevel: 0)} + assert_equal("#{__FILE__}:#{__LINE__-1}: warning: test warning\n", warning[0]) + end + def test_warning_warn_invalid_argument assert_raise(TypeError) do ::Warning.warn nil