From 2b21735058e33356f3755a3c2ab3f0b14e908e01 Mon Sep 17 00:00:00 2001 From: Thomas Jansen Date: Sun, 4 Oct 2009 10:55:56 +0200 Subject: [PATCH] leoslyrics plugin: handle parser exceptions Should fix bug #2286. Added exception handling for both calls of the SAX XML parser. Also removed trailing newlines from the result to keep us from scrolling down further than the last line. --- lyrics/03-leoslyrics.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lyrics/03-leoslyrics.py b/lyrics/03-leoslyrics.py index b55f0ec..2a40f37 100755 --- a/lyrics/03-leoslyrics.py +++ b/lyrics/03-leoslyrics.py @@ -21,7 +21,7 @@ # Load lyrics from leoslyrics.com # -from sys import argv, exit +from sys import argv, exit, stderr from urllib import urlencode, urlopen from xml.sax import make_parser, SAXException from xml.sax.handler import ContentHandler @@ -47,7 +47,11 @@ def search(artist, title): handler = SearchContentHandler() parser = make_parser() parser.setContentHandler(handler) - parser.parse(f) + try: + parser.parse(f) + except SAXException: + stderr.write("Failed to parse the search result!\n") + exit(1) return handler.hid class LyricsContentHandler(ContentHandler): @@ -75,10 +79,14 @@ def lyrics(hid): handler = LyricsContentHandler() parser = make_parser() parser.setContentHandler(handler) - parser.parse(f) + try: + parser.parse(f) + except SAXException: + stderr.write("Failed to parse the lyrics!\n") + exit(1) return handler.text hid = search(argv[1], argv[2]) if hid is None: exit(69) -print lyrics(hid).encode('utf-8') +print lyrics(hid).encode('utf-8').rstrip() -- 2.30.2