Skip to content

Commit

Permalink
Silence UnicodeError on invalid UTF-8 .desktop files
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Nov 26, 2012
1 parent ac94e0e commit 7a6e107
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions test/resources.py
Expand Up @@ -59,6 +59,16 @@
Categories:Application:Game:ArcadeGame
"""

# Test with invalid UTF-8
gnome_alsamixer_desktop = """[Desktop Entry]
Name=GNOME ALSA Mixer
Comment=ALSA sound mixer for GNOME
Comment[es]=Mezclador de sonido ALSA para GNOME
Comment[fr]=Mélangeur de son ALSA pour GNOME
Exec=gnome-alsamixer
Type=Application
"""

recently_used = """<?xml version="1.0"?>
<RecentFiles>
<RecentItem>
Expand Down
8 changes: 8 additions & 0 deletions test/test-desktop.py
Expand Up @@ -84,3 +84,11 @@ def test_invalid(self):
f.write(resources.spout_desktop)

self.assertRaises(ParsingError, DesktopEntry, test_file)

def test_invalid_unicode(self):
test_file = os.path.join(self.tmpdir, "gnome-alsamixer.desktop")
with io.open(test_file, "w", encoding='latin-1') as f:
f.write(resources.gnome_alsamixer_desktop)

# Just check this doesn't throw a UnicodeError.
DesktopEntry(test_file)
5 changes: 4 additions & 1 deletion xdg/IniFile.py
Expand Up @@ -45,7 +45,10 @@ def parse(self, filename, headers=None):
raise ParsingError("File not found", filename)

try:
fd = io.open(filename, 'r', encoding='utf-8')
# The content should be UTF-8, but legacy files can have other
# encodings, including mixed encodings in one file. We don't attempt
# to decode them, but we silence the errors.
fd = io.open(filename, 'r', encoding='utf-8', errors='replace')
except IOError as e:
if debug:
raise e
Expand Down

0 comments on commit 7a6e107

Please sign in to comment.