[ruby-core:111907] [Ruby master Bug#19354] Issues with arguments validation in IO.read

Issue #19354 has been reported by andrykonchin (Andrew Konchin). ---------------------------------------- Bug #19354: Issues with arguments validation in IO.read https://bugs.ruby-lang.org/issues/19354 * Author: andrykonchin (Andrew Konchin) * Status: Open * Priority: Normal * ruby -v: 3.2.1, 3.2.1 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I've noticed a strange error message when `IO.read` is called with additional positional argument: ```ruby IO.read("a.txt", 3, 0, {mode: "r+"}) # (irb):2:in `read': wrong number of arguments (given 3, expected 0..2) (ArgumentError) ``` But I would expect receiving `given 4, expected 1..3` as far as the first argument (file name) is mandatory and all the other arguments - are optional. I've encountered another related issue - looks like existing of a file with specified name is checked **before** number of arguments. So when passed additional argument and specified a name of not existing file - I receive error about wrong file name, but would expect more basic and essential error about wrong number of arguments: ```ruby IO.read("b.txt", 3, 0, {mode: "r+"}) # (irb):3:in `read': No such file or directory @ rb_sysopen - b.txt (Errno::ENOENT) ``` -- https://bugs.ruby-lang.org/

Issue #19354 has been updated by nobu (Nobuyoshi Nakada). Status changed from Open to Feedback `IO.read` opens the file given as the first argument, then delegates the other arguments to `IO#read` on the instance. Your error happens in the delegated `IO#read`. Since 3.0, keyword arguments are separated from mere hashes. I think you may want to write: ```ruby IO.read("a.txt", 3, 0, mode: "r+") ``` ---------------------------------------- Bug #19354: Issues with arguments validation in IO.read https://bugs.ruby-lang.org/issues/19354#change-101348 * Author: andrykonchin (Andrew Konchin) * Status: Feedback * Priority: Normal * ruby -v: 3.2.0, 3.1.3 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I've noticed a strange error message when `IO.read` is called with additional positional argument: ```ruby IO.read("a.txt", 3, 0, {mode: "r+"}) # (irb):2:in `read': wrong number of arguments (given 3, expected 0..2) (ArgumentError) ``` But I would expect receiving `given 4, expected 1..3` as far as the first argument (file name) is mandatory and all the other arguments - are optional. I've encountered another related issue - looks like existing of a file with specified name is checked **before** number of arguments. So when passed additional argument and specified a name of not existing file - I receive error about wrong file name, but would expect more basic and essential error about wrong number of arguments: ```ruby IO.read("b.txt", 3, 0, {mode: "r+"}) # (irb):3:in `read': No such file or directory @ rb_sysopen - b.txt (Errno::ENOENT) ``` -- https://bugs.ruby-lang.org/

Issue #19354 has been updated by andrykonchin (Andrew Konchin). Yeah, I understand that it's caused by the way how `IO.read` is implemented. My point is that from the end-user point of view current error message (`given 3, expected 0..2`) is slightly misleading/confusing. ---------------------------------------- Bug #19354: Issues with arguments validation in IO.read https://bugs.ruby-lang.org/issues/19354#change-101360 * Author: andrykonchin (Andrew Konchin) * Status: Feedback * Priority: Normal * ruby -v: 3.2.0, 3.1.3 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I've noticed a strange error message when `IO.read` is called with additional positional argument: ```ruby IO.read("a.txt", 3, 0, {mode: "r+"}) # (irb):2:in `read': wrong number of arguments (given 3, expected 0..2) (ArgumentError) ``` But I would expect receiving `given 4, expected 1..3` as far as the first argument (file name) is mandatory and all the other arguments - are optional. I've encountered another related issue - looks like existing of a file with specified name is checked **before** number of arguments. So when passed additional argument and specified a name of not existing file - I receive error about wrong file name, but would expect more basic and essential error about wrong number of arguments: ```ruby IO.read("b.txt", 3, 0, {mode: "r+"}) # (irb):3:in `read': No such file or directory @ rb_sysopen - b.txt (Errno::ENOENT) ``` -- https://bugs.ruby-lang.org/

Issue #19354 has been updated by Eregon (Benoit Daloze). I think the current error could be acceptable if it's clear IO#read is in the backtrace. But it's not part of the backtrace currently (and on top it's not possible to differentiate IO.read and IO#read from the backtrace): ``` $ ruby -e 'IO.read("a.txt", 3, 0, {mode: "r+"})' -e:1:in `read': wrong number of arguments (given 3, expected 0..2) (ArgumentError) from -e:1:in `<main>' ``` And so this is rather inconsistent (in expected args) with: ``` $ ruby -e 'IO.read("a.txt", 3, 0, {mode: "r+"}, 5, 6)' -e:1:in `read': wrong number of arguments (given 6, expected 1..4) (ArgumentError) from -e:1:in `<main>' ``` The second error in the description seems clearly wrong behavior. So I guess we should check args more strictly in IO.read itself, before calling IO#read. ---------------------------------------- Bug #19354: Issues with arguments validation in IO.read https://bugs.ruby-lang.org/issues/19354#change-101439 * Author: andrykonchin (Andrew Konchin) * Status: Feedback * Priority: Normal * ruby -v: 3.2.0, 3.1.3 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I've noticed a strange error message when `IO.read` is called with additional positional argument: ```ruby IO.read("a.txt", 3, 0, {mode: "r+"}) # (irb):2:in `read': wrong number of arguments (given 3, expected 0..2) (ArgumentError) ``` But I would expect receiving `given 4, expected 1..3` as far as the first argument (file name) is mandatory and all the other arguments - are optional. I've encountered another related issue - looks like existing of a file with specified name is checked **before** number of arguments. So when passed additional argument and specified a name of not existing file - I receive error about wrong file name, but would expect more basic and essential error about wrong number of arguments: ```ruby IO.read("b.txt", 3, 0, {mode: "r+"}) # (irb):3:in `read': No such file or directory @ rb_sysopen - b.txt (Errno::ENOENT) ``` -- https://bugs.ruby-lang.org/
participants (3)
-
andrykonchin (Andrew Konchin)
-
Eregon (Benoit Daloze)
-
nobu (Nobuyoshi Nakada)