From f2e4d107af58ad806556cc289befa3b2cf694d3c Mon Sep 17 00:00:00 2001 From: Kevin Clark Date: Thu, 26 Jun 2008 18:45:19 +0000 Subject: [PATCH] rb: The deprecation stuff should skip thrift library code when showing caller [THRIFT-56] Author: Kevin Ballard git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@671984 13f79535-47bb-0310-9956-ffa450edef68 --- lib/rb/lib/thrift/deprecation.rb | 10 +++++++--- lib/rb/spec/deprecation_spec.rb | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/rb/lib/thrift/deprecation.rb b/lib/rb/lib/thrift/deprecation.rb index 3dd1a57d..6a422a8b 100644 --- a/lib/rb/lib/thrift/deprecation.rb +++ b/lib/rb/lib/thrift/deprecation.rb @@ -62,7 +62,7 @@ module Thrift::DeprecationProxy # :nodoc: obj, name, warned = CLASS_MAPPING[klass_id] unless warned STDERR.puts "Warning: class #{name} is deprecated" - STDERR.puts " from #{caller.first}" + STDERR.puts " from #{Thrift::DeprecationProxy.process_caller(caller)}" CLASS_MAPPING[klass_id][2] = true end if klass.__id__ == self.__id__ @@ -92,7 +92,7 @@ module Thrift::DeprecationProxy # :nodoc: obj, name, warned = MODULE_MAPPING[mod_id] unless warned STDERR.puts "Warning: module #{name} is deprecated" - STDERR.puts " from #{caller.first}" + STDERR.puts " from #{Thrift::DeprecationProxy.process_caller(caller)}" MODULE_MAPPING[mod_id][2] = true end obj.instance_method(sym).bind(self).call(*args, &block) @@ -109,7 +109,7 @@ module Thrift::DeprecationProxy # :nodoc: obj, name, warned = MODULE_MAPPING[mod_id] unless warned STDERR.puts "Warning: module #{name} is deprecated" - STDERR.puts " from #{caller.first}" + STDERR.puts " from #{Thrift::DeprecationProxy.process_caller(caller)}" MODULE_MAPPING[mod_id][2] = true end obj.send sym, *args, &block @@ -119,6 +119,10 @@ module Thrift::DeprecationProxy # :nodoc: MODULE_MAPPING[mod_id][2] = false mod end + def self.process_caller(stack) + dir = File.dirname(__FILE__) + stack.find { |frame| frame[0,dir.size] != dir } + end end module Kernel diff --git a/lib/rb/spec/deprecation_spec.rb b/lib/rb/spec/deprecation_spec.rb index b0cc527b..4ca4ee30 100644 --- a/lib/rb/spec/deprecation_spec.rb +++ b/lib/rb/spec/deprecation_spec.rb @@ -311,12 +311,14 @@ describe "deprecate_module!" do def stub_stderr(mod, offset=1, called=nil) STDERR.should_receive(:puts).with("Warning: module #{mod} is deprecated") + source = Regexp.escape(__FILE__) + ":" if offset line = (called || caller.first)[/\d+$/].to_i + offset - STDERR.should_receive(:puts).with(" from #{__FILE__}:#{line}") + source += Regexp.escape(line.to_s) else - STDERR.should_receive(:puts).with(/^ from #{Regexp.escape(__FILE__)}:/) + source += "\d+" end + STDERR.should_receive(:puts).with(/^ from #{source}(?::in `[^']+')?$/) end it "should create a new global constant that points to the old one" do @@ -423,4 +425,15 @@ describe "deprecate_module!" do end end end + + it "should skip thrift library code when printing caller" do + klass = Class.new do + include ThriftStruct + FIELDS = { + 1 => {:name => "foo", :type => Thrift::Types::STRING} + } + end + stub_stderr('ThriftStruct') + klass.new(:foo => "foo") + end end -- 2.17.1