10
10
def get_length_reason (addr , utf8 = False , limit = EMAIL_MAX_LENGTH ):
11
11
"""Helper function to return an error message related to invalid length."""
12
12
diff = len (addr ) - limit
13
- reason = "({}{} character{} too many)"
14
13
prefix = "at least " if utf8 else ""
15
14
suffix = "s" if diff > 1 else ""
16
- return reason . format ( prefix , diff , suffix )
15
+ return f"( { prefix } { diff } character { suffix } too many)"
17
16
18
17
19
18
def safe_character_display (c ):
@@ -23,9 +22,9 @@ def safe_character_display(c):
23
22
24
23
# Construct a hex string in case the unicode name doesn't exist.
25
24
if ord (c ) < 0xFFFF :
26
- h = "U+{:04x}" . format ( ord (c )) .upper ()
25
+ h = f "U+{ ord (c ):04x } " .upper ()
27
26
else :
28
- h = "U+{:08x}" . format ( ord (c )) .upper ()
27
+ h = f "U+{ ord (c ):08x } " .upper ()
29
28
30
29
# Return the character name or, if it has no name, the hex string.
31
30
return unicodedata .name (c , h )
@@ -55,7 +54,7 @@ def validate_email_local_part(local, allow_smtputf8=True, allow_empty_local=Fals
55
54
# instead.
56
55
if len (local ) > LOCAL_PART_MAX_LENGTH :
57
56
reason = get_length_reason (local , limit = LOCAL_PART_MAX_LENGTH )
58
- raise EmailSyntaxError ("The email address is too long before the @-sign {}." . format ( reason ) )
57
+ raise EmailSyntaxError (f "The email address is too long before the @-sign { reason } ." )
59
58
60
59
# Check the local part against the non-internationalized regular expression.
61
60
# Most email addresses match this regex so it's probably fastest to check this first.
@@ -231,7 +230,7 @@ def validate_email_domain_part(domain, test_environment=False, globally_delivera
231
230
try :
232
231
domain = idna .uts46_remap (domain , std3_rules = False , transitional = False )
233
232
except idna .IDNAError as e :
234
- raise EmailSyntaxError ("The part after the @-sign contains invalid characters ({})." . format ( str ( e )) )
233
+ raise EmailSyntaxError (f "The part after the @-sign contains invalid characters ({ e } )." )
235
234
236
235
# The domain part is made up period-separated "labels." Each label must
237
236
# have at least one character and cannot start or end with dashes, which
@@ -274,7 +273,7 @@ def validate_email_domain_part(domain, test_environment=False, globally_delivera
274
273
# one the user supplied. Also I'm not sure if the length check applies
275
274
# to the internationalized form, the IDNA ASCII form, or even both!
276
275
raise EmailSyntaxError ("The email address is too long after the @-sign." )
277
- raise EmailSyntaxError ("The part after the @-sign contains invalid characters (%s )." % str ( e ) )
276
+ raise EmailSyntaxError (f "The part after the @-sign contains invalid characters ({ e } )." )
278
277
279
278
# Check the syntax of the string returned by idna.encode.
280
279
# It should never fail.
@@ -291,14 +290,14 @@ def validate_email_domain_part(domain, test_environment=False, globally_delivera
291
290
# is never reached for internationalized domains.)
292
291
if len (ascii_domain ) > DOMAIN_MAX_LENGTH :
293
292
reason = get_length_reason (ascii_domain , limit = DOMAIN_MAX_LENGTH )
294
- raise EmailSyntaxError ("The email address is too long after the @-sign {}." . format ( reason ) )
293
+ raise EmailSyntaxError (f "The email address is too long after the @-sign { reason } ." )
295
294
296
295
# Also check the label length limit.
297
296
# (RFC 1035 2.3.1)
298
297
for label in ascii_domain .split ("." ):
299
298
if len (label ) > DNS_LABEL_LENGTH_LIMIT :
300
299
reason = get_length_reason (label , limit = DNS_LABEL_LENGTH_LIMIT )
301
- raise EmailSyntaxError ("After the @-sign, periods cannot be separated by so many characters {}." . format ( reason ) )
300
+ raise EmailSyntaxError (f "After the @-sign, periods cannot be separated by so many characters { reason } ." )
302
301
303
302
if globally_deliverable :
304
303
# All publicly deliverable addresses have domain named with at least
@@ -337,7 +336,7 @@ def validate_email_domain_part(domain, test_environment=False, globally_delivera
337
336
try :
338
337
domain_i18n = idna .decode (ascii_domain .encode ('ascii' ))
339
338
except idna .IDNAError as e :
340
- raise EmailSyntaxError ("The part after the @-sign is not valid IDNA ({})." . format ( str ( e )) )
339
+ raise EmailSyntaxError (f "The part after the @-sign is not valid IDNA ({ e } )." )
341
340
342
341
# Check for invalid characters after normalization. These
343
342
# should never arise. See the similar checks above.
0 commit comments