Skip to content

Commit

Permalink
u-u: import "email" package late to workaround (LP: 2080940)
Browse files Browse the repository at this point in the history
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 python/cpython#124170 and
https://bugs.launchpad.net/ubuntu/+source/python3.8/+bug/2080940
for details.

Thanks to Julian Klode for his excellent analysis.
  • Loading branch information
mvo5 committed Nov 10, 2024
1 parent edb2f8b commit c8f846d
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions unattended-upgrade
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import contextlib
import copy
import datetime
import errno
import email.charset
import fcntl
import fnmatch
import gettext
Expand All @@ -40,6 +39,7 @@ import grp
import inspect
import io
from importlib.abc import Loader
import importlib
import importlib.util
import locale
import logging
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
import email.generator
import email.message
importlib.reload(email)
importlib.reload(email.charset)
importlib.reload(email.message)
importlib.reload(email.generator)
importlib.reload(email.errors)
# 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
Expand Down

0 comments on commit c8f846d

Please sign in to comment.