public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Godmar Back <godmar@gmail.com>
To: "Carlos O'Donell" <carlos@redhat.com>
Cc: libc-help <libc-help@sourceware.org>
Subject: Re: why does `LANG` not influence the locale in GNU/Linux
Date: Thu, 27 Jan 2022 00:40:07 -0500	[thread overview]
Message-ID: <CAB4+JY+NHW7tYdNvVJWr0-qv5zzuvSTf1yqX0Lj94Bkj0-9uMg@mail.gmail.com> (raw)
In-Reply-To: <033b9a58-46c7-ceff-212f-203bce879172@redhat.com>

On Thu, Jan 27, 2022 at 12:29 AM Carlos O'Donell <carlos@redhat.com> wrote:
>
> On 1/26/22 19:35, Godmar Back via Libc-help wrote:
> > If I am writing code that uses fgetwc to read from standard input, I
> > need to call setlocale(LC_CTYPE, "en_US.utf8"); to ensure that fgetwc
> > will treat stdin as a UTF-8 encoded stream.  This property appears to
> > hold (on Ubuntu 20, with default GNU libc), independent of the setting
> > of LANG.  LANG is set to en_US.utf8.
> >
> > By contrast, Python 3 changes its behavior with regard to the encoding
> > it assumes sys.stdin to be in based on the LANG variable - if set to
> > en_US.utf8, it'll decode as UTF-8, if set to C or unset, it'll treat
> > the input as 8-bit ASCII encoding (I believe).
> >
> > My question is why GNU libc (chose?) to not use LANG and what
> > standards, if any, apply here.
> > Is my characterization of the behavior correct?
>
> The ISO C standard and the POSIX standard have specified the behaviour.
>
> ISO C says that at program startup the equivalent of:
> setlocale (LC_ALL, "C"); /* (See 7.11.1.1.4) */
> is executed. Therefore C programs start in the C locale, not the locale
> as specified by LANG.
>
> POSIX defines several environment variables, and they have precedence
> rules for deciding which one should be used by the application [1].
> However, this just defines the categories, but it doesn't *apply* them to the
> running program. Internationalized programs that wish to initialize locale
> specific operation must call setlocale [2]:
>
> setlocale (LC_CALL, "");

There's an extraneous `C` here, I'm reading this as  `setlocale (LC_ALL, "")`.

>
> Which will set up the global locale according to the environment variables
> in the precedence as setup by POSIX.
>
> Does that answer your question?

Yes it does, thank you.

It's slightly ironic if you read, for instance PEP-538 [*] that claims
that Python has a need "to use the configured locale encoding by
default for consistency with other locale-aware components in the same
process or subprocesses" when the default behavior in a Unix
environment is to not be locale-aware and revert back to the C locale.

[*] https://www.python.org/dev/peps/pep-0538/

>
> --
> Cheers,
> Carlos.
>
> [1] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_02
> [2] https://pubs.opengroup.org/onlinepubs/9699919799/functions/setlocale.html
>

  reply	other threads:[~2022-01-27  5:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-27  0:35 Godmar Back
2022-01-27  5:28 ` Carlos O'Donell
2022-01-27  5:40   ` Godmar Back [this message]
2022-01-27  5:53     ` Carlos O'Donell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAB4+JY+NHW7tYdNvVJWr0-qv5zzuvSTf1yqX0Lj94Bkj0-9uMg@mail.gmail.com \
    --to=godmar@gmail.com \
    --cc=carlos@redhat.com \
    --cc=libc-help@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).