我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用email.errors.NonASCIILocalPartDefect()。
def sanitize_address(addr, encoding): """ Format a pair of (name, address) or an email address string. """ if not isinstance(addr, tuple): addr = parseaddr(force_text(addr)) nm, addr = addr localpart, domain = None, None nm = Header(nm, encoding).encode() try: addr.encode('ascii') except UnicodeEncodeError: # IDN or non-ascii in the local part localpart, domain = split_addr(addr, encoding) if six.PY2: # On Python 2, use the stdlib since `email.headerregistry` doesn't exist. from email.utils import formataddr if localpart and domain: addr = '@'.join([localpart, domain]) return formataddr((nm, addr)) # On Python 3, an `email.headerregistry.Address` object is used since # email.utils.formataddr() naively encodes the name as ascii (see #25986). from email.headerregistry import Address from email.errors import InvalidHeaderDefect, NonASCIILocalPartDefect if localpart and domain: address = Address(nm, username=localpart, domain=domain) return str(address) try: address = Address(nm, addr_spec=addr) except (InvalidHeaderDefect, NonASCIILocalPartDefect): localpart, domain = split_addr(addr, encoding) address = Address(nm, username=localpart, domain=domain) return str(address)
def get_local_part(value): """ local-part = dot-atom / quoted-string / obs-local-part """ local_part = LocalPart() leader = None if value[0] in CFWS_LEADER: leader, value = get_cfws(value) if not value: raise errors.HeaderParseError( "expected local-part but found '{}'".format(value)) try: token, value = get_dot_atom(value) except errors.HeaderParseError: try: token, value = get_word(value) except errors.HeaderParseError: if value[0] != '\\' and value[0] in PHRASE_ENDS: raise token = TokenList() if leader is not None: token[:0] = [leader] local_part.append(token) if value and (value[0]=='\\' or value[0] not in PHRASE_ENDS): obs_local_part, value = get_obs_local_part(str(local_part) + value) if obs_local_part.token_type == 'invalid-obs-local-part': local_part.defects.append(errors.InvalidHeaderDefect( "local-part is not dot-atom, quoted-string, or obs-local-part")) else: local_part.defects.append(errors.ObsoleteHeaderDefect( "local-part is not a dot-atom (contains CFWS)")) local_part[0] = obs_local_part try: local_part.value.encode('ascii') except UnicodeEncodeError: local_part.defects.append(errors.NonASCIILocalPartDefect( "local-part contains non-ASCII characters)")) return local_part, value