Skip to content

Commit c91654d

Browse files
committed
filediff: Replace broken pre-save encoding check (#239)
See c7bbe78 for the removal reasoning. Here we're just giving the user a somewhat better error message. Ideally we'd replicate the previous offer of "hey why don't you save as UTF-8 instead?", but the save lifecycle makes this difficult here. If this was an often-hit check then we'd do it, but... this code hasn't worked in over three years and people have only ever complained about bugs in the encoding check, not the save-as-UTF-8 bit.
1 parent 9eba8e2 commit c91654d

1 file changed

Lines changed: 12 additions & 34 deletions

File tree

meld/filediff.py

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,38 +1581,6 @@ def on_file_changed_response(msgarea, response_id, *args):
15811581
msgarea.show_all()
15821582
return False
15831583

1584-
start, end = buf.get_bounds()
1585-
text = buf.get_text(start, end, False)
1586-
1587-
source_encoding = bufdata.sourcefile.get_encoding()
1588-
if not source_encoding:
1589-
# no encoding for new blank comparison
1590-
source_encoding = GtkSource.Encoding.get_utf8()
1591-
1592-
while isinstance(text, str):
1593-
try:
1594-
encoding = source_encoding.get_charset()
1595-
text = text.encode(encoding)
1596-
except UnicodeEncodeError:
1597-
dialog_buttons = [
1598-
(_("_Cancel"), Gtk.ResponseType.CANCEL),
1599-
(_("_Save as UTF-8"), Gtk.ResponseType.OK),
1600-
]
1601-
reencode = misc.modal_dialog(
1602-
primary=_("Couldn’t encode text as “%s”") % encoding,
1603-
secondary=_(
1604-
"File “%s” contains characters that can’t be encoded "
1605-
"using encoding “%s”.\n"
1606-
"Would you like to save as UTF-8?") % (
1607-
bufdata.label, encoding),
1608-
buttons=dialog_buttons,
1609-
messagetype=Gtk.MessageType.WARNING
1610-
)
1611-
if reencode != Gtk.ResponseType.OK:
1612-
return False
1613-
1614-
source_encoding = GtkSource.Encoding.get_utf8()
1615-
16161584
saver = GtkSource.FileSaver.new_with_target(
16171585
self.textbuffer[pane], bufdata.sourcefile, bufdata.gfiletarget)
16181586
# TODO: Think about removing this flag and above handling, and instead
@@ -1640,10 +1608,20 @@ def file_saved_cb(self, saver, result, user_data):
16401608
# or invalid buffer characters.
16411609
filename = GLib.markup_escape_text(
16421610
gfile.get_parse_name())
1611+
1612+
if err.matches(Gio.io_error_quark(), Gio.IOErrorEnum.INVALID_DATA):
1613+
encoding = saver.get_file().get_encoding()
1614+
secondary = _(
1615+
"File “{}” contains characters that can’t be encoded "
1616+
"using its current encoding “{}”."
1617+
).format(filename, encoding.to_string())
1618+
else:
1619+
secondary = _("Couldn’t save file due to:\n%s") % (
1620+
GLib.markup_escape_text(str(err)))
1621+
16431622
misc.error_dialog(
16441623
primary=_("Could not save file %s.") % filename,
1645-
secondary=_("Couldn’t save file due to:\n%s") % (
1646-
GLib.markup_escape_text(str(err))),
1624+
secondary=secondary,
16471625
)
16481626
self.state = ComparisonState.SavingError
16491627
return

0 commit comments

Comments
 (0)