Ticket #10229: trac_10229-2.patch

File trac_10229-2.patch, 4.1 KB (added by ltw, 10 years ago)

fixed modified_attached_files()

  • sage/misc/preparser.py

    # HG changeset patch
    # User Kelvin Li <ltwisabc@gmail.com>
    # Date 1330037244 28800
    # Node ID d3b10b221878d8aab8492f1249703dd8e4b0c09f
    # Parent  c9ec4d501534074ccf2ef0a6f3a203bad4f78f51
    #10229: Fix handling of deleted/moved attached files.
    
    diff --git a/sage/misc/preparser.py b/sage/misc/preparser.py
    a b  
    213213#  Distributed under the terms of the GNU General Public License (GPL)
    214214#                  http://www.gnu.org/licenses/
    215215###########################################################################
    216 import os, re
     216import os, re, warnings
    217217
    218218implicit_mul_level = False
    219219numeric_literal_prefix = '_sage_const_'
     
    11831183    assert isinstance(contents, str)
    11841184
    11851185    # Reload attached files that have changed
    1186     for F, tm in attached.iteritems():
    1187         new_tm = os.path.getmtime(F)
    1188         if os.path.exists(F) and new_tm > tm:
    1189             contents = 'attach "%s"\n'%F + contents
     1186    for fpath in modified_attached_files():
     1187        contents = ('attach "%s"\n' % fpath) + contents
    11901188   
    11911189    # We keep track of which files have been loaded so far
    11921190    # in order to avoid a recursive load that would result
     
    18171815    return 'sage.misc.preparser.load(sage.misc.preparser.base64.b64decode("%s"),globals(),%s)'%(
    18181816        base64.b64encode(filename), attach)
    18191817
    1820 
    18211818def modified_attached_files():
    18221819    """
    1823     Returns an iterator over the names of the attached files that have
    1824     changed since last time this function was called.
     1820    Returns list of names of the attached files that need to be reloaded
     1821    since the last invocation of this function. Detaches any
     1822    inaccessible files and issues a warning for each.
    18251823
    18261824    OUTPUT:
    18271825
    1828     - an iterator over strings
     1826    - a list of strings
    18291827
    18301828    EXAMPLES::
    1831    
     1829
     1830        sage: from sage.misc.preparser import modified_attached_files
    18321831        sage: sage.misc.reset.reset_attached()
    1833         sage: t=tmp_filename()+'.py';
     1832        sage: t = tmp_filename() + '.py';
    18341833        sage: a = 0
    1835         sage: f = open(t,'w'); f.write("a = 5"); f.close()
     1834        sage: with open(t, 'w') as f:
     1835        ...       f.write("a = 5")
    18361836        sage: attach(t)
    18371837        sage: a
    18381838        5
    1839         sage: len(list(sage.misc.preparser.modified_attached_files()))
    1840         0
    1841         sage: import time; time.sleep(2)
    1842         sage: f = open(t,'w'); f.write("a = 10"); f.close()
    1843         sage: len(list(sage.misc.preparser.modified_attached_files()))
    1844         1
    1845         sage: len(list(sage.misc.preparser.modified_attached_files()))
    1846         0
     1839        sage: modified_attached_files()
     1840        []
     1841        sage: with open(t, 'w') as f:
     1842        ...       f.write("a = 10")
     1843        sage: modified_attached_files()
     1844        ['/....py']
     1845        sage: modified_attached_files()
     1846        []
     1847        sage: attach(t)
     1848        sage: a
     1849        10
     1850
     1851    Test that the bug reported in ticket #10229 is fixed::
     1852
     1853        sage: os.unlink(t)
     1854        sage: modified_attached_files()
     1855        doctest... Warning: Detached file ....py
     1856        []
     1857        sage: attached_files()
     1858        []
    18471859    """
    1848     # A generator is the perfect data structure here, since something
    1849     # could go wrong loading one file, and we end up only marking the
    1850     # ones that we returned as loaded.
    1851     for F in attached.keys():
    1852         tm = attached[F]
    1853         new_tm = os.path.getmtime(F)
    1854         if os.path.exists(F) and new_tm > tm:
    1855             # F is newer than last time we loaded it.
    1856             attached[F] = os.path.getmtime(F)
    1857             yield F
    1858    
     1860    global attached
     1861    new_attached = {}
     1862    for fpath, stamp in attached.iteritems():
     1863        try:
     1864            new_stamp = os.path.getmtime(fpath)
     1865        except OSError:
     1866            warnings.warn("Detached file %s" % fpath, Warning)
     1867        else:
     1868            new_attached[fpath] = new_stamp
     1869
     1870    modified_files = [fpath
     1871                      for fpath, new_stamp
     1872                      in new_attached.iteritems()
     1873                      if new_stamp > attached[fpath]]
     1874
     1875    attached = new_attached
     1876    return modified_files
     1877
    18591878def attached_files():
    18601879    """
    18611880    Returns a list of all files attached to the current session.