ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:90853] [Ruby trunk Bug#15497] Encoding of error messages should not depend on the locale encoding
       [not found] <redmine.issue-15497.20190102121808@ruby-lang.org>
@ 2019-01-02 12:18 ` eregontp
  2019-01-07 11:38 ` [ruby-core:90915] " duerst
  2019-01-07 13:21 ` [ruby-core:90917] " nobu
  2 siblings, 0 replies; 3+ messages in thread
From: eregontp @ 2019-01-02 12:18 UTC (permalink / raw)
  To: ruby-core

Issue #15497 has been reported by Eregon (Benoit Daloze).

----------------------------------------
Bug #15497: Encoding of error messages should not depend on the locale encoding
https://bugs.ruby-lang.org/issues/15497

* Author: Eregon (Benoit Daloze)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
This seems to happen mostly for internal errors, as `raise` in Ruby code of course just uses the passed String's encoding for the message.

Example:
```ruby
name = "été"
p name.encoding

begin
  Module.new.const_set(name, 1)
rescue => e
  p e
  p e.message.encoding
end
```

When run, it gives:
```
$ LANG=en_US.UTF-8 ruby c.rb
#<Encoding:UTF-8>
#<NameError: wrong constant name été>
#<Encoding:UTF-8>

$ LANG=C ruby c.rb   
#<Encoding:UTF-8>
#<NameError: wrong constant name "\u00E9t\u00E9">
#<Encoding:US-ASCII>
```

Depending on the locale encoding, the encoding of the message changes!
This seems very unexpected, is inconvenient for testing (e.g., https://github.com/ruby/spec/commit/a6101a6e and any test checking exception messages with non-US-ASCII characters),
and does not represent what is in the source code (here it's clearly a valid UTF-8 String).

I think for such a case, the encoding of the constant name should be used, i.e., UTF-8.
Another way to see it is the message should be built like `"wrong constant name ".force_encoding('us-ascii') + constant_name`.
Indeed, if we do build the message manually like that it works as expected:

```
name = "été"
begin
  raise "wrong constant name ".force_encoding('US-ASCII') + name
rescue => e
  p e
  p e.message.encoding
end
```
gives
```
$ LANG=en_US.UTF-8 ruby c.rb
#<Encoding:UTF-8>
#<RuntimeError: wrong constant name été>
#<Encoding:UTF-8>

$ LANG=C ruby c.rb          
#<Encoding:UTF-8>
#<RuntimeError: wrong constant name \u00E9t\u00E9>
#<Encoding:UTF-8>
```

Note that the message still looks different, but that's the effect of `Kernel#p`, because it does not know how to display UTF-8 characters in a US-ASCII terminal.
Nevertheless, both messages have the same bytes and encoding, which fixes all 3 problems mentioned above.

Setting `Encoding.default_internal` can workaround this but it's a bad workaround as this cannot work reliably in a multithreaded Ruby application,
affects many more things than just error messages, and the default behavior should be error messages with a deterministic encoding, just like `raise` in Ruby code.



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

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

* [ruby-core:90915] [Ruby trunk Bug#15497] Encoding of error messages should not depend on the locale encoding
       [not found] <redmine.issue-15497.20190102121808@ruby-lang.org>
  2019-01-02 12:18 ` [ruby-core:90853] [Ruby trunk Bug#15497] Encoding of error messages should not depend on the locale encoding eregontp
@ 2019-01-07 11:38 ` duerst
  2019-01-07 13:21 ` [ruby-core:90917] " nobu
  2 siblings, 0 replies; 3+ messages in thread
From: duerst @ 2019-01-07 11:38 UTC (permalink / raw)
  To: ruby-core

Issue #15497 has been updated by duerst (Martin Dürst).


I agree that the locale encoding should only be taken into account when the message is actually output, not as long as it is passed around inside Ruby.

----------------------------------------
Bug #15497: Encoding of error messages should not depend on the locale encoding
https://bugs.ruby-lang.org/issues/15497#change-76106

* Author: Eregon (Benoit Daloze)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
This seems to happen mostly for internal errors, as `raise` in Ruby code of course just uses the passed String's encoding for the message.

Example:
```ruby
name = "été"
p name.encoding

begin
  Module.new.const_set(name, 1)
rescue => e
  p e
  p e.message.encoding
end
```

When run, it gives:
```
$ LANG=en_US.UTF-8 ruby c.rb
#<Encoding:UTF-8>
#<NameError: wrong constant name été>
#<Encoding:UTF-8>

$ LANG=C ruby c.rb   
#<Encoding:UTF-8>
#<NameError: wrong constant name "\u00E9t\u00E9">
#<Encoding:US-ASCII>
```

Depending on the locale encoding, the encoding of the message changes!
This seems very unexpected, is inconvenient for testing (e.g., https://github.com/ruby/spec/commit/a6101a6e and any test checking exception messages with non-US-ASCII characters),
and does not represent what is in the source code (here it's clearly a valid UTF-8 String).

I think for such a case, the encoding of the constant name should be used, i.e., UTF-8.
Another way to see it is the message should be built like `"wrong constant name ".force_encoding('us-ascii') + constant_name`.
Indeed, if we do build the message manually like that it works as expected:

```
name = "été"
begin
  raise "wrong constant name ".force_encoding('US-ASCII') + name
rescue => e
  p e
  p e.message.encoding
end
```
gives
```
$ LANG=en_US.UTF-8 ruby c.rb
#<Encoding:UTF-8>
#<RuntimeError: wrong constant name été>
#<Encoding:UTF-8>

$ LANG=C ruby c.rb          
#<Encoding:UTF-8>
#<RuntimeError: wrong constant name \u00E9t\u00E9>
#<Encoding:UTF-8>
```

Note that the message still looks different, but that's the effect of `Kernel#p`, because it does not know how to display UTF-8 characters in a US-ASCII terminal.
Nevertheless, both messages have the same bytes and encoding, which fixes all 3 problems mentioned above.

Setting `Encoding.default_internal` can workaround this but it's a bad workaround as this cannot work reliably in a multithreaded Ruby application,
affects many more things than just error messages, and the default behavior should be error messages with a deterministic encoding, just like `raise` in Ruby code.



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

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

* [ruby-core:90917] [Ruby trunk Bug#15497] Encoding of error messages should not depend on the locale encoding
       [not found] <redmine.issue-15497.20190102121808@ruby-lang.org>
  2019-01-02 12:18 ` [ruby-core:90853] [Ruby trunk Bug#15497] Encoding of error messages should not depend on the locale encoding eregontp
  2019-01-07 11:38 ` [ruby-core:90915] " duerst
@ 2019-01-07 13:21 ` nobu
  2 siblings, 0 replies; 3+ messages in thread
From: nobu @ 2019-01-07 13:21 UTC (permalink / raw)
  To: ruby-core

Issue #15497 has been updated by nobu (Nobuyoshi Nakada).

Description updated

It is intended for not only different encoding characters, but also control characters, e.g., "\0".
The `message` is to display, and it is not good idea to show such chars directly, I guess.
And `name` is available for the bare purpose.


----------------------------------------
Bug #15497: Encoding of error messages should not depend on the locale encoding
https://bugs.ruby-lang.org/issues/15497#change-76109

* Author: Eregon (Benoit Daloze)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
This seems to happen mostly for internal errors, as `raise` in Ruby code of course just uses the passed String's encoding for the message.

Example:
```ruby
name = "été"
p name.encoding

begin
  Module.new.const_set(name, 1)
rescue => e
  p e
  p e.message.encoding
end
```

When run, it gives:
```
$ LANG=en_US.UTF-8 ruby c.rb
#<Encoding:UTF-8>
#<NameError: wrong constant name été>
#<Encoding:UTF-8>

$ LANG=C ruby c.rb   
#<Encoding:UTF-8>
#<NameError: wrong constant name "\u00E9t\u00E9">
#<Encoding:US-ASCII>
```

Depending on the locale encoding, the encoding of the message changes!
This seems very unexpected, is inconvenient for testing (e.g., https://github.com/ruby/spec/commit/a6101a6e and any test checking exception messages with non-US-ASCII characters),
and does not represent what is in the source code (here it's clearly a valid UTF-8 String).

I think for such a case, the encoding of the constant name should be used, i.e., UTF-8.
Another way to see it is the message should be built like `"wrong constant name ".force_encoding('us-ascii') + constant_name`.
Indeed, if we do build the message manually like that it works as expected:

```ruby
name = "été"
begin
  raise "wrong constant name ".force_encoding('US-ASCII') + name
rescue => e
  p e
  p e.message.encoding
end
```
gives
```
$ LANG=en_US.UTF-8 ruby c.rb
#<Encoding:UTF-8>
#<RuntimeError: wrong constant name été>
#<Encoding:UTF-8>

$ LANG=C ruby c.rb          
#<Encoding:UTF-8>
#<RuntimeError: wrong constant name \u00E9t\u00E9>
#<Encoding:UTF-8>
```

Note that the message still looks different, but that's the effect of `Kernel#p`, because it does not know how to display UTF-8 characters in a US-ASCII terminal.
Nevertheless, both messages have the same bytes and encoding, which fixes all 3 problems mentioned above.

Setting `Encoding.default_internal` can workaround this but it's a bad workaround as this cannot work reliably in a multithreaded Ruby application,
affects many more things than just error messages, and the default behavior should be error messages with a deterministic encoding, just like `raise` in Ruby code.



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

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

end of thread, other threads:[~2019-01-07 13:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-15497.20190102121808@ruby-lang.org>
2019-01-02 12:18 ` [ruby-core:90853] [Ruby trunk Bug#15497] Encoding of error messages should not depend on the locale encoding eregontp
2019-01-07 11:38 ` [ruby-core:90915] " duerst
2019-01-07 13:21 ` [ruby-core:90917] " 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).