Archive of RubyForge sup-devel mailing list
 help / color / mirror / Atom feed
From: Michael Hamann <michael@content-space.de>
To: sup-devel@rubyforge.org
Subject: [sup-devel] [PATCH] Fix monkey-patching of Tempfile
Date: Fri,  8 Oct 2010 16:03:36 +0200	[thread overview]
Message-ID: <1286546616-7072-1-git-send-email-michael@content-space.de> (raw)

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


             reply	other threads:[~2010-10-08 14:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-08 14:03 Michael Hamann [this message]
2010-10-14  1:22 ` Rich Lane

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1286546616-7072-1-git-send-email-michael@content-space.de \
    --to=michael@content-space.de \
    --cc=sup-devel@rubyforge.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox