class TestDefectRaising(TestDefectsBase, TestEmailBase): policy = TestDefectsBase.policy policy = policy.clone(raise_on_defect=True) raise_expected = True @contextlib.contextmanager def _raise_point(self, defect): with self.assertRaises(defect): yield
def flatten_message( message: Message, utf8: bool = False, cte_type: str = "8bit" ) -> bytes: # Make a local copy so we can delete the bcc headers. message_copy = copy.copy(message) del message_copy["Bcc"] del message_copy["Resent-Bcc"] if utf8: policy = email.policy.SMTPUTF8 # type: email.policy.Policy else: policy = email.policy.SMTP if policy.cte_type != cte_type: policy = policy.clone(cte_type=cte_type) with io.BytesIO() as messageio: generator = email.generator.BytesGenerator( # type: ignore messageio, policy=policy ) generator.flatten(message_copy) flat_message = messageio.getvalue() return flat_message