From b0bcdbd2a68e313f4f178cc41e7984a539c5bf27 Mon Sep 17 00:00:00 2001 From: matz Date: Thu, 29 Dec 2005 12:05:16 +0000 Subject: * 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 . [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 . [ruby-talk:165285] [ruby-core:6995] * ext/digest/sha2/sha2.c (ULL): support AIX C. a patch from Kailden . [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 . [ruby-core:06076] * string.c: remove global functions work on $_. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@9757 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/cgi.rb | 71 ++++++++++++++++++----- lib/delegate.rb | 171 +++++++++++++++++++++++++++++--------------------------- lib/sync.rb | 2 +- lib/thread.rb | 2 +- lib/yaml.rb | 2 +- 5 files changed, 148 insertions(+), 100 deletions(-) (limited to 'lib') diff --git a/lib/cgi.rb b/lib/cgi.rb index 43122212d..c1627cb52 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 b54b57d7b..3be335580 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 true + p foo2.iter{[55,true]} # => true foo2.error # raise error! end diff --git a/lib/sync.rb b/lib/sync.rb index 79522ed88..566cfbfb2 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 42611b7fc..5d4fedc73 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 3130ad115..fe8335c8f 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 -- cgit