sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.38k stars 469 forks source link

py3: an issue about dict iterators #24839

Closed fchapoton closed 6 years ago

fchapoton commented 6 years ago

some sums complain when using python3

File "src/sage/combinat/crystals/multisegments.py", line 432, in sage.combinat.crystals.multisegments.InfinityCrystalOfMultisegments.Element.phi
Failed example:
    mg.f(1).phi(0)
Exception raised:
    Traceback (most recent call last):
      File "/home/chapoton/sage3/local/lib/python3.6/site-packages/sage/doctest/forker.py", line 557, in _run
        self.compile_and_execute(example, compiler, test.globs)
      File "/home/chapoton/sage3/local/lib/python3.6/site-packages/sage/doctest/forker.py", line 967, in compile_and_execute
        exec(compiled, globs)
      File "<doctest sage.combinat.crystals.multisegments.InfinityCrystalOfMultisegments.Element.phi[5]>", line 1, in <module>
        mg.f(Integer(1)).phi(Integer(0))
      File "/home/chapoton/sage3/local/lib/python3.6/site-packages/sage/combinat/crystals/multisegments.py", line 436, in phi
        return self.epsilon(i) + self.weight().scalar(h[i])
      File "/home/chapoton/sage3/local/lib/python3.6/site-packages/sage/combinat/root_system/weight_space.py", line 506, in scalar
        return BR.sum(self[i] * c for (i, c) in lambdacheck)
    TypeError: iter() returned non-iterator of type 'dict_items'

CC: @jdemeyer @embray

Component: python3

Author: Frédéric Chapoton

Branch/Commit: bc7a60c

Reviewer: Erik Bray

Issue created by migration from https://trac.sagemath.org/ticket/24839

fchapoton commented 6 years ago

New commits:

52eee77trac 24839 changing sums in weight_space
fchapoton commented 6 years ago

Commit: 52eee77

fchapoton commented 6 years ago

Branch: u/chapoton/24839

fchapoton commented 6 years ago
comment:2

this is not fixing the issue..

fchapoton commented 6 years ago
comment:3
fchapoton commented 6 years ago

Description changed:

--- 
+++ 
@@ -1 +1,20 @@
-changing some sums that cause issue when using python3
+some sums complain when using python3
+
+```
+File "src/sage/combinat/crystals/multisegments.py", line 432, in sage.combinat.crystals.multisegments.InfinityCrystalOfMultisegments.Element.phi
+Failed example:
+    mg.f(1).phi(0)
+Exception raised:
+    Traceback (most recent call last):
+      File "/home/chapoton/sage3/local/lib/python3.6/site-packages/sage/doctest/forker.py", line 557, in _run
+        self.compile_and_execute(example, compiler, test.globs)
+      File "/home/chapoton/sage3/local/lib/python3.6/site-packages/sage/doctest/forker.py", line 967, in compile_and_execute
+        exec(compiled, globs)
+      File "<doctest sage.combinat.crystals.multisegments.InfinityCrystalOfMultisegments.Element.phi[5]>", line 1, in <module>
+        mg.f(Integer(1)).phi(Integer(0))
+      File "/home/chapoton/sage3/local/lib/python3.6/site-packages/sage/combinat/crystals/multisegments.py", line 436, in phi
+        return self.epsilon(i) + self.weight().scalar(h[i])
+      File "/home/chapoton/sage3/local/lib/python3.6/site-packages/sage/combinat/root_system/weight_space.py", line 506, in scalar
+        return BR.sum(self[i] * c for (i, c) in lambdacheck)
+    TypeError: iter() returned non-iterator of type 'dict_items'
+```
fchapoton commented 6 years ago
comment:4

Similar issue here under python3:

sage: d = DisjointSet(4)
sage: list(d)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-d0c0da2e41ad> in <module>()
----> 1 list(d)

TypeError: iter() returned non-iterator of type 'dict_values'

where the code is

return (<dict?>self.root_to_elements_dict()).itervalues()

that under python2 returns

sage: d = DisjointSet(4)
sage: iter(d)
<dictionary-valueiterator object at 0x7f0306d3f578>
jdemeyer commented 6 years ago
comment:7

Sigh... I guess it has to be iter(dict.itervalues()) or yield from dict.itervalues() but I guess that the former will be faster.

fchapoton commented 6 years ago
comment:8

Eurk. Ugly enough, indeed. No hope that Cython could do that for us ?

How to find all instances of this problem ? Maybe by looking for pyx files containing both __iter__ and .iter[a-z] ?

fchapoton commented 6 years ago
comment:9

Potential list

crypto/boolean_function.pyx (✓)
libs/gap/element.pyx
algebras/letterplace/free_algebra_element_letterplace.pyx
algebras/lie_algebras/lie_algebra_element.pyx
plot/plot3d/index_face_set.pyx
ext/fast_eval.pyx
numerical/linear_functions.pyx
misc/weak_dict.pyx
rings/polynomial/polydict.pyx
rings/polynomial/laurent_polynomial.pyx
rings/polynomial/pbori.pyx
rings/polynomial/multi_polynomial.pyx
rings/polynomial/polynomial_element.pyx
rings/power_series_mpoly.pyx
sets/disjoint_set.pyx (todo)
quivers/algebra_elements.pyx (✓)
modules/with_basis/indexed_element.pyx
symbolic/expression.pyx

found using

grep -l "def.*__iter__" $(grep -l -R "\.iter[ivk]" --include=*.pyx)
jdemeyer commented 6 years ago
comment:10

There is no need to fix everything on one ticket. In any case, doctests will show what is broken.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 6 years ago

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

bc7a60cpy3: fixing some iterators
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 6 years ago

Changed commit from 52eee77 to bc7a60c

fchapoton commented 6 years ago
comment:12

ok, let us content ourselves here with 3 changes

fchapoton commented 6 years ago
comment:13

bot is green

fchapoton commented 6 years ago
comment:14

ping ?

embray commented 6 years ago

Reviewer: Erik Bray

vbraun commented 6 years ago

Changed branch from u/chapoton/24839 to bc7a60c