From 9fa4d2927d58f5a91e6437f4295df7124862f647 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Sun, 10 Nov 2024 09:12:11 +0100 Subject: [PATCH] u-u: import "email" package late to workaround (LP: 2080940) This commit works around an issue when python itself is upgraded by u-u and the content of email.message now needs a newer email.errors but u-u already (implicitely) loaded that module so there is a old version in sys.modules. So instead import email as late as possible. See https://github.com/python/cpython/issues/124170 and https://bugs.launchpad.net/ubuntu/+source/python3.8/+bug/2080940 for details. Thanks to Julian Klode for his excellent analysis. --- unattended-upgrade | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/unattended-upgrade b/unattended-upgrade index 86358b19..c0441567 100755 --- a/unattended-upgrade +++ b/unattended-upgrade @@ -27,7 +27,6 @@ import contextlib import copy import datetime import errno -import email.charset import fcntl import fnmatch import gettext @@ -40,6 +39,7 @@ import grp import inspect import io from importlib.abc import Loader +import importlib import importlib.util import locale import logging @@ -71,7 +71,6 @@ except ImportError: from collections import defaultdict, namedtuple from datetime import date -from email.message import Message from gettext import gettext as _ from io import StringIO from optparse import ( @@ -1613,9 +1612,21 @@ def _send_mail_using_mailx(from_address, to_address, subject, body): def _send_mail_using_sendmail(from_address, to_address, subject, body): + # workaround LP:2080940, email.errors changes as part of a security + # update in python itself so import this as late as possible + import email + import email.charset + import email.errors + importlib.reload(email.errors) + import email.generator + import email.message + importlib.reload(email) + importlib.reload(email.charset) + importlib.reload(email.message) + importlib.reload(email.generator) # type: (str, str, str, str) -> int # format as a proper mail - msg = Message() + msg = email.message.Message() msg['Subject'] = subject msg['From'] = from_address msg['To'] = to_address