Changeset 11770:bdf2527fe917 in orange


Ignore:
Timestamp:
11/26/13 12:02:48 (5 months ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Do not truncate an existing file in case of an error in 'save_scheme'.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/OrangeCanvas/application/canvasmain.py

    r11760 r11770  
    88import operator 
    99from functools import partial 
     10from StringIO import StringIO 
    1011 
    1112import pkg_resources 
     
    11611162        self.last_scheme_dir = dirname 
    11621163        title = scheme.title or "untitled" 
     1164 
     1165        # First write the scheme to a buffer so we don't truncate an 
     1166        # existing scheme file if `scheme.save_to` raises an error. 
     1167        buffer = StringIO() 
    11631168        try: 
    1164             scheme.save_to(open(filename, "wb"), 
    1165                            pretty=True, pickle_fallback=True) 
     1169            scheme.save_to(buffer, pretty=True, pickle_fallback=True) 
     1170        except Exception: 
     1171            log.error("Error saving %r to %r", scheme, filename, exc_info=True) 
     1172            message_critical( 
     1173                self.tr('An error occurred while trying to save scheme ' 
     1174                        '"%s" to "%s"') % (title, basename), 
     1175                title=self.tr("Error saving %s") % basename, 
     1176                exc_info=True, 
     1177                parent=self 
     1178            ) 
     1179            return False 
     1180 
     1181        try: 
     1182            with open(filename, "wb") as f: 
     1183                f.write(buffer.getvalue()) 
    11661184            return True 
    11671185        except (IOError, OSError) as ex: 
Note: See TracChangeset for help on using the changeset viewer.