[PATCH 1/2] py3: Ensure map is wrapped in list

Stephen Finucane stephen.finucane at intel.com
Sat Feb 6 04:21:19 AEDT 2016


The map function doesn't return a list in Python 3, which means it
can't be used like one. Many of these issues were previously resolved
but there were some uncovered code paths. Resolve these, adding tests
to ensure it doesn't regress.

In addition, change what's serialized and returned. Instead of
returning a subset of the serialized values, only serialize a subset
of the original values. This means n items are serialized instead of
max.

Signed-off-by: Stephen Finucane <stephen.finucane at intel.com>
---
 patchwork/tests/test_xmlrpc.py | 35 ++++++++++++++++++++++++++++-------
 patchwork/views/xmlrpc.py      | 10 +++++-----
 2 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/patchwork/tests/test_xmlrpc.py b/patchwork/tests/test_xmlrpc.py
index e70ed30..c6f19bf 100644
--- a/patchwork/tests/test_xmlrpc.py
+++ b/patchwork/tests/test_xmlrpc.py
@@ -17,6 +17,7 @@
 # along with Patchwork; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
+from email.utils import make_msgid
 import unittest
 
 from django.conf import settings
@@ -43,15 +44,35 @@ class XMLRPCTest(LiveServerTestCase):
         self.assertRedirects(response,
                              reverse('help', kwargs={'path': 'pwclient/'}))
 
-    def testList(self):
+    def _createPatches(self, count=1):
         defaults.project.save()
         defaults.patch_author_person.save()
-        patch = Patch(project=defaults.project,
-                      submitter=defaults.patch_author_person,
-                      msgid=defaults.patch_name,
-                      content=defaults.patch)
-        patch.save()
 
+        patches = []
+
+        for _ in range(0, count):
+            patch = Patch(project=defaults.project,
+                          submitter=defaults.patch_author_person,
+                          msgid=make_msgid(),
+                          content=defaults.patch)
+            patch.save()
+            patches.append(patch)
+
+        return patches
+
+    def testListSingle(self):
+        patch_objs = self._createPatches()
         patches = self.rpc.patch_list()
         self.assertEqual(len(patches), 1)
-        self.assertEqual(patches[0]['id'], patch.id)
+        self.assertEqual(patches[0]['id'], patch_objs[0].id)
+
+    def testListMultiple(self):
+        patch_objs = self._createPatches(5)
+        patches = self.rpc.patch_list()
+        self.assertEqual(len(patches), 5)
+
+    def testListMaxCount(self):
+        patch_objs = self._createPatches(5)
+        patches = self.rpc.patch_list({'max_count': 2})
+        self.assertEqual(len(patches), 2)
+        self.assertEqual(patches[0]['id'], patch_objs[0].id)
diff --git a/patchwork/views/xmlrpc.py b/patchwork/views/xmlrpc.py
index 28fd03b..86abd6d 100644
--- a/patchwork/views/xmlrpc.py
+++ b/patchwork/views/xmlrpc.py
@@ -406,7 +406,7 @@ def project_list(search_str=None, max_count=0):
             projects = Project.objects.all()
 
         if max_count > 0:
-            return map(project_to_dict, projects)[:max_count]
+            return list(map(project_to_dict, projects[:max_count]))
         else:
             return list(map(project_to_dict, projects))
     except Project.DoesNotExist:
@@ -458,7 +458,7 @@ def person_list(search_str=None, max_count=0):
             people = Person.objects.all()
 
         if max_count > 0:
-            return map(person_to_dict, people)[:max_count]
+            return list(map(person_to_dict, people[:max_count]))
         else:
             return list(map(person_to_dict, people))
     except Person.DoesNotExist:
@@ -601,9 +601,9 @@ def patch_list(filt=None):
         patches = Patch.objects.filter(**dfilter)
 
         if max_count > 0:
-            return [patch_to_dict(patch) for patch in patches[:max_count]]
+            return list(map(patch_to_dict, patches[:max_count]))
         else:
-            return [patch_to_dict(patch) for patch in patches]
+            return list(map(patch_to_dict, patches))
     except Patch.DoesNotExist:
         return []
 
@@ -794,7 +794,7 @@ def state_list(search_str=None, max_count=0):
             states = State.objects.all()
 
         if max_count > 0:
-            return map(state_to_dict, states)[:max_count]
+            return list(map(state_to_dict, states[:max_count]))
         else:
             return list(map(state_to_dict, states))
     except State.DoesNotExist:
-- 
2.0.0



More information about the Patchwork mailing list