| 1 | """ |
| 2 | Stopgaps |
| 3 | """ |
| 4 | |
| 5 | ######################################################################## |
| 6 | # Copyright (C) 2012 William Stein <wstein@gmail.com> |
| 7 | # |
| 8 | # Distributed under the terms of the GNU General Public License (GPL) |
| 9 | # |
| 10 | # http://www.gnu.org/licenses/ |
| 11 | ######################################################################## |
| 12 | |
| 13 | import warnings |
| 14 | from warnings import warn, resetwarnings |
| 15 | |
| 16 | from sage.plot.plot import DOCTEST_MODE |
| 17 | cdef bint ENABLED = not DOCTEST_MODE |
| 18 | |
| 19 | def set_state(bint mode): |
| 20 | """ |
| 21 | Enable or disable the stopgap warnings. |
| 22 | |
| 23 | INPUT: |
| 24 | |
| 25 | - ``mode`` -- (bool); if True, enable stopgaps; otherwise, disable. |
| 26 | |
| 27 | EXAMPLES:: |
| 28 | |
| 29 | sage: import sage.misc.stopgap |
| 30 | sage: sage.misc.stopgap.set_state(False) |
| 31 | sage: sage.misc.stopgap.stopgap("Displays nothing.", 12345) |
| 32 | sage: sage.misc.stopgap.set_state(True) |
| 33 | sage: sage.misc.stopgap.stopgap("Displays something.", 123456) |
| 34 | doctest:...: |
| 35 | ******************************************************************************** |
| 36 | Displays something. |
| 37 | This issue is being tracked at http://trac.sagemath.org/sage_trac/ticket/123456. |
| 38 | ******************************************************************************** |
| 39 | sage: sage.misc.stopgap.set_state(False) |
| 40 | """ |
| 41 | global ENABLED |
| 42 | ENABLED = mode |
| 43 | |
| 44 | class StopgapWarning(Warning): |
| 45 | """ |
| 46 | This class is used to warn users of a known issue that may produce |
| 47 | mathematically incorrect results. |
| 48 | """ |
| 49 | pass |
| 50 | |
| 51 | cdef set _stopgap_cache = set([]) |
| 52 | |
| 53 | def stopgap(message, int ticket_no): |
| 54 | r""" |
| 55 | Issue a stopgap warning. |
| 56 | |
| 57 | INPUT: |
| 58 | |
| 59 | - ``message`` - an explanation of how an incorrect answer might be produced. |
| 60 | |
| 61 | - ``ticket_no`` - an integer, giving the number of the Trac ticket tracking the underlying issue. |
| 62 | |
| 63 | EXAMPLES:: |
| 64 | |
| 65 | sage: import sage.misc.stopgap |
| 66 | sage: sage.misc.stopgap.set_state(True) |
| 67 | sage: sage.misc.stopgap.stopgap("Computation of heights on elliptic curves over number fields can return very imprecise results.", 12509) |
| 68 | doctest:...: |
| 69 | ******************************************************************************** |
| 70 | Computation of heights on elliptic curves over number fields can return very imprecise results. |
| 71 | This issue is being tracked at http://trac.sagemath.org/sage_trac/ticket/12509. |
| 72 | ******************************************************************************** |
| 73 | sage: sage.misc.stopgap.stopgap("This is not printed", 12509) |
| 74 | sage: sage.misc.stopgap.set_state(False) # so rest of doctesting fine |
| 75 | """ |
| 76 | if not ENABLED or ticket_no in _stopgap_cache: |
| 77 | return |
| 78 | # We reset show_warning so that the message is not printed twice. |
| 79 | old_format = warnings.formatwarning |
| 80 | def my_format(message, category, filename, lineno, line=None): |
| 81 | return "%s:%s:\n%s\n%s\n%s\n" % (filename, lineno, "*"*80, message, "*"*80) |
| 82 | warnings.formatwarning = my_format |
| 83 | resetwarnings() |
| 84 | message = message + "\nThis issue is being tracked at http://trac.sagemath.org/sage_trac/ticket/%s."%ticket_no |
| 85 | warn(StopgapWarning(message), stacklevel=2) |
| 86 | warnings.formatwarning = old_format |
| 87 | _stopgap_cache.add(ticket_no) |