Ticket #12691: trac_12691-faster.patch

File trac_12691-faster.patch, 4.0 KB (added by was, 8 years ago)
  • module_list.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1332194832 25200
    # Node ID 553aff64b9852ee0fc60c057005dcc1d737b3276
    # Parent  a890fb30a94d4d668452bb31a56378982f8d13c7
    Trac #12691: Create a stopgap warning (based on code by David Roe)
    
    diff --git a/module_list.py b/module_list.py
    a b  
    10871087    Extension('sage.misc.session',
    10881088              sources = ['sage/misc/session.pyx']),
    10891089
     1090    Extension('sage.misc.stopgap',
     1091              sources = ['sage/misc/stopgap.pyx']),
     1092
    10901093    ################################
    10911094    ##
    10921095    ## sage.modular
  • new file sage/misc/stopgap.pyx

    diff --git a/sage/misc/stopgap.pyx b/sage/misc/stopgap.pyx
    new file mode 100644
    - +  
     1"""
     2Stopgaps
     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
     13import warnings
     14from warnings import warn, resetwarnings
     15
     16from sage.plot.plot import DOCTEST_MODE
     17cdef bint ENABLED = not DOCTEST_MODE
     18
     19def 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
     44class 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
     51cdef set _stopgap_cache = set([])
     52
     53def 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)