Skip to content

Commit e8518a5

Browse files
author
Release Manager
committed
sagemathgh-39481: Fixed issue in list_plot where it assumed data had been enumerated when it might not have been <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> Fixes sagemath#29960. Fixes issue where data was assumed to be enumerated but there are cases where it may not be. If the data was a list of complex numbers it was previously assumed that the data at the very end was guaranteed to be enumerated as it would enumerate the data in a previous catch block. However if the first element of the list is not a real number the list is not enumerated as the catch block fails before it enumerates anything. This was allowing weird errors as seen in the original bug fix request. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [X] The title is concise and informative. - [X] The description explains in detail what this PR is about. - [X] I have linked a relevant issue or discussion. - [X] I have created tests covering the changes. - [X] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies URL: sagemath#39481 Reported by: Noel-Roemmele Reviewer(s):
2 parents 5ec250a + 33316d1 commit e8518a5

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/sage/plot/plot.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3107,6 +3107,24 @@ def list_plot(data, plotjoined=False, **kwargs):
31073107
100.0
31083108
sage: d['ymin']
31093109
100.0
3110+
3111+
Verify that :issue:`38037` is fixed::
3112+
3113+
sage: list_plot([(0,-1),(1,-2),(2,-3),(3,-4),(4,None)])
3114+
Traceback (most recent call last):
3115+
...
3116+
TypeError: unable to coerce to a ComplexNumber:
3117+
<class 'sage.rings.integer.Integer'>
3118+
3119+
Test the codepath where ``list_enumerated`` is ``False``::
3120+
3121+
sage: list_plot([3+I, 4, I, 1+5*i, None, 1+i])
3122+
Graphics object consisting of 1 graphics primitive
3123+
3124+
Test the codepath where ``list_enumerated`` is ``True``::
3125+
3126+
sage: list_plot([4, 3+I, I, 1+5*i, None, 1+i])
3127+
Graphics object consisting of 1 graphics primitive
31103128
"""
31113129
from sage.plot.all import point
31123130
try:
@@ -3124,10 +3142,12 @@ def list_plot(data, plotjoined=False, **kwargs):
31243142
else:
31253143
list_data = list(data.items())
31263144
return list_plot(list_data, plotjoined=plotjoined, **kwargs)
3145+
list_enumerated = False
31273146
try:
31283147
from sage.rings.real_double import RDF
31293148
RDF(data[0])
31303149
data = list(enumerate(data))
3150+
list_enumerated = True
31313151
except TypeError: # we can get this TypeError if the element is a list
31323152
# or tuple or numpy array, or an element of CC, CDF
31333153
# We also want to avoid doing CC(data[0]) here since it will go
@@ -3138,6 +3158,7 @@ def list_plot(data, plotjoined=False, **kwargs):
31383158
# element of the Symbolic Ring.
31393159
if isinstance(data[0], Expression):
31403160
data = list(enumerate(data))
3161+
list_enumerated = True
31413162

31423163
try:
31433164
if plotjoined:
@@ -3150,9 +3171,11 @@ def list_plot(data, plotjoined=False, **kwargs):
31503171
# point3d() throws an IndexError on the (0,1) before it ever
31513172
# gets to (1, I).
31523173
from sage.rings.cc import CC
3153-
# if we get here, we already did "list(enumerate(data))",
3154-
# so look at z[1] in inner list
3155-
data = [(z.real(), z.imag()) for z in [CC(z[1]) for z in data]]
3174+
# It is not guaranteed that we enumerated the data so we have two cases
3175+
if list_enumerated:
3176+
data = [(z.real(), z.imag()) for z in [CC(z[1]) for z in data]]
3177+
else:
3178+
data = [(z.real(), z.imag()) for z in [CC(z) for z in data]]
31563179
if plotjoined:
31573180
return line(data, **kwargs)
31583181
else:

0 commit comments

Comments
 (0)