@@ -1521,6 +1521,59 @@ def test_labels(self):
15211521 self ._box .remove (key1 )
15221522 self .assertEqual (set (self ._box .get_labels ()), set (['blah' ]))
15231523
1524+ def test_labels_reopened (self ):
1525+ labels = ['unseen' , 'work' , 'caf\u00e9 ' ]
1526+ msg = mailbox .BabylMessage (self ._template % 0 )
1527+ msg .set_labels (labels )
1528+ self ._box .add (msg )
1529+ self ._box .add (self ._template % 1 )
1530+ self ._box .close ()
1531+ self ._box = self ._factory (self ._path )
1532+
1533+ msg = self ._box [0 ]
1534+ self .assertCountEqual (msg .get_labels (), labels )
1535+ self .assertCountEqual (self ._box .get_labels (), ['work' , 'caf\u00e9 ' ])
1536+ self .assertNotIn ('S' , mailbox .MaildirMessage (msg ).get_flags ())
1537+ self .assertNotIn ('R' , mailbox .mboxMessage (msg ).get_flags ())
1538+
1539+ # Copying a message and rewriting the mailbox must preserve labels.
1540+ self ._box .add (msg )
1541+ self ._box .remove (1 )
1542+ self ._box .flush ()
1543+ self ._box .close ()
1544+ self ._box = self ._factory (self ._path )
1545+ self .assertEqual (len (self ._box ), 2 )
1546+ for msg in self ._box :
1547+ self .assertCountEqual (msg .get_labels (), labels )
1548+ self .assertCountEqual (self ._box .get_labels (), ['work' , 'caf\u00e9 ' ])
1549+
1550+ def test_labels_non_utf8 (self ):
1551+ msg = mailbox .BabylMessage (self ._template % 0 )
1552+ msg .set_labels (['unseen' , 'work' ])
1553+ self ._box .add (msg )
1554+ self ._box .add (self ._template % 1 )
1555+ self ._box .close ()
1556+ with open (self ._path , 'rb' ) as f :
1557+ data = f .read ().replace (b'work' , b'w\xff rk' )
1558+ with open (self ._path , 'wb' ) as f :
1559+ f .write (data )
1560+ self ._box = self ._factory (self ._path )
1561+
1562+ labels = ['unseen' , 'w\udcff rk' ]
1563+ msg = self ._box [0 ]
1564+ self .assertCountEqual (msg .get_labels (), labels )
1565+ self .assertEqual (self ._box .get_labels (), ['w\udcff rk' ])
1566+ self ._box .add (msg )
1567+ self ._box .remove (1 )
1568+ self ._box .flush ()
1569+ self ._box .close ()
1570+ self ._box = self ._factory (self ._path )
1571+ self .assertEqual (len (self ._box ), 2 )
1572+ for msg in self ._box :
1573+ self .assertCountEqual (msg .get_labels (), labels )
1574+ with open (self ._path , 'rb' ) as f :
1575+ self .assertIn (b'Labels:w\xff rk' + mailbox .linesep , f .read ())
1576+
15241577 def test_visible_headers (self ):
15251578 # gh-157880: visible headers are written to the file and read back
15261579 msg = mailbox .BabylMessage (self ._template % 0 )
0 commit comments