|
22 | 22 |
|
23 | 23 | logger = logging.getLogger(__name__) |
24 | 24 |
|
| 25 | +def _encapsulate_item_for_combinfunc(item): |
| 26 | + """ |
| 27 | + This function has been extracted in order to |
| 28 | + make Github issue #28 easier to investigate. |
| 29 | + It replaces the following two lines of code, |
| 30 | + which occur twice in method genmatrix, just |
| 31 | + before the invocation of combinfunc. |
| 32 | + if not hasattr(item, '__iter__') or isinstance(item, tuple): |
| 33 | + item = [item] |
| 34 | + Logging was added to the original two lines |
| 35 | + and shows that the outcome of this snippet |
| 36 | + has changed between Python2.7 and Python3.5. |
| 37 | + This logging showed that the difference in |
| 38 | + outcome consisted of the handling of the builtin |
| 39 | + str class, which was encapsulated into a list in |
| 40 | + Python2.7 but returned naked in Python3.5. |
| 41 | + Adding a test for this specific class to the |
| 42 | + set of conditions appears to give correct behaviour |
| 43 | + under both versions. |
| 44 | + """ |
| 45 | + encapsulated_item = None |
| 46 | + if ( |
| 47 | + not hasattr(item, '__iter__') or |
| 48 | + isinstance(item, tuple) or |
| 49 | + isinstance(item, str) |
| 50 | + ): |
| 51 | + encapsulated_item = [item] |
| 52 | + else: |
| 53 | + encapsulated_item = item |
| 54 | + logging.debug( |
| 55 | + "item class:%s encapsulated as:%s ", |
| 56 | + item.__class__.__name__, |
| 57 | + encapsulated_item.__class__.__name__ |
| 58 | + ) |
| 59 | + return encapsulated_item |
| 60 | + |
25 | 61 |
|
26 | 62 | class Matrix(object): |
27 | 63 | """ |
@@ -123,10 +159,17 @@ def genmatrix(self, num_processes=1): |
123 | 159 | num_tasks_completed += 1 |
124 | 160 | else: |
125 | 161 | # Otherwise do it here, in line |
| 162 | + """ |
126 | 163 | if not hasattr(item, '__iter__') or isinstance(item, tuple): |
127 | 164 | item = [item] |
128 | 165 | if not hasattr(item2, '__iter__') or isinstance(item2, tuple): |
129 | 166 | item2 = [item2] |
| 167 | + """ |
| 168 | + # See the comment in function _encapsulate_item_for_combinfunc |
| 169 | + # for details of why the lines above have been replaced |
| 170 | + # by function invocations |
| 171 | + item = _encapsulate_item_for_combinfunc(item) |
| 172 | + item2 = _encapsulate_item_for_combinfunc(item2) |
130 | 173 | row[col_index] = self.combinfunc(item, item2) |
131 | 174 |
|
132 | 175 | if self.symmetric: |
|
0 commit comments