[PATCH 08 of 10] py3: use unicode literals in mdiff.py

Pulkit Goyal 7895pulkit at gmail.com
Tue Aug 2 16:27:30 EDT 2016


# HG changeset patch
# User Pulkit Goyal <7895pulkit at gmail.com>
# Date 1470168805 -19800
#      Wed Aug 03 01:43:25 2016 +0530
# Node ID f7cb9b3eeac9c22dc76f6c5976fda488ba992be2
# Parent  a77a7f6e8bfc90901d829257a782ff11e2bae0f7
py3: use unicode literals in mdiff.py

This is a near-sighted solution to unbust mdiff.py importing on Python
3. The import was previously failing due to a setattr() using a
bytes instance on the attribute name.

The behavior of str vs bytes keys is kinda wonky in Python.
In Python 2, key names are automagically converted from 1 type
to the other. e.g. if you set a unicode key then set a str key of
essentially the same value, the original key gets updated. But
on Python 3 there can be different keys for "identical" str and
bytes values! That's why this patch is short-signed: there will
certainly be consumers looking at the bytes keys of this dict.

But for now it unblocks importing of this module. And behavior
is identical in Python 2, so it should be safe.

diff -r a77a7f6e8bfc -r f7cb9b3eeac9 mercurial/mdiff.py
--- a/mercurial/mdiff.py	Wed Aug 03 01:39:08 2016 +0530
+++ b/mercurial/mdiff.py	Wed Aug 03 01:43:25 2016 +0530
@@ -45,17 +45,17 @@
     '''
 
     defaults = {
-        'context': 3,
-        'text': False,
-        'showfunc': False,
-        'git': False,
-        'nodates': False,
-        'nobinary': False,
-        'noprefix': False,
-        'ignorews': False,
-        'ignorewsamount': False,
-        'ignoreblanklines': False,
-        'upgrade': False,
+        u'context': 3,
+        u'text': False,
+        u'showfunc': False,
+        u'git': False,
+        u'nodates': False,
+        u'nobinary': False,
+        u'noprefix': False,
+        u'ignorews': False,
+        u'ignorewsamount': False,
+        u'ignoreblanklines': False,
+        u'upgrade': False,
         }
 
     def __init__(self, **opts):
diff -r a77a7f6e8bfc -r f7cb9b3eeac9 tests/test-check-py3-compat.t
--- a/tests/test-check-py3-compat.t	Wed Aug 03 01:39:08 2016 +0530
+++ b/tests/test-check-py3-compat.t	Wed Aug 03 01:43:25 2016 +0530
@@ -161,7 +161,7 @@
   mercurial/ui.py: error importing module: <TypeError> a bytes-like object is required, not 'str' (line *) (glob)
   mercurial/unionrepo.py: error importing module: <TypeError> a bytes-like object is required, not 'str' (line *) (glob)
   mercurial/url.py: error importing module: <TypeError> a bytes-like object is required, not 'str' (line *) (glob)
-  mercurial/verify.py: error importing: <TypeError> attribute name must be string, not 'bytes' (error at mdiff.py:*) (glob)
+  mercurial/verify.py: error importing module: <TypeError> a bytes-like object is required, not 'str' (line *) (glob)
   mercurial/win32.py: error importing module: <ImportError> No module named 'msvcrt' (line *) (glob)
   mercurial/windows.py: error importing module: <ImportError> No module named '_winreg' (line *) (glob)
   mercurial/wireproto.py: error importing module: <SyntaxError> invalid syntax (bundle2.py, line *) (line *) (glob)


More information about the Mercurial-devel mailing list