Ticket #11577: trac_11577-fix_save_to_filenames_with_dots.patch

File trac_11577-fix_save_to_filenames_with_dots.patch, 2.8 KB (added by leif, 8 years ago)

Sage library patch. Adds ".sobj" to filename if the object has no save() method. (Corrected and improved version.) Based on Sage 4.7.1.rc0.

  • sage/structure/sage_object.pyx

    # HG changeset patch
    # User Leif Leonhardy <not.really@online.de>
    # Date 1311460869 -7200
    # Node ID bc20c32a00f85b3b6c1d510b71d40dc5f8eb1cf2
    # Parent  99045c72f54cc6d141685bb51c04b8a08edf99c4
    #11577 Fix save() to filenames with dots in it for objects that don't have a save() method.
    
    Also the docstring now clarifies the behaviour w.r.t. filename extensions.
    
    diff -r 99045c72f54c -r bc20c32a00f8 sage/structure/sage_object.pyx
    a b  
    786786
    787787
    788788def save(obj, filename=None, compress=True, **kwds):
    789     """   
    790     Save obj to the file with name filename, which will
    791     have an .sobj extension added if it doesn't have one.
    792     This will *replace* the contents of filename.
     789    """
     790    Save ``obj`` to the file with name ``filename``, which will have an
     791    ``.sobj`` extension added if it doesn't have some, or ``obj`` doesn't
     792    have its own ``save()`` method, like e.g. Python tuples.
     793
     794    For image objects and alike (which have their own ``save()`` method),
     795    you may have to specify a specific extension, e.g. ``.png``, if you
     796    don't want the object to be saved as a Sage object (or likewise, if
     797    ``filename`` could be interpreted as already having some extension).
     798
     799    .. warning::
     800
     801       This will *replace* the contents of the file if it already exists.
    793802
    794803    EXAMPLES::
    795804   
     
    802811        [   3 -5/2]
    803812        sage: E = EllipticCurve([-1,0])
    804813        sage: P = plot(E)
    805         sage: save(P, objfile_short)
     814        sage: save(P, objfile_short)   # saves the plot to "test.sobj"
    806815        sage: save(P, filename=SAGE_TMP + "sage.png", xmin=-2)
     816        sage: save(P, SAGE_TMP + "filename.with.some.wrong.ext")
     817        Traceback (most recent call last):
     818        ...
     819        ValueError: allowed file extensions for images are '.eps', '.pdf', '.png', '.ps', '.sobj', '.svg'!
    807820        sage: print load(objfile)
    808821        Graphics object consisting of 2 graphics primitives
    809822        sage: save("A python string", SAGE_TMP + 'test')
     
    811824        'A python string'
    812825        sage: load(objfile_short)
    813826        'A python string'
     827   
     828    TESTS:
     829
     830    Check that #11577 is fixed::
     831
     832        sage: filename = SAGE_TMP + "foo.bar" # filename containing a dot
     833        sage: save((1,1),filename)            # saves tuple to "foo.bar.sobj"
     834        sage: load(filename)
     835        (1, 1)
    814836    """
    815837    # Add '.sobj' if the filename currently has no extension
    816     if os.path.splitext(filename)[1] == '':
     838    # and also if the object doesn't have its own save() method (#11577)
     839    # since we otherwise assume below it is an image object:
     840    if os.path.splitext(filename)[1] == ''  or not hasattr(obj,"save"):
    817841        filename += '.sobj'
    818842
    819843    if filename.endswith('.sobj'):