diff --git a/Lib/email/utils.py b/Lib/email/utils.py @@ -57,10 +54,15 @@ specialsre = re.compile(r'[][\\()<>@,:;".]') escapesre = re.compile(r'[\\"]') -# How to figure out if we are processing strings that come from a byte -# source with undecodable characters. -_has_surrogates = re.compile( - '([^\ud800-\udbff]|\A)[\udc00-\udfff]([^\udc00-\udfff]|\Z)').search +# How to figure out if we are processing strings that come from a byte source +# with undecodable characters. This is a self replacing function because the +# re compile is a significant source of import overhead, but the pre-compiled +# search function is the fastest run time check. See issue 11454. +def _has_surrogates(s): + import email.utils + f = re.compile('[\udc80-\udcff]').search + email.utils._has_surrogates = f + return f(s) # How to deal with a string containing bytes before handing it to the # application through the 'normal' interface.