jenkins-bot has submitted this change and it was merged.

Change subject: Allow ThreadGenerator param target to be iterable
......................................................................


Allow ThreadGenerator param target to be iterable

Detech if the param 'target' is iterable, and use it without
calling it first.

Included also basic tests.

Change-Id: I02152fdbc8e32f4677fb839f18282350034a329e
---
M pywikibot/tools.py
A tests/thread_tests.py
2 files changed, 42 insertions(+), 1 deletion(-)

Approvals:
  John Vandenberg: Looks good to me, but someone else must approve
  Mpaa: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/tools.py b/pywikibot/tools.py
index 6d26e71..c891c83 100644
--- a/pywikibot/tools.py
+++ b/pywikibot/tools.py
@@ -187,7 +187,12 @@
 
     def run(self):
         """Run the generator and store the results on the queue."""
-        self.__gen = self.generator(*self.args, **self.kwargs)
+        iterable = any([hasattr(self.generator, key)
+                        for key in ['__iter__', '__getitem__']])
+        if iterable and not self.args and not self.kwargs:
+            self.__gen = self.generator
+        else:
+            self.__gen = self.generator(*self.args, **self.kwargs)
         for result in self.__gen:
             while True:
                 if self.finished.isSet():
diff --git a/tests/thread_tests.py b/tests/thread_tests.py
new file mode 100644
index 0000000..3d86632
--- /dev/null
+++ b/tests/thread_tests.py
@@ -0,0 +1,36 @@
+# -*- coding: utf-8  -*-
+"""Tests for threading tools."""
+#
+# (C) Pywikibot team, 2014
+#
+# Distributed under the terms of the MIT license.
+#
+__version__ = '$Id$'
+
+
+from tests.aspects import TestCase
+from pywikibot.tools import ThreadedGenerator
+
+
+class BasicThreadedGeneratorTestCase(TestCase):
+
+    """ThreadedGenerator test cases."""
+
+    net = False
+
+    def test_run_from_iterable(self):
+        iterable = 'abcd'
+        thd_gen = ThreadedGenerator(target=iterable)
+        thd_gen.start()
+        self.assertEqual(list(thd_gen), list(iterable))
+
+    def gen_func(self):
+        iterable = 'abcd'
+        for i in iterable:
+            yield i
+
+    def test_run_from_gen_function(self):
+        iterable = 'abcd'
+        thd_gen = ThreadedGenerator(target=self.gen_func)
+        thd_gen.start()
+        self.assertEqual(list(thd_gen), list(iterable))

-- 
To view, visit https://gerrit.wikimedia.org/r/170885
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I02152fdbc8e32f4677fb839f18282350034a329e
Gerrit-PatchSet: 4
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Mpaa <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to