Archive of RubyForge sup-devel mailing list
 help / color / mirror / Atom feed
* [sup-devel] [PATCH] Fix monkey-patching of Tempfile
@ 2010-10-08 14:03 Michael Hamann
  2010-10-14  1:22 ` Rich Lane
  0 siblings, 1 reply; 2+ messages in thread
From: Michael Hamann @ 2010-10-08 14:03 UTC (permalink / raw)
  To: sup-devel

In Ruby 1.9.2 the interal function make_tmpname has changed it's
parameters and the second parameter can be nil now. This breaks the
monkey-patched Tempfile class in sup. Additionally, monkey-patching is
not really necessary as the Tempfile class supports an array as basename
instead of a string containing prefix and a suffix since Ruby version
1.8.7. The only place I've found where the suffix might matter is
directly in message-chunks.rb where I've changed the basename to an
array. Additionally the new make_tmpname function from Ruby 1.9.2 is
monkey-patched for Ruby versions lower than 1.8.7 now.

NOTE: As I don't have a working Ruby 1.8 setup for sup I haven't tested
this with older versions, but the code works here when the version check
is changed to 1.9.3 and I can't see anything that shouldn't work with
Ruby 1.8.6 and below.
---
 lib/sup/message-chunks.rb |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/lib/sup/message-chunks.rb b/lib/sup/message-chunks.rb
index c275f41..ba1834a 100644
--- a/lib/sup/message-chunks.rb
+++ b/lib/sup/message-chunks.rb
@@ -32,9 +32,25 @@ require 'tempfile'
 ## attachments are quotable; Signatures are not.
 
 ## monkey-patch time: make temp files have the right extension
-class Tempfile
-  def make_tmpname basename, n
-    sprintf '%d-%d-%s', $$, n, basename
+## Backport from Ruby 1.9.2 for versions lower than 1.8.7
+if RUBY_VERSION < '1.8.7'
+  class Tempfile
+    def make_tmpname(prefix_suffix, n)
+      case prefix_suffix
+      when String
+        prefix = prefix_suffix
+        suffix = ""
+      when Array
+        prefix = prefix_suffix[0]
+        suffix = prefix_suffix[1]
+      else
+        raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
+      end
+      t = Time.now.strftime("%Y%m%d")
+      path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
+      path << "-#{n}" if n
+      path << suffix
+    end
   end
 end
 
@@ -149,7 +165,7 @@ EOS
     end
 
     def write_to_disk
-      file = Tempfile.new(@filename || "sup-attachment")
+      file = Tempfile.new(["sup", @filename || "sup-attachment"])
       file.print @raw_content
       file.close
       file.path
-- 
1.7.3.1

_______________________________________________
Sup-devel mailing list
Sup-devel@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-devel


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [sup-devel] [PATCH] Fix monkey-patching of Tempfile
  2010-10-08 14:03 [sup-devel] [PATCH] Fix monkey-patching of Tempfile Michael Hamann
@ 2010-10-14  1:22 ` Rich Lane
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Lane @ 2010-10-14  1:22 UTC (permalink / raw)
  To: Michael Hamann; +Cc: sup-devel

Applied to master.
_______________________________________________
Sup-devel mailing list
Sup-devel@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-devel


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-10-14  1:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-08 14:03 [sup-devel] [PATCH] Fix monkey-patching of Tempfile Michael Hamann
2010-10-14  1:22 ` Rich Lane

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox