Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Also call graphics.show() in doctest mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vbraun committed Sep 19, 2014
1 parent 1a2b1cf commit 48a2320
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/sage/plot/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,10 @@ def show(self, filename=None, linkmode=False, **kwds):
specify ``ticks`` manually, this safety measure can be defeated::
sage: list_plot_loglog([(1,2),(2,3)], plotjoined=True, ticks=[[1],[1]])
doctest:...: UserWarning: The x-axis contains fewer than 2 ticks;
the logarithmic scale of the plot may not be apparent to the reader.
doctest:...: UserWarning: The y-axis contains fewer than 2 ticks;
the logarithmic scale of the plot may not be apparent to the reader.
Graphics object consisting of 1 graphics primitive
This one works, since the horizontal axis is automatically expanded
Expand Down Expand Up @@ -2318,7 +2322,9 @@ def matplotlib(self, filename=None,
``typeset`` must not be set to an arbitrary string::
sage: plot(x, typeset='garbage')
Graphics object consisting of 1 graphics primitive
doctest:...: FormatterWarning: Exception in text/plain formatter:
typeset must be set to one of 'default', 'latex', or 'type1'; got 'garbage'.
None
We verify that numerical options are changed to float before saving (:trac:`14741`).
By default, Sage 5.10 changes float objects to the `RealLiteral` type.
Expand Down
2 changes: 1 addition & 1 deletion src/sage/plot/plot3d/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1882,7 +1882,7 @@ cdef class PrimitiveObject(Graphics3d):
sage: G = dodecahedron(color='red')
sage: G.texture_set()
{Texture(texture540, red, ff0000)}
{Texture(texture..., red, ff0000)}
"""
return set([self.texture])

Expand Down
47 changes: 47 additions & 0 deletions src/sage/repl/display/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,51 @@ def __call__(self, obj):
return stream.getvalue()


class SageDoctestTextFormatter(SagePlainTextFormatter):

@warn_format_error
def __call__(self, obj):
"""
Display ``obj``.
For doctests, we both
* Print the textual representation. This makes it clear in the
documentation that the command returns graphics.
* Run ``show()`` on graphics objects, to test that it
correctly generates graphics. Note that, in
``DOCTEST_MODE``, the ``show()`` method will save graphics
to temporary files but not launch a viewer.
INPUT:
- ``obj`` -- anything.
OUTPUT:
String. The plain text representation.
EXAMPLES::
sage: class FooGraphics(SageObject):
....: def _graphics_(self):
....: print('showing graphics')
....: return True
....: def _repr_(self):
....: return 'Textual representation'
sage: from sage.repl.display.formatter import SageDoctestTextFormatter
sage: fmt = SageDoctestTextFormatter()
sage: fmt(FooGraphics())
showing graphics
'Textual representation'
"""
from sage.structure.sage_object import SageObject
if isinstance(obj, SageObject) and hasattr(obj, '_graphics_'):
obj._graphics_() # ignore whether there actually is graphics
return super(SageDoctestTextFormatter, self).__call__(obj)


class SageConsoleTextFormatter(SagePlainTextFormatter):

@warn_format_error
Expand Down Expand Up @@ -266,6 +311,8 @@ def __call__(self, obj):
....: def _graphics_(self):
....: print('showing graphics')
....: return True
....: def _repr_(self):
....: return 'Textual representation'
sage: from sage.repl.display.formatter import SageConsoleTextFormatter
sage: fmt = SageConsoleTextFormatter()
sage: fmt(FooGraphics())
Expand Down
4 changes: 2 additions & 2 deletions src/sage/repl/display/python_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import __builtin__


from sage.repl.display.formatter import SagePlainTextFormatter
from sage.repl.display.formatter import SageDoctestTextFormatter


class DoctestDisplayHook(object):
Expand All @@ -38,7 +38,7 @@ def __init__(self):
sage: print(set([1, 2, 3])) # Plain Python output
set([1, 2, 3])
"""
self.formatter = SagePlainTextFormatter()
self.formatter = SageDoctestTextFormatter()

def __call__(self, obj):
"""
Expand Down

0 comments on commit 48a2320

Please sign in to comment.