You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by kc...@apache.org on 2008/06/24 02:33:00 UTC

svn commit: r670976 - in /incubator/thrift/trunk/lib/rb: lib/thrift/deprecation.rb spec/deprecation_spec.rb

Author: kclark
Date: Mon Jun 23 17:33:00 2008
New Revision: 670976

URL: http://svn.apache.org/viewvc?rev=670976&view=rev
Log:
rb: Bugfix for deprecation code [kevin@rapleaf.com][THRIFT-50]

Modified:
    incubator/thrift/trunk/lib/rb/lib/thrift/deprecation.rb
    incubator/thrift/trunk/lib/rb/spec/deprecation_spec.rb

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/deprecation.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/deprecation.rb?rev=670976&r1=670975&r2=670976&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/deprecation.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/deprecation.rb Mon Jun 23 17:33:00 2008
@@ -35,17 +35,19 @@
   CLASS_MAPPING = {}
   MODULE_MAPPING = {}
   def self.new_class(obj, name)
+    klass_id = CLASS_MAPPING.size
+    CLASS_MAPPING[klass_id] = [obj, name, true]
     klass = Class.new(obj) do
       klass = self
       instance_methods.sort.reject { |x| [:__id__,:__send__].include? x.to_sym }.each do |sym|
         undef_method sym
       end
-      define_method :__thrift_deprecation_proxy_klass do
-        klass
+      define_method :__thrift_deprecation_proxy_klass_id do
+        klass_id
       end
       def method_missing(sym, *args, &block)
-        klass = __thrift_deprecation_proxy_klass
-        obj, name, warned = CLASS_MAPPING[klass.__id__]
+        klass_id = __thrift_deprecation_proxy_klass_id
+        obj, name, warned = CLASS_MAPPING[klass_id]
         obj.instance_method(sym).bind(self).call(*args, &block)
       end
       (class << self;self;end).class_eval do
@@ -53,15 +55,15 @@
           undef_method sym
         end
         define_method :__thrift_deprecation_proxy_klass do
-          klass
+          [klass, klass_id]
         end
         def method_missing(sym, *args, &block)
-          klass = __thrift_deprecation_proxy_klass
-          obj, name, warned = CLASS_MAPPING[klass.__id__]
+          klass, klass_id = __thrift_deprecation_proxy_klass
+          obj, name, warned = CLASS_MAPPING[klass_id]
           unless warned
             STDERR.puts "Warning: class #{name} is deprecated"
             STDERR.puts "  from #{caller.first}"
-            CLASS_MAPPING[__thrift_deprecation_proxy_klass.__id__][2] = true
+            CLASS_MAPPING[klass_id][2] = true
           end
           if klass.__id__ == self.__id__
             obj.send sym, *args, &block
@@ -71,25 +73,27 @@
         end
       end
     end
-    CLASS_MAPPING[klass.__id__] = [obj, name, false]
+    CLASS_MAPPING[klass_id][2] = false
     klass
   end
   def self.new_module(obj, name)
+    mod_id = MODULE_MAPPING.size
+    MODULE_MAPPING[mod_id] = [obj, name, true]
     mod = Module.new do
       include obj
       instance_methods.sort.reject { |x| [:__id__,:__send__].include? x.to_sym }.each do |sym|
         undef_method sym
       end
-      define_method :__thrift_deprecation_proxy_module do
-        mod
+      define_method :__thrift_deprecation_proxy_module_id do
+        mod_id
       end
       def method_missing(sym, *args, &block)
-        mod = __thrift_deprecation_proxy_module
-        obj, name, warned = MODULE_MAPPING[mod.__id__]
+        mod_id = __thrift_deprecation_proxy_module_id
+        obj, name, warned = MODULE_MAPPING[mod_id]
         unless warned
           STDERR.puts "Warning: module #{name} is deprecated"
           STDERR.puts "  from #{caller.first}"
-          MODULE_MAPPING[mod.__id__][2] = true
+          MODULE_MAPPING[mod_id][2] = true
         end
         obj.instance_method(sym).bind(self).call(*args, &block)
       end
@@ -97,22 +101,22 @@
         instance_methods.sort.reject { |x| [:__id__,:__send__].include? x.to_sym }.each do |sym|
           undef_method sym
         end
-        define_method :__thrift_deprecation_proxy_module do
-          mod
+        define_method :__thrift_deprecation_proxy_module_id do
+          mod_id
         end
         def method_missing(sym, *args, &block)
-          mod = __thrift_deprecation_proxy_module
-          obj, name, warned = MODULE_MAPPING[mod.__id__]
+          mod_id = __thrift_deprecation_proxy_module_id
+          obj, name, warned = MODULE_MAPPING[mod_id]
           unless warned
             STDERR.puts "Warning: module #{name} is deprecated"
             STDERR.puts "  from #{caller.first}"
-            MODULE_MAPPING[mod.__id__][2] = true
+            MODULE_MAPPING[mod_id][2] = true
           end
           obj.send sym, *args, &block
         end
       end
     end
-    MODULE_MAPPING[mod.__id__] = [obj, name, false]
+    MODULE_MAPPING[mod_id][2] = false
     mod
   end
 end

Modified: incubator/thrift/trunk/lib/rb/spec/deprecation_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/deprecation_spec.rb?rev=670976&r1=670975&r2=670976&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/deprecation_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/deprecation_spec.rb Mon Jun 23 17:33:00 2008
@@ -269,6 +269,31 @@
       end
     end
   end
+
+  it "should work when Object.inherited calls a method on self" do
+    ensure_const_removed :DeprecationSpecOldClass do
+      old_inherited = Object.method(:inherited)
+      begin
+        (class << Object;self;end).class_eval do
+          define_method :inherited do |cls|
+            cls.inspect
+            old_inherited.call(cls)
+          end
+        end
+        klass = Class.new do
+          def foo
+            "foo"
+          end
+        end
+        STDERR.should_receive(:puts).exactly(0).times
+        lambda { deprecate_class! :DeprecationSpecOldClass => klass }.should_not raise_error
+      ensure
+        (class << Object;self;end).class_eval do
+          define_method :inherited, old_inherited
+        end
+      end
+    end
+  end
 end
 
 describe "deprecate_module!" do