vuntz / maildir-to-ics

4 stars 2 forks source link

fix crash writing ics to file via STDOUT #15

Closed aspiers closed 8 years ago

aspiers commented 8 years ago

Fix a crash if the vevent contained non-ascii characters (e.g. in the DESCRIPTION field):

Traceback (most recent call last):
  File "/home/adam/bin/maildir-to-ics", line 898, in <module>
    ret = main(sys.argv)
  File "/home/adam/bin/maildir-to-ics", line 876, in main
    dest_ics, limit_past, limit_future)
  File "/home/adam/bin/maildir-to-ics", line 578, in write_ics_from_dir
    fp.write(buf)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 2912: ordinal not in range(128)

This happened because when redirecting STDOUT to a file, sys.stdout.encoding will be None. Another way of fixing this is to set the PYTHONIOENCODING environment variable, but it's better to fix inside the program rather than requiring the user to do this.