Patchwork [03,of,10] py3: make sure we return strings from __str__ and __repr__

login
register
mail settings
Submitter Pulkit Goyal
Date May 31, 2017, 9:46 p.m.
Message ID <25718d4a6de987771b43.1496267219@workspace>
Download mbox | patch
Permalink /patch/21098/
State Accepted
Headers show

Comments

Pulkit Goyal - May 31, 2017, 9:46 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1496255410 -19800
#      Thu Jun 01 00:00:10 2017 +0530
# Node ID 25718d4a6de987771b43874573e1095c9cfc5ab2
# Parent  93f5b615c900ec9b74b141aaeb162041f59d5737
py3: make sure we return strings from __str__ and __repr__

On Python 3:

>>> class abc:
...     def __repr__(self):
...             return b'abc'
...
>>> abc()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __repr__ returned non-string (type bytes)

>>> class abc:
...     def __str__(self):
...             return b'abc'
...
>>> str(abc())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __str__ returned non-string (type bytes)

So the __str__ and __repr__ must return strings.

Patch

diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -77,7 +77,7 @@ 
         return self.rev()
 
     def __repr__(self):
-        return "<%s %s>" % (type(self).__name__, str(self))
+        return r"<%s %s>" % (type(self).__name__, str(self))
 
     def __eq__(self, other):
         try:
@@ -1406,7 +1406,7 @@ 
             self._extra['branch'] = 'default'
 
     def __str__(self):
-        return str(self._parents[0]) + "+"
+        return str(self._parents[0]) + r"+"
 
     def __nonzero__(self):
         return True