@@ -299,7 +299,7 @@ def __init__(self, message=None, sequence=0, keepHeaders=[],
299299 cset_out = Charset (cset ).output_charset or cset
300300 if isinstance (cset_out , str ):
301301 # email 3.0.1 (python 2.4) doesn't like unicode
302- cset_out = cset_out .encode ('us-ascii' )
302+ cset_out = cset_out .encode ('us-ascii' , 'replace' )
303303 charset = message .get_content_charset (cset_out )
304304 if charset :
305305 charset = charset .lower ().strip ()
@@ -313,14 +313,17 @@ def __init__(self, message=None, sequence=0, keepHeaders=[],
313313 body = None
314314 if body and charset != Utils .GetCharSet (self ._lang ):
315315 if isinstance (charset , bytes ):
316- charset = charset .decode ()
316+ charset = charset .decode ('utf-8' , 'replace' )
317317 # decode body
318318 try :
319319 body = body .decode (charset )
320320 except (UnicodeError , LookupError ):
321321 body = None
322322 if body :
323- self .body = [l .decode () + "\n " if isinstance (l , bytes ) else l + "\n " for l in body .splitlines ()]
323+ # Handle both bytes and strings properly
324+ if isinstance (body , bytes ):
325+ body = body .decode ('utf-8' , 'replace' )
326+ self .body = [l + "\n " for l in body .splitlines ()]
324327
325328 self .decode_headers ()
326329
@@ -875,7 +878,7 @@ def processListArch(self):
875878 #if the working file is still here, the archiver may have
876879 # crashed during archiving. Save it, log an error, and move on.
877880 try :
878- wf = open (wname )
881+ wf = open (wname , 'r' )
879882 syslog ('error' ,
880883 'Archive working file %s present. '
881884 'Check %s for possibly unarchived msgs' ,
@@ -895,7 +898,7 @@ def processListArch(self):
895898 except IOError :
896899 pass
897900 os .rename (name ,wname )
898- archfile = open (wname )
901+ archfile = open (wname , 'r' )
899902 self .processUnixMailbox (archfile )
900903 archfile .close ()
901904 os .unlink (wname )
@@ -1039,7 +1042,7 @@ def open_new_archive(self, archive, archivedir):
10391042 index_html = os .path .join (archivedir , 'index.html' )
10401043 try :
10411044 os .unlink (index_html )
1042- except :
1045+ except ( OSError , IOError ) :
10431046 pass
10441047 os .symlink (self .DEFAULTINDEX + '.html' ,index_html )
10451048
@@ -1144,7 +1147,7 @@ def update_archive(self, archive):
11441147 oldgzip = os .path .join (self .basedir , '%s.old.txt.gz' % archive )
11451148 try :
11461149 # open the plain text file
1147- archt = open (txtfile )
1150+ archt = open (txtfile , 'r' )
11481151 except IOError :
11491152 return
11501153 try :
0 commit comments