Patchwork py3: use sysstr() to convert ProgrammingError bytes with no unicode error risk

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 13, 2018, 2:08 p.m.
Message ID <36631d7dc21d43e8b840.1536847717@mimosa>
Download mbox | patch
Permalink /patch/34599/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 13, 2018, 2:08 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1536845571 -32400
#      Thu Sep 13 22:32:51 2018 +0900
# Node ID 36631d7dc21d43e8b8409041fdbe8d6f3d530259
# Parent  c8514f858788ffebee5f5e01a24d20e00e9d68a8
py3: use sysstr() to convert ProgrammingError bytes with no unicode error risk

msg.decode('utf8') may fail if msg isn't an ASCII string, and that's possible
as we sometimes embed a filename in the error message for example.
Augie Fackler - Sept. 13, 2018, 3:04 p.m.
> On Sep 13, 2018, at 10:08, Yuya Nishihara <yuya@tcha.org> wrote:
> 
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1536845571 -32400
> #      Thu Sep 13 22:32:51 2018 +0900
> # Node ID 36631d7dc21d43e8b8409041fdbe8d6f3d530259
> # Parent  c8514f858788ffebee5f5e01a24d20e00e9d68a8
> py3: use sysstr() to convert ProgrammingError bytes with no unicode error risk

queued, thanks (and d'oh: I could have sworn I thought that through enough)

Patch

diff --git a/mercurial/error.py b/mercurial/error.py
--- a/mercurial/error.py
+++ b/mercurial/error.py
@@ -217,12 +217,10 @@  class ProgrammingError(Hint, RuntimeErro
     """Raised if a mercurial (core or extension) developer made a mistake"""
 
     def __init__(self, msg, *args, **kwargs):
-        if not isinstance(msg, str):
-            # This means we're on Python 3, because we got a
-            # bytes. Turn the message back into a string since this is
-            # an internal-only error that won't be printed except in a
-            # stack traces.
-            msg = msg.decode('utf8')
+        # On Python 3, turn the message back into a string since this is
+        # an internal-only error that won't be printed except in a
+        # stack traces.
+        msg = pycompat.sysstr(msg)
         super(ProgrammingError, self).__init__(msg, *args, **kwargs)
 
     __bytes__ = _tobytes