Patchwork [STABLE] curses: do not setlocale() at import time (issue5261)

login
register
mail settings
Submitter Yuya Nishihara
Date July 25, 2019, 12:42 p.m.
Message ID <315e0c8b8e63addcbfeb.1564058541@mimosa>
Download mbox | patch
Permalink /patch/41069/
State Accepted
Headers show

Comments

Yuya Nishihara - July 25, 2019, 12:42 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1564057709 -32400
#      Thu Jul 25 21:28:29 2019 +0900
# Branch stable
# Node ID 315e0c8b8e63addcbfebedfa4e832cf265cca203
# Parent  7fae3b0bd893b75e0fb65ad3032b7532089e2341
curses: do not setlocale() at import time (issue5261)

setlocale() can break date formatting/parsing functions because they are
locale dependent. We should avoid doing setlocale() as possible.

This patch moves setlocale() just before curses.wrapper(), which function
is documented to "initialize curses." I don't know the details about the
curses initialization, but I *think* this would work as well.

Maybe we can extract a curses setup function later.

https://www.mercurial-scm.org/pipermail/mercurial-devel/2019-February/128788.html
Augie Fackler - July 26, 2019, 11:15 p.m.
queued for stable, thanks

> On Jul 25, 2019, at 8:42 AM, Yuya Nishihara <yuya@tcha.org> wrote:
> 
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1564057709 -32400
> #      Thu Jul 25 21:28:29 2019 +0900
> # Branch stable
> # Node ID 315e0c8b8e63addcbfebedfa4e832cf265cca203
> # Parent  7fae3b0bd893b75e0fb65ad3032b7532089e2341
> curses: do not setlocale() at import time (issue5261)
> 
> setlocale() can break date formatting/parsing functions because they are
> locale dependent. We should avoid doing setlocale() as possible.
> 
> This patch moves setlocale() just before curses.wrapper(), which function
> is documented to "initialize curses." I don't know the details about the
> curses initialization, but I *think* this would work as well.
> 
> Maybe we can extract a curses setup function later.
> 
> https://www.mercurial-scm.org/pipermail/mercurial-devel/2019-February/128788.html
> 
> diff --git a/hgext/histedit.py b/hgext/histedit.py
> --- a/hgext/histedit.py
> +++ b/hgext/histedit.py
> @@ -201,6 +201,7 @@ except ImportError:
>     termios = None
> 
> import functools
> +import locale
> import os
> import struct
> 
> @@ -947,12 +948,6 @@ def findoutgoing(ui, repo, remote=None, 
> # Curses Support
> try:
>     import curses
> -
> -    # Curses requires setting the locale or it will default to the C
> -    # locale. This sets the locale to the user's default system
> -    # locale.
> -    import locale
> -    locale.setlocale(locale.LC_ALL, r'')
> except ImportError:
>     curses = None
> 
> @@ -1538,6 +1533,10 @@ def _chistedit(ui, repo, *freeargs, **op
>         ctxs = []
>         for i, r in enumerate(revs):
>             ctxs.append(histeditrule(repo[r], i))
> +        # Curses requires setting the locale or it will default to the C
> +        # locale. This sets the locale to the user's default system
> +        # locale.
> +        locale.setlocale(locale.LC_ALL, r'')
>         rc = curses.wrapper(functools.partial(_chisteditmain, repo, ctxs))
>         curses.echo()
>         curses.endwin()
> @@ -2323,4 +2322,3 @@ def extsetup(ui):
>     cmdutil.summaryhooks.add('histedit', summaryhook)
>     statemod.addunfinished('histedit', fname='histedit-state', allowcommit=True,
>                             continueflag=True, abortfunc=hgaborthistedit)
> -
> diff --git a/mercurial/crecord.py b/mercurial/crecord.py
> --- a/mercurial/crecord.py
> +++ b/mercurial/crecord.py
> @@ -29,10 +29,6 @@ from .utils import (
> )
> stringio = util.stringio
> 
> -# This is required for ncurses to display non-ASCII characters in default user
> -# locale encoding correctly.  --immerrr
> -locale.setlocale(locale.LC_ALL, r'')
> -
> # patch comments based on the git one
> diffhelptext = _("""# To remove '-' lines, make them ' ' lines (context).
> # To remove '+' lines, delete them.
> @@ -530,6 +526,9 @@ def chunkselector(ui, headerlist, operat
>     """
>     ui.write(_('starting interactive selection\n'))
>     chunkselector = curseschunkselector(headerlist, ui, operation)
> +    # This is required for ncurses to display non-ASCII characters in
> +    # default user locale encoding correctly.  --immerrr
> +    locale.setlocale(locale.LC_ALL, r'')
>     origsigtstp = sentinel = object()
>     if util.safehasattr(signal, 'SIGTSTP'):
>         origsigtstp = signal.getsignal(signal.SIGTSTP)
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Pulkit Goyal - July 29, 2019, 9:57 a.m.
Seems like it was not queued, so I queued it.

On Sat, Jul 27, 2019 at 2:17 AM Augie Fackler <raf@durin42.com> wrote:
>
> queued for stable, thanks
>
> > On Jul 25, 2019, at 8:42 AM, Yuya Nishihara <yuya@tcha.org> wrote:
> >
> > # HG changeset patch
> > # User Yuya Nishihara <yuya@tcha.org>
> > # Date 1564057709 -32400
> > #      Thu Jul 25 21:28:29 2019 +0900
> > # Branch stable
> > # Node ID 315e0c8b8e63addcbfebedfa4e832cf265cca203
> > # Parent  7fae3b0bd893b75e0fb65ad3032b7532089e2341
> > curses: do not setlocale() at import time (issue5261)
> >
> > setlocale() can break date formatting/parsing functions because they are
> > locale dependent. We should avoid doing setlocale() as possible.
> >
> > This patch moves setlocale() just before curses.wrapper(), which function
> > is documented to "initialize curses." I don't know the details about the
> > curses initialization, but I *think* this would work as well.
> >
> > Maybe we can extract a curses setup function later.
> >
> > https://www.mercurial-scm.org/pipermail/mercurial-devel/2019-February/128788.html
> >
> > diff --git a/hgext/histedit.py b/hgext/histedit.py
> > --- a/hgext/histedit.py
> > +++ b/hgext/histedit.py
> > @@ -201,6 +201,7 @@ except ImportError:
> >     termios = None
> >
> > import functools
> > +import locale
> > import os
> > import struct
> >
> > @@ -947,12 +948,6 @@ def findoutgoing(ui, repo, remote=None,
> > # Curses Support
> > try:
> >     import curses
> > -
> > -    # Curses requires setting the locale or it will default to the C
> > -    # locale. This sets the locale to the user's default system
> > -    # locale.
> > -    import locale
> > -    locale.setlocale(locale.LC_ALL, r'')
> > except ImportError:
> >     curses = None
> >
> > @@ -1538,6 +1533,10 @@ def _chistedit(ui, repo, *freeargs, **op
> >         ctxs = []
> >         for i, r in enumerate(revs):
> >             ctxs.append(histeditrule(repo[r], i))
> > +        # Curses requires setting the locale or it will default to the C
> > +        # locale. This sets the locale to the user's default system
> > +        # locale.
> > +        locale.setlocale(locale.LC_ALL, r'')
> >         rc = curses.wrapper(functools.partial(_chisteditmain, repo, ctxs))
> >         curses.echo()
> >         curses.endwin()
> > @@ -2323,4 +2322,3 @@ def extsetup(ui):
> >     cmdutil.summaryhooks.add('histedit', summaryhook)
> >     statemod.addunfinished('histedit', fname='histedit-state', allowcommit=True,
> >                             continueflag=True, abortfunc=hgaborthistedit)
> > -
> > diff --git a/mercurial/crecord.py b/mercurial/crecord.py
> > --- a/mercurial/crecord.py
> > +++ b/mercurial/crecord.py
> > @@ -29,10 +29,6 @@ from .utils import (
> > )
> > stringio = util.stringio
> >
> > -# This is required for ncurses to display non-ASCII characters in default user
> > -# locale encoding correctly.  --immerrr
> > -locale.setlocale(locale.LC_ALL, r'')
> > -
> > # patch comments based on the git one
> > diffhelptext = _("""# To remove '-' lines, make them ' ' lines (context).
> > # To remove '+' lines, delete them.
> > @@ -530,6 +526,9 @@ def chunkselector(ui, headerlist, operat
> >     """
> >     ui.write(_('starting interactive selection\n'))
> >     chunkselector = curseschunkselector(headerlist, ui, operation)
> > +    # This is required for ncurses to display non-ASCII characters in
> > +    # default user locale encoding correctly.  --immerrr
> > +    locale.setlocale(locale.LC_ALL, r'')
> >     origsigtstp = sentinel = object()
> >     if util.safehasattr(signal, 'SIGTSTP'):
> >         origsigtstp = signal.getsignal(signal.SIGTSTP)
> > _______________________________________________
> > Mercurial-devel mailing list
> > Mercurial-devel@mercurial-scm.org
> > https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -201,6 +201,7 @@  except ImportError:
     termios = None
 
 import functools
+import locale
 import os
 import struct
 
@@ -947,12 +948,6 @@  def findoutgoing(ui, repo, remote=None, 
 # Curses Support
 try:
     import curses
-
-    # Curses requires setting the locale or it will default to the C
-    # locale. This sets the locale to the user's default system
-    # locale.
-    import locale
-    locale.setlocale(locale.LC_ALL, r'')
 except ImportError:
     curses = None
 
@@ -1538,6 +1533,10 @@  def _chistedit(ui, repo, *freeargs, **op
         ctxs = []
         for i, r in enumerate(revs):
             ctxs.append(histeditrule(repo[r], i))
+        # Curses requires setting the locale or it will default to the C
+        # locale. This sets the locale to the user's default system
+        # locale.
+        locale.setlocale(locale.LC_ALL, r'')
         rc = curses.wrapper(functools.partial(_chisteditmain, repo, ctxs))
         curses.echo()
         curses.endwin()
@@ -2323,4 +2322,3 @@  def extsetup(ui):
     cmdutil.summaryhooks.add('histedit', summaryhook)
     statemod.addunfinished('histedit', fname='histedit-state', allowcommit=True,
                             continueflag=True, abortfunc=hgaborthistedit)
-
diff --git a/mercurial/crecord.py b/mercurial/crecord.py
--- a/mercurial/crecord.py
+++ b/mercurial/crecord.py
@@ -29,10 +29,6 @@  from .utils import (
 )
 stringio = util.stringio
 
-# This is required for ncurses to display non-ASCII characters in default user
-# locale encoding correctly.  --immerrr
-locale.setlocale(locale.LC_ALL, r'')
-
 # patch comments based on the git one
 diffhelptext = _("""# To remove '-' lines, make them ' ' lines (context).
 # To remove '+' lines, delete them.
@@ -530,6 +526,9 @@  def chunkselector(ui, headerlist, operat
     """
     ui.write(_('starting interactive selection\n'))
     chunkselector = curseschunkselector(headerlist, ui, operation)
+    # This is required for ncurses to display non-ASCII characters in
+    # default user locale encoding correctly.  --immerrr
+    locale.setlocale(locale.LC_ALL, r'')
     origsigtstp = sentinel = object()
     if util.safehasattr(signal, 'SIGTSTP'):
         origsigtstp = signal.getsignal(signal.SIGTSTP)