@@ -26,7 +26,6 @@ def split_email(email):
2626 # Since backslash-escaping is no longer needed because
2727 # the quotes are removed, remove backslash-escaping
2828 # to return in the normalized form.
29- import re
3029 local_part = re .sub (r"\\(.)" , "\\ 1" , local_part )
3130
3231 return local_part , domain_part , True
@@ -72,14 +71,14 @@ def validate_email_local_part(local: str, allow_smtputf8: bool = True, allow_emp
7271 if len (local ) == 0 :
7372 if not allow_empty_local :
7473 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+ }
8382
8483 # Check the length of the local part by counting characters.
8584 # (RFC 5321 4.5.3.1.1)
@@ -191,8 +190,8 @@ def validate_email_local_part(local: str, allow_smtputf8: bool = True, allow_emp
191190 # want to have an unhandled exception later.
192191 try :
193192 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
196195
197196 # If this address passes only by the quoted string form, re-quote it
198197 # and backslash-escape quotes and backslashes (removing any unnecessary
@@ -330,7 +329,7 @@ def validate_email_domain_name(domain, test_environment=False, globally_delivera
330329 try :
331330 domain = idna .uts46_remap (domain , std3_rules = False , transitional = False )
332331 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
334333
335334 # The domain part is made up dot-separated "labels." Each label must
336335 # 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
374373 # the length check is applied to a string that is different from the
375374 # one the user supplied. Also I'm not sure if the length check applies
376375 # 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
378377
379378 # Other errors seem to not be possible because the call to idna.uts46_remap
380379 # 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
382381
383382 # Check the syntax of the string returned by idna.encode.
384383 # It should never fail.
@@ -440,7 +439,7 @@ def validate_email_domain_name(domain, test_environment=False, globally_delivera
440439 try :
441440 domain_i18n = idna .decode (ascii_domain .encode ('ascii' ))
442441 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
444443
445444 # Check for invalid characters after normalization. These
446445 # should never arise. See the similar checks above.
@@ -518,7 +517,7 @@ def validate_email_domain_literal(domain_literal):
518517 try :
519518 addr = ipaddress .IPv4Address (domain_literal )
520519 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
522521
523522 # Return the IPv4Address object and the domain back unchanged.
524523 return {
@@ -531,7 +530,7 @@ def validate_email_domain_literal(domain_literal):
531530 try :
532531 addr = ipaddress .IPv6Address (domain_literal [5 :])
533532 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
535534
536535 # Return the IPv6Address object and construct a normalized
537536 # domain literal.
0 commit comments