@@ -26,7 +26,6 @@ def split_email(email):
26
26
# Since backslash-escaping is no longer needed because
27
27
# the quotes are removed, remove backslash-escaping
28
28
# to return in the normalized form.
29
- import re
30
29
local_part = re .sub (r"\\(.)" , "\\ 1" , local_part )
31
30
32
31
return local_part , domain_part , True
@@ -72,14 +71,14 @@ def validate_email_local_part(local: str, allow_smtputf8: bool = True, allow_emp
72
71
if len (local ) == 0 :
73
72
if not allow_empty_local :
74
73
raise EmailSyntaxError ("There must be something before the @-sign." )
75
- else :
76
- # The caller allows an empty local part. Useful for validating certain
77
- # Postfix aliases.
78
- return {
79
- "local_part" : local ,
80
- "ascii_local_part" : local ,
81
- "smtputf8" : False ,
82
- }
74
+
75
+ # The caller allows an empty local part. Useful for validating certain
76
+ # Postfix aliases.
77
+ return {
78
+ "local_part" : local ,
79
+ "ascii_local_part" : local ,
80
+ "smtputf8" : False ,
81
+ }
83
82
84
83
# Check the length of the local part by counting characters.
85
84
# (RFC 5321 4.5.3.1.1)
@@ -191,8 +190,8 @@ def validate_email_local_part(local: str, allow_smtputf8: bool = True, allow_emp
191
190
# want to have an unhandled exception later.
192
191
try :
193
192
local .encode ("utf8" )
194
- except ValueError :
195
- raise EmailSyntaxError ("The email address contains an invalid character." )
193
+ except ValueError as e :
194
+ raise EmailSyntaxError ("The email address contains an invalid character." ) from e
196
195
197
196
# If this address passes only by the quoted string form, re-quote it
198
197
# and backslash-escape quotes and backslashes (removing any unnecessary
@@ -330,7 +329,7 @@ def validate_email_domain_name(domain, test_environment=False, globally_delivera
330
329
try :
331
330
domain = idna .uts46_remap (domain , std3_rules = False , transitional = False )
332
331
except idna .IDNAError as e :
333
- raise EmailSyntaxError (f"The part after the @-sign contains invalid characters ({ e } )." )
332
+ raise EmailSyntaxError (f"The part after the @-sign contains invalid characters ({ e } )." ) from e
334
333
335
334
# The domain part is made up dot-separated "labels." Each label must
336
335
# have at least one character and cannot start or end with dashes, which
@@ -374,11 +373,11 @@ def validate_email_domain_name(domain, test_environment=False, globally_delivera
374
373
# the length check is applied to a string that is different from the
375
374
# one the user supplied. Also I'm not sure if the length check applies
376
375
# to the internationalized form, the IDNA ASCII form, or even both!
377
- raise EmailSyntaxError ("The email address is too long after the @-sign." )
376
+ raise EmailSyntaxError ("The email address is too long after the @-sign." ) from e
378
377
379
378
# Other errors seem to not be possible because the call to idna.uts46_remap
380
379
# would have already raised them.
381
- raise EmailSyntaxError (f"The part after the @-sign contains invalid characters ({ e } )." )
380
+ raise EmailSyntaxError (f"The part after the @-sign contains invalid characters ({ e } )." ) from e
382
381
383
382
# Check the syntax of the string returned by idna.encode.
384
383
# It should never fail.
@@ -440,7 +439,7 @@ def validate_email_domain_name(domain, test_environment=False, globally_delivera
440
439
try :
441
440
domain_i18n = idna .decode (ascii_domain .encode ('ascii' ))
442
441
except idna .IDNAError as e :
443
- raise EmailSyntaxError (f"The part after the @-sign is not valid IDNA ({ e } )." )
442
+ raise EmailSyntaxError (f"The part after the @-sign is not valid IDNA ({ e } )." ) from e
444
443
445
444
# Check for invalid characters after normalization. These
446
445
# should never arise. See the similar checks above.
@@ -518,7 +517,7 @@ def validate_email_domain_literal(domain_literal):
518
517
try :
519
518
addr = ipaddress .IPv4Address (domain_literal )
520
519
except ValueError as e :
521
- raise EmailSyntaxError (f"The address in brackets after the @-sign is not valid: It is not an IPv4 address ({ e } ) or is missing an address literal tag." )
520
+ raise EmailSyntaxError (f"The address in brackets after the @-sign is not valid: It is not an IPv4 address ({ e } ) or is missing an address literal tag." ) from e
522
521
523
522
# Return the IPv4Address object and the domain back unchanged.
524
523
return {
@@ -531,7 +530,7 @@ def validate_email_domain_literal(domain_literal):
531
530
try :
532
531
addr = ipaddress .IPv6Address (domain_literal [5 :])
533
532
except ValueError as e :
534
- raise EmailSyntaxError (f"The IPv6 address in brackets after the @-sign is not valid ({ e } )." )
533
+ raise EmailSyntaxError (f"The IPv6 address in brackets after the @-sign is not valid ({ e } )." ) from e
535
534
536
535
# Return the IPv6Address object and construct a normalized
537
536
# domain literal.
0 commit comments