Project

General

Profile

Bug #13835

Using 'open-uri' with 'tempfile' causes an exception

Added by thorsteneckel (Thorsten Eckel) about 2 years ago. Updated 28 days ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:82442]

Description

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>'

I created the pull request https://github.com/ruby/ruby/pull/1680 as a proposal. It's based on the previous pull request https://github.com/ruby/ruby/pull/1675 and feedback of nobu (Nobuyoshi Nakada) .

Greetings.

History

Updated by shevegen (Robert A. Heiler) about 2 years ago

Indeed, sounds like a bug; at the least it is somewhat surprising behaviour to me. Not
that I think I would have ever found the above behaviour, I always used open-uri with
a remote URL so far. :)

Updated by kigster (Konstantin Gredeskoul) about 2 years ago

shevegen (Robert A. Heiler) wrote:

Indeed, sounds like a bug; at the least it is somewhat surprising behaviour to me. Not
that I think I would have ever found the above behaviour, I always used open-uri with
a remote URL so far. :)

Hm, as I remember, the first argument to open should be a URl or a path, not a Tempfile instance, right?

In other words, changing the above example to open(temp_file.path, 'a') fixes the error for me.

Updated by kernigh (George Koehler) almost 2 years ago

Turns out that open(temp_file, 'a') works with the original open, but fails after loading open-uri.

This is because the instances of Tempfile respond to to_path, just like instances of File or Pathname. So open(temp_file, 'a') acts like open(temp_file.to_path, 'a'). I was confused because the documentation for Kernel.open (and other methods like File.path) has no mention of to_path. I needed to read Ruby's source code to learn about it.

The bug happens after loading open-uri. Then open(temp_file, 'a') tries to call temp_file.open('a'), but Tempfile#open fails to do what open-uri expects, and instead raises the ArgumentError.

Updated by jeremyevans0 (Jeremy Evans) 28 days ago

  • Status changed from Open to Closed

This issue no longer occurs in the master branch, probably due to 05aac90a1bcfeb180f5e78ea8b00a4d1b04d5eed. That commit changed the behavior so that open is not called on the first argument to Kernel#open if the object responds to to_path. This was done to avoid an warning when using Kernel#open with Pathname instances, but it looks like it fixed this issue as well.

Also available in: Atom PDF