我们从Python开源项目中,提取了以下37个代码示例,用于说明如何使用email.errors.InvalidHeaderDefect()。
def test_get_address_invalid_mailbox_invalid(self): address = self._test_get_x(parser.get_address, 'ping example.com, next', 'ping example.com', 'ping example.com', [errors.InvalidHeaderDefect, # addr-spec with no domain errors.InvalidHeaderDefect, # invalid local-part errors.InvalidHeaderDefect, # missing .s in local-part ], ', next') self.assertEqual(address.token_type, 'address') self.assertEqual(len(address.mailboxes), 0) self.assertEqual(len(address.all_mailboxes), 1) self.assertIsNone(address.all_mailboxes[0].domain) self.assertEqual(address.all_mailboxes[0].local_part, 'ping example.com') self.assertEqual(address[0].token_type, 'invalid-mailbox')
def get_comment(value): """comment = "(" *([FWS] ccontent) [FWS] ")" ccontent = ctext / quoted-pair / comment We handle nested comments here, and quoted-pair in our qp-ctext routine. """ if value and value[0] != '(': raise errors.HeaderParseError( "expected '(' but found '{}'".format(value)) comment = Comment() value = value[1:] while value and value[0] != ")": if value[0] in WSP: token, value = get_fws(value) elif value[0] == '(': token, value = get_comment(value) else: token, value = get_qp_ctext(value) comment.append(token) if not value: comment.defects.append(errors.InvalidHeaderDefect( "end of header inside comment")) return comment, value return comment, value[1:]
def get_mailbox(value): """ mailbox = name-addr / addr-spec """ # The only way to figure out if we are dealing with a name-addr or an # addr-spec is to try parsing each one. mailbox = Mailbox() try: token, value = get_name_addr(value) except errors.HeaderParseError: try: token, value = get_addr_spec(value) except errors.HeaderParseError: raise errors.HeaderParseError( "expected mailbox but found '{}'".format(value)) if any(isinstance(x, errors.InvalidHeaderDefect) for x in token.all_defects): mailbox.token_type = 'invalid-mailbox' mailbox.append(token) return mailbox, value
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 test_get_encoded_word_internal_spaces(self): self._test_get_x(parser.get_encoded_word, '=?us-ascii?q?this is a test?= bird', 'this is a test', 'this is a test', [errors.InvalidHeaderDefect], ' bird')
def test_get_unstructured_no_whitespace_between_ews(self): self._test_get_x(self._get_unst, '=?utf-8?q?foo?==?utf-8?q?bar?=', 'foobar', 'foobar', [errors.InvalidHeaderDefect], '') # get_qp_ctext
def test_get_bare_quoted_string_no_end_dquote(self): self._test_get_x(parser.get_bare_quoted_string, '"foo', '"foo"', 'foo', [errors.InvalidHeaderDefect], '') self._test_get_x(parser.get_bare_quoted_string, '"foo ', '"foo "', 'foo ', [errors.InvalidHeaderDefect], '')
def test_get_comment_no_end_paren(self): self._test_get_x(parser.get_comment, '(foo bar', '(foo bar)', ' ', [errors.InvalidHeaderDefect], '', ['foo bar']) self._test_get_x(parser.get_comment, '(foo bar ', '(foo bar )', ' ', [errors.InvalidHeaderDefect], '', ['foo bar '])
def test_get_comment_missing_end_of_nesting(self): self._test_get_x(parser.get_comment, '(((((foo)))', '(((((foo)))))', ' ', [errors.InvalidHeaderDefect]*2, '', ['((((foo))))'])
def test_get_quoted_string_header_ends_in_comment(self): qs = self._test_get_x(parser.get_quoted_string, ' (a) "bob" (a', ' (a) "bob" (a)', ' bob ', [errors.InvalidHeaderDefect], '') self.assertEqual(qs[0].comments, ['a']) self.assertEqual(qs[2].comments, ['a']) self.assertEqual(qs.content, 'bob') self.assertEqual(qs.quoted_value, ' "bob" ')
def test_get_quoted_string_header_ends_in_qcontent(self): qs = self._test_get_x(parser.get_quoted_string, ' (a) "bob', ' (a) "bob"', ' bob', [errors.InvalidHeaderDefect], '') self.assertEqual(qs[0].comments, ['a']) self.assertEqual(qs.content, 'bob') self.assertEqual(qs.quoted_value, ' "bob"')
def test_get_atom_header_ends_in_comment(self): atom = self._test_get_x(parser.get_atom, ' (a) bob (a', ' (a) bob (a)', ' bob ', [errors.InvalidHeaderDefect], '') self.assertEqual(atom[0].comments, ['a']) self.assertEqual(atom[2].comments, ['a'])
def test_get_phrase_pharse_must_start_with_word(self): phrase = self._test_get_x(parser.get_phrase, '(even weirder).name', '(even weirder).name', ' .name', [errors.InvalidHeaderDefect] + [errors.ObsoleteHeaderDefect]*2, '') self.assertEqual(len(phrase), 3) self.assertEqual(phrase[0].comments, ['even weirder'])
def test_get_local_part_complex_obsolete_invalid(self): local_part = self._test_get_x(parser.get_local_part, ' (foo )Fred (bar).(bird) A.(sheep)Johnson "and dogs"@python.org', ' (foo )Fred (bar).(bird) A.(sheep)Johnson "and dogs"', ' Fred . A. Johnson and dogs', [errors.InvalidHeaderDefect]*2, '@python.org') self.assertEqual(local_part.local_part, 'Fred.A.Johnson and dogs')
def test_get_local_part_trailing_dot_with_ws(self): local_part = self._test_get_x(parser.get_local_part, ' borris. @python.org', ' borris. ', ' borris. ', [errors.InvalidHeaderDefect]*2, '@python.org') self.assertEqual(local_part.local_part, 'borris.')
def test_get_local_part_quoted_strings_in_atom_list(self): local_part = self._test_get_x(parser.get_local_part, '""example" example"@example.com', '""example" example"', 'example example', [errors.InvalidHeaderDefect]*3, '@example.com') self.assertEqual(local_part.local_part, 'example example')
def test_get_angle_addr_empty(self): angle_addr = self._test_get_x(parser.get_angle_addr, '<>', '<>', '<>', [errors.InvalidHeaderDefect], '') self.assertEqual(angle_addr.token_type, 'angle-addr') self.assertIsNone(angle_addr.local_part) self.assertIsNone(angle_addr.domain) self.assertIsNone(angle_addr.route) self.assertEqual(angle_addr.addr_spec, '<>')
def test_get_display_name_pharse_must_start_with_word(self): display_name = self._test_get_x(parser.get_display_name, '(even weirder).name', '(even weirder).name', ' ".name"', [errors.InvalidHeaderDefect] + [errors.ObsoleteHeaderDefect]*2, '') self.assertEqual(len(display_name), 3) self.assertEqual(display_name[0].comments, ['even weirder']) self.assertEqual(display_name.display_name, '.name')
def test_get_group_list_comment_only_invalid(self): group_list = self._test_get_x(parser.get_group_list, '(bar)', '(bar)', ' ', [errors.InvalidHeaderDefect], '') self.assertEqual(group_list.token_type, 'group-list') self.assertEqual(len(group_list.mailboxes), 0) self.assertEqual(group_list.mailboxes, group_list.all_mailboxes) # get_group
def test_get_group_null_addr_spec(self): group = self._test_get_x(parser.get_group, 'foo: <>;', 'foo: <>;', 'foo: <>;', [errors.InvalidHeaderDefect], '') self.assertEqual(group.display_name, 'foo') self.assertEqual(len(group.mailboxes), 0) self.assertEqual(len(group.all_mailboxes), 1) self.assertEqual(group.all_mailboxes[0].value, '<>')
def test_encoded_word_inside_quotes(self): self._test_get_x(parser.get_bare_quoted_string, '"=?utf-8?Q?not_really_valid?="', '"not really valid"', 'not really valid', [errors.InvalidHeaderDefect], '') # get_comment
def test_get_cfws_header_ends_in_comment(self): cfws = self._test_get_x(parser.get_cfws, ' (foo ', ' (foo )', ' ', [errors.InvalidHeaderDefect], '', ['foo ']) self.assertEqual(cfws[1].content, 'foo ')