diff -r 2059910e7d76 -r ac776ef41428 .gitignore --- a/.gitignore Sat Jun 09 17:31:59 2012 +0100 +++ b/.gitignore Mon May 21 23:01:17 2012 -0400 @@ -5,18 +5,14 @@ *.pyd *.pyo *.rej -*.swp *~ -.gdb_history Doc/build/ Doc/tools/docutils/ -Doc/tools/jinja/ Doc/tools/jinja2/ Doc/tools/pygments/ Doc/tools/sphinx/ Lib/lib2to3/*.pickle Lib/_sysconfigdata.py -Lib/plat-mac/errors.rsrc.df.rsrc Makefile Makefile.pre Misc/python.pc @@ -35,34 +31,19 @@ PCbuild/*.o PCbuild/*.pdb PCbuild/Win32-temp-* -PCbuild/amd64/ -.purify Parser/pgen __pycache__ autom4te.cache build/ -buildno -config.cache -config.log -config.status -config.status.lineno -core -db_home config.log config.status libpython*.a libpython*.so* -platform pybuilddir.txt pyconfig.h python -python.exe python-gdb.py -python.exe-gdb.py -reflog.txt -.svn/ tags -TAGS .coverage coverage/ htmlcov/ diff -r 2059910e7d76 -r ac776ef41428 .hgeol --- a/.hgeol Sat Jun 09 17:31:59 2012 +0100 +++ b/.hgeol Mon May 21 23:01:17 2012 -0400 @@ -32,8 +32,6 @@ Lib/test/sndhdrdata/sndhdr.* = BIN Lib/test/test_email/data/msg_26.txt = BIN -Lib/venv/scripts/nt/* = BIN - # All other files (which presumably are human-editable) are "native". # This must be the last rule! diff -r 2059910e7d76 -r ac776ef41428 .hgtags --- a/.hgtags Sat Jun 09 17:31:59 2012 +0100 +++ b/.hgtags Mon May 21 23:01:17 2012 -0400 @@ -102,4 +102,3 @@ f1a9a6505731714f0e157453ff850e3b71615c45 v3.3.0a1 2f69db52d6de306cdaef0a0cc00cc823fb350b01 v3.3.0a2 0b53b70a40a00013505eb35e3660057b62be77be v3.3.0a3 -7c51388a3aa7ce76a8541bbbdfc05d2d259a162c v3.3.0a4 diff -r 2059910e7d76 -r ac776ef41428 Doc/c-api/code.rst --- a/Doc/c-api/code.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/c-api/code.rst Mon May 21 23:01:17 2012 -0400 @@ -31,11 +31,11 @@ Return true if *co* is a :class:`code` object -.. c:function:: int PyCode_GetNumFree(PyCodeObject *co) +.. c:function:: int PyCode_GetNumFree(PyObject *co) Return the number of free variables in *co*. -.. c:function:: PyCodeObject* PyCode_New(int argcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *lnotab) +.. c:function:: PyCodeObject *PyCode_New(int argcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *lnotab) Return a new code object. If you need a dummy code object to create a frame, use :c:func:`PyCode_NewEmpty` instead. Calling @@ -43,7 +43,7 @@ version since the definition of the bytecode changes often. -.. c:function:: PyCodeObject* PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) +.. c:function:: int PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) Return a new empty code object with the specified filename, function name, and first line number. It is illegal to diff -r 2059910e7d76 -r ac776ef41428 Doc/c-api/conversion.rst --- a/Doc/c-api/conversion.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/c-api/conversion.rst Mon May 21 23:01:17 2012 -0400 @@ -119,13 +119,13 @@ .. versionadded:: 3.1 -.. c:function:: int PyOS_stricmp(char *s1, char *s2) +.. c:function:: char* PyOS_stricmp(char *s1, char *s2) Case insensitive comparison of strings. The function works almost identically to :c:func:`strcmp` except that it ignores the case. -.. c:function:: int PyOS_strnicmp(char *s1, char *s2, Py_ssize_t size) +.. c:function:: char* PyOS_strnicmp(char *s1, char *s2, Py_ssize_t size) Case insensitive comparison of strings. The function works almost identically to :c:func:`strncmp` except that it ignores the case. diff -r 2059910e7d76 -r ac776ef41428 Doc/c-api/import.rst --- a/Doc/c-api/import.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/c-api/import.rst Mon May 21 23:01:17 2012 -0400 @@ -30,13 +30,13 @@ .. c:function:: PyObject* PyImport_ImportModuleNoBlock(const char *name) - This function is a deprecated alias of :c:func:`PyImport_ImportModule`. - - .. versionchanged:: 3.3 - This function used to fail immediately when the import lock was held - by another thread. In Python 3.3 though, the locking scheme switched - to per-module locks for most purposes, so this function's special - behaviour isn't needed anymore. + This version of :c:func:`PyImport_ImportModule` does not block. It's intended + to be used in C functions that import other modules to execute a function. + The import may block if another thread holds the import lock. The function + :c:func:`PyImport_ImportModuleNoBlock` never blocks. It first tries to fetch + the module from sys.modules and falls back to :c:func:`PyImport_ImportModule` + unless the lock is held, in which case the function will raise an + :exc:`ImportError`. .. c:function:: PyObject* PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist) diff -r 2059910e7d76 -r ac776ef41428 Doc/c-api/init.rst --- a/Doc/c-api/init.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/c-api/init.rst Mon May 21 23:01:17 2012 -0400 @@ -646,7 +646,7 @@ :c:func:`PyGILState_Release` on the same thread. -.. c:function:: PyThreadState* PyGILState_GetThisThreadState() +.. c:function:: PyThreadState PyGILState_GetThisThreadState() Get the current thread state for this thread. May return ``NULL`` if no GILState API has been used on the current thread. Note that the main thread diff -r 2059910e7d76 -r ac776ef41428 Doc/c-api/type.rst --- a/Doc/c-api/type.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/c-api/type.rst Mon May 21 23:01:17 2012 -0400 @@ -51,13 +51,13 @@ modification of the attributes or base classes of the type. -.. c:function:: int PyType_HasFeature(PyTypeObject *o, int feature) +.. c:function:: int PyType_HasFeature(PyObject *o, int feature) Return true if the type object *o* sets the feature *feature*. Type features are denoted by single bit flags. -.. c:function:: int PyType_IS_GC(PyTypeObject *o) +.. c:function:: int PyType_IS_GC(PyObject *o) Return true if the type object includes support for the cycle detector; this tests the type flag :const:`Py_TPFLAGS_HAVE_GC`. @@ -70,14 +70,13 @@ .. c:function:: PyObject* PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) - Generic handler for the :attr:`tp_alloc` slot of a type object. Use - Python's default memory allocation mechanism to allocate a new instance and - initialize all its contents to *NULL*. + XXX: Document. + .. c:function:: PyObject* PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) - Generic handler for the :attr:`tp_new` slot of a type object. Create a - new instance using the type's :attr:`tp_alloc` slot. + Generic handler for the :attr:`tp_new` slot of a type object. Initialize + all instance variables to *NULL*. .. c:function:: int PyType_Ready(PyTypeObject *type) diff -r 2059910e7d76 -r ac776ef41428 Doc/c-api/unicode.rst --- a/Doc/c-api/unicode.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/c-api/unicode.rst Mon May 21 23:01:17 2012 -0400 @@ -1615,7 +1615,7 @@ ISO-8859-1 if it contains non-ASCII characters". -.. c:function:: PyObject* PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) +.. c:function:: int PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) Rich compare two unicode strings and return one of the following: diff -r 2059910e7d76 -r ac776ef41428 Doc/c-api/veryhigh.rst --- a/Doc/c-api/veryhigh.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/c-api/veryhigh.rst Mon May 21 23:01:17 2012 -0400 @@ -95,6 +95,12 @@ leaving *closeit* set to ``0`` and *flags* set to *NULL*. +.. c:function:: int PyRun_SimpleFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags) + + This is a simplified interface to :c:func:`PyRun_SimpleFileExFlags` below, + leaving *closeit* set to ``0``. + + .. c:function:: int PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit) This is a simplified interface to :c:func:`PyRun_SimpleFileExFlags` below, diff -r 2059910e7d76 -r ac776ef41428 Doc/faq/library.rst --- a/Doc/faq/library.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/faq/library.rst Mon May 21 23:01:17 2012 -0400 @@ -351,7 +351,7 @@ Worker running with argument 5 ... -Consult the module's documentation for more details; the :class:`~queue.Queue` +Consult the module's documentation for more details; the :class:`~queue.Queue`` class provides a featureful interface. diff -r 2059910e7d76 -r ac776ef41428 Doc/howto/index.rst --- a/Doc/howto/index.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/howto/index.rst Mon May 21 23:01:17 2012 -0400 @@ -28,5 +28,4 @@ urllib2.rst webservers.rst argparse.rst - ipaddress.rst diff -r 2059910e7d76 -r ac776ef41428 Doc/howto/ipaddress.rst --- a/Doc/howto/ipaddress.rst Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,291 +0,0 @@ -.. _ipaddress-howto: - -*************** -Ipaddress Howto -*************** - -:author: Peter Moody - -.. topic:: Abstract - - This document is a gentle introduction to :mod:`ipaddress` module. - - -Creating Address/Network/Interface objects -========================================== - -Since :mod:`ipaddress` is a module for inspecting and manipulating IP address, -the first thing you'll want to do is create some objects. You can use -:mod:`ipaddress` to create objects from strings and integers. - - -A Note on IP Versions ---------------------- - -For readers that aren't particularly familiar with IP addressing, it's -important to know that the Internet Protocol is currently in the process -of moving from version 4 of the protocol to version 6. This transition is -occurring largely because version 4 of the protocol doesn't provide enough -addresses to handle the needs of the whole world, especially given the -increasing number of devices with direct connections to the internet. - -Explaining the details of the differences between the two versions of the -protocol is beyond the scope of this introduction, but readers need to at -least be aware that these two versions exist, and it will sometimes be -necessary to force the use of one version or the other. - - -IP Host Addresses ------------------ - -Addresses, often referred to as "host addresses" are the most basic unit -when working with IP addressing. The simplest way to create addresses is -to use the ``ip_address`` factory function, which automatically determines -whether to create an IPv4 or IPv6 address based on the passed in value:: - - >>> ipaddress.ip_address('192.0.2.1') - IPv4Address('192.0.2.1') - >>> ipaddress.ip_address('2001:DB8::1') - IPv6Address('2001:db8::1') - -Addresses can also be created directly from integers. Values that will -fit within 32 bits are assumed to be IPv4 addresses:: - - >>> ipaddress.ip_address(3221225985) - IPv4Address('192.0.2.1') - >>> ipaddress.ip_address(42540766411282592856903984951653826561) - IPv6Address('2001:db8::1') - -To force the use of IPv4 or IPv6 addresses, the relevant classes can be -invoked directly. This is particularly useful to force creation of IPv6 -addresses for small integers:: - - >>> ipaddress.ip_address(1) - IPv4Address('0.0.0.1') - >>> ipaddress.IPv4Address(1) - IPv4Address('0.0.0.1') - >>> ipaddress.IPv6Address(1) - IPv6Address('::1') - - -Defining Networks ------------------ - -Host addresses are usually grouped together into IP networks, so -:mod:`ipaddress` provides a way to create, inspect and manipulate network -definitions. IP network objects are constructed from strings that define the -range of host addresses that are part of that network. The simplest form -for that information is a "network address/network prefix" pair, where the -prefix defines the number of leading bits that are compared to determine -whether or not an address is part of the network and the network address -defines the expected value of those bits. - -As for addresses, a factory function is provided that determines the correct -IP version automatically:: - - >>> ipaddress.ip_network('192.0.2.0/24') - IPv4Network('192.0.2.0/24') - >>> ipaddress.ip_network('2001:db8::0/96') - IPv6Network('2001:db8::/96') - -Network objects cannot have any host bits set. The practical effect of this -is that ``192.0.2.1/24`` does not describe a network. Such definitions are -referred to as interface objects since the ip-on-a-network notation is -commonly used to describe network interfaces of a computer on a given network -and are described further in the next section. - -By default, attempting to create a network object with host bits set will -result in :exc:`ValueError` being raised. To request that the -additional bits instead be coerced to zero, the flag ``strict=False`` can -be passed to the constructor:: - - >>> ipaddress.ip_network('192.0.2.1/24') - Traceback (most recent call last): - ... - ValueError: 192.0.2.1/24 has host bits set - >>> ipaddress.ip_network('192.0.2.1/24', strict=False) - IPv4Network('192.0.2.0/24') - -While the string form offers significantly more flexibility, networks can -also be defined with integers, just like host addresses. In this case, the -network is considered to contain only the single address identified by the -integer, so the network prefix includes the entire network address:: - - >>> ipaddress.ip_network(3221225984) - IPv4Network('192.0.2.0/32') - >>> ipaddress.ip_network(42540766411282592856903984951653826560L) - IPv6Network('2001:db8::/128') - -Creation of a particular kind of network can be forced by calling the -class constructor directly instead of using the factory function. - - -Host Interfaces ---------------- - -As mentioned just above, if you need to describe an address on a particular -network, neither the address nor the network classes are sufficient. -Notation like ``192.0.2.1/24`` is commonly used network engineers and the -people who write tools for firewalls and routers as shorthand for "the host -``192.0.2.1`` on the network ``192.0.2.0/24``", Accordingly, :mod:`ipaddress` -provides a set of hybrid classes that associate an address with a particular -network. The interface for creation is identical to that for defining network -objects, except that the address portion isn't constrained to being a network -address. - - >>> ipaddress.ip_interface('192.0.2.1/24') - IPv4Interface('192.0.2.1/24') - >>> ipaddress.ip_network('2001:db8::1/96') - IPv6Interface('2001:db8::1/96') - -Integer inputs are accepted (as with networks), and use of a particular IP -version can be forced by calling the relevant constructor directly. - - -Inspecting Address/Network/Interface Objects -============================================ - -You've gone to the trouble of creating an IPv(4|6)(Address|Network|Interface) -object, so you probably want to get information about it. :mod:`ipaddress` -tries to make doing this easy and intuitive. - -Extracting the IP version:: - - >>> addr4 = ipaddress.ip_address('192.0.2.1') - >>> addr6 = ipaddress.ip_address('2001:db8::1') - >>> addr6.version - 6 - >>> addr4.version - 4 - -Obtaining the network from an interface:: - - >>> host4 = ipaddress.ip_interface('192.0.2.1/24') - >>> host4.network - IPv4Network('192.0.2.0/24') - >>> host6 = ipaddress.ip_interface('2001:db8::1/96') - >>> host6.network - IPv6Network('2001:db8::/96') - -Finding out how many individual addresses are in a network:: - - >>> net4 = ipaddress.ip_network('192.0.2.0/24') - >>> net4.numhosts - 256 - >>> net6 = ipaddress.ip_network('2001:db8::0/96') - >>> net6.numhosts - 4294967296 - -Iterating through the 'usable' addresses on a network:: - - >>> net4 = ipaddress.ip_network('192.0.2.0/24') - >>> for x in net4.iterhosts(): - print(x) - 192.0.2.1 - 192.0.2.2 - 192.0.2.3 - 192.0.2.4 - - 192.0.2.252 - 192.0.2.253 - 192.0.2.254 - - -Obtaining the netmask (i.e. set bits corresponding to the network prefix) or -the hostmask (any bits that are not part of the netmask): - - >>> net4 = ipaddress.ip_network('192.0.2.0/24') - >>> net4.netmask - IPv4Address('255.255.255.0') - >>> net4.hostmask - IPv4Address('0.0.0.255') - >>> net6 = ipaddress.ip_network('2001:db8::0/96') - >>> net6.netmask - IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff::') - >>> net6.hostmask - IPv6Address('::ffff:ffff') - - -Exploding or compressing the address:: - - >>> net6.exploded - '2001:0000:0000:0000:0000:0000:0000:0000/96' - >>> addr6.exploded - '2001:0000:0000:0000:0000:0000:0000:0001' - - -Networks as lists of Addresses -============================== - -It's sometimes useful to treat networks as lists. This means it is possible -to index them like this:: - - >>> net4[1] - IPv4Address('192.0.2.1') - >>> net4[-1] - IPv4Address('192.0.2.255') - >>> net6[1] - IPv6Address('2001::1') - >>> net6[-1] - IPv6Address('2001::ffff:ffff') - - -It also means that network objects lend themselves to using the list -membership test syntax like this:: - - if address in network: - # do something - -Containment testing is done efficiently based on the network prefix:: - - >>> addr4 = ipaddress.ip_address('192.0.2.1') - >>> addr4 in ipaddress.ip_network('192.0.2.0/24') - True - >>> addr4 in ipaddress.ip_network('192.0.3.0/24') - False - - -Comparisons -=========== - -:mod:`ipaddress` provides some simple, hopefully intuitive ways to compare -objects, where it makes sense:: - - >>> ipaddress.ip_address('192.0.2.1') < ipaddress.ip_address('192.0.2.2') - True - -A :exc:`TypeError` exception is raised if you try to compare objects of -different versions or different types. - - -Using IP Addresses with other modules -===================================== - -Other modules that use IP addresses (such as :mod:`socket`) usually won't -accept objects from this module directly. Instead, they must be coerced to -an integer or string that the other module will accept:: - - >>> addr4 = ipaddress.ip_address('192.0.2.1') - >>> str(addr4) - '192.0.2.1' - >>> int(addr4) - 3221225985 - - -Exceptions raised by :mod:`ipaddress` -===================================== - -If you try to create an address/network/interface object with an invalid value -for either the address or netmask, :mod:`ipaddress` will raise an -:exc:`AddressValueError` or :exc:`NetmaskValueError` respectively. However, -this applies only when calling the class constructors directly. The factory -functions and other module level functions will just raise :exc:`ValueError`. - -Both of the module specific exceptions have :exc:`ValueError` as their -parent class, so if you're not concerned with the particular type of error, -you can still do the following:: - - try: - ipaddress.IPv4Address(address) - except ValueError: - print('address/netmask is invalid:', address) diff -r 2059910e7d76 -r ac776ef41428 Doc/library/__future__.rst --- a/Doc/library/__future__.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/__future__.rst Mon May 21 23:01:17 2012 -0400 @@ -75,7 +75,7 @@ | division | 2.2.0a2 | 3.0 | :pep:`238`: | | | | | *Changing the Division Operator* | +------------------+-------------+--------------+---------------------------------------------+ -| absolute_import | 2.5.0a1 | 3.0 | :pep:`328`: | +| absolute_import | 2.5.0a1 | 2.7 | :pep:`328`: | | | | | *Imports: Multi-Line and Absolute/Relative* | +------------------+-------------+--------------+---------------------------------------------+ | with_statement | 2.5.0a1 | 2.6 | :pep:`343`: | diff -r 2059910e7d76 -r ac776ef41428 Doc/library/bz2.rst --- a/Doc/library/bz2.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/bz2.rst Mon May 21 23:01:17 2012 -0400 @@ -14,8 +14,7 @@ The :mod:`bz2` module contains: -* The :func:`.open` function and :class:`BZ2File` class for reading and - writing compressed files. +* The :class:`BZ2File` class for reading and writing compressed files. * The :class:`BZ2Compressor` and :class:`BZ2Decompressor` classes for incremental (de)compression. * The :func:`compress` and :func:`decompress` functions for one-shot @@ -27,47 +26,16 @@ (De)compression of files ------------------------ -.. function:: open(filename, mode='r', compresslevel=9, encoding=None, errors=None, newline=None) +.. class:: BZ2File(filename=None, mode='r', buffering=None, compresslevel=9, \*, fileobj=None) - Open a bzip2-compressed file in binary or text mode, returning a :term:`file - object`. + Open a bzip2-compressed file. - As with the constructor for :class:`BZ2File`, the *filename* argument can be - an actual filename (a :class:`str` or :class:`bytes` object), or an existing - file object to read from or write to. - - The *mode* argument can be any of ``'r'``, ``'rb'``, ``'w'``, ``'wb'``, - ``'a'``, or ``'ab'`` for binary mode, or ``'rt'``, ``'wt'``, or ``'at'`` for - text mode. The default is ``'rb'``. - - The *compresslevel* argument is an integer from 1 to 9, as for the - :class:`BZ2File` constructor. - - For binary mode, this function is equivalent to the :class:`BZ2File` - constructor: ``BZ2File(filename, mode, compresslevel=compresslevel)``. In - this case, the *encoding*, *errors* and *newline* arguments must not be - provided. - - For text mode, a :class:`BZ2File` object is created, and wrapped in an - :class:`io.TextIOWrapper` instance with the specified encoding, error - handling behavior, and line ending(s). - - .. versionadded:: 3.3 - - -.. class:: BZ2File(filename, mode='r', buffering=None, compresslevel=9) - - Open a bzip2-compressed file in binary mode. - - If *filename* is a :class:`str` or :class:`bytes` object, open the named file - directly. Otherwise, *filename* should be a :term:`file object`, which will - be used to read or write the compressed data. + The :class:`BZ2File` can wrap an existing :term:`file object` (given by + *fileobj*), or operate directly on a named file (named by *filename*). + Exactly one of these two parameters should be provided. The *mode* argument can be either ``'r'`` for reading (default), ``'w'`` for - overwriting, or ``'a'`` for appending. These can equivalently be given as - ``'rb'``, ``'wb'``, and ``'ab'`` respectively. - - If *filename* is a file object (rather than an actual file name), a mode of + overwriting, or ``'a'`` for appending. If *fileobj* is provided, a mode of ``'w'`` does not truncate the file, and is instead equivalent to ``'a'``. The *buffering* argument is ignored. Its use is deprecated. @@ -101,8 +69,7 @@ :meth:`read1` and :meth:`readinto` methods were added. .. versionchanged:: 3.3 - Support was added for *filename* being a :term:`file object` instead of an - actual filename. + The *fileobj* argument to the constructor was added. .. versionchanged:: 3.3 The ``'a'`` (append) mode was added, along with support for reading diff -r 2059910e7d76 -r ac776ef41428 Doc/library/contextlib.rst --- a/Doc/library/contextlib.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/contextlib.rst Mon May 21 23:01:17 2012 -0400 @@ -12,11 +12,8 @@ statement. For more information see also :ref:`typecontextmanager` and :ref:`context-managers`. +Functions provided: -Utilities ---------- - -Functions and classes provided: .. decorator:: contextmanager @@ -171,348 +168,6 @@ .. versionadded:: 3.2 -.. class:: ExitStack() - - A context manager that is designed to make it easy to programmatically - combine other context managers and cleanup functions, especially those - that are optional or otherwise driven by input data. - - For example, a set of files may easily be handled in a single with - statement as follows:: - - with ExitStack() as stack: - files = [stack.enter_context(open(fname)) for fname in filenames] - # All opened files will automatically be closed at the end of - # the with statement, even if attempts to open files later - # in the list throw an exception - - Each instance maintains a stack of registered callbacks that are called in - reverse order when the instance is closed (either explicitly or implicitly - at the end of a :keyword:`with` statement). Note that callbacks are *not* - invoked implicitly when the context stack instance is garbage collected. - - This stack model is used so that context managers that acquire their - resources in their ``__init__`` method (such as file objects) can be - handled correctly. - - Since registered callbacks are invoked in the reverse order of - registration, this ends up behaving as if multiple nested :keyword:`with` - statements had been used with the registered set of callbacks. This even - extends to exception handling - if an inner callback suppresses or replaces - an exception, then outer callbacks will be passed arguments based on that - updated state. - - This is a relatively low level API that takes care of the details of - correctly unwinding the stack of exit callbacks. It provides a suitable - foundation for higher level context managers that manipulate the exit - stack in application specific ways. - - .. versionadded:: 3.3 - - .. method:: enter_context(cm) - - Enters a new context manager and adds its :meth:`__exit__` method to - the callback stack. The return value is the result of the context - manager's own :meth:`__enter__` method. - - These context managers may suppress exceptions just as they normally - would if used directly as part of a :keyword:`with` statement. - - .. method:: push(exit) - - Adds a context manager's :meth:`__exit__` method to the callback stack. - - As ``__enter__`` is *not* invoked, this method can be used to cover - part of an :meth:`__enter__` implementation with a context manager's own - :meth:`__exit__` method. - - If passed an object that is not a context manager, this method assumes - it is a callback with the same signature as a context manager's - :meth:`__exit__` method and adds it directly to the callback stack. - - By returning true values, these callbacks can suppress exceptions the - same way context manager :meth:`__exit__` methods can. - - The passed in object is returned from the function, allowing this - method to be used as a function decorator. - - .. method:: callback(callback, *args, **kwds) - - Accepts an arbitrary callback function and arguments and adds it to - the callback stack. - - Unlike the other methods, callbacks added this way cannot suppress - exceptions (as they are never passed the exception details). - - The passed in callback is returned from the function, allowing this - method to be used as a function decorator. - - .. method:: pop_all() - - Transfers the callback stack to a fresh :class:`ExitStack` instance - and returns it. No callbacks are invoked by this operation - instead, - they will now be invoked when the new stack is closed (either - explicitly or implicitly at the end of a :keyword:`with` statement). - - For example, a group of files can be opened as an "all or nothing" - operation as follows:: - - with ExitStack() as stack: - files = [stack.enter_context(open(fname)) for fname in filenames] - close_files = stack.pop_all().close - # If opening any file fails, all previously opened files will be - # closed automatically. If all files are opened successfully, - # they will remain open even after the with statement ends. - # close_files() can then be invoked explicitly to close them all - - .. method:: close() - - Immediately unwinds the callback stack, invoking callbacks in the - reverse order of registration. For any context managers and exit - callbacks registered, the arguments passed in will indicate that no - exception occurred. - - -Examples and Recipes --------------------- - -This section describes some examples and recipes for making effective use of -the tools provided by :mod:`contextlib`. - - -Supporting a variable number of context managers -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -The primary use case for :class:`ExitStack` is the one given in the class -documentation: supporting a variable number of context managers and other -cleanup operations in a single :keyword:`with` statement. The variability -may come from the number of context managers needed being driven by user -input (such as opening a user specified collection of files), or from -some of the context managers being optional:: - - with ExitStack() as stack: - for resource in resources: - stack.enter_context(resource) - if need_special resource: - special = acquire_special_resource() - stack.callback(release_special_resource, special) - # Perform operations that use the acquired resources - -As shown, :class:`ExitStack` also makes it quite easy to use :keyword:`with` -statements to manage arbitrary resources that don't natively support the -context management protocol. - - -Simplifying support for single optional context managers -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -In the specific case of a single optional context manager, :class:`ExitStack` -instances can be used as a "do nothing" context manager, allowing a context -manager to easily be omitted without affecting the overall structure of -the source code:: - - def debug_trace(details): - if __debug__: - return TraceContext(details) - # Don't do anything special with the context in release mode - return ExitStack() - - with debug_trace(): - # Suite is traced in debug mode, but runs normally otherwise - - -Catching exceptions from ``__enter__`` methods -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -It is occasionally desirable to catch exceptions from an ``__enter__`` -method implementation, *without* inadvertently catching exceptions from -the :keyword:`with` statement body or the context manager's ``__exit__`` -method. By using :class:`ExitStack` the steps in the context management -protocol can be separated slightly in order to allow this:: - - stack = ExitStack() - try: - x = stack.enter_context(cm) - except Exception: - # handle __enter__ exception - else: - with stack: - # Handle normal case - -Actually needing to do this is likely to indicate that the underlying API -should be providing a direct resource management interface for use with -:keyword:`try`/:keyword:`except`/:keyword:`finally` statements, but not -all APIs are well designed in that regard. When a context manager is the -only resource management API provided, then :class:`ExitStack` can make it -easier to handle various situations that can't be handled directly in a -:keyword:`with` statement. - - -Cleaning up in an ``__enter__`` implementation -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -As noted in the documentation of :meth:`ExitStack.push`, this -method can be useful in cleaning up an already allocated resource if later -steps in the :meth:`__enter__` implementation fail. - -Here's an example of doing this for a context manager that accepts resource -acquisition and release functions, along with an optional validation function, -and maps them to the context management protocol:: - - from contextlib import contextmanager, ExitStack - - class ResourceManager(object): - - def __init__(self, acquire_resource, release_resource, check_resource_ok=None): - self.acquire_resource = acquire_resource - self.release_resource = release_resource - if check_resource_ok is None: - def check_resource_ok(resource): - return True - self.check_resource_ok = check_resource_ok - - @contextmanager - def _cleanup_on_error(self): - with ExitStack() as stack: - stack.push(self) - yield - # The validation check passed and didn't raise an exception - # Accordingly, we want to keep the resource, and pass it - # back to our caller - stack.pop_all() - - def __enter__(self): - resource = self.acquire_resource() - with self._cleanup_on_error(): - if not self.check_resource_ok(resource): - msg = "Failed validation for {!r}" - raise RuntimeError(msg.format(resource)) - return resource - - def __exit__(self, *exc_details): - # We don't need to duplicate any of our resource release logic - self.release_resource() - - -Replacing any use of ``try-finally`` and flag variables -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -A pattern you will sometimes see is a ``try-finally`` statement with a flag -variable to indicate whether or not the body of the ``finally`` clause should -be executed. In its simplest form (that can't already be handled just by -using an ``except`` clause instead), it looks something like this:: - - cleanup_needed = True - try: - result = perform_operation() - if result: - cleanup_needed = False - finally: - if cleanup_needed: - cleanup_resources() - -As with any ``try`` statement based code, this can cause problems for -development and review, because the setup code and the cleanup code can end -up being separated by arbitrarily long sections of code. - -:class:`ExitStack` makes it possible to instead register a callback for -execution at the end of a ``with`` statement, and then later decide to skip -executing that callback:: - - from contextlib import ExitStack - - with ExitStack() as stack: - stack.callback(cleanup_resources) - result = perform_operation() - if result: - stack.pop_all() - -This allows the intended cleanup up behaviour to be made explicit up front, -rather than requiring a separate flag variable. - -If a particular application uses this pattern a lot, it can be simplified -even further by means of a small helper class:: - - from contextlib import ExitStack - - class Callback(ExitStack): - def __init__(self, callback, *args, **kwds): - super(Callback, self).__init__() - self.callback(callback, *args, **kwds) - - def cancel(self): - self.pop_all() - - with Callback(cleanup_resources) as cb: - result = perform_operation() - if result: - cb.cancel() - -If the resource cleanup isn't already neatly bundled into a standalone -function, then it is still possible to use the decorator form of -:meth:`ExitStack.callback` to declare the resource cleanup in -advance:: - - from contextlib import ExitStack - - with ExitStack() as stack: - @stack.callback - def cleanup_resources(): - ... - result = perform_operation() - if result: - stack.pop_all() - -Due to the way the decorator protocol works, a callback function -declared this way cannot take any parameters. Instead, any resources to -be released must be accessed as closure variables - - -Using a context manager as a function decorator -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -:class:`ContextDecorator` makes it possible to use a context manager in -both an ordinary ``with`` statement and also as a function decorator. - -For example, it is sometimes useful to wrap functions or groups of statements -with a logger that can track the time of entry and time of exit. Rather than -writing both a function decorator and a context manager for the task, -inheriting from :class:`ContextDecorator` provides both capabilities in a -single definition:: - - from contextlib import ContextDecorator - import logging - - logging.basicConfig(level=logging.INFO) - - class track_entry_and_exit(ContextDecorator): - def __init__(self, name): - self.name = name - - def __enter__(self): - logging.info('Entering: {}'.format(name)) - - def __exit__(self, exc_type, exc, exc_tb): - logging.info('Exiting: {}'.format(name)) - -Instances of this class can be used as both a context manager:: - - with track_entry_and_exit('widget loader'): - print('Some time consuming activity goes here') - load_widget() - -And also as a function decorator:: - - @track_entry_and_exit('widget loader') - def activity(): - print('Some time consuming activity goes here') - load_widget() - -Note that there is one additional limitation when using context managers -as function decorators: there's no way to access the return value of -:meth:`__enter__`. If that value is needed, then it is still necessary to use -an explicit ``with`` statement. - .. seealso:: :pep:`0343` - The "with" statement diff -r 2059910e7d76 -r ac776ef41428 Doc/library/datetime.rst --- a/Doc/library/datetime.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/datetime.rst Mon May 21 23:01:17 2012 -0400 @@ -752,6 +752,17 @@ datetime(1970, 1, 1) + timedelta(seconds=timestamp) + There is no method to obtain the timestamp from a :class:`datetime` + instance, but POSIX timestamp corresponding to a :class:`datetime` + instance ``dt`` can be easily calculated as follows. For a naive + ``dt``:: + + timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1) + + And for an aware ``dt``:: + + timestamp = (dt - datetime(1970, 1, 1, tzinfo=timezone.utc)) / timedelta(seconds=1) + .. versionchanged:: 3.3 Raise :exc:`OverflowError` instead of :exc:`ValueError` if the timestamp is out of the range of values supported by the platform C @@ -1043,39 +1054,6 @@ Return the proleptic Gregorian ordinal of the date. The same as ``self.date().toordinal()``. -.. method:: datetime.timestamp() - - Return POSIX timestamp corresponding to the :class:`datetime` - instance. The return value is a :class:`float` similar to that - returned by :func:`time.time`. - - Naive :class:`datetime` instances are assumed to represent local - time and this method relies on the platform C :c:func:`mktime` - function to perform the conversion. Since :class:`datetime` - supports wider range of values than :c:func:`mktime` on many - platforms, this method may raise :exc:`OverflowError` for times far - in the past or far in the future. - - For aware :class:`datetime` instances, the return value is computed - as:: - - (dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds() - - .. versionadded:: 3.3 - - .. note:: - - There is no method to obtain the POSIX timestamp directly from a - naive :class:`datetime` instance representing UTC time. If your - application uses this convention and your system timezone is not - set to UTC, you can obtain the POSIX timestamp by supplying - ``tzinfo=timezone.utc``:: - - timestamp = dt.replace(tzinfo=timezone.utc).timestamp() - - or by calculating the timestamp directly:: - - timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1) .. method:: datetime.weekday() diff -r 2059910e7d76 -r ac776ef41428 Doc/library/development.rst --- a/Doc/library/development.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/development.rst Mon May 21 23:01:17 2012 -0400 @@ -23,4 +23,3 @@ unittest.mock-examples.rst 2to3.rst test.rst - venv.rst diff -r 2059910e7d76 -r ac776ef41428 Doc/library/email.charset.rst --- a/Doc/library/email.charset.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/email.charset.rst Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ -:mod:`email.charset`: Representing character sets -------------------------------------------------- +:mod:`email`: Representing character sets +----------------------------------------- .. module:: email.charset :synopsis: Character Sets diff -r 2059910e7d76 -r ac776ef41428 Doc/library/email.encoders.rst --- a/Doc/library/email.encoders.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/email.encoders.rst Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ -:mod:`email.encoders`: Encoders -------------------------------- +:mod:`email`: Encoders +---------------------- .. module:: email.encoders :synopsis: Encoders for email message payloads. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/email.errors.rst --- a/Doc/library/email.errors.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/email.errors.rst Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ -:mod:`email.errors`: Exception and Defect classes -------------------------------------------------- +:mod:`email`: Exception and Defect classes +------------------------------------------ .. module:: email.errors :synopsis: The exception classes used by the email package. @@ -73,38 +73,17 @@ * :class:`StartBoundaryNotFoundDefect` -- The start boundary claimed in the :mailheader:`Content-Type` header was never found. -* :class:`CloseBoundaryNotFoundDefect` -- A start boundary was found, but - no corresponding close boundary was ever found. - - .. versionadded: 3.3 - * :class:`FirstHeaderLineIsContinuationDefect` -- The message had a continuation line as its first header line. * :class:`MisplacedEnvelopeHeaderDefect` - A "Unix From" header was found in the middle of a header block. -* :class:`MissingHeaderBodySeparatorDefect` - A line was found while parsing - headers that had no leading white space but contained no ':'. Parsing - continues assuming that the line represents the first line of the body. - - .. versionadded: 3.3 - * :class:`MalformedHeaderDefect` -- A header was found that was missing a colon, or was otherwise malformed. - .. deprecated:: 3.3 - This defect has not been used for several Python versions. - * :class:`MultipartInvariantViolationDefect` -- A message claimed to be a :mimetype:`multipart`, but no subparts were found. Note that when a message has this defect, its :meth:`is_multipart` method may return false even though its content type claims to be :mimetype:`multipart`. -* :class:`InvalidBase64PaddingDefect` -- When decoding a block of base64 - enocded bytes, the padding was not correct. Enough padding is added to - perform the decode, but the resulting decoded bytes may be invalid. - -* :class:`InvalidBase64CharactersDefect` -- When decoding a block of base64 - enocded bytes, characters outside the base64 alphebet were encountered. - The characters are ignored, but the resulting decoded bytes may be invalid. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/email.generator.rst --- a/Doc/library/email.generator.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/email.generator.rst Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ -:mod:`email.generator`: Generating MIME documents -------------------------------------------------- +:mod:`email`: Generating MIME documents +--------------------------------------- .. module:: email.generator :synopsis: Generate flat text email messages from a message structure. @@ -32,7 +32,8 @@ :mod:`email.generator` module: -.. class:: Generator(outfp, mangle_from_=True, maxheaderlen=78, *, policy=None) +.. class:: Generator(outfp, mangle_from_=True, maxheaderlen=78, *, \ + policy=policy.default) The constructor for the :class:`Generator` class takes a :term:`file-like object` called *outfp* for an argument. *outfp* must support the :meth:`write` method @@ -54,9 +55,8 @@ The default is 78, as recommended (but not required) by :rfc:`2822`. The *policy* keyword specifies a :mod:`~email.policy` object that controls a - number of aspects of the generator's operation. If no *policy* is specified, - then the *policy* attached to the message object passed to :attr:`flatten` - is used. + number of aspects of the generator's operation. The default policy + maintains backward compatibility. .. versionchanged:: 3.3 Added the *policy* keyword. @@ -80,19 +80,19 @@ Optional *linesep* specifies the line separator character used to terminate lines in the output. If specified it overrides the value - specified by the *msg*\'s or ``Generator``\'s ``policy``. + specified by the ``Generator``\'s ``policy``. - Because strings cannot represent non-ASCII bytes, if the policy that - applies when ``flatten`` is run has :attr:`~email.policy.Policy.cte_type` - set to ``8bit``, ``Generator`` will operate as if it were set to - ``7bit``. This means that messages parsed with a Bytes parser that have - a :mailheader:`Content-Transfer-Encoding` of ``8bit`` will be converted - to a use a ``7bit`` Content-Transfer-Encoding. Non-ASCII bytes in the - headers will be :rfc:`2047` encoded with a charset of ``unknown-8bit``. + Because strings cannot represent non-ASCII bytes, ``Generator`` ignores + the value of the :attr:`~email.policy.Policy.must_be_7bit` + :mod:`~email.policy` setting and operates as if it were set ``True``. + This means that messages parsed with a Bytes parser that have a + :mailheader:`Content-Transfer-Encoding` of 8bit will be converted to a + use a 7bit Content-Transfer-Encoding. Non-ASCII bytes in the headers + will be :rfc:`2047` encoded with a charset of `unknown-8bit`. .. versionchanged:: 3.2 - Added support for re-encoding ``8bit`` message bodies, and the - *linesep* argument. + Added support for re-encoding 8bit message bodies, and the *linesep* + argument. .. method:: clone(fp) @@ -149,13 +149,13 @@ at *msg* to the output file specified when the :class:`BytesGenerator` instance was created. Subparts are visited depth-first and the resulting text will be properly MIME encoded. If the :mod:`~email.policy` option - :attr:`~email.policy.Policy.cte_type` is ``8bit`` (the default), + :attr:`~email.policy.Policy.must_be_7bit` is ``False`` (the default), then any bytes with the high bit set in the original parsed message that have not been modified will be copied faithfully to the output. If - ``cte_type`` is ``7bit``, the bytes will be converted as needed - using an ASCII-compatible Content-Transfer-Encoding. In particular, - RFC-invalid non-ASCII bytes in headers will be encoded using the MIME - ``unknown-8bit`` character set, thus rendering them RFC-compliant. + ``must_be_7bit`` is true, the bytes will be converted as needed using an + ASCII content-transfer-encoding. In particular, RFC-invalid non-ASCII + bytes in headers will be encoded using the MIME ``unknown-8bit`` + character set, thus rendering them RFC-compliant. .. XXX: There should be a complementary option that just does the RFC compliance transformation but leaves CTE 8bit parts alone. @@ -197,7 +197,7 @@ representing the part. -.. class:: DecodedGenerator(outfp, mangle_from_=True, maxheaderlen=78, fmt=None) +.. class:: DecodedGenerator(outfp[, mangle_from_=True, maxheaderlen=78, fmt=None) This class, derived from :class:`Generator` walks through all the subparts of a message. If the subpart is of main type :mimetype:`text`, then it prints the diff -r 2059910e7d76 -r ac776ef41428 Doc/library/email.header.rst --- a/Doc/library/email.header.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/email.header.rst Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ -:mod:`email.header`: Internationalized headers ----------------------------------------------- +:mod:`email`: Internationalized headers +--------------------------------------- .. module:: email.header :synopsis: Representing non-ASCII headers diff -r 2059910e7d76 -r ac776ef41428 Doc/library/email.headerregistry.rst --- a/Doc/library/email.headerregistry.rst Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,383 +0,0 @@ -:mod:`email.headerregistry`: Custom Header Objects --------------------------------------------------- - -.. module:: email.headerregistry - :synopsis: Automatic Parsing of headers based on the field name - -.. moduleauthor:: R. David Murray -.. sectionauthor:: R. David Murray - - -.. 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 -r 2059910e7d76 -r ac776ef41428 Doc/library/email.iterators.rst --- a/Doc/library/email.iterators.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/email.iterators.rst Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ -:mod:`email.iterators`: Iterators ---------------------------------- +:mod:`email`: Iterators +----------------------- .. module:: email.iterators :synopsis: Iterate over a message object tree. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/email.message.rst --- a/Doc/library/email.message.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/email.message.rst Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ -:mod:`email.message`: Representing an email message ---------------------------------------------------- +:mod:`email`: Representing an email message +------------------------------------------- .. module:: email.message :synopsis: The base class representing email messages. @@ -111,14 +111,10 @@ header. When ``True`` and the message is not a multipart, the payload will be decoded if this header's value is ``quoted-printable`` or ``base64``. If some other encoding is used, or :mailheader:`Content-Transfer-Encoding` - header is missing, the payload is + header is missing, or if the payload has bogus base64 data, the payload is returned as-is (undecoded). In all cases the returned value is binary data. If the message is a multipart and the *decode* flag is ``True``, - then ``None`` is returned. If the payload is base64 and it was not - perfectly formed (missing padding, characters outside the base64 - alphabet), then an appropriate defect will be added to the message's - defect property (:class:`~email.errors.InvalidBase64PaddingDefect` or - :class:`~email.errors.InvalidBase64CharactersDefect`, respectively). + then ``None`` is returned. When *decode* is ``False`` (the default) the body is returned as a string without decoding the :mailheader:`Content-Transfer-Encoding`. However, diff -r 2059910e7d76 -r ac776ef41428 Doc/library/email.mime.rst --- a/Doc/library/email.mime.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/email.mime.rst Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ -:mod:`email.mime`: Creating email and MIME objects from scratch ---------------------------------------------------------------- +:mod:`email`: Creating email and MIME objects from scratch +---------------------------------------------------------- .. module:: email.mime :synopsis: Build MIME messages. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/email.parser.rst --- a/Doc/library/email.parser.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/email.parser.rst Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ -:mod:`email.parser`: Parsing email messages -------------------------------------------- +:mod:`email`: Parsing email messages +------------------------------------ .. module:: email.parser :synopsis: Parse flat text email messages to produce a message object structure. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/email.policy.rst --- a/Doc/library/email.policy.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/email.policy.rst Mon May 21 23:01:17 2012 -0400 @@ -1,12 +1,9 @@ -:mod:`email.policy`: Policy Objects ------------------------------------ +:mod:`email`: Policy Objects +---------------------------- .. module:: email.policy :synopsis: Controlling the parsing and generating of messages -.. moduleauthor:: R. David Murray -.. sectionauthor:: R. David Murray - .. versionadded:: 3.3 @@ -26,100 +23,81 @@ control the behavior of various components of the email package during use. :class:`Policy` instances can be passed to various classes and methods in the email package to alter the default behavior. The settable values and their -defaults are described below. +defaults are described below. The :mod:`policy` module also provides some +pre-created :class:`Policy` instances. In addition to a :const:`default` +instance, there are instances tailored for certain applications. For example +there is an :const:`SMTP` :class:`Policy` with defaults appropriate for +generating output to be sent to an SMTP server. These are listed `below +`. -There is a default policy used by all classes in the email package. This -policy is named :class:`Compat32`, with a corresponding pre-defined instance -named :const:`compat32`. It provides for complete backward compatibility (in -some cases, including bug compatibility) with the pre-Python3.3 version of the -email package. - -The first part of this documentation covers the features of :class:`Policy`, an -:term:`abstract base class` that defines the features that are common to all -policy objects, including :const:`compat32`. This includes certain hook -methods that are called internally by the email package, which a custom policy -could override to obtain different behavior. - -When a :class:`~email.message.Message` object is created, it acquires a policy. -By default this will be :const:`compat32`, but a different policy can be -specified. If the ``Message`` is created by a :mod:`~email.parser`, a policy -passed to the parser will be the policy used by the ``Message`` it creates. If -the ``Message`` is created by the program, then the policy can be specified -when it is created. When a ``Message`` is passed to a :mod:`~email.generator`, -the generator uses the policy from the ``Message`` by default, but you can also -pass a specific policy to the generator that will override the one stored on -the ``Message`` object. - -:class:`Policy` instances are immutable, but they can be cloned, accepting the -same keyword arguments as the class constructor and returning a new -:class:`Policy` instance that is a copy of the original but with the specified -attributes values changed. +In general an application will only need to deal with setting the policy at the +input and output boundaries. Once parsed, a message is represented by a +:class:`~email.message.Message` object, which is designed to be independent of +the format that the message has "on the wire" when it is received, transmitted, +or displayed. Thus, a :class:`Policy` can be specified when parsing a message +to create a :class:`~email.message.Message`, and again when turning the +:class:`~email.message.Message` into some other representation. While often a +program will use the same :class:`Policy` for both input and output, the two +can be different. As an example, the following code could be used to read an email message from a file on disk and pass it to the system ``sendmail`` program on a Unix system:: >>> from email import msg_from_binary_file >>> from email.generator import BytesGenerator + >>> import email.policy >>> from subprocess import Popen, PIPE >>> with open('mymsg.txt', 'b') as f: - ... msg = msg_from_binary_file(f) + ... msg = msg_from_binary_file(f, policy=email.policy.mbox) >>> p = Popen(['sendmail', msg['To'][0].address], stdin=PIPE) - >>> g = BytesGenerator(p.stdin, policy=msg.policy.clone(linesep='\r\n')) + >>> g = BytesGenerator(p.stdin, policy=email.policy.SMTP) >>> g.flatten(msg) >>> p.stdin.close() >>> rc = p.wait() -Here we are telling :class:`~email.generator.BytesGenerator` to use the RFC -correct line separator characters when creating the binary string to feed into -``sendmail's`` ``stdin``, where the default policy would use ``\n`` line -separators. +.. XXX email.policy.mbox/MBOX does not exist yet Some email package methods accept a *policy* keyword argument, allowing the policy to be overridden for that method. For example, the following code uses -the :meth:`~email.message.Message.as_string` method of the *msg* object from -the previous example and writes the message to a file using the native line -separators for the platform on which it is running:: +the :meth:`~email.message.Message.as_string` method of the *msg* object from the +previous example and re-write it to a file using the native line separators for +the platform on which it is running:: >>> import os + >>> mypolicy = email.policy.Policy(linesep=os.linesep) >>> with open('converted.txt', 'wb') as f: - ... f.write(msg.as_string(policy=msg.policy.clone(linesep=os.linesep)) + ... f.write(msg.as_string(policy=mypolicy)) + +Policy instances are immutable, but they can be cloned, accepting the same +keyword arguments as the class constructor and returning a new :class:`Policy` +instance that is a copy of the original but with the specified attributes +values changed. For example, the following creates an SMTP policy that will +raise any defects detected as errors:: + + >>> strict_SMTP = email.policy.SMTP.clone(raise_on_defect=True) Policy objects can also be combined using the addition operator, producing a policy object whose settings are a combination of the non-default values of the summed objects:: - >>> compat_SMTP = email.policy.clone(linesep='\r\n') - >>> compat_strict = email.policy.clone(raise_on_defect=True) - >>> compat_strict_SMTP = compat_SMTP + compat_strict + >>> strict_SMTP = email.policy.SMTP + email.policy.strict This operation is not commutative; that is, the order in which the objects are added matters. To illustrate:: - >>> policy100 = compat32.clone(max_line_length=100) - >>> policy80 = compat32.clone(max_line_length=80) - >>> apolicy = policy100 + Policy80 + >>> Policy = email.policy.Policy + >>> apolicy = Policy(max_line_length=100) + Policy(max_line_length=80) >>> apolicy.max_line_length 80 - >>> apolicy = policy80 + policy100 + >>> apolicy = Policy(max_line_length=80) + Policy(max_line_length=100) >>> apolicy.max_line_length 100 .. class:: Policy(**kw) - This is the :term:`abstract base class` for all policy classes. It provides - default implementations for a couple of trivial methods, as well as the - implementation of the immutability property, the :meth:`clone` method, and - the constructor semantics. - - The constructor of a policy class can be passed various keyword arguments. - The arguments that may be specified are any non-method properties on this - class, plus any additional non-method properties on the concrete class. A - value specified in the constructor will override the default value for the - corresponding attribute. - - This class defines the following properties, and thus values for the - following may be passed in the constructor of any policy class: + The valid constructor keyword arguments are any of the attributes listed + below. .. attribute:: max_line_length @@ -132,28 +110,18 @@ The string to be used to terminate lines in serialized output. The default is ``\n`` because that's the internal end-of-line discipline used - by Python, though ``\r\n`` is required by the RFCs. + by Python, though ``\r\n`` is required by the RFCs. See `Policy + Instances`_ for policies that use an RFC conformant linesep. Setting it + to :attr:`os.linesep` may also be useful. - .. attribute:: cte_type + .. attribute:: must_be_7bit - Controls the type of Content Transfer Encodings that may be or are - required to be used. The possible values are: - - ======== =============================================================== - ``7bit`` all data must be "7 bit clean" (ASCII-only). This means that - where necessary data will be encoded using either - quoted-printable or base64 encoding. - - ``8bit`` data is not constrained to be 7 bit clean. Data in headers is - still required to be ASCII-only and so will be encoded (see - 'binary_fold' below for an exception), but body parts may use - the ``8bit`` CTE. - ======== =============================================================== - - A ``cte_type`` value of ``8bit`` only works with ``BytesGenerator``, not - ``Generator``, because strings cannot contain binary data. If a - ``Generator`` is operating under a policy that specifies - ``cte_type=8bit``, it will act as if ``cte_type`` is ``7bit``. + If ``True``, data output by a bytes generator is limited to ASCII + characters. If :const:`False` (the default), then bytes with the high + bit set are preserved and/or allowed in certain contexts (for example, + where possible a content transfer encoding of ``8bit`` will be used). + String generators act as if ``must_be_7bit`` is ``True`` regardless of + the policy in effect, since a string cannot represent non-ASCII bytes. .. attribute:: raise_on_defect @@ -161,337 +129,56 @@ :const:`False` (the default), defects will be passed to the :meth:`register_defect` method. - The following :class:`Policy` method is intended to be called by code using - the email library to create policy instances with custom settings: + :mod:`Policy` object also have the following methods: - .. method:: clone(**kw) + .. method:: handle_defect(obj, defect) + + *obj* is the object on which to register the defect. *defect* should be + an instance of a subclass of :class:`~email.errors.Defect`. + If :attr:`raise_on_defect` + is ``True`` the defect is raised as an exception. Otherwise *obj* and + *defect* are passed to :meth:`register_defect`. This method is intended + to be called by parsers when they encounter defects, and will not be + called by code that uses the email library unless that code is + implementing an alternate parser. + + .. method:: register_defect(obj, defect) + + *obj* is the object on which to register the defect. *defect* should be + a subclass of :class:`~email.errors.Defect`. This method is part of the + public API so that custom ``Policy`` subclasses can implement alternate + handling of defects. The default implementation calls the ``append`` + method of the ``defects`` attribute of *obj*. + + .. method:: clone(obj, *kw) Return a new :class:`Policy` instance whose attributes have the same values as the current instance, except where those attributes are given new values by the keyword arguments. - The remaining :class:`Policy` methods are called by the email package code, - and are not intended to be called by an application using the email package. - A custom policy must implement all of these methods. - .. method:: handle_defect(obj, defect) +Policy Instances +^^^^^^^^^^^^^^^^ - Handle a *defect* found on *obj*. When the email package calls this - method, *defect* will always be a subclass of - :class:`~email.errors.Defect`. - - The default implementation checks the :attr:`raise_on_defect` flag. If - it is ``True``, *defect* is raised as an exception. If it is ``False`` - (the default), *obj* and *defect* are passed to :meth:`register_defect`. - - .. method:: register_defect(obj, defect) - - Register a *defect* on *obj*. In the email package, *defect* will always - be a subclass of :class:`~email.errors.Defect`. - - The default implementation calls the ``append`` method of the ``defects`` - attribute of *obj*. When the email package calls :attr:`handle_defect`, - *obj* will normally have a ``defects`` attribute that has an ``append`` - method. Custom object types used with the email package (for example, - custom ``Message`` objects) should also provide such an attribute, - otherwise defects in parsed messages will raise unexpected errors. - - .. method:: header_max_count(name) - - Return the maximum allowed number of headers named *name*. - - Called when a header is added to a :class:`~email.message.Message` - object. If the returned value is not ``0`` or ``None``, and there are - already a number of headers with the name *name* equal to the value - returned, a :exc:`ValueError` is raised. - - Because the default behavior of ``Message.__setitem__`` is to append the - value to the list of headers, it is easy to create duplicate headers - without realizing it. This method allows certain headers to be limited - in the number of instances of that header that may be added to a - ``Message`` programmatically. (The limit is not observed by the parser, - which will faithfully produce as many headers as exist in the message - being parsed.) - - The default implementation returns ``None`` for all header names. - - .. method:: header_source_parse(sourcelines) - - The email package calls this method with a list of strings, each string - ending with the line separation characters found in the source being - parsed. The first line includes the field header name and separator. - All whitespace in the source is preserved. The method should return the - ``(name, value)`` tuple that is to be stored in the ``Message`` to - represent the parsed header. - - If an implementation wishes to retain compatibility with the existing - email package policies, *name* should be the case preserved name (all - characters up to the '``:``' separator), while *value* should be the - unfolded value (all line separator characters removed, but whitespace - kept intact), stripped of leading whitespace. - - *sourcelines* may contain surrogateescaped binary data. - - There is no default implementation - - .. method:: header_store_parse(name, value) - - The email package calls this method with the name and value provided by - the application program when the application program is modifying a - ``Message`` programmatically (as opposed to a ``Message`` created by a - parser). The method should return the ``(name, value)`` tuple that is to - be stored in the ``Message`` to represent the header. - - If an implementation wishes to retain compatibility with the existing - email package policies, the *name* and *value* should be strings or - string subclasses that do not change the content of the passed in - arguments. - - There is no default implementation - - .. method:: header_fetch_parse(name, value) - - The email package calls this method with the *name* and *value* currently - stored in the ``Message`` when that header is requested by the - application program, and whatever the method returns is what is passed - back to the application as the value of the header being retrieved. - Note that there may be more than one header with the same name stored in - the ``Message``; the method is passed the specific name and value of the - header destined to be returned to the application. - - *value* may contain surrogateescaped binary data. There should be no - surrogateescaped binary data in the value returned by the method. - - There is no default implementation - - .. method:: fold(name, value) - - The email package calls this method with the *name* and *value* currently - stored in the ``Message`` for a given header. The method should return a - string that represents that header "folded" correctly (according to the - policy settings) by composing the *name* with the *value* and inserting - :attr:`linesep` characters at the appropriate places. See :rfc:`5322` - for a discussion of the rules for folding email headers. - - *value* may contain surrogateescaped binary data. There should be no - surrogateescaped binary data in the string returned by the method. - - .. method:: fold_binary(name, value) - - The same as :meth:`fold`, except that the returned value should be a - bytes object rather than a string. - - *value* may contain surrogateescaped binary data. These could be - converted back into binary data in the returned bytes object. - - -.. class:: Compat32(**kw) - - This concrete :class:`Policy` is the backward compatibility policy. It - replicates the behavior of the email package in Python 3.2. The - :mod:`policy` module also defines an instance of this class, - :const:`compat32`, that is used as the default policy. Thus the default - behavior of the email package is to maintain compatibility with Python 3.2. - - The class provides the following concrete implementations of the - abstract methods of :class:`Policy`: - - .. method:: header_source_parse(sourcelines) - - The name is parsed as everything up to the '``:``' and returned - unmodified. The value is determined by stripping leading whitespace off - the remainder of the first line, joining all subsequent lines together, - and stripping any trailing carriage return or linefeed characters. - - .. method:: header_store_parse(name, value) - - The name and value are returned unmodified. - - .. method:: header_fetch_parse(name, value) - - If the value contains binary data, it is converted into a - :class:`~email.header.Header` object using the ``unknown-8bit`` charset. - Otherwise it is returned unmodified. - - .. method:: fold(name, value) - - Headers are folded using the :class:`~email.header.Header` folding - algorithm, which preserves existing line breaks in the value, and wraps - each resulting line to the ``max_line_length``. Non-ASCII binary data are - CTE encoded using the ``unknown-8bit`` charset. - - .. method:: fold_binary(name, value) - - Headers are folded using the :class:`~email.header.Header` folding - algorithm, which preserves existing line breaks in the value, and wraps - each resulting line to the ``max_line_length``. If ``cte_type`` is - ``7bit``, non-ascii binary data is CTE encoded using the ``unknown-8bit`` - charset. Otherwise the original source header is used, with its existing - line breaks and and any (RFC invalid) binary data it may contain. - - -.. note:: - - 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) - - This concrete :class:`Policy` provides behavior that is intended to be fully - compliant with the current email RFCs. These include (but are not limited - to) :rfc:`5322`, :rfc:`2047`, and the current MIME RFCs. - - This policy adds new header parsing and folding algorithms. Instead of - simple strings, headers are custom objects with custom attributes depending - on the type of the field. The parsing and folding algorithm fully implement - :rfc:`2047` and :rfc:`5322`. - - In addition to the settable attributes listed above that apply to all - policies, this policy adds the following additional attributes: - - .. attribute:: refold_source - - If the value for a header in the ``Message`` object originated from a - :mod:`~email.parser` (as opposed to being set by a program), this - attribute indicates whether or not a generator should refold that value - when transforming the message back into stream form. The possible values - are: - - ======== =============================================================== - ``none`` all source values use original folding - - ``long`` source values that have any line that is longer than - ``max_line_length`` will be refolded - - ``all`` all values are refolded. - ======== =============================================================== - - The default is ``long``. - - .. attribute:: header_factory - - 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 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`: - - .. method:: header_max_count(name) - - Returns the value of the - :attr:`~email.headerregistry.BaseHeader.max_count` attribute of the - specialized class used to represent the header with the given name. - - .. method:: header_source_parse(sourcelines) - - The implementation of this method is the same as that for the - :class:`Compat32` policy. - - .. method:: header_store_parse(name, value) - - The name is returned unchanged. If the input value has a ``name`` - attribute and it matches *name* ignoring case, the value is returned - unchanged. Otherwise the *name* and *value* are passed to - ``header_factory``, and the resulting custom header object is returned as - the value. In this case a ``ValueError`` is raised if the input value - contains CR or LF characters. - - .. method:: header_fetch_parse(name, value) - - If the value has a ``name`` attribute, it is returned to unmodified. - Otherwise the *name*, and the *value* with any CR or LF characters - removed, are passed to the ``header_factory``, and the resulting custom - header object is returned. Any surrogateescaped bytes get turned into - the unicode unknown-character glyph. - - .. method:: fold(name, value) - - Header folding is controlled by the :attr:`refold_source` policy setting. - A value is considered to be a 'source value' if and only if it does not - have a ``name`` attribute (having a ``name`` attribute means it is a - header object of some sort). If a source value needs to be refolded - according to the policy, it is converted into a custom header object by - passing the *name* and the *value* with any CR and LF characters removed - to the ``header_factory``. Folding of a custom header object is done by - calling its ``fold`` method with the current policy. - - Source values are split into lines using :meth:`~str.splitlines`. If - the value is not to be refolded, the lines are rejoined using the - ``linesep`` from the policy and returned. The exception is lines - containing non-ascii binary data. In that case the value is refolded - regardless of the ``refold_source`` setting, which causes the binary data - to be CTE encoded using the ``unknown-8bit`` charset. - - .. method:: fold_binary(name, value) - - The same as :meth:`fold` if :attr:`cte_type` is ``7bit``, except that - the returned value is bytes. - - If :attr:`cte_type` is ``8bit``, non-ASCII binary data is converted back - into bytes. Headers with binary data are not refolded, regardless of the - ``refold_header`` setting, since there is no way to know whether the - binary data consists of single byte characters or multibyte characters. - -The following instances of :class:`EmailPolicy` provide defaults suitable for -specific application domains. Note that in the future the behavior of these -instances (in particular the ``HTTP`` instance) may be adjusted to conform even -more closely to the RFCs relevant to their domains. +The following instances of :class:`Policy` provide defaults suitable for +specific common application domains. .. data:: default - An instance of ``EmailPolicy`` with all defaults unchanged. This policy - uses the standard Python ``\n`` line endings rather than the RFC-correct - ``\r\n``. + An instance of :class:`Policy` with all defaults unchanged. .. data:: SMTP - Suitable for serializing messages in conformance with the email RFCs. - Like ``default``, but with ``linesep`` set to ``\r\n``, which is RFC - compliant. + Output serialized from a message will conform to the email and SMTP + RFCs. The only changed attribute is :attr:`linesep`, which is set to + ``\r\n``. .. data:: HTTP - Suitable for serializing headers with for use in HTTP traffic. Like - ``SMTP`` except that ``max_line_length`` is set to ``None`` (unlimited). + Suitable for use when serializing headers for use in HTTP traffic. + :attr:`linesep` is set to ``\r\n``, and :attr:`max_line_length` is set to + :const:`None` (unlimited). .. data:: strict - Convenience instance. The same as ``default`` except that - ``raise_on_defect`` is set to ``True``. This allows any policy to be made - strict by writing:: - - somepolicy + policy.strict - -With all of these :class:`EmailPolicies <.EmailPolicy>`, the effective API of -the email package is changed from the Python 3.2 API in the following ways: - - * Setting a header on a :class:`~email.message.Message` results in that - header being parsed and a custom header object created. - - * Fetching a header value from a :class:`~email.message.Message` results - in that header being parsed and a custom header object created and - returned. - - * Any custom header object, or any header that is refolded due to the - policy settings, is folded using an algorithm that fully implements the - RFC folding algorithms, including knowing where encoded words are required - and allowed. - -From the application view, this means that any header obtained through the -:class:`~email.message.Message` is a custom header object with custom -attributes, whose string value is the fully decoded unicode value of the -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 in -:mod:`~email.headerregistry`. + :attr:`raise_on_defect` is set to :const:`True`. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/email.rst --- a/Doc/library/email.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/email.rst Mon May 21 23:01:17 2012 -0400 @@ -52,7 +52,6 @@ email.parser.rst email.generator.rst email.policy.rst - email.headerregistry.rst email.mime.rst email.header.rst email.charset.rst diff -r 2059910e7d76 -r ac776ef41428 Doc/library/email.util.rst --- a/Doc/library/email.util.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/email.util.rst Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ -:mod:`email.utils`: Miscellaneous utilities -------------------------------------------- +:mod:`email`: Miscellaneous utilities +------------------------------------- .. module:: email.utils :synopsis: Miscellaneous email package utilities. @@ -93,6 +93,8 @@ corresponding a :class:`~datetime.timezone` :class:`~datetime.tzinfo`. .. versionadded:: 3.3 + + .. function:: mktime_tz(tuple) Turn a 10-tuple as returned by :func:`parsedate_tz` into a UTC timestamp. It @@ -138,22 +140,6 @@ .. versionadded:: 3.3 -.. function:: localtime(dt=None) - - Return local time as an aware datetime object. If called without - arguments, return current time. Otherwise *dt* argument should be a - :class:`~datetime.datetime` instance, and it is converted to the local time - zone according to the system time zone database. If *dt* is naive (that - is, ``dt.tzinfo`` is ``None``), it is assumed to be in local time. In this - case, a positive or zero value for *isdst* causes ``localtime`` to presume - initially that summer time (for example, Daylight Saving Time) is or is not - (respectively) in effect for the specified time. A negative value for - *isdst* causes the ``localtime`` to attempt to divine whether summer time - is in effect for the specified time. - - .. versionadded:: 3.3 - - .. function:: make_msgid(idstring=None, domain=None) Returns a string suitable for an :rfc:`2822`\ -compliant diff -r 2059910e7d76 -r ac776ef41428 Doc/library/functions.rst --- a/Doc/library/functions.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/functions.rst Mon May 21 23:01:17 2012 -0400 @@ -797,20 +797,17 @@ *mode* is an optional string that specifies the mode in which the file is opened. It defaults to ``'r'`` which means open for reading in text mode. Other common values are ``'w'`` for writing (truncating the file if it - already exists), ``'x'`` for exclusive creation and ``'a'`` for appending - (which on *some* Unix systems, means that *all* writes append to the end of - the file regardless of the current seek position). In text mode, if - *encoding* is not specified the encoding used is platform dependent: - ``locale.getpreferredencoding(False)`` is called to get the current locale - encoding. (For reading and writing raw bytes use binary mode and leave - *encoding* unspecified.) The available modes are: + already exists), and ``'a'`` for appending (which on *some* Unix systems, + means that *all* writes append to the end of the file regardless of the + current seek position). In text mode, if *encoding* is not specified the + encoding used is platform dependent. (For reading and writing raw bytes use + binary mode and leave *encoding* unspecified.) The available modes are: ========= =============================================================== Character Meaning --------- --------------------------------------------------------------- ``'r'`` open for reading (default) ``'w'`` open for writing, truncating the file first - ``'x'`` open for exclusive creation, failing if the file already exists ``'a'`` open for writing, appending to the end of the file if it exists ``'b'`` binary mode ``'t'`` text mode (default) @@ -901,7 +898,6 @@ .. versionchanged:: 3.3 The *opener* parameter was added. - The ``'x'`` mode was added. The type of file object returned by the :func:`open` function depends on the mode. When :func:`open` is used to open a file in a text mode (``'w'``, @@ -930,8 +926,6 @@ .. versionchanged:: 3.3 :exc:`IOError` used to be raised, it is now an alias of :exc:`OSError`. - :exc:`FileExistsError` is now raised if the file opened in exclusive - creation mode (``'x'``) already exists. .. XXX works for bytes too, but should it? @@ -1330,12 +1324,10 @@ Accordingly, :func:`super` is undefined for implicit lookups using statements or operators such as ``super()[name]``. - Also note that, aside from the zero argument form, :func:`super` is not - limited to use inside methods. The two argument form specifies the - arguments exactly and makes the appropriate references. The zero - argument form only works inside a class definition, as the compiler fills - in the necessary details to correctly retrieve the class being defined, - as well as accessing the current instance for ordinary methods. + Also note that :func:`super` is not limited to use inside methods. The two + argument form specifies the arguments exactly and makes the appropriate + references. The zero argument form automatically searches the stack frame + for the class (``__class__``) and the first argument. For practical suggestions on how to design cooperative classes using :func:`super`, see `guide to using super() diff -r 2059910e7d76 -r ac776ef41428 Doc/library/functools.rst --- a/Doc/library/functools.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/functools.rst Mon May 21 23:01:17 2012 -0400 @@ -40,7 +40,7 @@ .. versionadded:: 3.2 -.. decorator:: lru_cache(maxsize=128, typed=False) +.. decorator:: lru_cache(maxsize=100, typed=False) Decorator to wrap a function with a memoizing callable that saves up to the *maxsize* most recent calls. It can save time when an expensive or I/O bound @@ -49,9 +49,8 @@ Since a dictionary is used to cache results, the positional and keyword arguments to the function must be hashable. - If *maxsize* is set to None, the LRU feature is disabled and the cache can - grow without bound. The LRU feature performs best when *maxsize* is a - power-of-two. + If *maxsize* is set to None, the LRU feature is disabled and the cache + can grow without bound. If *typed* is set to True, function arguments of different types will be cached separately. For example, ``f(3)`` and ``f(3.0)`` will be treated diff -r 2059910e7d76 -r ac776ef41428 Doc/library/gzip.rst --- a/Doc/library/gzip.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/gzip.rst Mon May 21 23:01:17 2012 -0400 @@ -14,10 +14,10 @@ The data compression is provided by the :mod:`zlib` module. The :mod:`gzip` module provides the :class:`GzipFile` class, as well as the -:func:`.open`, :func:`compress` and :func:`decompress` convenience functions. -The :class:`GzipFile` class reads and writes :program:`gzip`\ -format files, -automatically compressing or decompressing the data so that it looks like an -ordinary :term:`file object`. +:func:`gzip.open`, :func:`compress` and :func:`decompress` convenience +functions. The :class:`GzipFile` class reads and writes :program:`gzip`\ -format +files, automatically compressing or decompressing the data so that it looks like +an ordinary :term:`file object`. Note that additional file formats which can be decompressed by the :program:`gzip` and :program:`gunzip` programs, such as those produced by @@ -28,11 +28,9 @@ .. function:: open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None) - Open a gzip-compressed file in binary or text mode, returning a :term:`file - object`. + Open *filename* as a gzip-compressed file in binary or text mode. - The *filename* argument can be an actual filename (a :class:`str` or - :class:`bytes` object), or an existing file object to read from or write to. + Returns a :term:`file object`. The *mode* argument can be any of ``'r'``, ``'rb'``, ``'a'``, ``'ab'``, ``'w'``, or ``'wb'`` for binary mode, or ``'rt'``, ``'at'``, or ``'wt'`` for @@ -50,8 +48,8 @@ handling behavior, and line ending(s). .. versionchanged:: 3.3 - Added support for *filename* being a file object, support for text mode, - and the *encoding*, *errors* and *newline* arguments. + Support for text mode was added, along with the *encoding*, *errors* and + *newline* arguments. .. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None) @@ -77,7 +75,7 @@ is the mode of *fileobj* if discernible; otherwise, the default is ``'rb'``. Note that the file is always opened in binary mode. To open a compressed file - in text mode, use :func:`.open` (or wrap your :class:`GzipFile` with an + in text mode, use :func:`gzip.open` (or wrap your :class:`GzipFile` with an :class:`io.TextIOWrapper`). The *compresslevel* argument is an integer from ``1`` to ``9`` controlling the diff -r 2059910e7d76 -r ac776ef41428 Doc/library/http.client.rst --- a/Doc/library/http.client.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/http.client.rst Mon May 21 23:01:17 2012 -0400 @@ -382,8 +382,8 @@ | | | :rfc:`6585`, Section 6 | +------------------------------------------+---------+-----------------------------------------------------------------------+ -.. versionchanged:: 3.3 - Added codes ``428``, ``429``, ``431`` and ``511`` from :rfc:`6585`. + .. versionchanged:: 3.3 + Added codes ``428``, ``429``, ``431`` and ``511`` from :rfc:`6585`. .. data:: responses diff -r 2059910e7d76 -r ac776ef41428 Doc/library/http.cookiejar.rst --- a/Doc/library/http.cookiejar.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/http.cookiejar.rst Mon May 21 23:01:17 2012 -0400 @@ -707,7 +707,7 @@ The :class:`Cookie` class also defines the following method: -.. method:: Cookie.is_expired(now=None) +.. method:: Cookie.is_expired([now=None]) True if cookie has passed the time at which the server requested it should expire. If *now* is given (in seconds since the epoch), return whether the diff -r 2059910e7d76 -r ac776ef41428 Doc/library/http.server.rst --- a/Doc/library/http.server.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/http.server.rst Mon May 21 23:01:17 2012 -0400 @@ -400,9 +400,3 @@ Note that CGI scripts will be run with UID of user nobody, for security reasons. Problems with the CGI script will be translated to error 403. - -:class:`CGIHTTPRequestHandler` can be enabled in the command line by passing -the ``--cgi`` option.:: - - python -m http.server --cgi 8000 - diff -r 2059910e7d76 -r ac776ef41428 Doc/library/imp.rst --- a/Doc/library/imp.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/imp.rst Mon May 21 23:01:17 2012 -0400 @@ -107,6 +107,37 @@ in ``sys.modules``. +.. function:: lock_held() + + Return ``True`` if the import lock is currently held, else ``False``. On + platforms without threads, always return ``False``. + + On platforms with threads, a thread executing an import holds an internal lock + until the import is complete. This lock blocks other threads from doing an + import until the original import completes, which in turn prevents other threads + from seeing incomplete module objects constructed by the original thread while + in the process of completing its import (and the imports, if any, triggered by + that). + + +.. function:: acquire_lock() + + Acquire the interpreter's import lock for the current thread. This lock should + be used by import hooks to ensure thread-safety when importing modules. + + Once a thread has acquired the import lock, the same thread may acquire it + again without blocking; the thread must release it once for each time it has + acquired it. + + On platforms without threads, this function does nothing. + + +.. function:: release_lock() + + Release the interpreter's import lock. On platforms without threads, this + function does nothing. + + .. function:: reload(module) Reload a previously imported *module*. The argument must be a module object, so @@ -204,49 +235,6 @@ magic number, as returned by :func:`get_magic`. -The following functions help interact with the import system's internal -locking mechanism. Locking semantics of imports are an implementation -detail which may vary from release to release. However, Python ensures -that circular imports work without any deadlocks. - -.. versionchanged:: 3.3 - In Python 3.3, the locking scheme has changed to per-module locks for - the most part. A global import lock is kept for some critical tasks, - such as initializing the per-module locks. - - -.. function:: lock_held() - - Return ``True`` if the global import lock is currently held, else - ``False``. On platforms without threads, always return ``False``. - - On platforms with threads, a thread executing an import first holds a - global import lock, then sets up a per-module lock for the rest of the - import. This blocks other threads from importing the same module until - the original import completes, preventing other threads from seeing - incomplete module objects constructed by the original thread. An - exception is made for circular imports, which by construction have to - expose an incomplete module object at some point. - -.. function:: acquire_lock() - - Acquire the interpreter's global import lock for the current thread. - This lock should be used by import hooks to ensure thread-safety when - importing modules. - - Once a thread has acquired the import lock, the same thread may acquire it - again without blocking; the thread must release it once for each time it has - acquired it. - - On platforms without threads, this function does nothing. - - -.. function:: release_lock() - - Release the interpreter's global import lock. On platforms without - threads, this function does nothing. - - The following constants with integer values, defined in this module, are used to indicate the search result of :func:`find_module`. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/io.rst --- a/Doc/library/io.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/io.rst Mon May 21 23:01:17 2012 -0400 @@ -752,7 +752,7 @@ It inherits :class:`TextIOBase`. *encoding* gives the name of the encoding that the stream will be decoded or - encoded with. It defaults to ``locale.getpreferredencoding(False)``. + encoded with. It defaults to :func:`locale.getpreferredencoding`. *errors* is an optional string that specifies how encoding and decoding errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError` @@ -784,12 +784,6 @@ .. versionchanged:: 3.3 The *write_through* argument has been added. - .. versionchanged:: 3.3 - The default *encoding* is now ``locale.getpreferredencoding(False)`` - instead of ``locale.getpreferredencoding()``. Don't change temporary the - locale encoding using :func:`locale.setlocale`, use the current locale - encoding instead of the user preferred encoding. - :class:`TextIOWrapper` provides one attribute in addition to those of :class:`TextIOBase` and its parents: diff -r 2059910e7d76 -r ac776ef41428 Doc/library/json.rst --- a/Doc/library/json.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/json.rst Mon May 21 23:01:17 2012 -0400 @@ -213,9 +213,6 @@ This can be used to raise an exception if invalid JSON numbers are encountered. - .. versionchanged:: 3.1 - *parse_constant* doesn't get called on 'null', 'true', 'false' anymore. - To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls`` kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments will be passed to the constructor of the class. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/locale.rst --- a/Doc/library/locale.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/locale.rst Mon May 21 23:01:17 2012 -0400 @@ -160,22 +160,22 @@ .. data:: D_T_FMT - Get a string that can be used as a format string for :func:`time.strftime` to + Get a string that can be used as a format string for :func:`strftime` to represent date and time in a locale-specific way. .. data:: D_FMT - Get a string that can be used as a format string for :func:`time.strftime` to + Get a string that can be used as a format string for :func:`strftime` to represent a date in a locale-specific way. .. data:: T_FMT - Get a string that can be used as a format string for :func:`time.strftime` to + Get a string that can be used as a format string for :func:`strftime` to represent a time in a locale-specific way. .. data:: T_FMT_AMPM - Get a format string for :func:`time.strftime` to represent time in the am/pm + Get a format string for :func:`strftime` to represent time in the am/pm format. .. data:: DAY_1 ... DAY_7 @@ -239,24 +239,24 @@ then-emperor's reign. Normally it should not be necessary to use this value directly. Specifying - the ``E`` modifier in their format strings causes the :func:`time.strftime` + the ``E`` modifier in their format strings causes the :func:`strftime` function to use this information. The format of the returned string is not specified, and therefore you should not assume knowledge of it on different systems. .. data:: ERA_D_T_FMT - Get a format string for :func:`time.strftime` to represent date and time in a + Get a format string for :func:`strftime` to represent date and time in a locale-specific era-based way. .. data:: ERA_D_FMT - Get a format string for :func:`time.strftime` to represent a date in a + Get a format string for :func:`strftime` to represent a date in a locale-specific era-based way. .. data:: ERA_T_FMT - Get a format string for :func:`time.strftime` to represent a time in a + Get a format string for :func:`strftime` to represent a time in a locale-specific era-based way. .. data:: ALT_DIGITS @@ -475,11 +475,8 @@ locale somewhat painful to use correctly. Initially, when a program is started, the locale is the ``C`` locale, no matter -what the user's preferred locale is. There is one exception: the -:data:`LC_CTYPE` category is changed at startup to set the current locale -encoding to the user's preferred locale encoding. The program must explicitly -say that it wants the user's preferred locale settings for other categories by -calling ``setlocale(LC_ALL, '')``. +what the user's preferred locale is. The program must explicitly say that it +wants the user's preferred locale settings by calling ``setlocale(LC_ALL, '')``. It is generally a bad idea to call :func:`setlocale` in some library routine, since as a side effect it affects the entire program. Saving and restoring it diff -r 2059910e7d76 -r ac776ef41428 Doc/library/logging.rst --- a/Doc/library/logging.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/logging.rst Mon May 21 23:01:17 2012 -0400 @@ -962,8 +962,7 @@ effect is to disable all logging calls of severity *lvl* and below, so that if you call it with a value of INFO, then all INFO and DEBUG events would be discarded, whereas those of severity WARNING and above would be processed - according to the logger's effective level. To undo the effect of a call to - ``logging.disable(lvl)``, call ``logging.disable(logging.NOTSET)``. + according to the logger's effective level. .. function:: addLevelName(lvl, levelName) diff -r 2059910e7d76 -r ac776ef41428 Doc/library/lzma.rst --- a/Doc/library/lzma.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/lzma.rst Mon May 21 23:01:17 2012 -0400 @@ -29,50 +29,17 @@ Reading and writing compressed files ------------------------------------ -.. function:: open(filename, mode="rb", \*, format=None, check=-1, preset=None, filters=None, encoding=None, errors=None, newline=None) +.. class:: LZMAFile(filename=None, mode="r", \*, fileobj=None, format=None, check=-1, preset=None, filters=None) - Open an LZMA-compressed file in binary or text mode, returning a :term:`file - object`. + Open an LZMA-compressed file. - The *filename* argument can be either an actual file name (given as a - :class:`str` or :class:`bytes` object), in which case the named file is - opened, or it can be an existing file object to read from or write to. - - The *mode* argument can be any of ``"r"``, ``"rb"``, ``"w"``, ``"wb"``, - ``"a"`` or ``"ab"`` for binary mode, or ``"rt"``, ``"wt"``, or ``"at"`` for - text mode. The default is ``"rb"``. - - When opening a file for reading, the *format* and *filters* arguments have - the same meanings as for :class:`LZMADecompressor`. In this case, the *check* - and *preset* arguments should not be used. - - When opening a file for writing, the *format*, *check*, *preset* and - *filters* arguments have the same meanings as for :class:`LZMACompressor`. - - For binary mode, this function is equivalent to the :class:`LZMAFile` - constructor: ``LZMAFile(filename, mode, ...)``. In this case, the *encoding*, - *errors* and *newline* arguments must not be provided. - - For text mode, a :class:`LZMAFile` object is created, and wrapped in an - :class:`io.TextIOWrapper` instance with the specified encoding, error - handling behavior, and line ending(s). - - -.. class:: LZMAFile(filename=None, mode="r", \*, format=None, check=-1, preset=None, filters=None) - - Open an LZMA-compressed file in binary mode. - - An :class:`LZMAFile` can wrap an already-open :term:`file object`, or operate - directly on a named file. The *filename* argument specifies either the file - object to wrap, or the name of the file to open (as a :class:`str` or - :class:`bytes` object). When wrapping an existing file object, the wrapped - file will not be closed when the :class:`LZMAFile` is closed. + An :class:`LZMAFile` can wrap an existing :term:`file object` (given by + *fileobj*), or operate directly on a named file (named by *filename*). + Exactly one of these two parameters should be provided. If *fileobj* is + provided, it is not closed when the :class:`LZMAFile` is closed. The *mode* argument can be either ``"r"`` for reading (default), ``"w"`` for - overwriting, or ``"a"`` for appending. These can equivalently be given as - ``"rb"``, ``"wb"``, and ``"ab"`` respectively. - - If *filename* is a file object (rather than an actual file name), a mode of + overwriting, or ``"a"`` for appending. If *fileobj* is provided, a mode of ``"w"`` does not truncate the file, and is instead equivalent to ``"a"``. When opening a file for reading, the input file may be the concatenation of @@ -393,7 +360,7 @@ import lzma with open("file.xz", "wb") as f: f.write(b"This data will not be compressed\n") - with lzma.LZMAFile(f, "w") as lzf: + with lzma.LZMAFile(fileobj=f, mode="w") as lzf: lzf.write(b"This *will* be compressed\n") f.write(b"Not compressed\n") diff -r 2059910e7d76 -r ac776ef41428 Doc/library/multiprocessing.rst --- a/Doc/library/multiprocessing.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/multiprocessing.rst Mon May 21 23:01:17 2012 -0400 @@ -120,7 +120,9 @@ print(q.get()) # prints "[42, None, 'hello']" p.join() - Queues are thread and process safe. + Queues are thread and process safe, but note that they must never + be instantiated as a side effect of importing a module: this can lead + to a deadlock! (see :ref:`threaded-imports`) **Pipes** @@ -953,7 +955,7 @@ It is possible to create shared objects using shared memory which can be inherited by child processes. -.. function:: Value(typecode_or_type, *args, lock=True) +.. function:: Value(typecode_or_type, *args[, lock]) Return a :mod:`ctypes` object allocated from shared memory. By default the return value is actually a synchronized wrapper for the object. @@ -1045,7 +1047,7 @@ attributes which allow one to use it to store and retrieve strings -- see documentation for :mod:`ctypes`. -.. function:: Array(typecode_or_type, size_or_initializer, *, lock=True) +.. function:: Array(typecode_or_type, size_or_initializer, *args[, lock]) The same as :func:`RawArray` except that depending on the value of *lock* a process-safe synchronization wrapper may be returned instead of a raw ctypes @@ -1060,7 +1062,7 @@ Note that *lock* is a keyword-only argument. -.. function:: Value(typecode_or_type, *args, lock=True) +.. function:: Value(typecode_or_type, *args[, lock]) The same as :func:`RawValue` except that depending on the value of *lock* a process-safe synchronization wrapper may be returned instead of a raw ctypes diff -r 2059910e7d76 -r ac776ef41428 Doc/library/nis.rst --- a/Doc/library/nis.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/nis.rst Mon May 21 23:01:17 2012 -0400 @@ -17,7 +17,7 @@ The :mod:`nis` module defines the following functions: -.. function:: match(key, mapname, domain=default_domain) +.. function:: match(key, mapname[, domain=default_domain]) Return the match for *key* in map *mapname*, or raise an error (:exc:`nis.error`) if there is none. Both should be strings, *key* is 8-bit @@ -30,7 +30,7 @@ unspecified, lookup is in the default NIS domain. -.. function:: cat(mapname, domain=default_domain) +.. function:: cat(mapname[, domain=default_domain]) Return a dictionary mapping *key* to *value* such that ``match(key, mapname)==value``. Note that both keys and values of the dictionary are @@ -42,7 +42,7 @@ unspecified, lookup is in the default NIS domain. -.. function:: maps(domain=default_domain) +.. function:: maps([domain=default_domain]) Return a list of all valid maps. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/operator.rst --- a/Doc/library/operator.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/operator.rst Mon May 21 23:01:17 2012 -0400 @@ -404,7 +404,7 @@ +-----------------------+-------------------------+---------------------------------------+ Inplace Operators ------------------ +================= Many operations have an "in-place" version. Listed below are functions providing a more primitive access to in-place operators than the usual syntax diff -r 2059910e7d76 -r ac776ef41428 Doc/library/os.rst --- a/Doc/library/os.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/os.rst Mon May 21 23:01:17 2012 -0400 @@ -600,13 +600,26 @@ These functions create new :term:`file objects `. (See also :func:`open`.) -.. function:: fdopen(fd, *args, **kwargs) - - Return an open file object connected to the file descriptor *fd*. - This is an alias of :func:`open` and accepts the same arguments. - The only difference is that the first argument of :func:`fdopen` - must always be an integer. - +.. function:: fdopen(fd[, mode[, bufsize]]) + + .. index:: single: I/O control; buffering + + Return an open file object connected to the file descriptor *fd*. The *mode* + and *bufsize* arguments have the same meaning as the corresponding arguments to + the built-in :func:`open` function. + + When specified, the *mode* argument must start with one of the letters + ``'r'``, ``'w'``, ``'x'`` or ``'a'``, otherwise a :exc:`ValueError` is + raised. + + On Unix, when the *mode* argument starts with ``'a'``, the *O_APPEND* flag is + set on the file descriptor (which the :c:func:`fdopen` implementation already + does on most platforms). + + Availability: Unix, Windows. + + .. versionchanged:: 3.3 + The ``'x'`` mode was added. .. _os-fd-ops: @@ -1773,7 +1786,7 @@ Availability: Unix. -.. function:: mknod(filename[, mode=0o600[, device=0]]) +.. function:: mknod(filename[, mode=0o600[, device]]) Create a filesystem node (file, device special file or named pipe) named *filename*. *mode* specifies both the permissions to use and the type of node @@ -2128,8 +2141,6 @@ are processed, this application should turn the feature off until the library has been corrected. - .. deprecated:: 3.3 - .. function:: statvfs(path) diff -r 2059910e7d76 -r ac776ef41428 Doc/library/ossaudiodev.rst --- a/Doc/library/ossaudiodev.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/ossaudiodev.rst Mon May 21 23:01:17 2012 -0400 @@ -281,7 +281,7 @@ simple calculations. -.. method:: oss_audio_device.setparameters(format, nchannels, samplerate[, strict=False]) +.. method:: oss_audio_device.setparameters(format, nchannels, samplerate [, strict=False]) Set the key audio sampling parameters---sample format, number of channels, and sampling rate---in one method call. *format*, *nchannels*, and *samplerate* diff -r 2059910e7d76 -r ac776ef41428 Doc/library/packaging.compiler.rst --- a/Doc/library/packaging.compiler.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/packaging.compiler.rst Mon May 21 23:01:17 2012 -0400 @@ -255,7 +255,7 @@ that the runtime linker may search by default. - .. method:: CCompiler.define_macro(name, value=None) + .. method:: CCompiler.define_macro(name[, value=None]) Define a preprocessor macro for all compilations driven by this compiler object. The optional parameter *value* should be a string; if it is not @@ -298,7 +298,7 @@ (a list) to do the job. - .. method:: CCompiler.find_library_file(dirs, lib, debug=0) + .. method:: CCompiler.find_library_file(dirs, lib[, debug=0]) Search the specified list of directories for a static or shared library file *lib* and return the full path to that file. If *debug* is true, look for a @@ -306,7 +306,7 @@ ``None`` if *lib* wasn't found in any of the specified directories. - .. method:: CCompiler.has_function(funcname, includes=None, include_dirs=None, libraries=None, library_dirs=None) + .. method:: CCompiler.has_function(funcname [, includes=None, include_dirs=None, libraries=None, library_dirs=None]) Return a boolean indicating whether *funcname* is supported on the current platform. The optional arguments can be used to augment the compilation @@ -361,7 +361,7 @@ The following methods invoke stages in the build process. - .. method:: CCompiler.compile(sources, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None) + .. method:: CCompiler.compile(sources[, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None]) Compile one or more source files. Generates object files (e.g. transforms a :file:`.c` file to a :file:`.o` file.) @@ -405,7 +405,7 @@ Raises :exc:`CompileError` on failure. - .. method:: CCompiler.create_static_lib(objects, output_libname, output_dir=None, debug=0, target_lang=None) + .. method:: CCompiler.create_static_lib(objects, output_libname[, output_dir=None, debug=0, target_lang=None]) Link a bunch of stuff together to create a static library file. The "bunch of stuff" consists of the list of object files supplied as *objects*, the extra @@ -427,7 +427,7 @@ Raises :exc:`LibError` on failure. - .. method:: CCompiler.link(target_desc, objects, output_filename, output_dir=None, libraries=None, library_dirs=None, runtime_library_dirs=None, export_symbols=None, debug=0, extra_preargs=None, extra_postargs=None, build_temp=None, target_lang=None) + .. method:: CCompiler.link(target_desc, objects, output_filename[, output_dir=None, libraries=None, library_dirs=None, runtime_library_dirs=None, export_symbols=None, debug=0, extra_preargs=None, extra_postargs=None, build_temp=None, target_lang=None]) Link a bunch of stuff together to create an executable or shared library file. @@ -469,28 +469,28 @@ Raises :exc:`LinkError` on failure. - .. method:: CCompiler.link_executable(objects, output_progname, output_dir=None, libraries=None, library_dirs=None, runtime_library_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, target_lang=None) + .. method:: CCompiler.link_executable(objects, output_progname[, output_dir=None, libraries=None, library_dirs=None, runtime_library_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, target_lang=None]) Link an executable. *output_progname* is the name of the file executable, while *objects* are a list of object filenames to link in. Other arguments are as for the :meth:`link` method. - .. method:: CCompiler.link_shared_lib(objects, output_libname, output_dir=None, libraries=None, library_dirs=None, runtime_library_dirs=None, export_symbols=None, debug=0, extra_preargs=None, extra_postargs=None, build_temp=None, target_lang=None) + .. method:: CCompiler.link_shared_lib(objects, output_libname[, output_dir=None, libraries=None, library_dirs=None, runtime_library_dirs=None, export_symbols=None, debug=0, extra_preargs=None, extra_postargs=None, build_temp=None, target_lang=None]) Link a shared library. *output_libname* is the name of the output library, while *objects* is a list of object filenames to link in. Other arguments are as for the :meth:`link` method. - .. method:: CCompiler.link_shared_object(objects, output_filename, output_dir=None, libraries=None, library_dirs=None, runtime_library_dirs=None, export_symbols=None, debug=0, extra_preargs=None, extra_postargs=None, build_temp=None, target_lang=None) + .. method:: CCompiler.link_shared_object(objects, output_filename[, output_dir=None, libraries=None, library_dirs=None, runtime_library_dirs=None, export_symbols=None, debug=0, extra_preargs=None, extra_postargs=None, build_temp=None, target_lang=None]) Link a shared object. *output_filename* is the name of the shared object that will be created, while *objects* is a list of object filenames to link in. Other arguments are as for the :meth:`link` method. - .. method:: CCompiler.preprocess(source, output_file=None, macros=None, include_dirs=None, extra_preargs=None, extra_postargs=None) + .. method:: CCompiler.preprocess(source[, output_file=None, macros=None, include_dirs=None, extra_preargs=None, extra_postargs=None]) Preprocess a single C/C++ source file, named in *source*. Output will be written to file named *output_file*, or *stdout* if *output_file* not supplied. @@ -505,14 +505,14 @@ use by the various concrete subclasses. - .. method:: CCompiler.executable_filename(basename, strip_dir=0, output_dir='') + .. method:: CCompiler.executable_filename(basename[, strip_dir=0, output_dir='']) Returns the filename of the executable for the given *basename*. Typically for non-Windows platforms this is the same as the basename, while Windows will get a :file:`.exe` added. - .. method:: CCompiler.library_filename(libname, lib_type='static', strip_dir=0, output_dir='') + .. method:: CCompiler.library_filename(libname[, lib_type='static', strip_dir=0, output_dir='']) Returns the filename for the given library name on the current platform. On Unix a library with *lib_type* of ``'static'`` will typically be of the form @@ -520,18 +520,18 @@ :file:`liblibname.so`. - .. method:: CCompiler.object_filenames(source_filenames, strip_dir=0, output_dir='') + .. method:: CCompiler.object_filenames(source_filenames[, strip_dir=0, output_dir='']) Returns the name of the object files for the given source files. *source_filenames* should be a list of filenames. - .. method:: CCompiler.shared_object_filename(basename, strip_dir=0, output_dir='') + .. method:: CCompiler.shared_object_filename(basename[, strip_dir=0, output_dir='']) Returns the name of a shared object file for the given file name *basename*. - .. method:: CCompiler.execute(func, args, msg=None, level=1) + .. method:: CCompiler.execute(func, args[, msg=None, level=1]) Invokes :func:`packaging.util.execute` This method invokes a Python function *func* with the given arguments *args*, after logging and taking into account @@ -544,7 +544,7 @@ the given command. XXX see also. - .. method:: CCompiler.mkpath(name, mode=511) + .. method:: CCompiler.mkpath(name[, mode=511]) Invokes :func:`packaging.dir_util.mkpath`. This creates a directory and any missing ancestor directories. XXX see also. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/packaging.fancy_getopt.rst --- a/Doc/library/packaging.fancy_getopt.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/packaging.fancy_getopt.rst Mon May 21 23:01:17 2012 -0400 @@ -33,7 +33,7 @@ ``sys.argv[1:]`` if you pass ``None`` as *args*. -.. class:: FancyGetopt(option_table=None) +.. class:: FancyGetopt([option_table=None]) The option_table is a list of 3-tuples: ``(long_option, short_option, help_string)`` @@ -46,7 +46,7 @@ The :class:`FancyGetopt` class provides the following methods: -.. method:: FancyGetopt.getopt(args=None, object=None) +.. method:: FancyGetopt.getopt([args=None, object=None]) Parse command-line options in args. Store as attributes on *object*. @@ -67,7 +67,7 @@ yet. -.. method:: FancyGetopt.generate_help(header=None) +.. method:: FancyGetopt.generate_help([header=None]) Generate help text (a list of strings, one per suggested line of output) from the option table for this :class:`FancyGetopt` object. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/select.rst --- a/Doc/library/select.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/select.rst Mon May 21 23:01:17 2012 -0400 @@ -267,7 +267,7 @@ Remove a registered file descriptor from the epoll object. -.. method:: epoll.poll(timeout=-1, maxevents=-1) +.. method:: epoll.poll([timeout=-1[, maxevents=-1]]) Wait for events. timeout in seconds (float) diff -r 2059910e7d76 -r ac776ef41428 Doc/library/shutil.rst --- a/Doc/library/shutil.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/shutil.rst Mon May 21 23:01:17 2012 -0400 @@ -47,7 +47,7 @@ be copied. -.. function:: copyfile(src, dst, symlinks=False) +.. function:: copyfile(src, dst[, symlinks=False]) Copy the contents (no metadata) of the file named *src* to a file named *dst*. *dst* must be the complete target file name; look at @@ -67,7 +67,7 @@ Added *symlinks* argument. -.. function:: copymode(src, dst, symlinks=False) +.. function:: copymode(src, dst[, symlinks=False]) Copy the permission bits from *src* to *dst*. The file contents, owner, and group are unaffected. *src* and *dst* are path names given as strings. If @@ -78,7 +78,7 @@ .. versionchanged:: 3.3 Added *symlinks* argument. -.. function:: copystat(src, dst, symlinks=False) +.. function:: copystat(src, dst[, symlinks=False]) Copy the permission bits, last access time, last modification time, and flags from *src* to *dst*. The file contents, owner, and group are unaffected. *src* @@ -89,7 +89,7 @@ .. versionchanged:: 3.3 Added *symlinks* argument. -.. function:: copy(src, dst, symlinks=False)) +.. function:: copy(src, dst[, symlinks=False])) Copy the file *src* to the file or directory *dst*. If *dst* is a directory, a file with the same basename as *src* is created (or overwritten) in the @@ -100,7 +100,7 @@ .. versionchanged:: 3.3 Added *symlinks* argument. -.. function:: copy2(src, dst, symlinks=False) +.. function:: copy2(src, dst[, symlinks=False]) Similar to :func:`shutil.copy`, but metadata is copied as well. This is similar to the Unix command :program:`cp -p`. If *symlinks* is true, diff -r 2059910e7d76 -r ac776ef41428 Doc/library/smtpd.rst --- a/Doc/library/smtpd.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/smtpd.rst Mon May 21 23:01:17 2012 -0400 @@ -20,24 +20,17 @@ Additionally the SMTPChannel may be extended to implement very specific interaction behaviour with SMTP clients. -The code supports :RFC:`5321`, plus the :rfc:`1870` SIZE extension. - - SMTPServer Objects ------------------ -.. class:: SMTPServer(localaddr, remoteaddr, data_size_limit=33554432) +.. class:: SMTPServer(localaddr, remoteaddr) Create a new :class:`SMTPServer` object, which binds to local address *localaddr*. It will treat *remoteaddr* as an upstream SMTP relayer. It inherits from :class:`asyncore.dispatcher`, and so will insert itself into :mod:`asyncore`'s event loop on instantiation. - *data_size_limit* specifies the maximum number of bytes that will be - accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no - limit. - .. method:: process_message(peer, mailfrom, rcpttos, data) Raise :exc:`NotImplementedError` exception. Override this in subclasses to @@ -162,15 +155,11 @@ Command Action taken ======== =================================================================== HELO Accepts the greeting from the client and stores it in - :attr:`seen_greeting`. Sets server to base command mode. - EHLO Accepts the greeting from the client and stores it in - :attr:`seen_greeting`. Sets server to extended command mode. + :attr:`seen_greeting`. NOOP Takes no action. QUIT Closes the connection cleanly. MAIL Accepts the "MAIL FROM:" syntax and stores the supplied address as - :attr:`mailfrom`. In extended command mode, accepts the - :rfc:`1870` SIZE attribute and responds appropriately based on the - value of ``data_size_limit``. + :attr:`mailfrom`. RCPT Accepts the "RCPT TO:" syntax and stores the supplied addresses in the :attr:`rcpttos` list. RSET Resets the :attr:`mailfrom`, :attr:`rcpttos`, and @@ -178,7 +167,4 @@ DATA Sets the internal state to :attr:`DATA` and stores remaining lines from the client in :attr:`received_data` until the terminator "\r\n.\r\n" is received. - HELP Returns minimal information on command syntax - VRFY Returns code 252 (the server doesn't know if the address is valid) - EXPN Reports that the command is not implemented. ======== =================================================================== diff -r 2059910e7d76 -r ac776ef41428 Doc/library/sqlite3.rst --- a/Doc/library/sqlite3.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/sqlite3.rst Mon May 21 23:01:17 2012 -0400 @@ -526,7 +526,7 @@ or :const:`None` when no more data is available. -.. method:: Cursor.fetchmany(size=cursor.arraysize) +.. method:: Cursor.fetchmany([size=cursor.arraysize]) Fetches the next set of rows of a query result, returning a list. An empty list is returned when no more rows are available. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/stdtypes.rst --- a/Doc/library/stdtypes.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/stdtypes.rst Mon May 21 23:01:17 2012 -0400 @@ -1353,13 +1353,7 @@ Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless *keepends* is given and - true. This method uses the universal newlines approach to splitting lines. - Unlike :meth:`~str.split`, if the string ends with line boundary characters - the returned list does ``not`` have an empty last element. - - For example, ``'ab c\n\nde fg\rkl\r\n'.splitlines()`` returns - ``['ab c', '', 'de fg', 'kl']``, while the same call with ``splitlines(True)`` - returns ``['ab c\n', '\n, 'de fg\r', 'kl\r\n']``. + true. .. method:: str.startswith(prefix[, start[, end]]) diff -r 2059910e7d76 -r ac776ef41428 Doc/library/sys.rst --- a/Doc/library/sys.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/sys.rst Mon May 21 23:01:17 2012 -0400 @@ -29,32 +29,6 @@ command line, see the :mod:`fileinput` module. -.. data:: base_exec_prefix - - Set during Python startup, before ``site.py`` is run, to the same value as - :data:`exec_prefix`. If not running in a virtual environment, the values - will stay the same; if ``site.py`` finds that a virtual environment is in - use, the values of :data:`prefix` and :data:`exec_prefix` will be changed to - point to the virtual environment, whereas :data:`base_prefix` and - :data:`base_exec_prefix` will remain pointing to the base Python - installation (the one which the virtual environment was created from). - - .. versionadded:: 3.3 - - -.. data:: base_prefix - - Set during Python startup, before ``site.py`` is run, to the same value as - :data:`prefix`. If not running in a virtual environment, the values - will stay the same; if ``site.py`` finds that a virtual environment is in - use, the values of :data:`prefix` and :data:`exec_prefix` will be changed to - point to the virtual environment, whereas :data:`base_prefix` and - :data:`base_exec_prefix` will remain pointing to the base Python - installation (the one which the virtual environment was created from). - - .. versionadded:: 3.3 - - .. data:: byteorder An indicator of the native byte order. This will have the value ``'big'`` on @@ -225,10 +199,6 @@ installed in :file:`{exec_prefix}/lib/python{X.Y}/lib-dynload`, where *X.Y* is the version number of Python, for example ``3.2``. - .. note:: If a virtual environment is in effect, this value will be changed - in ``site.py`` to point to the virtual environment. The value for the - Python installation will still be available, via :data:`base_exec_prefix`. - .. data:: executable @@ -616,47 +586,6 @@ Thus ``2.1.0a3`` is hexversion ``0x020100a3``. - -.. data:: implementation - - An object containing information about the implementation of the - currently running Python interpreter. The following attributes are - required to exist in all Python implementations. - - *name* is the implementation's identifier, e.g. ``'cpython'``. The actual - string is defined by the Python implementation, but it is guaranteed to be - lower case. - - *version* is a named tuple, in the same format as - :data:`sys.version_info`. It represents the version of the Python - *implementation*. This has a distinct meaning from the specific - version of the Python *language* to which the currently running - interpreter conforms, which ``sys.version_info`` represents. For - example, for PyPy 1.8 ``sys.implementation.version`` might be - ``sys.version_info(1, 8, 0, 'final', 0)``, whereas ``sys.version_info`` - would be ``sys.version_info(2, 7, 2, 'final', 0)``. For CPython they - are the same value, since it is the reference implementation. - - *hexversion* is the implementation version in hexadecimal format, like - :data:`sys.hexversion`. - - *cache_tag* is the tag used by the import machinery in the filenames of - cached modules. By convention, it would be a composite of the - implementation's name and version, like ``'cpython-33'``. However, a - Python implementation may use some other value if appropriate. If - ``cache_tag`` is set to ``None``, it indicates that module caching should - be disabled. - - :data:`sys.implementation` may contain additional attributes specific to - the Python implementation. These non-standard attributes must start with - an underscore, and are not described here. Regardless of its contents, - :data:`sys.implementation` will not change during a run of the interpreter, - nor between implementation versions. (It may change between Python - language versions, however.) See `PEP 421` for more information. - - .. versionadded:: 3.3 - - .. data:: int_info A :term:`struct sequence` that holds information about Python's internal @@ -846,10 +775,6 @@ stored in :file:`{prefix}/include/python{X.Y}`, where *X.Y* is the version number of Python, for example ``3.2``. - .. note:: If a virtual environment is in effect, this value will be changed - in ``site.py`` to point to the virtual environment. The value for the - Python installation will still be available, via :data:`base_prefix`. - .. data:: ps1 ps2 diff -r 2059910e7d76 -r ac776ef41428 Doc/library/textwrap.rst --- a/Doc/library/textwrap.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/textwrap.rst Mon May 21 23:01:17 2012 -0400 @@ -107,15 +107,6 @@ expanded to spaces using the :meth:`expandtabs` method of *text*. - .. attribute:: tabsize - - (default: ``8``) If :attr:`expand_tabs` is true, then all tab characters - in *text* will be expanded to zero or more spaces, depending on the - current column and the given tab size. - - .. versionadded:: 3.3 - - .. attribute:: replace_whitespace (default: ``True``) If true, each whitespace character (as defined by diff -r 2059910e7d76 -r ac776ef41428 Doc/library/threading.rst --- a/Doc/library/threading.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/threading.rst Mon May 21 23:01:17 2012 -0400 @@ -426,12 +426,15 @@ Acquire a lock, blocking or non-blocking. - When invoked with the *blocking* argument set to ``True`` (the default), - block until the lock is unlocked, then set it to locked and return ``True``. + When invoked without arguments, block until the lock is unlocked, then set it to + locked, and return true. - When invoked with the *blocking* argument set to ``False``, do not block. - If a call with *blocking* set to ``True`` would block, return ``False`` - immediately; otherwise, set the lock to locked and return ``True``. + When invoked with the *blocking* argument set to true, do the same thing as when + called without arguments, and return true. + + When invoked with the *blocking* argument set to false, do not block. If a call + without an argument would block, return false immediately; otherwise, do the + same thing as when called without arguments, and return true. When invoked with the floating-point *timeout* argument set to a positive value, block for at most the number of seconds specified by *timeout* @@ -996,3 +999,27 @@ Currently, :class:`Lock`, :class:`RLock`, :class:`Condition`, :class:`Semaphore`, and :class:`BoundedSemaphore` objects may be used as :keyword:`with` statement context managers. + + +.. _threaded-imports: + +Importing in threaded code +-------------------------- + +While the import machinery is thread-safe, there are two key restrictions on +threaded imports due to inherent limitations in the way that thread-safety is +provided: + +* Firstly, other than in the main module, an import should not have the + side effect of spawning a new thread and then waiting for that thread in + any way. Failing to abide by this restriction can lead to a deadlock if + the spawned thread directly or indirectly attempts to import a module. +* Secondly, all import attempts must be completed before the interpreter + starts shutting itself down. This can be most easily achieved by only + performing imports from non-daemon threads created through the threading + module. Daemon threads and threads created directly with the thread + module will require some other form of synchronization to ensure they do + not attempt imports after system shutdown has commenced. Failure to + abide by this restriction will lead to intermittent exceptions and + crashes during interpreter shutdown (as the late imports attempt to + access machinery which is no longer in a valid state). diff -r 2059910e7d76 -r ac776ef41428 Doc/library/time.rst --- a/Doc/library/time.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/time.rst Mon May 21 23:01:17 2012 -0400 @@ -62,9 +62,9 @@ the units in which their value or argument is expressed. E.g. on most Unix systems, the clock "ticks" only 50 or 100 times a second. -* On the other hand, the precision of :func:`.time` and :func:`sleep` is better +* On the other hand, the precision of :func:`time` and :func:`sleep` is better than their Unix equivalents: times are expressed as floating point numbers, - :func:`.time` returns the most accurate time available (using Unix + :func:`time` returns the most accurate time available (using Unix :c:func:`gettimeofday` where available), and :func:`sleep` will accept a time with a nonzero fraction (Unix :c:func:`select` is used to implement this, where available). @@ -256,7 +256,7 @@ Convert a time expressed in seconds since the epoch to a string representing local time. If *secs* is not provided or :const:`None`, the current time as - returned by :func:`.time` is used. ``ctime(secs)`` is equivalent to + returned by :func:`time` is used. ``ctime(secs)`` is equivalent to ``asctime(localtime(secs))``. Locale information is not used by :func:`ctime`. @@ -284,7 +284,7 @@ Convert a time expressed in seconds since the epoch to a :class:`struct_time` in UTC in which the dst flag is always zero. If *secs* is not provided or - :const:`None`, the current time as returned by :func:`.time` is used. Fractions + :const:`None`, the current time as returned by :func:`time` is used. Fractions of a second are ignored. See above for a description of the :class:`struct_time` object. See :func:`calendar.timegm` for the inverse of this function. @@ -293,7 +293,7 @@ .. function:: localtime([secs]) Like :func:`gmtime` but converts to local time. If *secs* is not provided or - :const:`None`, the current time as returned by :func:`.time` is used. The dst + :const:`None`, the current time as returned by :func:`time` is used. The dst flag is set to ``1`` when DST applies to the given time. @@ -302,7 +302,7 @@ This is the inverse function of :func:`localtime`. Its argument is the :class:`struct_time` or full 9-tuple (since the dst flag is needed; use ``-1`` as the dst flag if it is unknown) which expresses the time in *local* time, not - UTC. It returns a floating point number, for compatibility with :func:`.time`. + UTC. It returns a floating point number, for compatibility with :func:`time`. If the input value cannot be represented as a valid time, either :exc:`OverflowError` or :exc:`ValueError` will be raised (which depends on whether the invalid value is caught by Python or the underlying C libraries). diff -r 2059910e7d76 -r ac776ef41428 Doc/library/types.rst --- a/Doc/library/types.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/types.rst Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ -:mod:`types` --- Dynamic type creation and names for built-in types -=================================================================== +:mod:`types` --- Names for built-in types +========================================= .. module:: types :synopsis: Names for built-in types. @@ -8,77 +8,20 @@ -------------- -This module defines utility function to assist in dynamic creation of -new types. +This module defines names for some object types that are used by the standard +Python interpreter, but not exposed as builtins like :class:`int` or +:class:`str` are. Also, it does not include some of the types that arise +transparently during processing such as the ``listiterator`` type. -It also defines names for some object types that are used by the standard -Python interpreter, but not exposed as builtins like :class:`int` or -:class:`str` are. +Typical use is for :func:`isinstance` or :func:`issubclass` checks. - -Dynamic Type Creation ---------------------- - -.. function:: new_class(name, bases=(), kwds=None, exec_body=None) - - Creates a class object dynamically using the appropriate metaclass. - - The first three arguments are the components that make up a class - definition header: the class name, the base classes (in order), the - keyword arguments (such as ``metaclass``). - - The *exec_body* argument is a callback that is used to populate the - freshly created class namespace. It should accept the class namespace - as its sole argument and update the namespace directly with the class - contents. If no callback is provided, it has the same effect as passing - in ``lambda ns: ns``. - - .. versionadded:: 3.3 - -.. function:: prepare_class(name, bases=(), kwds=None) - - Calculates the appropriate metaclass and creates the class namespace. - - The arguments are the components that make up a class definition header: - the class name, the base classes (in order) and the keyword arguments - (such as ``metaclass``). - - The return value is a 3-tuple: ``metaclass, namespace, kwds`` - - *metaclass* is the appropriate metaclass, *namespace* is the - prepared class namespace and *kwds* is an updated copy of the passed - in *kwds* argument with any ``'metaclass'`` entry removed. If no *kwds* - argument is passed in, this will be an empty dict. - - .. versionadded:: 3.3 - -.. seealso:: - - :ref:`metaclasses` - Full details of the class creation process supported by these functions - - :pep:`3115` - Metaclasses in Python 3000 - Introduced the ``__prepare__`` namespace hook - - -Standard Interpreter Types --------------------------- - -This module provides names for many of the types that are required to -implement a Python interpreter. It deliberately avoids including some of -the types that arise only incidentally during processing such as the -``listiterator`` type. - -Typical use is of these names is for :func:`isinstance` or -:func:`issubclass` checks. - -Standard names are defined for the following types: +The module defines the following names: .. data:: FunctionType LambdaType - The type of user-defined functions and functions created by - :keyword:`lambda` expressions. + The type of user-defined functions and functions created by :keyword:`lambda` + expressions. .. data:: GeneratorType @@ -194,27 +137,3 @@ Return a new view of the underlying mapping's values. -.. class:: SimpleNamespace - - A simple :class:`object` subclass that provides attribute access to its - namespace, as well as a meaningful repr. - - Unlike :class:`object`, with ``SimpleNamespace`` you can add and remove - attributes. If a ``SimpleNamespace`` object is initialized with keyword - arguments, those are directly added to the underlying namespace. - - The type is roughly equivalent to the following code:: - - class SimpleNamespace: - def __init__(self, **kwargs): - self.__dict__.update(kwargs) - def __repr__(self): - keys = sorted(self.__dict__) - items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys) - return "{}({})".format(type(self).__name__, ", ".join(items)) - - ``SimpleNamespace`` may be useful as a replacement for ``class NS: pass``. - However, for a structured record type use :func:`~collections.namedtuple` - instead. - - .. versionadded:: 3.3 diff -r 2059910e7d76 -r ac776ef41428 Doc/library/venv.rst --- a/Doc/library/venv.rst Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,190 +0,0 @@ -:mod:`venv` --- Creation of virtual environments -================================================ - -.. module:: venv - :synopsis: Creation of virtual environments. -.. moduleauthor:: Vinay Sajip -.. sectionauthor:: Vinay Sajip - - -.. index:: pair: Environments; virtual - -.. versionadded:: 3.3 - -**Source code:** :source:`Lib/venv.py` - --------------- - -The :mod:`venv` module provides support for creating lightweight -"virtual environments" with their own site directories, optionally -isolated from system site directories. Each virtual environment has -its own Python binary (allowing creation of environments with various -Python versions) and can have its own independent set of installed -Python packages in its site directories. - -Creating virtual environments ------------------------------ - -Creation of virtual environments is simplest executing the ``pyvenv`` -script:: - - pyvenv /path/to/new/virtual/environment - -Running this command creates the target directory (creating any parent -directories that don't exist already) and places a ``pyvenv.cfg`` file -in it with a ``home`` key pointing to the Python installation the -command was run from. It also creates a ``bin`` (or ``Scripts`` on -Windows) subdirectory containing a copy of the ``python`` binary (or -binaries, in the case of Windows) and the ``pysetup3`` script (to -facilitate easy installation of packages from PyPI into the new virtualenv). -It also creates an (initially empty) ``lib/pythonX.Y/site-packages`` -subdirectory (on Windows, this is ``Lib\site-packages``). - -.. highlight:: none - -On Windows, you may have to invoke the ``pyvenv`` script as follows, if you -don't have the relevant PATH and PATHEXT settings:: - - c:\Temp>c:\Python33\python c:\Python33\Tools\Scripts\pyvenv.py myenv - -or equivalently:: - - c:\Temp>c:\Python33\python -m venv myenv - -The command, if run with ``-h``, will show the available options:: - - usage: pyvenv [-h] [--system-site-packages] [--symlink] [--clear] - [--upgrade] ENV_DIR [ENV_DIR ...] - - Creates virtual Python environments in one or more target directories. - - positional arguments: - ENV_DIR A directory to create the environment in. - - optional arguments: - -h, --help show this help message and exit - --system-site-packages Give access to the global site-packages dir to the - virtual environment. - --symlink Attempt to symlink rather than copy. - --clear Delete the environment directory if it already exists. - If not specified and the directory exists, an error is - raised. - --upgrade Upgrade the environment directory to use this version - of Python, assuming Python has been upgraded in-place. - -If the target directory already exists an error will be raised, unless -the ``--clear`` or ``--upgrade`` option was provided. - -The created ``pyvenv.cfg`` file also includes the -``include-system-site-packages`` key, set to ``true`` if ``venv`` is -run with the ``--system-site-packages`` option, ``false`` otherwise. - -Multiple paths can be given to ``pyvenv``, in which case an identical -virtualenv will be created, according to the given options, at each -provided path. - - -API ---- - -The high-level method described above makes use of a simple API which provides -mechanisms for third-party virtual environment creators to customize -environment creation according to their needs. - -The :class:`EnvBuilder` class accepts the following keyword arguments on -instantiation: - - * ``system_site_packages`` - A Boolean value indicating that the - system Python site-packages should be available to the - environment (defaults to ``False``). - - * ``clear`` - A Boolean value which, if True, will delete any - existing target directory instead of raising an exception - (defaults to ``False``). - - * ``symlinks`` - A Boolean value indicating whether to attempt - to symlink the Python binary (and any necessary DLLs or other - binaries, e.g. ``pythonw.exe``), rather than copying. Defaults to - ``True`` on Linux and Unix systems, but ``False`` on Windows and - Mac OS X. - -The returned env-builder is an object which has a method, ``create``, -which takes as required argument the path (absolute or relative to the current -directory) of the target directory which is to contain the virtual environment. -The ``create`` method will either create the environment in the specified -directory, or raise an appropriate exception. - -Creators of third-party virtual environment tools will be free to use -the provided ``EnvBuilder`` class as a base class. - -.. highlight:: python - -The ``venv`` module will also provide a module-level function as a -convenience:: - - def create(env_dir, - system_site_packages=False, clear=False, symlinks=False): - builder = EnvBuilder( - system_site_packages=system_site_packages, - clear=clear, - symlinks=symlinks) - builder.create(env_dir) - -The ``create`` method of the ``EnvBuilder`` class illustrates the -hooks available for subclass customization:: - - def create(self, env_dir): - """ - Create a virtualized Python environment in a directory. - env_dir is the target directory to create an environment in. - """ - env_dir = os.path.abspath(env_dir) - context = self.create_directories(env_dir) - self.create_configuration(context) - self.setup_python(context) - self.setup_scripts(context) - self.post_setup(context) - -Each of the methods ``create_directories``, ``create_configuration``, -``setup_python``, ``setup_scripts`` and ``post_setup`` can be -overridden. The functions of these methods are: - - * ``create_directories`` - creates the environment directory and - all necessary directories, and returns a context object. This is - just a holder for attributes (such as paths), for use by the - other methods. - - * ``create_configuration`` - creates the ``pyvenv.cfg`` - configuration file in the environment. - - * ``setup_python`` - creates a copy of the Python executable (and, - under Windows, DLLs) in the environment. - - * ``setup_scripts`` - Installs activation scripts appropriate to the - platform into the virtual environment. - - * ``post_setup`` - A placeholder method which can be overridden - in third party implementations to pre-install packages in the - virtual environment or perform other post-creation steps. - -In addition, ``EnvBuilder`` provides an ``install_scripts`` utility -method that can be called from ``setup_scripts`` or ``post_setup`` in -subclasses to assist in installing custom scripts into the virtual -environment. The method accepts as arguments the context object (see -above) and a path to a directory. The directory should contain -subdirectories "common", "posix", "nt", each containing scripts -destined for the bin directory in the environment. The contents of -"common" and the directory corresponding to ``os.name`` are copied -after some text replacement of placeholders: - -* ``__VENV_DIR__`` is replaced with the absolute path of the - environment directory. - -* ``__VENV_NAME__`` is replaced with the environment name (final path - segment of environment directory). - -* ``__VENV_BIN_NAME__`` is replaced with the name of the bin directory - (either ``bin`` or ``Scripts``). - -* ``__VENV_PYTHON__`` is replaced with the absolute path of the - environment's executable. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/winreg.rst --- a/Doc/library/winreg.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/winreg.rst Mon May 21 23:01:17 2012 -0400 @@ -271,7 +271,7 @@ specified in *file_name* is relative to the remote computer. -.. function:: OpenKey(key, sub_key, reserved=0, access=KEY_READ) +.. function:: OpenKey(key, sub_key, reserved=0, access=KEY_ALL_ACCESS) Opens the specified key, returning a :ref:`handle object `. @@ -280,9 +280,9 @@ *sub_key* is a string that identifies the sub_key to open. - *reserved* is a reserved integer, and must be zero. The default is zero. + *res* is a reserved integer, and must be zero. The default is zero. - *access* is an integer that specifies an access mask that describes the desired + *sam* is an integer that specifies an access mask that describes the desired security access for the key. Default is :const:`KEY_READ`. See :ref:`Access Rights ` for other allowed values. diff -r 2059910e7d76 -r ac776ef41428 Doc/library/xml.etree.elementtree.rst --- a/Doc/library/xml.etree.elementtree.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/library/xml.etree.elementtree.rst Mon May 21 23:01:17 2012 -0400 @@ -476,30 +476,27 @@ .. versionadded:: 3.2 - .. method:: find(match, namespaces=None) + .. method:: find(match) Finds the first subelement matching *match*. *match* may be a tag name or a :ref:`path `. Returns an element instance - or ``None``. *namespaces* is an optional mapping from namespace prefix - to full name. + or ``None``. - .. method:: findall(match, namespaces=None) + .. method:: findall(match) Finds all matching subelements, by tag name or :ref:`path `. Returns a list containing all matching - elements in document order. *namespaces* is an optional mapping from - namespace prefix to full name. + elements in document order. - .. method:: findtext(match, default=None, namespaces=None) + .. method:: findtext(match, default=None) Finds text for the first subelement matching *match*. *match* may be a tag name or a :ref:`path `. Returns the text content of the first matching element, or *default* if no element was found. Note that if the matching element has no text content an empty string - is returned. *namespaces* is an optional mapping from namespace prefix - to full name. + is returned. .. method:: getchildren() @@ -531,13 +528,11 @@ .. versionadded:: 3.2 - .. method:: iterfind(match, namespaces=None) + .. method:: iterfind(match) Finds all matching subelements, by tag name or :ref:`path `. Returns an iterable yielding all - matching elements in document order. *namespaces* is an optional mapping - from namespace prefix to full name. - + matching elements in document order. .. versionadded:: 3.2 @@ -602,17 +597,17 @@ care. *element* is an element instance. - .. method:: find(match, namespaces=None) + .. method:: find(match) Same as :meth:`Element.find`, starting at the root of the tree. - .. method:: findall(match, namespaces=None) + .. method:: findall(match) Same as :meth:`Element.findall`, starting at the root of the tree. - .. method:: findtext(match, default=None, namespaces=None) + .. method:: findtext(match, default=None) Same as :meth:`Element.findtext`, starting at the root of the tree. @@ -635,7 +630,7 @@ to look for (default is to return all elements) - .. method:: iterfind(match, namespaces=None) + .. method:: iterfind(match) Same as :meth:`Element.iterfind`, starting at the root of the tree. @@ -646,8 +641,8 @@ Loads an external XML section into this element tree. *source* is a file name or :term:`file object`. *parser* is an optional parser instance. - If not given, the standard :class:`XMLParser` parser is used. Returns the - section root element. + If not given, the standard XMLParser parser is used. Returns the section + root element. .. method:: write(file, encoding="us-ascii", xml_declaration=None, method="xml") @@ -716,9 +711,9 @@ Generic element structure builder. This builder converts a sequence of start, data, and end method calls to a well-formed element structure. You can use this class to build an element structure using a custom XML parser, - or a parser for some other XML-like format. *element_factory*, when given, - must be a callable accepting two positional arguments: a tag and - a dict of attributes. It is expected to return a new element instance. + or a parser for some other XML-like format. The *element_factory* is called + to create new :class:`Element` instances when given. + .. method:: close() @@ -767,9 +762,9 @@ :class:`Element` structure builder for XML source data, based on the expat parser. *html* are predefined HTML entities. This flag is not supported by the current implementation. *target* is the target object. If omitted, the - builder uses an instance of the standard :class:`TreeBuilder` class. - *encoding* [1]_ is optional. If given, the value overrides the encoding - specified in the XML file. + builder uses an instance of the standard TreeBuilder class. *encoding* [1]_ + is optional. If given, the value overrides the encoding specified in the + XML file. .. method:: close() diff -r 2059910e7d76 -r ac776ef41428 Doc/reference/datamodel.rst --- a/Doc/reference/datamodel.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/reference/datamodel.rst Mon May 21 23:01:17 2012 -0400 @@ -35,19 +35,12 @@ Every object has an identity, a type and a value. An object's *identity* never changes once it has been created; you may think of it as the object's address in memory. The ':keyword:`is`' operator compares the identity of two objects; the -:func:`id` function returns an integer representing its identity. - -.. impl-detail:: - - For CPython, ``id(x)`` is the memory address where ``x`` is stored. - +:func:`id` function returns an integer representing its identity (currently +implemented as its address). An object's :dfn:`type` is also unchangeable. [#]_ An object's type determines the operations that the object supports (e.g., "does it have a length?") and also defines the possible values for objects of that type. The :func:`type` function returns an object's type (which is an object -itself). Like its identity, an object's :dfn:`type` is also unchangeable. -[#]_ - -The *value* of some objects can change. Objects whose value can +itself). The *value* of some objects can change. Objects whose value can change are said to be *mutable*; objects whose value is unchangeable once they are created are called *immutable*. (The value of an immutable container object that contains a reference to a mutable object can change when the latter's value @@ -1262,10 +1255,10 @@ immutable (if the object's hash value changes, it will be in the wrong hash bucket). + User-defined classes have :meth:`__eq__` and :meth:`__hash__` methods by default; with them, all objects compare unequal (except with themselves) - and ``x.__hash__()`` returns an appropriate value such that ``x == y`` - implies both that ``x is y`` and ``hash(x) == hash(y)``. + and ``x.__hash__()`` returns ``id(x)``. Classes which inherit a :meth:`__hash__` method from a parent class but change the meaning of :meth:`__eq__` such that the hash value returned is no @@ -1557,115 +1550,53 @@ Customizing class creation -------------------------- -By default, classes are constructed using :func:`type`. The class body is -executed in a new namespace and the class name is bound locally to the -result of ``type(name, bases, namespace)``. +By default, classes are constructed using :func:`type`. A class definition is +read into a separate namespace and the value of class name is bound to the +result of ``type(name, bases, dict)``. -The class creation process can be customised by passing the ``metaclass`` -keyword argument in the class definition line, or by inheriting from an -existing class that included such an argument. In the following example, -both ``MyClass`` and ``MySubclass`` are instances of ``Meta``:: +When the class definition is read, if a callable ``metaclass`` keyword argument +is passed after the bases in the class definition, the callable given will be +called instead of :func:`type`. If other keyword arguments are passed, they +will also be passed to the metaclass. This allows classes or functions to be +written which monitor or alter the class creation process: - class Meta(type): - pass +* Modifying the class dictionary prior to the class being created. - class MyClass(metaclass=Meta): - pass +* Returning an instance of another class -- essentially performing the role of a + factory function. - class MySubclass(MyClass): - pass +These steps will have to be performed in the metaclass's :meth:`__new__` method +-- :meth:`type.__new__` can then be called from this method to create a class +with different properties. This example adds a new element to the class +dictionary before creating the class:: -Any other keyword arguments that are specified in the class definition are -passed through to all metaclass operations described below. + class metacls(type): + def __new__(mcs, name, bases, dict): + dict['foo'] = 'metacls was here' + return type.__new__(mcs, name, bases, dict) -When a class definition is executed, the following steps occur: +You can of course also override other class methods (or add new methods); for +example defining a custom :meth:`__call__` method in the metaclass allows custom +behavior when the class is called, e.g. not always creating a new instance. -* the appropriate metaclass is determined -* the class namespace is prepared -* the class body is executed -* the class object is created +If the metaclass has a :meth:`__prepare__` attribute (usually implemented as a +class or static method), it is called before the class body is evaluated with +the name of the class and a tuple of its bases for arguments. It should return +an object that supports the mapping interface that will be used to store the +namespace of the class. The default is a plain dictionary. This could be used, +for example, to keep track of the order that class attributes are declared in by +returning an ordered dictionary. -Determining the appropriate metaclass -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +The appropriate metaclass is determined by the following precedence rules: -The appropriate metaclass for a class definition is determined as follows: +* If the ``metaclass`` keyword argument is passed with the bases, it is used. -* if no bases and no explicit metaclass are given, then :func:`type` is used -* if an explicit metaclass is given and it is *not* an instance of - :func:`type`, then it is used directly as the metaclass -* if an instance of :func:`type` is given as the explicit metaclass, or - bases are defined, then the most derived metaclass is used +* Otherwise, if there is at least one base class, its metaclass is used. -The most derived metaclass is selected from the explicitly specified -metaclass (if any) and the metaclasses (i.e. ``type(cls)``) of all specified -base classes. The most derived metaclass is one which is a subtype of *all* -of these candidate metaclasses. If none of the candidate metaclasses meets -that criterion, then the class definition will fail with ``TypeError``. - - -Preparing the class namespace -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -Once the appropriate metaclass has been identified, then the class namespace -is prepared. If the metaclass has a ``__prepare__`` attribute, it is called -as ``namespace = metaclass.__prepare__(name, bases, **kwds)`` (where the -additional keyword arguments, if any, come from the class definition). - -If the metaclass has no ``__prepare__`` attribute, then the class namespace -is initialised as an empty :func:`dict` instance. - -.. seealso:: - - :pep:`3115` - Metaclasses in Python 3000 - Introduced the ``__prepare__`` namespace hook - - -Executing the class body -^^^^^^^^^^^^^^^^^^^^^^^^ - -The class body is executed (approximately) as -``exec(body, globals(), namespace)``. The key difference from a normal -call to :func:`exec` is that lexical scoping allows the class body (including -any methods) to reference names from the current and outer scopes when the -class definition occurs inside a function. - -However, even when the class definition occurs inside the function, methods -defined inside the class still cannot see names defined at the class scope. -Class variables must be accessed through the first parameter of instance or -class methods, and cannot be accessed at all from static methods. - - -Creating the class object -^^^^^^^^^^^^^^^^^^^^^^^^^ - -Once the class namespace has been populated by executing the class body, -the class object is created by calling -``metaclass(name, bases, namespace, **kwds)`` (the additional keywords -passed here are the same as those passed to ``__prepare__``). - -This class object is the one that will be referenced by the zero-argument -form of :func:`super`. ``__class__`` is an implicit closure reference -created by the compiler if any methods in a class body refer to either -``__class__`` or ``super``. This allows the zero argument form of -:func:`super` to correctly identify the class being defined based on -lexical scoping, while the class or instance that was used to make the -current call is identified based on the first argument passed to the method. - -After the class object is created, it is passed to the class decorators -included in the class definition (if any) and the resulting object is bound -in the local namespace as the defined class. - -.. seealso:: - - :pep:`3135` - New super - Describes the implicit ``__class__`` closure reference - - -Metaclass example -^^^^^^^^^^^^^^^^^ +* Otherwise, the default metaclass (:class:`type`) is used. The potential uses for metaclasses are boundless. Some ideas that have been -explored include logging, interface checking, automatic delegation, automatic +explored including logging, interface checking, automatic delegation, automatic property creation, proxies, frameworks, and automatic resource locking/synchronization. @@ -1678,9 +1609,9 @@ def __prepare__(metacls, name, bases, **kwds): return collections.OrderedDict() - def __new__(cls, name, bases, namespace, **kwds): - result = type.__new__(cls, name, bases, dict(namespace)) - result.members = tuple(namespace) + def __new__(cls, name, bases, classdict): + result = type.__new__(cls, name, bases, dict(classdict)) + result.members = tuple(classdict) return result class A(metaclass=OrderedClass): diff -r 2059910e7d76 -r ac776ef41428 Doc/tools/sphinxext/susp-ignored.csv --- a/Doc/tools/sphinxext/susp-ignored.csv Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/tools/sphinxext/susp-ignored.csv Mon May 21 23:01:17 2012 -0400 @@ -30,40 +30,6 @@ howto/curses,,:red,"They are: 0:black, 1:red, 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and" howto/curses,,:white,"7:white." howto/curses,,:yellow,"They are: 0:black, 1:red, 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and" -howto/ipaddress,,:DB8,>>> ipaddress.ip_address('2001:DB8::1') -howto/ipaddress,,::,>>> ipaddress.ip_address('2001:DB8::1') -howto/ipaddress,,:db8,IPv6Address('2001:db8::1') -howto/ipaddress,,::,IPv6Address('2001:db8::1') -howto/ipaddress,,:db8,IPv6Address('2001:db8::1') -howto/ipaddress,,::,IPv6Address('2001:db8::1') -howto/ipaddress,,::,IPv6Address('::1') -howto/ipaddress,,:db8,>>> ipaddress.ip_network('2001:db8::0/96') -howto/ipaddress,,::,>>> ipaddress.ip_network('2001:db8::0/96') -howto/ipaddress,,:db8,IPv6Network('2001:db8::/96') -howto/ipaddress,,::,IPv6Network('2001:db8::/96') -howto/ipaddress,,:db8,IPv6Network('2001:db8::/128') -howto/ipaddress,,::,IPv6Network('2001:db8::/128') -howto/ipaddress,,:db8,>>> ipaddress.ip_network('2001:db8::1/96') -howto/ipaddress,,::,>>> ipaddress.ip_network('2001:db8::1/96') -howto/ipaddress,,:db8,IPv6Interface('2001:db8::1/96') -howto/ipaddress,,::,IPv6Interface('2001:db8::1/96') -howto/ipaddress,,:db8,>>> addr6 = ipaddress.ip_address('2001:db8::1') -howto/ipaddress,,::,>>> addr6 = ipaddress.ip_address('2001:db8::1') -howto/ipaddress,,:db8,>>> host6 = ipaddress.ip_interface('2001:db8::1/96') -howto/ipaddress,,::,>>> host6 = ipaddress.ip_interface('2001:db8::1/96') -howto/ipaddress,,:db8,IPv6Network('2001:db8::/96') -howto/ipaddress,,::,IPv6Network('2001:db8::/96') -howto/ipaddress,,:db8,>>> net6 = ipaddress.ip_network('2001:db8::0/96') -howto/ipaddress,,::,>>> net6 = ipaddress.ip_network('2001:db8::0/96') -howto/ipaddress,,:db8,>>> net6 = ipaddress.ip_network('2001:db8::0/96') -howto/ipaddress,,::,>>> net6 = ipaddress.ip_network('2001:db8::0/96') -howto/ipaddress,,:ffff,IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff::') -howto/ipaddress,,::,IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff::') -howto/ipaddress,,::,IPv6Address('::ffff:ffff') -howto/ipaddress,,:ffff,IPv6Address('::ffff:ffff') -howto/ipaddress,,::,IPv6Address('2001::1') -howto/ipaddress,,::,IPv6Address('2001::ffff:ffff') -howto/ipaddress,,:ffff,IPv6Address('2001::ffff:ffff') howto/logging,,:And,"WARNING:And this, too" howto/logging,,:And,"WARNING:root:And this, too" howto/logging,,:Doing,INFO:root:Doing something diff -r 2059910e7d76 -r ac776ef41428 Doc/tutorial/controlflow.rst --- a/Doc/tutorial/controlflow.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/tutorial/controlflow.rst Mon May 21 23:01:17 2012 -0400 @@ -187,13 +187,6 @@ (Yes, this is the correct code. Look closely: the ``else`` clause belongs to the :keyword:`for` loop, **not** the :keyword:`if` statement.) -When used with a loop, the ``else`` clause has more in common with the -``else`` clause of a :keyword:`try` statement than it does that of -:keyword:`if` statements: a :keyword:`try` statement's ``else`` clause runs -when no exception occurs, and a loop's ``else`` clause runs when no ``break`` -occurs. For more on the :keyword:`try` statement and exceptions, see -:ref:`tut-handling`. - .. _tut-pass: diff -r 2059910e7d76 -r ac776ef41428 Doc/using/index.rst --- a/Doc/using/index.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/using/index.rst Mon May 21 23:01:17 2012 -0400 @@ -17,4 +17,4 @@ unix.rst windows.rst mac.rst - scripts.rst + diff -r 2059910e7d76 -r ac776ef41428 Doc/using/scripts.rst --- a/Doc/using/scripts.rst Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,66 +0,0 @@ -.. _tools-and-scripts: - -Additional Tools and Scripts -============================ - -pyvenv - Creating virtual environments --------------------------------------- - -Creation of virtual environments is done by executing the ``pyvenv`` -script:: - - pyvenv /path/to/new/virtual/environment - -Running this command creates the target directory (creating any parent -directories that don't exist already) and places a ``pyvenv.cfg`` file -in it with a ``home`` key pointing to the Python installation the -command was run from. It also creates a ``bin`` (or ``Scripts`` on -Windows) subdirectory containing a copy of the ``python`` binary (or -binaries, in the case of Windows) and the ``pysetup3`` script (to -facilitate easy installation of packages from PyPI into the new virtualenv). -It also creates an (initially empty) ``lib/pythonX.Y/site-packages`` -subdirectory (on Windows, this is ``Lib\site-packages``). - -.. highlight:: none - -On Windows, you may have to invoke the ``pyvenv`` script as follows, if you -don't have the relevant PATH and PATHEXT settings:: - - c:\Temp>c:\Python33\python c:\Python33\Tools\Scripts\pyvenv.py myenv - -or equivalently:: - - c:\Temp>c:\Python33\python -m venv myenv - -The command, if run with ``-h``, will show the available options:: - - usage: pyvenv [-h] [--system-site-packages] [--symlink] [--clear] - [--upgrade] ENV_DIR [ENV_DIR ...] - - Creates virtual Python environments in one or more target directories. - - positional arguments: - ENV_DIR A directory to create the environment in. - - optional arguments: - -h, --help show this help message and exit - --system-site-packages Give access to the global site-packages dir to the - virtual environment. - --symlink Attempt to symlink rather than copy. - --clear Delete the environment directory if it already exists. - If not specified and the directory exists, an error is - raised. - --upgrade Upgrade the environment directory to use this version - of Python, assuming Python has been upgraded in-place. - -If the target directory already exists an error will be raised, unless -the ``--clear`` or ``--upgrade`` option was provided. - -The created ``pyvenv.cfg`` file also includes the -``include-system-site-packages`` key, set to ``true`` if ``venv`` is -run with the ``--system-site-packages`` option, ``false`` otherwise. - -Multiple paths can be given to ``pyvenv``, in which case an identical -virtualenv will be created, according to the given options, at each -provided path. - diff -r 2059910e7d76 -r ac776ef41428 Doc/whatsnew/3.3.rst --- a/Doc/whatsnew/3.3.rst Sat Jun 09 17:31:59 2012 +0100 +++ b/Doc/whatsnew/3.3.rst Mon May 21 23:01:17 2012 -0400 @@ -48,26 +48,6 @@ This article explains the new features in Python 3.3, compared to 3.2. -.. note:: Alpha users should be aware that this document is currently in - draft form. It will be updated substantially as Python 3.3 moves towards - release, so it's worth checking back even after reading earlier versions. - - -New packaging infrastructure -============================ - -The standard library's packaging infrastructure has been updated to adopt -some of the features developed by the wider community. - -* the :mod:`packaging` package and ``pysetup`` script (inspired by - ``setuptools``, ``distribute``, ``distutil2`` and ``pip``) -* the :mod:`venv` module and ``pyvenv`` script (inspired by ``virtualenv``) - (Note: at time of writing, :pep:`405` is accepted, but not yet implemented) -* native support for package directories that don't require ``__init__.py`` - marker files and can automatically span multiple path segments - (inspired by various third party approaches to namespace packages, as - described in :pep:`420`) - .. pep-3118-update: @@ -555,157 +535,6 @@ in the `Porting Python code`_ section of this document. -New Email Package Features -========================== - -Policy Framework ----------------- - -The email package now has a :mod:`~email.policy` framework. A -:class:`~email.policy.Policy` is an object with several methods and properties -that control how the email package behaves. The primary policy for Python 3.3 -is the :class:`~email.policy.Compat32` policy, which provides backward -compatibility with the email package in Python 3.2. A ``policy`` can be -specified when an email message is parsed by a :mod:`~email.parser`, or when a -:class:`~email.message.Message` object is created, or when an email is -serialized using a :mod:`~email.generator`. Unless overridden, a policy passed -to a ``parser`` is inherited by all the ``Message`` object and sub-objects -created by the ``parser``. By default a ``generator`` will use the policy of -the ``Message`` object it is serializing. The default policy is -:data:`~email.policy.compat32`. - -The minimum set of controls implemented by all ``policy`` objects are: - - =============== ======================================================= - max_line_length The maximum length, excluding the linesep character(s), - individual lines may have when a ``Message`` is - serialized. Defaults to 78. - - linesep The character used to separate individual lines when a - ``Message`` is serialized. Defaults to ``\n``. - - cte_type ``7bit`` or ``8bit``. ``8bit`` applies only to a - ``Bytes`` ``generator``, and means that non-ASCII may - be used where allowed by the protocol (or where it - exists in the original input). - - raise_on_defect Causes a ``parser`` to raise error when defects are - encountered instead of adding them to the ``Message`` - object's ``defects`` list. - =============== ======================================================= - -A new policy instance, with new settings, is created using the -:meth:`~email.policy.Policy.clone` method of policy objects. ``clone`` takes -any of the above controls as keyword arguments. Any control not specified in -the call retains its default value. Thus you can create a policy that uses -``\r\n`` linesep characters like this:: - - mypolicy = compat32.clone(linesep='\r\n') - -Policies can be used to make the generation of messages in the format needed by -your application simpler. Instead of having to remember to specify -``linesep='\r\n'`` in all the places you call a ``generator``, you can specify -it once, when you set the policy used by the ``parser`` or the ``Message``, -whichever your program uses to create ``Message`` objects. On the other hand, -if you need to generate messages in multiple forms, you can still specify the -parameters in the appropriate ``generator`` call. Or you can have custom -policy instances for your different cases, and pass those in when you create -the ``generator``. - - -Provisional Policy with New Header API --------------------------------------- - -While the policy framework is worthwhile all by itself, the main motivation for -introducing it is to allow the creation of new policies that implement new -features for the email package in a way that maintains backward compatibility -for those who do not use the new policies. Because the new policies introduce a -new API, we are releasing them in Python 3.3 as a :term:`provisional policy -`. Backwards incompatible changes (up to and including -removal of the code) may occur if deemed necessary by the core developers. - -The new policies are instances of :class:`~email.policy.EmailPolicy`, -and add the following additional controls: - - =============== ======================================================= - refold_source Controls whether or not headers parsed by a - :mod:`~email.parser` are refolded by the - :mod:`~email.generator`. It can be ``none``, ``long``, - or ``all``. The default is ``long``, which means that - source headers with a line longer than - ``max_line_length`` get refolded. ``none`` means no - line get refolded, and ``all`` means that all lines - get refolded. - - header_factory A callable that take a ``name`` and ``value`` and - produces a custom header object. - =============== ======================================================= - -The ``header_factory`` is the key to the new features provided by the new -policies. When one of the new policies is used, any header retrieved from -a ``Message`` object is an object produced by the ``header_factory``, and any -time you set a header on a ``Message`` it becomes an object produced by -``header_factory``. All such header objects have a ``name`` attribute equal -to the header name. Address and Date headers have additional attributes -that give you access to the parsed data of the header. This means you can now -do things like this:: - - >>> m = Message(policy=SMTP) - >>> m['To'] = 'Éric ' - >>> m['to'] - 'Éric ' - >>> m['to'].addresses - (Address(display_name='Éric', username='foo', domain='example.com'),) - >>> m['to'].addresses[0].username - 'foo' - >>> m['to'].addresses[0].display_name - 'Éric' - >>> m['Date'] = email.utils.localtime() - >>> m['Date'].datetime - datetime.datetime(2012, 5, 25, 21, 39, 24, 465484, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000), 'EDT')) - >>> m['Date'] - 'Fri, 25 May 2012 21:44:27 -0400' - >>> print(m) - To: =?utf-8?q?=C3=89ric?= - Date: Fri, 25 May 2012 21:44:27 -0400 - -You will note that the unicode display name is automatically encoded as -``utf-8`` when the message is serialized, but that when the header is accessed -directly, you get the unicode version. This eliminates any need to deal with -the :mod:`email.header` :meth:`~email.header.decode_header` or -:meth:`~email.header.make_header` functions. - -You can also create addresses from parts:: - - >>> m['cc'] = [Group('pals', [Address('Bob', 'bob', 'example.com'), - ... Address('Sally', 'sally', 'example.com')]), - ... Address('Bonzo', addr_spec='bonz@laugh.com')] - >>> print(m) - To: =?utf-8?q?=C3=89ric?= - Date: Fri, 25 May 2012 21:44:27 -0400 - cc: pals: Bob , Sally ;, Bonzo - -Decoding to unicode is done automatically:: - - >>> m2 = message_from_string(str(m)) - >>> m2['to'] - 'Éric ' - -When you parse a message, you can use the ``addresses`` and ``groups`` -attributes of the header objects to access the groups and individual -addresses:: - - >>> m2['cc'].addresses - (Address(display_name='Bob', username='bob', domain='example.com'), Address(display_name='Sally', username='sally', domain='example.com'), Address(display_name='Bonzo', username='bonz', domain='laugh.com')) - >>> m2['cc'].groups - (Group(display_name='pals', addresses=(Address(display_name='Bob', username='bob', domain='example.com'), Address(display_name='Sally', username='sally', domain='example.com')), Group(display_name=None, addresses=(Address(display_name='Bonzo', username='bonz', domain='laugh.com'),)) - -In summary, if you use one of the new policies, header manipulation works the -way it ought to: your application works with unicode strings, and the email -package transparently encodes and decodes the unicode to and from the RFC -standard Content Transfer Encodings. - - Other Language Changes ====================== @@ -744,23 +573,6 @@ .. XXX mention new error messages for passing wrong number of arguments to functions -A Finer-Grained Import Lock -=========================== - -Previous versions of CPython have always relied on a global import lock. -This led to unexpected annoyances, such as deadlocks when importing a module -would trigger code execution in a different thread as a side-effect. -Clumsy workarounds were sometimes employed, such as the -:c:func:`PyImport_ImportModuleNoBlock` C API function. - -In Python 3.3, importing a module takes a per-module lock. This correctly -serializes importation of a given module from multiple threads (preventing -the exposure of incompletely initialized modules), while eliminating the -aforementioned annoyances. - -(contributed by Antoine Pitrou in :issue:`9260`.) - - New and Improved Modules ======================== @@ -867,21 +679,6 @@ .. XXX addition of __slots__ to ABCs not recorded here: internal detail -contextlib ----------- - -:class:`~collections.ExitStack` now provides a solid foundation for -programmatic manipulation of context managers and similar cleanup -functionality. Unlike the previous ``contextlib.nested`` API (which was -deprecated and removed), the new API is designed to work correctly -regardless of whether context managers acquire their resources in -their ``__init__`` method (for example, file objects) or in their -``__enter__`` method (for example, synchronisation objects from the -:mod:`threading` module). - -(:issue:`13585`) - - crypt ----- @@ -1037,15 +834,6 @@ (Contributed by David Townshend in :issue:`12760`) -ipaddress ---------- - -The new :mod:`ipaddress` module provides tools for creating and manipulating -objects representing IPv4 and IPv6 addresses, networks and interfaces (i.e. -an IP address associated with a specific IP subnet). - -(Contributed by Google and Peter Moody in :pep:`3144`) - lzma ---- @@ -1434,10 +1222,6 @@ (:issue:`14386`) -The new functions `types.new_class` and `types.prepare_class` provide support -for PEP 3115 compliant dynamic type creation. (:issue:`14588`) - - urllib ------ @@ -1476,10 +1260,6 @@ * repeating a single ASCII letter and getting a substring of a ASCII strings is 4 times faster -* UTF-8 and UTF-16 decoding is now 2x to 4x faster. - - (contributed by Serhiy Storchaka, :issue:`14624` and :issue:`14738`.) - Build and C API Changes ======================= @@ -1626,6 +1406,11 @@ .. XXX add a point about hash randomization and that it's always on in 3.3 +* :issue:`14205`: A dict lookup now raises a :exc:`RuntimeError` if the dict is + modified during the lookup. If you implement your own comparison function for + objects used as dict keys and the dict is shared by multiple threads, access + to the dict should be protected by a lock. + * :issue:`12326`: On Linux, sys.platform doesn't contain the major version anymore. It is now always 'linux', instead of 'linux2' or 'linux3' depending on the Linux version used to build Python. Replace sys.platform == 'linux2' diff -r 2059910e7d76 -r ac776ef41428 Include/Python.h --- a/Include/Python.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Include/Python.h Mon May 21 23:01:17 2012 -0400 @@ -101,7 +101,6 @@ #include "warnings.h" #include "weakrefobject.h" #include "structseq.h" -#include "namespaceobject.h" #include "codecs.h" #include "pyerrors.h" diff -r 2059910e7d76 -r ac776ef41428 Include/complexobject.h --- a/Include/complexobject.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Include/complexobject.h Mon May 21 23:01:17 2012 -0400 @@ -63,12 +63,10 @@ /* Format the object based on the format_spec, as defined in PEP 3101 (Advanced String Formatting). */ #ifndef Py_LIMITED_API -PyAPI_FUNC(int) _PyComplex_FormatAdvancedWriter( - _PyUnicodeWriter *writer, - PyObject *obj, - PyObject *format_spec, - Py_ssize_t start, - Py_ssize_t end); +PyAPI_FUNC(PyObject *) _PyComplex_FormatAdvanced(PyObject *obj, + PyObject *format_spec, + Py_ssize_t start, + Py_ssize_t end); #endif #ifdef __cplusplus diff -r 2059910e7d76 -r ac776ef41428 Include/floatobject.h --- a/Include/floatobject.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Include/floatobject.h Mon May 21 23:01:17 2012 -0400 @@ -112,12 +112,10 @@ /* Format the object based on the format_spec, as defined in PEP 3101 (Advanced String Formatting). */ -PyAPI_FUNC(int) _PyFloat_FormatAdvancedWriter( - _PyUnicodeWriter *writer, - PyObject *obj, - PyObject *format_spec, - Py_ssize_t start, - Py_ssize_t end); +PyAPI_FUNC(PyObject *) _PyFloat_FormatAdvanced(PyObject *obj, + PyObject *format_spec, + Py_ssize_t start, + Py_ssize_t end); #endif /* Py_LIMITED_API */ #ifdef __cplusplus diff -r 2059910e7d76 -r ac776ef41428 Include/longobject.h --- a/Include/longobject.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Include/longobject.h Mon May 21 23:01:17 2012 -0400 @@ -151,22 +151,14 @@ /* _PyLong_Format: Convert the long to a string object with given base, appending a base prefix of 0[box] if base is 2, 8 or 16. */ -PyAPI_FUNC(PyObject *) _PyLong_Format(PyObject *obj, int base); - -PyAPI_FUNC(int) _PyLong_FormatWriter( - _PyUnicodeWriter *writer, - PyObject *obj, - int base, - int alternate); +PyAPI_FUNC(PyObject *) _PyLong_Format(PyObject *aa, int base); /* Format the object based on the format_spec, as defined in PEP 3101 (Advanced String Formatting). */ -PyAPI_FUNC(int) _PyLong_FormatAdvancedWriter( - _PyUnicodeWriter *writer, - PyObject *obj, - PyObject *format_spec, - Py_ssize_t start, - Py_ssize_t end); +PyAPI_FUNC(PyObject *) _PyLong_FormatAdvanced(PyObject *obj, + PyObject *format_spec, + Py_ssize_t start, + Py_ssize_t end); #endif /* Py_LIMITED_API */ /* These aren't really part of the long object, but they're handy. The diff -r 2059910e7d76 -r ac776ef41428 Include/memoryobject.h --- a/Include/memoryobject.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Include/memoryobject.h Mon May 21 23:01:17 2012 -0400 @@ -63,7 +63,6 @@ Py_ssize_t exports; /* number of buffer re-exports */ Py_buffer view; /* private copy of the exporter's view */ char format[_Py_MEMORYVIEW_MAX_FORMAT]; /* used for casting */ - PyObject *weakreflist; Py_ssize_t ob_array[1]; /* shape, strides, suboffsets */ } PyMemoryViewObject; #endif diff -r 2059910e7d76 -r ac776ef41428 Include/namespaceobject.h --- a/Include/namespaceobject.h Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ - -/* simple namespace object interface */ - -#ifndef NAMESPACEOBJECT_H -#define NAMESPACEOBJECT_H -#ifdef __cplusplus -extern "C" { -#endif - -PyAPI_DATA(PyTypeObject) _PyNamespace_Type; - -PyAPI_FUNC(PyObject *) _PyNamespace_New(PyObject *kwds); - -#ifdef __cplusplus -} -#endif -#endif /* !NAMESPACEOBJECT_H */ diff -r 2059910e7d76 -r ac776ef41428 Include/patchlevel.h --- a/Include/patchlevel.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Include/patchlevel.h Mon May 21 23:01:17 2012 -0400 @@ -20,10 +20,10 @@ #define PY_MINOR_VERSION 3 #define PY_MICRO_VERSION 0 #define PY_RELEASE_LEVEL PY_RELEASE_LEVEL_ALPHA -#define PY_RELEASE_SERIAL 4 +#define PY_RELEASE_SERIAL 3 /* Version as a string */ -#define PY_VERSION "3.3.0a4+" +#define PY_VERSION "3.3.0a3+" /*--end constants--*/ /* Version as a single 4-byte hex number, e.g. 0x010502B2 == 1.5.2b2. diff -r 2059910e7d76 -r ac776ef41428 Include/pyerrors.h --- a/Include/pyerrors.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Include/pyerrors.h Mon May 21 23:01:17 2012 -0400 @@ -87,7 +87,7 @@ PyAPI_FUNC(void) PyErr_SetExcInfo(PyObject *, PyObject *, PyObject *); #if defined(__clang__) || \ - (defined(__GNUC_MAJOR__) && \ + (defined(__GNUC__) && \ ((__GNUC_MAJOR__ >= 3) || \ (__GNUC_MAJOR__ == 2) && (__GNUC_MINOR__ >= 5))) #define _Py_NO_RETURN __attribute__((__noreturn__)) diff -r 2059910e7d76 -r ac776ef41428 Include/pyexpat.h --- a/Include/pyexpat.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Include/pyexpat.h Mon May 21 23:01:17 2012 -0400 @@ -43,8 +43,6 @@ XML_Parser parser, XML_UnknownEncodingHandler handler, void *encodingHandlerData); void (*SetUserData)(XML_Parser parser, void *userData); - void (*SetStartDoctypeDeclHandler)(XML_Parser parser, - XML_StartDoctypeDeclHandler start); /* always add new stuff to the end! */ }; diff -r 2059910e7d76 -r ac776ef41428 Include/pythonrun.h --- a/Include/pythonrun.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Include/pythonrun.h Mon May 21 23:01:17 2012 -0400 @@ -82,12 +82,9 @@ PyParser_SimpleParseFileFlags(FP, S, B, 0) #endif PyAPI_FUNC(struct _node *) PyParser_SimpleParseStringFlags(const char *, int, - int); -PyAPI_FUNC(struct _node *) PyParser_SimpleParseStringFlagsFilename(const char *, - const char *, - int, int); + int); PyAPI_FUNC(struct _node *) PyParser_SimpleParseFileFlags(FILE *, const char *, - int, int); + int, int); #ifndef Py_LIMITED_API PyAPI_FUNC(PyObject *) PyRun_StringFlags(const char *, int, PyObject *, diff -r 2059910e7d76 -r ac776ef41428 Include/structmember.h --- a/Include/structmember.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Include/structmember.h Mon May 21 23:01:17 2012 -0400 @@ -16,41 +16,42 @@ pointer is NULL. */ typedef struct PyMemberDef { - char *name; - int type; - Py_ssize_t offset; - int flags; - char *doc; + /* Current version, use this */ + char *name; + int type; + Py_ssize_t offset; + int flags; + char *doc; } PyMemberDef; /* Types */ -#define T_SHORT 0 -#define T_INT 1 -#define T_LONG 2 -#define T_FLOAT 3 -#define T_DOUBLE 4 -#define T_STRING 5 -#define T_OBJECT 6 +#define T_SHORT 0 +#define T_INT 1 +#define T_LONG 2 +#define T_FLOAT 3 +#define T_DOUBLE 4 +#define T_STRING 5 +#define T_OBJECT 6 /* XXX the ordering here is weird for binary compatibility */ -#define T_CHAR 7 /* 1-character string */ -#define T_BYTE 8 /* 8-bit signed int */ +#define T_CHAR 7 /* 1-character string */ +#define T_BYTE 8 /* 8-bit signed int */ /* unsigned variants: */ -#define T_UBYTE 9 -#define T_USHORT 10 -#define T_UINT 11 -#define T_ULONG 12 +#define T_UBYTE 9 +#define T_USHORT 10 +#define T_UINT 11 +#define T_ULONG 12 /* Added by Jack: strings contained in the structure */ -#define T_STRING_INPLACE 13 +#define T_STRING_INPLACE 13 /* Added by Lillo: bools contained in the structure (assumed char) */ -#define T_BOOL 14 +#define T_BOOL 14 -#define T_OBJECT_EX 16 /* Like T_OBJECT, but raises AttributeError - when the value is NULL, instead of - converting to None. */ +#define T_OBJECT_EX 16 /* Like T_OBJECT, but raises AttributeError + when the value is NULL, instead of + converting to None. */ #ifdef HAVE_LONG_LONG -#define T_LONGLONG 17 +#define T_LONGLONG 17 #define T_ULONGLONG 18 #endif /* HAVE_LONG_LONG */ @@ -59,10 +60,10 @@ /* Flags */ -#define READONLY 1 -#define READ_RESTRICTED 2 +#define READONLY 1 +#define READ_RESTRICTED 2 #define PY_WRITE_RESTRICTED 4 -#define RESTRICTED (READ_RESTRICTED | PY_WRITE_RESTRICTED) +#define RESTRICTED (READ_RESTRICTED | PY_WRITE_RESTRICTED) /* Current API, use this */ diff -r 2059910e7d76 -r ac776ef41428 Include/unicodeobject.h --- a/Include/unicodeobject.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Include/unicodeobject.h Mon May 21 23:01:17 2012 -0400 @@ -648,20 +648,8 @@ Py_ssize_t from_start, Py_ssize_t how_many ); - -/* Unsafe version of PyUnicode_CopyCharacters(): don't check arguments and so - may crash if parameters are invalid (e.g. if the output string - is too short). */ -PyAPI_FUNC(void) _PyUnicode_FastCopyCharacters( - PyObject *to, - Py_ssize_t to_start, - PyObject *from, - Py_ssize_t from_start, - Py_ssize_t how_many - ); #endif -#ifndef Py_LIMITED_API /* Fill a string with a character: write fill_char into unicode[start:start+length]. @@ -670,21 +658,13 @@ Return the number of written character, or return -1 and raise an exception on error. */ +#ifndef Py_LIMITED_API PyAPI_FUNC(Py_ssize_t) PyUnicode_Fill( PyObject *unicode, Py_ssize_t start, Py_ssize_t length, Py_UCS4 fill_char ); - -/* Unsafe version of PyUnicode_Fill(): don't check arguments and so may crash - if parameters are invalid (e.g. if length is longer than the string). */ -PyAPI_FUNC(void) _PyUnicode_FastFill( - PyObject *unicode, - Py_ssize_t start, - Py_ssize_t length, - Py_UCS4 fill_char - ); #endif /* Create a Unicode Object from the Py_UNICODE buffer u of the given @@ -716,19 +696,13 @@ const char *u /* UTF-8 encoded string */ ); -#ifndef Py_LIMITED_API /* Create a new string from a buffer of Py_UCS1, Py_UCS2 or Py_UCS4 characters. Scan the string to find the maximum character. */ +#ifndef Py_LIMITED_API PyAPI_FUNC(PyObject*) PyUnicode_FromKindAndData( int kind, const void *buffer, Py_ssize_t size); - -/* Create a new string from a buffer of ASCII characters. - WARNING: Don't check if the string contains any non-ASCII character. */ -PyAPI_FUNC(PyObject*) _PyUnicode_FromASCII( - const char *buffer, - Py_ssize_t size); #endif PyAPI_FUNC(PyObject*) PyUnicode_Substring( @@ -891,67 +865,12 @@ ); #ifndef Py_LIMITED_API -typedef struct { - PyObject *buffer; - void *data; - enum PyUnicode_Kind kind; - Py_UCS4 maxchar; - Py_ssize_t size; - Py_ssize_t pos; - /* minimum length of the buffer when overallocation is enabled, - see _PyUnicodeWriter_Init() */ - Py_ssize_t min_length; - unsigned char overallocate; - /* If readonly is 1, buffer is a shared string (cannot be modified) - and size is set to 0. */ - unsigned char readonly; -} _PyUnicodeWriter ; - -/* Initialize a Unicode writer. - - If min_length is greater than zero, _PyUnicodeWriter_Prepare() - overallocates the buffer and min_length is the minimum length in characters - of the buffer. */ -PyAPI_FUNC(void) -_PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length); - -/* Prepare the buffer to write 'length' characters - with the specified maximum character. - - Return 0 on success, raise an exception and return -1 on error. */ -#define _PyUnicodeWriter_Prepare(WRITER, LENGTH, MAXCHAR) \ - (((MAXCHAR) <= (WRITER)->maxchar \ - && (LENGTH) <= (WRITER)->size - (WRITER)->pos) \ - ? 0 \ - : (((LENGTH) == 0) \ - ? 0 \ - : _PyUnicodeWriter_PrepareInternal((WRITER), (LENGTH), (MAXCHAR)))) - -/* Don't call this function directly, use the _PyUnicodeWriter_Prepare() macro - instead. */ -PyAPI_FUNC(int) -_PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, - Py_ssize_t length, Py_UCS4 maxchar); - -PyAPI_FUNC(int) -_PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str); - -PyAPI_FUNC(PyObject *) -_PyUnicodeWriter_Finish(_PyUnicodeWriter *writer); - -PyAPI_FUNC(void) -_PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer); -#endif - -#ifndef Py_LIMITED_API /* Format the object based on the format_spec, as defined in PEP 3101 (Advanced String Formatting). */ -PyAPI_FUNC(int) _PyUnicode_FormatAdvancedWriter( - _PyUnicodeWriter *writer, - PyObject *obj, - PyObject *format_spec, - Py_ssize_t start, - Py_ssize_t end); +PyAPI_FUNC(PyObject *) _PyUnicode_FormatAdvanced(PyObject *obj, + PyObject *format_spec, + Py_ssize_t start, + Py_ssize_t end); #endif PyAPI_FUNC(void) PyUnicode_InternInPlace(PyObject **); diff -r 2059910e7d76 -r ac776ef41428 Lib/__future__.py --- a/Lib/__future__.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/__future__.py Mon May 21 23:01:17 2012 -0400 @@ -114,7 +114,7 @@ CO_FUTURE_DIVISION) absolute_import = _Feature((2, 5, 0, "alpha", 1), - (3, 0, 0, "alpha", 0), + (2, 7, 0, "alpha", 0), CO_FUTURE_ABSOLUTE_IMPORT) with_statement = _Feature((2, 5, 0, "alpha", 1), diff -r 2059910e7d76 -r ac776ef41428 Lib/_pyio.py --- a/Lib/_pyio.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/_pyio.py Mon May 21 23:01:17 2012 -0400 @@ -1448,7 +1448,7 @@ r"""Character and line based layer over a BufferedIOBase object, buffer. encoding gives the name of the encoding that the stream will be - decoded or encoded with. It defaults to locale.getpreferredencoding(False). + decoded or encoded with. It defaults to locale.getpreferredencoding. errors determines the strictness of encoding and decoding (see the codecs.register) and defaults to "strict". @@ -1487,7 +1487,7 @@ # Importing locale may fail if Python is being built encoding = "ascii" else: - encoding = locale.getpreferredencoding(False) + encoding = locale.getpreferredencoding() if not isinstance(encoding, str): raise ValueError("invalid encoding: %r" % encoding) diff -r 2059910e7d76 -r ac776ef41428 Lib/bz2.py --- a/Lib/bz2.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/bz2.py Mon May 21 23:01:17 2012 -0400 @@ -4,12 +4,11 @@ (de)compression, and functions for one-shot (de)compression. """ -__all__ = ["BZ2File", "BZ2Compressor", "BZ2Decompressor", - "open", "compress", "decompress"] +__all__ = ["BZ2File", "BZ2Compressor", "BZ2Decompressor", "compress", + "decompress"] __author__ = "Nadeem Vawda " -import builtins import io import warnings @@ -40,15 +39,16 @@ returned as bytes, and data to be written should be given as bytes. """ - def __init__(self, filename, mode="r", buffering=None, compresslevel=9): + def __init__(self, filename=None, mode="r", buffering=None, + compresslevel=9, *, fileobj=None): """Open a bzip2-compressed file. - If filename is a str or bytes object, is gives the name of the file to - be opened. Otherwise, it should be a file object, which will be used to - read or write the compressed data. + If filename is given, open the named file. Otherwise, operate on + the file object given by fileobj. Exactly one of these two + parameters should be provided. - mode can be 'r' for reading (default), 'w' for (over)writing, or 'a' for - appending. These can equivalently be given as 'rb', 'wb', and 'ab'. + mode can be 'r' for reading (default), 'w' for (over)writing, or + 'a' for appending. buffering is ignored. Its use is deprecated. @@ -91,15 +91,15 @@ else: raise ValueError("Invalid mode: {!r}".format(mode)) - if isinstance(filename, (str, bytes)): - self._fp = builtins.open(filename, mode) + if filename is not None and fileobj is None: + self._fp = open(filename, mode) self._closefp = True self._mode = mode_code - elif hasattr(filename, "read") or hasattr(filename, "write"): - self._fp = filename + elif fileobj is not None and filename is None: + self._fp = fileobj self._mode = mode_code else: - raise TypeError("filename must be a str or bytes object, or a file") + raise ValueError("Must give exactly one of filename and fileobj") def close(self): """Flush and close the file. @@ -392,46 +392,6 @@ return self._pos -def open(filename, mode="rb", compresslevel=9, - encoding=None, errors=None, newline=None): - """Open a bzip2-compressed file in binary or text mode. - - The filename argument can be an actual filename (a str or bytes object), or - an existing file object to read from or write to. - - The mode argument can be "r", "rb", "w", "wb", "a" or "ab" for binary mode, - or "rt", "wt" or "at" for text mode. The default mode is "rb", and the - default compresslevel is 9. - - For binary mode, this function is equivalent to the BZ2File constructor: - BZ2File(filename, mode, compresslevel). In this case, the encoding, errors - and newline arguments must not be provided. - - For text mode, a BZ2File object is created, and wrapped in an - io.TextIOWrapper instance with the specified encoding, error handling - behavior, and line ending(s). - - """ - if "t" in mode: - if "b" in mode: - raise ValueError("Invalid mode: %r" % (mode,)) - else: - if encoding is not None: - raise ValueError("Argument 'encoding' not supported in binary mode") - if errors is not None: - raise ValueError("Argument 'errors' not supported in binary mode") - if newline is not None: - raise ValueError("Argument 'newline' not supported in binary mode") - - bz_mode = mode.replace("t", "") - binary_file = BZ2File(filename, bz_mode, compresslevel=compresslevel) - - if "t" in mode: - return io.TextIOWrapper(binary_file, encoding, errors, newline) - else: - return binary_file - - def compress(data, compresslevel=9): """Compress a block of data. diff -r 2059910e7d76 -r ac776ef41428 Lib/contextlib.py --- a/Lib/contextlib.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/contextlib.py Mon May 21 23:01:17 2012 -0400 @@ -1,10 +1,9 @@ """Utilities for with-statement contexts. See PEP 343.""" import sys -from collections import deque from functools import wraps -__all__ = ["contextmanager", "closing", "ContextDecorator", "ExitStack"] +__all__ = ["contextmanager", "closing", "ContextDecorator"] class ContextDecorator(object): @@ -13,12 +12,12 @@ def _recreate_cm(self): """Return a recreated instance of self. - Allows an otherwise one-shot context manager like + Allows otherwise one-shot context managers like _GeneratorContextManager to support use as - a decorator via implicit recreation. + decorators via implicit recreation. - This is a private interface just for _GeneratorContextManager. - See issue #11647 for details. + Note: this is a private interface just for _GCM in 3.2 but will be + renamed and documented for third party use in 3.3 """ return self @@ -139,117 +138,3 @@ return self.thing def __exit__(self, *exc_info): self.thing.close() - - -# Inspired by discussions on http://bugs.python.org/issue13585 -class ExitStack(object): - """Context manager for dynamic management of a stack of exit callbacks - - For example: - - with ExitStack() as stack: - files = [stack.enter_context(open(fname)) for fname in filenames] - # All opened files will automatically be closed at the end of - # the with statement, even if attempts to open files later - # in the list throw an exception - - """ - def __init__(self): - self._exit_callbacks = deque() - - def pop_all(self): - """Preserve the context stack by transferring it to a new instance""" - new_stack = type(self)() - new_stack._exit_callbacks = self._exit_callbacks - self._exit_callbacks = deque() - return new_stack - - def _push_cm_exit(self, cm, cm_exit): - """Helper to correctly register callbacks to __exit__ methods""" - def _exit_wrapper(*exc_details): - return cm_exit(cm, *exc_details) - _exit_wrapper.__self__ = cm - self.push(_exit_wrapper) - - def push(self, exit): - """Registers a callback with the standard __exit__ method signature - - Can suppress exceptions the same way __exit__ methods can. - - Also accepts any object with an __exit__ method (registering a call - to the method instead of the object itself) - """ - # We use an unbound method rather than a bound method to follow - # the standard lookup behaviour for special methods - _cb_type = type(exit) - try: - exit_method = _cb_type.__exit__ - except AttributeError: - # Not a context manager, so assume its a callable - self._exit_callbacks.append(exit) - else: - self._push_cm_exit(exit, exit_method) - return exit # Allow use as a decorator - - def callback(self, callback, *args, **kwds): - """Registers an arbitrary callback and arguments. - - Cannot suppress exceptions. - """ - def _exit_wrapper(exc_type, exc, tb): - callback(*args, **kwds) - # We changed the signature, so using @wraps is not appropriate, but - # setting __wrapped__ may still help with introspection - _exit_wrapper.__wrapped__ = callback - self.push(_exit_wrapper) - return callback # Allow use as a decorator - - def enter_context(self, cm): - """Enters the supplied context manager - - If successful, also pushes its __exit__ method as a callback and - returns the result of the __enter__ method. - """ - # We look up the special methods on the type to match the with statement - _cm_type = type(cm) - _exit = _cm_type.__exit__ - result = _cm_type.__enter__(cm) - self._push_cm_exit(cm, _exit) - return result - - def close(self): - """Immediately unwind the context stack""" - self.__exit__(None, None, None) - - def __enter__(self): - return self - - def __exit__(self, *exc_details): - # We manipulate the exception state so it behaves as though - # we were actually nesting multiple with statements - frame_exc = sys.exc_info()[1] - def _fix_exception_context(new_exc, old_exc): - while 1: - exc_context = new_exc.__context__ - if exc_context in (None, frame_exc): - break - new_exc = exc_context - new_exc.__context__ = old_exc - - # Callbacks are invoked in LIFO order to match the behaviour of - # nested context managers - suppressed_exc = False - while self._exit_callbacks: - cb = self._exit_callbacks.pop() - try: - if cb(*exc_details): - suppressed_exc = True - exc_details = (None, None, None) - except: - new_exc_details = sys.exc_info() - # simulate the stack of exceptions by setting the context - _fix_exception_context(new_exc_details[1], exc_details[1]) - if not self._exit_callbacks: - raise - exc_details = new_exc_details - return suppressed_exc diff -r 2059910e7d76 -r ac776ef41428 Lib/ctypes/test/test_numbers.py --- a/Lib/ctypes/test/test_numbers.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/ctypes/test/test_numbers.py Mon May 21 23:01:17 2012 -0400 @@ -217,16 +217,6 @@ # probably be changed: self.assertRaises(TypeError, c_int, c_long(42)) - def test_float_overflow(self): - import sys - big_int = int(sys.float_info.max) * 2 - for t in float_types + [c_longdouble]: - self.assertRaises(OverflowError, t, big_int) - if (hasattr(t, "__ctype_be__")): - self.assertRaises(OverflowError, t.__ctype_be__, big_int) - if (hasattr(t, "__ctype_le__")): - self.assertRaises(OverflowError, t.__ctype_le__, big_int) - ## def test_perf(self): ## check_perf() diff -r 2059910e7d76 -r ac776ef41428 Lib/datetime.py --- a/Lib/datetime.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/datetime.py Mon May 21 23:01:17 2012 -0400 @@ -1434,15 +1434,6 @@ self.hour, self.minute, self.second, dst) - def timestamp(self): - "Return POSIX timestamp as float" - if self._tzinfo is None: - return _time.mktime((self.year, self.month, self.day, - self.hour, self.minute, self.second, - -1, -1, -1)) + self.microsecond / 1e6 - else: - return (self - _EPOCH).total_seconds() - def utctimetuple(self): "Return UTC time tuple compatible with time.gmtime()." offset = self.utcoffset() @@ -1898,7 +1889,7 @@ timezone.utc = timezone._create(timedelta(0)) timezone.min = timezone._create(timezone._minoffset) timezone.max = timezone._create(timezone._maxoffset) -_EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc) + """ Some time zone algebra. For a datetime x, let x.n = x stripped of its timezone -- its naive time. diff -r 2059910e7d76 -r ac776ef41428 Lib/dis.py --- a/Lib/dis.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/dis.py Mon May 21 23:01:17 2012 -0400 @@ -190,9 +190,6 @@ if free is None: free = co.co_cellvars + co.co_freevars print('(' + free[oparg] + ')', end=' ') - elif op in hasnargs: - print('(%d positional, %d keyword pair)' - % (code[i-2], code[i-1]), end=' ') print() def _disassemble_bytes(code, lasti=-1, varnames=None, names=None, @@ -232,9 +229,6 @@ print('(%d)' % oparg, end=' ') elif op in hascompare: print('(' + cmp_op[oparg] + ')', end=' ') - elif op in hasnargs: - print('(%d positional, %d keyword pair)' - % (code[i-2], code[i-1]), end=' ') print() def _disassemble_str(source): diff -r 2059910e7d76 -r ac776ef41428 Lib/distutils/__init__.py --- a/Lib/distutils/__init__.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/distutils/__init__.py Mon May 21 23:01:17 2012 -0400 @@ -13,5 +13,5 @@ # Updated automatically by the Python release process. # #--start constants-- -__version__ = "3.3.0a4" +__version__ = "3.3.0a3" #--end constants-- diff -r 2059910e7d76 -r ac776ef41428 Lib/distutils/command/bdist_rpm.py --- a/Lib/distutils/command/bdist_rpm.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/distutils/command/bdist_rpm.py Mon May 21 23:01:17 2012 -0400 @@ -190,7 +190,7 @@ if self.fix_python: self.python = sys.executable else: - self.python = "python3" + self.python = "python" elif self.fix_python: raise DistutilsOptionError( "--python and --fix-python are mutually exclusive options") @@ -320,7 +320,6 @@ rpm_cmd.append('-bb') else: rpm_cmd.append('-ba') - rpm_cmd.extend(['--define', '__python %s' % self.python]) if self.rpm3_mode: rpm_cmd.extend(['--define', '_topdir %s' % os.path.abspath(self.rpm_base)]) diff -r 2059910e7d76 -r ac776ef41428 Lib/distutils/sysconfig.py --- a/Lib/distutils/sysconfig.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/distutils/sysconfig.py Mon May 21 23:01:17 2012 -0400 @@ -18,8 +18,6 @@ # These are needed in a couple of spots, so just compute them once. PREFIX = os.path.normpath(sys.prefix) EXEC_PREFIX = os.path.normpath(sys.exec_prefix) -BASE_PREFIX = os.path.normpath(sys.base_prefix) -BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix) # Path to the base directory of the project. On Windows the binary may # live in project/PCBuild9. If we're dealing with an x64 Windows build, @@ -41,21 +39,11 @@ # different (hard-wired) directories. # Setup.local is available for Makefile builds including VPATH builds, # Setup.dist is available on Windows -def _is_python_source_dir(d): +def _python_build(): for fn in ("Setup.dist", "Setup.local"): - if os.path.isfile(os.path.join(d, "Modules", fn)): + if os.path.isfile(os.path.join(project_base, "Modules", fn)): return True return False -_sys_home = getattr(sys, '_home', None) -if _sys_home and os.name == 'nt' and \ - _sys_home.lower().endswith(('pcbuild', 'pcbuild\\amd64')): - _sys_home = os.path.dirname(_sys_home) - if _sys_home.endswith('pcbuild'): # must be amd64 - _sys_home = os.path.dirname(_sys_home) -def _python_build(): - if _sys_home: - return _is_python_source_dir(_sys_home) - return _is_python_source_dir(project_base) python_build = _python_build() # Calculate the build qualifier flags if they are defined. Adding the flags @@ -86,11 +74,11 @@ otherwise, this is the path to platform-specific header files (namely pyconfig.h). - If 'prefix' is supplied, use it instead of sys.base_prefix or - sys.base_exec_prefix -- i.e., ignore 'plat_specific'. + If 'prefix' is supplied, use it instead of sys.prefix or + sys.exec_prefix -- i.e., ignore 'plat_specific'. """ if prefix is None: - prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX + prefix = plat_specific and EXEC_PREFIX or PREFIX if os.name == "posix": if python_build: # Assume the executable is in the build directory. The @@ -98,12 +86,11 @@ # the build directory may not be the source directory, we # must use "srcdir" from the makefile to find the "Include" # directory. - base = _sys_home or os.path.dirname(os.path.abspath(sys.executable)) + base = os.path.dirname(os.path.abspath(sys.executable)) if plat_specific: return base else: - incdir = os.path.join(_sys_home or get_config_var('srcdir'), - 'Include') + incdir = os.path.join(get_config_var('srcdir'), 'Include') return os.path.normpath(incdir) python_dir = 'python' + get_python_version() + build_flags return os.path.join(prefix, "include", python_dir) @@ -128,14 +115,11 @@ containing standard Python library modules; otherwise, return the directory for site-specific modules. - If 'prefix' is supplied, use it instead of sys.base_prefix or - sys.base_exec_prefix -- i.e., ignore 'plat_specific'. + If 'prefix' is supplied, use it instead of sys.prefix or + sys.exec_prefix -- i.e., ignore 'plat_specific'. """ if prefix is None: - if standard_lib: - prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX - else: - prefix = plat_specific and EXEC_PREFIX or PREFIX + prefix = plat_specific and EXEC_PREFIX or PREFIX if os.name == "posix": libpython = os.path.join(prefix, @@ -248,9 +232,9 @@ """Return full pathname of installed pyconfig.h file.""" if python_build: if os.name == "nt": - inc_dir = os.path.join(_sys_home or project_base, "PC") + inc_dir = os.path.join(project_base, "PC") else: - inc_dir = _sys_home or project_base + inc_dir = project_base else: inc_dir = get_python_inc(plat_specific=1) if get_python_version() < '2.2': @@ -264,8 +248,7 @@ def get_makefile_filename(): """Return full pathname of installed Makefile from the Python build.""" if python_build: - return os.path.join(_sys_home or os.path.dirname(sys.executable), - "Makefile") + return os.path.join(os.path.dirname(sys.executable), "Makefile") lib_dir = get_python_lib(plat_specific=0, standard_lib=1) config_file = 'config-{}{}'.format(get_python_version(), build_flags) return os.path.join(lib_dir, config_file, 'Makefile') diff -r 2059910e7d76 -r ac776ef41428 Lib/email/_encoded_words.py --- a/Lib/email/_encoded_words.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,221 +0,0 @@ -""" Routines for manipulating RFC2047 encoded words. - -This is currently a package-private API, but will be considered for promotion -to a public API if there is demand. - -""" - -# An ecoded word looks like this: -# -# =?charset[*lang]?cte?encoded_string?= -# -# for more information about charset see the charset module. Here it is one -# of the preferred MIME charset names (hopefully; you never know when parsing). -# cte (Content Transfer Encoding) is either 'q' or 'b' (ignoring case). In -# theory other letters could be used for other encodings, but in practice this -# (almost?) never happens. There could be a public API for adding entries -# to to the CTE tables, but YAGNI for now. 'q' is Quoted Printable, 'b' is -# Base64. The meaning of encoded_string should be obvious. 'lang' is optional -# as indicated by the brackets (they are not part of the syntax) but is almost -# never encountered in practice. -# -# The general interface for a CTE decoder is that it takes the encoded_string -# as its argument, and returns a tuple (cte_decoded_string, defects). The -# cte_decoded_string is the original binary that was encoded using the -# specified cte. 'defects' is a list of MessageDefect instances indicating any -# problems encountered during conversion. 'charset' and 'lang' are the -# corresponding strings extracted from the EW, case preserved. -# -# The general interface for a CTE encoder is that it takes a binary sequence -# as input and returns the cte_encoded_string, which is an ascii-only string. -# -# Each decoder must also supply a length function that takes the binary -# sequence as its argument and returns the length of the resulting encoded -# string. -# -# The main API functions for the module are decode, which calls the decoder -# referenced by the cte specifier, and encode, which adds the appropriate -# RFC 2047 "chrome" to the encoded string, and can optionally automatically -# select the shortest possible encoding. See their docstrings below for -# details. - -import re -import base64 -import binascii -import functools -from string import ascii_letters, digits -from email import errors - -__all__ = ['decode_q', - 'encode_q', - 'decode_b', - 'encode_b', - 'len_q', - 'len_b', - 'decode', - 'encode', - ] - -# -# Quoted Printable -# - -# regex based decoder. -_q_byte_subber = functools.partial(re.compile(br'=([a-fA-F0-9]{2})').sub, - lambda m: bytes([int(m.group(1), 16)])) - -def decode_q(encoded): - encoded = encoded.replace(b'_', b' ') - return _q_byte_subber(encoded), [] - - -# dict mapping bytes to their encoded form -class _QByteMap(dict): - - safe = b'-!*+/' + ascii_letters.encode('ascii') + digits.encode('ascii') - - def __missing__(self, key): - if key in self.safe: - self[key] = chr(key) - else: - self[key] = "={:02X}".format(key) - return self[key] - -_q_byte_map = _QByteMap() - -# In headers spaces are mapped to '_'. -_q_byte_map[ord(' ')] = '_' - -def encode_q(bstring): - return ''.join(_q_byte_map[x] for x in bstring) - -def len_q(bstring): - return sum(len(_q_byte_map[x]) for x in bstring) - - -# -# Base64 -# - -def decode_b(encoded): - defects = [] - pad_err = len(encoded) % 4 - if pad_err: - defects.append(errors.InvalidBase64PaddingDefect()) - padded_encoded = encoded + b'==='[:4-pad_err] - else: - padded_encoded = encoded - try: - return base64.b64decode(padded_encoded, validate=True), defects - except binascii.Error: - # Since we had correct padding, this must an invalid char error. - defects = [errors.InvalidBase64CharactersDefect()] - # The non-alphabet characters are ignored as far as padding - # goes, but we don't know how many there are. So we'll just - # try various padding lengths until something works. - for i in 0, 1, 2, 3: - try: - return base64.b64decode(encoded+b'='*i, validate=False), defects - except binascii.Error: - if i==0: - defects.append(errors.InvalidBase64PaddingDefect()) - else: - # This should never happen. - raise AssertionError("unexpected binascii.Error") - -def encode_b(bstring): - return base64.b64encode(bstring).decode('ascii') - -def len_b(bstring): - groups_of_3, leftover = divmod(len(bstring), 3) - # 4 bytes out for each 3 bytes (or nonzero fraction thereof) in. - return groups_of_3 * 4 + (4 if leftover else 0) - - -_cte_decoders = { - 'q': decode_q, - 'b': decode_b, - } - -def decode(ew): - """Decode encoded word and return (string, charset, lang, defects) tuple. - - An RFC 2047/2243 encoded word has the form: - - =?charset*lang?cte?encoded_string?= - - where '*lang' may be omitted but the other parts may not be. - - This function expects exactly such a string (that is, it does not check the - syntax and may raise errors if the string is not well formed), and returns - the encoded_string decoded first from its Content Transfer Encoding and - then from the resulting bytes into unicode using the specified charset. If - the cte-decoded string does not successfully decode using the specified - character set, a defect is added to the defects list and the unknown octets - are replaced by the unicode 'unknown' character \uFDFF. - - The specified charset and language are returned. The default for language, - which is rarely if ever encountered, is the empty string. - - """ - _, charset, cte, cte_string, _ = ew.split('?') - charset, _, lang = charset.partition('*') - cte = cte.lower() - # Recover the original bytes and do CTE decoding. - bstring = cte_string.encode('ascii', 'surrogateescape') - bstring, defects = _cte_decoders[cte](bstring) - # Turn the CTE decoded bytes into unicode. - try: - string = bstring.decode(charset) - except UnicodeError: - defects.append(errors.UndecodableBytesDefect("Encoded word " - "contains bytes not decodable using {} charset".format(charset))) - string = bstring.decode(charset, 'surrogateescape') - except LookupError: - string = bstring.decode('ascii', 'surrogateescape') - if charset.lower() != 'unknown-8bit': - defects.append(errors.CharsetError("Unknown charset {} " - "in encoded word; decoded as unknown bytes".format(charset))) - return string, charset, lang, defects - - -_cte_encoders = { - 'q': encode_q, - 'b': encode_b, - } - -_cte_encode_length = { - 'q': len_q, - 'b': len_b, - } - -def encode(string, charset='utf-8', encoding=None, lang=''): - """Encode string using the CTE encoding that produces the shorter result. - - Produces an RFC 2047/2243 encoded word of the form: - - =?charset*lang?cte?encoded_string?= - - where '*lang' is omitted unless the 'lang' parameter is given a value. - Optional argument charset (defaults to utf-8) specifies the charset to use - to encode the string to binary before CTE encoding it. Optional argument - 'encoding' is the cte specifier for the encoding that should be used ('q' - or 'b'); if it is None (the default) the encoding which produces the - shortest encoded sequence is used, except that 'q' is preferred if it is up - to five characters longer. Optional argument 'lang' (default '') gives the - RFC 2243 language string to specify in the encoded word. - - """ - if charset == 'unknown-8bit': - bstring = string.encode('ascii', 'surrogateescape') - else: - bstring = string.encode(charset) - if encoding is None: - qlen = _cte_encode_length['q'](bstring) - blen = _cte_encode_length['b'](bstring) - # Bias toward q. 5 is arbitrary. - encoding = 'q' if qlen - blen < 5 else 'b' - encoded = _cte_encoders[encoding](bstring) - if lang: - lang = '*' + lang - return "=?{}{}?{}?{}?=".format(charset, lang, encoding, encoded) diff -r 2059910e7d76 -r ac776ef41428 Lib/email/_header_value_parser.py --- a/Lib/email/_header_value_parser.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,2155 +0,0 @@ -"""Header value parser implementing various email-related RFC parsing rules. - -The parsing methods defined in this module implement various email related -parsing rules. Principal among them is RFC 5322, which is the followon -to RFC 2822 and primarily a clarification of the former. It also implements -RFC 2047 encoded word decoding. - -RFC 5322 goes to considerable trouble to maintain backward compatibility with -RFC 822 in the parse phase, while cleaning up the structure on the generation -phase. This parser supports correct RFC 5322 generation by tagging white space -as folding white space only when folding is allowed in the non-obsolete rule -sets. Actually, the parser is even more generous when accepting input than RFC -5322 mandates, following the spirit of Postel's Law, which RFC 5322 encourages. -Where possible deviations from the standard are annotated on the 'defects' -attribute of tokens that deviate. - -The general structure of the parser follows RFC 5322, and uses its terminology -where there is a direct correspondence. Where the implementation requires a -somewhat different structure than that used by the formal grammar, new terms -that mimic the closest existing terms are used. Thus, it really helps to have -a copy of RFC 5322 handy when studying this code. - -Input to the parser is a string that has already been unfolded according to -RFC 5322 rules. According to the RFC this unfolding is the very first step, and -this parser leaves the unfolding step to a higher level message parser, which -will have already detected the line breaks that need unfolding while -determining the beginning and end of each header. - -The output of the parser is a TokenList object, which is a list subclass. A -TokenList is a recursive data structure. The terminal nodes of the structure -are Terminal objects, which are subclasses of str. These do not correspond -directly to terminal objects in the formal grammar, but are instead more -practical higher level combinations of true terminals. - -All TokenList and Terminal objects have a 'value' attribute, which produces the -semantically meaningful value of that part of the parse subtree. The value of -all whitespace tokens (no matter how many sub-tokens they may contain) is a -single space, as per the RFC rules. This includes 'CFWS', which is herein -included in the general class of whitespace tokens. There is one exception to -the rule that whitespace tokens are collapsed into single spaces in values: in -the value of a 'bare-quoted-string' (a quoted-string with no leading or -trailing whitespace), any whitespace that appeared between the quotation marks -is preserved in the returned value. Note that in all Terminal strings quoted -pairs are turned into their unquoted values. - -All TokenList and Terminal objects also have a string value, which attempts to -be a "canonical" representation of the RFC-compliant form of the substring that -produced the parsed subtree, including minimal use of quoted pair quoting. -Whitespace runs are not collapsed. - -Comment tokens also have a 'content' attribute providing the string found -between the parens (including any nested comments) with whitespace preserved. - -All TokenList and Terminal objects have a 'defects' attribute which is a -possibly empty list all of the defects found while creating the token. Defects -may appear on any token in the tree, and a composite list of all defects in the -subtree is available through the 'all_defects' attribute of any node. (For -Terminal notes x.defects == x.all_defects.) - -Each object in a parse tree is called a 'token', and each has a 'token_type' -attribute that gives the name from the RFC 5322 grammar that it represents. -Not all RFC 5322 nodes are produced, and there is one non-RFC 5322 node that -may be produced: 'ptext'. A 'ptext' is a string of printable ascii characters. -It is returned in place of lists of (ctext/quoted-pair) and -(qtext/quoted-pair). - -XXX: provide complete list of token types. -""" - -import re -from email import _encoded_words as _ew -from email import errors -from email import utils - -# -# Useful constants and functions -# - -WSP = set(' \t') -CFWS_LEADER = WSP | set('(') -SPECIALS = set(r'()<>@,:;.\"[]') -ATOM_ENDS = SPECIALS | WSP -DOT_ATOM_ENDS = ATOM_ENDS - set('.') -# '.', '"', and '(' do not end phrases in order to support obs-phrase -PHRASE_ENDS = SPECIALS - set('."(') - -def quote_string(value): - return '"'+str(value).replace('\\', '\\\\').replace('"', r'\"')+'"' - -# -# Accumulator for header folding -# - -class _Folded: - - def __init__(self, maxlen, policy): - self.maxlen = maxlen - self.policy = policy - self.lastlen = 0 - self.stickyspace = None - self.firstline = True - self.done = [] - self.current = [] - - def newline(self): - self.done.extend(self.current) - self.done.append(self.policy.linesep) - self.current.clear() - self.lastlen = 0 - - def finalize(self): - if self.current: - self.newline() - - def __str__(self): - return ''.join(self.done) - - def append(self, stoken): - self.current.append(stoken) - - def append_if_fits(self, token, stoken=None): - if stoken is None: - stoken = str(token) - l = len(stoken) - if self.stickyspace is not None: - stickyspace_len = len(self.stickyspace) - if self.lastlen + stickyspace_len + l <= self.maxlen: - self.current.append(self.stickyspace) - self.lastlen += stickyspace_len - self.current.append(stoken) - self.lastlen += l - self.stickyspace = None - self.firstline = False - return True - if token.has_fws: - ws = token.pop_leading_fws() - if ws is not None: - self.stickyspace += str(ws) - stickyspace_len += len(ws) - token._fold(self) - return True - if stickyspace_len and l + 1 <= self.maxlen: - margin = self.maxlen - l - if 0 < margin < stickyspace_len: - trim = stickyspace_len - margin - self.current.append(self.stickyspace[:trim]) - self.stickyspace = self.stickyspace[trim:] - stickyspace_len = trim - self.newline() - self.current.append(self.stickyspace) - self.current.append(stoken) - self.lastlen = l + stickyspace_len - self.stickyspace = None - self.firstline = False - return True - if not self.firstline: - self.newline() - self.current.append(self.stickyspace) - self.current.append(stoken) - self.stickyspace = None - self.firstline = False - return True - if self.lastlen + l <= self.maxlen: - self.current.append(stoken) - self.lastlen += l - return True - if l < self.maxlen: - self.newline() - self.current.append(stoken) - self.lastlen = l - return True - return False - -# -# TokenList and its subclasses -# - -class TokenList(list): - - token_type = None - - def __init__(self, *args, **kw): - super().__init__(*args, **kw) - self.defects = [] - - def __str__(self): - return ''.join(str(x) for x in self) - - def __repr__(self): - return '{}({})'.format(self.__class__.__name__, - super().__repr__()) - - @property - def value(self): - return ''.join(x.value for x in self if x.value) - - @property - def all_defects(self): - return sum((x.all_defects for x in self), self.defects) - - # - # Folding API - # - # parts(): - # - # return a list of objects that constitute the "higher level syntactic - # objects" specified by the RFC as the best places to fold a header line. - # The returned objects must include leading folding white space, even if - # this means mutating the underlying parse tree of the object. Each object - # is only responsible for returning *its* parts, and should not drill down - # to any lower level except as required to meet the leading folding white - # space constraint. - # - # _fold(folded): - # - # folded: the result accumulator. This is an instance of _Folded. - # (XXX: I haven't finished factoring this out yet, the folding code - # pretty much uses this as a state object.) When the folded.current - # contains as much text as will fit, the _fold method should call - # folded.newline. - # folded.lastlen: the current length of the test stored in folded.current. - # folded.maxlen: The maximum number of characters that may appear on a - # folded line. Differs from the policy setting in that "no limit" is - # represented by +inf, which means it can be used in the trivially - # logical fashion in comparisons. - # - # Currently no subclasses implement parts, and I think this will remain - # true. A subclass only needs to implement _fold when the generic version - # isn't sufficient. _fold will need to be implemented primarily when it is - # possible for encoded words to appear in the specialized token-list, since - # there is no generic algorithm that can know where exactly the encoded - # words are allowed. A _fold implementation is responsible for filling - # lines in the same general way that the top level _fold does. It may, and - # should, call the _fold method of sub-objects in a similar fashion to that - # of the top level _fold. - # - # XXX: I'm hoping it will be possible to factor the existing code further - # to reduce redundancy and make the logic clearer. - - @property - def parts(self): - klass = self.__class__ - this = [] - for token in self: - if token.startswith_fws(): - if this: - yield this[0] if len(this)==1 else klass(this) - this.clear() - end_ws = token.pop_trailing_ws() - this.append(token) - if end_ws: - yield klass(this) - this = [end_ws] - if this: - yield this[0] if len(this)==1 else klass(this) - - def startswith_fws(self): - return self[0].startswith_fws() - - def pop_leading_fws(self): - if self[0].token_type == 'fws': - return self.pop(0) - return self[0].pop_leading_fws() - - def pop_trailing_ws(self): - if self[-1].token_type == 'cfws': - return self.pop(-1) - return self[-1].pop_trailing_ws() - - @property - def has_fws(self): - for part in self: - if part.has_fws: - return True - return False - - def has_leading_comment(self): - return self[0].has_leading_comment() - - @property - def comments(self): - comments = [] - for token in self: - comments.extend(token.comments) - return comments - - def fold(self, *, policy): - # max_line_length 0/None means no limit, ie: infinitely long. - maxlen = policy.max_line_length or float("+inf") - folded = _Folded(maxlen, policy) - self._fold(folded) - folded.finalize() - return str(folded) - - def as_encoded_word(self, charset): - # This works only for things returned by 'parts', which include - # the leading fws, if any, that should be used. - res = [] - ws = self.pop_leading_fws() - if ws: - res.append(ws) - trailer = self.pop(-1) if self[-1].token_type=='fws' else '' - res.append(_ew.encode(str(self), charset)) - res.append(trailer) - return ''.join(res) - - def cte_encode(self, charset, policy): - res = [] - for part in self: - res.append(part.cte_encode(charset, policy)) - return ''.join(res) - - def _fold(self, folded): - for part in self.parts: - tstr = str(part) - tlen = len(tstr) - try: - str(part).encode('us-ascii') - except UnicodeEncodeError: - if any(isinstance(x, errors.UndecodableBytesDefect) - for x in part.all_defects): - charset = 'unknown-8bit' - else: - # XXX: this should be a policy setting - charset = 'utf-8' - tstr = part.cte_encode(charset, folded.policy) - tlen = len(tstr) - if folded.append_if_fits(part, tstr): - continue - # Peel off the leading whitespace if any and make it sticky, to - # avoid infinite recursion. - ws = part.pop_leading_fws() - if ws is not None: - # Peel off the leading whitespace and make it sticky, to - # avoid infinite recursion. - folded.stickyspace = str(part.pop(0)) - if folded.append_if_fits(part): - continue - if part.has_fws: - part._fold(folded) - continue - # There are no fold points in this one; it is too long for a single - # line and can't be split...we just have to put it on its own line. - folded.append(tstr) - folded.newline() - - def pprint(self, indent=''): - print('\n'.join(self._pp(indent=''))) - - def ppstr(self, indent=''): - return '\n'.join(self._pp(indent='')) - - def _pp(self, indent=''): - yield '{}{}/{}('.format( - indent, - self.__class__.__name__, - self.token_type) - for token in self: - for line in token._pp(indent+' '): - yield line - if self.defects: - extra = ' Defects: {}'.format(self.defects) - else: - extra = '' - yield '{}){}'.format(indent, extra) - - -class WhiteSpaceTokenList(TokenList): - - @property - def value(self): - return ' ' - - @property - def comments(self): - return [x.content for x in self if x.token_type=='comment'] - - -class UnstructuredTokenList(TokenList): - - token_type = 'unstructured' - - def _fold(self, folded): - if any(x.token_type=='encoded-word' for x in self): - return self._fold_encoded(folded) - # Here we can have either a pure ASCII string that may or may not - # have surrogateescape encoded bytes, or a unicode string. - last_ew = None - for part in self.parts: - tstr = str(part) - is_ew = False - try: - str(part).encode('us-ascii') - except UnicodeEncodeError: - if any(isinstance(x, errors.UndecodableBytesDefect) - for x in part.all_defects): - charset = 'unknown-8bit' - else: - charset = 'utf-8' - if last_ew is not None: - # We've already done an EW, combine this one with it - # if there's room. - chunk = get_unstructured( - ''.join(folded.current[last_ew:]+[tstr])).as_encoded_word(charset) - oldlastlen = sum(len(x) for x in folded.current[:last_ew]) - schunk = str(chunk) - lchunk = len(schunk) - if oldlastlen + lchunk <= folded.maxlen: - del folded.current[last_ew:] - folded.append(schunk) - folded.lastlen = oldlastlen + lchunk - continue - tstr = part.as_encoded_word(charset) - is_ew = True - if folded.append_if_fits(part, tstr): - if is_ew: - last_ew = len(folded.current) - 1 - continue - if is_ew or last_ew: - # It's too big to fit on the line, but since we've - # got encoded words we can use encoded word folding. - part._fold_as_ew(folded) - continue - # Peel off the leading whitespace if any and make it sticky, to - # avoid infinite recursion. - ws = part.pop_leading_fws() - if ws is not None: - folded.stickyspace = str(ws) - if folded.append_if_fits(part): - continue - if part.has_fws: - part.fold(folded) - continue - # It can't be split...we just have to put it on its own line. - folded.append(tstr) - folded.newline() - last_ew = None - - def cte_encode(self, charset, policy): - res = [] - last_ew = None - for part in self: - spart = str(part) - try: - spart.encode('us-ascii') - res.append(spart) - except UnicodeEncodeError: - if last_ew is None: - res.append(part.cte_encode(charset, policy)) - last_ew = len(res) - else: - tl = get_unstructured(''.join(res[last_ew:] + [spart])) - res.append(tl.as_encoded_word()) - return ''.join(res) - - -class Phrase(TokenList): - - token_type = 'phrase' - - def _fold(self, folded): - # As with Unstructured, we can have pure ASCII with or without - # surrogateescape encoded bytes, or we could have unicode. But this - # case is more complicated, since we have to deal with the various - # sub-token types and how they can be composed in the face of - # unicode-that-needs-CTE-encoding, and the fact that if a token a - # comment that becomes a barrier across which we can't compose encoded - # words. - last_ew = None - for part in self.parts: - tstr = str(part) - tlen = len(tstr) - has_ew = False - try: - str(part).encode('us-ascii') - except UnicodeEncodeError: - if any(isinstance(x, errors.UndecodableBytesDefect) - for x in part.all_defects): - charset = 'unknown-8bit' - else: - charset = 'utf-8' - if last_ew is not None and not part.has_leading_comment(): - # We've already done an EW, let's see if we can combine - # this one with it. The last_ew logic ensures that all we - # have at this point is atoms, no comments or quoted - # strings. So we can treat the text between the last - # encoded word and the content of this token as - # unstructured text, and things will work correctly. But - # we have to strip off any trailing comment on this token - # first, and if it is a quoted string we have to pull out - # the content (we're encoding it, so it no longer needs to - # be quoted). - if part[-1].token_type == 'cfws' and part.comments: - remainder = part.pop(-1) - else: - remainder = '' - for i, token in enumerate(part): - if token.token_type == 'bare-quoted-string': - part[i] = UnstructuredTokenList(token[:]) - chunk = get_unstructured( - ''.join(folded.current[last_ew:]+[tstr])).as_encoded_word(charset) - schunk = str(chunk) - lchunk = len(schunk) - if last_ew + lchunk <= folded.maxlen: - del folded.current[last_ew:] - folded.append(schunk) - folded.lastlen = sum(len(x) for x in folded.current) - continue - tstr = part.as_encoded_word(charset) - tlen = len(tstr) - has_ew = True - if folded.append_if_fits(part, tstr): - if has_ew and not part.comments: - last_ew = len(folded.current) - 1 - elif part.comments or part.token_type == 'quoted-string': - # If a comment is involved we can't combine EWs. And if a - # quoted string is involved, it's not worth the effort to - # try to combine them. - last_ew = None - continue - part._fold(folded) - - def cte_encode(self, charset, policy): - res = [] - last_ew = None - is_ew = False - for part in self: - spart = str(part) - try: - spart.encode('us-ascii') - res.append(spart) - except UnicodeEncodeError: - is_ew = True - if last_ew is None: - if not part.comments: - last_ew = len(res) - res.append(part.cte_encode(charset, policy)) - elif not part.has_leading_comment(): - if part[-1].token_type == 'cfws' and part.comments: - remainder = part.pop(-1) - else: - remainder = '' - for i, token in enumerate(part): - if token.token_type == 'bare-quoted-string': - part[i] = UnstructuredTokenList(token[:]) - tl = get_unstructured(''.join(res[last_ew:] + [spart])) - res[last_ew:] = [tl.as_encoded_word(charset)] - if part.comments or (not is_ew and part.token_type == 'quoted-string'): - last_ew = None - return ''.join(res) - -class Word(TokenList): - - token_type = 'word' - - -class CFWSList(WhiteSpaceTokenList): - - token_type = 'cfws' - - def has_leading_comment(self): - return bool(self.comments) - - -class Atom(TokenList): - - token_type = 'atom' - - -class EncodedWord(TokenList): - - token_type = 'encoded-word' - cte = None - charset = None - lang = None - - @property - def encoded(self): - if self.cte is not None: - return self.cte - _ew.encode(str(self), self.charset) - - - -class QuotedString(TokenList): - - token_type = 'quoted-string' - - @property - def content(self): - for x in self: - if x.token_type == 'bare-quoted-string': - return x.value - - @property - def quoted_value(self): - res = [] - for x in self: - if x.token_type == 'bare-quoted-string': - res.append(str(x)) - else: - res.append(x.value) - return ''.join(res) - - -class BareQuotedString(QuotedString): - - token_type = 'bare-quoted-string' - - def __str__(self): - return quote_string(''.join(self)) - - @property - def value(self): - return ''.join(str(x) for x in self) - - -class Comment(WhiteSpaceTokenList): - - token_type = 'comment' - - def __str__(self): - return ''.join(sum([ - ["("], - [self.quote(x) for x in self], - [")"], - ], [])) - - def quote(self, value): - if value.token_type == 'comment': - return str(value) - return str(value).replace('\\', '\\\\').replace( - '(', '\(').replace( - ')', '\)') - - @property - def content(self): - return ''.join(str(x) for x in self) - - @property - def comments(self): - return [self.content] - -class AddressList(TokenList): - - token_type = 'address-list' - - @property - def addresses(self): - return [x for x in self if x.token_type=='address'] - - @property - def mailboxes(self): - return sum((x.mailboxes - for x in self if x.token_type=='address'), []) - - @property - def all_mailboxes(self): - return sum((x.all_mailboxes - for x in self if x.token_type=='address'), []) - - -class Address(TokenList): - - token_type = 'address' - - @property - def display_name(self): - if self[0].token_type == 'group': - return self[0].display_name - - @property - def mailboxes(self): - if self[0].token_type == 'mailbox': - return [self[0]] - elif self[0].token_type == 'invalid-mailbox': - return [] - return self[0].mailboxes - - @property - def all_mailboxes(self): - if self[0].token_type == 'mailbox': - return [self[0]] - elif self[0].token_type == 'invalid-mailbox': - return [self[0]] - return self[0].all_mailboxes - -class MailboxList(TokenList): - - token_type = 'mailbox-list' - - @property - def mailboxes(self): - return [x for x in self if x.token_type=='mailbox'] - - @property - def all_mailboxes(self): - return [x for x in self - if x.token_type in ('mailbox', 'invalid-mailbox')] - - -class GroupList(TokenList): - - token_type = 'group-list' - - @property - def mailboxes(self): - if not self or self[0].token_type != 'mailbox-list': - return [] - return self[0].mailboxes - - @property - def all_mailboxes(self): - if not self or self[0].token_type != 'mailbox-list': - return [] - return self[0].all_mailboxes - - -class Group(TokenList): - - token_type = "group" - - @property - def mailboxes(self): - if self[2].token_type != 'group-list': - return [] - return self[2].mailboxes - - @property - def all_mailboxes(self): - if self[2].token_type != 'group-list': - return [] - return self[2].all_mailboxes - - @property - def display_name(self): - return self[0].display_name - - -class NameAddr(TokenList): - - token_type = 'name-addr' - - @property - def display_name(self): - if len(self) == 1: - return None - return self[0].display_name - - @property - def local_part(self): - return self[-1].local_part - - @property - def domain(self): - return self[-1].domain - - @property - def route(self): - return self[-1].route - - @property - def addr_spec(self): - return self[-1].addr_spec - - -class AngleAddr(TokenList): - - token_type = 'angle-addr' - - @property - def local_part(self): - for x in self: - if x.token_type == 'addr-spec': - return x.local_part - - @property - def domain(self): - for x in self: - if x.token_type == 'addr-spec': - return x.domain - - @property - def route(self): - for x in self: - if x.token_type == 'obs-route': - return x.domains - - @property - def addr_spec(self): - for x in self: - if x.token_type == 'addr-spec': - return x.addr_spec - else: - return '<>' - - -class ObsRoute(TokenList): - - token_type = 'obs-route' - - @property - def domains(self): - return [x.domain for x in self if x.token_type == 'domain'] - - -class Mailbox(TokenList): - - token_type = 'mailbox' - - @property - def display_name(self): - if self[0].token_type == 'name-addr': - return self[0].display_name - - @property - def local_part(self): - return self[0].local_part - - @property - def domain(self): - return self[0].domain - - @property - def route(self): - if self[0].token_type == 'name-addr': - return self[0].route - - @property - def addr_spec(self): - return self[0].addr_spec - - -class InvalidMailbox(TokenList): - - token_type = 'invalid-mailbox' - - @property - def display_name(self): - return None - - local_part = domain = route = addr_spec = display_name - - -class Domain(TokenList): - - token_type = 'domain' - - @property - def domain(self): - return ''.join(super().value.split()) - - -class DotAtom(TokenList): - - token_type = 'dot-atom' - - -class DotAtomText(TokenList): - - token_type = 'dot-atom-text' - - -class AddrSpec(TokenList): - - token_type = 'addr-spec' - - @property - def local_part(self): - return self[0].local_part - - @property - def domain(self): - if len(self) < 3: - return None - return self[-1].domain - - @property - def value(self): - if len(self) < 3: - return self[0].value - return self[0].value.rstrip()+self[1].value+self[2].value.lstrip() - - @property - def addr_spec(self): - nameset = set(self.local_part) - if len(nameset) > len(nameset-DOT_ATOM_ENDS): - lp = quote_string(self.local_part) - else: - lp = self.local_part - if self.domain is not None: - return lp + '@' + self.domain - return lp - - -class ObsLocalPart(TokenList): - - token_type = 'obs-local-part' - - -class DisplayName(Phrase): - - token_type = 'display-name' - - @property - def display_name(self): - res = TokenList(self) - if res[0].token_type == 'cfws': - res.pop(0) - else: - if res[0][0].token_type == 'cfws': - res[0] = TokenList(res[0][1:]) - if res[-1].token_type == 'cfws': - res.pop() - else: - if res[-1][-1].token_type == 'cfws': - res[-1] = TokenList(res[-1][:-1]) - return res.value - - @property - def value(self): - quote = False - if self.defects: - quote = True - else: - for x in self: - if x.token_type == 'quoted-string': - quote = True - if quote: - pre = post = '' - if self[0].token_type=='cfws' or self[0][0].token_type=='cfws': - pre = ' ' - if self[-1].token_type=='cfws' or self[-1][-1].token_type=='cfws': - post = ' ' - return pre+quote_string(self.display_name)+post - else: - return super().value - - -class LocalPart(TokenList): - - token_type = 'local-part' - - @property - def value(self): - if self[0].token_type == "quoted-string": - return self[0].quoted_value - else: - return self[0].value - - @property - def local_part(self): - # Strip whitespace from front, back, and around dots. - res = [DOT] - last = DOT - last_is_tl = False - for tok in self[0] + [DOT]: - if tok.token_type == 'cfws': - continue - if (last_is_tl and tok.token_type == 'dot' and - last[-1].token_type == 'cfws'): - res[-1] = TokenList(last[:-1]) - is_tl = isinstance(tok, TokenList) - if (is_tl and last.token_type == 'dot' and - tok[0].token_type == 'cfws'): - res.append(TokenList(tok[1:])) - else: - res.append(tok) - last = res[-1] - last_is_tl = is_tl - res = TokenList(res[1:-1]) - return res.value - - -class DomainLiteral(TokenList): - - token_type = 'domain-literal' - - @property - def domain(self): - return ''.join(super().value.split()) - - @property - def ip(self): - for x in self: - if x.token_type == 'ptext': - return x.value - - -class HeaderLabel(TokenList): - - token_type = 'header-label' - - -class Header(TokenList): - - token_type = 'header' - - def _fold(self, folded): - folded.append(str(self.pop(0))) - folded.lastlen = len(folded.current[0]) - # The first line of the header is different from all others: we don't - # want to start a new object on a new line if it has any fold points in - # it that would allow part of it to be on the first header line. - # Further, if the first fold point would fit on the new line, we want - # to do that, but if it doesn't we want to put it on the first line. - # Folded supports this via the stickyspace attribute. If this - # attribute is not None, it does the special handling. - folded.stickyspace = str(self.pop(0)) if self[0].token_type == 'cfws' else '' - rest = self.pop(0) - if self: - raise ValueError("Malformed Header token list") - rest._fold(folded) - - -# -# Terminal classes and instances -# - -class Terminal(str): - - def __new__(cls, value, token_type): - self = super().__new__(cls, value) - self.token_type = token_type - self.defects = [] - return self - - def __repr__(self): - return "{}({})".format(self.__class__.__name__, super().__repr__()) - - @property - def all_defects(self): - return list(self.defects) - - def _pp(self, indent=''): - return ["{}{}/{}({}){}".format( - indent, - self.__class__.__name__, - self.token_type, - super().__repr__(), - '' if not self.defects else ' {}'.format(self.defects), - )] - - def cte_encode(self, charset, policy): - value = str(self) - try: - value.encode('us-ascii') - return value - except UnicodeEncodeError: - return _ew.encode(value, charset) - - def pop_trailing_ws(self): - # This terminates the recursion. - return None - - def pop_leading_fws(self): - # This terminates the recursion. - return None - - @property - def comments(self): - return [] - - def has_leading_comment(self): - return False - - def __getnewargs__(self): - return(str(self), self.token_type) - - -class WhiteSpaceTerminal(Terminal): - - @property - def value(self): - return ' ' - - def startswith_fws(self): - return True - - has_fws = True - - -class ValueTerminal(Terminal): - - @property - def value(self): - return self - - def startswith_fws(self): - return False - - has_fws = False - - def as_encoded_word(self, charset): - return _ew.encode(str(self), charset) - - -class EWWhiteSpaceTerminal(WhiteSpaceTerminal): - - @property - def value(self): - return '' - - @property - def encoded(self): - return self[:] - - def __str__(self): - return '' - - has_fws = True - - -# XXX these need to become classes and used as instances so -# that a program can't change them in a parse tree and screw -# up other parse trees. Maybe should have tests for that, too. -DOT = ValueTerminal('.', 'dot') -ListSeparator = ValueTerminal(',', 'list-separator') -RouteComponentMarker = ValueTerminal('@', 'route-component-marker') - -# -# Parser -# - -"""Parse strings according to RFC822/2047/2822/5322 rules. - -This is a stateless parser. Each get_XXX function accepts a string and -returns either a Terminal or a TokenList representing the RFC object named -by the method and a string containing the remaining unparsed characters -from the input. Thus a parser method consumes the next syntactic construct -of a given type and returns a token representing the construct plus the -unparsed remainder of the input string. - -For example, if the first element of a structured header is a 'phrase', -then: - - phrase, value = get_phrase(value) - -returns the complete phrase from the start of the string value, plus any -characters left in the string after the phrase is removed. - -""" - -_wsp_splitter = re.compile(r'([{}]+)'.format(''.join(WSP))).split -_non_atom_end_matcher = re.compile(r"[^{}]+".format( - ''.join(ATOM_ENDS).replace('\\','\\\\').replace(']','\]'))).match -_non_printable_finder = re.compile(r"[\x00-\x20\x7F]").findall - -def _validate_xtext(xtext): - """If input token contains ASCII non-printables, register a defect.""" - - non_printables = _non_printable_finder(xtext) - if non_printables: - xtext.defects.append(errors.NonPrintableDefect(non_printables)) - if utils._has_surrogates(xtext): - xtext.defects.append(errors.UndecodableBytesDefect( - "Non-ASCII characters found in header token")) - -def _get_ptext_to_endchars(value, endchars): - """Scan printables/quoted-pairs until endchars and return unquoted ptext. - - This function turns a run of qcontent, ccontent-without-comments, or - dtext-with-quoted-printables into a single string by unquoting any - quoted printables. It returns the string, the remaining value, and - a flag that is True iff there were any quoted printables decoded. - - """ - fragment, *remainder = _wsp_splitter(value, 1) - vchars = [] - escape = False - had_qp = False - for pos in range(len(fragment)): - if fragment[pos] == '\\': - if escape: - escape = False - had_qp = True - else: - escape = True - continue - if escape: - escape = False - elif fragment[pos] in endchars: - break - vchars.append(fragment[pos]) - else: - pos = pos + 1 - return ''.join(vchars), ''.join([fragment[pos:]] + remainder), had_qp - -def _decode_ew_run(value): - """ Decode a run of RFC2047 encoded words. - - _decode_ew_run(value) -> (text, value, defects) - - Scans the supplied value for a run of tokens that look like they are RFC - 2047 encoded words, decodes those words into text according to RFC 2047 - rules (whitespace between encoded words is discarded), and returns the text - and the remaining value (including any leading whitespace on the remaining - value), as well as a list of any defects encountered while decoding. The - input value may not have any leading whitespace. - - """ - res = [] - defects = [] - last_ws = '' - while value: - try: - tok, ws, value = _wsp_splitter(value, 1) - except ValueError: - tok, ws, value = value, '', '' - if not (tok.startswith('=?') and tok.endswith('?=')): - return ''.join(res), last_ws + tok + ws + value, defects - text, charset, lang, new_defects = _ew.decode(tok) - res.append(text) - defects.extend(new_defects) - last_ws = ws - return ''.join(res), last_ws, defects - -def get_fws(value): - """FWS = 1*WSP - - This isn't the RFC definition. We're using fws to represent tokens where - folding can be done, but when we are parsing the *un*folding has already - been done so we don't need to watch out for CRLF. - - """ - newvalue = value.lstrip() - fws = WhiteSpaceTerminal(value[:len(value)-len(newvalue)], 'fws') - return fws, newvalue - -def get_encoded_word(value): - """ encoded-word = "=?" charset "?" encoding "?" encoded-text "?=" - - """ - ew = EncodedWord() - if not value.startswith('=?'): - raise errors.HeaderParseError( - "expected encoded word but found {}".format(value)) - tok, *remainder = value[2:].split('?=', 1) - if tok == value[2:]: - raise errors.HeaderParseError( - "expected encoded word but found {}".format(value)) - remstr = ''.join(remainder) - if remstr[:2].isdigit(): - rest, *remainder = remstr.split('?=', 1) - tok = tok + '?=' + rest - if len(tok.split()) > 1: - ew.defects.append(errors.InvalidHeaderDefect( - "whitespace inside encoded word")) - ew.cte = value - value = ''.join(remainder) - try: - text, charset, lang, defects = _ew.decode('=?' + tok + '?=') - except ValueError: - raise errors.HeaderParseError( - "encoded word format invalid: '{}'".format(ew.cte)) - ew.charset = charset - ew.lang = lang - ew.defects.extend(defects) - while text: - if text[0] in WSP: - token, text = get_fws(text) - ew.append(token) - continue - chars, *remainder = _wsp_splitter(text, 1) - vtext = ValueTerminal(chars, 'vtext') - _validate_xtext(vtext) - ew.append(vtext) - text = ''.join(remainder) - return ew, value - -def get_unstructured(value): - """unstructured = (*([FWS] vchar) *WSP) / obs-unstruct - obs-unstruct = *((*LF *CR *(obs-utext) *LF *CR)) / FWS) - obs-utext = %d0 / obs-NO-WS-CTL / LF / CR - - obs-NO-WS-CTL is control characters except WSP/CR/LF. - - So, basically, we have printable runs, plus control characters or nulls in - the obsolete syntax, separated by whitespace. Since RFC 2047 uses the - obsolete syntax in its specification, but requires whitespace on either - side of the encoded words, I can see no reason to need to separate the - non-printable-non-whitespace from the printable runs if they occur, so we - parse this into xtext tokens separated by WSP tokens. - - Because an 'unstructured' value must by definition constitute the entire - value, this 'get' routine does not return a remaining value, only the - parsed TokenList. - - """ - # XXX: but what about bare CR and LF? They might signal the start or - # end of an encoded word. YAGNI for now, since out current parsers - # will never send us strings with bard CR or LF. - - unstructured = UnstructuredTokenList() - while value: - if value[0] in WSP: - token, value = get_fws(value) - unstructured.append(token) - continue - if value.startswith('=?'): - try: - token, value = get_encoded_word(value) - except errors.HeaderParseError: - pass - else: - have_ws = True - if len(unstructured) > 0: - if unstructured[-1].token_type != 'fws': - unstructured.defects.append(errors.InvalidHeaderDefect( - "missing whitespace before encoded word")) - have_ws = False - if have_ws and len(unstructured) > 1: - if unstructured[-2].token_type == 'encoded-word': - unstructured[-1] = EWWhiteSpaceTerminal( - unstructured[-1], 'fws') - unstructured.append(token) - continue - tok, *remainder = _wsp_splitter(value, 1) - vtext = ValueTerminal(tok, 'vtext') - _validate_xtext(vtext) - unstructured.append(vtext) - value = ''.join(remainder) - return unstructured - -def get_qp_ctext(value): - """ctext = - - This is not the RFC ctext, since we are handling nested comments in comment - and unquoting quoted-pairs here. We allow anything except the '()' - characters, but if we find any ASCII other than the RFC defined printable - ASCII an NonPrintableDefect is added to the token's defects list. Since - quoted pairs are converted to their unquoted values, what is returned is - a 'ptext' token. In this case it is a WhiteSpaceTerminal, so it's value - is ' '. - - """ - ptext, value, _ = _get_ptext_to_endchars(value, '()') - ptext = WhiteSpaceTerminal(ptext, 'ptext') - _validate_xtext(ptext) - return ptext, value - -def get_qcontent(value): - """qcontent = qtext / quoted-pair - - We allow anything except the DQUOTE character, but if we find any ASCII - other than the RFC defined printable ASCII an NonPrintableDefect is - added to the token's defects list. Any quoted pairs are converted to their - unquoted values, so what is returned is a 'ptext' token. In this case it - is a ValueTerminal. - - """ - ptext, value, _ = _get_ptext_to_endchars(value, '"') - ptext = ValueTerminal(ptext, 'ptext') - _validate_xtext(ptext) - return ptext, value - -def get_atext(value): - """atext = - - We allow any non-ATOM_ENDS in atext, but add an InvalidATextDefect to - the token's defects list if we find non-atext characters. - """ - m = _non_atom_end_matcher(value) - if not m: - raise errors.HeaderParseError( - "expected atext but found '{}'".format(value)) - atext = m.group() - value = value[len(atext):] - atext = ValueTerminal(atext, 'atext') - _validate_xtext(atext) - return atext, value - -def get_bare_quoted_string(value): - """bare-quoted-string = DQUOTE *([FWS] qcontent) [FWS] DQUOTE - - A quoted-string without the leading or trailing white space. Its - value is the text between the quote marks, with whitespace - preserved and quoted pairs decoded. - """ - if value[0] != '"': - raise errors.HeaderParseError( - "expected '\"' but found '{}'".format(value)) - bare_quoted_string = BareQuotedString() - value = value[1:] - while value and value[0] != '"': - if value[0] in WSP: - token, value = get_fws(value) - else: - token, value = get_qcontent(value) - bare_quoted_string.append(token) - if not value: - bare_quoted_string.defects.append(errors.InvalidHeaderDefect( - "end of header inside quoted string")) - return bare_quoted_string, value - return bare_quoted_string, value[1:] - -def get_comment(value): - """comment = "(" *([FWS] ccontent) [FWS] ")" - ccontent = ctext / quoted-pair / comment - - We handle nested comments here, and quoted-pair in our qp-ctext routine. - """ - if value and value[0] != '(': - raise errors.HeaderParseError( - "expected '(' but found '{}'".format(value)) - comment = Comment() - value = value[1:] - while value and value[0] != ")": - if value[0] in WSP: - token, value = get_fws(value) - elif value[0] == '(': - token, value = get_comment(value) - else: - token, value = get_qp_ctext(value) - comment.append(token) - if not value: - comment.defects.append(errors.InvalidHeaderDefect( - "end of header inside comment")) - return comment, value - return comment, value[1:] - -def get_cfws(value): - """CFWS = (1*([FWS] comment) [FWS]) / FWS - - """ - cfws = CFWSList() - while value and value[0] in CFWS_LEADER: - if value[0] in WSP: - token, value = get_fws(value) - else: - token, value = get_comment(value) - cfws.append(token) - return cfws, value - -def get_quoted_string(value): - """quoted-string = [CFWS] [CFWS] - - 'bare-quoted-string' is an intermediate class defined by this - parser and not by the RFC grammar. It is the quoted string - without any attached CFWS. - """ - quoted_string = QuotedString() - if value and value[0] in CFWS_LEADER: - token, value = get_cfws(value) - quoted_string.append(token) - token, value = get_bare_quoted_string(value) - quoted_string.append(token) - if value and value[0] in CFWS_LEADER: - token, value = get_cfws(value) - quoted_string.append(token) - return quoted_string, value - -def get_atom(value): - """atom = [CFWS] 1*atext [CFWS] - - """ - atom = Atom() - if value and value[0] in CFWS_LEADER: - token, value = get_cfws(value) - atom.append(token) - if value and value[0] in ATOM_ENDS: - raise errors.HeaderParseError( - "expected atom but found '{}'".format(value)) - token, value = get_atext(value) - atom.append(token) - if value and value[0] in CFWS_LEADER: - token, value = get_cfws(value) - atom.append(token) - return atom, value - -def get_dot_atom_text(value): - """ dot-text = 1*atext *("." 1*atext) - - """ - dot_atom_text = DotAtomText() - if not value or value[0] in ATOM_ENDS: - raise errors.HeaderParseError("expected atom at a start of " - "dot-atom-text but found '{}'".format(value)) - while value and value[0] not in ATOM_ENDS: - token, value = get_atext(value) - dot_atom_text.append(token) - if value and value[0] == '.': - dot_atom_text.append(DOT) - value = value[1:] - if dot_atom_text[-1] is DOT: - raise errors.HeaderParseError("expected atom at end of dot-atom-text " - "but found '{}'".format('.'+value)) - return dot_atom_text, value - -def get_dot_atom(value): - """ dot-atom = [CFWS] dot-atom-text [CFWS] - - """ - dot_atom = DotAtom() - if value[0] in CFWS_LEADER: - token, value = get_cfws(value) - dot_atom.append(token) - token, value = get_dot_atom_text(value) - dot_atom.append(token) - if value and value[0] in CFWS_LEADER: - token, value = get_cfws(value) - dot_atom.append(token) - return dot_atom, value - -def get_word(value): - """word = atom / quoted-string - - Either atom or quoted-string may start with CFWS. We have to peel off this - CFWS first to determine which type of word to parse. Afterward we splice - the leading CFWS, if any, into the parsed sub-token. - - If neither an atom or a quoted-string is found before the next special, a - HeaderParseError is raised. - - The token returned is either an Atom or a QuotedString, as appropriate. - This means the 'word' level of the formal grammar is not represented in the - parse tree; this is because having that extra layer when manipulating the - parse tree is more confusing than it is helpful. - - """ - if value[0] in CFWS_LEADER: - leader, value = get_cfws(value) - else: - leader = None - if value[0]=='"': - token, value = get_quoted_string(value) - elif value[0] in SPECIALS: - raise errors.HeaderParseError("Expected 'atom' or 'quoted-string' " - "but found '{}'".format(value)) - else: - token, value = get_atom(value) - if leader is not None: - token[:0] = [leader] - return token, value - -def get_phrase(value): - """ phrase = 1*word / obs-phrase - obs-phrase = word *(word / "." / CFWS) - - This means a phrase can be a sequence of words, periods, and CFWS in any - order as long as it starts with at least one word. If anything other than - words is detected, an ObsoleteHeaderDefect is added to the token's defect - list. We also accept a phrase that starts with CFWS followed by a dot; - this is registered as an InvalidHeaderDefect, since it is not supported by - even the obsolete grammar. - - """ - phrase = Phrase() - try: - token, value = get_word(value) - phrase.append(token) - except errors.HeaderParseError: - phrase.defects.append(errors.InvalidHeaderDefect( - "phrase does not start with word")) - while value and value[0] not in PHRASE_ENDS: - if value[0]=='.': - phrase.append(DOT) - phrase.defects.append(errors.ObsoleteHeaderDefect( - "period in 'phrase'")) - value = value[1:] - else: - try: - token, value = get_word(value) - except errors.HeaderParseError: - if value[0] in CFWS_LEADER: - token, value = get_cfws(value) - phrase.defects.append(errors.ObsoleteHeaderDefect( - "comment found without atom")) - else: - raise - phrase.append(token) - return phrase, value - -def get_local_part(value): - """ local-part = dot-atom / quoted-string / obs-local-part - - """ - local_part = LocalPart() - leader = None - if value[0] in CFWS_LEADER: - leader, value = get_cfws(value) - if not value: - raise errors.HeaderParseError( - "expected local-part but found '{}'".format(value)) - try: - token, value = get_dot_atom(value) - except errors.HeaderParseError: - try: - token, value = get_word(value) - except errors.HeaderParseError: - if value[0] != '\\' and value[0] in PHRASE_ENDS: - raise - token = TokenList() - if leader is not None: - token[:0] = [leader] - local_part.append(token) - if value and (value[0]=='\\' or value[0] not in PHRASE_ENDS): - obs_local_part, value = get_obs_local_part(str(local_part) + value) - if obs_local_part.token_type == 'invalid-obs-local-part': - local_part.defects.append(errors.InvalidHeaderDefect( - "local-part is not dot-atom, quoted-string, or obs-local-part")) - else: - local_part.defects.append(errors.ObsoleteHeaderDefect( - "local-part is not a dot-atom (contains CFWS)")) - local_part[0] = obs_local_part - try: - local_part.value.encode('ascii') - except UnicodeEncodeError: - local_part.defects.append(errors.NonASCIILocalPartDefect( - "local-part contains non-ASCII characters)")) - return local_part, value - -def get_obs_local_part(value): - """ obs-local-part = word *("." word) - """ - obs_local_part = ObsLocalPart() - last_non_ws_was_dot = False - while value and (value[0]=='\\' or value[0] not in PHRASE_ENDS): - if value[0] == '.': - if last_non_ws_was_dot: - obs_local_part.defects.append(errors.InvalidHeaderDefect( - "invalid repeated '.'")) - obs_local_part.append(DOT) - last_non_ws_was_dot = True - value = value[1:] - continue - elif value[0]=='\\': - obs_local_part.append(ValueTerminal(value[0], - 'misplaced-special')) - value = value[1:] - obs_local_part.defects.append(errors.InvalidHeaderDefect( - "'\\' character outside of quoted-string/ccontent")) - last_non_ws_was_dot = False - continue - if obs_local_part and obs_local_part[-1].token_type != 'dot': - obs_local_part.defects.append(errors.InvalidHeaderDefect( - "missing '.' between words")) - try: - token, value = get_word(value) - last_non_ws_was_dot = False - except errors.HeaderParseError: - if value[0] not in CFWS_LEADER: - raise - token, value = get_cfws(value) - obs_local_part.append(token) - if (obs_local_part[0].token_type == 'dot' or - obs_local_part[0].token_type=='cfws' and - obs_local_part[1].token_type=='dot'): - obs_local_part.defects.append(errors.InvalidHeaderDefect( - "Invalid leading '.' in local part")) - if (obs_local_part[-1].token_type == 'dot' or - obs_local_part[-1].token_type=='cfws' and - obs_local_part[-2].token_type=='dot'): - obs_local_part.defects.append(errors.InvalidHeaderDefect( - "Invalid trailing '.' in local part")) - if obs_local_part.defects: - obs_local_part.token_type = 'invalid-obs-local-part' - return obs_local_part, value - -def get_dtext(value): - """ dtext = / obs-dtext - obs-dtext = obs-NO-WS-CTL / quoted-pair - - We allow anything except the excluded characters, but but if we find any - ASCII other than the RFC defined printable ASCII an NonPrintableDefect is - added to the token's defects list. Quoted pairs are converted to their - unquoted values, so what is returned is a ptext token, in this case a - ValueTerminal. If there were quoted-printables, an ObsoleteHeaderDefect is - added to the returned token's defect list. - - """ - ptext, value, had_qp = _get_ptext_to_endchars(value, '[]') - ptext = ValueTerminal(ptext, 'ptext') - if had_qp: - ptext.defects.append(errors.ObsoleteHeaderDefect( - "quoted printable found in domain-literal")) - _validate_xtext(ptext) - return ptext, value - -def _check_for_early_dl_end(value, domain_literal): - if value: - return False - domain_literal.append(errors.InvalidHeaderDefect( - "end of input inside domain-literal")) - domain_literal.append(ValueTerminal(']', 'domain-literal-end')) - return True - -def get_domain_literal(value): - """ domain-literal = [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS] - - """ - domain_literal = DomainLiteral() - if value[0] in CFWS_LEADER: - token, value = get_cfws(value) - domain_literal.append(token) - if not value: - raise errors.HeaderParseError("expected domain-literal") - if value[0] != '[': - raise errors.HeaderParseError("expected '[' at start of domain-literal " - "but found '{}'".format(value)) - value = value[1:] - if _check_for_early_dl_end(value, domain_literal): - return domain_literal, value - domain_literal.append(ValueTerminal('[', 'domain-literal-start')) - if value[0] in WSP: - token, value = get_fws(value) - domain_literal.append(token) - token, value = get_dtext(value) - domain_literal.append(token) - if _check_for_early_dl_end(value, domain_literal): - return domain_literal, value - if value[0] in WSP: - token, value = get_fws(value) - domain_literal.append(token) - if _check_for_early_dl_end(value, domain_literal): - return domain_literal, value - if value[0] != ']': - raise errors.HeaderParseError("expected ']' at end of domain-literal " - "but found '{}'".format(value)) - domain_literal.append(ValueTerminal(']', 'domain-literal-end')) - value = value[1:] - if value and value[0] in CFWS_LEADER: - token, value = get_cfws(value) - domain_literal.append(token) - return domain_literal, value - -def get_domain(value): - """ domain = dot-atom / domain-literal / obs-domain - obs-domain = atom *("." atom)) - - """ - domain = Domain() - leader = None - if value[0] in CFWS_LEADER: - leader, value = get_cfws(value) - if not value: - raise errors.HeaderParseError( - "expected domain but found '{}'".format(value)) - if value[0] == '[': - token, value = get_domain_literal(value) - if leader is not None: - token[:0] = [leader] - domain.append(token) - return domain, value - try: - token, value = get_dot_atom(value) - except errors.HeaderParseError: - token, value = get_atom(value) - if leader is not None: - token[:0] = [leader] - domain.append(token) - if value and value[0] == '.': - domain.defects.append(errors.ObsoleteHeaderDefect( - "domain is not a dot-atom (contains CFWS)")) - if domain[0].token_type == 'dot-atom': - domain[:] = domain[0] - while value and value[0] == '.': - domain.append(DOT) - token, value = get_atom(value[1:]) - domain.append(token) - return domain, value - -def get_addr_spec(value): - """ addr-spec = local-part "@" domain - - """ - addr_spec = AddrSpec() - token, value = get_local_part(value) - addr_spec.append(token) - if not value or value[0] != '@': - addr_spec.defects.append(errors.InvalidHeaderDefect( - "add-spec local part with no domain")) - return addr_spec, value - addr_spec.append(ValueTerminal('@', 'address-at-symbol')) - token, value = get_domain(value[1:]) - addr_spec.append(token) - return addr_spec, value - -def get_obs_route(value): - """ obs-route = obs-domain-list ":" - obs-domain-list = *(CFWS / ",") "@" domain *("," [CFWS] ["@" domain]) - - Returns an obs-route token with the appropriate sub-tokens (that is, - there is no obs-domain-list in the parse tree). - """ - obs_route = ObsRoute() - while value and (value[0]==',' or value[0] in CFWS_LEADER): - if value[0] in CFWS_LEADER: - token, value = get_cfws(value) - obs_route.append(token) - elif value[0] == ',': - obs_route.append(ListSeparator) - value = value[1:] - if not value or value[0] != '@': - raise errors.HeaderParseError( - "expected obs-route domain but found '{}'".format(value)) - obs_route.append(RouteComponentMarker) - token, value = get_domain(value[1:]) - obs_route.append(token) - while value and value[0]==',': - obs_route.append(ListSeparator) - value = value[1:] - if not value: - break - if value[0] in CFWS_LEADER: - token, value = get_cfws(value) - obs_route.append(token) - if value[0] == '@': - obs_route.append(RouteComponentMarker) - token, value = get_domain(value[1:]) - obs_route.append(token) - if not value: - raise errors.HeaderParseError("end of header while parsing obs-route") - if value[0] != ':': - raise errors.HeaderParseError( "expected ':' marking end of " - "obs-route but found '{}'".format(value)) - obs_route.append(ValueTerminal(':', 'end-of-obs-route-marker')) - return obs_route, value[1:] - -def get_angle_addr(value): - """ angle-addr = [CFWS] "<" addr-spec ">" [CFWS] / obs-angle-addr - obs-angle-addr = [CFWS] "<" obs-route addr-spec ">" [CFWS] - - """ - angle_addr = AngleAddr() - if value[0] in CFWS_LEADER: - token, value = get_cfws(value) - angle_addr.append(token) - if not value or value[0] != '<': - raise errors.HeaderParseError( - "expected angle-addr but found '{}'".format(value)) - angle_addr.append(ValueTerminal('<', 'angle-addr-start')) - value = value[1:] - # Although it is not legal per RFC5322, SMTP uses '<>' in certain - # circumstances. - if value[0] == '>': - angle_addr.append(ValueTerminal('>', 'angle-addr-end')) - angle_addr.defects.append(errors.InvalidHeaderDefect( - "null addr-spec in angle-addr")) - value = value[1:] - return angle_addr, value - try: - token, value = get_addr_spec(value) - except errors.HeaderParseError: - try: - token, value = get_obs_route(value) - angle_addr.defects.append(errors.ObsoleteHeaderDefect( - "obsolete route specification in angle-addr")) - except errors.HeaderParseError: - raise errors.HeaderParseError( - "expected addr-spec or obs-route but found '{}'".format(value)) - angle_addr.append(token) - token, value = get_addr_spec(value) - angle_addr.append(token) - if value and value[0] == '>': - value = value[1:] - else: - angle_addr.defects.append(errors.InvalidHeaderDefect( - "missing trailing '>' on angle-addr")) - angle_addr.append(ValueTerminal('>', 'angle-addr-end')) - if value and value[0] in CFWS_LEADER: - token, value = get_cfws(value) - angle_addr.append(token) - return angle_addr, value - -def get_display_name(value): - """ display-name = phrase - - Because this is simply a name-rule, we don't return a display-name - token containing a phrase, but rather a display-name token with - the content of the phrase. - - """ - display_name = DisplayName() - token, value = get_phrase(value) - display_name.extend(token[:]) - display_name.defects = token.defects[:] - return display_name, value - - -def get_name_addr(value): - """ name-addr = [display-name] angle-addr - - """ - name_addr = NameAddr() - # Both the optional display name and the angle-addr can start with cfws. - leader = None - if value[0] in CFWS_LEADER: - leader, value = get_cfws(value) - if not value: - raise errors.HeaderParseError( - "expected name-addr but found '{}'".format(leader)) - if value[0] != '<': - if value[0] in PHRASE_ENDS: - raise errors.HeaderParseError( - "expected name-addr but found '{}'".format(value)) - token, value = get_display_name(value) - if not value: - raise errors.HeaderParseError( - "expected name-addr but found '{}'".format(token)) - if leader is not None: - token[0][:0] = [leader] - leader = None - name_addr.append(token) - token, value = get_angle_addr(value) - if leader is not None: - token[:0] = [leader] - name_addr.append(token) - return name_addr, value - -def get_mailbox(value): - """ mailbox = name-addr / addr-spec - - """ - # The only way to figure out if we are dealing with a name-addr or an - # addr-spec is to try parsing each one. - mailbox = Mailbox() - try: - token, value = get_name_addr(value) - except errors.HeaderParseError: - try: - token, value = get_addr_spec(value) - except errors.HeaderParseError: - raise errors.HeaderParseError( - "expected mailbox but found '{}'".format(value)) - if any(isinstance(x, errors.InvalidHeaderDefect) - for x in token.all_defects): - mailbox.token_type = 'invalid-mailbox' - mailbox.append(token) - return mailbox, value - -def get_invalid_mailbox(value, endchars): - """ Read everything up to one of the chars in endchars. - - This is outside the formal grammar. The InvalidMailbox TokenList that is - returned acts like a Mailbox, but the data attributes are None. - - """ - invalid_mailbox = InvalidMailbox() - while value and value[0] not in endchars: - if value[0] in PHRASE_ENDS: - invalid_mailbox.append(ValueTerminal(value[0], - 'misplaced-special')) - value = value[1:] - else: - token, value = get_phrase(value) - invalid_mailbox.append(token) - return invalid_mailbox, value - -def get_mailbox_list(value): - """ mailbox-list = (mailbox *("," mailbox)) / obs-mbox-list - obs-mbox-list = *([CFWS] ",") mailbox *("," [mailbox / CFWS]) - - For this routine we go outside the formal grammar in order to improve error - handling. We recognize the end of the mailbox list only at the end of the - value or at a ';' (the group terminator). This is so that we can turn - invalid mailboxes into InvalidMailbox tokens and continue parsing any - remaining valid mailboxes. We also allow all mailbox entries to be null, - and this condition is handled appropriately at a higher level. - - """ - mailbox_list = MailboxList() - while value and value[0] != ';': - try: - token, value = get_mailbox(value) - mailbox_list.append(token) - except errors.HeaderParseError: - leader = None - if value[0] in CFWS_LEADER: - leader, value = get_cfws(value) - if not value or value[0] in ',;': - mailbox_list.append(leader) - mailbox_list.defects.append(errors.ObsoleteHeaderDefect( - "empty element in mailbox-list")) - else: - token, value = get_invalid_mailbox(value, ',;') - if leader is not None: - token[:0] = [leader] - mailbox_list.append(token) - mailbox_list.defects.append(errors.InvalidHeaderDefect( - "invalid mailbox in mailbox-list")) - elif value[0] == ',': - mailbox_list.defects.append(errors.ObsoleteHeaderDefect( - "empty element in mailbox-list")) - else: - token, value = get_invalid_mailbox(value, ',;') - if leader is not None: - token[:0] = [leader] - mailbox_list.append(token) - mailbox_list.defects.append(errors.InvalidHeaderDefect( - "invalid mailbox in mailbox-list")) - if value and value[0] not in ',;': - # Crap after mailbox; treat it as an invalid mailbox. - # The mailbox info will still be available. - mailbox = mailbox_list[-1] - mailbox.token_type = 'invalid-mailbox' - token, value = get_invalid_mailbox(value, ',;') - mailbox.extend(token) - mailbox_list.defects.append(errors.InvalidHeaderDefect( - "invalid mailbox in mailbox-list")) - if value and value[0] == ',': - mailbox_list.append(ListSeparator) - value = value[1:] - return mailbox_list, value - - -def get_group_list(value): - """ group-list = mailbox-list / CFWS / obs-group-list - obs-group-list = 1*([CFWS] ",") [CFWS] - - """ - group_list = GroupList() - if not value: - group_list.defects.append(errors.InvalidHeaderDefect( - "end of header before group-list")) - return group_list, value - leader = None - if value and value[0] in CFWS_LEADER: - leader, value = get_cfws(value) - if not value: - # This should never happen in email parsing, since CFWS-only is a - # legal alternative to group-list in a group, which is the only - # place group-list appears. - group_list.defects.append(errors.InvalidHeaderDefect( - "end of header in group-list")) - group_list.append(leader) - return group_list, value - if value[0] == ';': - group_list.append(leader) - return group_list, value - token, value = get_mailbox_list(value) - if len(token.all_mailboxes)==0: - if leader is not None: - group_list.append(leader) - group_list.extend(token) - group_list.defects.append(errors.ObsoleteHeaderDefect( - "group-list with empty entries")) - return group_list, value - if leader is not None: - token[:0] = [leader] - group_list.append(token) - return group_list, value - -def get_group(value): - """ group = display-name ":" [group-list] ";" [CFWS] - - """ - group = Group() - token, value = get_display_name(value) - if not value or value[0] != ':': - raise errors.HeaderParseError("expected ':' at end of group " - "display name but found '{}'".format(value)) - group.append(token) - group.append(ValueTerminal(':', 'group-display-name-terminator')) - value = value[1:] - if value and value[0] == ';': - group.append(ValueTerminal(';', 'group-terminator')) - return group, value[1:] - token, value = get_group_list(value) - group.append(token) - if not value: - group.defects.append(errors.InvalidHeaderDefect( - "end of header in group")) - if value[0] != ';': - raise errors.HeaderParseError( - "expected ';' at end of group but found {}".format(value)) - group.append(ValueTerminal(';', 'group-terminator')) - value = value[1:] - if value and value[0] in CFWS_LEADER: - token, value = get_cfws(value) - group.append(token) - return group, value - -def get_address(value): - """ address = mailbox / group - - Note that counter-intuitively, an address can be either a single address or - a list of addresses (a group). This is why the returned Address object has - a 'mailboxes' attribute which treats a single address as a list of length - one. When you need to differentiate between to two cases, extract the single - element, which is either a mailbox or a group token. - - """ - # The formal grammar isn't very helpful when parsing an address. mailbox - # and group, especially when allowing for obsolete forms, start off very - # similarly. It is only when you reach one of @, <, or : that you know - # what you've got. So, we try each one in turn, starting with the more - # likely of the two. We could perhaps make this more efficient by looking - # for a phrase and then branching based on the next character, but that - # would be a premature optimization. - address = Address() - try: - token, value = get_group(value) - except errors.HeaderParseError: - try: - token, value = get_mailbox(value) - except errors.HeaderParseError: - raise errors.HeaderParseError( - "expected address but found '{}'".format(value)) - address.append(token) - return address, value - -def get_address_list(value): - """ address_list = (address *("," address)) / obs-addr-list - obs-addr-list = *([CFWS] ",") address *("," [address / CFWS]) - - We depart from the formal grammar here by continuing to parse until the end - of the input, assuming the input to be entirely composed of an - address-list. This is always true in email parsing, and allows us - to skip invalid addresses to parse additional valid ones. - - """ - address_list = AddressList() - while value: - try: - token, value = get_address(value) - address_list.append(token) - except errors.HeaderParseError as err: - leader = None - if value[0] in CFWS_LEADER: - leader, value = get_cfws(value) - if not value or value[0] == ',': - address_list.append(leader) - address_list.defects.append(errors.ObsoleteHeaderDefect( - "address-list entry with no content")) - else: - token, value = get_invalid_mailbox(value, ',') - if leader is not None: - token[:0] = [leader] - address_list.append(Address([token])) - address_list.defects.append(errors.InvalidHeaderDefect( - "invalid address in address-list")) - elif value[0] == ',': - address_list.defects.append(errors.ObsoleteHeaderDefect( - "empty element in address-list")) - else: - token, value = get_invalid_mailbox(value, ',') - if leader is not None: - token[:0] = [leader] - address_list.append(Address([token])) - address_list.defects.append(errors.InvalidHeaderDefect( - "invalid address in address-list")) - if value and value[0] != ',': - # Crap after address; treat it as an invalid mailbox. - # The mailbox info will still be available. - mailbox = address_list[-1][0] - mailbox.token_type = 'invalid-mailbox' - token, value = get_invalid_mailbox(value, ',') - mailbox.extend(token) - address_list.defects.append(errors.InvalidHeaderDefect( - "invalid address in address-list")) - if value: # Must be a , at this point. - address_list.append(ValueTerminal(',', 'list-separator')) - value = value[1:] - return address_list, value diff -r 2059910e7d76 -r ac776ef41428 Lib/email/_policybase.py --- a/Lib/email/_policybase.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,358 +0,0 @@ -"""Policy framework for the email package. - -Allows fine grained feature control of how the package parses and emits data. -""" - -import abc -from email import header -from email import charset as _charset -from email.utils import _has_surrogates - -__all__ = [ - 'Policy', - 'Compat32', - 'compat32', - ] - - -class _PolicyBase: - - """Policy Object basic framework. - - This class is useless unless subclassed. A subclass should define - class attributes with defaults for any values that are to be - managed by the Policy object. The constructor will then allow - non-default values to be set for these attributes at instance - creation time. The instance will be callable, taking these same - attributes keyword arguments, and returning a new instance - identical to the called instance except for those values changed - by the keyword arguments. Instances may be added, yielding new - instances with any non-default values from the right hand - operand overriding those in the left hand operand. That is, - - A + B == A() - - The repr of an instance can be used to reconstruct the object - if and only if the repr of the values can be used to reconstruct - those values. - - """ - - def __init__(self, **kw): - """Create new Policy, possibly overriding some defaults. - - See class docstring for a list of overridable attributes. - - """ - for name, value in kw.items(): - if hasattr(self, name): - super(_PolicyBase,self).__setattr__(name, value) - else: - raise TypeError( - "{!r} is an invalid keyword argument for {}".format( - name, self.__class__.__name__)) - - def __repr__(self): - args = [ "{}={!r}".format(name, value) - for name, value in self.__dict__.items() ] - return "{}({})".format(self.__class__.__name__, ', '.join(args)) - - def clone(self, **kw): - """Return a new instance with specified attributes changed. - - The new instance has the same attribute values as the current object, - except for the changes passed in as keyword arguments. - - """ - newpolicy = self.__class__.__new__(self.__class__) - for attr, value in self.__dict__.items(): - object.__setattr__(newpolicy, attr, value) - for attr, value in kw.items(): - if not hasattr(self, attr): - raise TypeError( - "{!r} is an invalid keyword argument for {}".format( - attr, self.__class__.__name__)) - object.__setattr__(newpolicy, attr, value) - return newpolicy - - def __setattr__(self, name, value): - if hasattr(self, name): - msg = "{!r} object attribute {!r} is read-only" - else: - msg = "{!r} object has no attribute {!r}" - raise AttributeError(msg.format(self.__class__.__name__, name)) - - def __add__(self, other): - """Non-default values from right operand override those from left. - - The object returned is a new instance of the subclass. - - """ - return self.clone(**other.__dict__) - - -def _append_doc(doc, added_doc): - doc = doc.rsplit('\n', 1)[0] - added_doc = added_doc.split('\n', 1)[1] - return doc + '\n' + added_doc - -def _extend_docstrings(cls): - if cls.__doc__ and cls.__doc__.startswith('+'): - cls.__doc__ = _append_doc(cls.__bases__[0].__doc__, cls.__doc__) - for name, attr in cls.__dict__.items(): - if attr.__doc__ and attr.__doc__.startswith('+'): - for c in (c for base in cls.__bases__ for c in base.mro()): - doc = getattr(getattr(c, name), '__doc__') - if doc: - attr.__doc__ = _append_doc(doc, attr.__doc__) - break - return cls - - -class Policy(_PolicyBase, metaclass=abc.ABCMeta): - - r"""Controls for how messages are interpreted and formatted. - - Most of the classes and many of the methods in the email package accept - Policy objects as parameters. A Policy object contains a set of values and - functions that control how input is interpreted and how output is rendered. - For example, the parameter 'raise_on_defect' controls whether or not an RFC - violation results in an error being raised or not, while 'max_line_length' - controls the maximum length of output lines when a Message is serialized. - - Any valid attribute may be overridden when a Policy is created by passing - it as a keyword argument to the constructor. Policy objects are immutable, - but a new Policy object can be created with only certain values changed by - calling the Policy instance with keyword arguments. Policy objects can - also be added, producing a new Policy object in which the non-default - attributes set in the right hand operand overwrite those specified in the - left operand. - - Settable attributes: - - raise_on_defect -- If true, then defects should be raised as errors. - Default: False. - - linesep -- string containing the value to use as separation - between output lines. Default '\n'. - - cte_type -- Type of allowed content transfer encodings - - 7bit -- ASCII only - 8bit -- Content-Transfer-Encoding: 8bit is allowed - - Default: 8bit. Also controls the disposition of - (RFC invalid) binary data in headers; see the - documentation of the binary_fold method. - - max_line_length -- maximum length of lines, excluding 'linesep', - during serialization. None or 0 means no line - wrapping is done. Default is 78. - - """ - - raise_on_defect = False - linesep = '\n' - cte_type = '8bit' - max_line_length = 78 - - def handle_defect(self, obj, defect): - """Based on policy, either raise defect or call register_defect. - - handle_defect(obj, defect) - - defect should be a Defect subclass, but in any case must be an - Exception subclass. obj is the object on which the defect should be - registered if it is not raised. If the raise_on_defect is True, the - defect is raised as an error, otherwise the object and the defect are - passed to register_defect. - - This method is intended to be called by parsers that discover defects. - The email package parsers always call it with Defect instances. - - """ - if self.raise_on_defect: - raise defect - self.register_defect(obj, defect) - - def register_defect(self, obj, defect): - """Record 'defect' on 'obj'. - - Called by handle_defect if raise_on_defect is False. This method is - part of the Policy API so that Policy subclasses can implement custom - defect handling. The default implementation calls the append method of - the defects attribute of obj. The objects used by the email package by - default that get passed to this method will always have a defects - attribute with an append method. - - """ - obj.defects.append(defect) - - def header_max_count(self, name): - """Return the maximum allowed number of headers named 'name'. - - Called when a header is added to a Message object. If the returned - value is not 0 or None, and there are already a number of headers with - the name 'name' equal to the value returned, a ValueError is raised. - - Because the default behavior of Message's __setitem__ is to append the - value to the list of headers, it is easy to create duplicate headers - without realizing it. This method allows certain headers to be limited - in the number of instances of that header that may be added to a - Message programmatically. (The limit is not observed by the parser, - which will faithfully produce as many headers as exist in the message - being parsed.) - - The default implementation returns None for all header names. - """ - return None - - @abc.abstractmethod - def header_source_parse(self, sourcelines): - """Given a list of linesep terminated strings constituting the lines of - a single header, return the (name, value) tuple that should be stored - in the model. The input lines should retain their terminating linesep - characters. The lines passed in by the email package may contain - surrogateescaped binary data. - """ - raise NotImplementedError - - @abc.abstractmethod - def header_store_parse(self, name, value): - """Given the header name and the value provided by the application - program, return the (name, value) that should be stored in the model. - """ - raise NotImplementedError - - @abc.abstractmethod - def header_fetch_parse(self, name, value): - """Given the header name and the value from the model, return the value - to be returned to the application program that is requesting that - header. The value passed in by the email package may contain - surrogateescaped binary data if the lines were parsed by a BytesParser. - The returned value should not contain any surrogateescaped data. - - """ - raise NotImplementedError - - @abc.abstractmethod - def fold(self, name, value): - """Given the header name and the value from the model, return a string - containing linesep characters that implement the folding of the header - according to the policy controls. The value passed in by the email - package may contain surrogateescaped binary data if the lines were - parsed by a BytesParser. The returned value should not contain any - surrogateescaped data. - - """ - raise NotImplementedError - - @abc.abstractmethod - def fold_binary(self, name, value): - """Given the header name and the value from the model, return binary - data containing linesep characters that implement the folding of the - header according to the policy controls. The value passed in by the - email package may contain surrogateescaped binary data. - - """ - raise NotImplementedError - - -@_extend_docstrings -class Compat32(Policy): - - """+ - This particular policy is the backward compatibility Policy. It - replicates the behavior of the email package version 5.1. - """ - - def _sanitize_header(self, name, value): - # If the header value contains surrogates, return a Header using - # the unknown-8bit charset to encode the bytes as encoded words. - if not isinstance(value, str): - # Assume it is already a header object - return value - if _has_surrogates(value): - return header.Header(value, charset=_charset.UNKNOWN8BIT, - header_name=name) - else: - return value - - def header_source_parse(self, sourcelines): - """+ - The name is parsed as everything up to the ':' and returned unmodified. - The value is determined by stripping leading whitespace off the - remainder of the first line, joining all subsequent lines together, and - stripping any trailing carriage return or linefeed characters. - - """ - name, value = sourcelines[0].split(':', 1) - value = value.lstrip(' \t') + ''.join(sourcelines[1:]) - return (name, value.rstrip('\r\n')) - - def header_store_parse(self, name, value): - """+ - The name and value are returned unmodified. - """ - return (name, value) - - def header_fetch_parse(self, name, value): - """+ - If the value contains binary data, it is converted into a Header object - using the unknown-8bit charset. Otherwise it is returned unmodified. - """ - return self._sanitize_header(name, value) - - def fold(self, name, value): - """+ - Headers are folded using the Header folding algorithm, which preserves - existing line breaks in the value, and wraps each resulting line to the - max_line_length. Non-ASCII binary data are CTE encoded using the - unknown-8bit charset. - - """ - return self._fold(name, value, sanitize=True) - - def fold_binary(self, name, value): - """+ - Headers are folded using the Header folding algorithm, which preserves - existing line breaks in the value, and wraps each resulting line to the - max_line_length. If cte_type is 7bit, non-ascii binary data is CTE - encoded using the unknown-8bit charset. Otherwise the original source - header is used, with its existing line breaks and/or binary data. - - """ - folded = self._fold(name, value, sanitize=self.cte_type=='7bit') - return folded.encode('ascii', 'surrogateescape') - - def _fold(self, name, value, sanitize): - parts = [] - parts.append('%s: ' % name) - if isinstance(value, str): - if _has_surrogates(value): - if sanitize: - h = header.Header(value, - charset=_charset.UNKNOWN8BIT, - header_name=name) - else: - # If we have raw 8bit data in a byte string, we have no idea - # what the encoding is. There is no safe way to split this - # string. If it's ascii-subset, then we could do a normal - # ascii split, but if it's multibyte then we could break the - # string. There's no way to know so the least harm seems to - # be to not split the string and risk it being too long. - parts.append(value) - h = None - else: - h = header.Header(value, header_name=name) - else: - # Assume it is a Header-like object. - h = value - if h is not None: - parts.append(h.encode(linesep=self.linesep, - maxlinelen=self.max_line_length)) - parts.append(self.linesep) - return ''.join(parts) - - -compat32 = Compat32() diff -r 2059910e7d76 -r ac776ef41428 Lib/email/architecture.rst --- a/Lib/email/architecture.rst Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,216 +0,0 @@ -:mod:`email` Package Architecture -================================= - -Overview --------- - -The email package consists of three major components: - - Model - An object structure that represents an email message, and provides an - API for creating, querying, and modifying a message. - - Parser - Takes a sequence of characters or bytes and produces a model of the - email message represented by those characters or bytes. - - Generator - Takes a model and turns it into a sequence of characters or bytes. The - sequence can either be intended for human consumption (a printable - unicode string) or bytes suitable for transmission over the wire. In - the latter case all data is properly encoded using the content transfer - encodings specified by the relevant RFCs. - -Conceptually the package is organized around the model. The model provides both -"external" APIs intended for use by application programs using the library, -and "internal" APIs intended for use by the Parser and Generator components. -This division is intentionally a bit fuzy; the API described by this documentation -is all a public, stable API. This allows for an application with special needs -to implement its own parser and/or generator. - -In addition to the three major functional components, there is a third key -component to the architecture: - - Policy - An object that specifies various behavioral settings and carries - implementations of various behavior-controlling methods. - -The Policy framework provides a simple and convenient way to control the -behavior of the library, making it possible for the library to be used in a -very flexible fashion while leveraging the common code required to parse, -represent, and generate message-like objects. For example, in addition to the -default :rfc:`5322` email message policy, we also have a policy that manages -HTTP headers in a fashion compliant with :rfc:`2616`. Individual policy -controls, such as the maximum line length produced by the generator, can also -be controlled individually to meet specialized application requirements. - - -The Model ---------- - -The message model is implemented by the :class:`~email.message.Message` class. -The model divides a message into the two fundamental parts discussed by the -RFC: the header section and the body. The `Message` object acts as a -pseudo-dictionary of named headers. Its dictionary interface provides -convenient access to individual headers by name. However, all headers are kept -internally in an ordered list, so that the information about the order of the -headers in the original message is preserved. - -The `Message` object also has a `payload` that holds the body. A `payload` can -be one of two things: data, or a list of `Message` objects. The latter is used -to represent a multipart MIME message. Lists can be nested arbitrarily deeply -in order to represent the message, with all terminal leaves having non-list -data payloads. - - -Message Lifecycle ------------------ - -The general lifecyle of a message is: - - Creation - A `Message` object can be created by a Parser, or it can be - instantiated as an empty message by an application. - - Manipulation - The application may examine one or more headers, and/or the - payload, and it may modify one or more headers and/or - the payload. This may be done on the top level `Message` - object, or on any sub-object. - - Finalization - The Model is converted into a unicode or binary stream, - or the model is discarded. - - - -Header Policy Control During Lifecycle --------------------------------------- - -One of the major controls exerted by the Policy is the management of headers -during the `Message` lifecycle. Most applications don't need to be aware of -this. - -A header enters the model in one of two ways: via a Parser, or by being set to -a specific value by an application program after the Model already exists. -Similarly, a header exits the model in one of two ways: by being serialized by -a Generator, or by being retrieved from a Model by an application program. The -Policy object provides hooks for all four of these pathways. - -The model storage for headers is a list of (name, value) tuples. - -The Parser identifies headers during parsing, and passes them to the -:meth:`~email.policy.Policy.header_source_parse` method of the Policy. The -result of that method is the (name, value) tuple to be stored in the model. - -When an application program supplies a header value (for example, through the -`Message` object `__setitem__` interface), the name and the value are passed to -the :meth:`~email.policy.Policy.header_store_parse` method of the Policy, which -returns the (name, value) tuple to be stored in the model. - -When an application program retrieves a header (through any of the dict or list -interfaces of `Message`), the name and value are passed to the -:meth:`~email.policy.Policy.header_fetch_parse` method of the Policy to -obtain the value returned to the application. - -When a Generator requests a header during serialization, the name and value are -passed to the :meth:`~email.policy.Policy.fold` method of the Policy, which -returns a string containing line breaks in the appropriate places. The -:meth:`~email.policy.Policy.cte_type` Policy control determines whether or -not Content Transfer Encoding is performed on the data in the header. There is -also a :meth:`~email.policy.Policy.binary_fold` method for use by generators -that produce binary output, which returns the folded header as binary data, -possibly folded at different places than the corresponding string would be. - - -Handling Binary Data --------------------- - -In an ideal world all message data would conform to the RFCs, meaning that the -parser could decode the message into the idealized unicode message that the -sender originally wrote. In the real world, the email package must also be -able to deal with badly formatted messages, including messages containing -non-ASCII characters that either have no indicated character set or are not -valid characters in the indicated character set. - -Since email messages are *primarily* text data, and operations on message data -are primarily text operations (except for binary payloads of course), the model -stores all text data as unicode strings. Un-decodable binary inside text -data is handled by using the `surrogateescape` error handler of the ASCII -codec. As with the binary filenames the error handler was introduced to -handle, this allows the email package to "carry" the binary data received -during parsing along until the output stage, at which time it is regenerated -in its original form. - -This carried binary data is almost entirely an implementation detail. The one -place where it is visible in the API is in the "internal" API. A Parser must -do the `surrogateescape` encoding of binary input data, and pass that data to -the appropriate Policy method. The "internal" interface used by the Generator -to access header values preserves the `surrogateescaped` bytes. All other -interfaces convert the binary data either back into bytes or into a safe form -(losing information in some cases). - - -Backward Compatibility ----------------------- - -The :class:`~email.policy.Policy.Compat32` Policy provides backward -compatibility with version 5.1 of the email package. It does this via the -following implementation of the four+1 Policy methods described above: - -header_source_parse - Splits the first line on the colon to obtain the name, discards any spaces - after the colon, and joins the remainder of the line with all of the - remaining lines, preserving the linesep characters to obtain the value. - Trailing carriage return and/or linefeed characters are stripped from the - resulting value string. - -header_store_parse - Returns the name and value exactly as received from the application. - -header_fetch_parse - If the value contains any `surrogateescaped` binary data, return the value - as a :class:`~email.header.Header` object, using the character set - `unknown-8bit`. Otherwise just returns the value. - -fold - Uses :class:`~email.header.Header`'s folding to fold headers in the - same way the email5.1 generator did. - -binary_fold - Same as fold, but encodes to 'ascii'. - - -New Algorithm -------------- - -header_source_parse - Same as legacy behavior. - -header_store_parse - Same as legacy behavior. - -header_fetch_parse - If the value is already a header object, returns it. Otherwise, parses the - value using the new parser, and returns the resulting object as the value. - `surrogateescaped` bytes get turned into unicode unknown character code - points. - -fold - Uses the new header folding algorithm, respecting the policy settings. - surrogateescaped bytes are encoded using the ``unknown-8bit`` charset for - ``cte_type=7bit`` or ``8bit``. Returns a string. - - At some point there will also be a ``cte_type=unicode``, and for that - policy fold will serialize the idealized unicode message with RFC-like - folding, converting any surrogateescaped bytes into the unicode - unknown character glyph. - -binary_fold - Uses the new header folding algorithm, respecting the policy settings. - surrogateescaped bytes are encoded using the `unknown-8bit` charset for - ``cte_type=7bit``, and get turned back into bytes for ``cte_type=8bit``. - Returns bytes. - - At some point there will also be a ``cte_type=unicode``, and for that - policy binary_fold will serialize the message according to :rfc:``5335``. diff -r 2059910e7d76 -r ac776ef41428 Lib/email/errors.py --- a/Lib/email/errors.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/email/errors.py Mon May 21 23:01:17 2012 -0400 @@ -5,6 +5,7 @@ """email package exception classes.""" + class MessageError(Exception): """Base class for errors in the email package.""" @@ -29,13 +30,12 @@ """An illegal charset was given.""" + # These are parsing defects which the parser was able to work around. -class MessageDefect(ValueError): +class MessageDefect(Exception): """Base class for a message defect.""" def __init__(self, line=None): - if line is not None: - super().__init__(line) self.line = line class NoBoundaryInMultipartDefect(MessageDefect): @@ -44,64 +44,17 @@ class StartBoundaryNotFoundDefect(MessageDefect): """The claimed start boundary was never found.""" -class CloseBoundaryNotFoundDefect(MessageDefect): - """A start boundary was found, but not the corresponding close boundary.""" - class FirstHeaderLineIsContinuationDefect(MessageDefect): """A message had a continuation line as its first header line.""" class MisplacedEnvelopeHeaderDefect(MessageDefect): """A 'Unix-from' header was found in the middle of a header block.""" -class MissingHeaderBodySeparatorDefect(MessageDefect): - """Found line with no leading whitespace and no colon before blank line.""" -# XXX: backward compatibility, just in case (it was never emitted). -MalformedHeaderDefect = MissingHeaderBodySeparatorDefect +class MalformedHeaderDefect(MessageDefect): + """Found a header that was missing a colon, or was otherwise malformed.""" class MultipartInvariantViolationDefect(MessageDefect): """A message claimed to be a multipart but no subparts were found.""" class InvalidMultipartContentTransferEncodingDefect(MessageDefect): """An invalid content transfer encoding was set on the multipart itself.""" - -class UndecodableBytesDefect(MessageDefect): - """Header contained bytes that could not be decoded""" - -class InvalidBase64PaddingDefect(MessageDefect): - """base64 encoded sequence had an incorrect length""" - -class InvalidBase64CharactersDefect(MessageDefect): - """base64 encoded sequence had characters not in base64 alphabet""" - -# These errors are specific to header parsing. - -class HeaderDefect(MessageDefect): - """Base class for a header defect.""" - - def __init__(self, *args, **kw): - super().__init__(*args, **kw) - -class InvalidHeaderDefect(HeaderDefect): - """Header is not valid, message gives details.""" - -class HeaderMissingRequiredValue(HeaderDefect): - """A header that must have a value had none""" - -class NonPrintableDefect(HeaderDefect): - """ASCII characters outside the ascii-printable range found""" - - def __init__(self, non_printables): - super().__init__(non_printables) - self.non_printables = non_printables - - def __str__(self): - return ("the following ASCII non-printables found in header: " - "{}".format(self.non_printables)) - -class ObsoleteHeaderDefect(HeaderDefect): - """Header uses syntax declared obsolete by RFC 5322""" - -class NonASCIILocalPartDefect(HeaderDefect): - """local_part contains non-ASCII characters""" - # This defect only occurs during unicode parsing, not when - # parsing messages decoded from binary. diff -r 2059910e7d76 -r ac776ef41428 Lib/email/feedparser.py --- a/Lib/email/feedparser.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/email/feedparser.py Mon May 21 23:01:17 2012 -0400 @@ -25,7 +25,7 @@ from email import errors from email import message -from email._policybase import compat32 +from email import policy NLCRE = re.compile('\r\n|\r|\n') NLCRE_bol = re.compile('(\r\n|\r|\n)') @@ -135,7 +135,7 @@ class FeedParser: """A feed-style parser of email.""" - def __init__(self, _factory=message.Message, *, policy=compat32): + def __init__(self, _factory=message.Message, *, policy=policy.default): """_factory is called with no arguments to create a new message obj The policy keyword specifies a policy object that controls a number of @@ -145,12 +145,6 @@ """ self._factory = _factory self.policy = policy - try: - _factory(policy=self.policy) - self._factory_kwds = lambda: {'policy': self.policy} - except TypeError: - # Assume this is an old-style factory - self._factory_kwds = lambda: {} self._input = BufferedSubFile() self._msgstack = [] self._parse = self._parsegen().__next__ @@ -187,7 +181,7 @@ return root def _new_message(self): - msg = self._factory(**self._factory_kwds()) + msg = self._factory() if self._cur and self._cur.get_content_type() == 'multipart/digest': msg.set_default_type('message/rfc822') if self._msgstack: @@ -219,8 +213,6 @@ # (i.e. newline), just throw it away. Otherwise the line is # part of the body so push it back. if not NLCRE.match(line): - defect = errors.MissingHeaderBodySeparatorDefect() - self.policy.handle_defect(self._cur, defect) self._input.unreadline(line) break headers.append(line) @@ -324,7 +316,6 @@ capturing_preamble = True preamble = [] linesep = False - close_boundary_seen = False while True: line = self._input.readline() if line is NeedMoreData: @@ -339,7 +330,6 @@ # the closing boundary, then we need to initialize the # epilogue with the empty string (see below). if mo.group('end'): - close_boundary_seen = True linesep = mo.group('linesep') break # We saw an inter-part boundary. Were we in the preamble? @@ -408,6 +398,7 @@ # We've seen either the EOF or the end boundary. If we're still # capturing the preamble, we never saw the start boundary. Note # that as a defect and store the captured text as the payload. + # Everything from here to the EOF is epilogue. if capturing_preamble: defect = errors.StartBoundaryNotFoundDefect() self.policy.handle_defect(self._cur, defect) @@ -419,15 +410,8 @@ continue self._cur.epilogue = EMPTYSTRING.join(epilogue) return - # If we're not processing the preamble, then we might have seen - # EOF without seeing that end boundary...that is also a defect. - if not close_boundary_seen: - defect = errors.CloseBoundaryNotFoundDefect() - self.policy.handle_defect(self._cur, defect) - return - # Everything from here to the EOF is epilogue. If the end boundary - # ended in a newline, we'll need to make sure the epilogue isn't - # None + # If the end boundary ended in a newline, we'll need to make sure + # the epilogue isn't None if linesep: epilogue = [''] else: @@ -474,7 +458,9 @@ lastvalue.append(line) continue if lastheader: - self._cur.set_raw(*self.policy.header_source_parse(lastvalue)) + # XXX reconsider the joining of folded lines + lhdr = EMPTYSTRING.join(lastvalue)[:-1].rstrip('\r\n') + self._cur[lastheader] = lhdr lastheader, lastvalue = '', [] # Check for envelope header, i.e. unix-from if line.startswith('From '): @@ -498,17 +484,19 @@ self._cur.defects.append(defect) continue # Split the line on the colon separating field name from value. - # There will always be a colon, because if there wasn't the part of - # the parser that calls us would have started parsing the body. i = line.find(':') - assert i>0, "_parse_headers fed line with no : and no leading WS" + if i < 0: + defect = errors.MalformedHeaderDefect(line) + self._cur.defects.append(defect) + continue lastheader = line[:i] - lastvalue = [line] + lastvalue = [line[i+1:].lstrip()] # Done with all the lines, so handle the last header. if lastheader: - self._cur.set_raw(*self.policy.header_source_parse(lastvalue)) + # XXX reconsider the joining of folded lines + self._cur[lastheader] = EMPTYSTRING.join(lastvalue).rstrip('\r\n') - + class BytesFeedParser(FeedParser): """Like FeedParser, but feed accepts bytes.""" diff -r 2059910e7d76 -r ac776ef41428 Lib/email/generator.py --- a/Lib/email/generator.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/email/generator.py Mon May 21 23:01:17 2012 -0400 @@ -13,9 +13,9 @@ import warnings from io import StringIO, BytesIO -from email._policybase import compat32 +from email import policy from email.header import Header -from email.utils import _has_surrogates +from email.message import _has_surrogates import email.charset as _charset UNDERSCORE = '_' @@ -36,7 +36,7 @@ # def __init__(self, outfp, mangle_from_=True, maxheaderlen=None, *, - policy=None): + policy=policy.default): """Create the generator for message flattening. outfp is the output file-like object for writing the message to. It @@ -60,7 +60,8 @@ """ self._fp = outfp self._mangle_from_ = mangle_from_ - self.maxheaderlen = maxheaderlen + self._maxheaderlen = (maxheaderlen if maxheaderlen is not None else + policy.max_line_length) self.policy = policy def write(self, s): @@ -86,40 +87,20 @@ # from the msg, and _encoded_XXX constants for operating on data that # has already been converted (to bytes in the BytesGenerator) and # inserted into a temporary buffer. - policy = msg.policy if self.policy is None else self.policy - if linesep is not None: - policy = policy.clone(linesep=linesep) - if self.maxheaderlen is not None: - policy = policy.clone(max_line_length=self.maxheaderlen) - self._NL = policy.linesep + self._NL = linesep if linesep is not None else self.policy.linesep self._encoded_NL = self._encode(self._NL) self._EMPTY = '' self._encoded_EMTPY = self._encode('') - # Because we use clone (below) when we recursively process message - # subparts, and because clone uses the computed policy (not None), - # submessages will automatically get set to the computed policy when - # they are processed by this code. - old_gen_policy = self.policy - old_msg_policy = msg.policy - try: - self.policy = policy - msg.policy = policy - if unixfrom: - ufrom = msg.get_unixfrom() - if not ufrom: - ufrom = 'From nobody ' + time.ctime(time.time()) - self.write(ufrom + self._NL) - self._write(msg) - finally: - self.policy = old_gen_policy - msg.policy = old_msg_policy + if unixfrom: + ufrom = msg.get_unixfrom() + if not ufrom: + ufrom = 'From nobody ' + time.ctime(time.time()) + self.write(ufrom + self._NL) + self._write(msg) def clone(self, fp): """Clone this generator with the exact same options.""" - return self.__class__(fp, - self._mangle_from_, - None, # Use policy setting, which we've adjusted - policy=self.policy) + return self.__class__(fp, self._mangle_from_, self._maxheaderlen) # # Protected interface - undocumented ;/ @@ -194,8 +175,16 @@ # def _write_headers(self, msg): - for h, v in msg.raw_items(): - self.write(self.policy.fold(h, v)) + for h, v in msg.items(): + self.write('%s: ' % h) + if isinstance(v, Header): + self.write(v.encode( + maxlinelen=self._maxheaderlen, linesep=self._NL)+self._NL) + else: + # Header's got lots of smarts, so use it. + header = Header(v, maxlinelen=self._maxheaderlen, + header_name=h) + self.write(header.encode(linesep=self._NL)+self._NL) # A blank line always separates headers from body self.write(self._NL) @@ -276,12 +265,12 @@ # The contents of signed parts has to stay unmodified in order to keep # the signature intact per RFC1847 2.1, so we disable header wrapping. # RDM: This isn't enough to completely preserve the part, but it helps. - p = self.policy - self.policy = p.clone(max_line_length=0) + old_maxheaderlen = self._maxheaderlen try: + self._maxheaderlen = 0 self._handle_multipart(msg) finally: - self.policy = p + self._maxheaderlen = old_maxheaderlen def _handle_message_delivery_status(self, msg): # We can't just write the headers directly to self's file object @@ -358,9 +347,9 @@ Functionally identical to the base Generator except that the output is bytes and not string. When surrogates were used in the input to encode bytes, these are decoded back to bytes for output. If the policy has - cte_type set to 7bit, then the message is transformed such that the - non-ASCII bytes are properly content transfer encoded, using the charset - unknown-8bit. + must_be_7bit set true, then the message is transformed such that the + non-ASCII bytes are properly content transfer encoded, using the + charset unknown-8bit. The outfp object must accept bytes in its write method. """ @@ -381,8 +370,27 @@ def _write_headers(self, msg): # This is almost the same as the string version, except for handling # strings with 8bit bytes. - for h, v in msg.raw_items(): - self._fp.write(self.policy.fold_binary(h, v)) + for h, v in msg._headers: + self.write('%s: ' % h) + if isinstance(v, str): + if _has_surrogates(v): + if not self.policy.must_be_7bit: + # If we have raw 8bit data in a byte string, we have no idea + # what the encoding is. There is no safe way to split this + # string. If it's ascii-subset, then we could do a normal + # ascii split, but if it's multibyte then we could break the + # string. There's no way to know so the least harm seems to + # be to not split the string and risk it being too long. + self.write(v+NL) + continue + h = Header(v, charset=_charset.UNKNOWN8BIT, header_name=h) + else: + h = Header(v, header_name=h) + else: + # Assume it is a Header-like object. + h = v + self.write(h.encode(linesep=self._NL, + maxlinelen=self._maxheaderlen)+self._NL) # A blank line always separates headers from body self.write(self._NL) @@ -391,7 +399,7 @@ # just write it back out. if msg._payload is None: return - if _has_surrogates(msg._payload) and not self.policy.cte_type=='7bit': + if _has_surrogates(msg._payload) and not self.policy.must_be_7bit: self.write(msg._payload) else: super(BytesGenerator,self)._handle_text(msg) diff -r 2059910e7d76 -r ac776ef41428 Lib/email/header.py --- a/Lib/email/header.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/email/header.py Mon May 21 23:01:17 2012 -0400 @@ -40,6 +40,7 @@ \? # literal ? (?P.*?) # non-greedy up to the next ?= is the encoded string \?= # literal ?= + (?=[ \t]|$) # whitespace or the end of the string ''', re.VERBOSE | re.IGNORECASE | re.MULTILINE) # Field name regexp, including trailing colon, but not separating whitespace, @@ -85,12 +86,8 @@ words = [] for line in header.splitlines(): parts = ecre.split(line) - first = True while parts: - unencoded = parts.pop(0) - if first: - unencoded = unencoded.lstrip() - first = False + unencoded = parts.pop(0).strip() if unencoded: words.append((unencoded, None, None)) if parts: @@ -98,16 +95,6 @@ encoding = parts.pop(0).lower() encoded = parts.pop(0) words.append((encoded, encoding, charset)) - # Now loop over words and remove words that consist of whitespace - # between two encoded strings. - import sys - droplist = [] - for n, w in enumerate(words): - if n>1 and w[1] and words[n-2][1] and words[n-1][0].isspace(): - droplist.append(n-1) - for d in reversed(droplist): - del words[d] - # The next step is to decode each encoded word by applying the reverse # base64 or quopri transformation. decoded_words is now a list of the # form (decoded_word, charset). @@ -230,27 +217,22 @@ self._normalize() uchunks = [] lastcs = None - lastspace = None for string, charset in self._chunks: # We must preserve spaces between encoded and non-encoded word # boundaries, which means for us we need to add a space when we go # from a charset to None/us-ascii, or from None/us-ascii to a # charset. Only do this for the second and subsequent chunks. - # Don't add a space if the None/us-ascii string already has - # a space (trailing or leading depending on transition) nextcs = charset if nextcs == _charset.UNKNOWN8BIT: original_bytes = string.encode('ascii', 'surrogateescape') string = original_bytes.decode('ascii', 'replace') if uchunks: - hasspace = string and self._nonctext(string[0]) if lastcs not in (None, 'us-ascii'): - if nextcs in (None, 'us-ascii') and not hasspace: + if nextcs in (None, 'us-ascii'): uchunks.append(SPACE) nextcs = None - elif nextcs not in (None, 'us-ascii') and not lastspace: + elif nextcs not in (None, 'us-ascii'): uchunks.append(SPACE) - lastspace = string and self._nonctext(string[-1]) lastcs = nextcs uchunks.append(string) return EMPTYSTRING.join(uchunks) @@ -309,11 +291,6 @@ charset = UTF8 self._chunks.append((s, charset)) - def _nonctext(self, s): - """True if string s is not a ctext character of RFC822. - """ - return s.isspace() or s in ('(', ')', '\\') - def encode(self, splitchars=';, \t', maxlinelen=None, linesep='\n'): r"""Encode a message header into an RFC-compliant format. @@ -357,20 +334,7 @@ maxlinelen = 1000000 formatter = _ValueFormatter(self._headerlen, maxlinelen, self._continuation_ws, splitchars) - lastcs = None - hasspace = lastspace = None for string, charset in self._chunks: - if hasspace is not None: - hasspace = string and self._nonctext(string[0]) - import sys - if lastcs not in (None, 'us-ascii'): - if not hasspace or charset not in (None, 'us-ascii'): - formatter.add_transition() - elif charset not in (None, 'us-ascii') and not lastspace: - formatter.add_transition() - lastspace = string and self._nonctext(string[-1]) - lastcs = charset - hasspace = False lines = string.splitlines() if lines: formatter.feed('', lines[0], charset) @@ -387,7 +351,6 @@ formatter.feed(fws, sline, charset) if len(lines) > 1: formatter.newline() - if self._chunks: formatter.add_transition() value = formatter._str(linesep) if _embeded_header.search(value): diff -r 2059910e7d76 -r ac776ef41428 Lib/email/headerregistry.py --- a/Lib/email/headerregistry.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -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 -r 2059910e7d76 -r ac776ef41428 Lib/email/message.py --- a/Lib/email/message.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/email/message.py Mon May 21 23:01:17 2012 -0400 @@ -10,14 +10,14 @@ import uu import base64 import binascii +import warnings from io import BytesIO, StringIO # Intrapackage imports from email import utils from email import errors -from email._policybase import compat32 +from email import header from email import charset as _charset -from email._encoded_words import decode_b Charset = _charset.Charset SEMISPACE = '; ' @@ -26,6 +26,24 @@ # existence of which force quoting of the parameter value. tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]') +# How to figure out if we are processing strings that come from a byte +# source with undecodable characters. +_has_surrogates = re.compile( + '([^\ud800-\udbff]|\A)[\udc00-\udfff]([^\udc00-\udfff]|\Z)').search + + +# Helper functions +def _sanitize_header(name, value): + # If the header value contains surrogates, return a Header using + # the unknown-8bit charset to encode the bytes as encoded words. + if not isinstance(value, str): + # Assume it is already a header object + return value + if _has_surrogates(value): + return header.Header(value, charset=_charset.UNKNOWN8BIT, + header_name=name) + else: + return value def _splitparam(param): # Split header parameters. BAW: this may be too simple. It isn't @@ -118,8 +136,7 @@ you must use the explicit API to set or get all the headers. Not all of the mapping methods are implemented. """ - def __init__(self, policy=compat32): - self.policy = policy + def __init__(self): self._headers = [] self._unixfrom = None self._payload = None @@ -229,7 +246,7 @@ cte = str(self.get('content-transfer-encoding', '')).lower() # payload may be bytes here. if isinstance(payload, str): - if utils._has_surrogates(payload): + if _has_surrogates(payload): bpayload = payload.encode('ascii', 'surrogateescape') if not decode: try: @@ -250,12 +267,11 @@ if cte == 'quoted-printable': return utils._qdecode(bpayload) elif cte == 'base64': - # XXX: this is a bit of a hack; decode_b should probably be factored - # out somewhere, but I haven't figured out where yet. - value, defects = decode_b(b''.join(bpayload.splitlines())) - for defect in defects: - self.policy.handle_defect(self, defect) - return value + try: + return base64.b64decode(bpayload) + except binascii.Error: + # Incorrect padding + return bpayload elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'): in_file = BytesIO(bpayload) out_file = BytesIO() @@ -346,17 +362,7 @@ Note: this does not overwrite an existing header with the same field name. Use __delitem__() first to delete any existing headers. """ - max_count = self.policy.header_max_count(name) - if max_count: - lname = name.lower() - found = 0 - for k, v in self._headers: - if k.lower() == lname: - found += 1 - if found >= max_count: - raise ValueError("There may be at most {} {} headers " - "in a message".format(max_count, name)) - self._headers.append(self.policy.header_store_parse(name, val)) + self._headers.append((name, val)) def __delitem__(self, name): """Delete all occurrences of a header, if present. @@ -395,8 +401,7 @@ Any fields deleted and re-inserted are always appended to the header list. """ - return [self.policy.header_fetch_parse(k, v) - for k, v in self._headers] + return [_sanitize_header(k, v) for k, v in self._headers] def items(self): """Get all the message's header fields and values. @@ -406,8 +411,7 @@ Any fields deleted and re-inserted are always appended to the header list. """ - return [(k, self.policy.header_fetch_parse(k, v)) - for k, v in self._headers] + return [(k, _sanitize_header(k, v)) for k, v in self._headers] def get(self, name, failobj=None): """Get a header value. @@ -418,29 +422,10 @@ name = name.lower() for k, v in self._headers: if k.lower() == name: - return self.policy.header_fetch_parse(k, v) + return _sanitize_header(k, v) return failobj # - # "Internal" methods (public API, but only intended for use by a parser - # or generator, not normal application code. - # - - def set_raw(self, name, value): - """Store name and value in the model without modification. - - This is an "internal" API, intended only for use by a parser. - """ - self._headers.append((name, value)) - - def raw_items(self): - """Return the (name, value) header pairs without modification. - - This is an "internal" API, intended only for use by a generator. - """ - return iter(self._headers.copy()) - - # # Additional useful stuff # @@ -457,7 +442,7 @@ name = name.lower() for k, v in self._headers: if k.lower() == name: - values.append(self.policy.header_fetch_parse(k, v)) + values.append(_sanitize_header(k, v)) if not values: return failobj return values @@ -490,7 +475,7 @@ parts.append(_formatparam(k.replace('_', '-'), v)) if _value is not None: parts.insert(0, _value) - self[_name] = SEMISPACE.join(parts) + self._headers.append((_name, SEMISPACE.join(parts))) def replace_header(self, _name, _value): """Replace a header. @@ -502,7 +487,7 @@ _name = _name.lower() for i, (k, v) in zip(range(len(self._headers)), self._headers): if k.lower() == _name: - self._headers[i] = self.policy.header_store_parse(k, _value) + self._headers[i] = (k, _value) break else: raise KeyError(_name) @@ -820,8 +805,7 @@ parts.append(k) else: parts.append('%s=%s' % (k, v)) - val = SEMISPACE.join(parts) - newheaders.append(self.policy.header_store_parse(h, val)) + newheaders.append((h, SEMISPACE.join(parts))) else: newheaders.append((h, v)) diff -r 2059910e7d76 -r ac776ef41428 Lib/email/parser.py --- a/Lib/email/parser.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/email/parser.py Mon May 21 23:01:17 2012 -0400 @@ -11,12 +11,12 @@ from email.feedparser import FeedParser from email.message import Message -from email._policybase import compat32 +from email import policy class Parser: - def __init__(self, _class=Message, *, policy=compat32): + def __init__(self, _class=Message, *, policy=policy.default): """Parser of RFC 2822 and MIME email messages. Creates an in-memory object tree representing the email message, which diff -r 2059910e7d76 -r ac776ef41428 Lib/email/policy.py --- a/Lib/email/policy.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/email/policy.py Mon May 21 23:01:17 2012 -0400 @@ -1,188 +1,174 @@ -"""This will be the home for the policy that hooks in the new -code that adds all the email6 features. +"""Policy framework for the email package. + +Allows fine grained feature control of how the package parses and emits data. """ -from email._policybase import Policy, Compat32, compat32, _extend_docstrings -from email.utils import _has_surrogates -from email.headerregistry import HeaderRegistry as HeaderRegistry - __all__ = [ - 'Compat32', - 'compat32', 'Policy', - 'EmailPolicy', 'default', 'strict', 'SMTP', 'HTTP', ] -@_extend_docstrings -class EmailPolicy(Policy): - """+ - PROVISIONAL +class _PolicyBase: - The API extensions enabled by this this policy are currently provisional. - Refer to the documentation for details. + """Policy Object basic framework. - This policy adds new header parsing and folding algorithms. Instead of - simple strings, headers are custom objects with custom attributes - depending on the type of the field. The folding algorithm fully - implements RFCs 2047 and 5322. + This class is useless unless subclassed. A subclass should define + class attributes with defaults for any values that are to be + managed by the Policy object. The constructor will then allow + non-default values to be set for these attributes at instance + creation time. The instance will be callable, taking these same + attributes keyword arguments, and returning a new instance + identical to the called instance except for those values changed + by the keyword arguments. Instances may be added, yielding new + instances with any non-default values from the right hand + operand overriding those in the left hand operand. That is, - In addition to the settable attributes listed above that apply to - all Policies, this policy adds the following additional attributes: + A + B == A() - refold_source -- if the value for a header in the Message object - came from the parsing of some source, this attribute - indicates whether or not a generator should refold - that value when transforming the message back into - stream form. The possible values are: + The repr of an instance can be used to reconstruct the object + if and only if the repr of the values can be used to reconstruct + those values. - none -- all source values use original folding - long -- source values that have any line that is - longer than max_line_length will be - refolded - all -- all values are refolded. - - The default is 'long'. - - header_factory -- 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 RFC5322 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.) """ - refold_source = 'long' - header_factory = HeaderRegistry() + def __init__(self, **kw): + """Create new Policy, possibly overriding some defaults. - 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()) - super().__init__(**kw) - - def header_max_count(self, name): - """+ - The implementation for this class returns the max_count attribute from - the specialized header class that would be used to construct a header - of type 'name'. - """ - return self.header_factory[name].max_count - - # The logic of the next three methods is chosen such that it is possible to - # switch a Message object between a Compat32 policy and a policy derived - # from this class and have the results stay consistent. This allows a - # Message object constructed with this policy to be passed to a library - # that only handles Compat32 objects, or to receive such an object and - # convert it to use the newer style by just changing its policy. It is - # also chosen because it postpones the relatively expensive full rfc5322 - # parse until as late as possible when parsing from source, since in many - # applications only a few headers will actually be inspected. - - def header_source_parse(self, sourcelines): - """+ - The name is parsed as everything up to the ':' and returned unmodified. - The value is determined by stripping leading whitespace off the - remainder of the first line, joining all subsequent lines together, and - stripping any trailing carriage return or linefeed characters. (This - is the same as Compat32). + See class docstring for a list of overridable attributes. """ - name, value = sourcelines[0].split(':', 1) - value = value.lstrip(' \t') + ''.join(sourcelines[1:]) - return (name, value.rstrip('\r\n')) + for name, value in kw.items(): + if hasattr(self, name): + super(_PolicyBase,self).__setattr__(name, value) + else: + raise TypeError( + "{!r} is an invalid keyword argument for {}".format( + name, self.__class__.__name__)) - def header_store_parse(self, name, value): - """+ - The name is returned unchanged. If the input value has a 'name' - attribute and it matches the name ignoring case, the value is returned - unchanged. Otherwise the name and value are passed to header_factory - method, and the resulting custom header object is returned as the - value. In this case a ValueError is raised if the input value contains - CR or LF characters. + def __repr__(self): + args = [ "{}={!r}".format(name, value) + for name, value in self.__dict__.items() ] + return "{}({})".format(self.__class__.__name__, ', '.join(args)) + + def clone(self, **kw): + """Return a new instance with specified attributes changed. + + The new instance has the same attribute values as the current object, + except for the changes passed in as keyword arguments. """ - if hasattr(value, 'name') and value.name.lower() == name.lower(): - return (name, value) - if isinstance(value, str) and len(value.splitlines())>1: - raise ValueError("Header values may not contain linefeed " - "or carriage return characters") - return (name, self.header_factory(name, value)) + for attr, value in self.__dict__.items(): + if attr not in kw: + kw[attr] = value + return self.__class__(**kw) - def header_fetch_parse(self, name, value): - """+ - If the value has a 'name' attribute, it is returned to unmodified. - Otherwise the name and the value with any linesep characters removed - are passed to the header_factory method, and the resulting custom - header object is returned. Any surrogateescaped bytes get turned - into the unicode unknown-character glyph. + def __setattr__(self, name, value): + if hasattr(self, name): + msg = "{!r} object attribute {!r} is read-only" + else: + msg = "{!r} object has no attribute {!r}" + raise AttributeError(msg.format(self.__class__.__name__, name)) + + def __add__(self, other): + """Non-default values from right operand override those from left. + + The object returned is a new instance of the subclass. """ - if hasattr(value, 'name'): - return value - return self.header_factory(name, ''.join(value.splitlines())) + return self.clone(**other.__dict__) - def fold(self, name, value): - """+ - Header folding is controlled by the refold_source policy setting. A - value is considered to be a 'source value' if and only if it does not - have a 'name' attribute (having a 'name' attribute means it is a header - object of some sort). If a source value needs to be refolded according - to the policy, it is converted into a custom header object by passing - the name and the value with any linesep characters removed to the - header_factory method. Folding of a custom header object is done by - calling its fold method with the current policy. - Source values are split into lines using splitlines. If the value is - not to be refolded, the lines are rejoined using the linesep from the - policy and returned. The exception is lines containing non-ascii - binary data. In that case the value is refolded regardless of the - refold_source setting, which causes the binary data to be CTE encoded - using the unknown-8bit charset. +class Policy(_PolicyBase): + + """Controls for how messages are interpreted and formatted. + + Most of the classes and many of the methods in the email package + accept Policy objects as parameters. A Policy object contains a set + of values and functions that control how input is interpreted and how + output is rendered. For example, the parameter 'raise_on_defect' + controls whether or not an RFC violation throws an error or not, + while 'max_line_length' controls the maximum length of output lines + when a Message is serialized. + + Any valid attribute may be overridden when a Policy is created by + passing it as a keyword argument to the constructor. Policy + objects are immutable, but a new Policy object can be created + with only certain values changed by calling the Policy instance + with keyword arguments. Policy objects can also be added, + producing a new Policy object in which the non-default attributes + set in the right hand operand overwrite those specified in the + left operand. + + Settable attributes: + + raise_on_defect -- If true, then defects should be raised + as errors. Default False. + + linesep -- string containing the value to use as + separation between output lines. Default '\n'. + + must_be_7bit -- output must contain only 7bit clean data. + Default False. + + max_line_length -- maximum length of lines, excluding 'linesep', + during serialization. None means no line + wrapping is done. Default is 78. + + Methods: + + register_defect(obj, defect) + defect is a Defect instance. The default implementation appends defect + to the objs 'defects' attribute. + + handle_defect(obj, defect) + intended to be called by parser code that finds a defect. If + raise_on_defect is True, defect is raised as an error, otherwise + register_defect is called. + + """ + + raise_on_defect = False + linesep = '\n' + must_be_7bit = False + max_line_length = 78 + + def handle_defect(self, obj, defect): + """Based on policy, either raise defect or call register_defect. + + handle_defect(obj, defect) + + defect should be a Defect subclass, but in any case must be an + Exception subclass. obj is the object on which the defect should be + registered if it is not raised. If the raise_on_defect is True, the + defect is raised as an error, otherwise the object and the defect are + passed to register_defect. + + This class is intended to be called by parsers that discover defects, + and will not be called from code using the library unless that code is + implementing an alternate parser. """ - return self._fold(name, value, refold_binary=True) + if self.raise_on_defect: + raise defect + self.register_defect(obj, defect) - def fold_binary(self, name, value): - """+ - The same as fold if cte_type is 7bit, except that the returned value is - bytes. + def register_defect(self, obj, defect): + """Record 'defect' on 'obj'. - If cte_type is 8bit, non-ASCII binary data is converted back into - bytes. Headers with binary data are not refolded, regardless of the - refold_header setting, since there is no way to know whether the binary - data consists of single byte characters or multibyte characters. + Called by handle_defect if raise_on_defect is False. This method is + part of the Policy API so that Policy subclasses can implement custom + defect handling. The default implementation calls the append method + of the defects attribute of obj. """ - folded = self._fold(name, value, refold_binary=self.cte_type=='7bit') - return folded.encode('ascii', 'surrogateescape') + obj.defects.append(defect) - def _fold(self, name, value, refold_binary=False): - if hasattr(value, 'name'): - return value.fold(policy=self) - maxlen = self.max_line_length if self.max_line_length else float('inf') - lines = value.splitlines() - refold = (self.refold_source == 'all' or - self.refold_source == 'long' and - (len(lines[0])+len(name)+2 > maxlen or - any(len(x) > maxlen for x in lines[1:]))) - if refold or refold_binary and _has_surrogates(value): - return self.header_factory(name, ''.join(lines)).fold(policy=self) - return name + ': ' + self.linesep.join(lines) + self.linesep - -default = EmailPolicy() -# Make the default policy use the class default header_factory -del default.header_factory +default = Policy() strict = default.clone(raise_on_defect=True) SMTP = default.clone(linesep='\r\n') HTTP = default.clone(linesep='\r\n', max_line_length=None) diff -r 2059910e7d76 -r ac776ef41428 Lib/email/utils.py --- a/Lib/email/utils.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/email/utils.py Mon May 21 23:01:17 2012 -0400 @@ -57,17 +57,6 @@ specialsre = re.compile(r'[][\\()<>@,:;".]') escapesre = re.compile(r'[\\"]') -# How to figure out if we are processing strings that come from a byte -# source with undecodable characters. -_has_surrogates = re.compile( - '([^\ud800-\udbff]|\A)[\udc00-\udfff]([^\udc00-\udfff]|\Z)').search - -# How to deal with a string containing bytes before handing it to the -# application through the 'normal' interface. -def _sanitize(string): - # Turn any escaped bytes into unicode 'unknown' char. - original_bytes = string.encode('ascii', 'surrogateescape') - return original_bytes.decode('ascii', 'replace') # Helpers @@ -363,56 +352,3 @@ except LookupError: # charset is not a known codec. return unquote(text) - - -# -# datetime doesn't provide a localtime function yet, so provide one. Code -# adapted from the patch in issue 9527. This may not be perfect, but it is -# better than not having it. -# - -def localtime(dt=None, isdst=-1): - """Return local time as an aware datetime object. - - If called without arguments, return current time. Otherwise *dt* - argument should be a datetime instance, and it is converted to the - local time zone according to the system time zone database. If *dt* is - naive (that is, dt.tzinfo is None), it is assumed to be in local time. - In this case, a positive or zero value for *isdst* causes localtime to - presume initially that summer time (for example, Daylight Saving Time) - is or is not (respectively) in effect for the specified time. A - negative value for *isdst* causes the localtime() function to attempt - to divine whether summer time is in effect for the specified time. - - """ - if dt is None: - seconds = time.time() - else: - if dt.tzinfo is None: - # A naive datetime is given. Convert to a (localtime) - # timetuple and pass to system mktime together with - # the isdst hint. System mktime will return seconds - # sysce epoch. - tm = dt.timetuple()[:-1] + (isdst,) - seconds = time.mktime(tm) - else: - # An aware datetime is given. Use aware datetime - # arithmetics to find seconds since epoch. - delta = dt - datetime.datetime(1970, 1, 1, - tzinfo=datetime.timezone.utc) - seconds = delta.total_seconds() - tm = time.localtime(seconds) - - # XXX: The following logic may not work correctly if UTC - # offset has changed since time provided in dt. This will be - # corrected in C implementation for platforms that support - # tm_gmtoff. - if time.daylight and tm.tm_isdst: - offset = time.altzone - tzname = time.tzname[1] - else: - offset = time.timezone - tzname = time.tzname[0] - - tz = datetime.timezone(datetime.timedelta(seconds=-offset), tzname) - return datetime.datetime.fromtimestamp(seconds, tz) diff -r 2059910e7d76 -r ac776ef41428 Lib/functools.py --- a/Lib/functools.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/functools.py Mon May 21 23:01:17 2012 -0400 @@ -142,36 +142,31 @@ _CacheInfo = namedtuple("CacheInfo", ["hits", "misses", "maxsize", "currsize"]) -class _HashedSeq(list): +class _CacheKey(list): + 'Make a cache key from optionally typed positional and keyword arguments' + __slots__ = 'hashvalue' - def __init__(self, tup, hash=hash): - self[:] = tup - self.hashvalue = hash(tup) + def __init__(self, args, kwds, typed, + kwd_mark = (object(),), + sorted=sorted, tuple=tuple, type=type, hash=hash): + key = args + if kwds: + sorted_items = sorted(kwds.items()) + key += kwd_mark + for item in sorted_items: + key += item + if typed: + key += tuple(type(v) for v in args) + if kwds: + key += tuple(type(v) for k, v in sorted_items) + self[:] = key + self.hashvalue = hash(key) # so we only have to hash just once def __hash__(self): return self.hashvalue -def _make_key(args, kwds, typed, - kwd_mark = (object(),), - fasttypes = {int, str, frozenset, type(None)}, - sorted=sorted, tuple=tuple, type=type, len=len): - 'Make a cache key from optionally typed positional and keyword arguments' - key = args - if kwds: - sorted_items = sorted(kwds.items()) - key += kwd_mark - for item in sorted_items: - key += item - if typed: - key += tuple(type(v) for v in args) - if kwds: - key += tuple(type(v) for k, v in sorted_items) - elif len(key) == 1 and type(key[0]) in fasttypes: - return key[0] - return _HashedSeq(key) - -def lru_cache(maxsize=128, typed=False): +def lru_cache(maxsize=100, typed=False): """Least-recently-used cache decorator. If *maxsize* is set to None, the LRU features are disabled and the cache @@ -198,7 +193,7 @@ # Constants shared by all lru cache instances: sentinel = object() # unique object used to signal cache misses - make_key = _make_key # build a key from the function arguments + make_key = _CacheKey # build a key from the function arguments PREV, NEXT, KEY, RESULT = 0, 1, 2, 3 # names for the link fields def decorating_function(user_function): diff -r 2059910e7d76 -r ac776ef41428 Lib/gettext.py --- a/Lib/gettext.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/gettext.py Mon May 21 23:01:17 2012 -0400 @@ -55,7 +55,7 @@ 'dgettext', 'dngettext', 'gettext', 'ngettext', ] -_default_localedir = os.path.join(sys.base_prefix, 'share', 'locale') +_default_localedir = os.path.join(sys.prefix, 'share', 'locale') def c2py(plural): diff -r 2059910e7d76 -r ac776ef41428 Lib/gzip.py --- a/Lib/gzip.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/gzip.py Mon May 21 23:01:17 2012 -0400 @@ -20,9 +20,6 @@ encoding=None, errors=None, newline=None): """Open a gzip-compressed file in binary or text mode. - The filename argument can be an actual filename (a str or bytes object), or - an existing file object to read from or write to. - The mode argument can be "r", "rb", "w", "wb", "a" or "ab" for binary mode, or "rt", "wt" or "at" for text mode. The default mode is "rb", and the default compresslevel is 9. @@ -46,15 +43,7 @@ raise ValueError("Argument 'errors' not supported in binary mode") if newline is not None: raise ValueError("Argument 'newline' not supported in binary mode") - - gz_mode = mode.replace("t", "") - if isinstance(filename, (str, bytes)): - binary_file = GzipFile(filename, gz_mode, compresslevel) - elif hasattr(filename, "read") or hasattr(filename, "write"): - binary_file = GzipFile(None, gz_mode, compresslevel, filename) - else: - raise TypeError("filename must be a str or bytes object, or a file") - + binary_file = GzipFile(filename, mode.replace("t", ""), compresslevel) if "t" in mode: return io.TextIOWrapper(binary_file, encoding, errors, newline) else: diff -r 2059910e7d76 -r ac776ef41428 Lib/http/client.py --- a/Lib/http/client.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/http/client.py Mon May 21 23:01:17 2012 -0400 @@ -1076,7 +1076,7 @@ self.putrequest(method, url, **skips) - if body is not None and ('content-length' not in header_names): + if body and ('content-length' not in header_names): self._set_content_length(body) for hdr, value in headers.items(): self.putheader(hdr, value) diff -r 2059910e7d76 -r ac776ef41428 Lib/http/cookies.py --- a/Lib/http/cookies.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/http/cookies.py Mon May 21 23:01:17 2012 -0400 @@ -301,7 +301,7 @@ from time import gmtime, time now = time() year, month, day, hh, mm, ss, wd, y, z = gmtime(now + future) - return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % \ + return "%s, %02d-%3s-%4d %02d:%02d:%02d GMT" % \ (weekdayname[wd], day, monthname[month], year, hh, mm, ss) @@ -439,7 +439,7 @@ (?P # Start of group 'val' "(?:[^\\"]|\\.)*" # Any doublequoted string | # or - \w{3},\s[\w\d\s-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr + \w{3},\s[\w\d-]{9,11}\s[\d:]{8}\sGMT # Special case for "expires" attr | # or """ + _LegalCharsPatt + r"""* # Any word or empty string ) # End of group 'val' diff -r 2059910e7d76 -r ac776ef41428 Lib/http/server.py --- a/Lib/http/server.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/http/server.py Mon May 21 23:01:17 2012 -0400 @@ -100,8 +100,6 @@ import time import urllib.parse import copy -import argparse - # Default error message template DEFAULT_ERROR_MESSAGE = """\ @@ -1175,13 +1173,18 @@ def test(HandlerClass = BaseHTTPRequestHandler, - ServerClass = HTTPServer, protocol="HTTP/1.0", port=8000): + ServerClass = HTTPServer, protocol="HTTP/1.0"): """Test the HTTP request handler class. This runs an HTTP server on port 8000 (or the first command line argument). """ + + if sys.argv[1:]: + port = int(sys.argv[1]) + else: + port = 8000 server_address = ('', port) HandlerClass.protocol_version = protocol @@ -1197,15 +1200,4 @@ sys.exit(0) if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.add_argument('--cgi', action='store_true', - help='Run as CGI Server') - parser.add_argument('port', action='store', - default=8000, type=int, - nargs='?', - help='Specify alternate port [default: 8000]') - args = parser.parse_args() - if args.cgi: - test(HandlerClass=CGIHTTPRequestHandler, port=args.port) - else: - test(HandlerClass=SimpleHTTPRequestHandler, port=args.port) + test(HandlerClass=SimpleHTTPRequestHandler) diff -r 2059910e7d76 -r ac776ef41428 Lib/idlelib/AutoComplete.py --- a/Lib/idlelib/AutoComplete.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/idlelib/AutoComplete.py Mon May 21 23:01:17 2012 -0400 @@ -9,6 +9,9 @@ from idlelib.configHandler import idleConf +# This string includes all chars that may be in a file name (without a path +# separator) +FILENAME_CHARS = string.ascii_letters + string.digits + os.curdir + "._~#$:-" # This string includes all chars that may be in an identifier ID_CHARS = string.ascii_letters + string.digits + "_" @@ -121,26 +124,19 @@ curline = self.text.get("insert linestart", "insert") i = j = len(curline) if hp.is_in_string() and (not mode or mode==COMPLETE_FILES): - # Find the beginning of the string - # fetch_completions will look at the file system to determine whether the - # string value constitutes an actual file name - # XXX could consider raw strings here and unescape the string value if it's - # not raw. self._remove_autocomplete_window() mode = COMPLETE_FILES - # Find last separator or string start - while i and curline[i-1] not in "'\"" + SEPS: + while i and curline[i-1] in FILENAME_CHARS: i -= 1 comp_start = curline[i:j] j = i - # Find string start - while i and curline[i-1] not in "'\"": + while i and curline[i-1] in FILENAME_CHARS + SEPS: i -= 1 comp_what = curline[i:j] elif hp.is_in_code() and (not mode or mode==COMPLETE_ATTRIBUTES): self._remove_autocomplete_window() mode = COMPLETE_ATTRIBUTES - while i and curline[i-1] in ID_CHARS or ord(curline[i-1]) > 127: + while i and curline[i-1] in ID_CHARS: i -= 1 comp_start = curline[i:j] if i and curline[i-1] == '.': diff -r 2059910e7d76 -r ac776ef41428 Lib/idlelib/AutoCompleteWindow.py --- a/Lib/idlelib/AutoCompleteWindow.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/idlelib/AutoCompleteWindow.py Mon May 21 23:01:17 2012 -0400 @@ -354,15 +354,6 @@ # A modifier key, so ignore return - elif event.char and event.char >= ' ': - # Regular character with a non-length-1 keycode - self._change_start(self.start + event.char) - self.lasttypedstart = self.start - self.listbox.select_clear(0, int(self.listbox.curselection()[0])) - self.listbox.select_set(self._binary_search(self.start)) - self._selection_changed() - return "break" - else: # Unknown event, close the window and let it through. self.hide_window() diff -r 2059910e7d76 -r ac776ef41428 Lib/idlelib/CallTips.py --- a/Lib/idlelib/CallTips.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/idlelib/CallTips.py Mon May 21 23:01:17 2012 -0400 @@ -67,18 +67,18 @@ if not sur_paren: return hp.set_index(sur_paren[0]) - expression = hp.get_expression() - if not expression: + name = hp.get_expression() + if not name: return - if not evalfuncs and (expression.find('(') != -1): + if not evalfuncs and (name.find('(') != -1): return - argspec = self.fetch_tip(expression) + argspec = self.fetch_tip(name) if not argspec: return self.active_calltip = self._calltip_window() self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1]) - def fetch_tip(self, expression): + def fetch_tip(self, name): """Return the argument list and docstring of a function or class. If there is a Python subprocess, get the calltip there. Otherwise, @@ -94,59 +94,53 @@ """ try: rpcclt = self.editwin.flist.pyshell.interp.rpcclt - except AttributeError: + except: rpcclt = None if rpcclt: return rpcclt.remotecall("exec", "get_the_calltip", - (expression,), {}) + (name,), {}) else: - return get_argspec(get_entity(expression)) + entity = self.get_entity(name) + return get_argspec(entity) -def get_entity(expression): - """Return the object corresponding to expression evaluated - in a namespace spanning sys.modules and __main.dict__. - """ - if expression: - namespace = sys.modules.copy() - namespace.update(__main__.__dict__) - try: - return eval(expression, namespace) - except BaseException: - # An uncaught exception closes idle, and eval can raise any - # exception, especially if user classes are involved. - return None + def get_entity(self, name): + "Lookup name in a namespace spanning sys.modules and __main.dict__." + if name: + namespace = sys.modules.copy() + namespace.update(__main__.__dict__) + try: + return eval(name, namespace) + except (NameError, AttributeError): + return None -# The following are used in both get_argspec and tests -_self_pat = re.compile('self\,?\s*') -_default_callable_argspec = "No docstring, see docs." +def _find_constructor(class_ob): + "Find the nearest __init__() in the class tree." + try: + return class_ob.__init__.__func__ + except AttributeError: + for base in class_ob.__bases__: + init = _find_constructor(base) + if init: + return init + return None def get_argspec(ob): - '''Return a string describing the arguments and return of a callable object. - - For Python-coded functions and methods, the first line is introspected. - Delete 'self' parameter for classes (.__init__) and bound methods. - The last line is the first line of the doc string. For builtins, this typically - includes the arguments in addition to the return value. - - ''' + """Get a string describing the arguments for the given object.""" argspec = "" - if hasattr(ob, '__call__'): + if ob is not None: if isinstance(ob, type): - fob = getattr(ob, '__init__', None) - elif isinstance(ob.__call__, types.MethodType): - fob = ob.__call__ + fob = _find_constructor(ob) + if fob is None: + fob = lambda: None + elif isinstance(ob, types.MethodType): + fob = ob.__func__ else: fob = ob - if isinstance(fob, (types.FunctionType, types.MethodType)): + if isinstance(fob, (types.FunctionType, types.LambdaType)): argspec = inspect.formatargspec(*inspect.getfullargspec(fob)) - if (isinstance(ob, (type, types.MethodType)) or - isinstance(ob.__call__, types.MethodType)): - argspec = _self_pat.sub("", argspec) - - if isinstance(ob.__call__, types.MethodType): - doc = ob.__call__.__doc__ - else: - doc = getattr(ob, "__doc__", "") + pat = re.compile('self\,?\s*') + argspec = pat.sub("", argspec) + doc = getattr(ob, "__doc__", "") if doc: doc = doc.lstrip() pos = doc.find("\n") @@ -155,16 +149,13 @@ if argspec: argspec += "\n" argspec += doc[:pos] - if not argspec: - argspec = _default_callable_argspec return argspec ################################################# # -# Test code tests CallTips.fetch_tip, get_entity, and get_argspec - +# Test code +# def main(): - # Putting expected in docstrings results in doubled tips for test def t1(): "()" def t2(a, b=None): "(a, b=None)" def t3(a, *args): "(a, *args)" @@ -174,88 +165,39 @@ class TC(object): "(ai=None, *b)" - def __init__(self, ai=None, *b): "(self, ai=None, *b)" - def t1(self): "(self)" - def t2(self, ai, b=None): "(self, ai, b=None)" - def t3(self, ai, *args): "(self, ai, *args)" - def t4(self, *args): "(self, *args)" - def t5(self, ai, *args): "(self, ai, *args)" - def t6(self, ai, b=None, *args, **kw): "(self, ai, b=None, *args, **kw)" - @classmethod - def cm(cls, a): "(cls, a)" - @staticmethod - def sm(b): "(b)" - def __call__(self, ci): "(ci)" + def __init__(self, ai=None, *b): "(ai=None, *b)" + def t1(self): "()" + def t2(self, ai, b=None): "(ai, b=None)" + def t3(self, ai, *args): "(ai, *args)" + def t4(self, *args): "(*args)" + def t5(self, ai, *args): "(ai, *args)" + def t6(self, ai, b=None, *args, **kw): "(ai, b=None, *args, **kw)" + + __main__.__dict__.update(locals()) + + def test(tests): + ct = CallTips() + failed=[] + for t in tests: + expected = t.__doc__ + "\n" + t.__doc__ + name = t.__name__ + # exercise fetch_tip(), not just get_argspec() + try: + qualified_name = "%s.%s" % (t.__self__.__class__.__name__, name) + except AttributeError: + qualified_name = name + argspec = ct.fetch_tip(qualified_name) + if argspec != expected: + failed.append(t) + fmt = "%s - expected %s, but got %s" + print(fmt % (t.__name__, expected, get_argspec(t))) + print("%d of %d tests failed" % (len(failed), len(tests))) tc = TC() + tests = (t1, t2, t3, t4, t5, t6, + TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6) - # Python classes that inherit builtin methods - class Int(int): "Int(x[, base]) -> integer" - class List(list): "List() -> new empty list" - # Simulate builtin with no docstring for default argspec test - class SB: __call__ = None - - __main__.__dict__.update(locals()) # required for get_entity eval() - - num_tests = num_fail = 0 - tip = CallTips().fetch_tip - - def test(expression, expected): - nonlocal num_tests, num_fail - num_tests += 1 - argspec = tip(expression) - if argspec != expected: - num_fail += 1 - fmt = "%s - expected\n%r\n - but got\n%r" - print(fmt % (expression, expected, argspec)) - - def test_builtins(): - # if first line of a possibly multiline compiled docstring changes, - # must change corresponding test string - test('int', "int(x[, base]) -> integer") - test('Int', Int.__doc__) - test('types.MethodType', "method(function, instance)") - test('list', "list() -> new empty list") - test('List', List.__doc__) - test('list.__new__', - 'T.__new__(S, ...) -> a new object with type S, a subtype of T') - test('list.__init__', - 'x.__init__(...) initializes x; see help(type(x)) for signature') - append_doc = "L.append(object) -> None -- append object to end" - test('list.append', append_doc) - test('[].append', append_doc) - test('List.append', append_doc) - test('SB()', _default_callable_argspec) - - def test_funcs(): - for func in (t1, t2, t3, t4, t5, t6, TC,): - fdoc = func.__doc__ - test(func.__name__, fdoc + "\n" + fdoc) - for func in (TC.t1, TC.t2, TC.t3, TC.t4, TC.t5, TC.t6, TC.cm, TC.sm): - fdoc = func.__doc__ - test('TC.'+func.__name__, fdoc + "\n" + fdoc) - - def test_methods(): - for func in (tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6): - fdoc = func.__doc__ - test('tc.'+func.__name__, _self_pat.sub("", fdoc) + "\n" + fdoc) - fdoc = tc.__call__.__doc__ - test('tc', fdoc + "\n" + fdoc) - - def test_non_callables(): - # expression evaluates, but not to a callable - for expr in ('0', '0.0' 'num_tests', b'num_tests', '[]', '{}'): - test(expr, '') - # expression does not evaluate, but raises an exception - for expr in ('1a', 'xyx', 'num_tests.xyz', '[int][1]', '{0:int}[1]'): - test(expr, '') - - test_builtins() - test_funcs() - test_non_callables() - test_methods() - - print("%d of %d tests failed" % (num_fail, num_tests)) + test(tests) if __name__ == '__main__': main() diff -r 2059910e7d76 -r ac776ef41428 Lib/idlelib/ColorDelegator.py --- a/Lib/idlelib/ColorDelegator.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/idlelib/ColorDelegator.py Mon May 21 23:01:17 2012 -0400 @@ -21,11 +21,10 @@ # 1st 'file' colorized normal, 2nd as builtin, 3rd as string builtin = r"([^.'\"\\#]\b|^)" + any("BUILTIN", builtinlist) + r"\b" comment = any("COMMENT", [r"#[^\n]*"]) - stringprefix = r"(\br|u|ur|R|U|UR|Ur|uR|b|B|br|Br|bR|BR|rb|rB|Rb|RB)?" - sqstring = stringprefix + r"'[^'\\\n]*(\\.[^'\\\n]*)*'?" - dqstring = stringprefix + r'"[^"\\\n]*(\\.[^"\\\n]*)*"?' - sq3string = stringprefix + r"'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?" - dq3string = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?' + sqstring = r"(\b[rRbB])?'[^'\\\n]*(\\.[^'\\\n]*)*'?" + dqstring = r'(\b[rRbB])?"[^"\\\n]*(\\.[^"\\\n]*)*"?' + sq3string = r"(\b[rRbB])?'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?" + dq3string = r'(\b[rRbB])?"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?' string = any("STRING", [sq3string, dq3string, sqstring, dqstring]) return kw + "|" + builtin + "|" + comment + "|" + string +\ "|" + any("SYNC", [r"\n"]) diff -r 2059910e7d76 -r ac776ef41428 Lib/idlelib/EditorWindow.py --- a/Lib/idlelib/EditorWindow.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/idlelib/EditorWindow.py Mon May 21 23:01:17 2012 -0400 @@ -120,7 +120,7 @@ def __init__(self, flist=None, filename=None, key=None, root=None): if EditorWindow.help_url is None: - dochome = os.path.join(sys.base_prefix, 'Doc', 'index.html') + dochome = os.path.join(sys.prefix, 'Doc', 'index.html') if sys.platform.count('linux'): # look for html docs in a couple of standard places pyver = 'python-docs-' + '%s.%s.%s' % sys.version_info[:3] @@ -131,13 +131,13 @@ dochome = os.path.join(basepath, pyver, 'Doc', 'index.html') elif sys.platform[:3] == 'win': - chmfile = os.path.join(sys.base_prefix, 'Doc', + chmfile = os.path.join(sys.prefix, 'Doc', 'Python%s.chm' % _sphinx_version()) if os.path.isfile(chmfile): dochome = chmfile elif macosxSupport.runningAsOSXApp(): # documentation is stored inside the python framework - dochome = os.path.join(sys.base_prefix, + dochome = os.path.join(sys.prefix, 'Resources/English.lproj/Documentation/index.html') dochome = os.path.normpath(dochome) if os.path.isfile(dochome): @@ -860,7 +860,7 @@ # for each edit window instance, construct the recent files menu for instance in self.top.instance_dict: menu = instance.recent_files_menu - menu.delete(0, END) # clear, and rebuild: + menu.delete(1, END) # clear, and rebuild: for i, file_name in enumerate(rf_list): file_name = file_name.rstrip() # zap \n # make unicode string to display non-ASCII chars correctly diff -r 2059910e7d76 -r ac776ef41428 Lib/idlelib/GrepDialog.py --- a/Lib/idlelib/GrepDialog.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/idlelib/GrepDialog.py Mon May 21 23:01:17 2012 -0400 @@ -81,7 +81,7 @@ hits = 0 for fn in list: try: - f = open(fn, errors='replace') + f = open(fn) except IOError as msg: print(msg) continue diff -r 2059910e7d76 -r ac776ef41428 Lib/idlelib/IOBinding.py --- a/Lib/idlelib/IOBinding.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/idlelib/IOBinding.py Mon May 21 23:01:17 2012 -0400 @@ -156,33 +156,29 @@ self.filename_change_hook() def open(self, event=None, editFile=None): - flist = self.editwin.flist - # Save in case parent window is closed (ie, during askopenfile()). - if flist: + if self.editwin.flist: if not editFile: filename = self.askopenfile() else: filename=editFile if filename: - # If editFile is valid and already open, flist.open will - # shift focus to its existing window. - # If the current window exists and is a fresh unnamed, - # unmodified editor window (not an interpreter shell), - # pass self.loadfile to flist.open so it will load the file - # in the current window (if the file is not already open) - # instead of a new window. - if (self.editwin and - not getattr(self.editwin, 'interp', None) and - not self.filename and - self.get_saved()): - flist.open(filename, self.loadfile) + # If the current window has no filename and hasn't been + # modified, we replace its contents (no loss). Otherwise + # we open a new window. But we won't replace the + # shell window (which has an interp(reter) attribute), which + # gets set to "not modified" at every new prompt. + try: + interp = self.editwin.interp + except AttributeError: + interp = None + if not self.filename and self.get_saved() and not interp: + self.editwin.flist.open(filename, self.loadfile) else: - flist.open(filename) + self.editwin.flist.open(filename) else: - if self.text: - self.text.focus_set() + self.text.focus_set() return "break" - + # # Code for use outside IDLE: if self.get_saved(): reply = self.maybesave() diff -r 2059910e7d76 -r ac776ef41428 Lib/idlelib/NEWS.txt --- a/Lib/idlelib/NEWS.txt Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/idlelib/NEWS.txt Mon May 21 23:01:17 2012 -0400 @@ -1,17 +1,6 @@ -What's New in IDLE 3.3.0? +What's New in IDLE 3.3? ========================= -- Issue # 12510: Attempt to get certain tool tips no longer crashes IDLE. - Erroneous tool tips have been corrected. Default added for callables. - -- Issue10365: File open dialog now works instead of crashing even when - parent window is closed while dialog is open. - -- Issue 14876: use user-selected font for highlight configuration. - -- Issue #14937: Perform auto-completion of filenames in strings even for - non-ASCII filenames. Likewise for identifiers. - - Issue #8515: Set __file__ when run file in IDLE. Initial patch by Bruce Frederiksen. diff -r 2059910e7d76 -r ac776ef41428 Lib/idlelib/PyShell.py --- a/Lib/idlelib/PyShell.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/idlelib/PyShell.py Mon May 21 23:01:17 2012 -0400 @@ -309,11 +309,6 @@ "console": idleConf.GetHighlight(theme, "console"), }) - def removecolors(self): - # Don't remove shell color tags before "iomark" - for tag in self.tagdefs: - self.tag_remove(tag, "iomark", "end") - class ModifiedUndoDelegator(UndoDelegator): "Extend base class: forbid insert/delete before the I/O mark" @@ -1456,8 +1451,7 @@ if tkversionwarning: shell.interp.runcommand(''.join(("print('", tkversionwarning, "')"))) - while flist.inversedict: # keep IDLE running while files are open. - root.mainloop() + root.mainloop() root.destroy() if __name__ == "__main__": diff -r 2059910e7d76 -r ac776ef41428 Lib/idlelib/configDialog.py --- a/Lib/idlelib/configDialog.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/idlelib/configDialog.py Mon May 21 23:01:17 2012 -0400 @@ -187,7 +187,7 @@ text=' Highlighting Theme ') #frameCustom self.textHighlightSample=Text(frameCustom,relief=SOLID,borderwidth=1, - font=('courier',12,''),cursor='hand2',width=21,height=11, + font=('courier',12,''),cursor='hand2',width=21,height=10, takefocus=FALSE,highlightthickness=0,wrap=NONE) text=self.textHighlightSample text.bind('',lambda e: 'break') @@ -821,10 +821,8 @@ fontWeight=tkFont.BOLD else: fontWeight=tkFont.NORMAL - size=self.fontSize.get() - self.editFont.config(size=size, + self.editFont.config(size=self.fontSize.get(), weight=fontWeight,family=fontName) - self.textHighlightSample.configure(font=(fontName, size, fontWeight)) def SetHighlightTarget(self): if self.highlightTarget.get()=='Cursor': #bg not possible diff -r 2059910e7d76 -r ac776ef41428 Lib/idlelib/idlever.py --- a/Lib/idlelib/idlever.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/idlelib/idlever.py Mon May 21 23:01:17 2012 -0400 @@ -1,1 +1,1 @@ -IDLE_VERSION = "3.3.0a4" +IDLE_VERSION = "3.3.0a3" diff -r 2059910e7d76 -r ac776ef41428 Lib/importlib/_bootstrap.py --- a/Lib/importlib/_bootstrap.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/importlib/_bootstrap.py Mon May 21 23:01:17 2012 -0400 @@ -159,145 +159,6 @@ return type(_io)(name) -# Module-level locking ######################################################## - -# A dict mapping module names to weakrefs of _ModuleLock instances -_module_locks = {} -# A dict mapping thread ids to _ModuleLock instances -_blocking_on = {} - - -class _DeadlockError(RuntimeError): - pass - - -class _ModuleLock: - """A recursive lock implementation which is able to detect deadlocks - (e.g. thread 1 trying to take locks A then B, and thread 2 trying to - take locks B then A). - """ - - def __init__(self, name): - self.lock = _thread.allocate_lock() - self.wakeup = _thread.allocate_lock() - self.name = name - self.owner = None - self.count = 0 - self.waiters = 0 - - def has_deadlock(self): - # Deadlock avoidance for concurrent circular imports. - me = _thread.get_ident() - tid = self.owner - while True: - lock = _blocking_on.get(tid) - if lock is None: - return False - tid = lock.owner - if tid == me: - return True - - def acquire(self): - """ - Acquire the module lock. If a potential deadlock is detected, - a _DeadlockError is raised. - Otherwise, the lock is always acquired and True is returned. - """ - tid = _thread.get_ident() - _blocking_on[tid] = self - try: - while True: - with self.lock: - if self.count == 0 or self.owner == tid: - self.owner = tid - self.count += 1 - return True - if self.has_deadlock(): - raise _DeadlockError("deadlock detected by %r" % self) - if self.wakeup.acquire(False): - self.waiters += 1 - # Wait for a release() call - self.wakeup.acquire() - self.wakeup.release() - finally: - del _blocking_on[tid] - - def release(self): - tid = _thread.get_ident() - with self.lock: - if self.owner != tid: - raise RuntimeError("cannot release un-acquired lock") - assert self.count > 0 - self.count -= 1 - if self.count == 0: - self.owner = None - if self.waiters: - self.waiters -= 1 - self.wakeup.release() - - def __repr__(self): - return "_ModuleLock(%r) at %d" % (self.name, id(self)) - - -class _DummyModuleLock: - """A simple _ModuleLock equivalent for Python builds without - multi-threading support.""" - - def __init__(self, name): - self.name = name - self.count = 0 - - def acquire(self): - self.count += 1 - return True - - def release(self): - if self.count == 0: - raise RuntimeError("cannot release un-acquired lock") - self.count -= 1 - - def __repr__(self): - return "_DummyModuleLock(%r) at %d" % (self.name, id(self)) - - -# The following two functions are for consumption by Python/import.c. - -def _get_module_lock(name): - """Get or create the module lock for a given module name. - - Should only be called with the import lock taken.""" - lock = None - if name in _module_locks: - lock = _module_locks[name]() - if lock is None: - if _thread is None: - lock = _DummyModuleLock(name) - else: - lock = _ModuleLock(name) - def cb(_): - del _module_locks[name] - _module_locks[name] = _weakref.ref(lock, cb) - return lock - -def _lock_unlock_module(name): - """Release the global import lock, and acquires then release the - module lock for a given module name. - This is used to ensure a module is completely initialized, in the - event it is being imported by another thread. - - Should only be called with the import lock taken.""" - lock = _get_module_lock(name) - _imp.release_lock() - try: - lock.acquire() - except _DeadlockError: - # Concurrent circular import, we'll accept a partially initialized - # module object. - pass - else: - lock.release() - - # Finder/loader utility code ################################################## _PYCACHE = '__pycache__' @@ -403,15 +264,12 @@ else: module.__package__ = fullname.rpartition('.')[0] try: - module.__initializing__ = True # If __package__ was not set above, __import__() will do it later. return fxn(self, module, *args, **kwargs) except: if not is_reload: del sys.modules[fullname] raise - finally: - module.__initializing__ = False _wrap(module_for_loader_wrapper, fxn) return module_for_loader_wrapper @@ -468,10 +326,6 @@ """ @classmethod - def module_repr(cls, module): - return "".format(module.__name__) - - @classmethod def find_module(cls, fullname, path=None): """Find the built-in module. @@ -525,10 +379,6 @@ """ @classmethod - def module_repr(cls, m): - return "".format(m.__name__) - - @classmethod def find_module(cls, fullname, path=None): """Find a frozen module.""" return cls if _imp.is_frozen(fullname) else None @@ -541,10 +391,7 @@ """Load a frozen module.""" is_reload = fullname in sys.modules try: - m = _imp.init_frozen(fullname) - # Let our own module_repr() method produce a suitable repr. - del m.__file__ - return m + return _imp.init_frozen(fullname) except: if not is_reload and fullname in sys.modules: del sys.modules[fullname] @@ -771,9 +618,7 @@ @_check_name def load_module(self, fullname): """Load a module from a file.""" - # Issue #14857: Avoid the zero-argument form so the implementation - # of that form can be updated without breaking the frozen module - return super(FileLoader, self).load_module(fullname) + return super().load_module(fullname) @_check_name def get_filename(self, fullname): @@ -888,77 +733,6 @@ return None -class _NamespacePath: - """Represents a namespace package's path. It uses the module name - to find its parent module, and from there it looks up the parent's - __path__. When this changes, the module's own path is recomputed, - using path_finder. For top-leve modules, the parent module's path - is sys.path.""" - - def __init__(self, name, path, path_finder): - self._name = name - self._path = path - self._last_parent_path = tuple(self._get_parent_path()) - self._path_finder = path_finder - - def _find_parent_path_names(self): - """Returns a tuple of (parent-module-name, parent-path-attr-name)""" - parent, dot, me = self._name.rpartition('.') - if dot == '': - # This is a top-level module. sys.path contains the parent path. - return 'sys', 'path' - # Not a top-level module. parent-module.__path__ contains the - # parent path. - return parent, '__path__' - - def _get_parent_path(self): - parent_module_name, path_attr_name = self._find_parent_path_names() - return getattr(sys.modules[parent_module_name], path_attr_name) - - def _recalculate(self): - # If the parent's path has changed, recalculate _path - parent_path = tuple(self._get_parent_path()) # Make a copy - if parent_path != self._last_parent_path: - loader, new_path = self._path_finder(self._name, parent_path) - # Note that no changes are made if a loader is returned, but we - # do remember the new parent path - if loader is None: - self._path = new_path - self._last_parent_path = parent_path # Save the copy - return self._path - - def __iter__(self): - return iter(self._recalculate()) - - def __len__(self): - return len(self._recalculate()) - - def __repr__(self): - return "_NamespacePath({0!r})".format(self._path) - - def __contains__(self, item): - return item in self._recalculate() - - def append(self, item): - self._path.append(item) - - -class NamespaceLoader: - def __init__(self, name, path, path_finder): - self._path = _NamespacePath(name, path, path_finder) - - @classmethod - def module_repr(cls, module): - return "".format(module.__name__) - - @module_for_loader - def load_module(self, module): - """Load a namespace module.""" - _verbose_message('namespace module loaded with path {!r}', self._path) - module.__path__ = self._path - return module - - # Finders ##################################################################### class PathFinder: @@ -1000,46 +774,19 @@ return finder @classmethod - def _get_loader(cls, fullname, path): - """Find the loader or namespace_path for this module/package name.""" - # If this ends up being a namespace package, namespace_path is - # the list of paths that will become its __path__ - namespace_path = [] - for entry in path: - finder = cls._path_importer_cache(entry) - if finder is not None: - if hasattr(finder, 'find_loader'): - loader, portions = finder.find_loader(fullname) - else: - loader = finder.find_module(fullname) - portions = [] - if loader is not None: - # We found a loader: return it immediately. - return (loader, namespace_path) - # This is possibly part of a namespace package. - # Remember these path entries (if any) for when we - # create a namespace package, and continue iterating - # on path. - namespace_path.extend(portions) - else: - return (None, namespace_path) - - @classmethod def find_module(cls, fullname, path=None): """Find the module on sys.path or 'path' based on sys.path_hooks and sys.path_importer_cache.""" if path is None: path = sys.path - loader, namespace_path = cls._get_loader(fullname, path) - if loader is not None: - return loader + for entry in path: + finder = cls._path_importer_cache(entry) + if finder is not None: + loader = finder.find_module(fullname) + if loader: + return loader else: - if namespace_path: - # We found at least one namespace path. Return a - # loader which can create the namespace package. - return NamespaceLoader(fullname, namespace_path, cls._get_loader) - else: - return None + return None class FileFinder: @@ -1053,8 +800,8 @@ def __init__(self, path, *details): """Initialize with the path to search on and a variable number of - 3-tuples containing the loader, file suffixes the loader recognizes, - and a boolean of whether the loader handles packages.""" + 3-tuples containing the loader, file suffixes the loader recognizes, and + a boolean of whether the loader handles packages.""" packages = [] modules = [] for loader, suffixes, supports_packages in details: @@ -1075,19 +822,6 @@ def find_module(self, fullname): """Try to find a loader for the specified module.""" - # Call find_loader(). If it returns a string (indicating this - # is a namespace package portion), generate a warning and - # return None. - loader, portions = self.find_loader(fullname) - assert len(portions) in [0, 1] - if loader is None and len(portions): - msg = "Not importing directory {}: missing __init__" - _warnings.warn(msg.format(portions[0]), ImportWarning) - return loader - - def find_loader(self, fullname): - """Try to find a loader for the specified module, or the namespace - package portions. Returns (loader, list-of-portions).""" tail_module = fullname.rpartition('.')[2] try: mtime = _os.stat(self.path).st_mtime @@ -1111,17 +845,17 @@ init_filename = '__init__' + suffix full_path = _path_join(base_path, init_filename) if _path_isfile(full_path): - return (loader(fullname, full_path), [base_path]) + return loader(fullname, full_path) else: - # A namespace package, return the path - return (None, [base_path]) + msg = "Not importing directory {}: missing __init__" + _warnings.warn(msg.format(base_path), ImportWarning) # Check for a file w/ a proper suffix exists. for suffix, loader in self.modules: if cache_module + suffix in cache: full_path = _path_join(self.path, tail_module + suffix) if _path_isfile(full_path): - return (loader(fullname, full_path), []) - return (None, []) + return loader(fullname, full_path) + return None def _fill_cache(self): """Fill the cache of potential modules and packages for this directory.""" @@ -1198,8 +932,7 @@ if not sys.meta_path: _warnings.warn('sys.meta_path is empty', ImportWarning) for finder in sys.meta_path: - with _ImportLockContext(): - loader = finder.find_module(name, path) + loader = finder.find_module(name, path) if loader is not None: # The parent import may have already imported this module. if name not in sys.modules: @@ -1229,7 +962,8 @@ _ERR_MSG = 'No module named {!r}' -def _find_and_load_unlocked(name, import_): +def _find_and_load(name, import_): + """Find and load the module.""" path = None parent = name.rpartition('.')[0] if parent: @@ -1275,19 +1009,6 @@ return module -def _find_and_load(name, import_): - """Find and load the module, and release the import lock.""" - try: - lock = _get_module_lock(name) - finally: - _imp.release_lock() - lock.acquire() - try: - return _find_and_load_unlocked(name, import_) - finally: - lock.release() - - def _gcd_import(name, package=None, level=0): """Import and return the module based on its name, the package the call is being made from, and the level adjustment. @@ -1300,17 +1021,17 @@ _sanity_check(name, package, level) if level > 0: name = _resolve_name(name, package, level) - _imp.acquire_lock() - if name not in sys.modules: + with _ImportLockContext(): + try: + module = sys.modules[name] + if module is None: + message = ("import of {} halted; " + "None in sys.modules".format(name)) + raise ImportError(message, name=name) + return module + except KeyError: + pass # Don't want to chain the exception return _find_and_load(name, _gcd_import) - module = sys.modules[name] - if module is None: - _imp.release_lock() - message = ("import of {} halted; " - "None in sys.modules".format(name)) - raise ImportError(message, name=name) - _lock_unlock_module(name) - return module def _handle_fromlist(module, fromlist, import_): @@ -1428,17 +1149,7 @@ continue else: raise ImportError('importlib requires posix or nt') - - try: - thread_module = BuiltinImporter.load_module('_thread') - except ImportError: - # Python was built without threads - thread_module = None - weakref_module = BuiltinImporter.load_module('_weakref') - setattr(self_module, '_os', os_module) - setattr(self_module, '_thread', thread_module) - setattr(self_module, '_weakref', weakref_module) setattr(self_module, 'path_sep', path_sep) setattr(self_module, 'path_separators', set(path_separators)) # Constants diff -r 2059910e7d76 -r ac776ef41428 Lib/importlib/test/frozen/test_loader.py --- a/Lib/importlib/test/frozen/test_loader.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/importlib/test/frozen/test_loader.py Mon May 21 23:01:17 2012 -0400 @@ -10,46 +10,38 @@ def test_module(self): with util.uncache('__hello__'), captured_stdout() as stdout: module = machinery.FrozenImporter.load_module('__hello__') - check = {'__name__': '__hello__', - '__package__': '', - '__loader__': machinery.FrozenImporter, - } + check = {'__name__': '__hello__', '__file__': '', + '__package__': '', '__loader__': machinery.FrozenImporter} for attr, value in check.items(): self.assertEqual(getattr(module, attr), value) self.assertEqual(stdout.getvalue(), 'Hello world!\n') - self.assertFalse(hasattr(module, '__file__')) def test_package(self): with util.uncache('__phello__'), captured_stdout() as stdout: module = machinery.FrozenImporter.load_module('__phello__') - check = {'__name__': '__phello__', - '__package__': '__phello__', - '__path__': ['__phello__'], - '__loader__': machinery.FrozenImporter, - } + check = {'__name__': '__phello__', '__file__': '', + '__package__': '__phello__', '__path__': ['__phello__'], + '__loader__': machinery.FrozenImporter} for attr, value in check.items(): attr_value = getattr(module, attr) self.assertEqual(attr_value, value, "for __phello__.%s, %r != %r" % (attr, attr_value, value)) self.assertEqual(stdout.getvalue(), 'Hello world!\n') - self.assertFalse(hasattr(module, '__file__')) def test_lacking_parent(self): with util.uncache('__phello__', '__phello__.spam'), \ captured_stdout() as stdout: module = machinery.FrozenImporter.load_module('__phello__.spam') - check = {'__name__': '__phello__.spam', + check = {'__name__': '__phello__.spam', '__file__': '', '__package__': '__phello__', - '__loader__': machinery.FrozenImporter, - } + '__loader__': machinery.FrozenImporter} for attr, value in check.items(): attr_value = getattr(module, attr) self.assertEqual(attr_value, value, "for __phello__.spam.%s, %r != %r" % (attr, attr_value, value)) self.assertEqual(stdout.getvalue(), 'Hello world!\n') - self.assertFalse(hasattr(module, '__file__')) def test_module_reuse(self): with util.uncache('__hello__'), captured_stdout() as stdout: @@ -59,12 +51,6 @@ self.assertEqual(stdout.getvalue(), 'Hello world!\nHello world!\n') - def test_module_repr(self): - with util.uncache('__hello__'), captured_stdout(): - module = machinery.FrozenImporter.load_module('__hello__') - self.assertEqual(repr(module), - "") - def test_state_after_failure(self): # No way to trigger an error in a frozen module. pass diff -r 2059910e7d76 -r ac776ef41428 Lib/importlib/test/source/test_finder.py --- a/Lib/importlib/test/source/test_finder.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/importlib/test/source/test_finder.py Mon May 21 23:01:17 2012 -0400 @@ -106,17 +106,36 @@ loader = self.import_(pkg_dir, 'pkg.sub') self.assertTrue(hasattr(loader, 'load_module')) + # [sub empty] + def test_empty_sub_directory(self): + context = source_util.create_modules('pkg.__init__', 'pkg.sub.__init__') + with warnings.catch_warnings(): + warnings.simplefilter("error", ImportWarning) + with context as mapping: + os.unlink(mapping['pkg.sub.__init__']) + pkg_dir = os.path.dirname(mapping['pkg.__init__']) + with self.assertRaises(ImportWarning): + self.import_(pkg_dir, 'pkg.sub') + # [package over modules] def test_package_over_module(self): name = '_temp' loader = self.run_test(name, {'{0}.__init__'.format(name), name}) self.assertTrue('__init__' in loader.get_filename(name)) + def test_failure(self): with source_util.create_modules('blah') as mapping: nothing = self.import_(mapping['.root'], 'sdfsadsadf') self.assertTrue(nothing is None) + # [empty dir] + def test_empty_dir(self): + with warnings.catch_warnings(): + warnings.simplefilter("error", ImportWarning) + with self.assertRaises(ImportWarning): + self.run_test('pkg', {'pkg.__init__'}, unlink={'pkg.__init__'}) + def test_empty_string_for_dir(self): # The empty string from sys.path means to search in the cwd. finder = machinery.FileFinder('', (machinery.SourceFileLoader, diff -r 2059910e7d76 -r ac776ef41428 Lib/importlib/test/test_locks.py --- a/Lib/importlib/test/test_locks.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,115 +0,0 @@ -from importlib import _bootstrap -import time -import unittest -import weakref - -from test import support - -try: - import threading -except ImportError: - threading = None -else: - from test import lock_tests - - -LockType = _bootstrap._ModuleLock -DeadlockError = _bootstrap._DeadlockError - - -if threading is not None: - class ModuleLockAsRLockTests(lock_tests.RLockTests): - locktype = staticmethod(lambda: LockType("some_lock")) - - # _is_owned() unsupported - test__is_owned = None - # acquire(blocking=False) unsupported - test_try_acquire = None - test_try_acquire_contended = None - # `with` unsupported - test_with = None - # acquire(timeout=...) unsupported - test_timeout = None - # _release_save() unsupported - test_release_save_unacquired = None - -else: - class ModuleLockAsRLockTests(unittest.TestCase): - pass - - -@unittest.skipUnless(threading, "threads needed for this test") -class DeadlockAvoidanceTests(unittest.TestCase): - - def run_deadlock_avoidance_test(self, create_deadlock): - NLOCKS = 10 - locks = [LockType(str(i)) for i in range(NLOCKS)] - pairs = [(locks[i], locks[(i+1)%NLOCKS]) for i in range(NLOCKS)] - if create_deadlock: - NTHREADS = NLOCKS - else: - NTHREADS = NLOCKS - 1 - barrier = threading.Barrier(NTHREADS) - results = [] - def _acquire(lock): - """Try to acquire the lock. Return True on success, False on deadlock.""" - try: - lock.acquire() - except DeadlockError: - return False - else: - return True - def f(): - a, b = pairs.pop() - ra = _acquire(a) - barrier.wait() - rb = _acquire(b) - results.append((ra, rb)) - if rb: - b.release() - if ra: - a.release() - lock_tests.Bunch(f, NTHREADS).wait_for_finished() - self.assertEqual(len(results), NTHREADS) - return results - - def test_deadlock(self): - results = self.run_deadlock_avoidance_test(True) - # One of the threads detected a potential deadlock on its second - # acquire() call. - self.assertEqual(results.count((True, False)), 1) - self.assertEqual(results.count((True, True)), len(results) - 1) - - def test_no_deadlock(self): - results = self.run_deadlock_avoidance_test(False) - self.assertEqual(results.count((True, False)), 0) - self.assertEqual(results.count((True, True)), len(results)) - - -class LifetimeTests(unittest.TestCase): - - def test_lock_lifetime(self): - name = "xyzzy" - self.assertNotIn(name, _bootstrap._module_locks) - lock = _bootstrap._get_module_lock(name) - self.assertIn(name, _bootstrap._module_locks) - wr = weakref.ref(lock) - del lock - support.gc_collect() - self.assertNotIn(name, _bootstrap._module_locks) - self.assertIs(wr(), None) - - def test_all_locks(self): - support.gc_collect() - self.assertEqual(0, len(_bootstrap._module_locks)) - - -@support.reap_threads -def test_main(): - support.run_unittest(ModuleLockAsRLockTests, - DeadlockAvoidanceTests, - LifetimeTests) - - -if __name__ == '__main__': - test_main() diff -r 2059910e7d76 -r ac776ef41428 Lib/inspect.py --- a/Lib/inspect.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/inspect.py Mon May 21 23:01:17 2012 -0400 @@ -38,7 +38,6 @@ import sys import tokenize import types -import warnings from operator import attrgetter from collections import namedtuple diff -r 2059910e7d76 -r ac776ef41428 Lib/ipaddress.py --- a/Lib/ipaddress.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,2046 +0,0 @@ -# Copyright 2007 Google Inc. -# Licensed to PSF under a Contributor Agreement. - -"""A fast, lightweight IPv4/IPv6 manipulation library in Python. - -This library is used to create/poke/manipulate IPv4 and IPv6 addresses -and networks. - -""" - -__version__ = '1.0' - - -import struct - - -IPV4LENGTH = 32 -IPV6LENGTH = 128 - - -class AddressValueError(ValueError): - """A Value Error related to the address.""" - - -class NetmaskValueError(ValueError): - """A Value Error related to the netmask.""" - - -def ip_address(address): - """Take an IP string/int and return an object of the correct type. - - Args: - address: A string or integer, the IP address. Either IPv4 or - IPv6 addresses may be supplied; integers less than 2**32 will - be considered to be IPv4 by default. - - Returns: - An IPv4Address or IPv6Address object. - - Raises: - ValueError: if the *address* passed isn't either a v4 or a v6 - address - - """ - try: - return IPv4Address(address) - except (AddressValueError, NetmaskValueError): - pass - - try: - return IPv6Address(address) - except (AddressValueError, NetmaskValueError): - pass - - raise ValueError('%r does not appear to be an IPv4 or IPv6 address' % - address) - - -def ip_network(address, strict=True): - """Take an IP string/int and return an object of the correct type. - - Args: - address: A string or integer, the IP network. Either IPv4 or - IPv6 networks may be supplied; integers less than 2**32 will - be considered to be IPv4 by default. - - Returns: - An IPv4Network or IPv6Network object. - - Raises: - ValueError: if the string passed isn't either a v4 or a v6 - address. Or if the network has host bits set. - - """ - try: - return IPv4Network(address, strict) - except (AddressValueError, NetmaskValueError): - pass - - try: - return IPv6Network(address, strict) - except (AddressValueError, NetmaskValueError): - pass - - raise ValueError('%r does not appear to be an IPv4 or IPv6 network' % - address) - - -def ip_interface(address): - """Take an IP string/int and return an object of the correct type. - - Args: - address: A string or integer, the IP address. Either IPv4 or - IPv6 addresses may be supplied; integers less than 2**32 will - be considered to be IPv4 by default. - - Returns: - An IPv4Interface or IPv6Interface object. - - Raises: - ValueError: if the string passed isn't either a v4 or a v6 - address. - - Notes: - The IPv?Interface classes describe an Address on a particular - Network, so they're basically a combination of both the Address - and Network classes. - - """ - try: - return IPv4Interface(address) - except (AddressValueError, NetmaskValueError): - pass - - try: - return IPv6Interface(address) - except (AddressValueError, NetmaskValueError): - pass - - raise ValueError('%r does not appear to be an IPv4 or IPv6 network' % - address) - - -def v4_int_to_packed(address): - """Represent an address as 4 packed bytes in network (big-endian) order. - - Args: - address: An integer representation of an IPv4 IP address. - - Returns: - The integer address packed as 4 bytes in network (big-endian) order. - - Raises: - ValueError: If the integer is negative or too large to be an - IPv4 IP address. - - """ - try: - return struct.pack('!I', address) - except: - raise ValueError("Address negative or too large for IPv4") - - -def v6_int_to_packed(address): - """Represent an address as 16 packed bytes in network (big-endian) order. - - Args: - address: An integer representation of an IPv6 IP address. - - Returns: - The integer address packed as 16 bytes in network (big-endian) order. - - """ - try: - return struct.pack('!QQ', address >> 64, address & (2**64 - 1)) - except: - raise ValueError("Address negative or too large for IPv6") - - -def _find_address_range(addresses): - """Find a sequence of IPv#Address. - - Args: - addresses: a list of IPv#Address objects. - - Returns: - A tuple containing the first and last IP addresses in the sequence. - - """ - first = last = addresses[0] - for ip in addresses[1:]: - if ip._ip == last._ip + 1: - last = ip - else: - break - return (first, last) - - -def _get_prefix_length(number1, number2, bits): - """Get the number of leading bits that are same for two numbers. - - Args: - number1: an integer. - number2: another integer. - bits: the maximum number of bits to compare. - - Returns: - The number of leading bits that are the same for two numbers. - - """ - for i in range(bits): - if number1 >> i == number2 >> i: - return bits - i - return 0 - - -def _count_righthand_zero_bits(number, bits): - """Count the number of zero bits on the right hand side. - - Args: - number: an integer. - bits: maximum number of bits to count. - - Returns: - The number of zero bits on the right hand side of the number. - - """ - if number == 0: - return bits - for i in range(bits): - if (number >> i) % 2: - return i - - -def summarize_address_range(first, last): - """Summarize a network range given the first and last IP addresses. - - Example: - >>> summarize_address_range(IPv4Address('192.0.2.0'), - IPv4Address('192.0.2.130')) - [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), - IPv4Network('192.0.2.130/32')] - - Args: - first: the first IPv4Address or IPv6Address in the range. - last: the last IPv4Address or IPv6Address in the range. - - Returns: - An iterator of the summarized IPv(4|6) network objects. - - Raise: - TypeError: - If the first and last objects are not IP addresses. - If the first and last objects are not the same version. - ValueError: - If the last object is not greater than the first. - If the version of the first address is not 4 or 6. - - """ - if (not (isinstance(first, _BaseAddress) and - isinstance(last, _BaseAddress))): - raise TypeError('first and last must be IP addresses, not networks') - if first.version != last.version: - raise TypeError("%s and %s are not of the same version" % ( - str(first), str(last))) - if first > last: - raise ValueError('last IP address must be greater than first') - - if first.version == 4: - ip = IPv4Network - elif first.version == 6: - ip = IPv6Network - else: - raise ValueError('unknown IP version') - - ip_bits = first._max_prefixlen - first_int = first._ip - last_int = last._ip - while first_int <= last_int: - nbits = _count_righthand_zero_bits(first_int, ip_bits) - current = None - while nbits >= 0: - addend = 2**nbits - 1 - current = first_int + addend - nbits -= 1 - if current <= last_int: - break - prefix = _get_prefix_length(first_int, current, ip_bits) - net = ip('%s/%d' % (str(first), prefix)) - yield net - if current == ip._ALL_ONES: - break - first_int = current + 1 - first = first.__class__(first_int) - - -def _collapse_addresses_recursive(addresses): - """Loops through the addresses, collapsing concurrent netblocks. - - Example: - - ip1 = IPv4Network('192.0.2.0/26') - ip2 = IPv4Network('192.0.2.64/26') - ip3 = IPv4Network('192.0.2.128/26') - ip4 = IPv4Network('192.0.2.192/26') - - _collapse_addresses_recursive([ip1, ip2, ip3, ip4]) -> - [IPv4Network('192.0.2.0/24')] - - This shouldn't be called directly; it is called via - collapse_addresses([]). - - Args: - addresses: A list of IPv4Network's or IPv6Network's - - Returns: - A list of IPv4Network's or IPv6Network's depending on what we were - passed. - - """ - ret_array = [] - optimized = False - - for cur_addr in addresses: - if not ret_array: - ret_array.append(cur_addr) - continue - if (cur_addr.network_address >= ret_array[-1].network_address and - cur_addr.broadcast_address <= ret_array[-1].broadcast_address): - optimized = True - elif cur_addr == list(ret_array[-1].supernet().subnets())[1]: - ret_array.append(ret_array.pop().supernet()) - optimized = True - else: - ret_array.append(cur_addr) - - if optimized: - return _collapse_addresses_recursive(ret_array) - - return ret_array - - -def collapse_addresses(addresses): - """Collapse a list of IP objects. - - Example: - collapse_addresses([IPv4Network('192.0.2.0/25'), - IPv4Network('192.0.2.128/25')]) -> - [IPv4Network('192.0.2.0/24')] - - Args: - addresses: An iterator of IPv4Network or IPv6Network objects. - - Returns: - An iterator of the collapsed IPv(4|6)Network objects. - - Raises: - TypeError: If passed a list of mixed version objects. - - """ - i = 0 - addrs = [] - ips = [] - nets = [] - - # split IP addresses and networks - for ip in addresses: - if isinstance(ip, _BaseAddress): - if ips and ips[-1]._version != ip._version: - raise TypeError("%s and %s are not of the same version" % ( - str(ip), str(ips[-1]))) - ips.append(ip) - elif ip._prefixlen == ip._max_prefixlen: - if ips and ips[-1]._version != ip._version: - raise TypeError("%s and %s are not of the same version" % ( - str(ip), str(ips[-1]))) - try: - ips.append(ip.ip) - except AttributeError: - ips.append(ip.network_address) - else: - if nets and nets[-1]._version != ip._version: - raise TypeError("%s and %s are not of the same version" % ( - str(ip), str(nets[-1]))) - nets.append(ip) - - # sort and dedup - ips = sorted(set(ips)) - nets = sorted(set(nets)) - - while i < len(ips): - (first, last) = _find_address_range(ips[i:]) - i = ips.index(last) + 1 - addrs.extend(summarize_address_range(first, last)) - - return iter(_collapse_addresses_recursive(sorted( - addrs + nets, key=_BaseNetwork._get_networks_key))) - - -def get_mixed_type_key(obj): - """Return a key suitable for sorting between networks and addresses. - - Address and Network objects are not sortable by default; they're - fundamentally different so the expression - - IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24') - - doesn't make any sense. There are some times however, where you may wish - to have ipaddress sort these for you anyway. If you need to do this, you - can use this function as the key= argument to sorted(). - - Args: - obj: either a Network or Address object. - Returns: - appropriate key. - - """ - if isinstance(obj, _BaseNetwork): - return obj._get_networks_key() - elif isinstance(obj, _BaseAddress): - return obj._get_address_key() - return NotImplemented - - -class _IPAddressBase: - - """The mother class.""" - - @property - def exploded(self): - """Return the longhand version of the IP address as a string.""" - return self._explode_shorthand_ip_string() - - @property - def compressed(self): - """Return the shorthand version of the IP address as a string.""" - return str(self) - - def _ip_int_from_prefix(self, prefixlen=None): - """Turn the prefix length netmask into a int for comparison. - - Args: - prefixlen: An integer, the prefix length. - - Returns: - An integer. - - """ - if prefixlen is None: - prefixlen = self._prefixlen - return self._ALL_ONES ^ (self._ALL_ONES >> prefixlen) - - def _prefix_from_ip_int(self, ip_int, mask=32): - """Return prefix length from the decimal netmask. - - Args: - ip_int: An integer, the IP address. - mask: The netmask. Defaults to 32. - - Returns: - An integer, the prefix length. - - """ - while mask: - if ip_int & 1 == 1: - break - ip_int >>= 1 - mask -= 1 - - return mask - - def _ip_string_from_prefix(self, prefixlen=None): - """Turn a prefix length into a dotted decimal string. - - Args: - prefixlen: An integer, the netmask prefix length. - - Returns: - A string, the dotted decimal netmask string. - - """ - if not prefixlen: - prefixlen = self._prefixlen - return self._string_from_ip_int(self._ip_int_from_prefix(prefixlen)) - - -class _BaseAddress(_IPAddressBase): - - """A generic IP object. - - This IP class contains the version independent methods which are - used by single IP addresses. - - """ - - def __init__(self, address): - if (not isinstance(address, bytes) - and '/' in str(address)): - raise AddressValueError(address) - - def __index__(self): - return self._ip - - def __int__(self): - return self._ip - - def __eq__(self, other): - try: - return (self._ip == other._ip - and self._version == other._version) - except AttributeError: - return NotImplemented - - def __ne__(self, other): - eq = self.__eq__(other) - if eq is NotImplemented: - return NotImplemented - return not eq - - def __le__(self, other): - gt = self.__gt__(other) - if gt is NotImplemented: - return NotImplemented - return not gt - - def __ge__(self, other): - lt = self.__lt__(other) - if lt is NotImplemented: - return NotImplemented - return not lt - - def __lt__(self, other): - if self._version != other._version: - raise TypeError('%s and %s are not of the same version' % ( - str(self), str(other))) - if not isinstance(other, _BaseAddress): - raise TypeError('%s and %s are not of the same type' % ( - str(self), str(other))) - if self._ip != other._ip: - return self._ip < other._ip - return False - - def __gt__(self, other): - if self._version != other._version: - raise TypeError('%s and %s are not of the same version' % ( - str(self), str(other))) - if not isinstance(other, _BaseAddress): - raise TypeError('%s and %s are not of the same type' % ( - str(self), str(other))) - if self._ip != other._ip: - return self._ip > other._ip - return False - - # Shorthand for Integer addition and subtraction. This is not - # meant to ever support addition/subtraction of addresses. - def __add__(self, other): - if not isinstance(other, int): - return NotImplemented - return self.__class__(int(self) + other) - - def __sub__(self, other): - if not isinstance(other, int): - return NotImplemented - return self.__class__(int(self) - other) - - def __repr__(self): - return '%s(%r)' % (self.__class__.__name__, str(self)) - - def __str__(self): - return str(self._string_from_ip_int(self._ip)) - - def __hash__(self): - return hash(hex(int(self._ip))) - - def _get_address_key(self): - return (self._version, self) - - @property - def version(self): - raise NotImplementedError('BaseIP has no version') - - -class _BaseNetwork(_IPAddressBase): - - """A generic IP network object. - - This IP class contains the version independent methods which are - used by networks. - - """ - def __init__(self, address): - self._cache = {} - - def __index__(self): - return int(self.network_address) ^ self.prefixlen - - def __int__(self): - return int(self.network_address) - - def __repr__(self): - return '%s(%r)' % (self.__class__.__name__, str(self)) - - def __str__(self): - return '%s/%d' % (str(self.network_address), - self.prefixlen) - - def hosts(self): - """Generate Iterator over usable hosts in a network. - - This is like __iter__ except it doesn't return the network - or broadcast addresses. - - """ - cur = int(self.network_address) + 1 - bcast = int(self.broadcast_address) - 1 - while cur <= bcast: - cur += 1 - yield self._address_class(cur - 1) - - def __iter__(self): - cur = int(self.network_address) - bcast = int(self.broadcast_address) - while cur <= bcast: - cur += 1 - yield self._address_class(cur - 1) - - def __getitem__(self, n): - network = int(self.network_address) - broadcast = int(self.broadcast_address) - if n >= 0: - if network + n > broadcast: - raise IndexError - return self._address_class(network + n) - else: - n += 1 - if broadcast + n < network: - raise IndexError - return self._address_class(broadcast + n) - - def __lt__(self, other): - if self._version != other._version: - raise TypeError('%s and %s are not of the same version' % ( - str(self), str(other))) - if not isinstance(other, _BaseNetwork): - raise TypeError('%s and %s are not of the same type' % ( - str(self), str(other))) - if self.network_address != other.network_address: - return self.network_address < other.network_address - if self.netmask != other.netmask: - return self.netmask < other.netmask - return False - - def __gt__(self, other): - if self._version != other._version: - raise TypeError('%s and %s are not of the same version' % ( - str(self), str(other))) - if not isinstance(other, _BaseNetwork): - raise TypeError('%s and %s are not of the same type' % ( - str(self), str(other))) - if self.network_address != other.network_address: - return self.network_address > other.network_address - if self.netmask != other.netmask: - return self.netmask > other.netmask - return False - - def __le__(self, other): - gt = self.__gt__(other) - if gt is NotImplemented: - return NotImplemented - return not gt - - def __ge__(self, other): - lt = self.__lt__(other) - if lt is NotImplemented: - return NotImplemented - return not lt - - def __eq__(self, other): - if not isinstance(other, _BaseNetwork): - raise TypeError('%s and %s are not of the same type' % ( - str(self), str(other))) - return (self._version == other._version and - self.network_address == other.network_address and - int(self.netmask) == int(other.netmask)) - - def __ne__(self, other): - eq = self.__eq__(other) - if eq is NotImplemented: - return NotImplemented - return not eq - - def __hash__(self): - return hash(int(self.network_address) ^ int(self.netmask)) - - def __contains__(self, other): - # always false if one is v4 and the other is v6. - if self._version != other._version: - return False - # dealing with another network. - if isinstance(other, _BaseNetwork): - return False - # dealing with another address - else: - # address - return (int(self.network_address) <= int(other._ip) <= - int(self.broadcast_address)) - - def overlaps(self, other): - """Tell if self is partly contained in other.""" - return self.network_address in other or ( - self.broadcast_address in other or ( - other.network_address in self or ( - other.broadcast_address in self))) - - @property - def broadcast_address(self): - x = self._cache.get('broadcast_address') - if x is None: - x = self._address_class(int(self.network_address) | - int(self.hostmask)) - self._cache['broadcast_address'] = x - return x - - @property - def hostmask(self): - x = self._cache.get('hostmask') - if x is None: - x = self._address_class(int(self.netmask) ^ self._ALL_ONES) - self._cache['hostmask'] = x - return x - - @property - def with_prefixlen(self): - return '%s/%d' % (str(self.network_address), self._prefixlen) - - @property - def with_netmask(self): - return '%s/%s' % (str(self.network_address), str(self.netmask)) - - @property - def with_hostmask(self): - return '%s/%s' % (str(self.network_address), str(self.hostmask)) - - @property - def num_addresses(self): - """Number of hosts in the current subnet.""" - return int(self.broadcast_address) - int(self.network_address) + 1 - - @property - def version(self): - raise NotImplementedError('BaseNet has no version') - - @property - def _address_class(self): - raise NotImplementedError('BaseNet has no associated address class') - - @property - def prefixlen(self): - return self._prefixlen - - def address_exclude(self, other): - """Remove an address from a larger block. - - For example: - - addr1 = ip_network('192.0.2.0/28') - addr2 = ip_network('192.0.2.1/32') - addr1.address_exclude(addr2) = - [IPv4Network('192.0.2.0/32'), IPv4Network('192.0.2.2/31'), - IPv4Network('192.0.2.4/30'), IPv4Network('192.0.2.8/29')] - - or IPv6: - - addr1 = ip_network('2001:db8::1/32') - addr2 = ip_network('2001:db8::1/128') - addr1.address_exclude(addr2) = - [ip_network('2001:db8::1/128'), - ip_network('2001:db8::2/127'), - ip_network('2001:db8::4/126'), - ip_network('2001:db8::8/125'), - ... - ip_network('2001:db8:8000::/33')] - - Args: - other: An IPv4Network or IPv6Network object of the same type. - - Returns: - An iterator of the the IPv(4|6)Network objects which is self - minus other. - - Raises: - TypeError: If self and other are of difffering address - versions, or if other is not a network object. - ValueError: If other is not completely contained by self. - - """ - if not self._version == other._version: - raise TypeError("%s and %s are not of the same version" % ( - str(self), str(other))) - - if not isinstance(other, _BaseNetwork): - raise TypeError("%s is not a network object" % str(other)) - - if not (other.network_address >= self.network_address and - other.broadcast_address <= self.broadcast_address): - raise ValueError('%s not contained in %s' % (other, self)) - if other == self: - raise StopIteration - - # Make sure we're comparing the network of other. - other = other.__class__('%s/%s' % (str(other.network_address), - str(other.prefixlen))) - - s1, s2 = self.subnets() - while s1 != other and s2 != other: - if (other.network_address >= s1.network_address and - other.broadcast_address <= s1.broadcast_address): - yield s2 - s1, s2 = s1.subnets() - elif (other.network_address >= s2.network_address and - other.broadcast_address <= s2.broadcast_address): - yield s1 - s1, s2 = s2.subnets() - else: - # If we got here, there's a bug somewhere. - raise AssertionError('Error performing exclusion: ' - 's1: %s s2: %s other: %s' % - (str(s1), str(s2), str(other))) - if s1 == other: - yield s2 - elif s2 == other: - yield s1 - else: - # If we got here, there's a bug somewhere. - raise AssertionError('Error performing exclusion: ' - 's1: %s s2: %s other: %s' % - (str(s1), str(s2), str(other))) - - def compare_networks(self, other): - """Compare two IP objects. - - This is only concerned about the comparison of the integer - representation of the network addresses. This means that the - host bits aren't considered at all in this method. If you want - to compare host bits, you can easily enough do a - 'HostA._ip < HostB._ip' - - Args: - other: An IP object. - - Returns: - If the IP versions of self and other are the same, returns: - - -1 if self < other: - eg: IPv4Network('192.0.2.0/25') < IPv4Network('192.0.2.128/25') - IPv6Network('2001:db8::1000/124') < - IPv6Network('2001:db8::2000/124') - 0 if self == other - eg: IPv4Network('192.0.2.0/24') == IPv4Network('192.0.2.0/24') - IPv6Network('2001:db8::1000/124') == - IPv6Network('2001:db8::1000/124') - 1 if self > other - eg: IPv4Network('192.0.2.128/25') > IPv4Network('192.0.2.0/25') - IPv6Network('2001:db8::2000/124') > - IPv6Network('2001:db8::1000/124') - - Raises: - TypeError if the IP versions are different. - - """ - # does this need to raise a ValueError? - if self._version != other._version: - raise TypeError('%s and %s are not of the same type' % ( - str(self), str(other))) - # self._version == other._version below here: - if self.network_address < other.network_address: - return -1 - if self.network_address > other.network_address: - return 1 - # self.network_address == other.network_address below here: - if self.netmask < other.netmask: - return -1 - if self.netmask > other.netmask: - return 1 - return 0 - - def _get_networks_key(self): - """Network-only key function. - - Returns an object that identifies this address' network and - netmask. This function is a suitable "key" argument for sorted() - and list.sort(). - - """ - return (self._version, self.network_address, self.netmask) - - def subnets(self, prefixlen_diff=1, new_prefix=None): - """The subnets which join to make the current subnet. - - In the case that self contains only one IP - (self._prefixlen == 32 for IPv4 or self._prefixlen == 128 - for IPv6), yield an iterator with just ourself. - - Args: - prefixlen_diff: An integer, the amount the prefix length - should be increased by. This should not be set if - new_prefix is also set. - new_prefix: The desired new prefix length. This must be a - larger number (smaller prefix) than the existing prefix. - This should not be set if prefixlen_diff is also set. - - Returns: - An iterator of IPv(4|6) objects. - - Raises: - ValueError: The prefixlen_diff is too small or too large. - OR - prefixlen_diff and new_prefix are both set or new_prefix - is a smaller number than the current prefix (smaller - number means a larger network) - - """ - if self._prefixlen == self._max_prefixlen: - yield self - return - - if new_prefix is not None: - if new_prefix < self._prefixlen: - raise ValueError('new prefix must be longer') - if prefixlen_diff != 1: - raise ValueError('cannot set prefixlen_diff and new_prefix') - prefixlen_diff = new_prefix - self._prefixlen - - if prefixlen_diff < 0: - raise ValueError('prefix length diff must be > 0') - new_prefixlen = self._prefixlen + prefixlen_diff - - if not self._is_valid_netmask(str(new_prefixlen)): - raise ValueError( - 'prefix length diff %d is invalid for netblock %s' % ( - new_prefixlen, str(self))) - - first = self.__class__('%s/%s' % - (str(self.network_address), - str(self._prefixlen + prefixlen_diff))) - - yield first - current = first - while True: - broadcast = current.broadcast_address - if broadcast == self.broadcast_address: - return - new_addr = self._address_class(int(broadcast) + 1) - current = self.__class__('%s/%s' % (str(new_addr), - str(new_prefixlen))) - - yield current - - def supernet(self, prefixlen_diff=1, new_prefix=None): - """The supernet containing the current network. - - Args: - prefixlen_diff: An integer, the amount the prefix length of - the network should be decreased by. For example, given a - /24 network and a prefixlen_diff of 3, a supernet with a - /21 netmask is returned. - - Returns: - An IPv4 network object. - - Raises: - ValueError: If self.prefixlen - prefixlen_diff < 0. I.e., you have - a negative prefix length. - OR - If prefixlen_diff and new_prefix are both set or new_prefix is a - larger number than the current prefix (larger number means a - smaller network) - - """ - if self._prefixlen == 0: - return self - - if new_prefix is not None: - if new_prefix > self._prefixlen: - raise ValueError('new prefix must be shorter') - if prefixlen_diff != 1: - raise ValueError('cannot set prefixlen_diff and new_prefix') - prefixlen_diff = self._prefixlen - new_prefix - - if self.prefixlen - prefixlen_diff < 0: - raise ValueError( - 'current prefixlen is %d, cannot have a prefixlen_diff of %d' % - (self.prefixlen, prefixlen_diff)) - # TODO (pmoody): optimize this. - t = self.__class__('%s/%d' % (str(self.network_address), - self.prefixlen - prefixlen_diff), - strict=False) - return t.__class__('%s/%d' % (str(t.network_address), t.prefixlen)) - - -class _BaseV4: - - """Base IPv4 object. - - The following methods are used by IPv4 objects in both single IP - addresses and networks. - - """ - - # Equivalent to 255.255.255.255 or 32 bits of 1's. - _ALL_ONES = (2**IPV4LENGTH) - 1 - _DECIMAL_DIGITS = frozenset('0123456789') - - # the valid octets for host and netmasks. only useful for IPv4. - _valid_mask_octets = set((255, 254, 252, 248, 240, 224, 192, 128, 0)) - - def __init__(self, address): - self._version = 4 - self._max_prefixlen = IPV4LENGTH - - def _explode_shorthand_ip_string(self): - return str(self) - - def _ip_int_from_string(self, ip_str): - """Turn the given IP string into an integer for comparison. - - Args: - ip_str: A string, the IP ip_str. - - Returns: - The IP ip_str as an integer. - - Raises: - AddressValueError: if ip_str isn't a valid IPv4 Address. - - """ - octets = ip_str.split('.') - if len(octets) != 4: - raise AddressValueError(ip_str) - - packed_ip = 0 - for oc in octets: - try: - packed_ip = (packed_ip << 8) | self._parse_octet(oc) - except ValueError: - raise AddressValueError(ip_str) - return packed_ip - - def _parse_octet(self, octet_str): - """Convert a decimal octet into an integer. - - Args: - octet_str: A string, the number to parse. - - Returns: - The octet as an integer. - - Raises: - ValueError: if the octet isn't strictly a decimal from [0..255]. - - """ - # Whitelist the characters, since int() allows a lot of bizarre stuff. - if not self._DECIMAL_DIGITS.issuperset(octet_str): - raise ValueError - octet_int = int(octet_str, 10) - # Disallow leading zeroes, because no clear standard exists on - # whether these should be interpreted as decimal or octal. - if octet_int > 255 or (octet_str[0] == '0' and len(octet_str) > 1): - raise ValueError - return octet_int - - def _string_from_ip_int(self, ip_int): - """Turns a 32-bit integer into dotted decimal notation. - - Args: - ip_int: An integer, the IP address. - - Returns: - The IP address as a string in dotted decimal notation. - - """ - octets = [] - for _ in range(4): - octets.insert(0, str(ip_int & 0xFF)) - ip_int >>= 8 - return '.'.join(octets) - - def _is_valid_netmask(self, netmask): - """Verify that the netmask is valid. - - Args: - netmask: A string, either a prefix or dotted decimal - netmask. - - Returns: - A boolean, True if the prefix represents a valid IPv4 - netmask. - - """ - mask = netmask.split('.') - if len(mask) == 4: - if [x for x in mask if int(x) not in self._valid_mask_octets]: - return False - if [y for idx, y in enumerate(mask) if idx > 0 and - y > mask[idx - 1]]: - return False - return True - try: - netmask = int(netmask) - except ValueError: - return False - return 0 <= netmask <= self._max_prefixlen - - def _is_hostmask(self, ip_str): - """Test if the IP string is a hostmask (rather than a netmask). - - Args: - ip_str: A string, the potential hostmask. - - Returns: - A boolean, True if the IP string is a hostmask. - - """ - bits = ip_str.split('.') - try: - parts = [int(x) for x in bits if int(x) in self._valid_mask_octets] - except ValueError: - return False - if len(parts) != len(bits): - return False - if parts[0] < parts[-1]: - return True - return False - - @property - def max_prefixlen(self): - return self._max_prefixlen - - @property - def version(self): - return self._version - - @property - def is_reserved(self): - """Test if the address is otherwise IETF reserved. - - Returns: - A boolean, True if the address is within the - reserved IPv4 Network range. - - """ - reserved_network = IPv4Network('240.0.0.0/4') - if isinstance(self, _BaseAddress): - return self in reserved_network - return (self.network_address in reserved_network and - self.broadcast_address in reserved_network) - - @property - def is_private(self): - """Test if this address is allocated for private networks. - - Returns: - A boolean, True if the address is reserved per RFC 1918. - - """ - private_10 = IPv4Network('10.0.0.0/8') - private_172 = IPv4Network('172.16.0.0/12') - private_192 = IPv4Network('192.168.0.0/16') - if isinstance(self, _BaseAddress): - return (self in private_10 or self in private_172 or - self in private_192) - else: - return ((self.network_address in private_10 and - self.broadcast_address in private_10) or - (self.network_address in private_172 and - self.broadcast_address in private_172) or - (self.network_address in private_192 and - self.broadcast_address in private_192)) - - @property - def is_multicast(self): - """Test if the address is reserved for multicast use. - - Returns: - A boolean, True if the address is multicast. - See RFC 3171 for details. - - """ - multicast_network = IPv4Network('224.0.0.0/4') - if isinstance(self, _BaseAddress): - return self in IPv4Network('224.0.0.0/4') - return (self.network_address in multicast_network and - self.broadcast_address in multicast_network) - - @property - def is_unspecified(self): - """Test if the address is unspecified. - - Returns: - A boolean, True if this is the unspecified address as defined in - RFC 5735 3. - - """ - unspecified_address = IPv4Address('0.0.0.0') - if isinstance(self, _BaseAddress): - return self == unspecified_address - return (self.network_address == self.broadcast_address == - unspecified_address) - - @property - def is_loopback(self): - """Test if the address is a loopback address. - - Returns: - A boolean, True if the address is a loopback per RFC 3330. - - """ - loopback_address = IPv4Network('127.0.0.0/8') - if isinstance(self, _BaseAddress): - return self in loopback_address - - return (self.network_address in loopback_address and - self.broadcast_address in loopback_address) - - @property - def is_link_local(self): - """Test if the address is reserved for link-local. - - Returns: - A boolean, True if the address is link-local per RFC 3927. - - """ - linklocal_network = IPv4Network('169.254.0.0/16') - if isinstance(self, _BaseAddress): - return self in linklocal_network - return (self.network_address in linklocal_network and - self.broadcast_address in linklocal_network) - - -class IPv4Address(_BaseV4, _BaseAddress): - - """Represent and manipulate single IPv4 Addresses.""" - - def __init__(self, address): - - """ - Args: - address: A string or integer representing the IP - - Additionally, an integer can be passed, so - IPv4Address('192.0.2.1') == IPv4Address(3221225985). - or, more generally - IPv4Address(int(IPv4Address('192.0.2.1'))) == - IPv4Address('192.0.2.1') - - Raises: - AddressValueError: If ipaddress isn't a valid IPv4 address. - - """ - _BaseAddress.__init__(self, address) - _BaseV4.__init__(self, address) - - # Efficient constructor from integer. - if isinstance(address, int): - self._ip = address - if address < 0 or address > self._ALL_ONES: - raise AddressValueError(address) - return - - # Constructing from a packed address - if isinstance(address, bytes) and len(address) == 4: - self._ip = struct.unpack('!I', address)[0] - return - - # Assume input argument to be string or any object representation - # which converts into a formatted IP string. - addr_str = str(address) - self._ip = self._ip_int_from_string(addr_str) - - @property - def packed(self): - """The binary representation of this address.""" - return v4_int_to_packed(self._ip) - - -class IPv4Interface(IPv4Address): - - def __init__(self, address): - if isinstance(address, (bytes, int)): - IPv4Address.__init__(self, address) - self.network = IPv4Network(self._ip) - self._prefixlen = self._max_prefixlen - return - - addr = str(address).split('/') - if len(addr) > 2: - raise AddressValueError(address) - IPv4Address.__init__(self, addr[0]) - - self.network = IPv4Network(address, strict=False) - self._prefixlen = self.network._prefixlen - - self.netmask = self.network.netmask - self.hostmask = self.network.hostmask - - def __str__(self): - return '%s/%d' % (self._string_from_ip_int(self._ip), - self.network.prefixlen) - - def __eq__(self, other): - try: - return (IPv4Address.__eq__(self, other) and - self.network == other.network) - except AttributeError: - return NotImplemented - - def __hash__(self): - return self._ip ^ self._prefixlen ^ int(self.network.network_address) - - @property - def prefixlen(self): - return self._prefixlen - - @property - def ip(self): - return IPv4Address(self._ip) - - @property - def with_prefixlen(self): - return self - - @property - def with_netmask(self): - return '%s/%s' % (self._string_from_ip_int(self._ip), - self.netmask) - - @property - def with_hostmask(self): - return '%s/%s' % (self._string_from_ip_int(self._ip), - self.hostmask) - - -class IPv4Network(_BaseV4, _BaseNetwork): - - """This class represents and manipulates 32-bit IPv4 network + addresses.. - - Attributes: [examples for IPv4Network('192.0.2.0/27')] - .network_address: IPv4Address('192.0.2.0') - .hostmask: IPv4Address('0.0.0.31') - .broadcast_address: IPv4Address('192.0.2.32') - .netmask: IPv4Address('255.255.255.224') - .prefixlen: 27 - - """ - # Class to use when creating address objects - # TODO (ncoghlan): Investigate using IPv4Interface instead - _address_class = IPv4Address - - def __init__(self, address, strict=True): - - """Instantiate a new IPv4 network object. - - Args: - address: A string or integer representing the IP [& network]. - '192.0.2.0/24' - '192.0.2.0/255.255.255.0' - '192.0.0.2/0.0.0.255' - are all functionally the same in IPv4. Similarly, - '192.0.2.1' - '192.0.2.1/255.255.255.255' - '192.0.2.1/32' - are also functionaly equivalent. That is to say, failing to - provide a subnetmask will create an object with a mask of /32. - - If the mask (portion after the / in the argument) is given in - dotted quad form, it is treated as a netmask if it starts with a - non-zero field (e.g. /255.0.0.0 == /8) and as a hostmask if it - starts with a zero field (e.g. 0.255.255.255 == /8), with the - single exception of an all-zero mask which is treated as a - netmask == /0. If no mask is given, a default of /32 is used. - - Additionally, an integer can be passed, so - IPv4Network('192.0.2.1') == IPv4Network(3221225985) - or, more generally - IPv4Interface(int(IPv4Interface('192.0.2.1'))) == - IPv4Interface('192.0.2.1') - - Raises: - AddressValueError: If ipaddress isn't a valid IPv4 address. - NetmaskValueError: If the netmask isn't valid for - an IPv4 address. - ValueError: If strict is True and a network address is not - supplied. - - """ - - _BaseV4.__init__(self, address) - _BaseNetwork.__init__(self, address) - - # Constructing from a packed address - if isinstance(address, bytes) and len(address) == 4: - self.network_address = IPv4Address( - struct.unpack('!I', address)[0]) - self._prefixlen = self._max_prefixlen - self.netmask = IPv4Address(self._ALL_ONES) - #fixme: address/network test here - return - - # Efficient constructor from integer. - if isinstance(address, int): - self._prefixlen = self._max_prefixlen - self.netmask = IPv4Address(self._ALL_ONES) - if address < 0 or address > self._ALL_ONES: - raise AddressValueError(address) - self.network_address = IPv4Address(address) - #fixme: address/network test here. - return - - # Assume input argument to be string or any object representation - # which converts into a formatted IP prefix string. - addr = str(address).split('/') - self.network_address = IPv4Address(self._ip_int_from_string(addr[0])) - - if len(addr) > 2: - raise AddressValueError(address) - - if len(addr) == 2: - mask = addr[1].split('.') - - if len(mask) == 4: - # We have dotted decimal netmask. - if self._is_valid_netmask(addr[1]): - self.netmask = IPv4Address(self._ip_int_from_string( - addr[1])) - elif self._is_hostmask(addr[1]): - self.netmask = IPv4Address( - self._ip_int_from_string(addr[1]) ^ self._ALL_ONES) - else: - raise NetmaskValueError('%s is not a valid netmask' - % addr[1]) - - self._prefixlen = self._prefix_from_ip_int(int(self.netmask)) - else: - # We have a netmask in prefix length form. - if not self._is_valid_netmask(addr[1]): - raise NetmaskValueError(addr[1]) - self._prefixlen = int(addr[1]) - self.netmask = IPv4Address(self._ip_int_from_prefix( - self._prefixlen)) - else: - self._prefixlen = self._max_prefixlen - self.netmask = IPv4Address(self._ip_int_from_prefix( - self._prefixlen)) - - if strict: - if (IPv4Address(int(self.network_address) & int(self.netmask)) != - self.network_address): - raise ValueError('%s has host bits set' % self) - self.network_address = IPv4Address(int(self.network_address) & - int(self.netmask)) - - if self._prefixlen == (self._max_prefixlen - 1): - self.hosts = self.__iter__ - - -class _BaseV6: - - """Base IPv6 object. - - The following methods are used by IPv6 objects in both single IP - addresses and networks. - - """ - - _ALL_ONES = (2**IPV6LENGTH) - 1 - _HEXTET_COUNT = 8 - _HEX_DIGITS = frozenset('0123456789ABCDEFabcdef') - - def __init__(self, address): - self._version = 6 - self._max_prefixlen = IPV6LENGTH - - def _ip_int_from_string(self, ip_str): - """Turn an IPv6 ip_str into an integer. - - Args: - ip_str: A string, the IPv6 ip_str. - - Returns: - An int, the IPv6 address - - Raises: - AddressValueError: if ip_str isn't a valid IPv6 Address. - - """ - parts = ip_str.split(':') - - # An IPv6 address needs at least 2 colons (3 parts). - if len(parts) < 3: - raise AddressValueError(ip_str) - - # If the address has an IPv4-style suffix, convert it to hexadecimal. - if '.' in parts[-1]: - ipv4_int = IPv4Address(parts.pop())._ip - parts.append('%x' % ((ipv4_int >> 16) & 0xFFFF)) - parts.append('%x' % (ipv4_int & 0xFFFF)) - - # An IPv6 address can't have more than 8 colons (9 parts). - if len(parts) > self._HEXTET_COUNT + 1: - raise AddressValueError(ip_str) - - # Disregarding the endpoints, find '::' with nothing in between. - # This indicates that a run of zeroes has been skipped. - try: - skip_index, = ( - [i for i in range(1, len(parts) - 1) if not parts[i]] or - [None]) - except ValueError: - # Can't have more than one '::' - raise AddressValueError(ip_str) - - # parts_hi is the number of parts to copy from above/before the '::' - # parts_lo is the number of parts to copy from below/after the '::' - if skip_index is not None: - # If we found a '::', then check if it also covers the endpoints. - parts_hi = skip_index - parts_lo = len(parts) - skip_index - 1 - if not parts[0]: - parts_hi -= 1 - if parts_hi: - raise AddressValueError(ip_str) # ^: requires ^:: - if not parts[-1]: - parts_lo -= 1 - if parts_lo: - raise AddressValueError(ip_str) # :$ requires ::$ - parts_skipped = self._HEXTET_COUNT - (parts_hi + parts_lo) - if parts_skipped < 1: - raise AddressValueError(ip_str) - else: - # Otherwise, allocate the entire address to parts_hi. The - # endpoints could still be empty, but _parse_hextet() will check - # for that. - if len(parts) != self._HEXTET_COUNT: - raise AddressValueError(ip_str) - parts_hi = len(parts) - parts_lo = 0 - parts_skipped = 0 - - try: - # Now, parse the hextets into a 128-bit integer. - ip_int = 0 - for i in range(parts_hi): - ip_int <<= 16 - ip_int |= self._parse_hextet(parts[i]) - ip_int <<= 16 * parts_skipped - for i in range(-parts_lo, 0): - ip_int <<= 16 - ip_int |= self._parse_hextet(parts[i]) - return ip_int - except ValueError: - raise AddressValueError(ip_str) - - def _parse_hextet(self, hextet_str): - """Convert an IPv6 hextet string into an integer. - - Args: - hextet_str: A string, the number to parse. - - Returns: - The hextet as an integer. - - Raises: - ValueError: if the input isn't strictly a hex number from - [0..FFFF]. - - """ - # Whitelist the characters, since int() allows a lot of bizarre stuff. - if not self._HEX_DIGITS.issuperset(hextet_str): - raise ValueError - hextet_int = int(hextet_str, 16) - if hextet_int > 0xFFFF: - raise ValueError - return hextet_int - - def _compress_hextets(self, hextets): - """Compresses a list of hextets. - - Compresses a list of strings, replacing the longest continuous - sequence of "0" in the list with "" and adding empty strings at - the beginning or at the end of the string such that subsequently - calling ":".join(hextets) will produce the compressed version of - the IPv6 address. - - Args: - hextets: A list of strings, the hextets to compress. - - Returns: - A list of strings. - - """ - best_doublecolon_start = -1 - best_doublecolon_len = 0 - doublecolon_start = -1 - doublecolon_len = 0 - for index in range(len(hextets)): - if hextets[index] == '0': - doublecolon_len += 1 - if doublecolon_start == -1: - # Start of a sequence of zeros. - doublecolon_start = index - if doublecolon_len > best_doublecolon_len: - # This is the longest sequence of zeros so far. - best_doublecolon_len = doublecolon_len - best_doublecolon_start = doublecolon_start - else: - doublecolon_len = 0 - doublecolon_start = -1 - - if best_doublecolon_len > 1: - best_doublecolon_end = (best_doublecolon_start + - best_doublecolon_len) - # For zeros at the end of the address. - if best_doublecolon_end == len(hextets): - hextets += [''] - hextets[best_doublecolon_start:best_doublecolon_end] = [''] - # For zeros at the beginning of the address. - if best_doublecolon_start == 0: - hextets = [''] + hextets - - return hextets - - def _string_from_ip_int(self, ip_int=None): - """Turns a 128-bit integer into hexadecimal notation. - - Args: - ip_int: An integer, the IP address. - - Returns: - A string, the hexadecimal representation of the address. - - Raises: - ValueError: The address is bigger than 128 bits of all ones. - - """ - if ip_int is None: - ip_int = int(self._ip) - - if ip_int > self._ALL_ONES: - raise ValueError('IPv6 address is too large') - - hex_str = '%032x' % ip_int - hextets = [] - for x in range(0, 32, 4): - hextets.append('%x' % int(hex_str[x:x+4], 16)) - - hextets = self._compress_hextets(hextets) - return ':'.join(hextets) - - def _explode_shorthand_ip_string(self): - """Expand a shortened IPv6 address. - - Args: - ip_str: A string, the IPv6 address. - - Returns: - A string, the expanded IPv6 address. - - """ - if isinstance(self, IPv6Network): - ip_str = str(self.network_address) - elif isinstance(self, IPv6Interface): - ip_str = str(self.ip) - else: - ip_str = str(self) - - ip_int = self._ip_int_from_string(ip_str) - parts = [] - for i in range(self._HEXTET_COUNT): - parts.append('%04x' % (ip_int & 0xFFFF)) - ip_int >>= 16 - parts.reverse() - if isinstance(self, (_BaseNetwork, IPv6Interface)): - return '%s/%d' % (':'.join(parts), self.prefixlen) - return ':'.join(parts) - - @property - def max_prefixlen(self): - return self._max_prefixlen - - @property - def version(self): - return self._version - - @property - def is_multicast(self): - """Test if the address is reserved for multicast use. - - Returns: - A boolean, True if the address is a multicast address. - See RFC 2373 2.7 for details. - - """ - multicast_network = IPv6Network('ff00::/8') - if isinstance(self, _BaseAddress): - return self in multicast_network - return (self.network_address in multicast_network and - self.broadcast_address in multicast_network) - - @property - def is_reserved(self): - """Test if the address is otherwise IETF reserved. - - Returns: - A boolean, True if the address is within one of the - reserved IPv6 Network ranges. - - """ - reserved_networks = [IPv6Network('::/8'), IPv6Network('100::/8'), - IPv6Network('200::/7'), IPv6Network('400::/6'), - IPv6Network('800::/5'), IPv6Network('1000::/4'), - IPv6Network('4000::/3'), IPv6Network('6000::/3'), - IPv6Network('8000::/3'), IPv6Network('A000::/3'), - IPv6Network('C000::/3'), IPv6Network('E000::/4'), - IPv6Network('F000::/5'), IPv6Network('F800::/6'), - IPv6Network('FE00::/9')] - - if isinstance(self, _BaseAddress): - return len([x for x in reserved_networks if self in x]) > 0 - return len([x for x in reserved_networks if self.network_address in x - and self.broadcast_address in x]) > 0 - - @property - def is_link_local(self): - """Test if the address is reserved for link-local. - - Returns: - A boolean, True if the address is reserved per RFC 4291. - - """ - linklocal_network = IPv6Network('fe80::/10') - if isinstance(self, _BaseAddress): - return self in linklocal_network - return (self.network_address in linklocal_network and - self.broadcast_address in linklocal_network) - - @property - def is_site_local(self): - """Test if the address is reserved for site-local. - - Note that the site-local address space has been deprecated by RFC 3879. - Use is_private to test if this address is in the space of unique local - addresses as defined by RFC 4193. - - Returns: - A boolean, True if the address is reserved per RFC 3513 2.5.6. - - """ - sitelocal_network = IPv6Network('fec0::/10') - if isinstance(self, _BaseAddress): - return self in sitelocal_network - return (self.network_address in sitelocal_network and - self.broadcast_address in sitelocal_network) - - @property - def is_private(self): - """Test if this address is allocated for private networks. - - Returns: - A boolean, True if the address is reserved per RFC 4193. - - """ - private_network = IPv6Network('fc00::/7') - if isinstance(self, _BaseAddress): - return self in private_network - return (self.network_address in private_network and - self.broadcast_address in private_network) - - @property - def ipv4_mapped(self): - """Return the IPv4 mapped address. - - Returns: - If the IPv6 address is a v4 mapped address, return the - IPv4 mapped address. Return None otherwise. - - """ - if (self._ip >> 32) != 0xFFFF: - return None - return IPv4Address(self._ip & 0xFFFFFFFF) - - @property - def teredo(self): - """Tuple of embedded teredo IPs. - - Returns: - Tuple of the (server, client) IPs or None if the address - doesn't appear to be a teredo address (doesn't start with - 2001::/32) - - """ - if (self._ip >> 96) != 0x20010000: - return None - return (IPv4Address((self._ip >> 64) & 0xFFFFFFFF), - IPv4Address(~self._ip & 0xFFFFFFFF)) - - @property - def sixtofour(self): - """Return the IPv4 6to4 embedded address. - - Returns: - The IPv4 6to4-embedded address if present or None if the - address doesn't appear to contain a 6to4 embedded address. - - """ - if (self._ip >> 112) != 0x2002: - return None - return IPv4Address((self._ip >> 80) & 0xFFFFFFFF) - - @property - def is_unspecified(self): - """Test if the address is unspecified. - - Returns: - A boolean, True if this is the unspecified address as defined in - RFC 2373 2.5.2. - - """ - if isinstance(self, (IPv6Network, IPv6Interface)): - return int(self.network_address) == 0 and getattr( - self, '_prefixlen', 128) == 128 - return self._ip == 0 - - @property - def is_loopback(self): - """Test if the address is a loopback address. - - Returns: - A boolean, True if the address is a loopback address as defined in - RFC 2373 2.5.3. - - """ - if isinstance(self, IPv6Network): - return int(self) == 1 and getattr( - self, '_prefixlen', 128) == 128 - elif isinstance(self, IPv6Interface): - return int(self.network.network_address) == 1 and getattr( - self, '_prefixlen', 128) == 128 - return self._ip == 1 - - -class IPv6Address(_BaseV6, _BaseAddress): - - """Represent and manipulate single IPv6 Addresses.""" - - def __init__(self, address): - """Instantiate a new IPv6 address object. - - Args: - address: A string or integer representing the IP - - Additionally, an integer can be passed, so - IPv6Address('2001:db8::') == - IPv6Address(42540766411282592856903984951653826560) - or, more generally - IPv6Address(int(IPv6Address('2001:db8::'))) == - IPv6Address('2001:db8::') - - Raises: - AddressValueError: If address isn't a valid IPv6 address. - - """ - _BaseAddress.__init__(self, address) - _BaseV6.__init__(self, address) - - # Efficient constructor from integer. - if isinstance(address, int): - self._ip = address - if address < 0 or address > self._ALL_ONES: - raise AddressValueError(address) - return - - # Constructing from a packed address - if isinstance(address, bytes) and len(address) == 16: - tmp = struct.unpack('!QQ', address) - self._ip = (tmp[0] << 64) | tmp[1] - return - - # Assume input argument to be string or any object representation - # which converts into a formatted IP string. - addr_str = str(address) - if not addr_str: - raise AddressValueError('') - - self._ip = self._ip_int_from_string(addr_str) - - @property - def packed(self): - """The binary representation of this address.""" - return v6_int_to_packed(self._ip) - - -class IPv6Interface(IPv6Address): - - def __init__(self, address): - if isinstance(address, (bytes, int)): - IPv6Address.__init__(self, address) - self.network = IPv6Network(self._ip) - self._prefixlen = self._max_prefixlen - return - - addr = str(address).split('/') - IPv6Address.__init__(self, addr[0]) - self.network = IPv6Network(address, strict=False) - self.netmask = self.network.netmask - self._prefixlen = self.network._prefixlen - self.hostmask = self.network.hostmask - - def __str__(self): - return '%s/%d' % (self._string_from_ip_int(self._ip), - self.network.prefixlen) - - def __eq__(self, other): - try: - return (IPv6Address.__eq__(self, other) and - self.network == other.network) - except AttributeError: - return NotImplemented - - def __hash__(self): - return self._ip ^ self._prefixlen ^ int(self.network.network_address) - - @property - def prefixlen(self): - return self._prefixlen - - @property - def ip(self): - return IPv6Address(self._ip) - - @property - def with_prefixlen(self): - return self - - @property - def with_netmask(self): - return self.with_prefixlen - - @property - def with_hostmask(self): - return '%s/%s' % (self._string_from_ip_int(self._ip), - self.hostmask) - - -class IPv6Network(_BaseV6, _BaseNetwork): - - """This class represents and manipulates 128-bit IPv6 networks. - - Attributes: [examples for IPv6('2001:db8::1000/124')] - .network_address: IPv6Address('2001:db8::1000') - .hostmask: IPv6Address('::f') - .broadcast_address: IPv6Address('2001:db8::100f') - .netmask: IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0') - .prefixlen: 124 - - """ - - # Class to use when creating address objects - # TODO (ncoghlan): Investigate using IPv6Interface instead - _address_class = IPv6Address - - def __init__(self, address, strict=True): - """Instantiate a new IPv6 Network object. - - Args: - address: A string or integer representing the IPv6 network or the - IP and prefix/netmask. - '2001:db8::/128' - '2001:db8:0000:0000:0000:0000:0000:0000/128' - '2001:db8::' - are all functionally the same in IPv6. That is to say, - failing to provide a subnetmask will create an object with - a mask of /128. - - Additionally, an integer can be passed, so - IPv6Network('2001:db8::') == - IPv6Network(42540766411282592856903984951653826560) - or, more generally - IPv6Network(int(IPv6Network('2001:db8::'))) == - IPv6Network('2001:db8::') - - strict: A boolean. If true, ensure that we have been passed - A true network address, eg, 2001:db8::1000/124 and not an - IP address on a network, eg, 2001:db8::1/124. - - Raises: - AddressValueError: If address isn't a valid IPv6 address. - NetmaskValueError: If the netmask isn't valid for - an IPv6 address. - ValueError: If strict was True and a network address was not - supplied. - - """ - _BaseV6.__init__(self, address) - _BaseNetwork.__init__(self, address) - - # Efficient constructor from integer. - if isinstance(address, int): - if address < 0 or address > self._ALL_ONES: - raise AddressValueError(address) - self.network_address = IPv6Address(address) - self._prefixlen = self._max_prefixlen - self.netmask = IPv6Address(self._ALL_ONES) - return - - # Constructing from a packed address - if isinstance(address, bytes) and len(address) == 16: - tmp = struct.unpack('!QQ', address) - self.network_address = IPv6Address((tmp[0] << 64) | tmp[1]) - self._prefixlen = self._max_prefixlen - self.netmask = IPv6Address(self._ALL_ONES) - return - - # Assume input argument to be string or any object representation - # which converts into a formatted IP prefix string. - addr = str(address).split('/') - - if len(addr) > 2: - raise AddressValueError(address) - - self.network_address = IPv6Address(self._ip_int_from_string(addr[0])) - - if len(addr) == 2: - if self._is_valid_netmask(addr[1]): - self._prefixlen = int(addr[1]) - else: - raise NetmaskValueError(addr[1]) - else: - self._prefixlen = self._max_prefixlen - - self.netmask = IPv6Address(self._ip_int_from_prefix(self._prefixlen)) - if strict: - if (IPv6Address(int(self.network_address) & int(self.netmask)) != - self.network_address): - raise ValueError('%s has host bits set' % str(self)) - self.network_address = IPv6Address(int(self.network_address) & - int(self.netmask)) - - if self._prefixlen == (self._max_prefixlen - 1): - self.hosts = self.__iter__ - - def _is_valid_netmask(self, prefixlen): - """Verify that the netmask/prefixlen is valid. - - Args: - prefixlen: A string, the netmask in prefix length format. - - Returns: - A boolean, True if the prefix represents a valid IPv6 - netmask. - - """ - try: - prefixlen = int(prefixlen) - except ValueError: - return False - return 0 <= prefixlen <= self._max_prefixlen diff -r 2059910e7d76 -r ac776ef41428 Lib/json/encoder.py --- a/Lib/json/encoder.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/json/encoder.py Mon May 21 23:01:17 2012 -0400 @@ -27,7 +27,8 @@ ESCAPE_DCT.setdefault(chr(i), '\\u{0:04x}'.format(i)) #ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,)) -INFINITY = float('inf') +# Assume this produces an infinity on all machines (probably not guaranteed) +INFINITY = float('1e66666') FLOAT_REPR = repr def encode_basestring(s): diff -r 2059910e7d76 -r ac776ef41428 Lib/lzma.py --- a/Lib/lzma.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/lzma.py Mon May 21 23:01:17 2012 -0400 @@ -18,11 +18,10 @@ "MODE_FAST", "MODE_NORMAL", "PRESET_DEFAULT", "PRESET_EXTREME", "LZMACompressor", "LZMADecompressor", "LZMAFile", "LZMAError", - "open", "compress", "decompress", "is_check_supported", + "compress", "decompress", "is_check_supported", "encode_filter_properties", "decode_filter_properties", ] -import builtins import io from _lzma import * @@ -47,16 +46,16 @@ """ def __init__(self, filename=None, mode="r", *, - format=None, check=-1, preset=None, filters=None): - """Open an LZMA-compressed file in binary mode. + fileobj=None, format=None, check=-1, + preset=None, filters=None): + """Open an LZMA-compressed file. - filename can be either an actual file name (given as a str or - bytes object), in which case the named file is opened, or it can - be an existing file object to read from or write to. + If filename is given, open the named file. Otherwise, operate on + the file object given by fileobj. Exactly one of these two + parameters should be provided. mode can be "r" for reading (default), "w" for (over)writing, or - "a" for appending. These can equivalently be given as "rb", "wb", - and "ab" respectively. + "a" for appending. format specifies the container format to use for the file. If mode is "r", this defaults to FORMAT_AUTO. Otherwise, the @@ -95,7 +94,7 @@ self._pos = 0 self._size = -1 - if mode in ("r", "rb"): + if mode == "r": if check != -1: raise ValueError("Cannot specify an integrity check " "when opening a file for reading") @@ -111,7 +110,7 @@ self._init_args = {"format":format, "filters":filters} self._decompressor = LZMADecompressor(**self._init_args) self._buffer = None - elif mode in ("w", "wb", "a", "ab"): + elif mode in ("w", "a"): if format is None: format = FORMAT_XZ mode_code = _MODE_WRITE @@ -120,17 +119,16 @@ else: raise ValueError("Invalid mode: {!r}".format(mode)) - if isinstance(filename, (str, bytes)): - if "b" not in mode: - mode += "b" - self._fp = builtins.open(filename, mode) + if filename is not None and fileobj is None: + mode += "b" + self._fp = open(filename, mode) self._closefp = True self._mode = mode_code - elif hasattr(filename, "read") or hasattr(filename, "write"): - self._fp = filename + elif fileobj is not None and filename is None: + self._fp = fileobj self._mode = mode_code else: - raise TypeError("filename must be a str or bytes object, or a file") + raise ValueError("Must give exactly one of filename and fileobj") def close(self): """Flush and close the file. @@ -371,51 +369,6 @@ return self._pos -def open(filename, mode="rb", *, - format=None, check=-1, preset=None, filters=None, - encoding=None, errors=None, newline=None): - """Open an LZMA-compressed file in binary or text mode. - - filename can be either an actual file name (given as a str or bytes object), - in which case the named file is opened, or it can be an existing file object - to read from or write to. - - The mode argument can be "r", "rb" (default), "w", "wb", "a", or "ab" for - binary mode, or "rt", "wt" or "at" for text mode. - - The format, check, preset and filters arguments specify the compression - settings, as for LZMACompressor, LZMADecompressor and LZMAFile. - - For binary mode, this function is equivalent to the LZMAFile constructor: - LZMAFile(filename, mode, ...). In this case, the encoding, errors and - newline arguments must not be provided. - - For text mode, a LZMAFile object is created, and wrapped in an - io.TextIOWrapper instance with the specified encoding, error handling - behavior, and line ending(s). - - """ - if "t" in mode: - if "b" in mode: - raise ValueError("Invalid mode: %r" % (mode,)) - else: - if encoding is not None: - raise ValueError("Argument 'encoding' not supported in binary mode") - if errors is not None: - raise ValueError("Argument 'errors' not supported in binary mode") - if newline is not None: - raise ValueError("Argument 'newline' not supported in binary mode") - - lz_mode = mode.replace("t", "") - binary_file = LZMAFile(filename, lz_mode, format=format, check=check, - preset=preset, filters=filters) - - if "t" in mode: - return io.TextIOWrapper(binary_file, encoding, errors, newline) - else: - return binary_file - - def compress(data, format=FORMAT_XZ, check=-1, preset=None, filters=None): """Compress a block of data. diff -r 2059910e7d76 -r ac776ef41428 Lib/multiprocessing/__init__.py --- a/Lib/multiprocessing/__init__.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/multiprocessing/__init__.py Mon May 21 23:01:17 2012 -0400 @@ -228,19 +228,19 @@ from multiprocessing.sharedctypes import RawArray return RawArray(typecode_or_type, size_or_initializer) -def Value(typecode_or_type, *args, lock=True): +def Value(typecode_or_type, *args, **kwds): ''' Returns a synchronized shared object ''' from multiprocessing.sharedctypes import Value - return Value(typecode_or_type, *args, lock=lock) + return Value(typecode_or_type, *args, **kwds) -def Array(typecode_or_type, size_or_initializer, *, lock=True): +def Array(typecode_or_type, size_or_initializer, **kwds): ''' Returns a synchronized shared array ''' from multiprocessing.sharedctypes import Array - return Array(typecode_or_type, size_or_initializer, lock=lock) + return Array(typecode_or_type, size_or_initializer, **kwds) # # diff -r 2059910e7d76 -r ac776ef41428 Lib/multiprocessing/dummy/__init__.py --- a/Lib/multiprocessing/dummy/__init__.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/multiprocessing/dummy/__init__.py Mon May 21 23:01:17 2012 -0400 @@ -68,8 +68,7 @@ def start(self): assert self._parent is current_process() self._start_called = True - if hasattr(self._parent, '_children'): - self._parent._children[self] = None + self._parent._children[self] = None threading.Thread.start(self) @property diff -r 2059910e7d76 -r ac776ef41428 Lib/multiprocessing/forking.py --- a/Lib/multiprocessing/forking.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/multiprocessing/forking.py Mon May 21 23:01:17 2012 -0400 @@ -209,9 +209,6 @@ _tls = _thread._local() def __init__(self, process_obj): - cmd = ' '.join('"%s"' % x for x in get_command_line()) - prep_data = get_preparation_data(process_obj._name) - # create pipe for communication with child rfd, wfd = os.pipe() @@ -219,30 +216,31 @@ rhandle = duplicate(msvcrt.get_osfhandle(rfd), inheritable=True) os.close(rfd) - with open(wfd, 'wb', closefd=True) as to_child: - # start process - try: - hp, ht, pid, tid = _winapi.CreateProcess( - _python_exe, cmd + (' %s' % rhandle), - None, None, 1, 0, None, None, None - ) - _winapi.CloseHandle(ht) - finally: - close(rhandle) + # start process + cmd = get_command_line() + [rhandle] + cmd = ' '.join('"%s"' % x for x in cmd) + hp, ht, pid, tid = _winapi.CreateProcess( + _python_exe, cmd, None, None, 1, 0, None, None, None + ) + _winapi.CloseHandle(ht) + close(rhandle) - # set attributes of self - self.pid = pid - self.returncode = None - self._handle = hp - self.sentinel = int(hp) + # set attributes of self + self.pid = pid + self.returncode = None + self._handle = hp + self.sentinel = int(hp) - # send information to child - Popen._tls.process_handle = int(hp) - try: - dump(prep_data, to_child, HIGHEST_PROTOCOL) - dump(process_obj, to_child, HIGHEST_PROTOCOL) - finally: - del Popen._tls.process_handle + # send information to child + prep_data = get_preparation_data(process_obj._name) + to_child = os.fdopen(wfd, 'wb') + Popen._tls.process_handle = int(hp) + try: + dump(prep_data, to_child, HIGHEST_PROTOCOL) + dump(process_obj, to_child, HIGHEST_PROTOCOL) + finally: + del Popen._tls.process_handle + to_child.close() @staticmethod def thread_is_spawning(): @@ -326,8 +324,7 @@ return [sys.executable, '--multiprocessing-fork'] else: prog = 'from multiprocessing.forking import main; main()' - opts = util._args_from_interpreter_flags() - return [_python_exe] + opts + ['-c', prog, '--multiprocessing-fork'] + return [_python_exe, '-c', prog, '--multiprocessing-fork'] def main(): diff -r 2059910e7d76 -r ac776ef41428 Lib/multiprocessing/heap.py --- a/Lib/multiprocessing/heap.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/multiprocessing/heap.py Mon May 21 23:01:17 2012 -0400 @@ -205,7 +205,7 @@ self._lock.release() # -# Class representing a chunk of an mmap -- can be inherited by child process +# Class representing a chunk of an mmap -- can be inherited # class BufferWrapper(object): @@ -218,6 +218,11 @@ self._state = (block, size) Finalize(self, BufferWrapper._heap.free, args=(block,)) - def create_memoryview(self): + def get_address(self): (arena, start, stop), size = self._state - return memoryview(arena.buffer)[start:start+size] + address, length = _multiprocessing.address_of_buffer(arena.buffer) + assert size <= length + return address + start + + def get_size(self): + return self._state[1] diff -r 2059910e7d76 -r ac776ef41428 Lib/multiprocessing/managers.py --- a/Lib/multiprocessing/managers.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/multiprocessing/managers.py Mon May 21 23:01:17 2012 -0400 @@ -1035,11 +1035,12 @@ BaseListProxy = MakeProxyType('BaseListProxy', ( - '__add__', '__contains__', '__delitem__', '__getitem__', '__len__', - '__mul__', '__reversed__', '__rmul__', '__setitem__', + '__add__', '__contains__', '__delitem__', '__delslice__', + '__getitem__', '__getslice__', '__len__', '__mul__', + '__reversed__', '__rmul__', '__setitem__', '__setslice__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort', '__imul__' - )) + )) # XXX __getslice__ and __setslice__ unneeded in Py3.0 class ListProxy(BaseListProxy): def __iadd__(self, value): self._callmethod('extend', (value,)) @@ -1057,8 +1058,8 @@ ArrayProxy = MakeProxyType('ArrayProxy', ( - '__len__', '__getitem__', '__setitem__' - )) + '__len__', '__getitem__', '__setitem__', '__getslice__', '__setslice__' + )) # XXX __getslice__ and __setslice__ unneeded in Py3.0 PoolProxy = MakeProxyType('PoolProxy', ( diff -r 2059910e7d76 -r ac776ef41428 Lib/multiprocessing/pool.py --- a/Lib/multiprocessing/pool.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/multiprocessing/pool.py Mon May 21 23:01:17 2012 -0400 @@ -526,26 +526,32 @@ class ApplyResult(object): def __init__(self, cache, callback, error_callback): - self._event = threading.Event() + self._cond = threading.Condition(threading.Lock()) self._job = next(job_counter) self._cache = cache + self._ready = False self._callback = callback self._error_callback = error_callback cache[self._job] = self def ready(self): - return self._event.is_set() + return self._ready def successful(self): - assert self.ready() + assert self._ready return self._success def wait(self, timeout=None): - self._event.wait(timeout) + self._cond.acquire() + try: + if not self._ready: + self._cond.wait(timeout) + finally: + self._cond.release() def get(self, timeout=None): self.wait(timeout) - if not self.ready(): + if not self._ready: raise TimeoutError if self._success: return self._value @@ -558,7 +564,12 @@ self._callback(self._value) if self._error_callback and not self._success: self._error_callback(self._value) - self._event.set() + self._cond.acquire() + try: + self._ready = True + self._cond.notify() + finally: + self._cond.release() del self._cache[self._job] # @@ -575,8 +586,7 @@ self._chunksize = chunksize if chunksize <= 0: self._number_left = 0 - self._event.set() - del cache[self._job] + self._ready = True else: self._number_left = length//chunksize + bool(length % chunksize) @@ -589,14 +599,24 @@ if self._callback: self._callback(self._value) del self._cache[self._job] - self._event.set() + self._cond.acquire() + try: + self._ready = True + self._cond.notify() + finally: + self._cond.release() else: self._success = False self._value = result if self._error_callback: self._error_callback(self._value) del self._cache[self._job] - self._event.set() + self._cond.acquire() + try: + self._ready = True + self._cond.notify() + finally: + self._cond.release() # # Class whose instances are returned by `Pool.imap()` diff -r 2059910e7d76 -r ac776ef41428 Lib/multiprocessing/process.py --- a/Lib/multiprocessing/process.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/multiprocessing/process.py Mon May 21 23:01:17 2012 -0400 @@ -262,11 +262,11 @@ except SystemExit as e: if not e.args: exitcode = 1 - elif isinstance(e.args[0], int): + elif type(e.args[0]) is int: exitcode = e.args[0] else: - sys.stderr.write(str(e.args[0]) + '\n') - exitcode = 0 if isinstance(e.args[0], str) else 1 + sys.stderr.write(e.args[0] + '\n') + exitcode = 1 except: exitcode = 1 import traceback diff -r 2059910e7d76 -r ac776ef41428 Lib/multiprocessing/sharedctypes.py --- a/Lib/multiprocessing/sharedctypes.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/multiprocessing/sharedctypes.py Mon May 21 23:01:17 2012 -0400 @@ -63,7 +63,7 @@ result.__init__(*size_or_initializer) return result -def Value(typecode_or_type, *args, lock=True): +def Value(typecode_or_type, *args, lock=None): ''' Return a synchronization wrapper for a Value ''' @@ -76,10 +76,13 @@ raise AttributeError("'%r' has no method 'acquire'" % lock) return synchronized(obj, lock) -def Array(typecode_or_type, size_or_initializer, *, lock=True): +def Array(typecode_or_type, size_or_initializer, **kwds): ''' Return a synchronization wrapper for a RawArray ''' + lock = kwds.pop('lock', None) + if kwds: + raise ValueError('unrecognized keyword argument(s): %s' % list(kwds.keys())) obj = RawArray(typecode_or_type, size_or_initializer) if lock is False: return obj @@ -129,8 +132,7 @@ if length is not None: type_ = type_ * length ForkingPickler.register(type_, reduce_ctype) - buf = wrapper.create_memoryview() - obj = type_.from_buffer(buf) + obj = type_.from_address(wrapper.get_address()) obj._wrapper = wrapper return obj diff -r 2059910e7d76 -r ac776ef41428 Lib/multiprocessing/synchronize.py --- a/Lib/multiprocessing/synchronize.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/multiprocessing/synchronize.py Mon May 21 23:01:17 2012 -0400 @@ -216,7 +216,7 @@ try: # wait for notification or timeout - return self._wait_semaphore.acquire(True, timeout) + ret = self._wait_semaphore.acquire(True, timeout) finally: # indicate that this thread has woken self._woken_count.release() @@ -224,6 +224,7 @@ # reacquire lock for i in range(count): self._lock.acquire() + return ret def notify(self): assert self._lock._semlock._is_mine(), 'lock is not owned' diff -r 2059910e7d76 -r ac776ef41428 Lib/multiprocessing/util.py --- a/Lib/multiprocessing/util.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/multiprocessing/util.py Mon May 21 23:01:17 2012 -0400 @@ -7,15 +7,12 @@ # Licensed to PSF under a Contributor Agreement. # -import sys import functools -import os import itertools import weakref import atexit import threading # we want threading to install it's # cleanup function before multiprocessing does -from subprocess import _args_from_interpreter_flags from multiprocessing.process import current_process, active_children @@ -162,7 +159,6 @@ self._args = args self._kwargs = kwargs or {} self._key = (exitpriority, next(_finalizer_counter)) - self._pid = os.getpid() _finalizer_registry[self._key] = self @@ -170,7 +166,7 @@ # Need to bind these locally because the globals can have # been cleared at shutdown _finalizer_registry=_finalizer_registry, - sub_debug=sub_debug, getpid=os.getpid): + sub_debug=sub_debug): ''' Run the callback unless it has already been called or cancelled ''' @@ -179,13 +175,9 @@ except KeyError: sub_debug('finalizer no longer registered') else: - if self._pid != getpid(): - sub_debug('finalizer ignored because different process') - res = None - else: - sub_debug('finalizer calling %s with args %s and kwargs %s', - self._callback, self._args, self._kwargs) - res = self._callback(*self._args, **self._kwargs) + sub_debug('finalizer calling %s with args %s and kwargs %s', + self._callback, self._args, self._kwargs) + res = self._callback(*self._args, **self._kwargs) self._weakref = self._callback = self._args = \ self._kwargs = self._key = None return res diff -r 2059910e7d76 -r ac776ef41428 Lib/nntplib.py --- a/Lib/nntplib.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/nntplib.py Mon May 21 23:01:17 2012 -0400 @@ -166,7 +166,7 @@ parts.append(v.decode(enc or 'ascii')) else: parts.append(v) - return ''.join(parts) + return ' '.join(parts) def _parse_overview_fmt(lines): """Parse a list of string representing the response to LIST OVERVIEW.FMT diff -r 2059910e7d76 -r ac776ef41428 Lib/opcode.py --- a/Lib/opcode.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/opcode.py Mon May 21 23:01:17 2012 -0400 @@ -6,7 +6,7 @@ __all__ = ["cmp_op", "hasconst", "hasname", "hasjrel", "hasjabs", "haslocal", "hascompare", "hasfree", "opname", "opmap", - "HAVE_ARGUMENT", "EXTENDED_ARG", "hasnargs"] + "HAVE_ARGUMENT", "EXTENDED_ARG"] cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is', 'is not', 'exception match', 'BAD') @@ -18,7 +18,6 @@ haslocal = [] hascompare = [] hasfree = [] -hasnargs = [] opmap = {} opname = [''] * 256 @@ -153,7 +152,6 @@ def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3) def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8) -hasnargs.append(131) def_op('MAKE_FUNCTION', 132) # Number of args with default values def_op('BUILD_SLICE', 133) # Number of items def_op('MAKE_CLOSURE', 134) @@ -167,11 +165,8 @@ hasfree.append(138) def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8) -hasnargs.append(140) def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8) -hasnargs.append(141) def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8) -hasnargs.append(142) jrel_op('SETUP_WITH', 143) diff -r 2059910e7d76 -r ac776ef41428 Lib/os.py --- a/Lib/os.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/os.py Mon May 21 23:01:17 2012 -0400 @@ -30,9 +30,8 @@ # Note: more names are added to __all__ later. __all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep", - "defpath", "name", "path", "devnull", "SEEK_SET", "SEEK_CUR", - "SEEK_END", "fsencode", "fsdecode", "get_exec_path", "fdopen", - "popen", "extsep"] + "defpath", "name", "path", "devnull", + "SEEK_SET", "SEEK_CUR", "SEEK_END"] def _exists(name): return name in globals() @@ -51,7 +50,6 @@ from posix import * try: from posix import _exit - __all__.append('_exit') except ImportError: pass import posixpath as path @@ -66,7 +64,6 @@ from nt import * try: from nt import _exit - __all__.append('_exit') except ImportError: pass import ntpath as path @@ -81,7 +78,6 @@ from os2 import * try: from os2 import _exit - __all__.append('_exit') except ImportError: pass if sys.version.find('EMX GCC') == -1: @@ -100,7 +96,6 @@ from ce import * try: from ce import _exit - __all__.append('_exit') except ImportError: pass # We can use the standard Windows path. @@ -160,20 +155,8 @@ try: mkdir(name, mode) except OSError as e: - dir_exists = path.isdir(name) - expected_mode = _get_masked_mode(mode) - if dir_exists: - # S_ISGID is automatically copied by the OS from parent to child - # directories on mkdir. Don't consider it being set to be a mode - # mismatch as mkdir does not unset it when not specified in mode. - actual_mode = st.S_IMODE(lstat(name).st_mode) & ~st.S_ISGID - else: - actual_mode = -1 - if not (e.errno == errno.EEXIST and exist_ok and dir_exists and - actual_mode == expected_mode): - if dir_exists and actual_mode != expected_mode: - e.strerror += ' (mode %o != expected mode %o)' % ( - actual_mode, expected_mode) + if not (e.errno == errno.EEXIST and exist_ok and path.isdir(name) and + st.S_IMODE(lstat(name).st_mode) == _get_masked_mode(mode)): raise def removedirs(name): @@ -717,8 +700,6 @@ P_WAIT = 0 P_NOWAIT = P_NOWAITO = 1 - __all__.extend(["P_WAIT", "P_NOWAIT", "P_NOWAITO"]) - # XXX Should we support P_DETACH? I suppose it could fork()**2 # and close the std I/O streams. Also, P_OVERLAY is the same # as execv*()? diff -r 2059910e7d76 -r ac776ef41428 Lib/packaging/command/build_ext.py --- a/Lib/packaging/command/build_ext.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/packaging/command/build_ext.py Mon May 21 23:01:17 2012 -0400 @@ -182,10 +182,7 @@ # the 'libs' directory is for binary installs - we assume that # must be the *native* platform. But we don't really support # cross-compiling via a binary install anyway, so we let it go. - # Note that we must use sys.base_exec_prefix here rather than - # exec_prefix, since the Python libs are not copied to a virtual - # environment. - self.library_dirs.append(os.path.join(sys.base_exec_prefix, 'libs')) + self.library_dirs.append(os.path.join(sys.exec_prefix, 'libs')) if self.debug: self.build_temp = os.path.join(self.build_temp, "Debug") else: @@ -378,10 +375,6 @@ # Next, compile the source code to object files. - # XXX not honouring 'define_macros' or 'undef_macros' -- the - # CCompiler API needs to change to accommodate this, and I - # want to do one thing at a time! - # Two possible sources for extra compiler arguments: # - 'extra_compile_args' in Extension object # - CFLAGS environment variable (not particularly @@ -392,7 +385,7 @@ # command-line args. Hence we combine them in order: extra_args = ext.extra_compile_args or [] - macros = ext.define_macros[:] + macros = [(macro, '1') for macro in ext.define_macros] for undef in ext.undef_macros: macros.append((undef,)) diff -r 2059910e7d76 -r ac776ef41428 Lib/packaging/command/install_data.py --- a/Lib/packaging/command/install_data.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/packaging/command/install_data.py Mon May 21 23:01:17 2012 -0400 @@ -3,6 +3,7 @@ # Contributed by Bastian Kleineidam import os +from functools import partial from shutil import Error from sysconfig import get_paths, format_value from packaging import logger @@ -27,24 +28,24 @@ def initialize_options(self): self.install_dir = None + self.distinfo_dir = None self.outfiles = [] self.data_files_out = [] self.root = None self.force = False self.data_files = self.distribution.data_files self.warn_dir = True + self.extra_dist_info = [] def finalize_options(self): self.set_undefined_options('install_dist', ('install_data', 'install_dir'), 'root', 'force') + self.set_undefined_options('install_distinfo', + 'distinfo_dir') def run(self): - self.mkpath(self.install_dir) - for _file in self.data_files.items(): - destination = convert_path(self.expand_categories(_file[1])) - dir_dest = os.path.abspath(os.path.dirname(destination)) - + def do_copy(_file, dir_dest, destination): self.mkpath(dir_dest) try: out = self.copy_file(_file[0], dir_dest)[0] @@ -55,9 +56,20 @@ self.outfiles.append(out) self.data_files_out.append((_file[0], destination)) + self.mkpath(self.install_dir) + for _file in self.data_files.items(): + destination = convert_path(self.expand_categories(_file[1])) + dir_dest = os.path.abspath(os.path.dirname(destination)) + copy = partial(do_copy, _file, dir_dest, destination) + if destination.startswith(self.distinfo_dir): + self.extra_dist_info.append(copy) + else: + copy() + def expand_categories(self, path_with_categories): local_vars = get_paths() local_vars['distribution.name'] = self.distribution.metadata['Name'] + local_vars['dist-info'] = self.distinfo_dir expanded_path = format_value(path_with_categories, local_vars) expanded_path = format_value(expanded_path, local_vars) if '{' in expanded_path and '}' in expanded_path: diff -r 2059910e7d76 -r ac776ef41428 Lib/packaging/command/install_distinfo.py --- a/Lib/packaging/command/install_distinfo.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/packaging/command/install_distinfo.py Mon May 21 23:01:17 2012 -0400 @@ -36,6 +36,7 @@ def initialize_options(self): self.install_dir = None + self.install_base = None self.installer = None self.requested = None self.no_record = None @@ -44,7 +45,7 @@ def finalize_options(self): self.set_undefined_options('install_dist', - 'installer', 'requested', 'no_record') + 'installer', 'requested', 'no_record', 'install_base') self.set_undefined_options('install_lib', 'install_dir') @@ -63,26 +64,48 @@ basename = metadata.get_fullname(filesafe=True) + ".dist-info" - self.install_dir = os.path.join(self.install_dir, basename) - + self.distinfo_dir = os.path.join(self.install_dir, basename) + + @staticmethod + def pep376path(fpath, install_base, install_dir): + """Return PEP-376 path (but always with platform path separators)""" + if install_base is None: # needed for unit tests + return install_dir + # install_base is 'prefix' from PEP-376 + install_base = install_base.rstrip(os.path.sep) + os.path.sep + # install_dir is 'base location' from PEP-376 + install_dir = install_dir.rstrip(os.path.sep) + os.path.sep + install_dir_in_base = install_dir.startswith(install_base) + if fpath.startswith(install_dir) or \ + fpath.startswith(install_base) and install_dir_in_base: + rpath = os.path.relpath(fpath, install_dir) + else: + rpath = os.path.abspath(fpath) + return rpath + def run(self): - target = self.install_dir + target = self.distinfo_dir if os.path.isdir(target) and not os.path.islink(target): if not self.dry_run: rmtree(target) elif os.path.exists(target): - self.execute(os.unlink, (self.install_dir,), + self.execute(os.unlink, (self.distinfo_dir,), "removing " + target) self.execute(os.makedirs, (target,), "creating " + target) + + if not self.dry_run: + install_data = self.get_finalized_command('install_data') + while install_data.extra_dist_info: + install_data.extra_dist_info.pop()() - metadata_path = os.path.join(self.install_dir, 'METADATA') + metadata_path = os.path.join(self.distinfo_dir, 'METADATA') self.execute(self.distribution.metadata.write, (metadata_path,), "creating " + metadata_path) self.outfiles.append(metadata_path) - installer_path = os.path.join(self.install_dir, 'INSTALLER') + installer_path = os.path.join(self.distinfo_dir, 'INSTALLER') logger.info('creating %s', installer_path) if not self.dry_run: with open(installer_path, 'w') as f: @@ -90,7 +113,7 @@ self.outfiles.append(installer_path) if self.requested: - requested_path = os.path.join(self.install_dir, 'REQUESTED') + requested_path = os.path.join(self.distinfo_dir, 'REQUESTED') logger.info('creating %s', requested_path) if not self.dry_run: open(requested_path, 'wb').close() @@ -99,7 +122,7 @@ if not self.no_resources: install_data = self.get_finalized_command('install_data') if install_data.get_resources_out() != []: - resources_path = os.path.join(self.install_dir, + resources_path = os.path.join(self.distinfo_dir, 'RESOURCES') logger.info('creating %s', resources_path) if not self.dry_run: @@ -113,7 +136,7 @@ self.outfiles.append(resources_path) if not self.no_record: - record_path = os.path.join(self.install_dir, 'RECORD') + record_path = os.path.join(self.distinfo_dir, 'RECORD') logger.info('creating %s', record_path) if not self.dry_run: with open(record_path, 'w', encoding='utf-8') as f: @@ -121,22 +144,23 @@ lineterminator='\n', quotechar='"') - install = self.get_finalized_command('install_dist') + install = self.get_finalized_command('install_dist') for fpath in install.get_outputs(): + rpath = self.pep376path(fpath, self.install_base, self.install_dir) if fpath.endswith('.pyc') or fpath.endswith('.pyo'): # do not put size and md5 hash, as in PEP-376 - writer.writerow((fpath, '', '')) + writer.writerow((rpath, '', '')) else: size = os.path.getsize(fpath) with open(fpath, 'rb') as fp: hash = hashlib.md5() hash.update(fp.read()) md5sum = hash.hexdigest() - writer.writerow((fpath, md5sum, size)) + writer.writerow((rpath, md5sum, size)) # add the RECORD file itself - writer.writerow((record_path, '', '')) + writer.writerow((self.pep376path(record_path, self.install_base, self.install_dir), '', '')) self.outfiles.append(record_path) def get_outputs(self): diff -r 2059910e7d76 -r ac776ef41428 Lib/packaging/database.py --- a/Lib/packaging/database.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/packaging/database.py Mon May 21 23:01:17 2012 -0400 @@ -244,11 +244,6 @@ 'dist-info file %r does not belong to the %r %s ' 'distribution' % (path, self.name, self.version)) - # The file must be relative - if path not in DIST_FILES: - raise PackagingError('invalid path for a dist-info file: %r' % - path) - path = os.path.join(self.path, path) return open(path, open_flags) diff -r 2059910e7d76 -r ac776ef41428 Lib/packaging/tests/test_command_install_data.py --- a/Lib/packaging/tests/test_command_install_data.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/packaging/tests/test_command_install_data.py Mon May 21 23:01:17 2012 -0400 @@ -95,10 +95,12 @@ scripts_dir = self.mkdtemp() project_dir, dist = self.create_dist( name='Spamlib', version='0.1', - data_files={'spamd': '{scripts}/spamd'}) + data_files={'spamd': '{scripts}/spamd', + 'entry_points.txt': '{dist-info}/entry_points.txt'}) os.chdir(project_dir) self.write_file('spamd', '# Python script') + self.write_file('entry_points.txt', '[entry_points]\nham = eggs:cheese') sysconfig._SCHEMES.set(_get_default_scheme(), 'scripts', scripts_dir) sys.path.insert(0, install_dir) packaging.database.disable_cache() @@ -119,18 +121,21 @@ cmd = install_distinfo(dist) cmd.ensure_finalized() dist.command_obj['install_distinfo'] = cmd - cmd.run() + cmd.run() # first a few sanity checks self.assertEqual(os.listdir(scripts_dir), ['spamd']) - self.assertEqual(os.listdir(install_dir), ['Spamlib-0.1.dist-info']) + self.assertEqual(os.listdir(install_dir), ['Spamlib-0.1.dist-info']) # now the real test fn = os.path.join(install_dir, 'Spamlib-0.1.dist-info', 'RESOURCES') with open(fn, encoding='utf-8') as fp: content = fp.read().strip() - expected = 'spamd,%s' % os.path.join(scripts_dir, 'spamd') + expected = 'spamd,%s\nentry_points.txt,%s' % (os.path.join(scripts_dir, 'spamd'), + os.path.join(install_dir, + 'Spamlib-0.1.dist-info', + 'entry_points.txt')) self.assertEqual(content, expected) # just to be sure, we also test that get_file works here, even though diff -r 2059910e7d76 -r ac776ef41428 Lib/pkgutil.py --- a/Lib/pkgutil.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/pkgutil.py Mon May 21 23:01:17 2012 -0400 @@ -515,29 +515,19 @@ pname = os.path.join(*name.split('.')) # Reconstitute as relative path sname_pkg = name + ".pkg" + init_py = "__init__.py" path = path[:] # Start with a copy of the existing path for dir in sys.path: - if not isinstance(dir, str): + if not isinstance(dir, str) or not os.path.isdir(dir): continue - - finder = get_importer(dir) - if finder is not None: - # Is this finder PEP 420 compliant? - if hasattr(finder, 'find_loader'): - loader, portions = finder.find_loader(name) - else: - # No, no need to call it - loader = None - portions = [] - - for portion in portions: - # XXX This may still add duplicate entries to path on - # case-insensitive filesystems - if portion not in path: - path.append(portion) - + subdir = os.path.join(dir, pname) + # XXX This may still add duplicate entries to path on + # case-insensitive filesystems + initfile = os.path.join(subdir, init_py) + if subdir not in path and os.path.isfile(initfile): + path.append(subdir) # XXX Is this the right thing for subpackages like zope.app? # It looks for a file named "zope.app.pkg" pkgfile = os.path.join(dir, sname_pkg) diff -r 2059910e7d76 -r ac776ef41428 Lib/plistlib.py --- a/Lib/plistlib.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/plistlib.py Mon May 21 23:01:17 2012 -0400 @@ -237,26 +237,20 @@ self.endElement("data") def writeDict(self, d): - if d: - self.beginElement("dict") - items = sorted(d.items()) - for key, value in items: - if not isinstance(key, str): - raise TypeError("keys must be strings") - self.simpleElement("key", key) - self.writeValue(value) - self.endElement("dict") - else: - self.simpleElement("dict") + self.beginElement("dict") + items = sorted(d.items()) + for key, value in items: + if not isinstance(key, str): + raise TypeError("keys must be strings") + self.simpleElement("key", key) + self.writeValue(value) + self.endElement("dict") def writeArray(self, array): - if array: - self.beginElement("array") - for value in array: - self.writeValue(value) - self.endElement("array") - else: - self.simpleElement("array") + self.beginElement("array") + for value in array: + self.writeValue(value) + self.endElement("array") class _InternalDict(dict): diff -r 2059910e7d76 -r ac776ef41428 Lib/pyclbr.py --- a/Lib/pyclbr.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/pyclbr.py Mon May 21 23:01:17 2012 -0400 @@ -130,8 +130,6 @@ parent = _readmodule(package, path, inpackage) if inpackage is not None: package = "%s.%s" % (inpackage, package) - if not '__path__' in parent: - raise ImportError('No package named {}'.format(package)) return _readmodule(submodule, parent['__path__'], package) # Search the path for the module diff -r 2059910e7d76 -r ac776ef41428 Lib/pydoc.py --- a/Lib/pydoc.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/pydoc.py Mon May 21 23:01:17 2012 -0400 @@ -167,7 +167,7 @@ if name in {'__builtins__', '__doc__', '__file__', '__path__', '__module__', '__name__', '__slots__', '__package__', '__cached__', '__author__', '__credits__', '__date__', - '__version__', '__qualname__', '__initializing__'}: + '__version__', '__qualname__'}: return 0 # Private names are hidden, but special names are displayed. if name.startswith('__') and name.endswith('__'): return 1 @@ -369,7 +369,7 @@ docloc = os.environ.get("PYTHONDOCS", self.PYTHONDOCS) - basedir = os.path.join(sys.base_exec_prefix, "lib", + basedir = os.path.join(sys.exec_prefix, "lib", "python%d.%d" % sys.version_info[:2]) if (isinstance(object, type(os)) and (object.__name__ in ('errno', 'exceptions', 'gc', 'imp', diff -r 2059910e7d76 -r ac776ef41428 Lib/pydoc_data/topics.py --- a/Lib/pydoc_data/topics.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/pydoc_data/topics.py Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Autogenerated by Sphinx on Wed May 30 21:54:26 2012 +# Autogenerated by Sphinx on Tue May 1 09:25:14 2012 topics = {'assert': '\nThe ``assert`` statement\n************************\n\nAssert statements are a convenient way to insert debugging assertions\ninto a program:\n\n assert_stmt ::= "assert" expression ["," expression]\n\nThe simple form, ``assert expression``, is equivalent to\n\n if __debug__:\n if not expression: raise AssertionError\n\nThe extended form, ``assert expression1, expression2``, is equivalent\nto\n\n if __debug__:\n if not expression1: raise AssertionError(expression2)\n\nThese equivalences assume that ``__debug__`` and ``AssertionError``\nrefer to the built-in variables with those names. In the current\nimplementation, the built-in variable ``__debug__`` is ``True`` under\nnormal circumstances, ``False`` when optimization is requested\n(command line option -O). The current code generator emits no code\nfor an assert statement when optimization is requested at compile\ntime. Note that it is unnecessary to include the source code for the\nexpression that failed in the error message; it will be displayed as\npart of the stack trace.\n\nAssignments to ``__debug__`` are illegal. The value for the built-in\nvariable is determined when the interpreter starts.\n', 'assignment': '\nAssignment statements\n*********************\n\nAssignment statements are used to (re)bind names to values and to\nmodify attributes or items of mutable objects:\n\n assignment_stmt ::= (target_list "=")+ (expression_list | yield_expression)\n target_list ::= target ("," target)* [","]\n target ::= identifier\n | "(" target_list ")"\n | "[" target_list "]"\n | attributeref\n | subscription\n | slicing\n | "*" target\n\n(See section *Primaries* for the syntax definitions for the last three\nsymbols.)\n\nAn assignment statement evaluates the expression list (remember that\nthis can be a single expression or a comma-separated list, the latter\nyielding a tuple) and assigns the single resulting object to each of\nthe target lists, from left to right.\n\nAssignment is defined recursively depending on the form of the target\n(list). When a target is part of a mutable object (an attribute\nreference, subscription or slicing), the mutable object must\nultimately perform the assignment and decide about its validity, and\nmay raise an exception if the assignment is unacceptable. The rules\nobserved by various types and the exceptions raised are given with the\ndefinition of the object types (see section *The standard type\nhierarchy*).\n\nAssignment of an object to a target list, optionally enclosed in\nparentheses or square brackets, is recursively defined as follows.\n\n* If the target list is a single target: The object is assigned to\n that target.\n\n* If the target list is a comma-separated list of targets: The object\n must be an iterable with the same number of items as there are\n targets in the target list, and the items are assigned, from left to\n right, to the corresponding targets.\n\n * If the target list contains one target prefixed with an asterisk,\n called a "starred" target: The object must be a sequence with at\n least as many items as there are targets in the target list, minus\n one. The first items of the sequence are assigned, from left to\n right, to the targets before the starred target. The final items\n of the sequence are assigned to the targets after the starred\n target. A list of the remaining items in the sequence is then\n assigned to the starred target (the list can be empty).\n\n * Else: The object must be a sequence with the same number of items\n as there are targets in the target list, and the items are\n assigned, from left to right, to the corresponding targets.\n\nAssignment of an object to a single target is recursively defined as\nfollows.\n\n* If the target is an identifier (name):\n\n * If the name does not occur in a ``global`` or ``nonlocal``\n statement in the current code block: the name is bound to the\n object in the current local namespace.\n\n * Otherwise: the name is bound to the object in the global namespace\n or the outer namespace determined by ``nonlocal``, respectively.\n\n The name is rebound if it was already bound. This may cause the\n reference count for the object previously bound to the name to reach\n zero, causing the object to be deallocated and its destructor (if it\n has one) to be called.\n\n* If the target is a target list enclosed in parentheses or in square\n brackets: The object must be an iterable with the same number of\n items as there are targets in the target list, and its items are\n assigned, from left to right, to the corresponding targets.\n\n* If the target is an attribute reference: The primary expression in\n the reference is evaluated. It should yield an object with\n assignable attributes; if this is not the case, ``TypeError`` is\n raised. That object is then asked to assign the assigned object to\n the given attribute; if it cannot perform the assignment, it raises\n an exception (usually but not necessarily ``AttributeError``).\n\n Note: If the object is a class instance and the attribute reference\n occurs on both sides of the assignment operator, the RHS expression,\n ``a.x`` can access either an instance attribute or (if no instance\n attribute exists) a class attribute. The LHS target ``a.x`` is\n always set as an instance attribute, creating it if necessary.\n Thus, the two occurrences of ``a.x`` do not necessarily refer to the\n same attribute: if the RHS expression refers to a class attribute,\n the LHS creates a new instance attribute as the target of the\n assignment:\n\n class Cls:\n x = 3 # class variable\n inst = Cls()\n inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x as 3\n\n This description does not necessarily apply to descriptor\n attributes, such as properties created with ``property()``.\n\n* If the target is a subscription: The primary expression in the\n reference is evaluated. It should yield either a mutable sequence\n object (such as a list) or a mapping object (such as a dictionary).\n Next, the subscript expression is evaluated.\n\n If the primary is a mutable sequence object (such as a list), the\n subscript must yield an integer. If it is negative, the sequence\'s\n length is added to it. The resulting value must be a nonnegative\n integer less than the sequence\'s length, and the sequence is asked\n to assign the assigned object to its item with that index. If the\n index is out of range, ``IndexError`` is raised (assignment to a\n subscripted sequence cannot add new items to a list).\n\n If the primary is a mapping object (such as a dictionary), the\n subscript must have a type compatible with the mapping\'s key type,\n and the mapping is then asked to create a key/datum pair which maps\n the subscript to the assigned object. This can either replace an\n existing key/value pair with the same key value, or insert a new\n key/value pair (if no key with the same value existed).\n\n For user-defined objects, the ``__setitem__()`` method is called\n with appropriate arguments.\n\n* If the target is a slicing: The primary expression in the reference\n is evaluated. It should yield a mutable sequence object (such as a\n list). The assigned object should be a sequence object of the same\n type. Next, the lower and upper bound expressions are evaluated,\n insofar they are present; defaults are zero and the sequence\'s\n length. The bounds should evaluate to integers. If either bound is\n negative, the sequence\'s length is added to it. The resulting\n bounds are clipped to lie between zero and the sequence\'s length,\n inclusive. Finally, the sequence object is asked to replace the\n slice with the items of the assigned sequence. The length of the\n slice may be different from the length of the assigned sequence,\n thus changing the length of the target sequence, if the object\n allows it.\n\n**CPython implementation detail:** In the current implementation, the\nsyntax for targets is taken to be the same as for expressions, and\ninvalid syntax is rejected during the code generation phase, causing\nless detailed error messages.\n\nWARNING: Although the definition of assignment implies that overlaps\nbetween the left-hand side and the right-hand side are \'safe\' (for\nexample ``a, b = b, a`` swaps two variables), overlaps *within* the\ncollection of assigned-to variables are not safe! For instance, the\nfollowing program prints ``[0, 2]``:\n\n x = [0, 1]\n i = 0\n i, x[i] = 1, 2\n print(x)\n\nSee also:\n\n **PEP 3132** - Extended Iterable Unpacking\n The specification for the ``*target`` feature.\n\n\nAugmented assignment statements\n===============================\n\nAugmented assignment is the combination, in a single statement, of a\nbinary operation and an assignment statement:\n\n augmented_assignment_stmt ::= augtarget augop (expression_list | yield_expression)\n augtarget ::= identifier | attributeref | subscription | slicing\n augop ::= "+=" | "-=" | "*=" | "/=" | "//=" | "%=" | "**="\n | ">>=" | "<<=" | "&=" | "^=" | "|="\n\n(See section *Primaries* for the syntax definitions for the last three\nsymbols.)\n\nAn augmented assignment evaluates the target (which, unlike normal\nassignment statements, cannot be an unpacking) and the expression\nlist, performs the binary operation specific to the type of assignment\non the two operands, and assigns the result to the original target.\nThe target is only evaluated once.\n\nAn augmented assignment expression like ``x += 1`` can be rewritten as\n``x = x + 1`` to achieve a similar, but not exactly equal effect. In\nthe augmented version, ``x`` is only evaluated once. Also, when\npossible, the actual operation is performed *in-place*, meaning that\nrather than creating a new object and assigning that to the target,\nthe old object is modified instead.\n\nWith the exception of assigning to tuples and multiple targets in a\nsingle statement, the assignment done by augmented assignment\nstatements is handled the same way as normal assignments. Similarly,\nwith the exception of the possible *in-place* behavior, the binary\noperation performed by augmented assignment is the same as the normal\nbinary operations.\n\nFor targets which are attribute references, the same *caveat about\nclass and instance attributes* applies as for regular assignments.\n', 'atom-identifiers': '\nIdentifiers (Names)\n*******************\n\nAn identifier occurring as an atom is a name. See section\n*Identifiers and keywords* for lexical definition and section *Naming\nand binding* for documentation of naming and binding.\n\nWhen the name is bound to an object, evaluation of the atom yields\nthat object. When a name is not bound, an attempt to evaluate it\nraises a ``NameError`` exception.\n\n**Private name mangling:** When an identifier that textually occurs in\na class definition begins with two or more underscore characters and\ndoes not end in two or more underscores, it is considered a *private\nname* of that class. Private names are transformed to a longer form\nbefore code is generated for them. The transformation inserts the\nclass name in front of the name, with leading underscores removed, and\na single underscore inserted in front of the class name. For example,\nthe identifier ``__spam`` occurring in a class named ``Ham`` will be\ntransformed to ``_Ham__spam``. This transformation is independent of\nthe syntactical context in which the identifier is used. If the\ntransformed name is extremely long (longer than 255 characters),\nimplementation defined truncation may happen. If the class name\nconsists only of underscores, no transformation is done.\n', @@ -10,7 +10,7 @@ 'binary': '\nBinary arithmetic operations\n****************************\n\nThe binary arithmetic operations have the conventional priority\nlevels. Note that some of these operations also apply to certain non-\nnumeric types. Apart from the power operator, there are only two\nlevels, one for multiplicative operators and one for additive\noperators:\n\n m_expr ::= u_expr | m_expr "*" u_expr | m_expr "//" u_expr | m_expr "/" u_expr\n | m_expr "%" u_expr\n a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr\n\nThe ``*`` (multiplication) operator yields the product of its\narguments. The arguments must either both be numbers, or one argument\nmust be an integer and the other must be a sequence. In the former\ncase, the numbers are converted to a common type and then multiplied\ntogether. In the latter case, sequence repetition is performed; a\nnegative repetition factor yields an empty sequence.\n\nThe ``/`` (division) and ``//`` (floor division) operators yield the\nquotient of their arguments. The numeric arguments are first\nconverted to a common type. Integer division yields a float, while\nfloor division of integers results in an integer; the result is that\nof mathematical division with the \'floor\' function applied to the\nresult. Division by zero raises the ``ZeroDivisionError`` exception.\n\nThe ``%`` (modulo) operator yields the remainder from the division of\nthe first argument by the second. The numeric arguments are first\nconverted to a common type. A zero right argument raises the\n``ZeroDivisionError`` exception. The arguments may be floating point\nnumbers, e.g., ``3.14%0.7`` equals ``0.34`` (since ``3.14`` equals\n``4*0.7 + 0.34``.) The modulo operator always yields a result with\nthe same sign as its second operand (or zero); the absolute value of\nthe result is strictly smaller than the absolute value of the second\noperand [1].\n\nThe floor division and modulo operators are connected by the following\nidentity: ``x == (x//y)*y + (x%y)``. Floor division and modulo are\nalso connected with the built-in function ``divmod()``: ``divmod(x, y)\n== (x//y, x%y)``. [2].\n\nIn addition to performing the modulo operation on numbers, the ``%``\noperator is also overloaded by string objects to perform old-style\nstring formatting (also known as interpolation). The syntax for\nstring formatting is described in the Python Library Reference,\nsection *Old String Formatting Operations*.\n\nThe floor division operator, the modulo operator, and the ``divmod()``\nfunction are not defined for complex numbers. Instead, convert to a\nfloating point number using the ``abs()`` function if appropriate.\n\nThe ``+`` (addition) operator yields the sum of its arguments. The\narguments must either both be numbers or both sequences of the same\ntype. In the former case, the numbers are converted to a common type\nand then added together. In the latter case, the sequences are\nconcatenated.\n\nThe ``-`` (subtraction) operator yields the difference of its\narguments. The numeric arguments are first converted to a common\ntype.\n', 'bitwise': '\nBinary bitwise operations\n*************************\n\nEach of the three bitwise operations has a different priority level:\n\n and_expr ::= shift_expr | and_expr "&" shift_expr\n xor_expr ::= and_expr | xor_expr "^" and_expr\n or_expr ::= xor_expr | or_expr "|" xor_expr\n\nThe ``&`` operator yields the bitwise AND of its arguments, which must\nbe integers.\n\nThe ``^`` operator yields the bitwise XOR (exclusive OR) of its\narguments, which must be integers.\n\nThe ``|`` operator yields the bitwise (inclusive) OR of its arguments,\nwhich must be integers.\n', 'bltin-code-objects': '\nCode Objects\n************\n\nCode objects are used by the implementation to represent "pseudo-\ncompiled" executable Python code such as a function body. They differ\nfrom function objects because they don\'t contain a reference to their\nglobal execution environment. Code objects are returned by the built-\nin ``compile()`` function and can be extracted from function objects\nthrough their ``__code__`` attribute. See also the ``code`` module.\n\nA code object can be executed or evaluated by passing it (instead of a\nsource string) to the ``exec()`` or ``eval()`` built-in functions.\n\nSee *The standard type hierarchy* for more information.\n', - 'bltin-ellipsis-object': '\nThe Ellipsis Object\n*******************\n\nThis object is commonly used by slicing (see *Slicings*). It supports\nno special operations. There is exactly one ellipsis object, named\n``Ellipsis`` (a built-in name). ``type(Ellipsis)()`` produces the\n``Ellipsis`` singleton.\n\nIt is written as ``Ellipsis`` or ``...``.\n', + 'bltin-ellipsis-object': '\nThe Ellipsis Object\n*******************\n\nThis object is commonly used by slicing (see *Slicings*), but may also\nbe used in other situations where a sentinel value other than ``None``\nis needed. It supports no special operations. There is exactly one\nellipsis object, named ``Ellipsis`` (a built-in name).\n``type(Ellipsis)()`` produces the ``Ellipsis`` singleton.\n\nIt is written as ``Ellipsis`` or ``...``.\n', 'bltin-null-object': "\nThe Null Object\n***************\n\nThis object is returned by functions that don't explicitly return a\nvalue. It supports no special operations. There is exactly one null\nobject, named ``None`` (a built-in name). ``type(None)()`` produces\nthe same singleton.\n\nIt is written as ``None``.\n", 'bltin-type-objects': "\nType Objects\n************\n\nType objects represent the various object types. An object's type is\naccessed by the built-in function ``type()``. There are no special\noperations on types. The standard module ``types`` defines names for\nall standard built-in types.\n\nTypes are written like this: ````.\n", 'booleans': '\nBoolean operations\n******************\n\n or_test ::= and_test | or_test "or" and_test\n and_test ::= not_test | and_test "and" not_test\n not_test ::= comparison | "not" not_test\n\nIn the context of Boolean operations, and also when expressions are\nused by control flow statements, the following values are interpreted\nas false: ``False``, ``None``, numeric zero of all types, and empty\nstrings and containers (including strings, tuples, lists,\ndictionaries, sets and frozensets). All other values are interpreted\nas true. User-defined objects can customize their truth value by\nproviding a ``__bool__()`` method.\n\nThe operator ``not`` yields ``True`` if its argument is false,\n``False`` otherwise.\n\nThe expression ``x and y`` first evaluates *x*; if *x* is false, its\nvalue is returned; otherwise, *y* is evaluated and the resulting value\nis returned.\n\nThe expression ``x or y`` first evaluates *x*; if *x* is true, its\nvalue is returned; otherwise, *y* is evaluated and the resulting value\nis returned.\n\n(Note that neither ``and`` nor ``or`` restrict the value and type they\nreturn to ``False`` and ``True``, but rather return the last evaluated\nargument. This is sometimes useful, e.g., if ``s`` is a string that\nshould be replaced by a default value if it is empty, the expression\n``s or \'foo\'`` yields the desired value. Because ``not`` has to\ninvent a value anyway, it does not bother to return a value of the\nsame type as its argument, so e.g., ``not \'foo\'`` yields ``False``,\nnot ``\'\'``.)\n', @@ -23,7 +23,7 @@ 'context-managers': '\nWith Statement Context Managers\n*******************************\n\nA *context manager* is an object that defines the runtime context to\nbe established when executing a ``with`` statement. The context\nmanager handles the entry into, and the exit from, the desired runtime\ncontext for the execution of the block of code. Context managers are\nnormally invoked using the ``with`` statement (described in section\n*The with statement*), but can also be used by directly invoking their\nmethods.\n\nTypical uses of context managers include saving and restoring various\nkinds of global state, locking and unlocking resources, closing opened\nfiles, etc.\n\nFor more information on context managers, see *Context Manager Types*.\n\nobject.__enter__(self)\n\n Enter the runtime context related to this object. The ``with``\n statement will bind this method\'s return value to the target(s)\n specified in the ``as`` clause of the statement, if any.\n\nobject.__exit__(self, exc_type, exc_value, traceback)\n\n Exit the runtime context related to this object. The parameters\n describe the exception that caused the context to be exited. If the\n context was exited without an exception, all three arguments will\n be ``None``.\n\n If an exception is supplied, and the method wishes to suppress the\n exception (i.e., prevent it from being propagated), it should\n return a true value. Otherwise, the exception will be processed\n normally upon exit from this method.\n\n Note that ``__exit__()`` methods should not reraise the passed-in\n exception; this is the caller\'s responsibility.\n\nSee also:\n\n **PEP 0343** - The "with" statement\n The specification, background, and examples for the Python\n ``with`` statement.\n', 'continue': '\nThe ``continue`` statement\n**************************\n\n continue_stmt ::= "continue"\n\n``continue`` may only occur syntactically nested in a ``for`` or\n``while`` loop, but not nested in a function or class definition or\n``finally`` clause within that loop. It continues with the next cycle\nof the nearest enclosing loop.\n\nWhen ``continue`` passes control out of a ``try`` statement with a\n``finally`` clause, that ``finally`` clause is executed before really\nstarting the next loop cycle.\n', 'conversions': '\nArithmetic conversions\n**********************\n\nWhen a description of an arithmetic operator below uses the phrase\n"the numeric arguments are converted to a common type," this means\nthat the operator implementation for built-in types works that way:\n\n* If either argument is a complex number, the other is converted to\n complex;\n\n* otherwise, if either argument is a floating point number, the other\n is converted to floating point;\n\n* otherwise, both must be integers and no conversion is necessary.\n\nSome additional rules apply for certain operators (e.g., a string left\nargument to the \'%\' operator). Extensions must define their own\nconversion behavior.\n', - 'customization': '\nBasic customization\n*******************\n\nobject.__new__(cls[, ...])\n\n Called to create a new instance of class *cls*. ``__new__()`` is a\n static method (special-cased so you need not declare it as such)\n that takes the class of which an instance was requested as its\n first argument. The remaining arguments are those passed to the\n object constructor expression (the call to the class). The return\n value of ``__new__()`` should be the new object instance (usually\n an instance of *cls*).\n\n Typical implementations create a new instance of the class by\n invoking the superclass\'s ``__new__()`` method using\n ``super(currentclass, cls).__new__(cls[, ...])`` with appropriate\n arguments and then modifying the newly-created instance as\n necessary before returning it.\n\n If ``__new__()`` returns an instance of *cls*, then the new\n instance\'s ``__init__()`` method will be invoked like\n ``__init__(self[, ...])``, where *self* is the new instance and the\n remaining arguments are the same as were passed to ``__new__()``.\n\n If ``__new__()`` does not return an instance of *cls*, then the new\n instance\'s ``__init__()`` method will not be invoked.\n\n ``__new__()`` is intended mainly to allow subclasses of immutable\n types (like int, str, or tuple) to customize instance creation. It\n is also commonly overridden in custom metaclasses in order to\n customize class creation.\n\nobject.__init__(self[, ...])\n\n Called when the instance is created. The arguments are those\n passed to the class constructor expression. If a base class has an\n ``__init__()`` method, the derived class\'s ``__init__()`` method,\n if any, must explicitly call it to ensure proper initialization of\n the base class part of the instance; for example:\n ``BaseClass.__init__(self, [args...])``. As a special constraint\n on constructors, no value may be returned; doing so will cause a\n ``TypeError`` to be raised at runtime.\n\nobject.__del__(self)\n\n Called when the instance is about to be destroyed. This is also\n called a destructor. If a base class has a ``__del__()`` method,\n the derived class\'s ``__del__()`` method, if any, must explicitly\n call it to ensure proper deletion of the base class part of the\n instance. Note that it is possible (though not recommended!) for\n the ``__del__()`` method to postpone destruction of the instance by\n creating a new reference to it. It may then be called at a later\n time when this new reference is deleted. It is not guaranteed that\n ``__del__()`` methods are called for objects that still exist when\n the interpreter exits.\n\n Note: ``del x`` doesn\'t directly call ``x.__del__()`` --- the former\n decrements the reference count for ``x`` by one, and the latter\n is only called when ``x``\'s reference count reaches zero. Some\n common situations that may prevent the reference count of an\n object from going to zero include: circular references between\n objects (e.g., a doubly-linked list or a tree data structure with\n parent and child pointers); a reference to the object on the\n stack frame of a function that caught an exception (the traceback\n stored in ``sys.exc_info()[2]`` keeps the stack frame alive); or\n a reference to the object on the stack frame that raised an\n unhandled exception in interactive mode (the traceback stored in\n ``sys.last_traceback`` keeps the stack frame alive). The first\n situation can only be remedied by explicitly breaking the cycles;\n the latter two situations can be resolved by storing ``None`` in\n ``sys.last_traceback``. Circular references which are garbage are\n detected when the option cycle detector is enabled (it\'s on by\n default), but can only be cleaned up if there are no Python-\n level ``__del__()`` methods involved. Refer to the documentation\n for the ``gc`` module for more information about how\n ``__del__()`` methods are handled by the cycle detector,\n particularly the description of the ``garbage`` value.\n\n Warning: Due to the precarious circumstances under which ``__del__()``\n methods are invoked, exceptions that occur during their execution\n are ignored, and a warning is printed to ``sys.stderr`` instead.\n Also, when ``__del__()`` is invoked in response to a module being\n deleted (e.g., when execution of the program is done), other\n globals referenced by the ``__del__()`` method may already have\n been deleted or in the process of being torn down (e.g. the\n import machinery shutting down). For this reason, ``__del__()``\n methods should do the absolute minimum needed to maintain\n external invariants. Starting with version 1.5, Python\n guarantees that globals whose name begins with a single\n underscore are deleted from their module before other globals are\n deleted; if no other references to such globals exist, this may\n help in assuring that imported modules are still available at the\n time when the ``__del__()`` method is called.\n\nobject.__repr__(self)\n\n Called by the ``repr()`` built-in function to compute the\n "official" string representation of an object. If at all possible,\n this should look like a valid Python expression that could be used\n to recreate an object with the same value (given an appropriate\n environment). If this is not possible, a string of the form\n ``<...some useful description...>`` should be returned. The return\n value must be a string object. If a class defines ``__repr__()``\n but not ``__str__()``, then ``__repr__()`` is also used when an\n "informal" string representation of instances of that class is\n required.\n\n This is typically used for debugging, so it is important that the\n representation is information-rich and unambiguous.\n\nobject.__str__(self)\n\n Called by the ``str()`` built-in function and by the ``print()``\n function to compute the "informal" string representation of an\n object. This differs from ``__repr__()`` in that it does not have\n to be a valid Python expression: a more convenient or concise\n representation may be used instead. The return value must be a\n string object.\n\nobject.__bytes__(self)\n\n Called by ``bytes()`` to compute a byte-string representation of an\n object. This should return a ``bytes`` object.\n\nobject.__format__(self, format_spec)\n\n Called by the ``format()`` built-in function (and by extension, the\n ``format()`` method of class ``str``) to produce a "formatted"\n string representation of an object. The ``format_spec`` argument is\n a string that contains a description of the formatting options\n desired. The interpretation of the ``format_spec`` argument is up\n to the type implementing ``__format__()``, however most classes\n will either delegate formatting to one of the built-in types, or\n use a similar formatting option syntax.\n\n See *Format Specification Mini-Language* for a description of the\n standard formatting syntax.\n\n The return value must be a string object.\n\nobject.__lt__(self, other)\nobject.__le__(self, other)\nobject.__eq__(self, other)\nobject.__ne__(self, other)\nobject.__gt__(self, other)\nobject.__ge__(self, other)\n\n These are the so-called "rich comparison" methods. The\n correspondence between operator symbols and method names is as\n follows: ``xy`` calls ``x.__gt__(y)``, and ``x>=y`` calls\n ``x.__ge__(y)``.\n\n A rich comparison method may return the singleton\n ``NotImplemented`` if it does not implement the operation for a\n given pair of arguments. By convention, ``False`` and ``True`` are\n returned for a successful comparison. However, these methods can\n return any value, so if the comparison operator is used in a\n Boolean context (e.g., in the condition of an ``if`` statement),\n Python will call ``bool()`` on the value to determine if the result\n is true or false.\n\n There are no implied relationships among the comparison operators.\n The truth of ``x==y`` does not imply that ``x!=y`` is false.\n Accordingly, when defining ``__eq__()``, one should also define\n ``__ne__()`` so that the operators will behave as expected. See\n the paragraph on ``__hash__()`` for some important notes on\n creating *hashable* objects which support custom comparison\n operations and are usable as dictionary keys.\n\n There are no swapped-argument versions of these methods (to be used\n when the left argument does not support the operation but the right\n argument does); rather, ``__lt__()`` and ``__gt__()`` are each\n other\'s reflection, ``__le__()`` and ``__ge__()`` are each other\'s\n reflection, and ``__eq__()`` and ``__ne__()`` are their own\n reflection.\n\n Arguments to rich comparison methods are never coerced.\n\n To automatically generate ordering operations from a single root\n operation, see ``functools.total_ordering()``.\n\nobject.__hash__(self)\n\n Called by built-in function ``hash()`` and for operations on\n members of hashed collections including ``set``, ``frozenset``, and\n ``dict``. ``__hash__()`` should return an integer. The only\n required property is that objects which compare equal have the same\n hash value; it is advised to somehow mix together (e.g. using\n exclusive or) the hash values for the components of the object that\n also play a part in comparison of objects.\n\n If a class does not define an ``__eq__()`` method it should not\n define a ``__hash__()`` operation either; if it defines\n ``__eq__()`` but not ``__hash__()``, its instances will not be\n usable as items in hashable collections. If a class defines\n mutable objects and implements an ``__eq__()`` method, it should\n not implement ``__hash__()``, since the implementation of hashable\n collections requires that a key\'s hash value is immutable (if the\n object\'s hash value changes, it will be in the wrong hash bucket).\n\n User-defined classes have ``__eq__()`` and ``__hash__()`` methods\n by default; with them, all objects compare unequal (except with\n themselves) and ``x.__hash__()`` returns an appropriate value such\n that ``x == y`` implies both that ``x is y`` and ``hash(x) ==\n hash(y)``.\n\n Classes which inherit a ``__hash__()`` method from a parent class\n but change the meaning of ``__eq__()`` such that the hash value\n returned is no longer appropriate (e.g. by switching to a value-\n based concept of equality instead of the default identity based\n equality) can explicitly flag themselves as being unhashable by\n setting ``__hash__ = None`` in the class definition. Doing so means\n that not only will instances of the class raise an appropriate\n ``TypeError`` when a program attempts to retrieve their hash value,\n but they will also be correctly identified as unhashable when\n checking ``isinstance(obj, collections.Hashable)`` (unlike classes\n which define their own ``__hash__()`` to explicitly raise\n ``TypeError``).\n\n If a class that overrides ``__eq__()`` needs to retain the\n implementation of ``__hash__()`` from a parent class, the\n interpreter must be told this explicitly by setting ``__hash__ =\n .__hash__``. Otherwise the inheritance of\n ``__hash__()`` will be blocked, just as if ``__hash__`` had been\n explicitly set to ``None``.\n\n Note: Note by default the ``__hash__()`` values of str, bytes and\n datetime objects are "salted" with an unpredictable random value.\n Although they remain constant within an individual Python\n process, they are not predictable between repeated invocations of\n Python.This is intended to provide protection against a denial-\n of-service caused by carefully-chosen inputs that exploit the\n worst case performance of a dict insertion, O(n^2) complexity.\n See http://www.ocert.org/advisories/ocert-2011-003.html for\n details.Changing hash values affects the order in which keys are\n retrieved from a dict. Note Python has never made guarantees\n about this ordering (and it typically varies between 32-bit and\n 64-bit builds).See also ``PYTHONHASHSEED``.\n\n Changed in version 3.3: Hash randomization is enabled by default.\n\nobject.__bool__(self)\n\n Called to implement truth value testing and the built-in operation\n ``bool()``; should return ``False`` or ``True``. When this method\n is not defined, ``__len__()`` is called, if it is defined, and the\n object is considered true if its result is nonzero. If a class\n defines neither ``__len__()`` nor ``__bool__()``, all its instances\n are considered true.\n', + 'customization': '\nBasic customization\n*******************\n\nobject.__new__(cls[, ...])\n\n Called to create a new instance of class *cls*. ``__new__()`` is a\n static method (special-cased so you need not declare it as such)\n that takes the class of which an instance was requested as its\n first argument. The remaining arguments are those passed to the\n object constructor expression (the call to the class). The return\n value of ``__new__()`` should be the new object instance (usually\n an instance of *cls*).\n\n Typical implementations create a new instance of the class by\n invoking the superclass\'s ``__new__()`` method using\n ``super(currentclass, cls).__new__(cls[, ...])`` with appropriate\n arguments and then modifying the newly-created instance as\n necessary before returning it.\n\n If ``__new__()`` returns an instance of *cls*, then the new\n instance\'s ``__init__()`` method will be invoked like\n ``__init__(self[, ...])``, where *self* is the new instance and the\n remaining arguments are the same as were passed to ``__new__()``.\n\n If ``__new__()`` does not return an instance of *cls*, then the new\n instance\'s ``__init__()`` method will not be invoked.\n\n ``__new__()`` is intended mainly to allow subclasses of immutable\n types (like int, str, or tuple) to customize instance creation. It\n is also commonly overridden in custom metaclasses in order to\n customize class creation.\n\nobject.__init__(self[, ...])\n\n Called when the instance is created. The arguments are those\n passed to the class constructor expression. If a base class has an\n ``__init__()`` method, the derived class\'s ``__init__()`` method,\n if any, must explicitly call it to ensure proper initialization of\n the base class part of the instance; for example:\n ``BaseClass.__init__(self, [args...])``. As a special constraint\n on constructors, no value may be returned; doing so will cause a\n ``TypeError`` to be raised at runtime.\n\nobject.__del__(self)\n\n Called when the instance is about to be destroyed. This is also\n called a destructor. If a base class has a ``__del__()`` method,\n the derived class\'s ``__del__()`` method, if any, must explicitly\n call it to ensure proper deletion of the base class part of the\n instance. Note that it is possible (though not recommended!) for\n the ``__del__()`` method to postpone destruction of the instance by\n creating a new reference to it. It may then be called at a later\n time when this new reference is deleted. It is not guaranteed that\n ``__del__()`` methods are called for objects that still exist when\n the interpreter exits.\n\n Note: ``del x`` doesn\'t directly call ``x.__del__()`` --- the former\n decrements the reference count for ``x`` by one, and the latter\n is only called when ``x``\'s reference count reaches zero. Some\n common situations that may prevent the reference count of an\n object from going to zero include: circular references between\n objects (e.g., a doubly-linked list or a tree data structure with\n parent and child pointers); a reference to the object on the\n stack frame of a function that caught an exception (the traceback\n stored in ``sys.exc_info()[2]`` keeps the stack frame alive); or\n a reference to the object on the stack frame that raised an\n unhandled exception in interactive mode (the traceback stored in\n ``sys.last_traceback`` keeps the stack frame alive). The first\n situation can only be remedied by explicitly breaking the cycles;\n the latter two situations can be resolved by storing ``None`` in\n ``sys.last_traceback``. Circular references which are garbage are\n detected when the option cycle detector is enabled (it\'s on by\n default), but can only be cleaned up if there are no Python-\n level ``__del__()`` methods involved. Refer to the documentation\n for the ``gc`` module for more information about how\n ``__del__()`` methods are handled by the cycle detector,\n particularly the description of the ``garbage`` value.\n\n Warning: Due to the precarious circumstances under which ``__del__()``\n methods are invoked, exceptions that occur during their execution\n are ignored, and a warning is printed to ``sys.stderr`` instead.\n Also, when ``__del__()`` is invoked in response to a module being\n deleted (e.g., when execution of the program is done), other\n globals referenced by the ``__del__()`` method may already have\n been deleted or in the process of being torn down (e.g. the\n import machinery shutting down). For this reason, ``__del__()``\n methods should do the absolute minimum needed to maintain\n external invariants. Starting with version 1.5, Python\n guarantees that globals whose name begins with a single\n underscore are deleted from their module before other globals are\n deleted; if no other references to such globals exist, this may\n help in assuring that imported modules are still available at the\n time when the ``__del__()`` method is called.\n\nobject.__repr__(self)\n\n Called by the ``repr()`` built-in function to compute the\n "official" string representation of an object. If at all possible,\n this should look like a valid Python expression that could be used\n to recreate an object with the same value (given an appropriate\n environment). If this is not possible, a string of the form\n ``<...some useful description...>`` should be returned. The return\n value must be a string object. If a class defines ``__repr__()``\n but not ``__str__()``, then ``__repr__()`` is also used when an\n "informal" string representation of instances of that class is\n required.\n\n This is typically used for debugging, so it is important that the\n representation is information-rich and unambiguous.\n\nobject.__str__(self)\n\n Called by the ``str()`` built-in function and by the ``print()``\n function to compute the "informal" string representation of an\n object. This differs from ``__repr__()`` in that it does not have\n to be a valid Python expression: a more convenient or concise\n representation may be used instead. The return value must be a\n string object.\n\nobject.__bytes__(self)\n\n Called by ``bytes()`` to compute a byte-string representation of an\n object. This should return a ``bytes`` object.\n\nobject.__format__(self, format_spec)\n\n Called by the ``format()`` built-in function (and by extension, the\n ``format()`` method of class ``str``) to produce a "formatted"\n string representation of an object. The ``format_spec`` argument is\n a string that contains a description of the formatting options\n desired. The interpretation of the ``format_spec`` argument is up\n to the type implementing ``__format__()``, however most classes\n will either delegate formatting to one of the built-in types, or\n use a similar formatting option syntax.\n\n See *Format Specification Mini-Language* for a description of the\n standard formatting syntax.\n\n The return value must be a string object.\n\nobject.__lt__(self, other)\nobject.__le__(self, other)\nobject.__eq__(self, other)\nobject.__ne__(self, other)\nobject.__gt__(self, other)\nobject.__ge__(self, other)\n\n These are the so-called "rich comparison" methods. The\n correspondence between operator symbols and method names is as\n follows: ``xy`` calls ``x.__gt__(y)``, and ``x>=y`` calls\n ``x.__ge__(y)``.\n\n A rich comparison method may return the singleton\n ``NotImplemented`` if it does not implement the operation for a\n given pair of arguments. By convention, ``False`` and ``True`` are\n returned for a successful comparison. However, these methods can\n return any value, so if the comparison operator is used in a\n Boolean context (e.g., in the condition of an ``if`` statement),\n Python will call ``bool()`` on the value to determine if the result\n is true or false.\n\n There are no implied relationships among the comparison operators.\n The truth of ``x==y`` does not imply that ``x!=y`` is false.\n Accordingly, when defining ``__eq__()``, one should also define\n ``__ne__()`` so that the operators will behave as expected. See\n the paragraph on ``__hash__()`` for some important notes on\n creating *hashable* objects which support custom comparison\n operations and are usable as dictionary keys.\n\n There are no swapped-argument versions of these methods (to be used\n when the left argument does not support the operation but the right\n argument does); rather, ``__lt__()`` and ``__gt__()`` are each\n other\'s reflection, ``__le__()`` and ``__ge__()`` are each other\'s\n reflection, and ``__eq__()`` and ``__ne__()`` are their own\n reflection.\n\n Arguments to rich comparison methods are never coerced.\n\n To automatically generate ordering operations from a single root\n operation, see ``functools.total_ordering()``.\n\nobject.__hash__(self)\n\n Called by built-in function ``hash()`` and for operations on\n members of hashed collections including ``set``, ``frozenset``, and\n ``dict``. ``__hash__()`` should return an integer. The only\n required property is that objects which compare equal have the same\n hash value; it is advised to somehow mix together (e.g. using\n exclusive or) the hash values for the components of the object that\n also play a part in comparison of objects.\n\n If a class does not define an ``__eq__()`` method it should not\n define a ``__hash__()`` operation either; if it defines\n ``__eq__()`` but not ``__hash__()``, its instances will not be\n usable as items in hashable collections. If a class defines\n mutable objects and implements an ``__eq__()`` method, it should\n not implement ``__hash__()``, since the implementation of hashable\n collections requires that a key\'s hash value is immutable (if the\n object\'s hash value changes, it will be in the wrong hash bucket).\n\n User-defined classes have ``__eq__()`` and ``__hash__()`` methods\n by default; with them, all objects compare unequal (except with\n themselves) and ``x.__hash__()`` returns ``id(x)``.\n\n Classes which inherit a ``__hash__()`` method from a parent class\n but change the meaning of ``__eq__()`` such that the hash value\n returned is no longer appropriate (e.g. by switching to a value-\n based concept of equality instead of the default identity based\n equality) can explicitly flag themselves as being unhashable by\n setting ``__hash__ = None`` in the class definition. Doing so means\n that not only will instances of the class raise an appropriate\n ``TypeError`` when a program attempts to retrieve their hash value,\n but they will also be correctly identified as unhashable when\n checking ``isinstance(obj, collections.Hashable)`` (unlike classes\n which define their own ``__hash__()`` to explicitly raise\n ``TypeError``).\n\n If a class that overrides ``__eq__()`` needs to retain the\n implementation of ``__hash__()`` from a parent class, the\n interpreter must be told this explicitly by setting ``__hash__ =\n .__hash__``. Otherwise the inheritance of\n ``__hash__()`` will be blocked, just as if ``__hash__`` had been\n explicitly set to ``None``.\n\n Note: Note by default the ``__hash__()`` values of str, bytes and\n datetime objects are "salted" with an unpredictable random value.\n Although they remain constant within an individual Python\n process, they are not predictable between repeated invocations of\n Python.This is intended to provide protection against a denial-\n of-service caused by carefully-chosen inputs that exploit the\n worst case performance of a dict insertion, O(n^2) complexity.\n See http://www.ocert.org/advisories/ocert-2011-003.html for\n details.Changing hash values affects the order in which keys are\n retrieved from a dict. Note Python has never made guarantees\n about this ordering (and it typically varies between 32-bit and\n 64-bit builds).See also ``PYTHONHASHSEED``.\n\n Changed in version 3.3: Hash randomization is enabled by default.\n\nobject.__bool__(self)\n\n Called to implement truth value testing and the built-in operation\n ``bool()``; should return ``False`` or ``True``. When this method\n is not defined, ``__len__()`` is called, if it is defined, and the\n object is considered true if its result is nonzero. If a class\n defines neither ``__len__()`` nor ``__bool__()``, all its instances\n are considered true.\n', 'debugger': '\n``pdb`` --- The Python Debugger\n*******************************\n\nThe module ``pdb`` defines an interactive source code debugger for\nPython programs. It supports setting (conditional) breakpoints and\nsingle stepping at the source line level, inspection of stack frames,\nsource code listing, and evaluation of arbitrary Python code in the\ncontext of any stack frame. It also supports post-mortem debugging\nand can be called under program control.\n\nThe debugger is extensible -- it is actually defined as the class\n``Pdb``. This is currently undocumented but easily understood by\nreading the source. The extension interface uses the modules ``bdb``\nand ``cmd``.\n\nThe debugger\'s prompt is ``(Pdb)``. Typical usage to run a program\nunder control of the debugger is:\n\n >>> import pdb\n >>> import mymodule\n >>> pdb.run(\'mymodule.test()\')\n > (0)?()\n (Pdb) continue\n > (1)?()\n (Pdb) continue\n NameError: \'spam\'\n > (1)?()\n (Pdb)\n\nChanged in version 3.3: Tab-completion via the ``readline`` module is\navailable for commands and command arguments, e.g. the current global\nand local names are offered as arguments of the ``print`` command.\n\n``pdb.py`` can also be invoked as a script to debug other scripts.\nFor example:\n\n python3 -m pdb myscript.py\n\nWhen invoked as a script, pdb will automatically enter post-mortem\ndebugging if the program being debugged exits abnormally. After post-\nmortem debugging (or after normal exit of the program), pdb will\nrestart the program. Automatic restarting preserves pdb\'s state (such\nas breakpoints) and in most cases is more useful than quitting the\ndebugger upon program\'s exit.\n\nNew in version 3.2: ``pdb.py`` now accepts a ``-c`` option that\nexecutes commands as if given in a ``.pdbrc`` file, see *Debugger\nCommands*.\n\nThe typical usage to break into the debugger from a running program is\nto insert\n\n import pdb; pdb.set_trace()\n\nat the location you want to break into the debugger. You can then\nstep through the code following this statement, and continue running\nwithout the debugger using the ``continue`` command.\n\nThe typical usage to inspect a crashed program is:\n\n >>> import pdb\n >>> import mymodule\n >>> mymodule.test()\n Traceback (most recent call last):\n File "", line 1, in ?\n File "./mymodule.py", line 4, in test\n test2()\n File "./mymodule.py", line 3, in test2\n print(spam)\n NameError: spam\n >>> pdb.pm()\n > ./mymodule.py(3)test2()\n -> print(spam)\n (Pdb)\n\nThe module defines the following functions; each enters the debugger\nin a slightly different way:\n\npdb.run(statement, globals=None, locals=None)\n\n Execute the *statement* (given as a string or a code object) under\n debugger control. The debugger prompt appears before any code is\n executed; you can set breakpoints and type ``continue``, or you can\n step through the statement using ``step`` or ``next`` (all these\n commands are explained below). The optional *globals* and *locals*\n arguments specify the environment in which the code is executed; by\n default the dictionary of the module ``__main__`` is used. (See\n the explanation of the built-in ``exec()`` or ``eval()``\n functions.)\n\npdb.runeval(expression, globals=None, locals=None)\n\n Evaluate the *expression* (given as a string or a code object)\n under debugger control. When ``runeval()`` returns, it returns the\n value of the expression. Otherwise this function is similar to\n ``run()``.\n\npdb.runcall(function, *args, **kwds)\n\n Call the *function* (a function or method object, not a string)\n with the given arguments. When ``runcall()`` returns, it returns\n whatever the function call returned. The debugger prompt appears\n as soon as the function is entered.\n\npdb.set_trace()\n\n Enter the debugger at the calling stack frame. This is useful to\n hard-code a breakpoint at a given point in a program, even if the\n code is not otherwise being debugged (e.g. when an assertion\n fails).\n\npdb.post_mortem(traceback=None)\n\n Enter post-mortem debugging of the given *traceback* object. If no\n *traceback* is given, it uses the one of the exception that is\n currently being handled (an exception must be being handled if the\n default is to be used).\n\npdb.pm()\n\n Enter post-mortem debugging of the traceback found in\n ``sys.last_traceback``.\n\nThe ``run*`` functions and ``set_trace()`` are aliases for\ninstantiating the ``Pdb`` class and calling the method of the same\nname. If you want to access further features, you have to do this\nyourself:\n\nclass class pdb.Pdb(completekey=\'tab\', stdin=None, stdout=None, skip=None, nosigint=False)\n\n ``Pdb`` is the debugger class.\n\n The *completekey*, *stdin* and *stdout* arguments are passed to the\n underlying ``cmd.Cmd`` class; see the description there.\n\n The *skip* argument, if given, must be an iterable of glob-style\n module name patterns. The debugger will not step into frames that\n originate in a module that matches one of these patterns. [1]\n\n By default, Pdb sets a handler for the SIGINT signal (which is sent\n when the user presses Ctrl-C on the console) when you give a\n ``continue`` command. This allows you to break into the debugger\n again by pressing Ctrl-C. If you want Pdb not to touch the SIGINT\n handler, set *nosigint* tot true.\n\n Example call to enable tracing with *skip*:\n\n import pdb; pdb.Pdb(skip=[\'django.*\']).set_trace()\n\n New in version 3.1: The *skip* argument.\n\n New in version 3.2: The *nosigint* argument. Previously, a SIGINT\n handler was never set by Pdb.\n\n run(statement, globals=None, locals=None)\n runeval(expression, globals=None, locals=None)\n runcall(function, *args, **kwds)\n set_trace()\n\n See the documentation for the functions explained above.\n\n\nDebugger Commands\n=================\n\nThe commands recognized by the debugger are listed below. Most\ncommands can be abbreviated to one or two letters as indicated; e.g.\n``h(elp)`` means that either ``h`` or ``help`` can be used to enter\nthe help command (but not ``he`` or ``hel``, nor ``H`` or ``Help`` or\n``HELP``). Arguments to commands must be separated by whitespace\n(spaces or tabs). Optional arguments are enclosed in square brackets\n(``[]``) in the command syntax; the square brackets must not be typed.\nAlternatives in the command syntax are separated by a vertical bar\n(``|``).\n\nEntering a blank line repeats the last command entered. Exception: if\nthe last command was a ``list`` command, the next 11 lines are listed.\n\nCommands that the debugger doesn\'t recognize are assumed to be Python\nstatements and are executed in the context of the program being\ndebugged. Python statements can also be prefixed with an exclamation\npoint (``!``). This is a powerful way to inspect the program being\ndebugged; it is even possible to change a variable or call a function.\nWhen an exception occurs in such a statement, the exception name is\nprinted but the debugger\'s state is not changed.\n\nThe debugger supports *aliases*. Aliases can have parameters which\nallows one a certain level of adaptability to the context under\nexamination.\n\nMultiple commands may be entered on a single line, separated by\n``;;``. (A single ``;`` is not used as it is the separator for\nmultiple commands in a line that is passed to the Python parser.) No\nintelligence is applied to separating the commands; the input is split\nat the first ``;;`` pair, even if it is in the middle of a quoted\nstring.\n\nIf a file ``.pdbrc`` exists in the user\'s home directory or in the\ncurrent directory, it is read in and executed as if it had been typed\nat the debugger prompt. This is particularly useful for aliases. If\nboth files exist, the one in the home directory is read first and\naliases defined there can be overridden by the local file.\n\nChanged in version 3.2: ``.pdbrc`` can now contain commands that\ncontinue debugging, such as ``continue`` or ``next``. Previously,\nthese commands had no effect.\n\nh(elp) [command]\n\n Without argument, print the list of available commands. With a\n *command* as argument, print help about that command. ``help pdb``\n displays the full documentation (the docstring of the ``pdb``\n module). Since the *command* argument must be an identifier,\n ``help exec`` must be entered to get help on the ``!`` command.\n\nw(here)\n\n Print a stack trace, with the most recent frame at the bottom. An\n arrow indicates the current frame, which determines the context of\n most commands.\n\nd(own) [count]\n\n Move the current frame *count* (default one) levels down in the\n stack trace (to a newer frame).\n\nu(p) [count]\n\n Move the current frame *count* (default one) levels up in the stack\n trace (to an older frame).\n\nb(reak) [([filename:]lineno | function) [, condition]]\n\n With a *lineno* argument, set a break there in the current file.\n With a *function* argument, set a break at the first executable\n statement within that function. The line number may be prefixed\n with a filename and a colon, to specify a breakpoint in another\n file (probably one that hasn\'t been loaded yet). The file is\n searched on ``sys.path``. Note that each breakpoint is assigned a\n number to which all the other breakpoint commands refer.\n\n If a second argument is present, it is an expression which must\n evaluate to true before the breakpoint is honored.\n\n Without argument, list all breaks, including for each breakpoint,\n the number of times that breakpoint has been hit, the current\n ignore count, and the associated condition if any.\n\ntbreak [([filename:]lineno | function) [, condition]]\n\n Temporary breakpoint, which is removed automatically when it is\n first hit. The arguments are the same as for ``break``.\n\ncl(ear) [filename:lineno | bpnumber [bpnumber ...]]\n\n With a *filename:lineno* argument, clear all the breakpoints at\n this line. With a space separated list of breakpoint numbers, clear\n those breakpoints. Without argument, clear all breaks (but first\n ask confirmation).\n\ndisable [bpnumber [bpnumber ...]]\n\n Disable the breakpoints given as a space separated list of\n breakpoint numbers. Disabling a breakpoint means it cannot cause\n the program to stop execution, but unlike clearing a breakpoint, it\n remains in the list of breakpoints and can be (re-)enabled.\n\nenable [bpnumber [bpnumber ...]]\n\n Enable the breakpoints specified.\n\nignore bpnumber [count]\n\n Set the ignore count for the given breakpoint number. If count is\n omitted, the ignore count is set to 0. A breakpoint becomes active\n when the ignore count is zero. When non-zero, the count is\n decremented each time the breakpoint is reached and the breakpoint\n is not disabled and any associated condition evaluates to true.\n\ncondition bpnumber [condition]\n\n Set a new *condition* for the breakpoint, an expression which must\n evaluate to true before the breakpoint is honored. If *condition*\n is absent, any existing condition is removed; i.e., the breakpoint\n is made unconditional.\n\ncommands [bpnumber]\n\n Specify a list of commands for breakpoint number *bpnumber*. The\n commands themselves appear on the following lines. Type a line\n containing just ``end`` to terminate the commands. An example:\n\n (Pdb) commands 1\n (com) print some_variable\n (com) end\n (Pdb)\n\n To remove all commands from a breakpoint, type commands and follow\n it immediately with ``end``; that is, give no commands.\n\n With no *bpnumber* argument, commands refers to the last breakpoint\n set.\n\n You can use breakpoint commands to start your program up again.\n Simply use the continue command, or step, or any other command that\n resumes execution.\n\n Specifying any command resuming execution (currently continue,\n step, next, return, jump, quit and their abbreviations) terminates\n the command list (as if that command was immediately followed by\n end). This is because any time you resume execution (even with a\n simple next or step), you may encounter another breakpoint--which\n could have its own command list, leading to ambiguities about which\n list to execute.\n\n If you use the \'silent\' command in the command list, the usual\n message about stopping at a breakpoint is not printed. This may be\n desirable for breakpoints that are to print a specific message and\n then continue. If none of the other commands print anything, you\n see no sign that the breakpoint was reached.\n\ns(tep)\n\n Execute the current line, stop at the first possible occasion\n (either in a function that is called or on the next line in the\n current function).\n\nn(ext)\n\n Continue execution until the next line in the current function is\n reached or it returns. (The difference between ``next`` and\n ``step`` is that ``step`` stops inside a called function, while\n ``next`` executes called functions at (nearly) full speed, only\n stopping at the next line in the current function.)\n\nunt(il) [lineno]\n\n Without argument, continue execution until the line with a number\n greater than the current one is reached.\n\n With a line number, continue execution until a line with a number\n greater or equal to that is reached. In both cases, also stop when\n the current frame returns.\n\n Changed in version 3.2: Allow giving an explicit line number.\n\nr(eturn)\n\n Continue execution until the current function returns.\n\nc(ont(inue))\n\n Continue execution, only stop when a breakpoint is encountered.\n\nj(ump) lineno\n\n Set the next line that will be executed. Only available in the\n bottom-most frame. This lets you jump back and execute code again,\n or jump forward to skip code that you don\'t want to run.\n\n It should be noted that not all jumps are allowed -- for instance\n it is not possible to jump into the middle of a ``for`` loop or out\n of a ``finally`` clause.\n\nl(ist) [first[, last]]\n\n List source code for the current file. Without arguments, list 11\n lines around the current line or continue the previous listing.\n With ``.`` as argument, list 11 lines around the current line.\n With one argument, list 11 lines around at that line. With two\n arguments, list the given range; if the second argument is less\n than the first, it is interpreted as a count.\n\n The current line in the current frame is indicated by ``->``. If\n an exception is being debugged, the line where the exception was\n originally raised or propagated is indicated by ``>>``, if it\n differs from the current line.\n\n New in version 3.2: The ``>>`` marker.\n\nll | longlist\n\n List all source code for the current function or frame.\n Interesting lines are marked as for ``list``.\n\n New in version 3.2.\n\na(rgs)\n\n Print the argument list of the current function.\n\np(rint) expression\n\n Evaluate the *expression* in the current context and print its\n value.\n\npp expression\n\n Like the ``print`` command, except the value of the expression is\n pretty-printed using the ``pprint`` module.\n\nwhatis expression\n\n Print the type of the *expression*.\n\nsource expression\n\n Try to get source code for the given object and display it.\n\n New in version 3.2.\n\ndisplay [expression]\n\n Display the value of the expression if it changed, each time\n execution stops in the current frame.\n\n Without expression, list all display expressions for the current\n frame.\n\n New in version 3.2.\n\nundisplay [expression]\n\n Do not display the expression any more in the current frame.\n Without expression, clear all display expressions for the current\n frame.\n\n New in version 3.2.\n\ninteract\n\n Start an interative interpreter (using the ``code`` module) whose\n global namespace contains all the (global and local) names found in\n the current scope.\n\n New in version 3.2.\n\nalias [name [command]]\n\n Create an alias called *name* that executes *command*. The command\n must *not* be enclosed in quotes. Replaceable parameters can be\n indicated by ``%1``, ``%2``, and so on, while ``%*`` is replaced by\n all the parameters. If no command is given, the current alias for\n *name* is shown. If no arguments are given, all aliases are listed.\n\n Aliases may be nested and can contain anything that can be legally\n typed at the pdb prompt. Note that internal pdb commands *can* be\n overridden by aliases. Such a command is then hidden until the\n alias is removed. Aliasing is recursively applied to the first\n word of the command line; all other words in the line are left\n alone.\n\n As an example, here are two useful aliases (especially when placed\n in the ``.pdbrc`` file):\n\n # Print instance variables (usage "pi classInst")\n alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k])\n # Print instance variables in self\n alias ps pi self\n\nunalias name\n\n Delete the specified alias.\n\n! statement\n\n Execute the (one-line) *statement* in the context of the current\n stack frame. The exclamation point can be omitted unless the first\n word of the statement resembles a debugger command. To set a\n global variable, you can prefix the assignment command with a\n ``global`` statement on the same line, e.g.:\n\n (Pdb) global list_options; list_options = [\'-l\']\n (Pdb)\n\nrun [args ...]\nrestart [args ...]\n\n Restart the debugged Python program. If an argument is supplied,\n it is split with ``shlex`` and the result is used as the new\n ``sys.argv``. History, breakpoints, actions and debugger options\n are preserved. ``restart`` is an alias for ``run``.\n\nq(uit)\n\n Quit from the debugger. The program being executed is aborted.\n\n-[ Footnotes ]-\n\n[1] Whether a frame is considered to originate in a certain module is\n determined by the ``__name__`` in the frame globals.\n', 'del': '\nThe ``del`` statement\n*********************\n\n del_stmt ::= "del" target_list\n\nDeletion is recursively defined very similar to the way assignment is\ndefined. Rather than spelling it out in full details, here are some\nhints.\n\nDeletion of a target list recursively deletes each target, from left\nto right.\n\nDeletion of a name removes the binding of that name from the local or\nglobal namespace, depending on whether the name occurs in a ``global``\nstatement in the same code block. If the name is unbound, a\n``NameError`` exception will be raised.\n\nDeletion of attribute references, subscriptions and slicings is passed\nto the primary object involved; deletion of a slicing is in general\nequivalent to assignment of an empty slice of the right type (but even\nthis is determined by the sliced object).\n\nChanged in version 3.2.\n', 'dict': '\nDictionary displays\n*******************\n\nA dictionary display is a possibly empty series of key/datum pairs\nenclosed in curly braces:\n\n dict_display ::= "{" [key_datum_list | dict_comprehension] "}"\n key_datum_list ::= key_datum ("," key_datum)* [","]\n key_datum ::= expression ":" expression\n dict_comprehension ::= expression ":" expression comp_for\n\nA dictionary display yields a new dictionary object.\n\nIf a comma-separated sequence of key/datum pairs is given, they are\nevaluated from left to right to define the entries of the dictionary:\neach key object is used as a key into the dictionary to store the\ncorresponding datum. This means that you can specify the same key\nmultiple times in the key/datum list, and the final dictionary\'s value\nfor that key will be the last one given.\n\nA dict comprehension, in contrast to list and set comprehensions,\nneeds two expressions separated with a colon followed by the usual\n"for" and "if" clauses. When the comprehension is run, the resulting\nkey and value elements are inserted in the new dictionary in the order\nthey are produced.\n\nRestrictions on the types of the key values are listed earlier in\nsection *The standard type hierarchy*. (To summarize, the key type\nshould be *hashable*, which excludes all mutable objects.) Clashes\nbetween duplicate keys are not detected; the last datum (textually\nrightmost in the display) stored for a given key value prevails.\n', @@ -34,7 +34,7 @@ 'exprlists': '\nExpression lists\n****************\n\n expression_list ::= expression ( "," expression )* [","]\n\nAn expression list containing at least one comma yields a tuple. The\nlength of the tuple is the number of expressions in the list. The\nexpressions are evaluated from left to right.\n\nThe trailing comma is required only to create a single tuple (a.k.a. a\n*singleton*); it is optional in all other cases. A single expression\nwithout a trailing comma doesn\'t create a tuple, but rather yields the\nvalue of that expression. (To create an empty tuple, use an empty pair\nof parentheses: ``()``.)\n', 'floating': '\nFloating point literals\n***********************\n\nFloating point literals are described by the following lexical\ndefinitions:\n\n floatnumber ::= pointfloat | exponentfloat\n pointfloat ::= [intpart] fraction | intpart "."\n exponentfloat ::= (intpart | pointfloat) exponent\n intpart ::= digit+\n fraction ::= "." digit+\n exponent ::= ("e" | "E") ["+" | "-"] digit+\n\nNote that the integer and exponent parts are always interpreted using\nradix 10. For example, ``077e010`` is legal, and denotes the same\nnumber as ``77e10``. The allowed range of floating point literals is\nimplementation-dependent. Some examples of floating point literals:\n\n 3.14 10. .001 1e100 3.14e-10 0e0\n\nNote that numeric literals do not include a sign; a phrase like ``-1``\nis actually an expression composed of the unary operator ``-`` and the\nliteral ``1``.\n', 'for': '\nThe ``for`` statement\n*********************\n\nThe ``for`` statement is used to iterate over the elements of a\nsequence (such as a string, tuple or list) or other iterable object:\n\n for_stmt ::= "for" target_list "in" expression_list ":" suite\n ["else" ":" suite]\n\nThe expression list is evaluated once; it should yield an iterable\nobject. An iterator is created for the result of the\n``expression_list``. The suite is then executed once for each item\nprovided by the iterator, in the order of ascending indices. Each\nitem in turn is assigned to the target list using the standard rules\nfor assignments (see *Assignment statements*), and then the suite is\nexecuted. When the items are exhausted (which is immediately when the\nsequence is empty or an iterator raises a ``StopIteration``\nexception), the suite in the ``else`` clause, if present, is executed,\nand the loop terminates.\n\nA ``break`` statement executed in the first suite terminates the loop\nwithout executing the ``else`` clause\'s suite. A ``continue``\nstatement executed in the first suite skips the rest of the suite and\ncontinues with the next item, or with the ``else`` clause if there was\nno next item.\n\nThe suite may assign to the variable(s) in the target list; this does\nnot affect the next item assigned to it.\n\nNames in the target list are not deleted when the loop is finished,\nbut if the sequence is empty, it will not have been assigned to at all\nby the loop. Hint: the built-in function ``range()`` returns an\niterator of integers suitable to emulate the effect of Pascal\'s ``for\ni := a to b do``; e.g., ``list(range(3))`` returns the list ``[0, 1,\n2]``.\n\nNote: There is a subtlety when the sequence is being modified by the loop\n (this can only occur for mutable sequences, i.e. lists). An\n internal counter is used to keep track of which item is used next,\n and this is incremented on each iteration. When this counter has\n reached the length of the sequence the loop terminates. This means\n that if the suite deletes the current (or a previous) item from the\n sequence, the next item will be skipped (since it gets the index of\n the current item which has already been treated). Likewise, if the\n suite inserts an item in the sequence before the current item, the\n current item will be treated again the next time through the loop.\n This can lead to nasty bugs that can be avoided by making a\n temporary copy using a slice of the whole sequence, e.g.,\n\n for x in a[:]:\n if x < 0: a.remove(x)\n', - 'formatstrings': '\nFormat String Syntax\n********************\n\nThe ``str.format()`` method and the ``Formatter`` class share the same\nsyntax for format strings (although in the case of ``Formatter``,\nsubclasses can define their own format string syntax).\n\nFormat strings contain "replacement fields" surrounded by curly braces\n``{}``. Anything that is not contained in braces is considered literal\ntext, which is copied unchanged to the output. If you need to include\na brace character in the literal text, it can be escaped by doubling:\n``{{`` and ``}}``.\n\nThe grammar for a replacement field is as follows:\n\n replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"\n field_name ::= arg_name ("." attribute_name | "[" element_index "]")*\n arg_name ::= [identifier | integer]\n attribute_name ::= identifier\n element_index ::= integer | index_string\n index_string ::= +\n conversion ::= "r" | "s" | "a"\n format_spec ::= \n\nIn less formal terms, the replacement field can start with a\n*field_name* that specifies the object whose value is to be formatted\nand inserted into the output instead of the replacement field. The\n*field_name* is optionally followed by a *conversion* field, which is\npreceded by an exclamation point ``\'!\'``, and a *format_spec*, which\nis preceded by a colon ``\':\'``. These specify a non-default format\nfor the replacement value.\n\nSee also the *Format Specification Mini-Language* section.\n\nThe *field_name* itself begins with an *arg_name* that is either a\nnumber or a keyword. If it\'s a number, it refers to a positional\nargument, and if it\'s a keyword, it refers to a named keyword\nargument. If the numerical arg_names in a format string are 0, 1, 2,\n... in sequence, they can all be omitted (not just some) and the\nnumbers 0, 1, 2, ... will be automatically inserted in that order.\nBecause *arg_name* is not quote-delimited, it is not possible to\nspecify arbitrary dictionary keys (e.g., the strings ``\'10\'`` or\n``\':-]\'``) within a format string. The *arg_name* can be followed by\nany number of index or attribute expressions. An expression of the\nform ``\'.name\'`` selects the named attribute using ``getattr()``,\nwhile an expression of the form ``\'[index]\'`` does an index lookup\nusing ``__getitem__()``.\n\nChanged in version 3.1: The positional argument specifiers can be\nomitted, so ``\'{} {}\'`` is equivalent to ``\'{0} {1}\'``.\n\nSome simple format string examples:\n\n "First, thou shalt count to {0}" # References first positional argument\n "Bring me a {}" # Implicitly references the first positional argument\n "From {} to {}" # Same as "From {0} to {1}"\n "My quest is {name}" # References keyword argument \'name\'\n "Weight in tons {0.weight}" # \'weight\' attribute of first positional arg\n "Units destroyed: {players[0]}" # First element of keyword argument \'players\'.\n\nThe *conversion* field causes a type coercion before formatting.\nNormally, the job of formatting a value is done by the\n``__format__()`` method of the value itself. However, in some cases\nit is desirable to force a type to be formatted as a string,\noverriding its own definition of formatting. By converting the value\nto a string before calling ``__format__()``, the normal formatting\nlogic is bypassed.\n\nThree conversion flags are currently supported: ``\'!s\'`` which calls\n``str()`` on the value, ``\'!r\'`` which calls ``repr()`` and ``\'!a\'``\nwhich calls ``ascii()``.\n\nSome examples:\n\n "Harold\'s a clever {0!s}" # Calls str() on the argument first\n "Bring out the holy {name!r}" # Calls repr() on the argument first\n "More {!a}" # Calls ascii() on the argument first\n\nThe *format_spec* field contains a specification of how the value\nshould be presented, including such details as field width, alignment,\npadding, decimal precision and so on. Each value type can define its\nown "formatting mini-language" or interpretation of the *format_spec*.\n\nMost built-in types support a common formatting mini-language, which\nis described in the next section.\n\nA *format_spec* field can also include nested replacement fields\nwithin it. These nested replacement fields can contain only a field\nname; conversion flags and format specifications are not allowed. The\nreplacement fields within the format_spec are substituted before the\n*format_spec* string is interpreted. This allows the formatting of a\nvalue to be dynamically specified.\n\nSee the *Format examples* section for some examples.\n\n\nFormat Specification Mini-Language\n==================================\n\n"Format specifications" are used within replacement fields contained\nwithin a format string to define how individual values are presented\n(see *Format String Syntax*). They can also be passed directly to the\nbuilt-in ``format()`` function. Each formattable type may define how\nthe format specification is to be interpreted.\n\nMost built-in types implement the following options for format\nspecifications, although some of the formatting options are only\nsupported by the numeric types.\n\nA general convention is that an empty format string (``""``) produces\nthe same result as if you had called ``str()`` on the value. A non-\nempty format string typically modifies the result.\n\nThe general form of a *standard format specifier* is:\n\n format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]\n fill ::= \n align ::= "<" | ">" | "=" | "^"\n sign ::= "+" | "-" | " "\n width ::= integer\n precision ::= integer\n type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"\n\nThe *fill* character can be any character other than \'{\' or \'}\'. The\npresence of a fill character is signaled by the character following\nit, which must be one of the alignment options. If the second\ncharacter of *format_spec* is not a valid alignment option, then it is\nassumed that both the fill character and the alignment option are\nabsent.\n\nThe meaning of the various alignment options is as follows:\n\n +-----------+------------------------------------------------------------+\n | Option | Meaning |\n +===========+============================================================+\n | ``\'<\'`` | Forces the field to be left-aligned within the available |\n | | space (this is the default for most objects). |\n +-----------+------------------------------------------------------------+\n | ``\'>\'`` | Forces the field to be right-aligned within the available |\n | | space (this is the default for numbers). |\n +-----------+------------------------------------------------------------+\n | ``\'=\'`` | Forces the padding to be placed after the sign (if any) |\n | | but before the digits. This is used for printing fields |\n | | in the form \'+000000120\'. This alignment option is only |\n | | valid for numeric types. |\n +-----------+------------------------------------------------------------+\n | ``\'^\'`` | Forces the field to be centered within the available |\n | | space. |\n +-----------+------------------------------------------------------------+\n\nNote that unless a minimum field width is defined, the field width\nwill always be the same size as the data to fill it, so that the\nalignment option has no meaning in this case.\n\nThe *sign* option is only valid for number types, and can be one of\nthe following:\n\n +-----------+------------------------------------------------------------+\n | Option | Meaning |\n +===========+============================================================+\n | ``\'+\'`` | indicates that a sign should be used for both positive as |\n | | well as negative numbers. |\n +-----------+------------------------------------------------------------+\n | ``\'-\'`` | indicates that a sign should be used only for negative |\n | | numbers (this is the default behavior). |\n +-----------+------------------------------------------------------------+\n | space | indicates that a leading space should be used on positive |\n | | numbers, and a minus sign on negative numbers. |\n +-----------+------------------------------------------------------------+\n\nThe ``\'#\'`` option causes the "alternate form" to be used for the\nconversion. The alternate form is defined differently for different\ntypes. This option is only valid for integer, float, complex and\nDecimal types. For integers, when binary, octal, or hexadecimal output\nis used, this option adds the prefix respective ``\'0b\'``, ``\'0o\'``, or\n``\'0x\'`` to the output value. For floats, complex and Decimal the\nalternate form causes the result of the conversion to always contain a\ndecimal-point character, even if no digits follow it. Normally, a\ndecimal-point character appears in the result of these conversions\nonly if a digit follows it. In addition, for ``\'g\'`` and ``\'G\'``\nconversions, trailing zeros are not removed from the result.\n\nThe ``\',\'`` option signals the use of a comma for a thousands\nseparator. For a locale aware separator, use the ``\'n\'`` integer\npresentation type instead.\n\nChanged in version 3.1: Added the ``\',\'`` option (see also **PEP\n378**).\n\n*width* is a decimal integer defining the minimum field width. If not\nspecified, then the field width will be determined by the content.\n\nIf the *width* field is preceded by a zero (``\'0\'``) character, this\nenables zero-padding. This is equivalent to an *alignment* type of\n``\'=\'`` and a *fill* character of ``\'0\'``.\n\nThe *precision* is a decimal number indicating how many digits should\nbe displayed after the decimal point for a floating point value\nformatted with ``\'f\'`` and ``\'F\'``, or before and after the decimal\npoint for a floating point value formatted with ``\'g\'`` or ``\'G\'``.\nFor non-number types the field indicates the maximum field size - in\nother words, how many characters will be used from the field content.\nThe *precision* is not allowed for integer values.\n\nFinally, the *type* determines how the data should be presented.\n\nThe available string presentation types are:\n\n +-----------+------------------------------------------------------------+\n | Type | Meaning |\n +===========+============================================================+\n | ``\'s\'`` | String format. This is the default type for strings and |\n | | may be omitted. |\n +-----------+------------------------------------------------------------+\n | None | The same as ``\'s\'``. |\n +-----------+------------------------------------------------------------+\n\nThe available integer presentation types are:\n\n +-----------+------------------------------------------------------------+\n | Type | Meaning |\n +===========+============================================================+\n | ``\'b\'`` | Binary format. Outputs the number in base 2. |\n +-----------+------------------------------------------------------------+\n | ``\'c\'`` | Character. Converts the integer to the corresponding |\n | | unicode character before printing. |\n +-----------+------------------------------------------------------------+\n | ``\'d\'`` | Decimal Integer. Outputs the number in base 10. |\n +-----------+------------------------------------------------------------+\n | ``\'o\'`` | Octal format. Outputs the number in base 8. |\n +-----------+------------------------------------------------------------+\n | ``\'x\'`` | Hex format. Outputs the number in base 16, using lower- |\n | | case letters for the digits above 9. |\n +-----------+------------------------------------------------------------+\n | ``\'X\'`` | Hex format. Outputs the number in base 16, using upper- |\n | | case letters for the digits above 9. |\n +-----------+------------------------------------------------------------+\n | ``\'n\'`` | Number. This is the same as ``\'d\'``, except that it uses |\n | | the current locale setting to insert the appropriate |\n | | number separator characters. |\n +-----------+------------------------------------------------------------+\n | None | The same as ``\'d\'``. |\n +-----------+------------------------------------------------------------+\n\nIn addition to the above presentation types, integers can be formatted\nwith the floating point presentation types listed below (except\n``\'n\'`` and None). When doing so, ``float()`` is used to convert the\ninteger to a floating point number before formatting.\n\nThe available presentation types for floating point and decimal values\nare:\n\n +-----------+------------------------------------------------------------+\n | Type | Meaning |\n +===========+============================================================+\n | ``\'e\'`` | Exponent notation. Prints the number in scientific |\n | | notation using the letter \'e\' to indicate the exponent. |\n +-----------+------------------------------------------------------------+\n | ``\'E\'`` | Exponent notation. Same as ``\'e\'`` except it uses an upper |\n | | case \'E\' as the separator character. |\n +-----------+------------------------------------------------------------+\n | ``\'f\'`` | Fixed point. Displays the number as a fixed-point number. |\n +-----------+------------------------------------------------------------+\n | ``\'F\'`` | Fixed point. Same as ``\'f\'``, but converts ``nan`` to |\n | | ``NAN`` and ``inf`` to ``INF``. |\n +-----------+------------------------------------------------------------+\n | ``\'g\'`` | General format. For a given precision ``p >= 1``, this |\n | | rounds the number to ``p`` significant digits and then |\n | | formats the result in either fixed-point format or in |\n | | scientific notation, depending on its magnitude. The |\n | | precise rules are as follows: suppose that the result |\n | | formatted with presentation type ``\'e\'`` and precision |\n | | ``p-1`` would have exponent ``exp``. Then if ``-4 <= exp |\n | | < p``, the number is formatted with presentation type |\n | | ``\'f\'`` and precision ``p-1-exp``. Otherwise, the number |\n | | is formatted with presentation type ``\'e\'`` and precision |\n | | ``p-1``. In both cases insignificant trailing zeros are |\n | | removed from the significand, and the decimal point is |\n | | also removed if there are no remaining digits following |\n | | it. Positive and negative infinity, positive and negative |\n | | zero, and nans, are formatted as ``inf``, ``-inf``, ``0``, |\n | | ``-0`` and ``nan`` respectively, regardless of the |\n | | precision. A precision of ``0`` is treated as equivalent |\n | | to a precision of ``1``. |\n +-----------+------------------------------------------------------------+\n | ``\'G\'`` | General format. Same as ``\'g\'`` except switches to ``\'E\'`` |\n | | if the number gets too large. The representations of |\n | | infinity and NaN are uppercased, too. |\n +-----------+------------------------------------------------------------+\n | ``\'n\'`` | Number. This is the same as ``\'g\'``, except that it uses |\n | | the current locale setting to insert the appropriate |\n | | number separator characters. |\n +-----------+------------------------------------------------------------+\n | ``\'%\'`` | Percentage. Multiplies the number by 100 and displays in |\n | | fixed (``\'f\'``) format, followed by a percent sign. |\n +-----------+------------------------------------------------------------+\n | None | Similar to ``\'g\'``, except with at least one digit past |\n | | the decimal point and a default precision of 12. This is |\n | | intended to match ``str()``, except you can add the other |\n | | format modifiers. |\n +-----------+------------------------------------------------------------+\n\n\nFormat examples\n===============\n\nThis section contains examples of the new format syntax and comparison\nwith the old ``%``-formatting.\n\nIn most of the cases the syntax is similar to the old\n``%``-formatting, with the addition of the ``{}`` and with ``:`` used\ninstead of ``%``. For example, ``\'%03.2f\'`` can be translated to\n``\'{:03.2f}\'``.\n\nThe new format syntax also supports new and different options, shown\nin the follow examples.\n\nAccessing arguments by position:\n\n >>> \'{0}, {1}, {2}\'.format(\'a\', \'b\', \'c\')\n \'a, b, c\'\n >>> \'{}, {}, {}\'.format(\'a\', \'b\', \'c\') # 3.1+ only\n \'a, b, c\'\n >>> \'{2}, {1}, {0}\'.format(\'a\', \'b\', \'c\')\n \'c, b, a\'\n >>> \'{2}, {1}, {0}\'.format(*\'abc\') # unpacking argument sequence\n \'c, b, a\'\n >>> \'{0}{1}{0}\'.format(\'abra\', \'cad\') # arguments\' indices can be repeated\n \'abracadabra\'\n\nAccessing arguments by name:\n\n >>> \'Coordinates: {latitude}, {longitude}\'.format(latitude=\'37.24N\', longitude=\'-115.81W\')\n \'Coordinates: 37.24N, -115.81W\'\n >>> coord = {\'latitude\': \'37.24N\', \'longitude\': \'-115.81W\'}\n >>> \'Coordinates: {latitude}, {longitude}\'.format(**coord)\n \'Coordinates: 37.24N, -115.81W\'\n\nAccessing arguments\' attributes:\n\n >>> c = 3-5j\n >>> (\'The complex number {0} is formed from the real part {0.real} \'\n ... \'and the imaginary part {0.imag}.\').format(c)\n \'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.\'\n >>> class Point:\n ... def __init__(self, x, y):\n ... self.x, self.y = x, y\n ... def __str__(self):\n ... return \'Point({self.x}, {self.y})\'.format(self=self)\n ...\n >>> str(Point(4, 2))\n \'Point(4, 2)\'\n\nAccessing arguments\' items:\n\n >>> coord = (3, 5)\n >>> \'X: {0[0]}; Y: {0[1]}\'.format(coord)\n \'X: 3; Y: 5\'\n\nReplacing ``%s`` and ``%r``:\n\n >>> "repr() shows quotes: {!r}; str() doesn\'t: {!s}".format(\'test1\', \'test2\')\n "repr() shows quotes: \'test1\'; str() doesn\'t: test2"\n\nAligning the text and specifying a width:\n\n >>> \'{:<30}\'.format(\'left aligned\')\n \'left aligned \'\n >>> \'{:>30}\'.format(\'right aligned\')\n \' right aligned\'\n >>> \'{:^30}\'.format(\'centered\')\n \' centered \'\n >>> \'{:*^30}\'.format(\'centered\') # use \'*\' as a fill char\n \'***********centered***********\'\n\nReplacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign:\n\n >>> \'{:+f}; {:+f}\'.format(3.14, -3.14) # show it always\n \'+3.140000; -3.140000\'\n >>> \'{: f}; {: f}\'.format(3.14, -3.14) # show a space for positive numbers\n \' 3.140000; -3.140000\'\n >>> \'{:-f}; {:-f}\'.format(3.14, -3.14) # show only the minus -- same as \'{:f}; {:f}\'\n \'3.140000; -3.140000\'\n\nReplacing ``%x`` and ``%o`` and converting the value to different\nbases:\n\n >>> # format also supports binary numbers\n >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42)\n \'int: 42; hex: 2a; oct: 52; bin: 101010\'\n >>> # with 0x, 0o, or 0b as prefix:\n >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)\n \'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010\'\n\nUsing the comma as a thousands separator:\n\n >>> \'{:,}\'.format(1234567890)\n \'1,234,567,890\'\n\nExpressing a percentage:\n\n >>> points = 19\n >>> total = 22\n >>> \'Correct answers: {:.2%}\'.format(points/total)\n \'Correct answers: 86.36%\'\n\nUsing type-specific formatting:\n\n >>> import datetime\n >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n >>> \'{:%Y-%m-%d %H:%M:%S}\'.format(d)\n \'2010-07-04 12:15:58\'\n\nNesting arguments and more complex examples:\n\n >>> for align, text in zip(\'<^>\', [\'left\', \'center\', \'right\']):\n ... \'{0:{fill}{align}16}\'.format(text, fill=align, align=align)\n ...\n \'left<<<<<<<<<<<<\'\n \'^^^^^center^^^^^\'\n \'>>>>>>>>>>>right\'\n >>>\n >>> octets = [192, 168, 0, 1]\n >>> \'{:02X}{:02X}{:02X}{:02X}\'.format(*octets)\n \'C0A80001\'\n >>> int(_, 16)\n 3232235521\n >>>\n >>> width = 5\n >>> for num in range(5,12):\n ... for base in \'dXob\':\n ... print(\'{0:{width}{base}}\'.format(num, base=base, width=width), end=\' \')\n ... print()\n ...\n 5 5 5 101\n 6 6 6 110\n 7 7 7 111\n 8 8 10 1000\n 9 9 11 1001\n 10 A 12 1010\n 11 B 13 1011\n', + 'formatstrings': '\nFormat String Syntax\n********************\n\nThe ``str.format()`` method and the ``Formatter`` class share the same\nsyntax for format strings (although in the case of ``Formatter``,\nsubclasses can define their own format string syntax).\n\nFormat strings contain "replacement fields" surrounded by curly braces\n``{}``. Anything that is not contained in braces is considered literal\ntext, which is copied unchanged to the output. If you need to include\na brace character in the literal text, it can be escaped by doubling:\n``{{`` and ``}}``.\n\nThe grammar for a replacement field is as follows:\n\n replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"\n field_name ::= arg_name ("." attribute_name | "[" element_index "]")*\n arg_name ::= [identifier | integer]\n attribute_name ::= identifier\n element_index ::= integer | index_string\n index_string ::= +\n conversion ::= "r" | "s" | "a"\n format_spec ::= \n\nIn less formal terms, the replacement field can start with a\n*field_name* that specifies the object whose value is to be formatted\nand inserted into the output instead of the replacement field. The\n*field_name* is optionally followed by a *conversion* field, which is\npreceded by an exclamation point ``\'!\'``, and a *format_spec*, which\nis preceded by a colon ``\':\'``. These specify a non-default format\nfor the replacement value.\n\nSee also the *Format Specification Mini-Language* section.\n\nThe *field_name* itself begins with an *arg_name* that is either a\nnumber or a keyword. If it\'s a number, it refers to a positional\nargument, and if it\'s a keyword, it refers to a named keyword\nargument. If the numerical arg_names in a format string are 0, 1, 2,\n... in sequence, they can all be omitted (not just some) and the\nnumbers 0, 1, 2, ... will be automatically inserted in that order.\nBecause *arg_name* is not quote-delimited, it is not possible to\nspecify arbitrary dictionary keys (e.g., the strings ``\'10\'`` or\n``\':-]\'``) within a format string. The *arg_name* can be followed by\nany number of index or attribute expressions. An expression of the\nform ``\'.name\'`` selects the named attribute using ``getattr()``,\nwhile an expression of the form ``\'[index]\'`` does an index lookup\nusing ``__getitem__()``.\n\nChanged in version 3.1: The positional argument specifiers can be\nomitted, so ``\'{} {}\'`` is equivalent to ``\'{0} {1}\'``.\n\nSome simple format string examples:\n\n "First, thou shalt count to {0}" # References first positional argument\n "Bring me a {}" # Implicitly references the first positional argument\n "From {} to {}" # Same as "From {0} to {1}"\n "My quest is {name}" # References keyword argument \'name\'\n "Weight in tons {0.weight}" # \'weight\' attribute of first positional arg\n "Units destroyed: {players[0]}" # First element of keyword argument \'players\'.\n\nThe *conversion* field causes a type coercion before formatting.\nNormally, the job of formatting a value is done by the\n``__format__()`` method of the value itself. However, in some cases\nit is desirable to force a type to be formatted as a string,\noverriding its own definition of formatting. By converting the value\nto a string before calling ``__format__()``, the normal formatting\nlogic is bypassed.\n\nThree conversion flags are currently supported: ``\'!s\'`` which calls\n``str()`` on the value, ``\'!r\'`` which calls ``repr()`` and ``\'!a\'``\nwhich calls ``ascii()``.\n\nSome examples:\n\n "Harold\'s a clever {0!s}" # Calls str() on the argument first\n "Bring out the holy {name!r}" # Calls repr() on the argument first\n "More {!a}" # Calls ascii() on the argument first\n\nThe *format_spec* field contains a specification of how the value\nshould be presented, including such details as field width, alignment,\npadding, decimal precision and so on. Each value type can define its\nown "formatting mini-language" or interpretation of the *format_spec*.\n\nMost built-in types support a common formatting mini-language, which\nis described in the next section.\n\nA *format_spec* field can also include nested replacement fields\nwithin it. These nested replacement fields can contain only a field\nname; conversion flags and format specifications are not allowed. The\nreplacement fields within the format_spec are substituted before the\n*format_spec* string is interpreted. This allows the formatting of a\nvalue to be dynamically specified.\n\nSee the *Format examples* section for some examples.\n\n\nFormat Specification Mini-Language\n==================================\n\n"Format specifications" are used within replacement fields contained\nwithin a format string to define how individual values are presented\n(see *Format String Syntax*). They can also be passed directly to the\nbuilt-in ``format()`` function. Each formattable type may define how\nthe format specification is to be interpreted.\n\nMost built-in types implement the following options for format\nspecifications, although some of the formatting options are only\nsupported by the numeric types.\n\nA general convention is that an empty format string (``""``) produces\nthe same result as if you had called ``str()`` on the value. A non-\nempty format string typically modifies the result.\n\nThe general form of a *standard format specifier* is:\n\n format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]\n fill ::= \n align ::= "<" | ">" | "=" | "^"\n sign ::= "+" | "-" | " "\n width ::= integer\n precision ::= integer\n type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"\n\nThe *fill* character can be any character other than \'{\' or \'}\'. The\npresence of a fill character is signaled by the character following\nit, which must be one of the alignment options. If the second\ncharacter of *format_spec* is not a valid alignment option, then it is\nassumed that both the fill character and the alignment option are\nabsent.\n\nThe meaning of the various alignment options is as follows:\n\n +-----------+------------------------------------------------------------+\n | Option | Meaning |\n +===========+============================================================+\n | ``\'<\'`` | Forces the field to be left-aligned within the available |\n | | space (this is the default for most objects). |\n +-----------+------------------------------------------------------------+\n | ``\'>\'`` | Forces the field to be right-aligned within the available |\n | | space (this is the default for numbers). |\n +-----------+------------------------------------------------------------+\n | ``\'=\'`` | Forces the padding to be placed after the sign (if any) |\n | | but before the digits. This is used for printing fields |\n | | in the form \'+000000120\'. This alignment option is only |\n | | valid for numeric types. |\n +-----------+------------------------------------------------------------+\n | ``\'^\'`` | Forces the field to be centered within the available |\n | | space. |\n +-----------+------------------------------------------------------------+\n\nNote that unless a minimum field width is defined, the field width\nwill always be the same size as the data to fill it, so that the\nalignment option has no meaning in this case.\n\nThe *sign* option is only valid for number types, and can be one of\nthe following:\n\n +-----------+------------------------------------------------------------+\n | Option | Meaning |\n +===========+============================================================+\n | ``\'+\'`` | indicates that a sign should be used for both positive as |\n | | well as negative numbers. |\n +-----------+------------------------------------------------------------+\n | ``\'-\'`` | indicates that a sign should be used only for negative |\n | | numbers (this is the default behavior). |\n +-----------+------------------------------------------------------------+\n | space | indicates that a leading space should be used on positive |\n | | numbers, and a minus sign on negative numbers. |\n +-----------+------------------------------------------------------------+\n\nThe ``\'#\'`` option causes the "alternate form" to be used for the\nconversion. The alternate form is defined differently for different\ntypes. This option is only valid for integer, float, complex and\nDecimal types. For integers, when binary, octal, or hexadecimal output\nis used, this option adds the prefix respective ``\'0b\'``, ``\'0o\'``, or\n``\'0x\'`` to the output value. For floats, complex and Decimal the\nalternate form causes the result of the conversion to always contain a\ndecimal-point character, even if no digits follow it. Normally, a\ndecimal-point character appears in the result of these conversions\nonly if a digit follows it. In addition, for ``\'g\'`` and ``\'G\'``\nconversions, trailing zeros are not removed from the result.\n\nThe ``\',\'`` option signals the use of a comma for a thousands\nseparator. For a locale aware separator, use the ``\'n\'`` integer\npresentation type instead.\n\nChanged in version 3.1: Added the ``\',\'`` option (see also **PEP\n378**).\n\n*width* is a decimal integer defining the minimum field width. If not\nspecified, then the field width will be determined by the content.\n\nIf the *width* field is preceded by a zero (``\'0\'``) character, this\nenables zero-padding. This is equivalent to an *alignment* type of\n``\'=\'`` and a *fill* character of ``\'0\'``.\n\nThe *precision* is a decimal number indicating how many digits should\nbe displayed after the decimal point for a floating point value\nformatted with ``\'f\'`` and ``\'F\'``, or before and after the decimal\npoint for a floating point value formatted with ``\'g\'`` or ``\'G\'``.\nFor non-number types the field indicates the maximum field size - in\nother words, how many characters will be used from the field content.\nThe *precision* is not allowed for integer values.\n\nFinally, the *type* determines how the data should be presented.\n\nThe available string presentation types are:\n\n +-----------+------------------------------------------------------------+\n | Type | Meaning |\n +===========+============================================================+\n | ``\'s\'`` | String format. This is the default type for strings and |\n | | may be omitted. |\n +-----------+------------------------------------------------------------+\n | None | The same as ``\'s\'``. |\n +-----------+------------------------------------------------------------+\n\nThe available integer presentation types are:\n\n +-----------+------------------------------------------------------------+\n | Type | Meaning |\n +===========+============================================================+\n | ``\'b\'`` | Binary format. Outputs the number in base 2. |\n +-----------+------------------------------------------------------------+\n | ``\'c\'`` | Character. Converts the integer to the corresponding |\n | | unicode character before printing. |\n +-----------+------------------------------------------------------------+\n | ``\'d\'`` | Decimal Integer. Outputs the number in base 10. |\n +-----------+------------------------------------------------------------+\n | ``\'o\'`` | Octal format. Outputs the number in base 8. |\n +-----------+------------------------------------------------------------+\n | ``\'x\'`` | Hex format. Outputs the number in base 16, using lower- |\n | | case letters for the digits above 9. |\n +-----------+------------------------------------------------------------+\n | ``\'X\'`` | Hex format. Outputs the number in base 16, using upper- |\n | | case letters for the digits above 9. |\n +-----------+------------------------------------------------------------+\n | ``\'n\'`` | Number. This is the same as ``\'d\'``, except that it uses |\n | | the current locale setting to insert the appropriate |\n | | number separator characters. |\n +-----------+------------------------------------------------------------+\n | None | The same as ``\'d\'``. |\n +-----------+------------------------------------------------------------+\n\nIn addition to the above presentation types, integers can be formatted\nwith the floating point presentation types listed below (except\n``\'n\'`` and None). When doing so, ``float()`` is used to convert the\ninteger to a floating point number before formatting.\n\nThe available presentation types for floating point and decimal values\nare:\n\n +-----------+------------------------------------------------------------+\n | Type | Meaning |\n +===========+============================================================+\n | ``\'e\'`` | Exponent notation. Prints the number in scientific |\n | | notation using the letter \'e\' to indicate the exponent. |\n +-----------+------------------------------------------------------------+\n | ``\'E\'`` | Exponent notation. Same as ``\'e\'`` except it uses an upper |\n | | case \'E\' as the separator character. |\n +-----------+------------------------------------------------------------+\n | ``\'f\'`` | Fixed point. Displays the number as a fixed-point number. |\n +-----------+------------------------------------------------------------+\n | ``\'F\'`` | Fixed point. Same as ``\'f\'``, but converts ``nan`` to |\n | | ``NAN`` and ``inf`` to ``INF``. |\n +-----------+------------------------------------------------------------+\n | ``\'g\'`` | General format. For a given precision ``p >= 1``, this |\n | | rounds the number to ``p`` significant digits and then |\n | | formats the result in either fixed-point format or in |\n | | scientific notation, depending on its magnitude. The |\n | | precise rules are as follows: suppose that the result |\n | | formatted with presentation type ``\'e\'`` and precision |\n | | ``p-1`` would have exponent ``exp``. Then if ``-4 <= exp |\n | | < p``, the number is formatted with presentation type |\n | | ``\'f\'`` and precision ``p-1-exp``. Otherwise, the number |\n | | is formatted with presentation type ``\'e\'`` and precision |\n | | ``p-1``. In both cases insignificant trailing zeros are |\n | | removed from the significand, and the decimal point is |\n | | also removed if there are no remaining digits following |\n | | it. Positive and negative infinity, positive and negative |\n | | zero, and nans, are formatted as ``inf``, ``-inf``, ``0``, |\n | | ``-0`` and ``nan`` respectively, regardless of the |\n | | precision. A precision of ``0`` is treated as equivalent |\n | | to a precision of ``1``. |\n +-----------+------------------------------------------------------------+\n | ``\'G\'`` | General format. Same as ``\'g\'`` except switches to ``\'E\'`` |\n | | if the number gets too large. The representations of |\n | | infinity and NaN are uppercased, too. |\n +-----------+------------------------------------------------------------+\n | ``\'n\'`` | Number. This is the same as ``\'g\'``, except that it uses |\n | | the current locale setting to insert the appropriate |\n | | number separator characters. |\n +-----------+------------------------------------------------------------+\n | ``\'%\'`` | Percentage. Multiplies the number by 100 and displays in |\n | | fixed (``\'f\'``) format, followed by a percent sign. |\n +-----------+------------------------------------------------------------+\n | None | Similar to ``\'g\'``, except with at least one digit past |\n | | the decimal point and a default precision of 12. This is |\n | | intended to match ``str()``, except you can add the other |\n | | format modifiers. |\n +-----------+------------------------------------------------------------+\n\n\nFormat examples\n===============\n\nThis section contains examples of the new format syntax and comparison\nwith the old ``%``-formatting.\n\nIn most of the cases the syntax is similar to the old\n``%``-formatting, with the addition of the ``{}`` and with ``:`` used\ninstead of ``%``. For example, ``\'%03.2f\'`` can be translated to\n``\'{:03.2f}\'``.\n\nThe new format syntax also supports new and different options, shown\nin the follow examples.\n\nAccessing arguments by position:\n\n >>> \'{0}, {1}, {2}\'.format(\'a\', \'b\', \'c\')\n \'a, b, c\'\n >>> \'{}, {}, {}\'.format(\'a\', \'b\', \'c\') # 3.1+ only\n \'a, b, c\'\n >>> \'{2}, {1}, {0}\'.format(\'a\', \'b\', \'c\')\n \'c, b, a\'\n >>> \'{2}, {1}, {0}\'.format(*\'abc\') # unpacking argument sequence\n \'c, b, a\'\n >>> \'{0}{1}{0}\'.format(\'abra\', \'cad\') # arguments\' indices can be repeated\n \'abracadabra\'\n\nAccessing arguments by name:\n\n >>> \'Coordinates: {latitude}, {longitude}\'.format(latitude=\'37.24N\', longitude=\'-115.81W\')\n \'Coordinates: 37.24N, -115.81W\'\n >>> coord = {\'latitude\': \'37.24N\', \'longitude\': \'-115.81W\'}\n >>> \'Coordinates: {latitude}, {longitude}\'.format(**coord)\n \'Coordinates: 37.24N, -115.81W\'\n\nAccessing arguments\' attributes:\n\n >>> c = 3-5j\n >>> (\'The complex number {0} is formed from the real part {0.real} \'\n ... \'and the imaginary part {0.imag}.\').format(c)\n \'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.\'\n >>> class Point:\n ... def __init__(self, x, y):\n ... self.x, self.y = x, y\n ... def __str__(self):\n ... return \'Point({self.x}, {self.y})\'.format(self=self)\n ...\n >>> str(Point(4, 2))\n \'Point(4, 2)\'\n\nAccessing arguments\' items:\n\n >>> coord = (3, 5)\n >>> \'X: {0[0]}; Y: {0[1]}\'.format(coord)\n \'X: 3; Y: 5\'\n\nReplacing ``%s`` and ``%r``:\n\n >>> "repr() shows quotes: {!r}; str() doesn\'t: {!s}".format(\'test1\', \'test2\')\n "repr() shows quotes: \'test1\'; str() doesn\'t: test2"\n\nAligning the text and specifying a width:\n\n >>> \'{:<30}\'.format(\'left aligned\')\n \'left aligned \'\n >>> \'{:>30}\'.format(\'right aligned\')\n \' right aligned\'\n >>> \'{:^30}\'.format(\'centered\')\n \' centered \'\n >>> \'{:*^30}\'.format(\'centered\') # use \'*\' as a fill char\n \'***********centered***********\'\n\nReplacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign:\n\n >>> \'{:+f}; {:+f}\'.format(3.14, -3.14) # show it always\n \'+3.140000; -3.140000\'\n >>> \'{: f}; {: f}\'.format(3.14, -3.14) # show a space for positive numbers\n \' 3.140000; -3.140000\'\n >>> \'{:-f}; {:-f}\'.format(3.14, -3.14) # show only the minus -- same as \'{:f}; {:f}\'\n \'3.140000; -3.140000\'\n\nReplacing ``%x`` and ``%o`` and converting the value to different\nbases:\n\n >>> # format also supports binary numbers\n >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42)\n \'int: 42; hex: 2a; oct: 52; bin: 101010\'\n >>> # with 0x, 0o, or 0b as prefix:\n >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)\n \'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010\'\n\nUsing the comma as a thousands separator:\n\n >>> \'{:,}\'.format(1234567890)\n \'1,234,567,890\'\n\nExpressing a percentage:\n\n >>> points = 19\n >>> total = 22\n >>> \'Correct answers: {:.2%}\'.format(points/total)\n \'Correct answers: 86.36%\'\n\nUsing type-specific formatting:\n\n >>> import datetime\n >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n >>> \'{:%Y-%m-%d %H:%M:%S}\'.format(d)\n \'2010-07-04 12:15:58\'\n\nNesting arguments and more complex examples:\n\n >>> for align, text in zip(\'<^>\', [\'left\', \'center\', \'right\']):\n ... \'{0:{fill}{align}16}\'.format(text, fill=align, align=align)\n ...\n \'left<<<<<<<<<<<<\'\n \'^^^^^center^^^^^\'\n \'>>>>>>>>>>>right\'\n >>>\n >>> octets = [192, 168, 0, 1]\n >>> \'{:02X}{:02X}{:02X}{:02X}\'.format(*octets)\n \'C0A80001\'\n >>> int(_, 16)\n 3232235521\n >>>\n >>> width = 5\n >>> for num in range(5,12):\n ... for base in \'dXob\':\n ... print(\'{0:{width}{base}}\'.format(num, base=base, width=width), end=\' \')\n ... print()\n ...\n 5 5 5 101\n 6 6 6 110\n 7 7 7 111\n 8 8 10 1000\n 9 9 11 1001\n 10 A 12 1010\n 11 B 13 1011\n', 'function': '\nFunction definitions\n********************\n\nA function definition defines a user-defined function object (see\nsection *The standard type hierarchy*):\n\n funcdef ::= [decorators] "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite\n decorators ::= decorator+\n decorator ::= "@" dotted_name ["(" [parameter_list [","]] ")"] NEWLINE\n dotted_name ::= identifier ("." identifier)*\n parameter_list ::= (defparameter ",")*\n ( "*" [parameter] ("," defparameter)*\n [, "**" parameter]\n | "**" parameter\n | defparameter [","] )\n parameter ::= identifier [":" expression]\n defparameter ::= parameter ["=" expression]\n funcname ::= identifier\n\nA function definition is an executable statement. Its execution binds\nthe function name in the current local namespace to a function object\n(a wrapper around the executable code for the function). This\nfunction object contains a reference to the current global namespace\nas the global namespace to be used when the function is called.\n\nThe function definition does not execute the function body; this gets\nexecuted only when the function is called. [3]\n\nA function definition may be wrapped by one or more *decorator*\nexpressions. Decorator expressions are evaluated when the function is\ndefined, in the scope that contains the function definition. The\nresult must be a callable, which is invoked with the function object\nas the only argument. The returned value is bound to the function name\ninstead of the function object. Multiple decorators are applied in\nnested fashion. For example, the following code\n\n @f1(arg)\n @f2\n def func(): pass\n\nis equivalent to\n\n def func(): pass\n func = f1(arg)(f2(func))\n\nWhen one or more parameters have the form *parameter* ``=``\n*expression*, the function is said to have "default parameter values."\nFor a parameter with a default value, the corresponding argument may\nbe omitted from a call, in which case the parameter\'s default value is\nsubstituted. If a parameter has a default value, all following\nparameters up until the "``*``" must also have a default value ---\nthis is a syntactic restriction that is not expressed by the grammar.\n\n**Default parameter values are evaluated when the function definition\nis executed.** This means that the expression is evaluated once, when\nthe function is defined, and that the same "pre-computed" value is\nused for each call. This is especially important to understand when a\ndefault parameter is a mutable object, such as a list or a dictionary:\nif the function modifies the object (e.g. by appending an item to a\nlist), the default value is in effect modified. This is generally not\nwhat was intended. A way around this is to use ``None`` as the\ndefault, and explicitly test for it in the body of the function, e.g.:\n\n def whats_on_the_telly(penguin=None):\n if penguin is None:\n penguin = []\n penguin.append("property of the zoo")\n return penguin\n\nFunction call semantics are described in more detail in section\n*Calls*. A function call always assigns values to all parameters\nmentioned in the parameter list, either from position arguments, from\nkeyword arguments, or from default values. If the form\n"``*identifier``" is present, it is initialized to a tuple receiving\nany excess positional parameters, defaulting to the empty tuple. If\nthe form "``**identifier``" is present, it is initialized to a new\ndictionary receiving any excess keyword arguments, defaulting to a new\nempty dictionary. Parameters after "``*``" or "``*identifier``" are\nkeyword-only parameters and may only be passed used keyword arguments.\n\nParameters may have annotations of the form "``: expression``"\nfollowing the parameter name. Any parameter may have an annotation\neven those of the form ``*identifier`` or ``**identifier``. Functions\nmay have "return" annotation of the form "``-> expression``" after the\nparameter list. These annotations can be any valid Python expression\nand are evaluated when the function definition is executed.\nAnnotations may be evaluated in a different order than they appear in\nthe source code. The presence of annotations does not change the\nsemantics of a function. The annotation values are available as\nvalues of a dictionary keyed by the parameters\' names in the\n``__annotations__`` attribute of the function object.\n\nIt is also possible to create anonymous functions (functions not bound\nto a name), for immediate use in expressions. This uses lambda forms,\ndescribed in section *Lambdas*. Note that the lambda form is merely a\nshorthand for a simplified function definition; a function defined in\na "``def``" statement can be passed around or assigned to another name\njust like a function defined by a lambda form. The "``def``" form is\nactually more powerful since it allows the execution of multiple\nstatements and annotations.\n\n**Programmer\'s note:** Functions are first-class objects. A "``def``"\nform executed inside a function definition defines a local function\nthat can be returned or passed around. Free variables used in the\nnested function can access the local variables of the function\ncontaining the def. See section *Naming and binding* for details.\n\nSee also:\n\n **PEP 3107** - Function Annotations\n The original specification for function annotations.\n', 'global': '\nThe ``global`` statement\n************************\n\n global_stmt ::= "global" identifier ("," identifier)*\n\nThe ``global`` statement is a declaration which holds for the entire\ncurrent code block. It means that the listed identifiers are to be\ninterpreted as globals. It would be impossible to assign to a global\nvariable without ``global``, although free variables may refer to\nglobals without being declared global.\n\nNames listed in a ``global`` statement must not be used in the same\ncode block textually preceding that ``global`` statement.\n\nNames listed in a ``global`` statement must not be defined as formal\nparameters or in a ``for`` loop control target, ``class`` definition,\nfunction definition, or ``import`` statement.\n\n**CPython implementation detail:** The current implementation does not\nenforce the latter two restrictions, but programs should not abuse\nthis freedom, as future implementations may enforce them or silently\nchange the meaning of the program.\n\n**Programmer\'s note:** the ``global`` is a directive to the parser.\nIt applies only to code parsed at the same time as the ``global``\nstatement. In particular, a ``global`` statement contained in a string\nor code object supplied to the built-in ``exec()`` function does not\naffect the code block *containing* the function call, and code\ncontained in such a string is unaffected by ``global`` statements in\nthe code containing the function call. The same applies to the\n``eval()`` and ``compile()`` functions.\n', 'id-classes': '\nReserved classes of identifiers\n*******************************\n\nCertain classes of identifiers (besides keywords) have special\nmeanings. These classes are identified by the patterns of leading and\ntrailing underscore characters:\n\n``_*``\n Not imported by ``from module import *``. The special identifier\n ``_`` is used in the interactive interpreter to store the result of\n the last evaluation; it is stored in the ``builtins`` module. When\n not in interactive mode, ``_`` has no special meaning and is not\n defined. See section *The import statement*.\n\n Note: The name ``_`` is often used in conjunction with\n internationalization; refer to the documentation for the\n ``gettext`` module for more information on this convention.\n\n``__*__``\n System-defined names. These names are defined by the interpreter\n and its implementation (including the standard library). Current\n system names are discussed in the *Special method names* section\n and elsewhere. More will likely be defined in future versions of\n Python. *Any* use of ``__*__`` names, in any context, that does\n not follow explicitly documented use, is subject to breakage\n without warning.\n\n``__*``\n Class-private names. Names in this category, when used within the\n context of a class definition, are re-written to use a mangled form\n to help avoid name clashes between "private" attributes of base and\n derived classes. See section *Identifiers (Names)*.\n', @@ -50,7 +50,7 @@ 'nonlocal': '\nThe ``nonlocal`` statement\n**************************\n\n nonlocal_stmt ::= "nonlocal" identifier ("," identifier)*\n\nThe ``nonlocal`` statement causes the listed identifiers to refer to\npreviously bound variables in the nearest enclosing scope. This is\nimportant because the default behavior for binding is to search the\nlocal namespace first. The statement allows encapsulated code to\nrebind variables outside of the local scope besides the global\n(module) scope.\n\nNames listed in a ``nonlocal`` statement, unlike to those listed in a\n``global`` statement, must refer to pre-existing bindings in an\nenclosing scope (the scope in which a new binding should be created\ncannot be determined unambiguously).\n\nNames listed in a ``nonlocal`` statement must not collide with pre-\nexisting bindings in the local scope.\n\nSee also:\n\n **PEP 3104** - Access to Names in Outer Scopes\n The specification for the ``nonlocal`` statement.\n', 'numbers': "\nNumeric literals\n****************\n\nThere are three types of numeric literals: integers, floating point\nnumbers, and imaginary numbers. There are no complex literals\n(complex numbers can be formed by adding a real number and an\nimaginary number).\n\nNote that numeric literals do not include a sign; a phrase like ``-1``\nis actually an expression composed of the unary operator '``-``' and\nthe literal ``1``.\n", 'numeric-types': "\nEmulating numeric types\n***********************\n\nThe following methods can be defined to emulate numeric objects.\nMethods corresponding to operations that are not supported by the\nparticular kind of number implemented (e.g., bitwise operations for\nnon-integral numbers) should be left undefined.\n\nobject.__add__(self, other)\nobject.__sub__(self, other)\nobject.__mul__(self, other)\nobject.__truediv__(self, other)\nobject.__floordiv__(self, other)\nobject.__mod__(self, other)\nobject.__divmod__(self, other)\nobject.__pow__(self, other[, modulo])\nobject.__lshift__(self, other)\nobject.__rshift__(self, other)\nobject.__and__(self, other)\nobject.__xor__(self, other)\nobject.__or__(self, other)\n\n These methods are called to implement the binary arithmetic\n operations (``+``, ``-``, ``*``, ``/``, ``//``, ``%``,\n ``divmod()``, ``pow()``, ``**``, ``<<``, ``>>``, ``&``, ``^``,\n ``|``). For instance, to evaluate the expression ``x + y``, where\n *x* is an instance of a class that has an ``__add__()`` method,\n ``x.__add__(y)`` is called. The ``__divmod__()`` method should be\n the equivalent to using ``__floordiv__()`` and ``__mod__()``; it\n should not be related to ``__truediv__()``. Note that\n ``__pow__()`` should be defined to accept an optional third\n argument if the ternary version of the built-in ``pow()`` function\n is to be supported.\n\n If one of those methods does not support the operation with the\n supplied arguments, it should return ``NotImplemented``.\n\nobject.__radd__(self, other)\nobject.__rsub__(self, other)\nobject.__rmul__(self, other)\nobject.__rtruediv__(self, other)\nobject.__rfloordiv__(self, other)\nobject.__rmod__(self, other)\nobject.__rdivmod__(self, other)\nobject.__rpow__(self, other)\nobject.__rlshift__(self, other)\nobject.__rrshift__(self, other)\nobject.__rand__(self, other)\nobject.__rxor__(self, other)\nobject.__ror__(self, other)\n\n These methods are called to implement the binary arithmetic\n operations (``+``, ``-``, ``*``, ``/``, ``//``, ``%``,\n ``divmod()``, ``pow()``, ``**``, ``<<``, ``>>``, ``&``, ``^``,\n ``|``) with reflected (swapped) operands. These functions are only\n called if the left operand does not support the corresponding\n operation and the operands are of different types. [2] For\n instance, to evaluate the expression ``x - y``, where *y* is an\n instance of a class that has an ``__rsub__()`` method,\n ``y.__rsub__(x)`` is called if ``x.__sub__(y)`` returns\n *NotImplemented*.\n\n Note that ternary ``pow()`` will not try calling ``__rpow__()``\n (the coercion rules would become too complicated).\n\n Note: If the right operand's type is a subclass of the left operand's\n type and that subclass provides the reflected method for the\n operation, this method will be called before the left operand's\n non-reflected method. This behavior allows subclasses to\n override their ancestors' operations.\n\nobject.__iadd__(self, other)\nobject.__isub__(self, other)\nobject.__imul__(self, other)\nobject.__itruediv__(self, other)\nobject.__ifloordiv__(self, other)\nobject.__imod__(self, other)\nobject.__ipow__(self, other[, modulo])\nobject.__ilshift__(self, other)\nobject.__irshift__(self, other)\nobject.__iand__(self, other)\nobject.__ixor__(self, other)\nobject.__ior__(self, other)\n\n These methods are called to implement the augmented arithmetic\n assignments (``+=``, ``-=``, ``*=``, ``/=``, ``//=``, ``%=``,\n ``**=``, ``<<=``, ``>>=``, ``&=``, ``^=``, ``|=``). These methods\n should attempt to do the operation in-place (modifying *self*) and\n return the result (which could be, but does not have to be,\n *self*). If a specific method is not defined, the augmented\n assignment falls back to the normal methods. For instance, to\n execute the statement ``x += y``, where *x* is an instance of a\n class that has an ``__iadd__()`` method, ``x.__iadd__(y)`` is\n called. If *x* is an instance of a class that does not define a\n ``__iadd__()`` method, ``x.__add__(y)`` and ``y.__radd__(x)`` are\n considered, as with the evaluation of ``x + y``.\n\nobject.__neg__(self)\nobject.__pos__(self)\nobject.__abs__(self)\nobject.__invert__(self)\n\n Called to implement the unary arithmetic operations (``-``, ``+``,\n ``abs()`` and ``~``).\n\nobject.__complex__(self)\nobject.__int__(self)\nobject.__float__(self)\nobject.__round__(self[, n])\n\n Called to implement the built-in functions ``complex()``,\n ``int()``, ``float()`` and ``round()``. Should return a value of\n the appropriate type.\n\nobject.__index__(self)\n\n Called to implement ``operator.index()``. Also called whenever\n Python needs an integer object (such as in slicing, or in the\n built-in ``bin()``, ``hex()`` and ``oct()`` functions). Must return\n an integer.\n", - 'objects': '\nObjects, values and types\n*************************\n\n*Objects* are Python\'s abstraction for data. All data in a Python\nprogram is represented by objects or by relations between objects. (In\na sense, and in conformance to Von Neumann\'s model of a "stored\nprogram computer," code is also represented by objects.)\n\nEvery object has an identity, a type and a value. An object\'s\n*identity* never changes once it has been created; you may think of it\nas the object\'s address in memory. The \'``is``\' operator compares the\nidentity of two objects; the ``id()`` function returns an integer\nrepresenting its identity.\n\n**CPython implementation detail:** For CPython, ``id(x)`` is the\nmemory address where ``x`` is stored.\n\nAn object\'s type determines the operations that the object supports\n(e.g., "does it have a length?") and also defines the possible values\nfor objects of that type. The ``type()`` function returns an object\'s\ntype (which is an object itself). Like its identity, an object\'s\n*type* is also unchangeable. [1]\n\nThe *value* of some objects can change. Objects whose value can\nchange are said to be *mutable*; objects whose value is unchangeable\nonce they are created are called *immutable*. (The value of an\nimmutable container object that contains a reference to a mutable\nobject can change when the latter\'s value is changed; however the\ncontainer is still considered immutable, because the collection of\nobjects it contains cannot be changed. So, immutability is not\nstrictly the same as having an unchangeable value, it is more subtle.)\nAn object\'s mutability is determined by its type; for instance,\nnumbers, strings and tuples are immutable, while dictionaries and\nlists are mutable.\n\nObjects are never explicitly destroyed; however, when they become\nunreachable they may be garbage-collected. An implementation is\nallowed to postpone garbage collection or omit it altogether --- it is\na matter of implementation quality how garbage collection is\nimplemented, as long as no objects are collected that are still\nreachable.\n\n**CPython implementation detail:** CPython currently uses a reference-\ncounting scheme with (optional) delayed detection of cyclically linked\ngarbage, which collects most objects as soon as they become\nunreachable, but is not guaranteed to collect garbage containing\ncircular references. See the documentation of the ``gc`` module for\ninformation on controlling the collection of cyclic garbage. Other\nimplementations act differently and CPython may change. Do not depend\non immediate finalization of objects when they become unreachable (ex:\nalways close files).\n\nNote that the use of the implementation\'s tracing or debugging\nfacilities may keep objects alive that would normally be collectable.\nAlso note that catching an exception with a \'``try``...``except``\'\nstatement may keep objects alive.\n\nSome objects contain references to "external" resources such as open\nfiles or windows. It is understood that these resources are freed\nwhen the object is garbage-collected, but since garbage collection is\nnot guaranteed to happen, such objects also provide an explicit way to\nrelease the external resource, usually a ``close()`` method. Programs\nare strongly recommended to explicitly close such objects. The\n\'``try``...``finally``\' statement and the \'``with``\' statement provide\nconvenient ways to do this.\n\nSome objects contain references to other objects; these are called\n*containers*. Examples of containers are tuples, lists and\ndictionaries. The references are part of a container\'s value. In\nmost cases, when we talk about the value of a container, we imply the\nvalues, not the identities of the contained objects; however, when we\ntalk about the mutability of a container, only the identities of the\nimmediately contained objects are implied. So, if an immutable\ncontainer (like a tuple) contains a reference to a mutable object, its\nvalue changes if that mutable object is changed.\n\nTypes affect almost all aspects of object behavior. Even the\nimportance of object identity is affected in some sense: for immutable\ntypes, operations that compute new values may actually return a\nreference to any existing object with the same type and value, while\nfor mutable objects this is not allowed. E.g., after ``a = 1; b =\n1``, ``a`` and ``b`` may or may not refer to the same object with the\nvalue one, depending on the implementation, but after ``c = []; d =\n[]``, ``c`` and ``d`` are guaranteed to refer to two different,\nunique, newly created empty lists. (Note that ``c = d = []`` assigns\nthe same object to both ``c`` and ``d``.)\n', + 'objects': '\nObjects, values and types\n*************************\n\n*Objects* are Python\'s abstraction for data. All data in a Python\nprogram is represented by objects or by relations between objects. (In\na sense, and in conformance to Von Neumann\'s model of a "stored\nprogram computer," code is also represented by objects.)\n\nEvery object has an identity, a type and a value. An object\'s\n*identity* never changes once it has been created; you may think of it\nas the object\'s address in memory. The \'``is``\' operator compares the\nidentity of two objects; the ``id()`` function returns an integer\nrepresenting its identity (currently implemented as its address). An\nobject\'s *type* is also unchangeable. [1] An object\'s type determines\nthe operations that the object supports (e.g., "does it have a\nlength?") and also defines the possible values for objects of that\ntype. The ``type()`` function returns an object\'s type (which is an\nobject itself). The *value* of some objects can change. Objects\nwhose value can change are said to be *mutable*; objects whose value\nis unchangeable once they are created are called *immutable*. (The\nvalue of an immutable container object that contains a reference to a\nmutable object can change when the latter\'s value is changed; however\nthe container is still considered immutable, because the collection of\nobjects it contains cannot be changed. So, immutability is not\nstrictly the same as having an unchangeable value, it is more subtle.)\nAn object\'s mutability is determined by its type; for instance,\nnumbers, strings and tuples are immutable, while dictionaries and\nlists are mutable.\n\nObjects are never explicitly destroyed; however, when they become\nunreachable they may be garbage-collected. An implementation is\nallowed to postpone garbage collection or omit it altogether --- it is\na matter of implementation quality how garbage collection is\nimplemented, as long as no objects are collected that are still\nreachable.\n\n**CPython implementation detail:** CPython currently uses a reference-\ncounting scheme with (optional) delayed detection of cyclically linked\ngarbage, which collects most objects as soon as they become\nunreachable, but is not guaranteed to collect garbage containing\ncircular references. See the documentation of the ``gc`` module for\ninformation on controlling the collection of cyclic garbage. Other\nimplementations act differently and CPython may change. Do not depend\non immediate finalization of objects when they become unreachable (ex:\nalways close files).\n\nNote that the use of the implementation\'s tracing or debugging\nfacilities may keep objects alive that would normally be collectable.\nAlso note that catching an exception with a \'``try``...``except``\'\nstatement may keep objects alive.\n\nSome objects contain references to "external" resources such as open\nfiles or windows. It is understood that these resources are freed\nwhen the object is garbage-collected, but since garbage collection is\nnot guaranteed to happen, such objects also provide an explicit way to\nrelease the external resource, usually a ``close()`` method. Programs\nare strongly recommended to explicitly close such objects. The\n\'``try``...``finally``\' statement and the \'``with``\' statement provide\nconvenient ways to do this.\n\nSome objects contain references to other objects; these are called\n*containers*. Examples of containers are tuples, lists and\ndictionaries. The references are part of a container\'s value. In\nmost cases, when we talk about the value of a container, we imply the\nvalues, not the identities of the contained objects; however, when we\ntalk about the mutability of a container, only the identities of the\nimmediately contained objects are implied. So, if an immutable\ncontainer (like a tuple) contains a reference to a mutable object, its\nvalue changes if that mutable object is changed.\n\nTypes affect almost all aspects of object behavior. Even the\nimportance of object identity is affected in some sense: for immutable\ntypes, operations that compute new values may actually return a\nreference to any existing object with the same type and value, while\nfor mutable objects this is not allowed. E.g., after ``a = 1; b =\n1``, ``a`` and ``b`` may or may not refer to the same object with the\nvalue one, depending on the implementation, but after ``c = []; d =\n[]``, ``c`` and ``d`` are guaranteed to refer to two different,\nunique, newly created empty lists. (Note that ``c = d = []`` assigns\nthe same object to both ``c`` and ``d``.)\n', 'operator-summary': '\nSummary\n*******\n\nThe following table summarizes the operator precedences in Python,\nfrom lowest precedence (least binding) to highest precedence (most\nbinding). Operators in the same box have the same precedence. Unless\nthe syntax is explicitly given, operators are binary. Operators in\nthe same box group left to right (except for comparisons, including\ntests, which all have the same precedence and chain from left to right\n--- see section *Comparisons* --- and exponentiation, which groups\nfrom right to left).\n\n+-------------------------------------------------+---------------------------------------+\n| Operator | Description |\n+=================================================+=======================================+\n| ``lambda`` | Lambda expression |\n+-------------------------------------------------+---------------------------------------+\n| ``if`` -- ``else`` | Conditional expression |\n+-------------------------------------------------+---------------------------------------+\n| ``or`` | Boolean OR |\n+-------------------------------------------------+---------------------------------------+\n| ``and`` | Boolean AND |\n+-------------------------------------------------+---------------------------------------+\n| ``not`` *x* | Boolean NOT |\n+-------------------------------------------------+---------------------------------------+\n| ``in``, ``not`` ``in``, ``is``, ``is not``, | Comparisons, including membership |\n| ``<``, ``<=``, ``>``, ``>=``, ``!=``, ``==`` | tests and identity tests, |\n+-------------------------------------------------+---------------------------------------+\n| ``|`` | Bitwise OR |\n+-------------------------------------------------+---------------------------------------+\n| ``^`` | Bitwise XOR |\n+-------------------------------------------------+---------------------------------------+\n| ``&`` | Bitwise AND |\n+-------------------------------------------------+---------------------------------------+\n| ``<<``, ``>>`` | Shifts |\n+-------------------------------------------------+---------------------------------------+\n| ``+``, ``-`` | Addition and subtraction |\n+-------------------------------------------------+---------------------------------------+\n| ``*``, ``/``, ``//``, ``%`` | Multiplication, division, remainder |\n| | [5] |\n+-------------------------------------------------+---------------------------------------+\n| ``+x``, ``-x``, ``~x`` | Positive, negative, bitwise NOT |\n+-------------------------------------------------+---------------------------------------+\n| ``**`` | Exponentiation [6] |\n+-------------------------------------------------+---------------------------------------+\n| ``x[index]``, ``x[index:index]``, | Subscription, slicing, call, |\n| ``x(arguments...)``, ``x.attribute`` | attribute reference |\n+-------------------------------------------------+---------------------------------------+\n| ``(expressions...)``, ``[expressions...]``, | Binding or tuple display, list |\n| ``{key:datum...}``, ``{expressions...}`` | display, dictionary display, set |\n| | display |\n+-------------------------------------------------+---------------------------------------+\n\n-[ Footnotes ]-\n\n[1] While ``abs(x%y) < abs(y)`` is true mathematically, for floats it\n may not be true numerically due to roundoff. For example, and\n assuming a platform on which a Python float is an IEEE 754 double-\n precision number, in order that ``-1e-100 % 1e100`` have the same\n sign as ``1e100``, the computed result is ``-1e-100 + 1e100``,\n which is numerically exactly equal to ``1e100``. The function\n ``math.fmod()`` returns a result whose sign matches the sign of\n the first argument instead, and so returns ``-1e-100`` in this\n case. Which approach is more appropriate depends on the\n application.\n\n[2] If x is very close to an exact integer multiple of y, it\'s\n possible for ``x//y`` to be one larger than ``(x-x%y)//y`` due to\n rounding. In such cases, Python returns the latter result, in\n order to preserve that ``divmod(x,y)[0] * y + x % y`` be very\n close to ``x``.\n\n[3] While comparisons between strings make sense at the byte level,\n they may be counter-intuitive to users. For example, the strings\n ``"\\u00C7"`` and ``"\\u0327\\u0043"`` compare differently, even\n though they both represent the same unicode character (LATIN\n CAPITAL LETTER C WITH CEDILLA). To compare strings in a human\n recognizable way, compare using ``unicodedata.normalize()``.\n\n[4] Due to automatic garbage-collection, free lists, and the dynamic\n nature of descriptors, you may notice seemingly unusual behaviour\n in certain uses of the ``is`` operator, like those involving\n comparisons between instance methods, or constants. Check their\n documentation for more info.\n\n[5] The ``%`` operator is also used for string formatting; the same\n precedence applies.\n\n[6] The power operator ``**`` binds less tightly than an arithmetic or\n bitwise unary operator on its right, that is, ``2**-1`` is\n ``0.5``.\n', 'pass': '\nThe ``pass`` statement\n**********************\n\n pass_stmt ::= "pass"\n\n``pass`` is a null operation --- when it is executed, nothing happens.\nIt is useful as a placeholder when a statement is required\nsyntactically, but no code needs to be executed, for example:\n\n def f(arg): pass # a function that does nothing (yet)\n\n class C: pass # a class with no methods (yet)\n', 'power': '\nThe power operator\n******************\n\nThe power operator binds more tightly than unary operators on its\nleft; it binds less tightly than unary operators on its right. The\nsyntax is:\n\n power ::= primary ["**" u_expr]\n\nThus, in an unparenthesized sequence of power and unary operators, the\noperators are evaluated from right to left (this does not constrain\nthe evaluation order for the operands): ``-1**2`` results in ``-1``.\n\nThe power operator has the same semantics as the built-in ``pow()``\nfunction, when called with two arguments: it yields its left argument\nraised to the power of its right argument. The numeric arguments are\nfirst converted to a common type, and the result is of that type.\n\nFor int operands, the result has the same type as the operands unless\nthe second argument is negative; in that case, all arguments are\nconverted to float and a float result is delivered. For example,\n``10**2`` returns ``100``, but ``10**-2`` returns ``0.01``.\n\nRaising ``0.0`` to a negative power results in a\n``ZeroDivisionError``. Raising a negative number to a fractional power\nresults in a ``complex`` number. (In earlier versions it raised a\n``ValueError``.)\n', @@ -60,18 +60,18 @@ 'shifting': '\nShifting operations\n*******************\n\nThe shifting operations have lower priority than the arithmetic\noperations:\n\n shift_expr ::= a_expr | shift_expr ( "<<" | ">>" ) a_expr\n\nThese operators accept integers as arguments. They shift the first\nargument to the left or right by the number of bits given by the\nsecond argument.\n\nA right shift by *n* bits is defined as division by ``pow(2,n)``. A\nleft shift by *n* bits is defined as multiplication with ``pow(2,n)``.\n\nNote: In the current implementation, the right-hand operand is required to\n be at most ``sys.maxsize``. If the right-hand operand is larger\n than ``sys.maxsize`` an ``OverflowError`` exception is raised.\n', 'slicings': '\nSlicings\n********\n\nA slicing selects a range of items in a sequence object (e.g., a\nstring, tuple or list). Slicings may be used as expressions or as\ntargets in assignment or ``del`` statements. The syntax for a\nslicing:\n\n slicing ::= primary "[" slice_list "]"\n slice_list ::= slice_item ("," slice_item)* [","]\n slice_item ::= expression | proper_slice\n proper_slice ::= [lower_bound] ":" [upper_bound] [ ":" [stride] ]\n lower_bound ::= expression\n upper_bound ::= expression\n stride ::= expression\n\nThere is ambiguity in the formal syntax here: anything that looks like\nan expression list also looks like a slice list, so any subscription\ncan be interpreted as a slicing. Rather than further complicating the\nsyntax, this is disambiguated by defining that in this case the\ninterpretation as a subscription takes priority over the\ninterpretation as a slicing (this is the case if the slice list\ncontains no proper slice).\n\nThe semantics for a slicing are as follows. The primary must evaluate\nto a mapping object, and it is indexed (using the same\n``__getitem__()`` method as normal subscription) with a key that is\nconstructed from the slice list, as follows. If the slice list\ncontains at least one comma, the key is a tuple containing the\nconversion of the slice items; otherwise, the conversion of the lone\nslice item is the key. The conversion of a slice item that is an\nexpression is that expression. The conversion of a proper slice is a\nslice object (see section *The standard type hierarchy*) whose\n``start``, ``stop`` and ``step`` attributes are the values of the\nexpressions given as lower bound, upper bound and stride,\nrespectively, substituting ``None`` for missing expressions.\n', 'specialattrs': '\nSpecial Attributes\n******************\n\nThe implementation adds a few special read-only attributes to several\nobject types, where they are relevant. Some of these are not reported\nby the ``dir()`` built-in function.\n\nobject.__dict__\n\n A dictionary or other mapping object used to store an object\'s\n (writable) attributes.\n\ninstance.__class__\n\n The class to which a class instance belongs.\n\nclass.__bases__\n\n The tuple of base classes of a class object.\n\nclass.__name__\n\n The name of the class or type.\n\nclass.__qualname__\n\n The *qualified name* of the class or type.\n\n New in version 3.3.\n\nclass.__mro__\n\n This attribute is a tuple of classes that are considered when\n looking for base classes during method resolution.\n\nclass.mro()\n\n This method can be overridden by a metaclass to customize the\n method resolution order for its instances. It is called at class\n instantiation, and its result is stored in ``__mro__``.\n\nclass.__subclasses__()\n\n Each class keeps a list of weak references to its immediate\n subclasses. This method returns a list of all those references\n still alive. Example:\n\n >>> int.__subclasses__()\n []\n\n-[ Footnotes ]-\n\n[1] Additional information on these special methods may be found in\n the Python Reference Manual (*Basic customization*).\n\n[2] As a consequence, the list ``[1, 2]`` is considered equal to\n ``[1.0, 2.0]``, and similarly for tuples.\n\n[3] They must have since the parser can\'t tell the type of the\n operands.\n\n[4] Cased characters are those with general category property being\n one of "Lu" (Letter, uppercase), "Ll" (Letter, lowercase), or "Lt"\n (Letter, titlecase).\n\n[5] To format only a tuple you should therefore provide a singleton\n tuple whose only element is the tuple to be formatted.\n', - 'specialnames': '\nSpecial method names\n********************\n\nA class can implement certain operations that are invoked by special\nsyntax (such as arithmetic operations or subscripting and slicing) by\ndefining methods with special names. This is Python\'s approach to\n*operator overloading*, allowing classes to define their own behavior\nwith respect to language operators. For instance, if a class defines\na method named ``__getitem__()``, and ``x`` is an instance of this\nclass, then ``x[i]`` is roughly equivalent to ``type(x).__getitem__(x,\ni)``. Except where mentioned, attempts to execute an operation raise\nan exception when no appropriate method is defined (typically\n``AttributeError`` or ``TypeError``).\n\nWhen implementing a class that emulates any built-in type, it is\nimportant that the emulation only be implemented to the degree that it\nmakes sense for the object being modelled. For example, some\nsequences may work well with retrieval of individual elements, but\nextracting a slice may not make sense. (One example of this is the\n``NodeList`` interface in the W3C\'s Document Object Model.)\n\n\nBasic customization\n===================\n\nobject.__new__(cls[, ...])\n\n Called to create a new instance of class *cls*. ``__new__()`` is a\n static method (special-cased so you need not declare it as such)\n that takes the class of which an instance was requested as its\n first argument. The remaining arguments are those passed to the\n object constructor expression (the call to the class). The return\n value of ``__new__()`` should be the new object instance (usually\n an instance of *cls*).\n\n Typical implementations create a new instance of the class by\n invoking the superclass\'s ``__new__()`` method using\n ``super(currentclass, cls).__new__(cls[, ...])`` with appropriate\n arguments and then modifying the newly-created instance as\n necessary before returning it.\n\n If ``__new__()`` returns an instance of *cls*, then the new\n instance\'s ``__init__()`` method will be invoked like\n ``__init__(self[, ...])``, where *self* is the new instance and the\n remaining arguments are the same as were passed to ``__new__()``.\n\n If ``__new__()`` does not return an instance of *cls*, then the new\n instance\'s ``__init__()`` method will not be invoked.\n\n ``__new__()`` is intended mainly to allow subclasses of immutable\n types (like int, str, or tuple) to customize instance creation. It\n is also commonly overridden in custom metaclasses in order to\n customize class creation.\n\nobject.__init__(self[, ...])\n\n Called when the instance is created. The arguments are those\n passed to the class constructor expression. If a base class has an\n ``__init__()`` method, the derived class\'s ``__init__()`` method,\n if any, must explicitly call it to ensure proper initialization of\n the base class part of the instance; for example:\n ``BaseClass.__init__(self, [args...])``. As a special constraint\n on constructors, no value may be returned; doing so will cause a\n ``TypeError`` to be raised at runtime.\n\nobject.__del__(self)\n\n Called when the instance is about to be destroyed. This is also\n called a destructor. If a base class has a ``__del__()`` method,\n the derived class\'s ``__del__()`` method, if any, must explicitly\n call it to ensure proper deletion of the base class part of the\n instance. Note that it is possible (though not recommended!) for\n the ``__del__()`` method to postpone destruction of the instance by\n creating a new reference to it. It may then be called at a later\n time when this new reference is deleted. It is not guaranteed that\n ``__del__()`` methods are called for objects that still exist when\n the interpreter exits.\n\n Note: ``del x`` doesn\'t directly call ``x.__del__()`` --- the former\n decrements the reference count for ``x`` by one, and the latter\n is only called when ``x``\'s reference count reaches zero. Some\n common situations that may prevent the reference count of an\n object from going to zero include: circular references between\n objects (e.g., a doubly-linked list or a tree data structure with\n parent and child pointers); a reference to the object on the\n stack frame of a function that caught an exception (the traceback\n stored in ``sys.exc_info()[2]`` keeps the stack frame alive); or\n a reference to the object on the stack frame that raised an\n unhandled exception in interactive mode (the traceback stored in\n ``sys.last_traceback`` keeps the stack frame alive). The first\n situation can only be remedied by explicitly breaking the cycles;\n the latter two situations can be resolved by storing ``None`` in\n ``sys.last_traceback``. Circular references which are garbage are\n detected when the option cycle detector is enabled (it\'s on by\n default), but can only be cleaned up if there are no Python-\n level ``__del__()`` methods involved. Refer to the documentation\n for the ``gc`` module for more information about how\n ``__del__()`` methods are handled by the cycle detector,\n particularly the description of the ``garbage`` value.\n\n Warning: Due to the precarious circumstances under which ``__del__()``\n methods are invoked, exceptions that occur during their execution\n are ignored, and a warning is printed to ``sys.stderr`` instead.\n Also, when ``__del__()`` is invoked in response to a module being\n deleted (e.g., when execution of the program is done), other\n globals referenced by the ``__del__()`` method may already have\n been deleted or in the process of being torn down (e.g. the\n import machinery shutting down). For this reason, ``__del__()``\n methods should do the absolute minimum needed to maintain\n external invariants. Starting with version 1.5, Python\n guarantees that globals whose name begins with a single\n underscore are deleted from their module before other globals are\n deleted; if no other references to such globals exist, this may\n help in assuring that imported modules are still available at the\n time when the ``__del__()`` method is called.\n\nobject.__repr__(self)\n\n Called by the ``repr()`` built-in function to compute the\n "official" string representation of an object. If at all possible,\n this should look like a valid Python expression that could be used\n to recreate an object with the same value (given an appropriate\n environment). If this is not possible, a string of the form\n ``<...some useful description...>`` should be returned. The return\n value must be a string object. If a class defines ``__repr__()``\n but not ``__str__()``, then ``__repr__()`` is also used when an\n "informal" string representation of instances of that class is\n required.\n\n This is typically used for debugging, so it is important that the\n representation is information-rich and unambiguous.\n\nobject.__str__(self)\n\n Called by the ``str()`` built-in function and by the ``print()``\n function to compute the "informal" string representation of an\n object. This differs from ``__repr__()`` in that it does not have\n to be a valid Python expression: a more convenient or concise\n representation may be used instead. The return value must be a\n string object.\n\nobject.__bytes__(self)\n\n Called by ``bytes()`` to compute a byte-string representation of an\n object. This should return a ``bytes`` object.\n\nobject.__format__(self, format_spec)\n\n Called by the ``format()`` built-in function (and by extension, the\n ``format()`` method of class ``str``) to produce a "formatted"\n string representation of an object. The ``format_spec`` argument is\n a string that contains a description of the formatting options\n desired. The interpretation of the ``format_spec`` argument is up\n to the type implementing ``__format__()``, however most classes\n will either delegate formatting to one of the built-in types, or\n use a similar formatting option syntax.\n\n See *Format Specification Mini-Language* for a description of the\n standard formatting syntax.\n\n The return value must be a string object.\n\nobject.__lt__(self, other)\nobject.__le__(self, other)\nobject.__eq__(self, other)\nobject.__ne__(self, other)\nobject.__gt__(self, other)\nobject.__ge__(self, other)\n\n These are the so-called "rich comparison" methods. The\n correspondence between operator symbols and method names is as\n follows: ``xy`` calls ``x.__gt__(y)``, and ``x>=y`` calls\n ``x.__ge__(y)``.\n\n A rich comparison method may return the singleton\n ``NotImplemented`` if it does not implement the operation for a\n given pair of arguments. By convention, ``False`` and ``True`` are\n returned for a successful comparison. However, these methods can\n return any value, so if the comparison operator is used in a\n Boolean context (e.g., in the condition of an ``if`` statement),\n Python will call ``bool()`` on the value to determine if the result\n is true or false.\n\n There are no implied relationships among the comparison operators.\n The truth of ``x==y`` does not imply that ``x!=y`` is false.\n Accordingly, when defining ``__eq__()``, one should also define\n ``__ne__()`` so that the operators will behave as expected. See\n the paragraph on ``__hash__()`` for some important notes on\n creating *hashable* objects which support custom comparison\n operations and are usable as dictionary keys.\n\n There are no swapped-argument versions of these methods (to be used\n when the left argument does not support the operation but the right\n argument does); rather, ``__lt__()`` and ``__gt__()`` are each\n other\'s reflection, ``__le__()`` and ``__ge__()`` are each other\'s\n reflection, and ``__eq__()`` and ``__ne__()`` are their own\n reflection.\n\n Arguments to rich comparison methods are never coerced.\n\n To automatically generate ordering operations from a single root\n operation, see ``functools.total_ordering()``.\n\nobject.__hash__(self)\n\n Called by built-in function ``hash()`` and for operations on\n members of hashed collections including ``set``, ``frozenset``, and\n ``dict``. ``__hash__()`` should return an integer. The only\n required property is that objects which compare equal have the same\n hash value; it is advised to somehow mix together (e.g. using\n exclusive or) the hash values for the components of the object that\n also play a part in comparison of objects.\n\n If a class does not define an ``__eq__()`` method it should not\n define a ``__hash__()`` operation either; if it defines\n ``__eq__()`` but not ``__hash__()``, its instances will not be\n usable as items in hashable collections. If a class defines\n mutable objects and implements an ``__eq__()`` method, it should\n not implement ``__hash__()``, since the implementation of hashable\n collections requires that a key\'s hash value is immutable (if the\n object\'s hash value changes, it will be in the wrong hash bucket).\n\n User-defined classes have ``__eq__()`` and ``__hash__()`` methods\n by default; with them, all objects compare unequal (except with\n themselves) and ``x.__hash__()`` returns an appropriate value such\n that ``x == y`` implies both that ``x is y`` and ``hash(x) ==\n hash(y)``.\n\n Classes which inherit a ``__hash__()`` method from a parent class\n but change the meaning of ``__eq__()`` such that the hash value\n returned is no longer appropriate (e.g. by switching to a value-\n based concept of equality instead of the default identity based\n equality) can explicitly flag themselves as being unhashable by\n setting ``__hash__ = None`` in the class definition. Doing so means\n that not only will instances of the class raise an appropriate\n ``TypeError`` when a program attempts to retrieve their hash value,\n but they will also be correctly identified as unhashable when\n checking ``isinstance(obj, collections.Hashable)`` (unlike classes\n which define their own ``__hash__()`` to explicitly raise\n ``TypeError``).\n\n If a class that overrides ``__eq__()`` needs to retain the\n implementation of ``__hash__()`` from a parent class, the\n interpreter must be told this explicitly by setting ``__hash__ =\n .__hash__``. Otherwise the inheritance of\n ``__hash__()`` will be blocked, just as if ``__hash__`` had been\n explicitly set to ``None``.\n\n Note: Note by default the ``__hash__()`` values of str, bytes and\n datetime objects are "salted" with an unpredictable random value.\n Although they remain constant within an individual Python\n process, they are not predictable between repeated invocations of\n Python.This is intended to provide protection against a denial-\n of-service caused by carefully-chosen inputs that exploit the\n worst case performance of a dict insertion, O(n^2) complexity.\n See http://www.ocert.org/advisories/ocert-2011-003.html for\n details.Changing hash values affects the order in which keys are\n retrieved from a dict. Note Python has never made guarantees\n about this ordering (and it typically varies between 32-bit and\n 64-bit builds).See also ``PYTHONHASHSEED``.\n\n Changed in version 3.3: Hash randomization is enabled by default.\n\nobject.__bool__(self)\n\n Called to implement truth value testing and the built-in operation\n ``bool()``; should return ``False`` or ``True``. When this method\n is not defined, ``__len__()`` is called, if it is defined, and the\n object is considered true if its result is nonzero. If a class\n defines neither ``__len__()`` nor ``__bool__()``, all its instances\n are considered true.\n\n\nCustomizing attribute access\n============================\n\nThe following methods can be defined to customize the meaning of\nattribute access (use of, assignment to, or deletion of ``x.name``)\nfor class instances.\n\nobject.__getattr__(self, name)\n\n Called when an attribute lookup has not found the attribute in the\n usual places (i.e. it is not an instance attribute nor is it found\n in the class tree for ``self``). ``name`` is the attribute name.\n This method should return the (computed) attribute value or raise\n an ``AttributeError`` exception.\n\n Note that if the attribute is found through the normal mechanism,\n ``__getattr__()`` is not called. (This is an intentional asymmetry\n between ``__getattr__()`` and ``__setattr__()``.) This is done both\n for efficiency reasons and because otherwise ``__getattr__()``\n would have no way to access other attributes of the instance. Note\n that at least for instance variables, you can fake total control by\n not inserting any values in the instance attribute dictionary (but\n instead inserting them in another object). See the\n ``__getattribute__()`` method below for a way to actually get total\n control over attribute access.\n\nobject.__getattribute__(self, name)\n\n Called unconditionally to implement attribute accesses for\n instances of the class. If the class also defines\n ``__getattr__()``, the latter will not be called unless\n ``__getattribute__()`` either calls it explicitly or raises an\n ``AttributeError``. This method should return the (computed)\n attribute value or raise an ``AttributeError`` exception. In order\n to avoid infinite recursion in this method, its implementation\n should always call the base class method with the same name to\n access any attributes it needs, for example,\n ``object.__getattribute__(self, name)``.\n\n Note: This method may still be bypassed when looking up special methods\n as the result of implicit invocation via language syntax or\n built-in functions. See *Special method lookup*.\n\nobject.__setattr__(self, name, value)\n\n Called when an attribute assignment is attempted. This is called\n instead of the normal mechanism (i.e. store the value in the\n instance dictionary). *name* is the attribute name, *value* is the\n value to be assigned to it.\n\n If ``__setattr__()`` wants to assign to an instance attribute, it\n should call the base class method with the same name, for example,\n ``object.__setattr__(self, name, value)``.\n\nobject.__delattr__(self, name)\n\n Like ``__setattr__()`` but for attribute deletion instead of\n assignment. This should only be implemented if ``del obj.name`` is\n meaningful for the object.\n\nobject.__dir__(self)\n\n Called when ``dir()`` is called on the object. A sequence must be\n returned. ``dir()`` converts the returned sequence to a list and\n sorts it.\n\n\nImplementing Descriptors\n------------------------\n\nThe following methods only apply when an instance of the class\ncontaining the method (a so-called *descriptor* class) appears in an\n*owner* class (the descriptor must be in either the owner\'s class\ndictionary or in the class dictionary for one of its parents). In the\nexamples below, "the attribute" refers to the attribute whose name is\nthe key of the property in the owner class\' ``__dict__``.\n\nobject.__get__(self, instance, owner)\n\n Called to get the attribute of the owner class (class attribute\n access) or of an instance of that class (instance attribute\n access). *owner* is always the owner class, while *instance* is the\n instance that the attribute was accessed through, or ``None`` when\n the attribute is accessed through the *owner*. This method should\n return the (computed) attribute value or raise an\n ``AttributeError`` exception.\n\nobject.__set__(self, instance, value)\n\n Called to set the attribute on an instance *instance* of the owner\n class to a new value, *value*.\n\nobject.__delete__(self, instance)\n\n Called to delete the attribute on an instance *instance* of the\n owner class.\n\n\nInvoking Descriptors\n--------------------\n\nIn general, a descriptor is an object attribute with "binding\nbehavior", one whose attribute access has been overridden by methods\nin the descriptor protocol: ``__get__()``, ``__set__()``, and\n``__delete__()``. If any of those methods are defined for an object,\nit is said to be a descriptor.\n\nThe default behavior for attribute access is to get, set, or delete\nthe attribute from an object\'s dictionary. For instance, ``a.x`` has a\nlookup chain starting with ``a.__dict__[\'x\']``, then\n``type(a).__dict__[\'x\']``, and continuing through the base classes of\n``type(a)`` excluding metaclasses.\n\nHowever, if the looked-up value is an object defining one of the\ndescriptor methods, then Python may override the default behavior and\ninvoke the descriptor method instead. Where this occurs in the\nprecedence chain depends on which descriptor methods were defined and\nhow they were called.\n\nThe starting point for descriptor invocation is a binding, ``a.x``.\nHow the arguments are assembled depends on ``a``:\n\nDirect Call\n The simplest and least common call is when user code directly\n invokes a descriptor method: ``x.__get__(a)``.\n\nInstance Binding\n If binding to an object instance, ``a.x`` is transformed into the\n call: ``type(a).__dict__[\'x\'].__get__(a, type(a))``.\n\nClass Binding\n If binding to a class, ``A.x`` is transformed into the call:\n ``A.__dict__[\'x\'].__get__(None, A)``.\n\nSuper Binding\n If ``a`` is an instance of ``super``, then the binding ``super(B,\n obj).m()`` searches ``obj.__class__.__mro__`` for the base class\n ``A`` immediately preceding ``B`` and then invokes the descriptor\n with the call: ``A.__dict__[\'m\'].__get__(obj, obj.__class__)``.\n\nFor instance bindings, the precedence of descriptor invocation depends\non the which descriptor methods are defined. A descriptor can define\nany combination of ``__get__()``, ``__set__()`` and ``__delete__()``.\nIf it does not define ``__get__()``, then accessing the attribute will\nreturn the descriptor object itself unless there is a value in the\nobject\'s instance dictionary. If the descriptor defines ``__set__()``\nand/or ``__delete__()``, it is a data descriptor; if it defines\nneither, it is a non-data descriptor. Normally, data descriptors\ndefine both ``__get__()`` and ``__set__()``, while non-data\ndescriptors have just the ``__get__()`` method. Data descriptors with\n``__set__()`` and ``__get__()`` defined always override a redefinition\nin an instance dictionary. In contrast, non-data descriptors can be\noverridden by instances.\n\nPython methods (including ``staticmethod()`` and ``classmethod()``)\nare implemented as non-data descriptors. Accordingly, instances can\nredefine and override methods. This allows individual instances to\nacquire behaviors that differ from other instances of the same class.\n\nThe ``property()`` function is implemented as a data descriptor.\nAccordingly, instances cannot override the behavior of a property.\n\n\n__slots__\n---------\n\nBy default, instances of classes have a dictionary for attribute\nstorage. This wastes space for objects having very few instance\nvariables. The space consumption can become acute when creating large\nnumbers of instances.\n\nThe default can be overridden by defining *__slots__* in a class\ndefinition. The *__slots__* declaration takes a sequence of instance\nvariables and reserves just enough space in each instance to hold a\nvalue for each variable. Space is saved because *__dict__* is not\ncreated for each instance.\n\nobject.__slots__\n\n This class variable can be assigned a string, iterable, or sequence\n of strings with variable names used by instances. If defined in a\n class, *__slots__* reserves space for the declared variables and\n prevents the automatic creation of *__dict__* and *__weakref__* for\n each instance.\n\n\nNotes on using *__slots__*\n~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n* When inheriting from a class without *__slots__*, the *__dict__*\n attribute of that class will always be accessible, so a *__slots__*\n definition in the subclass is meaningless.\n\n* Without a *__dict__* variable, instances cannot be assigned new\n variables not listed in the *__slots__* definition. Attempts to\n assign to an unlisted variable name raises ``AttributeError``. If\n dynamic assignment of new variables is desired, then add\n ``\'__dict__\'`` to the sequence of strings in the *__slots__*\n declaration.\n\n* Without a *__weakref__* variable for each instance, classes defining\n *__slots__* do not support weak references to its instances. If weak\n reference support is needed, then add ``\'__weakref__\'`` to the\n sequence of strings in the *__slots__* declaration.\n\n* *__slots__* are implemented at the class level by creating\n descriptors (*Implementing Descriptors*) for each variable name. As\n a result, class attributes cannot be used to set default values for\n instance variables defined by *__slots__*; otherwise, the class\n attribute would overwrite the descriptor assignment.\n\n* The action of a *__slots__* declaration is limited to the class\n where it is defined. As a result, subclasses will have a *__dict__*\n unless they also define *__slots__* (which must only contain names\n of any *additional* slots).\n\n* If a class defines a slot also defined in a base class, the instance\n variable defined by the base class slot is inaccessible (except by\n retrieving its descriptor directly from the base class). This\n renders the meaning of the program undefined. In the future, a\n check may be added to prevent this.\n\n* Nonempty *__slots__* does not work for classes derived from\n "variable-length" built-in types such as ``int``, ``str`` and\n ``tuple``.\n\n* Any non-string iterable may be assigned to *__slots__*. Mappings may\n also be used; however, in the future, special meaning may be\n assigned to the values corresponding to each key.\n\n* *__class__* assignment works only if both classes have the same\n *__slots__*.\n\n\nCustomizing class creation\n==========================\n\nBy default, classes are constructed using ``type()``. The class body\nis executed in a new namespace and the class name is bound locally to\nthe result of ``type(name, bases, namespace)``.\n\nThe class creation process can be customised by passing the\n``metaclass`` keyword argument in the class definition line, or by\ninheriting from an existing class that included such an argument. In\nthe following example, both ``MyClass`` and ``MySubclass`` are\ninstances of ``Meta``:\n\n class Meta(type):\n pass\n\n class MyClass(metaclass=Meta):\n pass\n\n class MySubclass(MyClass):\n pass\n\nAny other keyword arguments that are specified in the class definition\nare passed through to all metaclass operations described below.\n\nWhen a class definition is executed, the following steps occur:\n\n* the appropriate metaclass is determined\n\n* the class namespace is prepared\n\n* the class body is executed\n\n* the class object is created\n\n\nDetermining the appropriate metaclass\n-------------------------------------\n\nThe appropriate metaclass for a class definition is determined as\nfollows:\n\n* if no bases and no explicit metaclass are given, then ``type()`` is\n used\n\n* if an explicit metaclass is given and it is *not* an instance of\n ``type()``, then it is used directly as the metaclass\n\n* if an instance of ``type()`` is given as the explicit metaclass, or\n bases are defined, then the most derived metaclass is used\n\nThe most derived metaclass is selected from the explicitly specified\nmetaclass (if any) and the metaclasses (i.e. ``type(cls)``) of all\nspecified base classes. The most derived metaclass is one which is a\nsubtype of *all* of these candidate metaclasses. If none of the\ncandidate metaclasses meets that criterion, then the class definition\nwill fail with ``TypeError``.\n\n\nPreparing the class namespace\n-----------------------------\n\nOnce the appropriate metaclass has been identified, then the class\nnamespace is prepared. If the metaclass has a ``__prepare__``\nattribute, it is called as ``namespace = metaclass.__prepare__(name,\nbases, **kwds)`` (where the additional keyword arguments, if any, come\nfrom the class definition).\n\nIf the metaclass has no ``__prepare__`` attribute, then the class\nnamespace is initialised as an empty ``dict()`` instance.\n\nSee also:\n\n **PEP 3115** - Metaclasses in Python 3000\n Introduced the ``__prepare__`` namespace hook\n\n\nExecuting the class body\n------------------------\n\nThe class body is executed (approximately) as ``exec(body, globals(),\nnamespace)``. The key difference from a normal call to ``exec()`` is\nthat lexical scoping allows the class body (including any methods) to\nreference names from the current and outer scopes when the class\ndefinition occurs inside a function.\n\nHowever, even when the class definition occurs inside the function,\nmethods defined inside the class still cannot see names defined at the\nclass scope. Class variables must be accessed through the first\nparameter of instance or class methods, and cannot be accessed at all\nfrom static methods.\n\n\nCreating the class object\n-------------------------\n\nOnce the class namespace has been populated by executing the class\nbody, the class object is created by calling ``metaclass(name, bases,\nnamespace, **kwds)`` (the additional keywords passed here are the same\nas those passed to ``__prepare__``).\n\nThis class object is the one that will be referenced by the zero-\nargument form of ``super()``. ``__class__`` is an implicit closure\nreference created by the compiler if any methods in a class body refer\nto either ``__class__`` or ``super``. This allows the zero argument\nform of ``super()`` to correctly identify the class being defined\nbased on lexical scoping, while the class or instance that was used to\nmake the current call is identified based on the first argument passed\nto the method.\n\nAfter the class object is created, it is passed to the class\ndecorators included in the class definition (if any) and the resulting\nobject is bound in the local namespace as the defined class.\n\nSee also:\n\n **PEP 3135** - New super\n Describes the implicit ``__class__`` closure reference\n\n\nMetaclass example\n-----------------\n\nThe potential uses for metaclasses are boundless. Some ideas that have\nbeen explored include logging, interface checking, automatic\ndelegation, automatic property creation, proxies, frameworks, and\nautomatic resource locking/synchronization.\n\nHere is an example of a metaclass that uses an\n``collections.OrderedDict`` to remember the order that class members\nwere defined:\n\n class OrderedClass(type):\n\n @classmethod\n def __prepare__(metacls, name, bases, **kwds):\n return collections.OrderedDict()\n\n def __new__(cls, name, bases, namespace, **kwds):\n result = type.__new__(cls, name, bases, dict(namespace))\n result.members = tuple(namespace)\n return result\n\n class A(metaclass=OrderedClass):\n def one(self): pass\n def two(self): pass\n def three(self): pass\n def four(self): pass\n\n >>> A.members\n (\'__module__\', \'one\', \'two\', \'three\', \'four\')\n\nWhen the class definition for *A* gets executed, the process begins\nwith calling the metaclass\'s ``__prepare__()`` method which returns an\nempty ``collections.OrderedDict``. That mapping records the methods\nand attributes of *A* as they are defined within the body of the class\nstatement. Once those definitions are executed, the ordered dictionary\nis fully populated and the metaclass\'s ``__new__()`` method gets\ninvoked. That method builds the new type and it saves the ordered\ndictionary keys in an attribute called ``members``.\n\n\nCustomizing instance and subclass checks\n========================================\n\nThe following methods are used to override the default behavior of the\n``isinstance()`` and ``issubclass()`` built-in functions.\n\nIn particular, the metaclass ``abc.ABCMeta`` implements these methods\nin order to allow the addition of Abstract Base Classes (ABCs) as\n"virtual base classes" to any class or type (including built-in\ntypes), including other ABCs.\n\nclass.__instancecheck__(self, instance)\n\n Return true if *instance* should be considered a (direct or\n indirect) instance of *class*. If defined, called to implement\n ``isinstance(instance, class)``.\n\nclass.__subclasscheck__(self, subclass)\n\n Return true if *subclass* should be considered a (direct or\n indirect) subclass of *class*. If defined, called to implement\n ``issubclass(subclass, class)``.\n\nNote that these methods are looked up on the type (metaclass) of a\nclass. They cannot be defined as class methods in the actual class.\nThis is consistent with the lookup of special methods that are called\non instances, only in this case the instance is itself a class.\n\nSee also:\n\n **PEP 3119** - Introducing Abstract Base Classes\n Includes the specification for customizing ``isinstance()`` and\n ``issubclass()`` behavior through ``__instancecheck__()`` and\n ``__subclasscheck__()``, with motivation for this functionality\n in the context of adding Abstract Base Classes (see the ``abc``\n module) to the language.\n\n\nEmulating callable objects\n==========================\n\nobject.__call__(self[, args...])\n\n Called when the instance is "called" as a function; if this method\n is defined, ``x(arg1, arg2, ...)`` is a shorthand for\n ``x.__call__(arg1, arg2, ...)``.\n\n\nEmulating container types\n=========================\n\nThe following methods can be defined to implement container objects.\nContainers usually are sequences (such as lists or tuples) or mappings\n(like dictionaries), but can represent other containers as well. The\nfirst set of methods is used either to emulate a sequence or to\nemulate a mapping; the difference is that for a sequence, the\nallowable keys should be the integers *k* for which ``0 <= k < N``\nwhere *N* is the length of the sequence, or slice objects, which\ndefine a range of items. It is also recommended that mappings provide\nthe methods ``keys()``, ``values()``, ``items()``, ``get()``,\n``clear()``, ``setdefault()``, ``pop()``, ``popitem()``, ``copy()``,\nand ``update()`` behaving similar to those for Python\'s standard\ndictionary objects. The ``collections`` module provides a\n``MutableMapping`` abstract base class to help create those methods\nfrom a base set of ``__getitem__()``, ``__setitem__()``,\n``__delitem__()``, and ``keys()``. Mutable sequences should provide\nmethods ``append()``, ``count()``, ``index()``, ``extend()``,\n``insert()``, ``pop()``, ``remove()``, ``reverse()`` and ``sort()``,\nlike Python standard list objects. Finally, sequence types should\nimplement addition (meaning concatenation) and multiplication (meaning\nrepetition) by defining the methods ``__add__()``, ``__radd__()``,\n``__iadd__()``, ``__mul__()``, ``__rmul__()`` and ``__imul__()``\ndescribed below; they should not define other numerical operators. It\nis recommended that both mappings and sequences implement the\n``__contains__()`` method to allow efficient use of the ``in``\noperator; for mappings, ``in`` should search the mapping\'s keys; for\nsequences, it should search through the values. It is further\nrecommended that both mappings and sequences implement the\n``__iter__()`` method to allow efficient iteration through the\ncontainer; for mappings, ``__iter__()`` should be the same as\n``keys()``; for sequences, it should iterate through the values.\n\nobject.__len__(self)\n\n Called to implement the built-in function ``len()``. Should return\n the length of the object, an integer ``>=`` 0. Also, an object\n that doesn\'t define a ``__bool__()`` method and whose ``__len__()``\n method returns zero is considered to be false in a Boolean context.\n\nNote: Slicing is done exclusively with the following three methods. A\n call like\n\n a[1:2] = b\n\n is translated to\n\n a[slice(1, 2, None)] = b\n\n and so forth. Missing slice items are always filled in with\n ``None``.\n\nobject.__getitem__(self, key)\n\n Called to implement evaluation of ``self[key]``. For sequence\n types, the accepted keys should be integers and slice objects.\n Note that the special interpretation of negative indexes (if the\n class wishes to emulate a sequence type) is up to the\n ``__getitem__()`` method. If *key* is of an inappropriate type,\n ``TypeError`` may be raised; if of a value outside the set of\n indexes for the sequence (after any special interpretation of\n negative values), ``IndexError`` should be raised. For mapping\n types, if *key* is missing (not in the container), ``KeyError``\n should be raised.\n\n Note: ``for`` loops expect that an ``IndexError`` will be raised for\n illegal indexes to allow proper detection of the end of the\n sequence.\n\nobject.__setitem__(self, key, value)\n\n Called to implement assignment to ``self[key]``. Same note as for\n ``__getitem__()``. This should only be implemented for mappings if\n the objects support changes to the values for keys, or if new keys\n can be added, or for sequences if elements can be replaced. The\n same exceptions should be raised for improper *key* values as for\n the ``__getitem__()`` method.\n\nobject.__delitem__(self, key)\n\n Called to implement deletion of ``self[key]``. Same note as for\n ``__getitem__()``. This should only be implemented for mappings if\n the objects support removal of keys, or for sequences if elements\n can be removed from the sequence. The same exceptions should be\n raised for improper *key* values as for the ``__getitem__()``\n method.\n\nobject.__iter__(self)\n\n This method is called when an iterator is required for a container.\n This method should return a new iterator object that can iterate\n over all the objects in the container. For mappings, it should\n iterate over the keys of the container, and should also be made\n available as the method ``keys()``.\n\n Iterator objects also need to implement this method; they are\n required to return themselves. For more information on iterator\n objects, see *Iterator Types*.\n\nobject.__reversed__(self)\n\n Called (if present) by the ``reversed()`` built-in to implement\n reverse iteration. It should return a new iterator object that\n iterates over all the objects in the container in reverse order.\n\n If the ``__reversed__()`` method is not provided, the\n ``reversed()`` built-in will fall back to using the sequence\n protocol (``__len__()`` and ``__getitem__()``). Objects that\n support the sequence protocol should only provide\n ``__reversed__()`` if they can provide an implementation that is\n more efficient than the one provided by ``reversed()``.\n\nThe membership test operators (``in`` and ``not in``) are normally\nimplemented as an iteration through a sequence. However, container\nobjects can supply the following special method with a more efficient\nimplementation, which also does not require the object be a sequence.\n\nobject.__contains__(self, item)\n\n Called to implement membership test operators. Should return true\n if *item* is in *self*, false otherwise. For mapping objects, this\n should consider the keys of the mapping rather than the values or\n the key-item pairs.\n\n For objects that don\'t define ``__contains__()``, the membership\n test first tries iteration via ``__iter__()``, then the old\n sequence iteration protocol via ``__getitem__()``, see *this\n section in the language reference*.\n\n\nEmulating numeric types\n=======================\n\nThe following methods can be defined to emulate numeric objects.\nMethods corresponding to operations that are not supported by the\nparticular kind of number implemented (e.g., bitwise operations for\nnon-integral numbers) should be left undefined.\n\nobject.__add__(self, other)\nobject.__sub__(self, other)\nobject.__mul__(self, other)\nobject.__truediv__(self, other)\nobject.__floordiv__(self, other)\nobject.__mod__(self, other)\nobject.__divmod__(self, other)\nobject.__pow__(self, other[, modulo])\nobject.__lshift__(self, other)\nobject.__rshift__(self, other)\nobject.__and__(self, other)\nobject.__xor__(self, other)\nobject.__or__(self, other)\n\n These methods are called to implement the binary arithmetic\n operations (``+``, ``-``, ``*``, ``/``, ``//``, ``%``,\n ``divmod()``, ``pow()``, ``**``, ``<<``, ``>>``, ``&``, ``^``,\n ``|``). For instance, to evaluate the expression ``x + y``, where\n *x* is an instance of a class that has an ``__add__()`` method,\n ``x.__add__(y)`` is called. The ``__divmod__()`` method should be\n the equivalent to using ``__floordiv__()`` and ``__mod__()``; it\n should not be related to ``__truediv__()``. Note that\n ``__pow__()`` should be defined to accept an optional third\n argument if the ternary version of the built-in ``pow()`` function\n is to be supported.\n\n If one of those methods does not support the operation with the\n supplied arguments, it should return ``NotImplemented``.\n\nobject.__radd__(self, other)\nobject.__rsub__(self, other)\nobject.__rmul__(self, other)\nobject.__rtruediv__(self, other)\nobject.__rfloordiv__(self, other)\nobject.__rmod__(self, other)\nobject.__rdivmod__(self, other)\nobject.__rpow__(self, other)\nobject.__rlshift__(self, other)\nobject.__rrshift__(self, other)\nobject.__rand__(self, other)\nobject.__rxor__(self, other)\nobject.__ror__(self, other)\n\n These methods are called to implement the binary arithmetic\n operations (``+``, ``-``, ``*``, ``/``, ``//``, ``%``,\n ``divmod()``, ``pow()``, ``**``, ``<<``, ``>>``, ``&``, ``^``,\n ``|``) with reflected (swapped) operands. These functions are only\n called if the left operand does not support the corresponding\n operation and the operands are of different types. [2] For\n instance, to evaluate the expression ``x - y``, where *y* is an\n instance of a class that has an ``__rsub__()`` method,\n ``y.__rsub__(x)`` is called if ``x.__sub__(y)`` returns\n *NotImplemented*.\n\n Note that ternary ``pow()`` will not try calling ``__rpow__()``\n (the coercion rules would become too complicated).\n\n Note: If the right operand\'s type is a subclass of the left operand\'s\n type and that subclass provides the reflected method for the\n operation, this method will be called before the left operand\'s\n non-reflected method. This behavior allows subclasses to\n override their ancestors\' operations.\n\nobject.__iadd__(self, other)\nobject.__isub__(self, other)\nobject.__imul__(self, other)\nobject.__itruediv__(self, other)\nobject.__ifloordiv__(self, other)\nobject.__imod__(self, other)\nobject.__ipow__(self, other[, modulo])\nobject.__ilshift__(self, other)\nobject.__irshift__(self, other)\nobject.__iand__(self, other)\nobject.__ixor__(self, other)\nobject.__ior__(self, other)\n\n These methods are called to implement the augmented arithmetic\n assignments (``+=``, ``-=``, ``*=``, ``/=``, ``//=``, ``%=``,\n ``**=``, ``<<=``, ``>>=``, ``&=``, ``^=``, ``|=``). These methods\n should attempt to do the operation in-place (modifying *self*) and\n return the result (which could be, but does not have to be,\n *self*). If a specific method is not defined, the augmented\n assignment falls back to the normal methods. For instance, to\n execute the statement ``x += y``, where *x* is an instance of a\n class that has an ``__iadd__()`` method, ``x.__iadd__(y)`` is\n called. If *x* is an instance of a class that does not define a\n ``__iadd__()`` method, ``x.__add__(y)`` and ``y.__radd__(x)`` are\n considered, as with the evaluation of ``x + y``.\n\nobject.__neg__(self)\nobject.__pos__(self)\nobject.__abs__(self)\nobject.__invert__(self)\n\n Called to implement the unary arithmetic operations (``-``, ``+``,\n ``abs()`` and ``~``).\n\nobject.__complex__(self)\nobject.__int__(self)\nobject.__float__(self)\nobject.__round__(self[, n])\n\n Called to implement the built-in functions ``complex()``,\n ``int()``, ``float()`` and ``round()``. Should return a value of\n the appropriate type.\n\nobject.__index__(self)\n\n Called to implement ``operator.index()``. Also called whenever\n Python needs an integer object (such as in slicing, or in the\n built-in ``bin()``, ``hex()`` and ``oct()`` functions). Must return\n an integer.\n\n\nWith Statement Context Managers\n===============================\n\nA *context manager* is an object that defines the runtime context to\nbe established when executing a ``with`` statement. The context\nmanager handles the entry into, and the exit from, the desired runtime\ncontext for the execution of the block of code. Context managers are\nnormally invoked using the ``with`` statement (described in section\n*The with statement*), but can also be used by directly invoking their\nmethods.\n\nTypical uses of context managers include saving and restoring various\nkinds of global state, locking and unlocking resources, closing opened\nfiles, etc.\n\nFor more information on context managers, see *Context Manager Types*.\n\nobject.__enter__(self)\n\n Enter the runtime context related to this object. The ``with``\n statement will bind this method\'s return value to the target(s)\n specified in the ``as`` clause of the statement, if any.\n\nobject.__exit__(self, exc_type, exc_value, traceback)\n\n Exit the runtime context related to this object. The parameters\n describe the exception that caused the context to be exited. If the\n context was exited without an exception, all three arguments will\n be ``None``.\n\n If an exception is supplied, and the method wishes to suppress the\n exception (i.e., prevent it from being propagated), it should\n return a true value. Otherwise, the exception will be processed\n normally upon exit from this method.\n\n Note that ``__exit__()`` methods should not reraise the passed-in\n exception; this is the caller\'s responsibility.\n\nSee also:\n\n **PEP 0343** - The "with" statement\n The specification, background, and examples for the Python\n ``with`` statement.\n\n\nSpecial method lookup\n=====================\n\nFor custom classes, implicit invocations of special methods are only\nguaranteed to work correctly if defined on an object\'s type, not in\nthe object\'s instance dictionary. That behaviour is the reason why\nthe following code raises an exception:\n\n >>> class C:\n ... pass\n ...\n >>> c = C()\n >>> c.__len__ = lambda: 5\n >>> len(c)\n Traceback (most recent call last):\n File "", line 1, in \n TypeError: object of type \'C\' has no len()\n\nThe rationale behind this behaviour lies with a number of special\nmethods such as ``__hash__()`` and ``__repr__()`` that are implemented\nby all objects, including type objects. If the implicit lookup of\nthese methods used the conventional lookup process, they would fail\nwhen invoked on the type object itself:\n\n >>> 1 .__hash__() == hash(1)\n True\n >>> int.__hash__() == hash(int)\n Traceback (most recent call last):\n File "", line 1, in \n TypeError: descriptor \'__hash__\' of \'int\' object needs an argument\n\nIncorrectly attempting to invoke an unbound method of a class in this\nway is sometimes referred to as \'metaclass confusion\', and is avoided\nby bypassing the instance when looking up special methods:\n\n >>> type(1).__hash__(1) == hash(1)\n True\n >>> type(int).__hash__(int) == hash(int)\n True\n\nIn addition to bypassing any instance attributes in the interest of\ncorrectness, implicit special method lookup generally also bypasses\nthe ``__getattribute__()`` method even of the object\'s metaclass:\n\n >>> class Meta(type):\n ... def __getattribute__(*args):\n ... print("Metaclass getattribute invoked")\n ... return type.__getattribute__(*args)\n ...\n >>> class C(object, metaclass=Meta):\n ... def __len__(self):\n ... return 10\n ... def __getattribute__(*args):\n ... print("Class getattribute invoked")\n ... return object.__getattribute__(*args)\n ...\n >>> c = C()\n >>> c.__len__() # Explicit lookup via instance\n Class getattribute invoked\n 10\n >>> type(c).__len__(c) # Explicit lookup via type\n Metaclass getattribute invoked\n 10\n >>> len(c) # Implicit lookup\n 10\n\nBypassing the ``__getattribute__()`` machinery in this fashion\nprovides significant scope for speed optimisations within the\ninterpreter, at the cost of some flexibility in the handling of\nspecial methods (the special method *must* be set on the class object\nitself in order to be consistently invoked by the interpreter).\n\n-[ Footnotes ]-\n\n[1] It *is* possible in some cases to change an object\'s type, under\n certain controlled conditions. It generally isn\'t a good idea\n though, since it can lead to some very strange behaviour if it is\n handled incorrectly.\n\n[2] For operands of the same type, it is assumed that if the non-\n reflected method (such as ``__add__()``) fails the operation is\n not supported, which is why the reflected method is not called.\n', - 'string-methods': '\nString Methods\n**************\n\nString objects support the methods listed below.\n\nIn addition, Python\'s strings support the sequence type methods\ndescribed in the *Sequence Types --- str, bytes, bytearray, list,\ntuple, range* section. To output formatted strings, see the *String\nFormatting* section. Also, see the ``re`` module for string functions\nbased on regular expressions.\n\nstr.capitalize()\n\n Return a copy of the string with its first character capitalized\n and the rest lowercased.\n\nstr.casefold()\n\n Return a casefolded copy of the string. Casefolded strings may be\n used for caseless matching.\n\n Casefolding is similar to lowercasing but more aggressive because\n it is intended to remove all case distinctions in a string. For\n example, the German lowercase letter ``\'\xc3\x9f\'`` is equivalent to\n ``"ss"``. Since it is already lowercase, ``lower()`` would do\n nothing to ``\'\xc3\x9f\'``; ``casefold()`` converts it to ``"ss"``.\n\n The casefolding algorithm is described in section 3.13 of the\n Unicode Standard.\n\n New in version 3.3.\n\nstr.center(width[, fillchar])\n\n Return centered in a string of length *width*. Padding is done\n using the specified *fillchar* (default is a space).\n\nstr.count(sub[, start[, end]])\n\n Return the number of non-overlapping occurrences of substring *sub*\n in the range [*start*, *end*]. Optional arguments *start* and\n *end* are interpreted as in slice notation.\n\nstr.encode(encoding="utf-8", errors="strict")\n\n Return an encoded version of the string as a bytes object. Default\n encoding is ``\'utf-8\'``. *errors* may be given to set a different\n error handling scheme. The default for *errors* is ``\'strict\'``,\n meaning that encoding errors raise a ``UnicodeError``. Other\n possible values are ``\'ignore\'``, ``\'replace\'``,\n ``\'xmlcharrefreplace\'``, ``\'backslashreplace\'`` and any other name\n registered via ``codecs.register_error()``, see section *Codec Base\n Classes*. For a list of possible encodings, see section *Standard\n Encodings*.\n\n Changed in version 3.1: Support for keyword arguments added.\n\nstr.endswith(suffix[, start[, end]])\n\n Return ``True`` if the string ends with the specified *suffix*,\n otherwise return ``False``. *suffix* can also be a tuple of\n suffixes to look for. With optional *start*, test beginning at\n that position. With optional *end*, stop comparing at that\n position.\n\nstr.expandtabs([tabsize])\n\n Return a copy of the string where all tab characters are replaced\n by zero or more spaces, depending on the current column and the\n given tab size. The column number is reset to zero after each\n newline occurring in the string. If *tabsize* is not given, a tab\n size of ``8`` characters is assumed. This doesn\'t understand other\n non-printing characters or escape sequences.\n\nstr.find(sub[, start[, end]])\n\n Return the lowest index in the string where substring *sub* is\n found, such that *sub* is contained in the slice ``s[start:end]``.\n Optional arguments *start* and *end* are interpreted as in slice\n notation. Return ``-1`` if *sub* is not found.\n\n Note: The ``find()`` method should be used only if you need to know the\n position of *sub*. To check if *sub* is a substring or not, use\n the ``in`` operator:\n\n >>> \'Py\' in \'Python\'\n True\n\nstr.format(*args, **kwargs)\n\n Perform a string formatting operation. The string on which this\n method is called can contain literal text or replacement fields\n delimited by braces ``{}``. Each replacement field contains either\n the numeric index of a positional argument, or the name of a\n keyword argument. Returns a copy of the string where each\n replacement field is replaced with the string value of the\n corresponding argument.\n\n >>> "The sum of 1 + 2 is {0}".format(1+2)\n \'The sum of 1 + 2 is 3\'\n\n See *Format String Syntax* for a description of the various\n formatting options that can be specified in format strings.\n\nstr.format_map(mapping)\n\n Similar to ``str.format(**mapping)``, except that ``mapping`` is\n used directly and not copied to a ``dict`` . This is useful if for\n example ``mapping`` is a dict subclass:\n\n >>> class Default(dict):\n ... def __missing__(self, key):\n ... return key\n ...\n >>> \'{name} was born in {country}\'.format_map(Default(name=\'Guido\'))\n \'Guido was born in country\'\n\n New in version 3.2.\n\nstr.index(sub[, start[, end]])\n\n Like ``find()``, but raise ``ValueError`` when the substring is not\n found.\n\nstr.isalnum()\n\n Return true if all characters in the string are alphanumeric and\n there is at least one character, false otherwise. A character\n ``c`` is alphanumeric if one of the following returns ``True``:\n ``c.isalpha()``, ``c.isdecimal()``, ``c.isdigit()``, or\n ``c.isnumeric()``.\n\nstr.isalpha()\n\n Return true if all characters in the string are alphabetic and\n there is at least one character, false otherwise. Alphabetic\n characters are those characters defined in the Unicode character\n database as "Letter", i.e., those with general category property\n being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note that this is\n different from the "Alphabetic" property defined in the Unicode\n Standard.\n\nstr.isdecimal()\n\n Return true if all characters in the string are decimal characters\n and there is at least one character, false otherwise. Decimal\n characters are those from general category "Nd". This category\n includes digit characters, and all characters that can be used to\n form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.\n\nstr.isdigit()\n\n Return true if all characters in the string are digits and there is\n at least one character, false otherwise. Digits include decimal\n characters and digits that need special handling, such as the\n compatibility superscript digits. Formally, a digit is a character\n that has the property value Numeric_Type=Digit or\n Numeric_Type=Decimal.\n\nstr.isidentifier()\n\n Return true if the string is a valid identifier according to the\n language definition, section *Identifiers and keywords*.\n\nstr.islower()\n\n Return true if all cased characters [4] in the string are lowercase\n and there is at least one cased character, false otherwise.\n\nstr.isnumeric()\n\n Return true if all characters in the string are numeric characters,\n and there is at least one character, false otherwise. Numeric\n characters include digit characters, and all characters that have\n the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION\n ONE FIFTH. Formally, numeric characters are those with the\n property value Numeric_Type=Digit, Numeric_Type=Decimal or\n Numeric_Type=Numeric.\n\nstr.isprintable()\n\n Return true if all characters in the string are printable or the\n string is empty, false otherwise. Nonprintable characters are\n those characters defined in the Unicode character database as\n "Other" or "Separator", excepting the ASCII space (0x20) which is\n considered printable. (Note that printable characters in this\n context are those which should not be escaped when ``repr()`` is\n invoked on a string. It has no bearing on the handling of strings\n written to ``sys.stdout`` or ``sys.stderr``.)\n\nstr.isspace()\n\n Return true if there are only whitespace characters in the string\n and there is at least one character, false otherwise. Whitespace\n characters are those characters defined in the Unicode character\n database as "Other" or "Separator" and those with bidirectional\n property being one of "WS", "B", or "S".\n\nstr.istitle()\n\n Return true if the string is a titlecased string and there is at\n least one character, for example uppercase characters may only\n follow uncased characters and lowercase characters only cased ones.\n Return false otherwise.\n\nstr.isupper()\n\n Return true if all cased characters [4] in the string are uppercase\n and there is at least one cased character, false otherwise.\n\nstr.join(iterable)\n\n Return a string which is the concatenation of the strings in the\n *iterable* *iterable*. A ``TypeError`` will be raised if there are\n any non-string values in *iterable*, including ``bytes`` objects.\n The separator between elements is the string providing this method.\n\nstr.ljust(width[, fillchar])\n\n Return the string left justified in a string of length *width*.\n Padding is done using the specified *fillchar* (default is a\n space). The original string is returned if *width* is less than or\n equal to ``len(s)``.\n\nstr.lower()\n\n Return a copy of the string with all the cased characters [4]\n converted to lowercase.\n\n The lowercasing algorithm used is described in section 3.13 of the\n Unicode Standard.\n\nstr.lstrip([chars])\n\n Return a copy of the string with leading characters removed. The\n *chars* argument is a string specifying the set of characters to be\n removed. If omitted or ``None``, the *chars* argument defaults to\n removing whitespace. The *chars* argument is not a prefix; rather,\n all combinations of its values are stripped:\n\n >>> \' spacious \'.lstrip()\n \'spacious \'\n >>> \'www.example.com\'.lstrip(\'cmowz.\')\n \'example.com\'\n\nstatic str.maketrans(x[, y[, z]])\n\n This static method returns a translation table usable for\n ``str.translate()``.\n\n If there is only one argument, it must be a dictionary mapping\n Unicode ordinals (integers) or characters (strings of length 1) to\n Unicode ordinals, strings (of arbitrary lengths) or None.\n Character keys will then be converted to ordinals.\n\n If there are two arguments, they must be strings of equal length,\n and in the resulting dictionary, each character in x will be mapped\n to the character at the same position in y. If there is a third\n argument, it must be a string, whose characters will be mapped to\n None in the result.\n\nstr.partition(sep)\n\n Split the string at the first occurrence of *sep*, and return a\n 3-tuple containing the part before the separator, the separator\n itself, and the part after the separator. If the separator is not\n found, return a 3-tuple containing the string itself, followed by\n two empty strings.\n\nstr.replace(old, new[, count])\n\n Return a copy of the string with all occurrences of substring *old*\n replaced by *new*. If the optional argument *count* is given, only\n the first *count* occurrences are replaced.\n\nstr.rfind(sub[, start[, end]])\n\n Return the highest index in the string where substring *sub* is\n found, such that *sub* is contained within ``s[start:end]``.\n Optional arguments *start* and *end* are interpreted as in slice\n notation. Return ``-1`` on failure.\n\nstr.rindex(sub[, start[, end]])\n\n Like ``rfind()`` but raises ``ValueError`` when the substring *sub*\n is not found.\n\nstr.rjust(width[, fillchar])\n\n Return the string right justified in a string of length *width*.\n Padding is done using the specified *fillchar* (default is a\n space). The original string is returned if *width* is less than or\n equal to ``len(s)``.\n\nstr.rpartition(sep)\n\n Split the string at the last occurrence of *sep*, and return a\n 3-tuple containing the part before the separator, the separator\n itself, and the part after the separator. If the separator is not\n found, return a 3-tuple containing two empty strings, followed by\n the string itself.\n\nstr.rsplit(sep=None, maxsplit=-1)\n\n Return a list of the words in the string, using *sep* as the\n delimiter string. If *maxsplit* is given, at most *maxsplit* splits\n are done, the *rightmost* ones. If *sep* is not specified or\n ``None``, any whitespace string is a separator. Except for\n splitting from the right, ``rsplit()`` behaves like ``split()``\n which is described in detail below.\n\nstr.rstrip([chars])\n\n Return a copy of the string with trailing characters removed. The\n *chars* argument is a string specifying the set of characters to be\n removed. If omitted or ``None``, the *chars* argument defaults to\n removing whitespace. The *chars* argument is not a suffix; rather,\n all combinations of its values are stripped:\n\n >>> \' spacious \'.rstrip()\n \' spacious\'\n >>> \'mississippi\'.rstrip(\'ipz\')\n \'mississ\'\n\nstr.split(sep=None, maxsplit=-1)\n\n Return a list of the words in the string, using *sep* as the\n delimiter string. If *maxsplit* is given, at most *maxsplit*\n splits are done (thus, the list will have at most ``maxsplit+1``\n elements). If *maxsplit* is not specified or ``-1``, then there is\n no limit on the number of splits (all possible splits are made).\n\n If *sep* is given, consecutive delimiters are not grouped together\n and are deemed to delimit empty strings (for example,\n ``\'1,,2\'.split(\',\')`` returns ``[\'1\', \'\', \'2\']``). The *sep*\n argument may consist of multiple characters (for example,\n ``\'1<>2<>3\'.split(\'<>\')`` returns ``[\'1\', \'2\', \'3\']``). Splitting\n an empty string with a specified separator returns ``[\'\']``.\n\n If *sep* is not specified or is ``None``, a different splitting\n algorithm is applied: runs of consecutive whitespace are regarded\n as a single separator, and the result will contain no empty strings\n at the start or end if the string has leading or trailing\n whitespace. Consequently, splitting an empty string or a string\n consisting of just whitespace with a ``None`` separator returns\n ``[]``.\n\n For example, ``\' 1 2 3 \'.split()`` returns ``[\'1\', \'2\', \'3\']``,\n and ``\' 1 2 3 \'.split(None, 1)`` returns ``[\'1\', \'2 3 \']``.\n\nstr.splitlines([keepends])\n\n Return a list of the lines in the string, breaking at line\n boundaries. Line breaks are not included in the resulting list\n unless *keepends* is given and true.\n\nstr.startswith(prefix[, start[, end]])\n\n Return ``True`` if string starts with the *prefix*, otherwise\n return ``False``. *prefix* can also be a tuple of prefixes to look\n for. With optional *start*, test string beginning at that\n position. With optional *end*, stop comparing string at that\n position.\n\nstr.strip([chars])\n\n Return a copy of the string with the leading and trailing\n characters removed. The *chars* argument is a string specifying the\n set of characters to be removed. If omitted or ``None``, the\n *chars* argument defaults to removing whitespace. The *chars*\n argument is not a prefix or suffix; rather, all combinations of its\n values are stripped:\n\n >>> \' spacious \'.strip()\n \'spacious\'\n >>> \'www.example.com\'.strip(\'cmowz.\')\n \'example\'\n\nstr.swapcase()\n\n Return a copy of the string with uppercase characters converted to\n lowercase and vice versa. Note that it is not necessarily true that\n ``s.swapcase().swapcase() == s``.\n\nstr.title()\n\n Return a titlecased version of the string where words start with an\n uppercase character and the remaining characters are lowercase.\n\n The algorithm uses a simple language-independent definition of a\n word as groups of consecutive letters. The definition works in\n many contexts but it means that apostrophes in contractions and\n possessives form word boundaries, which may not be the desired\n result:\n\n >>> "they\'re bill\'s friends from the UK".title()\n "They\'Re Bill\'S Friends From The Uk"\n\n A workaround for apostrophes can be constructed using regular\n expressions:\n\n >>> import re\n >>> def titlecase(s):\n return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n lambda mo: mo.group(0)[0].upper() +\n mo.group(0)[1:].lower(),\n s)\n\n >>> titlecase("they\'re bill\'s friends.")\n "They\'re Bill\'s Friends."\n\nstr.translate(map)\n\n Return a copy of the *s* where all characters have been mapped\n through the *map* which must be a dictionary of Unicode ordinals\n (integers) to Unicode ordinals, strings or ``None``. Unmapped\n characters are left untouched. Characters mapped to ``None`` are\n deleted.\n\n You can use ``str.maketrans()`` to create a translation map from\n character-to-character mappings in different formats.\n\n Note: An even more flexible approach is to create a custom character\n mapping codec using the ``codecs`` module (see\n ``encodings.cp1251`` for an example).\n\nstr.upper()\n\n Return a copy of the string with all the cased characters [4]\n converted to uppercase. Note that ``str.upper().isupper()`` might\n be ``False`` if ``s`` contains uncased characters or if the Unicode\n category of the resulting character(s) is not "Lu" (Letter,\n uppercase), but e.g. "Lt" (Letter, titlecase).\n\n The uppercasing algorithm used is described in section 3.13 of the\n Unicode Standard.\n\nstr.zfill(width)\n\n Return the numeric string left filled with zeros in a string of\n length *width*. A sign prefix is handled correctly. The original\n string is returned if *width* is less than or equal to ``len(s)``.\n', + 'specialnames': '\nSpecial method names\n********************\n\nA class can implement certain operations that are invoked by special\nsyntax (such as arithmetic operations or subscripting and slicing) by\ndefining methods with special names. This is Python\'s approach to\n*operator overloading*, allowing classes to define their own behavior\nwith respect to language operators. For instance, if a class defines\na method named ``__getitem__()``, and ``x`` is an instance of this\nclass, then ``x[i]`` is roughly equivalent to ``type(x).__getitem__(x,\ni)``. Except where mentioned, attempts to execute an operation raise\nan exception when no appropriate method is defined (typically\n``AttributeError`` or ``TypeError``).\n\nWhen implementing a class that emulates any built-in type, it is\nimportant that the emulation only be implemented to the degree that it\nmakes sense for the object being modelled. For example, some\nsequences may work well with retrieval of individual elements, but\nextracting a slice may not make sense. (One example of this is the\n``NodeList`` interface in the W3C\'s Document Object Model.)\n\n\nBasic customization\n===================\n\nobject.__new__(cls[, ...])\n\n Called to create a new instance of class *cls*. ``__new__()`` is a\n static method (special-cased so you need not declare it as such)\n that takes the class of which an instance was requested as its\n first argument. The remaining arguments are those passed to the\n object constructor expression (the call to the class). The return\n value of ``__new__()`` should be the new object instance (usually\n an instance of *cls*).\n\n Typical implementations create a new instance of the class by\n invoking the superclass\'s ``__new__()`` method using\n ``super(currentclass, cls).__new__(cls[, ...])`` with appropriate\n arguments and then modifying the newly-created instance as\n necessary before returning it.\n\n If ``__new__()`` returns an instance of *cls*, then the new\n instance\'s ``__init__()`` method will be invoked like\n ``__init__(self[, ...])``, where *self* is the new instance and the\n remaining arguments are the same as were passed to ``__new__()``.\n\n If ``__new__()`` does not return an instance of *cls*, then the new\n instance\'s ``__init__()`` method will not be invoked.\n\n ``__new__()`` is intended mainly to allow subclasses of immutable\n types (like int, str, or tuple) to customize instance creation. It\n is also commonly overridden in custom metaclasses in order to\n customize class creation.\n\nobject.__init__(self[, ...])\n\n Called when the instance is created. The arguments are those\n passed to the class constructor expression. If a base class has an\n ``__init__()`` method, the derived class\'s ``__init__()`` method,\n if any, must explicitly call it to ensure proper initialization of\n the base class part of the instance; for example:\n ``BaseClass.__init__(self, [args...])``. As a special constraint\n on constructors, no value may be returned; doing so will cause a\n ``TypeError`` to be raised at runtime.\n\nobject.__del__(self)\n\n Called when the instance is about to be destroyed. This is also\n called a destructor. If a base class has a ``__del__()`` method,\n the derived class\'s ``__del__()`` method, if any, must explicitly\n call it to ensure proper deletion of the base class part of the\n instance. Note that it is possible (though not recommended!) for\n the ``__del__()`` method to postpone destruction of the instance by\n creating a new reference to it. It may then be called at a later\n time when this new reference is deleted. It is not guaranteed that\n ``__del__()`` methods are called for objects that still exist when\n the interpreter exits.\n\n Note: ``del x`` doesn\'t directly call ``x.__del__()`` --- the former\n decrements the reference count for ``x`` by one, and the latter\n is only called when ``x``\'s reference count reaches zero. Some\n common situations that may prevent the reference count of an\n object from going to zero include: circular references between\n objects (e.g., a doubly-linked list or a tree data structure with\n parent and child pointers); a reference to the object on the\n stack frame of a function that caught an exception (the traceback\n stored in ``sys.exc_info()[2]`` keeps the stack frame alive); or\n a reference to the object on the stack frame that raised an\n unhandled exception in interactive mode (the traceback stored in\n ``sys.last_traceback`` keeps the stack frame alive). The first\n situation can only be remedied by explicitly breaking the cycles;\n the latter two situations can be resolved by storing ``None`` in\n ``sys.last_traceback``. Circular references which are garbage are\n detected when the option cycle detector is enabled (it\'s on by\n default), but can only be cleaned up if there are no Python-\n level ``__del__()`` methods involved. Refer to the documentation\n for the ``gc`` module for more information about how\n ``__del__()`` methods are handled by the cycle detector,\n particularly the description of the ``garbage`` value.\n\n Warning: Due to the precarious circumstances under which ``__del__()``\n methods are invoked, exceptions that occur during their execution\n are ignored, and a warning is printed to ``sys.stderr`` instead.\n Also, when ``__del__()`` is invoked in response to a module being\n deleted (e.g., when execution of the program is done), other\n globals referenced by the ``__del__()`` method may already have\n been deleted or in the process of being torn down (e.g. the\n import machinery shutting down). For this reason, ``__del__()``\n methods should do the absolute minimum needed to maintain\n external invariants. Starting with version 1.5, Python\n guarantees that globals whose name begins with a single\n underscore are deleted from their module before other globals are\n deleted; if no other references to such globals exist, this may\n help in assuring that imported modules are still available at the\n time when the ``__del__()`` method is called.\n\nobject.__repr__(self)\n\n Called by the ``repr()`` built-in function to compute the\n "official" string representation of an object. If at all possible,\n this should look like a valid Python expression that could be used\n to recreate an object with the same value (given an appropriate\n environment). If this is not possible, a string of the form\n ``<...some useful description...>`` should be returned. The return\n value must be a string object. If a class defines ``__repr__()``\n but not ``__str__()``, then ``__repr__()`` is also used when an\n "informal" string representation of instances of that class is\n required.\n\n This is typically used for debugging, so it is important that the\n representation is information-rich and unambiguous.\n\nobject.__str__(self)\n\n Called by the ``str()`` built-in function and by the ``print()``\n function to compute the "informal" string representation of an\n object. This differs from ``__repr__()`` in that it does not have\n to be a valid Python expression: a more convenient or concise\n representation may be used instead. The return value must be a\n string object.\n\nobject.__bytes__(self)\n\n Called by ``bytes()`` to compute a byte-string representation of an\n object. This should return a ``bytes`` object.\n\nobject.__format__(self, format_spec)\n\n Called by the ``format()`` built-in function (and by extension, the\n ``format()`` method of class ``str``) to produce a "formatted"\n string representation of an object. The ``format_spec`` argument is\n a string that contains a description of the formatting options\n desired. The interpretation of the ``format_spec`` argument is up\n to the type implementing ``__format__()``, however most classes\n will either delegate formatting to one of the built-in types, or\n use a similar formatting option syntax.\n\n See *Format Specification Mini-Language* for a description of the\n standard formatting syntax.\n\n The return value must be a string object.\n\nobject.__lt__(self, other)\nobject.__le__(self, other)\nobject.__eq__(self, other)\nobject.__ne__(self, other)\nobject.__gt__(self, other)\nobject.__ge__(self, other)\n\n These are the so-called "rich comparison" methods. The\n correspondence between operator symbols and method names is as\n follows: ``xy`` calls ``x.__gt__(y)``, and ``x>=y`` calls\n ``x.__ge__(y)``.\n\n A rich comparison method may return the singleton\n ``NotImplemented`` if it does not implement the operation for a\n given pair of arguments. By convention, ``False`` and ``True`` are\n returned for a successful comparison. However, these methods can\n return any value, so if the comparison operator is used in a\n Boolean context (e.g., in the condition of an ``if`` statement),\n Python will call ``bool()`` on the value to determine if the result\n is true or false.\n\n There are no implied relationships among the comparison operators.\n The truth of ``x==y`` does not imply that ``x!=y`` is false.\n Accordingly, when defining ``__eq__()``, one should also define\n ``__ne__()`` so that the operators will behave as expected. See\n the paragraph on ``__hash__()`` for some important notes on\n creating *hashable* objects which support custom comparison\n operations and are usable as dictionary keys.\n\n There are no swapped-argument versions of these methods (to be used\n when the left argument does not support the operation but the right\n argument does); rather, ``__lt__()`` and ``__gt__()`` are each\n other\'s reflection, ``__le__()`` and ``__ge__()`` are each other\'s\n reflection, and ``__eq__()`` and ``__ne__()`` are their own\n reflection.\n\n Arguments to rich comparison methods are never coerced.\n\n To automatically generate ordering operations from a single root\n operation, see ``functools.total_ordering()``.\n\nobject.__hash__(self)\n\n Called by built-in function ``hash()`` and for operations on\n members of hashed collections including ``set``, ``frozenset``, and\n ``dict``. ``__hash__()`` should return an integer. The only\n required property is that objects which compare equal have the same\n hash value; it is advised to somehow mix together (e.g. using\n exclusive or) the hash values for the components of the object that\n also play a part in comparison of objects.\n\n If a class does not define an ``__eq__()`` method it should not\n define a ``__hash__()`` operation either; if it defines\n ``__eq__()`` but not ``__hash__()``, its instances will not be\n usable as items in hashable collections. If a class defines\n mutable objects and implements an ``__eq__()`` method, it should\n not implement ``__hash__()``, since the implementation of hashable\n collections requires that a key\'s hash value is immutable (if the\n object\'s hash value changes, it will be in the wrong hash bucket).\n\n User-defined classes have ``__eq__()`` and ``__hash__()`` methods\n by default; with them, all objects compare unequal (except with\n themselves) and ``x.__hash__()`` returns ``id(x)``.\n\n Classes which inherit a ``__hash__()`` method from a parent class\n but change the meaning of ``__eq__()`` such that the hash value\n returned is no longer appropriate (e.g. by switching to a value-\n based concept of equality instead of the default identity based\n equality) can explicitly flag themselves as being unhashable by\n setting ``__hash__ = None`` in the class definition. Doing so means\n that not only will instances of the class raise an appropriate\n ``TypeError`` when a program attempts to retrieve their hash value,\n but they will also be correctly identified as unhashable when\n checking ``isinstance(obj, collections.Hashable)`` (unlike classes\n which define their own ``__hash__()`` to explicitly raise\n ``TypeError``).\n\n If a class that overrides ``__eq__()`` needs to retain the\n implementation of ``__hash__()`` from a parent class, the\n interpreter must be told this explicitly by setting ``__hash__ =\n .__hash__``. Otherwise the inheritance of\n ``__hash__()`` will be blocked, just as if ``__hash__`` had been\n explicitly set to ``None``.\n\n Note: Note by default the ``__hash__()`` values of str, bytes and\n datetime objects are "salted" with an unpredictable random value.\n Although they remain constant within an individual Python\n process, they are not predictable between repeated invocations of\n Python.This is intended to provide protection against a denial-\n of-service caused by carefully-chosen inputs that exploit the\n worst case performance of a dict insertion, O(n^2) complexity.\n See http://www.ocert.org/advisories/ocert-2011-003.html for\n details.Changing hash values affects the order in which keys are\n retrieved from a dict. Note Python has never made guarantees\n about this ordering (and it typically varies between 32-bit and\n 64-bit builds).See also ``PYTHONHASHSEED``.\n\n Changed in version 3.3: Hash randomization is enabled by default.\n\nobject.__bool__(self)\n\n Called to implement truth value testing and the built-in operation\n ``bool()``; should return ``False`` or ``True``. When this method\n is not defined, ``__len__()`` is called, if it is defined, and the\n object is considered true if its result is nonzero. If a class\n defines neither ``__len__()`` nor ``__bool__()``, all its instances\n are considered true.\n\n\nCustomizing attribute access\n============================\n\nThe following methods can be defined to customize the meaning of\nattribute access (use of, assignment to, or deletion of ``x.name``)\nfor class instances.\n\nobject.__getattr__(self, name)\n\n Called when an attribute lookup has not found the attribute in the\n usual places (i.e. it is not an instance attribute nor is it found\n in the class tree for ``self``). ``name`` is the attribute name.\n This method should return the (computed) attribute value or raise\n an ``AttributeError`` exception.\n\n Note that if the attribute is found through the normal mechanism,\n ``__getattr__()`` is not called. (This is an intentional asymmetry\n between ``__getattr__()`` and ``__setattr__()``.) This is done both\n for efficiency reasons and because otherwise ``__getattr__()``\n would have no way to access other attributes of the instance. Note\n that at least for instance variables, you can fake total control by\n not inserting any values in the instance attribute dictionary (but\n instead inserting them in another object). See the\n ``__getattribute__()`` method below for a way to actually get total\n control over attribute access.\n\nobject.__getattribute__(self, name)\n\n Called unconditionally to implement attribute accesses for\n instances of the class. If the class also defines\n ``__getattr__()``, the latter will not be called unless\n ``__getattribute__()`` either calls it explicitly or raises an\n ``AttributeError``. This method should return the (computed)\n attribute value or raise an ``AttributeError`` exception. In order\n to avoid infinite recursion in this method, its implementation\n should always call the base class method with the same name to\n access any attributes it needs, for example,\n ``object.__getattribute__(self, name)``.\n\n Note: This method may still be bypassed when looking up special methods\n as the result of implicit invocation via language syntax or\n built-in functions. See *Special method lookup*.\n\nobject.__setattr__(self, name, value)\n\n Called when an attribute assignment is attempted. This is called\n instead of the normal mechanism (i.e. store the value in the\n instance dictionary). *name* is the attribute name, *value* is the\n value to be assigned to it.\n\n If ``__setattr__()`` wants to assign to an instance attribute, it\n should call the base class method with the same name, for example,\n ``object.__setattr__(self, name, value)``.\n\nobject.__delattr__(self, name)\n\n Like ``__setattr__()`` but for attribute deletion instead of\n assignment. This should only be implemented if ``del obj.name`` is\n meaningful for the object.\n\nobject.__dir__(self)\n\n Called when ``dir()`` is called on the object. A sequence must be\n returned. ``dir()`` converts the returned sequence to a list and\n sorts it.\n\n\nImplementing Descriptors\n------------------------\n\nThe following methods only apply when an instance of the class\ncontaining the method (a so-called *descriptor* class) appears in an\n*owner* class (the descriptor must be in either the owner\'s class\ndictionary or in the class dictionary for one of its parents). In the\nexamples below, "the attribute" refers to the attribute whose name is\nthe key of the property in the owner class\' ``__dict__``.\n\nobject.__get__(self, instance, owner)\n\n Called to get the attribute of the owner class (class attribute\n access) or of an instance of that class (instance attribute\n access). *owner* is always the owner class, while *instance* is the\n instance that the attribute was accessed through, or ``None`` when\n the attribute is accessed through the *owner*. This method should\n return the (computed) attribute value or raise an\n ``AttributeError`` exception.\n\nobject.__set__(self, instance, value)\n\n Called to set the attribute on an instance *instance* of the owner\n class to a new value, *value*.\n\nobject.__delete__(self, instance)\n\n Called to delete the attribute on an instance *instance* of the\n owner class.\n\n\nInvoking Descriptors\n--------------------\n\nIn general, a descriptor is an object attribute with "binding\nbehavior", one whose attribute access has been overridden by methods\nin the descriptor protocol: ``__get__()``, ``__set__()``, and\n``__delete__()``. If any of those methods are defined for an object,\nit is said to be a descriptor.\n\nThe default behavior for attribute access is to get, set, or delete\nthe attribute from an object\'s dictionary. For instance, ``a.x`` has a\nlookup chain starting with ``a.__dict__[\'x\']``, then\n``type(a).__dict__[\'x\']``, and continuing through the base classes of\n``type(a)`` excluding metaclasses.\n\nHowever, if the looked-up value is an object defining one of the\ndescriptor methods, then Python may override the default behavior and\ninvoke the descriptor method instead. Where this occurs in the\nprecedence chain depends on which descriptor methods were defined and\nhow they were called.\n\nThe starting point for descriptor invocation is a binding, ``a.x``.\nHow the arguments are assembled depends on ``a``:\n\nDirect Call\n The simplest and least common call is when user code directly\n invokes a descriptor method: ``x.__get__(a)``.\n\nInstance Binding\n If binding to an object instance, ``a.x`` is transformed into the\n call: ``type(a).__dict__[\'x\'].__get__(a, type(a))``.\n\nClass Binding\n If binding to a class, ``A.x`` is transformed into the call:\n ``A.__dict__[\'x\'].__get__(None, A)``.\n\nSuper Binding\n If ``a`` is an instance of ``super``, then the binding ``super(B,\n obj).m()`` searches ``obj.__class__.__mro__`` for the base class\n ``A`` immediately preceding ``B`` and then invokes the descriptor\n with the call: ``A.__dict__[\'m\'].__get__(obj, obj.__class__)``.\n\nFor instance bindings, the precedence of descriptor invocation depends\non the which descriptor methods are defined. A descriptor can define\nany combination of ``__get__()``, ``__set__()`` and ``__delete__()``.\nIf it does not define ``__get__()``, then accessing the attribute will\nreturn the descriptor object itself unless there is a value in the\nobject\'s instance dictionary. If the descriptor defines ``__set__()``\nand/or ``__delete__()``, it is a data descriptor; if it defines\nneither, it is a non-data descriptor. Normally, data descriptors\ndefine both ``__get__()`` and ``__set__()``, while non-data\ndescriptors have just the ``__get__()`` method. Data descriptors with\n``__set__()`` and ``__get__()`` defined always override a redefinition\nin an instance dictionary. In contrast, non-data descriptors can be\noverridden by instances.\n\nPython methods (including ``staticmethod()`` and ``classmethod()``)\nare implemented as non-data descriptors. Accordingly, instances can\nredefine and override methods. This allows individual instances to\nacquire behaviors that differ from other instances of the same class.\n\nThe ``property()`` function is implemented as a data descriptor.\nAccordingly, instances cannot override the behavior of a property.\n\n\n__slots__\n---------\n\nBy default, instances of classes have a dictionary for attribute\nstorage. This wastes space for objects having very few instance\nvariables. The space consumption can become acute when creating large\nnumbers of instances.\n\nThe default can be overridden by defining *__slots__* in a class\ndefinition. The *__slots__* declaration takes a sequence of instance\nvariables and reserves just enough space in each instance to hold a\nvalue for each variable. Space is saved because *__dict__* is not\ncreated for each instance.\n\nobject.__slots__\n\n This class variable can be assigned a string, iterable, or sequence\n of strings with variable names used by instances. If defined in a\n class, *__slots__* reserves space for the declared variables and\n prevents the automatic creation of *__dict__* and *__weakref__* for\n each instance.\n\n\nNotes on using *__slots__*\n~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n* When inheriting from a class without *__slots__*, the *__dict__*\n attribute of that class will always be accessible, so a *__slots__*\n definition in the subclass is meaningless.\n\n* Without a *__dict__* variable, instances cannot be assigned new\n variables not listed in the *__slots__* definition. Attempts to\n assign to an unlisted variable name raises ``AttributeError``. If\n dynamic assignment of new variables is desired, then add\n ``\'__dict__\'`` to the sequence of strings in the *__slots__*\n declaration.\n\n* Without a *__weakref__* variable for each instance, classes defining\n *__slots__* do not support weak references to its instances. If weak\n reference support is needed, then add ``\'__weakref__\'`` to the\n sequence of strings in the *__slots__* declaration.\n\n* *__slots__* are implemented at the class level by creating\n descriptors (*Implementing Descriptors*) for each variable name. As\n a result, class attributes cannot be used to set default values for\n instance variables defined by *__slots__*; otherwise, the class\n attribute would overwrite the descriptor assignment.\n\n* The action of a *__slots__* declaration is limited to the class\n where it is defined. As a result, subclasses will have a *__dict__*\n unless they also define *__slots__* (which must only contain names\n of any *additional* slots).\n\n* If a class defines a slot also defined in a base class, the instance\n variable defined by the base class slot is inaccessible (except by\n retrieving its descriptor directly from the base class). This\n renders the meaning of the program undefined. In the future, a\n check may be added to prevent this.\n\n* Nonempty *__slots__* does not work for classes derived from\n "variable-length" built-in types such as ``int``, ``str`` and\n ``tuple``.\n\n* Any non-string iterable may be assigned to *__slots__*. Mappings may\n also be used; however, in the future, special meaning may be\n assigned to the values corresponding to each key.\n\n* *__class__* assignment works only if both classes have the same\n *__slots__*.\n\n\nCustomizing class creation\n==========================\n\nBy default, classes are constructed using ``type()``. A class\ndefinition is read into a separate namespace and the value of class\nname is bound to the result of ``type(name, bases, dict)``.\n\nWhen the class definition is read, if a callable ``metaclass`` keyword\nargument is passed after the bases in the class definition, the\ncallable given will be called instead of ``type()``. If other keyword\narguments are passed, they will also be passed to the metaclass. This\nallows classes or functions to be written which monitor or alter the\nclass creation process:\n\n* Modifying the class dictionary prior to the class being created.\n\n* Returning an instance of another class -- essentially performing the\n role of a factory function.\n\nThese steps will have to be performed in the metaclass\'s ``__new__()``\nmethod -- ``type.__new__()`` can then be called from this method to\ncreate a class with different properties. This example adds a new\nelement to the class dictionary before creating the class:\n\n class metacls(type):\n def __new__(mcs, name, bases, dict):\n dict[\'foo\'] = \'metacls was here\'\n return type.__new__(mcs, name, bases, dict)\n\nYou can of course also override other class methods (or add new\nmethods); for example defining a custom ``__call__()`` method in the\nmetaclass allows custom behavior when the class is called, e.g. not\nalways creating a new instance.\n\nIf the metaclass has a ``__prepare__()`` attribute (usually\nimplemented as a class or static method), it is called before the\nclass body is evaluated with the name of the class and a tuple of its\nbases for arguments. It should return an object that supports the\nmapping interface that will be used to store the namespace of the\nclass. The default is a plain dictionary. This could be used, for\nexample, to keep track of the order that class attributes are declared\nin by returning an ordered dictionary.\n\nThe appropriate metaclass is determined by the following precedence\nrules:\n\n* If the ``metaclass`` keyword argument is passed with the bases, it\n is used.\n\n* Otherwise, if there is at least one base class, its metaclass is\n used.\n\n* Otherwise, the default metaclass (``type``) is used.\n\nThe potential uses for metaclasses are boundless. Some ideas that have\nbeen explored including logging, interface checking, automatic\ndelegation, automatic property creation, proxies, frameworks, and\nautomatic resource locking/synchronization.\n\nHere is an example of a metaclass that uses an\n``collections.OrderedDict`` to remember the order that class members\nwere defined:\n\n class OrderedClass(type):\n\n @classmethod\n def __prepare__(metacls, name, bases, **kwds):\n return collections.OrderedDict()\n\n def __new__(cls, name, bases, classdict):\n result = type.__new__(cls, name, bases, dict(classdict))\n result.members = tuple(classdict)\n return result\n\n class A(metaclass=OrderedClass):\n def one(self): pass\n def two(self): pass\n def three(self): pass\n def four(self): pass\n\n >>> A.members\n (\'__module__\', \'one\', \'two\', \'three\', \'four\')\n\nWhen the class definition for *A* gets executed, the process begins\nwith calling the metaclass\'s ``__prepare__()`` method which returns an\nempty ``collections.OrderedDict``. That mapping records the methods\nand attributes of *A* as they are defined within the body of the class\nstatement. Once those definitions are executed, the ordered dictionary\nis fully populated and the metaclass\'s ``__new__()`` method gets\ninvoked. That method builds the new type and it saves the ordered\ndictionary keys in an attribute called ``members``.\n\n\nCustomizing instance and subclass checks\n========================================\n\nThe following methods are used to override the default behavior of the\n``isinstance()`` and ``issubclass()`` built-in functions.\n\nIn particular, the metaclass ``abc.ABCMeta`` implements these methods\nin order to allow the addition of Abstract Base Classes (ABCs) as\n"virtual base classes" to any class or type (including built-in\ntypes), including other ABCs.\n\nclass.__instancecheck__(self, instance)\n\n Return true if *instance* should be considered a (direct or\n indirect) instance of *class*. If defined, called to implement\n ``isinstance(instance, class)``.\n\nclass.__subclasscheck__(self, subclass)\n\n Return true if *subclass* should be considered a (direct or\n indirect) subclass of *class*. If defined, called to implement\n ``issubclass(subclass, class)``.\n\nNote that these methods are looked up on the type (metaclass) of a\nclass. They cannot be defined as class methods in the actual class.\nThis is consistent with the lookup of special methods that are called\non instances, only in this case the instance is itself a class.\n\nSee also:\n\n **PEP 3119** - Introducing Abstract Base Classes\n Includes the specification for customizing ``isinstance()`` and\n ``issubclass()`` behavior through ``__instancecheck__()`` and\n ``__subclasscheck__()``, with motivation for this functionality\n in the context of adding Abstract Base Classes (see the ``abc``\n module) to the language.\n\n\nEmulating callable objects\n==========================\n\nobject.__call__(self[, args...])\n\n Called when the instance is "called" as a function; if this method\n is defined, ``x(arg1, arg2, ...)`` is a shorthand for\n ``x.__call__(arg1, arg2, ...)``.\n\n\nEmulating container types\n=========================\n\nThe following methods can be defined to implement container objects.\nContainers usually are sequences (such as lists or tuples) or mappings\n(like dictionaries), but can represent other containers as well. The\nfirst set of methods is used either to emulate a sequence or to\nemulate a mapping; the difference is that for a sequence, the\nallowable keys should be the integers *k* for which ``0 <= k < N``\nwhere *N* is the length of the sequence, or slice objects, which\ndefine a range of items. It is also recommended that mappings provide\nthe methods ``keys()``, ``values()``, ``items()``, ``get()``,\n``clear()``, ``setdefault()``, ``pop()``, ``popitem()``, ``copy()``,\nand ``update()`` behaving similar to those for Python\'s standard\ndictionary objects. The ``collections`` module provides a\n``MutableMapping`` abstract base class to help create those methods\nfrom a base set of ``__getitem__()``, ``__setitem__()``,\n``__delitem__()``, and ``keys()``. Mutable sequences should provide\nmethods ``append()``, ``count()``, ``index()``, ``extend()``,\n``insert()``, ``pop()``, ``remove()``, ``reverse()`` and ``sort()``,\nlike Python standard list objects. Finally, sequence types should\nimplement addition (meaning concatenation) and multiplication (meaning\nrepetition) by defining the methods ``__add__()``, ``__radd__()``,\n``__iadd__()``, ``__mul__()``, ``__rmul__()`` and ``__imul__()``\ndescribed below; they should not define other numerical operators. It\nis recommended that both mappings and sequences implement the\n``__contains__()`` method to allow efficient use of the ``in``\noperator; for mappings, ``in`` should search the mapping\'s keys; for\nsequences, it should search through the values. It is further\nrecommended that both mappings and sequences implement the\n``__iter__()`` method to allow efficient iteration through the\ncontainer; for mappings, ``__iter__()`` should be the same as\n``keys()``; for sequences, it should iterate through the values.\n\nobject.__len__(self)\n\n Called to implement the built-in function ``len()``. Should return\n the length of the object, an integer ``>=`` 0. Also, an object\n that doesn\'t define a ``__bool__()`` method and whose ``__len__()``\n method returns zero is considered to be false in a Boolean context.\n\nNote: Slicing is done exclusively with the following three methods. A\n call like\n\n a[1:2] = b\n\n is translated to\n\n a[slice(1, 2, None)] = b\n\n and so forth. Missing slice items are always filled in with\n ``None``.\n\nobject.__getitem__(self, key)\n\n Called to implement evaluation of ``self[key]``. For sequence\n types, the accepted keys should be integers and slice objects.\n Note that the special interpretation of negative indexes (if the\n class wishes to emulate a sequence type) is up to the\n ``__getitem__()`` method. If *key* is of an inappropriate type,\n ``TypeError`` may be raised; if of a value outside the set of\n indexes for the sequence (after any special interpretation of\n negative values), ``IndexError`` should be raised. For mapping\n types, if *key* is missing (not in the container), ``KeyError``\n should be raised.\n\n Note: ``for`` loops expect that an ``IndexError`` will be raised for\n illegal indexes to allow proper detection of the end of the\n sequence.\n\nobject.__setitem__(self, key, value)\n\n Called to implement assignment to ``self[key]``. Same note as for\n ``__getitem__()``. This should only be implemented for mappings if\n the objects support changes to the values for keys, or if new keys\n can be added, or for sequences if elements can be replaced. The\n same exceptions should be raised for improper *key* values as for\n the ``__getitem__()`` method.\n\nobject.__delitem__(self, key)\n\n Called to implement deletion of ``self[key]``. Same note as for\n ``__getitem__()``. This should only be implemented for mappings if\n the objects support removal of keys, or for sequences if elements\n can be removed from the sequence. The same exceptions should be\n raised for improper *key* values as for the ``__getitem__()``\n method.\n\nobject.__iter__(self)\n\n This method is called when an iterator is required for a container.\n This method should return a new iterator object that can iterate\n over all the objects in the container. For mappings, it should\n iterate over the keys of the container, and should also be made\n available as the method ``keys()``.\n\n Iterator objects also need to implement this method; they are\n required to return themselves. For more information on iterator\n objects, see *Iterator Types*.\n\nobject.__reversed__(self)\n\n Called (if present) by the ``reversed()`` built-in to implement\n reverse iteration. It should return a new iterator object that\n iterates over all the objects in the container in reverse order.\n\n If the ``__reversed__()`` method is not provided, the\n ``reversed()`` built-in will fall back to using the sequence\n protocol (``__len__()`` and ``__getitem__()``). Objects that\n support the sequence protocol should only provide\n ``__reversed__()`` if they can provide an implementation that is\n more efficient than the one provided by ``reversed()``.\n\nThe membership test operators (``in`` and ``not in``) are normally\nimplemented as an iteration through a sequence. However, container\nobjects can supply the following special method with a more efficient\nimplementation, which also does not require the object be a sequence.\n\nobject.__contains__(self, item)\n\n Called to implement membership test operators. Should return true\n if *item* is in *self*, false otherwise. For mapping objects, this\n should consider the keys of the mapping rather than the values or\n the key-item pairs.\n\n For objects that don\'t define ``__contains__()``, the membership\n test first tries iteration via ``__iter__()``, then the old\n sequence iteration protocol via ``__getitem__()``, see *this\n section in the language reference*.\n\n\nEmulating numeric types\n=======================\n\nThe following methods can be defined to emulate numeric objects.\nMethods corresponding to operations that are not supported by the\nparticular kind of number implemented (e.g., bitwise operations for\nnon-integral numbers) should be left undefined.\n\nobject.__add__(self, other)\nobject.__sub__(self, other)\nobject.__mul__(self, other)\nobject.__truediv__(self, other)\nobject.__floordiv__(self, other)\nobject.__mod__(self, other)\nobject.__divmod__(self, other)\nobject.__pow__(self, other[, modulo])\nobject.__lshift__(self, other)\nobject.__rshift__(self, other)\nobject.__and__(self, other)\nobject.__xor__(self, other)\nobject.__or__(self, other)\n\n These methods are called to implement the binary arithmetic\n operations (``+``, ``-``, ``*``, ``/``, ``//``, ``%``,\n ``divmod()``, ``pow()``, ``**``, ``<<``, ``>>``, ``&``, ``^``,\n ``|``). For instance, to evaluate the expression ``x + y``, where\n *x* is an instance of a class that has an ``__add__()`` method,\n ``x.__add__(y)`` is called. The ``__divmod__()`` method should be\n the equivalent to using ``__floordiv__()`` and ``__mod__()``; it\n should not be related to ``__truediv__()``. Note that\n ``__pow__()`` should be defined to accept an optional third\n argument if the ternary version of the built-in ``pow()`` function\n is to be supported.\n\n If one of those methods does not support the operation with the\n supplied arguments, it should return ``NotImplemented``.\n\nobject.__radd__(self, other)\nobject.__rsub__(self, other)\nobject.__rmul__(self, other)\nobject.__rtruediv__(self, other)\nobject.__rfloordiv__(self, other)\nobject.__rmod__(self, other)\nobject.__rdivmod__(self, other)\nobject.__rpow__(self, other)\nobject.__rlshift__(self, other)\nobject.__rrshift__(self, other)\nobject.__rand__(self, other)\nobject.__rxor__(self, other)\nobject.__ror__(self, other)\n\n These methods are called to implement the binary arithmetic\n operations (``+``, ``-``, ``*``, ``/``, ``//``, ``%``,\n ``divmod()``, ``pow()``, ``**``, ``<<``, ``>>``, ``&``, ``^``,\n ``|``) with reflected (swapped) operands. These functions are only\n called if the left operand does not support the corresponding\n operation and the operands are of different types. [2] For\n instance, to evaluate the expression ``x - y``, where *y* is an\n instance of a class that has an ``__rsub__()`` method,\n ``y.__rsub__(x)`` is called if ``x.__sub__(y)`` returns\n *NotImplemented*.\n\n Note that ternary ``pow()`` will not try calling ``__rpow__()``\n (the coercion rules would become too complicated).\n\n Note: If the right operand\'s type is a subclass of the left operand\'s\n type and that subclass provides the reflected method for the\n operation, this method will be called before the left operand\'s\n non-reflected method. This behavior allows subclasses to\n override their ancestors\' operations.\n\nobject.__iadd__(self, other)\nobject.__isub__(self, other)\nobject.__imul__(self, other)\nobject.__itruediv__(self, other)\nobject.__ifloordiv__(self, other)\nobject.__imod__(self, other)\nobject.__ipow__(self, other[, modulo])\nobject.__ilshift__(self, other)\nobject.__irshift__(self, other)\nobject.__iand__(self, other)\nobject.__ixor__(self, other)\nobject.__ior__(self, other)\n\n These methods are called to implement the augmented arithmetic\n assignments (``+=``, ``-=``, ``*=``, ``/=``, ``//=``, ``%=``,\n ``**=``, ``<<=``, ``>>=``, ``&=``, ``^=``, ``|=``). These methods\n should attempt to do the operation in-place (modifying *self*) and\n return the result (which could be, but does not have to be,\n *self*). If a specific method is not defined, the augmented\n assignment falls back to the normal methods. For instance, to\n execute the statement ``x += y``, where *x* is an instance of a\n class that has an ``__iadd__()`` method, ``x.__iadd__(y)`` is\n called. If *x* is an instance of a class that does not define a\n ``__iadd__()`` method, ``x.__add__(y)`` and ``y.__radd__(x)`` are\n considered, as with the evaluation of ``x + y``.\n\nobject.__neg__(self)\nobject.__pos__(self)\nobject.__abs__(self)\nobject.__invert__(self)\n\n Called to implement the unary arithmetic operations (``-``, ``+``,\n ``abs()`` and ``~``).\n\nobject.__complex__(self)\nobject.__int__(self)\nobject.__float__(self)\nobject.__round__(self[, n])\n\n Called to implement the built-in functions ``complex()``,\n ``int()``, ``float()`` and ``round()``. Should return a value of\n the appropriate type.\n\nobject.__index__(self)\n\n Called to implement ``operator.index()``. Also called whenever\n Python needs an integer object (such as in slicing, or in the\n built-in ``bin()``, ``hex()`` and ``oct()`` functions). Must return\n an integer.\n\n\nWith Statement Context Managers\n===============================\n\nA *context manager* is an object that defines the runtime context to\nbe established when executing a ``with`` statement. The context\nmanager handles the entry into, and the exit from, the desired runtime\ncontext for the execution of the block of code. Context managers are\nnormally invoked using the ``with`` statement (described in section\n*The with statement*), but can also be used by directly invoking their\nmethods.\n\nTypical uses of context managers include saving and restoring various\nkinds of global state, locking and unlocking resources, closing opened\nfiles, etc.\n\nFor more information on context managers, see *Context Manager Types*.\n\nobject.__enter__(self)\n\n Enter the runtime context related to this object. The ``with``\n statement will bind this method\'s return value to the target(s)\n specified in the ``as`` clause of the statement, if any.\n\nobject.__exit__(self, exc_type, exc_value, traceback)\n\n Exit the runtime context related to this object. The parameters\n describe the exception that caused the context to be exited. If the\n context was exited without an exception, all three arguments will\n be ``None``.\n\n If an exception is supplied, and the method wishes to suppress the\n exception (i.e., prevent it from being propagated), it should\n return a true value. Otherwise, the exception will be processed\n normally upon exit from this method.\n\n Note that ``__exit__()`` methods should not reraise the passed-in\n exception; this is the caller\'s responsibility.\n\nSee also:\n\n **PEP 0343** - The "with" statement\n The specification, background, and examples for the Python\n ``with`` statement.\n\n\nSpecial method lookup\n=====================\n\nFor custom classes, implicit invocations of special methods are only\nguaranteed to work correctly if defined on an object\'s type, not in\nthe object\'s instance dictionary. That behaviour is the reason why\nthe following code raises an exception:\n\n >>> class C:\n ... pass\n ...\n >>> c = C()\n >>> c.__len__ = lambda: 5\n >>> len(c)\n Traceback (most recent call last):\n File "", line 1, in \n TypeError: object of type \'C\' has no len()\n\nThe rationale behind this behaviour lies with a number of special\nmethods such as ``__hash__()`` and ``__repr__()`` that are implemented\nby all objects, including type objects. If the implicit lookup of\nthese methods used the conventional lookup process, they would fail\nwhen invoked on the type object itself:\n\n >>> 1 .__hash__() == hash(1)\n True\n >>> int.__hash__() == hash(int)\n Traceback (most recent call last):\n File "", line 1, in \n TypeError: descriptor \'__hash__\' of \'int\' object needs an argument\n\nIncorrectly attempting to invoke an unbound method of a class in this\nway is sometimes referred to as \'metaclass confusion\', and is avoided\nby bypassing the instance when looking up special methods:\n\n >>> type(1).__hash__(1) == hash(1)\n True\n >>> type(int).__hash__(int) == hash(int)\n True\n\nIn addition to bypassing any instance attributes in the interest of\ncorrectness, implicit special method lookup generally also bypasses\nthe ``__getattribute__()`` method even of the object\'s metaclass:\n\n >>> class Meta(type):\n ... def __getattribute__(*args):\n ... print("Metaclass getattribute invoked")\n ... return type.__getattribute__(*args)\n ...\n >>> class C(object, metaclass=Meta):\n ... def __len__(self):\n ... return 10\n ... def __getattribute__(*args):\n ... print("Class getattribute invoked")\n ... return object.__getattribute__(*args)\n ...\n >>> c = C()\n >>> c.__len__() # Explicit lookup via instance\n Class getattribute invoked\n 10\n >>> type(c).__len__(c) # Explicit lookup via type\n Metaclass getattribute invoked\n 10\n >>> len(c) # Implicit lookup\n 10\n\nBypassing the ``__getattribute__()`` machinery in this fashion\nprovides significant scope for speed optimisations within the\ninterpreter, at the cost of some flexibility in the handling of\nspecial methods (the special method *must* be set on the class object\nitself in order to be consistently invoked by the interpreter).\n\n-[ Footnotes ]-\n\n[1] It *is* possible in some cases to change an object\'s type, under\n certain controlled conditions. It generally isn\'t a good idea\n though, since it can lead to some very strange behaviour if it is\n handled incorrectly.\n\n[2] For operands of the same type, it is assumed that if the non-\n reflected method (such as ``__add__()``) fails the operation is\n not supported, which is why the reflected method is not called.\n', + 'string-methods': '\nString Methods\n**************\n\nString objects support the methods listed below.\n\nIn addition, Python\'s strings support the sequence type methods\ndescribed in the *Sequence Types --- str, bytes, bytearray, list,\ntuple, range* section. To output formatted strings, see the *String\nFormatting* section. Also, see the ``re`` module for string functions\nbased on regular expressions.\n\nstr.capitalize()\n\n Return a copy of the string with its first character capitalized\n and the rest lowercased.\n\nstr.casefold()\n\n Return a casefolded copy of the string. Casefolded strings may be\n used for caseless matching.\n\n Casefolding is similar to lowercasing but more aggressive because\n it is intended to remove all case distinctions in a string. For\n example, the German lowercase letter ``\'\xc3\x9f\'`` is equivalent to\n ``"ss"``. Since it is already lowercase, ``lower()`` would do\n nothing to ``\'\xc3\x9f\'``; ``casefold()`` converts it to ``"ss"``.\n\n The casefolding algorithm is described in section 3.13 of the\n Unicode Standard.\n\n New in version 3.3.\n\nstr.center(width[, fillchar])\n\n Return centered in a string of length *width*. Padding is done\n using the specified *fillchar* (default is a space).\n\nstr.count(sub[, start[, end]])\n\n Return the number of non-overlapping occurrences of substring *sub*\n in the range [*start*, *end*]. Optional arguments *start* and\n *end* are interpreted as in slice notation.\n\nstr.encode(encoding="utf-8", errors="strict")\n\n Return an encoded version of the string as a bytes object. Default\n encoding is ``\'utf-8\'``. *errors* may be given to set a different\n error handling scheme. The default for *errors* is ``\'strict\'``,\n meaning that encoding errors raise a ``UnicodeError``. Other\n possible values are ``\'ignore\'``, ``\'replace\'``,\n ``\'xmlcharrefreplace\'``, ``\'backslashreplace\'`` and any other name\n registered via ``codecs.register_error()``, see section *Codec Base\n Classes*. For a list of possible encodings, see section *Standard\n Encodings*.\n\n Changed in version 3.1: Support for keyword arguments added.\n\nstr.endswith(suffix[, start[, end]])\n\n Return ``True`` if the string ends with the specified *suffix*,\n otherwise return ``False``. *suffix* can also be a tuple of\n suffixes to look for. With optional *start*, test beginning at\n that position. With optional *end*, stop comparing at that\n position.\n\nstr.expandtabs([tabsize])\n\n Return a copy of the string where all tab characters are replaced\n by zero or more spaces, depending on the current column and the\n given tab size. The column number is reset to zero after each\n newline occurring in the string. If *tabsize* is not given, a tab\n size of ``8`` characters is assumed. This doesn\'t understand other\n non-printing characters or escape sequences.\n\nstr.find(sub[, start[, end]])\n\n Return the lowest index in the string where substring *sub* is\n found, such that *sub* is contained in the slice ``s[start:end]``.\n Optional arguments *start* and *end* are interpreted as in slice\n notation. Return ``-1`` if *sub* is not found.\n\n Note: The ``find()`` method should be used only if you need to know the\n position of *sub*. To check if *sub* is a substring or not, use\n the ``in`` operator:\n\n >>> \'Py\' in \'Python\'\n True\n\nstr.format(*args, **kwargs)\n\n Perform a string formatting operation. The string on which this\n method is called can contain literal text or replacement fields\n delimited by braces ``{}``. Each replacement field contains either\n the numeric index of a positional argument, or the name of a\n keyword argument. Returns a copy of the string where each\n replacement field is replaced with the string value of the\n corresponding argument.\n\n >>> "The sum of 1 + 2 is {0}".format(1+2)\n \'The sum of 1 + 2 is 3\'\n\n See *Format String Syntax* for a description of the various\n formatting options that can be specified in format strings.\n\nstr.format_map(mapping)\n\n Similar to ``str.format(**mapping)``, except that ``mapping`` is\n used directly and not copied to a ``dict`` . This is useful if for\n example ``mapping`` is a dict subclass:\n\n >>> class Default(dict):\n ... def __missing__(self, key):\n ... return key\n ...\n >>> \'{name} was born in {country}\'.format_map(Default(name=\'Guido\'))\n \'Guido was born in country\'\n\n New in version 3.2.\n\nstr.index(sub[, start[, end]])\n\n Like ``find()``, but raise ``ValueError`` when the substring is not\n found.\n\nstr.isalnum()\n\n Return true if all characters in the string are alphanumeric and\n there is at least one character, false otherwise. A character\n ``c`` is alphanumeric if one of the following returns ``True``:\n ``c.isalpha()``, ``c.isdecimal()``, ``c.isdigit()``, or\n ``c.isnumeric()``.\n\nstr.isalpha()\n\n Return true if all characters in the string are alphabetic and\n there is at least one character, false otherwise. Alphabetic\n characters are those characters defined in the Unicode character\n database as "Letter", i.e., those with general category property\n being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note that this is\n different from the "Alphabetic" property defined in the Unicode\n Standard.\n\nstr.isdecimal()\n\n Return true if all characters in the string are decimal characters\n and there is at least one character, false otherwise. Decimal\n characters are those from general category "Nd". This category\n includes digit characters, and all characters that can be used to\n form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.\n\nstr.isdigit()\n\n Return true if all characters in the string are digits and there is\n at least one character, false otherwise. Digits include decimal\n characters and digits that need special handling, such as the\n compatibility superscript digits. Formally, a digit is a character\n that has the property value Numeric_Type=Digit or\n Numeric_Type=Decimal.\n\nstr.isidentifier()\n\n Return true if the string is a valid identifier according to the\n language definition, section *Identifiers and keywords*.\n\nstr.islower()\n\n Return true if all cased characters [4] in the string are lowercase\n and there is at least one cased character, false otherwise.\n\nstr.isnumeric()\n\n Return true if all characters in the string are numeric characters,\n and there is at least one character, false otherwise. Numeric\n characters include digit characters, and all characters that have\n the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION\n ONE FIFTH. Formally, numeric characters are those with the\n property value Numeric_Type=Digit, Numeric_Type=Decimal or\n Numeric_Type=Numeric.\n\nstr.isprintable()\n\n Return true if all characters in the string are printable or the\n string is empty, false otherwise. Nonprintable characters are\n those characters defined in the Unicode character database as\n "Other" or "Separator", excepting the ASCII space (0x20) which is\n considered printable. (Note that printable characters in this\n context are those which should not be escaped when ``repr()`` is\n invoked on a string. It has no bearing on the handling of strings\n written to ``sys.stdout`` or ``sys.stderr``.)\n\nstr.isspace()\n\n Return true if there are only whitespace characters in the string\n and there is at least one character, false otherwise. Whitespace\n characters are those characters defined in the Unicode character\n database as "Other" or "Separator" and those with bidirectional\n property being one of "WS", "B", or "S".\n\nstr.istitle()\n\n Return true if the string is a titlecased string and there is at\n least one character, for example uppercase characters may only\n follow uncased characters and lowercase characters only cased ones.\n Return false otherwise.\n\nstr.isupper()\n\n Return true if all cased characters [4] in the string are uppercase\n and there is at least one cased character, false otherwise.\n\nstr.join(iterable)\n\n Return a string which is the concatenation of the strings in the\n *iterable* *iterable*. A ``TypeError`` will be raised if there are\n any non-string values in *iterable*, including ``bytes`` objects.\n The separator between elements is the string providing this method.\n\nstr.ljust(width[, fillchar])\n\n Return the string left justified in a string of length *width*.\n Padding is done using the specified *fillchar* (default is a\n space). The original string is returned if *width* is less than or\n equal to ``len(s)``.\n\nstr.lower()\n\n Return a copy of the string with all the cased characters [4]\n converted to lowercase.\n\n The lowercasing algorithm used is described in section 3.13 of the\n Unicode Standard.\n\nstr.lstrip([chars])\n\n Return a copy of the string with leading characters removed. The\n *chars* argument is a string specifying the set of characters to be\n removed. If omitted or ``None``, the *chars* argument defaults to\n removing whitespace. The *chars* argument is not a prefix; rather,\n all combinations of its values are stripped:\n\n >>> \' spacious \'.lstrip()\n \'spacious \'\n >>> \'www.example.com\'.lstrip(\'cmowz.\')\n \'example.com\'\n\nstatic str.maketrans(x[, y[, z]])\n\n This static method returns a translation table usable for\n ``str.translate()``.\n\n If there is only one argument, it must be a dictionary mapping\n Unicode ordinals (integers) or characters (strings of length 1) to\n Unicode ordinals, strings (of arbitrary lengths) or None.\n Character keys will then be converted to ordinals.\n\n If there are two arguments, they must be strings of equal length,\n and in the resulting dictionary, each character in x will be mapped\n to the character at the same position in y. If there is a third\n argument, it must be a string, whose characters will be mapped to\n None in the result.\n\nstr.partition(sep)\n\n Split the string at the first occurrence of *sep*, and return a\n 3-tuple containing the part before the separator, the separator\n itself, and the part after the separator. If the separator is not\n found, return a 3-tuple containing the string itself, followed by\n two empty strings.\n\nstr.replace(old, new[, count])\n\n Return a copy of the string with all occurrences of substring *old*\n replaced by *new*. If the optional argument *count* is given, only\n the first *count* occurrences are replaced.\n\nstr.rfind(sub[, start[, end]])\n\n Return the highest index in the string where substring *sub* is\n found, such that *sub* is contained within ``s[start:end]``.\n Optional arguments *start* and *end* are interpreted as in slice\n notation. Return ``-1`` on failure.\n\nstr.rindex(sub[, start[, end]])\n\n Like ``rfind()`` but raises ``ValueError`` when the substring *sub*\n is not found.\n\nstr.rjust(width[, fillchar])\n\n Return the string right justified in a string of length *width*.\n Padding is done using the specified *fillchar* (default is a\n space). The original string is returned if *width* is less than or\n equal to ``len(s)``.\n\nstr.rpartition(sep)\n\n Split the string at the last occurrence of *sep*, and return a\n 3-tuple containing the part before the separator, the separator\n itself, and the part after the separator. If the separator is not\n found, return a 3-tuple containing two empty strings, followed by\n the string itself.\n\nstr.rsplit(sep=None, maxsplit=-1)\n\n Return a list of the words in the string, using *sep* as the\n delimiter string. If *maxsplit* is given, at most *maxsplit* splits\n are done, the *rightmost* ones. If *sep* is not specified or\n ``None``, any whitespace string is a separator. Except for\n splitting from the right, ``rsplit()`` behaves like ``split()``\n which is described in detail below.\n\nstr.rstrip([chars])\n\n Return a copy of the string with trailing characters removed. The\n *chars* argument is a string specifying the set of characters to be\n removed. If omitted or ``None``, the *chars* argument defaults to\n removing whitespace. The *chars* argument is not a suffix; rather,\n all combinations of its values are stripped:\n\n >>> \' spacious \'.rstrip()\n \' spacious\'\n >>> \'mississippi\'.rstrip(\'ipz\')\n \'mississ\'\n\nstr.split(sep=None, maxsplit=-1)\n\n Return a list of the words in the string, using *sep* as the\n delimiter string. If *maxsplit* is given, at most *maxsplit*\n splits are done (thus, the list will have at most ``maxsplit+1``\n elements). If *maxsplit* is not specified, then there is no limit\n on the number of splits (all possible splits are made).\n\n If *sep* is given, consecutive delimiters are not grouped together\n and are deemed to delimit empty strings (for example,\n ``\'1,,2\'.split(\',\')`` returns ``[\'1\', \'\', \'2\']``). The *sep*\n argument may consist of multiple characters (for example,\n ``\'1<>2<>3\'.split(\'<>\')`` returns ``[\'1\', \'2\', \'3\']``). Splitting\n an empty string with a specified separator returns ``[\'\']``.\n\n If *sep* is not specified or is ``None``, a different splitting\n algorithm is applied: runs of consecutive whitespace are regarded\n as a single separator, and the result will contain no empty strings\n at the start or end if the string has leading or trailing\n whitespace. Consequently, splitting an empty string or a string\n consisting of just whitespace with a ``None`` separator returns\n ``[]``.\n\n For example, ``\' 1 2 3 \'.split()`` returns ``[\'1\', \'2\', \'3\']``,\n and ``\' 1 2 3 \'.split(None, 1)`` returns ``[\'1\', \'2 3 \']``.\n\nstr.splitlines([keepends])\n\n Return a list of the lines in the string, breaking at line\n boundaries. Line breaks are not included in the resulting list\n unless *keepends* is given and true.\n\nstr.startswith(prefix[, start[, end]])\n\n Return ``True`` if string starts with the *prefix*, otherwise\n return ``False``. *prefix* can also be a tuple of prefixes to look\n for. With optional *start*, test string beginning at that\n position. With optional *end*, stop comparing string at that\n position.\n\nstr.strip([chars])\n\n Return a copy of the string with the leading and trailing\n characters removed. The *chars* argument is a string specifying the\n set of characters to be removed. If omitted or ``None``, the\n *chars* argument defaults to removing whitespace. The *chars*\n argument is not a prefix or suffix; rather, all combinations of its\n values are stripped:\n\n >>> \' spacious \'.strip()\n \'spacious\'\n >>> \'www.example.com\'.strip(\'cmowz.\')\n \'example\'\n\nstr.swapcase()\n\n Return a copy of the string with uppercase characters converted to\n lowercase and vice versa. Note that it is not necessarily true that\n ``s.swapcase().swapcase() == s``.\n\nstr.title()\n\n Return a titlecased version of the string where words start with an\n uppercase character and the remaining characters are lowercase.\n\n The algorithm uses a simple language-independent definition of a\n word as groups of consecutive letters. The definition works in\n many contexts but it means that apostrophes in contractions and\n possessives form word boundaries, which may not be the desired\n result:\n\n >>> "they\'re bill\'s friends from the UK".title()\n "They\'Re Bill\'S Friends From The Uk"\n\n A workaround for apostrophes can be constructed using regular\n expressions:\n\n >>> import re\n >>> def titlecase(s):\n return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n lambda mo: mo.group(0)[0].upper() +\n mo.group(0)[1:].lower(),\n s)\n\n >>> titlecase("they\'re bill\'s friends.")\n "They\'re Bill\'s Friends."\n\nstr.translate(map)\n\n Return a copy of the *s* where all characters have been mapped\n through the *map* which must be a dictionary of Unicode ordinals\n (integers) to Unicode ordinals, strings or ``None``. Unmapped\n characters are left untouched. Characters mapped to ``None`` are\n deleted.\n\n You can use ``str.maketrans()`` to create a translation map from\n character-to-character mappings in different formats.\n\n Note: An even more flexible approach is to create a custom character\n mapping codec using the ``codecs`` module (see\n ``encodings.cp1251`` for an example).\n\nstr.upper()\n\n Return a copy of the string with all the cased characters [4]\n converted to uppercase. Note that ``str.upper().isupper()`` might\n be ``False`` if ``s`` contains uncased characters or if the Unicode\n category of the resulting character(s) is not "Lu" (Letter,\n uppercase), but e.g. "Lt" (Letter, titlecase).\n\n The uppercasing algorithm used is described in section 3.13 of the\n Unicode Standard.\n\nstr.zfill(width)\n\n Return the numeric string left filled with zeros in a string of\n length *width*. A sign prefix is handled correctly. The original\n string is returned if *width* is less than or equal to ``len(s)``.\n', 'strings': '\nString and Bytes literals\n*************************\n\nString literals are described by the following lexical definitions:\n\n stringliteral ::= [stringprefix](shortstring | longstring)\n stringprefix ::= "r" | "u" | "ur" | "R" | "U" | "UR" | "Ur" | "uR"\n shortstring ::= "\'" shortstringitem* "\'" | \'"\' shortstringitem* \'"\'\n longstring ::= "\'\'\'" longstringitem* "\'\'\'" | \'"""\' longstringitem* \'"""\'\n shortstringitem ::= shortstringchar | stringescapeseq\n longstringitem ::= longstringchar | stringescapeseq\n shortstringchar ::= \n longstringchar ::= \n stringescapeseq ::= "\\" \n\n bytesliteral ::= bytesprefix(shortbytes | longbytes)\n bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB"\n shortbytes ::= "\'" shortbytesitem* "\'" | \'"\' shortbytesitem* \'"\'\n longbytes ::= "\'\'\'" longbytesitem* "\'\'\'" | \'"""\' longbytesitem* \'"""\'\n shortbytesitem ::= shortbyteschar | bytesescapeseq\n longbytesitem ::= longbyteschar | bytesescapeseq\n shortbyteschar ::= \n longbyteschar ::= \n bytesescapeseq ::= "\\" \n\nOne syntactic restriction not indicated by these productions is that\nwhitespace is not allowed between the ``stringprefix`` or\n``bytesprefix`` and the rest of the literal. The source character set\nis defined by the encoding declaration; it is UTF-8 if no encoding\ndeclaration is given in the source file; see section *Encoding\ndeclarations*.\n\nIn plain English: Both types of literals can be enclosed in matching\nsingle quotes (``\'``) or double quotes (``"``). They can also be\nenclosed in matching groups of three single or double quotes (these\nare generally referred to as *triple-quoted strings*). The backslash\n(``\\``) character is used to escape characters that otherwise have a\nspecial meaning, such as newline, backslash itself, or the quote\ncharacter.\n\nBytes literals are always prefixed with ``\'b\'`` or ``\'B\'``; they\nproduce an instance of the ``bytes`` type instead of the ``str`` type.\nThey may only contain ASCII characters; bytes with a numeric value of\n128 or greater must be expressed with escapes.\n\nAs of Python 3.3 it is possible again to prefix unicode strings with a\n``u`` prefix to simplify maintenance of dual 2.x and 3.x codebases.\n\nBoth string and bytes literals may optionally be prefixed with a\nletter ``\'r\'`` or ``\'R\'``; such strings are called *raw strings* and\ntreat backslashes as literal characters. As a result, in string\nliterals, ``\'\\U\'`` and ``\'\\u\'`` escapes in raw strings are not treated\nspecially.\n\n New in version 3.3: The ``\'rb\'`` prefix of raw bytes literals has\n been added as a synonym of ``\'br\'``.\n\n New in version 3.3: Support for the unicode legacy literal\n (``u\'value\'``) and other versions were reintroduced to simplify the\n maintenance of dual Python 2.x and 3.x codebases. See **PEP 414**\n for more information.\n\nIn triple-quoted strings, unescaped newlines and quotes are allowed\n(and are retained), except that three unescaped quotes in a row\nterminate the string. (A "quote" is the character used to open the\nstring, i.e. either ``\'`` or ``"``.)\n\nUnless an ``\'r\'`` or ``\'R\'`` prefix is present, escape sequences in\nstrings are interpreted according to rules similar to those used by\nStandard C. The recognized escape sequences are:\n\n+-------------------+-----------------------------------+---------+\n| Escape Sequence | Meaning | Notes |\n+===================+===================================+=========+\n| ``\\newline`` | Backslash and newline ignored | |\n+-------------------+-----------------------------------+---------+\n| ``\\\\`` | Backslash (``\\``) | |\n+-------------------+-----------------------------------+---------+\n| ``\\\'`` | Single quote (``\'``) | |\n+-------------------+-----------------------------------+---------+\n| ``\\"`` | Double quote (``"``) | |\n+-------------------+-----------------------------------+---------+\n| ``\\a`` | ASCII Bell (BEL) | |\n+-------------------+-----------------------------------+---------+\n| ``\\b`` | ASCII Backspace (BS) | |\n+-------------------+-----------------------------------+---------+\n| ``\\f`` | ASCII Formfeed (FF) | |\n+-------------------+-----------------------------------+---------+\n| ``\\n`` | ASCII Linefeed (LF) | |\n+-------------------+-----------------------------------+---------+\n| ``\\r`` | ASCII Carriage Return (CR) | |\n+-------------------+-----------------------------------+---------+\n| ``\\t`` | ASCII Horizontal Tab (TAB) | |\n+-------------------+-----------------------------------+---------+\n| ``\\v`` | ASCII Vertical Tab (VT) | |\n+-------------------+-----------------------------------+---------+\n| ``\\ooo`` | Character with octal value *ooo* | (1,3) |\n+-------------------+-----------------------------------+---------+\n| ``\\xhh`` | Character with hex value *hh* | (2,3) |\n+-------------------+-----------------------------------+---------+\n\nEscape sequences only recognized in string literals are:\n\n+-------------------+-----------------------------------+---------+\n| Escape Sequence | Meaning | Notes |\n+===================+===================================+=========+\n| ``\\N{name}`` | Character named *name* in the | (4) |\n| | Unicode database | |\n+-------------------+-----------------------------------+---------+\n| ``\\uxxxx`` | Character with 16-bit hex value | (5) |\n| | *xxxx* | |\n+-------------------+-----------------------------------+---------+\n| ``\\Uxxxxxxxx`` | Character with 32-bit hex value | (6) |\n| | *xxxxxxxx* | |\n+-------------------+-----------------------------------+---------+\n\nNotes:\n\n1. As in Standard C, up to three octal digits are accepted.\n\n2. Unlike in Standard C, exactly two hex digits are required.\n\n3. In a bytes literal, hexadecimal and octal escapes denote the byte\n with the given value. In a string literal, these escapes denote a\n Unicode character with the given value.\n\n4. Changed in version 3.3: Support for name aliases [1] has been\n added.\n\n5. Individual code units which form parts of a surrogate pair can be\n encoded using this escape sequence. Exactly four hex digits are\n required.\n\n6. Any Unicode character can be encoded this way, but characters\n outside the Basic Multilingual Plane (BMP) will be encoded using a\n surrogate pair if Python is compiled to use 16-bit code units (the\n default). Exactly eight hex digits are required.\n\nUnlike Standard C, all unrecognized escape sequences are left in the\nstring unchanged, i.e., *the backslash is left in the string*. (This\nbehavior is useful when debugging: if an escape sequence is mistyped,\nthe resulting output is more easily recognized as broken.) It is also\nimportant to note that the escape sequences only recognized in string\nliterals fall into the category of unrecognized escapes for bytes\nliterals.\n\nEven in a raw string, string quotes can be escaped with a backslash,\nbut the backslash remains in the string; for example, ``r"\\""`` is a\nvalid string literal consisting of two characters: a backslash and a\ndouble quote; ``r"\\"`` is not a valid string literal (even a raw\nstring cannot end in an odd number of backslashes). Specifically, *a\nraw string cannot end in a single backslash* (since the backslash\nwould escape the following quote character). Note also that a single\nbackslash followed by a newline is interpreted as those two characters\nas part of the string, *not* as a line continuation.\n', 'subscriptions': '\nSubscriptions\n*************\n\nA subscription selects an item of a sequence (string, tuple or list)\nor mapping (dictionary) object:\n\n subscription ::= primary "[" expression_list "]"\n\nThe primary must evaluate to an object that supports subscription,\ne.g. a list or dictionary. User-defined objects can support\nsubscription by defining a ``__getitem__()`` method.\n\nFor built-in objects, there are two types of objects that support\nsubscription:\n\nIf the primary is a mapping, the expression list must evaluate to an\nobject whose value is one of the keys of the mapping, and the\nsubscription selects the value in the mapping that corresponds to that\nkey. (The expression list is a tuple except if it has exactly one\nitem.)\n\nIf the primary is a sequence, the expression (list) must evaluate to\nan integer or a slice (as discussed in the following section).\n\nThe formal syntax makes no special provision for negative indices in\nsequences; however, built-in sequences all provide a ``__getitem__()``\nmethod that interprets negative indices by adding the length of the\nsequence to the index (so that ``x[-1]`` selects the last item of\n``x``). The resulting value must be a nonnegative integer less than\nthe number of items in the sequence, and the subscription selects the\nitem whose index is that value (counting from zero). Since the support\nfor negative indices and slicing occurs in the object\'s\n``__getitem__()`` method, subclasses overriding this method will need\nto explicitly add that support.\n\nA string\'s items are characters. A character is not a separate data\ntype but a string of exactly one character.\n', 'truth': "\nTruth Value Testing\n*******************\n\nAny object can be tested for truth value, for use in an ``if`` or\n``while`` condition or as operand of the Boolean operations below. The\nfollowing values are considered false:\n\n* ``None``\n\n* ``False``\n\n* zero of any numeric type, for example, ``0``, ``0.0``, ``0j``.\n\n* any empty sequence, for example, ``''``, ``()``, ``[]``.\n\n* any empty mapping, for example, ``{}``.\n\n* instances of user-defined classes, if the class defines a\n ``__bool__()`` or ``__len__()`` method, when that method returns the\n integer zero or ``bool`` value ``False``. [1]\n\nAll other values are considered true --- so objects of many types are\nalways true.\n\nOperations and built-in functions that have a Boolean result always\nreturn ``0`` or ``False`` for false and ``1`` or ``True`` for true,\nunless otherwise stated. (Important exception: the Boolean operations\n``or`` and ``and`` always return one of their operands.)\n", 'try': '\nThe ``try`` statement\n*********************\n\nThe ``try`` statement specifies exception handlers and/or cleanup code\nfor a group of statements:\n\n try_stmt ::= try1_stmt | try2_stmt\n try1_stmt ::= "try" ":" suite\n ("except" [expression ["as" target]] ":" suite)+\n ["else" ":" suite]\n ["finally" ":" suite]\n try2_stmt ::= "try" ":" suite\n "finally" ":" suite\n\nThe ``except`` clause(s) specify one or more exception handlers. When\nno exception occurs in the ``try`` clause, no exception handler is\nexecuted. When an exception occurs in the ``try`` suite, a search for\nan exception handler is started. This search inspects the except\nclauses in turn until one is found that matches the exception. An\nexpression-less except clause, if present, must be last; it matches\nany exception. For an except clause with an expression, that\nexpression is evaluated, and the clause matches the exception if the\nresulting object is "compatible" with the exception. An object is\ncompatible with an exception if it is the class or a base class of the\nexception object or a tuple containing an item compatible with the\nexception.\n\nIf no except clause matches the exception, the search for an exception\nhandler continues in the surrounding code and on the invocation stack.\n[1]\n\nIf the evaluation of an expression in the header of an except clause\nraises an exception, the original search for a handler is canceled and\na search starts for the new exception in the surrounding code and on\nthe call stack (it is treated as if the entire ``try`` statement\nraised the exception).\n\nWhen a matching except clause is found, the exception is assigned to\nthe target specified after the ``as`` keyword in that except clause,\nif present, and the except clause\'s suite is executed. All except\nclauses must have an executable block. When the end of this block is\nreached, execution continues normally after the entire try statement.\n(This means that if two nested handlers exist for the same exception,\nand the exception occurs in the try clause of the inner handler, the\nouter handler will not handle the exception.)\n\nWhen an exception has been assigned using ``as target``, it is cleared\nat the end of the except clause. This is as if\n\n except E as N:\n foo\n\nwas translated to\n\n except E as N:\n try:\n foo\n finally:\n del N\n\nThis means the exception must be assigned to a different name to be\nable to refer to it after the except clause. Exceptions are cleared\nbecause with the traceback attached to them, they form a reference\ncycle with the stack frame, keeping all locals in that frame alive\nuntil the next garbage collection occurs.\n\nBefore an except clause\'s suite is executed, details about the\nexception are stored in the ``sys`` module and can be access via\n``sys.exc_info()``. ``sys.exc_info()`` returns a 3-tuple consisting of\nthe exception class, the exception instance and a traceback object\n(see section *The standard type hierarchy*) identifying the point in\nthe program where the exception occurred. ``sys.exc_info()`` values\nare restored to their previous values (before the call) when returning\nfrom a function that handled an exception.\n\nThe optional ``else`` clause is executed if and when control flows off\nthe end of the ``try`` clause. [2] Exceptions in the ``else`` clause\nare not handled by the preceding ``except`` clauses.\n\nIf ``finally`` is present, it specifies a \'cleanup\' handler. The\n``try`` clause is executed, including any ``except`` and ``else``\nclauses. If an exception occurs in any of the clauses and is not\nhandled, the exception is temporarily saved. The ``finally`` clause is\nexecuted. If there is a saved exception, it is re-raised at the end\nof the ``finally`` clause. If the ``finally`` clause raises another\nexception or executes a ``return`` or ``break`` statement, the saved\nexception is set as the context of the new exception. The exception\ninformation is not available to the program during execution of the\n``finally`` clause.\n\nWhen a ``return``, ``break`` or ``continue`` statement is executed in\nthe ``try`` suite of a ``try``...``finally`` statement, the\n``finally`` clause is also executed \'on the way out.\' A ``continue``\nstatement is illegal in the ``finally`` clause. (The reason is a\nproblem with the current implementation --- this restriction may be\nlifted in the future).\n\nAdditional information on exceptions can be found in section\n*Exceptions*, and information on using the ``raise`` statement to\ngenerate exceptions may be found in section *The raise statement*.\n', 'types': '\nThe standard type hierarchy\n***************************\n\nBelow is a list of the types that are built into Python. Extension\nmodules (written in C, Java, or other languages, depending on the\nimplementation) can define additional types. Future versions of\nPython may add types to the type hierarchy (e.g., rational numbers,\nefficiently stored arrays of integers, etc.), although such additions\nwill often be provided via the standard library instead.\n\nSome of the type descriptions below contain a paragraph listing\n\'special attributes.\' These are attributes that provide access to the\nimplementation and are not intended for general use. Their definition\nmay change in the future.\n\nNone\n This type has a single value. There is a single object with this\n value. This object is accessed through the built-in name ``None``.\n It is used to signify the absence of a value in many situations,\n e.g., it is returned from functions that don\'t explicitly return\n anything. Its truth value is false.\n\nNotImplemented\n This type has a single value. There is a single object with this\n value. This object is accessed through the built-in name\n ``NotImplemented``. Numeric methods and rich comparison methods may\n return this value if they do not implement the operation for the\n operands provided. (The interpreter will then try the reflected\n operation, or some other fallback, depending on the operator.) Its\n truth value is true.\n\nEllipsis\n This type has a single value. There is a single object with this\n value. This object is accessed through the literal ``...`` or the\n built-in name ``Ellipsis``. Its truth value is true.\n\n``numbers.Number``\n These are created by numeric literals and returned as results by\n arithmetic operators and arithmetic built-in functions. Numeric\n objects are immutable; once created their value never changes.\n Python numbers are of course strongly related to mathematical\n numbers, but subject to the limitations of numerical representation\n in computers.\n\n Python distinguishes between integers, floating point numbers, and\n complex numbers:\n\n ``numbers.Integral``\n These represent elements from the mathematical set of integers\n (positive and negative).\n\n There are two types of integers:\n\n Integers (``int``)\n\n These represent numbers in an unlimited range, subject to\n available (virtual) memory only. For the purpose of shift\n and mask operations, a binary representation is assumed, and\n negative numbers are represented in a variant of 2\'s\n complement which gives the illusion of an infinite string of\n sign bits extending to the left.\n\n Booleans (``bool``)\n These represent the truth values False and True. The two\n objects representing the values False and True are the only\n Boolean objects. The Boolean type is a subtype of the integer\n type, and Boolean values behave like the values 0 and 1,\n respectively, in almost all contexts, the exception being\n that when converted to a string, the strings ``"False"`` or\n ``"True"`` are returned, respectively.\n\n The rules for integer representation are intended to give the\n most meaningful interpretation of shift and mask operations\n involving negative integers.\n\n ``numbers.Real`` (``float``)\n These represent machine-level double precision floating point\n numbers. You are at the mercy of the underlying machine\n architecture (and C or Java implementation) for the accepted\n range and handling of overflow. Python does not support single-\n precision floating point numbers; the savings in processor and\n memory usage that are usually the reason for using these is\n dwarfed by the overhead of using objects in Python, so there is\n no reason to complicate the language with two kinds of floating\n point numbers.\n\n ``numbers.Complex`` (``complex``)\n These represent complex numbers as a pair of machine-level\n double precision floating point numbers. The same caveats apply\n as for floating point numbers. The real and imaginary parts of a\n complex number ``z`` can be retrieved through the read-only\n attributes ``z.real`` and ``z.imag``.\n\nSequences\n These represent finite ordered sets indexed by non-negative\n numbers. The built-in function ``len()`` returns the number of\n items of a sequence. When the length of a sequence is *n*, the\n index set contains the numbers 0, 1, ..., *n*-1. Item *i* of\n sequence *a* is selected by ``a[i]``.\n\n Sequences also support slicing: ``a[i:j]`` selects all items with\n index *k* such that *i* ``<=`` *k* ``<`` *j*. When used as an\n expression, a slice is a sequence of the same type. This implies\n that the index set is renumbered so that it starts at 0.\n\n Some sequences also support "extended slicing" with a third "step"\n parameter: ``a[i:j:k]`` selects all items of *a* with index *x*\n where ``x = i + n*k``, *n* ``>=`` ``0`` and *i* ``<=`` *x* ``<``\n *j*.\n\n Sequences are distinguished according to their mutability:\n\n Immutable sequences\n An object of an immutable sequence type cannot change once it is\n created. (If the object contains references to other objects,\n these other objects may be mutable and may be changed; however,\n the collection of objects directly referenced by an immutable\n object cannot change.)\n\n The following types are immutable sequences:\n\n Strings\n A string is a sequence of values that represent Unicode\n codepoints. All the codepoints in range ``U+0000 - U+10FFFF``\n can be represented in a string. Python doesn\'t have a\n ``chr`` type, and every character in the string is\n represented as a string object with length ``1``. The built-\n in function ``ord()`` converts a character to its codepoint\n (as an integer); ``chr()`` converts an integer in range ``0 -\n 10FFFF`` to the corresponding character. ``str.encode()`` can\n be used to convert a ``str`` to ``bytes`` using the given\n encoding, and ``bytes.decode()`` can be used to achieve the\n opposite.\n\n Tuples\n The items of a tuple are arbitrary Python objects. Tuples of\n two or more items are formed by comma-separated lists of\n expressions. A tuple of one item (a \'singleton\') can be\n formed by affixing a comma to an expression (an expression by\n itself does not create a tuple, since parentheses must be\n usable for grouping of expressions). An empty tuple can be\n formed by an empty pair of parentheses.\n\n Bytes\n A bytes object is an immutable array. The items are 8-bit\n bytes, represented by integers in the range 0 <= x < 256.\n Bytes literals (like ``b\'abc\'`` and the built-in function\n ``bytes()`` can be used to construct bytes objects. Also,\n bytes objects can be decoded to strings via the ``decode()``\n method.\n\n Mutable sequences\n Mutable sequences can be changed after they are created. The\n subscription and slicing notations can be used as the target of\n assignment and ``del`` (delete) statements.\n\n There are currently two intrinsic mutable sequence types:\n\n Lists\n The items of a list are arbitrary Python objects. Lists are\n formed by placing a comma-separated list of expressions in\n square brackets. (Note that there are no special cases needed\n to form lists of length 0 or 1.)\n\n Byte Arrays\n A bytearray object is a mutable array. They are created by\n the built-in ``bytearray()`` constructor. Aside from being\n mutable (and hence unhashable), byte arrays otherwise provide\n the same interface and functionality as immutable bytes\n objects.\n\n The extension module ``array`` provides an additional example of\n a mutable sequence type, as does the ``collections`` module.\n\nSet types\n These represent unordered, finite sets of unique, immutable\n objects. As such, they cannot be indexed by any subscript. However,\n they can be iterated over, and the built-in function ``len()``\n returns the number of items in a set. Common uses for sets are fast\n membership testing, removing duplicates from a sequence, and\n computing mathematical operations such as intersection, union,\n difference, and symmetric difference.\n\n For set elements, the same immutability rules apply as for\n dictionary keys. Note that numeric types obey the normal rules for\n numeric comparison: if two numbers compare equal (e.g., ``1`` and\n ``1.0``), only one of them can be contained in a set.\n\n There are currently two intrinsic set types:\n\n Sets\n These represent a mutable set. They are created by the built-in\n ``set()`` constructor and can be modified afterwards by several\n methods, such as ``add()``.\n\n Frozen sets\n These represent an immutable set. They are created by the\n built-in ``frozenset()`` constructor. As a frozenset is\n immutable and *hashable*, it can be used again as an element of\n another set, or as a dictionary key.\n\nMappings\n These represent finite sets of objects indexed by arbitrary index\n sets. The subscript notation ``a[k]`` selects the item indexed by\n ``k`` from the mapping ``a``; this can be used in expressions and\n as the target of assignments or ``del`` statements. The built-in\n function ``len()`` returns the number of items in a mapping.\n\n There is currently a single intrinsic mapping type:\n\n Dictionaries\n These represent finite sets of objects indexed by nearly\n arbitrary values. The only types of values not acceptable as\n keys are values containing lists or dictionaries or other\n mutable types that are compared by value rather than by object\n identity, the reason being that the efficient implementation of\n dictionaries requires a key\'s hash value to remain constant.\n Numeric types used for keys obey the normal rules for numeric\n comparison: if two numbers compare equal (e.g., ``1`` and\n ``1.0``) then they can be used interchangeably to index the same\n dictionary entry.\n\n Dictionaries are mutable; they can be created by the ``{...}``\n notation (see section *Dictionary displays*).\n\n The extension modules ``dbm.ndbm`` and ``dbm.gnu`` provide\n additional examples of mapping types, as does the\n ``collections`` module.\n\nCallable types\n These are the types to which the function call operation (see\n section *Calls*) can be applied:\n\n User-defined functions\n A user-defined function object is created by a function\n definition (see section *Function definitions*). It should be\n called with an argument list containing the same number of items\n as the function\'s formal parameter list.\n\n Special attributes:\n\n +---------------------------+---------------------------------+-------------+\n | Attribute | Meaning | |\n +===========================+=================================+=============+\n | ``__doc__`` | The function\'s documentation | Writable |\n | | string, or ``None`` if | |\n | | unavailable | |\n +---------------------------+---------------------------------+-------------+\n | ``__name__`` | The function\'s name | Writable |\n +---------------------------+---------------------------------+-------------+\n | ``__qualname__`` | The function\'s *qualified name* | Writable |\n | | New in version 3.3. | |\n +---------------------------+---------------------------------+-------------+\n | ``__module__`` | The name of the module the | Writable |\n | | function was defined in, or | |\n | | ``None`` if unavailable. | |\n +---------------------------+---------------------------------+-------------+\n | ``__defaults__`` | A tuple containing default | Writable |\n | | argument values for those | |\n | | arguments that have defaults, | |\n | | or ``None`` if no arguments | |\n | | have a default value | |\n +---------------------------+---------------------------------+-------------+\n | ``__code__`` | The code object representing | Writable |\n | | the compiled function body. | |\n +---------------------------+---------------------------------+-------------+\n | ``__globals__`` | A reference to the dictionary | Read-only |\n | | that holds the function\'s | |\n | | global variables --- the global | |\n | | namespace of the module in | |\n | | which the function was defined. | |\n +---------------------------+---------------------------------+-------------+\n | ``__dict__`` | The namespace supporting | Writable |\n | | arbitrary function attributes. | |\n +---------------------------+---------------------------------+-------------+\n | ``__closure__`` | ``None`` or a tuple of cells | Read-only |\n | | that contain bindings for the | |\n | | function\'s free variables. | |\n +---------------------------+---------------------------------+-------------+\n | ``__annotations__`` | A dict containing annotations | Writable |\n | | of parameters. The keys of the | |\n | | dict are the parameter names, | |\n | | or ``\'return\'`` for the return | |\n | | annotation, if provided. | |\n +---------------------------+---------------------------------+-------------+\n | ``__kwdefaults__`` | A dict containing defaults for | Writable |\n | | keyword-only parameters. | |\n +---------------------------+---------------------------------+-------------+\n\n Most of the attributes labelled "Writable" check the type of the\n assigned value.\n\n Function objects also support getting and setting arbitrary\n attributes, which can be used, for example, to attach metadata\n to functions. Regular attribute dot-notation is used to get and\n set such attributes. *Note that the current implementation only\n supports function attributes on user-defined functions. Function\n attributes on built-in functions may be supported in the\n future.*\n\n Additional information about a function\'s definition can be\n retrieved from its code object; see the description of internal\n types below.\n\n Instance methods\n An instance method object combines a class, a class instance and\n any callable object (normally a user-defined function).\n\n Special read-only attributes: ``__self__`` is the class instance\n object, ``__func__`` is the function object; ``__doc__`` is the\n method\'s documentation (same as ``__func__.__doc__``);\n ``__name__`` is the method name (same as ``__func__.__name__``);\n ``__module__`` is the name of the module the method was defined\n in, or ``None`` if unavailable.\n\n Methods also support accessing (but not setting) the arbitrary\n function attributes on the underlying function object.\n\n User-defined method objects may be created when getting an\n attribute of a class (perhaps via an instance of that class), if\n that attribute is a user-defined function object or a class\n method object.\n\n When an instance method object is created by retrieving a user-\n defined function object from a class via one of its instances,\n its ``__self__`` attribute is the instance, and the method\n object is said to be bound. The new method\'s ``__func__``\n attribute is the original function object.\n\n When a user-defined method object is created by retrieving\n another method object from a class or instance, the behaviour is\n the same as for a function object, except that the ``__func__``\n attribute of the new instance is not the original method object\n but its ``__func__`` attribute.\n\n When an instance method object is created by retrieving a class\n method object from a class or instance, its ``__self__``\n attribute is the class itself, and its ``__func__`` attribute is\n the function object underlying the class method.\n\n When an instance method object is called, the underlying\n function (``__func__``) is called, inserting the class instance\n (``__self__``) in front of the argument list. For instance,\n when ``C`` is a class which contains a definition for a function\n ``f()``, and ``x`` is an instance of ``C``, calling ``x.f(1)``\n is equivalent to calling ``C.f(x, 1)``.\n\n When an instance method object is derived from a class method\n object, the "class instance" stored in ``__self__`` will\n actually be the class itself, so that calling either ``x.f(1)``\n or ``C.f(1)`` is equivalent to calling ``f(C,1)`` where ``f`` is\n the underlying function.\n\n Note that the transformation from function object to instance\n method object happens each time the attribute is retrieved from\n the instance. In some cases, a fruitful optimization is to\n assign the attribute to a local variable and call that local\n variable. Also notice that this transformation only happens for\n user-defined functions; other callable objects (and all non-\n callable objects) are retrieved without transformation. It is\n also important to note that user-defined functions which are\n attributes of a class instance are not converted to bound\n methods; this *only* happens when the function is an attribute\n of the class.\n\n Generator functions\n A function or method which uses the ``yield`` statement (see\n section *The yield statement*) is called a *generator function*.\n Such a function, when called, always returns an iterator object\n which can be used to execute the body of the function: calling\n the iterator\'s ``__next__()`` method will cause the function to\n execute until it provides a value using the ``yield`` statement.\n When the function executes a ``return`` statement or falls off\n the end, a ``StopIteration`` exception is raised and the\n iterator will have reached the end of the set of values to be\n returned.\n\n Built-in functions\n A built-in function object is a wrapper around a C function.\n Examples of built-in functions are ``len()`` and ``math.sin()``\n (``math`` is a standard built-in module). The number and type of\n the arguments are determined by the C function. Special read-\n only attributes: ``__doc__`` is the function\'s documentation\n string, or ``None`` if unavailable; ``__name__`` is the\n function\'s name; ``__self__`` is set to ``None`` (but see the\n next item); ``__module__`` is the name of the module the\n function was defined in or ``None`` if unavailable.\n\n Built-in methods\n This is really a different disguise of a built-in function, this\n time containing an object passed to the C function as an\n implicit extra argument. An example of a built-in method is\n ``alist.append()``, assuming *alist* is a list object. In this\n case, the special read-only attribute ``__self__`` is set to the\n object denoted by *alist*.\n\n Classes\n Classes are callable. These objects normally act as factories\n for new instances of themselves, but variations are possible for\n class types that override ``__new__()``. The arguments of the\n call are passed to ``__new__()`` and, in the typical case, to\n ``__init__()`` to initialize the new instance.\n\n Class Instances\n Instances of arbitrary classes can be made callable by defining\n a ``__call__()`` method in their class.\n\nModules\n Modules are imported by the ``import`` statement (see section *The\n import statement*). A module object has a namespace implemented by\n a dictionary object (this is the dictionary referenced by the\n __globals__ attribute of functions defined in the module).\n Attribute references are translated to lookups in this dictionary,\n e.g., ``m.x`` is equivalent to ``m.__dict__["x"]``. A module object\n does not contain the code object used to initialize the module\n (since it isn\'t needed once the initialization is done).\n\n Attribute assignment updates the module\'s namespace dictionary,\n e.g., ``m.x = 1`` is equivalent to ``m.__dict__["x"] = 1``.\n\n Special read-only attribute: ``__dict__`` is the module\'s namespace\n as a dictionary object.\n\n **CPython implementation detail:** Because of the way CPython\n clears module dictionaries, the module dictionary will be cleared\n when the module falls out of scope even if the dictionary still has\n live references. To avoid this, copy the dictionary or keep the\n module around while using its dictionary directly.\n\n Predefined (writable) attributes: ``__name__`` is the module\'s\n name; ``__doc__`` is the module\'s documentation string, or ``None``\n if unavailable; ``__file__`` is the pathname of the file from which\n the module was loaded, if it was loaded from a file. The\n ``__file__`` attribute is not present for C modules that are\n statically linked into the interpreter; for extension modules\n loaded dynamically from a shared library, it is the pathname of the\n shared library file.\n\nCustom classes\n Custom class types are typically created by class definitions (see\n section *Class definitions*). A class has a namespace implemented\n by a dictionary object. Class attribute references are translated\n to lookups in this dictionary, e.g., ``C.x`` is translated to\n ``C.__dict__["x"]`` (although there are a number of hooks which\n allow for other means of locating attributes). When the attribute\n name is not found there, the attribute search continues in the base\n classes. This search of the base classes uses the C3 method\n resolution order which behaves correctly even in the presence of\n \'diamond\' inheritance structures where there are multiple\n inheritance paths leading back to a common ancestor. Additional\n details on the C3 MRO used by Python can be found in the\n documentation accompanying the 2.3 release at\n http://www.python.org/download/releases/2.3/mro/.\n\n When a class attribute reference (for class ``C``, say) would yield\n a class method object, it is transformed into an instance method\n object whose ``__self__`` attributes is ``C``. When it would yield\n a static method object, it is transformed into the object wrapped\n by the static method object. See section *Implementing Descriptors*\n for another way in which attributes retrieved from a class may\n differ from those actually contained in its ``__dict__``.\n\n Class attribute assignments update the class\'s dictionary, never\n the dictionary of a base class.\n\n A class object can be called (see above) to yield a class instance\n (see below).\n\n Special attributes: ``__name__`` is the class name; ``__module__``\n is the module name in which the class was defined; ``__dict__`` is\n the dictionary containing the class\'s namespace; ``__bases__`` is a\n tuple (possibly empty or a singleton) containing the base classes,\n in the order of their occurrence in the base class list;\n ``__doc__`` is the class\'s documentation string, or None if\n undefined.\n\nClass instances\n A class instance is created by calling a class object (see above).\n A class instance has a namespace implemented as a dictionary which\n is the first place in which attribute references are searched.\n When an attribute is not found there, and the instance\'s class has\n an attribute by that name, the search continues with the class\n attributes. If a class attribute is found that is a user-defined\n function object, it is transformed into an instance method object\n whose ``__self__`` attribute is the instance. Static method and\n class method objects are also transformed; see above under\n "Classes". See section *Implementing Descriptors* for another way\n in which attributes of a class retrieved via its instances may\n differ from the objects actually stored in the class\'s\n ``__dict__``. If no class attribute is found, and the object\'s\n class has a ``__getattr__()`` method, that is called to satisfy the\n lookup.\n\n Attribute assignments and deletions update the instance\'s\n dictionary, never a class\'s dictionary. If the class has a\n ``__setattr__()`` or ``__delattr__()`` method, this is called\n instead of updating the instance dictionary directly.\n\n Class instances can pretend to be numbers, sequences, or mappings\n if they have methods with certain special names. See section\n *Special method names*.\n\n Special attributes: ``__dict__`` is the attribute dictionary;\n ``__class__`` is the instance\'s class.\n\nI/O objects (also known as file objects)\n A *file object* represents an open file. Various shortcuts are\n available to create file objects: the ``open()`` built-in function,\n and also ``os.popen()``, ``os.fdopen()``, and the ``makefile()``\n method of socket objects (and perhaps by other functions or methods\n provided by extension modules).\n\n The objects ``sys.stdin``, ``sys.stdout`` and ``sys.stderr`` are\n initialized to file objects corresponding to the interpreter\'s\n standard input, output and error streams; they are all open in text\n mode and therefore follow the interface defined by the\n ``io.TextIOBase`` abstract class.\n\nInternal types\n A few types used internally by the interpreter are exposed to the\n user. Their definitions may change with future versions of the\n interpreter, but they are mentioned here for completeness.\n\n Code objects\n Code objects represent *byte-compiled* executable Python code,\n or *bytecode*. The difference between a code object and a\n function object is that the function object contains an explicit\n reference to the function\'s globals (the module in which it was\n defined), while a code object contains no context; also the\n default argument values are stored in the function object, not\n in the code object (because they represent values calculated at\n run-time). Unlike function objects, code objects are immutable\n and contain no references (directly or indirectly) to mutable\n objects.\n\n Special read-only attributes: ``co_name`` gives the function\n name; ``co_argcount`` is the number of positional arguments\n (including arguments with default values); ``co_nlocals`` is the\n number of local variables used by the function (including\n arguments); ``co_varnames`` is a tuple containing the names of\n the local variables (starting with the argument names);\n ``co_cellvars`` is a tuple containing the names of local\n variables that are referenced by nested functions;\n ``co_freevars`` is a tuple containing the names of free\n variables; ``co_code`` is a string representing the sequence of\n bytecode instructions; ``co_consts`` is a tuple containing the\n literals used by the bytecode; ``co_names`` is a tuple\n containing the names used by the bytecode; ``co_filename`` is\n the filename from which the code was compiled;\n ``co_firstlineno`` is the first line number of the function;\n ``co_lnotab`` is a string encoding the mapping from bytecode\n offsets to line numbers (for details see the source code of the\n interpreter); ``co_stacksize`` is the required stack size\n (including local variables); ``co_flags`` is an integer encoding\n a number of flags for the interpreter.\n\n The following flag bits are defined for ``co_flags``: bit\n ``0x04`` is set if the function uses the ``*arguments`` syntax\n to accept an arbitrary number of positional arguments; bit\n ``0x08`` is set if the function uses the ``**keywords`` syntax\n to accept arbitrary keyword arguments; bit ``0x20`` is set if\n the function is a generator.\n\n Future feature declarations (``from __future__ import\n division``) also use bits in ``co_flags`` to indicate whether a\n code object was compiled with a particular feature enabled: bit\n ``0x2000`` is set if the function was compiled with future\n division enabled; bits ``0x10`` and ``0x1000`` were used in\n earlier versions of Python.\n\n Other bits in ``co_flags`` are reserved for internal use.\n\n If a code object represents a function, the first item in\n ``co_consts`` is the documentation string of the function, or\n ``None`` if undefined.\n\n Frame objects\n Frame objects represent execution frames. They may occur in\n traceback objects (see below).\n\n Special read-only attributes: ``f_back`` is to the previous\n stack frame (towards the caller), or ``None`` if this is the\n bottom stack frame; ``f_code`` is the code object being executed\n in this frame; ``f_locals`` is the dictionary used to look up\n local variables; ``f_globals`` is used for global variables;\n ``f_builtins`` is used for built-in (intrinsic) names;\n ``f_lasti`` gives the precise instruction (this is an index into\n the bytecode string of the code object).\n\n Special writable attributes: ``f_trace``, if not ``None``, is a\n function called at the start of each source code line (this is\n used by the debugger); ``f_lineno`` is the current line number\n of the frame --- writing to this from within a trace function\n jumps to the given line (only for the bottom-most frame). A\n debugger can implement a Jump command (aka Set Next Statement)\n by writing to f_lineno.\n\n Traceback objects\n Traceback objects represent a stack trace of an exception. A\n traceback object is created when an exception occurs. When the\n search for an exception handler unwinds the execution stack, at\n each unwound level a traceback object is inserted in front of\n the current traceback. When an exception handler is entered,\n the stack trace is made available to the program. (See section\n *The try statement*.) It is accessible as the third item of the\n tuple returned by ``sys.exc_info()``. When the program contains\n no suitable handler, the stack trace is written (nicely\n formatted) to the standard error stream; if the interpreter is\n interactive, it is also made available to the user as\n ``sys.last_traceback``.\n\n Special read-only attributes: ``tb_next`` is the next level in\n the stack trace (towards the frame where the exception\n occurred), or ``None`` if there is no next level; ``tb_frame``\n points to the execution frame of the current level;\n ``tb_lineno`` gives the line number where the exception\n occurred; ``tb_lasti`` indicates the precise instruction. The\n line number and last instruction in the traceback may differ\n from the line number of its frame object if the exception\n occurred in a ``try`` statement with no matching except clause\n or with a finally clause.\n\n Slice objects\n Slice objects are used to represent slices for ``__getitem__()``\n methods. They are also created by the built-in ``slice()``\n function.\n\n Special read-only attributes: ``start`` is the lower bound;\n ``stop`` is the upper bound; ``step`` is the step value; each is\n ``None`` if omitted. These attributes can have any type.\n\n Slice objects support one method:\n\n slice.indices(self, length)\n\n This method takes a single integer argument *length* and\n computes information about the slice that the slice object\n would describe if applied to a sequence of *length* items.\n It returns a tuple of three integers; respectively these are\n the *start* and *stop* indices and the *step* or stride\n length of the slice. Missing or out-of-bounds indices are\n handled in a manner consistent with regular slices.\n\n Static method objects\n Static method objects provide a way of defeating the\n transformation of function objects to method objects described\n above. A static method object is a wrapper around any other\n object, usually a user-defined method object. When a static\n method object is retrieved from a class or a class instance, the\n object actually returned is the wrapped object, which is not\n subject to any further transformation. Static method objects are\n not themselves callable, although the objects they wrap usually\n are. Static method objects are created by the built-in\n ``staticmethod()`` constructor.\n\n Class method objects\n A class method object, like a static method object, is a wrapper\n around another object that alters the way in which that object\n is retrieved from classes and class instances. The behaviour of\n class method objects upon such retrieval is described above,\n under "User-defined methods". Class method objects are created\n by the built-in ``classmethod()`` constructor.\n', 'typesfunctions': '\nFunctions\n*********\n\nFunction objects are created by function definitions. The only\noperation on a function object is to call it: ``func(argument-list)``.\n\nThere are really two flavors of function objects: built-in functions\nand user-defined functions. Both support the same operation (to call\nthe function), but the implementation is different, hence the\ndifferent object types.\n\nSee *Function definitions* for more information.\n', - 'typesmapping': '\nMapping Types --- ``dict``\n**************************\n\nA *mapping* object maps *hashable* values to arbitrary objects.\nMappings are mutable objects. There is currently only one standard\nmapping type, the *dictionary*. (For other containers see the built\nin ``list``, ``set``, and ``tuple`` classes, and the ``collections``\nmodule.)\n\nA dictionary\'s keys are *almost* arbitrary values. Values that are\nnot *hashable*, that is, values containing lists, dictionaries or\nother mutable types (that are compared by value rather than by object\nidentity) may not be used as keys. Numeric types used for keys obey\nthe normal rules for numeric comparison: if two numbers compare equal\n(such as ``1`` and ``1.0``) then they can be used interchangeably to\nindex the same dictionary entry. (Note however, that since computers\nstore floating-point numbers as approximations it is usually unwise to\nuse them as dictionary keys.)\n\nDictionaries can be created by placing a comma-separated list of\n``key: value`` pairs within braces, for example: ``{\'jack\': 4098,\n\'sjoerd\': 4127}`` or ``{4098: \'jack\', 4127: \'sjoerd\'}``, or by the\n``dict`` constructor.\n\nclass class dict([arg])\n\n Return a new dictionary initialized from an optional positional\n argument or from a set of keyword arguments. If no arguments are\n given, return a new empty dictionary. If the positional argument\n *arg* is a mapping object, return a dictionary mapping the same\n keys to the same values as does the mapping object. Otherwise the\n positional argument must be a sequence, a container that supports\n iteration, or an iterator object. The elements of the argument\n must each also be of one of those kinds, and each must in turn\n contain exactly two objects. The first is used as a key in the new\n dictionary, and the second as the key\'s value. If a given key is\n seen more than once, the last value associated with it is retained\n in the new dictionary.\n\n If keyword arguments are given, the keywords themselves with their\n associated values are added as items to the dictionary. If a key\n is specified both in the positional argument and as a keyword\n argument, the value associated with the keyword is retained in the\n dictionary. For example, these all return a dictionary equal to\n ``{"one": 1, "two": 2}``:\n\n * ``dict(one=1, two=2)``\n\n * ``dict({\'one\': 1, \'two\': 2})``\n\n * ``dict(zip((\'one\', \'two\'), (1, 2)))``\n\n * ``dict([[\'two\', 2], [\'one\', 1]])``\n\n The first example only works for keys that are valid Python\n identifiers; the others work with any valid keys.\n\n These are the operations that dictionaries support (and therefore,\n custom mapping types should support too):\n\n len(d)\n\n Return the number of items in the dictionary *d*.\n\n d[key]\n\n Return the item of *d* with key *key*. Raises a ``KeyError`` if\n *key* is not in the map.\n\n If a subclass of dict defines a method ``__missing__()``, if the\n key *key* is not present, the ``d[key]`` operation calls that\n method with the key *key* as argument. The ``d[key]`` operation\n then returns or raises whatever is returned or raised by the\n ``__missing__(key)`` call if the key is not present. No other\n operations or methods invoke ``__missing__()``. If\n ``__missing__()`` is not defined, ``KeyError`` is raised.\n ``__missing__()`` must be a method; it cannot be an instance\n variable:\n\n >>> class Counter(dict):\n ... def __missing__(self, key):\n ... return 0\n >>> c = Counter()\n >>> c[\'red\']\n 0\n >>> c[\'red\'] += 1\n >>> c[\'red\']\n 1\n\n See ``collections.Counter`` for a complete implementation\n including other methods helpful for accumulating and managing\n tallies.\n\n d[key] = value\n\n Set ``d[key]`` to *value*.\n\n del d[key]\n\n Remove ``d[key]`` from *d*. Raises a ``KeyError`` if *key* is\n not in the map.\n\n key in d\n\n Return ``True`` if *d* has a key *key*, else ``False``.\n\n key not in d\n\n Equivalent to ``not key in d``.\n\n iter(d)\n\n Return an iterator over the keys of the dictionary. This is a\n shortcut for ``iter(d.keys())``.\n\n clear()\n\n Remove all items from the dictionary.\n\n copy()\n\n Return a shallow copy of the dictionary.\n\n classmethod fromkeys(seq[, value])\n\n Create a new dictionary with keys from *seq* and values set to\n *value*.\n\n ``fromkeys()`` is a class method that returns a new dictionary.\n *value* defaults to ``None``.\n\n get(key[, default])\n\n Return the value for *key* if *key* is in the dictionary, else\n *default*. If *default* is not given, it defaults to ``None``,\n so that this method never raises a ``KeyError``.\n\n items()\n\n Return a new view of the dictionary\'s items (``(key, value)``\n pairs). See the *documentation of view objects*.\n\n keys()\n\n Return a new view of the dictionary\'s keys. See the\n *documentation of view objects*.\n\n pop(key[, default])\n\n If *key* is in the dictionary, remove it and return its value,\n else return *default*. If *default* is not given and *key* is\n not in the dictionary, a ``KeyError`` is raised.\n\n popitem()\n\n Remove and return an arbitrary ``(key, value)`` pair from the\n dictionary.\n\n ``popitem()`` is useful to destructively iterate over a\n dictionary, as often used in set algorithms. If the dictionary\n is empty, calling ``popitem()`` raises a ``KeyError``.\n\n setdefault(key[, default])\n\n If *key* is in the dictionary, return its value. If not, insert\n *key* with a value of *default* and return *default*. *default*\n defaults to ``None``.\n\n update([other])\n\n Update the dictionary with the key/value pairs from *other*,\n overwriting existing keys. Return ``None``.\n\n ``update()`` accepts either another dictionary object or an\n iterable of key/value pairs (as tuples or other iterables of\n length two). If keyword arguments are specified, the dictionary\n is then updated with those key/value pairs: ``d.update(red=1,\n blue=2)``.\n\n values()\n\n Return a new view of the dictionary\'s values. See the\n *documentation of view objects*.\n\nSee also:\n\n ``types.MappingProxyType`` can be used to create a read-only view\n of a ``dict``.\n\n\nDictionary view objects\n=======================\n\nThe objects returned by ``dict.keys()``, ``dict.values()`` and\n``dict.items()`` are *view objects*. They provide a dynamic view on\nthe dictionary\'s entries, which means that when the dictionary\nchanges, the view reflects these changes.\n\nDictionary views can be iterated over to yield their respective data,\nand support membership tests:\n\nlen(dictview)\n\n Return the number of entries in the dictionary.\n\niter(dictview)\n\n Return an iterator over the keys, values or items (represented as\n tuples of ``(key, value)``) in the dictionary.\n\n Keys and values are iterated over in an arbitrary order which is\n non-random, varies across Python implementations, and depends on\n the dictionary\'s history of insertions and deletions. If keys,\n values and items views are iterated over with no intervening\n modifications to the dictionary, the order of items will directly\n correspond. This allows the creation of ``(value, key)`` pairs\n using ``zip()``: ``pairs = zip(d.values(), d.keys())``. Another\n way to create the same list is ``pairs = [(v, k) for (k, v) in\n d.items()]``.\n\n Iterating views while adding or deleting entries in the dictionary\n may raise a ``RuntimeError`` or fail to iterate over all entries.\n\nx in dictview\n\n Return ``True`` if *x* is in the underlying dictionary\'s keys,\n values or items (in the latter case, *x* should be a ``(key,\n value)`` tuple).\n\nKeys views are set-like since their entries are unique and hashable.\nIf all values are hashable, so that ``(key, value)`` pairs are unique\nand hashable, then the items view is also set-like. (Values views are\nnot treated as set-like since the entries are generally not unique.)\nFor set-like views, all of the operations defined for the abstract\nbase class ``collections.Set`` are available (for example, ``==``,\n``<``, or ``^``).\n\nAn example of dictionary view usage:\n\n >>> dishes = {\'eggs\': 2, \'sausage\': 1, \'bacon\': 1, \'spam\': 500}\n >>> keys = dishes.keys()\n >>> values = dishes.values()\n\n >>> # iteration\n >>> n = 0\n >>> for val in values:\n ... n += val\n >>> print(n)\n 504\n\n >>> # keys and values are iterated over in the same order\n >>> list(keys)\n [\'eggs\', \'bacon\', \'sausage\', \'spam\']\n >>> list(values)\n [2, 1, 1, 500]\n\n >>> # view objects are dynamic and reflect dict changes\n >>> del dishes[\'eggs\']\n >>> del dishes[\'sausage\']\n >>> list(keys)\n [\'spam\', \'bacon\']\n\n >>> # set operations\n >>> keys & {\'eggs\', \'bacon\', \'salad\'}\n {\'bacon\'}\n >>> keys ^ {\'sausage\', \'juice\'}\n {\'juice\', \'sausage\', \'bacon\', \'spam\'}\n', + 'typesmapping': '\nMapping Types --- ``dict``\n**************************\n\nA *mapping* object maps *hashable* values to arbitrary objects.\nMappings are mutable objects. There is currently only one standard\nmapping type, the *dictionary*. (For other containers see the built\nin ``list``, ``set``, and ``tuple`` classes, and the ``collections``\nmodule.)\n\nA dictionary\'s keys are *almost* arbitrary values. Values that are\nnot *hashable*, that is, values containing lists, dictionaries or\nother mutable types (that are compared by value rather than by object\nidentity) may not be used as keys. Numeric types used for keys obey\nthe normal rules for numeric comparison: if two numbers compare equal\n(such as ``1`` and ``1.0``) then they can be used interchangeably to\nindex the same dictionary entry. (Note however, that since computers\nstore floating-point numbers as approximations it is usually unwise to\nuse them as dictionary keys.)\n\nDictionaries can be created by placing a comma-separated list of\n``key: value`` pairs within braces, for example: ``{\'jack\': 4098,\n\'sjoerd\': 4127}`` or ``{4098: \'jack\', 4127: \'sjoerd\'}``, or by the\n``dict`` constructor.\n\nclass class dict([arg])\n\n Return a new dictionary initialized from an optional positional\n argument or from a set of keyword arguments. If no arguments are\n given, return a new empty dictionary. If the positional argument\n *arg* is a mapping object, return a dictionary mapping the same\n keys to the same values as does the mapping object. Otherwise the\n positional argument must be a sequence, a container that supports\n iteration, or an iterator object. The elements of the argument\n must each also be of one of those kinds, and each must in turn\n contain exactly two objects. The first is used as a key in the new\n dictionary, and the second as the key\'s value. If a given key is\n seen more than once, the last value associated with it is retained\n in the new dictionary.\n\n If keyword arguments are given, the keywords themselves with their\n associated values are added as items to the dictionary. If a key\n is specified both in the positional argument and as a keyword\n argument, the value associated with the keyword is retained in the\n dictionary. For example, these all return a dictionary equal to\n ``{"one": 1, "two": 2}``:\n\n * ``dict(one=1, two=2)``\n\n * ``dict({\'one\': 1, \'two\': 2})``\n\n * ``dict(zip((\'one\', \'two\'), (1, 2)))``\n\n * ``dict([[\'two\', 2], [\'one\', 1]])``\n\n The first example only works for keys that are valid Python\n identifiers; the others work with any valid keys.\n\n These are the operations that dictionaries support (and therefore,\n custom mapping types should support too):\n\n len(d)\n\n Return the number of items in the dictionary *d*.\n\n d[key]\n\n Return the item of *d* with key *key*. Raises a ``KeyError`` if\n *key* is not in the map.\n\n If a subclass of dict defines a method ``__missing__()``, if the\n key *key* is not present, the ``d[key]`` operation calls that\n method with the key *key* as argument. The ``d[key]`` operation\n then returns or raises whatever is returned or raised by the\n ``__missing__(key)`` call if the key is not present. No other\n operations or methods invoke ``__missing__()``. If\n ``__missing__()`` is not defined, ``KeyError`` is raised.\n ``__missing__()`` must be a method; it cannot be an instance\n variable:\n\n >>> class Counter(dict):\n ... def __missing__(self, key):\n ... return 0\n >>> c = Counter()\n >>> c[\'red\']\n 0\n >>> c[\'red\'] += 1\n >>> c[\'red\']\n 1\n\n See ``collections.Counter`` for a complete implementation\n including other methods helpful for accumulating and managing\n tallies.\n\n Changed in version 3.3: If the dict is modified during the\n lookup, a ``RuntimeError`` exception is now raised.\n\n d[key] = value\n\n Set ``d[key]`` to *value*.\n\n del d[key]\n\n Remove ``d[key]`` from *d*. Raises a ``KeyError`` if *key* is\n not in the map.\n\n key in d\n\n Return ``True`` if *d* has a key *key*, else ``False``.\n\n key not in d\n\n Equivalent to ``not key in d``.\n\n iter(d)\n\n Return an iterator over the keys of the dictionary. This is a\n shortcut for ``iter(d.keys())``.\n\n clear()\n\n Remove all items from the dictionary.\n\n copy()\n\n Return a shallow copy of the dictionary.\n\n classmethod fromkeys(seq[, value])\n\n Create a new dictionary with keys from *seq* and values set to\n *value*.\n\n ``fromkeys()`` is a class method that returns a new dictionary.\n *value* defaults to ``None``.\n\n get(key[, default])\n\n Return the value for *key* if *key* is in the dictionary, else\n *default*. If *default* is not given, it defaults to ``None``,\n so that this method never raises a ``KeyError``.\n\n items()\n\n Return a new view of the dictionary\'s items (``(key, value)``\n pairs). See the *documentation of view objects*.\n\n keys()\n\n Return a new view of the dictionary\'s keys. See the\n *documentation of view objects*.\n\n pop(key[, default])\n\n If *key* is in the dictionary, remove it and return its value,\n else return *default*. If *default* is not given and *key* is\n not in the dictionary, a ``KeyError`` is raised.\n\n popitem()\n\n Remove and return an arbitrary ``(key, value)`` pair from the\n dictionary.\n\n ``popitem()`` is useful to destructively iterate over a\n dictionary, as often used in set algorithms. If the dictionary\n is empty, calling ``popitem()`` raises a ``KeyError``.\n\n setdefault(key[, default])\n\n If *key* is in the dictionary, return its value. If not, insert\n *key* with a value of *default* and return *default*. *default*\n defaults to ``None``.\n\n update([other])\n\n Update the dictionary with the key/value pairs from *other*,\n overwriting existing keys. Return ``None``.\n\n ``update()`` accepts either another dictionary object or an\n iterable of key/value pairs (as tuples or other iterables of\n length two). If keyword arguments are specified, the dictionary\n is then updated with those key/value pairs: ``d.update(red=1,\n blue=2)``.\n\n values()\n\n Return a new view of the dictionary\'s values. See the\n *documentation of view objects*.\n\nSee also:\n\n ``types.MappingProxyType`` can be used to create a read-only view\n of a ``dict``.\n\n\nDictionary view objects\n=======================\n\nThe objects returned by ``dict.keys()``, ``dict.values()`` and\n``dict.items()`` are *view objects*. They provide a dynamic view on\nthe dictionary\'s entries, which means that when the dictionary\nchanges, the view reflects these changes.\n\nDictionary views can be iterated over to yield their respective data,\nand support membership tests:\n\nlen(dictview)\n\n Return the number of entries in the dictionary.\n\niter(dictview)\n\n Return an iterator over the keys, values or items (represented as\n tuples of ``(key, value)``) in the dictionary.\n\n Keys and values are iterated over in an arbitrary order which is\n non-random, varies across Python implementations, and depends on\n the dictionary\'s history of insertions and deletions. If keys,\n values and items views are iterated over with no intervening\n modifications to the dictionary, the order of items will directly\n correspond. This allows the creation of ``(value, key)`` pairs\n using ``zip()``: ``pairs = zip(d.values(), d.keys())``. Another\n way to create the same list is ``pairs = [(v, k) for (k, v) in\n d.items()]``.\n\n Iterating views while adding or deleting entries in the dictionary\n may raise a ``RuntimeError`` or fail to iterate over all entries.\n\nx in dictview\n\n Return ``True`` if *x* is in the underlying dictionary\'s keys,\n values or items (in the latter case, *x* should be a ``(key,\n value)`` tuple).\n\nKeys views are set-like since their entries are unique and hashable.\nIf all values are hashable, so that ``(key, value)`` pairs are unique\nand hashable, then the items view is also set-like. (Values views are\nnot treated as set-like since the entries are generally not unique.)\nFor set-like views, all of the operations defined for the abstract\nbase class ``collections.Set`` are available (for example, ``==``,\n``<``, or ``^``).\n\nAn example of dictionary view usage:\n\n >>> dishes = {\'eggs\': 2, \'sausage\': 1, \'bacon\': 1, \'spam\': 500}\n >>> keys = dishes.keys()\n >>> values = dishes.values()\n\n >>> # iteration\n >>> n = 0\n >>> for val in values:\n ... n += val\n >>> print(n)\n 504\n\n >>> # keys and values are iterated over in the same order\n >>> list(keys)\n [\'eggs\', \'bacon\', \'sausage\', \'spam\']\n >>> list(values)\n [2, 1, 1, 500]\n\n >>> # view objects are dynamic and reflect dict changes\n >>> del dishes[\'eggs\']\n >>> del dishes[\'sausage\']\n >>> list(keys)\n [\'spam\', \'bacon\']\n\n >>> # set operations\n >>> keys & {\'eggs\', \'bacon\', \'salad\'}\n {\'bacon\'}\n >>> keys ^ {\'sausage\', \'juice\'}\n {\'juice\', \'sausage\', \'bacon\', \'spam\'}\n', 'typesmethods': "\nMethods\n*******\n\nMethods are functions that are called using the attribute notation.\nThere are two flavors: built-in methods (such as ``append()`` on\nlists) and class instance methods. Built-in methods are described\nwith the types that support them.\n\nIf you access a method (a function defined in a class namespace)\nthrough an instance, you get a special object: a *bound method* (also\ncalled *instance method*) object. When called, it will add the\n``self`` argument to the argument list. Bound methods have two\nspecial read-only attributes: ``m.__self__`` is the object on which\nthe method operates, and ``m.__func__`` is the function implementing\nthe method. Calling ``m(arg-1, arg-2, ..., arg-n)`` is completely\nequivalent to calling ``m.__func__(m.__self__, arg-1, arg-2, ...,\narg-n)``.\n\nLike function objects, bound method objects support getting arbitrary\nattributes. However, since method attributes are actually stored on\nthe underlying function object (``meth.__func__``), setting method\nattributes on bound methods is disallowed. Attempting to set a method\nattribute results in a ``TypeError`` being raised. In order to set a\nmethod attribute, you need to explicitly set it on the underlying\nfunction object:\n\n class C:\n def method(self):\n pass\n\n c = C()\n c.method.__func__.whoami = 'my name is c'\n\nSee *The standard type hierarchy* for more information.\n", 'typesmodules': "\nModules\n*******\n\nThe only special operation on a module is attribute access:\n``m.name``, where *m* is a module and *name* accesses a name defined\nin *m*'s symbol table. Module attributes can be assigned to. (Note\nthat the ``import`` statement is not, strictly speaking, an operation\non a module object; ``import foo`` does not require a module object\nnamed *foo* to exist, rather it requires an (external) *definition*\nfor a module named *foo* somewhere.)\n\nA special attribute of every module is ``__dict__``. This is the\ndictionary containing the module's symbol table. Modifying this\ndictionary will actually change the module's symbol table, but direct\nassignment to the ``__dict__`` attribute is not possible (you can\nwrite ``m.__dict__['a'] = 1``, which defines ``m.a`` to be ``1``, but\nyou can't write ``m.__dict__ = {}``). Modifying ``__dict__`` directly\nis not recommended.\n\nModules built into the interpreter are written like this: ````. If loaded from a file, they are written as\n````.\n", - 'typesseq': '\nSequence Types --- ``str``, ``bytes``, ``bytearray``, ``list``, ``tuple``, ``range``\n************************************************************************************\n\nThere are six sequence types: strings, byte sequences (``bytes``\nobjects), byte arrays (``bytearray`` objects), lists, tuples, and\nrange objects. For other containers see the built in ``dict`` and\n``set`` classes, and the ``collections`` module.\n\nStrings contain Unicode characters. Their literals are written in\nsingle or double quotes: ``\'xyzzy\'``, ``"frobozz"``. See *String and\nBytes literals* for more about string literals. In addition to the\nfunctionality described here, there are also string-specific methods\ndescribed in the *String Methods* section.\n\nBytes and bytearray objects contain single bytes -- the former is\nimmutable while the latter is a mutable sequence. Bytes objects can be\nconstructed by using the constructor, ``bytes()``, and from literals;\nuse a ``b`` prefix with normal string syntax: ``b\'xyzzy\'``. To\nconstruct byte arrays, use the ``bytearray()`` function.\n\nWhile string objects are sequences of characters (represented by\nstrings of length 1), bytes and bytearray objects are sequences of\n*integers* (between 0 and 255), representing the ASCII value of single\nbytes. That means that for a bytes or bytearray object *b*, ``b[0]``\nwill be an integer, while ``b[0:1]`` will be a bytes or bytearray\nobject of length 1. The representation of bytes objects uses the\nliteral format (``b\'...\'``) since it is generally more useful than\ne.g. ``bytes([50, 19, 100])``. You can always convert a bytes object\ninto a list of integers using ``list(b)``.\n\nAlso, while in previous Python versions, byte strings and Unicode\nstrings could be exchanged for each other rather freely (barring\nencoding issues), strings and bytes are now completely separate\nconcepts. There\'s no implicit en-/decoding if you pass an object of\nthe wrong type. A string always compares unequal to a bytes or\nbytearray object.\n\nLists are constructed with square brackets, separating items with\ncommas: ``[a, b, c]``. Tuples are constructed by the comma operator\n(not within square brackets), with or without enclosing parentheses,\nbut an empty tuple must have the enclosing parentheses, such as ``a,\nb, c`` or ``()``. A single item tuple must have a trailing comma,\nsuch as ``(d,)``.\n\nObjects of type range are created using the ``range()`` function.\nThey don\'t support concatenation or repetition, and using ``min()`` or\n``max()`` on them is inefficient.\n\nMost sequence types support the following operations. The ``in`` and\n``not in`` operations have the same priorities as the comparison\noperations. The ``+`` and ``*`` operations have the same priority as\nthe corresponding numeric operations. [3] Additional methods are\nprovided for *Mutable Sequence Types*.\n\nThis table lists the sequence operations sorted in ascending priority\n(operations in the same box have the same priority). In the table,\n*s* and *t* are sequences of the same type; *n*, *i*, *j* and *k* are\nintegers.\n\n+--------------------+----------------------------------+------------+\n| Operation | Result | Notes |\n+====================+==================================+============+\n| ``x in s`` | ``True`` if an item of *s* is | (1) |\n| | equal to *x*, else ``False`` | |\n+--------------------+----------------------------------+------------+\n| ``x not in s`` | ``False`` if an item of *s* is | (1) |\n| | equal to *x*, else ``True`` | |\n+--------------------+----------------------------------+------------+\n| ``s + t`` | the concatenation of *s* and *t* | (6) |\n+--------------------+----------------------------------+------------+\n| ``s * n, n * s`` | *n* shallow copies of *s* | (2) |\n| | concatenated | |\n+--------------------+----------------------------------+------------+\n| ``s[i]`` | *i*th item of *s*, origin 0 | (3) |\n+--------------------+----------------------------------+------------+\n| ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) |\n+--------------------+----------------------------------+------------+\n| ``s[i:j:k]`` | slice of *s* from *i* to *j* | (3)(5) |\n| | with step *k* | |\n+--------------------+----------------------------------+------------+\n| ``len(s)`` | length of *s* | |\n+--------------------+----------------------------------+------------+\n| ``min(s)`` | smallest item of *s* | |\n+--------------------+----------------------------------+------------+\n| ``max(s)`` | largest item of *s* | |\n+--------------------+----------------------------------+------------+\n| ``s.index(i)`` | index of the first occurence of | |\n| | *i* in *s* | |\n+--------------------+----------------------------------+------------+\n| ``s.count(i)`` | total number of occurences of | |\n| | *i* in *s* | |\n+--------------------+----------------------------------+------------+\n\nSequence types also support comparisons. In particular, tuples and\nlists are compared lexicographically by comparing corresponding\nelements. This means that to compare equal, every element must\ncompare equal and the two sequences must be of the same type and have\nthe same length. (For full details see *Comparisons* in the language\nreference.)\n\nNotes:\n\n1. When *s* is a string object, the ``in`` and ``not in`` operations\n act like a substring test.\n\n2. Values of *n* less than ``0`` are treated as ``0`` (which yields an\n empty sequence of the same type as *s*). Note also that the copies\n are shallow; nested structures are not copied. This often haunts\n new Python programmers; consider:\n\n >>> lists = [[]] * 3\n >>> lists\n [[], [], []]\n >>> lists[0].append(3)\n >>> lists\n [[3], [3], [3]]\n\n What has happened is that ``[[]]`` is a one-element list containing\n an empty list, so all three elements of ``[[]] * 3`` are (pointers\n to) this single empty list. Modifying any of the elements of\n ``lists`` modifies this single list. You can create a list of\n different lists this way:\n\n >>> lists = [[] for i in range(3)]\n >>> lists[0].append(3)\n >>> lists[1].append(5)\n >>> lists[2].append(7)\n >>> lists\n [[3], [5], [7]]\n\n3. If *i* or *j* is negative, the index is relative to the end of the\n string: ``len(s) + i`` or ``len(s) + j`` is substituted. But note\n that ``-0`` is still ``0``.\n\n4. The slice of *s* from *i* to *j* is defined as the sequence of\n items with index *k* such that ``i <= k < j``. If *i* or *j* is\n greater than ``len(s)``, use ``len(s)``. If *i* is omitted or\n ``None``, use ``0``. If *j* is omitted or ``None``, use\n ``len(s)``. If *i* is greater than or equal to *j*, the slice is\n empty.\n\n5. The slice of *s* from *i* to *j* with step *k* is defined as the\n sequence of items with index ``x = i + n*k`` such that ``0 <= n <\n (j-i)/k``. In other words, the indices are ``i``, ``i+k``,\n ``i+2*k``, ``i+3*k`` and so on, stopping when *j* is reached (but\n never including *j*). If *i* or *j* is greater than ``len(s)``,\n use ``len(s)``. If *i* or *j* are omitted or ``None``, they become\n "end" values (which end depends on the sign of *k*). Note, *k*\n cannot be zero. If *k* is ``None``, it is treated like ``1``.\n\n6. Concatenating immutable strings always results in a new object.\n This means that building up a string by repeated concatenation will\n have a quadratic runtime cost in the total string length. To get a\n linear runtime cost, you must switch to one of the alternatives\n below:\n\n * if concatenating ``str`` objects, you can build a list and use\n ``str.join()`` at the end;\n\n * if concatenating ``bytes`` objects, you can similarly use\n ``bytes.join()``, or you can do in-place concatenation with a\n ``bytearray`` object. ``bytearray`` objects are mutable and have\n an efficient overallocation mechanism.\n\n\nString Methods\n==============\n\nString objects support the methods listed below.\n\nIn addition, Python\'s strings support the sequence type methods\ndescribed in the *Sequence Types --- str, bytes, bytearray, list,\ntuple, range* section. To output formatted strings, see the *String\nFormatting* section. Also, see the ``re`` module for string functions\nbased on regular expressions.\n\nstr.capitalize()\n\n Return a copy of the string with its first character capitalized\n and the rest lowercased.\n\nstr.casefold()\n\n Return a casefolded copy of the string. Casefolded strings may be\n used for caseless matching.\n\n Casefolding is similar to lowercasing but more aggressive because\n it is intended to remove all case distinctions in a string. For\n example, the German lowercase letter ``\'\xc3\x9f\'`` is equivalent to\n ``"ss"``. Since it is already lowercase, ``lower()`` would do\n nothing to ``\'\xc3\x9f\'``; ``casefold()`` converts it to ``"ss"``.\n\n The casefolding algorithm is described in section 3.13 of the\n Unicode Standard.\n\n New in version 3.3.\n\nstr.center(width[, fillchar])\n\n Return centered in a string of length *width*. Padding is done\n using the specified *fillchar* (default is a space).\n\nstr.count(sub[, start[, end]])\n\n Return the number of non-overlapping occurrences of substring *sub*\n in the range [*start*, *end*]. Optional arguments *start* and\n *end* are interpreted as in slice notation.\n\nstr.encode(encoding="utf-8", errors="strict")\n\n Return an encoded version of the string as a bytes object. Default\n encoding is ``\'utf-8\'``. *errors* may be given to set a different\n error handling scheme. The default for *errors* is ``\'strict\'``,\n meaning that encoding errors raise a ``UnicodeError``. Other\n possible values are ``\'ignore\'``, ``\'replace\'``,\n ``\'xmlcharrefreplace\'``, ``\'backslashreplace\'`` and any other name\n registered via ``codecs.register_error()``, see section *Codec Base\n Classes*. For a list of possible encodings, see section *Standard\n Encodings*.\n\n Changed in version 3.1: Support for keyword arguments added.\n\nstr.endswith(suffix[, start[, end]])\n\n Return ``True`` if the string ends with the specified *suffix*,\n otherwise return ``False``. *suffix* can also be a tuple of\n suffixes to look for. With optional *start*, test beginning at\n that position. With optional *end*, stop comparing at that\n position.\n\nstr.expandtabs([tabsize])\n\n Return a copy of the string where all tab characters are replaced\n by zero or more spaces, depending on the current column and the\n given tab size. The column number is reset to zero after each\n newline occurring in the string. If *tabsize* is not given, a tab\n size of ``8`` characters is assumed. This doesn\'t understand other\n non-printing characters or escape sequences.\n\nstr.find(sub[, start[, end]])\n\n Return the lowest index in the string where substring *sub* is\n found, such that *sub* is contained in the slice ``s[start:end]``.\n Optional arguments *start* and *end* are interpreted as in slice\n notation. Return ``-1`` if *sub* is not found.\n\n Note: The ``find()`` method should be used only if you need to know the\n position of *sub*. To check if *sub* is a substring or not, use\n the ``in`` operator:\n\n >>> \'Py\' in \'Python\'\n True\n\nstr.format(*args, **kwargs)\n\n Perform a string formatting operation. The string on which this\n method is called can contain literal text or replacement fields\n delimited by braces ``{}``. Each replacement field contains either\n the numeric index of a positional argument, or the name of a\n keyword argument. Returns a copy of the string where each\n replacement field is replaced with the string value of the\n corresponding argument.\n\n >>> "The sum of 1 + 2 is {0}".format(1+2)\n \'The sum of 1 + 2 is 3\'\n\n See *Format String Syntax* for a description of the various\n formatting options that can be specified in format strings.\n\nstr.format_map(mapping)\n\n Similar to ``str.format(**mapping)``, except that ``mapping`` is\n used directly and not copied to a ``dict`` . This is useful if for\n example ``mapping`` is a dict subclass:\n\n >>> class Default(dict):\n ... def __missing__(self, key):\n ... return key\n ...\n >>> \'{name} was born in {country}\'.format_map(Default(name=\'Guido\'))\n \'Guido was born in country\'\n\n New in version 3.2.\n\nstr.index(sub[, start[, end]])\n\n Like ``find()``, but raise ``ValueError`` when the substring is not\n found.\n\nstr.isalnum()\n\n Return true if all characters in the string are alphanumeric and\n there is at least one character, false otherwise. A character\n ``c`` is alphanumeric if one of the following returns ``True``:\n ``c.isalpha()``, ``c.isdecimal()``, ``c.isdigit()``, or\n ``c.isnumeric()``.\n\nstr.isalpha()\n\n Return true if all characters in the string are alphabetic and\n there is at least one character, false otherwise. Alphabetic\n characters are those characters defined in the Unicode character\n database as "Letter", i.e., those with general category property\n being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note that this is\n different from the "Alphabetic" property defined in the Unicode\n Standard.\n\nstr.isdecimal()\n\n Return true if all characters in the string are decimal characters\n and there is at least one character, false otherwise. Decimal\n characters are those from general category "Nd". This category\n includes digit characters, and all characters that can be used to\n form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.\n\nstr.isdigit()\n\n Return true if all characters in the string are digits and there is\n at least one character, false otherwise. Digits include decimal\n characters and digits that need special handling, such as the\n compatibility superscript digits. Formally, a digit is a character\n that has the property value Numeric_Type=Digit or\n Numeric_Type=Decimal.\n\nstr.isidentifier()\n\n Return true if the string is a valid identifier according to the\n language definition, section *Identifiers and keywords*.\n\nstr.islower()\n\n Return true if all cased characters [4] in the string are lowercase\n and there is at least one cased character, false otherwise.\n\nstr.isnumeric()\n\n Return true if all characters in the string are numeric characters,\n and there is at least one character, false otherwise. Numeric\n characters include digit characters, and all characters that have\n the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION\n ONE FIFTH. Formally, numeric characters are those with the\n property value Numeric_Type=Digit, Numeric_Type=Decimal or\n Numeric_Type=Numeric.\n\nstr.isprintable()\n\n Return true if all characters in the string are printable or the\n string is empty, false otherwise. Nonprintable characters are\n those characters defined in the Unicode character database as\n "Other" or "Separator", excepting the ASCII space (0x20) which is\n considered printable. (Note that printable characters in this\n context are those which should not be escaped when ``repr()`` is\n invoked on a string. It has no bearing on the handling of strings\n written to ``sys.stdout`` or ``sys.stderr``.)\n\nstr.isspace()\n\n Return true if there are only whitespace characters in the string\n and there is at least one character, false otherwise. Whitespace\n characters are those characters defined in the Unicode character\n database as "Other" or "Separator" and those with bidirectional\n property being one of "WS", "B", or "S".\n\nstr.istitle()\n\n Return true if the string is a titlecased string and there is at\n least one character, for example uppercase characters may only\n follow uncased characters and lowercase characters only cased ones.\n Return false otherwise.\n\nstr.isupper()\n\n Return true if all cased characters [4] in the string are uppercase\n and there is at least one cased character, false otherwise.\n\nstr.join(iterable)\n\n Return a string which is the concatenation of the strings in the\n *iterable* *iterable*. A ``TypeError`` will be raised if there are\n any non-string values in *iterable*, including ``bytes`` objects.\n The separator between elements is the string providing this method.\n\nstr.ljust(width[, fillchar])\n\n Return the string left justified in a string of length *width*.\n Padding is done using the specified *fillchar* (default is a\n space). The original string is returned if *width* is less than or\n equal to ``len(s)``.\n\nstr.lower()\n\n Return a copy of the string with all the cased characters [4]\n converted to lowercase.\n\n The lowercasing algorithm used is described in section 3.13 of the\n Unicode Standard.\n\nstr.lstrip([chars])\n\n Return a copy of the string with leading characters removed. The\n *chars* argument is a string specifying the set of characters to be\n removed. If omitted or ``None``, the *chars* argument defaults to\n removing whitespace. The *chars* argument is not a prefix; rather,\n all combinations of its values are stripped:\n\n >>> \' spacious \'.lstrip()\n \'spacious \'\n >>> \'www.example.com\'.lstrip(\'cmowz.\')\n \'example.com\'\n\nstatic str.maketrans(x[, y[, z]])\n\n This static method returns a translation table usable for\n ``str.translate()``.\n\n If there is only one argument, it must be a dictionary mapping\n Unicode ordinals (integers) or characters (strings of length 1) to\n Unicode ordinals, strings (of arbitrary lengths) or None.\n Character keys will then be converted to ordinals.\n\n If there are two arguments, they must be strings of equal length,\n and in the resulting dictionary, each character in x will be mapped\n to the character at the same position in y. If there is a third\n argument, it must be a string, whose characters will be mapped to\n None in the result.\n\nstr.partition(sep)\n\n Split the string at the first occurrence of *sep*, and return a\n 3-tuple containing the part before the separator, the separator\n itself, and the part after the separator. If the separator is not\n found, return a 3-tuple containing the string itself, followed by\n two empty strings.\n\nstr.replace(old, new[, count])\n\n Return a copy of the string with all occurrences of substring *old*\n replaced by *new*. If the optional argument *count* is given, only\n the first *count* occurrences are replaced.\n\nstr.rfind(sub[, start[, end]])\n\n Return the highest index in the string where substring *sub* is\n found, such that *sub* is contained within ``s[start:end]``.\n Optional arguments *start* and *end* are interpreted as in slice\n notation. Return ``-1`` on failure.\n\nstr.rindex(sub[, start[, end]])\n\n Like ``rfind()`` but raises ``ValueError`` when the substring *sub*\n is not found.\n\nstr.rjust(width[, fillchar])\n\n Return the string right justified in a string of length *width*.\n Padding is done using the specified *fillchar* (default is a\n space). The original string is returned if *width* is less than or\n equal to ``len(s)``.\n\nstr.rpartition(sep)\n\n Split the string at the last occurrence of *sep*, and return a\n 3-tuple containing the part before the separator, the separator\n itself, and the part after the separator. If the separator is not\n found, return a 3-tuple containing two empty strings, followed by\n the string itself.\n\nstr.rsplit(sep=None, maxsplit=-1)\n\n Return a list of the words in the string, using *sep* as the\n delimiter string. If *maxsplit* is given, at most *maxsplit* splits\n are done, the *rightmost* ones. If *sep* is not specified or\n ``None``, any whitespace string is a separator. Except for\n splitting from the right, ``rsplit()`` behaves like ``split()``\n which is described in detail below.\n\nstr.rstrip([chars])\n\n Return a copy of the string with trailing characters removed. The\n *chars* argument is a string specifying the set of characters to be\n removed. If omitted or ``None``, the *chars* argument defaults to\n removing whitespace. The *chars* argument is not a suffix; rather,\n all combinations of its values are stripped:\n\n >>> \' spacious \'.rstrip()\n \' spacious\'\n >>> \'mississippi\'.rstrip(\'ipz\')\n \'mississ\'\n\nstr.split(sep=None, maxsplit=-1)\n\n Return a list of the words in the string, using *sep* as the\n delimiter string. If *maxsplit* is given, at most *maxsplit*\n splits are done (thus, the list will have at most ``maxsplit+1``\n elements). If *maxsplit* is not specified or ``-1``, then there is\n no limit on the number of splits (all possible splits are made).\n\n If *sep* is given, consecutive delimiters are not grouped together\n and are deemed to delimit empty strings (for example,\n ``\'1,,2\'.split(\',\')`` returns ``[\'1\', \'\', \'2\']``). The *sep*\n argument may consist of multiple characters (for example,\n ``\'1<>2<>3\'.split(\'<>\')`` returns ``[\'1\', \'2\', \'3\']``). Splitting\n an empty string with a specified separator returns ``[\'\']``.\n\n If *sep* is not specified or is ``None``, a different splitting\n algorithm is applied: runs of consecutive whitespace are regarded\n as a single separator, and the result will contain no empty strings\n at the start or end if the string has leading or trailing\n whitespace. Consequently, splitting an empty string or a string\n consisting of just whitespace with a ``None`` separator returns\n ``[]``.\n\n For example, ``\' 1 2 3 \'.split()`` returns ``[\'1\', \'2\', \'3\']``,\n and ``\' 1 2 3 \'.split(None, 1)`` returns ``[\'1\', \'2 3 \']``.\n\nstr.splitlines([keepends])\n\n Return a list of the lines in the string, breaking at line\n boundaries. Line breaks are not included in the resulting list\n unless *keepends* is given and true.\n\nstr.startswith(prefix[, start[, end]])\n\n Return ``True`` if string starts with the *prefix*, otherwise\n return ``False``. *prefix* can also be a tuple of prefixes to look\n for. With optional *start*, test string beginning at that\n position. With optional *end*, stop comparing string at that\n position.\n\nstr.strip([chars])\n\n Return a copy of the string with the leading and trailing\n characters removed. The *chars* argument is a string specifying the\n set of characters to be removed. If omitted or ``None``, the\n *chars* argument defaults to removing whitespace. The *chars*\n argument is not a prefix or suffix; rather, all combinations of its\n values are stripped:\n\n >>> \' spacious \'.strip()\n \'spacious\'\n >>> \'www.example.com\'.strip(\'cmowz.\')\n \'example\'\n\nstr.swapcase()\n\n Return a copy of the string with uppercase characters converted to\n lowercase and vice versa. Note that it is not necessarily true that\n ``s.swapcase().swapcase() == s``.\n\nstr.title()\n\n Return a titlecased version of the string where words start with an\n uppercase character and the remaining characters are lowercase.\n\n The algorithm uses a simple language-independent definition of a\n word as groups of consecutive letters. The definition works in\n many contexts but it means that apostrophes in contractions and\n possessives form word boundaries, which may not be the desired\n result:\n\n >>> "they\'re bill\'s friends from the UK".title()\n "They\'Re Bill\'S Friends From The Uk"\n\n A workaround for apostrophes can be constructed using regular\n expressions:\n\n >>> import re\n >>> def titlecase(s):\n return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n lambda mo: mo.group(0)[0].upper() +\n mo.group(0)[1:].lower(),\n s)\n\n >>> titlecase("they\'re bill\'s friends.")\n "They\'re Bill\'s Friends."\n\nstr.translate(map)\n\n Return a copy of the *s* where all characters have been mapped\n through the *map* which must be a dictionary of Unicode ordinals\n (integers) to Unicode ordinals, strings or ``None``. Unmapped\n characters are left untouched. Characters mapped to ``None`` are\n deleted.\n\n You can use ``str.maketrans()`` to create a translation map from\n character-to-character mappings in different formats.\n\n Note: An even more flexible approach is to create a custom character\n mapping codec using the ``codecs`` module (see\n ``encodings.cp1251`` for an example).\n\nstr.upper()\n\n Return a copy of the string with all the cased characters [4]\n converted to uppercase. Note that ``str.upper().isupper()`` might\n be ``False`` if ``s`` contains uncased characters or if the Unicode\n category of the resulting character(s) is not "Lu" (Letter,\n uppercase), but e.g. "Lt" (Letter, titlecase).\n\n The uppercasing algorithm used is described in section 3.13 of the\n Unicode Standard.\n\nstr.zfill(width)\n\n Return the numeric string left filled with zeros in a string of\n length *width*. A sign prefix is handled correctly. The original\n string is returned if *width* is less than or equal to ``len(s)``.\n\n\nOld String Formatting Operations\n================================\n\nNote: The formatting operations described here are modelled on C\'s\n printf() syntax. They only support formatting of certain builtin\n types. The use of a binary operator means that care may be needed\n in order to format tuples and dictionaries correctly. As the new\n *String Formatting* syntax is more flexible and handles tuples and\n dictionaries naturally, it is recommended for new code. However,\n there are no current plans to deprecate printf-style formatting.\n\nString objects have one unique built-in operation: the ``%`` operator\n(modulo). This is also known as the string *formatting* or\n*interpolation* operator. Given ``format % values`` (where *format* is\na string), ``%`` conversion specifications in *format* are replaced\nwith zero or more elements of *values*. The effect is similar to the\nusing ``sprintf()`` in the C language.\n\nIf *format* requires a single argument, *values* may be a single non-\ntuple object. [5] Otherwise, *values* must be a tuple with exactly\nthe number of items specified by the format string, or a single\nmapping object (for example, a dictionary).\n\nA conversion specifier contains two or more characters and has the\nfollowing components, which must occur in this order:\n\n1. The ``\'%\'`` character, which marks the start of the specifier.\n\n2. Mapping key (optional), consisting of a parenthesised sequence of\n characters (for example, ``(somename)``).\n\n3. Conversion flags (optional), which affect the result of some\n conversion types.\n\n4. Minimum field width (optional). If specified as an ``\'*\'``\n (asterisk), the actual width is read from the next element of the\n tuple in *values*, and the object to convert comes after the\n minimum field width and optional precision.\n\n5. Precision (optional), given as a ``\'.\'`` (dot) followed by the\n precision. If specified as ``\'*\'`` (an asterisk), the actual\n precision is read from the next element of the tuple in *values*,\n and the value to convert comes after the precision.\n\n6. Length modifier (optional).\n\n7. Conversion type.\n\nWhen the right argument is a dictionary (or other mapping type), then\nthe formats in the string *must* include a parenthesised mapping key\ninto that dictionary inserted immediately after the ``\'%\'`` character.\nThe mapping key selects the value to be formatted from the mapping.\nFor example:\n\n>>> print(\'%(language)s has %(number)03d quote types.\' %\n... {\'language\': "Python", "number": 2})\nPython has 002 quote types.\n\nIn this case no ``*`` specifiers may occur in a format (since they\nrequire a sequential parameter list).\n\nThe conversion flag characters are:\n\n+-----------+-----------------------------------------------------------------------+\n| Flag | Meaning |\n+===========+=======================================================================+\n| ``\'#\'`` | The value conversion will use the "alternate form" (where defined |\n| | below). |\n+-----------+-----------------------------------------------------------------------+\n| ``\'0\'`` | The conversion will be zero padded for numeric values. |\n+-----------+-----------------------------------------------------------------------+\n| ``\'-\'`` | The converted value is left adjusted (overrides the ``\'0\'`` |\n| | conversion if both are given). |\n+-----------+-----------------------------------------------------------------------+\n| ``\' \'`` | (a space) A blank should be left before a positive number (or empty |\n| | string) produced by a signed conversion. |\n+-----------+-----------------------------------------------------------------------+\n| ``\'+\'`` | A sign character (``\'+\'`` or ``\'-\'``) will precede the conversion |\n| | (overrides a "space" flag). |\n+-----------+-----------------------------------------------------------------------+\n\nA length modifier (``h``, ``l``, or ``L``) may be present, but is\nignored as it is not necessary for Python -- so e.g. ``%ld`` is\nidentical to ``%d``.\n\nThe conversion types are:\n\n+--------------+-------------------------------------------------------+---------+\n| Conversion | Meaning | Notes |\n+==============+=======================================================+=========+\n| ``\'d\'`` | Signed integer decimal. | |\n+--------------+-------------------------------------------------------+---------+\n| ``\'i\'`` | Signed integer decimal. | |\n+--------------+-------------------------------------------------------+---------+\n| ``\'o\'`` | Signed octal value. | (1) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'u\'`` | Obsolete type -- it is identical to ``\'d\'``. | (7) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'x\'`` | Signed hexadecimal (lowercase). | (2) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'X\'`` | Signed hexadecimal (uppercase). | (2) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'e\'`` | Floating point exponential format (lowercase). | (3) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'E\'`` | Floating point exponential format (uppercase). | (3) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'f\'`` | Floating point decimal format. | (3) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'F\'`` | Floating point decimal format. | (3) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'g\'`` | Floating point format. Uses lowercase exponential | (4) |\n| | format if exponent is less than -4 or not less than | |\n| | precision, decimal format otherwise. | |\n+--------------+-------------------------------------------------------+---------+\n| ``\'G\'`` | Floating point format. Uses uppercase exponential | (4) |\n| | format if exponent is less than -4 or not less than | |\n| | precision, decimal format otherwise. | |\n+--------------+-------------------------------------------------------+---------+\n| ``\'c\'`` | Single character (accepts integer or single character | |\n| | string). | |\n+--------------+-------------------------------------------------------+---------+\n| ``\'r\'`` | String (converts any Python object using ``repr()``). | (5) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'s\'`` | String (converts any Python object using ``str()``). | (5) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'a\'`` | String (converts any Python object using | (5) |\n| | ``ascii()``). | |\n+--------------+-------------------------------------------------------+---------+\n| ``\'%\'`` | No argument is converted, results in a ``\'%\'`` | |\n| | character in the result. | |\n+--------------+-------------------------------------------------------+---------+\n\nNotes:\n\n1. The alternate form causes a leading zero (``\'0\'``) to be inserted\n between left-hand padding and the formatting of the number if the\n leading character of the result is not already a zero.\n\n2. The alternate form causes a leading ``\'0x\'`` or ``\'0X\'`` (depending\n on whether the ``\'x\'`` or ``\'X\'`` format was used) to be inserted\n between left-hand padding and the formatting of the number if the\n leading character of the result is not already a zero.\n\n3. The alternate form causes the result to always contain a decimal\n point, even if no digits follow it.\n\n The precision determines the number of digits after the decimal\n point and defaults to 6.\n\n4. The alternate form causes the result to always contain a decimal\n point, and trailing zeroes are not removed as they would otherwise\n be.\n\n The precision determines the number of significant digits before\n and after the decimal point and defaults to 6.\n\n5. If precision is ``N``, the output is truncated to ``N`` characters.\n\n1. See **PEP 237**.\n\nSince Python strings have an explicit length, ``%s`` conversions do\nnot assume that ``\'\\0\'`` is the end of the string.\n\nChanged in version 3.1: ``%f`` conversions for numbers whose absolute\nvalue is over 1e50 are no longer replaced by ``%g`` conversions.\n\nAdditional string operations are defined in standard modules\n``string`` and ``re``.\n\n\nRange Type\n==========\n\nThe ``range`` type is an immutable sequence which is commonly used for\nlooping. The advantage of the ``range`` type is that an ``range``\nobject will always take the same amount of memory, no matter the size\nof the range it represents.\n\nRange objects have relatively little behavior: they support indexing,\ncontains, iteration, the ``len()`` function, and the following\nmethods:\n\nrange.count(x)\n\n Return the number of *i*\'s for which ``s[i] == x``.\n\n New in version 3.2.\n\nrange.index(x)\n\n Return the smallest *i* such that ``s[i] == x``. Raises\n ``ValueError`` when *x* is not in the range.\n\n New in version 3.2.\n\n\nMutable Sequence Types\n======================\n\nList and bytearray objects support additional operations that allow\nin-place modification of the object. Other mutable sequence types\n(when added to the language) should also support these operations.\nStrings and tuples are immutable sequence types: such objects cannot\nbe modified once created. The following operations are defined on\nmutable sequence types (where *x* is an arbitrary object).\n\nNote that while lists allow their items to be of any type, bytearray\nobject "items" are all integers in the range 0 <= x < 256.\n\n+--------------------------------+----------------------------------+-----------------------+\n| Operation | Result | Notes |\n+================================+==================================+=======================+\n| ``s[i] = x`` | item *i* of *s* is replaced by | |\n| | *x* | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s[i:j] = t`` | slice of *s* from *i* to *j* is | |\n| | replaced by the contents of the | |\n| | iterable *t* | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``del s[i:j]`` | same as ``s[i:j] = []`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s[i:j:k] = t`` | the elements of ``s[i:j:k]`` are | (1) |\n| | replaced by those of *t* | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``del s[i:j:k]`` | removes the elements of | |\n| | ``s[i:j:k]`` from the list | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.append(x)`` | same as ``s[len(s):len(s)] = | |\n| | [x]`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.extend(x)`` | same as ``s[len(s):len(s)] = x`` | (2) |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.clear()`` | remove all items from ``s`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.copy()`` | return a shallow copy of ``s`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.count(x)`` | return number of *i*\'s for which | |\n| | ``s[i] == x`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.index(x[, i[, j]])`` | return smallest *k* such that | (3) |\n| | ``s[k] == x`` and ``i <= k < j`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.insert(i, x)`` | same as ``s[i:i] = [x]`` | (4) |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.pop([i])`` | same as ``x = s[i]; del s[i]; | (5) |\n| | return x`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.remove(x)`` | same as ``del s[s.index(x)]`` | (3) |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.reverse()`` | reverses the items of *s* in | (6) |\n| | place | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.sort([key[, reverse]])`` | sort the items of *s* in place | (6), (7), (8) |\n+--------------------------------+----------------------------------+-----------------------+\n\nNotes:\n\n1. *t* must have the same length as the slice it is replacing.\n\n2. *x* can be any iterable object.\n\n3. Raises ``ValueError`` when *x* is not found in *s*. When a negative\n index is passed as the second or third parameter to the ``index()``\n method, the sequence length is added, as for slice indices. If it\n is still negative, it is truncated to zero, as for slice indices.\n\n4. When a negative index is passed as the first parameter to the\n ``insert()`` method, the sequence length is added, as for slice\n indices. If it is still negative, it is truncated to zero, as for\n slice indices.\n\n5. The optional argument *i* defaults to ``-1``, so that by default\n the last item is removed and returned.\n\n6. The ``sort()`` and ``reverse()`` methods modify the sequence in\n place for economy of space when sorting or reversing a large\n sequence. To remind you that they operate by side effect, they\n don\'t return the sorted or reversed sequence.\n\n7. The ``sort()`` method takes optional arguments for controlling the\n comparisons. Each must be specified as a keyword argument.\n\n *key* specifies a function of one argument that is used to extract\n a comparison key from each list element: ``key=str.lower``. The\n default value is ``None``. Use ``functools.cmp_to_key()`` to\n convert an old-style *cmp* function to a *key* function.\n\n *reverse* is a boolean value. If set to ``True``, then the list\n elements are sorted as if each comparison were reversed.\n\n The ``sort()`` method is guaranteed to be stable. A sort is stable\n if it guarantees not to change the relative order of elements that\n compare equal --- this is helpful for sorting in multiple passes\n (for example, sort by department, then by salary grade).\n\n **CPython implementation detail:** While a list is being sorted,\n the effect of attempting to mutate, or even inspect, the list is\n undefined. The C implementation of Python makes the list appear\n empty for the duration, and raises ``ValueError`` if it can detect\n that the list has been mutated during a sort.\n\n8. ``sort()`` is not supported by ``bytearray`` objects.\n\n New in version 3.3: ``clear()`` and ``copy()`` methods.\n\n\nBytes and Byte Array Methods\n============================\n\nBytes and bytearray objects, being "strings of bytes", have all\nmethods found on strings, with the exception of ``encode()``,\n``format()`` and ``isidentifier()``, which do not make sense with\nthese types. For converting the objects to strings, they have a\n``decode()`` method.\n\nWherever one of these methods needs to interpret the bytes as\ncharacters (e.g. the ``is...()`` methods), the ASCII character set is\nassumed.\n\nNew in version 3.3: The functions ``count()``, ``find()``,\n``index()``, ``rfind()`` and ``rindex()`` have additional semantics\ncompared to the corresponding string functions: They also accept an\ninteger in range 0 to 255 (a byte) as their first argument.\n\nNote: The methods on bytes and bytearray objects don\'t accept strings as\n their arguments, just as the methods on strings don\'t accept bytes\n as their arguments. For example, you have to write\n\n a = "abc"\n b = a.replace("a", "f")\n\n and\n\n a = b"abc"\n b = a.replace(b"a", b"f")\n\nbytes.decode(encoding="utf-8", errors="strict")\nbytearray.decode(encoding="utf-8", errors="strict")\n\n Return a string decoded from the given bytes. Default encoding is\n ``\'utf-8\'``. *errors* may be given to set a different error\n handling scheme. The default for *errors* is ``\'strict\'``, meaning\n that encoding errors raise a ``UnicodeError``. Other possible\n values are ``\'ignore\'``, ``\'replace\'`` and any other name\n registered via ``codecs.register_error()``, see section *Codec Base\n Classes*. For a list of possible encodings, see section *Standard\n Encodings*.\n\n Changed in version 3.1: Added support for keyword arguments.\n\nThe bytes and bytearray types have an additional class method:\n\nclassmethod bytes.fromhex(string)\nclassmethod bytearray.fromhex(string)\n\n This ``bytes`` class method returns a bytes or bytearray object,\n decoding the given string object. The string must contain two\n hexadecimal digits per byte, spaces are ignored.\n\n >>> bytes.fromhex(\'f0 f1f2 \')\n b\'\\xf0\\xf1\\xf2\'\n\nThe maketrans and translate methods differ in semantics from the\nversions available on strings:\n\nbytes.translate(table[, delete])\nbytearray.translate(table[, delete])\n\n Return a copy of the bytes or bytearray object where all bytes\n occurring in the optional argument *delete* are removed, and the\n remaining bytes have been mapped through the given translation\n table, which must be a bytes object of length 256.\n\n You can use the ``bytes.maketrans()`` method to create a\n translation table.\n\n Set the *table* argument to ``None`` for translations that only\n delete characters:\n\n >>> b\'read this short text\'.translate(None, b\'aeiou\')\n b\'rd ths shrt txt\'\n\nstatic bytes.maketrans(from, to)\nstatic bytearray.maketrans(from, to)\n\n This static method returns a translation table usable for\n ``bytes.translate()`` that will map each character in *from* into\n the character at the same position in *to*; *from* and *to* must be\n bytes objects and have the same length.\n\n New in version 3.1.\n', + 'typesseq': '\nSequence Types --- ``str``, ``bytes``, ``bytearray``, ``list``, ``tuple``, ``range``\n************************************************************************************\n\nThere are six sequence types: strings, byte sequences (``bytes``\nobjects), byte arrays (``bytearray`` objects), lists, tuples, and\nrange objects. For other containers see the built in ``dict`` and\n``set`` classes, and the ``collections`` module.\n\nStrings contain Unicode characters. Their literals are written in\nsingle or double quotes: ``\'xyzzy\'``, ``"frobozz"``. See *String and\nBytes literals* for more about string literals. In addition to the\nfunctionality described here, there are also string-specific methods\ndescribed in the *String Methods* section.\n\nBytes and bytearray objects contain single bytes -- the former is\nimmutable while the latter is a mutable sequence. Bytes objects can\nbe constructed the constructor, ``bytes()``, and from literals; use a\n``b`` prefix with normal string syntax: ``b\'xyzzy\'``. To construct\nbyte arrays, use the ``bytearray()`` function.\n\nWhile string objects are sequences of characters (represented by\nstrings of length 1), bytes and bytearray objects are sequences of\n*integers* (between 0 and 255), representing the ASCII value of single\nbytes. That means that for a bytes or bytearray object *b*, ``b[0]``\nwill be an integer, while ``b[0:1]`` will be a bytes or bytearray\nobject of length 1. The representation of bytes objects uses the\nliteral format (``b\'...\'``) since it is generally more useful than\ne.g. ``bytes([50, 19, 100])``. You can always convert a bytes object\ninto a list of integers using ``list(b)``.\n\nAlso, while in previous Python versions, byte strings and Unicode\nstrings could be exchanged for each other rather freely (barring\nencoding issues), strings and bytes are now completely separate\nconcepts. There\'s no implicit en-/decoding if you pass an object of\nthe wrong type. A string always compares unequal to a bytes or\nbytearray object.\n\nLists are constructed with square brackets, separating items with\ncommas: ``[a, b, c]``. Tuples are constructed by the comma operator\n(not within square brackets), with or without enclosing parentheses,\nbut an empty tuple must have the enclosing parentheses, such as ``a,\nb, c`` or ``()``. A single item tuple must have a trailing comma,\nsuch as ``(d,)``.\n\nObjects of type range are created using the ``range()`` function.\nThey don\'t support concatenation or repetition, and using ``min()`` or\n``max()`` on them is inefficient.\n\nMost sequence types support the following operations. The ``in`` and\n``not in`` operations have the same priorities as the comparison\noperations. The ``+`` and ``*`` operations have the same priority as\nthe corresponding numeric operations. [3] Additional methods are\nprovided for *Mutable Sequence Types*.\n\nThis table lists the sequence operations sorted in ascending priority\n(operations in the same box have the same priority). In the table,\n*s* and *t* are sequences of the same type; *n*, *i*, *j* and *k* are\nintegers.\n\n+--------------------+----------------------------------+------------+\n| Operation | Result | Notes |\n+====================+==================================+============+\n| ``x in s`` | ``True`` if an item of *s* is | (1) |\n| | equal to *x*, else ``False`` | |\n+--------------------+----------------------------------+------------+\n| ``x not in s`` | ``False`` if an item of *s* is | (1) |\n| | equal to *x*, else ``True`` | |\n+--------------------+----------------------------------+------------+\n| ``s + t`` | the concatenation of *s* and *t* | (6) |\n+--------------------+----------------------------------+------------+\n| ``s * n, n * s`` | *n* shallow copies of *s* | (2) |\n| | concatenated | |\n+--------------------+----------------------------------+------------+\n| ``s[i]`` | *i*th item of *s*, origin 0 | (3) |\n+--------------------+----------------------------------+------------+\n| ``s[i:j]`` | slice of *s* from *i* to *j* | (3)(4) |\n+--------------------+----------------------------------+------------+\n| ``s[i:j:k]`` | slice of *s* from *i* to *j* | (3)(5) |\n| | with step *k* | |\n+--------------------+----------------------------------+------------+\n| ``len(s)`` | length of *s* | |\n+--------------------+----------------------------------+------------+\n| ``min(s)`` | smallest item of *s* | |\n+--------------------+----------------------------------+------------+\n| ``max(s)`` | largest item of *s* | |\n+--------------------+----------------------------------+------------+\n| ``s.index(i)`` | index of the first occurence of | |\n| | *i* in *s* | |\n+--------------------+----------------------------------+------------+\n| ``s.count(i)`` | total number of occurences of | |\n| | *i* in *s* | |\n+--------------------+----------------------------------+------------+\n\nSequence types also support comparisons. In particular, tuples and\nlists are compared lexicographically by comparing corresponding\nelements. This means that to compare equal, every element must\ncompare equal and the two sequences must be of the same type and have\nthe same length. (For full details see *Comparisons* in the language\nreference.)\n\nNotes:\n\n1. When *s* is a string object, the ``in`` and ``not in`` operations\n act like a substring test.\n\n2. Values of *n* less than ``0`` are treated as ``0`` (which yields an\n empty sequence of the same type as *s*). Note also that the copies\n are shallow; nested structures are not copied. This often haunts\n new Python programmers; consider:\n\n >>> lists = [[]] * 3\n >>> lists\n [[], [], []]\n >>> lists[0].append(3)\n >>> lists\n [[3], [3], [3]]\n\n What has happened is that ``[[]]`` is a one-element list containing\n an empty list, so all three elements of ``[[]] * 3`` are (pointers\n to) this single empty list. Modifying any of the elements of\n ``lists`` modifies this single list. You can create a list of\n different lists this way:\n\n >>> lists = [[] for i in range(3)]\n >>> lists[0].append(3)\n >>> lists[1].append(5)\n >>> lists[2].append(7)\n >>> lists\n [[3], [5], [7]]\n\n3. If *i* or *j* is negative, the index is relative to the end of the\n string: ``len(s) + i`` or ``len(s) + j`` is substituted. But note\n that ``-0`` is still ``0``.\n\n4. The slice of *s* from *i* to *j* is defined as the sequence of\n items with index *k* such that ``i <= k < j``. If *i* or *j* is\n greater than ``len(s)``, use ``len(s)``. If *i* is omitted or\n ``None``, use ``0``. If *j* is omitted or ``None``, use\n ``len(s)``. If *i* is greater than or equal to *j*, the slice is\n empty.\n\n5. The slice of *s* from *i* to *j* with step *k* is defined as the\n sequence of items with index ``x = i + n*k`` such that ``0 <= n <\n (j-i)/k``. In other words, the indices are ``i``, ``i+k``,\n ``i+2*k``, ``i+3*k`` and so on, stopping when *j* is reached (but\n never including *j*). If *i* or *j* is greater than ``len(s)``,\n use ``len(s)``. If *i* or *j* are omitted or ``None``, they become\n "end" values (which end depends on the sign of *k*). Note, *k*\n cannot be zero. If *k* is ``None``, it is treated like ``1``.\n\n6. Concatenating immutable strings always results in a new object.\n This means that building up a string by repeated concatenation will\n have a quadratic runtime cost in the total string length. To get a\n linear runtime cost, you must switch to one of the alternatives\n below:\n\n * if concatenating ``str`` objects, you can build a list and use\n ``str.join()`` at the end;\n\n * if concatenating ``bytes`` objects, you can similarly use\n ``bytes.join()``, or you can do in-place concatenation with a\n ``bytearray`` object. ``bytearray`` objects are mutable and have\n an efficient overallocation mechanism.\n\n\nString Methods\n==============\n\nString objects support the methods listed below.\n\nIn addition, Python\'s strings support the sequence type methods\ndescribed in the *Sequence Types --- str, bytes, bytearray, list,\ntuple, range* section. To output formatted strings, see the *String\nFormatting* section. Also, see the ``re`` module for string functions\nbased on regular expressions.\n\nstr.capitalize()\n\n Return a copy of the string with its first character capitalized\n and the rest lowercased.\n\nstr.casefold()\n\n Return a casefolded copy of the string. Casefolded strings may be\n used for caseless matching.\n\n Casefolding is similar to lowercasing but more aggressive because\n it is intended to remove all case distinctions in a string. For\n example, the German lowercase letter ``\'\xc3\x9f\'`` is equivalent to\n ``"ss"``. Since it is already lowercase, ``lower()`` would do\n nothing to ``\'\xc3\x9f\'``; ``casefold()`` converts it to ``"ss"``.\n\n The casefolding algorithm is described in section 3.13 of the\n Unicode Standard.\n\n New in version 3.3.\n\nstr.center(width[, fillchar])\n\n Return centered in a string of length *width*. Padding is done\n using the specified *fillchar* (default is a space).\n\nstr.count(sub[, start[, end]])\n\n Return the number of non-overlapping occurrences of substring *sub*\n in the range [*start*, *end*]. Optional arguments *start* and\n *end* are interpreted as in slice notation.\n\nstr.encode(encoding="utf-8", errors="strict")\n\n Return an encoded version of the string as a bytes object. Default\n encoding is ``\'utf-8\'``. *errors* may be given to set a different\n error handling scheme. The default for *errors* is ``\'strict\'``,\n meaning that encoding errors raise a ``UnicodeError``. Other\n possible values are ``\'ignore\'``, ``\'replace\'``,\n ``\'xmlcharrefreplace\'``, ``\'backslashreplace\'`` and any other name\n registered via ``codecs.register_error()``, see section *Codec Base\n Classes*. For a list of possible encodings, see section *Standard\n Encodings*.\n\n Changed in version 3.1: Support for keyword arguments added.\n\nstr.endswith(suffix[, start[, end]])\n\n Return ``True`` if the string ends with the specified *suffix*,\n otherwise return ``False``. *suffix* can also be a tuple of\n suffixes to look for. With optional *start*, test beginning at\n that position. With optional *end*, stop comparing at that\n position.\n\nstr.expandtabs([tabsize])\n\n Return a copy of the string where all tab characters are replaced\n by zero or more spaces, depending on the current column and the\n given tab size. The column number is reset to zero after each\n newline occurring in the string. If *tabsize* is not given, a tab\n size of ``8`` characters is assumed. This doesn\'t understand other\n non-printing characters or escape sequences.\n\nstr.find(sub[, start[, end]])\n\n Return the lowest index in the string where substring *sub* is\n found, such that *sub* is contained in the slice ``s[start:end]``.\n Optional arguments *start* and *end* are interpreted as in slice\n notation. Return ``-1`` if *sub* is not found.\n\n Note: The ``find()`` method should be used only if you need to know the\n position of *sub*. To check if *sub* is a substring or not, use\n the ``in`` operator:\n\n >>> \'Py\' in \'Python\'\n True\n\nstr.format(*args, **kwargs)\n\n Perform a string formatting operation. The string on which this\n method is called can contain literal text or replacement fields\n delimited by braces ``{}``. Each replacement field contains either\n the numeric index of a positional argument, or the name of a\n keyword argument. Returns a copy of the string where each\n replacement field is replaced with the string value of the\n corresponding argument.\n\n >>> "The sum of 1 + 2 is {0}".format(1+2)\n \'The sum of 1 + 2 is 3\'\n\n See *Format String Syntax* for a description of the various\n formatting options that can be specified in format strings.\n\nstr.format_map(mapping)\n\n Similar to ``str.format(**mapping)``, except that ``mapping`` is\n used directly and not copied to a ``dict`` . This is useful if for\n example ``mapping`` is a dict subclass:\n\n >>> class Default(dict):\n ... def __missing__(self, key):\n ... return key\n ...\n >>> \'{name} was born in {country}\'.format_map(Default(name=\'Guido\'))\n \'Guido was born in country\'\n\n New in version 3.2.\n\nstr.index(sub[, start[, end]])\n\n Like ``find()``, but raise ``ValueError`` when the substring is not\n found.\n\nstr.isalnum()\n\n Return true if all characters in the string are alphanumeric and\n there is at least one character, false otherwise. A character\n ``c`` is alphanumeric if one of the following returns ``True``:\n ``c.isalpha()``, ``c.isdecimal()``, ``c.isdigit()``, or\n ``c.isnumeric()``.\n\nstr.isalpha()\n\n Return true if all characters in the string are alphabetic and\n there is at least one character, false otherwise. Alphabetic\n characters are those characters defined in the Unicode character\n database as "Letter", i.e., those with general category property\n being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note that this is\n different from the "Alphabetic" property defined in the Unicode\n Standard.\n\nstr.isdecimal()\n\n Return true if all characters in the string are decimal characters\n and there is at least one character, false otherwise. Decimal\n characters are those from general category "Nd". This category\n includes digit characters, and all characters that can be used to\n form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.\n\nstr.isdigit()\n\n Return true if all characters in the string are digits and there is\n at least one character, false otherwise. Digits include decimal\n characters and digits that need special handling, such as the\n compatibility superscript digits. Formally, a digit is a character\n that has the property value Numeric_Type=Digit or\n Numeric_Type=Decimal.\n\nstr.isidentifier()\n\n Return true if the string is a valid identifier according to the\n language definition, section *Identifiers and keywords*.\n\nstr.islower()\n\n Return true if all cased characters [4] in the string are lowercase\n and there is at least one cased character, false otherwise.\n\nstr.isnumeric()\n\n Return true if all characters in the string are numeric characters,\n and there is at least one character, false otherwise. Numeric\n characters include digit characters, and all characters that have\n the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION\n ONE FIFTH. Formally, numeric characters are those with the\n property value Numeric_Type=Digit, Numeric_Type=Decimal or\n Numeric_Type=Numeric.\n\nstr.isprintable()\n\n Return true if all characters in the string are printable or the\n string is empty, false otherwise. Nonprintable characters are\n those characters defined in the Unicode character database as\n "Other" or "Separator", excepting the ASCII space (0x20) which is\n considered printable. (Note that printable characters in this\n context are those which should not be escaped when ``repr()`` is\n invoked on a string. It has no bearing on the handling of strings\n written to ``sys.stdout`` or ``sys.stderr``.)\n\nstr.isspace()\n\n Return true if there are only whitespace characters in the string\n and there is at least one character, false otherwise. Whitespace\n characters are those characters defined in the Unicode character\n database as "Other" or "Separator" and those with bidirectional\n property being one of "WS", "B", or "S".\n\nstr.istitle()\n\n Return true if the string is a titlecased string and there is at\n least one character, for example uppercase characters may only\n follow uncased characters and lowercase characters only cased ones.\n Return false otherwise.\n\nstr.isupper()\n\n Return true if all cased characters [4] in the string are uppercase\n and there is at least one cased character, false otherwise.\n\nstr.join(iterable)\n\n Return a string which is the concatenation of the strings in the\n *iterable* *iterable*. A ``TypeError`` will be raised if there are\n any non-string values in *iterable*, including ``bytes`` objects.\n The separator between elements is the string providing this method.\n\nstr.ljust(width[, fillchar])\n\n Return the string left justified in a string of length *width*.\n Padding is done using the specified *fillchar* (default is a\n space). The original string is returned if *width* is less than or\n equal to ``len(s)``.\n\nstr.lower()\n\n Return a copy of the string with all the cased characters [4]\n converted to lowercase.\n\n The lowercasing algorithm used is described in section 3.13 of the\n Unicode Standard.\n\nstr.lstrip([chars])\n\n Return a copy of the string with leading characters removed. The\n *chars* argument is a string specifying the set of characters to be\n removed. If omitted or ``None``, the *chars* argument defaults to\n removing whitespace. The *chars* argument is not a prefix; rather,\n all combinations of its values are stripped:\n\n >>> \' spacious \'.lstrip()\n \'spacious \'\n >>> \'www.example.com\'.lstrip(\'cmowz.\')\n \'example.com\'\n\nstatic str.maketrans(x[, y[, z]])\n\n This static method returns a translation table usable for\n ``str.translate()``.\n\n If there is only one argument, it must be a dictionary mapping\n Unicode ordinals (integers) or characters (strings of length 1) to\n Unicode ordinals, strings (of arbitrary lengths) or None.\n Character keys will then be converted to ordinals.\n\n If there are two arguments, they must be strings of equal length,\n and in the resulting dictionary, each character in x will be mapped\n to the character at the same position in y. If there is a third\n argument, it must be a string, whose characters will be mapped to\n None in the result.\n\nstr.partition(sep)\n\n Split the string at the first occurrence of *sep*, and return a\n 3-tuple containing the part before the separator, the separator\n itself, and the part after the separator. If the separator is not\n found, return a 3-tuple containing the string itself, followed by\n two empty strings.\n\nstr.replace(old, new[, count])\n\n Return a copy of the string with all occurrences of substring *old*\n replaced by *new*. If the optional argument *count* is given, only\n the first *count* occurrences are replaced.\n\nstr.rfind(sub[, start[, end]])\n\n Return the highest index in the string where substring *sub* is\n found, such that *sub* is contained within ``s[start:end]``.\n Optional arguments *start* and *end* are interpreted as in slice\n notation. Return ``-1`` on failure.\n\nstr.rindex(sub[, start[, end]])\n\n Like ``rfind()`` but raises ``ValueError`` when the substring *sub*\n is not found.\n\nstr.rjust(width[, fillchar])\n\n Return the string right justified in a string of length *width*.\n Padding is done using the specified *fillchar* (default is a\n space). The original string is returned if *width* is less than or\n equal to ``len(s)``.\n\nstr.rpartition(sep)\n\n Split the string at the last occurrence of *sep*, and return a\n 3-tuple containing the part before the separator, the separator\n itself, and the part after the separator. If the separator is not\n found, return a 3-tuple containing two empty strings, followed by\n the string itself.\n\nstr.rsplit(sep=None, maxsplit=-1)\n\n Return a list of the words in the string, using *sep* as the\n delimiter string. If *maxsplit* is given, at most *maxsplit* splits\n are done, the *rightmost* ones. If *sep* is not specified or\n ``None``, any whitespace string is a separator. Except for\n splitting from the right, ``rsplit()`` behaves like ``split()``\n which is described in detail below.\n\nstr.rstrip([chars])\n\n Return a copy of the string with trailing characters removed. The\n *chars* argument is a string specifying the set of characters to be\n removed. If omitted or ``None``, the *chars* argument defaults to\n removing whitespace. The *chars* argument is not a suffix; rather,\n all combinations of its values are stripped:\n\n >>> \' spacious \'.rstrip()\n \' spacious\'\n >>> \'mississippi\'.rstrip(\'ipz\')\n \'mississ\'\n\nstr.split(sep=None, maxsplit=-1)\n\n Return a list of the words in the string, using *sep* as the\n delimiter string. If *maxsplit* is given, at most *maxsplit*\n splits are done (thus, the list will have at most ``maxsplit+1``\n elements). If *maxsplit* is not specified, then there is no limit\n on the number of splits (all possible splits are made).\n\n If *sep* is given, consecutive delimiters are not grouped together\n and are deemed to delimit empty strings (for example,\n ``\'1,,2\'.split(\',\')`` returns ``[\'1\', \'\', \'2\']``). The *sep*\n argument may consist of multiple characters (for example,\n ``\'1<>2<>3\'.split(\'<>\')`` returns ``[\'1\', \'2\', \'3\']``). Splitting\n an empty string with a specified separator returns ``[\'\']``.\n\n If *sep* is not specified or is ``None``, a different splitting\n algorithm is applied: runs of consecutive whitespace are regarded\n as a single separator, and the result will contain no empty strings\n at the start or end if the string has leading or trailing\n whitespace. Consequently, splitting an empty string or a string\n consisting of just whitespace with a ``None`` separator returns\n ``[]``.\n\n For example, ``\' 1 2 3 \'.split()`` returns ``[\'1\', \'2\', \'3\']``,\n and ``\' 1 2 3 \'.split(None, 1)`` returns ``[\'1\', \'2 3 \']``.\n\nstr.splitlines([keepends])\n\n Return a list of the lines in the string, breaking at line\n boundaries. Line breaks are not included in the resulting list\n unless *keepends* is given and true.\n\nstr.startswith(prefix[, start[, end]])\n\n Return ``True`` if string starts with the *prefix*, otherwise\n return ``False``. *prefix* can also be a tuple of prefixes to look\n for. With optional *start*, test string beginning at that\n position. With optional *end*, stop comparing string at that\n position.\n\nstr.strip([chars])\n\n Return a copy of the string with the leading and trailing\n characters removed. The *chars* argument is a string specifying the\n set of characters to be removed. If omitted or ``None``, the\n *chars* argument defaults to removing whitespace. The *chars*\n argument is not a prefix or suffix; rather, all combinations of its\n values are stripped:\n\n >>> \' spacious \'.strip()\n \'spacious\'\n >>> \'www.example.com\'.strip(\'cmowz.\')\n \'example\'\n\nstr.swapcase()\n\n Return a copy of the string with uppercase characters converted to\n lowercase and vice versa. Note that it is not necessarily true that\n ``s.swapcase().swapcase() == s``.\n\nstr.title()\n\n Return a titlecased version of the string where words start with an\n uppercase character and the remaining characters are lowercase.\n\n The algorithm uses a simple language-independent definition of a\n word as groups of consecutive letters. The definition works in\n many contexts but it means that apostrophes in contractions and\n possessives form word boundaries, which may not be the desired\n result:\n\n >>> "they\'re bill\'s friends from the UK".title()\n "They\'Re Bill\'S Friends From The Uk"\n\n A workaround for apostrophes can be constructed using regular\n expressions:\n\n >>> import re\n >>> def titlecase(s):\n return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n lambda mo: mo.group(0)[0].upper() +\n mo.group(0)[1:].lower(),\n s)\n\n >>> titlecase("they\'re bill\'s friends.")\n "They\'re Bill\'s Friends."\n\nstr.translate(map)\n\n Return a copy of the *s* where all characters have been mapped\n through the *map* which must be a dictionary of Unicode ordinals\n (integers) to Unicode ordinals, strings or ``None``. Unmapped\n characters are left untouched. Characters mapped to ``None`` are\n deleted.\n\n You can use ``str.maketrans()`` to create a translation map from\n character-to-character mappings in different formats.\n\n Note: An even more flexible approach is to create a custom character\n mapping codec using the ``codecs`` module (see\n ``encodings.cp1251`` for an example).\n\nstr.upper()\n\n Return a copy of the string with all the cased characters [4]\n converted to uppercase. Note that ``str.upper().isupper()`` might\n be ``False`` if ``s`` contains uncased characters or if the Unicode\n category of the resulting character(s) is not "Lu" (Letter,\n uppercase), but e.g. "Lt" (Letter, titlecase).\n\n The uppercasing algorithm used is described in section 3.13 of the\n Unicode Standard.\n\nstr.zfill(width)\n\n Return the numeric string left filled with zeros in a string of\n length *width*. A sign prefix is handled correctly. The original\n string is returned if *width* is less than or equal to ``len(s)``.\n\n\nOld String Formatting Operations\n================================\n\nNote: The formatting operations described here are modelled on C\'s\n printf() syntax. They only support formatting of certain builtin\n types. The use of a binary operator means that care may be needed\n in order to format tuples and dictionaries correctly. As the new\n *String Formatting* syntax is more flexible and handles tuples and\n dictionaries naturally, it is recommended for new code. However,\n there are no current plans to deprecate printf-style formatting.\n\nString objects have one unique built-in operation: the ``%`` operator\n(modulo). This is also known as the string *formatting* or\n*interpolation* operator. Given ``format % values`` (where *format* is\na string), ``%`` conversion specifications in *format* are replaced\nwith zero or more elements of *values*. The effect is similar to the\nusing ``sprintf()`` in the C language.\n\nIf *format* requires a single argument, *values* may be a single non-\ntuple object. [5] Otherwise, *values* must be a tuple with exactly\nthe number of items specified by the format string, or a single\nmapping object (for example, a dictionary).\n\nA conversion specifier contains two or more characters and has the\nfollowing components, which must occur in this order:\n\n1. The ``\'%\'`` character, which marks the start of the specifier.\n\n2. Mapping key (optional), consisting of a parenthesised sequence of\n characters (for example, ``(somename)``).\n\n3. Conversion flags (optional), which affect the result of some\n conversion types.\n\n4. Minimum field width (optional). If specified as an ``\'*\'``\n (asterisk), the actual width is read from the next element of the\n tuple in *values*, and the object to convert comes after the\n minimum field width and optional precision.\n\n5. Precision (optional), given as a ``\'.\'`` (dot) followed by the\n precision. If specified as ``\'*\'`` (an asterisk), the actual\n precision is read from the next element of the tuple in *values*,\n and the value to convert comes after the precision.\n\n6. Length modifier (optional).\n\n7. Conversion type.\n\nWhen the right argument is a dictionary (or other mapping type), then\nthe formats in the string *must* include a parenthesised mapping key\ninto that dictionary inserted immediately after the ``\'%\'`` character.\nThe mapping key selects the value to be formatted from the mapping.\nFor example:\n\n>>> print(\'%(language)s has %(number)03d quote types.\' %\n... {\'language\': "Python", "number": 2})\nPython has 002 quote types.\n\nIn this case no ``*`` specifiers may occur in a format (since they\nrequire a sequential parameter list).\n\nThe conversion flag characters are:\n\n+-----------+-----------------------------------------------------------------------+\n| Flag | Meaning |\n+===========+=======================================================================+\n| ``\'#\'`` | The value conversion will use the "alternate form" (where defined |\n| | below). |\n+-----------+-----------------------------------------------------------------------+\n| ``\'0\'`` | The conversion will be zero padded for numeric values. |\n+-----------+-----------------------------------------------------------------------+\n| ``\'-\'`` | The converted value is left adjusted (overrides the ``\'0\'`` |\n| | conversion if both are given). |\n+-----------+-----------------------------------------------------------------------+\n| ``\' \'`` | (a space) A blank should be left before a positive number (or empty |\n| | string) produced by a signed conversion. |\n+-----------+-----------------------------------------------------------------------+\n| ``\'+\'`` | A sign character (``\'+\'`` or ``\'-\'``) will precede the conversion |\n| | (overrides a "space" flag). |\n+-----------+-----------------------------------------------------------------------+\n\nA length modifier (``h``, ``l``, or ``L``) may be present, but is\nignored as it is not necessary for Python -- so e.g. ``%ld`` is\nidentical to ``%d``.\n\nThe conversion types are:\n\n+--------------+-------------------------------------------------------+---------+\n| Conversion | Meaning | Notes |\n+==============+=======================================================+=========+\n| ``\'d\'`` | Signed integer decimal. | |\n+--------------+-------------------------------------------------------+---------+\n| ``\'i\'`` | Signed integer decimal. | |\n+--------------+-------------------------------------------------------+---------+\n| ``\'o\'`` | Signed octal value. | (1) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'u\'`` | Obsolete type -- it is identical to ``\'d\'``. | (7) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'x\'`` | Signed hexadecimal (lowercase). | (2) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'X\'`` | Signed hexadecimal (uppercase). | (2) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'e\'`` | Floating point exponential format (lowercase). | (3) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'E\'`` | Floating point exponential format (uppercase). | (3) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'f\'`` | Floating point decimal format. | (3) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'F\'`` | Floating point decimal format. | (3) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'g\'`` | Floating point format. Uses lowercase exponential | (4) |\n| | format if exponent is less than -4 or not less than | |\n| | precision, decimal format otherwise. | |\n+--------------+-------------------------------------------------------+---------+\n| ``\'G\'`` | Floating point format. Uses uppercase exponential | (4) |\n| | format if exponent is less than -4 or not less than | |\n| | precision, decimal format otherwise. | |\n+--------------+-------------------------------------------------------+---------+\n| ``\'c\'`` | Single character (accepts integer or single character | |\n| | string). | |\n+--------------+-------------------------------------------------------+---------+\n| ``\'r\'`` | String (converts any Python object using ``repr()``). | (5) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'s\'`` | String (converts any Python object using ``str()``). | (5) |\n+--------------+-------------------------------------------------------+---------+\n| ``\'a\'`` | String (converts any Python object using | (5) |\n| | ``ascii()``). | |\n+--------------+-------------------------------------------------------+---------+\n| ``\'%\'`` | No argument is converted, results in a ``\'%\'`` | |\n| | character in the result. | |\n+--------------+-------------------------------------------------------+---------+\n\nNotes:\n\n1. The alternate form causes a leading zero (``\'0\'``) to be inserted\n between left-hand padding and the formatting of the number if the\n leading character of the result is not already a zero.\n\n2. The alternate form causes a leading ``\'0x\'`` or ``\'0X\'`` (depending\n on whether the ``\'x\'`` or ``\'X\'`` format was used) to be inserted\n between left-hand padding and the formatting of the number if the\n leading character of the result is not already a zero.\n\n3. The alternate form causes the result to always contain a decimal\n point, even if no digits follow it.\n\n The precision determines the number of digits after the decimal\n point and defaults to 6.\n\n4. The alternate form causes the result to always contain a decimal\n point, and trailing zeroes are not removed as they would otherwise\n be.\n\n The precision determines the number of significant digits before\n and after the decimal point and defaults to 6.\n\n5. If precision is ``N``, the output is truncated to ``N`` characters.\n\n1. See **PEP 237**.\n\nSince Python strings have an explicit length, ``%s`` conversions do\nnot assume that ``\'\\0\'`` is the end of the string.\n\nChanged in version 3.1: ``%f`` conversions for numbers whose absolute\nvalue is over 1e50 are no longer replaced by ``%g`` conversions.\n\nAdditional string operations are defined in standard modules\n``string`` and ``re``.\n\n\nRange Type\n==========\n\nThe ``range`` type is an immutable sequence which is commonly used for\nlooping. The advantage of the ``range`` type is that an ``range``\nobject will always take the same amount of memory, no matter the size\nof the range it represents.\n\nRange objects have relatively little behavior: they support indexing,\ncontains, iteration, the ``len()`` function, and the following\nmethods:\n\nrange.count(x)\n\n Return the number of *i*\'s for which ``s[i] == x``.\n\n New in version 3.2.\n\nrange.index(x)\n\n Return the smallest *i* such that ``s[i] == x``. Raises\n ``ValueError`` when *x* is not in the range.\n\n New in version 3.2.\n\n\nMutable Sequence Types\n======================\n\nList and bytearray objects support additional operations that allow\nin-place modification of the object. Other mutable sequence types\n(when added to the language) should also support these operations.\nStrings and tuples are immutable sequence types: such objects cannot\nbe modified once created. The following operations are defined on\nmutable sequence types (where *x* is an arbitrary object).\n\nNote that while lists allow their items to be of any type, bytearray\nobject "items" are all integers in the range 0 <= x < 256.\n\n+--------------------------------+----------------------------------+-----------------------+\n| Operation | Result | Notes |\n+================================+==================================+=======================+\n| ``s[i] = x`` | item *i* of *s* is replaced by | |\n| | *x* | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s[i:j] = t`` | slice of *s* from *i* to *j* is | |\n| | replaced by the contents of the | |\n| | iterable *t* | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``del s[i:j]`` | same as ``s[i:j] = []`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s[i:j:k] = t`` | the elements of ``s[i:j:k]`` are | (1) |\n| | replaced by those of *t* | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``del s[i:j:k]`` | removes the elements of | |\n| | ``s[i:j:k]`` from the list | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.append(x)`` | same as ``s[len(s):len(s)] = | |\n| | [x]`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.extend(x)`` | same as ``s[len(s):len(s)] = x`` | (2) |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.clear()`` | remove all items from ``s`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.copy()`` | return a shallow copy of ``s`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.count(x)`` | return number of *i*\'s for which | |\n| | ``s[i] == x`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.index(x[, i[, j]])`` | return smallest *k* such that | (3) |\n| | ``s[k] == x`` and ``i <= k < j`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.insert(i, x)`` | same as ``s[i:i] = [x]`` | (4) |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.pop([i])`` | same as ``x = s[i]; del s[i]; | (5) |\n| | return x`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.remove(x)`` | same as ``del s[s.index(x)]`` | (3) |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.reverse()`` | reverses the items of *s* in | (6) |\n| | place | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.sort([key[, reverse]])`` | sort the items of *s* in place | (6), (7), (8) |\n+--------------------------------+----------------------------------+-----------------------+\n\nNotes:\n\n1. *t* must have the same length as the slice it is replacing.\n\n2. *x* can be any iterable object.\n\n3. Raises ``ValueError`` when *x* is not found in *s*. When a negative\n index is passed as the second or third parameter to the ``index()``\n method, the sequence length is added, as for slice indices. If it\n is still negative, it is truncated to zero, as for slice indices.\n\n4. When a negative index is passed as the first parameter to the\n ``insert()`` method, the sequence length is added, as for slice\n indices. If it is still negative, it is truncated to zero, as for\n slice indices.\n\n5. The optional argument *i* defaults to ``-1``, so that by default\n the last item is removed and returned.\n\n6. The ``sort()`` and ``reverse()`` methods modify the sequence in\n place for economy of space when sorting or reversing a large\n sequence. To remind you that they operate by side effect, they\n don\'t return the sorted or reversed sequence.\n\n7. The ``sort()`` method takes optional arguments for controlling the\n comparisons. Each must be specified as a keyword argument.\n\n *key* specifies a function of one argument that is used to extract\n a comparison key from each list element: ``key=str.lower``. The\n default value is ``None``. Use ``functools.cmp_to_key()`` to\n convert an old-style *cmp* function to a *key* function.\n\n *reverse* is a boolean value. If set to ``True``, then the list\n elements are sorted as if each comparison were reversed.\n\n The ``sort()`` method is guaranteed to be stable. A sort is stable\n if it guarantees not to change the relative order of elements that\n compare equal --- this is helpful for sorting in multiple passes\n (for example, sort by department, then by salary grade).\n\n **CPython implementation detail:** While a list is being sorted,\n the effect of attempting to mutate, or even inspect, the list is\n undefined. The C implementation of Python makes the list appear\n empty for the duration, and raises ``ValueError`` if it can detect\n that the list has been mutated during a sort.\n\n8. ``sort()`` is not supported by ``bytearray`` objects.\n\n New in version 3.3: ``clear()`` and ``copy()`` methods.\n\n\nBytes and Byte Array Methods\n============================\n\nBytes and bytearray objects, being "strings of bytes", have all\nmethods found on strings, with the exception of ``encode()``,\n``format()`` and ``isidentifier()``, which do not make sense with\nthese types. For converting the objects to strings, they have a\n``decode()`` method.\n\nWherever one of these methods needs to interpret the bytes as\ncharacters (e.g. the ``is...()`` methods), the ASCII character set is\nassumed.\n\nNew in version 3.3: The functions ``count()``, ``find()``,\n``index()``, ``rfind()`` and ``rindex()`` have additional semantics\ncompared to the corresponding string functions: They also accept an\ninteger in range 0 to 255 (a byte) as their first argument.\n\nNote: The methods on bytes and bytearray objects don\'t accept strings as\n their arguments, just as the methods on strings don\'t accept bytes\n as their arguments. For example, you have to write\n\n a = "abc"\n b = a.replace("a", "f")\n\n and\n\n a = b"abc"\n b = a.replace(b"a", b"f")\n\nbytes.decode(encoding="utf-8", errors="strict")\nbytearray.decode(encoding="utf-8", errors="strict")\n\n Return a string decoded from the given bytes. Default encoding is\n ``\'utf-8\'``. *errors* may be given to set a different error\n handling scheme. The default for *errors* is ``\'strict\'``, meaning\n that encoding errors raise a ``UnicodeError``. Other possible\n values are ``\'ignore\'``, ``\'replace\'`` and any other name\n registered via ``codecs.register_error()``, see section *Codec Base\n Classes*. For a list of possible encodings, see section *Standard\n Encodings*.\n\n Changed in version 3.1: Added support for keyword arguments.\n\nThe bytes and bytearray types have an additional class method:\n\nclassmethod bytes.fromhex(string)\nclassmethod bytearray.fromhex(string)\n\n This ``bytes`` class method returns a bytes or bytearray object,\n decoding the given string object. The string must contain two\n hexadecimal digits per byte, spaces are ignored.\n\n >>> bytes.fromhex(\'f0 f1f2 \')\n b\'\\xf0\\xf1\\xf2\'\n\nThe maketrans and translate methods differ in semantics from the\nversions available on strings:\n\nbytes.translate(table[, delete])\nbytearray.translate(table[, delete])\n\n Return a copy of the bytes or bytearray object where all bytes\n occurring in the optional argument *delete* are removed, and the\n remaining bytes have been mapped through the given translation\n table, which must be a bytes object of length 256.\n\n You can use the ``bytes.maketrans()`` method to create a\n translation table.\n\n Set the *table* argument to ``None`` for translations that only\n delete characters:\n\n >>> b\'read this short text\'.translate(None, b\'aeiou\')\n b\'rd ths shrt txt\'\n\nstatic bytes.maketrans(from, to)\nstatic bytearray.maketrans(from, to)\n\n This static method returns a translation table usable for\n ``bytes.translate()`` that will map each character in *from* into\n the character at the same position in *to*; *from* and *to* must be\n bytes objects and have the same length.\n\n New in version 3.1.\n', 'typesseq-mutable': '\nMutable Sequence Types\n**********************\n\nList and bytearray objects support additional operations that allow\nin-place modification of the object. Other mutable sequence types\n(when added to the language) should also support these operations.\nStrings and tuples are immutable sequence types: such objects cannot\nbe modified once created. The following operations are defined on\nmutable sequence types (where *x* is an arbitrary object).\n\nNote that while lists allow their items to be of any type, bytearray\nobject "items" are all integers in the range 0 <= x < 256.\n\n+--------------------------------+----------------------------------+-----------------------+\n| Operation | Result | Notes |\n+================================+==================================+=======================+\n| ``s[i] = x`` | item *i* of *s* is replaced by | |\n| | *x* | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s[i:j] = t`` | slice of *s* from *i* to *j* is | |\n| | replaced by the contents of the | |\n| | iterable *t* | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``del s[i:j]`` | same as ``s[i:j] = []`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s[i:j:k] = t`` | the elements of ``s[i:j:k]`` are | (1) |\n| | replaced by those of *t* | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``del s[i:j:k]`` | removes the elements of | |\n| | ``s[i:j:k]`` from the list | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.append(x)`` | same as ``s[len(s):len(s)] = | |\n| | [x]`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.extend(x)`` | same as ``s[len(s):len(s)] = x`` | (2) |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.clear()`` | remove all items from ``s`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.copy()`` | return a shallow copy of ``s`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.count(x)`` | return number of *i*\'s for which | |\n| | ``s[i] == x`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.index(x[, i[, j]])`` | return smallest *k* such that | (3) |\n| | ``s[k] == x`` and ``i <= k < j`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.insert(i, x)`` | same as ``s[i:i] = [x]`` | (4) |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.pop([i])`` | same as ``x = s[i]; del s[i]; | (5) |\n| | return x`` | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.remove(x)`` | same as ``del s[s.index(x)]`` | (3) |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.reverse()`` | reverses the items of *s* in | (6) |\n| | place | |\n+--------------------------------+----------------------------------+-----------------------+\n| ``s.sort([key[, reverse]])`` | sort the items of *s* in place | (6), (7), (8) |\n+--------------------------------+----------------------------------+-----------------------+\n\nNotes:\n\n1. *t* must have the same length as the slice it is replacing.\n\n2. *x* can be any iterable object.\n\n3. Raises ``ValueError`` when *x* is not found in *s*. When a negative\n index is passed as the second or third parameter to the ``index()``\n method, the sequence length is added, as for slice indices. If it\n is still negative, it is truncated to zero, as for slice indices.\n\n4. When a negative index is passed as the first parameter to the\n ``insert()`` method, the sequence length is added, as for slice\n indices. If it is still negative, it is truncated to zero, as for\n slice indices.\n\n5. The optional argument *i* defaults to ``-1``, so that by default\n the last item is removed and returned.\n\n6. The ``sort()`` and ``reverse()`` methods modify the sequence in\n place for economy of space when sorting or reversing a large\n sequence. To remind you that they operate by side effect, they\n don\'t return the sorted or reversed sequence.\n\n7. The ``sort()`` method takes optional arguments for controlling the\n comparisons. Each must be specified as a keyword argument.\n\n *key* specifies a function of one argument that is used to extract\n a comparison key from each list element: ``key=str.lower``. The\n default value is ``None``. Use ``functools.cmp_to_key()`` to\n convert an old-style *cmp* function to a *key* function.\n\n *reverse* is a boolean value. If set to ``True``, then the list\n elements are sorted as if each comparison were reversed.\n\n The ``sort()`` method is guaranteed to be stable. A sort is stable\n if it guarantees not to change the relative order of elements that\n compare equal --- this is helpful for sorting in multiple passes\n (for example, sort by department, then by salary grade).\n\n **CPython implementation detail:** While a list is being sorted,\n the effect of attempting to mutate, or even inspect, the list is\n undefined. The C implementation of Python makes the list appear\n empty for the duration, and raises ``ValueError`` if it can detect\n that the list has been mutated during a sort.\n\n8. ``sort()`` is not supported by ``bytearray`` objects.\n\n New in version 3.3: ``clear()`` and ``copy()`` methods.\n', 'unary': '\nUnary arithmetic and bitwise operations\n***************************************\n\nAll unary arithmetic and bitwise operations have the same priority:\n\n u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr\n\nThe unary ``-`` (minus) operator yields the negation of its numeric\nargument.\n\nThe unary ``+`` (plus) operator yields its numeric argument unchanged.\n\nThe unary ``~`` (invert) operator yields the bitwise inversion of its\ninteger argument. The bitwise inversion of ``x`` is defined as\n``-(x+1)``. It only applies to integral numbers.\n\nIn all three cases, if the argument does not have the proper type, a\n``TypeError`` exception is raised.\n', 'while': '\nThe ``while`` statement\n***********************\n\nThe ``while`` statement is used for repeated execution as long as an\nexpression is true:\n\n while_stmt ::= "while" expression ":" suite\n ["else" ":" suite]\n\nThis repeatedly tests the expression and, if it is true, executes the\nfirst suite; if the expression is false (which may be the first time\nit is tested) the suite of the ``else`` clause, if present, is\nexecuted and the loop terminates.\n\nA ``break`` statement executed in the first suite terminates the loop\nwithout executing the ``else`` clause\'s suite. A ``continue``\nstatement executed in the first suite skips the rest of the suite and\ngoes back to testing the expression.\n', diff -r 2059910e7d76 -r ac776ef41428 Lib/random.py --- a/Lib/random.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/random.py Mon May 21 23:01:17 2012 -0400 @@ -96,7 +96,7 @@ None or no argument seeds from current time or from an operating system specific randomness source if available. - For version 2 (the default), all of the bits are used if *a* is a str, + For version 2 (the default), all of the bits are used if *a *is a str, bytes, or bytearray. For version 1, the hash() of *a* is used instead. If *a* is an int, all bits are used. diff -r 2059910e7d76 -r ac776ef41428 Lib/sched.py --- a/Lib/sched.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/sched.py Mon May 21 23:01:17 2012 -0400 @@ -35,10 +35,6 @@ import threading except ImportError: import dummy_threading as threading -try: - from time import monotonic as _time -except ImportError: - from time import time as _time __all__ = ["scheduler"] @@ -52,7 +48,7 @@ class scheduler: - def __init__(self, timefunc=_time, delayfunc=time.sleep): + def __init__(self, timefunc=time.time, delayfunc=time.sleep): """Initialize a new instance, passing the time and delay functions""" self._queue = [] diff -r 2059910e7d76 -r ac776ef41428 Lib/site.py --- a/Lib/site.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/site.py Mon May 21 23:01:17 2012 -0400 @@ -13,19 +13,6 @@ resulting directories, if they exist, are appended to sys.path, and also inspected for path configuration files. -If a file named "pyvenv.cfg" exists one directory above sys.executable, -sys.prefix and sys.exec_prefix are set to that directory and -it is also checked for site-packages and site-python (sys.prefix and -sys.exec_prefix will always be the "real" prefixes of the Python -installation). If "pyvenv.cfg" (a bootstrap configuration file) contains -the key "include-system-site-packages" set to anything other than "false" -(case-insensitive), the system-level prefixes will still also be -searched for site-packages; otherwise they won't. - -All of the resulting site-specific directories, if they exist, are -appended to sys.path, and also inspected for path configuration -files. - A path configuration file is a file whose name has the form .pth; its contents are additional directories (one per line) to be added to sys.path. Non-existing directories (or @@ -67,7 +54,6 @@ import sys import os -import re import builtins # Prefixes for site-packages; add additional prefixes like /usr/local here @@ -193,7 +179,6 @@ sitedir, sitedircase = makepath(sitedir) if not sitedircase in known_paths: sys.path.append(sitedir) # Add path component - known_paths.add(sitedircase) try: names = os.listdir(sitedir) except os.error: @@ -281,21 +266,18 @@ addsitedir(user_site, known_paths) return known_paths -def getsitepackages(prefixes=None): +def getsitepackages(): """Returns a list containing all global site-packages directories (and possibly site-python). - For each directory present in ``prefixes`` (or the global ``PREFIXES``), - this function will find its `site-packages` subdirectory depending on the - system environment, and will return a list of full paths. + For each directory present in the global ``PREFIXES``, this function + will find its `site-packages` subdirectory depending on the system + environment, and will return a list of full paths. """ sitepackages = [] seen = set() - if prefixes is None: - prefixes = PREFIXES - - for prefix in prefixes: + for prefix in PREFIXES: if not prefix or prefix in seen: continue seen.add(prefix) @@ -321,9 +303,9 @@ sys.version[:3], "site-packages")) return sitepackages -def addsitepackages(known_paths, prefixes=None): +def addsitepackages(known_paths): """Add site-packages (and possibly site-python) to sys.path""" - for sitedir in getsitepackages(prefixes): + for sitedir in getsitepackages(): if os.path.isdir(sitedir): addsitedir(sitedir, known_paths) @@ -493,61 +475,6 @@ encodings.aliases.aliases[enc] = 'mbcs' -CONFIG_LINE = re.compile(r'^(?P(\w|[-_])+)\s*=\s*(?P.*)\s*$') - -def venv(known_paths): - global PREFIXES, ENABLE_USER_SITE - - env = os.environ - if sys.platform == 'darwin' and '__PYTHONV_LAUNCHER__' in env: - executable = os.environ['__PYTHONV_LAUNCHER__'] - else: - executable = sys.executable - executable_dir, executable_name = os.path.split(executable) - site_prefix = os.path.dirname(executable_dir) - sys._home = None - if sys.platform == 'win32': - executable_name = os.path.splitext(executable_name)[0] - conf_basename = 'pyvenv.cfg' - candidate_confs = [ - conffile for conffile in ( - os.path.join(executable_dir, conf_basename), - os.path.join(site_prefix, conf_basename) - ) - if os.path.isfile(conffile) - ] - - if candidate_confs: - virtual_conf = candidate_confs[0] - system_site = "true" - with open(virtual_conf) as f: - for line in f: - line = line.strip() - m = CONFIG_LINE.match(line) - if m: - d = m.groupdict() - key, value = d['key'].lower(), d['value'] - if key == 'include-system-site-packages': - system_site = value.lower() - elif key == 'home': - sys._home = value - - sys.prefix = sys.exec_prefix = site_prefix - - # Doing this here ensures venv takes precedence over user-site - addsitepackages(known_paths, [sys.prefix]) - - # addsitepackages will process site_prefix again if its in PREFIXES, - # but that's ok; known_paths will prevent anything being added twice - if system_site == "true": - PREFIXES.insert(0, sys.prefix) - else: - PREFIXES = [sys.prefix] - ENABLE_USER_SITE = False - - return known_paths - - def execsitecustomize(): """Run custom site specific code, if available.""" try: @@ -590,7 +517,6 @@ abs_paths() known_paths = removeduppaths() - known_paths = venv(known_paths) if ENABLE_USER_SITE is None: ENABLE_USER_SITE = check_enableusersite() known_paths = addusersitepackages(known_paths) diff -r 2059910e7d76 -r ac776ef41428 Lib/smtpd.py --- a/Lib/smtpd.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/smtpd.py Mon May 21 23:01:17 2012 -0400 @@ -1,5 +1,5 @@ #! /usr/bin/env python3 -"""An RFC 5321 smtp proxy. +"""An RFC 2821 smtp proxy. Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]] @@ -20,11 +20,6 @@ Use `classname' as the concrete SMTP proxy class. Uses `PureProxy' by default. - --size limit - -s limit - Restrict the total size of the incoming message to "limit" number of - bytes via the RFC 1870 SIZE extension. Defaults to 33554432 bytes. - --debug -d Turn on debugging prints. @@ -40,9 +35,10 @@ and if remoteport is not given, then 25 is used. """ + # Overview: # -# This file implements the minimal SMTP protocol as defined in RFC 5321. It +# This file implements the minimal SMTP protocol as defined in RFC 821. It # has a hierarchy of classes which implement the backend functionality for the # smtpd. A number of classes are provided: # @@ -70,7 +66,7 @@ # # - support mailbox delivery # - alias files -# - Handle more ESMTP extensions +# - ESMTP # - handle error codes from the backend smtpd import sys @@ -81,14 +77,12 @@ import socket import asyncore import asynchat -import collections from warnings import warn -from email._header_value_parser import get_addr_spec, get_angle_addr __all__ = ["SMTPServer","DebuggingServer","PureProxy","MailmanProxy"] program = sys.argv[0] -__version__ = 'Python SMTP proxy version 0.3' +__version__ = 'Python SMTP proxy version 0.2' class Devnull: @@ -100,9 +94,9 @@ NEWLINE = '\n' EMPTYSTRING = '' COMMASPACE = ', ' -DATA_SIZE_DEFAULT = 33554432 + def usage(code, msg=''): print(__doc__ % globals(), file=sys.stderr) if msg: @@ -110,23 +104,19 @@ sys.exit(code) + class SMTPChannel(asynchat.async_chat): COMMAND = 0 DATA = 1 + data_size_limit = 33554432 command_size_limit = 512 - command_size_limits = collections.defaultdict(lambda x=command_size_limit: x) - command_size_limits.update({ - 'MAIL': command_size_limit + 26, - }) - max_command_size_limit = max(command_size_limits.values()) - def __init__(self, server, conn, addr, data_size_limit=DATA_SIZE_DEFAULT): + def __init__(self, server, conn, addr): asynchat.async_chat.__init__(self, conn) self.smtp_server = server self.conn = conn self.addr = addr - self.data_size_limit = data_size_limit self.received_lines = [] self.smtp_state = self.COMMAND self.seen_greeting = '' @@ -147,7 +137,6 @@ print('Peer:', repr(self.peer), file=DEBUGSTREAM) self.push('220 %s %s' % (self.fqdn, __version__)) self.set_terminator(b'\r\n') - self.extended_smtp = False # properties for backwards-compatibility @property @@ -279,7 +268,7 @@ def collect_incoming_data(self, data): limit = None if self.smtp_state == self.COMMAND: - limit = self.max_command_size_limit + limit = self.command_size_limit elif self.smtp_state == self.DATA: limit = self.data_size_limit if limit and self.num_bytes > limit: @@ -294,7 +283,11 @@ print('Data:', repr(line), file=DEBUGSTREAM) self.received_lines = [] if self.smtp_state == self.COMMAND: - sz, self.num_bytes = self.num_bytes, 0 + if self.num_bytes > self.command_size_limit: + self.push('500 Error: line too long') + self.num_bytes = 0 + return + self.num_bytes = 0 if not line: self.push('500 Error: bad syntax') return @@ -306,14 +299,9 @@ else: command = line[:i].upper() arg = line[i+1:].strip() - max_sz = (self.command_size_limits[command] - if self.extended_smtp else self.command_size_limit) - if sz > max_sz: - self.push('500 Error: line too long') - return method = getattr(self, 'smtp_' + command, None) if not method: - self.push('500 Error: command "%s" not recognized' % command) + self.push('502 Error: command "%s" not implemented' % command) return method(arg) return @@ -322,12 +310,12 @@ self.push('451 Internal confusion') self.num_bytes = 0 return - if self.data_size_limit and self.num_bytes > self.data_size_limit: + if self.num_bytes > self.data_size_limit: self.push('552 Error: Too much mail data') self.num_bytes = 0 return # Remove extraneous carriage returns and de-transparency according - # to RFC 5321, Section 4.5.2. + # to RFC 821, Section 4.5.2. data = [] for text in line.split('\r\n'): if text and text[0] == '.': @@ -345,7 +333,7 @@ self.num_bytes = 0 self.set_terminator(b'\r\n') if not status: - self.push('250 OK') + self.push('250 Ok') else: self.push(status) @@ -358,188 +346,66 @@ self.push('503 Duplicate HELO/EHLO') else: self.seen_greeting = arg - self.extended_smtp = False self.push('250 %s' % self.fqdn) - def smtp_EHLO(self, arg): - if not arg: - self.push('501 Syntax: EHLO hostname') - return - if self.seen_greeting: - self.push('503 Duplicate HELO/EHLO') - else: - self.seen_greeting = arg - self.extended_smtp = True - self.push('250-%s' % self.fqdn) - if self.data_size_limit: - self.push('250-SIZE %s' % self.data_size_limit) - self.push('250 HELP') - def smtp_NOOP(self, arg): if arg: self.push('501 Syntax: NOOP') else: - self.push('250 OK') + self.push('250 Ok') def smtp_QUIT(self, arg): # args is ignored self.push('221 Bye') self.close_when_done() - def _strip_command_keyword(self, keyword, arg): + # factored + def __getaddr(self, keyword, arg): + address = None keylen = len(keyword) if arg[:keylen].upper() == keyword: - return arg[keylen:].strip() - return '' - - def _getaddr(self, arg): - if not arg: - return '', '' - if arg.lstrip().startswith('<'): - address, rest = get_angle_addr(arg) - else: - address, rest = get_addr_spec(arg) - if not address: - return address, rest - return address.addr_spec, rest - - def _getparams(self, params): - # Return any parameters that appear to be syntactically valid according - # to RFC 1869, ignore all others. (Postel rule: accept what we can.) - params = [param.split('=', 1) for param in params.split() - if '=' in param] - return {k: v for k, v in params if k.isalnum()} - - def smtp_HELP(self, arg): - if arg: - extended = ' [SP ') - else: - self.push('501 Supported commands: EHLO HELO MAIL RCPT ' - 'DATA RSET NOOP QUIT VRFY') - else: - self.push('250 Supported commands: EHLO HELO MAIL RCPT DATA ' - 'RSET NOOP QUIT VRFY') - - def smtp_VRFY(self, arg): - if arg: - address, params = self._getaddr(arg) - if address: - self.push('252 Cannot VRFY user, but will accept message ' - 'and attempt delivery') - else: - self.push('502 Could not VRFY %s' % arg) - else: - self.push('501 Syntax: VRFY
') + address = arg[keylen:].strip() + if not address: + pass + elif address[0] == '<' and address[-1] == '>' and address != '<>': + # Addresses can be in the form but watch out + # for null address, e.g. <> + address = address[1:-1] + return address def smtp_MAIL(self, arg): if not self.seen_greeting: self.push('503 Error: send HELO first'); return + print('===> MAIL', arg, file=DEBUGSTREAM) - syntaxerr = '501 Syntax: MAIL FROM:
' - if self.extended_smtp: - syntaxerr += ' [SP ]' - if arg is None: - self.push(syntaxerr) - return - arg = self._strip_command_keyword('FROM:', arg) - address, params = self._getaddr(arg) + address = self.__getaddr('FROM:', arg) if arg else None if not address: - self.push(syntaxerr) - return - if not self.extended_smtp and params: - self.push(syntaxerr) - return - if not address: - self.push(syntaxerr) + self.push('501 Syntax: MAIL FROM:
') return if self.mailfrom: self.push('503 Error: nested MAIL command') return - params = self._getparams(params.upper()) - if params is None: - self.push(syntaxerr) - return - size = params.pop('SIZE', None) - if size: - if not size.isdigit(): - self.push(syntaxerr) - return - elif self.data_size_limit and int(size) > self.data_size_limit: - self.push('552 Error: message size exceeds fixed maximum message size') - return - if len(params.keys()) > 0: - self.push('555 MAIL FROM parameters not recognized or not implemented') - return self.mailfrom = address print('sender:', self.mailfrom, file=DEBUGSTREAM) - self.push('250 OK') + self.push('250 Ok') def smtp_RCPT(self, arg): if not self.seen_greeting: self.push('503 Error: send HELO first'); return + print('===> RCPT', arg, file=DEBUGSTREAM) if not self.mailfrom: self.push('503 Error: need MAIL command') return - syntaxerr = '501 Syntax: RCPT TO:
' - if self.extended_smtp: - syntaxerr += ' [SP ]' - if arg is None: - self.push(syntaxerr) - return - arg = self._strip_command_keyword('TO:', arg) - address, params = self._getaddr(arg) - if not address: - self.push(syntaxerr) - return - if params: - if self.extended_smtp: - params = self._getparams(params.upper()) - if params is None: - self.push(syntaxerr) - return - else: - self.push(syntaxerr) - return - if not address: - self.push(syntaxerr) - return - if params and len(params.keys()) > 0: - self.push('555 RCPT TO parameters not recognized or not implemented') - return + address = self.__getaddr('TO:', arg) if arg else None if not address: self.push('501 Syntax: RCPT TO:
') return self.rcpttos.append(address) print('recips:', self.rcpttos, file=DEBUGSTREAM) - self.push('250 OK') + self.push('250 Ok') def smtp_RSET(self, arg): if arg: @@ -550,12 +416,13 @@ self.rcpttos = [] self.received_data = '' self.smtp_state = self.COMMAND - self.push('250 OK') + self.push('250 Ok') def smtp_DATA(self, arg): if not self.seen_greeting: self.push('503 Error: send HELO first'); return + if not self.rcpttos: self.push('503 Error: need RCPT command') return @@ -566,20 +433,15 @@ self.set_terminator(b'\r\n.\r\n') self.push('354 End data with .') - # Commands that have not been implemented - def smtp_EXPN(self, arg): - self.push('502 EXPN not implemented') - + class SMTPServer(asyncore.dispatcher): # SMTPChannel class to use for managing client connections channel_class = SMTPChannel - def __init__(self, localaddr, remoteaddr, - data_size_limit=DATA_SIZE_DEFAULT): + def __init__(self, localaddr, remoteaddr): self._localaddr = localaddr self._remoteaddr = remoteaddr - self.data_size_limit = data_size_limit asyncore.dispatcher.__init__(self) try: self.create_socket(socket.AF_INET, socket.SOCK_STREAM) @@ -597,7 +459,7 @@ def handle_accepted(self, conn, addr): print('Incoming connection from %s' % repr(addr), file=DEBUGSTREAM) - channel = self.channel_class(self, conn, addr, self.data_size_limit) + channel = self.channel_class(self, conn, addr) # API for "doing something useful with the message" def process_message(self, peer, mailfrom, rcpttos, data): @@ -625,6 +487,7 @@ raise NotImplementedError + class DebuggingServer(SMTPServer): # Do something with the gathered message def process_message(self, peer, mailfrom, rcpttos, data): @@ -640,6 +503,7 @@ print('------------ END MESSAGE ------------') + class PureProxy(SMTPServer): def process_message(self, peer, mailfrom, rcpttos, data): lines = data.split('\n') @@ -680,6 +544,7 @@ return refused + class MailmanProxy(PureProxy): def process_message(self, peer, mailfrom, rcpttos, data): from io import StringIO @@ -758,18 +623,19 @@ msg.Enqueue(mlist, torequest=1) + class Options: setuid = 1 classname = 'PureProxy' - size_limit = None + def parseargs(): global DEBUGSTREAM try: opts, args = getopt.getopt( - sys.argv[1:], 'nVhc:s:d', - ['class=', 'nosetuid', 'version', 'help', 'size=', 'debug']) + sys.argv[1:], 'nVhc:d', + ['class=', 'nosetuid', 'version', 'help', 'debug']) except getopt.error as e: usage(1, e) @@ -786,13 +652,6 @@ options.classname = arg elif opt in ('-d', '--debug'): DEBUGSTREAM = sys.stderr - elif opt in ('-s', '--size'): - try: - int_size = int(arg) - options.size_limit = int_size - except: - print('Invalid size: ' + arg, file=sys.stderr) - sys.exit(1) # parse the rest of the arguments if len(args) < 1: @@ -827,6 +686,7 @@ return options + if __name__ == '__main__': options = parseargs() # Become nobody @@ -839,8 +699,7 @@ import __main__ as mod class_ = getattr(mod, classname) proxy = class_((options.localhost, options.localport), - (options.remotehost, options.remoteport), - options.size_limit) + (options.remotehost, options.remoteport)) if options.setuid: try: import pwd diff -r 2059910e7d76 -r ac776ef41428 Lib/subprocess.py --- a/Lib/subprocess.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/subprocess.py Mon May 21 23:01:17 2012 -0400 @@ -349,10 +349,6 @@ import builtins import warnings import errno -try: - from time import monotonic as _time -except ImportError: - from time import time as _time # Exception classes used by this module. class SubprocessError(Exception): pass @@ -479,37 +475,6 @@ continue -# XXX This function is only used by multiprocessing and the test suite, -# but it's here so that it can be imported when Python is compiled without -# threads. - -def _args_from_interpreter_flags(): - """Return a list of command-line arguments reproducing the current - settings in sys.flags and sys.warnoptions.""" - flag_opt_map = { - 'debug': 'd', - # 'inspect': 'i', - # 'interactive': 'i', - 'optimize': 'O', - 'dont_write_bytecode': 'B', - 'no_user_site': 's', - 'no_site': 'S', - 'ignore_environment': 'E', - 'verbose': 'v', - 'bytes_warning': 'b', - 'quiet': 'q', - 'hash_randomization': 'R', - } - args = [] - for flag, opt in flag_opt_map.items(): - v = getattr(sys.flags, flag) - if v > 0: - args.append('-' + opt * v) - for opt in sys.warnoptions: - args.append('-W' + opt) - return args - - def call(*popenargs, timeout=None, **kwargs): """Run command with arguments. Wait for command to complete or timeout, then return the returncode attribute. @@ -898,7 +863,7 @@ self.wait() else: if timeout is not None: - endtime = _time() + timeout + endtime = time.time() + timeout else: endtime = None @@ -921,14 +886,14 @@ if endtime is None: return None else: - return endtime - _time() + return endtime - time.time() def _check_timeout(self, endtime, orig_timeout): """Convenience for checking if a timeout has expired.""" if endtime is None: return - if _time() > endtime: + if time.time() > endtime: raise TimeoutExpired(self.args, orig_timeout) @@ -1025,7 +990,7 @@ if not os.path.exists(w9xpopen): # Eeek - file-not-found - possibly an embedding # situation - see if we can locate it in sys.exec_prefix - w9xpopen = os.path.join(os.path.dirname(sys.base_exec_prefix), + w9xpopen = os.path.join(os.path.dirname(sys.exec_prefix), "w9xpopen.exe") if not os.path.exists(w9xpopen): raise RuntimeError("Cannot locate w9xpopen.exe, which is " @@ -1475,7 +1440,7 @@ # printing. if endtime is not None or timeout is not None: if endtime is None: - endtime = _time() + timeout + endtime = time.time() + timeout elif timeout is None: timeout = self._remaining_time(endtime) diff -r 2059910e7d76 -r ac776ef41428 Lib/sysconfig.cfg --- a/Lib/sysconfig.cfg Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/sysconfig.cfg Mon May 21 23:01:17 2012 -0400 @@ -36,41 +36,41 @@ # User resource directory local = ~/.local/{distribution.name} -stdlib = {installed_base}/lib/python{py_version_short} +stdlib = {base}/lib/python{py_version_short} platstdlib = {platbase}/lib/python{py_version_short} purelib = {base}/lib/python{py_version_short}/site-packages platlib = {platbase}/lib/python{py_version_short}/site-packages -include = {installed_base}/include/python{py_version_short}{abiflags} -platinclude = {installed_platbase}/include/python{py_version_short}{abiflags} +include = {base}/include/python{py_version_short}{abiflags} +platinclude = {platbase}/include/python{py_version_short}{abiflags} data = {base} [posix_home] -stdlib = {installed_base}/lib/python +stdlib = {base}/lib/python platstdlib = {base}/lib/python purelib = {base}/lib/python platlib = {base}/lib/python -include = {installed_base}/include/python -platinclude = {installed_base}/include/python +include = {base}/include/python +platinclude = {base}/include/python scripts = {base}/bin data = {base} [nt] -stdlib = {installed_base}/Lib +stdlib = {base}/Lib platstdlib = {base}/Lib purelib = {base}/Lib/site-packages platlib = {base}/Lib/site-packages -include = {installed_base}/Include -platinclude = {installed_base}/Include +include = {base}/Include +platinclude = {base}/Include scripts = {base}/Scripts data = {base} [os2] -stdlib = {installed_base}/Lib +stdlib = {base}/Lib platstdlib = {base}/Lib purelib = {base}/Lib/site-packages platlib = {base}/Lib/site-packages -include = {installed_base}/Include -platinclude = {installed_base}/Include +include = {base}/Include +platinclude = {base}/Include scripts = {base}/Scripts data = {base} diff -r 2059910e7d76 -r ac776ef41428 Lib/sysconfig.py --- a/Lib/sysconfig.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/sysconfig.py Mon May 21 23:01:17 2012 -0400 @@ -3,7 +3,6 @@ import os import re import sys -import os from os.path import pardir, realpath from configparser import RawConfigParser @@ -62,15 +61,13 @@ _expand_globals(_SCHEMES) - # FIXME don't rely on sys.version here, its format is an implementation detail + # FIXME don't rely on sys.version here, its format is an implementatin detail # of CPython, use sys.version_info or sys.hexversion _PY_VERSION = sys.version.split()[0] _PY_VERSION_SHORT = sys.version[:3] _PY_VERSION_SHORT_NO_DOT = _PY_VERSION[0] + _PY_VERSION[2] _PREFIX = os.path.normpath(sys.prefix) -_BASE_PREFIX = os.path.normpath(sys.base_prefix) _EXEC_PREFIX = os.path.normpath(sys.exec_prefix) -_BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix) _CONFIG_VARS = None _USER_BASE = None @@ -97,24 +94,14 @@ if os.name == "nt" and "\\pcbuild\\amd64" in _PROJECT_BASE[-14:].lower(): _PROJECT_BASE = _safe_realpath(os.path.join(_PROJECT_BASE, pardir, pardir)) -def _is_python_source_dir(d): + +def is_python_build(): for fn in ("Setup.dist", "Setup.local"): - if os.path.isfile(os.path.join(d, "Modules", fn)): + if os.path.isfile(os.path.join(_PROJECT_BASE, "Modules", fn)): return True return False -_sys_home = getattr(sys, '_home', None) -if _sys_home and os.name == 'nt' and \ - _sys_home.lower().endswith(('pcbuild', 'pcbuild\\amd64')): - _sys_home = os.path.dirname(_sys_home) - if _sys_home.endswith('pcbuild'): # must be amd64 - _sys_home = os.path.dirname(_sys_home) -def is_python_build(check_home=False): - if check_home and _sys_home: - return _is_python_source_dir(_sys_home) - return _is_python_source_dir(_PROJECT_BASE) - -_PYTHON_BUILD = is_python_build(True) +_PYTHON_BUILD = is_python_build() if _PYTHON_BUILD: for scheme in ('posix_prefix', 'posix_home'): @@ -325,7 +312,7 @@ def get_makefile_filename(): """Return the path of the Makefile.""" if _PYTHON_BUILD: - return os.path.join(_sys_home or _PROJECT_BASE, "Makefile") + return os.path.join(_PROJECT_BASE, "Makefile") if hasattr(sys, 'abiflags'): config_dir_name = 'config-%s%s' % (_PY_VERSION_SHORT, sys.abiflags) else: @@ -425,9 +412,9 @@ """Return the path of pyconfig.h.""" if _PYTHON_BUILD: if os.name == "nt": - inc_dir = os.path.join(_sys_home or _PROJECT_BASE, "PC") + inc_dir = os.path.join(_PROJECT_BASE, "PC") else: - inc_dir = _sys_home or _PROJECT_BASE + inc_dir = _PROJECT_BASE else: inc_dir = get_path('platinclude') return os.path.join(inc_dir, 'pyconfig.h') @@ -485,9 +472,7 @@ _CONFIG_VARS['py_version'] = _PY_VERSION _CONFIG_VARS['py_version_short'] = _PY_VERSION_SHORT _CONFIG_VARS['py_version_nodot'] = _PY_VERSION[0] + _PY_VERSION[2] - _CONFIG_VARS['installed_base'] = _BASE_PREFIX _CONFIG_VARS['base'] = _PREFIX - _CONFIG_VARS['installed_platbase'] = _BASE_EXEC_PREFIX _CONFIG_VARS['platbase'] = _EXEC_PREFIX _CONFIG_VARS['projectbase'] = _PROJECT_BASE try: diff -r 2059910e7d76 -r ac776ef41428 Lib/tarfile.py --- a/Lib/tarfile.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/tarfile.py Mon May 21 23:01:17 2012 -0400 @@ -245,8 +245,8 @@ the high bit set. So we calculate two checksums, unsigned and signed. """ - unsigned_chksum = 256 + sum(struct.unpack_from("148B8x356B", buf)) - signed_chksum = 256 + sum(struct.unpack_from("148b8x356b", buf)) + unsigned_chksum = 256 + sum(struct.unpack("148B", buf[:148]) + struct.unpack("356B", buf[156:512])) + signed_chksum = 256 + sum(struct.unpack("148b", buf[:148]) + struct.unpack("356b", buf[156:512])) return unsigned_chksum, signed_chksum def copyfileobj(src, dst, length=None): @@ -1657,8 +1657,8 @@ except ImportError: raise CompressionError("bz2 module is not available") - fileobj = bz2.BZ2File(fileobj or name, mode, - compresslevel=compresslevel) + fileobj = bz2.BZ2File(filename=name if fileobj is None else None, + mode=mode, fileobj=fileobj, compresslevel=compresslevel) try: t = cls.taropen(name, mode, fileobj, **kwargs) @@ -1681,7 +1681,8 @@ except ImportError: raise CompressionError("lzma module is not available") - fileobj = lzma.LZMAFile(fileobj or name, mode, preset=preset) + fileobj = lzma.LZMAFile(filename=name if fileobj is None else None, + mode=mode, fileobj=fileobj, preset=preset) try: t = cls.taropen(name, mode, fileobj, **kwargs) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/datetimetester.py --- a/Lib/test/datetimetester.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/datetimetester.py Mon May 21 23:01:17 2012 -0400 @@ -1735,42 +1735,6 @@ got = self.theclass.utcfromtimestamp(ts) self.verify_field_equality(expected, got) - # Run with US-style DST rules: DST begins 2 a.m. on second Sunday in - # March (M3.2.0) and ends 2 a.m. on first Sunday in November (M11.1.0). - @support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0') - def test_timestamp_naive(self): - t = self.theclass(1970, 1, 1) - self.assertEqual(t.timestamp(), 18000.0) - t = self.theclass(1970, 1, 1, 1, 2, 3, 4) - self.assertEqual(t.timestamp(), - 18000.0 + 3600 + 2*60 + 3 + 4*1e-6) - # Missing hour may produce platform-dependent result - t = self.theclass(2012, 3, 11, 2, 30) - self.assertIn(self.theclass.fromtimestamp(t.timestamp()), - [t - timedelta(hours=1), t + timedelta(hours=1)]) - # Ambiguous hour defaults to DST - t = self.theclass(2012, 11, 4, 1, 30) - self.assertEqual(self.theclass.fromtimestamp(t.timestamp()), t) - - # Timestamp may raise an overflow error on some platforms - for t in [self.theclass(1,1,1), self.theclass(9999,12,12)]: - try: - s = t.timestamp() - except OverflowError: - pass - else: - self.assertEqual(self.theclass.fromtimestamp(s), t) - - def test_timestamp_aware(self): - t = self.theclass(1970, 1, 1, tzinfo=timezone.utc) - self.assertEqual(t.timestamp(), 0.0) - t = self.theclass(1970, 1, 1, 1, 2, 3, 4, tzinfo=timezone.utc) - self.assertEqual(t.timestamp(), - 3600 + 2*60 + 3 + 4*1e-6) - t = self.theclass(1970, 1, 1, 1, 2, 3, 4, - tzinfo=timezone(timedelta(hours=-5), 'EST')) - self.assertEqual(t.timestamp(), - 18000 + 3600 + 2*60 + 3 + 4*1e-6) def test_microsecond_rounding(self): for fts in [self.theclass.fromtimestamp, self.theclass.utcfromtimestamp]: diff -r 2059910e7d76 -r ac776ef41428 Lib/test/lock_tests.py --- a/Lib/test/lock_tests.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/lock_tests.py Mon May 21 23:01:17 2012 -0400 @@ -247,6 +247,7 @@ # Cannot release an unacquired lock lock = self.locktype() self.assertRaises(RuntimeError, lock.release) + self.assertRaises(RuntimeError, lock._release_save) lock.acquire() lock.acquire() lock.release() @@ -254,17 +255,6 @@ lock.release() lock.release() self.assertRaises(RuntimeError, lock.release) - - def test_release_save_unacquired(self): - # Cannot _release_save an unacquired lock - lock = self.locktype() - self.assertRaises(RuntimeError, lock._release_save) - lock.acquire() - lock.acquire() - lock.release() - lock.acquire() - lock.release() - lock.release() self.assertRaises(RuntimeError, lock._release_save) def test_different_thread(self): diff -r 2059910e7d76 -r ac776ef41428 Lib/test/namespace_pkgs/both_portions/foo/one.py --- a/Lib/test/namespace_pkgs/both_portions/foo/one.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -attr = 'both_portions foo one' diff -r 2059910e7d76 -r ac776ef41428 Lib/test/namespace_pkgs/both_portions/foo/two.py --- a/Lib/test/namespace_pkgs/both_portions/foo/two.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -attr = 'both_portions foo two' diff -r 2059910e7d76 -r ac776ef41428 Lib/test/namespace_pkgs/missing_directory.zip Binary file Lib/test/namespace_pkgs/missing_directory.zip has changed diff -r 2059910e7d76 -r ac776ef41428 Lib/test/namespace_pkgs/nested_portion1.zip Binary file Lib/test/namespace_pkgs/nested_portion1.zip has changed diff -r 2059910e7d76 -r ac776ef41428 Lib/test/namespace_pkgs/not_a_namespace_pkg/foo/one.py --- a/Lib/test/namespace_pkgs/not_a_namespace_pkg/foo/one.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -attr = 'portion1 foo one' diff -r 2059910e7d76 -r ac776ef41428 Lib/test/namespace_pkgs/portion1/foo/one.py --- a/Lib/test/namespace_pkgs/portion1/foo/one.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -attr = 'portion1 foo one' diff -r 2059910e7d76 -r ac776ef41428 Lib/test/namespace_pkgs/portion2/foo/two.py --- a/Lib/test/namespace_pkgs/portion2/foo/two.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -attr = 'portion2 foo two' diff -r 2059910e7d76 -r ac776ef41428 Lib/test/namespace_pkgs/project1/parent/child/one.py --- a/Lib/test/namespace_pkgs/project1/parent/child/one.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -attr = 'parent child one' diff -r 2059910e7d76 -r ac776ef41428 Lib/test/namespace_pkgs/project2/parent/child/two.py --- a/Lib/test/namespace_pkgs/project2/parent/child/two.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -attr = 'parent child two' diff -r 2059910e7d76 -r ac776ef41428 Lib/test/namespace_pkgs/project3/parent/child/three.py --- a/Lib/test/namespace_pkgs/project3/parent/child/three.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -attr = 'parent child three' diff -r 2059910e7d76 -r ac776ef41428 Lib/test/namespace_pkgs/top_level_portion1.zip Binary file Lib/test/namespace_pkgs/top_level_portion1.zip has changed diff -r 2059910e7d76 -r ac776ef41428 Lib/test/regrtest.py --- a/Lib/test/regrtest.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/regrtest.py Mon May 21 23:01:17 2012 -0400 @@ -564,7 +564,7 @@ random.shuffle(selected) if trace: import trace, tempfile - tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix, + tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix, tempfile.gettempdir()], trace=False, count=True) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/support.py --- a/Lib/test/support.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/support.py Mon May 21 23:01:17 2012 -0400 @@ -24,7 +24,6 @@ import fnmatch import logging.handlers import struct -import tempfile try: import _thread, threading @@ -52,25 +51,23 @@ lzma = None __all__ = [ - "Error", "TestFailed", "ResourceDenied", "import_module", "verbose", - "use_resources", "max_memuse", "record_original_stdout", + "Error", "TestFailed", "ResourceDenied", "import_module", + "verbose", "use_resources", "max_memuse", "record_original_stdout", "get_original_stdout", "unload", "unlink", "rmtree", "forget", "is_resource_enabled", "requires", "requires_freebsd_version", - "requires_linux_version", "requires_mac_ver", "find_unused_port", - "bind_port", "IPV6_ENABLED", "is_jython", "TESTFN", "HOST", "SAVEDCWD", - "temp_cwd", "findfile", "create_empty_file", "sortdict", - "check_syntax_error", "open_urlresource", "check_warnings", "CleanImport", - "EnvironmentVarGuard", "TransientResource", "captured_stdout", - "captured_stdin", "captured_stderr", "time_out", "socket_peer_reset", - "ioerror_peer_reset", "run_with_locale", 'temp_umask', + "requires_linux_version", "requires_mac_ver", "find_unused_port", "bind_port", + "IPV6_ENABLED", "is_jython", "TESTFN", "HOST", "SAVEDCWD", "temp_cwd", + "findfile", "create_empty_file", "sortdict", "check_syntax_error", "open_urlresource", + "check_warnings", "CleanImport", "EnvironmentVarGuard", "TransientResource", + "captured_stdout", "captured_stdin", "captured_stderr", "time_out", + "socket_peer_reset", "ioerror_peer_reset", "run_with_locale", 'temp_umask', "transient_internet", "set_memlimit", "bigmemtest", "bigaddrspacetest", "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup", "threading_cleanup", "reap_children", "cpython_only", "check_impl_detail", "get_attribute", "swap_item", "swap_attr", "requires_IEEE_754", "TestHandler", "Matcher", "can_symlink", "skip_unless_symlink", - "skip_unless_xattr", "import_fresh_module", "requires_zlib", - "PIPE_MAX_SIZE", "failfast", "anticipate_failure", "run_with_tz", - "requires_bz2", "requires_lzma" + "import_fresh_module", "requires_zlib", "PIPE_MAX_SIZE", "failfast", + "anticipate_failure", "run_with_tz", "requires_bz2", "requires_lzma" ] class Error(Exception): @@ -1599,7 +1596,24 @@ def args_from_interpreter_flags(): """Return a list of command-line arguments reproducing the current settings in sys.flags and sys.warnoptions.""" - return subprocess._args_from_interpreter_flags() + flag_opt_map = { + 'bytes_warning': 'b', + 'dont_write_bytecode': 'B', + 'hash_randomization': 'R', + 'ignore_environment': 'E', + 'no_user_site': 's', + 'no_site': 'S', + 'optimize': 'O', + 'verbose': 'v', + } + args = [] + for flag, opt in flag_opt_map.items(): + v = getattr(sys.flags, flag) + if v > 0: + args.append('-' + opt * v) + for opt in sys.warnoptions: + args.append('-W' + opt) + return args #============================================================ # Support for assertions about logging. @@ -1697,13 +1711,9 @@ if not hasattr(os, "setxattr"): can = False else: - tmp_fp, tmp_name = tempfile.mkstemp() try: with open(TESTFN, "wb") as fp: try: - # TESTFN & tempfile may use different file systems with - # different capabilities - os.fsetxattr(tmp_fp, b"user.test", b"") os.fsetxattr(fp.fileno(), b"user.test", b"") # Kernels < 2.6.39 don't respect setxattr flags. kernel_version = platform.release() @@ -1713,7 +1723,6 @@ can = False finally: unlink(TESTFN) - unlink(tmp_name) _can_xattr = can return can diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_builtin.py --- a/Lib/test/test_builtin.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_builtin.py Mon May 21 23:01:17 2012 -0400 @@ -1,21 +1,20 @@ # Python test set -- built-in functions -import ast -import builtins +import platform +import unittest +import sys +import warnings import collections import io -import locale import os +import ast +import types +import builtins +import random +import traceback +from test.support import TESTFN, unlink, run_unittest, check_warnings +from operator import neg import pickle -import platform -import random -import sys -import traceback -import types -import unittest -import warnings -from operator import neg -from test.support import TESTFN, unlink, run_unittest, check_warnings try: import pty, signal except ImportError: @@ -962,27 +961,6 @@ fp.close() unlink(TESTFN) - def test_open_default_encoding(self): - old_environ = dict(os.environ) - try: - # try to get a user preferred encoding different than the current - # locale encoding to check that open() uses the current locale - # encoding and not the user preferred encoding - for key in ('LC_ALL', 'LANG', 'LC_CTYPE'): - if key in os.environ: - del os.environ[key] - - self.write_testfile() - current_locale_encoding = locale.getpreferredencoding(False) - fp = open(TESTFN, 'w') - try: - self.assertEqual(fp.encoding, current_locale_encoding) - finally: - fp.close() - finally: - os.environ.clear() - os.environ.update(old_environ) - def test_ord(self): self.assertEqual(ord(' '), 32) self.assertEqual(ord('A'), 65) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_bz2.py --- a/Lib/test/test_bz2.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_bz2.py Mon May 21 23:01:17 2012 -0400 @@ -48,13 +48,6 @@ TEXT = b''.join(TEXT_LINES) DATA = b'BZh91AY&SY.\xc8N\x18\x00\x01>_\x80\x00\x10@\x02\xff\xf0\x01\x07n\x00?\xe7\xff\xe00\x01\x99\xaa\x00\xc0\x03F\x86\x8c#&\x83F\x9a\x03\x06\xa6\xd0\xa6\x93M\x0fQ\xa7\xa8\x06\x804hh\x12$\x11\xa4i4\xf14S\xd2\x88\xe5\xcd9gd6\x0b\n\xe9\x9b\xd5\x8a\x99\xf7\x08.K\x8ev\xfb\xf7xw\xbb\xdf\xa1\x92\xf1\xdd|/";\xa2\xba\x9f\xd5\xb1#A\xb6\xf6\xb3o\xc9\xc5y\\\xebO\xe7\x85\x9a\xbc\xb6f8\x952\xd5\xd7"%\x89>V,\xf7\xa6z\xe2\x9f\xa3\xdf\x11\x11"\xd6E)I\xa9\x13^\xca\xf3r\xd0\x03U\x922\xf26\xec\xb6\xed\x8b\xc3U\x13\x9d\xc5\x170\xa4\xfa^\x92\xacDF\x8a\x97\xd6\x19\xfe\xdd\xb8\xbd\x1a\x9a\x19\xa3\x80ankR\x8b\xe5\xd83]\xa9\xc6\x08\x82f\xf6\xb9"6l$\xb8j@\xc0\x8a\xb0l1..\xbak\x83ls\x15\xbc\xf4\xc1\x13\xbe\xf8E\xb8\x9d\r\xa8\x9dk\x84\xd3n\xfa\xacQ\x07\xb1%y\xaav\xb4\x08\xe0z\x1b\x16\xf5\x04\xe9\xcc\xb9\x08z\x1en7.G\xfc]\xc9\x14\xe1B@\xbb!8`' - def setUp(self): - self.filename = TESTFN - - def tearDown(self): - if os.path.isfile(self.filename): - os.unlink(self.filename) - if has_cmdline_bunzip2: def decompress(self, data): pop = subprocess.Popen("bunzip2", shell=True, @@ -77,24 +70,17 @@ class BZ2FileTest(BaseTest): "Test BZ2File type miscellaneous methods." + def setUp(self): + self.filename = TESTFN + + def tearDown(self): + if os.path.isfile(self.filename): + os.unlink(self.filename) + def createTempFile(self, streams=1): with open(self.filename, "wb") as f: f.write(self.DATA * streams) - def testBadArgs(self): - with self.assertRaises(TypeError): - BZ2File(123.456) - with self.assertRaises(ValueError): - BZ2File("/dev/null", "z") - with self.assertRaises(ValueError): - BZ2File("/dev/null", "rx") - with self.assertRaises(ValueError): - BZ2File("/dev/null", "rbt") - with self.assertRaises(ValueError): - BZ2File("/dev/null", compresslevel=0) - with self.assertRaises(ValueError): - BZ2File("/dev/null", compresslevel=10) - def testRead(self): self.createTempFile() with BZ2File(self.filename) as bz2f: @@ -362,7 +348,7 @@ def testFileno(self): self.createTempFile() with open(self.filename, 'rb') as rawf: - bz2f = BZ2File(rawf) + bz2f = BZ2File(fileobj=rawf) try: self.assertEqual(bz2f.fileno(), rawf.fileno()) finally: @@ -370,7 +356,7 @@ self.assertRaises(ValueError, bz2f.fileno) def testSeekable(self): - bz2f = BZ2File(BytesIO(self.DATA)) + bz2f = BZ2File(fileobj=BytesIO(self.DATA)) try: self.assertTrue(bz2f.seekable()) bz2f.read() @@ -379,7 +365,7 @@ bz2f.close() self.assertRaises(ValueError, bz2f.seekable) - bz2f = BZ2File(BytesIO(), mode="w") + bz2f = BZ2File(fileobj=BytesIO(), mode="w") try: self.assertFalse(bz2f.seekable()) finally: @@ -388,7 +374,7 @@ src = BytesIO(self.DATA) src.seekable = lambda: False - bz2f = BZ2File(src) + bz2f = BZ2File(fileobj=src) try: self.assertFalse(bz2f.seekable()) finally: @@ -396,7 +382,7 @@ self.assertRaises(ValueError, bz2f.seekable) def testReadable(self): - bz2f = BZ2File(BytesIO(self.DATA)) + bz2f = BZ2File(fileobj=BytesIO(self.DATA)) try: self.assertTrue(bz2f.readable()) bz2f.read() @@ -405,7 +391,7 @@ bz2f.close() self.assertRaises(ValueError, bz2f.readable) - bz2f = BZ2File(BytesIO(), mode="w") + bz2f = BZ2File(fileobj=BytesIO(), mode="w") try: self.assertFalse(bz2f.readable()) finally: @@ -413,7 +399,7 @@ self.assertRaises(ValueError, bz2f.readable) def testWritable(self): - bz2f = BZ2File(BytesIO(self.DATA)) + bz2f = BZ2File(fileobj=BytesIO(self.DATA)) try: self.assertFalse(bz2f.writable()) bz2f.read() @@ -422,7 +408,7 @@ bz2f.close() self.assertRaises(ValueError, bz2f.writable) - bz2f = BZ2File(BytesIO(), mode="w") + bz2f = BZ2File(fileobj=BytesIO(), mode="w") try: self.assertTrue(bz2f.writable()) finally: @@ -526,14 +512,14 @@ def testReadBytesIO(self): with BytesIO(self.DATA) as bio: - with BZ2File(bio) as bz2f: + with BZ2File(fileobj=bio) as bz2f: self.assertRaises(TypeError, bz2f.read, None) self.assertEqual(bz2f.read(), self.TEXT) self.assertFalse(bio.closed) def testPeekBytesIO(self): with BytesIO(self.DATA) as bio: - with BZ2File(bio) as bz2f: + with BZ2File(fileobj=bio) as bz2f: pdata = bz2f.peek() self.assertNotEqual(len(pdata), 0) self.assertTrue(self.TEXT.startswith(pdata)) @@ -541,7 +527,7 @@ def testWriteBytesIO(self): with BytesIO() as bio: - with BZ2File(bio, "w") as bz2f: + with BZ2File(fileobj=bio, mode="w") as bz2f: self.assertRaises(TypeError, bz2f.write) bz2f.write(self.TEXT) self.assertEqual(self.decompress(bio.getvalue()), self.TEXT) @@ -549,14 +535,14 @@ def testSeekForwardBytesIO(self): with BytesIO(self.DATA) as bio: - with BZ2File(bio) as bz2f: + with BZ2File(fileobj=bio) as bz2f: self.assertRaises(TypeError, bz2f.seek) bz2f.seek(150) self.assertEqual(bz2f.read(), self.TEXT[150:]) def testSeekBackwardsBytesIO(self): with BytesIO(self.DATA) as bio: - with BZ2File(bio) as bz2f: + with BZ2File(fileobj=bio) as bz2f: bz2f.read(500) bz2f.seek(-150, 1) self.assertEqual(bz2f.read(), self.TEXT[500-150:]) @@ -650,7 +636,9 @@ decompressed = None -class CompressDecompressTest(BaseTest): +class FuncTest(BaseTest): + "Test module functions" + def testCompress(self): data = bz2.compress(self.TEXT) self.assertEqual(self.decompress(data), self.TEXT) @@ -670,109 +658,12 @@ text = bz2.decompress(self.DATA * 5) self.assertEqual(text, self.TEXT * 5) - -class OpenTest(BaseTest): - def test_binary_modes(self): - with bz2.open(self.filename, "wb") as f: - f.write(self.TEXT) - with open(self.filename, "rb") as f: - file_data = bz2.decompress(f.read()) - self.assertEqual(file_data, self.TEXT) - with bz2.open(self.filename, "rb") as f: - self.assertEqual(f.read(), self.TEXT) - with bz2.open(self.filename, "ab") as f: - f.write(self.TEXT) - with open(self.filename, "rb") as f: - file_data = bz2.decompress(f.read()) - self.assertEqual(file_data, self.TEXT * 2) - - def test_implicit_binary_modes(self): - # Test implicit binary modes (no "b" or "t" in mode string). - with bz2.open(self.filename, "w") as f: - f.write(self.TEXT) - with open(self.filename, "rb") as f: - file_data = bz2.decompress(f.read()) - self.assertEqual(file_data, self.TEXT) - with bz2.open(self.filename, "r") as f: - self.assertEqual(f.read(), self.TEXT) - with bz2.open(self.filename, "a") as f: - f.write(self.TEXT) - with open(self.filename, "rb") as f: - file_data = bz2.decompress(f.read()) - self.assertEqual(file_data, self.TEXT * 2) - - def test_text_modes(self): - text = self.TEXT.decode("ascii") - text_native_eol = text.replace("\n", os.linesep) - with bz2.open(self.filename, "wt") as f: - f.write(text) - with open(self.filename, "rb") as f: - file_data = bz2.decompress(f.read()).decode("ascii") - self.assertEqual(file_data, text_native_eol) - with bz2.open(self.filename, "rt") as f: - self.assertEqual(f.read(), text) - with bz2.open(self.filename, "at") as f: - f.write(text) - with open(self.filename, "rb") as f: - file_data = bz2.decompress(f.read()).decode("ascii") - self.assertEqual(file_data, text_native_eol * 2) - - def test_fileobj(self): - with bz2.open(BytesIO(self.DATA), "r") as f: - self.assertEqual(f.read(), self.TEXT) - with bz2.open(BytesIO(self.DATA), "rb") as f: - self.assertEqual(f.read(), self.TEXT) - text = self.TEXT.decode("ascii") - with bz2.open(BytesIO(self.DATA), "rt") as f: - self.assertEqual(f.read(), text) - - def test_bad_params(self): - # Test invalid parameter combinations. - with self.assertRaises(ValueError): - bz2.open(self.filename, "wbt") - with self.assertRaises(ValueError): - bz2.open(self.filename, "rb", encoding="utf-8") - with self.assertRaises(ValueError): - bz2.open(self.filename, "rb", errors="ignore") - with self.assertRaises(ValueError): - bz2.open(self.filename, "rb", newline="\n") - - def test_encoding(self): - # Test non-default encoding. - text = self.TEXT.decode("ascii") - text_native_eol = text.replace("\n", os.linesep) - with bz2.open(self.filename, "wt", encoding="utf-16-le") as f: - f.write(text) - with open(self.filename, "rb") as f: - file_data = bz2.decompress(f.read()).decode("utf-16-le") - self.assertEqual(file_data, text_native_eol) - with bz2.open(self.filename, "rt", encoding="utf-16-le") as f: - self.assertEqual(f.read(), text) - - def test_encoding_error_handler(self): - # Test with non-default encoding error handler. - with bz2.open(self.filename, "wb") as f: - f.write(b"foo\xffbar") - with bz2.open(self.filename, "rt", encoding="ascii", errors="ignore") \ - as f: - self.assertEqual(f.read(), "foobar") - - def test_newline(self): - # Test with explicit newline (universal newline mode disabled). - text = self.TEXT.decode("ascii") - with bz2.open(self.filename, "wt", newline="\n") as f: - f.write(text) - with bz2.open(self.filename, "rt", newline="\r") as f: - self.assertEqual(f.readlines(), [text]) - - def test_main(): support.run_unittest( BZ2FileTest, BZ2CompressorTest, BZ2DecompressorTest, - CompressDecompressTest, - OpenTest, + FuncTest ) support.reap_children() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_calendar.py --- a/Lib/test/test_calendar.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_calendar.py Mon May 21 23:01:17 2012 -0400 @@ -5,17 +5,6 @@ from test.script_helper import assert_python_ok import time import locale -import sys - -result_2004_01_text = """ - January 2004 -Mo Tu We Th Fr Sa Su - 1 2 3 4 - 5 6 7 8 9 10 11 -12 13 14 15 16 17 18 -19 20 21 22 23 24 25 -26 27 28 29 30 31 -""" result_2004_text = """ 2004 @@ -56,11 +45,11 @@ """ result_2004_html = """ - + - + Calendar for 2004 @@ -180,135 +169,6 @@ """ -result_2004_days = [ - [[[0, 0, 0, 1, 2, 3, 4], - [5, 6, 7, 8, 9, 10, 11], - [12, 13, 14, 15, 16, 17, 18], - [19, 20, 21, 22, 23, 24, 25], - [26, 27, 28, 29, 30, 31, 0]], - [[0, 0, 0, 0, 0, 0, 1], - [2, 3, 4, 5, 6, 7, 8], - [9, 10, 11, 12, 13, 14, 15], - [16, 17, 18, 19, 20, 21, 22], - [23, 24, 25, 26, 27, 28, 29]], - [[1, 2, 3, 4, 5, 6, 7], - [8, 9, 10, 11, 12, 13, 14], - [15, 16, 17, 18, 19, 20, 21], - [22, 23, 24, 25, 26, 27, 28], - [29, 30, 31, 0, 0, 0, 0]]], - [[[0, 0, 0, 1, 2, 3, 4], - [5, 6, 7, 8, 9, 10, 11], - [12, 13, 14, 15, 16, 17, 18], - [19, 20, 21, 22, 23, 24, 25], - [26, 27, 28, 29, 30, 0, 0]], - [[0, 0, 0, 0, 0, 1, 2], - [3, 4, 5, 6, 7, 8, 9], - [10, 11, 12, 13, 14, 15, 16], - [17, 18, 19, 20, 21, 22, 23], - [24, 25, 26, 27, 28, 29, 30], - [31, 0, 0, 0, 0, 0, 0]], - [[0, 1, 2, 3, 4, 5, 6], - [7, 8, 9, 10, 11, 12, 13], - [14, 15, 16, 17, 18, 19, 20], - [21, 22, 23, 24, 25, 26, 27], - [28, 29, 30, 0, 0, 0, 0]]], - [[[0, 0, 0, 1, 2, 3, 4], - [5, 6, 7, 8, 9, 10, 11], - [12, 13, 14, 15, 16, 17, 18], - [19, 20, 21, 22, 23, 24, 25], - [26, 27, 28, 29, 30, 31, 0]], - [[0, 0, 0, 0, 0, 0, 1], - [2, 3, 4, 5, 6, 7, 8], - [9, 10, 11, 12, 13, 14, 15], - [16, 17, 18, 19, 20, 21, 22], - [23, 24, 25, 26, 27, 28, 29], - [30, 31, 0, 0, 0, 0, 0]], - [[0, 0, 1, 2, 3, 4, 5], - [6, 7, 8, 9, 10, 11, 12], - [13, 14, 15, 16, 17, 18, 19], - [20, 21, 22, 23, 24, 25, 26], - [27, 28, 29, 30, 0, 0, 0]]], - [[[0, 0, 0, 0, 1, 2, 3], - [4, 5, 6, 7, 8, 9, 10], - [11, 12, 13, 14, 15, 16, 17], - [18, 19, 20, 21, 22, 23, 24], - [25, 26, 27, 28, 29, 30, 31]], - [[1, 2, 3, 4, 5, 6, 7], - [8, 9, 10, 11, 12, 13, 14], - [15, 16, 17, 18, 19, 20, 21], - [22, 23, 24, 25, 26, 27, 28], - [29, 30, 0, 0, 0, 0, 0]], - [[0, 0, 1, 2, 3, 4, 5], - [6, 7, 8, 9, 10, 11, 12], - [13, 14, 15, 16, 17, 18, 19], - [20, 21, 22, 23, 24, 25, 26], - [27, 28, 29, 30, 31, 0, 0]]] -] - -result_2004_dates = \ - [[['12/29/03 12/30/03 12/31/03 01/01/04 01/02/04 01/03/04 01/04/04', - '01/05/04 01/06/04 01/07/04 01/08/04 01/09/04 01/10/04 01/11/04', - '01/12/04 01/13/04 01/14/04 01/15/04 01/16/04 01/17/04 01/18/04', - '01/19/04 01/20/04 01/21/04 01/22/04 01/23/04 01/24/04 01/25/04', - '01/26/04 01/27/04 01/28/04 01/29/04 01/30/04 01/31/04 02/01/04'], - ['01/26/04 01/27/04 01/28/04 01/29/04 01/30/04 01/31/04 02/01/04', - '02/02/04 02/03/04 02/04/04 02/05/04 02/06/04 02/07/04 02/08/04', - '02/09/04 02/10/04 02/11/04 02/12/04 02/13/04 02/14/04 02/15/04', - '02/16/04 02/17/04 02/18/04 02/19/04 02/20/04 02/21/04 02/22/04', - '02/23/04 02/24/04 02/25/04 02/26/04 02/27/04 02/28/04 02/29/04'], - ['03/01/04 03/02/04 03/03/04 03/04/04 03/05/04 03/06/04 03/07/04', - '03/08/04 03/09/04 03/10/04 03/11/04 03/12/04 03/13/04 03/14/04', - '03/15/04 03/16/04 03/17/04 03/18/04 03/19/04 03/20/04 03/21/04', - '03/22/04 03/23/04 03/24/04 03/25/04 03/26/04 03/27/04 03/28/04', - '03/29/04 03/30/04 03/31/04 04/01/04 04/02/04 04/03/04 04/04/04']], - [['03/29/04 03/30/04 03/31/04 04/01/04 04/02/04 04/03/04 04/04/04', - '04/05/04 04/06/04 04/07/04 04/08/04 04/09/04 04/10/04 04/11/04', - '04/12/04 04/13/04 04/14/04 04/15/04 04/16/04 04/17/04 04/18/04', - '04/19/04 04/20/04 04/21/04 04/22/04 04/23/04 04/24/04 04/25/04', - '04/26/04 04/27/04 04/28/04 04/29/04 04/30/04 05/01/04 05/02/04'], - ['04/26/04 04/27/04 04/28/04 04/29/04 04/30/04 05/01/04 05/02/04', - '05/03/04 05/04/04 05/05/04 05/06/04 05/07/04 05/08/04 05/09/04', - '05/10/04 05/11/04 05/12/04 05/13/04 05/14/04 05/15/04 05/16/04', - '05/17/04 05/18/04 05/19/04 05/20/04 05/21/04 05/22/04 05/23/04', - '05/24/04 05/25/04 05/26/04 05/27/04 05/28/04 05/29/04 05/30/04', - '05/31/04 06/01/04 06/02/04 06/03/04 06/04/04 06/05/04 06/06/04'], - ['05/31/04 06/01/04 06/02/04 06/03/04 06/04/04 06/05/04 06/06/04', - '06/07/04 06/08/04 06/09/04 06/10/04 06/11/04 06/12/04 06/13/04', - '06/14/04 06/15/04 06/16/04 06/17/04 06/18/04 06/19/04 06/20/04', - '06/21/04 06/22/04 06/23/04 06/24/04 06/25/04 06/26/04 06/27/04', - '06/28/04 06/29/04 06/30/04 07/01/04 07/02/04 07/03/04 07/04/04']], - [['06/28/04 06/29/04 06/30/04 07/01/04 07/02/04 07/03/04 07/04/04', - '07/05/04 07/06/04 07/07/04 07/08/04 07/09/04 07/10/04 07/11/04', - '07/12/04 07/13/04 07/14/04 07/15/04 07/16/04 07/17/04 07/18/04', - '07/19/04 07/20/04 07/21/04 07/22/04 07/23/04 07/24/04 07/25/04', - '07/26/04 07/27/04 07/28/04 07/29/04 07/30/04 07/31/04 08/01/04'], - ['07/26/04 07/27/04 07/28/04 07/29/04 07/30/04 07/31/04 08/01/04', - '08/02/04 08/03/04 08/04/04 08/05/04 08/06/04 08/07/04 08/08/04', - '08/09/04 08/10/04 08/11/04 08/12/04 08/13/04 08/14/04 08/15/04', - '08/16/04 08/17/04 08/18/04 08/19/04 08/20/04 08/21/04 08/22/04', - '08/23/04 08/24/04 08/25/04 08/26/04 08/27/04 08/28/04 08/29/04', - '08/30/04 08/31/04 09/01/04 09/02/04 09/03/04 09/04/04 09/05/04'], - ['08/30/04 08/31/04 09/01/04 09/02/04 09/03/04 09/04/04 09/05/04', - '09/06/04 09/07/04 09/08/04 09/09/04 09/10/04 09/11/04 09/12/04', - '09/13/04 09/14/04 09/15/04 09/16/04 09/17/04 09/18/04 09/19/04', - '09/20/04 09/21/04 09/22/04 09/23/04 09/24/04 09/25/04 09/26/04', - '09/27/04 09/28/04 09/29/04 09/30/04 10/01/04 10/02/04 10/03/04']], - [['09/27/04 09/28/04 09/29/04 09/30/04 10/01/04 10/02/04 10/03/04', - '10/04/04 10/05/04 10/06/04 10/07/04 10/08/04 10/09/04 10/10/04', - '10/11/04 10/12/04 10/13/04 10/14/04 10/15/04 10/16/04 10/17/04', - '10/18/04 10/19/04 10/20/04 10/21/04 10/22/04 10/23/04 10/24/04', - '10/25/04 10/26/04 10/27/04 10/28/04 10/29/04 10/30/04 10/31/04'], - ['11/01/04 11/02/04 11/03/04 11/04/04 11/05/04 11/06/04 11/07/04', - '11/08/04 11/09/04 11/10/04 11/11/04 11/12/04 11/13/04 11/14/04', - '11/15/04 11/16/04 11/17/04 11/18/04 11/19/04 11/20/04 11/21/04', - '11/22/04 11/23/04 11/24/04 11/25/04 11/26/04 11/27/04 11/28/04', - '11/29/04 11/30/04 12/01/04 12/02/04 12/03/04 12/04/04 12/05/04'], - ['11/29/04 11/30/04 12/01/04 12/02/04 12/03/04 12/04/04 12/05/04', - '12/06/04 12/07/04 12/08/04 12/09/04 12/10/04 12/11/04 12/12/04', - '12/13/04 12/14/04 12/15/04 12/16/04 12/17/04 12/18/04 12/19/04', - '12/20/04 12/21/04 12/22/04 12/23/04 12/24/04 12/25/04 12/26/04', - '12/27/04 12/28/04 12/29/04 12/30/04 12/31/04 01/01/05 01/02/05']]] - class OutputTestCase(unittest.TestCase): def normalize_calendar(self, s): @@ -323,13 +183,6 @@ lines.append(line) return lines - def check_htmlcalendar_encoding(self, req, res): - cal = calendar.HTMLCalendar() - self.assertEqual( - cal.formatyearpage(2004, encoding=req).strip(b' \t\n'), - (result_2004_html % {'e': res}).strip(' \t\n').encode(res) - ) - def test_output(self): self.assertEqual( self.normalize_calendar(calendar.calendar(2004)), @@ -342,60 +195,12 @@ result_2004_text.strip() ) - def test_output_htmlcalendar_encoding_ascii(self): - self.check_htmlcalendar_encoding('ascii', 'ascii') - - def test_output_htmlcalendar_encoding_utf8(self): - self.check_htmlcalendar_encoding('utf-8', 'utf-8') - - def test_output_htmlcalendar_encoding_default(self): - self.check_htmlcalendar_encoding(None, sys.getdefaultencoding()) - - def test_yeardatescalendar(self): - def shrink(cal): - return [[[' '.join('{:02d}/{:02d}/{}'.format( - d.month, d.day, str(d.year)[-2:]) for d in z) - for z in y] for y in x] for x in cal] + def test_output_htmlcalendar(self): + encoding = 'ascii' + cal = calendar.HTMLCalendar() self.assertEqual( - shrink(calendar.Calendar().yeardatescalendar(2004)), - result_2004_dates - ) - - def test_yeardayscalendar(self): - self.assertEqual( - calendar.Calendar().yeardayscalendar(2004), - result_2004_days - ) - - def test_formatweekheader_short(self): - self.assertEqual( - calendar.TextCalendar().formatweekheader(2), - 'Mo Tu We Th Fr Sa Su' - ) - - def test_formatweekheader_long(self): - self.assertEqual( - calendar.TextCalendar().formatweekheader(9), - ' Monday Tuesday Wednesday Thursday ' - ' Friday Saturday Sunday ' - ) - - def test_formatmonth(self): - self.assertEqual( - calendar.TextCalendar().formatmonth(2004, 1).strip(), - result_2004_01_text.strip() - ) - - def test_formatmonthname_with_year(self): - self.assertEqual( - calendar.HTMLCalendar().formatmonthname(2004, 1, withyear=True), - 'January 2004' - ) - - def test_formatmonthname_without_year(self): - self.assertEqual( - calendar.HTMLCalendar().formatmonthname(2004, 1, withyear=False), - 'January' + cal.formatyearpage(2004, encoding=encoding).strip(b' \t\n'), + result_2004_html.strip(' \t\n').encode(encoding) ) @@ -421,11 +226,7 @@ self.assertEqual(calendar.firstweekday(), calendar.MONDAY) calendar.setfirstweekday(orig) - def test_illegal_weekday_reported(self): - with self.assertRaisesRegex(calendar.IllegalWeekdayError, '123'): - calendar.setfirstweekday(123) - - def test_enumerate_weekdays(self): + def test_enumerateweekdays(self): self.assertRaises(IndexError, calendar.day_abbr.__getitem__, -10) self.assertRaises(IndexError, calendar.day_name.__getitem__, 10) self.assertEqual(len([d for d in calendar.day_abbr]), 7) @@ -451,7 +252,7 @@ # verify it "acts like a sequence" in two forms of iteration self.assertEqual(value[::-1], list(reversed(value))) - def test_locale_calendars(self): + def test_localecalendars(self): # ensure that Locale{Text,HTML}Calendar resets the locale properly # (it is still not thread-safe though) old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10) @@ -630,10 +431,6 @@ with self.assertRaises(calendar.IllegalMonthError): calendar.monthrange(2004, 13) - def test_illegal_month_reported(self): - with self.assertRaisesRegex(calendar.IllegalMonthError, '65'): - calendar.monthrange(2004, 65) - class LeapdaysTestCase(unittest.TestCase): def test_no_range(self): # test when no range i.e. two identical years as args diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_cmd.py --- a/Lib/test/test_cmd.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_cmd.py Mon May 21 23:01:17 2012 -0400 @@ -228,7 +228,7 @@ def test_coverage(coverdir): trace = support.import_module('trace') - tracer=trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,], + tracer=trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, count=1) tracer.run('reload(cmd);test_main()') r=tracer.results() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_cmd_line_script.py --- a/Lib/test/test_cmd_line_script.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_cmd_line_script.py Mon May 21 23:01:17 2012 -0400 @@ -7,7 +7,6 @@ import os.path import py_compile -import textwrap from test import support from test.script_helper import ( make_pkg, make_script, make_zip_pkg, make_zip_script, @@ -287,24 +286,6 @@ self._check_output(script_name, rc, out, script_name, script_name, '', '') - def test_pep_409_verbiage(self): - # Make sure PEP 409 syntax properly suppresses - # the context of an exception - script = textwrap.dedent("""\ - try: - raise ValueError - except: - raise NameError from None - """) - with temp_dir() as script_dir: - script_name = _make_test_script(script_dir, 'script', script) - exitcode, stdout, stderr = assert_python_failure(script_name) - text = stderr.decode('ascii').split('\n') - self.assertEqual(len(text), 4) - self.assertTrue(text[0].startswith('Traceback')) - self.assertTrue(text[1].startswith(' File ')) - self.assertTrue(text[3].startswith('NameError')) - def test_main(): support.run_unittest(CmdLineTest) support.reap_children() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_contextlib.py --- a/Lib/test/test_contextlib.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_contextlib.py Mon May 21 23:01:17 2012 -0400 @@ -370,231 +370,6 @@ self.assertEqual(state, [1, 'something else', 999]) -class TestExitStack(unittest.TestCase): - - def test_no_resources(self): - with ExitStack(): - pass - - def test_callback(self): - expected = [ - ((), {}), - ((1,), {}), - ((1,2), {}), - ((), dict(example=1)), - ((1,), dict(example=1)), - ((1,2), dict(example=1)), - ] - result = [] - def _exit(*args, **kwds): - """Test metadata propagation""" - result.append((args, kwds)) - with ExitStack() as stack: - for args, kwds in reversed(expected): - if args and kwds: - f = stack.callback(_exit, *args, **kwds) - elif args: - f = stack.callback(_exit, *args) - elif kwds: - f = stack.callback(_exit, **kwds) - else: - f = stack.callback(_exit) - self.assertIs(f, _exit) - for wrapper in stack._exit_callbacks: - self.assertIs(wrapper.__wrapped__, _exit) - self.assertNotEqual(wrapper.__name__, _exit.__name__) - self.assertIsNone(wrapper.__doc__, _exit.__doc__) - self.assertEqual(result, expected) - - def test_push(self): - exc_raised = ZeroDivisionError - def _expect_exc(exc_type, exc, exc_tb): - self.assertIs(exc_type, exc_raised) - def _suppress_exc(*exc_details): - return True - def _expect_ok(exc_type, exc, exc_tb): - self.assertIsNone(exc_type) - self.assertIsNone(exc) - self.assertIsNone(exc_tb) - class ExitCM(object): - def __init__(self, check_exc): - self.check_exc = check_exc - def __enter__(self): - self.fail("Should not be called!") - def __exit__(self, *exc_details): - self.check_exc(*exc_details) - with ExitStack() as stack: - stack.push(_expect_ok) - self.assertIs(stack._exit_callbacks[-1], _expect_ok) - cm = ExitCM(_expect_ok) - stack.push(cm) - self.assertIs(stack._exit_callbacks[-1].__self__, cm) - stack.push(_suppress_exc) - self.assertIs(stack._exit_callbacks[-1], _suppress_exc) - cm = ExitCM(_expect_exc) - stack.push(cm) - self.assertIs(stack._exit_callbacks[-1].__self__, cm) - stack.push(_expect_exc) - self.assertIs(stack._exit_callbacks[-1], _expect_exc) - stack.push(_expect_exc) - self.assertIs(stack._exit_callbacks[-1], _expect_exc) - 1/0 - - def test_enter_context(self): - class TestCM(object): - def __enter__(self): - result.append(1) - def __exit__(self, *exc_details): - result.append(3) - - result = [] - cm = TestCM() - with ExitStack() as stack: - @stack.callback # Registered first => cleaned up last - def _exit(): - result.append(4) - self.assertIsNotNone(_exit) - stack.enter_context(cm) - self.assertIs(stack._exit_callbacks[-1].__self__, cm) - result.append(2) - self.assertEqual(result, [1, 2, 3, 4]) - - def test_close(self): - result = [] - with ExitStack() as stack: - @stack.callback - def _exit(): - result.append(1) - self.assertIsNotNone(_exit) - stack.close() - result.append(2) - self.assertEqual(result, [1, 2]) - - def test_pop_all(self): - result = [] - with ExitStack() as stack: - @stack.callback - def _exit(): - result.append(3) - self.assertIsNotNone(_exit) - new_stack = stack.pop_all() - result.append(1) - result.append(2) - new_stack.close() - self.assertEqual(result, [1, 2, 3]) - - def test_exit_raise(self): - with self.assertRaises(ZeroDivisionError): - with ExitStack() as stack: - stack.push(lambda *exc: False) - 1/0 - - def test_exit_suppress(self): - with ExitStack() as stack: - stack.push(lambda *exc: True) - 1/0 - - def test_exit_exception_chaining_reference(self): - # Sanity check to make sure that ExitStack chaining matches - # actual nested with statements - class RaiseExc: - def __init__(self, exc): - self.exc = exc - def __enter__(self): - return self - def __exit__(self, *exc_details): - raise self.exc - - class RaiseExcWithContext: - def __init__(self, outer, inner): - self.outer = outer - self.inner = inner - def __enter__(self): - return self - def __exit__(self, *exc_details): - try: - raise self.inner - except: - raise self.outer - - class SuppressExc: - def __enter__(self): - return self - def __exit__(self, *exc_details): - type(self).saved_details = exc_details - return True - - try: - with RaiseExc(IndexError): - with RaiseExcWithContext(KeyError, AttributeError): - with SuppressExc(): - with RaiseExc(ValueError): - 1 / 0 - except IndexError as exc: - self.assertIsInstance(exc.__context__, KeyError) - self.assertIsInstance(exc.__context__.__context__, AttributeError) - # Inner exceptions were suppressed - self.assertIsNone(exc.__context__.__context__.__context__) - else: - self.fail("Expected IndexError, but no exception was raised") - # Check the inner exceptions - inner_exc = SuppressExc.saved_details[1] - self.assertIsInstance(inner_exc, ValueError) - self.assertIsInstance(inner_exc.__context__, ZeroDivisionError) - - def test_exit_exception_chaining(self): - # Ensure exception chaining matches the reference behaviour - def raise_exc(exc): - raise exc - - saved_details = None - def suppress_exc(*exc_details): - nonlocal saved_details - saved_details = exc_details - return True - - try: - with ExitStack() as stack: - stack.callback(raise_exc, IndexError) - stack.callback(raise_exc, KeyError) - stack.callback(raise_exc, AttributeError) - stack.push(suppress_exc) - stack.callback(raise_exc, ValueError) - 1 / 0 - except IndexError as exc: - self.assertIsInstance(exc.__context__, KeyError) - self.assertIsInstance(exc.__context__.__context__, AttributeError) - # Inner exceptions were suppressed - self.assertIsNone(exc.__context__.__context__.__context__) - else: - self.fail("Expected IndexError, but no exception was raised") - # Check the inner exceptions - inner_exc = saved_details[1] - self.assertIsInstance(inner_exc, ValueError) - self.assertIsInstance(inner_exc.__context__, ZeroDivisionError) - - def test_exit_exception_chaining_suppress(self): - with ExitStack() as stack: - stack.push(lambda *exc: True) - stack.push(lambda *exc: 1/0) - stack.push(lambda *exc: {}[1]) - - def test_excessive_nesting(self): - # The original implementation would die with RecursionError here - with ExitStack() as stack: - for i in range(10000): - stack.callback(int) - - def test_instance_bypass(self): - class Example(object): pass - cm = Example() - cm.__exit__ = object() - stack = ExitStack() - self.assertRaises(AttributeError, stack.enter_context, cm) - stack.push(cm) - self.assertIs(stack._exit_callbacks[-1], cm) - - # This is needed to make the test actually run under regrtest.py! def test_main(): support.run_unittest(__name__) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_dis.py --- a/Lib/test/test_dis.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_dis.py Mon May 21 23:01:17 2012 -0400 @@ -38,7 +38,7 @@ dis_f = """\ %-4d 0 LOAD_GLOBAL 0 (print) 3 LOAD_FAST 0 (a) - 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair) + 6 CALL_FUNCTION 1 9 POP_TOP %-4d 10 LOAD_CONST 1 (1) @@ -50,7 +50,7 @@ dis_f_co_code = """\ 0 LOAD_GLOBAL 0 (0) 3 LOAD_FAST 0 (0) - 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair) + 6 CALL_FUNCTION 1 9 POP_TOP 10 LOAD_CONST 1 (1) 13 RETURN_VALUE @@ -68,7 +68,7 @@ 6 LOAD_CONST 1 (1) %-4d 9 LOAD_CONST 2 (10) - 12 CALL_FUNCTION 2 (2 positional, 0 keyword pair) + 12 CALL_FUNCTION 2 15 GET_ITER >> 16 FOR_ITER 6 (to 25) 19 STORE_FAST 0 (res) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_doctest.py --- a/Lib/test/test_doctest.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_doctest.py Mon May 21 23:01:17 2012 -0400 @@ -2543,7 +2543,7 @@ def test_coverage(coverdir): trace = support.import_module('trace') - tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,], + tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, count=1) tracer.run('test_main()') r = tracer.results() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/__init__.py --- a/Lib/test/test_email/__init__.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_email/__init__.py Mon May 21 23:01:17 2012 -0400 @@ -3,8 +3,6 @@ import unittest import test.support import email -from email.message import Message -from email._policybase import compat32 from test.test_email import __file__ as landmark # Run all tests in package for '-m unittest test.test_email' @@ -38,26 +36,16 @@ class TestEmailBase(unittest.TestCase): maxDiff = None - # Currently the default policy is compat32. By setting that as the default - # here we make minimal changes in the test_email tests compared to their - # pre-3.3 state. - policy = compat32 def __init__(self, *args, **kw): super().__init__(*args, **kw) self.addTypeEqualityFunc(bytes, self.assertBytesEqual) - # Backward compatibility to minimize test_email test changes. ndiffAssertEqual = unittest.TestCase.assertEqual def _msgobj(self, filename): with openfile(filename) as fp: - return email.message_from_file(fp, policy=self.policy) - - def _str_msg(self, string, message=Message, policy=None): - if policy is None: - policy = self.policy - return email.message_from_string(string, message, policy=policy) + return email.message_from_file(fp) def _bytes_repr(self, b): return [repr(x) for x in b.splitlines(keepends=True)] @@ -65,86 +53,3 @@ def assertBytesEqual(self, first, second, msg): """Our byte strings are really encoded strings; improve diff output""" self.assertEqual(self._bytes_repr(first), self._bytes_repr(second)) - - def assertDefectsEqual(self, actual, expected): - self.assertEqual(len(actual), len(expected), actual) - for i in range(len(actual)): - self.assertIsInstance(actual[i], expected[i], - 'item {}'.format(i)) - - -def parameterize(cls): - """A test method parameterization class decorator. - - Parameters are specified as the value of a class attribute that ends with - the string '_params'. Call the portion before '_params' the prefix. Then - a method to be parameterized must have the same prefix, the string - '_as_', and an arbitrary suffix. - - The value of the _params attribute may be either a dictionary or a list. - The values in the dictionary and the elements of the list may either be - single values, or a list. If single values, they are turned into single - element tuples. However derived, the resulting sequence is passed via - *args to the parameterized test function. - - In a _params dictioanry, the keys become part of the name of the generated - tests. In a _params list, the values in the list are converted into a - string by joining the string values of the elements of the tuple by '_' and - converting any blanks into '_'s, and this become part of the name. - The full name of a generated test is a 'test_' prefix, the portion of the - test function name after the '_as_' separator, plus an '_', plus the name - derived as explained above. - - For example, if we have: - - count_params = range(2) - - def count_as_foo_arg(self, foo): - self.assertEqual(foo+1, myfunc(foo)) - - we will get parameterized test methods named: - test_foo_arg_0 - test_foo_arg_1 - test_foo_arg_2 - - Or we could have: - - example_params = {'foo': ('bar', 1), 'bing': ('bang', 2)} - - def example_as_myfunc_input(self, name, count): - self.assertEqual(name+str(count), myfunc(name, count)) - - and get: - test_myfunc_input_foo - test_myfunc_input_bing - - Note: if and only if the generated test name is a valid identifier can it - be used to select the test individually from the unittest command line. - - """ - paramdicts = {} - for name, attr in cls.__dict__.items(): - if name.endswith('_params'): - if not hasattr(attr, 'keys'): - d = {} - for x in attr: - if not hasattr(x, '__iter__'): - x = (x,) - n = '_'.join(str(v) for v in x).replace(' ', '_') - d[n] = x - attr = d - paramdicts[name[:-7] + '_as_'] = attr - testfuncs = {} - for name, attr in cls.__dict__.items(): - for paramsname, paramsdict in paramdicts.items(): - if name.startswith(paramsname): - testnameroot = 'test_' + name[len(paramsname):] - for paramname, params in paramsdict.items(): - test = (lambda self, name=name, params=params: - getattr(self, name)(*params)) - testname = testnameroot + '_' + paramname - test.__name__ = testname - testfuncs[testname] = test - for key, value in testfuncs.items(): - setattr(cls, key, value) - return cls diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/test__encoded_words.py --- a/Lib/test/test_email/test__encoded_words.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,187 +0,0 @@ -import unittest -from email import _encoded_words as _ew -from email import errors -from test.test_email import TestEmailBase - - -class TestDecodeQ(TestEmailBase): - - def _test(self, source, ex_result, ex_defects=[]): - result, defects = _ew.decode_q(source) - self.assertEqual(result, ex_result) - self.assertDefectsEqual(defects, ex_defects) - - def test_no_encoded(self): - self._test(b'foobar', b'foobar') - - def test_spaces(self): - self._test(b'foo=20bar=20', b'foo bar ') - self._test(b'foo_bar_', b'foo bar ') - - def test_run_of_encoded(self): - self._test(b'foo=20=20=21=2Cbar', b'foo !,bar') - - -class TestDecodeB(TestEmailBase): - - def _test(self, source, ex_result, ex_defects=[]): - result, defects = _ew.decode_b(source) - self.assertEqual(result, ex_result) - self.assertDefectsEqual(defects, ex_defects) - - def test_simple(self): - self._test(b'Zm9v', b'foo') - - def test_missing_padding(self): - self._test(b'dmk', b'vi', [errors.InvalidBase64PaddingDefect]) - - def test_invalid_character(self): - self._test(b'dm\x01k===', b'vi', [errors.InvalidBase64CharactersDefect]) - - def test_invalid_character_and_bad_padding(self): - self._test(b'dm\x01k', b'vi', [errors.InvalidBase64CharactersDefect, - errors.InvalidBase64PaddingDefect]) - - -class TestDecode(TestEmailBase): - - def test_wrong_format_input_raises(self): - with self.assertRaises(ValueError): - _ew.decode('=?badone?=') - with self.assertRaises(ValueError): - _ew.decode('=?') - with self.assertRaises(ValueError): - _ew.decode('') - - def _test(self, source, result, charset='us-ascii', lang='', defects=[]): - res, char, l, d = _ew.decode(source) - self.assertEqual(res, result) - self.assertEqual(char, charset) - self.assertEqual(l, lang) - self.assertDefectsEqual(d, defects) - - def test_simple_q(self): - self._test('=?us-ascii?q?foo?=', 'foo') - - def test_simple_b(self): - self._test('=?us-ascii?b?dmk=?=', 'vi') - - def test_q_case_ignored(self): - self._test('=?us-ascii?Q?foo?=', 'foo') - - def test_b_case_ignored(self): - self._test('=?us-ascii?B?dmk=?=', 'vi') - - def test_non_trivial_q(self): - self._test('=?latin-1?q?=20F=fcr=20Elise=20?=', ' Für Elise ', 'latin-1') - - def test_q_escpaed_bytes_preserved(self): - self._test(b'=?us-ascii?q?=20\xACfoo?='.decode('us-ascii', - 'surrogateescape'), - ' \uDCACfoo', - defects = [errors.UndecodableBytesDefect]) - - def test_b_undecodable_bytes_ignored_with_defect(self): - self._test(b'=?us-ascii?b?dm\xACk?='.decode('us-ascii', - 'surrogateescape'), - 'vi', - defects = [ - errors.InvalidBase64CharactersDefect, - errors.InvalidBase64PaddingDefect]) - - def test_b_invalid_bytes_ignored_with_defect(self): - self._test('=?us-ascii?b?dm\x01k===?=', - 'vi', - defects = [errors.InvalidBase64CharactersDefect]) - - def test_b_invalid_bytes_incorrect_padding(self): - self._test('=?us-ascii?b?dm\x01k?=', - 'vi', - defects = [ - errors.InvalidBase64CharactersDefect, - errors.InvalidBase64PaddingDefect]) - - def test_b_padding_defect(self): - self._test('=?us-ascii?b?dmk?=', - 'vi', - defects = [errors.InvalidBase64PaddingDefect]) - - def test_nonnull_lang(self): - self._test('=?us-ascii*jive?q?test?=', 'test', lang='jive') - - def test_unknown_8bit_charset(self): - self._test('=?unknown-8bit?q?foo=ACbar?=', - b'foo\xacbar'.decode('ascii', 'surrogateescape'), - charset = 'unknown-8bit', - defects = []) - - def test_unknown_charset(self): - self._test('=?foobar?q?foo=ACbar?=', - b'foo\xacbar'.decode('ascii', 'surrogateescape'), - charset = 'foobar', - # XXX Should this be a new Defect instead? - defects = [errors.CharsetError]) - - -class TestEncodeQ(TestEmailBase): - - def _test(self, src, expected): - self.assertEqual(_ew.encode_q(src), expected) - - def test_all_safe(self): - self._test(b'foobar', 'foobar') - - def test_spaces(self): - self._test(b'foo bar ', 'foo_bar_') - - def test_run_of_encodables(self): - self._test(b'foo ,,bar', 'foo__=2C=2Cbar') - - -class TestEncodeB(TestEmailBase): - - def test_simple(self): - self.assertEqual(_ew.encode_b(b'foo'), 'Zm9v') - - def test_padding(self): - self.assertEqual(_ew.encode_b(b'vi'), 'dmk=') - - -class TestEncode(TestEmailBase): - - def test_q(self): - self.assertEqual(_ew.encode('foo', 'utf-8', 'q'), '=?utf-8?q?foo?=') - - def test_b(self): - self.assertEqual(_ew.encode('foo', 'utf-8', 'b'), '=?utf-8?b?Zm9v?=') - - def test_auto_q(self): - self.assertEqual(_ew.encode('foo', 'utf-8'), '=?utf-8?q?foo?=') - - def test_auto_q_if_short_mostly_safe(self): - self.assertEqual(_ew.encode('vi.', 'utf-8'), '=?utf-8?q?vi=2E?=') - - def test_auto_b_if_enough_unsafe(self): - self.assertEqual(_ew.encode('.....', 'utf-8'), '=?utf-8?b?Li4uLi4=?=') - - def test_auto_b_if_long_unsafe(self): - self.assertEqual(_ew.encode('vi.vi.vi.vi.vi.', 'utf-8'), - '=?utf-8?b?dmkudmkudmkudmkudmku?=') - - def test_auto_q_if_long_mostly_safe(self): - self.assertEqual(_ew.encode('vi vi vi.vi ', 'utf-8'), - '=?utf-8?q?vi_vi_vi=2Evi_?=') - - def test_utf8_default(self): - self.assertEqual(_ew.encode('foo'), '=?utf-8?q?foo?=') - - def test_lang(self): - self.assertEqual(_ew.encode('foo', lang='jive'), '=?utf-8*jive?q?foo?=') - - def test_unknown_8bit(self): - self.assertEqual(_ew.encode('foo\uDCACbar', charset='unknown-8bit'), - '=?unknown-8bit?q?foo=ACbar?=') - - -if __name__ == '__main__': - unittest.main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/test__header_value_parser.py --- a/Lib/test/test_email/test__header_value_parser.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,2479 +0,0 @@ -import string -import unittest -from email import _header_value_parser as parser -from email import errors -from email import policy -from test.test_email import TestEmailBase - -class TestTokens(TestEmailBase): - - # EWWhiteSpaceTerminal - - def test_EWWhiteSpaceTerminal(self): - x = parser.EWWhiteSpaceTerminal(' \t', 'fws') - self.assertEqual(x, ' \t') - self.assertEqual(str(x), '') - self.assertEqual(x.value, '') - self.assertEqual(x.encoded, ' \t') - - # UnstructuredTokenList - - def test_undecodable_bytes_error_preserved(self): - badstr = b"le pouf c\xaflebre".decode('ascii', 'surrogateescape') - unst = parser.get_unstructured(badstr) - self.assertDefectsEqual(unst.all_defects, [errors.UndecodableBytesDefect]) - parts = list(unst.parts) - self.assertDefectsEqual(parts[0].all_defects, []) - self.assertDefectsEqual(parts[1].all_defects, []) - self.assertDefectsEqual(parts[2].all_defects, [errors.UndecodableBytesDefect]) - - -class TestParser(TestEmailBase): - - # _wsp_splitter - - rfc_printable_ascii = bytes(range(33, 127)).decode('ascii') - rfc_atext_chars = (string.ascii_letters + string.digits + - "!#$%&\'*+-/=?^_`{}|~") - rfc_dtext_chars = rfc_printable_ascii.translate(str.maketrans('','',r'\[]')) - - def test__wsp_splitter_one_word(self): - self.assertEqual(parser._wsp_splitter('foo', 1), ['foo']) - - def test__wsp_splitter_two_words(self): - self.assertEqual(parser._wsp_splitter('foo def', 1), - ['foo', ' ', 'def']) - - def test__wsp_splitter_ws_runs(self): - self.assertEqual(parser._wsp_splitter('foo \t def jik', 1), - ['foo', ' \t ', 'def jik']) - - - # test harness - - def _test_get_x(self, method, input, string, value, defects, - remainder, comments=None): - token, rest = method(input) - self.assertEqual(str(token), string) - self.assertEqual(token.value, value) - self.assertDefectsEqual(token.all_defects, defects) - self.assertEqual(rest, remainder) - if comments is not None: - self.assertEqual(token.comments, comments) - return token - - # get_fws - - def test_get_fws_only(self): - fws = self._test_get_x(parser.get_fws, ' \t ', ' \t ', ' ', [], '') - self.assertEqual(fws.token_type, 'fws') - - def test_get_fws_space(self): - self._test_get_x(parser.get_fws, ' foo', ' ', ' ', [], 'foo') - - def test_get_fws_ws_run(self): - self._test_get_x(parser.get_fws, ' \t foo ', ' \t ', ' ', [], 'foo ') - - # get_encoded_word - - def test_get_encoded_word_missing_start_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_encoded_word('abc') - - def test_get_encoded_word_missing_end_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_encoded_word('=?abc') - - def test_get_encoded_word_missing_middle_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_encoded_word('=?abc?=') - - def test_get_encoded_word_valid_ew(self): - self._test_get_x(parser.get_encoded_word, - '=?us-ascii?q?this_is_a_test?= bird', - 'this is a test', - 'this is a test', - [], - ' bird') - - def test_get_encoded_word_internal_spaces(self): - self._test_get_x(parser.get_encoded_word, - '=?us-ascii?q?this is a test?= bird', - 'this is a test', - 'this is a test', - [errors.InvalidHeaderDefect], - ' bird') - - def test_get_encoded_word_gets_first(self): - self._test_get_x(parser.get_encoded_word, - '=?us-ascii?q?first?= =?utf-8?q?second?=', - 'first', - 'first', - [], - ' =?utf-8?q?second?=') - - def test_get_encoded_word_gets_first_even_if_no_space(self): - self._test_get_x(parser.get_encoded_word, - '=?us-ascii?q?first?==?utf-8?q?second?=', - 'first', - 'first', - [], - '=?utf-8?q?second?=') - - def test_get_encoded_word_sets_extra_attributes(self): - ew = self._test_get_x(parser.get_encoded_word, - '=?us-ascii*jive?q?first_second?=', - 'first second', - 'first second', - [], - '') - self.assertEqual(ew.encoded, '=?us-ascii*jive?q?first_second?=') - self.assertEqual(ew.charset, 'us-ascii') - self.assertEqual(ew.lang, 'jive') - - def test_get_encoded_word_lang_default_is_blank(self): - ew = self._test_get_x(parser.get_encoded_word, - '=?us-ascii?q?first_second?=', - 'first second', - 'first second', - [], - '') - self.assertEqual(ew.encoded, '=?us-ascii?q?first_second?=') - self.assertEqual(ew.charset, 'us-ascii') - self.assertEqual(ew.lang, '') - - def test_get_encoded_word_non_printable_defect(self): - self._test_get_x(parser.get_encoded_word, - '=?us-ascii?q?first\x02second?=', - 'first\x02second', - 'first\x02second', - [errors.NonPrintableDefect], - '') - - def test_get_encoded_word_leading_internal_space(self): - self._test_get_x(parser.get_encoded_word, - '=?us-ascii?q?=20foo?=', - ' foo', - ' foo', - [], - '') - - # get_unstructured - - def _get_unst(self, value): - token = parser.get_unstructured(value) - return token, '' - - def test_get_unstructured_null(self): - self._test_get_x(self._get_unst, '', '', '', [], '') - - def test_get_unstructured_one_word(self): - self._test_get_x(self._get_unst, 'foo', 'foo', 'foo', [], '') - - def test_get_unstructured_normal_phrase(self): - self._test_get_x(self._get_unst, 'foo bar bird', - 'foo bar bird', - 'foo bar bird', - [], - '') - - def test_get_unstructured_normal_phrase_with_whitespace(self): - self._test_get_x(self._get_unst, 'foo \t bar bird', - 'foo \t bar bird', - 'foo bar bird', - [], - '') - - def test_get_unstructured_leading_whitespace(self): - self._test_get_x(self._get_unst, ' foo bar', - ' foo bar', - ' foo bar', - [], - '') - - def test_get_unstructured_trailing_whitespace(self): - self._test_get_x(self._get_unst, 'foo bar ', - 'foo bar ', - 'foo bar ', - [], - '') - - def test_get_unstructured_leading_and_trailing_whitespace(self): - self._test_get_x(self._get_unst, ' foo bar ', - ' foo bar ', - ' foo bar ', - [], - '') - - def test_get_unstructured_one_valid_ew_no_ws(self): - self._test_get_x(self._get_unst, '=?us-ascii?q?bar?=', - 'bar', - 'bar', - [], - '') - - def test_get_unstructured_one_ew_trailing_ws(self): - self._test_get_x(self._get_unst, '=?us-ascii?q?bar?= ', - 'bar ', - 'bar ', - [], - '') - - def test_get_unstructured_one_valid_ew_trailing_text(self): - self._test_get_x(self._get_unst, '=?us-ascii?q?bar?= bird', - 'bar bird', - 'bar bird', - [], - '') - - def test_get_unstructured_phrase_with_ew_in_middle_of_text(self): - self._test_get_x(self._get_unst, 'foo =?us-ascii?q?bar?= bird', - 'foo bar bird', - 'foo bar bird', - [], - '') - - def test_get_unstructured_phrase_with_two_ew(self): - self._test_get_x(self._get_unst, - 'foo =?us-ascii?q?bar?= =?us-ascii?q?bird?=', - 'foo barbird', - 'foo barbird', - [], - '') - - def test_get_unstructured_phrase_with_two_ew_trailing_ws(self): - self._test_get_x(self._get_unst, - 'foo =?us-ascii?q?bar?= =?us-ascii?q?bird?= ', - 'foo barbird ', - 'foo barbird ', - [], - '') - - def test_get_unstructured_phrase_with_ew_with_leading_ws(self): - self._test_get_x(self._get_unst, - ' =?us-ascii?q?bar?=', - ' bar', - ' bar', - [], - '') - - def test_get_unstructured_phrase_with_two_ew_extra_ws(self): - self._test_get_x(self._get_unst, - 'foo =?us-ascii?q?bar?= \t =?us-ascii?q?bird?=', - 'foo barbird', - 'foo barbird', - [], - '') - - def test_get_unstructured_two_ew_extra_ws_trailing_text(self): - self._test_get_x(self._get_unst, - '=?us-ascii?q?test?= =?us-ascii?q?foo?= val', - 'testfoo val', - 'testfoo val', - [], - '') - - def test_get_unstructured_ew_with_internal_ws(self): - self._test_get_x(self._get_unst, - '=?iso-8859-1?q?hello=20world?=', - 'hello world', - 'hello world', - [], - '') - - def test_get_unstructured_ew_with_internal_leading_ws(self): - self._test_get_x(self._get_unst, - ' =?us-ascii?q?=20test?= =?us-ascii?q?=20foo?= val', - ' test foo val', - ' test foo val', - [], - '') - - def test_get_unstructured_invaild_ew(self): - self._test_get_x(self._get_unst, - '=?test val', - '=?test val', - '=?test val', - [], - '') - - def test_get_unstructured_undecodable_bytes(self): - self._test_get_x(self._get_unst, - b'test \xACfoo val'.decode('ascii', 'surrogateescape'), - 'test \uDCACfoo val', - 'test \uDCACfoo val', - [errors.UndecodableBytesDefect], - '') - - def test_get_unstructured_undecodable_bytes_in_EW(self): - self._test_get_x(self._get_unst, - (b'=?us-ascii?q?=20test?= =?us-ascii?q?=20\xACfoo?=' - b' val').decode('ascii', 'surrogateescape'), - ' test \uDCACfoo val', - ' test \uDCACfoo val', - [errors.UndecodableBytesDefect]*2, - '') - - def test_get_unstructured_missing_base64_padding(self): - self._test_get_x(self._get_unst, - '=?utf-8?b?dmk?=', - 'vi', - 'vi', - [errors.InvalidBase64PaddingDefect], - '') - - def test_get_unstructured_invalid_base64_character(self): - self._test_get_x(self._get_unst, - '=?utf-8?b?dm\x01k===?=', - 'vi', - 'vi', - [errors.InvalidBase64CharactersDefect], - '') - - def test_get_unstructured_invalid_base64_character_and_bad_padding(self): - self._test_get_x(self._get_unst, - '=?utf-8?b?dm\x01k?=', - 'vi', - 'vi', - [errors.InvalidBase64CharactersDefect, - errors.InvalidBase64PaddingDefect], - '') - - def test_get_unstructured_no_whitespace_between_ews(self): - self._test_get_x(self._get_unst, - '=?utf-8?q?foo?==?utf-8?q?bar?=', - 'foobar', - 'foobar', - [errors.InvalidHeaderDefect], - '') - - # get_qp_ctext - - def test_get_qp_ctext_only(self): - ptext = self._test_get_x(parser.get_qp_ctext, - 'foobar', 'foobar', ' ', [], '') - self.assertEqual(ptext.token_type, 'ptext') - - def test_get_qp_ctext_all_printables(self): - with_qp = self.rfc_printable_ascii.replace('\\', '\\\\') - with_qp = with_qp. replace('(', r'\(') - with_qp = with_qp.replace(')', r'\)') - ptext = self._test_get_x(parser.get_qp_ctext, - with_qp, self.rfc_printable_ascii, ' ', [], '') - - def test_get_qp_ctext_two_words_gets_first(self): - self._test_get_x(parser.get_qp_ctext, - 'foo de', 'foo', ' ', [], ' de') - - def test_get_qp_ctext_following_wsp_preserved(self): - self._test_get_x(parser.get_qp_ctext, - 'foo \t\tde', 'foo', ' ', [], ' \t\tde') - - def test_get_qp_ctext_up_to_close_paren_only(self): - self._test_get_x(parser.get_qp_ctext, - 'foo)', 'foo', ' ', [], ')') - - def test_get_qp_ctext_wsp_before_close_paren_preserved(self): - self._test_get_x(parser.get_qp_ctext, - 'foo )', 'foo', ' ', [], ' )') - - def test_get_qp_ctext_close_paren_mid_word(self): - self._test_get_x(parser.get_qp_ctext, - 'foo)bar', 'foo', ' ', [], ')bar') - - def test_get_qp_ctext_up_to_open_paren_only(self): - self._test_get_x(parser.get_qp_ctext, - 'foo(', 'foo', ' ', [], '(') - - def test_get_qp_ctext_wsp_before_open_paren_preserved(self): - self._test_get_x(parser.get_qp_ctext, - 'foo (', 'foo', ' ', [], ' (') - - def test_get_qp_ctext_open_paren_mid_word(self): - self._test_get_x(parser.get_qp_ctext, - 'foo(bar', 'foo', ' ', [], '(bar') - - def test_get_qp_ctext_non_printables(self): - ptext = self._test_get_x(parser.get_qp_ctext, - 'foo\x00bar)', 'foo\x00bar', ' ', - [errors.NonPrintableDefect], ')') - self.assertEqual(ptext.defects[0].non_printables[0], '\x00') - - # get_qcontent - - def test_get_qcontent_only(self): - ptext = self._test_get_x(parser.get_qcontent, - 'foobar', 'foobar', 'foobar', [], '') - self.assertEqual(ptext.token_type, 'ptext') - - def test_get_qcontent_all_printables(self): - with_qp = self.rfc_printable_ascii.replace('\\', '\\\\') - with_qp = with_qp. replace('"', r'\"') - ptext = self._test_get_x(parser.get_qcontent, with_qp, - self.rfc_printable_ascii, - self.rfc_printable_ascii, [], '') - - def test_get_qcontent_two_words_gets_first(self): - self._test_get_x(parser.get_qcontent, - 'foo de', 'foo', 'foo', [], ' de') - - def test_get_qcontent_following_wsp_preserved(self): - self._test_get_x(parser.get_qcontent, - 'foo \t\tde', 'foo', 'foo', [], ' \t\tde') - - def test_get_qcontent_up_to_dquote_only(self): - self._test_get_x(parser.get_qcontent, - 'foo"', 'foo', 'foo', [], '"') - - def test_get_qcontent_wsp_before_close_paren_preserved(self): - self._test_get_x(parser.get_qcontent, - 'foo "', 'foo', 'foo', [], ' "') - - def test_get_qcontent_close_paren_mid_word(self): - self._test_get_x(parser.get_qcontent, - 'foo"bar', 'foo', 'foo', [], '"bar') - - def test_get_qcontent_non_printables(self): - ptext = self._test_get_x(parser.get_qcontent, - 'foo\x00fg"', 'foo\x00fg', 'foo\x00fg', - [errors.NonPrintableDefect], '"') - self.assertEqual(ptext.defects[0].non_printables[0], '\x00') - - # get_atext - - def test_get_atext_only(self): - atext = self._test_get_x(parser.get_atext, - 'foobar', 'foobar', 'foobar', [], '') - self.assertEqual(atext.token_type, 'atext') - - def test_get_atext_all_atext(self): - atext = self._test_get_x(parser.get_atext, self.rfc_atext_chars, - self.rfc_atext_chars, - self.rfc_atext_chars, [], '') - - def test_get_atext_two_words_gets_first(self): - self._test_get_x(parser.get_atext, - 'foo bar', 'foo', 'foo', [], ' bar') - - def test_get_atext_following_wsp_preserved(self): - self._test_get_x(parser.get_atext, - 'foo \t\tbar', 'foo', 'foo', [], ' \t\tbar') - - def test_get_atext_up_to_special(self): - self._test_get_x(parser.get_atext, - 'foo@bar', 'foo', 'foo', [], '@bar') - - def test_get_atext_non_printables(self): - atext = self._test_get_x(parser.get_atext, - 'foo\x00bar(', 'foo\x00bar', 'foo\x00bar', - [errors.NonPrintableDefect], '(') - self.assertEqual(atext.defects[0].non_printables[0], '\x00') - - # get_bare_quoted_string - - def test_get_bare_quoted_string_only(self): - bqs = self._test_get_x(parser.get_bare_quoted_string, - '"foo"', '"foo"', 'foo', [], '') - self.assertEqual(bqs.token_type, 'bare-quoted-string') - - def test_get_bare_quoted_string_must_start_with_dquote(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_bare_quoted_string('foo"') - with self.assertRaises(errors.HeaderParseError): - parser.get_bare_quoted_string(' "foo"') - - def test_get_bare_quoted_string_following_wsp_preserved(self): - self._test_get_x(parser.get_bare_quoted_string, - '"foo"\t bar', '"foo"', 'foo', [], '\t bar') - - def test_get_bare_quoted_string_multiple_words(self): - self._test_get_x(parser.get_bare_quoted_string, - '"foo bar moo"', '"foo bar moo"', 'foo bar moo', [], '') - - def test_get_bare_quoted_string_multiple_words_wsp_preserved(self): - self._test_get_x(parser.get_bare_quoted_string, - '" foo moo\t"', '" foo moo\t"', ' foo moo\t', [], '') - - def test_get_bare_quoted_string_end_dquote_mid_word(self): - self._test_get_x(parser.get_bare_quoted_string, - '"foo"bar', '"foo"', 'foo', [], 'bar') - - def test_get_bare_quoted_string_quoted_dquote(self): - self._test_get_x(parser.get_bare_quoted_string, - r'"foo\"in"a', r'"foo\"in"', 'foo"in', [], 'a') - - def test_get_bare_quoted_string_non_printables(self): - self._test_get_x(parser.get_bare_quoted_string, - '"a\x01a"', '"a\x01a"', 'a\x01a', - [errors.NonPrintableDefect], '') - - def test_get_bare_quoted_string_no_end_dquote(self): - self._test_get_x(parser.get_bare_quoted_string, - '"foo', '"foo"', 'foo', - [errors.InvalidHeaderDefect], '') - self._test_get_x(parser.get_bare_quoted_string, - '"foo ', '"foo "', 'foo ', - [errors.InvalidHeaderDefect], '') - - def test_get_bare_quoted_string_empty_quotes(self): - self._test_get_x(parser.get_bare_quoted_string, - '""', '""', '', [], '') - - # get_comment - - def test_get_comment_only(self): - comment = self._test_get_x(parser.get_comment, - '(comment)', '(comment)', ' ', [], '', ['comment']) - self.assertEqual(comment.token_type, 'comment') - - def test_get_comment_must_start_with_paren(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_comment('foo"') - with self.assertRaises(errors.HeaderParseError): - parser.get_comment(' (foo"') - - def test_get_comment_following_wsp_preserved(self): - self._test_get_x(parser.get_comment, - '(comment) \t', '(comment)', ' ', [], ' \t', ['comment']) - - def test_get_comment_multiple_words(self): - self._test_get_x(parser.get_comment, - '(foo bar) \t', '(foo bar)', ' ', [], ' \t', ['foo bar']) - - def test_get_comment_multiple_words_wsp_preserved(self): - self._test_get_x(parser.get_comment, - '( foo bar\t ) \t', '( foo bar\t )', ' ', [], ' \t', - [' foo bar\t ']) - - def test_get_comment_end_paren_mid_word(self): - self._test_get_x(parser.get_comment, - '(foo)bar', '(foo)', ' ', [], 'bar', ['foo']) - - def test_get_comment_quoted_parens(self): - self._test_get_x(parser.get_comment, - '(foo\) \(\)bar)', '(foo\) \(\)bar)', ' ', [], '', ['foo) ()bar']) - - def test_get_comment_non_printable(self): - self._test_get_x(parser.get_comment, - '(foo\x7Fbar)', '(foo\x7Fbar)', ' ', - [errors.NonPrintableDefect], '', ['foo\x7Fbar']) - - def test_get_comment_no_end_paren(self): - self._test_get_x(parser.get_comment, - '(foo bar', '(foo bar)', ' ', - [errors.InvalidHeaderDefect], '', ['foo bar']) - self._test_get_x(parser.get_comment, - '(foo bar ', '(foo bar )', ' ', - [errors.InvalidHeaderDefect], '', ['foo bar ']) - - def test_get_comment_nested_comment(self): - comment = self._test_get_x(parser.get_comment, - '(foo(bar))', '(foo(bar))', ' ', [], '', ['foo(bar)']) - self.assertEqual(comment[1].content, 'bar') - - def test_get_comment_nested_comment_wsp(self): - comment = self._test_get_x(parser.get_comment, - '(foo ( bar ) )', '(foo ( bar ) )', ' ', [], '', ['foo ( bar ) ']) - self.assertEqual(comment[2].content, ' bar ') - - def test_get_comment_empty_comment(self): - self._test_get_x(parser.get_comment, - '()', '()', ' ', [], '', ['']) - - def test_get_comment_multiple_nesting(self): - comment = self._test_get_x(parser.get_comment, - '(((((foo)))))', '(((((foo)))))', ' ', [], '', ['((((foo))))']) - for i in range(4, 0, -1): - self.assertEqual(comment[0].content, '('*(i-1)+'foo'+')'*(i-1)) - comment = comment[0] - self.assertEqual(comment.content, 'foo') - - def test_get_comment_missing_end_of_nesting(self): - self._test_get_x(parser.get_comment, - '(((((foo)))', '(((((foo)))))', ' ', - [errors.InvalidHeaderDefect]*2, '', ['((((foo))))']) - - def test_get_comment_qs_in_nested_comment(self): - comment = self._test_get_x(parser.get_comment, - '(foo (b\)))', '(foo (b\)))', ' ', [], '', ['foo (b\))']) - self.assertEqual(comment[2].content, 'b)') - - # get_cfws - - def test_get_cfws_only_ws(self): - cfws = self._test_get_x(parser.get_cfws, - ' \t \t', ' \t \t', ' ', [], '', []) - self.assertEqual(cfws.token_type, 'cfws') - - def test_get_cfws_only_comment(self): - cfws = self._test_get_x(parser.get_cfws, - '(foo)', '(foo)', ' ', [], '', ['foo']) - self.assertEqual(cfws[0].content, 'foo') - - def test_get_cfws_only_mixed(self): - cfws = self._test_get_x(parser.get_cfws, - ' (foo ) ( bar) ', ' (foo ) ( bar) ', ' ', [], '', - ['foo ', ' bar']) - self.assertEqual(cfws[1].content, 'foo ') - self.assertEqual(cfws[3].content, ' bar') - - def test_get_cfws_ends_at_non_leader(self): - cfws = self._test_get_x(parser.get_cfws, - '(foo) bar', '(foo) ', ' ', [], 'bar', ['foo']) - self.assertEqual(cfws[0].content, 'foo') - - def test_get_cfws_ends_at_non_printable(self): - cfws = self._test_get_x(parser.get_cfws, - '(foo) \x07', '(foo) ', ' ', [], '\x07', ['foo']) - self.assertEqual(cfws[0].content, 'foo') - - def test_get_cfws_non_printable_in_comment(self): - cfws = self._test_get_x(parser.get_cfws, - '(foo \x07) "test"', '(foo \x07) ', ' ', - [errors.NonPrintableDefect], '"test"', ['foo \x07']) - self.assertEqual(cfws[0].content, 'foo \x07') - - def test_get_cfws_header_ends_in_comment(self): - cfws = self._test_get_x(parser.get_cfws, - ' (foo ', ' (foo )', ' ', - [errors.InvalidHeaderDefect], '', ['foo ']) - self.assertEqual(cfws[1].content, 'foo ') - - def test_get_cfws_multiple_nested_comments(self): - cfws = self._test_get_x(parser.get_cfws, - '(foo (bar)) ((a)(a))', '(foo (bar)) ((a)(a))', ' ', [], - '', ['foo (bar)', '(a)(a)']) - self.assertEqual(cfws[0].comments, ['foo (bar)']) - self.assertEqual(cfws[2].comments, ['(a)(a)']) - - # get_quoted_string - - def test_get_quoted_string_only(self): - qs = self._test_get_x(parser.get_quoted_string, - '"bob"', '"bob"', 'bob', [], '') - self.assertEqual(qs.token_type, 'quoted-string') - self.assertEqual(qs.quoted_value, '"bob"') - self.assertEqual(qs.content, 'bob') - - def test_get_quoted_string_with_wsp(self): - qs = self._test_get_x(parser.get_quoted_string, - '\t "bob" ', '\t "bob" ', ' bob ', [], '') - self.assertEqual(qs.quoted_value, ' "bob" ') - self.assertEqual(qs.content, 'bob') - - def test_get_quoted_string_with_comments_and_wsp(self): - qs = self._test_get_x(parser.get_quoted_string, - ' (foo) "bob"(bar)', ' (foo) "bob"(bar)', ' bob ', [], '') - self.assertEqual(qs[0][1].content, 'foo') - self.assertEqual(qs[2][0].content, 'bar') - self.assertEqual(qs.content, 'bob') - self.assertEqual(qs.quoted_value, ' "bob" ') - - def test_get_quoted_string_with_multiple_comments(self): - qs = self._test_get_x(parser.get_quoted_string, - ' (foo) (bar) "bob"(bird)', ' (foo) (bar) "bob"(bird)', ' bob ', - [], '') - self.assertEqual(qs[0].comments, ['foo', 'bar']) - self.assertEqual(qs[2].comments, ['bird']) - self.assertEqual(qs.content, 'bob') - self.assertEqual(qs.quoted_value, ' "bob" ') - - def test_get_quoted_string_non_printable_in_comment(self): - qs = self._test_get_x(parser.get_quoted_string, - ' (\x0A) "bob"', ' (\x0A) "bob"', ' bob', - [errors.NonPrintableDefect], '') - self.assertEqual(qs[0].comments, ['\x0A']) - self.assertEqual(qs.content, 'bob') - self.assertEqual(qs.quoted_value, ' "bob"') - - def test_get_quoted_string_non_printable_in_qcontent(self): - qs = self._test_get_x(parser.get_quoted_string, - ' (a) "a\x0B"', ' (a) "a\x0B"', ' a\x0B', - [errors.NonPrintableDefect], '') - self.assertEqual(qs[0].comments, ['a']) - self.assertEqual(qs.content, 'a\x0B') - self.assertEqual(qs.quoted_value, ' "a\x0B"') - - def test_get_quoted_string_internal_ws(self): - qs = self._test_get_x(parser.get_quoted_string, - ' (a) "foo bar "', ' (a) "foo bar "', ' foo bar ', - [], '') - self.assertEqual(qs[0].comments, ['a']) - self.assertEqual(qs.content, 'foo bar ') - self.assertEqual(qs.quoted_value, ' "foo bar "') - - def test_get_quoted_string_header_ends_in_comment(self): - qs = self._test_get_x(parser.get_quoted_string, - ' (a) "bob" (a', ' (a) "bob" (a)', ' bob ', - [errors.InvalidHeaderDefect], '') - self.assertEqual(qs[0].comments, ['a']) - self.assertEqual(qs[2].comments, ['a']) - self.assertEqual(qs.content, 'bob') - self.assertEqual(qs.quoted_value, ' "bob" ') - - def test_get_quoted_string_header_ends_in_qcontent(self): - qs = self._test_get_x(parser.get_quoted_string, - ' (a) "bob', ' (a) "bob"', ' bob', - [errors.InvalidHeaderDefect], '') - self.assertEqual(qs[0].comments, ['a']) - self.assertEqual(qs.content, 'bob') - self.assertEqual(qs.quoted_value, ' "bob"') - - def test_get_quoted_string_no_quoted_string(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_quoted_string(' (ab) xyz') - - def test_get_quoted_string_qs_ends_at_noncfws(self): - qs = self._test_get_x(parser.get_quoted_string, - '\t "bob" fee', '\t "bob" ', ' bob ', [], 'fee') - self.assertEqual(qs.content, 'bob') - self.assertEqual(qs.quoted_value, ' "bob" ') - - # get_atom - - def test_get_atom_only(self): - atom = self._test_get_x(parser.get_atom, - 'bob', 'bob', 'bob', [], '') - self.assertEqual(atom.token_type, 'atom') - - def test_get_atom_with_wsp(self): - self._test_get_x(parser.get_atom, - '\t bob ', '\t bob ', ' bob ', [], '') - - def test_get_atom_with_comments_and_wsp(self): - atom = self._test_get_x(parser.get_atom, - ' (foo) bob(bar)', ' (foo) bob(bar)', ' bob ', [], '') - self.assertEqual(atom[0][1].content, 'foo') - self.assertEqual(atom[2][0].content, 'bar') - - def test_get_atom_with_multiple_comments(self): - atom = self._test_get_x(parser.get_atom, - ' (foo) (bar) bob(bird)', ' (foo) (bar) bob(bird)', ' bob ', - [], '') - self.assertEqual(atom[0].comments, ['foo', 'bar']) - self.assertEqual(atom[2].comments, ['bird']) - - def test_get_atom_non_printable_in_comment(self): - atom = self._test_get_x(parser.get_atom, - ' (\x0A) bob', ' (\x0A) bob', ' bob', - [errors.NonPrintableDefect], '') - self.assertEqual(atom[0].comments, ['\x0A']) - - def test_get_atom_non_printable_in_atext(self): - atom = self._test_get_x(parser.get_atom, - ' (a) a\x0B', ' (a) a\x0B', ' a\x0B', - [errors.NonPrintableDefect], '') - self.assertEqual(atom[0].comments, ['a']) - - def test_get_atom_header_ends_in_comment(self): - atom = self._test_get_x(parser.get_atom, - ' (a) bob (a', ' (a) bob (a)', ' bob ', - [errors.InvalidHeaderDefect], '') - self.assertEqual(atom[0].comments, ['a']) - self.assertEqual(atom[2].comments, ['a']) - - def test_get_atom_no_atom(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_atom(' (ab) ') - - def test_get_atom_no_atom_before_special(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_atom(' (ab) @') - - def test_get_atom_atom_ends_at_special(self): - atom = self._test_get_x(parser.get_atom, - ' (foo) bob(bar) @bang', ' (foo) bob(bar) ', ' bob ', [], '@bang') - self.assertEqual(atom[0].comments, ['foo']) - self.assertEqual(atom[2].comments, ['bar']) - - def test_get_atom_atom_ends_at_noncfws(self): - atom = self._test_get_x(parser.get_atom, - 'bob fred', 'bob ', 'bob ', [], 'fred') - - # get_dot_atom_text - - def test_get_dot_atom_text(self): - dot_atom_text = self._test_get_x(parser.get_dot_atom_text, - 'foo.bar.bang', 'foo.bar.bang', 'foo.bar.bang', [], '') - self.assertEqual(dot_atom_text.token_type, 'dot-atom-text') - self.assertEqual(len(dot_atom_text), 5) - - def test_get_dot_atom_text_lone_atom_is_valid(self): - dot_atom_text = self._test_get_x(parser.get_dot_atom_text, - 'foo', 'foo', 'foo', [], '') - - def test_get_dot_atom_text_raises_on_leading_dot(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_dot_atom_text('.foo.bar') - - def test_get_dot_atom_text_raises_on_trailing_dot(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_dot_atom_text('foo.bar.') - - def test_get_dot_atom_text_raises_on_leading_non_atext(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_dot_atom_text(' foo.bar') - with self.assertRaises(errors.HeaderParseError): - parser.get_dot_atom_text('@foo.bar') - with self.assertRaises(errors.HeaderParseError): - parser.get_dot_atom_text('"foo.bar"') - - def test_get_dot_atom_text_trailing_text_preserved(self): - dot_atom_text = self._test_get_x(parser.get_dot_atom_text, - 'foo@bar', 'foo', 'foo', [], '@bar') - - def test_get_dot_atom_text_trailing_ws_preserved(self): - dot_atom_text = self._test_get_x(parser.get_dot_atom_text, - 'foo .bar', 'foo', 'foo', [], ' .bar') - - # get_dot_atom - - def test_get_dot_atom_only(self): - dot_atom = self._test_get_x(parser.get_dot_atom, - 'foo.bar.bing', 'foo.bar.bing', 'foo.bar.bing', [], '') - self.assertEqual(dot_atom.token_type, 'dot-atom') - self.assertEqual(len(dot_atom), 1) - - def test_get_dot_atom_with_wsp(self): - self._test_get_x(parser.get_dot_atom, - '\t foo.bar.bing ', '\t foo.bar.bing ', ' foo.bar.bing ', [], '') - - def test_get_dot_atom_with_comments_and_wsp(self): - self._test_get_x(parser.get_dot_atom, - ' (sing) foo.bar.bing (here) ', ' (sing) foo.bar.bing (here) ', - ' foo.bar.bing ', [], '') - - def test_get_dot_atom_space_ends_dot_atom(self): - self._test_get_x(parser.get_dot_atom, - ' (sing) foo.bar .bing (here) ', ' (sing) foo.bar ', - ' foo.bar ', [], '.bing (here) ') - - def test_get_dot_atom_no_atom_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_dot_atom(' (foo) ') - - def test_get_dot_atom_leading_dot_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_dot_atom(' (foo) .bar') - - def test_get_dot_atom_two_dots_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_dot_atom('bar..bang') - - def test_get_dot_atom_trailing_dot_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_dot_atom(' (foo) bar.bang. foo') - - # get_word (if this were black box we'd repeat all the qs/atom tests) - - def test_get_word_atom_yields_atom(self): - word = self._test_get_x(parser.get_word, - ' (foo) bar (bang) :ah', ' (foo) bar (bang) ', ' bar ', [], ':ah') - self.assertEqual(word.token_type, 'atom') - self.assertEqual(word[0].token_type, 'cfws') - - def test_get_word_qs_yields_qs(self): - word = self._test_get_x(parser.get_word, - '"bar " (bang) ah', '"bar " (bang) ', 'bar ', [], 'ah') - self.assertEqual(word.token_type, 'quoted-string') - self.assertEqual(word[0].token_type, 'bare-quoted-string') - self.assertEqual(word[0].value, 'bar ') - self.assertEqual(word.content, 'bar ') - - def test_get_word_ends_at_dot(self): - self._test_get_x(parser.get_word, - 'foo.', 'foo', 'foo', [], '.') - - # get_phrase - - def test_get_phrase_simple(self): - phrase = self._test_get_x(parser.get_phrase, - '"Fred A. Johnson" is his name, oh.', - '"Fred A. Johnson" is his name', - 'Fred A. Johnson is his name', - [], - ', oh.') - self.assertEqual(phrase.token_type, 'phrase') - - def test_get_phrase_complex(self): - phrase = self._test_get_x(parser.get_phrase, - ' (A) bird (in (my|your)) "hand " is messy\t<>\t', - ' (A) bird (in (my|your)) "hand " is messy\t', - ' bird hand is messy ', - [], - '<>\t') - self.assertEqual(phrase[0][0].comments, ['A']) - self.assertEqual(phrase[0][2].comments, ['in (my|your)']) - - def test_get_phrase_obsolete(self): - phrase = self._test_get_x(parser.get_phrase, - 'Fred A.(weird).O Johnson', - 'Fred A.(weird).O Johnson', - 'Fred A. .O Johnson', - [errors.ObsoleteHeaderDefect]*3, - '') - self.assertEqual(len(phrase), 7) - self.assertEqual(phrase[3].comments, ['weird']) - - def test_get_phrase_pharse_must_start_with_word(self): - phrase = self._test_get_x(parser.get_phrase, - '(even weirder).name', - '(even weirder).name', - ' .name', - [errors.InvalidHeaderDefect] + [errors.ObsoleteHeaderDefect]*2, - '') - self.assertEqual(len(phrase), 3) - self.assertEqual(phrase[0].comments, ['even weirder']) - - def test_get_phrase_ending_with_obsolete(self): - phrase = self._test_get_x(parser.get_phrase, - 'simple phrase.(with trailing comment):boo', - 'simple phrase.(with trailing comment)', - 'simple phrase. ', - [errors.ObsoleteHeaderDefect]*2, - ':boo') - self.assertEqual(len(phrase), 4) - self.assertEqual(phrase[3].comments, ['with trailing comment']) - - def get_phrase_cfws_only_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_phrase(' (foo) ') - - # get_local_part - - def test_get_local_part_simple(self): - local_part = self._test_get_x(parser.get_local_part, - 'dinsdale@python.org', 'dinsdale', 'dinsdale', [], '@python.org') - self.assertEqual(local_part.token_type, 'local-part') - self.assertEqual(local_part.local_part, 'dinsdale') - - def test_get_local_part_with_dot(self): - local_part = self._test_get_x(parser.get_local_part, - 'Fred.A.Johnson@python.org', - 'Fred.A.Johnson', - 'Fred.A.Johnson', - [], - '@python.org') - self.assertEqual(local_part.local_part, 'Fred.A.Johnson') - - def test_get_local_part_with_whitespace(self): - local_part = self._test_get_x(parser.get_local_part, - ' Fred.A.Johnson @python.org', - ' Fred.A.Johnson ', - ' Fred.A.Johnson ', - [], - '@python.org') - self.assertEqual(local_part.local_part, 'Fred.A.Johnson') - - def test_get_local_part_with_cfws(self): - local_part = self._test_get_x(parser.get_local_part, - ' (foo) Fred.A.Johnson (bar (bird)) @python.org', - ' (foo) Fred.A.Johnson (bar (bird)) ', - ' Fred.A.Johnson ', - [], - '@python.org') - self.assertEqual(local_part.local_part, 'Fred.A.Johnson') - self.assertEqual(local_part[0][0].comments, ['foo']) - self.assertEqual(local_part[0][2].comments, ['bar (bird)']) - - def test_get_local_part_simple_quoted(self): - local_part = self._test_get_x(parser.get_local_part, - '"dinsdale"@python.org', '"dinsdale"', '"dinsdale"', [], '@python.org') - self.assertEqual(local_part.token_type, 'local-part') - self.assertEqual(local_part.local_part, 'dinsdale') - - def test_get_local_part_with_quoted_dot(self): - local_part = self._test_get_x(parser.get_local_part, - '"Fred.A.Johnson"@python.org', - '"Fred.A.Johnson"', - '"Fred.A.Johnson"', - [], - '@python.org') - self.assertEqual(local_part.local_part, 'Fred.A.Johnson') - - def test_get_local_part_quoted_with_whitespace(self): - local_part = self._test_get_x(parser.get_local_part, - ' "Fred A. Johnson" @python.org', - ' "Fred A. Johnson" ', - ' "Fred A. Johnson" ', - [], - '@python.org') - self.assertEqual(local_part.local_part, 'Fred A. Johnson') - - def test_get_local_part_quoted_with_cfws(self): - local_part = self._test_get_x(parser.get_local_part, - ' (foo) " Fred A. Johnson " (bar (bird)) @python.org', - ' (foo) " Fred A. Johnson " (bar (bird)) ', - ' " Fred A. Johnson " ', - [], - '@python.org') - self.assertEqual(local_part.local_part, ' Fred A. Johnson ') - self.assertEqual(local_part[0][0].comments, ['foo']) - self.assertEqual(local_part[0][2].comments, ['bar (bird)']) - - - def test_get_local_part_simple_obsolete(self): - local_part = self._test_get_x(parser.get_local_part, - 'Fred. A.Johnson@python.org', - 'Fred. A.Johnson', - 'Fred. A.Johnson', - [errors.ObsoleteHeaderDefect], - '@python.org') - self.assertEqual(local_part.local_part, 'Fred.A.Johnson') - - def test_get_local_part_complex_obsolete_1(self): - local_part = self._test_get_x(parser.get_local_part, - ' (foo )Fred (bar).(bird) A.(sheep)Johnson."and dogs "@python.org', - ' (foo )Fred (bar).(bird) A.(sheep)Johnson."and dogs "', - ' Fred . A. Johnson.and dogs ', - [errors.ObsoleteHeaderDefect], - '@python.org') - self.assertEqual(local_part.local_part, 'Fred.A.Johnson.and dogs ') - - def test_get_local_part_complex_obsolete_invalid(self): - local_part = self._test_get_x(parser.get_local_part, - ' (foo )Fred (bar).(bird) A.(sheep)Johnson "and dogs"@python.org', - ' (foo )Fred (bar).(bird) A.(sheep)Johnson "and dogs"', - ' Fred . A. Johnson and dogs', - [errors.InvalidHeaderDefect]*2, - '@python.org') - self.assertEqual(local_part.local_part, 'Fred.A.Johnson and dogs') - - def test_get_local_part_no_part_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_local_part(' (foo) ') - - def test_get_local_part_special_instead_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_local_part(' (foo) @python.org') - - def test_get_local_part_trailing_dot(self): - local_part = self._test_get_x(parser.get_local_part, - ' borris.@python.org', - ' borris.', - ' borris.', - [errors.InvalidHeaderDefect]*2, - '@python.org') - self.assertEqual(local_part.local_part, 'borris.') - - def test_get_local_part_trailing_dot_with_ws(self): - local_part = self._test_get_x(parser.get_local_part, - ' borris. @python.org', - ' borris. ', - ' borris. ', - [errors.InvalidHeaderDefect]*2, - '@python.org') - self.assertEqual(local_part.local_part, 'borris.') - - def test_get_local_part_leading_dot(self): - local_part = self._test_get_x(parser.get_local_part, - '.borris@python.org', - '.borris', - '.borris', - [errors.InvalidHeaderDefect]*2, - '@python.org') - self.assertEqual(local_part.local_part, '.borris') - - def test_get_local_part_leading_dot_after_ws(self): - local_part = self._test_get_x(parser.get_local_part, - ' .borris@python.org', - ' .borris', - ' .borris', - [errors.InvalidHeaderDefect]*2, - '@python.org') - self.assertEqual(local_part.local_part, '.borris') - - def test_get_local_part_double_dot_raises(self): - local_part = self._test_get_x(parser.get_local_part, - ' borris.(foo).natasha@python.org', - ' borris.(foo).natasha', - ' borris. .natasha', - [errors.InvalidHeaderDefect]*2, - '@python.org') - self.assertEqual(local_part.local_part, 'borris..natasha') - - def test_get_local_part_quoted_strings_in_atom_list(self): - local_part = self._test_get_x(parser.get_local_part, - '""example" example"@example.com', - '""example" example"', - 'example example', - [errors.InvalidHeaderDefect]*3, - '@example.com') - self.assertEqual(local_part.local_part, 'example example') - - def test_get_local_part_valid_and_invalid_qp_in_atom_list(self): - local_part = self._test_get_x(parser.get_local_part, - r'"\\"example\\" example"@example.com', - r'"\\"example\\" example"', - r'\example\\ example', - [errors.InvalidHeaderDefect]*5, - '@example.com') - self.assertEqual(local_part.local_part, r'\example\\ example') - - def test_get_local_part_unicode_defect(self): - # Currently this only happens when parsing unicode, not when parsing - # stuff that was originally binary. - local_part = self._test_get_x(parser.get_local_part, - 'exámple@example.com', - 'exámple', - 'exámple', - [errors.NonASCIILocalPartDefect], - '@example.com') - self.assertEqual(local_part.local_part, 'exámple') - - # get_dtext - - def test_get_dtext_only(self): - dtext = self._test_get_x(parser.get_dtext, - 'foobar', 'foobar', 'foobar', [], '') - self.assertEqual(dtext.token_type, 'ptext') - - def test_get_dtext_all_dtext(self): - dtext = self._test_get_x(parser.get_dtext, self.rfc_dtext_chars, - self.rfc_dtext_chars, - self.rfc_dtext_chars, [], '') - - def test_get_dtext_two_words_gets_first(self): - self._test_get_x(parser.get_dtext, - 'foo bar', 'foo', 'foo', [], ' bar') - - def test_get_dtext_following_wsp_preserved(self): - self._test_get_x(parser.get_dtext, - 'foo \t\tbar', 'foo', 'foo', [], ' \t\tbar') - - def test_get_dtext_non_printables(self): - dtext = self._test_get_x(parser.get_dtext, - 'foo\x00bar]', 'foo\x00bar', 'foo\x00bar', - [errors.NonPrintableDefect], ']') - self.assertEqual(dtext.defects[0].non_printables[0], '\x00') - - def test_get_dtext_with_qp(self): - ptext = self._test_get_x(parser.get_dtext, - r'foo\]\[\\bar\b\e\l\l', - r'foo][\barbell', - r'foo][\barbell', - [errors.ObsoleteHeaderDefect], - '') - - def test_get_dtext_up_to_close_bracket_only(self): - self._test_get_x(parser.get_dtext, - 'foo]', 'foo', 'foo', [], ']') - - def test_get_dtext_wsp_before_close_bracket_preserved(self): - self._test_get_x(parser.get_dtext, - 'foo ]', 'foo', 'foo', [], ' ]') - - def test_get_dtext_close_bracket_mid_word(self): - self._test_get_x(parser.get_dtext, - 'foo]bar', 'foo', 'foo', [], ']bar') - - def test_get_dtext_up_to_open_bracket_only(self): - self._test_get_x(parser.get_dtext, - 'foo[', 'foo', 'foo', [], '[') - - def test_get_dtext_wsp_before_open_bracket_preserved(self): - self._test_get_x(parser.get_dtext, - 'foo [', 'foo', 'foo', [], ' [') - - def test_get_dtext_open_bracket_mid_word(self): - self._test_get_x(parser.get_dtext, - 'foo[bar', 'foo', 'foo', [], '[bar') - - # get_domain_literal - - def test_get_domain_literal_only(self): - domain_literal = domain_literal = self._test_get_x(parser.get_domain_literal, - '[127.0.0.1]', - '[127.0.0.1]', - '[127.0.0.1]', - [], - '') - self.assertEqual(domain_literal.token_type, 'domain-literal') - self.assertEqual(domain_literal.domain, '[127.0.0.1]') - self.assertEqual(domain_literal.ip, '127.0.0.1') - - def test_get_domain_literal_with_internal_ws(self): - domain_literal = self._test_get_x(parser.get_domain_literal, - '[ 127.0.0.1\t ]', - '[ 127.0.0.1\t ]', - '[ 127.0.0.1 ]', - [], - '') - self.assertEqual(domain_literal.domain, '[127.0.0.1]') - self.assertEqual(domain_literal.ip, '127.0.0.1') - - def test_get_domain_literal_with_surrounding_cfws(self): - domain_literal = self._test_get_x(parser.get_domain_literal, - '(foo)[ 127.0.0.1] (bar)', - '(foo)[ 127.0.0.1] (bar)', - ' [ 127.0.0.1] ', - [], - '') - self.assertEqual(domain_literal.domain, '[127.0.0.1]') - self.assertEqual(domain_literal.ip, '127.0.0.1') - - def test_get_domain_literal_no_start_char_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_domain_literal('(foo) ') - - def test_get_domain_literal_no_start_char_before_special_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_domain_literal('(foo) @') - - def test_get_domain_literal_bad_dtext_char_before_special_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_domain_literal('(foo) [abc[@') - - # get_domain - - def test_get_domain_regular_domain_only(self): - domain = self._test_get_x(parser.get_domain, - 'example.com', - 'example.com', - 'example.com', - [], - '') - self.assertEqual(domain.token_type, 'domain') - self.assertEqual(domain.domain, 'example.com') - - def test_get_domain_domain_literal_only(self): - domain = self._test_get_x(parser.get_domain, - '[127.0.0.1]', - '[127.0.0.1]', - '[127.0.0.1]', - [], - '') - self.assertEqual(domain.token_type, 'domain') - self.assertEqual(domain.domain, '[127.0.0.1]') - - def test_get_domain_with_cfws(self): - domain = self._test_get_x(parser.get_domain, - '(foo) example.com(bar)\t', - '(foo) example.com(bar)\t', - ' example.com ', - [], - '') - self.assertEqual(domain.domain, 'example.com') - - def test_get_domain_domain_literal_with_cfws(self): - domain = self._test_get_x(parser.get_domain, - '(foo)[127.0.0.1]\t(bar)', - '(foo)[127.0.0.1]\t(bar)', - ' [127.0.0.1] ', - [], - '') - self.assertEqual(domain.domain, '[127.0.0.1]') - - def test_get_domain_domain_with_cfws_ends_at_special(self): - domain = self._test_get_x(parser.get_domain, - '(foo)example.com\t(bar), next', - '(foo)example.com\t(bar)', - ' example.com ', - [], - ', next') - self.assertEqual(domain.domain, 'example.com') - - def test_get_domain_domain_literal_with_cfws_ends_at_special(self): - domain = self._test_get_x(parser.get_domain, - '(foo)[127.0.0.1]\t(bar), next', - '(foo)[127.0.0.1]\t(bar)', - ' [127.0.0.1] ', - [], - ', next') - self.assertEqual(domain.domain, '[127.0.0.1]') - - def test_get_domain_obsolete(self): - domain = self._test_get_x(parser.get_domain, - '(foo) example . (bird)com(bar)\t', - '(foo) example . (bird)com(bar)\t', - ' example . com ', - [errors.ObsoleteHeaderDefect], - '') - self.assertEqual(domain.domain, 'example.com') - - def test_get_domain_no_non_cfws_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_domain(" (foo)\t") - - def test_get_domain_no_atom_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_domain(" (foo)\t, broken") - - - # get_addr_spec - - def test_get_addr_spec_normal(self): - addr_spec = self._test_get_x(parser.get_addr_spec, - 'dinsdale@example.com', - 'dinsdale@example.com', - 'dinsdale@example.com', - [], - '') - self.assertEqual(addr_spec.token_type, 'addr-spec') - self.assertEqual(addr_spec.local_part, 'dinsdale') - self.assertEqual(addr_spec.domain, 'example.com') - self.assertEqual(addr_spec.addr_spec, 'dinsdale@example.com') - - def test_get_addr_spec_with_doamin_literal(self): - addr_spec = self._test_get_x(parser.get_addr_spec, - 'dinsdale@[127.0.0.1]', - 'dinsdale@[127.0.0.1]', - 'dinsdale@[127.0.0.1]', - [], - '') - self.assertEqual(addr_spec.local_part, 'dinsdale') - self.assertEqual(addr_spec.domain, '[127.0.0.1]') - self.assertEqual(addr_spec.addr_spec, 'dinsdale@[127.0.0.1]') - - def test_get_addr_spec_with_cfws(self): - addr_spec = self._test_get_x(parser.get_addr_spec, - '(foo) dinsdale(bar)@ (bird) example.com (bog)', - '(foo) dinsdale(bar)@ (bird) example.com (bog)', - ' dinsdale@example.com ', - [], - '') - self.assertEqual(addr_spec.local_part, 'dinsdale') - self.assertEqual(addr_spec.domain, 'example.com') - self.assertEqual(addr_spec.addr_spec, 'dinsdale@example.com') - - def test_get_addr_spec_with_qouoted_string_and_cfws(self): - addr_spec = self._test_get_x(parser.get_addr_spec, - '(foo) "roy a bug"(bar)@ (bird) example.com (bog)', - '(foo) "roy a bug"(bar)@ (bird) example.com (bog)', - ' "roy a bug"@example.com ', - [], - '') - self.assertEqual(addr_spec.local_part, 'roy a bug') - self.assertEqual(addr_spec.domain, 'example.com') - self.assertEqual(addr_spec.addr_spec, '"roy a bug"@example.com') - - def test_get_addr_spec_ends_at_special(self): - addr_spec = self._test_get_x(parser.get_addr_spec, - '(foo) "roy a bug"(bar)@ (bird) example.com (bog) , next', - '(foo) "roy a bug"(bar)@ (bird) example.com (bog) ', - ' "roy a bug"@example.com ', - [], - ', next') - self.assertEqual(addr_spec.local_part, 'roy a bug') - self.assertEqual(addr_spec.domain, 'example.com') - self.assertEqual(addr_spec.addr_spec, '"roy a bug"@example.com') - - def test_get_addr_spec_quoted_strings_in_atom_list(self): - addr_spec = self._test_get_x(parser.get_addr_spec, - '""example" example"@example.com', - '""example" example"@example.com', - 'example example@example.com', - [errors.InvalidHeaderDefect]*3, - '') - self.assertEqual(addr_spec.local_part, 'example example') - self.assertEqual(addr_spec.domain, 'example.com') - self.assertEqual(addr_spec.addr_spec, '"example example"@example.com') - - def test_get_addr_spec_dot_atom(self): - addr_spec = self._test_get_x(parser.get_addr_spec, - 'star.a.star@example.com', - 'star.a.star@example.com', - 'star.a.star@example.com', - [], - '') - self.assertEqual(addr_spec.local_part, 'star.a.star') - self.assertEqual(addr_spec.domain, 'example.com') - self.assertEqual(addr_spec.addr_spec, 'star.a.star@example.com') - - # get_obs_route - - def test_get_obs_route_simple(self): - obs_route = self._test_get_x(parser.get_obs_route, - '@example.com, @two.example.com:', - '@example.com, @two.example.com:', - '@example.com, @two.example.com:', - [], - '') - self.assertEqual(obs_route.token_type, 'obs-route') - self.assertEqual(obs_route.domains, ['example.com', 'two.example.com']) - - def test_get_obs_route_complex(self): - obs_route = self._test_get_x(parser.get_obs_route, - '(foo),, (blue)@example.com (bar),@two.(foo) example.com (bird):', - '(foo),, (blue)@example.com (bar),@two.(foo) example.com (bird):', - ' ,, @example.com ,@two. example.com :', - [errors.ObsoleteHeaderDefect], # This is the obs-domain - '') - self.assertEqual(obs_route.token_type, 'obs-route') - self.assertEqual(obs_route.domains, ['example.com', 'two.example.com']) - - def test_get_obs_route_no_route_before_end_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_obs_route('(foo) @example.com,') - - def test_get_obs_route_no_route_before_special_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_obs_route('(foo) [abc],') - - def test_get_obs_route_no_route_before_special_raises2(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_obs_route('(foo) @example.com [abc],') - - # get_angle_addr - - def test_get_angle_addr_simple(self): - angle_addr = self._test_get_x(parser.get_angle_addr, - '', - '', - '', - [], - '') - self.assertEqual(angle_addr.token_type, 'angle-addr') - self.assertEqual(angle_addr.local_part, 'dinsdale') - self.assertEqual(angle_addr.domain, 'example.com') - self.assertIsNone(angle_addr.route) - self.assertEqual(angle_addr.addr_spec, 'dinsdale@example.com') - - def test_get_angle_addr_empty(self): - angle_addr = self._test_get_x(parser.get_angle_addr, - '<>', - '<>', - '<>', - [errors.InvalidHeaderDefect], - '') - self.assertEqual(angle_addr.token_type, 'angle-addr') - self.assertIsNone(angle_addr.local_part) - self.assertIsNone(angle_addr.domain) - self.assertIsNone(angle_addr.route) - self.assertEqual(angle_addr.addr_spec, '<>') - - def test_get_angle_addr_with_cfws(self): - angle_addr = self._test_get_x(parser.get_angle_addr, - ' (foo) (bar)', - ' (foo) (bar)', - ' ', - [], - '') - self.assertEqual(angle_addr.token_type, 'angle-addr') - self.assertEqual(angle_addr.local_part, 'dinsdale') - self.assertEqual(angle_addr.domain, 'example.com') - self.assertIsNone(angle_addr.route) - self.assertEqual(angle_addr.addr_spec, 'dinsdale@example.com') - - def test_get_angle_addr_qs_and_domain_literal(self): - angle_addr = self._test_get_x(parser.get_angle_addr, - '<"Fred Perfect"@[127.0.0.1]>', - '<"Fred Perfect"@[127.0.0.1]>', - '<"Fred Perfect"@[127.0.0.1]>', - [], - '') - self.assertEqual(angle_addr.local_part, 'Fred Perfect') - self.assertEqual(angle_addr.domain, '[127.0.0.1]') - self.assertIsNone(angle_addr.route) - self.assertEqual(angle_addr.addr_spec, '"Fred Perfect"@[127.0.0.1]') - - def test_get_angle_addr_internal_cfws(self): - angle_addr = self._test_get_x(parser.get_angle_addr, - '<(foo) dinsdale@example.com(bar)>', - '<(foo) dinsdale@example.com(bar)>', - '< dinsdale@example.com >', - [], - '') - self.assertEqual(angle_addr.local_part, 'dinsdale') - self.assertEqual(angle_addr.domain, 'example.com') - self.assertIsNone(angle_addr.route) - self.assertEqual(angle_addr.addr_spec, 'dinsdale@example.com') - - def test_get_angle_addr_obs_route(self): - angle_addr = self._test_get_x(parser.get_angle_addr, - '(foo)<@example.com, (bird) @two.example.com: dinsdale@example.com> (bar) ', - '(foo)<@example.com, (bird) @two.example.com: dinsdale@example.com> (bar) ', - ' <@example.com, @two.example.com: dinsdale@example.com> ', - [errors.ObsoleteHeaderDefect], - '') - self.assertEqual(angle_addr.local_part, 'dinsdale') - self.assertEqual(angle_addr.domain, 'example.com') - self.assertEqual(angle_addr.route, ['example.com', 'two.example.com']) - self.assertEqual(angle_addr.addr_spec, 'dinsdale@example.com') - - def test_get_angle_addr_missing_closing_angle(self): - angle_addr = self._test_get_x(parser.get_angle_addr, - '', - '', - [errors.InvalidHeaderDefect], - '') - self.assertEqual(angle_addr.local_part, 'dinsdale') - self.assertEqual(angle_addr.domain, 'example.com') - self.assertIsNone(angle_addr.route) - self.assertEqual(angle_addr.addr_spec, 'dinsdale@example.com') - - def test_get_angle_addr_missing_closing_angle_with_cfws(self): - angle_addr = self._test_get_x(parser.get_angle_addr, - '', - '', - [errors.InvalidHeaderDefect], - '') - self.assertEqual(angle_addr.local_part, 'dinsdale') - self.assertEqual(angle_addr.domain, 'example.com') - self.assertIsNone(angle_addr.route) - self.assertEqual(angle_addr.addr_spec, 'dinsdale@example.com') - - def test_get_angle_addr_ends_at_special(self): - angle_addr = self._test_get_x(parser.get_angle_addr, - ' (foo), next', - ' (foo)', - ' ', - [], - ', next') - self.assertEqual(angle_addr.local_part, 'dinsdale') - self.assertEqual(angle_addr.domain, 'example.com') - self.assertIsNone(angle_addr.route) - self.assertEqual(angle_addr.addr_spec, 'dinsdale@example.com') - - def test_get_angle_addr_no_angle_raise(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_angle_addr('(foo) ') - - def test_get_angle_addr_no_angle_before_special_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_angle_addr('(foo) , next') - - def test_get_angle_addr_no_angle_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_angle_addr('bar') - - def test_get_angle_addr_special_after_angle_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_angle_addr('(foo) <, bar') - - # get_display_name This is phrase but with a different value. - - def test_get_display_name_simple(self): - display_name = self._test_get_x(parser.get_display_name, - 'Fred A Johnson', - 'Fred A Johnson', - 'Fred A Johnson', - [], - '') - self.assertEqual(display_name.token_type, 'display-name') - self.assertEqual(display_name.display_name, 'Fred A Johnson') - - def test_get_display_name_complex1(self): - display_name = self._test_get_x(parser.get_display_name, - '"Fred A. Johnson" is his name, oh.', - '"Fred A. Johnson" is his name', - '"Fred A. Johnson is his name"', - [], - ', oh.') - self.assertEqual(display_name.token_type, 'display-name') - self.assertEqual(display_name.display_name, 'Fred A. Johnson is his name') - - def test_get_display_name_complex2(self): - display_name = self._test_get_x(parser.get_display_name, - ' (A) bird (in (my|your)) "hand " is messy\t<>\t', - ' (A) bird (in (my|your)) "hand " is messy\t', - ' "bird hand is messy" ', - [], - '<>\t') - self.assertEqual(display_name[0][0].comments, ['A']) - self.assertEqual(display_name[0][2].comments, ['in (my|your)']) - self.assertEqual(display_name.display_name, 'bird hand is messy') - - def test_get_display_name_obsolete(self): - display_name = self._test_get_x(parser.get_display_name, - 'Fred A.(weird).O Johnson', - 'Fred A.(weird).O Johnson', - '"Fred A. .O Johnson"', - [errors.ObsoleteHeaderDefect]*3, - '') - self.assertEqual(len(display_name), 7) - self.assertEqual(display_name[3].comments, ['weird']) - self.assertEqual(display_name.display_name, 'Fred A. .O Johnson') - - def test_get_display_name_pharse_must_start_with_word(self): - display_name = self._test_get_x(parser.get_display_name, - '(even weirder).name', - '(even weirder).name', - ' ".name"', - [errors.InvalidHeaderDefect] + [errors.ObsoleteHeaderDefect]*2, - '') - self.assertEqual(len(display_name), 3) - self.assertEqual(display_name[0].comments, ['even weirder']) - self.assertEqual(display_name.display_name, '.name') - - def test_get_display_name_ending_with_obsolete(self): - display_name = self._test_get_x(parser.get_display_name, - 'simple phrase.(with trailing comment):boo', - 'simple phrase.(with trailing comment)', - '"simple phrase." ', - [errors.ObsoleteHeaderDefect]*2, - ':boo') - self.assertEqual(len(display_name), 4) - self.assertEqual(display_name[3].comments, ['with trailing comment']) - self.assertEqual(display_name.display_name, 'simple phrase.') - - # get_name_addr - - def test_get_name_addr_angle_addr_only(self): - name_addr = self._test_get_x(parser.get_name_addr, - '', - '', - '', - [], - '') - self.assertEqual(name_addr.token_type, 'name-addr') - self.assertIsNone(name_addr.display_name) - self.assertEqual(name_addr.local_part, 'dinsdale') - self.assertEqual(name_addr.domain, 'example.com') - self.assertIsNone(name_addr.route) - self.assertEqual(name_addr.addr_spec, 'dinsdale@example.com') - - def test_get_name_addr_atom_name(self): - name_addr = self._test_get_x(parser.get_name_addr, - 'Dinsdale ', - 'Dinsdale ', - 'Dinsdale ', - [], - '') - self.assertEqual(name_addr.token_type, 'name-addr') - self.assertEqual(name_addr.display_name, 'Dinsdale') - self.assertEqual(name_addr.local_part, 'dinsdale') - self.assertEqual(name_addr.domain, 'example.com') - self.assertIsNone(name_addr.route) - self.assertEqual(name_addr.addr_spec, 'dinsdale@example.com') - - def test_get_name_addr_atom_name_with_cfws(self): - name_addr = self._test_get_x(parser.get_name_addr, - '(foo) Dinsdale (bar) (bird)', - '(foo) Dinsdale (bar) (bird)', - ' Dinsdale ', - [], - '') - self.assertEqual(name_addr.display_name, 'Dinsdale') - self.assertEqual(name_addr.local_part, 'dinsdale') - self.assertEqual(name_addr.domain, 'example.com') - self.assertIsNone(name_addr.route) - self.assertEqual(name_addr.addr_spec, 'dinsdale@example.com') - - def test_get_name_addr_name_with_cfws_and_dots(self): - name_addr = self._test_get_x(parser.get_name_addr, - '(foo) Roy.A.Bear (bar) (bird)', - '(foo) Roy.A.Bear (bar) (bird)', - ' "Roy.A.Bear" ', - [errors.ObsoleteHeaderDefect]*2, - '') - self.assertEqual(name_addr.display_name, 'Roy.A.Bear') - self.assertEqual(name_addr.local_part, 'dinsdale') - self.assertEqual(name_addr.domain, 'example.com') - self.assertIsNone(name_addr.route) - self.assertEqual(name_addr.addr_spec, 'dinsdale@example.com') - - def test_get_name_addr_qs_name(self): - name_addr = self._test_get_x(parser.get_name_addr, - '"Roy.A.Bear" ', - '"Roy.A.Bear" ', - '"Roy.A.Bear" ', - [], - '') - self.assertEqual(name_addr.display_name, 'Roy.A.Bear') - self.assertEqual(name_addr.local_part, 'dinsdale') - self.assertEqual(name_addr.domain, 'example.com') - self.assertIsNone(name_addr.route) - self.assertEqual(name_addr.addr_spec, 'dinsdale@example.com') - - def test_get_name_addr_with_route(self): - name_addr = self._test_get_x(parser.get_name_addr, - '"Roy.A.Bear" <@two.example.com: dinsdale@example.com>', - '"Roy.A.Bear" <@two.example.com: dinsdale@example.com>', - '"Roy.A.Bear" <@two.example.com: dinsdale@example.com>', - [errors.ObsoleteHeaderDefect], - '') - self.assertEqual(name_addr.display_name, 'Roy.A.Bear') - self.assertEqual(name_addr.local_part, 'dinsdale') - self.assertEqual(name_addr.domain, 'example.com') - self.assertEqual(name_addr.route, ['two.example.com']) - self.assertEqual(name_addr.addr_spec, 'dinsdale@example.com') - - def test_get_name_addr_ends_at_special(self): - name_addr = self._test_get_x(parser.get_name_addr, - '"Roy.A.Bear" , next', - '"Roy.A.Bear" ', - '"Roy.A.Bear" ', - [], - ', next') - self.assertEqual(name_addr.display_name, 'Roy.A.Bear') - self.assertEqual(name_addr.local_part, 'dinsdale') - self.assertEqual(name_addr.domain, 'example.com') - self.assertIsNone(name_addr.route) - self.assertEqual(name_addr.addr_spec, 'dinsdale@example.com') - - def test_get_name_addr_no_content_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_name_addr(' (foo) ') - - def test_get_name_addr_no_content_before_special_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_name_addr(' (foo) ,') - - def test_get_name_addr_no_angle_after_display_name_raises(self): - with self.assertRaises(errors.HeaderParseError): - parser.get_name_addr('foo bar') - - # get_mailbox - - def test_get_mailbox_addr_spec_only(self): - mailbox = self._test_get_x(parser.get_mailbox, - 'dinsdale@example.com', - 'dinsdale@example.com', - 'dinsdale@example.com', - [], - '') - self.assertEqual(mailbox.token_type, 'mailbox') - self.assertIsNone(mailbox.display_name) - self.assertEqual(mailbox.local_part, 'dinsdale') - self.assertEqual(mailbox.domain, 'example.com') - self.assertIsNone(mailbox.route) - self.assertEqual(mailbox.addr_spec, 'dinsdale@example.com') - - def test_get_mailbox_angle_addr_only(self): - mailbox = self._test_get_x(parser.get_mailbox, - '', - '', - '', - [], - '') - self.assertEqual(mailbox.token_type, 'mailbox') - self.assertIsNone(mailbox.display_name) - self.assertEqual(mailbox.local_part, 'dinsdale') - self.assertEqual(mailbox.domain, 'example.com') - self.assertIsNone(mailbox.route) - self.assertEqual(mailbox.addr_spec, 'dinsdale@example.com') - - def test_get_mailbox_name_addr(self): - mailbox = self._test_get_x(parser.get_mailbox, - '"Roy A. Bear" ', - '"Roy A. Bear" ', - '"Roy A. Bear" ', - [], - '') - self.assertEqual(mailbox.token_type, 'mailbox') - self.assertEqual(mailbox.display_name, 'Roy A. Bear') - self.assertEqual(mailbox.local_part, 'dinsdale') - self.assertEqual(mailbox.domain, 'example.com') - self.assertIsNone(mailbox.route) - self.assertEqual(mailbox.addr_spec, 'dinsdale@example.com') - - def test_get_mailbox_ends_at_special(self): - mailbox = self._test_get_x(parser.get_mailbox, - '"Roy A. Bear" , rest', - '"Roy A. Bear" ', - '"Roy A. Bear" ', - [], - ', rest') - self.assertEqual(mailbox.token_type, 'mailbox') - self.assertEqual(mailbox.display_name, 'Roy A. Bear') - self.assertEqual(mailbox.local_part, 'dinsdale') - self.assertEqual(mailbox.domain, 'example.com') - self.assertIsNone(mailbox.route) - self.assertEqual(mailbox.addr_spec, 'dinsdale@example.com') - - def test_get_mailbox_quoted_strings_in_atom_list(self): - mailbox = self._test_get_x(parser.get_mailbox, - '""example" example"@example.com', - '""example" example"@example.com', - 'example example@example.com', - [errors.InvalidHeaderDefect]*3, - '') - self.assertEqual(mailbox.local_part, 'example example') - self.assertEqual(mailbox.domain, 'example.com') - self.assertEqual(mailbox.addr_spec, '"example example"@example.com') - - # get_mailbox_list - - def test_get_mailbox_list_single_addr(self): - mailbox_list = self._test_get_x(parser.get_mailbox_list, - 'dinsdale@example.com', - 'dinsdale@example.com', - 'dinsdale@example.com', - [], - '') - self.assertEqual(mailbox_list.token_type, 'mailbox-list') - self.assertEqual(len(mailbox_list.mailboxes), 1) - mailbox = mailbox_list.mailboxes[0] - self.assertIsNone(mailbox.display_name) - self.assertEqual(mailbox.local_part, 'dinsdale') - self.assertEqual(mailbox.domain, 'example.com') - self.assertIsNone(mailbox.route) - self.assertEqual(mailbox.addr_spec, 'dinsdale@example.com') - self.assertEqual(mailbox_list.mailboxes, - mailbox_list.all_mailboxes) - - def test_get_mailbox_list_two_simple_addr(self): - mailbox_list = self._test_get_x(parser.get_mailbox_list, - 'dinsdale@example.com, dinsdale@test.example.com', - 'dinsdale@example.com, dinsdale@test.example.com', - 'dinsdale@example.com, dinsdale@test.example.com', - [], - '') - self.assertEqual(mailbox_list.token_type, 'mailbox-list') - self.assertEqual(len(mailbox_list.mailboxes), 2) - self.assertEqual(mailbox_list.mailboxes[0].addr_spec, - 'dinsdale@example.com') - self.assertEqual(mailbox_list.mailboxes[1].addr_spec, - 'dinsdale@test.example.com') - self.assertEqual(mailbox_list.mailboxes, - mailbox_list.all_mailboxes) - - def test_get_mailbox_list_two_name_addr(self): - mailbox_list = self._test_get_x(parser.get_mailbox_list, - ('"Roy A. Bear" ,' - ' "Fred Flintstone" '), - ('"Roy A. Bear" ,' - ' "Fred Flintstone" '), - ('"Roy A. Bear" ,' - ' "Fred Flintstone" '), - [], - '') - self.assertEqual(len(mailbox_list.mailboxes), 2) - self.assertEqual(mailbox_list.mailboxes[0].addr_spec, - 'dinsdale@example.com') - self.assertEqual(mailbox_list.mailboxes[0].display_name, - 'Roy A. Bear') - self.assertEqual(mailbox_list.mailboxes[1].addr_spec, - 'dinsdale@test.example.com') - self.assertEqual(mailbox_list.mailboxes[1].display_name, - 'Fred Flintstone') - self.assertEqual(mailbox_list.mailboxes, - mailbox_list.all_mailboxes) - - def test_get_mailbox_list_two_complex(self): - mailbox_list = self._test_get_x(parser.get_mailbox_list, - ('(foo) "Roy A. Bear" (bar),' - ' "Fred Flintstone" '), - ('(foo) "Roy A. Bear" (bar),' - ' "Fred Flintstone" '), - (' "Roy A. Bear" ,' - ' "Fred Flintstone" '), - [errors.ObsoleteHeaderDefect], - '') - self.assertEqual(len(mailbox_list.mailboxes), 2) - self.assertEqual(mailbox_list.mailboxes[0].addr_spec, - 'dinsdale@example.com') - self.assertEqual(mailbox_list.mailboxes[0].display_name, - 'Roy A. Bear') - self.assertEqual(mailbox_list.mailboxes[1].addr_spec, - 'dinsdale@test.example.com') - self.assertEqual(mailbox_list.mailboxes[1].display_name, - 'Fred Flintstone') - self.assertEqual(mailbox_list.mailboxes, - mailbox_list.all_mailboxes) - - def test_get_mailbox_list_unparseable_mailbox_null(self): - mailbox_list = self._test_get_x(parser.get_mailbox_list, - ('"Roy A. Bear"[] dinsdale@example.com,' - ' "Fred Flintstone" '), - ('"Roy A. Bear"[] dinsdale@example.com,' - ' "Fred Flintstone" '), - ('"Roy A. Bear"[] dinsdale@example.com,' - ' "Fred Flintstone" '), - [errors.InvalidHeaderDefect, # the 'extra' text after the local part - errors.InvalidHeaderDefect, # the local part with no angle-addr - errors.ObsoleteHeaderDefect, # period in extra text (example.com) - errors.ObsoleteHeaderDefect], # (bird) in valid address. - '') - self.assertEqual(len(mailbox_list.mailboxes), 1) - self.assertEqual(len(mailbox_list.all_mailboxes), 2) - self.assertEqual(mailbox_list.all_mailboxes[0].token_type, - 'invalid-mailbox') - self.assertIsNone(mailbox_list.all_mailboxes[0].display_name) - self.assertEqual(mailbox_list.all_mailboxes[0].local_part, - 'Roy A. Bear') - self.assertIsNone(mailbox_list.all_mailboxes[0].domain) - self.assertEqual(mailbox_list.all_mailboxes[0].addr_spec, - '"Roy A. Bear"') - self.assertIs(mailbox_list.all_mailboxes[1], - mailbox_list.mailboxes[0]) - self.assertEqual(mailbox_list.mailboxes[0].addr_spec, - 'dinsdale@test.example.com') - self.assertEqual(mailbox_list.mailboxes[0].display_name, - 'Fred Flintstone') - - def test_get_mailbox_list_junk_after_valid_address(self): - mailbox_list = self._test_get_x(parser.get_mailbox_list, - ('"Roy A. Bear" @@,' - ' "Fred Flintstone" '), - ('"Roy A. Bear" @@,' - ' "Fred Flintstone" '), - ('"Roy A. Bear" @@,' - ' "Fred Flintstone" '), - [errors.InvalidHeaderDefect], - '') - self.assertEqual(len(mailbox_list.mailboxes), 1) - self.assertEqual(len(mailbox_list.all_mailboxes), 2) - self.assertEqual(mailbox_list.all_mailboxes[0].addr_spec, - 'dinsdale@example.com') - self.assertEqual(mailbox_list.all_mailboxes[0].display_name, - 'Roy A. Bear') - self.assertEqual(mailbox_list.all_mailboxes[0].token_type, - 'invalid-mailbox') - self.assertIs(mailbox_list.all_mailboxes[1], - mailbox_list.mailboxes[0]) - self.assertEqual(mailbox_list.mailboxes[0].addr_spec, - 'dinsdale@test.example.com') - self.assertEqual(mailbox_list.mailboxes[0].display_name, - 'Fred Flintstone') - - def test_get_mailbox_list_empty_list_element(self): - mailbox_list = self._test_get_x(parser.get_mailbox_list, - ('"Roy A. Bear" , (bird),,' - ' "Fred Flintstone" '), - ('"Roy A. Bear" , (bird),,' - ' "Fred Flintstone" '), - ('"Roy A. Bear" , ,,' - ' "Fred Flintstone" '), - [errors.ObsoleteHeaderDefect]*2, - '') - self.assertEqual(len(mailbox_list.mailboxes), 2) - self.assertEqual(mailbox_list.all_mailboxes, - mailbox_list.mailboxes) - self.assertEqual(mailbox_list.all_mailboxes[0].addr_spec, - 'dinsdale@example.com') - self.assertEqual(mailbox_list.all_mailboxes[0].display_name, - 'Roy A. Bear') - self.assertEqual(mailbox_list.mailboxes[1].addr_spec, - 'dinsdale@test.example.com') - self.assertEqual(mailbox_list.mailboxes[1].display_name, - 'Fred Flintstone') - - def test_get_mailbox_list_only_empty_elements(self): - mailbox_list = self._test_get_x(parser.get_mailbox_list, - '(foo),, (bar)', - '(foo),, (bar)', - ' ,, ', - [errors.ObsoleteHeaderDefect]*3, - '') - self.assertEqual(len(mailbox_list.mailboxes), 0) - self.assertEqual(mailbox_list.all_mailboxes, - mailbox_list.mailboxes) - - # get_group_list - - def test_get_group_list_cfws_only(self): - group_list = self._test_get_x(parser.get_group_list, - '(hidden);', - '(hidden)', - ' ', - [], - ';') - self.assertEqual(group_list.token_type, 'group-list') - self.assertEqual(len(group_list.mailboxes), 0) - self.assertEqual(group_list.mailboxes, - group_list.all_mailboxes) - - def test_get_group_list_mailbox_list(self): - group_list = self._test_get_x(parser.get_group_list, - 'dinsdale@example.org, "Fred A. Bear" ', - 'dinsdale@example.org, "Fred A. Bear" ', - 'dinsdale@example.org, "Fred A. Bear" ', - [], - '') - self.assertEqual(group_list.token_type, 'group-list') - self.assertEqual(len(group_list.mailboxes), 2) - self.assertEqual(group_list.mailboxes, - group_list.all_mailboxes) - self.assertEqual(group_list.mailboxes[1].display_name, - 'Fred A. Bear') - - def test_get_group_list_obs_group_list(self): - group_list = self._test_get_x(parser.get_group_list, - ', (foo),,(bar)', - ', (foo),,(bar)', - ', ,, ', - [errors.ObsoleteHeaderDefect], - '') - self.assertEqual(group_list.token_type, 'group-list') - self.assertEqual(len(group_list.mailboxes), 0) - self.assertEqual(group_list.mailboxes, - group_list.all_mailboxes) - - def test_get_group_list_comment_only_invalid(self): - group_list = self._test_get_x(parser.get_group_list, - '(bar)', - '(bar)', - ' ', - [errors.InvalidHeaderDefect], - '') - self.assertEqual(group_list.token_type, 'group-list') - self.assertEqual(len(group_list.mailboxes), 0) - self.assertEqual(group_list.mailboxes, - group_list.all_mailboxes) - - # get_group - - def test_get_group_empty(self): - group = self._test_get_x(parser.get_group, - 'Monty Python:;', - 'Monty Python:;', - 'Monty Python:;', - [], - '') - self.assertEqual(group.token_type, 'group') - self.assertEqual(group.display_name, 'Monty Python') - self.assertEqual(len(group.mailboxes), 0) - self.assertEqual(group.mailboxes, - group.all_mailboxes) - - def test_get_group_null_addr_spec(self): - group = self._test_get_x(parser.get_group, - 'foo: <>;', - 'foo: <>;', - 'foo: <>;', - [errors.InvalidHeaderDefect], - '') - self.assertEqual(group.display_name, 'foo') - self.assertEqual(len(group.mailboxes), 0) - self.assertEqual(len(group.all_mailboxes), 1) - self.assertEqual(group.all_mailboxes[0].value, '<>') - - def test_get_group_cfws_only(self): - group = self._test_get_x(parser.get_group, - 'Monty Python: (hidden);', - 'Monty Python: (hidden);', - 'Monty Python: ;', - [], - '') - self.assertEqual(group.token_type, 'group') - self.assertEqual(group.display_name, 'Monty Python') - self.assertEqual(len(group.mailboxes), 0) - self.assertEqual(group.mailboxes, - group.all_mailboxes) - - def test_get_group_single_mailbox(self): - group = self._test_get_x(parser.get_group, - 'Monty Python: "Fred A. Bear" ;', - 'Monty Python: "Fred A. Bear" ;', - 'Monty Python: "Fred A. Bear" ;', - [], - '') - self.assertEqual(group.token_type, 'group') - self.assertEqual(group.display_name, 'Monty Python') - self.assertEqual(len(group.mailboxes), 1) - self.assertEqual(group.mailboxes, - group.all_mailboxes) - self.assertEqual(group.mailboxes[0].addr_spec, - 'dinsdale@example.com') - - def test_get_group_mixed_list(self): - group = self._test_get_x(parser.get_group, - ('Monty Python: "Fred A. Bear" ,' - '(foo) Roger , x@test.example.com;'), - ('Monty Python: "Fred A. Bear" ,' - '(foo) Roger , x@test.example.com;'), - ('Monty Python: "Fred A. Bear" ,' - ' Roger , x@test.example.com;'), - [], - '') - self.assertEqual(group.token_type, 'group') - self.assertEqual(group.display_name, 'Monty Python') - self.assertEqual(len(group.mailboxes), 3) - self.assertEqual(group.mailboxes, - group.all_mailboxes) - self.assertEqual(group.mailboxes[0].display_name, - 'Fred A. Bear') - self.assertEqual(group.mailboxes[1].display_name, - 'Roger') - self.assertEqual(group.mailboxes[2].local_part, 'x') - - def test_get_group_one_invalid(self): - group = self._test_get_x(parser.get_group, - ('Monty Python: "Fred A. Bear" ,' - '(foo) Roger ping@exampele.com, x@test.example.com;'), - ('Monty Python: "Fred A. Bear" ,' - '(foo) Roger ping@exampele.com, x@test.example.com;'), - ('Monty Python: "Fred A. Bear" ,' - ' Roger ping@exampele.com, x@test.example.com;'), - [errors.InvalidHeaderDefect, # non-angle addr makes local part invalid - errors.InvalidHeaderDefect], # and its not obs-local either: no dots. - '') - self.assertEqual(group.token_type, 'group') - self.assertEqual(group.display_name, 'Monty Python') - self.assertEqual(len(group.mailboxes), 2) - self.assertEqual(len(group.all_mailboxes), 3) - self.assertEqual(group.mailboxes[0].display_name, - 'Fred A. Bear') - self.assertEqual(group.mailboxes[1].local_part, 'x') - self.assertIsNone(group.all_mailboxes[1].display_name) - - # get_address - - def test_get_address_simple(self): - address = self._test_get_x(parser.get_address, - 'dinsdale@example.com', - 'dinsdale@example.com', - 'dinsdale@example.com', - [], - '') - self.assertEqual(address.token_type, 'address') - self.assertEqual(len(address.mailboxes), 1) - self.assertEqual(address.mailboxes, - address.all_mailboxes) - self.assertEqual(address.mailboxes[0].domain, - 'example.com') - self.assertEqual(address[0].token_type, - 'mailbox') - - def test_get_address_complex(self): - address = self._test_get_x(parser.get_address, - '(foo) "Fred A. Bear" <(bird)dinsdale@example.com>', - '(foo) "Fred A. Bear" <(bird)dinsdale@example.com>', - ' "Fred A. Bear" < dinsdale@example.com>', - [], - '') - self.assertEqual(address.token_type, 'address') - self.assertEqual(len(address.mailboxes), 1) - self.assertEqual(address.mailboxes, - address.all_mailboxes) - self.assertEqual(address.mailboxes[0].display_name, - 'Fred A. Bear') - self.assertEqual(address[0].token_type, - 'mailbox') - - def test_get_address_empty_group(self): - address = self._test_get_x(parser.get_address, - 'Monty Python:;', - 'Monty Python:;', - 'Monty Python:;', - [], - '') - self.assertEqual(address.token_type, 'address') - self.assertEqual(len(address.mailboxes), 0) - self.assertEqual(address.mailboxes, - address.all_mailboxes) - self.assertEqual(address[0].token_type, - 'group') - self.assertEqual(address[0].display_name, - 'Monty Python') - - def test_get_address_group(self): - address = self._test_get_x(parser.get_address, - 'Monty Python: x@example.com, y@example.com;', - 'Monty Python: x@example.com, y@example.com;', - 'Monty Python: x@example.com, y@example.com;', - [], - '') - self.assertEqual(address.token_type, 'address') - self.assertEqual(len(address.mailboxes), 2) - self.assertEqual(address.mailboxes, - address.all_mailboxes) - self.assertEqual(address[0].token_type, - 'group') - self.assertEqual(address[0].display_name, - 'Monty Python') - self.assertEqual(address.mailboxes[0].local_part, 'x') - - def test_get_address_quoted_local_part(self): - address = self._test_get_x(parser.get_address, - '"foo bar"@example.com', - '"foo bar"@example.com', - '"foo bar"@example.com', - [], - '') - self.assertEqual(address.token_type, 'address') - self.assertEqual(len(address.mailboxes), 1) - self.assertEqual(address.mailboxes, - address.all_mailboxes) - self.assertEqual(address.mailboxes[0].domain, - 'example.com') - self.assertEqual(address.mailboxes[0].local_part, - 'foo bar') - self.assertEqual(address[0].token_type, 'mailbox') - - def test_get_address_ends_at_special(self): - address = self._test_get_x(parser.get_address, - 'dinsdale@example.com, next', - 'dinsdale@example.com', - 'dinsdale@example.com', - [], - ', next') - self.assertEqual(address.token_type, 'address') - self.assertEqual(len(address.mailboxes), 1) - self.assertEqual(address.mailboxes, - address.all_mailboxes) - self.assertEqual(address.mailboxes[0].domain, - 'example.com') - self.assertEqual(address[0].token_type, 'mailbox') - - def test_get_address_invalid_mailbox_invalid(self): - address = self._test_get_x(parser.get_address, - 'ping example.com, next', - 'ping example.com', - 'ping example.com', - [errors.InvalidHeaderDefect, # addr-spec with no domain - errors.InvalidHeaderDefect, # invalid local-part - errors.InvalidHeaderDefect, # missing .s in local-part - ], - ', next') - self.assertEqual(address.token_type, 'address') - self.assertEqual(len(address.mailboxes), 0) - self.assertEqual(len(address.all_mailboxes), 1) - self.assertIsNone(address.all_mailboxes[0].domain) - self.assertEqual(address.all_mailboxes[0].local_part, 'ping example.com') - self.assertEqual(address[0].token_type, 'invalid-mailbox') - - def test_get_address_quoted_strings_in_atom_list(self): - address = self._test_get_x(parser.get_address, - '""example" example"@example.com', - '""example" example"@example.com', - 'example example@example.com', - [errors.InvalidHeaderDefect]*3, - '') - self.assertEqual(address.all_mailboxes[0].local_part, 'example example') - self.assertEqual(address.all_mailboxes[0].domain, 'example.com') - self.assertEqual(address.all_mailboxes[0].addr_spec, '"example example"@example.com') - - - # get_address_list - - def test_get_address_list_mailboxes_simple(self): - address_list = self._test_get_x(parser.get_address_list, - 'dinsdale@example.com', - 'dinsdale@example.com', - 'dinsdale@example.com', - [], - '') - self.assertEqual(address_list.token_type, 'address-list') - self.assertEqual(len(address_list.mailboxes), 1) - self.assertEqual(address_list.mailboxes, - address_list.all_mailboxes) - self.assertEqual([str(x) for x in address_list.mailboxes], - [str(x) for x in address_list.addresses]) - self.assertEqual(address_list.mailboxes[0].domain, 'example.com') - self.assertEqual(address_list[0].token_type, 'address') - self.assertIsNone(address_list[0].display_name) - - def test_get_address_list_mailboxes_two_simple(self): - address_list = self._test_get_x(parser.get_address_list, - 'foo@example.com, "Fred A. Bar" ', - 'foo@example.com, "Fred A. Bar" ', - 'foo@example.com, "Fred A. Bar" ', - [], - '') - self.assertEqual(address_list.token_type, 'address-list') - self.assertEqual(len(address_list.mailboxes), 2) - self.assertEqual(address_list.mailboxes, - address_list.all_mailboxes) - self.assertEqual([str(x) for x in address_list.mailboxes], - [str(x) for x in address_list.addresses]) - self.assertEqual(address_list.mailboxes[0].local_part, 'foo') - self.assertEqual(address_list.mailboxes[1].display_name, "Fred A. Bar") - - def test_get_address_list_mailboxes_complex(self): - address_list = self._test_get_x(parser.get_address_list, - ('"Roy A. Bear" , ' - '(ping) Foo ,' - 'Nobody Is. Special '), - ('"Roy A. Bear" , ' - '(ping) Foo ,' - 'Nobody Is. Special '), - ('"Roy A. Bear" , ' - 'Foo ,' - '"Nobody Is. Special" '), - [errors.ObsoleteHeaderDefect, # period in Is. - errors.ObsoleteHeaderDefect], # cfws in domain - '') - self.assertEqual(address_list.token_type, 'address-list') - self.assertEqual(len(address_list.mailboxes), 3) - self.assertEqual(address_list.mailboxes, - address_list.all_mailboxes) - self.assertEqual([str(x) for x in address_list.mailboxes], - [str(x) for x in address_list.addresses]) - self.assertEqual(address_list.mailboxes[0].domain, 'example.com') - self.assertEqual(address_list.mailboxes[0].token_type, 'mailbox') - self.assertEqual(address_list.addresses[0].token_type, 'address') - self.assertEqual(address_list.mailboxes[1].local_part, 'x') - self.assertEqual(address_list.mailboxes[2].display_name, - 'Nobody Is. Special') - - def test_get_address_list_mailboxes_invalid_addresses(self): - address_list = self._test_get_x(parser.get_address_list, - ('"Roy A. Bear" , ' - '(ping) Foo x@example.com[],' - 'Nobody Is. Special <(bird)example.(bad)com>'), - ('"Roy A. Bear" , ' - '(ping) Foo x@example.com[],' - 'Nobody Is. Special <(bird)example.(bad)com>'), - ('"Roy A. Bear" , ' - 'Foo x@example.com[],' - '"Nobody Is. Special" < example. com>'), - [errors.InvalidHeaderDefect, # invalid address in list - errors.InvalidHeaderDefect, # 'Foo x' local part invalid. - errors.InvalidHeaderDefect, # Missing . in 'Foo x' local part - errors.ObsoleteHeaderDefect, # period in 'Is.' disp-name phrase - errors.InvalidHeaderDefect, # no domain part in addr-spec - errors.ObsoleteHeaderDefect], # addr-spec has comment in it - '') - self.assertEqual(address_list.token_type, 'address-list') - self.assertEqual(len(address_list.mailboxes), 1) - self.assertEqual(len(address_list.all_mailboxes), 3) - self.assertEqual([str(x) for x in address_list.all_mailboxes], - [str(x) for x in address_list.addresses]) - self.assertEqual(address_list.mailboxes[0].domain, 'example.com') - self.assertEqual(address_list.mailboxes[0].token_type, 'mailbox') - self.assertEqual(address_list.addresses[0].token_type, 'address') - self.assertEqual(address_list.addresses[1].token_type, 'address') - self.assertEqual(len(address_list.addresses[0].mailboxes), 1) - self.assertEqual(len(address_list.addresses[1].mailboxes), 0) - self.assertEqual(len(address_list.addresses[1].mailboxes), 0) - self.assertEqual( - address_list.addresses[1].all_mailboxes[0].local_part, 'Foo x') - self.assertEqual( - address_list.addresses[2].all_mailboxes[0].display_name, - "Nobody Is. Special") - - def test_get_address_list_group_empty(self): - address_list = self._test_get_x(parser.get_address_list, - 'Monty Python: ;', - 'Monty Python: ;', - 'Monty Python: ;', - [], - '') - self.assertEqual(address_list.token_type, 'address-list') - self.assertEqual(len(address_list.mailboxes), 0) - self.assertEqual(address_list.mailboxes, - address_list.all_mailboxes) - self.assertEqual(len(address_list.addresses), 1) - self.assertEqual(address_list.addresses[0].token_type, 'address') - self.assertEqual(address_list.addresses[0].display_name, 'Monty Python') - self.assertEqual(len(address_list.addresses[0].mailboxes), 0) - - def test_get_address_list_group_simple(self): - address_list = self._test_get_x(parser.get_address_list, - 'Monty Python: dinsdale@example.com;', - 'Monty Python: dinsdale@example.com;', - 'Monty Python: dinsdale@example.com;', - [], - '') - self.assertEqual(address_list.token_type, 'address-list') - self.assertEqual(len(address_list.mailboxes), 1) - self.assertEqual(address_list.mailboxes, - address_list.all_mailboxes) - self.assertEqual(address_list.mailboxes[0].domain, 'example.com') - self.assertEqual(address_list.addresses[0].display_name, - 'Monty Python') - self.assertEqual(address_list.addresses[0].mailboxes[0].domain, - 'example.com') - - def test_get_address_list_group_and_mailboxes(self): - address_list = self._test_get_x(parser.get_address_list, - ('Monty Python: dinsdale@example.com, "Fred" ;, ' - 'Abe , Bee '), - ('Monty Python: dinsdale@example.com, "Fred" ;, ' - 'Abe , Bee '), - ('Monty Python: dinsdale@example.com, "Fred" ;, ' - 'Abe , Bee '), - [], - '') - self.assertEqual(address_list.token_type, 'address-list') - self.assertEqual(len(address_list.mailboxes), 4) - self.assertEqual(address_list.mailboxes, - address_list.all_mailboxes) - self.assertEqual(len(address_list.addresses), 3) - self.assertEqual(address_list.mailboxes[0].local_part, 'dinsdale') - self.assertEqual(address_list.addresses[0].display_name, - 'Monty Python') - self.assertEqual(address_list.addresses[0].mailboxes[0].domain, - 'example.com') - self.assertEqual(address_list.addresses[0].mailboxes[1].local_part, - 'flint') - self.assertEqual(address_list.addresses[1].mailboxes[0].local_part, - 'x') - self.assertEqual(address_list.addresses[2].mailboxes[0].local_part, - 'y') - self.assertEqual(str(address_list.addresses[1]), - str(address_list.mailboxes[2])) - - -class TestFolding(TestEmailBase): - - policy = policy.default - - def _test(self, tl, folded, policy=policy): - self.assertEqual(tl.fold(policy=policy), folded, tl.ppstr()) - - def test_simple_unstructured_no_folds(self): - self._test(parser.get_unstructured("This is a test"), - "This is a test\n") - - def test_simple_unstructured_folded(self): - self._test(parser.get_unstructured("This is also a test, but this " - "time there are enough words (and even some " - "symbols) to make it wrap; at least in theory."), - "This is also a test, but this time there are enough " - "words (and even some\n" - " symbols) to make it wrap; at least in theory.\n") - - def test_unstructured_with_unicode_no_folds(self): - self._test(parser.get_unstructured("hübsch kleiner beißt"), - "=?utf-8?q?h=C3=BCbsch_kleiner_bei=C3=9Ft?=\n") - - def test_one_ew_on_each_of_two_wrapped_lines(self): - self._test(parser.get_unstructured("Mein kleiner Kaktus ist sehr " - "hübsch. Es hat viele Stacheln " - "und oft beißt mich."), - "Mein kleiner Kaktus ist sehr =?utf-8?q?h=C3=BCbsch=2E?= " - "Es hat viele Stacheln\n" - " und oft =?utf-8?q?bei=C3=9Ft?= mich.\n") - - def test_ews_combined_before_wrap(self): - self._test(parser.get_unstructured("Mein Kaktus ist hübsch. " - "Es beißt mich. " - "And that's all I'm sayin."), - "Mein Kaktus ist =?utf-8?q?h=C3=BCbsch=2E__Es_bei=C3=9Ft?= " - "mich. And that's\n" - " all I'm sayin.\n") - - # XXX Need test of an encoded word so long that it needs to be wrapped - - def test_simple_address(self): - self._test(parser.get_address_list("abc ")[0], - "abc \n") - - def test_address_list_folding_at_commas(self): - self._test(parser.get_address_list('abc , ' - '"Fred Blunt" , ' - '"J.P.Cool" , ' - '"K<>y" , ' - 'Firesale , ' - '')[0], - 'abc , "Fred Blunt" ,\n' - ' "J.P.Cool" , "K<>y" ,\n' - ' Firesale , \n') - - def test_address_list_with_unicode_names(self): - self._test(parser.get_address_list( - 'Hübsch Kaktus , ' - 'beißt beißt ')[0], - '=?utf-8?q?H=C3=BCbsch?= Kaktus ,\n' - ' =?utf-8?q?bei=C3=9Ft_bei=C3=9Ft?= \n') - - def test_address_list_with_unicode_names_in_quotes(self): - self._test(parser.get_address_list( - '"Hübsch Kaktus" , ' - '"beißt" beißt ')[0], - '=?utf-8?q?H=C3=BCbsch?= Kaktus ,\n' - ' =?utf-8?q?bei=C3=9Ft_bei=C3=9Ft?= \n') - - # XXX Need tests with comments on various sides of a unicode token, - # and with unicode tokens in the comments. Spaces inside the quotes - # currently don't do the right thing. - - def test_initial_whitespace_splitting(self): - body = parser.get_unstructured(' ' + 'x'*77) - header = parser.Header([ - parser.HeaderLabel([parser.ValueTerminal('test:', 'atext')]), - parser.CFWSList([parser.WhiteSpaceTerminal(' ', 'fws')]), body]) - self._test(header, 'test: \n ' + 'x'*77 + '\n') - - def test_whitespace_splitting(self): - self._test(parser.get_unstructured('xxx ' + 'y'*77), - 'xxx \n ' + 'y'*77 + '\n') - -if __name__ == '__main__': - unittest.main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/test_asian_codecs.py --- a/Lib/test/test_email/test_asian_codecs.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_email/test_asian_codecs.py Mon May 21 23:01:17 2012 -0400 @@ -41,7 +41,7 @@ Hello World! =?iso-2022-jp?b?GyRCJU8lbSE8JW8hPCVrJUkhKhsoQg==?= =?iso-8859-1?q?Gr=FC=DF_Gott!?=""") eq(decode_header(h.encode()), - [(b'Hello World! ', None), + [(b'Hello World!', None), (b'\x1b$B%O%m!<%o!<%k%I!*\x1b(B', 'iso-2022-jp'), (b'Gr\xfc\xdf Gott!', gcode)]) subject_bytes = (b'test-ja \xa4\xd8\xc5\xea\xb9\xc6\xa4\xb5' diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/test_defect_handling.py --- a/Lib/test/test_email/test_defect_handling.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,320 +0,0 @@ -import textwrap -import unittest -import contextlib -from email import policy -from email import errors -from test.test_email import TestEmailBase - - -class TestDefectsBase: - - policy = policy.default - raise_expected = False - - @contextlib.contextmanager - def _raise_point(self, defect): - yield - - def test_same_boundary_inner_outer(self): - source = textwrap.dedent("""\ - Subject: XX - From: xx@xx.dk - To: XX - Mime-version: 1.0 - Content-type: multipart/mixed; - boundary="MS_Mac_OE_3071477847_720252_MIME_Part" - - --MS_Mac_OE_3071477847_720252_MIME_Part - Content-type: multipart/alternative; - boundary="MS_Mac_OE_3071477847_720252_MIME_Part" - - --MS_Mac_OE_3071477847_720252_MIME_Part - Content-type: text/plain; charset="ISO-8859-1" - Content-transfer-encoding: quoted-printable - - text - - --MS_Mac_OE_3071477847_720252_MIME_Part - Content-type: text/html; charset="ISO-8859-1" - Content-transfer-encoding: quoted-printable - - - - --MS_Mac_OE_3071477847_720252_MIME_Part-- - - --MS_Mac_OE_3071477847_720252_MIME_Part - Content-type: image/gif; name="xx.gif"; - Content-disposition: attachment - Content-transfer-encoding: base64 - - Some removed base64 encoded chars. - - --MS_Mac_OE_3071477847_720252_MIME_Part-- - - """) - # XXX better would be to actually detect the duplicate. - with self._raise_point(errors.StartBoundaryNotFoundDefect): - msg = self._str_msg(source) - if self.raise_expected: return - inner = msg.get_payload(0) - self.assertTrue(hasattr(inner, 'defects')) - self.assertEqual(len(self.get_defects(inner)), 1) - self.assertTrue(isinstance(self.get_defects(inner)[0], - errors.StartBoundaryNotFoundDefect)) - - def test_multipart_no_boundary(self): - source = textwrap.dedent("""\ - Date: Fri, 6 Apr 2001 09:23:06 -0800 (GMT-0800) - From: foobar - Subject: broken mail - MIME-Version: 1.0 - Content-Type: multipart/report; report-type=delivery-status; - - --JAB03225.986577786/zinfandel.lacita.com - - One part - - --JAB03225.986577786/zinfandel.lacita.com - Content-Type: message/delivery-status - - Header: Another part - - --JAB03225.986577786/zinfandel.lacita.com-- - """) - with self._raise_point(errors.NoBoundaryInMultipartDefect): - msg = self._str_msg(source) - if self.raise_expected: return - self.assertTrue(isinstance(msg.get_payload(), str)) - self.assertEqual(len(self.get_defects(msg)), 2) - self.assertTrue(isinstance(self.get_defects(msg)[0], - errors.NoBoundaryInMultipartDefect)) - self.assertTrue(isinstance(self.get_defects(msg)[1], - errors.MultipartInvariantViolationDefect)) - - multipart_msg = textwrap.dedent("""\ - Date: Wed, 14 Nov 2007 12:56:23 GMT - From: foo@bar.invalid - To: foo@bar.invalid - Subject: Content-Transfer-Encoding: base64 and multipart - MIME-Version: 1.0 - Content-Type: multipart/mixed; - boundary="===============3344438784458119861=="{} - - --===============3344438784458119861== - Content-Type: text/plain - - Test message - - --===============3344438784458119861== - Content-Type: application/octet-stream - Content-Transfer-Encoding: base64 - - YWJj - - --===============3344438784458119861==-- - """) - - def test_multipart_invalid_cte(self): - with self._raise_point( - errors.InvalidMultipartContentTransferEncodingDefect): - msg = self._str_msg( - self.multipart_msg.format( - "\nContent-Transfer-Encoding: base64")) - if self.raise_expected: return - self.assertEqual(len(self.get_defects(msg)), 1) - self.assertIsInstance(self.get_defects(msg)[0], - errors.InvalidMultipartContentTransferEncodingDefect) - - def test_multipart_no_cte_no_defect(self): - if self.raise_expected: return - msg = self._str_msg(self.multipart_msg.format('')) - self.assertEqual(len(self.get_defects(msg)), 0) - - def test_multipart_valid_cte_no_defect(self): - if self.raise_expected: return - for cte in ('7bit', '8bit', 'BINary'): - msg = self._str_msg( - self.multipart_msg.format("\nContent-Transfer-Encoding: "+cte)) - self.assertEqual(len(self.get_defects(msg)), 0, "cte="+cte) - - def test_lying_multipart(self): - source = textwrap.dedent("""\ - From: "Allison Dunlap" - To: yyy@example.com - Subject: 64423 - Date: Sun, 11 Jul 2004 16:09:27 -0300 - MIME-Version: 1.0 - Content-Type: multipart/alternative; - - Blah blah blah - """) - with self._raise_point(errors.NoBoundaryInMultipartDefect): - msg = self._str_msg(source) - if self.raise_expected: return - self.assertTrue(hasattr(msg, 'defects')) - self.assertEqual(len(self.get_defects(msg)), 2) - self.assertTrue(isinstance(self.get_defects(msg)[0], - errors.NoBoundaryInMultipartDefect)) - self.assertTrue(isinstance(self.get_defects(msg)[1], - errors.MultipartInvariantViolationDefect)) - - def test_missing_start_boundary(self): - source = textwrap.dedent("""\ - Content-Type: multipart/mixed; boundary="AAA" - From: Mail Delivery Subsystem - To: yyy@example.com - - --AAA - - Stuff - - --AAA - Content-Type: message/rfc822 - - From: webmaster@python.org - To: zzz@example.com - Content-Type: multipart/mixed; boundary="BBB" - - --BBB-- - - --AAA-- - - """) - # The message structure is: - # - # multipart/mixed - # text/plain - # message/rfc822 - # multipart/mixed [*] - # - # [*] This message is missing its start boundary - with self._raise_point(errors.StartBoundaryNotFoundDefect): - outer = self._str_msg(source) - if self.raise_expected: return - bad = outer.get_payload(1).get_payload(0) - self.assertEqual(len(self.get_defects(bad)), 1) - self.assertTrue(isinstance(self.get_defects(bad)[0], - errors.StartBoundaryNotFoundDefect)) - - def test_first_line_is_continuation_header(self): - with self._raise_point(errors.FirstHeaderLineIsContinuationDefect): - msg = self._str_msg(' Line 1\nSubject: test\n\nbody') - if self.raise_expected: return - self.assertEqual(msg.keys(), ['Subject']) - self.assertEqual(msg.get_payload(), 'body') - self.assertEqual(len(self.get_defects(msg)), 1) - self.assertDefectsEqual(self.get_defects(msg), - [errors.FirstHeaderLineIsContinuationDefect]) - self.assertEqual(self.get_defects(msg)[0].line, ' Line 1\n') - - def test_missing_header_body_separator(self): - # Our heuristic if we see a line that doesn't look like a header (no - # leading whitespace but no ':') is to assume that the blank line that - # separates the header from the body is missing, and to stop parsing - # headers and start parsing the body. - with self._raise_point(errors.MissingHeaderBodySeparatorDefect): - msg = self._str_msg('Subject: test\nnot a header\nTo: abc\n\nb\n') - if self.raise_expected: return - self.assertEqual(msg.keys(), ['Subject']) - self.assertEqual(msg.get_payload(), 'not a header\nTo: abc\n\nb\n') - self.assertDefectsEqual(self.get_defects(msg), - [errors.MissingHeaderBodySeparatorDefect]) - - def test_bad_padding_in_base64_payload(self): - source = textwrap.dedent("""\ - Subject: test - MIME-Version: 1.0 - Content-Type: text/plain; charset="utf-8" - Content-Transfer-Encoding: base64 - - dmk - """) - msg = self._str_msg(source) - with self._raise_point(errors.InvalidBase64PaddingDefect): - payload = msg.get_payload(decode=True) - if self.raise_expected: return - self.assertEqual(payload, b'vi') - self.assertDefectsEqual(self.get_defects(msg), - [errors.InvalidBase64PaddingDefect]) - - def test_invalid_chars_in_base64_payload(self): - source = textwrap.dedent("""\ - Subject: test - MIME-Version: 1.0 - Content-Type: text/plain; charset="utf-8" - Content-Transfer-Encoding: base64 - - dm\x01k=== - """) - msg = self._str_msg(source) - with self._raise_point(errors.InvalidBase64CharactersDefect): - payload = msg.get_payload(decode=True) - if self.raise_expected: return - self.assertEqual(payload, b'vi') - self.assertDefectsEqual(self.get_defects(msg), - [errors.InvalidBase64CharactersDefect]) - - def test_missing_ending_boundary(self): - source = textwrap.dedent("""\ - To: 1@harrydomain4.com - Subject: Fwd: 1 - MIME-Version: 1.0 - Content-Type: multipart/alternative; - boundary="------------000101020201080900040301" - - --------------000101020201080900040301 - Content-Type: text/plain; charset=ISO-8859-1 - Content-Transfer-Encoding: 7bit - - Alternative 1 - - --------------000101020201080900040301 - Content-Type: text/html; charset=ISO-8859-1 - Content-Transfer-Encoding: 7bit - - Alternative 2 - - """) - with self._raise_point(errors.CloseBoundaryNotFoundDefect): - msg = self._str_msg(source) - if self.raise_expected: return - self.assertEqual(len(msg.get_payload()), 2) - self.assertEqual(msg.get_payload(1).get_payload(), 'Alternative 2\n') - self.assertDefectsEqual(self.get_defects(msg), - [errors.CloseBoundaryNotFoundDefect]) - - -class TestDefectDetection(TestDefectsBase, TestEmailBase): - - def get_defects(self, obj): - return obj.defects - - -class TestDefectCapture(TestDefectsBase, TestEmailBase): - - class CapturePolicy(policy.EmailPolicy): - captured = None - def register_defect(self, obj, defect): - self.captured.append(defect) - - def setUp(self): - self.policy = self.CapturePolicy(captured=list()) - - def get_defects(self, obj): - return self.policy.captured - - -class TestDefectRaising(TestDefectsBase, TestEmailBase): - - policy = TestDefectsBase.policy - policy = policy.clone(raise_on_defect=True) - raise_expected = True - - @contextlib.contextmanager - def _raise_point(self, defect): - with self.assertRaises(defect): - yield - - -if __name__ == '__main__': - unittest.main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/test_email.py --- a/Lib/test/test_email/test_email.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_email/test_email.py Mon May 21 23:01:17 2012 -0400 @@ -16,7 +16,6 @@ from itertools import chain import email -import email.policy from email.charset import Charset from email.header import Header, decode_header, make_header @@ -513,7 +512,6 @@ eq(msg.values(), ['One Hundred', 'Twenty', 'Three', 'Eleven']) self.assertRaises(KeyError, msg.replace_header, 'Fourth', 'Missing') - # test_defect_handling:test_invalid_chars_in_base64_payload def test_broken_base64_payload(self): x = 'AwDp0P7//y6LwKEAcPa/6Q=9' msg = Message() @@ -521,10 +519,7 @@ msg['content-transfer-encoding'] = 'base64' msg.set_payload(x) self.assertEqual(msg.get_payload(decode=True), - (b'\x03\x00\xe9\xd0\xfe\xff\xff.\x8b\xc0' - b'\xa1\x00p\xf6\xbf\xe9\x0f')) - self.assertIsInstance(msg.defects[0], - errors.InvalidBase64CharactersDefect) + bytes(x, 'raw-unicode-escape')) def test_broken_unicode_payload(self): # This test improves coverage but is not a compliance test. @@ -1810,7 +1805,11 @@ # Test some badly formatted messages -class TestNonConformant(TestEmailBase): +class TestNonConformantBase: + + def _msgobj(self, filename): + with openfile(filename) as fp: + return email.message_from_file(fp, policy=self.policy) def test_parse_missing_minor_type(self): eq = self.assertEqual @@ -1819,26 +1818,24 @@ eq(msg.get_content_maintype(), 'text') eq(msg.get_content_subtype(), 'plain') - # test_defect_handling def test_same_boundary_inner_outer(self): unless = self.assertTrue msg = self._msgobj('msg_15.txt') # XXX We can probably eventually do better inner = msg.get_payload(0) unless(hasattr(inner, 'defects')) - self.assertEqual(len(inner.defects), 1) - unless(isinstance(inner.defects[0], + self.assertEqual(len(self.get_defects(inner)), 1) + unless(isinstance(self.get_defects(inner)[0], errors.StartBoundaryNotFoundDefect)) - # test_defect_handling def test_multipart_no_boundary(self): unless = self.assertTrue msg = self._msgobj('msg_25.txt') unless(isinstance(msg.get_payload(), str)) - self.assertEqual(len(msg.defects), 2) - unless(isinstance(msg.defects[0], + self.assertEqual(len(self.get_defects(msg)), 2) + unless(isinstance(self.get_defects(msg)[0], errors.NoBoundaryInMultipartDefect)) - unless(isinstance(msg.defects[1], + unless(isinstance(self.get_defects(msg)[1], errors.MultipartInvariantViolationDefect)) multipart_msg = textwrap.dedent("""\ @@ -1864,26 +1861,27 @@ --===============3344438784458119861==-- """) - # test_defect_handling def test_multipart_invalid_cte(self): - msg = self._str_msg( - self.multipart_msg.format("\nContent-Transfer-Encoding: base64")) - self.assertEqual(len(msg.defects), 1) - self.assertIsInstance(msg.defects[0], + msg = email.message_from_string( + self.multipart_msg.format("\nContent-Transfer-Encoding: base64"), + policy = self.policy) + self.assertEqual(len(self.get_defects(msg)), 1) + self.assertIsInstance(self.get_defects(msg)[0], errors.InvalidMultipartContentTransferEncodingDefect) - # test_defect_handling def test_multipart_no_cte_no_defect(self): - msg = self._str_msg(self.multipart_msg.format('')) - self.assertEqual(len(msg.defects), 0) - - # test_defect_handling + msg = email.message_from_string( + self.multipart_msg.format(''), + policy = self.policy) + self.assertEqual(len(self.get_defects(msg)), 0) + def test_multipart_valid_cte_no_defect(self): for cte in ('7bit', '8bit', 'BINary'): - msg = self._str_msg( + msg = email.message_from_string( self.multipart_msg.format( - "\nContent-Transfer-Encoding: {}".format(cte))) - self.assertEqual(len(msg.defects), 0) + "\nContent-Transfer-Encoding: {}".format(cte)), + policy = self.policy) + self.assertEqual(len(self.get_defects(msg)), 0) def test_invalid_content_type(self): eq = self.assertEqual @@ -1934,18 +1932,16 @@ counter to RFC 2822, there's no separating newline here """) - # test_defect_handling def test_lying_multipart(self): unless = self.assertTrue msg = self._msgobj('msg_41.txt') unless(hasattr(msg, 'defects')) - self.assertEqual(len(msg.defects), 2) - unless(isinstance(msg.defects[0], + self.assertEqual(len(self.get_defects(msg)), 2) + unless(isinstance(self.get_defects(msg)[0], errors.NoBoundaryInMultipartDefect)) - unless(isinstance(msg.defects[1], + unless(isinstance(self.get_defects(msg)[1], errors.MultipartInvariantViolationDefect)) - # test_defect_handling def test_missing_start_boundary(self): outer = self._msgobj('msg_42.txt') # The message structure is: @@ -1957,33 +1953,71 @@ # # [*] This message is missing its start boundary bad = outer.get_payload(1).get_payload(0) - self.assertEqual(len(bad.defects), 1) - self.assertTrue(isinstance(bad.defects[0], + self.assertEqual(len(self.get_defects(bad)), 1) + self.assertTrue(isinstance(self.get_defects(bad)[0], errors.StartBoundaryNotFoundDefect)) - # test_defect_handling def test_first_line_is_continuation_header(self): eq = self.assertEqual - m = ' Line 1\nSubject: test\n\nbody' - msg = email.message_from_string(m) - eq(msg.keys(), ['Subject']) - eq(msg.get_payload(), 'body') - eq(len(msg.defects), 1) - self.assertDefectsEqual(msg.defects, - [errors.FirstHeaderLineIsContinuationDefect]) - eq(msg.defects[0].line, ' Line 1\n') - - # test_defect_handling - def test_missing_header_body_separator(self): - # Our heuristic if we see a line that doesn't look like a header (no - # leading whitespace but no ':') is to assume that the blank line that - # separates the header from the body is missing, and to stop parsing - # headers and start parsing the body. - msg = self._str_msg('Subject: test\nnot a header\nTo: abc\n\nb\n') - self.assertEqual(msg.keys(), ['Subject']) - self.assertEqual(msg.get_payload(), 'not a header\nTo: abc\n\nb\n') - self.assertDefectsEqual(msg.defects, - [errors.MissingHeaderBodySeparatorDefect]) + m = ' Line 1\nLine 2\nLine 3' + msg = email.message_from_string(m, policy=self.policy) + eq(msg.keys(), []) + eq(msg.get_payload(), 'Line 2\nLine 3') + eq(len(self.get_defects(msg)), 1) + self.assertTrue(isinstance(self.get_defects(msg)[0], + errors.FirstHeaderLineIsContinuationDefect)) + eq(self.get_defects(msg)[0].line, ' Line 1\n') + + +class TestNonConformant(TestNonConformantBase, TestEmailBase): + + policy=email.policy.default + + def get_defects(self, obj): + return obj.defects + + +class TestNonConformantCapture(TestNonConformantBase, TestEmailBase): + + class CapturePolicy(email.policy.Policy): + captured = None + def register_defect(self, obj, defect): + self.captured.append(defect) + + def setUp(self): + self.policy = self.CapturePolicy(captured=list()) + + def get_defects(self, obj): + return self.policy.captured + + +class TestRaisingDefects(TestEmailBase): + + def _msgobj(self, filename): + with openfile(filename) as fp: + return email.message_from_file(fp, policy=email.policy.strict) + + def test_same_boundary_inner_outer(self): + with self.assertRaises(errors.StartBoundaryNotFoundDefect): + self._msgobj('msg_15.txt') + + def test_multipart_no_boundary(self): + with self.assertRaises(errors.NoBoundaryInMultipartDefect): + self._msgobj('msg_25.txt') + + def test_lying_multipart(self): + with self.assertRaises(errors.NoBoundaryInMultipartDefect): + self._msgobj('msg_41.txt') + + + def test_missing_start_boundary(self): + with self.assertRaises(errors.StartBoundaryNotFoundDefect): + self._msgobj('msg_42.txt') + + def test_first_line_is_continuation_header(self): + m = ' Line 1\nLine 2\nLine 3' + with self.assertRaises(errors.FirstHeaderLineIsContinuationDefect): + msg = email.message_from_string(m, policy=email.policy.strict) # Test RFC 2047 header encoding and decoding @@ -1994,9 +2028,9 @@ foo bar =?mac-iceland?q?r=8Aksm=9Arg=8Cs?=""" dh = decode_header(s) eq(dh, [ - (b'Re: ', None), + (b'Re:', None), (b'r\x8aksm\x9arg\x8cs', 'mac-iceland'), - (b' baz foo bar ', None), + (b'baz foo bar', None), (b'r\x8aksm\x9arg\x8cs', 'mac-iceland')]) header = make_header(dh) eq(str(header), @@ -2005,38 +2039,36 @@ Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm?= =?mac-iceland?q?=9Arg=8Cs?=""") - def test_whitespace_keeper_unicode(self): + def test_whitespace_eater_unicode(self): eq = self.assertEqual s = '=?ISO-8859-1?Q?Andr=E9?= Pirard ' dh = decode_header(s) eq(dh, [(b'Andr\xe9', 'iso-8859-1'), - (b' Pirard ', None)]) + (b'Pirard ', None)]) header = str(make_header(dh)) eq(header, 'Andr\xe9 Pirard ') - def test_whitespace_keeper_unicode_2(self): + def test_whitespace_eater_unicode_2(self): eq = self.assertEqual s = 'The =?iso-8859-1?b?cXVpY2sgYnJvd24gZm94?= jumped over the =?iso-8859-1?b?bGF6eSBkb2c=?=' dh = decode_header(s) - eq(dh, [(b'The ', None), (b'quick brown fox', 'iso-8859-1'), - (b' jumped over the ', None), (b'lazy dog', 'iso-8859-1')]) + eq(dh, [(b'The', None), (b'quick brown fox', 'iso-8859-1'), + (b'jumped over the', None), (b'lazy dog', 'iso-8859-1')]) hu = str(make_header(dh)) eq(hu, 'The quick brown fox jumped over the lazy dog') def test_rfc2047_missing_whitespace(self): s = 'Sm=?ISO-8859-1?B?9g==?=rg=?ISO-8859-1?B?5Q==?=sbord' dh = decode_header(s) + self.assertEqual(dh, [(s, None)]) + + def test_rfc2047_with_whitespace(self): + s = 'Sm =?ISO-8859-1?B?9g==?= rg =?ISO-8859-1?B?5Q==?= sbord' + dh = decode_header(s) self.assertEqual(dh, [(b'Sm', None), (b'\xf6', 'iso-8859-1'), (b'rg', None), (b'\xe5', 'iso-8859-1'), (b'sbord', None)]) - def test_rfc2047_with_whitespace(self): - s = 'Sm =?ISO-8859-1?B?9g==?= rg =?ISO-8859-1?B?5Q==?= sbord' - dh = decode_header(s) - self.assertEqual(dh, [(b'Sm ', None), (b'\xf6', 'iso-8859-1'), - (b' rg ', None), (b'\xe5', 'iso-8859-1'), - (b' sbord', None)]) - def test_rfc2047_B_bad_padding(self): s = '=?iso-8859-1?B?%s?=' data = [ # only test complete bytes @@ -2053,67 +2085,6 @@ self.assertEqual(decode_header(s), [(b'andr\xe9=zz', 'iso-8659-1')]) - def test_rfc2047_rfc2047_1(self): - # 1st testcase at end of rfc2047 - s = '(=?ISO-8859-1?Q?a?=)' - self.assertEqual(decode_header(s), - [(b'(', None), (b'a', 'iso-8859-1'), (b')', None)]) - - def test_rfc2047_rfc2047_2(self): - # 2nd testcase at end of rfc2047 - s = '(=?ISO-8859-1?Q?a?= b)' - self.assertEqual(decode_header(s), - [(b'(', None), (b'a', 'iso-8859-1'), (b' b)', None)]) - - def test_rfc2047_rfc2047_3(self): - # 3rd testcase at end of rfc2047 - s = '(=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=)' - self.assertEqual(decode_header(s), - [(b'(', None), (b'ab', 'iso-8859-1'), (b')', None)]) - - def test_rfc2047_rfc2047_4(self): - # 4th testcase at end of rfc2047 - s = '(=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=)' - self.assertEqual(decode_header(s), - [(b'(', None), (b'ab', 'iso-8859-1'), (b')', None)]) - - def test_rfc2047_rfc2047_5a(self): - # 5th testcase at end of rfc2047 newline is \r\n - s = '(=?ISO-8859-1?Q?a?=\r\n =?ISO-8859-1?Q?b?=)' - self.assertEqual(decode_header(s), - [(b'(', None), (b'ab', 'iso-8859-1'), (b')', None)]) - - def test_rfc2047_rfc2047_5b(self): - # 5th testcase at end of rfc2047 newline is \n - s = '(=?ISO-8859-1?Q?a?=\n =?ISO-8859-1?Q?b?=)' - self.assertEqual(decode_header(s), - [(b'(', None), (b'ab', 'iso-8859-1'), (b')', None)]) - - def test_rfc2047_rfc2047_6(self): - # 6th testcase at end of rfc2047 - s = '(=?ISO-8859-1?Q?a_b?=)' - self.assertEqual(decode_header(s), - [(b'(', None), (b'a b', 'iso-8859-1'), (b')', None)]) - - def test_rfc2047_rfc2047_7(self): - # 7th testcase at end of rfc2047 - s = '(=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?=)' - self.assertEqual(decode_header(s), - [(b'(', None), (b'a', 'iso-8859-1'), (b' b', 'iso-8859-2'), - (b')', None)]) - self.assertEqual(make_header(decode_header(s)).encode(), s.lower()) - self.assertEqual(str(make_header(decode_header(s))), '(a b)') - - def test_multiline_header(self): - s = '=?windows-1252?q?=22M=FCller_T=22?=\r\n ' - self.assertEqual(decode_header(s), - [(b'"M\xfcller T"', 'windows-1252'), - (b'', None)]) - self.assertEqual(make_header(decode_header(s)).encode(), - ''.join(s.splitlines())) - self.assertEqual(str(make_header(decode_header(s))), - '"Müller T" ') - # Test the MIMEMessage class class TestMIMEMessage(TestEmailBase): @@ -2639,13 +2610,6 @@ for subpart in msg.walk(): unless(isinstance(subpart, MyMessage)) - def test_custom_message_does_not_require_arguments(self): - class MyMessage(Message): - def __init__(self): - super().__init__() - msg = self._str_msg("Subject: test\n\ntest", MyMessage) - self.assertTrue(isinstance(msg, MyMessage)) - def test__all__(self): module = __import__('email') self.assertEqual(sorted(module.__all__), [ @@ -3173,6 +3137,25 @@ g.flatten(msg, linesep='\r\n') self.assertEqual(s.getvalue(), text) + def test_crlf_control_via_policy(self): + with openfile('msg_26.txt', newline='\n') as fp: + text = fp.read() + msg = email.message_from_string(text) + s = StringIO() + g = email.generator.Generator(s, policy=email.policy.SMTP) + g.flatten(msg) + self.assertEqual(s.getvalue(), text) + + def test_flatten_linesep_overrides_policy(self): + # msg_27 is lf separated + with openfile('msg_27.txt', newline='\n') as fp: + text = fp.read() + msg = email.message_from_string(text) + s = StringIO() + g = email.generator.Generator(s, policy=email.policy.SMTP) + g.flatten(msg, linesep='\n') + self.assertEqual(s.getvalue(), text) + maxDiff = None def test_multipart_digest_with_extra_mime_headers(self): @@ -3338,19 +3321,15 @@ self.assertEqual(msg.get_payload(decode=True), 'pöstál\n'.encode('utf-8')) - # test_defect_handling:test_invalid_chars_in_base64_payload def test_8bit_in_base64_body(self): - # If we get 8bit bytes in a base64 body, we can just ignore them - # as being outside the base64 alphabet and decode anyway. But - # we register a defect. + # Sticking an 8bit byte in a base64 block makes it undecodable by + # normal means, so the block is returned undecoded, but as bytes. m = self.bodytest_msg.format(charset='utf-8', cte='base64', bodyline='cMO2c3RhbAá=').encode('utf-8') msg = email.message_from_bytes(m) self.assertEqual(msg.get_payload(decode=True), - 'pöstal'.encode('utf-8')) - self.assertIsInstance(msg.defects[0], - errors.InvalidBase64CharactersDefect) + 'cMO2c3RhbAá=\n'.encode('utf-8')) def test_8bit_in_uuencode_body(self): # Sticking an 8bit byte in a uuencode block makes it undecodable by @@ -3667,6 +3646,44 @@ s.getvalue(), 'Subject: =?utf-8?b?xb5sdcWlb3XEjWvDvSBrxa/FiA==?=\r\n\r\n') + def test_crlf_control_via_policy(self): + # msg_26 is crlf terminated + with openfile('msg_26.txt', 'rb') as fp: + text = fp.read() + msg = email.message_from_bytes(text) + s = BytesIO() + g = email.generator.BytesGenerator(s, policy=email.policy.SMTP) + g.flatten(msg) + self.assertEqual(s.getvalue(), text) + + def test_flatten_linesep_overrides_policy(self): + # msg_27 is lf separated + with openfile('msg_27.txt', 'rb') as fp: + text = fp.read() + msg = email.message_from_bytes(text) + s = BytesIO() + g = email.generator.BytesGenerator(s, policy=email.policy.SMTP) + g.flatten(msg, linesep='\n') + self.assertEqual(s.getvalue(), text) + + def test_must_be_7bit_handles_unknown_8bit(self): + msg = email.message_from_bytes(self.non_latin_bin_msg) + out = BytesIO() + g = email.generator.BytesGenerator(out, + policy=email.policy.default.clone(must_be_7bit=True)) + g.flatten(msg) + self.assertEqual(out.getvalue(), + self.non_latin_bin_msg_as7bit_wrapped.encode('ascii')) + + def test_must_be_7bit_transforms_8bit_cte(self): + msg = email.message_from_bytes(self.latin_bin_msg) + out = BytesIO() + g = email.generator.BytesGenerator(out, + policy=email.policy.default.clone(must_be_7bit=True)) + g.flatten(msg) + self.assertEqual(out.getvalue(), + self.latin_bin_msg_as7bit.encode('ascii')) + maxDiff = None @@ -4451,11 +4468,11 @@ h = make_header(decode_header(s)) eq(h.encode(), s) - def test_whitespace_keeper(self): + def test_whitespace_eater(self): eq = self.assertEqual s = 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztk=?= =?koi8-r?q?=CA?= zz.' parts = decode_header(s) - eq(parts, [(b'Subject: ', None), (b'\xf0\xd2\xcf\xd7\xc5\xd2\xcb\xc1 \xce\xc1 \xc6\xc9\xce\xc1\xcc\xd8\xce\xd9\xca', 'koi8-r'), (b' zz.', None)]) + eq(parts, [(b'Subject:', None), (b'\xf0\xd2\xcf\xd7\xc5\xd2\xcb\xc1 \xce\xc1 \xc6\xc9\xce\xc1\xcc\xd8\xce\xd9\xca', 'koi8-r'), (b'zz.', None)]) hdr = make_header(parts) eq(hdr.encode(), 'Subject: =?koi8-r?b?8NLP18XSy8EgzsEgxsnOwczYztnK?= zz.') diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/test_generator.py --- a/Lib/test/test_email/test_generator.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_email/test_generator.py Mon May 21 23:01:17 2012 -0400 @@ -4,19 +4,14 @@ from email import message_from_string, message_from_bytes from email.generator import Generator, BytesGenerator from email import policy -from test.test_email import TestEmailBase, parameterize +from test.test_email import TestEmailBase +# XXX: move generator tests from test_email into here at some point. -@parameterize -class TestGeneratorBase: - policy = policy.default +class TestGeneratorBase(): - def msgmaker(self, msg, policy=None): - policy = self.policy if policy is None else policy - return self.msgfunc(msg, policy=policy) - - refold_long_expected = { + long_subject = { 0: textwrap.dedent("""\ To: whom_it_may_concern@example.com From: nobody_you_want_to_know@example.com @@ -26,32 +21,33 @@ None """), - # From is wrapped because wrapped it fits in 40. 40: textwrap.dedent("""\ To: whom_it_may_concern@example.com - From: + From:\x20 nobody_you_want_to_know@example.com Subject: We the willing led by the - unknowing are doing the impossible for - the ungrateful. We have done so much - for so long with so little we are now - qualified to do anything with nothing. + unknowing are doing the + impossible for the ungrateful. We have + done so much for so long with so little + we are now qualified to do anything + with nothing. None """), - # Neither to nor from fit even if put on a new line, - # so we leave them sticking out on the first line. 20: textwrap.dedent("""\ - To: whom_it_may_concern@example.com - From: nobody_you_want_to_know@example.com + To:\x20 + whom_it_may_concern@example.com + From:\x20 + nobody_you_want_to_know@example.com Subject: We the willing led by the unknowing are doing - the impossible for - the ungrateful. We - have done so much - for so long with so - little we are now + the + impossible for the + ungrateful. We have + done so much for so + long with so little + we are now qualified to do anything with nothing. @@ -59,140 +55,81 @@ None """), } - refold_long_expected[100] = refold_long_expected[0] + long_subject[100] = long_subject[0] - refold_all_expected = refold_long_expected.copy() - refold_all_expected[0] = ( - "To: whom_it_may_concern@example.com\n" - "From: nobody_you_want_to_know@example.com\n" - "Subject: We the willing led by the unknowing are doing the " - "impossible for the ungrateful. We have done so much for " - "so long with so little we are now qualified to do anything " - "with nothing.\n" - "\n" - "None\n") - refold_all_expected[100] = ( - "To: whom_it_may_concern@example.com\n" - "From: nobody_you_want_to_know@example.com\n" - "Subject: We the willing led by the unknowing are doing the " - "impossible for the ungrateful. We have\n" - " done so much for so long with so little we are now qualified " - "to do anything with nothing.\n" - "\n" - "None\n") + def maxheaderlen_parameter_test(self, n): + msg = self.msgmaker(self.long_subject[0]) + s = self.ioclass() + g = self.genclass(s, maxheaderlen=n) + g.flatten(msg) + self.assertEqual(s.getvalue(), self.long_subject[n]) - length_params = [n for n in refold_long_expected] + def test_maxheaderlen_parameter_0(self): + self.maxheaderlen_parameter_test(0) - def length_as_maxheaderlen_parameter(self, n): - msg = self.msgmaker(self.typ(self.refold_long_expected[0])) + def test_maxheaderlen_parameter_100(self): + self.maxheaderlen_parameter_test(100) + + def test_maxheaderlen_parameter_40(self): + self.maxheaderlen_parameter_test(40) + + def test_maxheaderlen_parameter_20(self): + self.maxheaderlen_parameter_test(20) + + def maxheaderlen_policy_test(self, n): + msg = self.msgmaker(self.long_subject[0]) s = self.ioclass() - g = self.genclass(s, maxheaderlen=n, policy=self.policy) + g = self.genclass(s, policy=policy.default.clone(max_line_length=n)) g.flatten(msg) - self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[n])) + self.assertEqual(s.getvalue(), self.long_subject[n]) - def length_as_max_line_length_policy(self, n): - msg = self.msgmaker(self.typ(self.refold_long_expected[0])) - s = self.ioclass() - g = self.genclass(s, policy=self.policy.clone(max_line_length=n)) - g.flatten(msg) - self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[n])) + def test_maxheaderlen_policy_0(self): + self.maxheaderlen_policy_test(0) - def length_as_maxheaderlen_parm_overrides_policy(self, n): - msg = self.msgmaker(self.typ(self.refold_long_expected[0])) + def test_maxheaderlen_policy_100(self): + self.maxheaderlen_policy_test(100) + + def test_maxheaderlen_policy_40(self): + self.maxheaderlen_policy_test(40) + + def test_maxheaderlen_policy_20(self): + self.maxheaderlen_policy_test(20) + + def maxheaderlen_parm_overrides_policy_test(self, n): + msg = self.msgmaker(self.long_subject[0]) s = self.ioclass() g = self.genclass(s, maxheaderlen=n, - policy=self.policy.clone(max_line_length=10)) + policy=policy.default.clone(max_line_length=10)) g.flatten(msg) - self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[n])) + self.assertEqual(s.getvalue(), self.long_subject[n]) - def length_as_max_line_length_with_refold_none_does_not_fold(self, n): - msg = self.msgmaker(self.typ(self.refold_long_expected[0])) - s = self.ioclass() - g = self.genclass(s, policy=self.policy.clone(refold_source='none', - max_line_length=n)) - g.flatten(msg) - self.assertEqual(s.getvalue(), self.typ(self.refold_long_expected[0])) + def test_maxheaderlen_parm_overrides_policy_0(self): + self.maxheaderlen_parm_overrides_policy_test(0) - def length_as_max_line_length_with_refold_all_folds(self, n): - msg = self.msgmaker(self.typ(self.refold_long_expected[0])) - s = self.ioclass() - g = self.genclass(s, policy=self.policy.clone(refold_source='all', - max_line_length=n)) - g.flatten(msg) - self.assertEqual(s.getvalue(), self.typ(self.refold_all_expected[n])) + def test_maxheaderlen_parm_overrides_policy_100(self): + self.maxheaderlen_parm_overrides_policy_test(100) - def test_crlf_control_via_policy(self): - source = "Subject: test\r\n\r\ntest body\r\n" - expected = source - msg = self.msgmaker(self.typ(source)) - s = self.ioclass() - g = self.genclass(s, policy=policy.SMTP) - g.flatten(msg) - self.assertEqual(s.getvalue(), self.typ(expected)) + def test_maxheaderlen_parm_overrides_policy_40(self): + self.maxheaderlen_parm_overrides_policy_test(40) - def test_flatten_linesep_overrides_policy(self): - source = "Subject: test\n\ntest body\n" - expected = source - msg = self.msgmaker(self.typ(source)) - s = self.ioclass() - g = self.genclass(s, policy=policy.SMTP) - g.flatten(msg, linesep='\n') - self.assertEqual(s.getvalue(), self.typ(expected)) + def test_maxheaderlen_parm_overrides_policy_20(self): + self.maxheaderlen_parm_overrides_policy_test(20) class TestGenerator(TestGeneratorBase, TestEmailBase): - msgfunc = staticmethod(message_from_string) + msgmaker = staticmethod(message_from_string) genclass = Generator ioclass = io.StringIO - typ = str class TestBytesGenerator(TestGeneratorBase, TestEmailBase): - msgfunc = staticmethod(message_from_bytes) + msgmaker = staticmethod(message_from_bytes) genclass = BytesGenerator ioclass = io.BytesIO - typ = lambda self, x: x.encode('ascii') - - def test_cte_type_7bit_handles_unknown_8bit(self): - source = ("Subject: Maintenant je vous présente mon " - "collègue\n\n").encode('utf-8') - expected = ('Subject: Maintenant je vous =?unknown-8bit?q?' - 'pr=C3=A9sente_mon_coll=C3=A8gue?=\n\n').encode('ascii') - msg = message_from_bytes(source) - s = io.BytesIO() - g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit')) - g.flatten(msg) - self.assertEqual(s.getvalue(), expected) - - def test_cte_type_7bit_transforms_8bit_cte(self): - source = textwrap.dedent("""\ - From: foo@bar.com - To: Dinsdale - Subject: Nudge nudge, wink, wink - Mime-Version: 1.0 - Content-Type: text/plain; charset="latin-1" - Content-Transfer-Encoding: 8bit - - oh là là, know what I mean, know what I mean? - """).encode('latin1') - msg = message_from_bytes(source) - expected = textwrap.dedent("""\ - From: foo@bar.com - To: Dinsdale - Subject: Nudge nudge, wink, wink - Mime-Version: 1.0 - Content-Type: text/plain; charset="iso-8859-1" - Content-Transfer-Encoding: quoted-printable - - oh l=E0 l=E0, know what I mean, know what I mean? - """).encode('ascii') - s = io.BytesIO() - g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit', - linesep='\n')) - g.flatten(msg) - self.assertEqual(s.getvalue(), expected) + long_subject = {key: x.encode('ascii') + for key, x in TestGeneratorBase.long_subject.items()} if __name__ == '__main__': diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/test_headerregistry.py --- a/Lib/test/test_email/test_headerregistry.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,729 +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, parameterize -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) - - -@parameterize -class TestAddressHeader(TestHeaderBase): - - example_params = { - - '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 example_as_address(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) - - def example_as_group(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) - - 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.example_params.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=policy.default), - '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=policy.default), - '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=policy.default), 'Subject: test\n') - - def test_fold_unstructured_short(self): - h = self.make_header('Subject', 'test test test') - self.assertEqual(h.fold(policy=policy.default), - '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=policy.default), 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=policy.default), - 'Date: Sat, 02 Feb 2002 17:00:06 -0800\n') - - - -if __name__ == '__main__': - unittest.main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/test_message.py --- a/Lib/test/test_email/test_message.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ -import unittest -from email import policy -from test.test_email import TestEmailBase - - -class Test(TestEmailBase): - - policy = policy.default - - def test_error_on_setitem_if_max_count_exceeded(self): - m = self._str_msg("") - m['To'] = 'abc@xyz' - with self.assertRaises(ValueError): - m['To'] = 'xyz@abc' - - -if __name__ == '__main__': - unittest.main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/test_parser.py --- a/Lib/test/test_email/test_parser.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -import io -import email -import unittest -from email.message import Message -from test.test_email import TestEmailBase - - -class TestCustomMessage(TestEmailBase): - - class MyMessage(Message): - def __init__(self, policy): - self.check_policy = policy - super().__init__() - - MyPolicy = TestEmailBase.policy.clone(linesep='boo') - - def test_custom_message_gets_policy_if_possible_from_string(self): - msg = email.message_from_string("Subject: bogus\n\nmsg\n", - self.MyMessage, - policy=self.MyPolicy) - self.assertTrue(isinstance(msg, self.MyMessage)) - self.assertIs(msg.check_policy, self.MyPolicy) - - def test_custom_message_gets_policy_if_possible_from_file(self): - source_file = io.StringIO("Subject: bogus\n\nmsg\n") - msg = email.message_from_file(source_file, - self.MyMessage, - policy=self.MyPolicy) - self.assertTrue(isinstance(msg, self.MyMessage)) - self.assertIs(msg.check_policy, self.MyPolicy) - - # XXX add tests for other functions that take Message arg. - - -if __name__ == '__main__': - unittest.main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/test_pickleable.py --- a/Lib/test/test_email/test_pickleable.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,74 +0,0 @@ -import unittest -import textwrap -import copy -import pickle -import email -import email.message -from email import policy -from email.headerregistry import HeaderRegistry -from test.test_email import TestEmailBase, parameterize - - -@parameterize -class TestPickleCopyHeader(TestEmailBase): - - header_factory = HeaderRegistry() - - unstructured = header_factory('subject', 'this is a test') - - header_params = { - 'subject': ('subject', 'this is a test'), - 'from': ('from', 'frodo@mordor.net'), - 'to': ('to', 'a: k@b.com, y@z.com;, j@f.com'), - 'date': ('date', 'Tue, 29 May 2012 09:24:26 +1000'), - } - - def header_as_deepcopy(self, name, value): - header = self.header_factory(name, value) - h = copy.deepcopy(header) - self.assertEqual(str(h), str(header)) - - def header_as_pickle(self, name, value): - header = self.header_factory(name, value) - p = pickle.dumps(header) - h = pickle.loads(p) - self.assertEqual(str(h), str(header)) - - -@parameterize -class TestPickleCopyMessage(TestEmailBase): - - # Message objects are a sequence, so we have to make them a one-tuple in - # msg_params so they get passed to the parameterized test method as a - # single argument instead of as a list of headers. - msg_params = {} - - # Note: there will be no custom header objects in the parsed message. - msg_params['parsed'] = (email.message_from_string(textwrap.dedent("""\ - Date: Tue, 29 May 2012 09:24:26 +1000 - From: frodo@mordor.net - To: bilbo@underhill.org - Subject: help - - I think I forgot the ring. - """), policy=policy.default),) - - msg_params['created'] = (email.message.Message(policy=policy.default),) - msg_params['created'][0]['Date'] = 'Tue, 29 May 2012 09:24:26 +1000' - msg_params['created'][0]['From'] = 'frodo@mordor.net' - msg_params['created'][0]['To'] = 'bilbo@underhill.org' - msg_params['created'][0]['Subject'] = 'help' - msg_params['created'][0].set_payload('I think I forgot the ring.') - - def msg_as_deepcopy(self, msg): - msg2 = copy.deepcopy(msg) - self.assertEqual(msg2.as_string(), msg.as_string()) - - def msg_as_pickle(self, msg): - p = pickle.dumps(msg) - msg2 = pickle.loads(p) - self.assertEqual(msg2.as_string(), msg.as_string()) - - -if __name__ == '__main__': - unittest.main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/test_policy.py --- a/Lib/test/test_email/test_policy.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_email/test_policy.py Mon May 21 23:01:17 2012 -0400 @@ -1,102 +1,62 @@ -import io import types -import textwrap import unittest import email.policy -import email.parser -import email.generator -from email import headerregistry - -def make_defaults(base_defaults, differences): - defaults = base_defaults.copy() - defaults.update(differences) - return defaults class PolicyAPITests(unittest.TestCase): longMessage = True - # Base default values. - compat32_defaults = { + # These default values are the ones set on email.policy.default. + # If any of these defaults change, the docs must be updated. + policy_defaults = { 'max_line_length': 78, 'linesep': '\n', - 'cte_type': '8bit', + 'must_be_7bit': False, 'raise_on_defect': False, } - # These default values are the ones set on email.policy.default. - # If any of these defaults change, the docs must be updated. - policy_defaults = compat32_defaults.copy() - policy_defaults.update({ - 'raise_on_defect': False, - 'header_factory': email.policy.EmailPolicy.header_factory, - 'refold_source': 'long', - }) - # For each policy under test, we give here what we expect the defaults to - # be for that policy. The second argument to make defaults is the - # difference between the base defaults and that for the particular policy. - new_policy = email.policy.EmailPolicy() + # For each policy under test, we give here the values of the attributes + # that are different from the defaults for that policy. policies = { - email.policy.compat32: make_defaults(compat32_defaults, {}), - email.policy.default: make_defaults(policy_defaults, {}), - email.policy.SMTP: make_defaults(policy_defaults, - {'linesep': '\r\n'}), - email.policy.HTTP: make_defaults(policy_defaults, - {'linesep': '\r\n', - 'max_line_length': None}), - email.policy.strict: make_defaults(policy_defaults, - {'raise_on_defect': True}), - new_policy: make_defaults(policy_defaults, {}), + email.policy.Policy(): {}, + email.policy.default: {}, + email.policy.SMTP: {'linesep': '\r\n'}, + email.policy.HTTP: {'linesep': '\r\n', 'max_line_length': None}, + email.policy.strict: {'raise_on_defect': True}, } - # Creating a new policy creates a new header factory. There is a test - # later that proves this. - policies[new_policy]['header_factory'] = new_policy.header_factory def test_defaults(self): - for policy, expected in self.policies.items(): + for policy, changed_defaults in self.policies.items(): + expected = self.policy_defaults.copy() + expected.update(changed_defaults) for attr, value in expected.items(): self.assertEqual(getattr(policy, attr), value, ("change {} docs/docstrings if defaults have " "changed").format(policy)) def test_all_attributes_covered(self): - for policy, expected in self.policies.items(): - for attr in dir(policy): - if (attr.startswith('_') or - isinstance(getattr(email.policy.EmailPolicy, attr), - types.FunctionType)): - continue - else: - self.assertIn(attr, expected, - "{} is not fully tested".format(attr)) - - def test_abc(self): - with self.assertRaises(TypeError) as cm: - email.policy.Policy() - msg = str(cm.exception) - abstract_methods = ('fold', - 'fold_binary', - 'header_fetch_parse', - 'header_source_parse', - 'header_store_parse') - for method in abstract_methods: - self.assertIn(method, msg) + for attr in dir(email.policy.default): + if (attr.startswith('_') or + isinstance(getattr(email.policy.Policy, attr), + types.FunctionType)): + continue + else: + self.assertIn(attr, self.policy_defaults, + "{} is not fully tested".format(attr)) def test_policy_is_immutable(self): - for policy, defaults in self.policies.items(): - for attr in defaults: + for policy in self.policies: + for attr in self.policy_defaults: with self.assertRaisesRegex(AttributeError, attr+".*read-only"): setattr(policy, attr, None) with self.assertRaisesRegex(AttributeError, 'no attribute.*foo'): policy.foo = None - def test_set_policy_attrs_when_cloned(self): - # None of the attributes has a default value of None, so we set them - # all to None in the clone call and check that it worked. - for policyclass, defaults in self.policies.items(): - testattrdict = {attr: None for attr in defaults} + def test_set_policy_attrs_when_calledl(self): + testattrdict = { attr: None for attr in self.policy_defaults } + for policyclass in self.policies: policy = policyclass.clone(**testattrdict) - for attr in defaults: + for attr in self.policy_defaults: self.assertIsNone(getattr(policy, attr)) def test_reject_non_policy_keyword_when_called(self): @@ -128,7 +88,7 @@ self.defects = [] obj = Dummy() defect = object() - policy = email.policy.EmailPolicy() + policy = email.policy.Policy() policy.register_defect(obj, defect) self.assertEqual(obj.defects, [defect]) defect2 = object() @@ -157,7 +117,7 @@ email.policy.default.handle_defect(foo, defect2) self.assertEqual(foo.defects, [defect1, defect2]) - class MyPolicy(email.policy.EmailPolicy): + class MyPolicy(email.policy.Policy): defects = None def __init__(self, *args, **kw): super().__init__(*args, defects=[], **kw) @@ -182,141 +142,9 @@ self.assertEqual(my_policy.defects, [defect1, defect2]) self.assertEqual(foo.defects, []) - 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) - - class Foo: - parse = headerregistry.UnstructuredHeader.parse - - def test_each_Policy_gets_unique_factory(self): - policy1 = email.policy.EmailPolicy() - policy2 = email.policy.EmailPolicy() - 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) - h = policy2.header_factory('foo', 'test') - self.assertNotIsInstance(h, self.Foo) - self.assertIsInstance(h, headerregistry.UnstructuredHeader) - - def test_clone_copies_factory(self): - policy1 = email.policy.EmailPolicy() - policy2 = policy1.clone() - policy1.header_factory.map_to_type('foo', self.Foo) - h = policy1.header_factory('foo', 'test') - self.assertIsInstance(h, self.Foo) - h = policy2.header_factory('foo', 'test') - self.assertIsInstance(h, self.Foo) - - def test_new_factory_overrides_default(self): - mypolicy = email.policy.EmailPolicy() - myfactory = mypolicy.header_factory - newpolicy = mypolicy + email.policy.strict - self.assertEqual(newpolicy.header_factory, myfactory) - newpolicy = email.policy.strict + mypolicy - self.assertEqual(newpolicy.header_factory, myfactory) - - def test_adding_default_policies_preserves_default_factory(self): - newpolicy = email.policy.default + email.policy.strict - self.assertEqual(newpolicy.header_factory, - email.policy.EmailPolicy.header_factory) - self.assertEqual(newpolicy.__dict__, {'raise_on_defect': True}) - # XXX: Need subclassing tests. # For adding subclassed objects, make sure the usual rules apply (subclass # wins), but that the order still works (right overrides left). - -class TestPolicyPropagation(unittest.TestCase): - - # The abstract methods are used by the parser but not by the wrapper - # functions that call it, so if the exception gets raised we know that the - # policy was actually propagated all the way to feedparser. - class MyPolicy(email.policy.Policy): - def badmethod(self, *args, **kw): - raise Exception("test") - fold = fold_binary = header_fetch_parser = badmethod - header_source_parse = header_store_parse = badmethod - - def test_message_from_string(self): - with self.assertRaisesRegex(Exception, "^test$"): - email.message_from_string("Subject: test\n\n", - policy=self.MyPolicy) - - def test_message_from_bytes(self): - with self.assertRaisesRegex(Exception, "^test$"): - email.message_from_bytes(b"Subject: test\n\n", - policy=self.MyPolicy) - - def test_message_from_file(self): - f = io.StringIO('Subject: test\n\n') - with self.assertRaisesRegex(Exception, "^test$"): - email.message_from_file(f, policy=self.MyPolicy) - - def test_message_from_binary_file(self): - f = io.BytesIO(b'Subject: test\n\n') - with self.assertRaisesRegex(Exception, "^test$"): - email.message_from_binary_file(f, policy=self.MyPolicy) - - # These are redundant, but we need them for black-box completeness. - - def test_parser(self): - p = email.parser.Parser(policy=self.MyPolicy) - with self.assertRaisesRegex(Exception, "^test$"): - p.parsestr('Subject: test\n\n') - - def test_bytes_parser(self): - p = email.parser.BytesParser(policy=self.MyPolicy) - with self.assertRaisesRegex(Exception, "^test$"): - p.parsebytes(b'Subject: test\n\n') - - # Now that we've established that all the parse methods get the - # policy in to feedparser, we can use message_from_string for - # the rest of the propagation tests. - - def _make_msg(self, source='Subject: test\n\n', policy=None): - self.policy = email.policy.default.clone() if policy is None else policy - return email.message_from_string(source, policy=self.policy) - - def test_parser_propagates_policy_to_message(self): - msg = self._make_msg() - self.assertIs(msg.policy, self.policy) - - def test_parser_propagates_policy_to_sub_messages(self): - msg = self._make_msg(textwrap.dedent("""\ - Subject: mime test - MIME-Version: 1.0 - Content-Type: multipart/mixed, boundary="XXX" - - --XXX - Content-Type: text/plain - - test - --XXX - Content-Type: text/plain - - test2 - --XXX-- - """)) - for part in msg.walk(): - self.assertIs(part.policy, self.policy) - - def test_message_policy_propagates_to_generator(self): - msg = self._make_msg("Subject: test\nTo: foo\n\n", - policy=email.policy.default.clone(linesep='X')) - s = io.StringIO() - g = email.generator.Generator(s) - g.flatten(msg) - self.assertEqual(s.getvalue(), "Subject: testXTo: fooXX") - - def test_message_policy_used_by_as_string(self): - msg = self._make_msg("Subject: test\nTo: foo\n\n", - policy=email.policy.default.clone(linesep='X')) - self.assertEqual(msg.as_string(), "Subject: testXTo: fooXX") - - if __name__ == '__main__': unittest.main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_email/test_utils.py --- a/Lib/test/test_email/test_utils.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_email/test_utils.py Mon May 21 23:01:17 2012 -0400 @@ -1,7 +1,5 @@ import datetime from email import utils -import test.support -import time import unittest class DateTimeTests(unittest.TestCase): @@ -45,74 +43,3 @@ self.assertEqual( utils.parsedate_to_datetime(self.datestring + ' -0000'), self.naive_dt) - - -class LocaltimeTests(unittest.TestCase): - - def test_localtime_is_tz_aware_daylight_true(self): - test.support.patch(self, time, 'daylight', True) - t = utils.localtime() - self.assertIsNot(t.tzinfo, None) - - def test_localtime_is_tz_aware_daylight_false(self): - test.support.patch(self, time, 'daylight', False) - t = utils.localtime() - self.assertIsNot(t.tzinfo, None) - - def test_localtime_daylight_true_dst_false(self): - test.support.patch(self, time, 'daylight', True) - t0 = datetime.datetime(2012, 3, 12, 1, 1) - t1 = utils.localtime(t0, isdst=-1) - t2 = utils.localtime(t1) - self.assertEqual(t1, t2) - - def test_localtime_daylight_false_dst_false(self): - test.support.patch(self, time, 'daylight', False) - t0 = datetime.datetime(2012, 3, 12, 1, 1) - t1 = utils.localtime(t0, isdst=-1) - t2 = utils.localtime(t1) - self.assertEqual(t1, t2) - - def test_localtime_daylight_true_dst_true(self): - test.support.patch(self, time, 'daylight', True) - t0 = datetime.datetime(2012, 3, 12, 1, 1) - t1 = utils.localtime(t0, isdst=1) - t2 = utils.localtime(t1) - self.assertEqual(t1, t2) - - def test_localtime_daylight_false_dst_true(self): - test.support.patch(self, time, 'daylight', False) - t0 = datetime.datetime(2012, 3, 12, 1, 1) - t1 = utils.localtime(t0, isdst=1) - t2 = utils.localtime(t1) - self.assertEqual(t1, t2) - - def test_localtime_epoch_utc_daylight_true(self): - test.support.patch(self, time, 'daylight', True) - t0 = datetime.datetime(1970, 1, 1, tzinfo = datetime.timezone.utc) - t1 = utils.localtime(t0) - self.assertEqual(t0, t1) - - def test_localtime_epoch_utc_daylight_false(self): - test.support.patch(self, time, 'daylight', False) - t0 = datetime.datetime(1970, 1, 1, tzinfo = datetime.timezone.utc) - t1 = utils.localtime(t0) - self.assertEqual(t0, t1) - - def test_localtime_epoch_notz_daylight_true(self): - test.support.patch(self, time, 'daylight', True) - t0 = datetime.datetime(1970, 1, 1) - t1 = utils.localtime(t0) - t2 = utils.localtime(t0.replace(tzinfo=None)) - self.assertEqual(t1, t2) - - def test_localtime_epoch_notz_daylight_false(self): - test.support.patch(self, time, 'daylight', False) - t0 = datetime.datetime(1970, 1, 1) - t1 = utils.localtime(t0) - t2 = utils.localtime(t0.replace(tzinfo=None)) - self.assertEqual(t1, t2) - - -if __name__ == '__main__': - unittest.main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_frozen.py --- a/Lib/test/test_frozen.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_frozen.py Mon May 21 23:01:17 2012 -0400 @@ -7,7 +7,7 @@ class FrozenTests(unittest.TestCase): module_attrs = frozenset(['__builtins__', '__cached__', '__doc__', - '__loader__', '__name__', + '__file__', '__loader__', '__name__', '__package__']) package_attrs = frozenset(list(module_attrs) + ['__path__']) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_gzip.py --- a/Lib/test/test_gzip.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_gzip.py Mon May 21 23:01:17 2012 -0400 @@ -424,21 +424,8 @@ file_data = gzip.decompress(f.read()).decode("ascii") self.assertEqual(file_data, uncompressed_raw * 2) - def test_fileobj(self): - uncompressed_bytes = data1 * 50 - uncompressed_str = uncompressed_bytes.decode("ascii") - compressed = gzip.compress(uncompressed_bytes) - with gzip.open(io.BytesIO(compressed), "r") as f: - self.assertEqual(f.read(), uncompressed_bytes) - with gzip.open(io.BytesIO(compressed), "rb") as f: - self.assertEqual(f.read(), uncompressed_bytes) - with gzip.open(io.BytesIO(compressed), "rt") as f: - self.assertEqual(f.read(), uncompressed_str) - def test_bad_params(self): # Test invalid parameter combinations. - with self.assertRaises(TypeError): - gzip.open(123.456) with self.assertRaises(ValueError): gzip.open(self.filename, "wbt") with self.assertRaises(ValueError): diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_http_cookies.py --- a/Lib/test/test_http_cookies.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_http_cookies.py Mon May 21 23:01:17 2012 -0400 @@ -95,13 +95,13 @@ # loading 'expires' C = cookies.SimpleCookie() - C.load('Customer="W"; expires=Wed, 01 Jan 2010 00:00:00 GMT') + C.load('Customer="W"; expires=Wed, 01-Jan-2010 00:00:00 GMT') self.assertEqual(C['Customer']['expires'], - 'Wed, 01 Jan 2010 00:00:00 GMT') + 'Wed, 01-Jan-2010 00:00:00 GMT') C = cookies.SimpleCookie() - C.load('Customer="W"; expires=Wed, 01 Jan 98 00:00:00 GMT') + C.load('Customer="W"; expires=Wed, 01-Jan-98 00:00:00 GMT') self.assertEqual(C['Customer']['expires'], - 'Wed, 01 Jan 98 00:00:00 GMT') + 'Wed, 01-Jan-98 00:00:00 GMT') # 'max-age' C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"') diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_httplib.py --- a/Lib/test/test_httplib.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_httplib.py Mon May 21 23:01:17 2012 -0400 @@ -99,34 +99,6 @@ conn.request('POST', '/', body, headers) self.assertEqual(conn._buffer.count[header.lower()], 1) - def test_content_length_0(self): - - class ContentLengthChecker(list): - def __init__(self): - list.__init__(self) - self.content_length = None - def append(self, item): - kv = item.split(b':', 1) - if len(kv) > 1 and kv[0].lower() == b'content-length': - self.content_length = kv[1].strip() - list.append(self, item) - - # POST with empty body - conn = client.HTTPConnection('example.com') - conn.sock = FakeSocket(None) - conn._buffer = ContentLengthChecker() - conn.request('POST', '/', '') - self.assertEqual(conn._buffer.content_length, b'0', - 'Header Content-Length not set') - - # PUT request with empty body - conn = client.HTTPConnection('example.com') - conn.sock = FakeSocket(None) - conn._buffer = ContentLengthChecker() - conn.request('PUT', '/', '') - self.assertEqual(conn._buffer.content_length, b'0', - 'Header Content-Length not set') - def test_putheader(self): conn = client.HTTPConnection('example.com') conn.sock = FakeSocket(None) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_import.py --- a/Lib/test/test_import.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_import.py Mon May 21 23:01:17 2012 -0400 @@ -286,6 +286,12 @@ import test.support as y self.assertIs(y, test.support, y.__name__) + def test_import_initless_directory_warning(self): + with check_warnings(('', ImportWarning)): + # Just a random non-package directory we always expect to be + # somewhere in sys.path... + self.assertRaises(ImportError, __import__, "site-packages") + def test_import_by_filename(self): path = os.path.abspath(TESTFN) encoding = sys.getfilesystemencoding() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_io.py --- a/Lib/test/test_io.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_io.py Mon May 21 23:01:17 2012 -0400 @@ -19,21 +19,20 @@ # test both implementations. This file has lots of examples. ################################################################################ -import abc -import array -import errno -import locale import os -import pickle -import random -import signal import sys import time +import array +import random import unittest +import weakref +import abc +import signal +import errno import warnings -import weakref +import pickle +from itertools import cycle, count from collections import deque -from itertools import cycle, count from test import support import codecs @@ -644,19 +643,6 @@ with self.open("non-existent", "r", opener=opener) as f: self.assertEqual(f.read(), "egg\n") - def test_fileio_closefd(self): - # Issue #4841 - with self.open(__file__, 'rb') as f1, \ - self.open(__file__, 'rb') as f2: - fileio = self.FileIO(f1.fileno(), closefd=False) - # .__init__() must not close f1 - fileio.__init__(f2.fileno(), closefd=False) - f1.readline() - # .close() must not close f2 - fileio.close() - f2.readline() - - class CIOTest(IOTest): def test_IOBase_finalize(self): @@ -1882,24 +1868,6 @@ t.write("A\rB") self.assertEqual(r.getvalue(), b"XY\nZA\rB") - def test_default_encoding(self): - old_environ = dict(os.environ) - try: - # try to get a user preferred encoding different than the current - # locale encoding to check that TextIOWrapper() uses the current - # locale encoding and not the user preferred encoding - for key in ('LC_ALL', 'LANG', 'LC_CTYPE'): - if key in os.environ: - del os.environ[key] - - current_locale_encoding = locale.getpreferredencoding(False) - b = self.BytesIO() - t = self.TextIOWrapper(b) - self.assertEqual(t.encoding, current_locale_encoding) - finally: - os.environ.clear() - os.environ.update(old_environ) - def test_encoding(self): # Check the encoding attribute is always set, and valid b = self.BytesIO() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_ipaddress.py --- a/Lib/test/test_ipaddress.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1272 +0,0 @@ -# Copyright 2007 Google Inc. -# Licensed to PSF under a Contributor Agreement. - -"""Unittest for ipaddress module.""" - - -import unittest -import ipaddress - - -# Compatibility function to cast str to bytes objects -_cb = lambda bytestr: bytes(bytestr, 'charmap') - - -class IpaddrUnitTest(unittest.TestCase): - - def setUp(self): - self.ipv4_address = ipaddress.IPv4Address('1.2.3.4') - self.ipv4_interface = ipaddress.IPv4Interface('1.2.3.4/24') - self.ipv4_network = ipaddress.IPv4Network('1.2.3.0/24') - #self.ipv4_hostmask = ipaddress.IPv4Interface('10.0.0.1/0.255.255.255') - self.ipv6_address = ipaddress.IPv6Interface( - '2001:658:22a:cafe:200:0:0:1') - self.ipv6_interface = ipaddress.IPv6Interface( - '2001:658:22a:cafe:200:0:0:1/64') - self.ipv6_network = ipaddress.IPv6Network('2001:658:22a:cafe::/64') - - def testRepr(self): - self.assertEqual("IPv4Interface('1.2.3.4/32')", - repr(ipaddress.IPv4Interface('1.2.3.4'))) - self.assertEqual("IPv6Interface('::1/128')", - repr(ipaddress.IPv6Interface('::1'))) - - # issue57 - def testAddressIntMath(self): - self.assertEqual(ipaddress.IPv4Address('1.1.1.1') + 255, - ipaddress.IPv4Address('1.1.2.0')) - self.assertEqual(ipaddress.IPv4Address('1.1.1.1') - 256, - ipaddress.IPv4Address('1.1.0.1')) - self.assertEqual(ipaddress.IPv6Address('::1') + (2**16 - 2), - ipaddress.IPv6Address('::ffff')) - self.assertEqual(ipaddress.IPv6Address('::ffff') - (2**16 - 2), - ipaddress.IPv6Address('::1')) - - def testInvalidStrings(self): - def AssertInvalidIP(ip_str): - self.assertRaises(ValueError, ipaddress.ip_address, ip_str) - AssertInvalidIP("") - AssertInvalidIP("016.016.016.016") - AssertInvalidIP("016.016.016") - AssertInvalidIP("016.016") - AssertInvalidIP("016") - AssertInvalidIP("000.000.000.000") - AssertInvalidIP("000") - AssertInvalidIP("0x0a.0x0a.0x0a.0x0a") - AssertInvalidIP("0x0a.0x0a.0x0a") - AssertInvalidIP("0x0a.0x0a") - AssertInvalidIP("0x0a") - AssertInvalidIP("42.42.42.42.42") - AssertInvalidIP("42.42.42") - AssertInvalidIP("42.42") - AssertInvalidIP("42") - AssertInvalidIP("42..42.42") - AssertInvalidIP("42..42.42.42") - AssertInvalidIP("42.42.42.42.") - AssertInvalidIP("42.42.42.42...") - AssertInvalidIP(".42.42.42.42") - AssertInvalidIP("...42.42.42.42") - AssertInvalidIP("42.42.42.-0") - AssertInvalidIP("42.42.42.+0") - AssertInvalidIP(".") - AssertInvalidIP("...") - AssertInvalidIP("bogus") - AssertInvalidIP("bogus.com") - AssertInvalidIP("192.168.0.1.com") - AssertInvalidIP("12345.67899.-54321.-98765") - AssertInvalidIP("257.0.0.0") - AssertInvalidIP("42.42.42.-42") - AssertInvalidIP("3ffe::1.net") - AssertInvalidIP("3ffe::1::1") - AssertInvalidIP("1::2::3::4:5") - AssertInvalidIP("::7:6:5:4:3:2:") - AssertInvalidIP(":6:5:4:3:2:1::") - AssertInvalidIP("2001::db:::1") - AssertInvalidIP("FEDC:9878") - AssertInvalidIP("+1.+2.+3.4") - AssertInvalidIP("1.2.3.4e0") - AssertInvalidIP("::7:6:5:4:3:2:1:0") - AssertInvalidIP("7:6:5:4:3:2:1:0::") - AssertInvalidIP("9:8:7:6:5:4:3::2:1") - AssertInvalidIP("0:1:2:3::4:5:6:7") - AssertInvalidIP("3ffe:0:0:0:0:0:0:0:1") - AssertInvalidIP("3ffe::10000") - AssertInvalidIP("3ffe::goog") - AssertInvalidIP("3ffe::-0") - AssertInvalidIP("3ffe::+0") - AssertInvalidIP("3ffe::-1") - AssertInvalidIP(":") - AssertInvalidIP(":::") - AssertInvalidIP("::1.2.3") - AssertInvalidIP("::1.2.3.4.5") - AssertInvalidIP("::1.2.3.4:") - AssertInvalidIP("1.2.3.4::") - AssertInvalidIP("2001:db8::1:") - AssertInvalidIP(":2001:db8::1") - AssertInvalidIP(":1:2:3:4:5:6:7") - AssertInvalidIP("1:2:3:4:5:6:7:") - AssertInvalidIP(":1:2:3:4:5:6:") - - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv4Interface, '') - self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv4Interface, - 'google.com') - self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv4Interface, - '::1.2.3.4') - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv6Interface, '') - self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Interface, - 'google.com') - self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Interface, - '1.2.3.4') - self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Interface, - 'cafe:cafe::/128/190') - self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Interface, - '1234:axy::b') - self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Address, - '1234:axy::b') - self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Address, - '2001:db8:::1') - self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Address, - '2001:888888::1') - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv4Address(1)._ip_int_from_string, - '1.a.2.3') - self.assertEqual(False, ipaddress.IPv4Interface(1)._is_hostmask( - '1.a.2.3')) - self.assertRaises(ValueError, ipaddress.ip_interface, 'bogus') - self.assertRaises(ValueError, ipaddress.IPv4Address, '127.0.0.1/32') - self.assertRaises(ValueError, ipaddress.v4_int_to_packed, -1) - self.assertRaises(ValueError, ipaddress.v4_int_to_packed, - 2 ** ipaddress.IPV4LENGTH) - self.assertRaises(ValueError, ipaddress.v6_int_to_packed, -1) - self.assertRaises(ValueError, ipaddress.v6_int_to_packed, - 2 ** ipaddress.IPV6LENGTH) - - def testInternals(self): - first, last = ipaddress._find_address_range([ - ipaddress.IPv4Address('10.10.10.10'), - ipaddress.IPv4Address('10.10.10.12')]) - self.assertEqual(first, last) - self.assertEqual(0, ipaddress._get_prefix_length(2**32, 0, 32)) - self.assertEqual(128, ipaddress._count_righthand_zero_bits(0, 128)) - base_ip = ipaddress._BaseAddress('127.0.0.1') - try: - base_ip.version - self.fail('_BaseAddress.version didn\'t raise NotImplementedError') - except NotImplementedError: - pass - self.assertEqual("IPv4Network('1.2.3.0/24')", repr(self.ipv4_network)) - self.assertEqual('0x1020318', hex(self.ipv4_network)) - self.assertRaises(TypeError, self.ipv4_network.__eq__, object()) - - def testGetNetwork(self): - self.assertEqual(int(self.ipv4_network.network_address), 16909056) - self.assertEqual(str(self.ipv4_network.network_address), '1.2.3.0') - - self.assertEqual(int(self.ipv6_network.network_address), - 42540616829182469433403647294022090752) - self.assertEqual(str(self.ipv6_network.network_address), - '2001:658:22a:cafe::') - self.assertEqual(str(self.ipv6_network.hostmask), - '::ffff:ffff:ffff:ffff') - - def testBadVersionComparison(self): - # These should always raise TypeError - v4addr = ipaddress.ip_address('1.1.1.1') - v4net = ipaddress.ip_network('1.1.1.1') - v6addr = ipaddress.ip_address('::1') - v6net = ipaddress.ip_address('::1') - - self.assertRaises(TypeError, v4addr.__lt__, v6addr) - self.assertRaises(TypeError, v4addr.__gt__, v6addr) - self.assertRaises(TypeError, v4net.__lt__, v6net) - self.assertRaises(TypeError, v4net.__gt__, v6net) - - self.assertRaises(TypeError, v6addr.__lt__, v4addr) - self.assertRaises(TypeError, v6addr.__gt__, v4addr) - self.assertRaises(TypeError, v6net.__lt__, v4net) - self.assertRaises(TypeError, v6net.__gt__, v4net) - - def testMixedTypeComparison(self): - v4addr = ipaddress.ip_address('1.1.1.1') - v4net = ipaddress.ip_network('1.1.1.1/32') - v6addr = ipaddress.ip_address('::1') - v6net = ipaddress.ip_network('::1/128') - - self.assertFalse(v4net.__contains__(v6net)) - self.assertFalse(v6net.__contains__(v4net)) - - self.assertRaises(TypeError, lambda: v4addr < v4net) - self.assertRaises(TypeError, lambda: v4addr > v4net) - self.assertRaises(TypeError, lambda: v4net < v4addr) - self.assertRaises(TypeError, lambda: v4net > v4addr) - - self.assertRaises(TypeError, lambda: v6addr < v6net) - self.assertRaises(TypeError, lambda: v6addr > v6net) - self.assertRaises(TypeError, lambda: v6net < v6addr) - self.assertRaises(TypeError, lambda: v6net > v6addr) - - # with get_mixed_type_key, you can sort addresses and network. - self.assertEqual([v4addr, v4net], - sorted([v4net, v4addr], - key=ipaddress.get_mixed_type_key)) - self.assertEqual([v6addr, v6net], - sorted([v6net, v6addr], - key=ipaddress.get_mixed_type_key)) - self.assertEqual(NotImplemented, ipaddress.get_mixed_type_key(object)) - - def testIpFromInt(self): - self.assertEqual(self.ipv4_interface._ip, - ipaddress.IPv4Interface(16909060)._ip) - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv4Interface, 2**32) - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv4Interface, -1) - - ipv4 = ipaddress.ip_network('1.2.3.4') - ipv6 = ipaddress.ip_network('2001:658:22a:cafe:200:0:0:1') - self.assertEqual(ipv4, ipaddress.ip_network(int(ipv4))) - self.assertEqual(ipv6, ipaddress.ip_network(int(ipv6))) - - v6_int = 42540616829182469433547762482097946625 - self.assertEqual(self.ipv6_interface._ip, - ipaddress.IPv6Interface(v6_int)._ip) - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv6Interface, 2**128) - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv6Interface, -1) - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv6Network, 2**128) - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv6Network, -1) - - self.assertEqual(ipaddress.ip_network(self.ipv4_address._ip).version, - 4) - self.assertEqual(ipaddress.ip_network(self.ipv6_address._ip).version, - 6) - - def testIpFromPacked(self): - ip = ipaddress.ip_network - - self.assertEqual(self.ipv4_interface._ip, - ipaddress.ip_interface(_cb('\x01\x02\x03\x04'))._ip) - self.assertEqual(ip('255.254.253.252'), - ip(_cb('\xff\xfe\xfd\xfc'))) - self.assertRaises(ValueError, ipaddress.ip_network, _cb('\x00' * 3)) - self.assertRaises(ValueError, ipaddress.ip_network, _cb('\x00' * 5)) - self.assertEqual(self.ipv6_interface.ip, - ipaddress.ip_interface( - _cb('\x20\x01\x06\x58\x02\x2a\xca\xfe' - '\x02\x00\x00\x00\x00\x00\x00\x01')).ip) - self.assertEqual(ip('ffff:2:3:4:ffff::'), - ip(_cb('\xff\xff\x00\x02\x00\x03\x00\x04' + - '\xff\xff' + '\x00' * 6))) - self.assertEqual(ip('::'), - ip(_cb('\x00' * 16))) - self.assertRaises(ValueError, ip, _cb('\x00' * 15)) - self.assertRaises(ValueError, ip, _cb('\x00' * 17)) - - def testGetIp(self): - self.assertEqual(int(self.ipv4_interface.ip), 16909060) - self.assertEqual(str(self.ipv4_interface.ip), '1.2.3.4') - - self.assertEqual(int(self.ipv6_interface.ip), - 42540616829182469433547762482097946625) - self.assertEqual(str(self.ipv6_interface.ip), - '2001:658:22a:cafe:200::1') - - def testGetNetmask(self): - self.assertEqual(int(self.ipv4_network.netmask), 4294967040) - self.assertEqual(str(self.ipv4_network.netmask), '255.255.255.0') - self.assertEqual(int(self.ipv6_network.netmask), - 340282366920938463444927863358058659840) - self.assertEqual(self.ipv6_network.prefixlen, 64) - - def testZeroNetmask(self): - ipv4_zero_netmask = ipaddress.IPv4Interface('1.2.3.4/0') - self.assertEqual(int(ipv4_zero_netmask.network.netmask), 0) - self.assertTrue(ipv4_zero_netmask.network._is_valid_netmask( - str(0))) - self.assertTrue(ipv4_zero_netmask._is_valid_netmask('0')) - self.assertTrue(ipv4_zero_netmask._is_valid_netmask('0.0.0.0')) - self.assertFalse(ipv4_zero_netmask._is_valid_netmask('invalid')) - - ipv6_zero_netmask = ipaddress.IPv6Interface('::1/0') - self.assertEqual(int(ipv6_zero_netmask.network.netmask), 0) - self.assertTrue(ipv6_zero_netmask.network._is_valid_netmask( - str(0))) - - def testIPv4NetAndHostmasks(self): - net = self.ipv4_network - self.assertFalse(net._is_valid_netmask('invalid')) - self.assertTrue(net._is_valid_netmask('128.128.128.128')) - self.assertFalse(net._is_valid_netmask('128.128.128.127')) - self.assertFalse(net._is_valid_netmask('128.128.128.255')) - self.assertTrue(net._is_valid_netmask('255.128.128.128')) - - self.assertFalse(net._is_hostmask('invalid')) - self.assertTrue(net._is_hostmask('128.255.255.255')) - self.assertFalse(net._is_hostmask('255.255.255.255')) - self.assertFalse(net._is_hostmask('1.2.3.4')) - - net = ipaddress.IPv4Network('127.0.0.0/0.0.0.255') - self.assertEqual(24, net.prefixlen) - - def testGetBroadcast(self): - self.assertEqual(int(self.ipv4_network.broadcast_address), 16909311) - self.assertEqual(str(self.ipv4_network.broadcast_address), '1.2.3.255') - - self.assertEqual(int(self.ipv6_network.broadcast_address), - 42540616829182469451850391367731642367) - self.assertEqual(str(self.ipv6_network.broadcast_address), - '2001:658:22a:cafe:ffff:ffff:ffff:ffff') - - def testGetPrefixlen(self): - self.assertEqual(self.ipv4_interface.prefixlen, 24) - self.assertEqual(self.ipv6_interface.prefixlen, 64) - - def testGetSupernet(self): - self.assertEqual(self.ipv4_network.supernet().prefixlen, 23) - self.assertEqual(str(self.ipv4_network.supernet().network_address), - '1.2.2.0') - self.assertEqual( - ipaddress.IPv4Interface('0.0.0.0/0').network.supernet(), - ipaddress.IPv4Network('0.0.0.0/0')) - - self.assertEqual(self.ipv6_network.supernet().prefixlen, 63) - self.assertEqual(str(self.ipv6_network.supernet().network_address), - '2001:658:22a:cafe::') - self.assertEqual(ipaddress.IPv6Interface('::0/0').network.supernet(), - ipaddress.IPv6Network('::0/0')) - - def testGetSupernet3(self): - self.assertEqual(self.ipv4_network.supernet(3).prefixlen, 21) - self.assertEqual(str(self.ipv4_network.supernet(3).network_address), - '1.2.0.0') - - self.assertEqual(self.ipv6_network.supernet(3).prefixlen, 61) - self.assertEqual(str(self.ipv6_network.supernet(3).network_address), - '2001:658:22a:caf8::') - - def testGetSupernet4(self): - self.assertRaises(ValueError, self.ipv4_network.supernet, - prefixlen_diff=2, new_prefix=1) - self.assertRaises(ValueError, self.ipv4_network.supernet, - new_prefix=25) - self.assertEqual(self.ipv4_network.supernet(prefixlen_diff=2), - self.ipv4_network.supernet(new_prefix=22)) - - self.assertRaises(ValueError, self.ipv6_network.supernet, - prefixlen_diff=2, new_prefix=1) - self.assertRaises(ValueError, self.ipv6_network.supernet, - new_prefix=65) - self.assertEqual(self.ipv6_network.supernet(prefixlen_diff=2), - self.ipv6_network.supernet(new_prefix=62)) - - def testHosts(self): - hosts = list(self.ipv4_network.hosts()) - self.assertEqual(254, len(hosts)) - self.assertEqual(ipaddress.IPv4Address('1.2.3.1'), hosts[0]) - self.assertEqual(ipaddress.IPv4Address('1.2.3.254'), hosts[-1]) - - # special case where only 1 bit is left for address - self.assertEqual([ipaddress.IPv4Address('2.0.0.0'), - ipaddress.IPv4Address('2.0.0.1')], - list(ipaddress.ip_network('2.0.0.0/31').hosts())) - - def testFancySubnetting(self): - self.assertEqual(sorted(self.ipv4_network.subnets(prefixlen_diff=3)), - sorted(self.ipv4_network.subnets(new_prefix=27))) - self.assertRaises(ValueError, list, - self.ipv4_network.subnets(new_prefix=23)) - self.assertRaises(ValueError, list, - self.ipv4_network.subnets(prefixlen_diff=3, - new_prefix=27)) - self.assertEqual(sorted(self.ipv6_network.subnets(prefixlen_diff=4)), - sorted(self.ipv6_network.subnets(new_prefix=68))) - self.assertRaises(ValueError, list, - self.ipv6_network.subnets(new_prefix=63)) - self.assertRaises(ValueError, list, - self.ipv6_network.subnets(prefixlen_diff=4, - new_prefix=68)) - - def testGetSubnets(self): - self.assertEqual(list(self.ipv4_network.subnets())[0].prefixlen, 25) - self.assertEqual(str(list( - self.ipv4_network.subnets())[0].network_address), - '1.2.3.0') - self.assertEqual(str(list( - self.ipv4_network.subnets())[1].network_address), - '1.2.3.128') - - self.assertEqual(list(self.ipv6_network.subnets())[0].prefixlen, 65) - - def testGetSubnetForSingle32(self): - ip = ipaddress.IPv4Network('1.2.3.4/32') - subnets1 = [str(x) for x in ip.subnets()] - subnets2 = [str(x) for x in ip.subnets(2)] - self.assertEqual(subnets1, ['1.2.3.4/32']) - self.assertEqual(subnets1, subnets2) - - def testGetSubnetForSingle128(self): - ip = ipaddress.IPv6Network('::1/128') - subnets1 = [str(x) for x in ip.subnets()] - subnets2 = [str(x) for x in ip.subnets(2)] - self.assertEqual(subnets1, ['::1/128']) - self.assertEqual(subnets1, subnets2) - - def testSubnet2(self): - ips = [str(x) for x in self.ipv4_network.subnets(2)] - self.assertEqual( - ips, - ['1.2.3.0/26', '1.2.3.64/26', '1.2.3.128/26', '1.2.3.192/26']) - - ipsv6 = [str(x) for x in self.ipv6_network.subnets(2)] - self.assertEqual( - ipsv6, - ['2001:658:22a:cafe::/66', - '2001:658:22a:cafe:4000::/66', - '2001:658:22a:cafe:8000::/66', - '2001:658:22a:cafe:c000::/66']) - - def testSubnetFailsForLargeCidrDiff(self): - self.assertRaises(ValueError, list, - self.ipv4_interface.network.subnets(9)) - self.assertRaises(ValueError, list, - self.ipv4_network.subnets(9)) - self.assertRaises(ValueError, list, - self.ipv6_interface.network.subnets(65)) - self.assertRaises(ValueError, list, - self.ipv6_network.subnets(65)) - - def testSupernetFailsForLargeCidrDiff(self): - self.assertRaises(ValueError, - self.ipv4_interface.network.supernet, 25) - self.assertRaises(ValueError, - self.ipv6_interface.network.supernet, 65) - - def testSubnetFailsForNegativeCidrDiff(self): - self.assertRaises(ValueError, list, - self.ipv4_interface.network.subnets(-1)) - self.assertRaises(ValueError, list, - self.ipv4_network.subnets(-1)) - self.assertRaises(ValueError, list, - self.ipv6_interface.network.subnets(-1)) - self.assertRaises(ValueError, list, - self.ipv6_network.subnets(-1)) - - def testGetNum_Addresses(self): - self.assertEqual(self.ipv4_network.num_addresses, 256) - self.assertEqual(list(self.ipv4_network.subnets())[0].num_addresses, - 128) - self.assertEqual(self.ipv4_network.supernet().num_addresses, 512) - - self.assertEqual(self.ipv6_network.num_addresses, 18446744073709551616) - self.assertEqual(list(self.ipv6_network.subnets())[0].num_addresses, - 9223372036854775808) - self.assertEqual(self.ipv6_network.supernet().num_addresses, - 36893488147419103232) - - def testContains(self): - self.assertTrue(ipaddress.IPv4Interface('1.2.3.128/25') in - self.ipv4_network) - self.assertFalse(ipaddress.IPv4Interface('1.2.4.1/24') in - self.ipv4_network) - # We can test addresses and string as well. - addr1 = ipaddress.IPv4Address('1.2.3.37') - self.assertTrue(addr1 in self.ipv4_network) - # issue 61, bad network comparison on like-ip'd network objects - # with identical broadcast addresses. - self.assertFalse(ipaddress.IPv4Network('1.1.0.0/16').__contains__( - ipaddress.IPv4Network('1.0.0.0/15'))) - - def testBadAddress(self): - self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv4Interface, - 'poop') - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv4Interface, '1.2.3.256') - - self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Interface, - 'poopv6') - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv4Interface, '1.2.3.4/32/24') - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv4Network, '1.2.3.4/32/24') - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv4Interface, '10/8') - self.assertRaises(ipaddress.AddressValueError, - ipaddress.IPv6Interface, '10/8') - - - def testBadNetMask(self): - self.assertRaises(ipaddress.NetmaskValueError, - ipaddress.IPv4Interface, '1.2.3.4/') - self.assertRaises(ipaddress.NetmaskValueError, - ipaddress.IPv4Interface, '1.2.3.4/33') - self.assertRaises(ipaddress.NetmaskValueError, - ipaddress.IPv4Interface, '1.2.3.4/254.254.255.256') - self.assertRaises(ipaddress.NetmaskValueError, - ipaddress.IPv4Interface, '1.1.1.1/240.255.0.0') - self.assertRaises(ipaddress.NetmaskValueError, - ipaddress.IPv6Interface, '::1/') - self.assertRaises(ipaddress.NetmaskValueError, - ipaddress.IPv6Interface, '::1/129') - - def testNth(self): - self.assertEqual(str(self.ipv4_network[5]), '1.2.3.5') - self.assertRaises(IndexError, self.ipv4_network.__getitem__, 256) - - self.assertEqual(str(self.ipv6_network[5]), - '2001:658:22a:cafe::5') - - def testGetitem(self): - # http://code.google.com/p/ipaddr-py/issues/detail?id=15 - addr = ipaddress.IPv4Network('172.31.255.128/255.255.255.240') - self.assertEqual(28, addr.prefixlen) - addr_list = list(addr) - self.assertEqual('172.31.255.128', str(addr_list[0])) - self.assertEqual('172.31.255.128', str(addr[0])) - self.assertEqual('172.31.255.143', str(addr_list[-1])) - self.assertEqual('172.31.255.143', str(addr[-1])) - self.assertEqual(addr_list[-1], addr[-1]) - - def testEqual(self): - self.assertTrue(self.ipv4_interface == - ipaddress.IPv4Interface('1.2.3.4/24')) - self.assertFalse(self.ipv4_interface == - ipaddress.IPv4Interface('1.2.3.4/23')) - self.assertFalse(self.ipv4_interface == - ipaddress.IPv6Interface('::1.2.3.4/24')) - self.assertFalse(self.ipv4_interface == '') - self.assertFalse(self.ipv4_interface == []) - self.assertFalse(self.ipv4_interface == 2) - - self.assertTrue(self.ipv6_interface == - ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/64')) - self.assertFalse(self.ipv6_interface == - ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/63')) - self.assertFalse(self.ipv6_interface == - ipaddress.IPv4Interface('1.2.3.4/23')) - self.assertFalse(self.ipv6_interface == '') - self.assertFalse(self.ipv6_interface == []) - self.assertFalse(self.ipv6_interface == 2) - - def testNotEqual(self): - self.assertFalse(self.ipv4_interface != - ipaddress.IPv4Interface('1.2.3.4/24')) - self.assertTrue(self.ipv4_interface != - ipaddress.IPv4Interface('1.2.3.4/23')) - self.assertTrue(self.ipv4_interface != - ipaddress.IPv6Interface('::1.2.3.4/24')) - self.assertTrue(self.ipv4_interface != '') - self.assertTrue(self.ipv4_interface != []) - self.assertTrue(self.ipv4_interface != 2) - - self.assertTrue(self.ipv4_address != - ipaddress.IPv4Address('1.2.3.5')) - self.assertTrue(self.ipv4_address != '') - self.assertTrue(self.ipv4_address != []) - self.assertTrue(self.ipv4_address != 2) - - self.assertFalse(self.ipv6_interface != - ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/64')) - self.assertTrue(self.ipv6_interface != - ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/63')) - self.assertTrue(self.ipv6_interface != - ipaddress.IPv4Interface('1.2.3.4/23')) - self.assertTrue(self.ipv6_interface != '') - self.assertTrue(self.ipv6_interface != []) - self.assertTrue(self.ipv6_interface != 2) - - self.assertTrue(self.ipv6_address != - ipaddress.IPv4Address('1.2.3.4')) - self.assertTrue(self.ipv6_address != '') - self.assertTrue(self.ipv6_address != []) - self.assertTrue(self.ipv6_address != 2) - - def testSlash32Constructor(self): - self.assertEqual(str(ipaddress.IPv4Interface( - '1.2.3.4/255.255.255.255')), '1.2.3.4/32') - - def testSlash128Constructor(self): - self.assertEqual(str(ipaddress.IPv6Interface('::1/128')), - '::1/128') - - def testSlash0Constructor(self): - self.assertEqual(str(ipaddress.IPv4Interface('1.2.3.4/0.0.0.0')), - '1.2.3.4/0') - - def testCollapsing(self): - # test only IP addresses including some duplicates - ip1 = ipaddress.IPv4Address('1.1.1.0') - ip2 = ipaddress.IPv4Address('1.1.1.1') - ip3 = ipaddress.IPv4Address('1.1.1.2') - ip4 = ipaddress.IPv4Address('1.1.1.3') - ip5 = ipaddress.IPv4Address('1.1.1.4') - ip6 = ipaddress.IPv4Address('1.1.1.0') - # check that addreses are subsumed properly. - collapsed = ipaddress.collapse_addresses( - [ip1, ip2, ip3, ip4, ip5, ip6]) - self.assertEqual(list(collapsed), - [ipaddress.IPv4Network('1.1.1.0/30'), - ipaddress.IPv4Network('1.1.1.4/32')]) - - # test a mix of IP addresses and networks including some duplicates - ip1 = ipaddress.IPv4Address('1.1.1.0') - ip2 = ipaddress.IPv4Address('1.1.1.1') - ip3 = ipaddress.IPv4Address('1.1.1.2') - ip4 = ipaddress.IPv4Address('1.1.1.3') - #ip5 = ipaddress.IPv4Interface('1.1.1.4/30') - #ip6 = ipaddress.IPv4Interface('1.1.1.4/30') - # check that addreses are subsumed properly. - collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3, ip4]) - self.assertEqual(list(collapsed), - [ipaddress.IPv4Network('1.1.1.0/30')]) - - # test only IP networks - ip1 = ipaddress.IPv4Network('1.1.0.0/24') - ip2 = ipaddress.IPv4Network('1.1.1.0/24') - ip3 = ipaddress.IPv4Network('1.1.2.0/24') - ip4 = ipaddress.IPv4Network('1.1.3.0/24') - ip5 = ipaddress.IPv4Network('1.1.4.0/24') - # stored in no particular order b/c we want CollapseAddr to call - # [].sort - ip6 = ipaddress.IPv4Network('1.1.0.0/22') - # check that addreses are subsumed properly. - collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3, ip4, ip5, - ip6]) - self.assertEqual(list(collapsed), - [ipaddress.IPv4Network('1.1.0.0/22'), - ipaddress.IPv4Network('1.1.4.0/24')]) - - # test that two addresses are supernet'ed properly - collapsed = ipaddress.collapse_addresses([ip1, ip2]) - self.assertEqual(list(collapsed), - [ipaddress.IPv4Network('1.1.0.0/23')]) - - # test same IP networks - ip_same1 = ip_same2 = ipaddress.IPv4Network('1.1.1.1/32') - self.assertEqual(list(ipaddress.collapse_addresses( - [ip_same1, ip_same2])), - [ip_same1]) - - # test same IP addresses - ip_same1 = ip_same2 = ipaddress.IPv4Address('1.1.1.1') - self.assertEqual(list(ipaddress.collapse_addresses( - [ip_same1, ip_same2])), - [ipaddress.ip_network('1.1.1.1/32')]) - ip1 = ipaddress.IPv6Network('2001::/100') - ip2 = ipaddress.IPv6Network('2001::/120') - ip3 = ipaddress.IPv6Network('2001::/96') - # test that ipv6 addresses are subsumed properly. - collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3]) - self.assertEqual(list(collapsed), [ip3]) - - # the toejam test - addr_tuples = [ - (ipaddress.ip_address('1.1.1.1'), - ipaddress.ip_address('::1')), - (ipaddress.IPv4Network('1.1.0.0/24'), - ipaddress.IPv6Network('2001::/120')), - (ipaddress.IPv4Network('1.1.0.0/32'), - ipaddress.IPv6Network('2001::/128')), - ] - for ip1, ip2 in addr_tuples: - self.assertRaises(TypeError, ipaddress.collapse_addresses, - [ip1, ip2]) - - def testSummarizing(self): - #ip = ipaddress.ip_address - #ipnet = ipaddress.ip_network - summarize = ipaddress.summarize_address_range - ip1 = ipaddress.ip_address('1.1.1.0') - ip2 = ipaddress.ip_address('1.1.1.255') - - # summarize works only for IPv4 & IPv6 - class IPv7Address(ipaddress.IPv6Address): - @property - def version(self): - return 7 - ip_invalid1 = IPv7Address('::1') - ip_invalid2 = IPv7Address('::1') - self.assertRaises(ValueError, list, - summarize(ip_invalid1, ip_invalid2)) - # test that a summary over ip4 & ip6 fails - self.assertRaises(TypeError, list, - summarize(ip1, ipaddress.IPv6Address('::1'))) - # test a /24 is summarized properly - self.assertEqual(list(summarize(ip1, ip2))[0], - ipaddress.ip_network('1.1.1.0/24')) - # test an IPv4 range that isn't on a network byte boundary - ip2 = ipaddress.ip_address('1.1.1.8') - self.assertEqual(list(summarize(ip1, ip2)), - [ipaddress.ip_network('1.1.1.0/29'), - ipaddress.ip_network('1.1.1.8')]) - # all! - ip1 = ipaddress.IPv4Address(0) - ip2 = ipaddress.IPv4Address(ipaddress.IPv4Address._ALL_ONES) - self.assertEqual([ipaddress.IPv4Network('0.0.0.0/0')], - list(summarize(ip1, ip2))) - - ip1 = ipaddress.ip_address('1::') - ip2 = ipaddress.ip_address('1:ffff:ffff:ffff:ffff:ffff:ffff:ffff') - # test a IPv6 is sumamrized properly - self.assertEqual(list(summarize(ip1, ip2))[0], - ipaddress.ip_network('1::/16')) - # test an IPv6 range that isn't on a network byte boundary - ip2 = ipaddress.ip_address('2::') - self.assertEqual(list(summarize(ip1, ip2)), - [ipaddress.ip_network('1::/16'), - ipaddress.ip_network('2::/128')]) - - # test exception raised when first is greater than last - self.assertRaises(ValueError, list, - summarize(ipaddress.ip_address('1.1.1.0'), - ipaddress.ip_address('1.1.0.0'))) - # test exception raised when first and last aren't IP addresses - self.assertRaises(TypeError, list, - summarize(ipaddress.ip_network('1.1.1.0'), - ipaddress.ip_network('1.1.0.0'))) - self.assertRaises(TypeError, list, - summarize(ipaddress.ip_network('1.1.1.0'), - ipaddress.ip_network('1.1.0.0'))) - # test exception raised when first and last are not same version - self.assertRaises(TypeError, list, - summarize(ipaddress.ip_address('::'), - ipaddress.ip_network('1.1.0.0'))) - - def testAddressComparison(self): - self.assertTrue(ipaddress.ip_address('1.1.1.1') <= - ipaddress.ip_address('1.1.1.1')) - self.assertTrue(ipaddress.ip_address('1.1.1.1') <= - ipaddress.ip_address('1.1.1.2')) - self.assertTrue(ipaddress.ip_address('::1') <= - ipaddress.ip_address('::1')) - self.assertTrue(ipaddress.ip_address('::1') <= - ipaddress.ip_address('::2')) - - def testNetworkComparison(self): - # ip1 and ip2 have the same network address - ip1 = ipaddress.IPv4Network('1.1.1.0/24') - ip2 = ipaddress.IPv4Network('1.1.1.0/32') - ip3 = ipaddress.IPv4Network('1.1.2.0/24') - - self.assertTrue(ip1 < ip3) - self.assertTrue(ip3 > ip2) - - self.assertEqual(ip1.compare_networks(ip1), 0) - - # if addresses are the same, sort by netmask - self.assertEqual(ip1.compare_networks(ip2), -1) - self.assertEqual(ip2.compare_networks(ip1), 1) - - self.assertEqual(ip1.compare_networks(ip3), -1) - self.assertEqual(ip3.compare_networks(ip1), 1) - self.assertTrue(ip1._get_networks_key() < ip3._get_networks_key()) - - ip1 = ipaddress.IPv6Network('2001:2000::/96') - ip2 = ipaddress.IPv6Network('2001:2001::/96') - ip3 = ipaddress.IPv6Network('2001:ffff:2000::/96') - - self.assertTrue(ip1 < ip3) - self.assertTrue(ip3 > ip2) - self.assertEqual(ip1.compare_networks(ip3), -1) - self.assertTrue(ip1._get_networks_key() < ip3._get_networks_key()) - - # Test comparing different protocols. - # Should always raise a TypeError. - self.assertRaises(TypeError, - self.ipv4_network.compare_networks, - self.ipv6_network) - ipv6 = ipaddress.IPv6Interface('::/0') - ipv4 = ipaddress.IPv4Interface('0.0.0.0/0') - self.assertRaises(TypeError, ipv4.__lt__, ipv6) - self.assertRaises(TypeError, ipv4.__gt__, ipv6) - self.assertRaises(TypeError, ipv6.__lt__, ipv4) - self.assertRaises(TypeError, ipv6.__gt__, ipv4) - - # Regression test for issue 19. - ip1 = ipaddress.ip_network('10.1.2.128/25') - self.assertFalse(ip1 < ip1) - self.assertFalse(ip1 > ip1) - ip2 = ipaddress.ip_network('10.1.3.0/24') - self.assertTrue(ip1 < ip2) - self.assertFalse(ip2 < ip1) - self.assertFalse(ip1 > ip2) - self.assertTrue(ip2 > ip1) - ip3 = ipaddress.ip_network('10.1.3.0/25') - self.assertTrue(ip2 < ip3) - self.assertFalse(ip3 < ip2) - self.assertFalse(ip2 > ip3) - self.assertTrue(ip3 > ip2) - - # Regression test for issue 28. - ip1 = ipaddress.ip_network('10.10.10.0/31') - ip2 = ipaddress.ip_network('10.10.10.0') - ip3 = ipaddress.ip_network('10.10.10.2/31') - ip4 = ipaddress.ip_network('10.10.10.2') - sorted = [ip1, ip2, ip3, ip4] - unsorted = [ip2, ip4, ip1, ip3] - unsorted.sort() - self.assertEqual(sorted, unsorted) - unsorted = [ip4, ip1, ip3, ip2] - unsorted.sort() - self.assertEqual(sorted, unsorted) - self.assertRaises(TypeError, ip1.__lt__, - ipaddress.ip_address('10.10.10.0')) - self.assertRaises(TypeError, ip2.__lt__, - ipaddress.ip_address('10.10.10.0')) - - # <=, >= - self.assertTrue(ipaddress.ip_network('1.1.1.1') <= - ipaddress.ip_network('1.1.1.1')) - self.assertTrue(ipaddress.ip_network('1.1.1.1') <= - ipaddress.ip_network('1.1.1.2')) - self.assertFalse(ipaddress.ip_network('1.1.1.2') <= - ipaddress.ip_network('1.1.1.1')) - self.assertTrue(ipaddress.ip_network('::1') <= - ipaddress.ip_network('::1')) - self.assertTrue(ipaddress.ip_network('::1') <= - ipaddress.ip_network('::2')) - self.assertFalse(ipaddress.ip_network('::2') <= - ipaddress.ip_network('::1')) - - def testStrictNetworks(self): - self.assertRaises(ValueError, ipaddress.ip_network, '192.168.1.1/24') - self.assertRaises(ValueError, ipaddress.ip_network, '::1/120') - - def testOverlaps(self): - other = ipaddress.IPv4Network('1.2.3.0/30') - other2 = ipaddress.IPv4Network('1.2.2.0/24') - other3 = ipaddress.IPv4Network('1.2.2.64/26') - self.assertTrue(self.ipv4_network.overlaps(other)) - self.assertFalse(self.ipv4_network.overlaps(other2)) - self.assertTrue(other2.overlaps(other3)) - - def testEmbeddedIpv4(self): - ipv4_string = '192.168.0.1' - ipv4 = ipaddress.IPv4Interface(ipv4_string) - v4compat_ipv6 = ipaddress.IPv6Interface('::%s' % ipv4_string) - self.assertEqual(int(v4compat_ipv6.ip), int(ipv4.ip)) - v4mapped_ipv6 = ipaddress.IPv6Interface('::ffff:%s' % ipv4_string) - self.assertNotEqual(v4mapped_ipv6.ip, ipv4.ip) - self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Interface, - '2001:1.1.1.1:1.1.1.1') - - # Issue 67: IPv6 with embedded IPv4 address not recognized. - def testIPv6AddressTooLarge(self): - # RFC4291 2.5.5.2 - self.assertEqual(ipaddress.ip_address('::FFFF:192.0.2.1'), - ipaddress.ip_address('::FFFF:c000:201')) - # RFC4291 2.2 (part 3) x::d.d.d.d - self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1'), - ipaddress.ip_address('FFFF::c000:201')) - - def testIPVersion(self): - self.assertEqual(self.ipv4_address.version, 4) - self.assertEqual(self.ipv6_address.version, 6) - - def testMaxPrefixLength(self): - self.assertEqual(self.ipv4_interface.max_prefixlen, 32) - self.assertEqual(self.ipv6_interface.max_prefixlen, 128) - - def testPacked(self): - self.assertEqual(self.ipv4_address.packed, - _cb('\x01\x02\x03\x04')) - self.assertEqual(ipaddress.IPv4Interface('255.254.253.252').packed, - _cb('\xff\xfe\xfd\xfc')) - self.assertEqual(self.ipv6_address.packed, - _cb('\x20\x01\x06\x58\x02\x2a\xca\xfe' - '\x02\x00\x00\x00\x00\x00\x00\x01')) - self.assertEqual(ipaddress.IPv6Interface('ffff:2:3:4:ffff::').packed, - _cb('\xff\xff\x00\x02\x00\x03\x00\x04\xff\xff' - + '\x00' * 6)) - self.assertEqual(ipaddress.IPv6Interface('::1:0:0:0:0').packed, - _cb('\x00' * 6 + '\x00\x01' + '\x00' * 8)) - - def testIpStrFromPrefixlen(self): - ipv4 = ipaddress.IPv4Interface('1.2.3.4/24') - self.assertEqual(ipv4._ip_string_from_prefix(), '255.255.255.0') - self.assertEqual(ipv4._ip_string_from_prefix(28), '255.255.255.240') - - def testIpType(self): - ipv4net = ipaddress.ip_network('1.2.3.4') - ipv4addr = ipaddress.ip_address('1.2.3.4') - ipv6net = ipaddress.ip_network('::1.2.3.4') - ipv6addr = ipaddress.ip_address('::1.2.3.4') - self.assertEqual(ipaddress.IPv4Network, type(ipv4net)) - self.assertEqual(ipaddress.IPv4Address, type(ipv4addr)) - self.assertEqual(ipaddress.IPv6Network, type(ipv6net)) - self.assertEqual(ipaddress.IPv6Address, type(ipv6addr)) - - def testReservedIpv4(self): - # test networks - self.assertEqual(True, ipaddress.ip_interface( - '224.1.1.1/31').is_multicast) - self.assertEqual(False, ipaddress.ip_network('240.0.0.0').is_multicast) - self.assertEqual(True, ipaddress.ip_network('240.0.0.0').is_reserved) - - self.assertEqual(True, ipaddress.ip_interface( - '192.168.1.1/17').is_private) - self.assertEqual(False, ipaddress.ip_network('192.169.0.0').is_private) - self.assertEqual(True, ipaddress.ip_network( - '10.255.255.255').is_private) - self.assertEqual(False, ipaddress.ip_network('11.0.0.0').is_private) - self.assertEqual(False, ipaddress.ip_network('11.0.0.0').is_reserved) - self.assertEqual(True, ipaddress.ip_network( - '172.31.255.255').is_private) - self.assertEqual(False, ipaddress.ip_network('172.32.0.0').is_private) - self.assertEqual(True, - ipaddress.ip_network('169.254.1.0/24').is_link_local) - - self.assertEqual(True, - ipaddress.ip_interface( - '169.254.100.200/24').is_link_local) - self.assertEqual(False, - ipaddress.ip_interface( - '169.255.100.200/24').is_link_local) - - self.assertEqual(True, - ipaddress.ip_network( - '127.100.200.254/32').is_loopback) - self.assertEqual(True, ipaddress.ip_network( - '127.42.0.0/16').is_loopback) - self.assertEqual(False, ipaddress.ip_network('128.0.0.0').is_loopback) - - # test addresses - self.assertEqual(True, ipaddress.ip_address('0.0.0.0').is_unspecified) - self.assertEqual(True, ipaddress.ip_address('224.1.1.1').is_multicast) - self.assertEqual(False, ipaddress.ip_address('240.0.0.0').is_multicast) - self.assertEqual(True, ipaddress.ip_address('240.0.0.1').is_reserved) - self.assertEqual(False, - ipaddress.ip_address('239.255.255.255').is_reserved) - - self.assertEqual(True, ipaddress.ip_address('192.168.1.1').is_private) - self.assertEqual(False, ipaddress.ip_address('192.169.0.0').is_private) - self.assertEqual(True, ipaddress.ip_address( - '10.255.255.255').is_private) - self.assertEqual(False, ipaddress.ip_address('11.0.0.0').is_private) - self.assertEqual(True, ipaddress.ip_address( - '172.31.255.255').is_private) - self.assertEqual(False, ipaddress.ip_address('172.32.0.0').is_private) - - self.assertEqual(True, - ipaddress.ip_address('169.254.100.200').is_link_local) - self.assertEqual(False, - ipaddress.ip_address('169.255.100.200').is_link_local) - - self.assertEqual(True, - ipaddress.ip_address('127.100.200.254').is_loopback) - self.assertEqual(True, ipaddress.ip_address('127.42.0.0').is_loopback) - self.assertEqual(False, ipaddress.ip_address('128.0.0.0').is_loopback) - self.assertEqual(True, ipaddress.ip_network('0.0.0.0').is_unspecified) - - def testReservedIpv6(self): - - self.assertEqual(True, ipaddress.ip_network('ffff::').is_multicast) - self.assertEqual(True, ipaddress.ip_network(2**128 - 1).is_multicast) - self.assertEqual(True, ipaddress.ip_network('ff00::').is_multicast) - self.assertEqual(False, ipaddress.ip_network('fdff::').is_multicast) - - self.assertEqual(True, ipaddress.ip_network('fecf::').is_site_local) - self.assertEqual(True, ipaddress.ip_network( - 'feff:ffff:ffff:ffff::').is_site_local) - self.assertEqual(False, ipaddress.ip_network( - 'fbf:ffff::').is_site_local) - self.assertEqual(False, ipaddress.ip_network('ff00::').is_site_local) - - self.assertEqual(True, ipaddress.ip_network('fc00::').is_private) - self.assertEqual(True, ipaddress.ip_network( - 'fc00:ffff:ffff:ffff::').is_private) - self.assertEqual(False, ipaddress.ip_network('fbff:ffff::').is_private) - self.assertEqual(False, ipaddress.ip_network('fe00::').is_private) - - self.assertEqual(True, ipaddress.ip_network('fea0::').is_link_local) - self.assertEqual(True, ipaddress.ip_network( - 'febf:ffff::').is_link_local) - self.assertEqual(False, ipaddress.ip_network( - 'fe7f:ffff::').is_link_local) - self.assertEqual(False, ipaddress.ip_network('fec0::').is_link_local) - - self.assertEqual(True, ipaddress.ip_interface('0:0::0:01').is_loopback) - self.assertEqual(False, ipaddress.ip_interface('::1/127').is_loopback) - self.assertEqual(False, ipaddress.ip_network('::').is_loopback) - self.assertEqual(False, ipaddress.ip_network('::2').is_loopback) - - self.assertEqual(True, ipaddress.ip_network('0::0').is_unspecified) - self.assertEqual(False, ipaddress.ip_network('::1').is_unspecified) - self.assertEqual(False, ipaddress.ip_network('::/127').is_unspecified) - - # test addresses - self.assertEqual(True, ipaddress.ip_address('ffff::').is_multicast) - self.assertEqual(True, ipaddress.ip_address(2**128 - 1).is_multicast) - self.assertEqual(True, ipaddress.ip_address('ff00::').is_multicast) - self.assertEqual(False, ipaddress.ip_address('fdff::').is_multicast) - - self.assertEqual(True, ipaddress.ip_address('fecf::').is_site_local) - self.assertEqual(True, ipaddress.ip_address( - 'feff:ffff:ffff:ffff::').is_site_local) - self.assertEqual(False, ipaddress.ip_address( - 'fbf:ffff::').is_site_local) - self.assertEqual(False, ipaddress.ip_address('ff00::').is_site_local) - - self.assertEqual(True, ipaddress.ip_address('fc00::').is_private) - self.assertEqual(True, ipaddress.ip_address( - 'fc00:ffff:ffff:ffff::').is_private) - self.assertEqual(False, ipaddress.ip_address('fbff:ffff::').is_private) - self.assertEqual(False, ipaddress.ip_address('fe00::').is_private) - - self.assertEqual(True, ipaddress.ip_address('fea0::').is_link_local) - self.assertEqual(True, ipaddress.ip_address( - 'febf:ffff::').is_link_local) - self.assertEqual(False, ipaddress.ip_address( - 'fe7f:ffff::').is_link_local) - self.assertEqual(False, ipaddress.ip_address('fec0::').is_link_local) - - self.assertEqual(True, ipaddress.ip_address('0:0::0:01').is_loopback) - self.assertEqual(True, ipaddress.ip_address('::1').is_loopback) - self.assertEqual(False, ipaddress.ip_address('::2').is_loopback) - - self.assertEqual(True, ipaddress.ip_address('0::0').is_unspecified) - self.assertEqual(False, ipaddress.ip_address('::1').is_unspecified) - - # some generic IETF reserved addresses - self.assertEqual(True, ipaddress.ip_address('100::').is_reserved) - self.assertEqual(True, ipaddress.ip_network('4000::1/128').is_reserved) - - def testIpv4Mapped(self): - self.assertEqual( - ipaddress.ip_address('::ffff:192.168.1.1').ipv4_mapped, - ipaddress.ip_address('192.168.1.1')) - self.assertEqual(ipaddress.ip_address('::c0a8:101').ipv4_mapped, None) - self.assertEqual(ipaddress.ip_address('::ffff:c0a8:101').ipv4_mapped, - ipaddress.ip_address('192.168.1.1')) - - def testAddrExclude(self): - addr1 = ipaddress.ip_network('10.1.1.0/24') - addr2 = ipaddress.ip_network('10.1.1.0/26') - addr3 = ipaddress.ip_network('10.2.1.0/24') - addr4 = ipaddress.ip_address('10.1.1.0') - addr5 = ipaddress.ip_network('2001:db8::0/32') - self.assertEqual(sorted(list(addr1.address_exclude(addr2))), - [ipaddress.ip_network('10.1.1.64/26'), - ipaddress.ip_network('10.1.1.128/25')]) - self.assertRaises(ValueError, list, addr1.address_exclude(addr3)) - self.assertRaises(TypeError, list, addr1.address_exclude(addr4)) - self.assertRaises(TypeError, list, addr1.address_exclude(addr5)) - self.assertEqual(list(addr1.address_exclude(addr1)), []) - - def testHash(self): - self.assertEqual(hash(ipaddress.ip_interface('10.1.1.0/24')), - hash(ipaddress.ip_interface('10.1.1.0/24'))) - self.assertEqual(hash(ipaddress.ip_network('10.1.1.0/24')), - hash(ipaddress.ip_network('10.1.1.0/24'))) - self.assertEqual(hash(ipaddress.ip_address('10.1.1.0')), - hash(ipaddress.ip_address('10.1.1.0'))) - # i70 - self.assertEqual(hash(ipaddress.ip_address('1.2.3.4')), - hash(ipaddress.ip_address( - int(ipaddress.ip_address('1.2.3.4')._ip)))) - ip1 = ipaddress.ip_address('10.1.1.0') - ip2 = ipaddress.ip_address('1::') - dummy = {} - dummy[self.ipv4_address] = None - dummy[self.ipv6_address] = None - dummy[ip1] = None - dummy[ip2] = None - self.assertTrue(self.ipv4_address in dummy) - self.assertTrue(ip2 in dummy) - - def testIPBases(self): - net = self.ipv4_network - self.assertEqual('1.2.3.0/24', net.compressed) - self.assertEqual( - net._ip_int_from_prefix(24), - net._ip_int_from_prefix(None)) - net = self.ipv6_network - self.assertRaises(ValueError, net._string_from_ip_int, 2**128 + 1) - self.assertEqual( - self.ipv6_address._string_from_ip_int(self.ipv6_address._ip), - self.ipv6_address._string_from_ip_int(None)) - - def testIPv6NetworkHelpers(self): - net = self.ipv6_network - self.assertEqual('2001:658:22a:cafe::/64', net.with_prefixlen) - self.assertEqual('2001:658:22a:cafe::/ffff:ffff:ffff:ffff::', - net.with_netmask) - self.assertEqual('2001:658:22a:cafe::/::ffff:ffff:ffff:ffff', - net.with_hostmask) - self.assertEqual('2001:658:22a:cafe::/64', str(net)) - - def testIPv4NetworkHelpers(self): - net = self.ipv4_network - self.assertEqual('1.2.3.0/24', net.with_prefixlen) - self.assertEqual('1.2.3.0/255.255.255.0', net.with_netmask) - self.assertEqual('1.2.3.0/0.0.0.255', net.with_hostmask) - self.assertEqual('1.2.3.0/24', str(net)) - - def testCopyConstructor(self): - addr1 = ipaddress.ip_network('10.1.1.0/24') - addr2 = ipaddress.ip_network(addr1) - addr3 = ipaddress.ip_interface('2001:658:22a:cafe:200::1/64') - addr4 = ipaddress.ip_interface(addr3) - addr5 = ipaddress.IPv4Address('1.1.1.1') - addr6 = ipaddress.IPv6Address('2001:658:22a:cafe:200::1') - - self.assertEqual(addr1, addr2) - self.assertEqual(addr3, addr4) - self.assertEqual(addr5, ipaddress.IPv4Address(addr5)) - self.assertEqual(addr6, ipaddress.IPv6Address(addr6)) - - def testCompressIPv6Address(self): - test_addresses = { - '1:2:3:4:5:6:7:8': '1:2:3:4:5:6:7:8/128', - '2001:0:0:4:0:0:0:8': '2001:0:0:4::8/128', - '2001:0:0:4:5:6:7:8': '2001::4:5:6:7:8/128', - '2001:0:3:4:5:6:7:8': '2001:0:3:4:5:6:7:8/128', - '2001:0:3:4:5:6:7:8': '2001:0:3:4:5:6:7:8/128', - '0:0:3:0:0:0:0:ffff': '0:0:3::ffff/128', - '0:0:0:4:0:0:0:ffff': '::4:0:0:0:ffff/128', - '0:0:0:0:5:0:0:ffff': '::5:0:0:ffff/128', - '1:0:0:4:0:0:7:8': '1::4:0:0:7:8/128', - '0:0:0:0:0:0:0:0': '::/128', - '0:0:0:0:0:0:0:0/0': '::/0', - '0:0:0:0:0:0:0:1': '::1/128', - '2001:0658:022a:cafe:0000:0000:0000:0000/66': - '2001:658:22a:cafe::/66', - '::1.2.3.4': '::102:304/128', - '1:2:3:4:5:ffff:1.2.3.4': '1:2:3:4:5:ffff:102:304/128', - '::7:6:5:4:3:2:1': '0:7:6:5:4:3:2:1/128', - '::7:6:5:4:3:2:0': '0:7:6:5:4:3:2:0/128', - '7:6:5:4:3:2:1::': '7:6:5:4:3:2:1:0/128', - '0:6:5:4:3:2:1::': '0:6:5:4:3:2:1:0/128', - } - for uncompressed, compressed in list(test_addresses.items()): - self.assertEqual(compressed, str(ipaddress.IPv6Interface( - uncompressed))) - - def testExplodeShortHandIpStr(self): - addr1 = ipaddress.IPv6Interface('2001::1') - addr2 = ipaddress.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1') - addr3 = ipaddress.IPv6Network('2001::/96') - addr4 = ipaddress.IPv4Address('192.168.178.1') - self.assertEqual('2001:0000:0000:0000:0000:0000:0000:0001/128', - addr1.exploded) - self.assertEqual('0000:0000:0000:0000:0000:0000:0000:0001/128', - ipaddress.IPv6Interface('::1/128').exploded) - # issue 77 - self.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1', - addr2.exploded) - self.assertEqual('2001:0000:0000:0000:0000:0000:0000:0000/96', - addr3.exploded) - self.assertEqual('192.168.178.1', addr4.exploded) - - def testIntRepresentation(self): - self.assertEqual(16909060, int(self.ipv4_address)) - self.assertEqual(42540616829182469433547762482097946625, - int(self.ipv6_address)) - - def testHexRepresentation(self): - self.assertEqual(hex(0x1020304), - hex(self.ipv4_address)) - - self.assertEqual(hex(0x20010658022ACAFE0200000000000001), - hex(self.ipv6_address)) - - def testForceVersion(self): - self.assertEqual(ipaddress.ip_network(1).version, 4) - self.assertEqual(ipaddress.IPv6Network(1).version, 6) - - def testWithStar(self): - self.assertEqual(str(self.ipv4_interface.with_prefixlen), "1.2.3.4/24") - self.assertEqual(str(self.ipv4_interface.with_netmask), - "1.2.3.4/255.255.255.0") - self.assertEqual(str(self.ipv4_interface.with_hostmask), - "1.2.3.4/0.0.0.255") - - self.assertEqual(str(self.ipv6_interface.with_prefixlen), - '2001:658:22a:cafe:200::1/64') - # rfc3513 sec 2.3 says that ipv6 only uses cidr notation for - # subnets - self.assertEqual(str(self.ipv6_interface.with_netmask), - '2001:658:22a:cafe:200::1/64') - # this probably don't make much sense, but it's included for - # compatibility with ipv4 - self.assertEqual(str(self.ipv6_interface.with_hostmask), - '2001:658:22a:cafe:200::1/::ffff:ffff:ffff:ffff') - - def testNetworkElementCaching(self): - # V4 - make sure we're empty - self.assertFalse('network_address' in self.ipv4_network._cache) - self.assertFalse('broadcast_address' in self.ipv4_network._cache) - self.assertFalse('hostmask' in self.ipv4_network._cache) - - # V4 - populate and test - self.assertEqual(self.ipv4_network.network_address, - ipaddress.IPv4Address('1.2.3.0')) - self.assertEqual(self.ipv4_network.broadcast_address, - ipaddress.IPv4Address('1.2.3.255')) - self.assertEqual(self.ipv4_network.hostmask, - ipaddress.IPv4Address('0.0.0.255')) - - # V4 - check we're cached - self.assertTrue('broadcast_address' in self.ipv4_network._cache) - self.assertTrue('hostmask' in self.ipv4_network._cache) - - # V6 - make sure we're empty - self.assertFalse('broadcast_address' in self.ipv6_network._cache) - self.assertFalse('hostmask' in self.ipv6_network._cache) - - # V6 - populate and test - self.assertEqual(self.ipv6_network.network_address, - ipaddress.IPv6Address('2001:658:22a:cafe::')) - self.assertEqual(self.ipv6_interface.network.network_address, - ipaddress.IPv6Address('2001:658:22a:cafe::')) - - self.assertEqual( - self.ipv6_network.broadcast_address, - ipaddress.IPv6Address('2001:658:22a:cafe:ffff:ffff:ffff:ffff')) - self.assertEqual(self.ipv6_network.hostmask, - ipaddress.IPv6Address('::ffff:ffff:ffff:ffff')) - self.assertEqual( - self.ipv6_interface.network.broadcast_address, - ipaddress.IPv6Address('2001:658:22a:cafe:ffff:ffff:ffff:ffff')) - self.assertEqual(self.ipv6_interface.network.hostmask, - ipaddress.IPv6Address('::ffff:ffff:ffff:ffff')) - - # V6 - check we're cached - self.assertTrue('broadcast_address' in self.ipv6_network._cache) - self.assertTrue('hostmask' in self.ipv6_network._cache) - self.assertTrue( - 'broadcast_address' in self.ipv6_interface.network._cache) - self.assertTrue('hostmask' in self.ipv6_interface.network._cache) - - def testTeredo(self): - # stolen from wikipedia - server = ipaddress.IPv4Address('65.54.227.120') - client = ipaddress.IPv4Address('192.0.2.45') - teredo_addr = '2001:0000:4136:e378:8000:63bf:3fff:fdd2' - self.assertEqual((server, client), - ipaddress.ip_address(teredo_addr).teredo) - bad_addr = '2000::4136:e378:8000:63bf:3fff:fdd2' - self.assertFalse(ipaddress.ip_address(bad_addr).teredo) - bad_addr = '2001:0001:4136:e378:8000:63bf:3fff:fdd2' - self.assertFalse(ipaddress.ip_address(bad_addr).teredo) - - # i77 - teredo_addr = ipaddress.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1') - self.assertEqual((ipaddress.IPv4Address('94.245.121.253'), - ipaddress.IPv4Address('95.26.244.94')), - teredo_addr.teredo) - - def testsixtofour(self): - sixtofouraddr = ipaddress.ip_address('2002:ac1d:2d64::1') - bad_addr = ipaddress.ip_address('2000:ac1d:2d64::1') - self.assertEqual(ipaddress.IPv4Address('172.29.45.100'), - sixtofouraddr.sixtofour) - self.assertFalse(bad_addr.sixtofour) - - -if __name__ == '__main__': - unittest.main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_logging.py --- a/Lib/test/test_logging.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_logging.py Mon May 21 23:01:17 2012 -0400 @@ -582,7 +582,6 @@ self.assertFalse(h.shouldFlush(r)) h.close() - @unittest.skipIf(os.name == 'nt', 'WatchedFileHandler not appropriate for Windows.') @unittest.skipUnless(threading, 'Threading required for this test.') def test_race(self): # Issue #14632 refers. @@ -664,7 +663,6 @@ self.smtp_server = server self.conn = conn self.addr = addr - self.data_size_limit = None self.received_lines = [] self.smtp_state = self.COMMAND self.seen_greeting = '' @@ -684,7 +682,6 @@ return self.push('220 %s %s' % (self.fqdn, smtpd.__version__)) self.set_terminator(b'\r\n') - self.extended_smtp = False class TestSMTPServer(smtpd.SMTPServer): @@ -712,7 +709,6 @@ def __init__(self, addr, handler, poll_interval, sockmap): self._localaddr = addr self._remoteaddr = None - self.data_size_limit = None self.sockmap = sockmap asyncore.dispatcher.__init__(self, map=sockmap) try: diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_lzma.py --- a/Lib/test/test_lzma.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_lzma.py Mon May 21 23:01:17 2012 -0400 @@ -358,11 +358,11 @@ class FileTestCase(unittest.TestCase): def test_init(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: pass - with LZMAFile(BytesIO(), "w") as f: + with LZMAFile(fileobj=BytesIO(), mode="w") as f: pass - with LZMAFile(BytesIO(), "a") as f: + with LZMAFile(fileobj=BytesIO(), mode="a") as f: pass def test_init_with_filename(self): @@ -374,101 +374,90 @@ with LZMAFile(TESTFN, "a") as f: pass - def test_init_mode(self): - with TempFile(TESTFN): - with LZMAFile(TESTFN, "r"): - pass - with LZMAFile(TESTFN, "rb"): - pass - with LZMAFile(TESTFN, "w"): - pass - with LZMAFile(TESTFN, "wb"): - pass - with LZMAFile(TESTFN, "a"): - pass - with LZMAFile(TESTFN, "ab"): - pass - def test_init_bad_mode(self): with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), (3, "x")) + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), mode=(3, "x")) with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), "") + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), mode="") with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), "x") + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), mode="x") with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), "rt") + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), mode="rb") with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), "r+") + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), mode="r+") with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), "wt") + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), mode="wb") with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), "w+") + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), mode="w+") with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), "rw") + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), mode="rw") def test_init_bad_check(self): with self.assertRaises(TypeError): - LZMAFile(BytesIO(), "w", check=b"asd") + LZMAFile(fileobj=BytesIO(), mode="w", check=b"asd") # CHECK_UNKNOWN and anything above CHECK_ID_MAX should be invalid. with self.assertRaises(LZMAError): - LZMAFile(BytesIO(), "w", check=lzma.CHECK_UNKNOWN) + LZMAFile(fileobj=BytesIO(), mode="w", check=lzma.CHECK_UNKNOWN) with self.assertRaises(LZMAError): - LZMAFile(BytesIO(), "w", check=lzma.CHECK_ID_MAX + 3) + LZMAFile(fileobj=BytesIO(), mode="w", check=lzma.CHECK_ID_MAX + 3) # Cannot specify a check with mode="r". with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), check=lzma.CHECK_NONE) + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), check=lzma.CHECK_NONE) with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), check=lzma.CHECK_CRC32) + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), check=lzma.CHECK_CRC32) with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), check=lzma.CHECK_CRC64) + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), check=lzma.CHECK_CRC64) with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), check=lzma.CHECK_SHA256) + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), check=lzma.CHECK_SHA256) with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), check=lzma.CHECK_UNKNOWN) + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), check=lzma.CHECK_UNKNOWN) def test_init_bad_preset(self): with self.assertRaises(TypeError): - LZMAFile(BytesIO(), "w", preset=4.39) + LZMAFile(fileobj=BytesIO(), mode="w", preset=4.39) with self.assertRaises(LZMAError): - LZMAFile(BytesIO(), "w", preset=10) + LZMAFile(fileobj=BytesIO(), mode="w", preset=10) with self.assertRaises(LZMAError): - LZMAFile(BytesIO(), "w", preset=23) + LZMAFile(fileobj=BytesIO(), mode="w", preset=23) with self.assertRaises(OverflowError): - LZMAFile(BytesIO(), "w", preset=-1) + LZMAFile(fileobj=BytesIO(), mode="w", preset=-1) with self.assertRaises(OverflowError): - LZMAFile(BytesIO(), "w", preset=-7) + LZMAFile(fileobj=BytesIO(), mode="w", preset=-7) with self.assertRaises(TypeError): - LZMAFile(BytesIO(), "w", preset="foo") + LZMAFile(fileobj=BytesIO(), mode="w", preset="foo") # Cannot specify a preset with mode="r". with self.assertRaises(ValueError): - LZMAFile(BytesIO(COMPRESSED_XZ), preset=3) + LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), preset=3) def test_init_bad_filter_spec(self): with self.assertRaises(TypeError): - LZMAFile(BytesIO(), "w", filters=[b"wobsite"]) + LZMAFile(fileobj=BytesIO(), mode="w", filters=[b"wobsite"]) with self.assertRaises(ValueError): - LZMAFile(BytesIO(), "w", filters=[{"xyzzy": 3}]) + LZMAFile(fileobj=BytesIO(), mode="w", filters=[{"xyzzy": 3}]) with self.assertRaises(ValueError): - LZMAFile(BytesIO(), "w", filters=[{"id": 98765}]) + LZMAFile(fileobj=BytesIO(), mode="w", filters=[{"id": 98765}]) with self.assertRaises(ValueError): - LZMAFile(BytesIO(), "w", + LZMAFile(fileobj=BytesIO(), mode="w", filters=[{"id": lzma.FILTER_LZMA2, "foo": 0}]) with self.assertRaises(ValueError): - LZMAFile(BytesIO(), "w", + LZMAFile(fileobj=BytesIO(), mode="w", filters=[{"id": lzma.FILTER_DELTA, "foo": 0}]) with self.assertRaises(ValueError): - LZMAFile(BytesIO(), "w", + LZMAFile(fileobj=BytesIO(), mode="w", filters=[{"id": lzma.FILTER_X86, "foo": 0}]) def test_init_with_preset_and_filters(self): with self.assertRaises(ValueError): - LZMAFile(BytesIO(), "w", format=lzma.FORMAT_RAW, - preset=6, filters=FILTERS_RAW_1) + LZMAFile(fileobj=BytesIO(), mode="w", format=lzma.FORMAT_RAW, + preset=6, filters=FILTERS_RAW_1) + + def test_init_with_filename_and_fileobj(self): + with self.assertRaises(ValueError): + LZMAFile("/dev/null", fileobj=BytesIO()) def test_close(self): with BytesIO(COMPRESSED_XZ) as src: - f = LZMAFile(src) + f = LZMAFile(fileobj=src) f.close() # LZMAFile.close() should not close the underlying file object. self.assertFalse(src.closed) @@ -487,7 +476,7 @@ f.close() def test_closed(self): - f = LZMAFile(BytesIO(COMPRESSED_XZ)) + f = LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) try: self.assertFalse(f.closed) f.read() @@ -496,7 +485,7 @@ f.close() self.assertTrue(f.closed) - f = LZMAFile(BytesIO(), "w") + f = LZMAFile(fileobj=BytesIO(), mode="w") try: self.assertFalse(f.closed) finally: @@ -504,7 +493,7 @@ self.assertTrue(f.closed) def test_fileno(self): - f = LZMAFile(BytesIO(COMPRESSED_XZ)) + f = LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) try: self.assertRaises(UnsupportedOperation, f.fileno) finally: @@ -520,7 +509,7 @@ self.assertRaises(ValueError, f.fileno) def test_seekable(self): - f = LZMAFile(BytesIO(COMPRESSED_XZ)) + f = LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) try: self.assertTrue(f.seekable()) f.read() @@ -529,7 +518,7 @@ f.close() self.assertRaises(ValueError, f.seekable) - f = LZMAFile(BytesIO(), "w") + f = LZMAFile(fileobj=BytesIO(), mode="w") try: self.assertFalse(f.seekable()) finally: @@ -538,7 +527,7 @@ src = BytesIO(COMPRESSED_XZ) src.seekable = lambda: False - f = LZMAFile(src) + f = LZMAFile(fileobj=src) try: self.assertFalse(f.seekable()) finally: @@ -546,7 +535,7 @@ self.assertRaises(ValueError, f.seekable) def test_readable(self): - f = LZMAFile(BytesIO(COMPRESSED_XZ)) + f = LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) try: self.assertTrue(f.readable()) f.read() @@ -555,7 +544,7 @@ f.close() self.assertRaises(ValueError, f.readable) - f = LZMAFile(BytesIO(), "w") + f = LZMAFile(fileobj=BytesIO(), mode="w") try: self.assertFalse(f.readable()) finally: @@ -563,7 +552,7 @@ self.assertRaises(ValueError, f.readable) def test_writable(self): - f = LZMAFile(BytesIO(COMPRESSED_XZ)) + f = LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) try: self.assertFalse(f.writable()) f.read() @@ -572,7 +561,7 @@ f.close() self.assertRaises(ValueError, f.writable) - f = LZMAFile(BytesIO(), "w") + f = LZMAFile(fileobj=BytesIO(), mode="w") try: self.assertTrue(f.writable()) finally: @@ -580,46 +569,50 @@ self.assertRaises(ValueError, f.writable) def test_read(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: self.assertEqual(f.read(), INPUT) self.assertEqual(f.read(), b"") - with LZMAFile(BytesIO(COMPRESSED_ALONE)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_ALONE)) as f: self.assertEqual(f.read(), INPUT) - with LZMAFile(BytesIO(COMPRESSED_XZ), format=lzma.FORMAT_XZ) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), + format=lzma.FORMAT_XZ) as f: self.assertEqual(f.read(), INPUT) self.assertEqual(f.read(), b"") - with LZMAFile(BytesIO(COMPRESSED_ALONE), format=lzma.FORMAT_ALONE) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_ALONE), + format=lzma.FORMAT_ALONE) as f: self.assertEqual(f.read(), INPUT) self.assertEqual(f.read(), b"") - with LZMAFile(BytesIO(COMPRESSED_RAW_1), + with LZMAFile(fileobj=BytesIO(COMPRESSED_RAW_1), format=lzma.FORMAT_RAW, filters=FILTERS_RAW_1) as f: self.assertEqual(f.read(), INPUT) self.assertEqual(f.read(), b"") - with LZMAFile(BytesIO(COMPRESSED_RAW_2), + with LZMAFile(fileobj=BytesIO(COMPRESSED_RAW_2), format=lzma.FORMAT_RAW, filters=FILTERS_RAW_2) as f: self.assertEqual(f.read(), INPUT) self.assertEqual(f.read(), b"") - with LZMAFile(BytesIO(COMPRESSED_RAW_3), + with LZMAFile(fileobj=BytesIO(COMPRESSED_RAW_3), format=lzma.FORMAT_RAW, filters=FILTERS_RAW_3) as f: self.assertEqual(f.read(), INPUT) self.assertEqual(f.read(), b"") - with LZMAFile(BytesIO(COMPRESSED_RAW_4), + with LZMAFile(fileobj=BytesIO(COMPRESSED_RAW_4), format=lzma.FORMAT_RAW, filters=FILTERS_RAW_4) as f: self.assertEqual(f.read(), INPUT) self.assertEqual(f.read(), b"") def test_read_0(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: self.assertEqual(f.read(0), b"") - with LZMAFile(BytesIO(COMPRESSED_ALONE)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_ALONE)) as f: self.assertEqual(f.read(0), b"") - with LZMAFile(BytesIO(COMPRESSED_XZ), format=lzma.FORMAT_XZ) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), + format=lzma.FORMAT_XZ) as f: self.assertEqual(f.read(0), b"") - with LZMAFile(BytesIO(COMPRESSED_ALONE), format=lzma.FORMAT_ALONE) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_ALONE), + format=lzma.FORMAT_ALONE) as f: self.assertEqual(f.read(0), b"") def test_read_10(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: chunks = [] while True: result = f.read(10) @@ -630,11 +623,11 @@ self.assertEqual(b"".join(chunks), INPUT) def test_read_multistream(self): - with LZMAFile(BytesIO(COMPRESSED_XZ * 5)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ * 5)) as f: self.assertEqual(f.read(), INPUT * 5) - with LZMAFile(BytesIO(COMPRESSED_XZ + COMPRESSED_ALONE)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ + COMPRESSED_ALONE)) as f: self.assertEqual(f.read(), INPUT * 2) - with LZMAFile(BytesIO(COMPRESSED_RAW_3 * 4), + with LZMAFile(fileobj=BytesIO(COMPRESSED_RAW_3 * 4), format=lzma.FORMAT_RAW, filters=FILTERS_RAW_3) as f: self.assertEqual(f.read(), INPUT * 4) @@ -644,7 +637,7 @@ saved_buffer_size = lzma._BUFFER_SIZE lzma._BUFFER_SIZE = len(COMPRESSED_XZ) try: - with LZMAFile(BytesIO(COMPRESSED_XZ * 5)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ * 5)) as f: self.assertEqual(f.read(), INPUT * 5) finally: lzma._BUFFER_SIZE = saved_buffer_size @@ -656,20 +649,20 @@ self.assertEqual(f.read(), b"") def test_read_incomplete(self): - with LZMAFile(BytesIO(COMPRESSED_XZ[:128])) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ[:128])) as f: self.assertRaises(EOFError, f.read) def test_read_bad_args(self): - f = LZMAFile(BytesIO(COMPRESSED_XZ)) + f = LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) f.close() self.assertRaises(ValueError, f.read) - with LZMAFile(BytesIO(), "w") as f: + with LZMAFile(fileobj=BytesIO(), mode="w") as f: self.assertRaises(ValueError, f.read) - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: self.assertRaises(TypeError, f.read, None) def test_read1(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: blocks = [] while True: result = f.read1() @@ -680,11 +673,11 @@ self.assertEqual(f.read1(), b"") def test_read1_0(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: self.assertEqual(f.read1(0), b"") def test_read1_10(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: blocks = [] while True: result = f.read1(10) @@ -695,7 +688,7 @@ self.assertEqual(f.read1(), b"") def test_read1_multistream(self): - with LZMAFile(BytesIO(COMPRESSED_XZ * 5)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ * 5)) as f: blocks = [] while True: result = f.read1() @@ -706,76 +699,78 @@ self.assertEqual(f.read1(), b"") def test_read1_bad_args(self): - f = LZMAFile(BytesIO(COMPRESSED_XZ)) + f = LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) f.close() self.assertRaises(ValueError, f.read1) - with LZMAFile(BytesIO(), "w") as f: + with LZMAFile(fileobj=BytesIO(), mode="w") as f: self.assertRaises(ValueError, f.read1) - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: self.assertRaises(TypeError, f.read1, None) def test_peek(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: result = f.peek() self.assertGreater(len(result), 0) self.assertTrue(INPUT.startswith(result)) self.assertEqual(f.read(), INPUT) - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: result = f.peek(10) self.assertGreater(len(result), 0) self.assertTrue(INPUT.startswith(result)) self.assertEqual(f.read(), INPUT) def test_peek_bad_args(self): - with LZMAFile(BytesIO(), "w") as f: + with LZMAFile(fileobj=BytesIO(), mode="w") as f: self.assertRaises(ValueError, f.peek) def test_iterator(self): with BytesIO(INPUT) as f: lines = f.readlines() - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: self.assertListEqual(list(iter(f)), lines) - with LZMAFile(BytesIO(COMPRESSED_ALONE)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_ALONE)) as f: self.assertListEqual(list(iter(f)), lines) - with LZMAFile(BytesIO(COMPRESSED_XZ), format=lzma.FORMAT_XZ) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), + format=lzma.FORMAT_XZ) as f: self.assertListEqual(list(iter(f)), lines) - with LZMAFile(BytesIO(COMPRESSED_ALONE), format=lzma.FORMAT_ALONE) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_ALONE), + format=lzma.FORMAT_ALONE) as f: self.assertListEqual(list(iter(f)), lines) - with LZMAFile(BytesIO(COMPRESSED_RAW_2), + with LZMAFile(fileobj=BytesIO(COMPRESSED_RAW_2), format=lzma.FORMAT_RAW, filters=FILTERS_RAW_2) as f: self.assertListEqual(list(iter(f)), lines) def test_readline(self): with BytesIO(INPUT) as f: lines = f.readlines() - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: for line in lines: self.assertEqual(f.readline(), line) def test_readlines(self): with BytesIO(INPUT) as f: lines = f.readlines() - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: self.assertListEqual(f.readlines(), lines) def test_write(self): with BytesIO() as dst: - with LZMAFile(dst, "w") as f: + with LZMAFile(fileobj=dst, mode="w") as f: f.write(INPUT) expected = lzma.compress(INPUT) self.assertEqual(dst.getvalue(), expected) with BytesIO() as dst: - with LZMAFile(dst, "w", format=lzma.FORMAT_XZ) as f: + with LZMAFile(fileobj=dst, mode="w", format=lzma.FORMAT_XZ) as f: f.write(INPUT) expected = lzma.compress(INPUT, format=lzma.FORMAT_XZ) self.assertEqual(dst.getvalue(), expected) with BytesIO() as dst: - with LZMAFile(dst, "w", format=lzma.FORMAT_ALONE) as f: + with LZMAFile(fileobj=dst, mode="w", format=lzma.FORMAT_ALONE) as f: f.write(INPUT) expected = lzma.compress(INPUT, format=lzma.FORMAT_ALONE) self.assertEqual(dst.getvalue(), expected) with BytesIO() as dst: - with LZMAFile(dst, "w", format=lzma.FORMAT_RAW, + with LZMAFile(fileobj=dst, mode="w", format=lzma.FORMAT_RAW, filters=FILTERS_RAW_2) as f: f.write(INPUT) expected = lzma.compress(INPUT, format=lzma.FORMAT_RAW, @@ -784,7 +779,7 @@ def test_write_10(self): with BytesIO() as dst: - with LZMAFile(dst, "w") as f: + with LZMAFile(fileobj=dst, mode="w") as f: for start in range(0, len(INPUT), 10): f.write(INPUT[start:start+10]) expected = lzma.compress(INPUT) @@ -796,11 +791,11 @@ part3 = INPUT[1536:] expected = b"".join(lzma.compress(x) for x in (part1, part2, part3)) with BytesIO() as dst: - with LZMAFile(dst, "w") as f: + with LZMAFile(fileobj=dst, mode="w") as f: f.write(part1) - with LZMAFile(dst, "a") as f: + with LZMAFile(fileobj=dst, mode="a") as f: f.write(part2) - with LZMAFile(dst, "a") as f: + with LZMAFile(fileobj=dst, mode="a") as f: f.write(part3) self.assertEqual(dst.getvalue(), expected) @@ -832,12 +827,12 @@ unlink(TESTFN) def test_write_bad_args(self): - f = LZMAFile(BytesIO(), "w") + f = LZMAFile(fileobj=BytesIO(), mode="w") f.close() self.assertRaises(ValueError, f.write, b"foo") - with LZMAFile(BytesIO(COMPRESSED_XZ), "r") as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ), mode="r") as f: self.assertRaises(ValueError, f.write, b"bar") - with LZMAFile(BytesIO(), "w") as f: + with LZMAFile(fileobj=BytesIO(), mode="w") as f: self.assertRaises(TypeError, f.write, None) self.assertRaises(TypeError, f.write, "text") self.assertRaises(TypeError, f.write, 789) @@ -846,75 +841,75 @@ with BytesIO(INPUT) as f: lines = f.readlines() with BytesIO() as dst: - with LZMAFile(dst, "w") as f: + with LZMAFile(fileobj=dst, mode="w") as f: f.writelines(lines) expected = lzma.compress(INPUT) self.assertEqual(dst.getvalue(), expected) def test_seek_forward(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: f.seek(555) self.assertEqual(f.read(), INPUT[555:]) def test_seek_forward_across_streams(self): - with LZMAFile(BytesIO(COMPRESSED_XZ * 2)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ * 2)) as f: f.seek(len(INPUT) + 123) self.assertEqual(f.read(), INPUT[123:]) def test_seek_forward_relative_to_current(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: f.read(100) f.seek(1236, 1) self.assertEqual(f.read(), INPUT[1336:]) def test_seek_forward_relative_to_end(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: f.seek(-555, 2) self.assertEqual(f.read(), INPUT[-555:]) def test_seek_backward(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: f.read(1001) f.seek(211) self.assertEqual(f.read(), INPUT[211:]) def test_seek_backward_across_streams(self): - with LZMAFile(BytesIO(COMPRESSED_XZ * 2)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ * 2)) as f: f.read(len(INPUT) + 333) f.seek(737) self.assertEqual(f.read(), INPUT[737:] + INPUT) def test_seek_backward_relative_to_end(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: f.seek(-150, 2) self.assertEqual(f.read(), INPUT[-150:]) def test_seek_past_end(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: f.seek(len(INPUT) + 9001) self.assertEqual(f.tell(), len(INPUT)) self.assertEqual(f.read(), b"") def test_seek_past_start(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: f.seek(-88) self.assertEqual(f.tell(), 0) self.assertEqual(f.read(), INPUT) def test_seek_bad_args(self): - f = LZMAFile(BytesIO(COMPRESSED_XZ)) + f = LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) f.close() self.assertRaises(ValueError, f.seek, 0) - with LZMAFile(BytesIO(), "w") as f: + with LZMAFile(fileobj=BytesIO(), mode="w") as f: self.assertRaises(ValueError, f.seek, 0) - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: self.assertRaises(ValueError, f.seek, 0, 3) self.assertRaises(ValueError, f.seek, 9, ()) self.assertRaises(TypeError, f.seek, None) self.assertRaises(TypeError, f.seek, b"derp") def test_tell(self): - with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: + with LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) as f: pos = 0 while True: self.assertEqual(f.tell(), pos) @@ -923,118 +918,18 @@ break pos += len(result) self.assertEqual(f.tell(), len(INPUT)) - with LZMAFile(BytesIO(), "w") as f: + with LZMAFile(fileobj=BytesIO(), mode="w") as f: for pos in range(0, len(INPUT), 144): self.assertEqual(f.tell(), pos) f.write(INPUT[pos:pos+144]) self.assertEqual(f.tell(), len(INPUT)) def test_tell_bad_args(self): - f = LZMAFile(BytesIO(COMPRESSED_XZ)) + f = LZMAFile(fileobj=BytesIO(COMPRESSED_XZ)) f.close() self.assertRaises(ValueError, f.tell) -class OpenTestCase(unittest.TestCase): - - def test_binary_modes(self): - with lzma.open(BytesIO(COMPRESSED_XZ), "rb") as f: - self.assertEqual(f.read(), INPUT) - with BytesIO() as bio: - with lzma.open(bio, "wb") as f: - f.write(INPUT) - file_data = lzma.decompress(bio.getvalue()) - self.assertEqual(file_data, INPUT) - with lzma.open(bio, "ab") as f: - f.write(INPUT) - file_data = lzma.decompress(bio.getvalue()) - self.assertEqual(file_data, INPUT * 2) - - def test_text_modes(self): - uncompressed = INPUT.decode("ascii") - uncompressed_raw = uncompressed.replace("\n", os.linesep) - with lzma.open(BytesIO(COMPRESSED_XZ), "rt") as f: - self.assertEqual(f.read(), uncompressed) - with BytesIO() as bio: - with lzma.open(bio, "wt") as f: - f.write(uncompressed) - file_data = lzma.decompress(bio.getvalue()).decode("ascii") - self.assertEqual(file_data, uncompressed_raw) - with lzma.open(bio, "at") as f: - f.write(uncompressed) - file_data = lzma.decompress(bio.getvalue()).decode("ascii") - self.assertEqual(file_data, uncompressed_raw * 2) - - def test_filename(self): - with TempFile(TESTFN): - with lzma.open(TESTFN, "wb") as f: - f.write(INPUT) - with open(TESTFN, "rb") as f: - file_data = lzma.decompress(f.read()) - self.assertEqual(file_data, INPUT) - with lzma.open(TESTFN, "rb") as f: - self.assertEqual(f.read(), INPUT) - with lzma.open(TESTFN, "ab") as f: - f.write(INPUT) - with lzma.open(TESTFN, "rb") as f: - self.assertEqual(f.read(), INPUT * 2) - - def test_bad_params(self): - # Test invalid parameter combinations. - with self.assertRaises(ValueError): - lzma.open(TESTFN, "") - with self.assertRaises(ValueError): - lzma.open(TESTFN, "x") - with self.assertRaises(ValueError): - lzma.open(TESTFN, "rbt") - with self.assertRaises(ValueError): - lzma.open(TESTFN, "rb", encoding="utf-8") - with self.assertRaises(ValueError): - lzma.open(TESTFN, "rb", errors="ignore") - with self.assertRaises(ValueError): - lzma.open(TESTFN, "rb", newline="\n") - - def test_format_and_filters(self): - # Test non-default format and filter chain. - options = {"format": lzma.FORMAT_RAW, "filters": FILTERS_RAW_1} - with lzma.open(BytesIO(COMPRESSED_RAW_1), "rb", **options) as f: - self.assertEqual(f.read(), INPUT) - with BytesIO() as bio: - with lzma.open(bio, "wb", **options) as f: - f.write(INPUT) - file_data = lzma.decompress(bio.getvalue(), **options) - self.assertEqual(file_data, INPUT) - - def test_encoding(self): - # Test non-default encoding. - uncompressed = INPUT.decode("ascii") - uncompressed_raw = uncompressed.replace("\n", os.linesep) - with BytesIO() as bio: - with lzma.open(bio, "wt", encoding="utf-16-le") as f: - f.write(uncompressed) - file_data = lzma.decompress(bio.getvalue()).decode("utf-16-le") - self.assertEqual(file_data, uncompressed_raw) - bio.seek(0) - with lzma.open(bio, "rt", encoding="utf-16-le") as f: - self.assertEqual(f.read(), uncompressed) - - def test_encoding_error_handler(self): - # Test wih non-default encoding error handler. - with BytesIO(lzma.compress(b"foo\xffbar")) as bio: - with lzma.open(bio, "rt", encoding="ascii", errors="ignore") as f: - self.assertEqual(f.read(), "foobar") - - def test_newline(self): - # Test with explicit newline (universal newline mode disabled). - text = INPUT.decode("ascii") - with BytesIO() as bio: - with lzma.open(bio, "wt", newline="\n") as f: - f.write(text) - bio.seek(0) - with lzma.open(bio, "rt", newline="\r") as f: - self.assertEqual(f.readlines(), [text]) - - class MiscellaneousTestCase(unittest.TestCase): def test_is_check_supported(self): @@ -1485,7 +1380,6 @@ CompressorDecompressorTestCase, CompressDecompressFunctionTestCase, FileTestCase, - OpenTestCase, MiscellaneousTestCase, ) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_memoryview.py --- a/Lib/test/test_memoryview.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_memoryview.py Mon May 21 23:01:17 2012 -0400 @@ -336,21 +336,6 @@ m = self._view(b) self.assertRaises(ValueError, hash, m) - def test_weakref(self): - # Check memoryviews are weakrefable - for tp in self._types: - b = tp(self._source) - m = self._view(b) - L = [] - def callback(wr, b=b): - L.append(b) - wr = weakref.ref(m, callback) - self.assertIs(wr(), m) - del m - test.support.gc_collect() - self.assertIs(wr(), None) - self.assertIs(L[0], b) - # Variations on source objects for the buffer: bytes-like objects, then arrays # with itemsize > 1. # NOTE: support for multi-dimensional objects is unimplemented. diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_module.py --- a/Lib/test/test_module.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_module.py Mon May 21 23:01:17 2012 -0400 @@ -5,15 +5,6 @@ import sys ModuleType = type(sys) -class FullLoader: - @classmethod - def module_repr(cls, m): - return "".format(m.__name__) - -class BareLoader: - pass - - class ModuleTests(unittest.TestCase): def test_uninitialized(self): # An uninitialized module has no __dict__ or __name__, @@ -89,90 +80,8 @@ gc_collect() self.assertEqual(destroyed, [1]) - def test_module_repr_minimal(self): - # reprs when modules have no __file__, __name__, or __loader__ - m = ModuleType('foo') - del m.__name__ - self.assertEqual(repr(m), "") - - def test_module_repr_with_name(self): - m = ModuleType('foo') - self.assertEqual(repr(m), "") - - def test_module_repr_with_name_and_filename(self): - m = ModuleType('foo') - m.__file__ = '/tmp/foo.py' - self.assertEqual(repr(m), "") - - def test_module_repr_with_filename_only(self): - m = ModuleType('foo') - del m.__name__ - m.__file__ = '/tmp/foo.py' - self.assertEqual(repr(m), "") - - def test_module_repr_with_bare_loader_but_no_name(self): - m = ModuleType('foo') - del m.__name__ - # Yes, a class not an instance. - m.__loader__ = BareLoader - self.assertEqual( - repr(m), ")>") - - def test_module_repr_with_full_loader_but_no_name(self): - # m.__loader__.module_repr() will fail because the module has no - # m.__name__. This exception will get suppressed and instead the - # loader's repr will be used. - m = ModuleType('foo') - del m.__name__ - # Yes, a class not an instance. - m.__loader__ = FullLoader - self.assertEqual( - repr(m), ")>") - - def test_module_repr_with_bare_loader(self): - m = ModuleType('foo') - # Yes, a class not an instance. - m.__loader__ = BareLoader - self.assertEqual( - repr(m), ")>") - - def test_module_repr_with_full_loader(self): - m = ModuleType('foo') - # Yes, a class not an instance. - m.__loader__ = FullLoader - self.assertEqual( - repr(m), "") - - def test_module_repr_with_bare_loader_and_filename(self): - # Because the loader has no module_repr(), use the file name. - m = ModuleType('foo') - # Yes, a class not an instance. - m.__loader__ = BareLoader - m.__file__ = '/tmp/foo.py' - self.assertEqual(repr(m), "") - - def test_module_repr_with_full_loader_and_filename(self): - # Even though the module has an __file__, use __loader__.module_repr() - m = ModuleType('foo') - # Yes, a class not an instance. - m.__loader__ = FullLoader - m.__file__ = '/tmp/foo.py' - self.assertEqual(repr(m), "") - - def test_module_repr_builtin(self): - self.assertEqual(repr(sys), "") - - def test_module_repr_source(self): - r = repr(unittest) - self.assertEqual(r[:25], "") - - # frozen and namespace module reprs are tested in importlib. - - def test_main(): run_unittest(ModuleTests) - if __name__ == '__main__': test_main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_multiprocessing.py --- a/Lib/test/test_multiprocessing.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_multiprocessing.py Mon May 21 23:01:17 2012 -0400 @@ -439,36 +439,6 @@ 1/0 # MARKER - @classmethod - def _test_sys_exit(cls, reason, testfn): - sys.stderr = open(testfn, 'w') - sys.exit(reason) - - def test_sys_exit(self): - # See Issue 13854 - if self.TYPE == 'threads': - return - - testfn = test.support.TESTFN - self.addCleanup(test.support.unlink, testfn) - - for reason, code in (([1, 2, 3], 1), ('ignore this', 0)): - p = self.Process(target=self._test_sys_exit, args=(reason, testfn)) - p.daemon = True - p.start() - p.join(5) - self.assertEqual(p.exitcode, code) - - with open(testfn, 'r') as f: - self.assertEqual(f.read().rstrip(), str(reason)) - - for reason in (True, False, 8): - p = self.Process(target=sys.exit, args=(reason,)) - p.daemon = True - p.start() - p.join(5) - self.assertEqual(p.exitcode, reason) - # # # @@ -986,34 +956,6 @@ p.join(5) self.assertTrue(success.value) - @classmethod - def _test_wait_result(cls, c, pid): - with c: - c.notify() - time.sleep(1) - if pid is not None: - os.kill(pid, signal.SIGINT) - - def test_wait_result(self): - if isinstance(self, ProcessesMixin) and sys.platform != 'win32': - pid = os.getpid() - else: - pid = None - - c = self.Condition() - with c: - self.assertFalse(c.wait(0)) - self.assertFalse(c.wait(0.1)) - - p = self.Process(target=self._test_wait_result, args=(c, pid)) - p.start() - - self.assertTrue(c.wait(10)) - if pid is not None: - self.assertRaises(KeyboardInterrupt, c.wait, 10) - - p.join() - class _TestEvent(BaseTestCase): @@ -1372,18 +1314,6 @@ join() self.assertLess(join.elapsed, 0.5) - def test_empty_iterable(self): - # See Issue 12157 - p = self.Pool(1) - - self.assertEqual(p.map(sqr, []), []) - self.assertEqual(list(p.imap(sqr, [])), []) - self.assertEqual(list(p.imap_unordered(sqr, [])), []) - self.assertEqual(p.map_async(sqr, []).get(), []) - - p.close() - p.join() - def raising(): raise KeyError("key") @@ -2529,7 +2459,7 @@ 'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 'Event', 'Value', 'Array', 'RawValue', 'RawArray', 'current_process', 'active_children', 'Pipe', - 'connection', 'JoinableQueue', 'Pool' + 'connection', 'JoinableQueue' ))) testcases_processes = create_test_cases(ProcessesMixin, type='processes') @@ -2543,7 +2473,7 @@ locals().update(get_attributes(manager, ( 'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 'Event', 'Value', 'Array', 'list', 'dict', - 'Namespace', 'JoinableQueue', 'Pool' + 'Namespace', 'JoinableQueue' ))) testcases_manager = create_test_cases(ManagerMixin, type='manager') @@ -2557,7 +2487,7 @@ 'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Condition', 'Event', 'Value', 'Array', 'current_process', 'active_children', 'Pipe', 'connection', 'dict', 'list', - 'Namespace', 'JoinableQueue', 'Pool' + 'Namespace', 'JoinableQueue' ))) testcases_threads = create_test_cases(ThreadsMixin, type='threads') @@ -2884,41 +2814,8 @@ with self.assertRaises(ValueError): multiprocessing.connection.Listener('/var/test.pipe') -# -# Issue 12098: check sys.flags of child matches that for parent -# - -class TestFlags(unittest.TestCase): - @classmethod - def run_in_grandchild(cls, conn): - conn.send(tuple(sys.flags)) - - @classmethod - def run_in_child(cls): - import json - r, w = multiprocessing.Pipe(duplex=False) - p = multiprocessing.Process(target=cls.run_in_grandchild, args=(w,)) - p.start() - grandchild_flags = r.recv() - p.join() - r.close() - w.close() - flags = (tuple(sys.flags), grandchild_flags) - print(json.dumps(flags)) - - def test_flags(self): - import json, subprocess - # start child process using unusual flags - prog = ('from test.test_multiprocessing import TestFlags; ' + - 'TestFlags.run_in_child()') - data = subprocess.check_output( - [sys.executable, '-E', '-S', '-O', '-c', prog]) - child_flags, grandchild_flags = json.loads(data.decode('ascii')) - self.assertEqual(child_flags, grandchild_flags) - testcases_other = [OtherTest, TestInvalidHandle, TestInitializers, - TestStdinBadfiledescriptor, TestWait, TestInvalidFamily, - TestFlags] + TestStdinBadfiledescriptor, TestWait, TestInvalidFamily] # # diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_namespace_pkgs.py --- a/Lib/test/test_namespace_pkgs.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,284 +0,0 @@ -import sys -import contextlib -import unittest -import os - -import importlib.test.util -from test.support import run_unittest - -# needed tests: -# -# need to test when nested, so that the top-level path isn't sys.path -# need to test dynamic path detection, both at top-level and nested -# with dynamic path, check when a loader is returned on path reload (that is, -# trying to switch from a namespace package to a regular package) - - -@contextlib.contextmanager -def sys_modules_context(): - """ - Make sure sys.modules is the same object and has the same content - when exiting the context as when entering. - - Similar to importlib.test.util.uncache, but doesn't require explicit - names. - """ - sys_modules_saved = sys.modules - sys_modules_copy = sys.modules.copy() - try: - yield - finally: - sys.modules = sys_modules_saved - sys.modules.clear() - sys.modules.update(sys_modules_copy) - - -@contextlib.contextmanager -def namespace_tree_context(**kwargs): - """ - Save import state and sys.modules cache and restore it on exit. - Typical usage: - - >>> with namespace_tree_context(path=['/tmp/xxyy/portion1', - ... '/tmp/xxyy/portion2']): - ... pass - """ - # use default meta_path and path_hooks unless specified otherwise - kwargs.setdefault('meta_path', sys.meta_path) - kwargs.setdefault('path_hooks', sys.path_hooks) - import_context = importlib.test.util.import_state(**kwargs) - with import_context, sys_modules_context(): - yield - -class NamespacePackageTest(unittest.TestCase): - """ - Subclasses should define self.root and self.paths (under that root) - to be added to sys.path. - """ - root = os.path.join(os.path.dirname(__file__), 'namespace_pkgs') - - def setUp(self): - self.resolved_paths = [ - os.path.join(self.root, path) for path in self.paths - ] - self.ctx = namespace_tree_context(path=self.resolved_paths) - self.ctx.__enter__() - - def tearDown(self): - # TODO: will we ever want to pass exc_info to __exit__? - self.ctx.__exit__(None, None, None) - -class SingleNamespacePackage(NamespacePackageTest): - paths = ['portion1'] - - def test_simple_package(self): - import foo.one - self.assertEqual(foo.one.attr, 'portion1 foo one') - - def test_cant_import_other(self): - with self.assertRaises(ImportError): - import foo.two - - def test_module_repr(self): - import foo.one - self.assertEqual(repr(foo), "") - - -class DynamicPatheNamespacePackage(NamespacePackageTest): - paths = ['portion1'] - - def test_dynamic_path(self): - # Make sure only 'foo.one' can be imported - import foo.one - self.assertEqual(foo.one.attr, 'portion1 foo one') - - with self.assertRaises(ImportError): - import foo.two - - # Now modify sys.path - sys.path.append(os.path.join(self.root, 'portion2')) - - # And make sure foo.two is now importable - import foo.two - self.assertEqual(foo.two.attr, 'portion2 foo two') - - -class CombinedNamespacePackages(NamespacePackageTest): - paths = ['both_portions'] - - def test_imports(self): - import foo.one - import foo.two - self.assertEqual(foo.one.attr, 'both_portions foo one') - self.assertEqual(foo.two.attr, 'both_portions foo two') - - -class SeparatedNamespacePackages(NamespacePackageTest): - paths = ['portion1', 'portion2'] - - def test_imports(self): - import foo.one - import foo.two - self.assertEqual(foo.one.attr, 'portion1 foo one') - self.assertEqual(foo.two.attr, 'portion2 foo two') - - -class SeparatedOverlappingNamespacePackages(NamespacePackageTest): - paths = ['portion1', 'both_portions'] - - def test_first_path_wins(self): - import foo.one - import foo.two - self.assertEqual(foo.one.attr, 'portion1 foo one') - self.assertEqual(foo.two.attr, 'both_portions foo two') - - def test_first_path_wins_again(self): - sys.path.reverse() - import foo.one - import foo.two - self.assertEqual(foo.one.attr, 'both_portions foo one') - self.assertEqual(foo.two.attr, 'both_portions foo two') - - def test_first_path_wins_importing_second_first(self): - import foo.two - import foo.one - self.assertEqual(foo.one.attr, 'portion1 foo one') - self.assertEqual(foo.two.attr, 'both_portions foo two') - - -class SingleZipNamespacePackage(NamespacePackageTest): - paths = ['top_level_portion1.zip'] - - def test_simple_package(self): - import foo.one - self.assertEqual(foo.one.attr, 'portion1 foo one') - - def test_cant_import_other(self): - with self.assertRaises(ImportError): - import foo.two - - -class SeparatedZipNamespacePackages(NamespacePackageTest): - paths = ['top_level_portion1.zip', 'portion2'] - - def test_imports(self): - import foo.one - import foo.two - self.assertEqual(foo.one.attr, 'portion1 foo one') - self.assertEqual(foo.two.attr, 'portion2 foo two') - self.assertIn('top_level_portion1.zip', foo.one.__file__) - self.assertNotIn('.zip', foo.two.__file__) - - -class SingleNestedZipNamespacePackage(NamespacePackageTest): - paths = ['nested_portion1.zip/nested_portion1'] - - def test_simple_package(self): - import foo.one - self.assertEqual(foo.one.attr, 'portion1 foo one') - - def test_cant_import_other(self): - with self.assertRaises(ImportError): - import foo.two - - -class SeparatedNestedZipNamespacePackages(NamespacePackageTest): - paths = ['nested_portion1.zip/nested_portion1', 'portion2'] - - def test_imports(self): - import foo.one - import foo.two - self.assertEqual(foo.one.attr, 'portion1 foo one') - self.assertEqual(foo.two.attr, 'portion2 foo two') - fn = os.path.join('nested_portion1.zip', 'nested_portion1') - self.assertIn(fn, foo.one.__file__) - self.assertNotIn('.zip', foo.two.__file__) - - -class LegacySupport(NamespacePackageTest): - paths = ['not_a_namespace_pkg', 'portion1', 'portion2', 'both_portions'] - - def test_non_namespace_package_takes_precedence(self): - import foo.one - with self.assertRaises(ImportError): - import foo.two - self.assertIn('__init__', foo.__file__) - self.assertNotIn('namespace', str(foo.__loader__).lower()) - - -class DynamicPathCalculation(NamespacePackageTest): - paths = ['project1', 'project2'] - - def test_project3_fails(self): - import parent.child.one - self.assertEqual(len(parent.__path__), 2) - self.assertEqual(len(parent.child.__path__), 2) - import parent.child.two - self.assertEqual(len(parent.__path__), 2) - self.assertEqual(len(parent.child.__path__), 2) - - self.assertEqual(parent.child.one.attr, 'parent child one') - self.assertEqual(parent.child.two.attr, 'parent child two') - - with self.assertRaises(ImportError): - import parent.child.three - - self.assertEqual(len(parent.__path__), 2) - self.assertEqual(len(parent.child.__path__), 2) - - def test_project3_succeeds(self): - import parent.child.one - self.assertEqual(len(parent.__path__), 2) - self.assertEqual(len(parent.child.__path__), 2) - import parent.child.two - self.assertEqual(len(parent.__path__), 2) - self.assertEqual(len(parent.child.__path__), 2) - - self.assertEqual(parent.child.one.attr, 'parent child one') - self.assertEqual(parent.child.two.attr, 'parent child two') - - with self.assertRaises(ImportError): - import parent.child.three - - # now add project3 - sys.path.append(os.path.join(self.root, 'project3')) - import parent.child.three - - # the paths dynamically get longer, to include the new directories - self.assertEqual(len(parent.__path__), 3) - self.assertEqual(len(parent.child.__path__), 3) - - self.assertEqual(parent.child.three.attr, 'parent child three') - - -class ZipWithMissingDirectory(NamespacePackageTest): - paths = ['missing_directory.zip'] - - @unittest.expectedFailure - def test_missing_directory(self): - # This will fail because missing_directory.zip contains: - # Length Date Time Name - # --------- ---------- ----- ---- - # 29 2012-05-03 18:13 foo/one.py - # 0 2012-05-03 20:57 bar/ - # 38 2012-05-03 20:57 bar/two.py - # --------- ------- - # 67 3 files - - # Because there is no 'foo/', the zipimporter currently doesn't - # know that foo is a namespace package - - import foo.one - - def test_present_directory(self): - # This succeeds because there is a "bar/" in the zip file - import bar.two - self.assertEqual(bar.two.attr, 'missing_directory foo two') - - -def test_main(): - run_unittest(*NamespacePackageTest.__subclasses__()) - - -if __name__ == "__main__": - test_main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_os.py --- a/Lib/test/test_os.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_os.py Mon May 21 23:01:17 2012 -0400 @@ -30,9 +30,7 @@ threading = None from test.script_helper import assert_python_ok -with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - os.stat_float_times(True) +os.stat_float_times(True) st = os.stat(__file__) stat_supports_subsecond = ( # check if float and int timestamps are different @@ -390,9 +388,7 @@ filename = self.fname os.utime(filename, (0, 0)) set_time_func(filename, atime, mtime) - with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - os.stat_float_times(True) + os.stat_float_times(True) st = os.stat(filename) self.assertAlmostEqual(st.st_atime, atime, places=3) self.assertAlmostEqual(st.st_mtime, mtime, places=3) @@ -842,31 +838,6 @@ os.makedirs(path, mode=mode, exist_ok=True) os.umask(old_mask) - def test_exist_ok_s_isgid_directory(self): - path = os.path.join(support.TESTFN, 'dir1') - S_ISGID = stat.S_ISGID - mode = 0o777 - old_mask = os.umask(0o022) - try: - existing_testfn_mode = stat.S_IMODE( - os.lstat(support.TESTFN).st_mode) - os.chmod(support.TESTFN, existing_testfn_mode | S_ISGID) - if (os.lstat(support.TESTFN).st_mode & S_ISGID != S_ISGID): - raise unittest.SkipTest('No support for S_ISGID dir mode.') - # The os should apply S_ISGID from the parent dir for us, but - # this test need not depend on that behavior. Be explicit. - os.makedirs(path, mode | S_ISGID) - # http://bugs.python.org/issue14992 - # Should not fail when the bit is already set. - os.makedirs(path, mode, exist_ok=True) - # remove the bit. - os.chmod(path, stat.S_IMODE(os.lstat(path).st_mode) & ~S_ISGID) - with self.assertRaises(OSError): - # Should fail when the bit is not already set when demanded. - os.makedirs(path, mode | S_ISGID, exist_ok=True) - finally: - os.umask(old_mask) - def test_exist_ok_existing_regular_file(self): base = support.TESTFN path = os.path.join(support.TESTFN, 'dir1') diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_pkg.py --- a/Lib/test/test_pkg.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_pkg.py Mon May 21 23:01:17 2012 -0400 @@ -23,8 +23,6 @@ def fixdir(lst): if "__builtins__" in lst: lst.remove("__builtins__") - if "__initializing__" in lst: - lst.remove("__initializing__") return lst diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_pkgutil.py --- a/Lib/test/test_pkgutil.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_pkgutil.py Mon May 21 23:01:17 2012 -0400 @@ -138,11 +138,10 @@ del sys.modules['foo'] -# These tests, especially the setup and cleanup, are hideous. They -# need to be cleaned up once issue 14715 is addressed. class ExtendPathTests(unittest.TestCase): def create_init(self, pkgname): dirname = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, dirname) sys.path.insert(0, dirname) pkgdir = os.path.join(dirname, pkgname) @@ -157,12 +156,22 @@ with open(module_name, 'w') as fl: print('value={}'.format(value), file=fl) + def setUp(self): + # Create 2 directories on sys.path + self.pkgname = 'foo' + self.dirname_0 = self.create_init(self.pkgname) + self.dirname_1 = self.create_init(self.pkgname) + + def tearDown(self): + del sys.path[0] + del sys.path[0] + del sys.modules['foo'] + del sys.modules['foo.bar'] + del sys.modules['foo.baz'] + def test_simple(self): - pkgname = 'foo' - dirname_0 = self.create_init(pkgname) - dirname_1 = self.create_init(pkgname) - self.create_submodule(dirname_0, pkgname, 'bar', 0) - self.create_submodule(dirname_1, pkgname, 'baz', 1) + self.create_submodule(self.dirname_0, self.pkgname, 'bar', 0) + self.create_submodule(self.dirname_1, self.pkgname, 'baz', 1) import foo.bar import foo.baz # Ensure we read the expected values @@ -171,45 +180,8 @@ # Ensure the path is set up correctly self.assertEqual(sorted(foo.__path__), - sorted([os.path.join(dirname_0, pkgname), - os.path.join(dirname_1, pkgname)])) - - # Cleanup - shutil.rmtree(dirname_0) - shutil.rmtree(dirname_1) - del sys.path[0] - del sys.path[0] - del sys.modules['foo'] - del sys.modules['foo.bar'] - del sys.modules['foo.baz'] - - def test_mixed_namespace(self): - pkgname = 'foo' - dirname_0 = self.create_init(pkgname) - dirname_1 = self.create_init(pkgname) - self.create_submodule(dirname_0, pkgname, 'bar', 0) - # Turn this into a PEP 420 namespace package - os.unlink(os.path.join(dirname_0, pkgname, '__init__.py')) - self.create_submodule(dirname_1, pkgname, 'baz', 1) - import foo.bar - import foo.baz - # Ensure we read the expected values - self.assertEqual(foo.bar.value, 0) - self.assertEqual(foo.baz.value, 1) - - # Ensure the path is set up correctly - self.assertEqual(sorted(foo.__path__), - sorted([os.path.join(dirname_0, pkgname), - os.path.join(dirname_1, pkgname)])) - - # Cleanup - shutil.rmtree(dirname_0) - shutil.rmtree(dirname_1) - del sys.path[0] - del sys.path[0] - del sys.modules['foo'] - del sys.modules['foo.bar'] - del sys.modules['foo.baz'] + sorted([os.path.join(self.dirname_0, self.pkgname), + os.path.join(self.dirname_1, self.pkgname)])) # XXX: test .pkg files diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_plistlib.py --- a/Lib/test/test_plistlib.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_plistlib.py Mon May 21 23:01:17 2012 -0400 @@ -55,10 +55,6 @@ aString Doodah - anEmptyDict - - anEmptyList - anInt 728 nestedData @@ -116,8 +112,6 @@ someMoreData = plistlib.Data(b"\0\1\2\3" * 10), nestedData = [plistlib.Data(b"\0\1\2\3" * 10)], aDate = datetime.datetime(2004, 10, 26, 10, 33, 33), - anEmptyDict = dict(), - anEmptyList = list() ) pl['\xc5benraa'] = "That was a unicode key." return pl diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_pyclbr.py --- a/Lib/test/test_pyclbr.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_pyclbr.py Mon May 21 23:01:17 2012 -0400 @@ -167,11 +167,6 @@ cm('email.parser') cm('test.test_pyclbr') - def test_issue_14798(self): - # test ImportError is raised when the first part of a dotted name is - # not a package - self.assertRaises(ImportError, pyclbr.readmodule_ex, 'asyncore.foo') - def test_main(): run_unittest(PyclbrTest) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_raise.py --- a/Lib/test/test_raise.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_raise.py Mon May 21 23:01:17 2012 -0400 @@ -3,13 +3,27 @@ """Tests for the raise statement.""" -from test import support +from test import support, script_helper import re import sys import types import unittest +try: + from resource import setrlimit, RLIMIT_CORE, error as resource_error +except ImportError: + prepare_subprocess = None +else: + def prepare_subprocess(): + # don't create core file + try: + setrlimit(RLIMIT_CORE, (0, 0)) + except (ValueError, resource_error): + pass + + + def get_tb(): try: raise OSError() @@ -210,6 +224,43 @@ class TestTraceback(unittest.TestCase): + def get_output(self, code, filename=None): + """ + Run the specified code in Python (in a new child process) and read the + output from the standard error or from a file (if filename is set). + Return the output lines as a list. + """ + options = {} + if prepare_subprocess: + options['preexec_fn'] = prepare_subprocess + process = script_helper.spawn_python('-c', code, **options) + stdout, stderr = process.communicate() + exitcode = process.wait() + output = support.strip_python_stderr(stdout) + output = output.decode('ascii', 'backslashreplace') + if filename: + self.assertEqual(output, '') + with open(filename, "rb") as fp: + output = fp.read() + output = output.decode('ascii', 'backslashreplace') + output = re.sub('Current thread 0x[0-9a-f]+', + 'Current thread XXX', + output) + return output.splitlines(), exitcode + + def test_traceback_verbiage(self): + code = """ +try: + raise ValueError +except: + raise NameError from None +""" + text, exitcode = self.get_output(code) + self.assertEqual(len(text), 3) + self.assertTrue(text[0].startswith('Traceback')) + self.assertTrue(text[1].startswith(' File ')) + self.assertTrue(text[2].startswith('NameError')) + def test_sets_traceback(self): try: raise IndexError() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_smtpd.py --- a/Lib/test/test_smtpd.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_smtpd.py Mon May 21 23:01:17 2012 -0400 @@ -1,4 +1,4 @@ -import unittest +from unittest import TestCase from test import support, mock_socket import socket import io @@ -26,7 +26,7 @@ raise DummyDispatcherBroken() -class SMTPDServerTest(unittest.TestCase): +class SMTPDServerTest(TestCase): def setUp(self): smtpd.socket = asyncore.socket = mock_socket @@ -39,7 +39,7 @@ channel.socket.queue_recv(line) channel.handle_read() - write_line(b'HELO example') + write_line(b'HELO test.example') write_line(b'MAIL From:eggs@example') write_line(b'RCPT To:spam@example') write_line(b'DATA') @@ -50,7 +50,7 @@ asyncore.socket = smtpd.socket = socket -class SMTPDChannelTest(unittest.TestCase): +class SMTPDChannelTest(TestCase): def setUp(self): smtpd.socket = asyncore.socket = mock_socket self.old_debugstream = smtpd.DEBUGSTREAM @@ -79,94 +79,36 @@ self.assertEqual(self.channel.socket.last, b'500 Error: bad syntax\r\n') - def test_EHLO(self): - self.write_line(b'EHLO example') - self.assertEqual(self.channel.socket.last, b'250 HELP\r\n') - - def test_EHLO_bad_syntax(self): - self.write_line(b'EHLO') + def test_EHLO_not_implemented(self): + self.write_line(b'EHLO test.example') self.assertEqual(self.channel.socket.last, - b'501 Syntax: EHLO hostname\r\n') - - def test_EHLO_duplicate(self): - self.write_line(b'EHLO example') - self.write_line(b'EHLO example') - self.assertEqual(self.channel.socket.last, - b'503 Duplicate HELO/EHLO\r\n') - - def test_EHLO_HELO_duplicate(self): - self.write_line(b'EHLO example') - self.write_line(b'HELO example') - self.assertEqual(self.channel.socket.last, - b'503 Duplicate HELO/EHLO\r\n') + b'502 Error: command "EHLO" not implemented\r\n') def test_HELO(self): name = smtpd.socket.getfqdn() - self.write_line(b'HELO example') + self.write_line(b'HELO test.example') self.assertEqual(self.channel.socket.last, '250 {}\r\n'.format(name).encode('ascii')) - def test_HELO_EHLO_duplicate(self): - self.write_line(b'HELO example') - self.write_line(b'EHLO example') - self.assertEqual(self.channel.socket.last, - b'503 Duplicate HELO/EHLO\r\n') - - def test_HELP(self): - self.write_line(b'HELP') - self.assertEqual(self.channel.socket.last, - b'250 Supported commands: EHLO HELO MAIL RCPT ' + \ - b'DATA RSET NOOP QUIT VRFY\r\n') - - def test_HELP_command(self): - self.write_line(b'HELP MAIL') - self.assertEqual(self.channel.socket.last, - b'250 Syntax: MAIL FROM:
\r\n') - - def test_HELP_command_unknown(self): - self.write_line(b'HELP SPAM') - self.assertEqual(self.channel.socket.last, - b'501 Supported commands: EHLO HELO MAIL RCPT ' + \ - b'DATA RSET NOOP QUIT VRFY\r\n') - def test_HELO_bad_syntax(self): self.write_line(b'HELO') self.assertEqual(self.channel.socket.last, b'501 Syntax: HELO hostname\r\n') def test_HELO_duplicate(self): - self.write_line(b'HELO example') - self.write_line(b'HELO example') + self.write_line(b'HELO test.example') + self.write_line(b'HELO test.example') self.assertEqual(self.channel.socket.last, b'503 Duplicate HELO/EHLO\r\n') - def test_HELO_parameter_rejected_when_extensions_not_enabled(self): - self.extended_smtp = False - self.write_line(b'HELO example') - self.write_line(b'MAIL from: SIZE=1234') - self.assertEqual(self.channel.socket.last, - b'501 Syntax: MAIL FROM:
\r\n') - - def test_MAIL_allows_space_after_colon(self): - self.write_line(b'HELO example') - self.write_line(b'MAIL from: ') - self.assertEqual(self.channel.socket.last, - b'250 OK\r\n') - - def test_extended_MAIL_allows_space_after_colon(self): - self.write_line(b'EHLO example') - self.write_line(b'MAIL from: size=20') - self.assertEqual(self.channel.socket.last, - b'250 OK\r\n') - def test_NOOP(self): self.write_line(b'NOOP') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') def test_HELO_NOOP(self): self.write_line(b'HELO example') self.write_line(b'NOOP') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') def test_NOOP_bad_syntax(self): self.write_line(b'NOOP hi') @@ -194,29 +136,15 @@ def test_command_too_long(self): self.write_line(b'HELO example') - self.write_line(b'MAIL from: ' + + self.write_line(b'MAIL from ' + b'a' * self.channel.command_size_limit + b'@example') self.assertEqual(self.channel.socket.last, b'500 Error: line too long\r\n') - def test_MAIL_command_limit_extended_with_SIZE(self): - self.write_line(b'EHLO example') - fill_len = self.channel.command_size_limit - len('MAIL from:<@example>') - self.write_line(b'MAIL from:<' + - b'a' * fill_len + - b'@example> SIZE=1234') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - - self.write_line(b'MAIL from:<' + - b'a' * (fill_len + 26) + - b'@example> SIZE=1234') - self.assertEqual(self.channel.socket.last, - b'500 Error: line too long\r\n') - - def test_data_longer_than_default_data_size_limit(self): - # Hack the default so we don't have to generate so much data. - self.channel.data_size_limit = 1048 + def test_data_too_long(self): + # Small hack. Setting limit to 2K octets here will save us some time. + self.channel.data_size_limit = 2048 self.write_line(b'HELO example') self.write_line(b'MAIL From:eggs@example') self.write_line(b'RCPT To:spam@example') @@ -226,93 +154,28 @@ self.assertEqual(self.channel.socket.last, b'552 Error: Too much mail data\r\n') - def test_MAIL_size_parameter(self): - self.write_line(b'EHLO example') - self.write_line(b'MAIL FROM: SIZE=512') - self.assertEqual(self.channel.socket.last, - b'250 OK\r\n') - - def test_MAIL_invalid_size_parameter(self): - self.write_line(b'EHLO example') - self.write_line(b'MAIL FROM: SIZE=invalid') - self.assertEqual(self.channel.socket.last, - b'501 Syntax: MAIL FROM:
[SP ]\r\n') - - def test_MAIL_RCPT_unknown_parameters(self): - self.write_line(b'EHLO example') - self.write_line(b'MAIL FROM: ham=green') - self.assertEqual(self.channel.socket.last, - b'555 MAIL FROM parameters not recognized or not implemented\r\n') - - self.write_line(b'MAIL FROM:') - self.write_line(b'RCPT TO: ham=green') - self.assertEqual(self.channel.socket.last, - b'555 RCPT TO parameters not recognized or not implemented\r\n') - - def test_MAIL_size_parameter_larger_than_default_data_size_limit(self): - self.channel.data_size_limit = 1048 - self.write_line(b'EHLO example') - self.write_line(b'MAIL FROM: SIZE=2096') - self.assertEqual(self.channel.socket.last, - b'552 Error: message size exceeds fixed maximum message size\r\n') - def test_need_MAIL(self): self.write_line(b'HELO example') self.write_line(b'RCPT to:spam@example') self.assertEqual(self.channel.socket.last, b'503 Error: need MAIL command\r\n') - def test_MAIL_syntax_HELO(self): + def test_MAIL_syntax(self): self.write_line(b'HELO example') self.write_line(b'MAIL from eggs@example') self.assertEqual(self.channel.socket.last, - b'501 Syntax: MAIL FROM:
\r\n') + b'501 Syntax: MAIL FROM:
\r\n') - def test_MAIL_syntax_EHLO(self): - self.write_line(b'EHLO example') - self.write_line(b'MAIL from eggs@example') - self.assertEqual(self.channel.socket.last, - b'501 Syntax: MAIL FROM:
[SP ]\r\n') - - def test_MAIL_missing_address(self): + def test_MAIL_missing_from(self): self.write_line(b'HELO example') self.write_line(b'MAIL from:') self.assertEqual(self.channel.socket.last, - b'501 Syntax: MAIL FROM:
\r\n') + b'501 Syntax: MAIL FROM:
\r\n') def test_MAIL_chevrons(self): self.write_line(b'HELO example') self.write_line(b'MAIL from:') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - - def test_MAIL_empty_chevrons(self): - self.write_line(b'EHLO example') - self.write_line(b'MAIL from:<>') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - - def test_MAIL_quoted_localpart(self): - self.write_line(b'EHLO example') - self.write_line(b'MAIL from: <"Fred Blogs"@example.com>') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com') - - def test_MAIL_quoted_localpart_no_angles(self): - self.write_line(b'EHLO example') - self.write_line(b'MAIL from: "Fred Blogs"@example.com') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com') - - def test_MAIL_quoted_localpart_with_size(self): - self.write_line(b'EHLO example') - self.write_line(b'MAIL from: <"Fred Blogs"@example.com> SIZE=1000') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com') - - def test_MAIL_quoted_localpart_with_size_no_angles(self): - self.write_line(b'EHLO example') - self.write_line(b'MAIL from: "Fred Blogs"@example.com SIZE=1000') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com') + self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') def test_nested_MAIL(self): self.write_line(b'HELO example') @@ -321,22 +184,6 @@ self.assertEqual(self.channel.socket.last, b'503 Error: nested MAIL command\r\n') - def test_VRFY(self): - self.write_line(b'VRFY eggs@example') - self.assertEqual(self.channel.socket.last, - b'252 Cannot VRFY user, but will accept message and attempt ' + \ - b'delivery\r\n') - - def test_VRFY_syntax(self): - self.write_line(b'VRFY') - self.assertEqual(self.channel.socket.last, - b'501 Syntax: VRFY
\r\n') - - def test_EXPN_not_implemented(self): - self.write_line(b'EXPN') - self.assertEqual(self.channel.socket.last, - b'502 EXPN not implemented\r\n') - def test_no_HELO_MAIL(self): self.write_line(b'MAIL from:') self.assertEqual(self.channel.socket.last, @@ -349,26 +196,13 @@ self.assertEqual(self.channel.socket.last, b'503 Error: need RCPT command\r\n') - def test_RCPT_syntax_HELO(self): + def test_RCPT_syntax(self): self.write_line(b'HELO example') - self.write_line(b'MAIL From: eggs@example') + self.write_line(b'MAIL From:eggs@example') self.write_line(b'RCPT to eggs@example') self.assertEqual(self.channel.socket.last, b'501 Syntax: RCPT TO:
\r\n') - def test_RCPT_syntax_EHLO(self): - self.write_line(b'EHLO example') - self.write_line(b'MAIL From: eggs@example') - self.write_line(b'RCPT to eggs@example') - self.assertEqual(self.channel.socket.last, - b'501 Syntax: RCPT TO:
[SP ]\r\n') - - def test_RCPT_lowercase_to_OK(self): - self.write_line(b'HELO example') - self.write_line(b'MAIL From: eggs@example') - self.write_line(b'RCPT to: ') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - def test_no_HELO_RCPT(self): self.write_line(b'RCPT to eggs@example') self.assertEqual(self.channel.socket.last, @@ -377,15 +211,15 @@ def test_data_dialog(self): self.write_line(b'HELO example') self.write_line(b'MAIL From:eggs@example') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') self.write_line(b'RCPT To:spam@example') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') self.write_line(b'DATA') self.assertEqual(self.channel.socket.last, b'354 End data with .\r\n') self.write_line(b'data\r\nmore\r\n.') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') self.assertEqual(self.server.messages, [('peer', 'eggs@example', ['spam@example'], 'data\nmore')]) @@ -433,7 +267,7 @@ self.write_line(b'MAIL From:eggs@example') self.write_line(b'RCPT To:spam@example') self.write_line(b'RSET') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') self.write_line(b'MAIL From:foo@example') self.write_line(b'RCPT To:eggs@example') self.write_line(b'DATA') @@ -444,18 +278,12 @@ def test_HELO_RSET(self): self.write_line(b'HELO example') self.write_line(b'RSET') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') def test_RSET_syntax(self): self.write_line(b'RSET hi') self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n') - def test_unknown_command(self): - self.write_line(b'UNKNOWN_CMD') - self.assertEqual(self.channel.socket.last, - b'500 Error: command "UNKNOWN_CMD" not ' + \ - b'recognized\r\n') - def test_attribute_deprecations(self): with support.check_warnings(('', DeprecationWarning)): spam = self.channel._SMTPChannel__server @@ -502,56 +330,8 @@ with support.check_warnings(('', DeprecationWarning)): self.channel._SMTPChannel__addr = 'spam' - -class SMTPDChannelWithDataSizeLimitTest(unittest.TestCase): - - def setUp(self): - smtpd.socket = asyncore.socket = mock_socket - self.old_debugstream = smtpd.DEBUGSTREAM - self.debug = smtpd.DEBUGSTREAM = io.StringIO() - self.server = DummyServer('a', 'b') - conn, addr = self.server.accept() - # Set DATA size limit to 32 bytes for easy testing - self.channel = smtpd.SMTPChannel(self.server, conn, addr, 32) - - def tearDown(self): - asyncore.close_all() - asyncore.socket = smtpd.socket = socket - smtpd.DEBUGSTREAM = self.old_debugstream - - def write_line(self, line): - self.channel.socket.queue_recv(line) - self.channel.handle_read() - - def test_data_limit_dialog(self): - self.write_line(b'HELO example') - self.write_line(b'MAIL From:eggs@example') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - self.write_line(b'RCPT To:spam@example') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - - self.write_line(b'DATA') - self.assertEqual(self.channel.socket.last, - b'354 End data with .\r\n') - self.write_line(b'data\r\nmore\r\n.') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - self.assertEqual(self.server.messages, - [('peer', 'eggs@example', ['spam@example'], 'data\nmore')]) - - def test_data_limit_dialog_too_much_data(self): - self.write_line(b'HELO example') - self.write_line(b'MAIL From:eggs@example') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - self.write_line(b'RCPT To:spam@example') - self.assertEqual(self.channel.socket.last, b'250 OK\r\n') - - self.write_line(b'DATA') - self.assertEqual(self.channel.socket.last, - b'354 End data with .\r\n') - self.write_line(b'This message is longer than 32 bytes\r\n.') - self.assertEqual(self.channel.socket.last, - b'552 Error: Too much mail data\r\n') - +def test_main(): + support.run_unittest(SMTPDServerTest, SMTPDChannelTest) if __name__ == "__main__": - unittest.main() + test_main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_smtplib.py --- a/Lib/test/test_smtplib.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_smtplib.py Mon May 21 23:01:17 2012 -0400 @@ -229,13 +229,13 @@ def testNOOP(self): smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) - expected = (250, b'OK') + expected = (250, b'Ok') self.assertEqual(smtp.noop(), expected) smtp.quit() def testRSET(self): smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) - expected = (250, b'OK') + expected = (250, b'Ok') self.assertEqual(smtp.rset(), expected) smtp.quit() @@ -246,18 +246,10 @@ self.assertEqual(smtp.ehlo(), expected) smtp.quit() - def testNotImplemented(self): - # EXPN isn't implemented in DebuggingServer + def testVRFY(self): + # VRFY isn't implemented in DebuggingServer smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) - expected = (502, b'EXPN not implemented') - smtp.putcmd('EXPN') - self.assertEqual(smtp.getreply(), expected) - smtp.quit() - - def testVRFY(self): - smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) - expected = (252, b'Cannot VRFY user, but will accept message ' + \ - b'and attempt delivery') + expected = (502, b'Error: command "VRFY" not implemented') self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected) self.assertEqual(smtp.verify('nobody@nowhere.com'), expected) smtp.quit() @@ -273,8 +265,7 @@ def testHELP(self): smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) - self.assertEqual(smtp.help(), b'Supported commands: EHLO HELO MAIL ' + \ - b'RCPT DATA RSET NOOP QUIT VRFY') + self.assertEqual(smtp.help(), b'Error: command "HELP" not implemented') smtp.quit() def testSend(self): diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_subprocess.py --- a/Lib/test/test_subprocess.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_subprocess.py Mon May 21 23:01:17 2012 -0400 @@ -189,8 +189,6 @@ p.wait() self.assertEqual(p.stderr, None) - @unittest.skipIf(sys.base_prefix != sys.prefix, - 'Test is not venv-compatible') def test_executable_with_cwd(self): python_dir = os.path.dirname(os.path.realpath(sys.executable)) p = subprocess.Popen(["somethingyoudonthave", "-c", @@ -199,8 +197,6 @@ p.wait() self.assertEqual(p.returncode, 47) - @unittest.skipIf(sys.base_prefix != sys.prefix, - 'Test is not venv-compatible') @unittest.skipIf(sysconfig.is_python_build(), "need an installed Python. See #7774") def test_executable_without_cwd(self): diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_super.py --- a/Lib/test/test_super.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_super.py Mon May 21 23:01:17 2012 -0400 @@ -81,7 +81,6 @@ self.assertEqual(E().f(), 'AE') - @unittest.expectedFailure def test___class___set(self): # See issue #12370 class X(A): @@ -92,29 +91,6 @@ self.assertEqual(x.f(), 'A') self.assertEqual(x.__class__, 413) - def test___class___instancemethod(self): - # See issue #14857 - class X: - def f(self): - return __class__ - self.assertIs(X().f(), X) - - def test___class___classmethod(self): - # See issue #14857 - class X: - @classmethod - def f(cls): - return __class__ - self.assertIs(X.f(), X) - - def test___class___staticmethod(self): - # See issue #14857 - class X: - @staticmethod - def f(): - return __class__ - self.assertIs(X.f(), X) - def test_main(): support.run_unittest(TestSuper) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_sys.py --- a/Lib/test/test_sys.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_sys.py Mon May 21 23:01:17 2012 -0400 @@ -419,7 +419,6 @@ self.assertIsInstance(sys.builtin_module_names, tuple) self.assertIsInstance(sys.copyright, str) self.assertIsInstance(sys.exec_prefix, str) - self.assertIsInstance(sys.base_exec_prefix, str) self.assertIsInstance(sys.executable, str) self.assertEqual(len(sys.float_info), 11) self.assertEqual(sys.float_info.radix, 2) @@ -451,7 +450,6 @@ self.assertEqual(sys.maxunicode, 0x10FFFF) self.assertIsInstance(sys.platform, str) self.assertIsInstance(sys.prefix, str) - self.assertIsInstance(sys.base_prefix, str) self.assertIsInstance(sys.version, str) vi = sys.version_info self.assertIsInstance(vi[:], tuple) @@ -543,8 +541,6 @@ out = p.communicate()[0].strip() self.assertEqual(out, b'?') - @unittest.skipIf(sys.base_prefix != sys.prefix, - 'Test is not venv-compatible') def test_executable(self): # sys.executable should be absolute self.assertEqual(os.path.abspath(sys.executable), sys.executable) @@ -581,28 +577,6 @@ expected = None self.check_fsencoding(fs_encoding, expected) - def test_implementation(self): - # This test applies to all implementations equally. - - levels = {'alpha': 0xA, 'beta': 0xB, 'candidate': 0xC, 'release': 0xF} - - self.assertTrue(hasattr(sys.implementation, 'name')) - self.assertTrue(hasattr(sys.implementation, 'version')) - self.assertTrue(hasattr(sys.implementation, 'hexversion')) - self.assertTrue(hasattr(sys.implementation, 'cache_tag')) - - version = sys.implementation.version - self.assertEqual(version[:2], (version.major, version.minor)) - - hexversion = (version.major << 24 | version.minor << 16 | - version.micro << 8 | levels[version.releaselevel] << 4 | - version.serial << 0) - self.assertEqual(sys.implementation.hexversion, hexversion) - - # PEP 421 requires that .name be lower case. - self.assertEqual(sys.implementation.name, - sys.implementation.name.lower()) - class SizeofTest(unittest.TestCase): @@ -800,7 +774,7 @@ check(int(PyLong_BASE**2-1), size(vh) + 2*self.longdigit) check(int(PyLong_BASE**2), size(vh) + 3*self.longdigit) # memoryview - check(memoryview(b''), size(h + 'PPiP4P2i5P3c2P')) + check(memoryview(b''), size(h + 'PPiP4P2i5P3cP')) # module check(unittest, size(h + '3P')) # None diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_sysconfig.py --- a/Lib/test/test_sysconfig.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_sysconfig.py Mon May 21 23:01:17 2012 -0400 @@ -260,17 +260,12 @@ # the global scheme mirrors the distinction between prefix and # exec-prefix but not the user scheme, so we have to adapt the paths # before comparing (issue #9100) - adapt = sys.base_prefix != sys.base_exec_prefix + adapt = sys.prefix != sys.exec_prefix for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'): global_path = get_path(name, 'posix_prefix') if adapt: - global_path = global_path.replace(sys.exec_prefix, sys.base_prefix) - base = base.replace(sys.exec_prefix, sys.base_prefix) - elif sys.base_prefix != sys.prefix: - # virtual environment? Likewise, we have to adapt the paths - # before comparing - global_path = global_path.replace(sys.base_prefix, sys.prefix) - base = base.replace(sys.base_prefix, sys.prefix) + global_path = global_path.replace(sys.exec_prefix, sys.prefix) + base = base.replace(sys.exec_prefix, sys.prefix) user_path = get_path(name, 'posix_user') self.assertEqual(user_path, global_path.replace(base, user, 1)) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_textwrap.py --- a/Lib/test/test_textwrap.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_textwrap.py Mon May 21 23:01:17 2012 -0400 @@ -91,14 +91,6 @@ result = wrapper.fill(text) self.check(result, '\n'.join(expect)) - text = "\tTest\tdefault\t\ttabsize." - expect = [" Test default tabsize."] - self.check_wrap(text, 80, expect) - - text = "\tTest\tcustom\t\ttabsize." - expect = [" Test custom tabsize."] - self.check_wrap(text, 80, expect, tabsize=4) - def test_fix_sentence_endings(self): wrapper = TextWrapper(60, fix_sentence_endings=True) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_threaded_import.py --- a/Lib/test/test_threaded_import.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_threaded_import.py Mon May 21 23:01:17 2012 -0400 @@ -12,7 +12,7 @@ import shutil import unittest from test.support import ( - verbose, import_module, run_unittest, TESTFN, reap_threads, forget, unlink) + verbose, import_module, run_unittest, TESTFN, reap_threads) threading = import_module('threading') def task(N, done, done_tasks, errors): @@ -187,7 +187,7 @@ contents = contents % {'delay': delay} with open(os.path.join(TESTFN, name + ".py"), "wb") as f: f.write(contents.encode('utf-8')) - self.addCleanup(forget, name) + self.addCleanup(sys.modules.pop, name, None) results = [] def import_ab(): @@ -204,23 +204,6 @@ t2.join() self.assertEqual(set(results), {'a', 'b'}) - def test_side_effect_import(self): - code = """if 1: - import threading - def target(): - import random - t = threading.Thread(target=target) - t.start() - t.join()""" - sys.path.insert(0, os.curdir) - self.addCleanup(sys.path.remove, os.curdir) - filename = TESTFN + ".py" - with open(filename, "wb") as f: - f.write(code.encode('utf-8')) - self.addCleanup(unlink, filename) - self.addCleanup(forget, TESTFN) - __import__(TESTFN) - @reap_threads def test_main(): diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_time.py --- a/Lib/test/test_time.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_time.py Mon May 21 23:01:17 2012 -0400 @@ -380,13 +380,10 @@ time.perf_counter() def test_process_time(self): - # process_time() should not include time spend during a sleep start = time.process_time() - time.sleep(0.100) + time.sleep(0.1) stop = time.process_time() - # use 20 ms because process_time() has usually a resolution of 15 ms - # on Windows - self.assertLess(stop - start, 0.020) + self.assertLess(stop - start, 0.01) info = time.get_clock_info('process_time') self.assertTrue(info.monotonic) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_trace.py --- a/Lib/test/test_trace.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_trace.py Mon May 21 23:01:17 2012 -0400 @@ -316,8 +316,8 @@ # Ignore all files, nothing should be traced nor printed libpath = os.path.normpath(os.path.dirname(os.__file__)) # sys.prefix does not work when running from a checkout - tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix, - libpath], trace=0, count=1) + tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix, libpath], + trace=0, count=1) with captured_stdout() as stdout: self._coverage(tracer) if os.path.exists(TESTFN): diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_types.py --- a/Lib/test/test_types.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_types.py Mon May 21 23:01:17 2012 -0400 @@ -747,398 +747,8 @@ self.assertEqual(copy['key1'], 27) -class ClassCreationTests(unittest.TestCase): - - class Meta(type): - def __init__(cls, name, bases, ns, **kw): - super().__init__(name, bases, ns) - @staticmethod - def __new__(mcls, name, bases, ns, **kw): - return super().__new__(mcls, name, bases, ns) - @classmethod - def __prepare__(mcls, name, bases, **kw): - ns = super().__prepare__(name, bases) - ns["y"] = 1 - ns.update(kw) - return ns - - def test_new_class_basics(self): - C = types.new_class("C") - self.assertEqual(C.__name__, "C") - self.assertEqual(C.__bases__, (object,)) - - def test_new_class_subclass(self): - C = types.new_class("C", (int,)) - self.assertTrue(issubclass(C, int)) - - def test_new_class_meta(self): - Meta = self.Meta - settings = {"metaclass": Meta, "z": 2} - # We do this twice to make sure the passed in dict isn't mutated - for i in range(2): - C = types.new_class("C" + str(i), (), settings) - self.assertIsInstance(C, Meta) - self.assertEqual(C.y, 1) - self.assertEqual(C.z, 2) - - def test_new_class_exec_body(self): - Meta = self.Meta - def func(ns): - ns["x"] = 0 - C = types.new_class("C", (), {"metaclass": Meta, "z": 2}, func) - self.assertIsInstance(C, Meta) - self.assertEqual(C.x, 0) - self.assertEqual(C.y, 1) - self.assertEqual(C.z, 2) - - def test_new_class_exec_body(self): - #Test that keywords are passed to the metaclass: - def meta_func(name, bases, ns, **kw): - return name, bases, ns, kw - res = types.new_class("X", - (int, object), - dict(metaclass=meta_func, x=0)) - self.assertEqual(res, ("X", (int, object), {}, {"x": 0})) - - def test_new_class_defaults(self): - # Test defaults/keywords: - C = types.new_class("C", (), {}, None) - self.assertEqual(C.__name__, "C") - self.assertEqual(C.__bases__, (object,)) - - def test_new_class_meta_with_base(self): - Meta = self.Meta - def func(ns): - ns["x"] = 0 - C = types.new_class(name="C", - bases=(int,), - kwds=dict(metaclass=Meta, z=2), - exec_body=func) - self.assertTrue(issubclass(C, int)) - self.assertIsInstance(C, Meta) - self.assertEqual(C.x, 0) - self.assertEqual(C.y, 1) - self.assertEqual(C.z, 2) - - # Many of the following tests are derived from test_descr.py - def test_prepare_class(self): - # Basic test of metaclass derivation - expected_ns = {} - class A(type): - def __new__(*args, **kwargs): - return type.__new__(*args, **kwargs) - - def __prepare__(*args): - return expected_ns - - B = types.new_class("B", (object,)) - C = types.new_class("C", (object,), {"metaclass": A}) - - # The most derived metaclass of D is A rather than type. - meta, ns, kwds = types.prepare_class("D", (B, C), {"metaclass": type}) - self.assertIs(meta, A) - self.assertIs(ns, expected_ns) - self.assertEqual(len(kwds), 0) - - def test_metaclass_derivation(self): - # issue1294232: correct metaclass calculation - new_calls = [] # to check the order of __new__ calls - class AMeta(type): - def __new__(mcls, name, bases, ns): - new_calls.append('AMeta') - return super().__new__(mcls, name, bases, ns) - @classmethod - def __prepare__(mcls, name, bases): - return {} - - class BMeta(AMeta): - def __new__(mcls, name, bases, ns): - new_calls.append('BMeta') - return super().__new__(mcls, name, bases, ns) - @classmethod - def __prepare__(mcls, name, bases): - ns = super().__prepare__(name, bases) - ns['BMeta_was_here'] = True - return ns - - A = types.new_class("A", (), {"metaclass": AMeta}) - self.assertEqual(new_calls, ['AMeta']) - new_calls.clear() - - B = types.new_class("B", (), {"metaclass": BMeta}) - # BMeta.__new__ calls AMeta.__new__ with super: - self.assertEqual(new_calls, ['BMeta', 'AMeta']) - new_calls.clear() - - C = types.new_class("C", (A, B)) - # The most derived metaclass is BMeta: - self.assertEqual(new_calls, ['BMeta', 'AMeta']) - new_calls.clear() - # BMeta.__prepare__ should've been called: - self.assertIn('BMeta_was_here', C.__dict__) - - # The order of the bases shouldn't matter: - C2 = types.new_class("C2", (B, A)) - self.assertEqual(new_calls, ['BMeta', 'AMeta']) - new_calls.clear() - self.assertIn('BMeta_was_here', C2.__dict__) - - # Check correct metaclass calculation when a metaclass is declared: - D = types.new_class("D", (C,), {"metaclass": type}) - self.assertEqual(new_calls, ['BMeta', 'AMeta']) - new_calls.clear() - self.assertIn('BMeta_was_here', D.__dict__) - - E = types.new_class("E", (C,), {"metaclass": AMeta}) - self.assertEqual(new_calls, ['BMeta', 'AMeta']) - new_calls.clear() - self.assertIn('BMeta_was_here', E.__dict__) - - def test_metaclass_override_function(self): - # Special case: the given metaclass isn't a class, - # so there is no metaclass calculation. - class A(metaclass=self.Meta): - pass - - marker = object() - def func(*args, **kwargs): - return marker - - X = types.new_class("X", (), {"metaclass": func}) - Y = types.new_class("Y", (object,), {"metaclass": func}) - Z = types.new_class("Z", (A,), {"metaclass": func}) - self.assertIs(marker, X) - self.assertIs(marker, Y) - self.assertIs(marker, Z) - - def test_metaclass_override_callable(self): - # The given metaclass is a class, - # but not a descendant of type. - new_calls = [] # to check the order of __new__ calls - prepare_calls = [] # to track __prepare__ calls - class ANotMeta: - def __new__(mcls, *args, **kwargs): - new_calls.append('ANotMeta') - return super().__new__(mcls) - @classmethod - def __prepare__(mcls, name, bases): - prepare_calls.append('ANotMeta') - return {} - - class BNotMeta(ANotMeta): - def __new__(mcls, *args, **kwargs): - new_calls.append('BNotMeta') - return super().__new__(mcls) - @classmethod - def __prepare__(mcls, name, bases): - prepare_calls.append('BNotMeta') - return super().__prepare__(name, bases) - - A = types.new_class("A", (), {"metaclass": ANotMeta}) - self.assertIs(ANotMeta, type(A)) - self.assertEqual(prepare_calls, ['ANotMeta']) - prepare_calls.clear() - self.assertEqual(new_calls, ['ANotMeta']) - new_calls.clear() - - B = types.new_class("B", (), {"metaclass": BNotMeta}) - self.assertIs(BNotMeta, type(B)) - self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta']) - prepare_calls.clear() - self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta']) - new_calls.clear() - - C = types.new_class("C", (A, B)) - self.assertIs(BNotMeta, type(C)) - self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta']) - prepare_calls.clear() - self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta']) - new_calls.clear() - - C2 = types.new_class("C2", (B, A)) - self.assertIs(BNotMeta, type(C2)) - self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta']) - prepare_calls.clear() - self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta']) - new_calls.clear() - - # This is a TypeError, because of a metaclass conflict: - # BNotMeta is neither a subclass, nor a superclass of type - with self.assertRaises(TypeError): - D = types.new_class("D", (C,), {"metaclass": type}) - - E = types.new_class("E", (C,), {"metaclass": ANotMeta}) - self.assertIs(BNotMeta, type(E)) - self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta']) - prepare_calls.clear() - self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta']) - new_calls.clear() - - F = types.new_class("F", (object(), C)) - self.assertIs(BNotMeta, type(F)) - self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta']) - prepare_calls.clear() - self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta']) - new_calls.clear() - - F2 = types.new_class("F2", (C, object())) - self.assertIs(BNotMeta, type(F2)) - self.assertEqual(prepare_calls, ['BNotMeta', 'ANotMeta']) - prepare_calls.clear() - self.assertEqual(new_calls, ['BNotMeta', 'ANotMeta']) - new_calls.clear() - - # TypeError: BNotMeta is neither a - # subclass, nor a superclass of int - with self.assertRaises(TypeError): - X = types.new_class("X", (C, int())) - with self.assertRaises(TypeError): - X = types.new_class("X", (int(), C)) - - -class SimpleNamespaceTests(unittest.TestCase): - - def test_constructor(self): - ns1 = types.SimpleNamespace() - ns2 = types.SimpleNamespace(x=1, y=2) - ns3 = types.SimpleNamespace(**dict(x=1, y=2)) - - with self.assertRaises(TypeError): - types.SimpleNamespace(1, 2, 3) - - self.assertEqual(len(ns1.__dict__), 0) - self.assertEqual(vars(ns1), {}) - self.assertEqual(len(ns2.__dict__), 2) - self.assertEqual(vars(ns2), {'y': 2, 'x': 1}) - self.assertEqual(len(ns3.__dict__), 2) - self.assertEqual(vars(ns3), {'y': 2, 'x': 1}) - - def test_unbound(self): - ns1 = vars(types.SimpleNamespace()) - ns2 = vars(types.SimpleNamespace(x=1, y=2)) - - self.assertEqual(ns1, {}) - self.assertEqual(ns2, {'y': 2, 'x': 1}) - - def test_underlying_dict(self): - ns1 = types.SimpleNamespace() - ns2 = types.SimpleNamespace(x=1, y=2) - ns3 = types.SimpleNamespace(a=True, b=False) - mapping = ns3.__dict__ - del ns3 - - self.assertEqual(ns1.__dict__, {}) - self.assertEqual(ns2.__dict__, {'y': 2, 'x': 1}) - self.assertEqual(mapping, dict(a=True, b=False)) - - def test_attrget(self): - ns = types.SimpleNamespace(x=1, y=2, w=3) - - self.assertEqual(ns.x, 1) - self.assertEqual(ns.y, 2) - self.assertEqual(ns.w, 3) - with self.assertRaises(AttributeError): - ns.z - - def test_attrset(self): - ns1 = types.SimpleNamespace() - ns2 = types.SimpleNamespace(x=1, y=2, w=3) - ns1.a = 'spam' - ns1.b = 'ham' - ns2.z = 4 - ns2.theta = None - - self.assertEqual(ns1.__dict__, dict(a='spam', b='ham')) - self.assertEqual(ns2.__dict__, dict(x=1, y=2, w=3, z=4, theta=None)) - - def test_attrdel(self): - ns1 = types.SimpleNamespace() - ns2 = types.SimpleNamespace(x=1, y=2, w=3) - - with self.assertRaises(AttributeError): - del ns1.spam - with self.assertRaises(AttributeError): - del ns2.spam - - del ns2.y - self.assertEqual(vars(ns2), dict(w=3, x=1)) - ns2.y = 'spam' - self.assertEqual(vars(ns2), dict(w=3, x=1, y='spam')) - del ns2.y - self.assertEqual(vars(ns2), dict(w=3, x=1)) - - ns1.spam = 5 - self.assertEqual(vars(ns1), dict(spam=5)) - del ns1.spam - self.assertEqual(vars(ns1), {}) - - def test_repr(self): - ns1 = types.SimpleNamespace(x=1, y=2, w=3) - ns2 = types.SimpleNamespace() - ns2.x = "spam" - ns2._y = 5 - - self.assertEqual(repr(ns1), "namespace(w=3, x=1, y=2)") - self.assertEqual(repr(ns2), "namespace(_y=5, x='spam')") - - def test_nested(self): - ns1 = types.SimpleNamespace(a=1, b=2) - ns2 = types.SimpleNamespace() - ns3 = types.SimpleNamespace(x=ns1) - ns2.spam = ns1 - ns2.ham = '?' - ns2.spam = ns3 - - self.assertEqual(vars(ns1), dict(a=1, b=2)) - self.assertEqual(vars(ns2), dict(spam=ns3, ham='?')) - self.assertEqual(ns2.spam, ns3) - self.assertEqual(vars(ns3), dict(x=ns1)) - self.assertEqual(ns3.x.a, 1) - - def test_recursive(self): - ns1 = types.SimpleNamespace(c='cookie') - ns2 = types.SimpleNamespace() - ns3 = types.SimpleNamespace(x=1) - ns1.spam = ns1 - ns2.spam = ns3 - ns3.spam = ns2 - - self.assertEqual(ns1.spam, ns1) - self.assertEqual(ns1.spam.spam, ns1) - self.assertEqual(ns1.spam.spam, ns1.spam) - self.assertEqual(ns2.spam, ns3) - self.assertEqual(ns3.spam, ns2) - self.assertEqual(ns2.spam.spam, ns2) - - def test_recursive_repr(self): - ns1 = types.SimpleNamespace(c='cookie') - ns2 = types.SimpleNamespace() - ns3 = types.SimpleNamespace(x=1) - ns1.spam = ns1 - ns2.spam = ns3 - ns3.spam = ns2 - - self.assertEqual(repr(ns1), - "namespace(c='cookie', spam=namespace(...))") - self.assertEqual(repr(ns2), - "namespace(spam=namespace(spam=namespace(...), x=1))") - - def test_as_dict(self): - ns = types.SimpleNamespace(spam='spamspamspam') - - with self.assertRaises(TypeError): - len(ns) - with self.assertRaises(TypeError): - iter(ns) - with self.assertRaises(TypeError): - 'spam' in ns - with self.assertRaises(TypeError): - ns['spam'] - - def test_main(): - run_unittest(TypesTests, MappingProxyTests, ClassCreationTests, - SimpleNamespaceTests) + run_unittest(TypesTests, MappingProxyTests) if __name__ == '__main__': test_main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_urlparse.py --- a/Lib/test/test_urlparse.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_urlparse.py Mon May 21 23:01:17 2012 -0400 @@ -524,11 +524,6 @@ self.assertEqual(p.port, 80) self.assertEqual(p.geturl(), url) - # Verify an illegal port is returned as None - url = b"HTTP://WWW.PYTHON.ORG:65536/doc/#frag" - p = urllib.parse.urlsplit(url) - self.assertEqual(p.port, None) - def test_attributes_bad_port(self): """Check handling of non-integer ports.""" p = urllib.parse.urlsplit("http://www.example.net:foo") @@ -641,20 +636,11 @@ ('s3', 'foo.com', '/stuff', '', '', '')) self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff"), ('x-newscheme', 'foo.com', '/stuff', '', '', '')) - self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff?query#fragment"), - ('x-newscheme', 'foo.com', '/stuff', '', 'query', 'fragment')) - self.assertEqual(urllib.parse.urlparse("x-newscheme://foo.com/stuff?query"), - ('x-newscheme', 'foo.com', '/stuff', '', 'query', '')) - # And for bytes... self.assertEqual(urllib.parse.urlparse(b"s3://foo.com/stuff"), (b's3', b'foo.com', b'/stuff', b'', b'', b'')) self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff"), (b'x-newscheme', b'foo.com', b'/stuff', b'', b'', b'')) - self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff?query#fragment"), - (b'x-newscheme', b'foo.com', b'/stuff', b'', b'query', b'fragment')) - self.assertEqual(urllib.parse.urlparse(b"x-newscheme://foo.com/stuff?query"), - (b'x-newscheme', b'foo.com', b'/stuff', b'', b'query', b'')) def test_mixed_types_rejected(self): # Several functions that process either strings or ASCII encoded bytes @@ -811,13 +797,6 @@ encoding='utf-8') self.assertRaises(TypeError, urllib.parse.quote, b'foo', errors='strict') - def test_issue14072(self): - p1 = urllib.parse.urlsplit('tel:+31-641044153') - self.assertEqual(p1.scheme, 'tel') - self.assertEqual(p1.path, '+31-641044153') - p2 = urllib.parse.urlsplit('tel:+31641044153') - self.assertEqual(p2.scheme, 'tel') - self.assertEqual(p2.path, '+31641044153') def test_main(): support.run_unittest(UrlParseTestCase) diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_venv.py --- a/Lib/test/test_venv.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,145 +0,0 @@ -""" -Test harness for the venv module. - -Copyright (C) 2011-2012 Vinay Sajip. -""" - -import os -import os.path -import shutil -import sys -import tempfile -from test.support import (captured_stdout, captured_stderr, run_unittest, - can_symlink) -import unittest -import venv - -class BaseTest(unittest.TestCase): - """Base class for venv tests.""" - - def setUp(self): - self.env_dir = tempfile.mkdtemp() - if os.name == 'nt': - self.bindir = 'Scripts' - self.ps3name = 'pysetup3.py' - self.lib = ('Lib',) - self.include = 'Include' - else: - self.bindir = 'bin' - self.ps3name = 'pysetup3' - self.lib = ('lib', 'python%s' % sys.version[:3]) - self.include = 'include' - if sys.platform == 'darwin' and '__PYTHONV_LAUNCHER__' in os.environ: - executable = os.environ['__PYTHONV_LAUNCHER__'] - else: - executable = sys.executable - self.exe = os.path.split(executable)[-1] - - def tearDown(self): - shutil.rmtree(self.env_dir) - - def run_with_capture(self, func, *args, **kwargs): - with captured_stdout() as output: - with captured_stderr() as error: - func(*args, **kwargs) - return output.getvalue(), error.getvalue() - - def get_env_file(self, *args): - return os.path.join(self.env_dir, *args) - - def get_text_file_contents(self, *args): - with open(self.get_env_file(*args), 'r') as f: - result = f.read() - return result - -class BasicTest(BaseTest): - """Test venv module functionality.""" - - def isdir(self, *args): - fn = self.get_env_file(*args) - self.assertTrue(os.path.isdir(fn)) - - def test_defaults(self): - """ - Test the create function with default arguments. - """ - shutil.rmtree(self.env_dir) - self.run_with_capture(venv.create, self.env_dir) - self.isdir(self.bindir) - self.isdir(self.include) - self.isdir(*self.lib) - data = self.get_text_file_contents('pyvenv.cfg') - if sys.platform == 'darwin' and ('__PYTHONV_LAUNCHER__' - in os.environ): - executable = os.environ['__PYTHONV_LAUNCHER__'] - else: - executable = sys.executable - path = os.path.dirname(executable) - self.assertIn('home = %s' % path, data) - data = self.get_text_file_contents(self.bindir, self.ps3name) - self.assertTrue(data.startswith('#!%s%s' % (self.env_dir, os.sep))) - fn = self.get_env_file(self.bindir, self.exe) - if not os.path.exists(fn): # diagnostics for Windows buildbot failures - bd = self.get_env_file(self.bindir) - print('Contents of %r:' % bd) - print(' %r' % os.listdir(bd)) - self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) - - def test_overwrite_existing(self): - """ - Test control of overwriting an existing environment directory. - """ - self.assertRaises(ValueError, venv.create, self.env_dir) - builder = venv.EnvBuilder(clear=True) - builder.create(self.env_dir) - - def test_upgrade(self): - """ - Test upgrading an existing environment directory. - """ - builder = venv.EnvBuilder(upgrade=True) - self.run_with_capture(builder.create, self.env_dir) - self.isdir(self.bindir) - self.isdir(self.include) - self.isdir(*self.lib) - fn = self.get_env_file(self.bindir, self.exe) - if not os.path.exists(fn): # diagnostics for Windows buildbot failures - bd = self.get_env_file(self.bindir) - print('Contents of %r:' % bd) - print(' %r' % os.listdir(bd)) - self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) - - def test_isolation(self): - """ - Test isolation from system site-packages - """ - for ssp, s in ((True, 'true'), (False, 'false')): - builder = venv.EnvBuilder(clear=True, system_site_packages=ssp) - builder.create(self.env_dir) - data = self.get_text_file_contents('pyvenv.cfg') - self.assertIn('include-system-site-packages = %s\n' % s, data) - - @unittest.skipUnless(can_symlink(), 'Needs symlinks') - def test_symlinking(self): - """ - Test symlinking works as expected - """ - for usl in (False, True): - builder = venv.EnvBuilder(clear=True, symlinks=usl) - if (usl and sys.platform == 'darwin' and - '__PYTHONV_LAUNCHER__' in os.environ): - self.assertRaises(ValueError, builder.create, self.env_dir) - else: - builder.create(self.env_dir) - fn = self.get_env_file(self.bindir, self.exe) - # Don't test when False, because e.g. 'python' is always - # symlinked to 'python3.3' in the env, even when symlinking in - # general isn't wanted. - if usl: - self.assertTrue(os.path.islink(fn)) - -def test_main(): - run_unittest(BasicTest) - -if __name__ == "__main__": - test_main() diff -r 2059910e7d76 -r ac776ef41428 Lib/test/test_xml_etree.py --- a/Lib/test/test_xml_etree.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/test/test_xml_etree.py Mon May 21 23:01:17 2012 -0400 @@ -62,22 +62,6 @@ """ -SAMPLE_XML_NS_ELEMS = """ - - - - Apples - Bananas - - - - - African Coffee Table - 80 - 120 - - -""" def sanity(): """ @@ -1930,10 +1914,6 @@ self.assertIsInstance(mye, MyElement) self.assertEqual(mye.tag, 'foo') - # test that attribute assignment works (issue 14849) - mye.text = "joe" - self.assertEqual(mye.text, "joe") - def test_Element_subclass_constructor(self): class MyElement(ET.Element): def __init__(self, tag, attrib={}, **extra): @@ -1959,8 +1939,6 @@ ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' 'text') - sample2 = '''sometext''' - def test_dummy_builder(self): class BaseDummyBuilder: def close(self): @@ -1981,34 +1959,13 @@ parser.feed(self.sample1) self.assertIsNone(parser.close()) - def test_subclass(self): - class MyTreeBuilder(ET.TreeBuilder): - def foobar(self, x): - return x * 2 + # XXX in _elementtree, the constructor of TreeBuilder expects no + # arguments + @unittest.expectedFailure + def test_element_factory(self): + tb = ET.TreeBuilder(element_factory=lambda: ET.Element()) - tb = MyTreeBuilder() - self.assertEqual(tb.foobar(10), 20) - - parser = ET.XMLParser(target=tb) - parser.feed(self.sample1) - - e = parser.close() - self.assertEqual(e.tag, 'html') - - def test_element_factory(self): - lst = [] - def myfactory(tag, attrib): - nonlocal lst - lst.append(tag) - return ET.Element(tag, attrib) - - tb = ET.TreeBuilder(element_factory=myfactory) - parser = ET.XMLParser(target=tb) - parser.feed(self.sample2) - parser.close() - - self.assertEqual(lst, ['toplevel']) - + @unittest.expectedFailure # XXX issue 14007 with C ElementTree def test_doctype(self): class DoctypeParser: _doctype = None @@ -2027,52 +1984,6 @@ 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')) -class XMLParserTest(unittest.TestCase): - sample1 = '22' - sample2 = ('' - 'text') - - def _check_sample_element(self, e): - self.assertEqual(e.tag, 'file') - self.assertEqual(e[0].tag, 'line') - self.assertEqual(e[0].text, '22') - - def test_constructor_args(self): - # Positional args. The first (html) is not supported, but should be - # nevertheless correctly accepted. - parser = ET.XMLParser(None, ET.TreeBuilder(), 'utf-8') - parser.feed(self.sample1) - self._check_sample_element(parser.close()) - - # Now as keyword args. - parser2 = ET.XMLParser(encoding='utf-8', html=[{}], target=ET.TreeBuilder()) - parser2.feed(self.sample1) - self._check_sample_element(parser2.close()) - - def test_subclass(self): - class MyParser(ET.XMLParser): - pass - parser = MyParser() - parser.feed(self.sample1) - self._check_sample_element(parser.close()) - - def test_subclass_doctype(self): - _doctype = None - class MyParserWithDoctype(ET.XMLParser): - def doctype(self, name, pubid, system): - nonlocal _doctype - _doctype = (name, pubid, system) - - parser = MyParserWithDoctype() - parser.feed(self.sample2) - parser.close() - self.assertEqual(_doctype, - ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN', - 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')) - - class NoAcceleratorTest(unittest.TestCase): # Test that the C accelerator was not imported for pyET def test_correct_import_pyET(self): @@ -2080,17 +1991,6 @@ self.assertEqual(pyET.SubElement.__module__, 'xml.etree.ElementTree') -class NamespaceParseTest(unittest.TestCase): - def test_find_with_namespace(self): - nsmap = {'h': 'hello', 'f': 'foo'} - doc = ET.fromstring(SAMPLE_XML_NS_ELEMS) - - self.assertEqual(len(doc.findall('{hello}table', nsmap)), 1) - self.assertEqual(len(doc.findall('.//{hello}td', nsmap)), 2) - self.assertEqual(len(doc.findall('.//{foo}name', nsmap)), 1) - - - class ElementSlicingTest(unittest.TestCase): def _elem_tags(self, elemlist): return [e.tag for e in elemlist] @@ -2198,41 +2098,6 @@ ERRORS.codes[ERRORS.XML_ERROR_SYNTAX]) -class KeywordArgsTest(unittest.TestCase): - # Test various issues with keyword arguments passed to ET.Element - # constructor and methods - def test_issue14818(self): - x = ET.XML("foo") - self.assertEqual(x.find('a', None), - x.find(path='a', namespaces=None)) - self.assertEqual(x.findtext('a', None, None), - x.findtext(path='a', default=None, namespaces=None)) - self.assertEqual(x.findall('a', None), - x.findall(path='a', namespaces=None)) - self.assertEqual(list(x.iterfind('a', None)), - list(x.iterfind(path='a', namespaces=None))) - - self.assertEqual(ET.Element('a').attrib, {}) - elements = [ - ET.Element('a', dict(href="#", id="foo")), - ET.Element('a', attrib=dict(href="#", id="foo")), - ET.Element('a', dict(href="#"), id="foo"), - ET.Element('a', href="#", id="foo"), - ET.Element('a', dict(href="#", id="foo"), href="#", id="foo"), - ] - for e in elements: - self.assertEqual(e.tag, 'a') - self.assertEqual(e.attrib, dict(href="#", id="foo")) - - e2 = ET.SubElement(elements[0], 'foobar', attrib={'key1': 'value1'}) - self.assertEqual(e2.attrib['key1'], 'value1') - - with self.assertRaisesRegex(TypeError, 'must be dict, not str'): - ET.Element('a', "I'm not a dict") - with self.assertRaisesRegex(TypeError, 'must be dict, not str'): - ET.Element('a', attrib="I'm not a dict") - - # -------------------------------------------------------------------- @@ -2288,10 +2153,7 @@ StringIOTest, ParseErrorTest, ElementTreeTest, - NamespaceParseTest, - TreeBuilderTest, - XMLParserTest, - KeywordArgsTest] + TreeBuilderTest] if module is pyET: # Run the tests specific to the Python implementation test_classes += [NoAcceleratorTest] diff -r 2059910e7d76 -r ac776ef41428 Lib/textwrap.py --- a/Lib/textwrap.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/textwrap.py Mon May 21 23:01:17 2012 -0400 @@ -39,11 +39,8 @@ of wrapped output; also counts towards each line's width. expand_tabs (default: true) Expand tabs in input text to spaces before further processing. - Each tab will become 0 .. 'tabsize' spaces, depending on its position - in its line. If false, each tab is treated as a single character. - tabsize (default: 8) - Expand tabs in input text to 0 .. 'tabsize' spaces, unless - 'expand_tabs' is false. + Each tab will become 1 .. 8 spaces, depending on its position in + its line. If false, each tab is treated as a single character. replace_whitespace (default: true) Replace all whitespace characters in the input text by spaces after tab expansion. Note that if expand_tabs is false and @@ -103,8 +100,7 @@ fix_sentence_endings=False, break_long_words=True, drop_whitespace=True, - break_on_hyphens=True, - tabsize=8): + break_on_hyphens=True): self.width = width self.initial_indent = initial_indent self.subsequent_indent = subsequent_indent @@ -114,7 +110,6 @@ self.break_long_words = break_long_words self.drop_whitespace = drop_whitespace self.break_on_hyphens = break_on_hyphens - self.tabsize = tabsize # -- Private methods ----------------------------------------------- @@ -128,7 +123,7 @@ becomes " foo bar baz". """ if self.expand_tabs: - text = text.expandtabs(self.tabsize) + text = text.expandtabs() if self.replace_whitespace: text = text.translate(self.unicode_whitespace_trans) return text diff -r 2059910e7d76 -r ac776ef41428 Lib/tkinter/_fix.py --- a/Lib/tkinter/_fix.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/tkinter/_fix.py Mon May 21 23:01:17 2012 -0400 @@ -46,10 +46,10 @@ s = "\\" + s[3:] return s -prefix = os.path.join(sys.base_prefix,"tcl") +prefix = os.path.join(sys.prefix,"tcl") if not os.path.exists(prefix): # devdir/../tcltk/lib - prefix = os.path.join(sys.base_prefix, os.path.pardir, "tcltk", "lib") + prefix = os.path.join(sys.prefix, os.path.pardir, "tcltk", "lib") prefix = os.path.abspath(prefix) # if this does not exist, no further search is needed if os.path.exists(prefix): diff -r 2059910e7d76 -r ac776ef41428 Lib/token.py --- a/Lib/token.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/token.py Mon May 21 23:01:17 2012 -0400 @@ -70,7 +70,7 @@ tok_name = {value: name for name, value in globals().items() - if isinstance(value, int) and not name.startswith('_')} + if isinstance(value, int)} __all__.extend(tok_name.values()) def ISTERMINAL(x): diff -r 2059910e7d76 -r ac776ef41428 Lib/trace.py --- a/Lib/trace.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/trace.py Mon May 21 23:01:17 2012 -0400 @@ -39,8 +39,8 @@ # create a Trace object, telling it what to ignore, and whether to # do tracing or line-counting or both. - tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,], - trace=0, count=1) + tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0, + count=1) # run the new command using the given tracer tracer.run('main()') # make a report, placing output in /tmp @@ -61,10 +61,6 @@ import dis import pickle from warnings import warn as _warn -try: - from time import monotonic as _time -except ImportError: - from time import time as _time try: import threading @@ -480,7 +476,7 @@ self._caller_cache = {} self.start_time = None if timing: - self.start_time = _time() + self.start_time = time.time() if countcallers: self.globaltrace = self.globaltrace_trackcallers elif countfuncs: @@ -618,7 +614,7 @@ self.counts[key] = self.counts.get(key, 0) + 1 if self.start_time: - print('%.2f' % (_time() - self.start_time), end=' ') + print('%.2f' % (time.time() - self.start_time), end=' ') bname = os.path.basename(filename) print("%s(%d): %s" % (bname, lineno, linecache.getline(filename, lineno)), end='') @@ -631,7 +627,7 @@ lineno = frame.f_lineno if self.start_time: - print('%.2f' % (_time() - self.start_time), end=' ') + print('%.2f' % (time.time() - self.start_time), end=' ') bname = os.path.basename(filename) print("%s(%d): %s" % (bname, lineno, linecache.getline(filename, lineno)), end='') @@ -753,10 +749,10 @@ # should I also call expanduser? (after all, could use $HOME) s = s.replace("$prefix", - os.path.join(sys.base_prefix, "lib", + os.path.join(sys.prefix, "lib", "python" + sys.version[:3])) s = s.replace("$exec_prefix", - os.path.join(sys.base_exec_prefix, "lib", + os.path.join(sys.exec_prefix, "lib", "python" + sys.version[:3])) s = os.path.normpath(s) ignore_dirs.append(s) diff -r 2059910e7d76 -r ac776ef41428 Lib/types.py --- a/Lib/types.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/types.py Mon May 21 23:01:17 2012 -0400 @@ -13,7 +13,6 @@ LambdaType = type(lambda: None) # Same as FunctionType CodeType = type(_f.__code__) MappingProxyType = type(type.__dict__) -SimpleNamespace = type(sys.implementation) def _g(): yield 1 @@ -41,61 +40,3 @@ MemberDescriptorType = type(FunctionType.__globals__) del sys, _f, _g, _C, # Not for export - - -# Provide a PEP 3115 compliant mechanism for class creation -def new_class(name, bases=(), kwds=None, exec_body=None): - """Create a class object dynamically using the appropriate metaclass.""" - meta, ns, kwds = prepare_class(name, bases, kwds) - if exec_body is not None: - exec_body(ns) - return meta(name, bases, ns, **kwds) - -def prepare_class(name, bases=(), kwds=None): - """Call the __prepare__ method of the appropriate metaclass. - - Returns (metaclass, namespace, kwds) as a 3-tuple - - *metaclass* is the appropriate metaclass - *namespace* is the prepared class namespace - *kwds* is an updated copy of the passed in kwds argument with any - 'metaclass' entry removed. If no kwds argument is passed in, this will - be an empty dict. - """ - if kwds is None: - kwds = {} - else: - kwds = dict(kwds) # Don't alter the provided mapping - if 'metaclass' in kwds: - meta = kwds.pop('metaclass') - else: - if bases: - meta = type(bases[0]) - else: - meta = type - if isinstance(meta, type): - # when meta is a type, we first determine the most-derived metaclass - # instead of invoking the initial candidate directly - meta = _calculate_meta(meta, bases) - if hasattr(meta, '__prepare__'): - ns = meta.__prepare__(name, bases, **kwds) - else: - ns = {} - return meta, ns, kwds - -def _calculate_meta(meta, bases): - """Calculate the most derived metaclass.""" - winner = meta - for base in bases: - base_meta = type(base) - if issubclass(winner, base_meta): - continue - if issubclass(base_meta, winner): - winner = base_meta - continue - # else: - raise TypeError("metaclass conflict: " - "the metaclass of a derived class " - "must be a (non-strict) subclass " - "of the metaclasses of all its bases") - return winner diff -r 2059910e7d76 -r ac776ef41428 Lib/unittest/mock.py --- a/Lib/unittest/mock.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/unittest/mock.py Mon May 21 23:01:17 2012 -0400 @@ -39,9 +39,6 @@ FILTER_DIR = True -# Workaround for issue #12370 -# Without this, the __class__ properties wouldn't be set correctly -_safe_super = super def _is_instance_mock(obj): # can't use isinstance on Mock objects because they override __class__ @@ -400,7 +397,7 @@ if kwargs: self.configure_mock(**kwargs) - _safe_super(NonCallableMock, self).__init__( + super(NonCallableMock, self).__init__( spec, wraps, name, spec_set, parent, _spec_state ) @@ -510,8 +507,6 @@ self.method_calls = _CallList() for child in self._mock_children.values(): - if isinstance(child, _SpecState): - continue child.reset_mock() ret = self._mock_return_value @@ -666,7 +661,6 @@ # but not method calls _check_and_set_parent(self, value, None, name) setattr(type(self), name, value) - self._mock_children[name] = value elif name == '__class__': self._spec_class = value return @@ -826,7 +820,7 @@ _spec_state=None, _new_name='', _new_parent=None, **kwargs): self.__dict__['_mock_return_value'] = return_value - _safe_super(CallableMixin, self).__init__( + super(CallableMixin, self).__init__( spec, wraps, name, spec_set, parent, _spec_state, _new_name, _new_parent, **kwargs ) @@ -1696,7 +1690,7 @@ class MagicMixin(object): def __init__(self, *args, **kw): - _safe_super(MagicMixin, self).__init__(*args, **kw) + super(MagicMixin, self).__init__(*args, **kw) self._mock_set_magics() diff -r 2059910e7d76 -r ac776ef41428 Lib/unittest/test/testmock/testhelpers.py --- a/Lib/unittest/test/testmock/testhelpers.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/unittest/test/testmock/testhelpers.py Mon May 21 23:01:17 2012 -0400 @@ -355,13 +355,6 @@ self.assertEqual(mock(), 'foo') - def test_autospec_reset_mock(self): - m = create_autospec(int) - int(m) - m.reset_mock() - self.assertEqual(m.__int__.call_count, 0) - - def test_mocking_unbound_methods(self): class Foo(object): def foo(self, foo): diff -r 2059910e7d76 -r ac776ef41428 Lib/unittest/test/testmock/testmagicmethods.py --- a/Lib/unittest/test/testmock/testmagicmethods.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/unittest/test/testmock/testmagicmethods.py Mon May 21 23:01:17 2012 -0400 @@ -345,14 +345,6 @@ self.assertEqual(mock[1][2][3], 3) - def test_magic_method_reset_mock(self): - mock = MagicMock() - str(mock) - self.assertTrue(mock.__str__.called) - mock.reset_mock() - self.assertFalse(mock.__str__.called) - - def test_dir(self): # overriding the default implementation for mock in Mock(), MagicMock(): diff -r 2059910e7d76 -r ac776ef41428 Lib/urllib/parse.py --- a/Lib/urllib/parse.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/urllib/parse.py Mon May 21 23:01:17 2012 -0400 @@ -44,9 +44,16 @@ 'imap', 'wais', 'file', 'mms', 'https', 'shttp', 'snews', 'prospero', 'rtsp', 'rtspu', 'rsync', '', 'svn', 'svn+ssh', 'sftp', 'nfs', 'git', 'git+ssh'] +non_hierarchical = ['gopher', 'hdl', 'mailto', 'news', + 'telnet', 'wais', 'imap', 'snews', 'sip', 'sips'] uses_params = ['ftp', 'hdl', 'prospero', 'http', 'imap', 'https', 'shttp', 'rtsp', 'rtspu', 'sip', 'sips', 'mms', '', 'sftp'] +uses_query = ['http', 'wais', 'imap', 'https', 'shttp', 'mms', + 'gopher', 'rtsp', 'rtspu', 'sip', 'sips', ''] +uses_fragment = ['ftp', 'hdl', 'http', 'gopher', 'news', + 'nntp', 'wais', 'https', 'shttp', 'snews', + 'file', 'prospero', ''] # Characters valid in scheme names scheme_chars = ('abcdefghijklmnopqrstuvwxyz' @@ -143,9 +150,6 @@ port = self._hostinfo[1] if port is not None: port = int(port, 10) - # Return None on an illegal port - if not ( 0 <= port <= 65535): - return None return port @@ -341,21 +345,21 @@ if c not in scheme_chars: break else: - # make sure "url" is not actually a port number (in which case - # "scheme" is really part of the path) - rest = url[i+1:] - if not rest or any(c not in '0123456789' for c in rest): - # not a port number - scheme, url = url[:i].lower(), rest + try: + # make sure "url" is not actually a port number (in which case + # "scheme" is really part of the path + _testportnum = int(url[i+1:]) + except ValueError: + scheme, url = url[:i].lower(), url[i+1:] if url[:2] == '//': netloc, url = _splitnetloc(url, 2) if (('[' in netloc and ']' not in netloc) or (']' in netloc and '[' not in netloc)): raise ValueError("Invalid IPv6 URL") - if allow_fragments and '#' in url: + if allow_fragments and scheme in uses_fragment and '#' in url: url, fragment = url.split('#', 1) - if '?' in url: + if scheme in uses_query and '?' in url: url, query = url.split('?', 1) v = SplitResult(scheme, netloc, url, query, fragment) _parse_cache[key] = v @@ -707,7 +711,7 @@ def quote_from_bytes(bs, safe='/'): """Like quote(), but accepts a bytes object rather than a str, and does not perform string-to-bytes encoding. It always returns an ASCII string. - quote_from_bytes(b'abc def\x3f') -> 'abc%20def%3f' + quote_from_bytes(b'abc def\xab') -> 'abc%20def%AB' """ if not isinstance(bs, (bytes, bytearray)): raise TypeError("quote_from_bytes() expected bytes") diff -r 2059910e7d76 -r ac776ef41428 Lib/venv/__init__.py --- a/Lib/venv/__init__.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,400 +0,0 @@ -""" -Virtual environment (venv) package for Python. Based on PEP 405. - -Copyright (C) 20011-2012 Vinay Sajip. All Rights Reserved. - -usage: python -m venv [-h] [--system-site-packages] [--symlinks] [--clear] - [--upgrade] - ENV_DIR [ENV_DIR ...] - -Creates virtual Python environments in one or more target directories. - -positional arguments: - ENV_DIR A directory to create the environment in. - -optional arguments: - -h, --help show this help message and exit - --system-site-packages - Give the virtual environment access to the system - site-packages dir. - --symlinks Attempt to symlink rather than copy. - --clear Delete the environment directory if it already exists. - If not specified and the directory exists, an error is - raised. - --upgrade Upgrade the environment directory to use this version - of Python, assuming Python has been upgraded in-place. -""" -import base64 -import io -import logging -import os -import os.path -import shutil -import sys -import sysconfig -try: - import threading -except ImportError: - threading = None -import zipfile - -logger = logging.getLogger(__name__) - -class Context: - """ - Holds information about a current venv creation/upgrade request. - """ - pass - - -class EnvBuilder: - """ - This class exists to allow virtual environment creation to be - customised. The constructor parameters determine the builder's - behaviour when called upon to create a virtual environment. - - By default, the builder makes the system (global) site-packages dir - available to the created environment. - - By default, the creation process uses symlinks wherever possible. - - :param system_site_packages: If True, the system (global) site-packages - dir is available to created environments. - :param clear: If True and the target directory exists, it is deleted. - Otherwise, if the target directory exists, an error is - raised. - :param symlinks: If True, attempt to symlink rather than copy files into - virtual environment. - :param upgrade: If True, upgrade an existing virtual environment. - """ - - def __init__(self, system_site_packages=False, clear=False, - symlinks=False, upgrade=False): - self.system_site_packages = system_site_packages - self.clear = clear - self.symlinks = symlinks - self.upgrade = upgrade - - def create(self, env_dir): - """ - Create a virtual environment in a directory. - - :param env_dir: The target directory to create an environment in. - - """ - if (self.symlinks and - sys.platform == 'darwin' and - sysconfig.get_config_var('PYTHONFRAMEWORK')): - # Symlinking the stub executable in an OSX framework build will - # result in a broken virtual environment. - raise ValueError( - 'Symlinking is not supported on OSX framework Python.') - env_dir = os.path.abspath(env_dir) - context = self.ensure_directories(env_dir) - self.create_configuration(context) - self.setup_python(context) - if not self.upgrade: - self.setup_scripts(context) - self.post_setup(context) - - def ensure_directories(self, env_dir): - """ - Create the directories for the environment. - - Returns a context object which holds paths in the environment, - for use by subsequent logic. - """ - - def create_if_needed(d): - if not os.path.exists(d): - os.makedirs(d) - - if os.path.exists(env_dir) and not (self.clear or self.upgrade): - raise ValueError('Directory exists: %s' % env_dir) - if os.path.exists(env_dir) and self.clear: - shutil.rmtree(env_dir) - context = Context() - context.env_dir = env_dir - context.env_name = os.path.split(env_dir)[1] - context.prompt = '(%s) ' % context.env_name - create_if_needed(env_dir) - env = os.environ - if sys.platform == 'darwin' and '__PYTHONV_LAUNCHER__' in env: - executable = os.environ['__PYTHONV_LAUNCHER__'] - else: - executable = sys.executable - dirname, exename = os.path.split(os.path.abspath(executable)) - context.executable = executable - context.python_dir = dirname - context.python_exe = exename - if sys.platform == 'win32': - binname = 'Scripts' - incpath = 'Include' - libpath = os.path.join(env_dir, 'Lib', 'site-packages') - else: - binname = 'bin' - incpath = 'include' - libpath = os.path.join(env_dir, 'lib', 'python%d.%d' % sys.version_info[:2], 'site-packages') - context.inc_path = path = os.path.join(env_dir, incpath) - create_if_needed(path) - create_if_needed(libpath) - context.bin_path = binpath = os.path.join(env_dir, binname) - context.bin_name = binname - context.env_exe = os.path.join(binpath, exename) - create_if_needed(binpath) - return context - - def create_configuration(self, context): - """ - Create a configuration file indicating where the environment's Python - was copied from, and whether the system site-packages should be made - available in the environment. - - :param context: The information for the environment creation request - being processed. - """ - context.cfg_path = path = os.path.join(context.env_dir, 'pyvenv.cfg') - with open(path, 'w', encoding='utf-8') as f: - f.write('home = %s\n' % context.python_dir) - if self.system_site_packages: - incl = 'true' - else: - incl = 'false' - f.write('include-system-site-packages = %s\n' % incl) - f.write('version = %d.%d.%d\n' % sys.version_info[:3]) - - if os.name == 'nt': - def include_binary(self, f): - if f.endswith(('.pyd', '.dll')): - result = True - else: - result = f.startswith('python') and f.endswith('.exe') - return result - - def symlink_or_copy(self, src, dst): - """ - Try symlinking a file, and if that fails, fall back to copying. - """ - force_copy = not self.symlinks - if not force_copy: - try: - if not os.path.islink(dst): # can't link to itself! - os.symlink(src, dst) - except Exception: # may need to use a more specific exception - logger.warning('Unable to symlink %r to %r', src, dst) - force_copy = True - if force_copy: - shutil.copyfile(src, dst) - - def setup_python(self, context): - """ - Set up a Python executable in the environment. - - :param context: The information for the environment creation request - being processed. - """ - binpath = context.bin_path - exename = context.python_exe - path = context.env_exe - copier = self.symlink_or_copy - copier(context.executable, path) - dirname = context.python_dir - if os.name != 'nt': - if not os.path.islink(path): - os.chmod(path, 0o755) - for suffix in ('python', 'python3'): - path = os.path.join(binpath, suffix) - if not os.path.exists(path): - os.symlink(exename, path) - else: - subdir = 'DLLs' - include = self.include_binary - files = [f for f in os.listdir(dirname) if include(f)] - for f in files: - src = os.path.join(dirname, f) - dst = os.path.join(binpath, f) - if dst != context.env_exe: # already done, above - copier(src, dst) - dirname = os.path.join(dirname, subdir) - if os.path.isdir(dirname): - files = [f for f in os.listdir(dirname) if include(f)] - for f in files: - src = os.path.join(dirname, f) - dst = os.path.join(binpath, f) - copier(src, dst) - # copy init.tcl over - for root, dirs, files in os.walk(context.python_dir): - if 'init.tcl' in files: - tcldir = os.path.basename(root) - tcldir = os.path.join(context.env_dir, 'Lib', tcldir) - os.makedirs(tcldir) - src = os.path.join(root, 'init.tcl') - dst = os.path.join(tcldir, 'init.tcl') - shutil.copyfile(src, dst) - break - - def setup_scripts(self, context): - """ - Set up scripts into the created environment from a directory. - - This method installs the default scripts into the environment - being created. You can prevent the default installation by overriding - this method if you really need to, or if you need to specify - a different location for the scripts to install. By default, the - 'scripts' directory in the venv package is used as the source of - scripts to install. - """ - path = os.path.abspath(os.path.dirname(__file__)) - path = os.path.join(path, 'scripts') - self.install_scripts(context, path) - - def post_setup(self, context): - """ - Hook for post-setup modification of the venv. Subclasses may install - additional packages or scripts here, add activation shell scripts, etc. - - :param context: The information for the environment creation request - being processed. - """ - pass - - def replace_variables(self, text, context): - """ - Replace variable placeholders in script text with context-specific - variables. - - Return the text passed in , but with variables replaced. - - :param text: The text in which to replace placeholder variables. - :param context: The information for the environment creation request - being processed. - """ - text = text.replace('__VENV_DIR__', context.env_dir) - text = text.replace('__VENV_NAME__', context.prompt) - text = text.replace('__VENV_BIN_NAME__', context.bin_name) - text = text.replace('__VENV_PYTHON__', context.env_exe) - return text - - def install_scripts(self, context, path): - """ - Install scripts into the created environment from a directory. - - :param context: The information for the environment creation request - being processed. - :param path: Absolute pathname of a directory containing script. - Scripts in the 'common' subdirectory of this directory, - and those in the directory named for the platform - being run on, are installed in the created environment. - Placeholder variables are replaced with environment- - specific values. - """ - binpath = context.bin_path - plen = len(path) - for root, dirs, files in os.walk(path): - if root == path: # at top-level, remove irrelevant dirs - for d in dirs[:]: - if d not in ('common', os.name): - dirs.remove(d) - continue # ignore files in top level - for f in files: - srcfile = os.path.join(root, f) - suffix = root[plen:].split(os.sep)[2:] - if not suffix: - dstdir = binpath - else: - dstdir = os.path.join(binpath, *suffix) - if not os.path.exists(dstdir): - os.makedirs(dstdir) - dstfile = os.path.join(dstdir, f) - with open(srcfile, 'rb') as f: - data = f.read() - if srcfile.endswith('.exe'): - mode = 'wb' - else: - mode = 'w' - data = data.decode('utf-8') - data = self.replace_variables(data, context) - with open(dstfile, mode) as f: - f.write(data) - os.chmod(dstfile, 0o755) - - -def create(env_dir, system_site_packages=False, clear=False, symlinks=False): - """ - Create a virtual environment in a directory. - - By default, makes the system (global) site-packages dir available to - the created environment. - - :param env_dir: The target directory to create an environment in. - :param system_site_packages: If True, the system (global) site-packages - dir is available to the environment. - :param clear: If True and the target directory exists, it is deleted. - Otherwise, if the target directory exists, an error is - raised. - :param symlinks: If True, attempt to symlink rather than copy files into - virtual environment. - """ - builder = EnvBuilder(system_site_packages=system_site_packages, - clear=clear, symlinks=symlinks) - builder.create(env_dir) - -def main(args=None): - compatible = True - if sys.version_info < (3, 3): - compatible = False - elif not hasattr(sys, 'base_prefix'): - compatible = False - if not compatible: - raise ValueError('This script is only for use with ' - 'Python 3.3 (pythonv variant)') - else: - import argparse - - parser = argparse.ArgumentParser(prog=__name__, - description='Creates virtual Python ' - 'environments in one or ' - 'more target ' - 'directories.') - parser.add_argument('dirs', metavar='ENV_DIR', nargs='+', - help='A directory to create the environment in.') - parser.add_argument('--system-site-packages', default=False, - action='store_true', dest='system_site', - help='Give the virtual environment access to the ' - 'system site-packages dir.') - if os.name == 'nt' or (sys.platform == 'darwin' and - sysconfig.get_config_var('PYTHONFRAMEWORK')): - use_symlinks = False - else: - use_symlinks = True - parser.add_argument('--symlinks', default=use_symlinks, - action='store_true', dest='symlinks', - help="Attempt to symlink rather than copy.") - parser.add_argument('--clear', default=False, action='store_true', - dest='clear', help='Delete the environment ' - 'directory if it already ' - 'exists. If not specified and ' - 'the directory exists, an error' - ' is raised.') - parser.add_argument('--upgrade', default=False, action='store_true', - dest='upgrade', help='Upgrade the environment ' - 'directory to use this version ' - 'of Python, assuming Python ' - 'has been upgraded in-place.') - options = parser.parse_args(args) - if options.upgrade and options.clear: - raise ValueError('you cannot supply --upgrade and --clear together.') - builder = EnvBuilder(system_site_packages=options.system_site, - clear=options.clear, symlinks=options.symlinks, - upgrade=options.upgrade) - for d in options.dirs: - builder.create(d) - -if __name__ == '__main__': - rc = 1 - try: - main() - rc = 0 - except Exception as e: - print('Error: %s' % e, file=sys.stderr) - sys.exit(rc) diff -r 2059910e7d76 -r ac776ef41428 Lib/venv/__main__.py --- a/Lib/venv/__main__.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -import sys -from . import main - -rc = 1 -try: - main() - rc = 0 -except Exception as e: - print('Error: %s' % e, file=sys.stderr) -sys.exit(rc) diff -r 2059910e7d76 -r ac776ef41428 Lib/venv/scripts/nt/Activate.ps1 --- a/Lib/venv/scripts/nt/Activate.ps1 Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -$env:VIRTUAL_ENV="__VENV_DIR__" - -# Revert to original values -if (Test-Path function:_OLD_VIRTUAL_PROMPT) { - copy-item function:_OLD_VIRTUAL_PROMPT function:prompt - remove-item function:_OLD_VIRTUAL_PROMPT -} - -if (Test-Path env:_OLD_VIRTUAL_PYTHONHOME) { - copy-item env:_OLD_VIRTUAL_PYTHONHOME env:PYTHONHOME - remove-item env:_OLD_VIRTUAL_PYTHONHOME -} - -if (Test-Path env:_OLD_VIRTUAL_PATH) { - copy-item env:_OLD_VIRTUAL_PATH env:PATH - remove-item env:_OLD_VIRTUAL_PATH -} - -# Set the prompt to include the env name -copy-item function:prompt function:_OLD_VIRTUAL_PROMPT -function prompt { - Write-Host -NoNewline -ForegroundColor Green [__VENV_NAME__] - _OLD_VIRTUAL_PROMPT -} - -# Clear PYTHONHOME -if (Test-Path env:PYTHONHOME) { - copy-item env:PYTHONHOME env:_OLD_VIRTUAL_PYTHONHOME - remove-item env:PYTHONHOME -} - -# Add the venv to the PATH -copy-item env:PATH env:_OLD_VIRTUAL_PATH -$env:PATH = "$env:VIRTUAL_ENV\__VENV_BIN_NAME__;$env:PATH" diff -r 2059910e7d76 -r ac776ef41428 Lib/venv/scripts/nt/Deactivate.ps1 --- a/Lib/venv/scripts/nt/Deactivate.ps1 Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,19 +0,0 @@ -# Revert to original values -if (Test-Path function:_OLD_VIRTUAL_PROMPT) { - copy-item function:_OLD_VIRTUAL_PROMPT function:prompt - remove-item function:_OLD_VIRTUAL_PROMPT -} - -if (Test-Path env:_OLD_VIRTUAL_PYTHONHOME) { - copy-item env:_OLD_VIRTUAL_PYTHONHOME env:PYTHONHOME - remove-item env:_OLD_VIRTUAL_PYTHONHOME -} - -if (Test-Path env:_OLD_VIRTUAL_PATH) { - copy-item env:_OLD_VIRTUAL_PATH env:PATH - remove-item env:_OLD_VIRTUAL_PATH -} - -if (Test-Path env:VIRTUAL_ENV) { - remove-item env:VIRTUAL_ENV -} diff -r 2059910e7d76 -r ac776ef41428 Lib/venv/scripts/nt/activate.bat --- a/Lib/venv/scripts/nt/activate.bat Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -@echo off -set VIRTUAL_ENV=__VENV_DIR__ - -if not defined PROMPT ( - set PROMPT=$P$G -) - -if defined _OLD_VIRTUAL_PROMPT ( - set PROMPT=%_OLD_VIRTUAL_PROMPT% -) - -if defined _OLD_VIRTUAL_PYTHONHOME ( - set PYTHONHOME=%_OLD_VIRTUAL_PYTHONHOME% -) - -set _OLD_VIRTUAL_PROMPT=%PROMPT% -set PROMPT=__VENV_NAME__%PROMPT% - -if defined PYTHONHOME ( - set _OLD_VIRTUAL_PYTHONHOME=%PYTHONHOME% - set PYTHONHOME= -) - -if defined _OLD_VIRTUAL_PATH set PATH=%_OLD_VIRTUAL_PATH%; goto SKIPPATH - -set _OLD_VIRTUAL_PATH=%PATH% - -:SKIPPATH -set PATH=%VIRTUAL_ENV%\__VENV_BIN_NAME__;%PATH% - -:END diff -r 2059910e7d76 -r ac776ef41428 Lib/venv/scripts/nt/deactivate.bat --- a/Lib/venv/scripts/nt/deactivate.bat Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -@echo off - -if defined _OLD_VIRTUAL_PROMPT ( - set PROMPT=%_OLD_VIRTUAL_PROMPT% -) -set _OLD_VIRTUAL_PROMPT= - -if defined _OLD_VIRTUAL_PYTHONHOME ( - set PYTHONHOME=%_OLD_VIRTUAL_PYTHONHOME% - set _OLD_VIRTUAL_PYTHONHOME= -) - -if defined _OLD_VIRTUAL_PATH set PATH=%_OLD_VIRTUAL_PATH% - -set _OLD_VIRTUAL_PATH= - -:END diff -r 2059910e7d76 -r ac776ef41428 Lib/venv/scripts/nt/pysetup3.py --- a/Lib/venv/scripts/nt/pysetup3.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -#!__VENV_PYTHON__ -if __name__ == '__main__': - rc = 1 - try: - import sys, re, packaging.run - sys.argv[0] = re.sub('-script.pyw?$', '', sys.argv[0]) - rc = packaging.run.main() # None interpreted as 0 - except Exception: - # use syntax which works with either 2.x or 3.x - sys.stderr.write('%s\n' % sys.exc_info()[1]) - sys.exit(rc) diff -r 2059910e7d76 -r ac776ef41428 Lib/venv/scripts/posix/activate --- a/Lib/venv/scripts/posix/activate Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -# This file must be used with "source bin/activate" *from bash* -# you cannot run it directly - -deactivate () { - # reset old environment variables - if [ -n "$_OLD_VIRTUAL_PATH" ] ; then - PATH="$_OLD_VIRTUAL_PATH" - export PATH - unset _OLD_VIRTUAL_PATH - fi - if [ -n "$_OLD_VIRTUAL_PYTHONHOME" ] ; then - PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME" - export PYTHONHOME - unset _OLD_VIRTUAL_PYTHONHOME - fi - - # This should detect bash and zsh, which have a hash command that must - # be called to get it to forget past commands. Without forgetting - # past commands the $PATH changes we made may not be respected - if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then - hash -r - fi - - if [ -n "$_OLD_VIRTUAL_PS1" ] ; then - PS1="$_OLD_VIRTUAL_PS1" - export PS1 - unset _OLD_VIRTUAL_PS1 - fi - - unset VIRTUAL_ENV - if [ ! "$1" = "nondestructive" ] ; then - # Self destruct! - unset -f deactivate - fi -} - -# unset irrelavent variables -deactivate nondestructive - -VIRTUAL_ENV="__VENV_DIR__" -export VIRTUAL_ENV - -_OLD_VIRTUAL_PATH="$PATH" -PATH="$VIRTUAL_ENV/__VENV_BIN_NAME__:$PATH" -export PATH - -# unset PYTHONHOME if set -# this will fail if PYTHONHOME is set to the empty string (which is bad anyway) -# could use `if (set -u; : $PYTHONHOME) ;` in bash -if [ -n "$PYTHONHOME" ] ; then - _OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME" - unset PYTHONHOME -fi - -if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then - _OLD_VIRTUAL_PS1="$PS1" - if [ "x__VENV_NAME__" != x ] ; then - PS1="__VENV_NAME__$PS1" - else - if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then - # special case for Aspen magic directories - # see http://www.zetadev.com/software/aspen/ - PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1" - else - PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1" - fi - fi - export PS1 -fi - -# This should detect bash and zsh, which have a hash command that must -# be called to get it to forget past commands. Without forgetting -# past commands the $PATH changes we made may not be respected -if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then - hash -r -fi diff -r 2059910e7d76 -r ac776ef41428 Lib/venv/scripts/posix/pysetup3 --- a/Lib/venv/scripts/posix/pysetup3 Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -#!__VENV_PYTHON__ -if __name__ == '__main__': - rc = 1 - try: - import sys, re, packaging.run - sys.argv[0] = re.sub('-script.pyw?$', '', sys.argv[0]) - rc = packaging.run.main() # None interpreted as 0 - except Exception: - # use syntax which works with either 2.x or 3.x - sys.stderr.write('%s\n' % sys.exc_info()[1]) - sys.exit(rc) diff -r 2059910e7d76 -r ac776ef41428 Lib/xml/etree/ElementTree.py --- a/Lib/xml/etree/ElementTree.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Lib/xml/etree/ElementTree.py Mon May 21 23:01:17 2012 -0400 @@ -205,9 +205,6 @@ # constructor def __init__(self, tag, attrib={}, **extra): - if not isinstance(attrib, dict): - raise TypeError("attrib must be dict, not %s" % ( - attrib.__class__.__name__,)) attrib = attrib.copy() attrib.update(extra) self.tag = tag diff -r 2059910e7d76 -r ac776ef41428 Mac/Makefile.in --- a/Mac/Makefile.in Sat Jun 09 17:31:59 2012 +0100 +++ b/Mac/Makefile.in Mon May 21 23:01:17 2012 -0400 @@ -72,7 +72,7 @@ for fn in python3 pythonw3 idle3 pydoc3 python3-config \ python$(VERSION) pythonw$(VERSION) idle$(VERSION) \ pydoc$(VERSION) python$(VERSION)-config 2to3 \ - 2to3-$(VERSION) pyvenv pyvenv-$(VERSION) ;\ + 2to3-$(VERSION) ;\ do \ ln -fs "$(prefix)/bin/$${fn}" "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin/$${fn}" ;\ done @@ -93,7 +93,7 @@ $(INSTALL) -d -m $(DIRMODE) "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin" ;\ fi for fn in python$(VERSION) pythonw$(VERSION) idle$(VERSION) \ - pydoc$(VERSION) python$(VERSION)-config 2to3-$(VERSION) pyvenv-$(VERSION) ;\ + pydoc$(VERSION) python$(VERSION)-config 2to3-$(VERSION);\ do \ ln -fs "$(prefix)/bin/$${fn}" "$(DESTDIR)$(FRAMEWORKUNIXTOOLSPREFIX)/bin/$${fn}" ;\ done diff -r 2059910e7d76 -r ac776ef41428 Mac/Tools/pythonw.c --- a/Mac/Tools/pythonw.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Mac/Tools/pythonw.c Mon May 21 23:01:17 2012 -0400 @@ -150,18 +150,6 @@ int main(int argc, char **argv) { char* exec_path = get_python_path(); - static char path[PATH_MAX * 2]; - static char real_path[PATH_MAX * 2]; - int status; - uint32_t size = PATH_MAX * 2; - - /* Set the original executable path in the environment. */ - status = _NSGetExecutablePath(path, &size); - if (status == 0) { - if (realpath(path, real_path) != NULL) { - setenv("__PYTHONV_LAUNCHER__", real_path, 1); - } - } /* * Let argv[0] refer to the new interpreter. This is needed to diff -r 2059910e7d76 -r ac776ef41428 Makefile.pre.in --- a/Makefile.pre.in Sat Jun 09 17:31:59 2012 +0100 +++ b/Makefile.pre.in Mon May 21 23:01:17 2012 -0400 @@ -392,7 +392,6 @@ Objects/memoryobject.o \ Objects/methodobject.o \ Objects/moduleobject.o \ - Objects/namespaceobject.o \ Objects/object.o \ Objects/obmalloc.o \ Objects/capsule.o \ @@ -767,7 +766,6 @@ $(srcdir)/Include/methodobject.h \ $(srcdir)/Include/modsupport.h \ $(srcdir)/Include/moduleobject.h \ - $(srcdir)/Include/namespaceobject.h \ $(srcdir)/Include/node.h \ $(srcdir)/Include/object.h \ $(srcdir)/Include/objimpl.h \ @@ -949,8 +947,6 @@ (cd $(DESTDIR)$(BINDIR); $(LN) -s 2to3-$(VERSION) 2to3) -rm -f $(DESTDIR)$(BINDIR)/pysetup3 (cd $(DESTDIR)$(BINDIR); $(LN) -s pysetup$(VERSION) pysetup3) - -rm -f $(DESTDIR)$(BINDIR)/pyvenv - (cd $(DESTDIR)$(BINDIR); $(LN) -s pyvenv-$(VERSION) pyvenv) # Install the manual page maninstall: @@ -976,24 +972,6 @@ test/cjkencodings test/decimaltestdata test/xmltestdata \ test/subprocessdata test/sndhdrdata \ test/tracedmodules test/encoded_modules \ - test/namespace_pkgs \ - test/namespace_pkgs/both_portions \ - test/namespace_pkgs/both_portions/foo \ - test/namespace_pkgs/not_a_namespace_pkg \ - test/namespace_pkgs/not_a_namespace_pkg/foo \ - test/namespace_pkgs/portion1 \ - test/namespace_pkgs/portion1/foo \ - test/namespace_pkgs/portion2 \ - test/namespace_pkgs/portion2/foo \ - test/namespace_pkgs/project1 \ - test/namespace_pkgs/project1/parent \ - test/namespace_pkgs/project1/parent/child \ - test/namespace_pkgs/project2 \ - test/namespace_pkgs/project2/parent \ - test/namespace_pkgs/project2/parent/child \ - test/namespace_pkgs/project3 \ - test/namespace_pkgs/project3/parent \ - test/namespace_pkgs/project3/parent/child \ collections concurrent concurrent/futures encodings \ email email/mime test/test_email test/test_email/data \ html json test/json_tests http dbm xmlrpc \ @@ -1060,7 +1038,6 @@ turtledemo \ multiprocessing multiprocessing/dummy \ unittest unittest/test unittest/test/testmock \ - venv venv/scripts venv/scripts/posix \ curses pydoc_data $(MACHDEPS) libinstall: build_all $(srcdir)/Lib/$(PLATDIR) $(srcdir)/Modules/xxmodule.c @for i in $(SCRIPTDIR) $(LIBDEST); \ diff -r 2059910e7d76 -r ac776ef41428 Misc/ACKS --- a/Misc/ACKS Sat Jun 09 17:31:59 2012 +0100 +++ b/Misc/ACKS Mon May 21 23:01:17 2012 -0400 @@ -112,7 +112,6 @@ Matias Bordese Jurjen Bos Peter Bosch -Dan Boswell Eric Bouck Thierry Bousch Sebastian Boving @@ -162,7 +161,6 @@ Donn Cave Charles Cazabon Per Cederqvist -Matej Cepl Octavian Cerna Pascal Chambon John Chandler @@ -363,7 +361,6 @@ Dan Gass Andrew Gaul Stephen M. Gava -Xavier de Gaye Harry Henry Gebel Marius Gedminas Thomas Gellekum @@ -460,7 +457,6 @@ Brian Hooper Randall Hopper Nadav Horesh -Alon Horev Jan Hosang Ken Howard Brad Howes @@ -496,7 +492,6 @@ Jack Jansen Bill Janssen Thomas Jarosch -Juhana Jauhiainen Zbigniew Jędrzejewski-Szmek Julien Jehannet Drew Jenkins @@ -509,7 +504,6 @@ Matt Joiner Thomas Jollans Nicolas Joly -Brian K. Jones Evan Jones Jeremy Jones Richard Jones @@ -663,7 +657,6 @@ Anthony Martin Owen Martin Sébastien Martini -Sidney San Martín Roger Masse Nick Mathewson Simon Mathieu @@ -708,7 +701,6 @@ Doug Moen The Dragon De Monsyne Skip Montanaro -Peter Moody Paul Moore Derek Morr James A Morrison @@ -784,7 +776,6 @@ Joe Peterson Randy Pausch Samuele Pedroni -Justin Peel Marcel van der Peijl Berker Peksag Steven Pemberton @@ -959,7 +950,6 @@ Itamar Shtull-Trauring Eric Siegerman Paul Sijben -Tim Silk Kirill Simonov Nathan Paul Simons Adam Simpkins @@ -989,7 +979,6 @@ Joel Stanley Oliver Steele Greg Stein -Baruch Sterin Chris Stern Alex Stewart Victor Stinner @@ -1045,7 +1034,6 @@ Richard Townsend David Townshend Laurence Tratt -Alberto Trevino Matthias Troffaes John Tromp Jason Trowbridge diff -r 2059910e7d76 -r ac776ef41428 Misc/NEWS --- a/Misc/NEWS Sat Jun 09 17:31:59 2012 +0100 +++ b/Misc/NEWS Mon May 21 23:01:17 2012 -0400 @@ -2,115 +2,14 @@ Python News +++++++++++ -What's New in Python 3.3.0 Beta 1? -================================== +What's New in Python 3.3.0 Alpha 4? +=================================== *Release date: XX-XXX-2012* Core and Builtins ----------------- -- Issue #11022: open() and io.TextIOWrapper are now calling - locale.getpreferredencoding(False) instead of locale.getpreferredencoding() - in text mode if the encoding is not specified. Don't change temporary the - locale encoding using locale.setlocale(), use the current locale encoding - instead of the user preferred encoding. - -- Issue #14673: Add Eric Snow's sys.implementation implementation. - -Library -------- - -- Issue #2736: Added datetime.timestamp() method. - -- Issue #13854: Make multiprocessing properly handle non-integer - non-string argument to SystemExit. - -- Issue #12157: Make pool.map() empty iterables correctly. Initial - patch by mouad. - -- Issue #11823: disassembly now shows argument counts on calls with keyword args. - -- Issue #14711: os.stat_float_times() has been deprecated. - -- LZMAFile now accepts the modes "rb"/"wb"/"ab" as synonyms of "r"/"w"/"a". - -- The bz2 and lzma modules now each contain an open() function, allowing - compressed files to readily be opened in text mode as well as binary mode. - -- BZ2File.__init__() and LZMAFile.__init__() now accept a file object as their - first argument, rather than requiring a separate "fileobj" argument. - -- gzip.open() now accepts file objects as well as filenames. - -- Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError - when the path existed and had the S_ISGID mode bit set when it was - not explicitly asked for. This is no longer an exception as mkdir - cannot control if the OS sets that bit for it or not. - -- Issue #14989: Make the CGI enable option to http.server available via command - line. - -- Issue #14987: Add a missing import statement to inspect. - -- Issue #1079: email.header.decode_header now correctly parses all the examples - in RFC2047. There is a necessary visible behavior change: the leading and/or - trailing whitespace on ASCII parts is now preserved. - -- Issue #14969: Better handling of exception chaining in contextlib.ExitStack - -- Issue #14962: Update text coloring in IDLE shell window after changing - options. Patch by Roger Serwy. - -- Issue #14963: Convert contextlib.ExitStack.__exit__ to use an iterative - algorithm (Patch by Alon Horev) - -Extension Modules ------------------ - -- Issue #15000: Support the "unique" x32 architecture in _posixsubprocess.c. - -Tests ------ - -- Issue #14963 (partial): Add test cases for exception handling behaviour - in contextlib.ExitStack (Initial patch by Alon Horev) - - -What's New in Python 3.3.0 Alpha 4? -=================================== - -*Release date: 31-May-2012* - -Core and Builtins ------------------ - -- Issue #14835: Make plistlib output empty arrays & dicts like OS X. - Patch by Sidney San Martín. - -- Issue #14744: Use the new _PyUnicodeWriter internal API to speed up - str%args and str.format(args). - -- Issue #14930: Make memoryview objects weakrefable. - -- Issue #14775: Fix a potential quadratic dict build-up due to the garbage - collector repeatedly trying to untrack dicts. - -- Issue #14857: fix regression in references to PEP 3135 implicit __class__ - closure variable (Reopens issue #12370) - -- Issue #14712 (PEP 405): Virtual environments. Implemented by Vinay Sajip. - -- Issue #14660 (PEP 420): Namespace packages. Implemented by Eric Smith. - -- Issue #14494: Fix __future__.py and its documentation to note that - absolute imports are the default behavior in 3.0 instead of 2.7. - Patch by Sven Marnach. - -- Issue #9260: A finer-grained import lock. Most of the import sequence - now uses per-module locks rather than the global import lock, eliminating - well-known issues with threads and imports. - - Issue #14624: UTF-16 decoding is now 3x to 4x faster on various inputs. Patch by Serhiy Storchaka. @@ -135,104 +34,6 @@ Library ------- -- Issue #14690: Use monotonic clock instead of system clock in the sched, - subprocess and trace modules. - -- Issue #14958: Change IDLE systax highlighting to recognize all string and - byte literals supported in Python 3.3. - -- Issue #10997: Prevent a duplicate entry in IDLE's "Recent Files" menu. - -- Issue #14443: Tell rpmbuild to use the correct version of Python in - bdist_rpm. Initial patch by Ross Lagerwall. - -- Issue #14929: Stop Idle 3.x from closing on Unicode decode errors when - grepping. Patch by Roger Serwy. - -- Issue #12515: email now registers a defect if it gets to EOF while parsing - a MIME part without seeing the closing MIME boundary. - -- Issue #12510: Attempting to get invalid tooltip no longer closes Idle. - Other tooltipss have been corrected or improved and the number of tests - has been tripled. Original patch by Roger Serwy. - -- Issue #1672568: email now always decodes base64 payloads, adding padding and - ignoring non-base64-alphabet characters if needed, and registering defects - for any such problems. - -- Issue #14925: email now registers a defect when the parser decides that there - is a missing header/body separator line. MalformedHeaderDefect, which the - existing code would never actually generate, is deprecated. - -- Issue #10365: File open dialog now works instead of crashing even when - the parent window is closed before the dialog. Patch by Roger Serwy. - -- Issue #8739: Updated smtpd to support RFC 5321, and added support for the - RFC 1870 SIZE extension. - -- Issue #665194: Added a localtime function to email.utils to provide an - aware local datetime for use in setting Date headers. - -- Issue #12586: Added new provisional policies that implement convenient - unicode support for email headers. See What's New for details. - -- Issue #14731: Refactored email Policy framework to support full backward - compatibility with Python 3.2 by default yet allow for the introduction of - new features through new policies. Note that Policy.must_be_7bit is renamed - to cte_type. - -- Issue #14876: Use user-selected font for highlight configuration. - -- Issue #14920: Fix the help(urllib.parse) failure on locale C on terminals. - Have ascii characters in help. - -- Issue #14548: Make multiprocessing finalizers check pid before - running to cope with possibility of gc running just after fork. - -- Issue #14863: Update the documentation of os.fdopen() to reflect the - fact that it's only a thin wrapper around open() anymore. - -- Issue #14036: Add an additional check to validate that port in urlparse does - not go in illegal range and returns None. - -- Issue #14862: Add missing names to os.__all__ - -- Issue #14875: Use float('inf') instead of float('1e66666') in the json module. - -- Issue #13585: Added contextlib.ExitStack - -- PEP 3144, Issue #14814: Added the ipaddress module - -- Issue #14426: Correct the Date format in Expires attribute of Set-Cookie - Header in Cookie.py. - -- Issue #14588: The types module now provide new_class() and prepare_class() - functions to support PEP 3115 compliant dynamic class creation. Patch by - Daniel Urban and Nick Coghlan. - -- Issue #13152: Allow to specify a custom tabsize for expanding tabs in - textwrap. Patch by John Feuerstein. - -- Issue #14721: Send the correct 'Content-length: 0' header when the body is an - empty string ''. Initial Patch contributed by Arve Knudsen. - -- Issue #14072: Fix parsing of 'tel' URIs in urlparse by making the check for - ports stricter. - -- Issue #9374: Generic parsing of query and fragment portions of url for any - scheme. Supported both by RFC3986 and RFC2396. - -- Issue #14798: Fix the functions in pyclbr to raise an ImportError - when the first part of a dotted name is not a package. Patch by - Xavier de Gaye. - -- Issue #12098: multiprocessing on Windows now starts child processes - using the same sys.flags as the current process. Initial patch by - Sergey Mezentsev. - -- Issue #13031: Small speed-up for tarfile when unzipping tarfiles. - Patch by Justin Peel. - - Issue #14780: urllib.request.urlopen() now has a ``cadefault`` argument to use the default certificate store. Initial patch by James Oakley. @@ -327,24 +128,11 @@ Build ----- -- Issue #14472: Update .gitignore. Patch by Matej Cepl. - -- Upgrade Windows library versions: bzip 1.0.6, OpenSSL 1.0.1c. - - Issue #14693: Under non-Windows platforms, hashlib's fallback modules are always compiled, even if OpenSSL is present at build time. - Issue #13210: Windows build now uses VS2010, ported from VS2008. -Documentation -------------- - -- Issue #14588: The language reference now accurately documents the Python 3 - class definition process. Patch by Nick Coghlan. - -- Issue #14943: Correct a default argument value for winreg.OpenKey - and correctly list the argument names in the function's explanation. - What's New in Python 3.3.0 Alpha 3? =================================== @@ -811,10 +599,6 @@ Extension Modules ----------------- -- Issue #9041: An issue in ctypes.c_longdouble, ctypes.c_double, and - ctypes.c_float that caused an incorrect exception to be returned in the - case of overflow has been fixed. - - Issue #14212: The re module didn't retain a reference to buffers it was scanning, resulting in segfaults. diff -r 2059910e7d76 -r ac776ef41428 Misc/RPM/python-3.3.spec --- a/Misc/RPM/python-3.3.spec Sat Jun 09 17:31:59 2012 +0100 +++ b/Misc/RPM/python-3.3.spec Mon May 21 23:01:17 2012 -0400 @@ -39,7 +39,7 @@ %define name python #--start constants-- -%define version 3.3.0a4 +%define version 3.3.0a3 %define libvers 3.3 #--end constants-- %define release 1pydotorg diff -r 2059910e7d76 -r ac776ef41428 Modules/_ctypes/cfield.c --- a/Modules/_ctypes/cfield.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/_ctypes/cfield.c Mon May 21 23:01:17 2012 -0400 @@ -999,8 +999,12 @@ long double x; x = PyFloat_AsDouble(value); - if (x == -1 && PyErr_Occurred()) + if (x == -1 && PyErr_Occurred()) { + PyErr_Format(PyExc_TypeError, + " float expected instead of %s instance", + value->ob_type->tp_name); return NULL; + } memcpy(ptr, &x, sizeof(long double)); _RET(value); } @@ -1019,8 +1023,12 @@ double x; x = PyFloat_AsDouble(value); - if (x == -1 && PyErr_Occurred()) + if (x == -1 && PyErr_Occurred()) { + PyErr_Format(PyExc_TypeError, + " float expected instead of %s instance", + value->ob_type->tp_name); return NULL; + } memcpy(ptr, &x, sizeof(double)); _RET(value); } @@ -1039,8 +1047,12 @@ double x; x = PyFloat_AsDouble(value); - if (x == -1 && PyErr_Occurred()) + if (x == -1 && PyErr_Occurred()) { + PyErr_Format(PyExc_TypeError, + " float expected instead of %s instance", + value->ob_type->tp_name); return NULL; + } #ifdef WORDS_BIGENDIAN if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1)) return NULL; @@ -1067,8 +1079,12 @@ float x; x = (float)PyFloat_AsDouble(value); - if (x == -1 && PyErr_Occurred()) + if (x == -1 && PyErr_Occurred()) { + PyErr_Format(PyExc_TypeError, + " float expected instead of %s instance", + value->ob_type->tp_name); return NULL; + } memcpy(ptr, &x, sizeof(x)); _RET(value); } @@ -1087,8 +1103,12 @@ float x; x = (float)PyFloat_AsDouble(value); - if (x == -1 && PyErr_Occurred()) + if (x == -1 && PyErr_Occurred()) { + PyErr_Format(PyExc_TypeError, + " float expected instead of %s instance", + value->ob_type->tp_name); return NULL; + } #ifdef WORDS_BIGENDIAN if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1)) return NULL; diff -r 2059910e7d76 -r ac776ef41428 Modules/_datetimemodule.c --- a/Modules/_datetimemodule.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/_datetimemodule.c Mon May 21 23:01:17 2012 -0400 @@ -766,8 +766,6 @@ /* The interned UTC timezone instance */ static PyObject *PyDateTime_TimeZone_UTC; -/* The interned Epoch datetime instance */ -static PyObject *PyDateTime_Epoch; /* Create new timezone instance checking offset range. This function does not check the name argument. Caller must assure @@ -4750,44 +4748,6 @@ } static PyObject * -datetime_timestamp(PyDateTime_DateTime *self) -{ - PyObject *result; - - if (HASTZINFO(self) && self->tzinfo != Py_None) { - PyObject *delta; - delta = datetime_subtract((PyObject *)self, PyDateTime_Epoch); - if (delta == NULL) - return NULL; - result = delta_total_seconds(delta); - Py_DECREF(delta); - } - else { - struct tm time; - time_t timestamp; - memset((void *) &time, '\0', sizeof(struct tm)); - time.tm_year = GET_YEAR(self) - 1900; - time.tm_mon = GET_MONTH(self) - 1; - time.tm_mday = GET_DAY(self); - time.tm_hour = DATE_GET_HOUR(self); - time.tm_min = DATE_GET_MINUTE(self); - time.tm_sec = DATE_GET_SECOND(self); - time.tm_wday = -1; - time.tm_isdst = -1; - timestamp = mktime(&time); - /* Return value of -1 does not necessarily mean an error, but tm_wday - * cannot remain set to -1 if mktime succeeded. */ - if (timestamp == (time_t)(-1) && time.tm_wday == -1) { - PyErr_SetString(PyExc_OverflowError, - "timestamp out of range"); - return NULL; - } - result = PyFloat_FromDouble(timestamp + DATE_GET_MICROSECOND(self) / 1e6); - } - return result; -} - -static PyObject * datetime_getdate(PyDateTime_DateTime *self) { return new_date(GET_YEAR(self), @@ -4934,9 +4894,6 @@ {"timetuple", (PyCFunction)datetime_timetuple, METH_NOARGS, PyDoc_STR("Return time tuple, compatible with time.localtime().")}, - {"timestamp", (PyCFunction)datetime_timestamp, METH_NOARGS, - PyDoc_STR("Return POSIX timestamp as float.")}, - {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, @@ -5194,12 +5151,6 @@ return NULL; Py_DECREF(x); - /* Epoch */ - PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, - PyDateTime_TimeZone_UTC); - if (PyDateTime_Epoch == NULL) - return NULL; - /* module initialization */ PyModule_AddIntConstant(m, "MINYEAR", MINYEAR); PyModule_AddIntConstant(m, "MAXYEAR", MAXYEAR); diff -r 2059910e7d76 -r ac776ef41428 Modules/_decimal/libmpdec/mpdecimal.c --- a/Modules/_decimal/libmpdec/mpdecimal.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/_decimal/libmpdec/mpdecimal.c Mon May 21 23:01:17 2012 -0400 @@ -3903,7 +3903,8 @@ /* abs(a) <= 9 * 10**(-prec-1) */ if (_mpd_cmp(&aa, &lim) <= 0) { _settriple(result, 0, 1, 0); - *status |= MPD_Rounded|MPD_Inexact; + _mpd_zeropad(result, ctx, status); + *status = MPD_Rounded|MPD_Inexact; return 1; } @@ -3953,18 +3954,8 @@ } /* - * Internal function, specials have been dealt with. Apart from Overflow - * and Underflow, two cases must be considered for the error of the result: - * - * 1) abs(a) <= 9 * 10**(-prec-1) ==> result == 1 - * - * Absolute error: abs(1 - e**x) < 10**(-prec) - * ------------------------------------------- - * - * 2) abs(a) > 9 * 10**(-prec-1) - * - * Relative error: abs(result - e**x) < 0.5 * 10**(-prec) * e**x - * ------------------------------------------------------------- + * Internal function, specials have been dealt with. The result has a + * relative error of less than 0.5 * 10**(-ctx->prec). * * The algorithm is from Hull&Abrham, Variable Precision Exponential Function, * ACM Transactions on Mathematical Software, Vol. 12, No. 2, June 1986. @@ -3989,7 +3980,7 @@ mpd_context_t workctx; MPD_NEW_STATIC(tmp,0,0,0,0); MPD_NEW_STATIC(sum,0,0,0,0); - MPD_NEW_CONST(word,0,0,1,1,1,1); + MPD_NEW_CONST(word,0,0,0,1,1,1); mpd_ssize_t j, n, t; assert(!mpd_isspecial(a)); @@ -4008,9 +3999,9 @@ * * MAX-EMAX+1 < log10(e^(0.1*10*t)) <= log10(e^(r*10^t)) < adjexp(e^(r*10^t))+1 * - * (2) -1 < r <= -0.1, so e^r <= e^-0.1. If t > MAX_T, underflow occurs: + * (2) -1 < r <= -0.1, so e^r <= e^-0.1. It t > MAX_T, underflow occurs: * - * adjexp(e^(r*10^t)) <= log10(e^(r*10^t)) <= log10(e^(-0.1*10^t)) < MIN-ETINY + * adjexp(e^(r*10^t)) <= log10(e^(r*10^t)) <= log10(e^(-0.1*10^t) < MIN-ETINY */ #if defined(CONFIG_64) #define MPD_EXP_MAX_T 19 @@ -4083,6 +4074,8 @@ } #endif + _mpd_zeropad(result, ctx, status); + mpd_del(&tmp); mpd_del(&sum); *status |= (workctx.status&MPD_Errors); @@ -4122,8 +4115,6 @@ MPD_NEW_STATIC(ulp, 0,0,0,0); MPD_NEW_STATIC(aa, 0,0,0,0); mpd_ssize_t prec; - mpd_ssize_t ulpexp; - uint32_t workstatus; if (result == a) { if (!mpd_qcopy(&aa, a, status)) { @@ -4137,20 +4128,12 @@ prec = ctx->prec + 3; while (1) { workctx.prec = prec; - workstatus = 0; - - _mpd_qexp(result, a, &workctx, &workstatus); - *status |= workstatus; - - ulpexp = result->exp + result->digits - workctx.prec; - if (workstatus & MPD_Underflow) { - /* The effective work precision is result->digits. */ - ulpexp = result->exp; - } - _ssettriple(&ulp, MPD_POS, 1, ulpexp); + _mpd_qexp(result, a, &workctx, status); + _ssettriple(&ulp, MPD_POS, 1, + result->exp + result->digits-workctx.prec); /* - * At this point [1]: + * At this point: * 1) abs(result - e**x) < 0.5 * 10**(-prec) * e**x * 2) result - ulp < e**x < result + ulp * 3) result - ulp < result < result + ulp @@ -4158,9 +4141,6 @@ * If round(result-ulp)==round(result+ulp), then * round(result)==round(e**x). Therefore the result * is correctly rounded. - * - * [1] If abs(a) <= 9 * 10**(-prec-1), use the absolute - * error for a similar argument. */ workctx.prec = ctx->prec; mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status); @@ -4168,7 +4148,6 @@ if (mpd_isspecial(result) || mpd_iszerocoeff(result) || mpd_qcmp(&t1, &t2, status) == 0) { workctx.clamp = ctx->clamp; - _mpd_zeropad(result, &workctx, status); mpd_check_underflow(result, &workctx, status); mpd_qfinalize(result, &workctx, status); break; @@ -4182,7 +4161,6 @@ } else { _mpd_qexp(result, a, &workctx, status); - _mpd_zeropad(result, &workctx, status); mpd_check_underflow(result, &workctx, status); mpd_qfinalize(result, &workctx, status); } @@ -4212,18 +4190,6 @@ *status |= workstatus; } -/* - * Schedule the optimal precision increase for the Newton iteration. - * v := input operand - * z_0 := initial approximation - * initprec := natural number such that abs(log(v) - z_0) < 10**-initprec - * maxprec := target precision - * - * For convenience the output klist contains the elements in reverse order: - * klist := [k_n-1, ..., k_0], where - * 1) k_0 <= initprec and - * 2) abs(log(v) - result) < 10**(-2*k_n-1 + 1) <= 10**-maxprec. - */ static inline int ln_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2], mpd_ssize_t maxprec, mpd_ssize_t initprec) @@ -4243,7 +4209,6 @@ return i-1; } -/* The constants have been verified with both decimal.py and mpfr. */ #ifdef CONFIG_64 #if MPD_RDIGITS != 19 #error "mpdecimal.c: MPD_RDIGITS must be 19." @@ -4298,14 +4263,7 @@ (mpd_uint_t *)mpd_ln10_data }; -/* - * Set 'result' to log(10). - * Ulp error: abs(result - log(10)) < ulp(log(10)) - * Relative error : abs(result - log(10)) < 5 * 10**-prec * log(10) - * - * NOTE: The relative error is not derived from the ulp error, but - * calculated separately using the fact that 23/10 < log(10) < 24/10. - */ +/* Set 'result' to ln(10), with 'prec' digits, using ROUND_HALF_EVEN. */ void mpd_qln10(mpd_t *result, mpd_ssize_t prec, uint32_t *status) { @@ -4340,7 +4298,7 @@ mpd_maxcontext(&varcontext); varcontext.round = MPD_ROUND_TRUNC; - i = ln_schedule_prec(klist, prec+2, -result->exp); + i = ln_schedule_prec(klist, prec+2, result->digits); for (; i >= 0; i--) { varcontext.prec = 2*klist[i]+3; result->flags ^= MPD_NEG; @@ -4359,18 +4317,7 @@ mpd_qfinalize(result, &maxcontext, status); } -/* - * Initial approximations for the ln() iteration. The values have the - * following properties (established with both decimal.py and mpfr): - * - * Index 0 - 400, logarithms of x in [1.00, 5.00]: - * abs(lnapprox[i] * 10**-3 - log((i+100)/100)) < 10**-2 - * abs(lnapprox[i] * 10**-3 - log((i+1+100)/100)) < 10**-2 - * - * Index 401 - 899, logarithms of x in (0.500, 0.999]: - * abs(-lnapprox[i] * 10**-3 - log((i+100)/1000)) < 10**-2 - * abs(-lnapprox[i] * 10**-3 - log((i+1+100)/1000)) < 10**-2 - */ +/* Initial approximations for the ln() iteration */ static const uint16_t lnapprox[900] = { /* index 0 - 400: log((i+100)/100) * 1000 */ 0, 10, 20, 30, 39, 49, 58, 68, 77, 86, 95, 104, 113, 122, 131, 140, 148, 157, @@ -4437,10 +4384,7 @@ 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; -/* - * Internal ln() function that does not check for specials, zero or one. - * Relative error: abs(result - log(a)) < 0.1 * 10**-prec * abs(log(a)) - */ +/* Internal ln() function that does not check for specials, zero or one. */ static void _mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status) @@ -4485,16 +4429,10 @@ mpd_setdigits(z); if (x <= 400) { - /* Reduce the input operand to 1.00 <= v <= 5.00. Let y = x + 100, - * so 100 <= y <= 500. Since y contains the most significant digits - * of v, y/100 <= v < (y+1)/100 and abs(z - log(v)) < 10**-2. */ v.exp = -(a_digits - 1); t = a_exp + a_digits - 1; } else { - /* Reduce the input operand to 0.500 < v <= 0.999. Let y = x + 100, - * so 500 < y <= 999. Since y contains the most significant digits - * of v, y/1000 <= v < (y+1)/1000 and abs(z - log(v)) < 10**-2. */ v.exp = -a_digits; t = a_exp + a_digits; mpd_set_negative(z); @@ -4505,46 +4443,37 @@ varcontext.round = MPD_ROUND_TRUNC; maxprec = ctx->prec + 2; - if (t == 0 && (x <= 15 || x >= 800)) { - /* 0.900 <= v <= 1.15: Estimate the magnitude of the logarithm. - * If ln(v) will underflow, skip the loop. Otherwise, adjust the - * precision upwards in order to obtain a sufficient number of - * significant digits. + if (x <= 10 || x >= 805) { + /* v is close to 1: Estimate the magnitude of the logarithm. + * If v = 1 or ln(v) will underflow, skip the loop. Otherwise, + * adjust the precision upwards in order to obtain a sufficient + * number of significant digits. * - * Case v > 1: - * abs((v-1)/10) < abs((v-1)/v) < abs(ln(v)) < abs(v-1) - * Case v < 1: - * abs(v-1) < abs(ln(v)) < abs((v-1)/v) < abs((v-1)*10) + * 1) x/(1+x) < ln(1+x) < x, for x > -1, x != 0 + * + * 2) (v-1)/v < ln(v) < v-1 */ + mpd_t *lower = &tmp; + mpd_t *upper = &vtmp; int cmp = _mpd_cmp(&v, &one); - /* Upper bound (assume v > 1): abs(v-1), unrounded */ - _mpd_qsub(&tmp, &v, &one, &maxcontext, &maxcontext.status); - if (maxcontext.status & MPD_Errors) { - mpd_seterror(result, MPD_Malloc_error, status); - goto finish; - } + varcontext.round = MPD_ROUND_CEILING; + varcontext.prec = maxprec; + mpd_qsub(upper, &v, &one, &varcontext, &varcontext.status); + varcontext.round = MPD_ROUND_FLOOR; + mpd_qdiv(lower, upper, &v, &varcontext, &varcontext.status); + varcontext.round = MPD_ROUND_TRUNC; if (cmp < 0) { - /* v < 1: abs((v-1)*10) */ - tmp.exp += 1; - } - if (mpd_adjexp(&tmp) < mpd_etiny(ctx)) { - /* The upper bound is less than etiny: Underflow to zero */ - _settriple(result, (cmp<0), 1, mpd_etiny(ctx)-1); - goto finish; - } - /* Lower bound: abs((v-1)/10) or abs(v-1) */ - tmp.exp -= 1; - if (mpd_adjexp(&tmp) < 0) { - /* Absolute error of the loop: abs(z - log(v)) < 10**-p. If - * p = ctx->prec+2-adjexp(lower), then the relative error of - * the result is (using 10**adjexp(x) <= abs(x)): - * - * abs(z - log(v)) / abs(log(v)) < 10**-p / abs(log(v)) - * <= 10**(-ctx->prec-2) - */ - maxprec = maxprec - mpd_adjexp(&tmp); + _mpd_ptrswap(&upper, &lower); + } + if (mpd_adjexp(upper) < mpd_etiny(ctx)) { + _settriple(z, (cmp<0), 1, mpd_etiny(ctx)-1); + goto postloop; + } + /* XXX optimization: t == 0 && mpd_adjexp(lower) < 0 */ + if (mpd_adjexp(lower) < 0) { + maxprec = maxprec - mpd_adjexp(lower); } } @@ -4572,37 +4501,14 @@ } } - /* - * Case t == 0: - * t * log(10) == 0, the result does not change and the analysis - * above applies. If v < 0.900 or v > 1.15, the relative error is - * less than 10**(-ctx.prec-1). - * Case t != 0: - * z := approx(log(v)) - * y := approx(log(10)) - * p := maxprec = ctx->prec + 2 - * Absolute errors: - * 1) abs(z - log(v)) < 10**-p - * 2) abs(y - log(10)) < 10**-p - * The multiplication is exact, so: - * 3) abs(t*y - t*log(10)) < t*10**-p - * The sum is exact, so: - * 4) abs((z + t*y) - (log(v) + t*log(10))) < (abs(t) + 1) * 10**-p - * Bounds for log(v) and log(10): - * 5) -7/10 < log(v) < 17/10 - * 6) 23/10 < log(10) < 24/10 - * Using 4), 5), 6) and t != 0, the relative error is: - * - * 7) relerr < ((abs(t) + 1)*10**-p) / abs(log(v) + t*log(10)) - * < 0.5 * 10**(-p + 1) = 0.5 * 10**(-ctx->prec-1) - */ - mpd_qln10(&v, maxprec+1, status); +postloop: + mpd_qln10(&v, maxprec+2, status); mpd_qmul_ssize(&tmp, &v, t, &maxcontext, status); - mpd_qadd(result, &tmp, z, &maxcontext, status); + varcontext.prec = maxprec+2; + mpd_qadd(result, &tmp, z, &varcontext, status); finish: - *status |= (MPD_Inexact|MPD_Rounded); mpd_del(&v); mpd_del(&vtmp); mpd_del(&tmp); @@ -4639,26 +4545,14 @@ _settriple(result, MPD_POS, 0, 0); return; } - /* - * Check if the result will overflow (0 < x, x != 1): - * 1) log10(x) < 0 iff adjexp(x) < 0 - * 2) 0 < x /\ x <= y ==> adjexp(x) <= adjexp(y) - * 3) 0 < x /\ x != 1 ==> 2 * abs(log10(x)) < abs(log(x)) - * 4) adjexp(x) <= log10(x) < adjexp(x) + 1 + /* Check if the result will overflow. * - * Case adjexp(x) >= 0: - * 5) 2 * adjexp(x) < abs(log(x)) - * Case adjexp(x) > 0: - * 6) adjexp(2 * adjexp(x)) <= adjexp(abs(log(x))) - * Case adjexp(x) == 0: - * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) + * 1) adjexp(a) + 1 > log10(a) >= adjexp(a) * - * Case adjexp(x) < 0: - * 7) 2 * (-adjexp(x) - 1) < abs(log(x)) - * Case adjexp(x) < -1: - * 8) adjexp(2 * (-adjexp(x) - 1)) <= adjexp(abs(log(x))) - * Case adjexp(x) == -1: - * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) + * 2) |log10(a)| >= adjexp(a), if adjexp(a) >= 0 + * |log10(a)| > -adjexp(a)-1, if adjexp(a) < 0 + * + * 3) |log(a)| > 2*|log10(a)| */ adjexp = mpd_adjexp(a); t = (adjexp < 0) ? -adjexp-1 : adjexp; @@ -4693,7 +4587,7 @@ workctx.prec = prec; _mpd_qln(result, a, &workctx, status); _ssettriple(&ulp, MPD_POS, 1, - result->exp + result->digits-workctx.prec); + result->exp + result->digits-workctx.prec-1); workctx.prec = ctx->prec; mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status); @@ -4719,34 +4613,21 @@ } } -/* - * Internal log10() function that does not check for specials, zero or one. - * Case SKIP_FINALIZE: - * Relative error: abs(result - log10(a)) < 0.1 * 10**-prec * abs(log10(a)) - * Case DO_FINALIZE: - * Ulp error: abs(result - log10(a)) < ulp(log10(a)) - */ -enum {SKIP_FINALIZE, DO_FINALIZE}; +/* Internal log10() function that does not check for specials, zero, ... */ static void -_mpd_qlog10(int action, mpd_t *result, const mpd_t *a, - const mpd_context_t *ctx, uint32_t *status) +_mpd_qlog10(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) { mpd_context_t workctx; MPD_NEW_STATIC(ln10,0,0,0,0); mpd_maxcontext(&workctx); workctx.prec = ctx->prec + 3; - /* relative error: 0.1 * 10**(-p-3). The specific underflow shortcut - * in _mpd_qln() does not change the final result. */ _mpd_qln(result, a, &workctx, status); - /* relative error: 5 * 10**(-p-3) */ mpd_qln10(&ln10, workctx.prec, status); - if (action == DO_FINALIZE) { - workctx = *ctx; - workctx.round = MPD_ROUND_HALF_EVEN; - } - /* SKIP_FINALIZE: relative error: 5 * 10**(-p-3) */ + workctx = *ctx; + workctx.round = MPD_ROUND_HALF_EVEN; _mpd_qdiv(NO_IDEAL_EXP, result, result, &ln10, &workctx, status); mpd_del(&ln10); @@ -4793,25 +4674,12 @@ mpd_qfinalize(result, &workctx, status); return; } - /* - * Check if the result will overflow (0 < x, x != 1): - * 1) log10(x) < 0 iff adjexp(x) < 0 - * 2) 0 < x /\ x <= y ==> adjexp(x) <= adjexp(y) - * 3) adjexp(x) <= log10(x) < adjexp(x) + 1 + /* Check if the result will overflow. * - * Case adjexp(x) >= 0: - * 4) adjexp(x) <= abs(log10(x)) - * Case adjexp(x) > 0: - * 5) adjexp(adjexp(x)) <= adjexp(abs(log10(x))) - * Case adjexp(x) == 0: - * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) + * 1) adjexp(a) + 1 > log10(a) >= adjexp(a) * - * Case adjexp(x) < 0: - * 6) -adjexp(x) - 1 < abs(log10(x)) - * Case adjexp(x) < -1: - * 7) adjexp(-adjexp(x) - 1) <= adjexp(abs(log(x))) - * Case adjexp(x) == -1: - * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) + * 2) |log10(a)| >= adjexp(a), if adjexp(a) >= 0 + * |log10(a)| > -adjexp(a)-1, if adjexp(a) < 0 */ adjexp = mpd_adjexp(a); t = (adjexp < 0) ? -adjexp-1 : adjexp; @@ -4840,9 +4708,9 @@ prec = ctx->prec + 3; while (1) { workctx.prec = prec; - _mpd_qlog10(SKIP_FINALIZE, result, a, &workctx, status); + _mpd_qlog10(result, a, &workctx, status); _ssettriple(&ulp, MPD_POS, 1, - result->exp + result->digits-workctx.prec); + result->exp + result->digits-workctx.prec-1); workctx.prec = ctx->prec; mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status); @@ -4862,7 +4730,7 @@ mpd_del(&aa); } else { - _mpd_qlog10(DO_FINALIZE, result, a, &workctx, status); + _mpd_qlog10(result, a, &workctx, status); mpd_check_underflow(result, &workctx, status); } } @@ -5543,24 +5411,32 @@ if (small->len == 1) { - rdata = mpd_calloc(rsize, sizeof *rdata); - if (rdata != NULL) { - _mpd_shortmul(rdata, big->data, big->len, small->data[0]); - } + if ((rdata = mpd_calloc(rsize, sizeof *rdata)) == NULL) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } + _mpd_shortmul(rdata, big->data, big->len, small->data[0]); } else if (rsize <= 1024) { rdata = _mpd_kmul(big->data, small->data, big->len, small->len, &rsize); + if (rdata == NULL) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } } else if (rsize <= 3*MPD_MAXTRANSFORM_2N) { rdata = _mpd_fntmul(big->data, small->data, big->len, small->len, &rsize); + if (rdata == NULL) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } } else { rdata = _mpd_kmul_fnt(big->data, small->data, big->len, small->len, &rsize); - } - - if (rdata == NULL) { - mpd_seterror(result, MPD_Malloc_error, status); - return; + if (rdata == NULL) { + mpd_seterror(result, MPD_Malloc_error, status); /* GCOV_UNLIKELY */ + return; /* GCOV_UNLIKELY */ + } } if (mpd_isdynamic_data(result)) { diff -r 2059910e7d76 -r ac776ef41428 Modules/_elementtree.c --- a/Modules/_elementtree.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/_elementtree.c Mon May 21 23:01:17 2012 -0400 @@ -58,6 +58,9 @@ /* Leave defined to include the expat-based XMLParser type */ #define USE_EXPAT +/* Define to do all expat calls via pyexpat's embedded expat library */ +/* #define USE_PYEXPAT_CAPI */ + /* An element can hold this many children without extra memory allocations. */ #define STATIC_CHILDREN 4 @@ -188,15 +191,6 @@ return result; } -/* Is the given object an empty dictionary? -*/ -static int -is_empty_dict(PyObject *obj) -{ - return PyDict_CheckExact(obj) && PyDict_Size(obj) == 0; -} - - /* -------------------------------------------------------------------- */ /* the Element type */ @@ -303,9 +297,14 @@ self = PyObject_GC_New(ElementObject, &Element_Type); if (self == NULL) return NULL; + + /* use None for empty dictionaries */ + if (PyDict_CheckExact(attrib) && !PyDict_Size(attrib)) + attrib = Py_None; + self->extra = NULL; - if (attrib != Py_None && !is_empty_dict(attrib)) { + if (attrib != Py_None) { if (create_extra(self, attrib) < 0) { PyObject_Del(self); return NULL; @@ -348,41 +347,6 @@ return (PyObject *)e; } -/* Helper function for extracting the attrib dictionary from a keywords dict. - * This is required by some constructors/functions in this module that can - * either accept attrib as a keyword argument or all attributes splashed - * directly into *kwds. - * If there is no 'attrib' keyword, return an empty dict. - */ -static PyObject* -get_attrib_from_keywords(PyObject *kwds) -{ - PyObject *attrib_str = PyUnicode_FromString("attrib"); - PyObject *attrib = PyDict_GetItem(kwds, attrib_str); - - if (attrib) { - /* If attrib was found in kwds, copy its value and remove it from - * kwds - */ - if (!PyDict_Check(attrib)) { - Py_DECREF(attrib_str); - PyErr_Format(PyExc_TypeError, "attrib must be dict, not %.100s", - Py_TYPE(attrib)->tp_name); - return NULL; - } - attrib = PyDict_Copy(attrib); - PyDict_DelItem(kwds, attrib_str); - } else { - attrib = PyDict_New(); - } - - Py_DECREF(attrib_str); - - if (attrib) - PyDict_Update(attrib, kwds); - return attrib; -} - static int element_init(PyObject *self, PyObject *args, PyObject *kwds) { @@ -394,37 +358,35 @@ if (!PyArg_ParseTuple(args, "O|O!:Element", &tag, &PyDict_Type, &attrib)) return -1; - if (attrib) { - /* attrib passed as positional arg */ - attrib = PyDict_Copy(attrib); + if (attrib || kwds) { + attrib = (attrib) ? PyDict_Copy(attrib) : PyDict_New(); if (!attrib) return -1; - if (kwds) { - if (PyDict_Update(attrib, kwds) < 0) { - return -1; - } - } - } else if (kwds) { - /* have keywords args */ - attrib = get_attrib_from_keywords(kwds); - if (!attrib) - return -1; + if (kwds) + PyDict_Update(attrib, kwds); } else { - /* no attrib arg, no kwds, so no attributes */ Py_INCREF(Py_None); attrib = Py_None; } self_elem = (ElementObject *)self; - if (attrib != Py_None && !is_empty_dict(attrib)) { + /* Use None for empty dictionaries */ + if (PyDict_CheckExact(attrib) && PyDict_Size(attrib) == 0) { + Py_INCREF(Py_None); + attrib = Py_None; + } + + if (attrib != Py_None) { if (create_extra(self_elem, attrib) < 0) { PyObject_Del(self_elem); return -1; } } - /* We own a reference to attrib here and it's no longer needed. */ + /* If create_extra needed attrib, it took a reference to it, so we can + * release ours anyway. + */ Py_DECREF(attrib); /* Replace the objects already pointed to by tag, text and tail. */ @@ -574,7 +536,7 @@ } static PyObject* -subelement(PyObject *self, PyObject *args, PyObject *kwds) +subelement(PyObject* self, PyObject* args, PyObject* kw) { PyObject* elem; @@ -586,23 +548,13 @@ &PyDict_Type, &attrib)) return NULL; - if (attrib) { - /* attrib passed as positional arg */ - attrib = PyDict_Copy(attrib); + if (attrib || kw) { + attrib = (attrib) ? PyDict_Copy(attrib) : PyDict_New(); if (!attrib) return NULL; - if (kwds) { - if (PyDict_Update(attrib, kwds) < 0) { - return NULL; - } - } - } else if (kwds) { - /* have keyword args */ - attrib = get_attrib_from_keywords(kwds); - if (!attrib) - return NULL; + if (kw) + PyDict_Update(attrib, kw); } else { - /* no attrib arg, no kwds, so no attribute */ Py_INCREF(Py_None); attrib = Py_None; } @@ -929,15 +881,13 @@ } static PyObject* -element_find(ElementObject *self, PyObject *args, PyObject *kwds) +element_find(ElementObject* self, PyObject* args) { int i; PyObject* tag; PyObject* namespaces = Py_None; - static char *kwlist[] = {"path", "namespaces", 0}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:find", kwlist, - &tag, &namespaces)) + + if (!PyArg_ParseTuple(args, "O|O:find", &tag, &namespaces)) return NULL; if (checkpath(tag) || namespaces != Py_None) { @@ -963,17 +913,15 @@ } static PyObject* -element_findtext(ElementObject *self, PyObject *args, PyObject *kwds) +element_findtext(ElementObject* self, PyObject* args) { int i; PyObject* tag; PyObject* default_value = Py_None; PyObject* namespaces = Py_None; _Py_IDENTIFIER(findtext); - static char *kwlist[] = {"path", "default", "namespaces", 0}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO:findtext", kwlist, - &tag, &default_value, &namespaces)) + + if (!PyArg_ParseTuple(args, "O|OO:findtext", &tag, &default_value, &namespaces)) return NULL; if (checkpath(tag) || namespaces != Py_None) @@ -1003,16 +951,14 @@ } static PyObject* -element_findall(ElementObject *self, PyObject *args, PyObject *kwds) +element_findall(ElementObject* self, PyObject* args) { int i; PyObject* out; PyObject* tag; PyObject* namespaces = Py_None; - static char *kwlist[] = {"path", "namespaces", 0}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:findall", kwlist, - &tag, &namespaces)) + + if (!PyArg_ParseTuple(args, "O|O:findall", &tag, &namespaces)) return NULL; if (checkpath(tag) || namespaces != Py_None) { @@ -1044,15 +990,13 @@ } static PyObject* -element_iterfind(ElementObject *self, PyObject *args, PyObject *kwds) +element_iterfind(ElementObject* self, PyObject* args) { PyObject* tag; PyObject* namespaces = Py_None; _Py_IDENTIFIER(iterfind); - static char *kwlist[] = {"path", "namespaces", 0}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:iterfind", kwlist, - &tag, &namespaces)) + + if (!PyArg_ParseTuple(args, "O|O:iterfind", &tag, &namespaces)) return NULL; return _PyObject_CallMethodId( @@ -1623,9 +1567,9 @@ {"get", (PyCFunction) element_get, METH_VARARGS}, {"set", (PyCFunction) element_set, METH_VARARGS}, - {"find", (PyCFunction) element_find, METH_VARARGS | METH_KEYWORDS}, - {"findtext", (PyCFunction) element_findtext, METH_VARARGS | METH_KEYWORDS}, - {"findall", (PyCFunction) element_findall, METH_VARARGS | METH_KEYWORDS}, + {"find", (PyCFunction) element_find, METH_VARARGS}, + {"findtext", (PyCFunction) element_findtext, METH_VARARGS}, + {"findall", (PyCFunction) element_findall, METH_VARARGS}, {"append", (PyCFunction) element_append, METH_VARARGS}, {"extend", (PyCFunction) element_extend, METH_VARARGS}, @@ -1634,7 +1578,7 @@ {"iter", (PyCFunction) element_iter, METH_VARARGS}, {"itertext", (PyCFunction) element_itertext, METH_VARARGS}, - {"iterfind", (PyCFunction) element_iterfind, METH_VARARGS | METH_KEYWORDS}, + {"iterfind", (PyCFunction) element_iterfind, METH_VARARGS}, {"getiterator", (PyCFunction) element_iter, METH_VARARGS}, {"getchildren", (PyCFunction) element_getchildren, METH_VARARGS}, @@ -1696,15 +1640,16 @@ return res; } -static PyObject* -element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value) +static int +element_setattr(ElementObject* self, const char* name, PyObject* value) { - char *name = ""; - if (PyUnicode_Check(nameobj)) - name = _PyUnicode_AsString(nameobj); - - if (name == NULL) - return NULL; + if (value == NULL) { + PyErr_SetString( + PyExc_AttributeError, + "can't delete element attributes" + ); + return -1; + } if (strcmp(name, "tag") == 0) { Py_DECREF(self->tag); @@ -1726,10 +1671,10 @@ Py_INCREF(self->extra->attrib); } else { PyErr_SetString(PyExc_AttributeError, name); - return NULL; + return -1; } - return NULL; + return 0; } static PySequenceMethods element_as_sequence = { @@ -1755,7 +1700,7 @@ (destructor)element_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ - 0, /* tp_setattr */ + (setattrfunc)element_setattr, /* tp_setattr */ 0, /* tp_reserved */ (reprfunc)element_repr, /* tp_repr */ 0, /* tp_as_number */ @@ -1765,7 +1710,7 @@ 0, /* tp_call */ 0, /* tp_str */ (getattrofunc)element_getattro, /* tp_getattro */ - (setattrofunc)element_setattro, /* tp_setattro */ + 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ @@ -1796,24 +1741,23 @@ typedef struct { PyObject_HEAD - PyObject *root; /* root node (first created node) */ - - ElementObject *this; /* current node */ - ElementObject *last; /* most recently created node */ - - PyObject *data; /* data collector (string or list), or NULL */ - - PyObject *stack; /* element stack */ - Py_ssize_t index; /* current stack size (0 means empty) */ - - PyObject *element_factory; + PyObject* root; /* root node (first created node) */ + + ElementObject* this; /* current node */ + ElementObject* last; /* most recently created node */ + + PyObject* data; /* data collector (string or list), or NULL */ + + PyObject* stack; /* element stack */ + Py_ssize_t index; /* current stack size (0=empty) */ /* element tracing */ - PyObject *events; /* list of events, or NULL if not collecting */ - PyObject *start_event_obj; /* event objects (NULL to ignore) */ - PyObject *end_event_obj; - PyObject *start_ns_event_obj; - PyObject *end_ns_event_obj; + PyObject* events; /* list of events, or NULL if not collecting */ + PyObject* start_event_obj; /* event objects (NULL to ignore) */ + PyObject* end_event_obj; + PyObject* start_ns_event_obj; + PyObject* end_ns_event_obj; + } TreeBuilderObject; static PyTypeObject TreeBuilder_Type; @@ -1823,70 +1767,48 @@ /* -------------------------------------------------------------------- */ /* constructor and destructor */ -static PyObject * -treebuilder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +LOCAL(PyObject*) +treebuilder_new(void) { - TreeBuilderObject *t = (TreeBuilderObject *)type->tp_alloc(type, 0); - if (t != NULL) { - t->root = NULL; - - Py_INCREF(Py_None); - t->this = (ElementObject *)Py_None; - Py_INCREF(Py_None); - t->last = (ElementObject *)Py_None; - - t->data = NULL; - t->element_factory = NULL; - t->stack = PyList_New(20); - if (!t->stack) { - Py_DECREF(t->this); - Py_DECREF(t->last); - return NULL; - } - t->index = 0; - - t->events = NULL; - t->start_event_obj = t->end_event_obj = NULL; - t->start_ns_event_obj = t->end_ns_event_obj = NULL; - } - return (PyObject *)t; + TreeBuilderObject* self; + + self = PyObject_New(TreeBuilderObject, &TreeBuilder_Type); + if (self == NULL) + return NULL; + + self->root = NULL; + + Py_INCREF(Py_None); + self->this = (ElementObject*) Py_None; + + Py_INCREF(Py_None); + self->last = (ElementObject*) Py_None; + + self->data = NULL; + + self->stack = PyList_New(20); + self->index = 0; + + self->events = NULL; + self->start_event_obj = self->end_event_obj = NULL; + self->start_ns_event_obj = self->end_ns_event_obj = NULL; + + ALLOC(sizeof(TreeBuilderObject), "create treebuilder"); + + return (PyObject*) self; } -static int -treebuilder_init(PyObject *self, PyObject *args, PyObject *kwds) +static PyObject* +treebuilder(PyObject* self_, PyObject* args) { - static char *kwlist[] = {"element_factory", 0}; - PyObject *element_factory = NULL; - TreeBuilderObject *self_tb = (TreeBuilderObject *)self; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:TreeBuilder", kwlist, - &element_factory)) { - return -1; - } - - if (element_factory) { - Py_INCREF(element_factory); - Py_XDECREF(self_tb->element_factory); - self_tb->element_factory = element_factory; - } - - return 0; + if (!PyArg_ParseTuple(args, ":TreeBuilder")) + return NULL; + + return treebuilder_new(); } -static int -treebuilder_gc_traverse(TreeBuilderObject *self, visitproc visit, void *arg) -{ - Py_VISIT(self->root); - Py_VISIT(self->this); - Py_VISIT(self->last); - Py_VISIT(self->data); - Py_VISIT(self->stack); - Py_VISIT(self->element_factory); - return 0; -} - -static int -treebuilder_gc_clear(TreeBuilderObject *self) +static void +treebuilder_dealloc(TreeBuilderObject* self) { Py_XDECREF(self->end_ns_event_obj); Py_XDECREF(self->start_ns_event_obj); @@ -1897,17 +1819,11 @@ Py_XDECREF(self->data); Py_DECREF(self->last); Py_DECREF(self->this); - Py_CLEAR(self->element_factory); Py_XDECREF(self->root); - return 0; -} - -static void -treebuilder_dealloc(TreeBuilderObject *self) -{ - PyObject_GC_UnTrack(self); - treebuilder_gc_clear(self); - Py_TYPE(self)->tp_free((PyObject *)self); + + RELEASE(sizeof(TreeBuilderObject), "destroy treebuilder"); + + PyObject_Del(self); } /* -------------------------------------------------------------------- */ @@ -1935,14 +1851,9 @@ self->data = NULL; } - if (self->element_factory) { - node = PyObject_CallFunction(self->element_factory, "OO", tag, attrib); - } else { - node = create_new_element(tag, attrib); - } - if (!node) { + node = create_new_element(tag, attrib); + if (!node) return NULL; - } this = (PyObject*) self->this; @@ -2201,42 +2112,31 @@ PyVarObject_HEAD_INIT(NULL, 0) "TreeBuilder", sizeof(TreeBuilderObject), 0, /* methods */ - (destructor)treebuilder_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_reserved */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, - /* tp_flags */ - 0, /* tp_doc */ - (traverseproc)treebuilder_gc_traverse, /* tp_traverse */ - (inquiry)treebuilder_gc_clear, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - treebuilder_methods, /* tp_methods */ - 0, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - (initproc)treebuilder_init, /* tp_init */ - PyType_GenericAlloc, /* tp_alloc */ - treebuilder_new, /* tp_new */ - 0, /* tp_free */ + (destructor)treebuilder_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + 0, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + treebuilder_methods, /* tp_methods */ + 0, /* tp_members */ }; /* ==================================================================== */ @@ -2245,39 +2145,38 @@ #if defined(USE_EXPAT) #include "expat.h" + +#if defined(USE_PYEXPAT_CAPI) #include "pyexpat.h" -static struct PyExpat_CAPI *expat_capi; +static struct PyExpat_CAPI* expat_capi; #define EXPAT(func) (expat_capi->func) - -static XML_Memory_Handling_Suite ExpatMemoryHandler = { - PyObject_Malloc, PyObject_Realloc, PyObject_Free}; +#else +#define EXPAT(func) (XML_##func) +#endif typedef struct { PyObject_HEAD XML_Parser parser; - PyObject *target; - PyObject *entity; - - PyObject *names; - - PyObject *handle_start; - PyObject *handle_data; - PyObject *handle_end; - - PyObject *handle_comment; - PyObject *handle_pi; - PyObject *handle_doctype; - - PyObject *handle_close; + PyObject* target; + PyObject* entity; + + PyObject* names; + + PyObject* handle_start; + PyObject* handle_data; + PyObject* handle_end; + + PyObject* handle_comment; + PyObject* handle_pi; + + PyObject* handle_close; } XMLParserObject; static PyTypeObject XMLParser_Type; -#define XMLParser_CheckExact(op) (Py_TYPE(op) == &XMLParser_Type) - /* helpers */ LOCAL(PyObject*) @@ -2481,20 +2380,17 @@ attrib = Py_None; } - /* If we get None, pass an empty dictionary on */ - if (attrib == Py_None) { - Py_DECREF(attrib); - attrib = PyDict_New(); - if (!attrib) - return; - } - - if (TreeBuilder_CheckExact(self->target)) { + if (TreeBuilder_CheckExact(self->target)) /* shortcut */ res = treebuilder_handle_start((TreeBuilderObject*) self->target, tag, attrib); - } else if (self->handle_start) { + if (attrib == Py_None) { + Py_DECREF(attrib); + attrib = PyDict_New(); + if (!attrib) + return; + } res = PyObject_CallFunction(self->handle_start, "OO", tag, attrib); } else res = NULL; @@ -2604,78 +2500,6 @@ } } -static void -expat_start_doctype_handler(XMLParserObject *self, - const XML_Char *doctype_name, - const XML_Char *sysid, - const XML_Char *pubid, - int has_internal_subset) -{ - PyObject *self_pyobj = (PyObject *)self; - PyObject *doctype_name_obj, *sysid_obj, *pubid_obj; - PyObject *parser_doctype = NULL; - PyObject *res = NULL; - - doctype_name_obj = makeuniversal(self, doctype_name); - if (!doctype_name_obj) - return; - - if (sysid) { - sysid_obj = makeuniversal(self, sysid); - if (!sysid_obj) { - Py_DECREF(doctype_name_obj); - return; - } - } else { - Py_INCREF(Py_None); - sysid_obj = Py_None; - } - - if (pubid) { - pubid_obj = makeuniversal(self, pubid); - if (!pubid_obj) { - Py_DECREF(doctype_name_obj); - Py_DECREF(sysid_obj); - return; - } - } else { - Py_INCREF(Py_None); - pubid_obj = Py_None; - } - - /* If the target has a handler for doctype, call it. */ - if (self->handle_doctype) { - res = PyObject_CallFunction(self->handle_doctype, "OOO", - doctype_name_obj, pubid_obj, sysid_obj); - Py_CLEAR(res); - } - - /* Now see if the parser itself has a doctype method. If yes and it's - * a subclass, call it but warn about deprecation. If it's not a subclass - * (i.e. vanilla XMLParser), do nothing. - */ - parser_doctype = PyObject_GetAttrString(self_pyobj, "doctype"); - if (parser_doctype) { - if (!XMLParser_CheckExact(self_pyobj)) { - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "This method of XMLParser is deprecated. Define" - " doctype() method on the TreeBuilder target.", - 1) < 0) { - goto clear; - } - res = PyObject_CallFunction(parser_doctype, "OOO", - doctype_name_obj, pubid_obj, sysid_obj); - Py_CLEAR(res); - } - } - -clear: - Py_XDECREF(parser_doctype); - Py_DECREF(doctype_name_obj); - Py_DECREF(pubid_obj); - Py_DECREF(sysid_obj); -} - static void expat_pi_handler(XMLParserObject* self, const XML_Char* target_in, const XML_Char* data_in) @@ -2741,131 +2565,121 @@ } /* -------------------------------------------------------------------- */ - -static PyObject * -xmlparser_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +/* constructor and destructor */ + +static PyObject* +xmlparser(PyObject* self_, PyObject* args, PyObject* kw) { - XMLParserObject *self = (XMLParserObject *)type->tp_alloc(type, 0); - if (self) { - self->parser = NULL; - self->target = self->entity = self->names = NULL; - self->handle_start = self->handle_data = self->handle_end = NULL; - self->handle_comment = self->handle_pi = self->handle_close = NULL; - self->handle_doctype = NULL; + XMLParserObject* self; + /* FIXME: does this need to be static? */ + static XML_Memory_Handling_Suite memory_handler; + + PyObject* target = NULL; + char* encoding = NULL; + static char* kwlist[] = { "target", "encoding", NULL }; + if (!PyArg_ParseTupleAndKeywords(args, kw, "|Oz:XMLParser", kwlist, + &target, &encoding)) + return NULL; + +#if defined(USE_PYEXPAT_CAPI) + if (!expat_capi) { + PyErr_SetString( + PyExc_RuntimeError, "cannot load dispatch table from pyexpat" + ); + return NULL; } - return (PyObject *)self; -} - -static int -xmlparser_init(PyObject *self, PyObject *args, PyObject *kwds) -{ - XMLParserObject *self_xp = (XMLParserObject *)self; - PyObject *target = NULL, *html = NULL; - char *encoding = NULL; - static char *kwlist[] = {"html", "target", "encoding", 0}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOz:XMLParser", kwlist, - &html, &target, &encoding)) { - return -1; +#endif + + self = PyObject_New(XMLParserObject, &XMLParser_Type); + if (self == NULL) + return NULL; + + self->entity = PyDict_New(); + if (!self->entity) { + PyObject_Del(self); + return NULL; } - self_xp->entity = PyDict_New(); - if (!self_xp->entity) - return -1; - - self_xp->names = PyDict_New(); - if (!self_xp->names) { - Py_XDECREF(self_xp->entity); - return -1; + self->names = PyDict_New(); + if (!self->names) { + PyObject_Del(self->entity); + PyObject_Del(self); + return NULL; } - self_xp->parser = EXPAT(ParserCreate_MM)(encoding, &ExpatMemoryHandler, "}"); - if (!self_xp->parser) { - Py_XDECREF(self_xp->entity); - Py_XDECREF(self_xp->names); + memory_handler.malloc_fcn = PyObject_Malloc; + memory_handler.realloc_fcn = PyObject_Realloc; + memory_handler.free_fcn = PyObject_Free; + + self->parser = EXPAT(ParserCreate_MM)(encoding, &memory_handler, "}"); + if (!self->parser) { + PyObject_Del(self->names); + PyObject_Del(self->entity); + PyObject_Del(self); PyErr_NoMemory(); - return -1; + return NULL; } - if (target) { + /* setup target handlers */ + if (!target) { + target = treebuilder_new(); + if (!target) { + EXPAT(ParserFree)(self->parser); + PyObject_Del(self->names); + PyObject_Del(self->entity); + PyObject_Del(self); + return NULL; + } + } else Py_INCREF(target); - } else { - target = treebuilder_new(&TreeBuilder_Type, NULL, NULL); - if (!target) { - Py_XDECREF(self_xp->entity); - Py_XDECREF(self_xp->names); - EXPAT(ParserFree)(self_xp->parser); - return -1; - } - } - self_xp->target = target; - - self_xp->handle_start = PyObject_GetAttrString(target, "start"); - self_xp->handle_data = PyObject_GetAttrString(target, "data"); - self_xp->handle_end = PyObject_GetAttrString(target, "end"); - self_xp->handle_comment = PyObject_GetAttrString(target, "comment"); - self_xp->handle_pi = PyObject_GetAttrString(target, "pi"); - self_xp->handle_close = PyObject_GetAttrString(target, "close"); - self_xp->handle_doctype = PyObject_GetAttrString(target, "doctype"); + self->target = target; + + self->handle_start = PyObject_GetAttrString(target, "start"); + self->handle_data = PyObject_GetAttrString(target, "data"); + self->handle_end = PyObject_GetAttrString(target, "end"); + self->handle_comment = PyObject_GetAttrString(target, "comment"); + self->handle_pi = PyObject_GetAttrString(target, "pi"); + self->handle_close = PyObject_GetAttrString(target, "close"); PyErr_Clear(); - + /* configure parser */ - EXPAT(SetUserData)(self_xp->parser, self_xp); + EXPAT(SetUserData)(self->parser, self); EXPAT(SetElementHandler)( - self_xp->parser, + self->parser, (XML_StartElementHandler) expat_start_handler, (XML_EndElementHandler) expat_end_handler ); EXPAT(SetDefaultHandlerExpand)( - self_xp->parser, + self->parser, (XML_DefaultHandler) expat_default_handler ); EXPAT(SetCharacterDataHandler)( - self_xp->parser, + self->parser, (XML_CharacterDataHandler) expat_data_handler ); - if (self_xp->handle_comment) + if (self->handle_comment) EXPAT(SetCommentHandler)( - self_xp->parser, + self->parser, (XML_CommentHandler) expat_comment_handler ); - if (self_xp->handle_pi) + if (self->handle_pi) EXPAT(SetProcessingInstructionHandler)( - self_xp->parser, + self->parser, (XML_ProcessingInstructionHandler) expat_pi_handler ); - EXPAT(SetStartDoctypeDeclHandler)( - self_xp->parser, - (XML_StartDoctypeDeclHandler) expat_start_doctype_handler - ); EXPAT(SetUnknownEncodingHandler)( - self_xp->parser, + self->parser, (XML_UnknownEncodingHandler) expat_unknown_encoding_handler, NULL ); - return 0; + ALLOC(sizeof(XMLParserObject), "create expatparser"); + + return (PyObject*) self; } -static int -xmlparser_gc_traverse(XMLParserObject *self, visitproc visit, void *arg) -{ - Py_VISIT(self->handle_close); - Py_VISIT(self->handle_pi); - Py_VISIT(self->handle_comment); - Py_VISIT(self->handle_end); - Py_VISIT(self->handle_data); - Py_VISIT(self->handle_start); - - Py_VISIT(self->target); - Py_VISIT(self->entity); - Py_VISIT(self->names); - - return 0; -} - -static int -xmlparser_gc_clear(XMLParserObject *self) +static void +xmlparser_dealloc(XMLParserObject* self) { EXPAT(ParserFree)(self->parser); @@ -2875,22 +2689,18 @@ Py_XDECREF(self->handle_end); Py_XDECREF(self->handle_data); Py_XDECREF(self->handle_start); - Py_XDECREF(self->handle_doctype); - - Py_XDECREF(self->target); - Py_XDECREF(self->entity); - Py_XDECREF(self->names); - - return 0; + + Py_DECREF(self->target); + Py_DECREF(self->entity); + Py_DECREF(self->names); + + RELEASE(sizeof(XMLParserObject), "destroy expatparser"); + + PyObject_Del(self); } -static void -xmlparser_dealloc(XMLParserObject* self) -{ - PyObject_GC_UnTrack(self); - xmlparser_gc_clear(self); - Py_TYPE(self)->tp_free((PyObject *)self); -} +/* -------------------------------------------------------------------- */ +/* methods (in alphabetical order) */ LOCAL(PyObject*) expat_parse(XMLParserObject* self, char* data, int data_len, int final) @@ -3032,13 +2842,7 @@ } static PyObject* -xmlparser_doctype(XMLParserObject *self, PyObject *args) -{ - Py_RETURN_NONE; -} - -static PyObject* -xmlparser_setevents(XMLParserObject *self, PyObject* args) +xmlparser_setevents(XMLParserObject* self, PyObject* args) { /* activate element event reporting */ @@ -3142,7 +2946,6 @@ {"close", (PyCFunction) xmlparser_close, METH_VARARGS}, {"_parse", (PyCFunction) xmlparser_parse, METH_VARARGS}, {"_setevents", (PyCFunction) xmlparser_setevents, METH_VARARGS}, - {"doctype", (PyCFunction) xmlparser_doctype, METH_VARARGS}, {NULL, NULL} }; @@ -3174,42 +2977,31 @@ PyVarObject_HEAD_INIT(NULL, 0) "XMLParser", sizeof(XMLParserObject), 0, /* methods */ - (destructor)xmlparser_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_reserved */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - (getattrofunc)xmlparser_getattro, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, - /* tp_flags */ - 0, /* tp_doc */ - (traverseproc)xmlparser_gc_traverse, /* tp_traverse */ - (inquiry)xmlparser_gc_clear, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - xmlparser_methods, /* tp_methods */ - 0, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - (initproc)xmlparser_init, /* tp_init */ - PyType_GenericAlloc, /* tp_alloc */ - xmlparser_new, /* tp_new */ - 0, /* tp_free */ + (destructor)xmlparser_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + (getattrofunc)xmlparser_getattro, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + 0, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + xmlparser_methods, /* tp_methods */ + 0, /* tp_members */ }; #endif @@ -3219,6 +3011,10 @@ static PyMethodDef _functions[] = { {"SubElement", (PyCFunction) subelement, METH_VARARGS|METH_KEYWORDS}, + {"TreeBuilder", (PyCFunction) treebuilder, METH_VARARGS}, +#if defined(USE_EXPAT) + {"XMLParser", (PyCFunction) xmlparser, METH_VARARGS|METH_KEYWORDS}, +#endif {NULL, NULL} }; @@ -3304,7 +3100,8 @@ elementtree_iter_obj = PyDict_GetItemString(g, "iter"); elementtree_itertext_obj = PyDict_GetItemString(g, "itertext"); - /* link against pyexpat */ +#if defined(USE_PYEXPAT_CAPI) + /* link against pyexpat, if possible */ expat_capi = PyCapsule_Import(PyExpat_CAPSULE_NAME, 0); if (expat_capi) { /* check that it's usable */ @@ -3312,16 +3109,10 @@ expat_capi->size < sizeof(struct PyExpat_CAPI) || expat_capi->MAJOR_VERSION != XML_MAJOR_VERSION || expat_capi->MINOR_VERSION != XML_MINOR_VERSION || - expat_capi->MICRO_VERSION != XML_MICRO_VERSION) { + expat_capi->MICRO_VERSION != XML_MICRO_VERSION) expat_capi = NULL; - } } - if (!expat_capi) { - PyErr_SetString( - PyExc_RuntimeError, "cannot load dispatch table from pyexpat" - ); - return NULL; - } +#endif elementtree_parseerror_obj = PyErr_NewException( "xml.etree.ElementTree.ParseError", PyExc_SyntaxError, NULL @@ -3332,13 +3123,5 @@ Py_INCREF((PyObject *)&Element_Type); PyModule_AddObject(m, "Element", (PyObject *)&Element_Type); - Py_INCREF((PyObject *)&TreeBuilder_Type); - PyModule_AddObject(m, "TreeBuilder", (PyObject *)&TreeBuilder_Type); - -#if defined(USE_EXPAT) - Py_INCREF((PyObject *)&XMLParser_Type); - PyModule_AddObject(m, "XMLParser", (PyObject *)&XMLParser_Type); -#endif - return m; } diff -r 2059910e7d76 -r ac776ef41428 Modules/_io/_iomodule.c --- a/Modules/_io/_iomodule.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/_io/_iomodule.c Mon May 21 23:01:17 2012 -0400 @@ -112,9 +112,8 @@ "'a' for appending (which on some Unix systems, means that all writes\n" "append to the end of the file regardless of the current seek position).\n" "In text mode, if encoding is not specified the encoding used is platform\n" -"dependent: locale.getpreferredencoding(False) is called to get the\n" -"current locale encoding. (For reading and writing raw bytes use binary\n" -"mode and leave encoding unspecified.) The available modes are:\n" +"dependent. (For reading and writing raw bytes use binary mode and leave\n" +"encoding unspecified.) The available modes are:\n" "\n" "========= ===============================================================\n" "Character Meaning\n" diff -r 2059910e7d76 -r ac776ef41428 Modules/_io/fileio.c --- a/Modules/_io/fileio.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/_io/fileio.c Mon May 21 23:01:17 2012 -0400 @@ -230,13 +230,9 @@ assert(PyFileIO_Check(oself)); if (self->fd >= 0) { - if (self->closefd) { - /* Have to close the existing file first. */ - if (internal_close(self) < 0) - return -1; - } - else - self->fd = -1; + /* Have to close the existing file first. */ + if (internal_close(self) < 0) + return -1; } if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|siO:fileio", diff -r 2059910e7d76 -r ac776ef41428 Modules/_io/textio.c --- a/Modules/_io/textio.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/_io/textio.c Mon May 21 23:01:17 2012 -0400 @@ -630,7 +630,7 @@ "Character and line based layer over a BufferedIOBase object, buffer.\n" "\n" "encoding gives the name of the encoding that the stream will be\n" - "decoded or encoded with. It defaults to locale.getpreferredencoding(False).\n" + "decoded or encoded with. It defaults to locale.getpreferredencoding.\n" "\n" "errors determines the strictness of encoding and decoding (see the\n" "codecs.register) and defaults to \"strict\".\n" @@ -898,7 +898,7 @@ else { use_locale: self->encoding = _PyObject_CallMethodId( - state->locale_module, &PyId_getpreferredencoding, "O", Py_False); + state->locale_module, &PyId_getpreferredencoding, NULL); if (self->encoding == NULL) { catch_ImportError: /* diff -r 2059910e7d76 -r ac776ef41428 Modules/_localemodule.c --- a/Modules/_localemodule.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/_localemodule.c Mon May 21 23:01:17 2012 -0400 @@ -257,12 +257,11 @@ n2 = wcsxfrm(buf, s, n1); if (n2 >= (size_t)n1) { /* more space needed */ - wchar_t * new_buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t)); - if (!new_buf) { + buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t)); + if (!buf) { PyErr_NoMemory(); goto exit; } - buf = new_buf; n2 = wcsxfrm(buf, s, n2+1); } result = PyUnicode_FromWideChar(buf, n2); diff -r 2059910e7d76 -r ac776ef41428 Modules/_multiprocessing/multiprocessing.c --- a/Modules/_multiprocessing/multiprocessing.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/_multiprocessing/multiprocessing.c Mon May 21 23:01:17 2012 -0400 @@ -51,6 +51,20 @@ return NULL; } + +static PyObject* +multiprocessing_address_of_buffer(PyObject *self, PyObject *obj) +{ + void *buffer; + Py_ssize_t buffer_len; + + if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0) + return NULL; + + return Py_BuildValue("Nn", + PyLong_FromVoidPtr(buffer), buffer_len); +} + #ifdef MS_WINDOWS static PyObject * multiprocessing_closesocket(PyObject *self, PyObject *args) @@ -123,6 +137,9 @@ */ static PyMethodDef module_methods[] = { + {"address_of_buffer", multiprocessing_address_of_buffer, METH_O, + "address_of_buffer(obj) -> int\n" + "Return address of obj assuming obj supports buffer inteface"}, #ifdef MS_WINDOWS {"closesocket", multiprocessing_closesocket, METH_VARARGS, ""}, {"recv", multiprocessing_recv, METH_VARARGS, ""}, diff -r 2059910e7d76 -r ac776ef41428 Modules/_posixsubprocess.c --- a/Modules/_posixsubprocess.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/_posixsubprocess.c Mon May 21 23:01:17 2012 -0400 @@ -177,15 +177,8 @@ * chooses to break compatibility with all existing binaries. Highly Unlikely. */ struct linux_dirent { -#if defined(__x86_64__) && defined(__ILP32__) - /* Support the wacky x32 ABI (fake 32-bit userspace speaking to x86_64 - * kernel interfaces) - https://sites.google.com/site/x32abi/ */ - unsigned long long d_ino; - unsigned long long d_off; -#else unsigned long d_ino; /* Inode number */ unsigned long d_off; /* Offset to next linux_dirent */ -#endif unsigned short d_reclen; /* Length of this linux_dirent */ char d_name[256]; /* Filename (null-terminated) */ }; diff -r 2059910e7d76 -r ac776ef41428 Modules/_randommodule.c --- a/Modules/_randommodule.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/_randommodule.c Mon May 21 23:01:17 2012 -0400 @@ -210,7 +210,7 @@ PyObject *masklower = NULL; PyObject *thirtytwo = NULL; PyObject *n = NULL; - unsigned long *new_key, *key = NULL; + unsigned long *key = NULL; unsigned long keymax; /* # of allocated slots in key */ unsigned long keyused; /* # of used slots in key */ int err; @@ -287,11 +287,10 @@ PyErr_NoMemory(); goto Done; } - new_key = (unsigned long *)PyMem_Realloc(key, + key = (unsigned long *)PyMem_Realloc(key, bigger * sizeof(*key)); - if (new_key == NULL) + if (key == NULL) goto Done; - key = new_key; keymax = bigger; } assert(keyused < keymax); diff -r 2059910e7d76 -r ac776ef41428 Modules/gcmodule.c --- a/Modules/gcmodule.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/gcmodule.c Mon May 21 23:01:17 2012 -0400 @@ -116,46 +116,6 @@ http://mail.python.org/pipermail/python-dev/2008-June/080579.html */ -/* - NOTE: about untracking of mutable objects. - - Certain types of container cannot participate in a reference cycle, and - so do not need to be tracked by the garbage collector. Untracking these - objects reduces the cost of garbage collections. However, determining - which objects may be untracked is not free, and the costs must be - weighed against the benefits for garbage collection. - - There are two possible strategies for when to untrack a container: - - i) When the container is created. - ii) When the container is examined by the garbage collector. - - Tuples containing only immutable objects (integers, strings etc, and - recursively, tuples of immutable objects) do not need to be tracked. - The interpreter creates a large number of tuples, many of which will - not survive until garbage collection. It is therefore not worthwhile - to untrack eligible tuples at creation time. - - Instead, all tuples except the empty tuple are tracked when created. - During garbage collection it is determined whether any surviving tuples - can be untracked. A tuple can be untracked if all of its contents are - already not tracked. Tuples are examined for untracking in all garbage - collection cycles. It may take more than one cycle to untrack a tuple. - - Dictionaries containing only immutable objects also do not need to be - tracked. Dictionaries are untracked when created. If a tracked item is - inserted into a dictionary (either as a key or value), the dictionary - becomes tracked. During a full garbage collection (all generations), - the collector will untrack any dictionaries whose contents are not - tracked. - - The module provides the python function is_tracked(obj), which returns - the CURRENT tracking status of the object. Subsequent garbage - collections may change the tracking status of the object. - - Untracking of certain containers was introduced in issue #4688, and - the algorithm was refined in response to issue #14775. -*/ /* set for debugging information */ #define DEBUG_STATS (1<<0) /* print collection statistics */ @@ -477,6 +437,9 @@ if (PyTuple_CheckExact(op)) { _PyTuple_MaybeUntrack(op); } + else if (PyDict_CheckExact(op)) { + _PyDict_MaybeUntrack(op); + } } else { /* This *may* be unreachable. To make progress, @@ -494,20 +457,6 @@ } } -/* Try to untrack all currently tracked dictionaries */ -static void -untrack_dicts(PyGC_Head *head) -{ - PyGC_Head *next, *gc = head->gc.gc_next; - while (gc != head) { - PyObject *op = FROM_GC(gc); - next = gc->gc.gc_next; - if (PyDict_CheckExact(op)) - _PyDict_MaybeUntrack(op); - gc = next; - } -} - /* Return true if object has a finalization method. */ static int has_finalizer(PyObject *op) @@ -907,9 +856,6 @@ gc_list_merge(young, old); } else { - /* We only untrack dicts in full collections, to avoid quadratic - dict build-up. See issue #14775. */ - untrack_dicts(young); long_lived_pending = 0; long_lived_total = gc_list_size(young); } diff -r 2059910e7d76 -r ac776ef41428 Modules/getpath.c --- a/Modules/getpath.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/getpath.c Mon May 21 23:01:17 2012 -0400 @@ -260,59 +260,6 @@ wcscpy(path, buffer); } -/* search for a prefix value in an environment file. If found, copy it - to the provided buffer, which is expected to be no more than MAXPATHLEN - bytes long. -*/ - -static int -find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value) -{ - int result = 0; /* meaning not found */ - char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */ - - fseek(env_file, 0, SEEK_SET); - while (!feof(env_file)) { - char * p = fgets(buffer, MAXPATHLEN*2, env_file); - wchar_t tmpbuffer[MAXPATHLEN*2+1]; - PyObject * decoded; - int n; - - if (p == NULL) - break; - n = strlen(p); - if (p[n - 1] != '\n') { - /* line has overflowed - bail */ - break; - } - if (p[0] == '#') /* Comment - skip */ - continue; - decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape"); - if (decoded != NULL) { - Py_ssize_t k; - wchar_t * state; - k = PyUnicode_AsWideChar(decoded, - tmpbuffer, MAXPATHLEN * 2); - Py_DECREF(decoded); - if (k >= 0) { - wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n", &state); - if ((tok != NULL) && !wcscmp(tok, key)) { - tok = wcstok(NULL, L" \t", &state); - if ((tok != NULL) && !wcscmp(tok, L"=")) { - tok = wcstok(NULL, L"\r\n", &state); - if (tok != NULL) { - wcsncpy(value, tok, MAXPATHLEN); - result = 1; - break; - } - } - } - } - } - } - return result; -} - /* search_for_prefix requires that argv0_path be no more than MAXPATHLEN bytes long. */ @@ -618,39 +565,6 @@ MAXPATHLEN bytes long. */ - /* Search for an environment configuration file, first in the - executable's directory and then in the parent directory. - If found, open it for use when searching for prefixes. - */ - - { - wchar_t tmpbuffer[MAXPATHLEN+1]; - wchar_t *env_cfg = L"pyvenv.cfg"; - FILE * env_file = NULL; - - wcscpy(tmpbuffer, argv0_path); - joinpath(tmpbuffer, env_cfg); - env_file = _Py_wfopen(tmpbuffer, L"r"); - if (env_file == NULL) { - errno = 0; - reduce(tmpbuffer); - reduce(tmpbuffer); - joinpath(tmpbuffer, env_cfg); - env_file = _Py_wfopen(tmpbuffer, L"r"); - if (env_file == NULL) { - errno = 0; - } - } - if (env_file != NULL) { - /* Look for a 'home' variable and set argv0_path to it, if found */ - if (find_env_config_value(env_file, L"home", tmpbuffer)) { - wcscpy(argv0_path, tmpbuffer); - } - fclose(env_file); - env_file = NULL; - } - } - if (!(pfound = search_for_prefix(argv0_path, home, _prefix))) { if (!Py_FrozenFlag) fprintf(stderr, diff -r 2059910e7d76 -r ac776ef41428 Modules/posixmodule.c --- a/Modules/posixmodule.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/posixmodule.c Mon May 21 23:01:17 2012 -0400 @@ -1721,10 +1721,6 @@ int newval = -1; if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) return NULL; - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "stat_float_times() is deprecated", - 1)) - return NULL; if (newval == -1) /* Return old value */ return PyBool_FromLong(_stat_float_times); @@ -3609,7 +3605,7 @@ PyObject *args; PyObject *kwargs; - /* input/output */ + /* input/output */ PyObject **path; /* output only */ @@ -3659,17 +3655,17 @@ timet[1] = ua.mtime_s -/* +/* * utime_read_time_arguments() processes arguments for the utime * family of functions. */ typedef enum { - UTIME_SUCCESS = 0, - UTIME_PARSE_FAILURE = 1, - UTIME_TIMES_AND_NS_COLLISION = 2, - UTIME_TIMES_CONVERSION_FAILURE = 3, - UTIME_NS_CONVERSION_FAILURE = 4, + utime_success = 0, + utime_parse_failure = 1, + utime_times_and_ns_collision = 2, + utime_times_conversion_failure = 3, + utime_ns_conversion_failure = 4, } utime_status; static utime_status @@ -3701,14 +3697,14 @@ format, kwlist, ua->path, ×, &ns); if (!parse_result) - return UTIME_PARSE_FAILURE; + return utime_parse_failure; if (times && ns) { PyErr_Format(PyExc_RuntimeError, "%s: you may specify either 'times'" " or 'ns' but not both", ua->function_name); - return_value = UTIME_TIMES_AND_NS_COLLISION; + return_value = utime_times_and_ns_collision; goto fail; } @@ -3718,7 +3714,7 @@ "%s: 'times' must be either" " a tuple of two ints or None", ua->function_name); - return_value = UTIME_TIMES_CONVERSION_FAILURE; + return_value = utime_times_conversion_failure; goto fail; } ua->now = 0; @@ -3726,10 +3722,10 @@ &ua->atime_s, &ua->atime_ns) == -1 || _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), &ua->mtime_s, &ua->mtime_ns) == -1) { - return_value = UTIME_TIMES_CONVERSION_FAILURE; + return_value = utime_times_conversion_failure; goto fail; } - return UTIME_SUCCESS; + return utime_success; } if (ns) { @@ -3737,7 +3733,7 @@ PyErr_Format(PyExc_TypeError, "%s: 'ns' must be a tuple of two ints", ua->function_name); - return_value = UTIME_NS_CONVERSION_FAILURE; + return_value = utime_ns_conversion_failure; goto fail; } ua->now = 0; @@ -3745,15 +3741,15 @@ &ua->atime_s, &ua->atime_ns) || !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), &ua->mtime_s, &ua->mtime_ns)) { - return_value = UTIME_NS_CONVERSION_FAILURE; + return_value = utime_ns_conversion_failure; goto fail; } - return UTIME_SUCCESS; + return utime_success; } /* either times=None, or neither times nor ns was specified. use "now". */ ua->now = 1; - return UTIME_SUCCESS; + return utime_success; fail: if (ua->converter) @@ -3790,7 +3786,7 @@ switch (utime_read_time_arguments(&ua)) { default: return NULL; - case UTIME_SUCCESS: { + case utime_success: { wchar_t *wpath = PyUnicode_AsUnicode(upath); if (wpath == NULL) return NULL; @@ -3803,7 +3799,7 @@ return win32_error_object("utime", upath); break; } - case UTIME_PARSE_FAILURE: { + case utime_parse_failure: { const char *apath; /* Drop the argument parsing error as narrow strings are also valid. */ @@ -3811,7 +3807,7 @@ ua.path_format = 'y'; ua.path = (PyObject **)&apath; - if (utime_read_time_arguments(&ua) != UTIME_SUCCESS) + if (utime_read_time_arguments(&ua) != utime_success) return NULL; if (win32_warn_bytes_api()) return NULL; @@ -3866,7 +3862,7 @@ ua.path = &opath; ua.converter = PyUnicode_FSConverter; - if (utime_read_time_arguments(&ua) != UTIME_SUCCESS) + if (utime_read_time_arguments(&ua) != utime_success) return NULL; path = PyBytes_AsString(opath); if (ua.now) { @@ -3919,7 +3915,7 @@ ua.path = (PyObject **)&fd; ua.first_argument_name = "fd"; - if (utime_read_time_arguments(&ua) != UTIME_SUCCESS) + if (utime_read_time_arguments(&ua) != utime_success) return NULL; if (ua.now) { @@ -3964,7 +3960,7 @@ ua.path = &opath; ua.converter = PyUnicode_FSConverter; - if (utime_read_time_arguments(&ua) != UTIME_SUCCESS) + if (utime_read_time_arguments(&ua) != utime_success) return NULL; path = PyBytes_AsString(opath); diff -r 2059910e7d76 -r ac776ef41428 Modules/pyexpat.c --- a/Modules/pyexpat.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/pyexpat.c Mon May 21 23:01:17 2012 -0400 @@ -1904,7 +1904,6 @@ capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler; capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler; capi.SetUserData = XML_SetUserData; - capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler; /* export using capsule */ capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL); diff -r 2059910e7d76 -r ac776ef41428 Modules/unicodedata.c --- a/Modules/unicodedata.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/unicodedata.c Mon May 21 23:01:17 2012 -0400 @@ -526,16 +526,13 @@ /* Hangul Decomposition adds three characters in a single step, so we need atleast that much room. */ if (space < 3) { - Py_UCS4 *new_output; osize += 10; space += 10; - new_output = PyMem_Realloc(output, osize*sizeof(Py_UCS4)); - if (new_output == NULL) { - PyMem_Free(output); + output = PyMem_Realloc(output, osize*sizeof(Py_UCS4)); + if (output == NULL) { PyErr_NoMemory(); return NULL; } - output = new_output; } /* Hangul Decomposition. */ if (SBase <= code && code < (SBase+SCount)) { diff -r 2059910e7d76 -r ac776ef41428 Modules/zipimport.c --- a/Modules/zipimport.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Modules/zipimport.c Mon May 21 23:01:17 2012 -0400 @@ -259,29 +259,6 @@ MI_PACKAGE }; -/* Does this path represent a directory? - on error, return < 0 - if not a dir, return 0 - if a dir, return 1 -*/ -static int -check_is_directory(ZipImporter *self, PyObject* prefix, PyObject *path) -{ - PyObject *dirpath; - int res; - - /* See if this is a "directory". If so, it's eligible to be part - of a namespace package. We test by seeing if the name, with an - appended path separator, exists. */ - dirpath = PyUnicode_FromFormat("%U%U%c", prefix, path, SEP); - if (dirpath == NULL) - return -1; - /* If dirpath is present in self->files, we have a directory. */ - res = PyDict_Contains(self->files, dirpath); - Py_DECREF(dirpath); - return res; -} - /* Return some information about a module. */ static enum zi_module_info get_module_info(ZipImporter *self, PyObject *fullname) @@ -319,53 +296,6 @@ return MI_NOT_FOUND; } -typedef enum { - FL_ERROR, - FL_NOT_FOUND, - FL_MODULE_FOUND, - FL_NS_FOUND -} find_loader_result; - -/* The guts of "find_loader" and "find_module". Return values: - -1: error - 0: no loader or namespace portions found - 1: module/package found - 2: namespace portion found: *namespace_portion will point to the name -*/ -static find_loader_result -find_loader(ZipImporter *self, PyObject *fullname, PyObject **namespace_portion) -{ - enum zi_module_info mi; - - *namespace_portion = NULL; - - mi = get_module_info(self, fullname); - if (mi == MI_ERROR) - return FL_ERROR; - if (mi == MI_NOT_FOUND) { - /* Not a module or regular package. See if this is a directory, and - therefore possibly a portion of a namespace package. */ - int is_dir = check_is_directory(self, self->prefix, fullname); - if (is_dir < 0) - return -1; - if (is_dir) { - /* This is possibly a portion of a namespace - package. Return the string representing its path, - without a trailing separator. */ - *namespace_portion = PyUnicode_FromFormat("%U%c%U%U", - self->archive, SEP, - self->prefix, fullname); - if (*namespace_portion == NULL) - return FL_ERROR; - return FL_NS_FOUND; - } - return FL_NOT_FOUND; - } - /* This is a module or package. */ - return FL_MODULE_FOUND; -} - - /* Check whether we can satisfy the import of the module named by 'fullname'. Return self if we can, None if we can't. */ static PyObject * @@ -374,63 +304,21 @@ ZipImporter *self = (ZipImporter *)obj; PyObject *path = NULL; PyObject *fullname; - PyObject *namespace_portion = NULL; - PyObject *result = NULL; + enum zi_module_info mi; - if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path)) + if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", + &fullname, &path)) return NULL; - switch (find_loader(self, fullname, &namespace_portion)) { - case FL_ERROR: + mi = get_module_info(self, fullname); + if (mi == MI_ERROR) return NULL; - case FL_NS_FOUND: - /* A namespace portion is not allowed via find_module, so return None. */ - Py_DECREF(namespace_portion); - /* FALL THROUGH */ - case FL_NOT_FOUND: - result = Py_None; - break; - case FL_MODULE_FOUND: - result = (PyObject *)self; - break; + if (mi == MI_NOT_FOUND) { + Py_INCREF(Py_None); + return Py_None; } - Py_INCREF(result); - return result; -} - - -/* Check whether we can satisfy the import of the module named by - 'fullname', or whether it could be a portion of a namespace - package. Return self if we can load it, a string containing the - full path if it's a possible namespace portion, None if we - can't load it. */ -static PyObject * -zipimporter_find_loader(PyObject *obj, PyObject *args) -{ - ZipImporter *self = (ZipImporter *)obj; - PyObject *path = NULL; - PyObject *fullname; - PyObject *result = NULL; - PyObject *namespace_portion = NULL; - - if (!PyArg_ParseTuple(args, "U|O:zipimporter.find_module", &fullname, &path)) - return NULL; - - switch (find_loader(self, fullname, &namespace_portion)) { - case FL_ERROR: - return NULL; - case FL_NOT_FOUND: /* Not found, return (None, []) */ - result = Py_BuildValue("O[]", Py_None); - break; - case FL_MODULE_FOUND: /* Return (self, []) */ - result = Py_BuildValue("O[]", self); - break; - case FL_NS_FOUND: /* Return (None, [namespace_portion]) */ - result = Py_BuildValue("O[O]", Py_None, namespace_portion); - Py_DECREF(namespace_portion); - return result; - } - return result; + Py_INCREF(self); + return (PyObject *)self; } /* Load and return the module named by 'fullname'. */ @@ -670,16 +558,6 @@ The optional 'path' argument is ignored -- it's there for compatibility\n\ with the importer protocol."); -PyDoc_STRVAR(doc_find_loader, -"find_loader(fullname, path=None) -> self, str or None.\n\ -\n\ -Search for a module specified by 'fullname'. 'fullname' must be the\n\ -fully qualified (dotted) module name. It returns the zipimporter\n\ -instance itself if the module was found, a string containing the\n\ -full path name if it's possibly a portion of a namespace package,\n\ -or None otherwise. The optional 'path' argument is ignored -- it's\n\ - there for compatibility with the importer protocol."); - PyDoc_STRVAR(doc_load_module, "load_module(fullname) -> module.\n\ \n\ @@ -721,8 +599,6 @@ static PyMethodDef zipimporter_methods[] = { {"find_module", zipimporter_find_module, METH_VARARGS, doc_find_module}, - {"find_loader", zipimporter_find_loader, METH_VARARGS, - doc_find_loader}, {"load_module", zipimporter_load_module, METH_VARARGS, doc_load_module}, {"get_data", zipimporter_get_data, METH_VARARGS, diff -r 2059910e7d76 -r ac776ef41428 Objects/bytesobject.c --- a/Objects/bytesobject.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Objects/bytesobject.c Mon May 21 23:01:17 2012 -0400 @@ -2577,12 +2577,6 @@ PyErr_BadInternalCall(); return NULL; } - - if (PyBytes_CheckExact(x)) { - Py_INCREF(x); - return x; - } - /* Use the modern buffer interface */ if (PyObject_CheckBuffer(x)) { Py_buffer view; diff -r 2059910e7d76 -r ac776ef41428 Objects/complexobject.c --- a/Objects/complexobject.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Objects/complexobject.c Mon May 21 23:01:17 2012 -0400 @@ -699,22 +699,11 @@ complex__format__(PyObject* self, PyObject* args) { PyObject *format_spec; - _PyUnicodeWriter writer; - int ret; if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) - return NULL; - - _PyUnicodeWriter_Init(&writer, 0); - ret = _PyComplex_FormatAdvancedWriter( - &writer, - self, - format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); - if (ret == -1) { - _PyUnicodeWriter_Dealloc(&writer); - return NULL; - } - return _PyUnicodeWriter_Finish(&writer); + return NULL; + return _PyComplex_FormatAdvanced(self, format_spec, 0, + PyUnicode_GET_LENGTH(format_spec)); } #if 0 diff -r 2059910e7d76 -r ac776ef41428 Objects/floatobject.c --- a/Objects/floatobject.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Objects/floatobject.c Mon May 21 23:01:17 2012 -0400 @@ -267,15 +267,13 @@ float_repr(PyFloatObject *v) { PyObject *result; - char *buf; - - buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v), - 'r', 0, - Py_DTSF_ADD_DOT_0, - NULL); + char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v), + 'r', 0, + Py_DTSF_ADD_DOT_0, + NULL); if (!buf) return PyErr_NoMemory(); - result = _PyUnicode_FromASCII(buf, strlen(buf)); + result = PyUnicode_FromString(buf); PyMem_Free(buf); return result; } @@ -1705,22 +1703,11 @@ float__format__(PyObject *self, PyObject *args) { PyObject *format_spec; - _PyUnicodeWriter writer; - int ret; if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) return NULL; - - _PyUnicodeWriter_Init(&writer, 0); - ret = _PyFloat_FormatAdvancedWriter( - &writer, - self, - format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); - if (ret == -1) { - _PyUnicodeWriter_Dealloc(&writer); - return NULL; - } - return _PyUnicodeWriter_Finish(&writer); + return _PyFloat_FormatAdvanced(self, format_spec, 0, + PyUnicode_GET_LENGTH(format_spec)); } PyDoc_STRVAR(float__format__doc, diff -r 2059910e7d76 -r ac776ef41428 Objects/frameobject.c --- a/Objects/frameobject.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Objects/frameobject.c Mon May 21 23:01:17 2012 -0400 @@ -663,13 +663,11 @@ f = free_list; free_list = free_list->f_back; if (Py_SIZE(f) < extras) { - PyFrameObject *new_f = PyObject_GC_Resize(PyFrameObject, f, extras); - if (new_f == NULL) { - PyObject_GC_Del(f); + f = PyObject_GC_Resize(PyFrameObject, f, extras); + if (f == NULL) { Py_DECREF(builtins); return NULL; } - f = new_f; } _Py_NewReference((PyObject *)f); } diff -r 2059910e7d76 -r ac776ef41428 Objects/longobject.c --- a/Objects/longobject.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Objects/longobject.c Mon May 21 23:01:17 2012 -0400 @@ -1550,22 +1550,20 @@ string. (Return value is non-shared so that callers can modify the returned value if necessary.) */ -static int -long_to_decimal_string_internal(PyObject *aa, - PyObject **p_output, - _PyUnicodeWriter *writer) +static PyObject * +long_to_decimal_string(PyObject *aa) { PyLongObject *scratch, *a; PyObject *str; Py_ssize_t size, strlen, size_a, i, j; digit *pout, *pin, rem, tenpow; + unsigned char *p; int negative; - enum PyUnicode_Kind kind; a = (PyLongObject *)aa; if (a == NULL || !PyLong_Check(a)) { PyErr_BadInternalCall(); - return -1; + return NULL; } size_a = ABS(Py_SIZE(a)); negative = Py_SIZE(a) < 0; @@ -1582,13 +1580,13 @@ if (size_a > PY_SSIZE_T_MAX / PyLong_SHIFT) { PyErr_SetString(PyExc_OverflowError, "long is too large to format"); - return -1; + return NULL; } /* the expression size_a * PyLong_SHIFT is now safe from overflow */ size = 1 + size_a * PyLong_SHIFT / (3 * _PyLong_DECIMAL_SHIFT); scratch = _PyLong_New(size); if (scratch == NULL) - return -1; + return NULL; /* convert array of base _PyLong_BASE digits in pin to an array of base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP, @@ -1611,7 +1609,7 @@ /* check for keyboard interrupt */ SIGCHECK({ Py_DECREF(scratch); - return -1; + return NULL; }); } /* pout should have at least one digit, so that the case when a = 0 @@ -1627,113 +1625,65 @@ tenpow *= 10; strlen++; } - if (writer) { - if (_PyUnicodeWriter_Prepare(writer, strlen, '9') == -1) - return -1; - kind = writer->kind; - str = NULL; - } - else { - str = PyUnicode_New(strlen, '9'); - if (str == NULL) { - Py_DECREF(scratch); - return -1; + str = PyUnicode_New(strlen, '9'); + if (str == NULL) { + Py_DECREF(scratch); + return NULL; + } + + /* fill the string right-to-left */ + assert(PyUnicode_KIND(str) == PyUnicode_1BYTE_KIND); + p = PyUnicode_1BYTE_DATA(str) + strlen; + *p = '\0'; + /* pout[0] through pout[size-2] contribute exactly + _PyLong_DECIMAL_SHIFT digits each */ + for (i=0; i < size - 1; i++) { + rem = pout[i]; + for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) { + *--p = '0' + rem % 10; + rem /= 10; } - kind = PyUnicode_KIND(str); - } - -#define WRITE_DIGITS(TYPE) \ - do { \ - if (writer) \ - p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + strlen; \ - else \ - p = (TYPE*)PyUnicode_DATA(str) + strlen; \ - \ - *p = '\0'; \ - /* pout[0] through pout[size-2] contribute exactly \ - _PyLong_DECIMAL_SHIFT digits each */ \ - for (i=0; i < size - 1; i++) { \ - rem = pout[i]; \ - for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) { \ - *--p = '0' + rem % 10; \ - rem /= 10; \ - } \ - } \ - /* pout[size-1]: always produce at least one decimal digit */ \ - rem = pout[i]; \ - do { \ - *--p = '0' + rem % 10; \ - rem /= 10; \ - } while (rem != 0); \ - \ - /* and sign */ \ - if (negative) \ - *--p = '-'; \ - \ - /* check we've counted correctly */ \ - if (writer) \ - assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ - else \ - assert(p == (TYPE*)PyUnicode_DATA(str)); \ - } while (0) - - /* fill the string right-to-left */ - if (kind == PyUnicode_1BYTE_KIND) { - Py_UCS1 *p; - WRITE_DIGITS(Py_UCS1); - } - else if (kind == PyUnicode_2BYTE_KIND) { - Py_UCS2 *p; - WRITE_DIGITS(Py_UCS2); - } - else { - Py_UCS4 *p; - assert (kind == PyUnicode_4BYTE_KIND); - WRITE_DIGITS(Py_UCS4); - } -#undef WRITE_DIGITS - + } + /* pout[size-1]: always produce at least one decimal digit */ + rem = pout[i]; + do { + *--p = '0' + rem % 10; + rem /= 10; + } while (rem != 0); + + /* and sign */ + if (negative) + *--p = '-'; + + /* check we've counted correctly */ + assert(p == PyUnicode_1BYTE_DATA(str)); + assert(_PyUnicode_CheckConsistency(str, 1)); Py_DECREF(scratch); - if (writer) { - writer->pos += strlen; - } - else { - assert(_PyUnicode_CheckConsistency(str, 1)); - *p_output = (PyObject *)str; - } - return 0; + return (PyObject *)str; } -static PyObject * -long_to_decimal_string(PyObject *aa) -{ - PyObject *v; - if (long_to_decimal_string_internal(aa, &v, NULL) == -1) - return NULL; - return v; -} - /* Convert a long int object to a string, using a given conversion base, - which should be one of 2, 8 or 16. Return a string object. - If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x' - if alternate is nonzero. */ - -static int -long_format_binary(PyObject *aa, int base, int alternate, - PyObject **p_output, _PyUnicodeWriter *writer) + which should be one of 2, 8, 10 or 16. Return a string object. + If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. */ + +PyObject * +_PyLong_Format(PyObject *aa, int base) { register PyLongObject *a = (PyLongObject *)aa; PyObject *v; Py_ssize_t sz; Py_ssize_t size_a; - enum PyUnicode_Kind kind; + Py_UCS1 *p; int negative; int bits; - assert(base == 2 || base == 8 || base == 16); + assert(base == 2 || base == 8 || base == 10 || base == 16); + if (base == 10) + return long_to_decimal_string((PyObject *)a); + if (a == NULL || !PyLong_Check(a)) { PyErr_BadInternalCall(); - return -1; + return NULL; } size_a = ABS(Py_SIZE(a)); negative = Py_SIZE(a) < 0; @@ -1756,7 +1706,7 @@ /* Compute exact length 'sz' of output string. */ if (size_a == 0) { - sz = 1; + sz = 3; } else { Py_ssize_t size_a_in_bits; @@ -1764,126 +1714,56 @@ if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) { PyErr_SetString(PyExc_OverflowError, "int is too large to format"); - return -1; + return NULL; } size_a_in_bits = (size_a - 1) * PyLong_SHIFT + bits_in_digit(a->ob_digit[size_a - 1]); - /* Allow 1 character for a '-' sign. */ - sz = negative + (size_a_in_bits + (bits - 1)) / bits; - } - if (alternate) { - /* 2 characters for prefix */ - sz += 2; - } - - if (writer) { - if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1) - return -1; - kind = writer->kind; - v = NULL; + /* Allow 2 characters for prefix and 1 for a '-' sign. */ + sz = 2 + negative + (size_a_in_bits + (bits - 1)) / bits; + } + + v = PyUnicode_New(sz, 'x'); + if (v == NULL) { + return NULL; + } + assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND); + + p = PyUnicode_1BYTE_DATA(v) + sz; + if (size_a == 0) { + *--p = '0'; } else { - v = PyUnicode_New(sz, 'x'); - if (v == NULL) - return -1; - kind = PyUnicode_KIND(v); - } - -#define WRITE_DIGITS(TYPE) \ - do { \ - if (writer) \ - p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + sz; \ - else \ - p = (TYPE*)PyUnicode_DATA(v) + sz; \ - \ - if (size_a == 0) { \ - *--p = '0'; \ - } \ - else { \ - /* JRH: special case for power-of-2 bases */ \ - twodigits accum = 0; \ - int accumbits = 0; /* # of bits in accum */ \ - Py_ssize_t i; \ - for (i = 0; i < size_a; ++i) { \ - accum |= (twodigits)a->ob_digit[i] << accumbits; \ - accumbits += PyLong_SHIFT; \ - assert(accumbits >= bits); \ - do { \ - char cdigit; \ - cdigit = (char)(accum & (base - 1)); \ - cdigit += (cdigit < 10) ? '0' : 'a'-10; \ - *--p = cdigit; \ - accumbits -= bits; \ - accum >>= bits; \ - } while (i < size_a-1 ? accumbits >= bits : accum > 0); \ - } \ - } \ - \ - if (alternate) { \ - if (base == 16) \ - *--p = 'x'; \ - else if (base == 8) \ - *--p = 'o'; \ - else /* (base == 2) */ \ - *--p = 'b'; \ - *--p = '0'; \ - } \ - if (negative) \ - *--p = '-'; \ - if (writer) \ - assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \ - else \ - assert(p == (TYPE*)PyUnicode_DATA(v)); \ - } while (0) - - if (kind == PyUnicode_1BYTE_KIND) { - Py_UCS1 *p; - WRITE_DIGITS(Py_UCS1); - } - else if (kind == PyUnicode_2BYTE_KIND) { - Py_UCS2 *p; - WRITE_DIGITS(Py_UCS2); - } - else { - Py_UCS4 *p; - assert (kind == PyUnicode_4BYTE_KIND); - WRITE_DIGITS(Py_UCS4); - } -#undef WRITE_DIGITS - - if (writer) { - writer->pos += sz; - } - else { - assert(_PyUnicode_CheckConsistency(v, 1)); - *p_output = v; - } - return 0; -} - -PyObject * -_PyLong_Format(PyObject *obj, int base) -{ - PyObject *str; - int err; - if (base == 10) - err = long_to_decimal_string_internal(obj, &str, NULL); - else - err = long_format_binary(obj, base, 1, &str, NULL); - if (err == -1) - return NULL; - return str; -} - -int -_PyLong_FormatWriter(_PyUnicodeWriter *writer, - PyObject *obj, - int base, int alternate) -{ - if (base == 10) - return long_to_decimal_string_internal(obj, NULL, writer); - else - return long_format_binary(obj, base, alternate, NULL, writer); + /* JRH: special case for power-of-2 bases */ + twodigits accum = 0; + int accumbits = 0; /* # of bits in accum */ + Py_ssize_t i; + for (i = 0; i < size_a; ++i) { + accum |= (twodigits)a->ob_digit[i] << accumbits; + accumbits += PyLong_SHIFT; + assert(accumbits >= bits); + do { + char cdigit; + cdigit = (char)(accum & (base - 1)); + cdigit += (cdigit < 10) ? '0' : 'a'-10; + *--p = cdigit; + accumbits -= bits; + accum >>= bits; + } while (i < size_a-1 ? accumbits >= bits : accum > 0); + } + } + + if (base == 16) + *--p = 'x'; + else if (base == 8) + *--p = 'o'; + else /* (base == 2) */ + *--p = 'b'; + *--p = '0'; + if (negative) + *--p = '-'; + assert(p == PyUnicode_1BYTE_DATA(v)); + assert(_PyUnicode_CheckConsistency(v, 1)); + return v; } /* Table of digit values for 8-bit string -> integer conversion. @@ -4352,22 +4232,11 @@ long__format__(PyObject *self, PyObject *args) { PyObject *format_spec; - _PyUnicodeWriter writer; - int ret; if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) return NULL; - - _PyUnicodeWriter_Init(&writer, 0); - ret = _PyLong_FormatAdvancedWriter( - &writer, - self, - format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); - if (ret == -1) { - _PyUnicodeWriter_Dealloc(&writer); - return NULL; - } - return _PyUnicodeWriter_Finish(&writer); + return _PyLong_FormatAdvanced(self, format_spec, 0, + PyUnicode_GET_LENGTH(format_spec)); } /* Return a pair (q, r) such that a = b * q + r, and diff -r 2059910e7d76 -r ac776ef41428 Objects/memoryobject.c --- a/Objects/memoryobject.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Objects/memoryobject.c Mon May 21 23:01:17 2012 -0400 @@ -595,7 +595,6 @@ mv->view.shape = mv->ob_array; mv->view.strides = mv->ob_array + ndim; mv->view.suboffsets = mv->ob_array + 2 * ndim; - mv->weakreflist = NULL; _PyObject_GC_TRACK(mv); return mv; @@ -970,8 +969,6 @@ _PyObject_GC_UNTRACK(self); (void)_memory_release(self); Py_CLEAR(self->mbuf); - if (self->weakreflist != NULL) - PyObject_ClearWeakRefs((PyObject *) self); PyObject_GC_Del(self); } @@ -2611,7 +2608,7 @@ (traverseproc)memory_traverse, /* tp_traverse */ (inquiry)memory_clear, /* tp_clear */ memory_richcompare, /* tp_richcompare */ - offsetof(PyMemoryViewObject, weakreflist),/* tp_weaklistoffset */ + 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ memory_methods, /* tp_methods */ diff -r 2059910e7d76 -r ac776ef41428 Objects/moduleobject.c --- a/Objects/moduleobject.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Objects/moduleobject.c Mon May 21 23:01:17 2012 -0400 @@ -366,28 +366,8 @@ static PyObject * module_repr(PyModuleObject *m) { - PyObject *name, *filename, *repr, *loader = NULL; + PyObject *name, *filename, *repr; - /* See if the module has an __loader__. If it does, give the loader the - * first shot at producing a repr for the module. - */ - if (m->md_dict != NULL) { - loader = PyDict_GetItemString(m->md_dict, "__loader__"); - } - if (loader != NULL) { - repr = PyObject_CallMethod(loader, "module_repr", "(O)", - (PyObject *)m, NULL); - if (repr == NULL) { - PyErr_Clear(); - } - else { - return repr; - } - } - /* __loader__.module_repr(m) did not provide us with a repr. Next, see if - * the module has an __file__. If it doesn't then use repr(__loader__) if - * it exists, otherwise, just use module.__name__. - */ name = PyModule_GetNameObject((PyObject *)m); if (name == NULL) { PyErr_Clear(); @@ -398,17 +378,8 @@ filename = PyModule_GetFilenameObject((PyObject *)m); if (filename == NULL) { PyErr_Clear(); - /* There's no m.__file__, so if there was an __loader__, use that in - * the repr, otherwise, the only thing you can use is m.__name__ - */ - if (loader == NULL) { - repr = PyUnicode_FromFormat("", name); - } - else { - repr = PyUnicode_FromFormat("", name, loader); - } + repr = PyUnicode_FromFormat("", name); } - /* Finally, use m.__file__ */ else { repr = PyUnicode_FromFormat("", name, filename); Py_DECREF(filename); diff -r 2059910e7d76 -r ac776ef41428 Objects/namespaceobject.c --- a/Objects/namespaceobject.c Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,225 +0,0 @@ -/* namespace object implementation */ - -#include "Python.h" -#include "structmember.h" - - -typedef struct { - PyObject_HEAD - PyObject *ns_dict; -} _PyNamespaceObject; - - -static PyMemberDef namespace_members[] = { - {"__dict__", T_OBJECT, offsetof(_PyNamespaceObject, ns_dict), READONLY}, - {NULL} -}; - - -/* Methods */ - -static PyObject * -namespace_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - _PyNamespaceObject *ns; - ns = PyObject_GC_New(_PyNamespaceObject, &_PyNamespace_Type); - if (ns == NULL) - return NULL; - - ns->ns_dict = PyDict_New(); - if (ns->ns_dict == NULL) { - Py_DECREF(ns); - return NULL; - } - - PyObject_GC_Track(ns); - return (PyObject *)ns; -} - - -static int -namespace_init(_PyNamespaceObject *ns, PyObject *args, PyObject *kwds) -{ - /* ignore args if it's NULL or empty */ - if (args != NULL) { - Py_ssize_t argcount = PyObject_Size(args); - if (argcount < 0) - return argcount; - else if (argcount > 0) { - PyErr_Format(PyExc_TypeError, "no positional arguments expected"); - return -1; - } - } - if (kwds == NULL) - return 0; - return PyDict_Update(ns->ns_dict, kwds); -} - - -static void -namespace_dealloc(_PyNamespaceObject *ns) -{ - PyObject_GC_UnTrack(ns); - Py_CLEAR(ns->ns_dict); - Py_TYPE(ns)->tp_free((PyObject *)ns); -} - - -static PyObject * -namespace_repr(_PyNamespaceObject *ns) -{ - int i, loop_error = 0; - PyObject *pairs = NULL, *d = NULL, *keys = NULL, *keys_iter = NULL; - PyObject *key; - PyObject *separator, *pairsrepr, *repr = NULL; - - i = Py_ReprEnter((PyObject *)ns); - if (i != 0) { - return i > 0 ? PyUnicode_FromString("namespace(...)") : NULL; - } - - pairs = PyList_New(0); - if (pairs == NULL) - goto error; - - d = ((_PyNamespaceObject *)ns)->ns_dict; - assert(d != NULL); - Py_INCREF(d); - - keys = PyDict_Keys(d); - if (keys == NULL) - goto error; - if (PyList_Sort(keys) != 0) - goto error; - - keys_iter = PyObject_GetIter(keys); - if (keys_iter == NULL) - goto error; - - while ((key = PyIter_Next(keys_iter)) != NULL) { - if (PyUnicode_Check(key) && PyUnicode_GET_SIZE(key) > 0) { - PyObject *value, *item; - - value = PyDict_GetItem(d, key); - assert(value != NULL); - - item = PyUnicode_FromFormat("%S=%R", key, value); - if (item == NULL) { - loop_error = 1; - } - else { - loop_error = PyList_Append(pairs, item); - Py_DECREF(item); - } - } - - Py_DECREF(key); - if (loop_error) - goto error; - } - - separator = PyUnicode_FromString(", "); - if (separator == NULL) - goto error; - - pairsrepr = PyUnicode_Join(separator, pairs); - Py_DECREF(separator); - if (pairsrepr == NULL) - goto error; - - repr = PyUnicode_FromFormat("%s(%S)", - ((PyObject *)ns)->ob_type->tp_name, pairsrepr); - Py_DECREF(pairsrepr); - -error: - Py_XDECREF(pairs); - Py_XDECREF(d); - Py_XDECREF(keys); - Py_XDECREF(keys_iter); - Py_ReprLeave((PyObject *)ns); - - return repr; -} - - -static int -namespace_traverse(_PyNamespaceObject *ns, visitproc visit, void *arg) -{ - Py_VISIT(ns->ns_dict); - return 0; -} - - -static int -namespace_clear(_PyNamespaceObject *ns) -{ - Py_CLEAR(ns->ns_dict); - return 0; -} - - -PyDoc_STRVAR(namespace_doc, -"A simple attribute-based namespace.\n\ -\n\ -namespace(**kwargs)"); - -PyTypeObject _PyNamespace_Type = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) - "namespace", /* tp_name */ - sizeof(_PyNamespaceObject), /* tp_size */ - 0, /* tp_itemsize */ - (destructor)namespace_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_reserved */ - (reprfunc)namespace_repr, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - PyObject_GenericGetAttr, /* tp_getattro */ - PyObject_GenericSetAttr, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | - Py_TPFLAGS_BASETYPE, /* tp_flags */ - namespace_doc, /* tp_doc */ - (traverseproc)namespace_traverse, /* tp_traverse */ - (inquiry)namespace_clear, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - 0, /* tp_methods */ - namespace_members, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - offsetof(_PyNamespaceObject, ns_dict), /* tp_dictoffset */ - (initproc)namespace_init, /* tp_init */ - PyType_GenericAlloc, /* tp_alloc */ - (newfunc)namespace_new, /* tp_new */ - PyObject_GC_Del, /* tp_free */ -}; - - -PyObject * -_PyNamespace_New(PyObject *kwds) -{ - PyObject *ns = namespace_new(&_PyNamespace_Type, NULL, NULL); - if (ns == NULL) - return NULL; - - if (kwds == NULL) - return ns; - if (PyDict_Update(((_PyNamespaceObject *)ns)->ns_dict, kwds) != 0) { - Py_DECREF(ns); - return NULL; - } - - return (PyObject *)ns; -} diff -r 2059910e7d76 -r ac776ef41428 Objects/object.c --- a/Objects/object.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Objects/object.c Mon May 21 23:01:17 2012 -0400 @@ -1707,9 +1707,6 @@ if (PyType_Ready(&PyZip_Type) < 0) Py_FatalError("Can't initialize zip type"); - - if (PyType_Ready(&_PyNamespace_Type) < 0) - Py_FatalError("Can't initialize namespace type"); } diff -r 2059910e7d76 -r ac776ef41428 Objects/stringlib/asciilib.h --- a/Objects/stringlib/asciilib.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Objects/stringlib/asciilib.h Mon May 21 23:01:17 2012 -0400 @@ -18,7 +18,7 @@ #define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL #define STRINGLIB_STR PyUnicode_1BYTE_DATA #define STRINGLIB_LEN PyUnicode_GET_LENGTH -#define STRINGLIB_NEW(STR,LEN) _PyUnicode_FromASCII((char*)(STR),(LEN)) +#define STRINGLIB_NEW unicode_fromascii #define STRINGLIB_RESIZE not_supported #define STRINGLIB_CHECK PyUnicode_Check #define STRINGLIB_CHECK_EXACT PyUnicode_CheckExact diff -r 2059910e7d76 -r ac776ef41428 Objects/stringlib/unicode_format.h --- a/Objects/stringlib/unicode_format.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Objects/stringlib/unicode_format.h Mon May 21 23:01:17 2012 -0400 @@ -499,26 +499,26 @@ int ok = 0; PyObject *result = NULL; PyObject *format_spec_object = NULL; - int (*formatter) (_PyUnicodeWriter*, PyObject *, PyObject *, Py_ssize_t, Py_ssize_t) = NULL; - int err; + PyObject *(*formatter)(PyObject *, PyObject *, Py_ssize_t, Py_ssize_t) = NULL; + Py_ssize_t len; /* If we know the type exactly, skip the lookup of __format__ and just call the formatter directly. */ if (PyUnicode_CheckExact(fieldobj)) - formatter = _PyUnicode_FormatAdvancedWriter; + formatter = _PyUnicode_FormatAdvanced; else if (PyLong_CheckExact(fieldobj)) - formatter = _PyLong_FormatAdvancedWriter; + formatter =_PyLong_FormatAdvanced; else if (PyFloat_CheckExact(fieldobj)) - formatter = _PyFloat_FormatAdvancedWriter; - else if (PyComplex_CheckExact(fieldobj)) - formatter = _PyComplex_FormatAdvancedWriter; + formatter = _PyFloat_FormatAdvanced; + + /* XXX: for 2.6, convert format_spec to the appropriate type + (unicode, str) */ if (formatter) { /* we know exactly which formatter will be called when __format__ is looked up, so call it directly, instead. */ - err = formatter(writer, fieldobj, format_spec->str, - format_spec->start, format_spec->end); - return (err == 0); + result = formatter(fieldobj, format_spec->str, + format_spec->start, format_spec->end); } else { /* We need to create an object out of the pointers we have, because @@ -536,11 +536,17 @@ } if (result == NULL) goto done; + if (PyUnicode_READY(result) == -1) + goto done; - if (_PyUnicodeWriter_WriteStr(writer, result) == -1) + len = PyUnicode_GET_LENGTH(result); + if (_PyUnicodeWriter_Prepare(writer, + len, PyUnicode_MAX_CHAR_VALUE(result)) == -1) goto done; + copy_characters(writer->buffer, writer->pos, + result, 0, len); + writer->pos += len; ok = 1; - done: Py_XDECREF(format_spec_object); Py_XDECREF(result); @@ -891,19 +897,16 @@ err = _PyUnicodeWriter_Prepare(writer, sublen, maxchar); if (err == -1) return 0; - _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, - literal.str, literal.start, sublen); + copy_characters(writer->buffer, writer->pos, + literal.str, literal.start, sublen); writer->pos += sublen; } - if (field_present) { - if (iter.str.start == iter.str.end) - writer->overallocate = 0; + if (field_present) if (!output_markup(&field_name, &format_spec, format_spec_needs_expanding, conversion, writer, args, kwargs, recursion_depth, auto_number)) return 0; - } } return result; } @@ -918,7 +921,7 @@ int recursion_depth, AutoNumber *auto_number) { _PyUnicodeWriter writer; - Py_ssize_t minlen; + Py_ssize_t initlen; /* check the recursion level */ if (recursion_depth <= 0) { @@ -927,8 +930,9 @@ return NULL; } - minlen = PyUnicode_GET_LENGTH(input->str) + 100; - _PyUnicodeWriter_Init(&writer, minlen); + initlen = PyUnicode_GET_LENGTH(input->str) + 100; + if (_PyUnicodeWriter_Init(&writer, initlen, 127) == -1) + return NULL; if (!do_markup(input, args, kwargs, &writer, recursion_depth, auto_number)) { diff -r 2059910e7d76 -r ac776ef41428 Objects/typeobject.c --- a/Objects/typeobject.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Objects/typeobject.c Mon May 21 23:01:17 2012 -0400 @@ -6436,7 +6436,7 @@ PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); assert(PyUnicode_Check(name)); if (!PyUnicode_CompareWithASCIIString(name, - "__class__")) { + "@__class__")) { Py_ssize_t index = co->co_nlocals + PyTuple_GET_SIZE(co->co_cellvars) + i; PyObject *cell = f->f_localsplus[index]; diff -r 2059910e7d76 -r ac776ef41428 Objects/unicodeobject.c --- a/Objects/unicodeobject.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Objects/unicodeobject.c Mon May 21 23:01:17 2012 -0400 @@ -225,10 +225,16 @@ /* forward */ static PyUnicodeObject *_PyUnicode_New(Py_ssize_t length); static PyObject* get_latin1_char(unsigned char ch); +static void copy_characters( + PyObject *to, Py_ssize_t to_start, + PyObject *from, Py_ssize_t from_start, + Py_ssize_t how_many); static int unicode_modifiable(PyObject *unicode); static PyObject * +unicode_fromascii(const unsigned char *s, Py_ssize_t size); +static PyObject * _PyUnicode_FromUCS1(const unsigned char *s, Py_ssize_t size); static PyObject * _PyUnicode_FromUCS2(const Py_UCS2 *s, Py_ssize_t size); @@ -777,7 +783,7 @@ return NULL; copy_length = Py_MIN(length, PyUnicode_GET_LENGTH(unicode)); - _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, copy_length); + copy_characters(copy, 0, unicode, 0, copy_length); return copy; } else { @@ -1148,16 +1154,15 @@ assert(0 <= from_start); assert(0 <= to_start); assert(PyUnicode_Check(from)); + assert(PyUnicode_Check(to)); assert(PyUnicode_IS_READY(from)); + assert(PyUnicode_IS_READY(to)); assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); + assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); if (how_many == 0) return 0; - assert(PyUnicode_Check(to)); - assert(PyUnicode_IS_READY(to)); - assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); - from_kind = PyUnicode_KIND(from); from_data = PyUnicode_DATA(from); to_kind = PyUnicode_KIND(to); @@ -1262,10 +1267,10 @@ return 0; } -void -_PyUnicode_FastCopyCharacters( - PyObject *to, Py_ssize_t to_start, - PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many) +static void +copy_characters(PyObject *to, Py_ssize_t to_start, + PyObject *from, Py_ssize_t from_start, + Py_ssize_t how_many) { (void)_copy_characters(to, to_start, from, from_start, how_many, 0); } @@ -1287,14 +1292,6 @@ if (PyUnicode_READY(to) == -1) return -1; - if (from_start < 0) { - PyErr_SetString(PyExc_IndexError, "string index out of range"); - return -1; - } - if (to_start < 0) { - PyErr_SetString(PyExc_IndexError, "string index out of range"); - return -1; - } how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many); if (to_start + how_many > PyUnicode_GET_LENGTH(to)) { PyErr_Format(PyExc_SystemError, @@ -1644,7 +1641,7 @@ maxchar); if (result == NULL) return -1; - _PyUnicode_FastCopyCharacters(result, 0, *p_unicode, 0, length); + PyUnicode_CopyCharacters(result, 0, *p_unicode, 0, length); Py_DECREF(*p_unicode); *p_unicode = result; return 0; @@ -1844,10 +1841,9 @@ /* Internal function, doesn't check maximum character */ -PyObject* -_PyUnicode_FromASCII(const char *buffer, Py_ssize_t size) -{ - const unsigned char *s = (const unsigned char *)buffer; +static PyObject* +unicode_fromascii(const unsigned char* s, Py_ssize_t size) +{ PyObject *unicode; if (size == 1) { #ifdef Py_DEBUG @@ -2089,7 +2085,7 @@ return; } copy = PyUnicode_New(len, max_char); - _PyUnicode_FastCopyCharacters(copy, 0, unicode, 0, len); + copy_characters(copy, 0, unicode, 0, len); Py_DECREF(unicode); *p_unicode = copy; } @@ -2757,7 +2753,7 @@ (void) va_arg(vargs, char *); size = PyUnicode_GET_LENGTH(*callresult); assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); - _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); + copy_characters(string, i, *callresult, 0, size); i += size; /* We're done with the unicode()/repr() => forget it */ Py_DECREF(*callresult); @@ -2771,7 +2767,7 @@ Py_ssize_t size; assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); size = PyUnicode_GET_LENGTH(obj); - _PyUnicode_FastCopyCharacters(string, i, obj, 0, size); + copy_characters(string, i, obj, 0, size); i += size; break; } @@ -2783,13 +2779,13 @@ if (obj) { size = PyUnicode_GET_LENGTH(obj); assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); - _PyUnicode_FastCopyCharacters(string, i, obj, 0, size); + copy_characters(string, i, obj, 0, size); i += size; } else { size = PyUnicode_GET_LENGTH(*callresult); assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); - _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); + copy_characters(string, i, *callresult, 0, size); i += size; Py_DECREF(*callresult); } @@ -2804,7 +2800,7 @@ /* unused, since we already have the result */ (void) va_arg(vargs, PyObject *); assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); - _PyUnicode_FastCopyCharacters(string, i, *callresult, 0, size); + copy_characters(string, i, *callresult, 0, size); i += size; /* We're done with the unicode()/repr() => forget it */ Py_DECREF(*callresult); @@ -4175,7 +4171,7 @@ if (unicode_widen(output, *outpos, PyUnicode_MAX_CHAR_VALUE(repunicode)) < 0) goto onError; - _PyUnicode_FastCopyCharacters(*output, *outpos, repunicode, 0, replen); + copy_characters(*output, *outpos, repunicode, 0, replen); *outpos += replen; } else { @@ -8343,15 +8339,13 @@ Py_ssize_t requiredsize) { Py_ssize_t oldsize = *psize; - Py_UCS4 *new_outobj; if (requiredsize > oldsize) { /* exponentially overallocate to minimize reallocations */ if (requiredsize < 2 * oldsize) requiredsize = 2 * oldsize; - new_outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); - if (new_outobj == 0) + *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4)); + if (*outobj == 0) return -1; - *outobj = new_outobj; *psize = requiredsize; } return 0; @@ -9222,14 +9216,12 @@ /* If the maxchar increased so that the kind changed, not all characters are representable anymore and we need to fix the string again. This only happens in very few cases. */ - _PyUnicode_FastCopyCharacters(v, 0, - self, 0, PyUnicode_GET_LENGTH(self)); + copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); maxchar_old = fixfct(v); assert(maxchar_old > 0 && maxchar_old <= maxchar_new); } else { - _PyUnicode_FastCopyCharacters(v, 0, - u, 0, PyUnicode_GET_LENGTH(self)); + copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); } Py_DECREF(u); assert(_PyUnicode_CheckConsistency(v, 1)); @@ -9611,7 +9603,7 @@ res_data += kind * seplen; } else { - _PyUnicode_FastCopyCharacters(res, res_offset, sep, 0, seplen); + copy_characters(res, res_offset, sep, 0, seplen); res_offset += seplen; } } @@ -9624,7 +9616,7 @@ res_data += kind * itemlen; } else { - _PyUnicode_FastCopyCharacters(res, res_offset, item, 0, itemlen); + copy_characters(res, res_offset, item, 0, itemlen); res_offset += itemlen; } } @@ -9671,25 +9663,13 @@ } \ } while (0) -void -_PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, - Py_UCS4 fill_char) -{ - const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); - const void *data = PyUnicode_DATA(unicode); - assert(PyUnicode_IS_READY(unicode)); - assert(unicode_modifiable(unicode)); - assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode)); - assert(start >= 0); - assert(start + length <= PyUnicode_GET_LENGTH(unicode)); - FILL(kind, data, fill_char, start, length); -} - Py_ssize_t PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, Py_UCS4 fill_char) { Py_ssize_t maxlen; + enum PyUnicode_Kind kind; + void *data; if (!PyUnicode_Check(unicode)) { PyErr_BadInternalCall(); @@ -9700,10 +9680,6 @@ if (unicode_check_modifiable(unicode)) return -1; - if (start < 0) { - PyErr_SetString(PyExc_IndexError, "string index out of range"); - return -1; - } if (fill_char > PyUnicode_MAX_CHAR_VALUE(unicode)) { PyErr_SetString(PyExc_ValueError, "fill character is bigger than " @@ -9716,7 +9692,9 @@ if (length <= 0) return 0; - _PyUnicode_FastFill(unicode, start, length, fill_char); + kind = PyUnicode_KIND(unicode); + data = PyUnicode_DATA(unicode); + FILL(kind, data, fill_char, start, length); return length; } @@ -9756,7 +9734,7 @@ FILL(kind, data, fill, 0, left); if (right) FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); - _PyUnicode_FastCopyCharacters(u, left, self, 0, _PyUnicode_LENGTH(self)); + copy_characters(u, left, self, 0, _PyUnicode_LENGTH(self)); assert(_PyUnicode_CheckConsistency(u, 1)); return u; } @@ -10080,7 +10058,7 @@ u = PyUnicode_New(slen, maxchar); if (!u) goto error; - _PyUnicode_FastCopyCharacters(u, 0, self, 0, slen); + copy_characters(u, 0, self, 0, slen); rkind = PyUnicode_KIND(u); PyUnicode_WRITE(rkind, PyUnicode_DATA(u), pos, u2); @@ -10648,8 +10626,8 @@ w = PyUnicode_New(new_len, maxchar); if (w == NULL) goto onError; - _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len); - _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len); + copy_characters(w, 0, u, 0, u_len); + copy_characters(w, u_len, v, 0, v_len); Py_DECREF(u); Py_DECREF(v); assert(_PyUnicode_CheckConsistency(w, 1)); @@ -10724,7 +10702,7 @@ goto error; } /* copy 'right' into the newly allocated area of 'left' */ - _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len); + copy_characters(*p_left, left_len, right, 0, right_len); } else { maxchar = PyUnicode_MAX_CHAR_VALUE(left); @@ -10735,8 +10713,8 @@ res = PyUnicode_New(new_len, maxchar); if (res == NULL) goto error; - _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len); - _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len); + copy_characters(res, 0, left, 0, left_len); + copy_characters(res, left_len, right, 0, right_len); Py_DECREF(left); *p_left = res; } @@ -11672,7 +11650,7 @@ length = end - start; if (PyUnicode_IS_ASCII(self)) { data = PyUnicode_1BYTE_DATA(self); - return _PyUnicode_FromASCII((char*)(data + start), length); + return unicode_fromascii(data + start, length); } else { kind = PyUnicode_KIND(self); @@ -12038,23 +12016,16 @@ (categories Z* and C* except ASCII space) */ if (!Py_UNICODE_ISPRINTABLE(ch)) { - PyUnicode_WRITE(okind, odata, o++, '\\'); /* Map 8-bit characters to '\xhh' */ if (ch <= 0xff) { + PyUnicode_WRITE(okind, odata, o++, '\\'); PyUnicode_WRITE(okind, odata, o++, 'x'); PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]); PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]); } - /* Map 16-bit characters to '\uxxxx' */ - else if (ch <= 0xffff) { - PyUnicode_WRITE(okind, odata, o++, 'u'); - PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); - PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); - PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); - PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); - } /* Map 21-bit characters to '\U00xxxxxx' */ - else { + else if (ch >= 0x10000) { + PyUnicode_WRITE(okind, odata, o++, '\\'); PyUnicode_WRITE(okind, odata, o++, 'U'); PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 28) & 0xF]); PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 24) & 0xF]); @@ -12065,6 +12036,15 @@ PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); } + /* Map 16-bit characters to '\uxxxx' */ + else { + PyUnicode_WRITE(okind, odata, o++, '\\'); + PyUnicode_WRITE(okind, odata, o++, 'u'); + PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 12) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 8) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0xF]); + PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0xF]); + } } /* Copy characters as-is */ else { @@ -12789,74 +12769,60 @@ return PyBool_FromLong(result); } +typedef struct { + PyObject *buffer; + void *data; + enum PyUnicode_Kind kind; + Py_UCS4 maxchar; + Py_ssize_t pos; +} _PyUnicodeWriter ; + Py_LOCAL_INLINE(void) _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) { - writer->size = PyUnicode_GET_LENGTH(writer->buffer); writer->maxchar = PyUnicode_MAX_CHAR_VALUE(writer->buffer); writer->data = PyUnicode_DATA(writer->buffer); writer->kind = PyUnicode_KIND(writer->buffer); } -void -_PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length) -{ - memset(writer, 0, sizeof(*writer)); -#ifdef Py_DEBUG - writer->kind = 5; /* invalid kind */ -#endif - writer->min_length = Py_MAX(min_length, 100); - writer->overallocate = (min_length > 0); -} - -int -_PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer, - Py_ssize_t length, Py_UCS4 maxchar) +Py_LOCAL(int) +_PyUnicodeWriter_Init(_PyUnicodeWriter *writer, + Py_ssize_t length, Py_UCS4 maxchar) +{ + writer->pos = 0; + writer->buffer = PyUnicode_New(length, maxchar); + if (writer->buffer == NULL) + return -1; + _PyUnicodeWriter_Update(writer); + return 0; +} + +Py_LOCAL_INLINE(int) +_PyUnicodeWriter_Prepare(_PyUnicodeWriter *writer, + Py_ssize_t length, Py_UCS4 maxchar) { Py_ssize_t newlen; PyObject *newbuffer; - assert(length > 0); - if (length > PY_SSIZE_T_MAX - writer->pos) { PyErr_NoMemory(); return -1; } newlen = writer->pos + length; - if (writer->buffer == NULL) { - if (writer->overallocate) { - /* overallocate 25% to limit the number of resize */ - if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) - newlen += newlen / 4; - if (newlen < writer->min_length) - newlen = writer->min_length; - } - writer->buffer = PyUnicode_New(newlen, maxchar); - if (writer->buffer == NULL) - return -1; - _PyUnicodeWriter_Update(writer); - return 0; - } - - if (newlen > writer->size) { - if (writer->overallocate) { - /* overallocate 25% to limit the number of resize */ - if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) - newlen += newlen / 4; - if (newlen < writer->min_length) - newlen = writer->min_length; - } - - if (maxchar > writer->maxchar || writer->readonly) { + if (newlen > PyUnicode_GET_LENGTH(writer->buffer)) { + /* overallocate 25% to limit the number of resize */ + if (newlen <= (PY_SSIZE_T_MAX - newlen / 4)) + newlen += newlen / 4; + + if (maxchar > writer->maxchar) { /* resize + widen */ newbuffer = PyUnicode_New(newlen, maxchar); if (newbuffer == NULL) return -1; - _PyUnicode_FastCopyCharacters(newbuffer, 0, - writer->buffer, 0, writer->pos); + PyUnicode_CopyCharacters(newbuffer, 0, + writer->buffer, 0, writer->pos); Py_DECREF(writer->buffer); - writer->readonly = 0; } else { newbuffer = resize_compact(writer->buffer, newlen); @@ -12867,76 +12833,25 @@ _PyUnicodeWriter_Update(writer); } else if (maxchar > writer->maxchar) { - assert(!writer->readonly); - newbuffer = PyUnicode_New(writer->size, maxchar); - if (newbuffer == NULL) + if (unicode_widen(&writer->buffer, writer->pos, maxchar) < 0) return -1; - _PyUnicode_FastCopyCharacters(newbuffer, 0, - writer->buffer, 0, writer->pos); + _PyUnicodeWriter_Update(writer); + } + return 0; +} + +Py_LOCAL(PyObject *) +_PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) +{ + if (PyUnicode_Resize(&writer->buffer, writer->pos) < 0) { Py_DECREF(writer->buffer); - writer->buffer = newbuffer; - _PyUnicodeWriter_Update(writer); - } - return 0; -} - -int -_PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) -{ - Py_UCS4 maxchar; - Py_ssize_t len; - - if (PyUnicode_READY(str) == -1) - return -1; - len = PyUnicode_GET_LENGTH(str); - if (len == 0) - return 0; - maxchar = PyUnicode_MAX_CHAR_VALUE(str); - if (maxchar > writer->maxchar || len > writer->size - writer->pos) { - if (writer->buffer == NULL && !writer->overallocate) { - Py_INCREF(str); - writer->buffer = str; - _PyUnicodeWriter_Update(writer); - writer->readonly = 1; - writer->size = 0; - writer->pos += len; - return 0; - } - if (_PyUnicodeWriter_PrepareInternal(writer, len, maxchar) == -1) - return -1; - } - _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, - str, 0, len); - writer->pos += len; - return 0; -} - -PyObject * -_PyUnicodeWriter_Finish(_PyUnicodeWriter *writer) -{ - if (writer->pos == 0) { - Py_XDECREF(writer->buffer); - Py_INCREF(unicode_empty); - return unicode_empty; - } - if (writer->readonly) { - assert(PyUnicode_GET_LENGTH(writer->buffer) == writer->pos); - return writer->buffer; - } - if (PyUnicode_GET_LENGTH(writer->buffer) != writer->pos) { - PyObject *newbuffer; - newbuffer = resize_compact(writer->buffer, writer->pos); - if (newbuffer == NULL) { - Py_DECREF(writer->buffer); - return NULL; - } - writer->buffer = newbuffer; + return NULL; } assert(_PyUnicode_CheckConsistency(writer->buffer, 1)); return writer->buffer; } -void +Py_LOCAL(void) _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer) { Py_CLEAR(writer->buffer); @@ -12959,24 +12874,14 @@ static PyObject * unicode__format__(PyObject* self, PyObject* args) { - PyObject *format_spec; - _PyUnicodeWriter writer; - int ret; + PyObject *format_spec, *out; if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) return NULL; - if (PyUnicode_READY(self) == -1) - return NULL; - _PyUnicodeWriter_Init(&writer, 0); - ret = _PyUnicode_FormatAdvancedWriter(&writer, - self, format_spec, 0, - PyUnicode_GET_LENGTH(format_spec)); - if (ret == -1) { - _PyUnicodeWriter_Dealloc(&writer); - return NULL; - } - return _PyUnicodeWriter_Finish(&writer); + out = _PyUnicode_FormatAdvanced(self, format_spec, 0, + PyUnicode_GET_LENGTH(format_spec)); + return out; } PyDoc_STRVAR(p_format__doc__, @@ -13206,17 +13111,16 @@ /* Returns a new reference to a PyUnicode object, or NULL on failure. */ -static int -formatfloat(PyObject *v, int flags, int prec, int type, - PyObject **p_output, _PyUnicodeWriter *writer) +static PyObject * +formatfloat(PyObject *v, int flags, int prec, int type) { char *p; + PyObject *result; double x; - Py_ssize_t len; x = PyFloat_AsDouble(v); if (x == -1.0 && PyErr_Occurred()) - return -1; + return NULL; if (prec < 0) prec = 6; @@ -13224,20 +13128,10 @@ p = PyOS_double_to_string(x, type, prec, (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); if (p == NULL) - return -1; - len = strlen(p); - if (writer) { - if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1) - return -1; - memcpy((char*)writer->data + writer->pos * writer->kind, - p, - len); - writer->pos += len; - } - else - *p_output = _PyUnicode_FromASCII(p, len); + return NULL; + result = unicode_fromascii((unsigned char*)p, strlen(p)); PyMem_Free(p); - return 0; + return result; } /* formatlong() emulates the format codes d, u, o, x and X, and @@ -13373,7 +13267,7 @@ } if (!PyUnicode_Check(result) || len != PyUnicode_GET_LENGTH(result)) { PyObject *unicode; - unicode = _PyUnicode_FromASCII(buf, len); + unicode = unicode_fromascii((unsigned char *)buf, len); Py_DECREF(result); result = unicode; } @@ -13442,7 +13336,8 @@ fmtcnt = PyUnicode_GET_LENGTH(uformat); fmtpos = 0; - _PyUnicodeWriter_Init(&writer, fmtcnt + 100); + if (_PyUnicodeWriter_Init(&writer, fmtcnt + 100, 127) < 0) + goto onError; if (PyTuple_Check(args)) { arglen = PyTuple_Size(args); @@ -13473,8 +13368,8 @@ if (_PyUnicodeWriter_Prepare(&writer, sublen, maxchar) == -1) goto onError; - _PyUnicode_FastCopyCharacters(writer.buffer, writer.pos, - uformat, nonfmtpos, sublen); + copy_characters(writer.buffer, writer.pos, + uformat, nonfmtpos, sublen); writer.pos += sublen; } else { @@ -13635,8 +13530,6 @@ "incomplete format"); goto onError; } - if (fmtcnt == 0) - writer.overallocate = 0; if (c == '%') { if (_PyUnicodeWriter_Prepare(&writer, 1, '%') == -1) @@ -13646,6 +13539,7 @@ continue; } + v = getnextarg(args, arglen, &argidx); if (v == NULL) goto onError; @@ -13658,13 +13552,6 @@ case 's': case 'r': case 'a': - if (PyLong_CheckExact(v) && width == -1 && prec == -1) { - /* Fast path */ - if (_PyLong_FormatWriter(&writer, v, 10, flags & F_ALT) == -1) - goto onError; - goto nextarg; - } - if (PyUnicode_CheckExact(v) && c == 's') { temp = v; Py_INCREF(temp); @@ -13685,32 +13572,6 @@ case 'o': case 'x': case 'X': - if (PyLong_CheckExact(v) - && width == -1 && prec == -1 - && !(flags & (F_SIGN | F_BLANK))) - { - /* Fast path */ - switch(c) - { - case 'd': - case 'i': - case 'u': - if (_PyLong_FormatWriter(&writer, v, 10, flags & F_ALT) == -1) - goto onError; - goto nextarg; - case 'x': - if (_PyLong_FormatWriter(&writer, v, 16, flags & F_ALT) == -1) - goto onError; - goto nextarg; - case 'o': - if (_PyLong_FormatWriter(&writer, v, 8, flags & F_ALT) == -1) - goto onError; - goto nextarg; - default: - break; - } - } - isnumok = 0; if (PyNumber_Check(v)) { PyObject *iobj=NULL; @@ -13750,20 +13611,10 @@ case 'F': case 'g': case 'G': - if (width == -1 && prec == -1 - && !(flags & (F_SIGN | F_BLANK))) - { - /* Fast path */ - if (formatfloat(v, flags, prec, c, NULL, &writer) == -1) - goto onError; - goto nextarg; - } - sign = 1; if (flags & F_ZERO) fill = '0'; - if (formatfloat(v, flags, prec, c, &temp, NULL) == -1) - temp = NULL; + temp = formatfloat(v, flags, prec, c); break; case 'c': @@ -13771,14 +13622,6 @@ Py_UCS4 ch = formatchar(v); if (ch == (Py_UCS4) -1) goto onError; - if (width == -1 && prec == -1) { - /* Fast path */ - if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1) - goto onError; - PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch); - writer.pos += 1; - goto nextarg; - } temp = PyUnicode_FromOrdinal(ch); break; } @@ -13795,16 +13638,6 @@ if (temp == NULL) goto onError; assert (PyUnicode_Check(temp)); - - if (width == -1 && prec == -1 - && !(flags & (F_SIGN | F_BLANK))) - { - /* Fast path */ - if (_PyUnicodeWriter_WriteStr(&writer, temp) == -1) - goto onError; - goto nextarg; - } - if (PyUnicode_READY(temp) == -1) { Py_CLEAR(temp); goto onError; @@ -13843,15 +13676,15 @@ if (!(flags & F_LJUST)) { if (sign) { if ((width-1) > len) - bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); + bufmaxchar = Py_MAX(bufmaxchar, fill); } else { if (width > len) - bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); + bufmaxchar = Py_MAX(bufmaxchar, fill); } } maxchar = _PyUnicode_FindMaxChar(temp, 0, pindex+len); - bufmaxchar = MAX_MAXCHAR(bufmaxchar, maxchar); + bufmaxchar = Py_MAX(bufmaxchar, maxchar); buflen = width; if (sign && len == width) @@ -13904,8 +13737,8 @@ } } - _PyUnicode_FastCopyCharacters(writer.buffer, writer.pos, - temp, pindex, len); + copy_characters(writer.buffer, writer.pos, + temp, pindex, len); writer.pos += len; if (width > len) { sublen = width - len; @@ -13913,7 +13746,6 @@ writer.pos += sublen; } -nextarg: if (dict && (argidx < arglen) && c != '%') { PyErr_SetString(PyExc_TypeError, "not all arguments converted during string formatting"); diff -r 2059910e7d76 -r ac776ef41428 PC/VC6/readme.txt --- a/PC/VC6/readme.txt Sat Jun 09 17:31:59 2012 +0100 +++ b/PC/VC6/readme.txt Mon May 21 23:01:17 2012 -0400 @@ -153,9 +153,10 @@ Unpack into the "dist" directory, retaining the folder name from the archive - for example, the latest stable OpenSSL will install as - dist/openssl-1.0.1c + dist/openssl-1.0.0a - You need to use version 1.0.1c of OpenSSL. + You can (theoretically) use any version of OpenSSL you like - the + build process will automatically select the latest version. You can install the NASM assembler from http://www.nasm.us/ diff -r 2059910e7d76 -r ac776ef41428 PC/VS8.0/pyproject.vsprops --- a/PC/VS8.0/pyproject.vsprops Sat Jun 09 17:31:59 2012 +0100 +++ b/PC/VS8.0/pyproject.vsprops Mon May 21 23:01:17 2012 -0400 @@ -58,7 +58,7 @@ /> = 0) { - wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n"); - if ((tok != NULL) && !wcscmp(tok, key)) { - tok = wcstok(NULL, L" \t"); - if ((tok != NULL) && !wcscmp(tok, L"=")) { - tok = wcstok(NULL, L"\r\n"); - if (tok != NULL) { - wcsncpy(value, tok, MAXPATHLEN); - result = 1; - break; - } - } - } - } - } - } - return result; -} - static void calculate_path(void) { @@ -504,40 +457,6 @@ /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */ wcscpy(argv0_path, progpath); reduce(argv0_path); - - /* Search for an environment configuration file, first in the - executable's directory and then in the parent directory. - If found, open it for use when searching for prefixes. - */ - - { - wchar_t tmpbuffer[MAXPATHLEN+1]; - wchar_t *env_cfg = L"pyvenv.cfg"; - FILE * env_file = NULL; - - wcscpy(tmpbuffer, argv0_path); - join(tmpbuffer, env_cfg); - env_file = _Py_wfopen(tmpbuffer, L"r"); - if (env_file == NULL) { - errno = 0; - reduce(tmpbuffer); - reduce(tmpbuffer); - join(tmpbuffer, env_cfg); - env_file = _Py_wfopen(tmpbuffer, L"r"); - if (env_file == NULL) { - errno = 0; - } - } - if (env_file != NULL) { - /* Look for a 'home' variable and set argv0_path to it, if found */ - if (find_env_config_value(env_file, L"home", tmpbuffer)) { - wcscpy(argv0_path, tmpbuffer); - } - fclose(env_file); - env_file = NULL; - } - } - if (pythonhome == NULL || *pythonhome == '\0') { if (search_for_prefix(argv0_path, LANDMARK)) pythonhome = prefix; diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_bz2.vcxproj --- a/PCbuild/_bz2.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_bz2.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_ctypes.vcxproj --- a/PCbuild/_ctypes.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_ctypes.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,18 @@ AllRules.ruleset + .pyd + .pyd + .pyd + $(SolutionDir)$(PlatformName)-pgo\ + $(SolutionDir)$(PlatformName)-temp-pgi\$(ProjectName)\ + $(SolutionDir)$(PlatformName)-pgo\ + $(SolutionDir)$(PlatformName)-temp-pgi\$(ProjectName)\ + .pyd + $(OutDirPGI)\ + $(SolutionDir)$(PlatformName)-temp-pgi\$(ProjectName)\ + .pyd + .pyd @@ -166,6 +178,7 @@ 0x1D1A0000 + $(OutDir)python33_d.lib;%(AdditionalDependencies) @@ -189,6 +202,7 @@ /EXPORT:DllGetClassObject,PRIVATE /EXPORT:DllCanUnloadNow,PRIVATE %(AdditionalOptions) NotSet 0x1D1A0000 + $(OutDir)python33.lib;%(AdditionalDependencies) @@ -213,6 +227,7 @@ NotSet 0x1D1A0000 MachineX64 + $(OutDir)python33.lib;%(AdditionalDependencies) @@ -237,6 +252,7 @@ NotSet 0x1D1A0000 MachineX64 + $(SolutionDir)\$(PlatformShortName)\python33.lib;%(AdditionalDependencies) @@ -283,11 +299,6 @@ $(IntDir)win64.obj;%(Outputs) - - - {cf7ac3d1-e2df-41d2-bea6-1e2556cdea26} - - diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_ctypes_test.vcxproj --- a/PCbuild/_ctypes_test.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_ctypes_test.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd @@ -184,6 +190,7 @@ {cf7ac3d1-e2df-41d2-bea6-1e2556cdea26} + false diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_decimal.vcxproj --- a/PCbuild/_decimal.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_decimal.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -113,8 +113,6 @@ - - @@ -148,6 +146,20 @@ AllRules.ruleset + .pyd + .pyd + $(SolutionDir)\amd64\ + $(SolutionDir)$(PlatformName)-temp-$(Configuration)\$(ProjectName)\ + .pyd + $(SolutionDir)$(PlatformName)-pgo\ + $(SolutionDir)$(PlatformName)-temp-pgi\$(ProjectName)\ + $(SolutionDir)$(PlatformName)-pgo\ + $(SolutionDir)$(PlatformName)-temp-pgi\$(ProjectName)\ + .pyd + $(OutDirPGI)\ + $(SolutionDir)$(PlatformName)-temp-pgi\$(ProjectName)\ + .pyd + .pyd @@ -301,11 +313,6 @@ $(IntDir)vcdiv64.obj;%(Outputs) - - - {cf7ac3d1-e2df-41d2-bea6-1e2556cdea26} - - diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_elementtree.vcxproj --- a/PCbuild/_elementtree.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_elementtree.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_hashlib.vcxproj --- a/PCbuild/_hashlib.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_hashlib.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,11 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_lzma.vcxproj --- a/PCbuild/_lzma.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_lzma.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_msi.vcxproj --- a/PCbuild/_msi.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_msi.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_multiprocessing.vcxproj --- a/PCbuild/_multiprocessing.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_multiprocessing.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_socket.vcxproj --- a/PCbuild/_socket.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_socket.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_sqlite3.vcxproj --- a/PCbuild/_sqlite3.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_sqlite3.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_ssl.vcxproj --- a/PCbuild/_ssl.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_ssl.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,11 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_testbuffer.vcxproj --- a/PCbuild/_testbuffer.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_testbuffer.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,19 @@ AllRules.ruleset + .pyd + .pyd + .pyd + $(SolutionDir)$(PlatformName)-pgo\ + $(SolutionDir)$(PlatformName)-temp-pgi\$(ProjectName)\ + $(SolutionDir)$(PlatformName)-pgo\ + $(SolutionDir)$(PlatformName)-temp-pgi\$(ProjectName)\ + .pyd + $(OutDirPGI)\ + $(ProjectName) + $(SolutionDir)$(PlatformName)-temp-pgi\$(ProjectName)\ + .pyd + .pyd @@ -160,6 +173,7 @@ 0x1e1F0000 + $(OutDir)python33_d.lib;%(AdditionalDependencies) @@ -173,6 +187,7 @@ 0x1e1F0000 + $(OutDir)python33.lib;%(AdditionalDependencies) @@ -187,6 +202,7 @@ 0x1e1F0000 MachineX64 + $(OutDir)python33.lib;%(AdditionalDependencies) @@ -201,16 +217,12 @@ 0x1e1F0000 MachineX64 + $(SolutionDir)\$(PlatformShortName)\python33.lib;%(AdditionalDependencies) - - - {cf7ac3d1-e2df-41d2-bea6-1e2556cdea26} - - diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_testcapi.vcxproj --- a/PCbuild/_testcapi.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_testcapi.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/_tkinter.vcxproj --- a/PCbuild/_tkinter.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/_tkinter.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,11 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/build.bat --- a/PCbuild/build.bat Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/build.bat Mon May 21 23:01:17 2012 -0400 @@ -5,15 +5,14 @@ setlocal set platf=Win32 set conf=Release -set target=build -set dir=%~dp0 +set build= :CheckOpts if "%1"=="-c" (set conf=%2) & shift & shift & goto CheckOpts if "%1"=="-p" (set platf=%2) & shift & shift & goto CheckOpts -if "%1"=="-r" (set target=rebuild) & shift & goto CheckOpts +if "%1"=="-r" (set build=/rebuild) & shift & goto CheckOpts if "%1"=="-d" (set conf=Debug) & shift & goto CheckOpts -set cmd=msbuild /p:useenv=true %dir%pcbuild.sln /t:%target% /p:Configuration=%conf% /p:Platform=%platf% +set cmd=vcbuild /useenv pcbuild.sln %build% "%conf%|%platf%" echo %cmd% %cmd% diff -r 2059910e7d76 -r ac776ef41428 PCbuild/build_ssl.py --- a/PCbuild/build_ssl.py Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/build_ssl.py Mon May 21 23:01:17 2012 -0400 @@ -63,13 +63,37 @@ print(" Please install ActivePerl and ensure it appears on your path") return None -# Fetch SSL directory from VC properties -def get_ssl_dir(): - propfile = (os.path.join(os.path.dirname(__file__), 'pyproject.props')) - with open(propfile) as f: - m = re.search('openssl-([^<]+)<', f.read()) - return "..\..\openssl-"+m.group(1) - +# Locate the best SSL directory given a few roots to look into. +def find_best_ssl_dir(sources): + candidates = [] + for s in sources: + try: + # note: do not abspath s; the build will fail if any + # higher up directory name has spaces in it. + fnames = os.listdir(s) + except os.error: + fnames = [] + for fname in fnames: + fqn = os.path.join(s, fname) + if os.path.isdir(fqn) and fname.startswith("openssl-"): + candidates.append(fqn) + # Now we have all the candidates, locate the best. + best_parts = [] + best_name = None + for c in candidates: + parts = re.split("[.-]", os.path.basename(c))[1:] + # eg - openssl-0.9.7-beta1 - ignore all "beta" or any other qualifiers + if len(parts) >= 4: + continue + if parts > best_parts: + best_parts = parts + best_name = c + if best_name is not None: + print("Found an SSL directory at '%s'" % (best_name,)) + else: + print("Could not find an SSL directory in '%s'" % (sources,)) + sys.stdout.flush() + return best_name def create_makefile64(makefile, m32): """Create and fix makefile for 64bit @@ -178,7 +202,7 @@ print("No Perl installation was found. Existing Makefiles are used.") sys.stdout.flush() # Look for SSL 2 levels up from pcbuild - ie, same place zlib etc all live. - ssl_dir = get_ssl_dir() + ssl_dir = find_best_ssl_dir(("..\\..",)) if ssl_dir is None: sys.exit(1) @@ -228,9 +252,9 @@ # Now run make. if arch == "amd64": - rc = os.system("nasm -f win64 -DNEAR -Ox -g ms\\uptable.asm") + rc = os.system("ml64 -c -Foms\\uptable.obj ms\\uptable.asm") if rc: - print("nasm assembler has failed.") + print("ml64 assembler has failed.") sys.exit(rc) copy(r"crypto\buildinf_%s.h" % arch, r"crypto\buildinf.h") diff -r 2059910e7d76 -r ac776ef41428 PCbuild/build_tkinter.py --- a/PCbuild/build_tkinter.py Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/build_tkinter.py Mon May 21 23:01:17 2012 -0400 @@ -11,8 +11,8 @@ here = os.path.abspath(os.path.dirname(__file__)) par = os.path.pardir -TCL = "tcl8.5.11" -TK = "tk8.5.11" +TCL = "tcl8.5.9" +TK = "tk8.5.9" TIX = "tix-8.4.3.x" ROOT = os.path.abspath(os.path.join(here, par, par)) @@ -32,7 +32,7 @@ def build(platform, clean): if platform == "Win32": dest = os.path.join(ROOT, "tcltk") - machine = "IX86" + machine = "X86" elif platform == "AMD64": dest = os.path.join(ROOT, "tcltk64") machine = "AMD64" diff -r 2059910e7d76 -r ac776ef41428 PCbuild/debug.props --- a/PCbuild/debug.props Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/debug.props Mon May 21 23:01:17 2012 -0400 @@ -1,7 +1,6 @@  - _d $(OutDir)kill_python_d.exe @@ -12,11 +11,11 @@ _DEBUG;%(PreprocessorDefinitions) + + $(OutDir)$(TargetName)$(TargetExt) + - - $(PyDebugExt) - $(KillPythonExe) diff -r 2059910e7d76 -r ac776ef41428 PCbuild/env.bat --- a/PCbuild/env.bat Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/env.bat Mon May 21 23:01:17 2012 -0400 @@ -1,9 +1,5 @@ @echo off -set VS10=%ProgramFiles(x86)%\Microsoft Visual Studio 10.0 -IF EXIST "%VS10%" GOTO ok -set VS10=%ProgramFiles%\Microsoft Visual Studio 10.0 -:ok - +set VS9=%ProgramFiles%\Microsoft Visual Studio 9.0 echo Build environments: x86, ia64, amd64, x86_amd64, x86_ia64 echo. -call "%VS10%\VC\vcvarsall.bat" %1 +call "%VS9%\VC\vcvarsall.bat" %1 diff -r 2059910e7d76 -r ac776ef41428 PCbuild/kill_python.vcxproj --- a/PCbuild/kill_python.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/kill_python.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -82,6 +82,12 @@ AllRules.ruleset + .exe + .exe + .exe + .exe + $(SolutionDir)$(PlatformName)-pgo\ + $(SolutionDir)$(PlatformName)-temp-pgi\$(ProjectName)\ diff -r 2059910e7d76 -r ac776ef41428 PCbuild/make_buildinfo.vcxproj --- a/PCbuild/make_buildinfo.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/make_buildinfo.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -30,6 +30,7 @@ AllRules.ruleset + .exe @@ -40,6 +41,7 @@ $(OutDir)make_buildinfo.exe + $(TargetDir)$(TargetName).pdb Console diff -r 2059910e7d76 -r ac776ef41428 PCbuild/make_versioninfo.vcxproj --- a/PCbuild/make_versioninfo.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/make_versioninfo.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -54,15 +54,9 @@ - - - - - - @@ -79,6 +73,8 @@ AllRules.ruleset + .exe + .exe @@ -101,6 +97,7 @@ $(SolutionDir)make_versioninfo.exe + $(TargetDir)$(TargetName).pdb Console 0x1d000000 @@ -154,6 +151,7 @@ $(SolutionDir)make_versioninfo_d.exe + $(TargetDir)$(TargetName).pdb Console 0x1d000000 diff -r 2059910e7d76 -r ac776ef41428 PCbuild/pcbuild.sln --- a/PCbuild/pcbuild.sln Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/pcbuild.sln Mon May 21 23:01:17 2012 -0400 @@ -21,8 +21,14 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "winsound", "winsound.vcxproj", "{28B5D777-DDF2-4B6B-B34F-31D938813856}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_decimal", "_decimal.vcxproj", "{0E9791DB-593A-465F-98BC-681011311617}" + ProjectSection(ProjectDependencies) = postProject + {CF7AC3D1-E2DF-41D2-BEA6-1E2556CDEA26} = {CF7AC3D1-E2DF-41D2-BEA6-1E2556CDEA26} + EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_ctypes", "_ctypes.vcxproj", "{0E9791DB-593A-465F-98BC-681011311618}" + ProjectSection(ProjectDependencies) = postProject + {CF7AC3D1-E2DF-41D2-BEA6-1E2556CDEA26} = {CF7AC3D1-E2DF-41D2-BEA6-1E2556CDEA26} + EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_ctypes_test", "_ctypes_test.vcxproj", "{9EC7190A-249F-4180-A900-548FDCF3055F}" EndProject @@ -65,8 +71,14 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "python3dll", "python3dll.vcxproj", "{885D4898-D08D-4091-9C40-C700CFE3FC5A}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "xxlimited", "xxlimited.vcxproj", "{F749B822-B489-4CA5-A3AD-CE078F5F338A}" + ProjectSection(ProjectDependencies) = postProject + {CF7AC3D1-E2DF-41D2-BEA6-1E2556CDEA26} = {CF7AC3D1-E2DF-41D2-BEA6-1E2556CDEA26} + EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_testbuffer", "_testbuffer.vcxproj", "{A2697BD3-28C1-4AEC-9106-8B748639FD16}" + ProjectSection(ProjectDependencies) = postProject + {CF7AC3D1-E2DF-41D2-BEA6-1E2556CDEA26} = {CF7AC3D1-E2DF-41D2-BEA6-1E2556CDEA26} + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -535,8 +547,8 @@ {885D4898-D08D-4091-9C40-C700CFE3FC5A}.Release|Win32.Build.0 = Release|Win32 {885D4898-D08D-4091-9C40-C700CFE3FC5A}.Release|x64.ActiveCfg = Release|x64 {885D4898-D08D-4091-9C40-C700CFE3FC5A}.Release|x64.Build.0 = Release|x64 - {F749B822-B489-4CA5-A3AD-CE078F5F338A}.Debug|Win32.ActiveCfg = Release|Win32 - {F749B822-B489-4CA5-A3AD-CE078F5F338A}.Debug|x64.ActiveCfg = Release|x64 + {F749B822-B489-4CA5-A3AD-CE078F5F338A}.Debug|Win32.ActiveCfg = Debug|Win32 + {F749B822-B489-4CA5-A3AD-CE078F5F338A}.Debug|x64.ActiveCfg = Debug|x64 {F749B822-B489-4CA5-A3AD-CE078F5F338A}.PGInstrument|Win32.ActiveCfg = PGInstrument|Win32 {F749B822-B489-4CA5-A3AD-CE078F5F338A}.PGInstrument|Win32.Build.0 = PGInstrument|Win32 {F749B822-B489-4CA5-A3AD-CE078F5F338A}.PGInstrument|x64.ActiveCfg = PGInstrument|x64 diff -r 2059910e7d76 -r ac776ef41428 PCbuild/pyd.props --- a/PCbuild/pyd.props Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/pyd.props Mon May 21 23:01:17 2012 -0400 @@ -1,14 +1,13 @@  + - <_ProjectFileVersion>10.0.30319.1 false false - .pyd @@ -16,6 +15,9 @@ MultiThreadedDLL + $(OutDir)$(ProjectName).pyd + $(OutDir)$(ProjectName).pdb + $(OutDir)$(TargetName).lib diff -r 2059910e7d76 -r ac776ef41428 PCbuild/pyd_d.props --- a/PCbuild/pyd_d.props Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/pyd_d.props Mon May 21 23:01:17 2012 -0400 @@ -1,10 +1,12 @@  + - - + + $(SolutionDir)python_d.exe + <_ProjectFileVersion>10.0.30319.1 false @@ -21,11 +23,19 @@ Py_BUILD_CORE_MODULE;%(PreprocessorDefinitions) MultiThreadedDebugDLL - + + $(OutDir)$(ProjectName)_d.pyd + $(OutDir)$(ProjectName)_d.pdb + $(OutDir)$(TargetName).lib + - + + + $(PythonExe) + + \ No newline at end of file diff -r 2059910e7d76 -r ac776ef41428 PCbuild/pyexpat.vcxproj --- a/PCbuild/pyexpat.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/pyexpat.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/pyproject.props --- a/PCbuild/pyproject.props Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/pyproject.props Mon May 21 23:01:17 2012 -0400 @@ -1,14 +1,13 @@  - python33$(PyDebugExt) - $(SolutionDir)python$(PyDebugExt).exe - $(OutDir)kill_python$(PyDebugExt).exe + python33 + $(SolutionDir)\python.exe ..\.. $(externalsDir)\sqlite-3.7.12 $(externalsDir)\bzip2-1.0.6 $(externalsDir)\xz-5.0.3 - $(externalsDir)\openssl-1.0.1c + $(externalsDir)\openssl-1.0.0a $(externalsDir)\tcltk $(externalsDir)\tcltk64 $(tcltkDir)\lib\tcl85.lib;$(tcltkDir)\lib\tk85.lib @@ -18,9 +17,10 @@ <_ProjectFileVersion>10.0.30319.1 - $(SolutionDir) + $(SolutionDir)\ $(SolutionDir)$(PlatformName)-temp-$(Configuration)\$(ProjectName)\ false + .dll @@ -59,9 +59,6 @@ $(PythonExe) - - $(KillPythonExe) - $(externalsDir) diff -r 2059910e7d76 -r ac776ef41428 PCbuild/python.vcxproj --- a/PCbuild/python.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/python.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -156,6 +156,14 @@ AllRules.ruleset + .exe + .exe + .exe + .exe + .exe + .exe + .exe + .exe diff -r 2059910e7d76 -r ac776ef41428 PCbuild/pythoncore.vcxproj --- a/PCbuild/pythoncore.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/pythoncore.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -1,4 +1,4 @@ - + @@ -77,51 +77,51 @@ + - + - + - + - - + - + - + - + @@ -150,14 +150,15 @@ AllRules.ruleset - $(PyDllName) + $(PyDllName)_d + $(PyDllName) + $(PyDllName)_d + $(PyDllName) + .dll + $(PyDllName) + $(PyDllName) $(PyDllName) - $(PyDllName) - $(PyDllName) - $(PyDllName) $(PyDllName) - $(PyDllName) - $(PyDllName) @@ -179,7 +180,9 @@ $(IntDir)getbuildinfo.o;%(AdditionalDependencies) $(OutDir)$(PyDllName).dll libc;%(IgnoreSpecificDefaultLibraries) + $(OutDir)$(PyDllName).pdb 0x1e000000 + $(OutDir)$(PyDllName).lib @@ -203,8 +206,11 @@ $(IntDir)getbuildinfo.o;%(AdditionalDependencies) + $(OutDir)$(PyDllName).dll libc;%(IgnoreSpecificDefaultLibraries) + $(OutDir)$(PyDllName).pdb 0x1e000000 + $(OutDir)$(PyDllName).lib @@ -228,8 +234,11 @@ $(IntDir)getbuildinfo.o;%(AdditionalDependencies) + $(OutDir)$(PyDllName)_d.dll libc;%(IgnoreSpecificDefaultLibraries) + $(OutDir)$(PyDllName)_d.pdb 0x1e000000 + $(OutDir)$(PyDllName)_d.lib @@ -256,8 +265,11 @@ $(IntDir)getbuildinfo.o;%(AdditionalDependencies) + $(OutDir)$(PyDllName)_d.dll libc;%(IgnoreSpecificDefaultLibraries) + $(OutDir)$(PyDllName)_d.pdb 0x1e000000 + $(OutDir)$(PyDllName)_d.lib @@ -280,7 +292,9 @@ $(IntDir)getbuildinfo.o;%(AdditionalDependencies) $(OutDir)$(PyDllName).dll libc;%(IgnoreSpecificDefaultLibraries) + $(OutDir)$(PyDllName).pdb 0x1e000000 + $(OutDirPGI)$(PyDllName).lib @@ -304,8 +318,11 @@ $(IntDir)getbuildinfo.o;%(AdditionalDependencies) + $(OutDir)$(PyDllName).dll libc;%(IgnoreSpecificDefaultLibraries) + $(OutDir)$(PyDllName).pdb 0x1e000000 + $(OutDirPGI)$(PyDllName).lib MachineX64 @@ -329,7 +346,9 @@ $(IntDir)getbuildinfo.o;%(AdditionalDependencies) $(OutDir)$(PyDllName).dll libc;%(IgnoreSpecificDefaultLibraries) + $(OutDir)$(PyDllName).pdb 0x1e000000 + $(OutDirPGI)$(PyDllName).lib @@ -353,8 +372,11 @@ $(IntDir)getbuildinfo.o;%(AdditionalDependencies) + $(OutDir)$(PyDllName).dll libc;%(IgnoreSpecificDefaultLibraries) + $(OutDir)$(PyDllName).pdb 0x1e000000 + $(OutDirPGI)$(PyDllName).lib MachineX64 @@ -385,6 +407,7 @@ + @@ -402,7 +425,6 @@ - @@ -421,6 +443,7 @@ + @@ -441,6 +464,7 @@ + @@ -448,6 +472,7 @@ + @@ -580,7 +605,6 @@ - diff -r 2059910e7d76 -r ac776ef41428 PCbuild/pythoncore.vcxproj.filters --- a/PCbuild/pythoncore.vcxproj.filters Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/pythoncore.vcxproj.filters Mon May 21 23:01:17 2012 -0400 @@ -111,6 +111,9 @@ Include + + Include + Include @@ -216,6 +219,9 @@ Include + + Include + Include @@ -276,6 +282,9 @@ Include + + Include + Include @@ -297,6 +306,9 @@ Modules + + Modules + Modules diff -r 2059910e7d76 -r ac776ef41428 PCbuild/pythonw.vcxproj --- a/PCbuild/pythonw.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/pythonw.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -149,6 +149,14 @@ AllRules.ruleset + .exe + .exe + .exe + .pyd + .exe + .exe + .exe + .exe diff -r 2059910e7d76 -r ac776ef41428 PCbuild/readme.txt --- a/PCbuild/readme.txt Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/readme.txt Mon May 21 23:01:17 2012 -0400 @@ -1,4 +1,4 @@ -Building Python using VC++ 10.0 +Building Python using VC++ 9.0 ------------------------------ This directory is used to build Python for Win32 and x64 platforms, e.g. @@ -62,11 +62,17 @@ C RUNTIME --------- -Visual Studio 2010 uses version 10 of the C runtime (MSVCRT9). The executables -no longer use the "Side by Side" assemblies used in previous versions of the -compiler. This simplifies distribution of applications. -The run time libraries are avalible under the VC/Redist folder of your visual studio -distribution. For more info, see the Readme in the VC/Redist folder. +Visual Studio 2008 uses version 9 of the C runtime (MSVCRT9). The executables +are linked to a CRT "side by side" assembly which must be present on the target +machine. This is avalible under the VC/Redist folder of your visual studio +distribution. On XP and later operating systems that support +side-by-side assemblies it is not enough to have the msvcrt90.dll present, +it has to be there as a whole assembly, that is, a folder with the .dll +and a .manifest. Also, a check is made for the correct version. +Therefore, one should distribute this assembly with the dlls, and keep +it in the same directory. For compatibility with older systems, one should +also set the PATH to this directory so that the dll can be found. +For more info, see the Readme in the VC/Redist folder. SUBPROJECTS ----------- @@ -142,7 +148,7 @@ Get the source code through - svn export http://svn.python.org/projects/external/openssl-1.0.1c + svn export http://svn.python.org/projects/external/openssl-1.0.0a ** NOTE: if you use the Tools\buildbot\external(-amd64).bat approach for obtaining external sources then you don't need to manually get the source diff -r 2059910e7d76 -r ac776ef41428 PCbuild/release.props --- a/PCbuild/release.props Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/release.props Mon May 21 23:01:17 2012 -0400 @@ -1,7 +1,7 @@  - + $(OutDir)kill_python.exe <_ProjectFileVersion>10.0.30319.1 @@ -12,8 +12,8 @@ - - $(PyDebugExt) + + $(KillPythonExe) \ No newline at end of file diff -r 2059910e7d76 -r ac776ef41428 PCbuild/select.vcxproj --- a/PCbuild/select.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/select.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/sqlite3.vcxproj --- a/PCbuild/sqlite3.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/sqlite3.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -150,6 +150,9 @@ AllRules.ruleset + .dll + .dll + .dll diff -r 2059910e7d76 -r ac776ef41428 PCbuild/unicodedata.vcxproj --- a/PCbuild/unicodedata.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/unicodedata.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/vs9to10.py --- a/PCbuild/vs9to10.py Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -#Run this file after automatic convertsion of the VisualStudio 2008 solution by VisualStudio 2010. -#This can be done whenever the 2008 solution changes. -#It will make the necessary cleanup and updates to the vcxproj files -#the .props files need to be maintained by hand if the .vsprops files change - -from __future__ import with_statement -import sys -import os -import os.path - -def vs9to10(src, dest): - for name in os.listdir(src): - path, ext = os.path.splitext(name) - if ext.lower() not in ('.vcxproj',): - continue - - filename = os.path.normpath(os.path.join(src, name)) - destname = os.path.normpath(os.path.join(dest, name)) - print("%s -> %s" % (filename, destname)) - - lines = [] - lastline = b"" - importgroup = False - with open(filename, 'rb') as fin: - for line in fin: - #remove redundant linker output info - if b"" in line: - continue - if b"" in line: - continue - if b"" in line and b"" in line: - continue - - #add new property sheet to the pythoncore - if importgroup and "pythoncore" in name.lower(): - if b"" in line: - if b"debug.props" in lastline: - lines.append(b' \r\n') - elif b"pythoncore" not in lastline: - lines.append(b' \r\n') - if b"" in line: - importgroup = False - lines.append(line) - lastline = line - with open(destname, 'wb') as fout: - for line in lines: - fout.write(line) - -if __name__ == "__main__": - src = "." if len(sys.argv) < 2 else sys.argv[1] - name = os.path.basename(os.path.abspath(src)) - dest = os.path.abspath(os.path.join(src, "..", name + "Upd")) - os.makedirs(dest) - vs9to10(src, dest) diff -r 2059910e7d76 -r ac776ef41428 PCbuild/w9xpopen.vcxproj --- a/PCbuild/w9xpopen.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/w9xpopen.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -84,51 +84,27 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff -r 2059910e7d76 -r ac776ef41428 PCbuild/winsound.vcxproj --- a/PCbuild/winsound.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/winsound.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -148,6 +148,12 @@ AllRules.ruleset + .pyd + .pyd + .pyd + .pyd + .pyd + .pyd diff -r 2059910e7d76 -r ac776ef41428 PCbuild/x64.props --- a/PCbuild/x64.props Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/x64.props Mon May 21 23:01:17 2012 -0400 @@ -6,7 +6,7 @@ <_ProjectFileVersion>10.0.30319.1 <_PropertySheetDisplayName>amd64 - $(SolutionDir)amd64\ + $(SolutionDir)\amd64\ $(SolutionDir)$(PlatformName)-temp-$(Configuration)\$(ProjectName)\ diff -r 2059910e7d76 -r ac776ef41428 PCbuild/xxlimited.vcxproj --- a/PCbuild/xxlimited.vcxproj Sat Jun 09 17:31:59 2012 +0100 +++ b/PCbuild/xxlimited.vcxproj Mon May 21 23:01:17 2012 -0400 @@ -117,6 +117,12 @@ AllRules.ruleset + .pyd + .pyd + $(SolutionDir)$(PlatformName)-pgo\ + $(SolutionDir)$(PlatformName)-temp-pgi\$(ProjectName)\ + .pyd + .pyd @@ -126,6 +132,7 @@ wsock32.lib;%(AdditionalDependencies) libc;%(IgnoreSpecificDefaultLibraries) 0x1D110000 + $(OutDir)$(ProjectName).pyd @@ -133,7 +140,7 @@ X64 - wsock32.lib;%(AdditionalDependencies) + wsock32.lib;$(SolutionDir)\$(PlatformShortName)\python33.lib;%(AdditionalDependencies) libc;%(IgnoreSpecificDefaultLibraries) 0x1D110000 @@ -146,6 +153,7 @@ wsock32.lib;%(AdditionalDependencies) libc;%(IgnoreSpecificDefaultLibraries) 0x1D110000 + $(OutDirPGI)$(ProjectName).pyd @@ -153,10 +161,11 @@ X64 - wsock32.lib;%(AdditionalDependencies) + wsock32.lib;$(OutDir)python33.lib;%(AdditionalDependencies) libc;%(IgnoreSpecificDefaultLibraries) 0x1D110000 MachineX64 + $(OutDirPGI)$(ProjectName).pyd @@ -183,11 +192,6 @@ - - - {cf7ac3d1-e2df-41d2-bea6-1e2556cdea26} - - diff -r 2059910e7d76 -r ac776ef41428 Python/bltinmodule.c --- a/Python/bltinmodule.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Python/bltinmodule.c Mon May 21 23:01:17 2012 -0400 @@ -163,8 +163,10 @@ cls = PyEval_CallObjectWithKeywords(meta, margs, mkw); Py_DECREF(margs); } - if (cls != NULL && PyCell_Check(cell)) - PyCell_Set(cell, cls); + if (cls != NULL && PyCell_Check(cell)) { + Py_INCREF(cls); + PyCell_SET(cell, cls); + } Py_DECREF(cell); } Py_DECREF(ns); diff -r 2059910e7d76 -r ac776ef41428 Python/ceval.c --- a/Python/ceval.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Python/ceval.c Mon May 21 23:01:17 2012 -0400 @@ -3107,8 +3107,6 @@ tail = PyUnicode_FromFormat(", %U, and %U", PyList_GET_ITEM(names, len - 2), PyList_GET_ITEM(names, len - 1)); - if (tail == NULL) - return; /* Chop off the last two objects in the list. This shouldn't actually fail, but we can't be too careful. */ err = PyList_SetSlice(names, len - 2, len, NULL); diff -r 2059910e7d76 -r ac776ef41428 Python/ceval_gil.h --- a/Python/ceval_gil.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Python/ceval_gil.h Mon May 21 23:01:17 2012 -0400 @@ -313,14 +313,13 @@ static void destroy_gil(void) { - /* some pthread-like implementations tie the mutex to the cond - * and must have the cond destroyed first. - */ - COND_FINI(gil_cond); MUTEX_FINI(gil_mutex); #ifdef FORCE_SWITCHING + MUTEX_FINI(switch_mutex); +#endif + COND_FINI(gil_cond); +#ifdef FORCE_SWITCHING COND_FINI(switch_cond); - MUTEX_FINI(switch_mutex); #endif _Py_atomic_store_explicit(&gil_locked, -1, _Py_memory_order_release); _Py_ANNOTATE_RWLOCK_DESTROY(&gil_locked); diff -r 2059910e7d76 -r ac776ef41428 Python/compile.c --- a/Python/compile.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Python/compile.c Mon May 21 23:01:17 2012 -0400 @@ -1676,7 +1676,7 @@ return 0; } /* return the (empty) __class__ cell */ - str = PyUnicode_InternFromString("__class__"); + str = PyUnicode_InternFromString("@__class__"); if (str == NULL) { compiler_exit_scope(c); return 0; diff -r 2059910e7d76 -r ac776ef41428 Python/formatter_unicode.c --- a/Python/formatter_unicode.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Python/formatter_unicode.c Mon May 21 23:01:17 2012 -0400 @@ -316,28 +316,21 @@ /* Do the padding, and return a pointer to where the caller-supplied content goes. */ static Py_ssize_t -fill_padding(_PyUnicodeWriter *writer, - Py_ssize_t nchars, +fill_padding(PyObject *s, Py_ssize_t start, Py_ssize_t nchars, Py_UCS4 fill_char, Py_ssize_t n_lpadding, Py_ssize_t n_rpadding) { - Py_ssize_t pos; - /* Pad on left. */ - if (n_lpadding) { - pos = writer->pos; - _PyUnicode_FastFill(writer->buffer, pos, n_lpadding, fill_char); - } + if (n_lpadding) + PyUnicode_Fill(s, start, start + n_lpadding, fill_char); /* Pad on right. */ - if (n_rpadding) { - pos = writer->pos + nchars + n_lpadding; - _PyUnicode_FastFill(writer->buffer, pos, n_rpadding, fill_char); - } + if (n_rpadding) + PyUnicode_Fill(s, start + nchars + n_lpadding, + start + nchars + n_lpadding + n_rpadding, fill_char); /* Pointer to the user content. */ - writer->pos += n_lpadding; - return 0; + return start + n_lpadding; } /************************************************************************/ @@ -548,7 +541,7 @@ as determined in calc_number_widths(). Return -1 on error, or 0 on success. */ static int -fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec, +fill_number(PyObject *out, Py_ssize_t pos, const NumberFieldWidths *spec, PyObject *digits, Py_ssize_t d_start, Py_ssize_t d_end, PyObject *prefix, Py_ssize_t p_start, Py_UCS4 fill_char, @@ -556,38 +549,36 @@ { /* Used to keep track of digits, decimal, and remainder. */ Py_ssize_t d_pos = d_start; - const enum PyUnicode_Kind kind = writer->kind; - const void *data = writer->data; + unsigned int kind = PyUnicode_KIND(out); + void *data = PyUnicode_DATA(out); Py_ssize_t r; if (spec->n_lpadding) { - _PyUnicode_FastFill(writer->buffer, - writer->pos, spec->n_lpadding, fill_char); - writer->pos += spec->n_lpadding; + PyUnicode_Fill(out, pos, pos + spec->n_lpadding, fill_char); + pos += spec->n_lpadding; } if (spec->n_sign == 1) { - PyUnicode_WRITE(kind, data, writer->pos, spec->sign); - writer->pos++; + PyUnicode_WRITE(kind, data, pos++, spec->sign); } if (spec->n_prefix) { - _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, - prefix, p_start, - spec->n_prefix); + if (PyUnicode_CopyCharacters(out, pos, + prefix, p_start, + spec->n_prefix) < 0) + return -1; if (toupper) { Py_ssize_t t; for (t = 0; t < spec->n_prefix; t++) { - Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t); + Py_UCS4 c = PyUnicode_READ(kind, data, pos + t); c = Py_TOUPPER(c); assert (c <= 127); - PyUnicode_WRITE(kind, data, writer->pos + t, c); + PyUnicode_WRITE(kind, data, pos + t, c); } } - writer->pos += spec->n_prefix; + pos += spec->n_prefix; } if (spec->n_spadding) { - _PyUnicode_FastFill(writer->buffer, - writer->pos, spec->n_spadding, fill_char); - writer->pos += spec->n_spadding; + PyUnicode_Fill(out, pos, pos + spec->n_spadding, fill_char); + pos += spec->n_spadding; } /* Only for type 'c' special case, it has no digits. */ @@ -603,7 +594,7 @@ return -1; } r = _PyUnicode_InsertThousandsGrouping( - writer->buffer, writer->pos, + out, pos, spec->n_grouped_digits, pdigits + kind * d_pos, spec->n_digits, spec->n_min_width, @@ -618,38 +609,34 @@ if (toupper) { Py_ssize_t t; for (t = 0; t < spec->n_grouped_digits; t++) { - Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t); + Py_UCS4 c = PyUnicode_READ(kind, data, pos + t); c = Py_TOUPPER(c); if (c > 127) { PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit"); return -1; } - PyUnicode_WRITE(kind, data, writer->pos + t, c); + PyUnicode_WRITE(kind, data, pos + t, c); } } - writer->pos += spec->n_grouped_digits; + pos += spec->n_grouped_digits; if (spec->n_decimal) { - _PyUnicode_FastCopyCharacters( - writer->buffer, writer->pos, - locale->decimal_point, 0, spec->n_decimal); - writer->pos += spec->n_decimal; + if (PyUnicode_CopyCharacters(out, pos, locale->decimal_point, 0, spec->n_decimal) < 0) + return -1; + pos += spec->n_decimal; d_pos += 1; } if (spec->n_remainder) { - _PyUnicode_FastCopyCharacters( - writer->buffer, writer->pos, - digits, d_pos, spec->n_remainder); - writer->pos += spec->n_remainder; + if (PyUnicode_CopyCharacters(out, pos, digits, d_pos, spec->n_remainder) < 0) + return -1; + pos += spec->n_remainder; d_pos += spec->n_remainder; } if (spec->n_rpadding) { - _PyUnicode_FastFill(writer->buffer, - writer->pos, spec->n_rpadding, - fill_char); - writer->pos += spec->n_rpadding; + PyUnicode_Fill(out, pos, pos + spec->n_rpadding, fill_char); + pos += spec->n_rpadding; } return 0; } @@ -720,20 +707,17 @@ /*********** string formatting ******************************************/ /************************************************************************/ -static int -format_string_internal(PyObject *value, const InternalFormatSpec *format, - _PyUnicodeWriter *writer) +static PyObject * +format_string_internal(PyObject *value, const InternalFormatSpec *format) { Py_ssize_t lpad; Py_ssize_t rpad; Py_ssize_t total; - Py_ssize_t len; - int result = -1; + Py_ssize_t pos; + Py_ssize_t len = PyUnicode_GET_LENGTH(value); + PyObject *result = NULL; Py_UCS4 maxchar; - assert(PyUnicode_IS_READY(value)); - len = PyUnicode_GET_LENGTH(value); - /* sign is not allowed on strings */ if (format->sign != '\0') { PyErr_SetString(PyExc_ValueError, @@ -757,11 +741,6 @@ goto done; } - if (format->width == -1 && format->precision == -1) { - /* Fast path */ - return _PyUnicodeWriter_WriteStr(writer, value); - } - /* if precision is specified, output no more that format.precision characters */ if (format->precision >= 0 && len >= format->precision) { @@ -775,23 +754,21 @@ maxchar = Py_MAX(maxchar, format->fill_char); /* allocate the resulting string */ - if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1) + result = PyUnicode_New(total, maxchar); + if (result == NULL) goto done; /* Write into that space. First the padding. */ - result = fill_padding(writer, len, - format->fill_char=='\0'?' ':format->fill_char, - lpad, rpad); - if (result == -1) - goto done; + pos = fill_padding(result, 0, len, + format->fill_char=='\0'?' ':format->fill_char, + lpad, rpad); /* Then the source string. */ - _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, - value, 0, len); - writer->pos += (len + rpad); - result = 0; + if (PyUnicode_CopyCharacters(result, pos, value, 0, len) < 0) + Py_CLEAR(result); done: + assert(!result || _PyUnicode_CheckConsistency(result, 1)); return result; } @@ -803,11 +780,11 @@ typedef PyObject* (*IntOrLongToString)(PyObject *value, int base); -static int -format_long_internal(PyObject *value, const InternalFormatSpec *format, - _PyUnicodeWriter *writer) +static PyObject * +format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format, + IntOrLongToString tostring) { - int result = -1; + PyObject *result = NULL; Py_UCS4 maxchar = 127; PyObject *tmp = NULL; Py_ssize_t inumeric_chars; @@ -821,6 +798,7 @@ Py_ssize_t prefix = 0; NumberFieldWidths spec; long x; + int err; /* Locale settings, either from the actual locale or from a hard-code pseudo-locale */ @@ -894,23 +872,13 @@ break; } - if (format->sign != '+' && format->sign != ' ' - && format->width == -1 - && format->type != 'X' && format->type != 'n' - && !format->thousands_separators - && PyLong_CheckExact(value)) - { - /* Fast path */ - return _PyLong_FormatWriter(writer, value, base, format->alternate); - } - /* The number of prefix chars is the same as the leading chars to skip */ if (format->alternate) n_prefix = leading_chars_to_skip; /* Do the hard part, converting to a string in a given base */ - tmp = _PyLong_Format(value, base); + tmp = tostring(value, base); if (tmp == NULL || PyUnicode_READY(tmp) == -1) goto done; @@ -946,19 +914,23 @@ &locale, format, &maxchar); /* Allocate the memory. */ - if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1) + result = PyUnicode_New(n_total, maxchar); + if (!result) goto done; /* Populate the memory. */ - result = fill_number(writer, &spec, - tmp, inumeric_chars, inumeric_chars + n_digits, - tmp, prefix, - format->fill_char == '\0' ? ' ' : format->fill_char, - &locale, format->type == 'X'); + err = fill_number(result, 0, &spec, + tmp, inumeric_chars, inumeric_chars + n_digits, + tmp, prefix, + format->fill_char == '\0' ? ' ' : format->fill_char, + &locale, format->type == 'X'); + if (err) + Py_CLEAR(result); done: Py_XDECREF(tmp); free_locale_info(&locale); + assert(!result || _PyUnicode_CheckConsistency(result, 1)); return result; } @@ -966,11 +938,16 @@ /*********** float formatting *******************************************/ /************************************************************************/ +static PyObject* +strtounicode(char *charbuffer, Py_ssize_t len) +{ + return PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, charbuffer, len); +} + /* much of this is taken from unicodeobject.c */ -static int +static PyObject * format_float_internal(PyObject *value, - const InternalFormatSpec *format, - _PyUnicodeWriter *writer) + const InternalFormatSpec *format) { char *buf = NULL; /* buffer returned from PyOS_double_to_string */ Py_ssize_t n_digits; @@ -985,11 +962,12 @@ Py_ssize_t index; NumberFieldWidths spec; int flags = 0; - int result = -1; + PyObject *result = NULL; Py_UCS4 maxchar = 127; Py_UCS4 sign_char = '\0'; int float_type; /* Used to see if we have a nan, inf, or regular float. */ PyObject *unicode_tmp = NULL; + int err; /* Locale settings, either from the actual locale or from a hard-code pseudo-locale */ @@ -1046,25 +1024,13 @@ /* Since there is no unicode version of PyOS_double_to_string, just use the 8 bit version and then convert to unicode. */ - unicode_tmp = _PyUnicode_FromASCII(buf, n_digits); - PyMem_Free(buf); + unicode_tmp = strtounicode(buf, n_digits); if (unicode_tmp == NULL) goto done; - - if (format->sign != '+' && format->sign != ' ' - && format->width == -1 - && format->type != 'n' - && !format->thousands_separators) - { - /* Fast path */ - result = _PyUnicodeWriter_WriteStr(writer, unicode_tmp); - Py_DECREF(unicode_tmp); - return result; - } + index = 0; /* Is a sign character present in the output? If so, remember it and skip it */ - index = 0; if (PyUnicode_READ_CHAR(unicode_tmp, index) == '-') { sign_char = '-'; ++index; @@ -1089,19 +1055,24 @@ &locale, format, &maxchar); /* Allocate the memory. */ - if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1) + result = PyUnicode_New(n_total, maxchar); + if (result == NULL) goto done; /* Populate the memory. */ - result = fill_number(writer, &spec, - unicode_tmp, index, index + n_digits, - NULL, 0, - format->fill_char == '\0' ? ' ' : format->fill_char, - &locale, 0); + err = fill_number(result, 0, &spec, + unicode_tmp, index, index + n_digits, + NULL, 0, + format->fill_char == '\0' ? ' ' : format->fill_char, + &locale, 0); + if (err) + Py_CLEAR(result); done: + PyMem_Free(buf); Py_DECREF(unicode_tmp); free_locale_info(&locale); + assert(!result || _PyUnicode_CheckConsistency(result, 1)); return result; } @@ -1109,10 +1080,9 @@ /*********** complex formatting *****************************************/ /************************************************************************/ -static int +static PyObject * format_complex_internal(PyObject *value, - const InternalFormatSpec *format, - _PyUnicodeWriter *writer) + const InternalFormatSpec *format) { double re; double im; @@ -1136,10 +1106,11 @@ NumberFieldWidths re_spec; NumberFieldWidths im_spec; int flags = 0; - int result = -1; + PyObject *result = NULL; Py_UCS4 maxchar = 127; - enum PyUnicode_Kind rkind; + int rkind; void *rdata; + Py_ssize_t index; Py_UCS4 re_sign_char = '\0'; Py_UCS4 im_sign_char = '\0'; int re_float_type; /* Used to see if we have a nan, inf, or regular float. */ @@ -1151,6 +1122,7 @@ Py_ssize_t total; PyObject *re_unicode_tmp = NULL; PyObject *im_unicode_tmp = NULL; + int err; /* Locale settings, either from the actual locale or from a hard-code pseudo-locale */ @@ -1219,12 +1191,12 @@ /* Since there is no unicode version of PyOS_double_to_string, just use the 8 bit version and then convert to unicode. */ - re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits); + re_unicode_tmp = strtounicode(re_buf, n_re_digits); if (re_unicode_tmp == NULL) goto done; i_re = 0; - im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits); + im_unicode_tmp = strtounicode(im_buf, n_im_digits); if (im_unicode_tmp == NULL) goto done; i_im = 0; @@ -1289,49 +1261,47 @@ if (lpad || rpad) maxchar = Py_MAX(maxchar, format->fill_char); - if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1) + result = PyUnicode_New(total, maxchar); + if (result == NULL) goto done; - rkind = writer->kind; - rdata = writer->data; + rkind = PyUnicode_KIND(result); + rdata = PyUnicode_DATA(result); /* Populate the memory. First, the padding. */ - result = fill_padding(writer, - n_re_total + n_im_total + 1 + add_parens * 2, - format->fill_char=='\0' ? ' ' : format->fill_char, - lpad, rpad); - if (result == -1) - goto done; + index = fill_padding(result, 0, + n_re_total + n_im_total + 1 + add_parens * 2, + format->fill_char=='\0' ? ' ' : format->fill_char, + lpad, rpad); - if (add_parens) { - PyUnicode_WRITE(rkind, rdata, writer->pos, '('); - writer->pos++; - } + if (add_parens) + PyUnicode_WRITE(rkind, rdata, index++, '('); if (!skip_re) { - result = fill_number(writer, &re_spec, - re_unicode_tmp, i_re, i_re + n_re_digits, - NULL, 0, - 0, - &locale, 0); - if (result == -1) + err = fill_number(result, index, &re_spec, + re_unicode_tmp, i_re, i_re + n_re_digits, + NULL, 0, + 0, + &locale, 0); + if (err) { + Py_CLEAR(result); goto done; + } + index += n_re_total; } - result = fill_number(writer, &im_spec, - im_unicode_tmp, i_im, i_im + n_im_digits, - NULL, 0, - 0, - &locale, 0); - if (result == -1) + err = fill_number(result, index, &im_spec, + im_unicode_tmp, i_im, i_im + n_im_digits, + NULL, 0, + 0, + &locale, 0); + if (err) { + Py_CLEAR(result); goto done; - PyUnicode_WRITE(rkind, rdata, writer->pos, 'j'); - writer->pos++; + } + index += n_im_total; + PyUnicode_WRITE(rkind, rdata, index++, 'j'); - if (add_parens) { - PyUnicode_WRITE(rkind, rdata, writer->pos, ')'); - writer->pos++; - } - - writer->pos += rpad; + if (add_parens) + PyUnicode_WRITE(rkind, rdata, index++, ')'); done: PyMem_Free(re_buf); @@ -1339,79 +1309,61 @@ Py_XDECREF(re_unicode_tmp); Py_XDECREF(im_unicode_tmp); free_locale_info(&locale); + assert(!result || _PyUnicode_CheckConsistency(result, 1)); return result; } /************************************************************************/ /*********** built in formatters ****************************************/ /************************************************************************/ -int -format_obj(PyObject *obj, _PyUnicodeWriter *writer) -{ - PyObject *str; - int err; - - str = PyObject_Str(obj); - if (str == NULL) - return -1; - err = _PyUnicodeWriter_WriteStr(writer, str); - Py_DECREF(str); - return err; -} - -int -_PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer, - PyObject *obj, - PyObject *format_spec, - Py_ssize_t start, Py_ssize_t end) +PyObject * +_PyUnicode_FormatAdvanced(PyObject *obj, + PyObject *format_spec, + Py_ssize_t start, Py_ssize_t end) { InternalFormatSpec format; - - assert(PyUnicode_Check(obj)); + PyObject *result; /* check for the special case of zero length format spec, make it equivalent to str(obj) */ - if (start == end) { - if (PyUnicode_CheckExact(obj)) - return _PyUnicodeWriter_WriteStr(writer, obj); - else - return format_obj(obj, writer); - } + if (start == end) + return PyObject_Str(obj); /* parse the format_spec */ if (!parse_internal_render_format_spec(format_spec, start, end, &format, 's', '<')) - return -1; + return NULL; /* type conversion? */ switch (format.type) { case 's': /* no type conversion needed, already a string. do the formatting */ - return format_string_internal(obj, &format, writer); + result = format_string_internal(obj, &format); + if (result != NULL) + assert(_PyUnicode_CheckConsistency(result, 1)); + break; default: /* unknown */ unknown_presentation_type(format.type, obj->ob_type->tp_name); - return -1; + result = NULL; } + return result; } -int -_PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer, - PyObject *obj, - PyObject *format_spec, - Py_ssize_t start, Py_ssize_t end) +static PyObject* +format_int_or_long(PyObject* obj, PyObject* format_spec, + Py_ssize_t start, Py_ssize_t end, + IntOrLongToString tostring) { - PyObject *tmp = NULL, *str = NULL; + PyObject *result = NULL; + PyObject *tmp = NULL; InternalFormatSpec format; - int result = -1; /* check for the special case of zero length format spec, make it equivalent to str(obj) */ if (start == end) { - if (PyLong_CheckExact(obj)) - return _PyLong_FormatWriter(writer, obj, 10, 0); - else - return format_obj(obj, writer); + result = PyObject_Str(obj); + goto done; } /* parse the format_spec */ @@ -1430,7 +1382,7 @@ case 'n': /* no type conversion needed, already an int (or long). do the formatting */ - result = format_long_internal(obj, &format, writer); + result = format_int_or_long_internal(obj, &format, tostring); break; case 'e': @@ -1444,7 +1396,7 @@ tmp = PyNumber_Float(obj); if (tmp == NULL) goto done; - result = format_float_internal(tmp, &format, writer); + result = format_float_internal(tmp, &format); break; default: @@ -1455,27 +1407,41 @@ done: Py_XDECREF(tmp); - Py_XDECREF(str); return result; } -int -_PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer, - PyObject *obj, - PyObject *format_spec, - Py_ssize_t start, Py_ssize_t end) +/* Need to define long_format as a function that will convert a long + to a string. In 3.0, _PyLong_Format has the correct signature. */ +#define long_format _PyLong_Format + +PyObject * +_PyLong_FormatAdvanced(PyObject *obj, + PyObject *format_spec, + Py_ssize_t start, Py_ssize_t end) { + return format_int_or_long(obj, format_spec, start, end, + long_format); +} + +PyObject * +_PyFloat_FormatAdvanced(PyObject *obj, + PyObject *format_spec, + Py_ssize_t start, Py_ssize_t end) +{ + PyObject *result = NULL; InternalFormatSpec format; /* check for the special case of zero length format spec, make it equivalent to str(obj) */ - if (start == end) - return format_obj(obj, writer); + if (start == end) { + result = PyObject_Str(obj); + goto done; + } /* parse the format_spec */ if (!parse_internal_render_format_spec(format_spec, start, end, &format, '\0', '>')) - return -1; + goto done; /* type conversion? */ switch (format.type) { @@ -1489,32 +1455,38 @@ case 'n': case '%': /* no conversion, already a float. do the formatting */ - return format_float_internal(obj, &format, writer); + result = format_float_internal(obj, &format); + break; default: /* unknown */ unknown_presentation_type(format.type, obj->ob_type->tp_name); - return -1; + goto done; } + +done: + return result; } -int -_PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer, - PyObject *obj, - PyObject *format_spec, - Py_ssize_t start, Py_ssize_t end) +PyObject * +_PyComplex_FormatAdvanced(PyObject *obj, + PyObject *format_spec, + Py_ssize_t start, Py_ssize_t end) { + PyObject *result = NULL; InternalFormatSpec format; /* check for the special case of zero length format spec, make it equivalent to str(obj) */ - if (start == end) - return format_obj(obj, writer); + if (start == end) { + result = PyObject_Str(obj); + goto done; + } /* parse the format_spec */ if (!parse_internal_render_format_spec(format_spec, start, end, &format, '\0', '>')) - return -1; + goto done; /* type conversion? */ switch (format.type) { @@ -1527,11 +1499,15 @@ case 'G': case 'n': /* no conversion, already a complex. do the formatting */ - return format_complex_internal(obj, &format, writer); + result = format_complex_internal(obj, &format); + break; default: /* unknown */ unknown_presentation_type(format.type, obj->ob_type->tp_name); - return -1; + goto done; } + +done: + return result; } diff -r 2059910e7d76 -r ac776ef41428 Python/getargs.c --- a/Python/getargs.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Python/getargs.c Mon May 21 23:01:17 2012 -0400 @@ -1167,11 +1167,8 @@ case 'U': { /* PyUnicode object */ PyObject **p = va_arg(*p_va, PyObject **); - if (PyUnicode_Check(arg)) { - if (PyUnicode_READY(arg) == -1) - RETURN_ERR_OCCURRED; + if (PyUnicode_Check(arg)) *p = arg; - } else return converterr("str", arg, msgbuf, bufsize); break; diff -r 2059910e7d76 -r ac776ef41428 Python/import.c --- a/Python/import.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Python/import.c Mon May 21 23:01:17 2012 -0400 @@ -106,7 +106,6 @@ Python 3.3a0 3200 (__qualname__ added) 3210 (added size modulo 2**32 to the pyc header) Python 3.3a1 3220 (changed PEP 380 implementation) - Python 3.3a4 3230 (revert changes to implicit __class__ closure) */ /* MAGIC must change whenever the bytecode emitted by the compiler may no @@ -119,7 +118,7 @@ #define STRIFY(name) QUOTE(name) #define MAJOR STRIFY(PY_MAJOR_VERSION) #define MINOR STRIFY(PY_MINOR_VERSION) -#define MAGIC (3230 | ((long)'\r'<<16) | ((long)'\n'<<24)) +#define MAGIC (3220 | ((long)'\r'<<16) | ((long)'\n'<<24)) #define TAG "cpython-" MAJOR MINOR; #define CACHEDIR "__pycache__" /* Current magic word and string tag as globals. */ @@ -1371,7 +1370,47 @@ PyObject * PyImport_ImportModuleNoBlock(const char *name) { - return PyImport_ImportModule(name); + PyObject *nameobj, *modules, *result; +#ifdef WITH_THREAD + long me; +#endif + + /* Try to get the module from sys.modules[name] */ + modules = PyImport_GetModuleDict(); + if (modules == NULL) + return NULL; + + nameobj = PyUnicode_FromString(name); + if (nameobj == NULL) + return NULL; + result = PyDict_GetItem(modules, nameobj); + if (result != NULL) { + Py_DECREF(nameobj); + Py_INCREF(result); + return result; + } + PyErr_Clear(); +#ifdef WITH_THREAD + /* check the import lock + * me might be -1 but I ignore the error here, the lock function + * takes care of the problem */ + me = PyThread_get_thread_ident(); + if (import_lock_thread == -1 || import_lock_thread == me) { + /* no thread or me is holding the lock */ + result = PyImport_Import(nameobj); + } + else { + PyErr_Format(PyExc_ImportError, + "Failed to import %R because the import lock" + "is held by another thread.", + nameobj); + result = NULL; + } +#else + result = PyImport_Import(nameobj); +#endif + Py_DECREF(nameobj); + return result; } @@ -1381,13 +1420,11 @@ int level) { _Py_IDENTIFIER(__import__); - _Py_IDENTIFIER(__initializing__); _Py_IDENTIFIER(__package__); _Py_IDENTIFIER(__path__); _Py_IDENTIFIER(__name__); _Py_IDENTIFIER(_find_and_load); _Py_IDENTIFIER(_handle_fromlist); - _Py_IDENTIFIER(_lock_unlock_module); _Py_static_string(single_dot, "."); PyObject *abs_name = NULL; PyObject *builtins_import = NULL; @@ -1570,48 +1607,16 @@ goto error_with_unlock; } else if (mod != NULL) { - PyObject *value; - int initializing = 0; - Py_INCREF(mod); - /* Only call _bootstrap._lock_unlock_module() if __initializing__ is true. */ - value = _PyObject_GetAttrId(mod, &PyId___initializing__); - if (value == NULL) - PyErr_Clear(); - else { - initializing = PyObject_IsTrue(value); - Py_DECREF(value); - if (initializing == -1) - PyErr_Clear(); - } - if (initializing > 0) { - /* _bootstrap._lock_unlock_module() releases the import lock */ - value = _PyObject_CallMethodObjIdArgs(interp->importlib, - &PyId__lock_unlock_module, abs_name, - NULL); - if (value == NULL) - goto error; - Py_DECREF(value); - } - else { -#ifdef WITH_THREAD - if (_PyImport_ReleaseLock() < 0) { - PyErr_SetString(PyExc_RuntimeError, "not holding the import lock"); - goto error; - } -#endif - } } else { - /* _bootstrap._find_and_load() releases the import lock */ mod = _PyObject_CallMethodObjIdArgs(interp->importlib, &PyId__find_and_load, abs_name, builtins_import, NULL); if (mod == NULL) { - goto error; + goto error_with_unlock; } } - /* From now on we don't hold the import lock anymore. */ if (PyObject_Not(fromlist)) { if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) { @@ -1620,12 +1625,12 @@ PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot); if (borrowed_dot == NULL) { - goto error; + goto error_with_unlock; } partition = PyUnicode_Partition(name, borrowed_dot); if (partition == NULL) { - goto error; + goto error_with_unlock; } if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) { @@ -1633,7 +1638,7 @@ Py_DECREF(partition); final_mod = mod; Py_INCREF(mod); - goto error; + goto exit_with_unlock; } front = PyTuple_GET_ITEM(partition, 0); @@ -1652,7 +1657,7 @@ abs_name_len - cut_off); Py_DECREF(front); if (to_return == NULL) { - goto error; + goto error_with_unlock; } final_mod = PyDict_GetItem(interp->modules, to_return); @@ -1678,8 +1683,8 @@ fromlist, builtins_import, NULL); } - goto error; + exit_with_unlock: error_with_unlock: #ifdef WITH_THREAD if (_PyImport_ReleaseLock() < 0) { diff -r 2059910e7d76 -r ac776ef41428 Python/importlib.h Binary file Python/importlib.h has changed diff -r 2059910e7d76 -r ac776ef41428 Python/symtable.c --- a/Python/symtable.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Python/symtable.c Mon May 21 23:01:17 2012 -0400 @@ -221,10 +221,17 @@ struct symtable * PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future) { - struct symtable *st = symtable_new(); + struct symtable *st; asdl_seq *seq; int i; + if (__class__ == NULL) { + __class__ = PyUnicode_InternFromString("@__class__"); + if (__class__ == NULL) + return NULL; + } + + st = symtable_new(); if (st == NULL) return st; st->st_filename = filename; @@ -740,8 +747,6 @@ } else { /* Special-case __class__ */ - if (!GET_IDENTIFIER(__class__)) - goto error; assert(PySet_Contains(local, __class__) == 1); if (PySet_Add(newbound, __class__) < 0) goto error; @@ -779,7 +784,7 @@ NULL)) goto error; else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree, - "__class__")) + "@__class__")) goto error; /* Records the results of the analysis in the symbol table entry */ if (!update_symbols(ste->ste_symbols, scopes, bound, newfree, @@ -1106,8 +1111,7 @@ if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, (void *)s, s->lineno, s->col_offset)) return 0; - if (!GET_IDENTIFIER(__class__) || - !symtable_add_def(st, __class__, DEF_LOCAL) || + if (!symtable_add_def(st, __class__, DEF_LOCAL) || !GET_IDENTIFIER(__locals__) || !symtable_add_def(st, __locals__, DEF_PARAM)) { symtable_exit_block(st, s); @@ -1372,8 +1376,7 @@ if (e->v.Name.ctx == Load && st->st_cur->ste_type == FunctionBlock && !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) { - if (!GET_IDENTIFIER(__class__) || - !symtable_add_def(st, __class__, USE)) + if (!symtable_add_def(st, __class__, USE)) return 0; } break; diff -r 2059910e7d76 -r ac776ef41428 Python/sysmodule.c --- a/Python/sysmodule.c Sat Jun 09 17:31:59 2012 +0100 +++ b/Python/sysmodule.c Mon May 21 23:01:17 2012 -0400 @@ -1261,7 +1261,6 @@ float_info -- a struct sequence with information about the float implementation.\n\ float_repr_style -- string indicating the style of repr() output for floats\n\ hexversion -- version information encoded as a single integer\n\ -implementation -- Python implementation information.\n\ int_info -- a struct sequence with information about the int implementation.\n\ maxsize -- the largest supported length of containers.\n\ maxunicode -- the value of the largest Unicode codepoint\n\ @@ -1455,69 +1454,6 @@ return version_info; } -static PyObject * -make_impl_info(PyObject *version_info) -{ - int res; - PyObject *impl_info, *value, *ns; - - impl_info = PyDict_New(); - if (impl_info == NULL) - return NULL; - - /* populate the dict */ - -#define NAME "cpython" -#define QUOTE(arg) #arg -#define STRIFY(name) QUOTE(name) -#define MAJOR STRIFY(PY_MAJOR_VERSION) -#define MINOR STRIFY(PY_MINOR_VERSION) -#define TAG NAME "-" MAJOR MINOR - value = PyUnicode_FromString(NAME); - if (value == NULL) - goto error; - res = PyDict_SetItemString(impl_info, "name", value); - Py_DECREF(value); - if (res < 0) - goto error; - - value = PyUnicode_FromString(TAG); - if (value == NULL) - goto error; - res = PyDict_SetItemString(impl_info, "cache_tag", value); - Py_DECREF(value); - if (res < 0) - goto error; -#undef NAME -#undef QUOTE -#undef STRIFY -#undef MAJOR -#undef MINOR -#undef TAG - - res = PyDict_SetItemString(impl_info, "version", version_info); - if (res < 0) - goto error; - - value = PyLong_FromLong(PY_VERSION_HEX); - if (value == NULL) - goto error; - res = PyDict_SetItemString(impl_info, "hexversion", value); - Py_DECREF(value); - if (res < 0) - goto error; - - /* dict ready */ - - ns = _PyNamespace_New(impl_info); - Py_DECREF(impl_info); - return ns; - -error: - Py_CLEAR(impl_info); - return NULL; -} - static struct PyModuleDef sysmodule = { PyModuleDef_HEAD_INIT, "sys", @@ -1533,7 +1469,7 @@ PyObject * _PySys_Init(void) { - PyObject *m, *v, *sysdict, *version_info; + PyObject *m, *v, *sysdict; char *s; m = PyModule_Create(&sysmodule); @@ -1592,10 +1528,6 @@ PyUnicode_FromWideChar(Py_GetPrefix(), -1)); SET_SYS_FROM_STRING("exec_prefix", PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); - SET_SYS_FROM_STRING("base_prefix", - PyUnicode_FromWideChar(Py_GetPrefix(), -1)); - SET_SYS_FROM_STRING("base_exec_prefix", - PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); SET_SYS_FROM_STRING("maxsize", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); SET_SYS_FROM_STRING("float_info", @@ -1653,15 +1585,11 @@ /* version_info */ if (VersionInfoType.tp_name == 0) PyStructSequence_InitType(&VersionInfoType, &version_info_desc); - version_info = make_version_info(); - SET_SYS_FROM_STRING("version_info", version_info); + SET_SYS_FROM_STRING("version_info", make_version_info()); /* prevent user from creating new instances */ VersionInfoType.tp_init = NULL; VersionInfoType.tp_new = NULL; - /* implementation */ - SET_SYS_FROM_STRING("implementation", make_impl_info(version_info)); - /* flags */ if (FlagsType.tp_name == 0) PyStructSequence_InitType(&FlagsType, &flags_desc); diff -r 2059910e7d76 -r ac776ef41428 Python/thread_pthread.h --- a/Python/thread_pthread.h Sat Jun 09 17:31:59 2012 +0100 +++ b/Python/thread_pthread.h Mon May 21 23:01:17 2012 -0400 @@ -443,15 +443,12 @@ dprintf(("PyThread_free_lock(%p) called\n", lock)); - /* some pthread-like implementations tie the mutex to the cond - * and must have the cond destroyed first. - */ + status = pthread_mutex_destroy( &thelock->mut ); + CHECK_STATUS("pthread_mutex_destroy"); + status = pthread_cond_destroy( &thelock->lock_released ); CHECK_STATUS("pthread_cond_destroy"); - status = pthread_mutex_destroy( &thelock->mut ); - CHECK_STATUS("pthread_mutex_destroy"); - free((void *)thelock); } @@ -534,12 +531,12 @@ thelock->locked = 0; + status = pthread_mutex_unlock( &thelock->mut ); + CHECK_STATUS("pthread_mutex_unlock[3]"); + /* wake up someone (anyone, if any) waiting on the lock */ status = pthread_cond_signal( &thelock->lock_released ); CHECK_STATUS("pthread_cond_signal"); - - status = pthread_mutex_unlock( &thelock->mut ); - CHECK_STATUS("pthread_mutex_unlock[3]"); } #endif /* USE_SEMAPHORES */ diff -r 2059910e7d76 -r ac776ef41428 README --- a/README Sat Jun 09 17:31:59 2012 +0100 +++ b/README Mon May 21 23:01:17 2012 -0400 @@ -1,4 +1,4 @@ -This is Python version 3.3.0 alpha 4 +This is Python version 3.3.0 alpha 3 ==================================== Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, diff -r 2059910e7d76 -r ac776ef41428 Tools/buildbot/external-common.bat --- a/Tools/buildbot/external-common.bat Sat Jun 09 17:31:59 2012 +0100 +++ b/Tools/buildbot/external-common.bat Mon May 21 23:01:17 2012 -0400 @@ -14,7 +14,7 @@ @rem if exist tk8.4.16 rd /s/q tk8.4.16 @rem if exist tk-8.4.18.1 rd /s/q tk-8.4.18.1 @rem if exist db-4.4.20 rd /s/q db-4.4.20 -@rem if exist openssl-1.0.1c rd /s/q openssl-1.0.1c +@rem if exist openssl-1.0.0a rd /s/q openssl-1.0.0a @rem if exist sqlite-3.7.12 rd /s/q sqlite-3.7.12 @rem bzip @@ -24,10 +24,7 @@ ) @rem OpenSSL -if not exist openssl-1.0.1c ( - rd /s/q openssl-1.0.0j - svn export http://svn.python.org/projects/external/openssl-1.0.1c -) +if not exist openssl-1.0.0a svn export http://svn.python.org/projects/external/openssl-1.0.0a @rem tcl/tk if not exist tcl-8.5.11.0 ( diff -r 2059910e7d76 -r ac776ef41428 Tools/msi/msi.py --- a/Tools/msi/msi.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Tools/msi/msi.py Mon May 21 23:01:17 2012 -0400 @@ -875,7 +875,7 @@ def extract_msvcr100(): # Find the redistributable files if msilib.Win64: - arch = "x64" + arch = "amd64" else: arch = "x86" dir = os.path.join(os.environ['VS100COMNTOOLS'], r"..\..\VC\redist\%s\Microsoft.VC100.CRT" % arch) @@ -1122,7 +1122,6 @@ lib.add_file("2to3.py", src="2to3") lib.add_file("pydoc3.py", src="pydoc3") lib.add_file("pysetup3.py", src="pysetup3") - lib.add_file("pyvenv.py", src="pyvenv") if have_tcl: lib.start_component("pydocgui.pyw", tcltk, keyfile="pydocgui.pyw") lib.add_file("pydocgui.pyw") diff -r 2059910e7d76 -r ac776ef41428 Tools/scripts/pyvenv --- a/Tools/scripts/pyvenv Sat Jun 09 17:31:59 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -#!/usr/bin/env python3 -if __name__ == '__main__': - import sys - rc = 1 - try: - import venv - venv.main() - rc = 0 - except Exception as e: - print('Error: %s' % e, file=sys.stderr) - sys.exit(rc) diff -r 2059910e7d76 -r ac776ef41428 Tools/scripts/run_tests.py --- a/Tools/scripts/run_tests.py Sat Jun 09 17:31:59 2012 +0100 +++ b/Tools/scripts/run_tests.py Mon May 21 23:01:17 2012 -0400 @@ -10,10 +10,6 @@ import os import sys import test.support -try: - import threading -except ImportError: - threading = None def is_multiprocess_flag(arg): @@ -38,7 +34,7 @@ ]) if sys.platform == 'win32': args.append('-n') # Silence alerts under Windows - if threading and not any(is_multiprocess_flag(arg) for arg in regrtest_args): + if not any(is_multiprocess_flag(arg) for arg in regrtest_args): args.extend(['-j', '0']) # Use all CPU cores if not any(is_resource_use_flag(arg) for arg in regrtest_args): args.extend(['-u', 'all,-largefile,-audio,-gui']) diff -r 2059910e7d76 -r ac776ef41428 setup.py --- a/setup.py Sat Jun 09 17:31:59 2012 +0100 +++ b/setup.py Mon May 21 23:01:17 2012 -0400 @@ -431,7 +431,7 @@ for directory in reversed(options.dirs): add_dir_to_list(dir_list, directory) - if os.path.normpath(sys.base_prefix) != '/usr' \ + if os.path.normpath(sys.prefix) != '/usr' \ and not sysconfig.get_config_var('PYTHONFRAMEWORK'): # OSX note: Don't add LIBDIR and INCLUDEDIR to building a framework # (PYTHONFRAMEWORK is set) to avoid # linking problems when @@ -1978,7 +1978,7 @@ newoutfiles = [] newupdated_files = [] for filename in outfiles: - if filename.endswith(('2to3', 'pyvenv')): + if filename.endswith('2to3'): newfilename = filename + fullversion else: newfilename = filename + minoronly @@ -2046,8 +2046,7 @@ # check the PyBuildScripts command above, and change the links # created by the bininstall target in Makefile.pre.in scripts = ["Tools/scripts/pydoc3", "Tools/scripts/idle3", - "Tools/scripts/2to3", "Tools/scripts/pysetup3", - "Tools/scripts/pyvenv"] + "Tools/scripts/2to3", "Tools/scripts/pysetup3"] ) # --install-platlib