← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-fix-get-unique into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-fix-get-unique into launchpad:master.

Commit message:
Fix getUniqueBytes and getUniqueUnicode on Python 3

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

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

Most calls to the factory method getUniqueString should be replaced with getUniqueBytes or getUniqueUnicode, but they previously only had their expected behaviour on Python 2.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-fix-get-unique into launchpad:master.
diff --git a/lib/lp/testing/factory.py b/lib/lp/testing/factory.py
index b7dc38f..dab0cbf 100644
--- a/lib/lp/testing/factory.py
+++ b/lib/lp/testing/factory.py
@@ -490,10 +490,16 @@ class ObjectFactory(
 
     # XXX cjwatson 2020-02-20: We should disentangle this; most uses of
     # getUniqueString should probably use getUniqueUnicode instead.
-    getUniqueBytes = getUniqueString
+    if sys.version_info[0] >= 3:
+        def getUniqueBytes(self, prefix=None):
+            return self.getUniqueString(prefix=prefix).encode('UTF-8')
 
-    def getUniqueUnicode(self, prefix=None):
-        return self.getUniqueString(prefix=prefix).decode('latin-1')
+        getUniqueUnicode = getUniqueString
+    else:
+        getUniqueBytes = getUniqueString
+
+        def getUniqueUnicode(self, prefix=None):
+            return self.getUniqueString(prefix=prefix).decode('UTF-8')
 
     def getUniqueURL(self, scheme=None, host=None):
         """Return a URL unique to this run of the test case."""