Patchwork [2,of,2] tests: avoid implicit conversion of str to unicode

login
register
mail settings
Submitter Manuel Jacob
Date March 6, 2020, 10:21 a.m.
Message ID <b67ead721ec5c9c39ba7.1583490099@tmp>
Download mbox | patch
Permalink /patch/45547/
State Accepted
Headers show

Comments

Manuel Jacob - March 6, 2020, 10:21 a.m.
# HG changeset patch
# User Manuel Jacob <me@manueljacob.de>
# Date 1583488364 -3600
#      Fri Mar 06 10:52:44 2020 +0100
# Node ID b67ead721ec5c9c39ba7e41d2687848408c33321
# Parent  53c5a127b144d4fea08ae466cd9e46a56095b288
# EXP-Topic remove-implicit-str-conversions
tests: avoid implicit conversion of str to unicode

On Python 2, str.encode('utf-8') implicitly converts the string to unicode and
then back to str.

Using _sys2bytes() ensures that opt is only encoded on Python 3, where opt is
unicode.

Although contrived, before this change, a UnicodeDecodeError could be triggered
on Python 2 when passing non-ascii values to --extra-config-opt.

Patch

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -1331,7 +1331,7 @@ 
 
         extraextensions = []
         for opt in self._extraconfigopts:
-            section, key = opt.encode('utf-8').split(b'.', 1)
+            section, key = _sys2bytes(opt).split(b'.', 1)
             if section != 'extensions':
                 continue
             name = key.split(b'=', 1)[0]
@@ -1432,7 +1432,7 @@ 
             hgrc.write(b'server-header = testing stub value\n')
 
             for opt in self._extraconfigopts:
-                section, key = opt.encode('utf-8').split(b'.', 1)
+                section, key = _sys2bytes(opt).split(b'.', 1)
                 assert b'=' in key, (
                     'extra config opt %s must ' 'have an = for assignment' % opt
                 )