From 885a547d34360f91b8f5b5cb277c79600a7e1e64 Mon Sep 17 00:00:00 2001 From: Andreas Ruppen Date: Tue, 22 Nov 2016 21:11:16 +0100 Subject: [PATCH] Handle UnicodeEncodeError in Error messages. Depending on the input, I might be that instead of collecting the thrown error message, python throws a new UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 96: ordinal not in range(128). So we use the six library and unicode function to properly treat the error message before adding it to the stack of already collected error messages. --- CHANGES.rst | 2 +- docs/credits.rst | 1 + src/icalendar/cal.py | 6 ++- src/icalendar/tests/apple_xlocation_test.py | 25 +++++++++++ src/icalendar/tests/x_location.ics | 48 +++++++++++++++++++++ 5 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/icalendar/tests/apple_xlocation_test.py create mode 100644 src/icalendar/tests/x_location.ics diff --git a/CHANGES.rst b/CHANGES.rst index c8a90f6..d8f8680 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -14,7 +14,7 @@ New features: Bug fixes: -- *add item here* +- Encode error message before adding it to the stack of collected error messages. 3.11 (2016-11-18) diff --git a/docs/credits.rst b/docs/credits.rst index 21e34b2..8e73d99 100644 --- a/docs/credits.rst +++ b/docs/credits.rst @@ -51,6 +51,7 @@ icalendar contributors - tgecho - tisto - TomTry +- Andreas Ruppen Find out who contributed:: diff --git a/src/icalendar/cal.py b/src/icalendar/cal.py index 9c854eb..9ace041 100644 --- a/src/icalendar/cal.py +++ b/src/icalendar/cal.py @@ -20,6 +20,8 @@ import pytz import dateutil.rrule from pytz.tzinfo import DstTzInfo +from icalendar.compat import unicode_type + ###################################### # The component factory @@ -333,7 +335,7 @@ class Component(CaselessDict): component = stack[-1] if stack else None if not component or not component.ignore_exceptions: raise - component.errors.append((None, str(e))) + component.errors.append((None, unicode_type(e))) continue uname = name.upper() @@ -382,7 +384,7 @@ class Component(CaselessDict): except ValueError as e: if not component.ignore_exceptions: raise - component.errors.append((uname, str(e))) + component.errors.append((uname, unicode_type(e))) component.add(name, None, encode=0) else: vals.params = params diff --git a/src/icalendar/tests/apple_xlocation_test.py b/src/icalendar/tests/apple_xlocation_test.py new file mode 100644 index 0000000..a58e510 --- /dev/null +++ b/src/icalendar/tests/apple_xlocation_test.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +from icalendar.tests import unittest + +import datetime +import icalendar +import os +import pytz + +class TestEncoding(unittest.TestCase): + + def test_apple_xlocation(self): + """ + Test if error messages are encode properly. + """ + try: + directory = os.path.dirname(__file__) + data = open(os.path.join(directory, 'x_location.ics'), 'rb').read() + cal = icalendar.Calendar.from_ical(data) + for event in cal.walk('vevent'): + self.assertEqual(len(event.errors), 1, 'Got too many errors') + error = event.errors[0][1] + self.assertTrue(error.startswith(u'Content line could not be parsed into parts')) + + except UnicodeEncodeError as e: + self.fail("There is something wrong with encoding in the collected error messages") diff --git a/src/icalendar/tests/x_location.ics b/src/icalendar/tests/x_location.ics new file mode 100644 index 0000000..97ffbe7 --- /dev/null +++ b/src/icalendar/tests/x_location.ics @@ -0,0 +1,48 @@ +BEGIN:VCALENDAR +PRODID:-//Google Inc//Google Calendar 70.9054//EN +VERSION:2.0 +CALSCALE:GREGORIAN +METHOD:PUBLISH +X-WR-CALNAME:ITC +X-WR-TIMEZONE:Europe/Zurich +X-WR-CALDESC:ITC Bookings +BEGIN:VTIMEZONE +TZID:Europe/Zurich +X-LIC-LOCATION:Europe/Zurich +BEGIN:DAYLIGHT +TZOFFSETFROM:+0100 +TZOFFSETTO:+0200 +TZNAME:CEST +DTSTART:19700329T020000 +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETFROM:+0200 +TZOFFSETTO:+0100 +TZNAME:CET +DTSTART:19701025T030000 +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU +END:STANDARD +END:VTIMEZONE +BEGIN:VEVENT +DTSTART;TZID=Europe/Zurich:20161028T140000 +DTEND;TZID=Europe/Zurich:20161028T143000 +RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR +DTSTAMP:20161031T192828Z +UID:BFE33ADD-5553-48B5-B5A5-F9DA5CA4C393 +CREATED:20161029T121229Z +DESCRIPTION:Some Description +LAST-MODIFIED:20161029T121229Z +LOCATION:Roadstar 16\n12764 Happyville\nDenmark +SEQUENCE:0 +STATUS:CONFIRMED +SUMMARY:Daily Sync +TRANSP:OPAQUE +X-APPLE-STRUCTURED-LOCATION;VALUE=URI;X-ADDRESS="Röadstar 16\n12764 Happyvi + lle\nDenmark";X-APPLE-MAPKIT-HANDLE=CAESARoSCWYTYFhHQBEGfw4hQCIBDQoHRGVubW + FyaxJES0hhcHB5dmlsbGUqSGFwcHl2aWxsZTIHSGFwcHl2aWxsZToEMTI3NjRCDQpSb2Fkc3Rh + cloCMTZiUm9hZHN0YXIgMTYBEU1vcmRvcgENCk1vcmRvcioSUm9hZHN0YXIgMTYyUm9hZHN0YX + IgMTYxMjc2NCBIYXBweXZpbGxlMgdEZW5tYXJrOThA=;X-APPLE-RADIUS=49.913058665846 + 98;X-APPLE-REFERENCEFRAME=1;X-TITLE=:geo:52.382762,7.528319 +END:VEVENT +END:VCALENDAR