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.

pull/204/head
Andreas Ruppen 2016-11-22 21:11:16 +01:00
rodzic b8e242af4d
commit 885a547d34
5 zmienionych plików z 79 dodań i 3 usunięć

Wyświetl plik

@ -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)

Wyświetl plik

@ -51,6 +51,7 @@ icalendar contributors
- tgecho <tgecho@gmail.com>
- tisto <tisto@plone.org>
- TomTry <tom.try@gmail.com>
- Andreas Ruppen <andreas.ruppen@gmail.com>
Find out who contributed::

Wyświetl plik

@ -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

Wyświetl plik

@ -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")

Wyświetl plik

@ -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