Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug: Using 'open-uri' with 'tempfile' causes an exception. #1675

Closed
wants to merge 4 commits into from

Conversation

thorsteneckel
Copy link

Hi there,

try this in your current ruby env:

require 'tempfile'
require 'open-uri'

temp_file = Tempfile.new
open(temp_file, 'a')

Get this:

/Users/~/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/tempfile.rb:142:in `open': wrong number of arguments (given 1, expected 0) (ArgumentError)
  from /Users/~/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/open-uri.rb:31:in `open'
  from debug.rb:5:in `<main>'

Greetings.

@@ -120,6 +120,7 @@ def test_tempfile_rest_parameter
assert_nothing_raised {
temp_file = Tempfile.new
open(temp_file, 'a')
temp_file.close
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use ensure.

temp_file = Tempfile.new
assert_nothing_raised { open(temp_file, 'a') }
ensure
temp_file.close
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use close! instead of close.

zammad-sync pushed a commit to zammad/zammad that referenced this pull request Aug 16, 2017
…pull request #1675. Using 'open' with a temp file causes an exception.

This is caused by the newly introduced Exchange integration dependencies 'viewpoint' and 'autodiscover' which depend on 'httpclient' which requires 'open-uri' somewhere. These cause the integration tests to fail - namely UserAgentTest#test_check_request.

Link: ruby/ruby#1675
@nobu
Copy link
Member

nobu commented Aug 18, 2017

As we don't accept patches on maintenance branches directly, please rebase on the trunk, and file an issue at https://bugs.ruby-lang.org/projects/ruby-trunk/issues/new.

And, I'm uncertain if it's better to check by the arity of open method.
Another idea is to check by to_io before open.

diff --git i/lib/open-uri.rb w/lib/open-uri.rb
index ce608a9296..b445b7bca5 100644
--- i/lib/open-uri.rb
+++ w/lib/open-uri.rb
@@ -27,7 +27,18 @@
   # We can accept URIs and strings that begin with http://, https:// and
   # ftp://. In these cases, the opened file object is extended by OpenURI::Meta.
   def open(name, *rest, &block) # :doc:
-    if name.respond_to?(:open) && !name.method(:open).parameters.empty?
+    if name.respond_to?(:to_io)
+      io = name.to_io.dup
+      if block
+        begin
+          yield io
+        ensure
+          io.close
+        end
+      else
+        io
+      end
+    elsif name.respond_to?(:open)
       name.open(*rest, &block)
     elsif name.respond_to?(:to_str) &&
           %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&

@thorsteneckel
Copy link
Author

Closing. Applied changed and feedback in #1680 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants