ruby-changes:49040
From: shyouhei <ko1@a...>
Date: Tue, 12 Dec 2017 20:56:33 +0900 (JST)
Subject: [ruby-changes:49040] shyouhei:r61155 (trunk): Add uplevel keyword to Kernel#warn and use it
shyouhei 2017-12-12 20:56:25 +0900 (Tue, 12 Dec 2017) New Revision: 61155 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=61155 Log: 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@j... Signed-off-by: Urabe Shyouhei shyouhei@r... Modified files: trunk/error.c trunk/lib/cgi/core.rb trunk/lib/cmath.rb trunk/lib/delegate.rb trunk/lib/drb/ssl.rb trunk/lib/forwardable.rb trunk/lib/ipaddr.rb trunk/lib/irb/init.rb trunk/lib/irb/locale.rb trunk/lib/matrix.rb trunk/lib/net/ftp.rb trunk/lib/net/http/generic_request.rb trunk/lib/net/http/header.rb trunk/lib/net/http/response.rb trunk/lib/net/http.rb trunk/lib/net/imap.rb trunk/lib/ostruct.rb trunk/lib/rexml/cdata.rb trunk/lib/rexml/comment.rb trunk/lib/rexml/element.rb trunk/lib/rexml/instruction.rb trunk/lib/rexml/node.rb trunk/lib/rexml/text.rb trunk/lib/rss/rss.rb trunk/lib/tempfile.rb trunk/lib/timeout.rb trunk/lib/uri/common.rb trunk/lib/uri/generic.rb trunk/lib/webrick/server.rb trunk/lib/webrick/utils.rb trunk/lib/yaml.rb trunk/test/ruby/test_exception.rb Index: lib/forwardable.rb =================================================================== --- lib/forwardable.rb (revision 61154) +++ lib/forwardable.rb (revision 61155) @@ -206,7 +206,7 @@ module Forwardable https://github.com/ruby/ruby/blob/trunk/lib/forwardable.rb#L206 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) Index: lib/uri/generic.rb =================================================================== --- lib/uri/generic.rb (revision 61154) +++ lib/uri/generic.rb (revision 61155) @@ -1517,7 +1517,7 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/generic.rb#L1517 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 Index: lib/yaml.rb =================================================================== --- lib/yaml.rb (revision 61154) +++ lib/yaml.rb (revision 61155) @@ -5,9 +5,9 @@ https://github.com/ruby/ruby/blob/trunk/lib/yaml.rb#L5 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 Index: test/ruby/test_exception.rb =================================================================== --- test/ruby/test_exception.rb (revision 61154) +++ test/ruby/test_exception.rb (revision 61155) @@ -1003,6 +1003,11 @@ $stderr = $stdout; raise "\x82\xa0"') do https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L1003 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 Index: lib/net/http/generic_request.rb =================================================================== --- lib/net/http/generic_request.rb (revision 61154) +++ lib/net/http/generic_request.rb (revision 61155) @@ -82,7 +82,7 @@ class Net::HTTPGenericRequest https://github.com/ruby/ruby/blob/trunk/lib/net/http/generic_request.rb#L82 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 https://github.com/ruby/ruby/blob/trunk/lib/net/http/generic_request.rb#L299 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 Index: lib/net/http/header.rb =================================================================== --- lib/net/http/header.rb (revision 61154) +++ lib/net/http/header.rb (revision 61155) @@ -14,9 +14,9 @@ module Net::HTTPHeader https://github.com/ruby/ruby/blob/trunk/lib/net/http/header.rb#L14 @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 Index: lib/net/http/response.rb =================================================================== --- lib/net/http/response.rb (revision 61154) +++ lib/net/http/response.rb (revision 61155) @@ -140,17 +140,17 @@ class Net::HTTPResponse https://github.com/ruby/ruby/blob/trunk/lib/net/http/response.rb#L140 # 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 Index: lib/net/http.rb =================================================================== --- lib/net/http.rb (revision 61154) +++ lib/net/http.rb (revision 61155) @@ -708,7 +708,7 @@ module Net #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L708 # 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 Index: lib/net/imap.rb =================================================================== --- lib/net/imap.rb (revision 61154) +++ lib/net/imap.rb (revision 61155) @@ -2032,8 +2032,7 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/imap.rb#L2032 # 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 https://github.com/ruby/ruby/blob/trunk/lib/net/imap.rb#L2059 # 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 https://github.com/ruby/ruby/blob/trunk/lib/net/imap.rb#L2088 # 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 https://github.com/ruby/ruby/blob/trunk/lib/net/imap.rb#L2148 # 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 Index: lib/net/ftp.rb =================================================================== --- lib/net/ftp.rb (revision 61154) +++ lib/net/ftp.rb (revision 61155) @@ -310,13 +310,13 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L310 # 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+. Index: lib/matrix.rb =================================================================== --- lib/matrix.rb (revision 61154) +++ lib/matrix.rb (revision 61155) @@ -1184,7 +1184,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1184 # 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 https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1242 # 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 https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L1435 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 https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L2098 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 Index: lib/drb/ssl.rb =================================================================== --- lib/drb/ssl.rb (revision 61154) +++ lib/drb/ssl.rb (revision 61155) @@ -336,7 +336,7 @@ module DRb https://github.com/ruby/ruby/blob/trunk/lib/drb/ssl.rb#L336 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 Index: lib/timeout.rb =================================================================== --- lib/timeout.rb (revision 61154) +++ lib/timeout.rb (revision 61155) @@ -118,7 +118,7 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L118 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 Index: lib/cmath.rb =================================================================== --- lib/cmath.rb (revision 61154) +++ lib/cmath.rb (revision 61155) @@ -50,7 +50,7 @@ module CMath https://github.com/ruby/ruby/blob/trunk/lib/cmath.rb#L50 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 Index: lib/ostruct.rb =================================================================== --- lib/ostruct.rb (revision 61154) +++ lib/ostruct.rb (revision 61155) @@ -162,7 +162,7 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L162 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 https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L181 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 Index: lib/webrick/server.rb =================================================================== --- lib/webrick/server.rb (revision 61154) +++ lib/webrick/server.rb (revision 61155) @@ -103,7 +103,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/server.rb#L103 @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 Index: lib/webrick/utils.rb =================================================================== --- lib/webrick/utils.rb (revision 61154) +++ lib/webrick/utils.rb (revision 61155) @@ -37,7 +37,7 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/utils.rb#L37 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 Index: lib/rexml/node.rb =================================================================== --- lib/rexml/node.rb (revision 61154) +++ lib/rexml/node.rb (revision 61155) @@ -26,7 +26,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/node.rb#L26 # 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 Index: lib/rexml/instruction.rb =================================================================== --- lib/rexml/instruction.rb (revision 61154) +++ lib/rexml/instruction.rb (revision 61155) @@ -43,7 +43,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/instruction.rb#L43 # 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 Index: lib/rexml/element.rb =================================================================== --- lib/rexml/element.rb (revision 61154) +++ lib/rexml/element.rb (revision 61155) @@ -710,7 +710,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/element.rb#L710 # 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" Index: lib/rexml/comment.rb =================================================================== --- lib/rexml/comment.rb (revision 61154) +++ lib/rexml/comment.rb (revision 61155) @@ -48,7 +48,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/comment.rb#L48 # 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 Index: lib/rexml/cdata.rb =================================================================== --- lib/rexml/cdata.rb (revision 61154) +++ lib/rexml/cdata.rb (revision 61155) @@ -58,7 +58,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/cdata.rb#L58 # c = CData.new( " Some text " ) # c.write( $stdout ) #-> <![CDATA[ Some text ]]> 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 Index: lib/rexml/text.rb =================================================================== --- lib/rexml/text.rb (revision 61154) +++ lib/rexml/text.rb (revision 61155) @@ -293,7 +293,7 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/text.rb#L293 # 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 Index: lib/ipaddr.rb =================================================================== --- lib/ipaddr.rb (revision 61154) +++ lib/ipaddr.rb (revision 61155) @@ -310,7 +310,7 @@ class IPAddr https://github.com/ruby/ruby/blob/trunk/lib/ipaddr.rb#L310 # 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 https://github.com/ruby/ruby/blob/trunk/lib/ipaddr.rb#L336 # 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 Index: lib/rss/rss.rb =================================================================== --- lib/rss/rss.rb (revision 61154) +++ lib/rss/rss.rb (revision 61155) @@ (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/