Project

General

Profile

Actions

Bug #10493

closed

Wrong encoding for Etc.getlogin in Windows

Added by davispuh (Dāvis Mosāns) about 10 years ago. Updated almost 10 years ago.

Status:
Closed
Assignee:
-
Target version:
ruby -v:
ruby 2.1.4p265 (2014-10-27 revision 48166) [x64-mingw32]
[ruby-core:66163]

Description

name = Etc.getlogin # I've Windows username "Dāvis"
puts name           # D�vis
puts name.inspect   # "D\xE2vis"
puts name.encoding  # ASCII-8BIT
name.force_encoding('Windows-1257')
puts name           # Dāvis

As you can see \xE2 isn't "ā" in ASCII, but it is for Windws-1257 encoding.

Updated by nobu (Nobuyoshi Nakada) about 10 years ago

  • Description updated (diff)
  • Category set to ext
  • Status changed from Open to Feedback
  • Target version set to 2.2.0
  • Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN to 2.0.0: REQUIRED, 2.1: REQUIRED

Is your Encoding.default_external Windows-1257?

Updated by nobu (Nobuyoshi Nakada) about 10 years ago

  • Status changed from Feedback to Closed
  • % Done changed from 0 to 100

Applied in changeset r48360.


win32.c: login name encoding

Updated by davispuh (Dāvis Mosāns) about 10 years ago

No, Encoding.default_external is based on current consoles encoding. And by default it is IBM775, but can be different for each opened console. Also it's not related to Encoding which is used by Windows ANSI API. Windows-1257 encoding is set in Control Panel for non-Unicode programs and will be used for all Windows ANSI functions. Proper way is to always use Windows Unicode/Wide API and then it will be encoded with UTF-16.

Thanks for patch, I'll test it when it will be released.

Updated by duerst (Martin Dürst) about 10 years ago

Dāvis Mosāns wrote:

No, Encoding.default_external is based on current consoles encoding. And by default it is IBM775, but can be different for each opened console. Also it's not related to Encoding which is used by Windows ANSI API. Windows-1257 encoding is set in Control Panel for non-Unicode programs and will be used for all Windows ANSI functions.

Can you say which section of the control panel that is?

Proper way is to always use Windows Unicode/Wide API and then it will be encoded with UTF-16.

Thanks for patch, I'll test it when it will be released.

It would be highly desirable to test it before it's released.

Updated by davispuh (Dāvis Mosāns) about 10 years ago

Control Panel -> Region -> Administrative -> Change system locale

Here's article describing how to change locale for Windows 7 and 8

For Windows 7
Windows 7

For Windows 8
Windows 8

To test it now I would need to setup environment and compile Ruby, but I never have done that and currently don't really have time for it. Anyway I'll see maybe I will be able to find time for it someday.

Updated by luislavena (Luis Lavena) about 10 years ago

Davis,

You can build Ruby easily by using RubyInstaller recipes:

https://github.com/oneclick/rubyinstaller

You will also need a clone of Ruby source code and usage of LOCAL to indicate the PATH where to find Ruby source code.

See examples in RubyInstaller's readme.

Cheers.

Updated by davispuh (Dāvis Mosāns) about 10 years ago

Thanks, that was surprisingly easy. Only trunk didn't build so I compiled from ruby_2_1 branch and cherry-picked r48360, r48364
Now I can confirm that patch works.

name = Etc.getlogin
puts name.inspect   # "D\u0101vis"
puts name.encoding  # UTF-8

But looks like encoding for string outputting have been broken somewhere (I hope it's fixed in some commits not yet merged in this branch)

puts "\u0101"
=> ā   # on released Ruby 2.1.4p265
=> Ä?  # on 2.1.5p272, I just compiled from ruby_2_1 branch

It used to be that when outputting string it's encoded to Encoding.default_external, but that's not the case for this.

Updated by usa (Usaku NAKAMURA) almost 10 years ago

  • Backport changed from 2.0.0: REQUIRED, 2.1: REQUIRED to 2.0.0: DONE, 2.1: REQUIRED

Backported into ruby_2_0_0 at r48634.

memo:
I changed the patch a little, because 2.0.0 doesn't have rb_w32_wstr_to_mbstr().
2.1 has the function, so I guess it's no problem to backport to 2.1 from trunk.

Updated by nagachika (Tomoyuki Chikanaga) almost 10 years ago

  • Backport changed from 2.0.0: DONE, 2.1: REQUIRED to 2.0.0: DONE, 2.1: DONE

Backported into ruby_2_1 at r49473.

usa-san, thank you for your advice. I'll keep my eyes on RubyCI, especially on vc-x64.

Updated by usa (Usaku NAKAMURA) almost 10 years ago

Tomoyuki Chikanaga wrote:

Backported into ruby_2_1 at r49473.
usa-san, thank you for your advice. I'll keep my eyes on RubyCI, especially on vc-x64.

It seems good. (I've tested on my box.)
Thank you!

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0