Patchwork [04,of,10,STABLE] py3: decode encoding literal before passing to .decode()

login
register
mail settings
Submitter Denis Laxalde
Date Oct. 24, 2019, 3:30 p.m.
Message ID <0e2dd04f18a589180731.1571931035@steppe.local>
Download mbox | patch
Permalink /patch/42567/
State Accepted
Headers show

Comments

Denis Laxalde - Oct. 24, 2019, 3:30 p.m.
# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1571920100 -7200
#      Thu Oct 24 14:28:20 2019 +0200
# Branch stable
# Node ID 0e2dd04f18a589180731471cc6a62a10f097bc59
# Parent  3b9e9dfaf1e032307fa725c69b4c1dee4d92f53e
py3: decode encoding literal before passing to .decode()

bytes.decode(<encoding>) wants an str as "encoding" parameter,
it raises a TypeError if given a bytestring. encoding.encoding and
encoding.fallbackencoding are bytes values.
Yuya Nishihara - Oct. 25, 2019, 12:05 p.m.
On Thu, 24 Oct 2019 17:30:35 +0200, Denis Laxalde wrote:
> # HG changeset patch
> # User Denis Laxalde <denis.laxalde@logilab.fr>
> # Date 1571920100 -7200
> #      Thu Oct 24 14:28:20 2019 +0200
> # Branch stable
> # Node ID 0e2dd04f18a589180731471cc6a62a10f097bc59
> # Parent  3b9e9dfaf1e032307fa725c69b4c1dee4d92f53e
> py3: decode encoding literal before passing to .decode()
> 
> bytes.decode(<encoding>) wants an str as "encoding" parameter,
> it raises a TypeError if given a bytestring. encoding.encoding and
> encoding.fallbackencoding are bytes values.
> 
> diff --git a/mercurial/mail.py b/mercurial/mail.py
> --- a/mercurial/mail.py
> +++ b/mercurial/mail.py
> @@ -342,6 +342,7 @@ def _encode(ui, s, charsets):
>          s.decode('ascii')
>      except UnicodeDecodeError:
>          for ics in (encoding.encoding, encoding.fallbackencoding):
> +            ics = ics.decode()

Better to not rely on the default platform encoding. Use pycompat.sysstr()
instead.

Patch

diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -342,6 +342,7 @@  def _encode(ui, s, charsets):
         s.decode('ascii')
     except UnicodeDecodeError:
         for ics in (encoding.encoding, encoding.fallbackencoding):
+            ics = ics.decode()
             try:
                 u = s.decode(ics)
             except UnicodeDecodeError: