diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-12-29 12:05:16 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-12-29 12:05:16 +0000 |
commit | de3bff164c3b8405f40d2e89cf726f7e865102d1 (patch) | |
tree | 8f24b7069ab459f2829b3c986f9759111c5181e9 /lib | |
parent | e5226ea394da345a1abf7719d4e0482de89df26e (diff) | |
download | ruby-de3bff164c3b8405f40d2e89cf726f7e865102d1.tar.gz |
* eval.c (rb_mod_define_method): should save safe_level in the
proc object. [ruby-dev:28146]
* test/drb/drbtest.rb (DRbService::self.ext_service): increase
timeout limit. a patch from Kazuhiro NISHIYAMA
<zn at mbf.nifty.com>. [ruby-dev:28132]
* eval.c (ev_const_get): fixed a bug in constant reference during
instance_eval. [yarv-dev:707]
* eval.c (ev_const_defined): ditto.
* lib/yaml.rb (YAML::add_domain_type): typo fixed. a patch from
Joel VanderWerf <vjoel at path.berkeley.edu>.
[ruby-talk:165285] [ruby-core:6995]
* ext/digest/sha2/sha2.c (ULL): support AIX C. a patch from
Kailden <kailden at gmail.com>. [ruby-core:06984]
* ext/syck/rubyext.c (rb_syck_compile): avoid potential memory
leak.
* ext/syck/rubyext.c (syck_set_ivars): avoid potential memory
leak by explicit symbol allocation.
* lib/delegate.rb (Delegator::method_missing): should delegate
block as well.
* lib/cgi.rb (CGI::QueryExtension::MorphingBody): fix criteria to
use Tempfile. A fix from Zev Blut <rubyzbibd at ubit.com>.
[ruby-core:06076]
* string.c: remove global functions work on $_.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9757 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cgi.rb | 71 | ||||
-rw-r--r-- | lib/delegate.rb | 171 | ||||
-rw-r--r-- | lib/sync.rb | 2 | ||||
-rw-r--r-- | lib/thread.rb | 2 | ||||
-rw-r--r-- | lib/yaml.rb | 2 |
5 files changed, 148 insertions, 100 deletions
diff --git a/lib/cgi.rb b/lib/cgi.rb index 43122212d5..c1627cb521 100644 --- a/lib/cgi.rb +++ b/lib/cgi.rb @@ -993,22 +993,9 @@ class CGI loop do head = nil - if 10240 < content_length - require "tempfile" - body = Tempfile.new("CGI") - else - begin - require "stringio" - body = StringIO.new - rescue LoadError - require "tempfile" - body = Tempfile.new("CGI") - end - end - body.binmode if defined? body.binmode + body = MorphingBody.new until head and /#{boundary}(?:#{EOL}|--)/n.match(buf) - if (not head) and /#{EOL}#{EOL}/n.match(buf) buf = buf.sub(/\A((?:.|\n)*?#{EOL})#{EOL}/n) do head = $1.dup @@ -1042,6 +1029,7 @@ class CGI "" end + p body body.rewind /Content-Disposition:.* filename="?([^\";]*)"?/ni.match(head) @@ -1062,7 +1050,7 @@ class CGI end /Content-Disposition:.* name="?([^\";]*)"?/ni.match(head) - name = $1.dup + name = ($1 || "").dup if params.has_key?(name) params[name].push(body) @@ -1102,6 +1090,59 @@ class CGI end private :read_from_cmdline + # A wrapper class to use a StringIO object as the body and switch + # to a TempFile when the passed threshold is passed. + class MorphingBody + begin + require "stringio" + @@small_buffer = lambda{StringIO.new} + rescue LoadError + require "tempfile" + @@small_buffer = lambda{ + n = Tempfile.new("CGI") + n.binmode + n + } + end + + def initialize(morph_threshold = 10240) + @threshold = morph_threshold + @body = @@small_buffer.call + @cur_size = 0 + @morph_check = true + end + + def print(data) + if @morph_check && (@cur_size + data.size > @threshold) + convert_body + end + @body.print data + end + def rewind + @body.rewind + end + def path + @body.path + end + + # returns the true body object. + def extract + @body + end + + private + def convert_body + new_body = TempFile.new("CGI") + new_body.binmode if defined? @body.binmode + new_body.binmode if defined? new_body.binmode + + @body.rewind + new_body.print @body.read + @body = new_body + @morph_check = false + end + end + # Initialize the data from the query. # # Handles multipart forms (in particular, forms that involve file uploads). diff --git a/lib/delegate.rb b/lib/delegate.rb index b54b57d7b7..3be335580b 100644 --- a/lib/delegate.rb +++ b/lib/delegate.rb @@ -121,61 +121,93 @@ class Delegator undef_method m end - # - # Pass in the _obj_ to delegate method calls to. All methods supported by - # _obj_ will be delegated to. - # - def initialize(obj) - __setobj__(obj) - end + module MethodDelegation + # + # Pass in the _obj_ to delegate method calls to. All methods supported by + # _obj_ will be delegated to. + # + def initialize(obj) + __setobj__(obj) + end - # Handles the magic of delegation through \_\_getobj\_\_. - def method_missing(m, *args) - begin - target = self.__getobj__ - unless target.respond_to?(m) - super(m, *args) + # Handles the magic of delegation through \_\_getobj\_\_. + def method_missing(m, *args, &block) + begin + target = self.__getobj__ + unless target.respond_to?(m) + super(m, *args, &block) + else + target.__send__(m, *args, &block) + end + rescue Exception + $@.delete_if{|s| /^#{__FILE__}:\d+:in `method_missing'$/ =~ s} #` + ::Kernel::raise end - target.__send__(m, *args) - rescue Exception - $@.delete_if{|s| /^#{__FILE__}:\d+:in `method_missing'$/ =~ s} #` - ::Kernel::raise end - end - # - # Checks for a method provided by this the delegate object by fowarding the - # call through \_\_getobj\_\_. - # - def respond_to?(m) - return true if super - return self.__getobj__.respond_to?(m) - end + # + # Checks for a method provided by this the delegate object by fowarding the + # call through \_\_getobj\_\_. + # + def respond_to?(m) + return true if super + return self.__getobj__.respond_to?(m) + end - # - # This method must be overridden by subclasses and should return the object - # method calls are being delegated to. - # - def __getobj__ - raise NotImplementedError, "need to define `__getobj__'" - end + # + # Returns true if two objects are considered same. + # + def ==(obj) + return true if obj.equal?(self) + self.__getobj__ == obj + end - # - # This method must be overridden by subclasses and change the object delegate - # to _obj_. - # - def __setobj__(obj) - raise NotImplementedError, "need to define `__setobj__'" - end + # + # Returns true only if two objects are identical. + # + def equal?(obj) + self.object_id == obj.object_id + end - # Serialization support for the object returned by \_\_getobj\_\_. - def marshal_dump - __getobj__ - end - # Reinitializes delegation from a serialized object. - def marshal_load(obj) - __setobj__(obj) + # + # This method must be overridden by subclasses and should return the object + # method calls are being delegated to. + # + def __getobj__ + raise NotImplementedError, "need to define `__getobj__'" + end + + # + # This method must be overridden by subclasses and change the object delegate + # to _obj_. + # + def __setobj__(obj) + raise NotImplementedError, "need to define `__setobj__'" + end + + # Serialization support for the object returned by \_\_getobj\_\_. + def marshal_dump + __getobj__ + end + # Reinitializes delegation from a serialized object. + def marshal_load(obj) + __setobj__(obj) + end + + # Clone support for the object returned by \_\_getobj\_\_. + def clone + new = super + new.__setobj__(__getobj__.clone) + new + end + # Duplication support for the object returned by \_\_getobj\_\_. + def dup + new = super + new.__setobj__(__getobj__.dup) + new + end end + include MethodDelegation end # @@ -208,19 +240,6 @@ class SimpleDelegator<Delegator raise ArgumentError, "cannot delegate to self" if self.equal?(obj) @_sd_obj = obj end - - # Clone support for the object returned by \_\_getobj\_\_. - def clone - copy = super - copy.__setobj__(__getobj__.clone) - copy - end - # Duplication support for the object returned by \_\_getobj\_\_. - def dup - copy = super - copy.__setobj__(__getobj__.dup) - copy - end end # :stopdoc: @@ -243,24 +262,12 @@ def DelegateClass(superclass) klass = Class.new methods = superclass.public_instance_methods(true) methods -= [ - "__id__", "object_id", "__send__", "respond_to?", + "__id__", "object_id", "__send__", "respond_to?", "==", "equal?", "initialize", "method_missing", "__getobj__", "__setobj__", "clone", "dup", "marshal_dump", "marshal_load", ] klass.module_eval { - def initialize(obj) # :nodoc: - @_dc_obj = obj - end - def method_missing(m, *args) # :nodoc: - unless @_dc_obj.respond_to?(m) - super(m, *args) - end - @_dc_obj.__send__(m, *args) - end - def respond_to?(m) # :nodoc: - return true if super - return @_dc_obj.respond_to?(m) - end + include Delegator::MethodDelegation def __getobj__ # :nodoc: @_dc_obj end @@ -268,14 +275,6 @@ def DelegateClass(superclass) raise ArgumentError, "cannot delegate to self" if self.equal?(obj) @_dc_obj = obj end - def clone # :nodoc: - super - __setobj__(__getobj__.clone) - end - def dup # :nodoc: - super - __setobj__(__getobj__.dup) - end } for method in methods begin @@ -309,15 +308,23 @@ if __FILE__ == $0 p ary.class ary.push 25 p ary + ary.push 42 + ary.each {|x| p x} foo = Object.new def foo.test 25 end + def foo.iter + yield self + end def foo.error raise 'this is OK' end foo2 = SimpleDelegator.new(foo) + p foo2 + foo2.instance_eval{print "foo\n"} p foo.test == foo2.test # => true + p foo2.iter{[55,true]} # => true foo2.error # raise error! end diff --git a/lib/sync.rb b/lib/sync.rb index 79522ed885..566cfbfb2d 100644 --- a/lib/sync.rb +++ b/lib/sync.rb @@ -40,7 +40,7 @@ # unless defined? Thread - fail "Thread not available for this ruby interpreter" + raise "Thread not available for this ruby interpreter" end module Sync_m diff --git a/lib/thread.rb b/lib/thread.rb index 42611b7fce..5d4fedc73c 100644 --- a/lib/thread.rb +++ b/lib/thread.rb @@ -9,7 +9,7 @@ # unless defined? Thread - fail "Thread not available for this ruby interpreter" + raise "Thread not available for this ruby interpreter" end unless defined? ThreadError diff --git a/lib/yaml.rb b/lib/yaml.rb index 3130ad115d..fe8335c8f0 100644 --- a/lib/yaml.rb +++ b/lib/yaml.rb @@ -310,7 +310,7 @@ module YAML # # Add a transfer method for a builtin type # - def YAML.add_ruby_type( type, &transfer_proc ) + def YAML.add_ruby_type( type_tag, &transfer_proc ) resolver.add_type( "tag:ruby.yaml.org,2002:#{ type_tag }", transfer_proc ) end |