Ticket #11577: trac_11577-jhp.v2.patch

File trac_11577-jhp.v2.patch, 3.0 KB (added by jhpalmieri, 8 years ago)

apply only this patch

  • sage/structure/sage_object.pyx

    # HG changeset patch
    # User Leif Leonhardy <not.really@online.de>
    # Date 1311460869 -7200
    # Node ID 306479782e73026d738d551c767f1bf01531144a
    # Parent  2a2abbcad325ccca9399981ceddf5897eb467e64
    #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 --git a/sage/structure/sage_object.pyx b/sage/structure/sage_object.pyx
    a b def load(*filename, compress=True, verbo 
    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 one, or if ``obj``
     792    doesn't have its own ``save()`` method, like e.g. Python tuples.
     793
     794    For image objects and the like (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   
    def save(obj, filename=None, compress=Tr 
    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')
    def save(obj, filename=None, compress=Tr 
    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] == ''
     841        or (os.path.splitext(filename)[1] != '.sobj'
     842             and not hasattr(obj,"save"))):
    817843        filename += '.sobj'
    818844
    819845    if filename.endswith('.sobj'):