← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-remaining-doctest-unicode-strings into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-remaining-doctest-unicode-strings into launchpad:master.

Commit message:
Fix remaining u'...' doctest examples for Python 3

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/398699

I missed a couple of spots when landing earlier similar changes.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-remaining-doctest-unicode-strings into launchpad:master.
diff --git a/lib/lp/registry/browser/tests/karmacontext-views.txt b/lib/lp/registry/browser/tests/karmacontext-views.txt
index 3546713..e412bba 100644
--- a/lib/lp/registry/browser/tests/karmacontext-views.txt
+++ b/lib/lp/registry/browser/tests/karmacontext-views.txt
@@ -15,18 +15,25 @@ contributors on a given context and the top contributors by category.
     >>> view = create_initialized_view(
     ...     product, '+topcontributors', principal=user)
     >>> contributors = view._getTopContributorsWithLimit(limit=3)
-    >>> [(contrib.person.name, contrib.karmavalue)
-    ...     for contrib in contributors]
-    [(u'name16', 175), (u'mark', 22), (u'carlos', 9)]
+    >>> for contrib in contributors:
+    ...     print(contrib.person.name, contrib.karmavalue)
+    name16 175
+    mark 22
+    carlos 9
 
     >>> contributors = view.top_contributors_by_category
     >>> categories = sorted(contributors.keys())
     >>> for category in categories:
-    ...     print(category, [(contrib.person.name, contrib.karmavalue)
-    ...                      for contrib in contributors[category]])
-    Bug Management [(u'name16', 11)]
-    Specification Tracking [(u'mark', 22)]
-    Translations in Rosetta [(u'name16', 164), (u'carlos', 9)]
+    ...     print(category)
+    ...     for contrib in contributors[category]:
+    ...         print(contrib.person.name, contrib.karmavalue)
+    Bug Management
+    name16 11
+    Specification Tracking
+    mark 22
+    Translations in Rosetta
+    name16 164
+    carlos 9
 
 The view renders summaries by category.
 
diff --git a/lib/lp/services/database/postgresql.py b/lib/lp/services/database/postgresql.py
index a3fae61..b735476 100644
--- a/lib/lp/services/database/postgresql.py
+++ b/lib/lp/services/database/postgresql.py
@@ -21,6 +21,28 @@ from lp.services.database.sqlbase import (
     )
 
 
+def _py3ish_repr(value):
+    """Like `repr`, but uses Python 3 spelling of text.
+
+    This is a local helper for doctests.
+    """
+    if isinstance(value, six.text_type):
+        value = value.encode('unicode_escape').decode('ASCII')
+        if "'" in value and '"' not in value:
+            return '"%s"' % value
+        else:
+            return "'%s'" % value.replace("'", "\\'")
+    elif isinstance(value, tuple):
+        if len(value) == 1:
+            return '(' + _py3ish_repr(value[0]) + ',)'
+        else:
+            return '(' + ', '.join(_py3ish_repr(item) for item in value) + ')'
+    elif isinstance(value, list):
+        return '[' + ', '.join(_py3ish_repr(item) for item in value) + ']'
+    else:
+        return repr(value)
+
+
 def listReferences(cur, table, column, indirect=True, _state=None):
     """Return a list of all foreign key references to the given table column
 
@@ -43,11 +65,11 @@ def listReferences(cur, table, column, indirect=True, _state=None):
     to change keys.
 
     >>> for r in listReferences(cur, 'a', 'aid'):
-    ...     print(repr(r))
-    (u'a', u'selfref', u'a', u'aid', u'a', u'a')
-    (u'b', u'aid', u'a', u'aid', u'c', u'c')
-    (u'c', u'aid', u'b', u'aid', u'a', u'a')
-    (u'd', u'aid', u'b', u'aid', u'a', u'a')
+    ...     print(_py3ish_repr(r))
+    ('a', 'selfref', 'a', 'aid', 'a', 'a')
+    ('b', 'aid', 'a', 'aid', 'c', 'c')
+    ('c', 'aid', 'b', 'aid', 'a', 'a')
+    ('d', 'aid', 'b', 'aid', 'a', 'a')
 
     Of course, there might not be any references
 
@@ -115,27 +137,27 @@ def listUniques(cur, table, column):
 
     Simple UNIQUE index
 
-    >>> listUniques(cur, 'b', 'aid')
-    [(u'aid',)]
+    >>> print(_py3ish_repr(listUniques(cur, 'b', 'aid')))
+    [('aid',)]
 
     Primary keys are UNIQUE indexes too
 
-    >>> listUniques(cur, 'a', 'aid')
-    [(u'aid',)]
+    >>> print(_py3ish_repr(listUniques(cur, 'a', 'aid')))
+    [('aid',)]
 
     Compound indexes
 
-    >>> listUniques(cur, 'c', 'aid')
-    [(u'aid', u'bid')]
-    >>> listUniques(cur, 'c', 'bid')
-    [(u'aid', u'bid')]
+    >>> print(_py3ish_repr(listUniques(cur, 'c', 'aid')))
+    [('aid', 'bid')]
+    >>> print(_py3ish_repr(listUniques(cur, 'c', 'bid')))
+    [('aid', 'bid')]
 
     And any combination
 
     >>> l = listUniques(cur, 'd', 'aid')
     >>> l.sort()
-    >>> l
-    [(u'aid',), (u'aid', u'bid')]
+    >>> print(_py3ish_repr(l))
+    [('aid',), ('aid', 'bid')]
 
     If there are no UNIQUE indexes using the secified column
 
@@ -196,9 +218,9 @@ def listSequences(cur):
     standalone.
 
     >>> for r in listSequences(cur):
-    ...     print(repr(r))
-    (u'public', u'a_aid_seq', u'a', u'aid')
-    (u'public', u'standalone', None, None)
+    ...     print(_py3ish_repr(r))
+    ('public', 'a_aid_seq', 'a', 'aid')
+    ('public', 'standalone', None, None)
 
     """
     sql = """