From ea9766897bf1d2ccf610ff9ce805acca7c4cce6f Mon Sep 17 00:00:00 2001 From: R David Murray Date: Sun, 27 May 2012 15:03:38 -0400 Subject: Make headerregistry fully part of the provisional api. When I made the checkin of the provisional email policy, I knew that Address and Group needed to be made accessible from somewhere. The more I looked at it, though, the more it became clear that since this is a provisional API anyway, there's no good reason to hide headerregistry as a private API. It was designed to ultimately be part of the public API, and so it should be part of the provisional API. This patch fully documents the headerregistry API, and deletes the abbreviated version of those docs I had added to the provisional policy docs. --- Doc/library/email.headerregistry.rst | 379 ++++++++++++++ Doc/library/email.policy.rst | 186 +------ Lib/email/_headerregistry.py | 456 ----------------- Lib/email/headerregistry.py | 456 +++++++++++++++++ Lib/email/policy.py | 6 +- Lib/test/test_email/test__headerregistry.py | 739 ---------------------------- Lib/test/test_email/test_headerregistry.py | 738 +++++++++++++++++++++++++++ Lib/test/test_email/test_pickleable.py | 2 +- Lib/test/test_email/test_policy.py | 12 +- 9 files changed, 1595 insertions(+), 1379 deletions(-) create mode 100644 Doc/library/email.headerregistry.rst delete mode 100644 Lib/email/_headerregistry.py create mode 100644 Lib/email/headerregistry.py delete mode 100644 Lib/test/test_email/test__headerregistry.py create mode 100644 Lib/test/test_email/test_headerregistry.py diff --git a/Doc/library/email.headerregistry.rst b/Doc/library/email.headerregistry.rst new file mode 100644 index 0000000..4fc9594 --- /dev/null +++ b/Doc/library/email.headerregistry.rst @@ -0,0 +1,379 @@ +:mod:`email.headerregistry`: Custom Header Objects +-------------------------------------------------- + +.. module:: email.headerregistry + :synopsis: Automatic Parsing of headers based on the field name + +.. note:: + + The headerregistry module has been included in the standard library on a + :term:`provisional basis `. Backwards incompatible + changes (up to and including removal of the module) may occur if deemed + necessary by the core developers. + +.. versionadded:: 3.3 + as a :term:`provisional module ` + +Headers are represented by customized subclasses of :class:`str`. The +particular class used to represent a given header is determined by the +:attr:`~email.policy.EmailPolicy.header_factory` of the :mod:`~email.policy` in +effect when the headers are created. This section documents the particular +``header_factory`` implemented by the email package for handling :RFC:`5322` +compliant email messages, which not only provides customized header objects for +various header types, but also provides an extension mechanism for applications +to add their own custom header types. + +When using any of the policy objects derived from +:data:`~email.policy.EmailPolicy`, all headers are produced by +:class:`.HeaderRegistry` and have :class:`.BaseHeader` as their last base +class. Each header class has an additional base class that is determined by +the type of the header. For example, many headers have the class +:class:`.UnstructuredHeader` as their other base class. The specialized second +class for a header is determined by the name of the header, using a lookup +table stored in the :class:`.HeaderRegistry`. All of this is managed +transparently for the typical application program, but interfaces are provided +for modifying the default behavior for use by more complex applications. + +The sections below first document the header base classes and their attributes, +followed by the API for modifying the behavior of :class:`.HeaderRegistry`, and +finally the support classes used to represent the data parsed from structured +headers. + + +.. class:: BaseHeader(name, value) + + *name* and *value* are passed to ``BaseHeader`` from the + :attr:`~email.policy.EmailPolicy.header_factory` call. The string value of + any header object is the *value* fully decoded to unicode. + + This base class defines the following read-only properties: + + + .. attribute:: name + + The name of the header (the portion of the field before the ':'). This + is exactly the value passed in the :attr:`~EmailPolicy.header_factory` + call for *name*; that is, case is preserved. + + + .. attribute:: defects + + A tuple of :exc:`~email.errors.HeaderDefect` instances reporting any + RFC compliance problems found during parsing. The email package tries to + be complete about detecting compliance issues. See the :mod:`errors` + module for a discussion of the types of defects that may be reported. + + + .. attribute:: max_count + + The maximum number of headers of this type that can have the same + ``name``. A value of ``None`` means unlimited. The ``BaseHeader`` value + for this attribute is ``None``; it is expected that specialized header + classes will override this value as needed. + + ``BaseHeader`` also provides the following method, which is called by the + email library code and should not in general be called by application + programs: + + .. method:: fold(*, policy) + + Return a string containing :attr:`~email.policy.Policy.linesep` + characters as required to correctly fold the header according + to *policy*. A :attr:`~email.policy.Policy.cte_type` of + ``8bit`` will be treated as if it were ``7bit``, since strings + may not contain binary data. + + + ``BaseHeader`` by itself cannot be used to create a header object. It + defines a protocol that each specialized header cooperates with in order to + produce the header object. Specifically, ``BaseHeader`` requires that + the specialized class provide a :func:`classmethod` named ``parse``. This + method is called as follows:: + + parse(string, kwds) + + ``kwds`` is a dictionary containing one pre-initialized key, ``defects``. + ``defects`` is an empty list. The parse method should append any detected + defects to this list. On return, the ``kwds`` dictionary *must* contain + values for at least the keys ``decoded`` and ``defects``. ``decoded`` + should be the string value for the header (that is, the header value fully + decoded to unicode). The parse method should assume that *string* may + contain transport encoded parts, but should correctly handle all valid + unicode characters as well so that it can parse un-encoded header values. + + ``BaseHeader``'s ``__new__`` then creates the header instance, and calls its + ``init`` method. The specialized class only needs to provide an ``init`` + method if it wishes to set additional attributes beyond those provided by + ``BaseHeader`` itself. Such an ``init`` method should look like this:: + + def init(self, *args, **kw): + self._myattr = kw.pop('myattr') + super().init(*args, **kw) + + That is, anything extra that the specialized class puts in to the ``kwds`` + dictionary should be removed and handled, and the remaining contents of + ``kw`` (and ``args``) passed to the ``BaseHeader`` ``init`` method. + + +.. class:: UnstructuredHeader + + An "unstructured" header is the default type of header in :rfc:`5322`. + Any header that does not have a specified syntax is treated as + unstructured. The classic example of an unstructured header is the + :mailheader:`Subject` header. + + In :rfc:`5322`, an unstructured header is a run of arbitrary text in the + ASCII character set. :rfc:`2047`, however, has an :rfc:`5322` compatible + mechanism for encoding non-ASCII text as ASCII characters within a header + value. When a *value* containing encoded words is passed to the + constructor, the ``UnstructuredHeader`` parser converts such encoded words + back in to the original unicode, following the :rfc:`2047` rules for + unstructured text. The parser uses heuristics to attempt to decode certain + non-compliant encoded words. Defects are registered in such cases, as well + as defects for issues such as invalid characters within the encoded words or + the non-encoded text. + + This header type provides no additional attributes. + + +.. class:: DateHeader + + :rfc:`5322` specifies a very specific format for dates within email headers. + The ``DateHeader`` parser recognizes that date format, as well as + recognizing a number of variant forms that are sometimes found "in the + wild". + + This header type provides the following additional attributes: + + .. attribute:: datetime + + If the header value can be recognized as a valid date of one form or + another, this attribute will contain a :class:`~datetime.datetime` + instance representing that date. If the timezone of the input date is + specified as ``-0000`` (indicating it is in UTC but contains no + information about the source timezone), then :attr:`.datetime` will be a + naive :class:`~datetime.datetime`. If a specific timezone offset is + found (including `+0000`), then :attr:`.datetime` will contain an aware + ``datetime`` that uses :class:`datetime.timezone` to record the timezone + offset. + + The ``decoded`` value of the header is determined by formatting the + ``datetime`` according to the :rfc:`5322` rules; that is, it is set to:: + + email.utils.format_datetime(self.datetime) + + When creating a ``DateHeader``, *value* may be + :class:`~datetime.datetime` instance. This means, for example, that + the following code is valid and does what one would expect:: + + msg['Date'] = datetime(2011, 7, 15, 21) + + Because this is a naive ``datetime`` it will be interpreted as a UTC + timestamp, and the resulting value will have a timezone of ``-0000``. Much + more useful is to use the :func:`~email.utils.localtime` function from the + :mod:`~email.utils` module:: + + msg['Date'] = utils.localtime() + + This example sets the date header to the current time and date using + the current timezone offset. + + +.. class:: AddressHeader + + Address headers are one of the most complex structured header types. + The ``AddressHeader`` class provides a generic interface to any address + header. + + This header type provides the following additional attributes: + + + .. attribute:: groups + + A tuple of :class:`.Group` objects encoding the + addresses and groups found in the header value. Addresses that are + not part of a group are represented in this list as single-address + ``Groups`` whose :attr:`~.Group.display_name` is ``None``. + + + .. attribute:: addresses + + A tuple of :class:`.Address` objects encoding all + of the individual addresses from the header value. If the header value + contains any groups, the individual addresses from the group are included + in the list at the point where the group occurs in the value (that is, + the list of addresses is "flattened" into a one dimensional list). + + The ``decoded`` value of the header will have all encoded words decoded to + unicode. :class:`~encodings.idna` encoded domain names are also decoded to unicode. The + ``decoded`` value is set by :attr:`~str.join`\ ing the :class:`str` value of + the elements of the ``groups`` attribute with ``', '``. + + A list of :class:`.Address` and :class:`.Group` objects in any combination + may be used to set the value of an address header. ``Group`` objects whose + ``display_name`` is ``None`` will be interpreted as single addresses, which + allows an address list to be copied with groups intact by using the list + obtained ``groups`` attribute of the source header. + + +.. class:: SingleAddressHeader + + A subclass of :class:`.AddressHeader` that adds one + additional attribute: + + + .. attribute:: address + + The single address encoded by the header value. If the header value + actually contains more than one address (which would be a violation of + the RFC under the default :mod:`policy`), accessing this attribute will + result in a :exc:`ValueError`. + + +Each of the above classes also has a ``Unique`` variant (for example, +``UniqueUnstructuredHeader``). The only difference is that in the ``Unique`` +variant, :attr:`~.BaseHeader.max_count` is set to 1. + + +.. class:: HeaderRegistry(base_class=BaseHeader, \ + default_class=UnstructuredHeader, \ + use_default_map=True) + + This is the factory used by :class:`~email.policy.EmailPolicy` by default. + ``HeaderRegistry`` builds the class used to create a header instance + dynamically, using *base_class* and a specialized class retrieved from a + registry that it holds. When a given header name does not appear in the + registry, the class specified by *default_class* is used as the specialized + class. When *use_default_map* is ``True`` (the default), the standard + mapping of header names to classes is copied in to the registry during + initialization. *base_class* is always the last class in the generated + class's ``__bases__`` list. + + The default mappings are: + + :subject: UniqueUnstructuredHeader + :date: UniqueDateHeader + :resent-date: DateHeader + :orig-date: UniqueDateHeader + :sender: UniqueSingleAddressHeader + :resent-sender: SingleAddressHeader + :to: UniqueAddressHeader + :resent-to: AddressHeader + :cc: UniqueAddressHeader + :resent-cc: AddressHeader + :from: UniqueAddressHeader + :resent-from: AddressHeader + :reply-to: UniqueAddressHeader + + ``HeaderRegistry`` has the following methods: + + + .. method:: map_to_type(self, name, cls) + + *name* is the name of the header to be mapped. It will be converted to + lower case in the registry. *cls* is the specialized class to be used, + along with *base_class*, to create the class used to instantiate headers + that match *name*. + + + .. method:: __getitem__(name) + + Construct and return a class to handle creating a *name* header. + + + .. method:: __call__(name, value) + + Retrieves the specialized header associated with *name* from the + registry (using *default_class* if *name* does not appear in the + registry) and composes it with *base_class* to produce a class, + calls the constructed class's constructor, passing it the same + argument list, and finally returns the class instance created thereby. + + +The following classes are the classes used to represent data parsed from +structured headers and can, in general, be used by an application program to +construct structured values to assign to specific headers. + + +.. class:: Address(display_name='', username='', domain='', addr_spec=None) + + The class used to represent an email address. The general form of an + address is:: + + [display_name] + + or:: + + username@domain + + where each part must conform to specific syntax rules spelled out in + :rfc:`5322`. + + As a convenience *addr_spec* can be specified instead of *username* and + *domain*, in which case *username* and *domain* will be parsed from the + *addr_spec*. An *addr_spec* must be a properly RFC quoted string; if it is + not ``Address`` will raise an error. Unicode characters are allowed and + will be property encoded when serialized. However, per the RFCs, unicode is + *not* allowed in the username portion of the address. + + .. attribute:: display_name + + The display name portion of the address, if any, with all quoting + removed. If the address does not have a display name, this attribute + will be an empty string. + + .. attribute:: username + + The ``username`` portion of the address, with all quoting removed. + + .. attribute:: domain + + The ``domain`` portion of the address. + + .. attribute:: addr_spec + + The ``username@domain`` portion of the address, correctly quoted + for use as a bare address (the second form shown above). This + attribute is not mutable. + + .. method:: __str__() + + The ``str`` value of the object is the address quoted according to + :rfc:`5322` rules, but with no Content Transfer Encoding of any non-ASCII + characters. + + To support SMTP (:rfc:`5321`), ``Address`` handles one special case: if + ``username`` and ``domain`` are both the empty string (or ``None``), then + the string value of the ``Address`` is ``<>``. + + +.. class:: Group(display_name=None, addresses=None) + + The class used to represent an address group. The general form of an + address group is:: + + display_name: [address-list]; + + As a convenience for processing lists of addresses that consist of a mixture + of groups and single addresses, a ``Group`` may also be used to represent + single addresses that are not part of a group by setting *display_name* to + ``None`` and providing a list of the single address as *addresses*. + + .. attribute:: display_name + + The ``display_name`` of the group. If it is ``None`` and there is + exactly one ``Address`` in ``addresses``, then the ``Group`` represents a + single address that is not in a group. + + .. attribute:: addresses + + A possibly empty tuple of :class:`.Address` objects representing the + addresses in the group. + + .. method:: __str__() + + The ``str`` value of a ``Group`` is formatted according to :rfc:`5322`, + but with no Content Transfer Encoding of any non-ASCII characters. If + ``display_name`` is none and there is a single ``Address`` in the + ``addresses`` list, the ``str`` value will be the same as the ``str`` of + that single ``Address``. diff --git a/Doc/library/email.policy.rst b/Doc/library/email.policy.rst index c1734e2..2ba0dba 100644 --- a/Doc/library/email.policy.rst +++ b/Doc/library/email.policy.rst @@ -310,10 +310,10 @@ added matters. To illustrate:: .. note:: - The remainder of the classes documented below are included in the standard - library on a :term:`provisional basis `. Backwards - incompatible changes (up to and including removal of the feature) may occur - if deemed necessary by the core developers. + The documentation below describes new policies that are included in the + standard library on a :term:`provisional basis `. + Backwards incompatible changes (up to and including removal of the feature) + may occur if deemed necessary by the core developers. .. class:: EmailPolicy(**kw) @@ -353,12 +353,12 @@ added matters. To illustrate:: A callable that takes two arguments, ``name`` and ``value``, where ``name`` is a header field name and ``value`` is an unfolded header field - value, and returns a string-like object that represents that header. A - default ``header_factory`` is provided that understands some of the - :RFC:`5322` header field types. (Currently address fields and date - fields have special treatment, while all other fields are treated as - unstructured. This list will be completed before the extension is marked - stable.) + value, and returns a string subclass that represents that header. A + default ``header_factory`` (see :mod:`~email.headerregistry`) is provided + that understands some of the :RFC:`5322` header field types. (Currently + address fields and date fields have special treatment, while all other + fields are treated as unstructured. This list will be completed before + the extension is marked stable.) The class provides the following concrete implementations of the abstract methods of :class:`Policy`: @@ -465,167 +465,5 @@ header. Likewise, a header may be assigned a new value, or a new header created, using a unicode string, and the policy will take care of converting the unicode string into the correct RFC encoded form. -The custom header objects and their attributes are described below. All custom -header objects are string subclasses, and their string value is the fully -decoded value of the header field (the part of the field after the ``:``) - - -.. class:: BaseHeader - - This is the base class for all custom header objects. It provides the - following attributes: - - .. attribute:: name - - The header field name (the portion of the field before the ':'). - - .. attribute:: defects - - A possibly empty list of :class:`~email.errors.MessageDefect` objects - that record any RFC violations found while parsing the header field. - - .. method:: fold(*, policy) - - Return a string containing :attr:`~email.policy.Policy.linesep` - characters as required to correctly fold the header according - to *policy*. A :attr:`~email.policy.Policy.cte_type` of - ``8bit`` will be treated as if it were ``7bit``, since strings - may not contain binary data. - - -.. class:: UnstructuredHeader - - The class used for any header that does not have a more specific - type. (The :mailheader:`Subject` header is an example of an - unstructured header.) It does not have any additional attributes. - - -.. class:: DateHeader - - The value of this type of header is a single date and time value. The - primary example of this type of header is the :mailheader:`Date` header. - - .. attribute:: datetime - - A :class:`~datetime.datetime` encoding the date and time from the - header value. - - The ``datetime`` will be a naive ``datetime`` if the value either does - not have a specified timezone (which would be a violation of the RFC) or - if the timezone is specified as ``-0000``. This timezone value indicates - that the date and time is to be considered to be in UTC, but with no - indication of the local timezone in which it was generated. (This - contrasts to ``+0000``, which indicates a date and time that really is in - the UTC ``0000`` timezone.) - - If the header value contains a valid timezone that is not ``-0000``, the - ``datetime`` will be an aware ``datetime`` having a - :class:`~datetime.tzinfo` set to the :class:`~datetime.timezone` - indicated by the header value. - - A ``datetime`` may also be assigned to a :mailheader:`Date` type header. - The resulting string value will use a timezone of ``-0000`` if the - ``datetime`` is naive, and the appropriate UTC offset if the ``datetime`` is - aware. - - -.. class:: AddressHeader - - This class is used for all headers that can contain addresses, whether they - are supposed to be singleton addresses or a list. - - .. attribute:: addresses - - A list of :class:`.Address` objects listing all of the addresses that - could be parsed out of the field value. - - .. attribute:: groups - - A list of :class:`.Group` objects. Every address in :attr:`.addresses` - appears in one of the group objects in the tuple. Addresses that are not - syntactically part of a group are represented by ``Group`` objects whose - ``name`` is ``None``. - - In addition to addresses in string form, any combination of - :class:`.Address` and :class:`.Group` objects, singly or in a list, may be - assigned to an address header. - - -.. class:: Address(display_name='', username='', domain='', addr_spec=None): - - The class used to represent an email address. The general form of an - address is:: - - [display_name] - - or:: - - username@domain - - where each part must conform to specific syntax rules spelled out in - :rfc:`5322`. - - As a convenience *addr_spec* can be specified instead of *username* and - *domain*, in which case *username* and *domain* will be parsed from the - *addr_spec*. An *addr_spec* must be a properly RFC quoted string; if it is - not ``Address`` will raise an error. Unicode characters are allowed and - will be property encoded when serialized. However, per the RFCs, unicode is - *not* allowed in the username portion of the address. - - .. attribute:: display_name - - The display name portion of the address, if any, with all quoting - removed. If the address does not have a display name, this attribute - will be an empty string. - - .. attribute:: username - - The ``username`` portion of the address, with all quoting removed. - - .. attribute:: domain - - The ``domain`` portion of the address. - - .. attribute:: addr_spec - - The ``username@domain`` portion of the address, correctly quoted - for use as a bare address (the second form shown above). This - attribute is not mutable. - - .. method:: __str__() - - The ``str`` value of the object is the address quoted according to - :rfc:`5322` rules, but with no Content Transfer Encoding of any non-ASCII - characters. - - -.. class:: Group(display_name=None, addresses=None) - - The class used to represent an address group. The general form of an - address group is:: - - display_name: [address-list]; - - As a convenience for processing lists of addresses that consist of a mixture - of groups and single addresses, a ``Group`` may also be used to represent - single addresses that are not part of a group by setting *display_name* to - ``None`` and providing a list of the single address as *addresses*. - - .. attribute:: display_name - - The ``display_name`` of the group. If it is ``None`` and there is - exactly one ``Address`` in ``addresses``, then the ``Group`` represents a - single address that is not in a group. - - .. attribute:: addresses - - A possibly empty tuple of :class:`.Address` objects representing the - addresses in the group. - - .. method:: __str__() - - The ``str`` value of a ``Group`` is formatted according to :rfc:`5322`, - but with no Content Transfer Encoding of any non-ASCII characters. If - ``display_name`` is none and there is a single ``Address`` in the - ``addresses` list, the ``str`` value will be the same as the ``str`` of - that single ``Address``. +The custom header objects and their attributes are described in +:mod:`~email.headerregistry`. diff --git a/Lib/email/_headerregistry.py b/Lib/email/_headerregistry.py deleted file mode 100644 index 6588546..0000000 --- a/Lib/email/_headerregistry.py +++ /dev/null @@ -1,456 +0,0 @@ -"""Representing and manipulating email headers via custom objects. - -This module provides an implementation of the HeaderRegistry API. -The implementation is designed to flexibly follow RFC5322 rules. - -Eventually HeaderRegistry will be a public API, but it isn't yet, -and will probably change some before that happens. - -""" - -from email import utils -from email import errors -from email import _header_value_parser as parser - -class Address: - - def __init__(self, display_name='', username='', domain='', addr_spec=None): - """Create an object represeting a full email address. - - An address can have a 'display_name', a 'username', and a 'domain'. In - addition to specifying the username and domain separately, they may be - specified together by using the addr_spec keyword *instead of* the - username and domain keywords. If an addr_spec string is specified it - must be properly quoted according to RFC 5322 rules; an error will be - raised if it is not. - - An Address object has display_name, username, domain, and addr_spec - attributes, all of which are read-only. The addr_spec and the string - value of the object are both quoted according to RFC5322 rules, but - without any Content Transfer Encoding. - - """ - # This clause with its potential 'raise' may only happen when an - # application program creates an Address object using an addr_spec - # keyword. The email library code itself must always supply username - # and domain. - if addr_spec is not None: - if username or domain: - raise TypeError("addrspec specified when username and/or " - "domain also specified") - a_s, rest = parser.get_addr_spec(addr_spec) - if rest: - raise ValueError("Invalid addr_spec; only '{}' " - "could be parsed from '{}'".format( - a_s, addr_spec)) - if a_s.all_defects: - raise a_s.all_defects[0] - username = a_s.local_part - domain = a_s.domain - self._display_name = display_name - self._username = username - self._domain = domain - - @property - def display_name(self): - return self._display_name - - @property - def username(self): - return self._username - - @property - def domain(self): - return self._domain - - @property - def addr_spec(self): - """The addr_spec (username@domain) portion of the address, quoted - according to RFC 5322 rules, but with no Content Transfer Encoding. - """ - nameset = set(self.username) - if len(nameset) > len(nameset-parser.DOT_ATOM_ENDS): - lp = parser.quote_string(self.username) - else: - lp = self.username - if self.domain: - return lp + '@' + self.domain - if not lp: - return '<>' - return lp - - def __repr__(self): - return "Address(display_name={!r}, username={!r}, domain={!r})".format( - self.display_name, self.username, self.domain) - - def __str__(self): - nameset = set(self.display_name) - if len(nameset) > len(nameset-parser.SPECIALS): - disp = parser.quote_string(self.display_name) - else: - disp = self.display_name - if disp: - addr_spec = '' if self.addr_spec=='<>' else self.addr_spec - return "{} <{}>".format(disp, addr_spec) - return self.addr_spec - - def __eq__(self, other): - if type(other) != type(self): - return False - return (self.display_name == other.display_name and - self.username == other.username and - self.domain == other.domain) - - -class Group: - - def __init__(self, display_name=None, addresses=None): - """Create an object representing an address group. - - An address group consists of a display_name followed by colon and an - list of addresses (see Address) terminated by a semi-colon. The Group - is created by specifying a display_name and a possibly empty list of - Address objects. A Group can also be used to represent a single - address that is not in a group, which is convenient when manipulating - lists that are a combination of Groups and individual Addresses. In - this case the display_name should be set to None. In particular, the - string representation of a Group whose display_name is None is the same - as the Address object, if there is one and only one Address object in - the addresses list. - - """ - self._display_name = display_name - self._addresses = tuple(addresses) if addresses else tuple() - - @property - def display_name(self): - return self._display_name - - @property - def addresses(self): - return self._addresses - - def __repr__(self): - return "Group(display_name={!r}, addresses={!r}".format( - self.display_name, self.addresses) - - def __str__(self): - if self.display_name is None and len(self.addresses)==1: - return str(self.addresses[0]) - disp = self.display_name - if disp is not None: - nameset = set(disp) - if len(nameset) > len(nameset-parser.SPECIALS): - disp = parser.quote_string(disp) - adrstr = ", ".join(str(x) for x in self.addresses) - adrstr = ' ' + adrstr if adrstr else adrstr - return "{}:{};".format(disp, adrstr) - - def __eq__(self, other): - if type(other) != type(self): - return False - return (self.display_name == other.display_name and - self.addresses == other.addresses) - - -# Header Classes # - -class BaseHeader(str): - - """Base class for message headers. - - Implements generic behavior and provides tools for subclasses. - - A subclass must define a classmethod named 'parse' that takes an unfolded - value string and a dictionary as its arguments. The dictionary will - contain one key, 'defects', initialized to an empty list. After the call - the dictionary must contain two additional keys: parse_tree, set to the - parse tree obtained from parsing the header, and 'decoded', set to the - string value of the idealized representation of the data from the value. - (That is, encoded words are decoded, and values that have canonical - representations are so represented.) - - The defects key is intended to collect parsing defects, which the message - parser will subsequently dispose of as appropriate. The parser should not, - insofar as practical, raise any errors. Defects should be added to the - list instead. The standard header parsers register defects for RFC - compliance issues, for obsolete RFC syntax, and for unrecoverable parsing - errors. - - The parse method may add additional keys to the dictionary. In this case - the subclass must define an 'init' method, which will be passed the - dictionary as its keyword arguments. The method should use (usually by - setting them as the value of similarly named attributes) and remove all the - extra keys added by its parse method, and then use super to call its parent - class with the remaining arguments and keywords. - - The subclass should also make sure that a 'max_count' attribute is defined - that is either None or 1. XXX: need to better define this API. - - """ - - def __new__(cls, name, value): - kwds = {'defects': []} - cls.parse(value, kwds) - if utils._has_surrogates(kwds['decoded']): - kwds['decoded'] = utils._sanitize(kwds['decoded']) - self = str.__new__(cls, kwds['decoded']) - del kwds['decoded'] - self.init(name, **kwds) - return self - - def init(self, name, *, parse_tree, defects): - self._name = name - self._parse_tree = parse_tree - self._defects = defects - - @property - def name(self): - return self._name - - @property - def defects(self): - return tuple(self._defects) - - def __reduce__(self): - return ( - _reconstruct_header, - ( - self.__class__.__name__, - self.__class__.__bases__, - str(self), - ), - self.__dict__) - - @classmethod - def _reconstruct(cls, value): - return str.__new__(cls, value) - - def fold(self, *, policy): - """Fold header according to policy. - - The parsed representation of the header is folded according to - RFC5322 rules, as modified by the policy. If the parse tree - contains surrogateescaped bytes, the bytes are CTE encoded using - the charset 'unknown-8bit". - - Any non-ASCII characters in the parse tree are CTE encoded using - charset utf-8. XXX: make this a policy setting. - - The returned value is an ASCII-only string possibly containing linesep - characters, and ending with a linesep character. The string includes - the header name and the ': ' separator. - - """ - # At some point we need to only put fws here if it was in the source. - header = parser.Header([ - parser.HeaderLabel([ - parser.ValueTerminal(self.name, 'header-name'), - parser.ValueTerminal(':', 'header-sep')]), - parser.CFWSList([parser.WhiteSpaceTerminal(' ', 'fws')]), - self._parse_tree]) - return header.fold(policy=policy) - - -def _reconstruct_header(cls_name, bases, value): - return type(cls_name, bases, {})._reconstruct(value) - - -class UnstructuredHeader: - - max_count = None - value_parser = staticmethod(parser.get_unstructured) - - @classmethod - def parse(cls, value, kwds): - kwds['parse_tree'] = cls.value_parser(value) - kwds['decoded'] = str(kwds['parse_tree']) - - -class UniqueUnstructuredHeader(UnstructuredHeader): - - max_count = 1 - - -class DateHeader: - - """Header whose value consists of a single timestamp. - - Provides an additional attribute, datetime, which is either an aware - datetime using a timezone, or a naive datetime if the timezone - in the input string is -0000. Also accepts a datetime as input. - The 'value' attribute is the normalized form of the timestamp, - which means it is the output of format_datetime on the datetime. - """ - - max_count = None - - # This is used only for folding, not for creating 'decoded'. - value_parser = staticmethod(parser.get_unstructured) - - @classmethod - def parse(cls, value, kwds): - if not value: - kwds['defects'].append(errors.HeaderMissingRequiredValue()) - kwds['datetime'] = None - kwds['decoded'] = '' - kwds['parse_tree'] = parser.TokenList() - return - if isinstance(value, str): - value = utils.parsedate_to_datetime(value) - kwds['datetime'] = value - kwds['decoded'] = utils.format_datetime(kwds['datetime']) - kwds['parse_tree'] = cls.value_parser(kwds['decoded']) - - def init(self, *args, **kw): - self._datetime = kw.pop('datetime') - super().init(*args, **kw) - - @property - def datetime(self): - return self._datetime - - -class UniqueDateHeader(DateHeader): - - max_count = 1 - - -class AddressHeader: - - max_count = None - - @staticmethod - def value_parser(value): - address_list, value = parser.get_address_list(value) - assert not value, 'this should not happen' - return address_list - - @classmethod - def parse(cls, value, kwds): - if isinstance(value, str): - # We are translating here from the RFC language (address/mailbox) - # to our API language (group/address). - kwds['parse_tree'] = address_list = cls.value_parser(value) - groups = [] - for addr in address_list.addresses: - groups.append(Group(addr.display_name, - [Address(mb.display_name or '', - mb.local_part or '', - mb.domain or '') - for mb in addr.all_mailboxes])) - defects = list(address_list.all_defects) - else: - # Assume it is Address/Group stuff - if not hasattr(value, '__iter__'): - value = [value] - groups = [Group(None, [item]) if not hasattr(item, 'addresses') - else item - for item in value] - defects = [] - kwds['groups'] = groups - kwds['defects'] = defects - kwds['decoded'] = ', '.join([str(item) for item in groups]) - if 'parse_tree' not in kwds: - kwds['parse_tree'] = cls.value_parser(kwds['decoded']) - - def init(self, *args, **kw): - self._groups = tuple(kw.pop('groups')) - self._addresses = None - super().init(*args, **kw) - - @property - def groups(self): - return self._groups - - @property - def addresses(self): - if self._addresses is None: - self._addresses = tuple([address for group in self._groups - for address in group.addresses]) - return self._addresses - - -class UniqueAddressHeader(AddressHeader): - - max_count = 1 - - -class SingleAddressHeader(AddressHeader): - - @property - def address(self): - if len(self.addresses)!=1: - raise ValueError(("value of single address header {} is not " - "a single address").format(self.name)) - return self.addresses[0] - - -class UniqueSingleAddressHeader(SingleAddressHeader): - - max_count = 1 - - -# The header factory # - -_default_header_map = { - 'subject': UniqueUnstructuredHeader, - 'date': UniqueDateHeader, - 'resent-date': DateHeader, - 'orig-date': UniqueDateHeader, - 'sender': UniqueSingleAddressHeader, - 'resent-sender': SingleAddressHeader, - 'to': UniqueAddressHeader, - 'resent-to': AddressHeader, - 'cc': UniqueAddressHeader, - 'resent-cc': AddressHeader, - 'bcc': UniqueAddressHeader, - 'resent-bcc': AddressHeader, - 'from': UniqueAddressHeader, - 'resent-from': AddressHeader, - 'reply-to': UniqueAddressHeader, - } - -class HeaderRegistry: - - """A header_factory and header registry.""" - - def __init__(self, base_class=BaseHeader, default_class=UnstructuredHeader, - use_default_map=True): - """Create a header_factory that works with the Policy API. - - base_class is the class that will be the last class in the created - header class's __bases__ list. default_class is the class that will be - used if "name" (see __call__) does not appear in the registry. - use_default_map controls whether or not the default mapping of names to - specialized classes is copied in to the registry when the factory is - created. The default is True. - - """ - self.registry = {} - self.base_class = base_class - self.default_class = default_class - if use_default_map: - self.registry.update(_default_header_map) - - def map_to_type(self, name, cls): - """Register cls as the specialized class for handling "name" headers. - - """ - self.registry[name.lower()] = cls - - def __getitem__(self, name): - cls = self.registry.get(name.lower(), self.default_class) - return type('_'+cls.__name__, (cls, self.base_class), {}) - - def __call__(self, name, value): - """Create a header instance for header 'name' from 'value'. - - Creates a header instance by creating a specialized class for parsing - and representing the specified header by combining the factory - base_class with a specialized class from the registry or the - default_class, and passing the name and value to the constructed - class's constructor. - - """ - return self[name](name, value) diff --git a/Lib/email/headerregistry.py b/Lib/email/headerregistry.py new file mode 100644 index 0000000..6588546 --- /dev/null +++ b/Lib/email/headerregistry.py @@ -0,0 +1,456 @@ +"""Representing and manipulating email headers via custom objects. + +This module provides an implementation of the HeaderRegistry API. +The implementation is designed to flexibly follow RFC5322 rules. + +Eventually HeaderRegistry will be a public API, but it isn't yet, +and will probably change some before that happens. + +""" + +from email import utils +from email import errors +from email import _header_value_parser as parser + +class Address: + + def __init__(self, display_name='', username='', domain='', addr_spec=None): + """Create an object represeting a full email address. + + An address can have a 'display_name', a 'username', and a 'domain'. In + addition to specifying the username and domain separately, they may be + specified together by using the addr_spec keyword *instead of* the + username and domain keywords. If an addr_spec string is specified it + must be properly quoted according to RFC 5322 rules; an error will be + raised if it is not. + + An Address object has display_name, username, domain, and addr_spec + attributes, all of which are read-only. The addr_spec and the string + value of the object are both quoted according to RFC5322 rules, but + without any Content Transfer Encoding. + + """ + # This clause with its potential 'raise' may only happen when an + # application program creates an Address object using an addr_spec + # keyword. The email library code itself must always supply username + # and domain. + if addr_spec is not None: + if username or domain: + raise TypeError("addrspec specified when username and/or " + "domain also specified") + a_s, rest = parser.get_addr_spec(addr_spec) + if rest: + raise ValueError("Invalid addr_spec; only '{}' " + "could be parsed from '{}'".format( + a_s, addr_spec)) + if a_s.all_defects: + raise a_s.all_defects[0] + username = a_s.local_part + domain = a_s.domain + self._display_name = display_name + self._username = username + self._domain = domain + + @property + def display_name(self): + return self._display_name + + @property + def username(self): + return self._username + + @property + def domain(self): + return self._domain + + @property + def addr_spec(self): + """The addr_spec (username@domain) portion of the address, quoted + according to RFC 5322 rules, but with no Content Transfer Encoding. + """ + nameset = set(self.username) + if len(nameset) > len(nameset-parser.DOT_ATOM_ENDS): + lp = parser.quote_string(self.username) + else: + lp = self.username + if self.domain: + return lp + '@' + self.domain + if not lp: + return '<>' + return lp + + def __repr__(self): + return "Address(display_name={!r}, username={!r}, domain={!r})".format( + self.display_name, self.username, self.domain) + + def __str__(self): + nameset = set(self.display_name) + if len(nameset) > len(nameset-parser.SPECIALS): + disp = parser.quote_string(self.display_name) + else: + disp = self.display_name + if disp: + addr_spec = '' if self.addr_spec=='<>' else self.addr_spec + return "{} <{}>".format(disp, addr_spec) + return self.addr_spec + + def __eq__(self, other): + if type(other) != type(self): + return False + return (self.display_name == other.display_name and + self.username == other.username and + self.domain == other.domain) + + +class Group: + + def __init__(self, display_name=None, addresses=None): + """Create an object representing an address group. + + An address group consists of a display_name followed by colon and an + list of addresses (see Address) terminated by a semi-colon. The Group + is created by specifying a display_name and a possibly empty list of + Address objects. A Group can also be used to represent a single + address that is not in a group, which is convenient when manipulating + lists that are a combination of Groups and individual Addresses. In + this case the display_name should be set to None. In particular, the + string representation of a Group whose display_name is None is the same + as the Address object, if there is one and only one Address object in + the addresses list. + + """ + self._display_name = display_name + self._addresses = tuple(addresses) if addresses else tuple() + + @property + def display_name(self): + return self._display_name + + @property + def addresses(self): + return self._addresses + + def __repr__(self): + return "Group(display_name={!r}, addresses={!r}".format( + self.display_name, self.addresses) + + def __str__(self): + if self.display_name is None and len(self.addresses)==1: + return str(self.addresses[0]) + disp = self.display_name + if disp is not None: + nameset = set(disp) + if len(nameset) > len(nameset-parser.SPECIALS): + disp = parser.quote_string(disp) + adrstr = ", ".join(str(x) for x in self.addresses) + adrstr = ' ' + adrstr if adrstr else adrstr + return "{}:{};".format(disp, adrstr) + + def __eq__(self, other): + if type(other) != type(self): + return False + return (self.display_name == other.display_name and + self.addresses == other.addresses) + + +# Header Classes # + +class BaseHeader(str): + + """Base class for message headers. + + Implements generic behavior and provides tools for subclasses. + + A subclass must define a classmethod named 'parse' that takes an unfolded + value string and a dictionary as its arguments. The dictionary will + contain one key, 'defects', initialized to an empty list. After the call + the dictionary must contain two additional keys: parse_tree, set to the + parse tree obtained from parsing the header, and 'decoded', set to the + string value of the idealized representation of the data from the value. + (That is, encoded words are decoded, and values that have canonical + representations are so represented.) + + The defects key is intended to collect parsing defects, which the message + parser will subsequently dispose of as appropriate. The parser should not, + insofar as practical, raise any errors. Defects should be added to the + list instead. The standard header parsers register defects for RFC + compliance issues, for obsolete RFC syntax, and for unrecoverable parsing + errors. + + The parse method may add additional keys to the dictionary. In this case + the subclass must define an 'init' method, which will be passed the + dictionary as its keyword arguments. The method should use (usually by + setting them as the value of similarly named attributes) and remove all the + extra keys added by its parse method, and then use super to call its parent + class with the remaining arguments and keywords. + + The subclass should also make sure that a 'max_count' attribute is defined + that is either None or 1. XXX: need to better define this API. + + """ + + def __new__(cls, name, value): + kwds = {'defects': []} + cls.parse(value, kwds) + if utils._has_surrogates(kwds['decoded']): + kwds['decoded'] = utils._sanitize(kwds['decoded']) + self = str.__new__(cls, kwds['decoded']) + del kwds['decoded'] + self.init(name, **kwds) + return self + + def init(self, name, *, parse_tree, defects): + self._name = name + self._parse_tree = parse_tree + self._defects = defects + + @property + def name(self): + return self._name + + @property + def defects(self): + return tuple(self._defects) + + def __reduce__(self): + return ( + _reconstruct_header, + ( + self.__class__.__name__, + self.__class__.__bases__, + str(self), + ), + self.__dict__) + + @classmethod + def _reconstruct(cls, value): + return str.__new__(cls, value) + + def fold(self, *, policy): + """Fold header according to policy. + + The parsed representation of the header is folded according to + RFC5322 rules, as modified by the policy. If the parse tree + contains surrogateescaped bytes, the bytes are CTE encoded using + the charset 'unknown-8bit". + + Any non-ASCII characters in the parse tree are CTE encoded using + charset utf-8. XXX: make this a policy setting. + + The returned value is an ASCII-only string possibly containing linesep + characters, and ending with a linesep character. The string includes + the header name and the ': ' separator. + + """ + # At some point we need to only put fws here if it was in the source. + header = parser.Header([ + parser.HeaderLabel([ + parser.ValueTerminal(self.name, 'header-name'), + parser.ValueTerminal(':', 'header-sep')]), + parser.CFWSList([parser.WhiteSpaceTerminal(' ', 'fws')]), + self._parse_tree]) + return header.fold(policy=policy) + + +def _reconstruct_header(cls_name, bases, value): + return type(cls_name, bases, {})._reconstruct(value) + + +class UnstructuredHeader: + + max_count = None + value_parser = staticmethod(parser.get_unstructured) + + @classmethod + def parse(cls, value, kwds): + kwds['parse_tree'] = cls.value_parser(value) + kwds['decoded'] = str(kwds['parse_tree']) + + +class UniqueUnstructuredHeader(UnstructuredHeader): + + max_count = 1 + + +class DateHeader: + + """Header whose value consists of a single timestamp. + + Provides an additional attribute, datetime, which is either an aware + datetime using a timezone, or a naive datetime if the timezone + in the input string is -0000. Also accepts a datetime as input. + The 'value' attribute is the normalized form of the timestamp, + which means it is the output of format_datetime on the datetime. + """ + + max_count = None + + # This is used only for folding, not for creating 'decoded'. + value_parser = staticmethod(parser.get_unstructured) + + @classmethod + def parse(cls, value, kwds): + if not value: + kwds['defects'].append(errors.HeaderMissingRequiredValue()) + kwds['datetime'] = None + kwds['decoded'] = '' + kwds['parse_tree'] = parser.TokenList() + return + if isinstance(value, str): + value = utils.parsedate_to_datetime(value) + kwds['datetime'] = value + kwds['decoded'] = utils.format_datetime(kwds['datetime']) + kwds['parse_tree'] = cls.value_parser(kwds['decoded']) + + def init(self, *args, **kw): + self._datetime = kw.pop('datetime') + super().init(*args, **kw) + + @property + def datetime(self): + return self._datetime + + +class UniqueDateHeader(DateHeader): + + max_count = 1 + + +class AddressHeader: + + max_count = None + + @staticmethod + def value_parser(value): + address_list, value = parser.get_address_list(value) + assert not value, 'this should not happen' + return address_list + + @classmethod + def parse(cls, value, kwds): + if isinstance(value, str): + # We are translating here from the RFC language (address/mailbox) + # to our API language (group/address). + kwds['parse_tree'] = address_list = cls.value_parser(value) + groups = [] + for addr in address_list.addresses: + groups.append(Group(addr.display_name, + [Address(mb.display_name or '', + mb.local_part or '', + mb.domain or '') + for mb in addr.all_mailboxes])) + defects = list(address_list.all_defects) + else: + # Assume it is Address/Group stuff + if not hasattr(value, '__iter__'): + value = [value] + groups = [Group(None, [item]) if not hasattr(item, 'addresses') + else item + for item in value] + defects = [] + kwds['groups'] = groups + kwds['defects'] = defects + kwds['decoded'] = ', '.join([str(item) for item in groups]) + if 'parse_tree' not in kwds: + kwds['parse_tree'] = cls.value_parser(kwds['decoded']) + + def init(self, *args, **kw): + self._groups = tuple(kw.pop('groups')) + self._addresses = None + super().init(*args, **kw) + + @property + def groups(self): + return self._groups + + @property + def addresses(self): + if self._addresses is None: + self._addresses = tuple([address for group in self._groups + for address in group.addresses]) + return self._addresses + + +class UniqueAddressHeader(AddressHeader): + + max_count = 1 + + +class SingleAddressHeader(AddressHeader): + + @property + def address(self): + if len(self.addresses)!=1: + raise ValueError(("value of single address header {} is not " + "a single address").format(self.name)) + return self.addresses[0] + + +class UniqueSingleAddressHeader(SingleAddressHeader): + + max_count = 1 + + +# The header factory # + +_default_header_map = { + 'subject': UniqueUnstructuredHeader, + 'date': UniqueDateHeader, + 'resent-date': DateHeader, + 'orig-date': UniqueDateHeader, + 'sender': UniqueSingleAddressHeader, + 'resent-sender': SingleAddressHeader, + 'to': UniqueAddressHeader, + 'resent-to': AddressHeader, + 'cc': UniqueAddressHeader, + 'resent-cc': AddressHeader, + 'bcc': UniqueAddressHeader, + 'resent-bcc': AddressHeader, + 'from': UniqueAddressHeader, + 'resent-from': AddressHeader, + 'reply-to': UniqueAddressHeader, + } + +class HeaderRegistry: + + """A header_factory and header registry.""" + + def __init__(self, base_class=BaseHeader, default_class=UnstructuredHeader, + use_default_map=True): + """Create a header_factory that works with the Policy API. + + base_class is the class that will be the last class in the created + header class's __bases__ list. default_class is the class that will be + used if "name" (see __call__) does not appear in the registry. + use_default_map controls whether or not the default mapping of names to + specialized classes is copied in to the registry when the factory is + created. The default is True. + + """ + self.registry = {} + self.base_class = base_class + self.default_class = default_class + if use_default_map: + self.registry.update(_default_header_map) + + def map_to_type(self, name, cls): + """Register cls as the specialized class for handling "name" headers. + + """ + self.registry[name.lower()] = cls + + def __getitem__(self, name): + cls = self.registry.get(name.lower(), self.default_class) + return type('_'+cls.__name__, (cls, self.base_class), {}) + + def __call__(self, name, value): + """Create a header instance for header 'name' from 'value'. + + Creates a header instance by creating a specialized class for parsing + and representing the specified header by combining the factory + base_class with a specialized class from the registry or the + default_class, and passing the name and value to the constructed + class's constructor. + + """ + return self[name](name, value) diff --git a/Lib/email/policy.py b/Lib/email/policy.py index 18946c3..47ed66b 100644 --- a/Lib/email/policy.py +++ b/Lib/email/policy.py @@ -4,7 +4,7 @@ code that adds all the email6 features. from email._policybase import Policy, Compat32, compat32 from email.utils import _has_surrogates -from email._headerregistry import HeaderRegistry as _HeaderRegistry +from email.headerregistry import HeaderRegistry as HeaderRegistry __all__ = [ 'Compat32', @@ -60,13 +60,13 @@ class EmailPolicy(Policy): """ refold_source = 'long' - header_factory = _HeaderRegistry() + header_factory = HeaderRegistry() def __init__(self, **kw): # Ensure that each new instance gets a unique header factory # (as opposed to clones, which share the factory). if 'header_factory' not in kw: - object.__setattr__(self, 'header_factory', _HeaderRegistry()) + object.__setattr__(self, 'header_factory', HeaderRegistry()) super().__init__(**kw) # The logic of the next three methods is chosen such that it is possible to diff --git a/Lib/test/test_email/test__headerregistry.py b/Lib/test/test_email/test__headerregistry.py deleted file mode 100644 index 23bc5ff..0000000 --- a/Lib/test/test_email/test__headerregistry.py +++ /dev/null @@ -1,739 +0,0 @@ -import datetime -import textwrap -import unittest -from email import errors -from email import policy -from email.message import Message -from test.test_email import TestEmailBase -from email import _headerregistry -# Address and Group are public but I'm not sure where to put them yet. -from email._headerregistry import Address, Group - - -class TestHeaderRegistry(TestEmailBase): - - def test_arbitrary_name_unstructured(self): - factory = _headerregistry.HeaderRegistry() - h = factory('foobar', 'test') - self.assertIsInstance(h, _headerregistry.BaseHeader) - self.assertIsInstance(h, _headerregistry.UnstructuredHeader) - - def test_name_case_ignored(self): - factory = _headerregistry.HeaderRegistry() - # Whitebox check that test is valid - self.assertNotIn('Subject', factory.registry) - h = factory('Subject', 'test') - self.assertIsInstance(h, _headerregistry.BaseHeader) - self.assertIsInstance(h, _headerregistry.UniqueUnstructuredHeader) - - class FooBase: - def __init__(self, *args, **kw): - pass - - def test_override_default_base_class(self): - factory = _headerregistry.HeaderRegistry(base_class=self.FooBase) - h = factory('foobar', 'test') - self.assertIsInstance(h, self.FooBase) - self.assertIsInstance(h, _headerregistry.UnstructuredHeader) - - class FooDefault: - parse = _headerregistry.UnstructuredHeader.parse - - def test_override_default_class(self): - factory = _headerregistry.HeaderRegistry(default_class=self.FooDefault) - h = factory('foobar', 'test') - self.assertIsInstance(h, _headerregistry.BaseHeader) - self.assertIsInstance(h, self.FooDefault) - - def test_override_default_class_only_overrides_default(self): - factory = _headerregistry.HeaderRegistry(default_class=self.FooDefault) - h = factory('subject', 'test') - self.assertIsInstance(h, _headerregistry.BaseHeader) - self.assertIsInstance(h, _headerregistry.UniqueUnstructuredHeader) - - def test_dont_use_default_map(self): - factory = _headerregistry.HeaderRegistry(use_default_map=False) - h = factory('subject', 'test') - self.assertIsInstance(h, _headerregistry.BaseHeader) - self.assertIsInstance(h, _headerregistry.UnstructuredHeader) - - def test_map_to_type(self): - factory = _headerregistry.HeaderRegistry() - h1 = factory('foobar', 'test') - factory.map_to_type('foobar', _headerregistry.UniqueUnstructuredHeader) - h2 = factory('foobar', 'test') - self.assertIsInstance(h1, _headerregistry.BaseHeader) - self.assertIsInstance(h1, _headerregistry.UnstructuredHeader) - self.assertIsInstance(h2, _headerregistry.BaseHeader) - self.assertIsInstance(h2, _headerregistry.UniqueUnstructuredHeader) - - -class TestHeaderBase(TestEmailBase): - - factory = _headerregistry.HeaderRegistry() - - def make_header(self, name, value): - return self.factory(name, value) - - -class TestBaseHeaderFeatures(TestHeaderBase): - - def test_str(self): - h = self.make_header('subject', 'this is a test') - self.assertIsInstance(h, str) - self.assertEqual(h, 'this is a test') - self.assertEqual(str(h), 'this is a test') - - def test_substr(self): - h = self.make_header('subject', 'this is a test') - self.assertEqual(h[5:7], 'is') - - def test_has_name(self): - h = self.make_header('subject', 'this is a test') - self.assertEqual(h.name, 'subject') - - def _test_attr_ro(self, attr): - h = self.make_header('subject', 'this is a test') - with self.assertRaises(AttributeError): - setattr(h, attr, 'foo') - - def test_name_read_only(self): - self._test_attr_ro('name') - - def test_defects_read_only(self): - self._test_attr_ro('defects') - - def test_defects_is_tuple(self): - h = self.make_header('subject', 'this is a test') - self.assertEqual(len(h.defects), 0) - self.assertIsInstance(h.defects, tuple) - # Make sure it is still true when there are defects. - h = self.make_header('date', '') - self.assertEqual(len(h.defects), 1) - self.assertIsInstance(h.defects, tuple) - - # XXX: FIXME - #def test_CR_in_value(self): - # # XXX: this also re-raises the issue of embedded headers, - # # need test and solution for that. - # value = '\r'.join(['this is', ' a test']) - # h = self.make_header('subject', value) - # self.assertEqual(h, value) - # self.assertDefectsEqual(h.defects, [errors.ObsoleteHeaderDefect]) - - def test_RFC2047_value_decoded(self): - value = '=?utf-8?q?this_is_a_test?=' - h = self.make_header('subject', value) - self.assertEqual(h, 'this is a test') - - -class TestDateHeader(TestHeaderBase): - - datestring = 'Sun, 23 Sep 2001 20:10:55 -0700' - utcoffset = datetime.timedelta(hours=-7) - tz = datetime.timezone(utcoffset) - dt = datetime.datetime(2001, 9, 23, 20, 10, 55, tzinfo=tz) - - def test_parse_date(self): - h = self.make_header('date', self.datestring) - self.assertEqual(h, self.datestring) - self.assertEqual(h.datetime, self.dt) - self.assertEqual(h.datetime.utcoffset(), self.utcoffset) - self.assertEqual(h.defects, ()) - - def test_set_from_datetime(self): - h = self.make_header('date', self.dt) - self.assertEqual(h, self.datestring) - self.assertEqual(h.datetime, self.dt) - self.assertEqual(h.defects, ()) - - def test_date_header_properties(self): - h = self.make_header('date', self.datestring) - self.assertIsInstance(h, _headerregistry.UniqueDateHeader) - self.assertEqual(h.max_count, 1) - self.assertEqual(h.defects, ()) - - def test_resent_date_header_properties(self): - h = self.make_header('resent-date', self.datestring) - self.assertIsInstance(h, _headerregistry.DateHeader) - self.assertEqual(h.max_count, None) - self.assertEqual(h.defects, ()) - - def test_no_value_is_defect(self): - h = self.make_header('date', '') - self.assertEqual(len(h.defects), 1) - self.assertIsInstance(h.defects[0], errors.HeaderMissingRequiredValue) - - def test_datetime_read_only(self): - h = self.make_header('date', self.datestring) - with self.assertRaises(AttributeError): - h.datetime = 'foo' - - def test_set_date_header_from_datetime(self): - m = Message(policy=policy.default) - m['Date'] = self.dt - self.assertEqual(m['Date'], self.datestring) - self.assertEqual(m['Date'].datetime, self.dt) - - -class TestAddressHeader(TestHeaderBase): - - examples = { - - 'empty': - ('<>', - [errors.InvalidHeaderDefect], - '<>', - '', - '<>', - '', - '', - None), - - 'address_only': - ('zippy@pinhead.com', - [], - 'zippy@pinhead.com', - '', - 'zippy@pinhead.com', - 'zippy', - 'pinhead.com', - None), - - 'name_and_address': - ('Zaphrod Beblebrux ', - [], - 'Zaphrod Beblebrux ', - 'Zaphrod Beblebrux', - 'zippy@pinhead.com', - 'zippy', - 'pinhead.com', - None), - - 'quoted_local_part': - ('Zaphrod Beblebrux <"foo bar"@pinhead.com>', - [], - 'Zaphrod Beblebrux <"foo bar"@pinhead.com>', - 'Zaphrod Beblebrux', - '"foo bar"@pinhead.com', - 'foo bar', - 'pinhead.com', - None), - - 'quoted_parens_in_name': - (r'"A \(Special\) Person" ', - [], - '"A (Special) Person" ', - 'A (Special) Person', - 'person@dom.ain', - 'person', - 'dom.ain', - None), - - 'quoted_backslashes_in_name': - (r'"Arthur \\Backslash\\ Foobar" ', - [], - r'"Arthur \\Backslash\\ Foobar" ', - r'Arthur \Backslash\ Foobar', - 'person@dom.ain', - 'person', - 'dom.ain', - None), - - 'name_with_dot': - ('John X. Doe ', - [errors.ObsoleteHeaderDefect], - '"John X. Doe" ', - 'John X. Doe', - 'jxd@example.com', - 'jxd', - 'example.com', - None), - - 'quoted_strings_in_local_part': - ('""example" example"@example.com', - [errors.InvalidHeaderDefect]*3, - '"example example"@example.com', - '', - '"example example"@example.com', - 'example example', - 'example.com', - None), - - 'escaped_quoted_strings_in_local_part': - (r'"\"example\" example"@example.com', - [], - r'"\"example\" example"@example.com', - '', - r'"\"example\" example"@example.com', - r'"example" example', - 'example.com', - None), - - 'escaped_escapes_in_local_part': - (r'"\\"example\\" example"@example.com', - [errors.InvalidHeaderDefect]*5, - r'"\\example\\\\ example"@example.com', - '', - r'"\\example\\\\ example"@example.com', - r'\example\\ example', - 'example.com', - None), - - 'spaces_in_unquoted_local_part_collapsed': - ('merwok wok @example.com', - [errors.InvalidHeaderDefect]*2, - '"merwok wok"@example.com', - '', - '"merwok wok"@example.com', - 'merwok wok', - 'example.com', - None), - - 'spaces_around_dots_in_local_part_removed': - ('merwok. wok . wok@example.com', - [errors.ObsoleteHeaderDefect], - 'merwok.wok.wok@example.com', - '', - 'merwok.wok.wok@example.com', - 'merwok.wok.wok', - 'example.com', - None), - - } - - # XXX: Need many more examples, and in particular some with names in - # trailing comments, which aren't currently handled. comments in - # general are not handled yet. - - def _test_single_addr(self, source, defects, decoded, display_name, - addr_spec, username, domain, comment): - h = self.make_header('sender', source) - self.assertEqual(h, decoded) - self.assertDefectsEqual(h.defects, defects) - a = h.address - self.assertEqual(str(a), decoded) - self.assertEqual(len(h.groups), 1) - self.assertEqual([a], list(h.groups[0].addresses)) - self.assertEqual([a], list(h.addresses)) - self.assertEqual(a.display_name, display_name) - self.assertEqual(a.addr_spec, addr_spec) - self.assertEqual(a.username, username) - self.assertEqual(a.domain, domain) - # XXX: we have no comment support yet. - #self.assertEqual(a.comment, comment) - - for name in examples: - locals()['test_'+name] = ( - lambda self, name=name: - self._test_single_addr(*self.examples[name])) - - def _test_group_single_addr(self, source, defects, decoded, display_name, - addr_spec, username, domain, comment): - source = 'foo: {};'.format(source) - gdecoded = 'foo: {};'.format(decoded) if decoded else 'foo:;' - h = self.make_header('to', source) - self.assertEqual(h, gdecoded) - self.assertDefectsEqual(h.defects, defects) - self.assertEqual(h.groups[0].addresses, h.addresses) - self.assertEqual(len(h.groups), 1) - self.assertEqual(len(h.addresses), 1) - a = h.addresses[0] - self.assertEqual(str(a), decoded) - self.assertEqual(a.display_name, display_name) - self.assertEqual(a.addr_spec, addr_spec) - self.assertEqual(a.username, username) - self.assertEqual(a.domain, domain) - - for name in examples: - locals()['test_group_'+name] = ( - lambda self, name=name: - self._test_group_single_addr(*self.examples[name])) - - def test_simple_address_list(self): - value = ('Fred , foo@example.com, ' - '"Harry W. Hastings" ') - h = self.make_header('to', value) - self.assertEqual(h, value) - self.assertEqual(len(h.groups), 3) - self.assertEqual(len(h.addresses), 3) - for i in range(3): - self.assertEqual(h.groups[i].addresses[0], h.addresses[i]) - self.assertEqual(str(h.addresses[0]), 'Fred ') - self.assertEqual(str(h.addresses[1]), 'foo@example.com') - self.assertEqual(str(h.addresses[2]), - '"Harry W. Hastings" ') - self.assertEqual(h.addresses[2].display_name, - 'Harry W. Hastings') - - def test_complex_address_list(self): - examples = list(self.examples.values()) - source = ('dummy list:;, another: (empty);,' + - ', '.join([x[0] for x in examples[:4]]) + ', ' + - r'"A \"list\"": ' + - ', '.join([x[0] for x in examples[4:6]]) + ';,' + - ', '.join([x[0] for x in examples[6:]]) - ) - # XXX: the fact that (empty) disappears here is a potential API design - # bug. We don't currently have a way to preserve comments. - expected = ('dummy list:;, another:;, ' + - ', '.join([x[2] for x in examples[:4]]) + ', ' + - r'"A \"list\"": ' + - ', '.join([x[2] for x in examples[4:6]]) + ';, ' + - ', '.join([x[2] for x in examples[6:]]) - ) - - h = self.make_header('to', source) - self.assertEqual(h.split(','), expected.split(',')) - self.assertEqual(h, expected) - self.assertEqual(len(h.groups), 7 + len(examples) - 6) - self.assertEqual(h.groups[0].display_name, 'dummy list') - self.assertEqual(h.groups[1].display_name, 'another') - self.assertEqual(h.groups[6].display_name, 'A "list"') - self.assertEqual(len(h.addresses), len(examples)) - for i in range(4): - self.assertIsNone(h.groups[i+2].display_name) - self.assertEqual(str(h.groups[i+2].addresses[0]), examples[i][2]) - for i in range(7, 7 + len(examples) - 6): - self.assertIsNone(h.groups[i].display_name) - self.assertEqual(str(h.groups[i].addresses[0]), examples[i-1][2]) - for i in range(len(examples)): - self.assertEqual(str(h.addresses[i]), examples[i][2]) - self.assertEqual(h.addresses[i].addr_spec, examples[i][4]) - - def test_address_read_only(self): - h = self.make_header('sender', 'abc@xyz.com') - with self.assertRaises(AttributeError): - h.address = 'foo' - - def test_addresses_read_only(self): - h = self.make_header('sender', 'abc@xyz.com') - with self.assertRaises(AttributeError): - h.addresses = 'foo' - - def test_groups_read_only(self): - h = self.make_header('sender', 'abc@xyz.com') - with self.assertRaises(AttributeError): - h.groups = 'foo' - - def test_addresses_types(self): - source = 'me ' - h = self.make_header('to', source) - self.assertIsInstance(h.addresses, tuple) - self.assertIsInstance(h.addresses[0], Address) - - def test_groups_types(self): - source = 'me ' - h = self.make_header('to', source) - self.assertIsInstance(h.groups, tuple) - self.assertIsInstance(h.groups[0], Group) - - def test_set_from_Address(self): - h = self.make_header('to', Address('me', 'foo', 'example.com')) - self.assertEqual(h, 'me ') - - def test_set_from_Address_list(self): - h = self.make_header('to', [Address('me', 'foo', 'example.com'), - Address('you', 'bar', 'example.com')]) - self.assertEqual(h, 'me , you ') - - def test_set_from_Address_and_Group_list(self): - h = self.make_header('to', [Address('me', 'foo', 'example.com'), - Group('bing', [Address('fiz', 'z', 'b.com'), - Address('zif', 'f', 'c.com')]), - Address('you', 'bar', 'example.com')]) - self.assertEqual(h, 'me , bing: fiz , ' - 'zif ;, you ') - self.assertEqual(h.fold(policy=policy.default.clone(max_line_length=40)), - 'to: me ,\n' - ' bing: fiz , zif ;,\n' - ' you \n') - - def test_set_from_Group_list(self): - h = self.make_header('to', [Group('bing', [Address('fiz', 'z', 'b.com'), - Address('zif', 'f', 'c.com')])]) - self.assertEqual(h, 'bing: fiz , zif ;') - - -class TestAddressAndGroup(TestEmailBase): - - def _test_attr_ro(self, obj, attr): - with self.assertRaises(AttributeError): - setattr(obj, attr, 'foo') - - def test_address_display_name_ro(self): - self._test_attr_ro(Address('foo', 'bar', 'baz'), 'display_name') - - def test_address_username_ro(self): - self._test_attr_ro(Address('foo', 'bar', 'baz'), 'username') - - def test_address_domain_ro(self): - self._test_attr_ro(Address('foo', 'bar', 'baz'), 'domain') - - def test_group_display_name_ro(self): - self._test_attr_ro(Group('foo'), 'display_name') - - def test_group_addresses_ro(self): - self._test_attr_ro(Group('foo'), 'addresses') - - def test_address_from_username_domain(self): - a = Address('foo', 'bar', 'baz') - self.assertEqual(a.display_name, 'foo') - self.assertEqual(a.username, 'bar') - self.assertEqual(a.domain, 'baz') - self.assertEqual(a.addr_spec, 'bar@baz') - self.assertEqual(str(a), 'foo ') - - def test_address_from_addr_spec(self): - a = Address('foo', addr_spec='bar@baz') - self.assertEqual(a.display_name, 'foo') - self.assertEqual(a.username, 'bar') - self.assertEqual(a.domain, 'baz') - self.assertEqual(a.addr_spec, 'bar@baz') - self.assertEqual(str(a), 'foo ') - - def test_address_with_no_display_name(self): - a = Address(addr_spec='bar@baz') - self.assertEqual(a.display_name, '') - self.assertEqual(a.username, 'bar') - self.assertEqual(a.domain, 'baz') - self.assertEqual(a.addr_spec, 'bar@baz') - self.assertEqual(str(a), 'bar@baz') - - def test_null_address(self): - a = Address() - self.assertEqual(a.display_name, '') - self.assertEqual(a.username, '') - self.assertEqual(a.domain, '') - self.assertEqual(a.addr_spec, '<>') - self.assertEqual(str(a), '<>') - - def test_domain_only(self): - # This isn't really a valid address. - a = Address(domain='buzz') - self.assertEqual(a.display_name, '') - self.assertEqual(a.username, '') - self.assertEqual(a.domain, 'buzz') - self.assertEqual(a.addr_spec, '@buzz') - self.assertEqual(str(a), '@buzz') - - def test_username_only(self): - # This isn't really a valid address. - a = Address(username='buzz') - self.assertEqual(a.display_name, '') - self.assertEqual(a.username, 'buzz') - self.assertEqual(a.domain, '') - self.assertEqual(a.addr_spec, 'buzz') - self.assertEqual(str(a), 'buzz') - - def test_display_name_only(self): - a = Address('buzz') - self.assertEqual(a.display_name, 'buzz') - self.assertEqual(a.username, '') - self.assertEqual(a.domain, '') - self.assertEqual(a.addr_spec, '<>') - self.assertEqual(str(a), 'buzz <>') - - def test_quoting(self): - # Ideally we'd check every special individually, but I'm not up for - # writing that many tests. - a = Address('Sara J.', 'bad name', 'example.com') - self.assertEqual(a.display_name, 'Sara J.') - self.assertEqual(a.username, 'bad name') - self.assertEqual(a.domain, 'example.com') - self.assertEqual(a.addr_spec, '"bad name"@example.com') - self.assertEqual(str(a), '"Sara J." <"bad name"@example.com>') - - def test_il8n(self): - a = Address('Éric', 'wok', 'exàmple.com') - self.assertEqual(a.display_name, 'Éric') - self.assertEqual(a.username, 'wok') - self.assertEqual(a.domain, 'exàmple.com') - self.assertEqual(a.addr_spec, 'wok@exàmple.com') - self.assertEqual(str(a), 'Éric ') - - # XXX: there is an API design issue that needs to be solved here. - #def test_non_ascii_username_raises(self): - # with self.assertRaises(ValueError): - # Address('foo', 'wők', 'example.com') - - def test_non_ascii_username_in_addr_spec_raises(self): - with self.assertRaises(ValueError): - Address('foo', addr_spec='wők@example.com') - - def test_address_addr_spec_and_username_raises(self): - with self.assertRaises(TypeError): - Address('foo', username='bing', addr_spec='bar@baz') - - def test_address_addr_spec_and_domain_raises(self): - with self.assertRaises(TypeError): - Address('foo', domain='bing', addr_spec='bar@baz') - - def test_address_addr_spec_and_username_and_domain_raises(self): - with self.assertRaises(TypeError): - Address('foo', username='bong', domain='bing', addr_spec='bar@baz') - - def test_space_in_addr_spec_username_raises(self): - with self.assertRaises(ValueError): - Address('foo', addr_spec="bad name@example.com") - - def test_bad_addr_sepc_raises(self): - with self.assertRaises(ValueError): - Address('foo', addr_spec="name@ex[]ample.com") - - def test_empty_group(self): - g = Group('foo') - self.assertEqual(g.display_name, 'foo') - self.assertEqual(g.addresses, tuple()) - self.assertEqual(str(g), 'foo:;') - - def test_empty_group_list(self): - g = Group('foo', addresses=[]) - self.assertEqual(g.display_name, 'foo') - self.assertEqual(g.addresses, tuple()) - self.assertEqual(str(g), 'foo:;') - - def test_null_group(self): - g = Group() - self.assertIsNone(g.display_name) - self.assertEqual(g.addresses, tuple()) - self.assertEqual(str(g), 'None:;') - - def test_group_with_addresses(self): - addrs = [Address('b', 'b', 'c'), Address('a', 'b','c')] - g = Group('foo', addrs) - self.assertEqual(g.display_name, 'foo') - self.assertEqual(g.addresses, tuple(addrs)) - self.assertEqual(str(g), 'foo: b , a ;') - - def test_group_with_addresses_no_display_name(self): - addrs = [Address('b', 'b', 'c'), Address('a', 'b','c')] - g = Group(addresses=addrs) - self.assertIsNone(g.display_name) - self.assertEqual(g.addresses, tuple(addrs)) - self.assertEqual(str(g), 'None: b , a ;') - - def test_group_with_one_address_no_display_name(self): - addrs = [Address('b', 'b', 'c')] - g = Group(addresses=addrs) - self.assertIsNone(g.display_name) - self.assertEqual(g.addresses, tuple(addrs)) - self.assertEqual(str(g), 'b ') - - def test_display_name_quoting(self): - g = Group('foo.bar') - self.assertEqual(g.display_name, 'foo.bar') - self.assertEqual(g.addresses, tuple()) - self.assertEqual(str(g), '"foo.bar":;') - - def test_display_name_blanks_not_quoted(self): - g = Group('foo bar') - self.assertEqual(g.display_name, 'foo bar') - self.assertEqual(g.addresses, tuple()) - self.assertEqual(str(g), 'foo bar:;') - - def test_set_message_header_from_address(self): - a = Address('foo', 'bar', 'example.com') - m = Message(policy=policy.default) - m['To'] = a - self.assertEqual(m['to'], 'foo ') - self.assertEqual(m['to'].addresses, (a,)) - - def test_set_message_header_from_group(self): - g = Group('foo bar') - m = Message(policy=policy.default) - m['To'] = g - self.assertEqual(m['to'], 'foo bar:;') - self.assertEqual(m['to'].addresses, g.addresses) - - -class TestFolding(TestHeaderBase): - - def test_short_unstructured(self): - h = self.make_header('subject', 'this is a test') - self.assertEqual(h.fold(policy=self.policy), - 'subject: this is a test\n') - - def test_long_unstructured(self): - h = self.make_header('Subject', 'This is a long header ' - 'line that will need to be folded into two lines ' - 'and will demonstrate basic folding') - self.assertEqual(h.fold(policy=self.policy), - 'Subject: This is a long header line that will ' - 'need to be folded into two lines\n' - ' and will demonstrate basic folding\n') - - def test_unstructured_short_max_line_length(self): - h = self.make_header('Subject', 'this is a short header ' - 'that will be folded anyway') - self.assertEqual( - h.fold(policy=policy.default.clone(max_line_length=20)), - textwrap.dedent("""\ - Subject: this is a - short header that - will be folded - anyway - """)) - - def test_fold_unstructured_single_word(self): - h = self.make_header('Subject', 'test') - self.assertEqual(h.fold(policy=self.policy), 'Subject: test\n') - - def test_fold_unstructured_short(self): - h = self.make_header('Subject', 'test test test') - self.assertEqual(h.fold(policy=self.policy), - 'Subject: test test test\n') - - def test_fold_unstructured_with_overlong_word(self): - h = self.make_header('Subject', 'thisisaverylonglineconsistingofa' - 'singlewordthatwontfit') - self.assertEqual( - h.fold(policy=policy.default.clone(max_line_length=20)), - 'Subject: thisisaverylonglineconsistingofasinglewordthatwontfit\n') - - def test_fold_unstructured_with_two_overlong_words(self): - h = self.make_header('Subject', 'thisisaverylonglineconsistingofa' - 'singlewordthatwontfit plusanotherverylongwordthatwontfit') - self.assertEqual( - h.fold(policy=policy.default.clone(max_line_length=20)), - 'Subject: thisisaverylonglineconsistingofasinglewordthatwontfit\n' - ' plusanotherverylongwordthatwontfit\n') - - def test_fold_unstructured_with_slightly_long_word(self): - h = self.make_header('Subject', 'thislongwordislessthanmaxlinelen') - self.assertEqual( - h.fold(policy=policy.default.clone(max_line_length=35)), - 'Subject:\n thislongwordislessthanmaxlinelen\n') - - def test_fold_unstructured_with_commas(self): - # The old wrapper would fold this at the commas. - h = self.make_header('Subject', "This header is intended to " - "demonstrate, in a fairly susinct way, that we now do " - "not give a , special treatment in unstructured headers.") - self.assertEqual( - h.fold(policy=policy.default.clone(max_line_length=60)), - textwrap.dedent("""\ - Subject: This header is intended to demonstrate, in a fairly - susinct way, that we now do not give a , special treatment - in unstructured headers. - """)) - - def test_fold_address_list(self): - h = self.make_header('To', '"Theodore H. Perfect" , ' - '"My address is very long because my name is long" , ' - '"Only A. Friend" ') - self.assertEqual(h.fold(policy=self.policy), textwrap.dedent("""\ - To: "Theodore H. Perfect" , - "My address is very long because my name is long" , - "Only A. Friend" - """)) - - def test_fold_date_header(self): - h = self.make_header('Date', 'Sat, 2 Feb 2002 17:00:06 -0800') - self.assertEqual(h.fold(policy=self.policy), - 'Date: Sat, 02 Feb 2002 17:00:06 -0800\n') - - - -if __name__ == '__main__': - unittest.main() diff --git a/Lib/test/test_email/test_headerregistry.py b/Lib/test/test_email/test_headerregistry.py new file mode 100644 index 0000000..fa8fd97 --- /dev/null +++ b/Lib/test/test_email/test_headerregistry.py @@ -0,0 +1,738 @@ +import datetime +import textwrap +import unittest +from email import errors +from email import policy +from email.message import Message +from test.test_email import TestEmailBase +from email import headerregistry +from email.headerregistry import Address, Group + + +class TestHeaderRegistry(TestEmailBase): + + def test_arbitrary_name_unstructured(self): + factory = headerregistry.HeaderRegistry() + h = factory('foobar', 'test') + self.assertIsInstance(h, headerregistry.BaseHeader) + self.assertIsInstance(h, headerregistry.UnstructuredHeader) + + def test_name_case_ignored(self): + factory = headerregistry.HeaderRegistry() + # Whitebox check that test is valid + self.assertNotIn('Subject', factory.registry) + h = factory('Subject', 'test') + self.assertIsInstance(h, headerregistry.BaseHeader) + self.assertIsInstance(h, headerregistry.UniqueUnstructuredHeader) + + class FooBase: + def __init__(self, *args, **kw): + pass + + def test_override_default_base_class(self): + factory = headerregistry.HeaderRegistry(base_class=self.FooBase) + h = factory('foobar', 'test') + self.assertIsInstance(h, self.FooBase) + self.assertIsInstance(h, headerregistry.UnstructuredHeader) + + class FooDefault: + parse = headerregistry.UnstructuredHeader.parse + + def test_override_default_class(self): + factory = headerregistry.HeaderRegistry(default_class=self.FooDefault) + h = factory('foobar', 'test') + self.assertIsInstance(h, headerregistry.BaseHeader) + self.assertIsInstance(h, self.FooDefault) + + def test_override_default_class_only_overrides_default(self): + factory = headerregistry.HeaderRegistry(default_class=self.FooDefault) + h = factory('subject', 'test') + self.assertIsInstance(h, headerregistry.BaseHeader) + self.assertIsInstance(h, headerregistry.UniqueUnstructuredHeader) + + def test_dont_use_default_map(self): + factory = headerregistry.HeaderRegistry(use_default_map=False) + h = factory('subject', 'test') + self.assertIsInstance(h, headerregistry.BaseHeader) + self.assertIsInstance(h, headerregistry.UnstructuredHeader) + + def test_map_to_type(self): + factory = headerregistry.HeaderRegistry() + h1 = factory('foobar', 'test') + factory.map_to_type('foobar', headerregistry.UniqueUnstructuredHeader) + h2 = factory('foobar', 'test') + self.assertIsInstance(h1, headerregistry.BaseHeader) + self.assertIsInstance(h1, headerregistry.UnstructuredHeader) + self.assertIsInstance(h2, headerregistry.BaseHeader) + self.assertIsInstance(h2, headerregistry.UniqueUnstructuredHeader) + + +class TestHeaderBase(TestEmailBase): + + factory = headerregistry.HeaderRegistry() + + def make_header(self, name, value): + return self.factory(name, value) + + +class TestBaseHeaderFeatures(TestHeaderBase): + + def test_str(self): + h = self.make_header('subject', 'this is a test') + self.assertIsInstance(h, str) + self.assertEqual(h, 'this is a test') + self.assertEqual(str(h), 'this is a test') + + def test_substr(self): + h = self.make_header('subject', 'this is a test') + self.assertEqual(h[5:7], 'is') + + def test_has_name(self): + h = self.make_header('subject', 'this is a test') + self.assertEqual(h.name, 'subject') + + def _test_attr_ro(self, attr): + h = self.make_header('subject', 'this is a test') + with self.assertRaises(AttributeError): + setattr(h, attr, 'foo') + + def test_name_read_only(self): + self._test_attr_ro('name') + + def test_defects_read_only(self): + self._test_attr_ro('defects') + + def test_defects_is_tuple(self): + h = self.make_header('subject', 'this is a test') + self.assertEqual(len(h.defects), 0) + self.assertIsInstance(h.defects, tuple) + # Make sure it is still true when there are defects. + h = self.make_header('date', '') + self.assertEqual(len(h.defects), 1) + self.assertIsInstance(h.defects, tuple) + + # XXX: FIXME + #def test_CR_in_value(self): + # # XXX: this also re-raises the issue of embedded headers, + # # need test and solution for that. + # value = '\r'.join(['this is', ' a test']) + # h = self.make_header('subject', value) + # self.assertEqual(h, value) + # self.assertDefectsEqual(h.defects, [errors.ObsoleteHeaderDefect]) + + def test_RFC2047_value_decoded(self): + value = '=?utf-8?q?this_is_a_test?=' + h = self.make_header('subject', value) + self.assertEqual(h, 'this is a test') + + +class TestDateHeader(TestHeaderBase): + + datestring = 'Sun, 23 Sep 2001 20:10:55 -0700' + utcoffset = datetime.timedelta(hours=-7) + tz = datetime.timezone(utcoffset) + dt = datetime.datetime(2001, 9, 23, 20, 10, 55, tzinfo=tz) + + def test_parse_date(self): + h = self.make_header('date', self.datestring) + self.assertEqual(h, self.datestring) + self.assertEqual(h.datetime, self.dt) + self.assertEqual(h.datetime.utcoffset(), self.utcoffset) + self.assertEqual(h.defects, ()) + + def test_set_from_datetime(self): + h = self.make_header('date', self.dt) + self.assertEqual(h, self.datestring) + self.assertEqual(h.datetime, self.dt) + self.assertEqual(h.defects, ()) + + def test_date_header_properties(self): + h = self.make_header('date', self.datestring) + self.assertIsInstance(h, headerregistry.UniqueDateHeader) + self.assertEqual(h.max_count, 1) + self.assertEqual(h.defects, ()) + + def test_resent_date_header_properties(self): + h = self.make_header('resent-date', self.datestring) + self.assertIsInstance(h, headerregistry.DateHeader) + self.assertEqual(h.max_count, None) + self.assertEqual(h.defects, ()) + + def test_no_value_is_defect(self): + h = self.make_header('date', '') + self.assertEqual(len(h.defects), 1) + self.assertIsInstance(h.defects[0], errors.HeaderMissingRequiredValue) + + def test_datetime_read_only(self): + h = self.make_header('date', self.datestring) + with self.assertRaises(AttributeError): + h.datetime = 'foo' + + def test_set_date_header_from_datetime(self): + m = Message(policy=policy.default) + m['Date'] = self.dt + self.assertEqual(m['Date'], self.datestring) + self.assertEqual(m['Date'].datetime, self.dt) + + +class TestAddressHeader(TestHeaderBase): + + examples = { + + 'empty': + ('<>', + [errors.InvalidHeaderDefect], + '<>', + '', + '<>', + '', + '', + None), + + 'address_only': + ('zippy@pinhead.com', + [], + 'zippy@pinhead.com', + '', + 'zippy@pinhead.com', + 'zippy', + 'pinhead.com', + None), + + 'name_and_address': + ('Zaphrod Beblebrux ', + [], + 'Zaphrod Beblebrux ', + 'Zaphrod Beblebrux', + 'zippy@pinhead.com', + 'zippy', + 'pinhead.com', + None), + + 'quoted_local_part': + ('Zaphrod Beblebrux <"foo bar"@pinhead.com>', + [], + 'Zaphrod Beblebrux <"foo bar"@pinhead.com>', + 'Zaphrod Beblebrux', + '"foo bar"@pinhead.com', + 'foo bar', + 'pinhead.com', + None), + + 'quoted_parens_in_name': + (r'"A \(Special\) Person" ', + [], + '"A (Special) Person" ', + 'A (Special) Person', + 'person@dom.ain', + 'person', + 'dom.ain', + None), + + 'quoted_backslashes_in_name': + (r'"Arthur \\Backslash\\ Foobar" ', + [], + r'"Arthur \\Backslash\\ Foobar" ', + r'Arthur \Backslash\ Foobar', + 'person@dom.ain', + 'person', + 'dom.ain', + None), + + 'name_with_dot': + ('John X. Doe ', + [errors.ObsoleteHeaderDefect], + '"John X. Doe" ', + 'John X. Doe', + 'jxd@example.com', + 'jxd', + 'example.com', + None), + + 'quoted_strings_in_local_part': + ('""example" example"@example.com', + [errors.InvalidHeaderDefect]*3, + '"example example"@example.com', + '', + '"example example"@example.com', + 'example example', + 'example.com', + None), + + 'escaped_quoted_strings_in_local_part': + (r'"\"example\" example"@example.com', + [], + r'"\"example\" example"@example.com', + '', + r'"\"example\" example"@example.com', + r'"example" example', + 'example.com', + None), + + 'escaped_escapes_in_local_part': + (r'"\\"example\\" example"@example.com', + [errors.InvalidHeaderDefect]*5, + r'"\\example\\\\ example"@example.com', + '', + r'"\\example\\\\ example"@example.com', + r'\example\\ example', + 'example.com', + None), + + 'spaces_in_unquoted_local_part_collapsed': + ('merwok wok @example.com', + [errors.InvalidHeaderDefect]*2, + '"merwok wok"@example.com', + '', + '"merwok wok"@example.com', + 'merwok wok', + 'example.com', + None), + + 'spaces_around_dots_in_local_part_removed': + ('merwok. wok . wok@example.com', + [errors.ObsoleteHeaderDefect], + 'merwok.wok.wok@example.com', + '', + 'merwok.wok.wok@example.com', + 'merwok.wok.wok', + 'example.com', + None), + + } + + # XXX: Need many more examples, and in particular some with names in + # trailing comments, which aren't currently handled. comments in + # general are not handled yet. + + def _test_single_addr(self, source, defects, decoded, display_name, + addr_spec, username, domain, comment): + h = self.make_header('sender', source) + self.assertEqual(h, decoded) + self.assertDefectsEqual(h.defects, defects) + a = h.address + self.assertEqual(str(a), decoded) + self.assertEqual(len(h.groups), 1) + self.assertEqual([a], list(h.groups[0].addresses)) + self.assertEqual([a], list(h.addresses)) + self.assertEqual(a.display_name, display_name) + self.assertEqual(a.addr_spec, addr_spec) + self.assertEqual(a.username, username) + self.assertEqual(a.domain, domain) + # XXX: we have no comment support yet. + #self.assertEqual(a.comment, comment) + + for name in examples: + locals()['test_'+name] = ( + lambda self, name=name: + self._test_single_addr(*self.examples[name])) + + def _test_group_single_addr(self, source, defects, decoded, display_name, + addr_spec, username, domain, comment): + source = 'foo: {};'.format(source) + gdecoded = 'foo: {};'.format(decoded) if decoded else 'foo:;' + h = self.make_header('to', source) + self.assertEqual(h, gdecoded) + self.assertDefectsEqual(h.defects, defects) + self.assertEqual(h.groups[0].addresses, h.addresses) + self.assertEqual(len(h.groups), 1) + self.assertEqual(len(h.addresses), 1) + a = h.addresses[0] + self.assertEqual(str(a), decoded) + self.assertEqual(a.display_name, display_name) + self.assertEqual(a.addr_spec, addr_spec) + self.assertEqual(a.username, username) + self.assertEqual(a.domain, domain) + + for name in examples: + locals()['test_group_'+name] = ( + lambda self, name=name: + self._test_group_single_addr(*self.examples[name])) + + def test_simple_address_list(self): + value = ('Fred , foo@example.com, ' + '"Harry W. Hastings" ') + h = self.make_header('to', value) + self.assertEqual(h, value) + self.assertEqual(len(h.groups), 3) + self.assertEqual(len(h.addresses), 3) + for i in range(3): + self.assertEqual(h.groups[i].addresses[0], h.addresses[i]) + self.assertEqual(str(h.addresses[0]), 'Fred ') + self.assertEqual(str(h.addresses[1]), 'foo@example.com') + self.assertEqual(str(h.addresses[2]), + '"Harry W. Hastings" ') + self.assertEqual(h.addresses[2].display_name, + 'Harry W. Hastings') + + def test_complex_address_list(self): + examples = list(self.examples.values()) + source = ('dummy list:;, another: (empty);,' + + ', '.join([x[0] for x in examples[:4]]) + ', ' + + r'"A \"list\"": ' + + ', '.join([x[0] for x in examples[4:6]]) + ';,' + + ', '.join([x[0] for x in examples[6:]]) + ) + # XXX: the fact that (empty) disappears here is a potential API design + # bug. We don't currently have a way to preserve comments. + expected = ('dummy list:;, another:;, ' + + ', '.join([x[2] for x in examples[:4]]) + ', ' + + r'"A \"list\"": ' + + ', '.join([x[2] for x in examples[4:6]]) + ';, ' + + ', '.join([x[2] for x in examples[6:]]) + ) + + h = self.make_header('to', source) + self.assertEqual(h.split(','), expected.split(',')) + self.assertEqual(h, expected) + self.assertEqual(len(h.groups), 7 + len(examples) - 6) + self.assertEqual(h.groups[0].display_name, 'dummy list') + self.assertEqual(h.groups[1].display_name, 'another') + self.assertEqual(h.groups[6].display_name, 'A "list"') + self.assertEqual(len(h.addresses), len(examples)) + for i in range(4): + self.assertIsNone(h.groups[i+2].display_name) + self.assertEqual(str(h.groups[i+2].addresses[0]), examples[i][2]) + for i in range(7, 7 + len(examples) - 6): + self.assertIsNone(h.groups[i].display_name) + self.assertEqual(str(h.groups[i].addresses[0]), examples[i-1][2]) + for i in range(len(examples)): + self.assertEqual(str(h.addresses[i]), examples[i][2]) + self.assertEqual(h.addresses[i].addr_spec, examples[i][4]) + + def test_address_read_only(self): + h = self.make_header('sender', 'abc@xyz.com') + with self.assertRaises(AttributeError): + h.address = 'foo' + + def test_addresses_read_only(self): + h = self.make_header('sender', 'abc@xyz.com') + with self.assertRaises(AttributeError): + h.addresses = 'foo' + + def test_groups_read_only(self): + h = self.make_header('sender', 'abc@xyz.com') + with self.assertRaises(AttributeError): + h.groups = 'foo' + + def test_addresses_types(self): + source = 'me ' + h = self.make_header('to', source) + self.assertIsInstance(h.addresses, tuple) + self.assertIsInstance(h.addresses[0], Address) + + def test_groups_types(self): + source = 'me ' + h = self.make_header('to', source) + self.assertIsInstance(h.groups, tuple) + self.assertIsInstance(h.groups[0], Group) + + def test_set_from_Address(self): + h = self.make_header('to', Address('me', 'foo', 'example.com')) + self.assertEqual(h, 'me ') + + def test_set_from_Address_list(self): + h = self.make_header('to', [Address('me', 'foo', 'example.com'), + Address('you', 'bar', 'example.com')]) + self.assertEqual(h, 'me , you ') + + def test_set_from_Address_and_Group_list(self): + h = self.make_header('to', [Address('me', 'foo', 'example.com'), + Group('bing', [Address('fiz', 'z', 'b.com'), + Address('zif', 'f', 'c.com')]), + Address('you', 'bar', 'example.com')]) + self.assertEqual(h, 'me , bing: fiz , ' + 'zif ;, you ') + self.assertEqual(h.fold(policy=policy.default.clone(max_line_length=40)), + 'to: me ,\n' + ' bing: fiz , zif ;,\n' + ' you \n') + + def test_set_from_Group_list(self): + h = self.make_header('to', [Group('bing', [Address('fiz', 'z', 'b.com'), + Address('zif', 'f', 'c.com')])]) + self.assertEqual(h, 'bing: fiz , zif ;') + + +class TestAddressAndGroup(TestEmailBase): + + def _test_attr_ro(self, obj, attr): + with self.assertRaises(AttributeError): + setattr(obj, attr, 'foo') + + def test_address_display_name_ro(self): + self._test_attr_ro(Address('foo', 'bar', 'baz'), 'display_name') + + def test_address_username_ro(self): + self._test_attr_ro(Address('foo', 'bar', 'baz'), 'username') + + def test_address_domain_ro(self): + self._test_attr_ro(Address('foo', 'bar', 'baz'), 'domain') + + def test_group_display_name_ro(self): + self._test_attr_ro(Group('foo'), 'display_name') + + def test_group_addresses_ro(self): + self._test_attr_ro(Group('foo'), 'addresses') + + def test_address_from_username_domain(self): + a = Address('foo', 'bar', 'baz') + self.assertEqual(a.display_name, 'foo') + self.assertEqual(a.username, 'bar') + self.assertEqual(a.domain, 'baz') + self.assertEqual(a.addr_spec, 'bar@baz') + self.assertEqual(str(a), 'foo ') + + def test_address_from_addr_spec(self): + a = Address('foo', addr_spec='bar@baz') + self.assertEqual(a.display_name, 'foo') + self.assertEqual(a.username, 'bar') + self.assertEqual(a.domain, 'baz') + self.assertEqual(a.addr_spec, 'bar@baz') + self.assertEqual(str(a), 'foo ') + + def test_address_with_no_display_name(self): + a = Address(addr_spec='bar@baz') + self.assertEqual(a.display_name, '') + self.assertEqual(a.username, 'bar') + self.assertEqual(a.domain, 'baz') + self.assertEqual(a.addr_spec, 'bar@baz') + self.assertEqual(str(a), 'bar@baz') + + def test_null_address(self): + a = Address() + self.assertEqual(a.display_name, '') + self.assertEqual(a.username, '') + self.assertEqual(a.domain, '') + self.assertEqual(a.addr_spec, '<>') + self.assertEqual(str(a), '<>') + + def test_domain_only(self): + # This isn't really a valid address. + a = Address(domain='buzz') + self.assertEqual(a.display_name, '') + self.assertEqual(a.username, '') + self.assertEqual(a.domain, 'buzz') + self.assertEqual(a.addr_spec, '@buzz') + self.assertEqual(str(a), '@buzz') + + def test_username_only(self): + # This isn't really a valid address. + a = Address(username='buzz') + self.assertEqual(a.display_name, '') + self.assertEqual(a.username, 'buzz') + self.assertEqual(a.domain, '') + self.assertEqual(a.addr_spec, 'buzz') + self.assertEqual(str(a), 'buzz') + + def test_display_name_only(self): + a = Address('buzz') + self.assertEqual(a.display_name, 'buzz') + self.assertEqual(a.username, '') + self.assertEqual(a.domain, '') + self.assertEqual(a.addr_spec, '<>') + self.assertEqual(str(a), 'buzz <>') + + def test_quoting(self): + # Ideally we'd check every special individually, but I'm not up for + # writing that many tests. + a = Address('Sara J.', 'bad name', 'example.com') + self.assertEqual(a.display_name, 'Sara J.') + self.assertEqual(a.username, 'bad name') + self.assertEqual(a.domain, 'example.com') + self.assertEqual(a.addr_spec, '"bad name"@example.com') + self.assertEqual(str(a), '"Sara J." <"bad name"@example.com>') + + def test_il8n(self): + a = Address('Éric', 'wok', 'exàmple.com') + self.assertEqual(a.display_name, 'Éric') + self.assertEqual(a.username, 'wok') + self.assertEqual(a.domain, 'exàmple.com') + self.assertEqual(a.addr_spec, 'wok@exàmple.com') + self.assertEqual(str(a), 'Éric ') + + # XXX: there is an API design issue that needs to be solved here. + #def test_non_ascii_username_raises(self): + # with self.assertRaises(ValueError): + # Address('foo', 'wők', 'example.com') + + def test_non_ascii_username_in_addr_spec_raises(self): + with self.assertRaises(ValueError): + Address('foo', addr_spec='wők@example.com') + + def test_address_addr_spec_and_username_raises(self): + with self.assertRaises(TypeError): + Address('foo', username='bing', addr_spec='bar@baz') + + def test_address_addr_spec_and_domain_raises(self): + with self.assertRaises(TypeError): + Address('foo', domain='bing', addr_spec='bar@baz') + + def test_address_addr_spec_and_username_and_domain_raises(self): + with self.assertRaises(TypeError): + Address('foo', username='bong', domain='bing', addr_spec='bar@baz') + + def test_space_in_addr_spec_username_raises(self): + with self.assertRaises(ValueError): + Address('foo', addr_spec="bad name@example.com") + + def test_bad_addr_sepc_raises(self): + with self.assertRaises(ValueError): + Address('foo', addr_spec="name@ex[]ample.com") + + def test_empty_group(self): + g = Group('foo') + self.assertEqual(g.display_name, 'foo') + self.assertEqual(g.addresses, tuple()) + self.assertEqual(str(g), 'foo:;') + + def test_empty_group_list(self): + g = Group('foo', addresses=[]) + self.assertEqual(g.display_name, 'foo') + self.assertEqual(g.addresses, tuple()) + self.assertEqual(str(g), 'foo:;') + + def test_null_group(self): + g = Group() + self.assertIsNone(g.display_name) + self.assertEqual(g.addresses, tuple()) + self.assertEqual(str(g), 'None:;') + + def test_group_with_addresses(self): + addrs = [Address('b', 'b', 'c'), Address('a', 'b','c')] + g = Group('foo', addrs) + self.assertEqual(g.display_name, 'foo') + self.assertEqual(g.addresses, tuple(addrs)) + self.assertEqual(str(g), 'foo: b , a ;') + + def test_group_with_addresses_no_display_name(self): + addrs = [Address('b', 'b', 'c'), Address('a', 'b','c')] + g = Group(addresses=addrs) + self.assertIsNone(g.display_name) + self.assertEqual(g.addresses, tuple(addrs)) + self.assertEqual(str(g), 'None: b , a ;') + + def test_group_with_one_address_no_display_name(self): + addrs = [Address('b', 'b', 'c')] + g = Group(addresses=addrs) + self.assertIsNone(g.display_name) + self.assertEqual(g.addresses, tuple(addrs)) + self.assertEqual(str(g), 'b ') + + def test_display_name_quoting(self): + g = Group('foo.bar') + self.assertEqual(g.display_name, 'foo.bar') + self.assertEqual(g.addresses, tuple()) + self.assertEqual(str(g), '"foo.bar":;') + + def test_display_name_blanks_not_quoted(self): + g = Group('foo bar') + self.assertEqual(g.display_name, 'foo bar') + self.assertEqual(g.addresses, tuple()) + self.assertEqual(str(g), 'foo bar:;') + + def test_set_message_header_from_address(self): + a = Address('foo', 'bar', 'example.com') + m = Message(policy=policy.default) + m['To'] = a + self.assertEqual(m['to'], 'foo ') + self.assertEqual(m['to'].addresses, (a,)) + + def test_set_message_header_from_group(self): + g = Group('foo bar') + m = Message(policy=policy.default) + m['To'] = g + self.assertEqual(m['to'], 'foo bar:;') + self.assertEqual(m['to'].addresses, g.addresses) + + +class TestFolding(TestHeaderBase): + + def test_short_unstructured(self): + h = self.make_header('subject', 'this is a test') + self.assertEqual(h.fold(policy=self.policy), + 'subject: this is a test\n') + + def test_long_unstructured(self): + h = self.make_header('Subject', 'This is a long header ' + 'line that will need to be folded into two lines ' + 'and will demonstrate basic folding') + self.assertEqual(h.fold(policy=self.policy), + 'Subject: This is a long header line that will ' + 'need to be folded into two lines\n' + ' and will demonstrate basic folding\n') + + def test_unstructured_short_max_line_length(self): + h = self.make_header('Subject', 'this is a short header ' + 'that will be folded anyway') + self.assertEqual( + h.fold(policy=policy.default.clone(max_line_length=20)), + textwrap.dedent("""\ + Subject: this is a + short header that + will be folded + anyway + """)) + + def test_fold_unstructured_single_word(self): + h = self.make_header('Subject', 'test') + self.assertEqual(h.fold(policy=self.policy), 'Subject: test\n') + + def test_fold_unstructured_short(self): + h = self.make_header('Subject', 'test test test') + self.assertEqual(h.fold(policy=self.policy), + 'Subject: test test test\n') + + def test_fold_unstructured_with_overlong_word(self): + h = self.make_header('Subject', 'thisisaverylonglineconsistingofa' + 'singlewordthatwontfit') + self.assertEqual( + h.fold(policy=policy.default.clone(max_line_length=20)), + 'Subject: thisisaverylonglineconsistingofasinglewordthatwontfit\n') + + def test_fold_unstructured_with_two_overlong_words(self): + h = self.make_header('Subject', 'thisisaverylonglineconsistingofa' + 'singlewordthatwontfit plusanotherverylongwordthatwontfit') + self.assertEqual( + h.fold(policy=policy.default.clone(max_line_length=20)), + 'Subject: thisisaverylonglineconsistingofasinglewordthatwontfit\n' + ' plusanotherverylongwordthatwontfit\n') + + def test_fold_unstructured_with_slightly_long_word(self): + h = self.make_header('Subject', 'thislongwordislessthanmaxlinelen') + self.assertEqual( + h.fold(policy=policy.default.clone(max_line_length=35)), + 'Subject:\n thislongwordislessthanmaxlinelen\n') + + def test_fold_unstructured_with_commas(self): + # The old wrapper would fold this at the commas. + h = self.make_header('Subject', "This header is intended to " + "demonstrate, in a fairly susinct way, that we now do " + "not give a , special treatment in unstructured headers.") + self.assertEqual( + h.fold(policy=policy.default.clone(max_line_length=60)), + textwrap.dedent("""\ + Subject: This header is intended to demonstrate, in a fairly + susinct way, that we now do not give a , special treatment + in unstructured headers. + """)) + + def test_fold_address_list(self): + h = self.make_header('To', '"Theodore H. Perfect" , ' + '"My address is very long because my name is long" , ' + '"Only A. Friend" ') + self.assertEqual(h.fold(policy=self.policy), textwrap.dedent("""\ + To: "Theodore H. Perfect" , + "My address is very long because my name is long" , + "Only A. Friend" + """)) + + def test_fold_date_header(self): + h = self.make_header('Date', 'Sat, 2 Feb 2002 17:00:06 -0800') + self.assertEqual(h.fold(policy=self.policy), + 'Date: Sat, 02 Feb 2002 17:00:06 -0800\n') + + + +if __name__ == '__main__': + unittest.main() diff --git a/Lib/test/test_email/test_pickleable.py b/Lib/test/test_email/test_pickleable.py index e4c77ca..9ebf933 100644 --- a/Lib/test/test_email/test_pickleable.py +++ b/Lib/test/test_email/test_pickleable.py @@ -4,7 +4,7 @@ import copy import pickle from email import policy from email import message_from_string -from email._headerregistry import HeaderRegistry +from email.headerregistry import HeaderRegistry from test.test_email import TestEmailBase class TestPickleCopyHeader(TestEmailBase): diff --git a/Lib/test/test_email/test_policy.py b/Lib/test/test_email/test_policy.py index 45a7666..983bd49 100644 --- a/Lib/test/test_email/test_policy.py +++ b/Lib/test/test_email/test_policy.py @@ -5,7 +5,7 @@ import unittest import email.policy import email.parser import email.generator -from email import _headerregistry +from email import headerregistry def make_defaults(base_defaults, differences): defaults = base_defaults.copy() @@ -185,11 +185,11 @@ class PolicyAPITests(unittest.TestCase): def test_default_header_factory(self): h = email.policy.default.header_factory('Test', 'test') self.assertEqual(h.name, 'Test') - self.assertIsInstance(h, _headerregistry.UnstructuredHeader) - self.assertIsInstance(h, _headerregistry.BaseHeader) + self.assertIsInstance(h, headerregistry.UnstructuredHeader) + self.assertIsInstance(h, headerregistry.BaseHeader) class Foo: - parse = _headerregistry.UnstructuredHeader.parse + parse = headerregistry.UnstructuredHeader.parse def test_each_Policy_gets_unique_factory(self): policy1 = email.policy.EmailPolicy() @@ -197,10 +197,10 @@ class PolicyAPITests(unittest.TestCase): policy1.header_factory.map_to_type('foo', self.Foo) h = policy1.header_factory('foo', 'test') self.assertIsInstance(h, self.Foo) - self.assertNotIsInstance(h, _headerregistry.UnstructuredHeader) + self.assertNotIsInstance(h, headerregistry.UnstructuredHeader) h = policy2.header_factory('foo', 'test') self.assertNotIsInstance(h, self.Foo) - self.assertIsInstance(h, _headerregistry.UnstructuredHeader) + self.assertIsInstance(h, headerregistry.UnstructuredHeader) def test_clone_copies_factory(self): policy1 = email.policy.EmailPolicy() -- cgit v0.12