Patchwork [05,of,10] pycompat: add boolclass for py3 compat

login
register
mail settings
Submitter timeless
Date May 12, 2016, 1:23 a.m.
Message ID <e670b15b74f80135c28e.1463016193@gcc2-power8.osuosl.org>
Download mbox | patch
Permalink /patch/15053/
State Under Review, archived
Delegated to: Yuya Nishihara
Headers show

Comments

timeless - May 12, 2016, 1:23 a.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1463011579 0
#      Thu May 12 00:06:19 2016 +0000
# Node ID e670b15b74f80135c28e3ac57b0580ad98b47cb9
# Parent  6a4c3eb0da5d63a0394bf68abe0b5d49c40ab96d
# Available At bb://timeless/mercurial-crew
#              hg pull bb://timeless/mercurial-crew -r e670b15b74f8
pycompat: add boolclass for py3 compat

Python 2 uses __nonzero__ when checking to see if a class is true,
Python 3 uses __bool__ for the same.

Patch

diff -r 6a4c3eb0da5d -r e670b15b74f8 contrib/check-code.py
--- a/contrib/check-code.py	Wed May 11 23:24:41 2016 +0000
+++ b/contrib/check-code.py	Thu May 12 00:06:19 2016 +0000
@@ -318,7 +318,7 @@ 
     (r'ui\.(status|progress|write|note|warn)\([\'\"]x',
      "missing _() in ui message (use () to hide false-positives)"),
     (r'release\(.*wlock, .*lock\)', "wrong lock release order"),
-    (r'\b__bool__\b', "__bool__ should be __nonzero__ in Python 2"),
+    (r'\bdef __bool__\b|\b__bool__\(', "__bool__ should be __nonzero__ in Python 2"),
     (r'os\.path\.join\(.*, *(""|\'\')\)',
      "use pathutil.normasprefix(path) instead of os.path.join(path, '')"),
     (r'\s0[0-7]+\b', 'legacy octal syntax; use "0o" prefix instead of "0"'),
diff -r 6a4c3eb0da5d -r e670b15b74f8 mercurial/pycompat.py
--- a/mercurial/pycompat.py	Wed May 11 23:24:41 2016 +0000
+++ b/mercurial/pycompat.py	Thu May 12 00:06:19 2016 +0000
@@ -36,12 +36,19 @@ 
         return d.iteritems()
     def itervalues(d):
         return d.itervalues()
+    def boolclass(cls):
+        """class decorator for classes with __bool__ method"""
+        return cls
 except AttributeError:
     # Python 3
     def iteritems(d):
         return iter(d.items())
     def itervalues(d):
         return iter(d.values())
+    def boolclass(cls):
+        """class decorator for classes with __bool__ method"""
+        cls.__bool__ = cls.__nonzero__
+        return cls
 
 try:
     import cStringIO as io
diff -r 6a4c3eb0da5d -r e670b15b74f8 mercurial/util.py
--- a/mercurial/util.py	Wed May 11 23:24:41 2016 +0000
+++ b/mercurial/util.py	Thu May 12 00:06:19 2016 +0000
@@ -46,6 +46,7 @@ 
 )
 
 for attr in (
+    'boolclass',
     'email',
     'empty',
     'http',