ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:71354] [Ruby trunk - Bug #11661] [Open] sprintf causes a KeyError instead of using a default value for hash substitution
       [not found] <redmine.issue-11661.20151105204817@ruby-lang.org>
@ 2015-11-05 20:48 ` eddyluten
  2015-11-08  7:58 ` [ruby-core:71386] [Ruby trunk - Feature #11661] [Feedback] " nobu
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: eddyluten @ 2015-11-05 20:48 UTC (permalink / raw)
  To: ruby-core

Issue #11661 has been reported by Eddy Luten.

----------------------------------------
Bug #11661: sprintf causes a KeyError instead of using a default value for hash substitution
https://bugs.ruby-lang.org/issues/11661

* Author: Eddy Luten
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* ruby -v: 2.3.0-dev
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
When using a format string that substitutes hash values (or using the `%` operator on a string), instead of using the Hash's default value if a key is not present, it causes a KeyError.

As an end-user, to get around this, my hash needs to know about all the possible keys ahead of time and pre-assign a value to them or handle the KeyError. Logically, I would assume that the `sprintf` implementation would use the default Hash value.

I wanted to open this issue to see what your collective thoughts were on this since I have a fork running locally that fixes this issue and was wondering if I could send a patch/PR for this.

This issue is reproducible using the following code:

```ruby
my_hash = Hash.new('world')
puts "hello %{location}" % my_hash # expecting "hello world"
# "KeyError: key{location} not found"
```



-- 
https://bugs.ruby-lang.org/

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

* [ruby-core:71386] [Ruby trunk - Feature #11661] [Feedback] sprintf causes a KeyError instead of using a default value for hash substitution
       [not found] <redmine.issue-11661.20151105204817@ruby-lang.org>
  2015-11-05 20:48 ` [ruby-core:71354] [Ruby trunk - Bug #11661] [Open] sprintf causes a KeyError instead of using a default value for hash substitution eddyluten
@ 2015-11-08  7:58 ` nobu
  2015-11-08  8:36 ` [ruby-core:71387] [Ruby trunk - Feature #11661] [Assigned] " nobu
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: nobu @ 2015-11-08  7:58 UTC (permalink / raw)
  To: ruby-core

Issue #11661 has been updated by Nobuyoshi Nakada.

Tracker changed from Bug to Feature
Status changed from Open to Feedback

[GH-1087](https://github.com/ruby/ruby/pull/1087) doesn't work properly with a default proc expecting an argument, but segfaults.

My patch.

~~~diff
diff --git i/sprintf.c w/sprintf.c
index 705d3ce..eee5695 100644
--- i/sprintf.c
+++ w/sprintf.c
@@ -605,13 +605,10 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
 		}
 		CHECKNAMEARG(start, len, enc);
 		get_hash(&hash, argc, argv);
-		sym = rb_check_symbol_cstr(start + 1,
-					   len - 2 /* without parenthesis */,
-					   enc);
-		if (sym != Qnil) nextvalue = rb_hash_lookup2(hash, sym, Qundef);
-		if (nextvalue == Qundef) {
-		    rb_enc_raise(enc, rb_eKeyError, "key%.*s not found", len, start);
-		}
+		sym = rb_cstr_intern(start + 1,
+				     len - 2 /* without parenthesis */,
+				     enc);
+		nextvalue = rb_hash_aref(hash, sym);
 		if (term == '}') goto format_s;
 		p++;
 		goto retry;
~~~

----------------------------------------
Feature #11661: sprintf causes a KeyError instead of using a default value for hash substitution
https://bugs.ruby-lang.org/issues/11661#change-54757

* Author: Eddy Luten
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
When using a format string that substitutes hash values (or using the `%` operator on a string), instead of using the Hash's default value if a key is not present, it causes a KeyError.

As an end-user, to get around this, my hash needs to know about all the possible keys ahead of time and pre-assign a value to them or handle the KeyError. Logically, I would assume that the `sprintf` implementation would use the default Hash value.

I wanted to open this issue to see what your collective thoughts were on this since I have a fork running locally that fixes this issue and was wondering if I could send a patch/PR for this.

This issue is reproducible using the following code:

```ruby
my_hash = Hash.new('world')
puts "hello %{location}" % my_hash # expecting "hello world"
# "KeyError: key{location} not found"
```



-- 
https://bugs.ruby-lang.org/

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

* [ruby-core:71387] [Ruby trunk - Feature #11661] [Assigned] sprintf causes a KeyError instead of using a default value for hash substitution
       [not found] <redmine.issue-11661.20151105204817@ruby-lang.org>
  2015-11-05 20:48 ` [ruby-core:71354] [Ruby trunk - Bug #11661] [Open] sprintf causes a KeyError instead of using a default value for hash substitution eddyluten
  2015-11-08  7:58 ` [ruby-core:71386] [Ruby trunk - Feature #11661] [Feedback] " nobu
@ 2015-11-08  8:36 ` nobu
  2015-11-10 11:37 ` [ruby-core:71430] [Ruby trunk - Bug #11661] " nobu
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: nobu @ 2015-11-08  8:36 UTC (permalink / raw)
  To: ruby-core

Issue #11661 has been updated by Nobuyoshi Nakada.

Status changed from Feedback to Assigned

----------------------------------------
Feature #11661: sprintf causes a KeyError instead of using a default value for hash substitution
https://bugs.ruby-lang.org/issues/11661#change-54758

* Author: Eddy Luten
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
When using a format string that substitutes hash values (or using the `%` operator on a string), instead of using the Hash's default value if a key is not present, it causes a KeyError.

As an end-user, to get around this, my hash needs to know about all the possible keys ahead of time and pre-assign a value to them or handle the KeyError. Logically, I would assume that the `sprintf` implementation would use the default Hash value.

I wanted to open this issue to see what your collective thoughts were on this since I have a fork running locally that fixes this issue and was wondering if I could send a patch/PR for this.

This issue is reproducible using the following code:

```ruby
my_hash = Hash.new('world')
puts "hello %{location}" % my_hash # expecting "hello world"
# "KeyError: key{location} not found"
```



-- 
https://bugs.ruby-lang.org/

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

* [ruby-core:71430] [Ruby trunk - Bug #11661] sprintf causes a KeyError instead of using a default value for hash substitution
       [not found] <redmine.issue-11661.20151105204817@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2015-11-08  8:36 ` [ruby-core:71387] [Ruby trunk - Feature #11661] [Assigned] " nobu
@ 2015-11-10 11:37 ` nobu
  2015-11-10 12:34 ` [ruby-core:71431] " hanmac
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: nobu @ 2015-11-10 11:37 UTC (permalink / raw)
  To: ruby-core

Issue #11661 has been updated by Nobuyoshi Nakada.

Tracker changed from Feature to Bug
Backport set to 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED

----------------------------------------
Bug #11661: sprintf causes a KeyError instead of using a default value for hash substitution
https://bugs.ruby-lang.org/issues/11661#change-54800

* Author: Eddy Luten
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* ruby -v: 
* Backport: 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED
----------------------------------------
When using a format string that substitutes hash values (or using the `%` operator on a string), instead of using the Hash's default value if a key is not present, it causes a KeyError.

As an end-user, to get around this, my hash needs to know about all the possible keys ahead of time and pre-assign a value to them or handle the KeyError. Logically, I would assume that the `sprintf` implementation would use the default Hash value.

I wanted to open this issue to see what your collective thoughts were on this since I have a fork running locally that fixes this issue and was wondering if I could send a patch/PR for this.

This issue is reproducible using the following code:

```ruby
my_hash = Hash.new('world')
puts "hello %{location}" % my_hash # expecting "hello world"
# "KeyError: key{location} not found"
```



-- 
https://bugs.ruby-lang.org/

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

* [ruby-core:71431] [Ruby trunk - Bug #11661] sprintf causes a KeyError instead of using a default value for hash substitution
       [not found] <redmine.issue-11661.20151105204817@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2015-11-10 11:37 ` [ruby-core:71430] [Ruby trunk - Bug #11661] " nobu
@ 2015-11-10 12:34 ` hanmac
  2015-11-10 19:57 ` [ruby-core:71434] " eddyluten
  2015-11-10 23:21 ` [ruby-core:71436] " nobu
  6 siblings, 0 replies; 7+ messages in thread
From: hanmac @ 2015-11-10 12:34 UTC (permalink / raw)
  To: ruby-core

Issue #11661 has been updated by Hans Mackowiak.


hm might it be possible that sprintf only uses default value/or default proc if the hash does has default,
but does still raise KeyError if it doesnt?

another idea would be that is does try to use hash[key] function if hash is a non-hash object.
(but that might reduce the calcing speed if not checked right)

----------------------------------------
Bug #11661: sprintf causes a KeyError instead of using a default value for hash substitution
https://bugs.ruby-lang.org/issues/11661#change-54801

* Author: Eddy Luten
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* ruby -v: 
* Backport: 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED
----------------------------------------
When using a format string that substitutes hash values (or using the `%` operator on a string), instead of using the Hash's default value if a key is not present, it causes a KeyError.

As an end-user, to get around this, my hash needs to know about all the possible keys ahead of time and pre-assign a value to them or handle the KeyError. Logically, I would assume that the `sprintf` implementation would use the default Hash value.

I wanted to open this issue to see what your collective thoughts were on this since I have a fork running locally that fixes this issue and was wondering if I could send a patch/PR for this.

This issue is reproducible using the following code:

```ruby
my_hash = Hash.new('world')
puts "hello %{location}" % my_hash # expecting "hello world"
# "KeyError: key{location} not found"
```



-- 
https://bugs.ruby-lang.org/

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

* [ruby-core:71434] [Ruby trunk - Bug #11661] sprintf causes a KeyError instead of using a default value for hash substitution
       [not found] <redmine.issue-11661.20151105204817@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2015-11-10 12:34 ` [ruby-core:71431] " hanmac
@ 2015-11-10 19:57 ` eddyluten
  2015-11-10 23:21 ` [ruby-core:71436] " nobu
  6 siblings, 0 replies; 7+ messages in thread
From: eddyluten @ 2015-11-10 19:57 UTC (permalink / raw)
  To: ruby-core

Issue #11661 has been updated by Eddy Luten.


Hans Mackowiak wrote:
> hm might it be possible that sprintf only uses default value/or default proc if the hash does has default,
> but does still raise KeyError if it doesnt?
> 
> another idea would be that is does try to use hash[key] function if hash is a non-hash object.
> (but that might reduce the calcing speed if not checked right)

Nobuyoshi Nakada: this was the change I initially made myself but couldn't get it to work properly with hash substitutions. Maybe I did something wrong in my testing, I will attempt to use your changes, thanks! :)

Hans Mackowiak: so raise KeyError is the default is `nil`? I think that could work, since if you wanted empty strings as a default, you could simply set an empty string as default value. However, logically, since `"#{nil}" (and `nil.to_s` for that case) works and returns an empty string, the modulus operator version of it should work the same. What do you think?

Also a general question, how would I go about making the Rubyspec tests pass for this particular pull request? Do I open another PR for the tests in Rubyspec? If so, how do I link these two PRs to work together in the Travis CI build acceptance test?

----------------------------------------
Bug #11661: sprintf causes a KeyError instead of using a default value for hash substitution
https://bugs.ruby-lang.org/issues/11661#change-54803

* Author: Eddy Luten
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* ruby -v: 
* Backport: 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED
----------------------------------------
When using a format string that substitutes hash values (or using the `%` operator on a string), instead of using the Hash's default value if a key is not present, it causes a KeyError.

As an end-user, to get around this, my hash needs to know about all the possible keys ahead of time and pre-assign a value to them or handle the KeyError. Logically, I would assume that the `sprintf` implementation would use the default Hash value.

I wanted to open this issue to see what your collective thoughts were on this since I have a fork running locally that fixes this issue and was wondering if I could send a patch/PR for this.

This issue is reproducible using the following code:

```ruby
my_hash = Hash.new('world')
puts "hello %{location}" % my_hash # expecting "hello world"
# "KeyError: key{location} not found"
```



-- 
https://bugs.ruby-lang.org/

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

* [ruby-core:71436] [Ruby trunk - Bug #11661] sprintf causes a KeyError instead of using a default value for hash substitution
       [not found] <redmine.issue-11661.20151105204817@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2015-11-10 19:57 ` [ruby-core:71434] " eddyluten
@ 2015-11-10 23:21 ` nobu
  6 siblings, 0 replies; 7+ messages in thread
From: nobu @ 2015-11-10 23:21 UTC (permalink / raw)
  To: ruby-core

Issue #11661 has been updated by Nobuyoshi Nakada.


Unfortunately, this change conflict with rubyspec.
How should we do:

1. backport the fix and remove the failed spec
2. add version guard and fix only the trunk
3. keep the current behavior

----------------------------------------
Bug #11661: sprintf causes a KeyError instead of using a default value for hash substitution
https://bugs.ruby-lang.org/issues/11661#change-54805

* Author: Eddy Luten
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* ruby -v: 
* Backport: 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED
----------------------------------------
When using a format string that substitutes hash values (or using the `%` operator on a string), instead of using the Hash's default value if a key is not present, it causes a KeyError.

As an end-user, to get around this, my hash needs to know about all the possible keys ahead of time and pre-assign a value to them or handle the KeyError. Logically, I would assume that the `sprintf` implementation would use the default Hash value.

I wanted to open this issue to see what your collective thoughts were on this since I have a fork running locally that fixes this issue and was wondering if I could send a patch/PR for this.

This issue is reproducible using the following code:

```ruby
my_hash = Hash.new('world')
puts "hello %{location}" % my_hash # expecting "hello world"
# "KeyError: key{location} not found"
```



-- 
https://bugs.ruby-lang.org/

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

end of thread, other threads:[~2015-11-10 22:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-11661.20151105204817@ruby-lang.org>
2015-11-05 20:48 ` [ruby-core:71354] [Ruby trunk - Bug #11661] [Open] sprintf causes a KeyError instead of using a default value for hash substitution eddyluten
2015-11-08  7:58 ` [ruby-core:71386] [Ruby trunk - Feature #11661] [Feedback] " nobu
2015-11-08  8:36 ` [ruby-core:71387] [Ruby trunk - Feature #11661] [Assigned] " nobu
2015-11-10 11:37 ` [ruby-core:71430] [Ruby trunk - Bug #11661] " nobu
2015-11-10 12:34 ` [ruby-core:71431] " hanmac
2015-11-10 19:57 ` [ruby-core:71434] " eddyluten
2015-11-10 23:21 ` [ruby-core:71436] " nobu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).