From e152a77d96df7479ce798ebd5baee7d41af99db3 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sat, 24 May 2008 18:31:28 +0000 Subject: socketserver renaming reversal part 3: move the module into the right place and fix all references to it. Closes #2926. --- Doc/library/basehttpserver.rst | 2 +- Doc/library/logging.rst | 10 +- Doc/library/repr.rst | 5 +- Doc/library/simplexmlrpcserver.rst | 4 +- Doc/library/socket.rst | 2 +- Doc/library/socketserver.rst | 41 ++- Doc/whatsnew/2.6.rst | 656 +++++++++++++++++------------------ Lib/BaseHTTPServer.py | 8 +- Lib/SimpleXMLRPCServer.py | 6 +- Lib/SocketServer.py | 681 +++++++++++++++++++++++++++++++++++++ Lib/idlelib/rpc.py | 14 +- Lib/lib-old/SocketServer.py | 681 ------------------------------------- Lib/logging/config.py | 2 +- Lib/test/test___all__.py | 2 +- Lib/test/test_logging.py | 2 +- Lib/test/test_py3kwarn.py | 1 - Lib/test/test_socketserver.py | 58 ++-- Lib/test/test_wsgiref.py | 2 +- Misc/NEWS | 3 - Misc/cheatsheet | 2 +- 20 files changed, 1090 insertions(+), 1092 deletions(-) create mode 100644 Lib/SocketServer.py delete mode 100644 Lib/lib-old/SocketServer.py diff --git a/Doc/library/basehttpserver.rst b/Doc/library/basehttpserver.rst index 7d50613..0fbf8b0 100644 --- a/Doc/library/basehttpserver.rst +++ b/Doc/library/basehttpserver.rst @@ -21,7 +21,7 @@ Usually, this module isn't used directly, but is used as a basis for building functioning Web servers. See the :mod:`SimpleHTTPServer` and :mod:`CGIHTTPServer` modules. -The first class, :class:`HTTPServer`, is a :class:`socketserver.TCPServer` +The first class, :class:`HTTPServer`, is a :class:`SocketServer.TCPServer` subclass. It creates and listens at the HTTP socket, dispatching the requests to a handler. Code to create and run the server looks like this:: diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst index 6ece996..bca1ece 100644 --- a/Doc/library/logging.rst +++ b/Doc/library/logging.rst @@ -1299,17 +1299,17 @@ the receiving end. A simple way of doing this is attaching a logger2.warning('Jail zesty vixen who grabbed pay from quack.') logger2.error('The five boxing wizards jump quickly.') -At the receiving end, you can set up a receiver using the :mod:`socketserver` +At the receiving end, you can set up a receiver using the :mod:`SocketServer` module. Here is a basic working example:: import cPickle import logging import logging.handlers - import socketserver + import SocketServer import struct - class LogRecordStreamHandler(socketserver.StreamRequestHandler): + class LogRecordStreamHandler(SocketServer.StreamRequestHandler): """Handler for a streaming logging request. This basically logs the record using whatever logging policy is @@ -1351,7 +1351,7 @@ module. Here is a basic working example:: # cycles and network bandwidth! logger.handle(record) - class LogRecordSocketReceiver(socketserver.ThreadingTCPServer): + class LogRecordSocketReceiver(SocketServer.ThreadingTCPServer): """simple TCP socket-based logging receiver suitable for testing. """ @@ -1360,7 +1360,7 @@ module. Here is a basic working example:: def __init__(self, host='localhost', port=logging.handlers.DEFAULT_TCP_LOGGING_PORT, handler=LogRecordStreamHandler): - socketserver.ThreadingTCPServer.__init__(self, (host, port), handler) + SocketServer.ThreadingTCPServer.__init__(self, (host, port), handler) self.abort = 0 self.timeout = 1 self.logname = None diff --git a/Doc/library/repr.rst b/Doc/library/repr.rst index bd9743d..29c5a61 100644 --- a/Doc/library/repr.rst +++ b/Doc/library/repr.rst @@ -7,8 +7,9 @@ .. sectionauthor:: Fred L. Drake, Jr. .. note:: - The :mod:`repr` module has been renamed to :mod:`reprlib` in - Python 3.0. + The :mod:`repr` module has been renamed to :mod:`reprlib` in Python 3.0. The + :term:`2to3` tool will automatically adapt imports when converting your + sources to 3.0. The :mod:`repr` module provides a means for producing object representations with limits on the size of the resulting strings. This is used in the Python diff --git a/Doc/library/simplexmlrpcserver.rst b/Doc/library/simplexmlrpcserver.rst index d434210..c788d55 100644 --- a/Doc/library/simplexmlrpcserver.rst +++ b/Doc/library/simplexmlrpcserver.rst @@ -22,7 +22,7 @@ XML-RPC servers written in Python. Servers can either be free standing, using functions that can be called by the XML-RPC protocol. The *requestHandler* parameter should be a factory for request handler instances; it defaults to :class:`SimpleXMLRPCRequestHandler`. The *addr* and *requestHandler* parameters - are passed to the :class:`socketserver.TCPServer` constructor. If *logRequests* + are passed to the :class:`SocketServer.TCPServer` constructor. If *logRequests* is true (the default), requests will be logged; setting this parameter to false will turn off logging. The *allow_none* and *encoding* parameters are passed on to :mod:`xmlrpclib` and control the XML-RPC responses that will be returned @@ -63,7 +63,7 @@ SimpleXMLRPCServer Objects -------------------------- The :class:`SimpleXMLRPCServer` class is based on -:class:`socketserver.TCPServer` and provides a means of creating simple, stand +:class:`SocketServer.TCPServer` and provides a means of creating simple, stand alone XML-RPC servers. diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst index d05120e..e5a8167 100644 --- a/Doc/library/socket.rst +++ b/Doc/library/socket.rst @@ -481,7 +481,7 @@ The module :mod:`socket` exports the following constants and functions: .. seealso:: - Module :mod:`socketserver` + Module :mod:`SocketServer` Classes that simplify writing network servers. diff --git a/Doc/library/socketserver.rst b/Doc/library/socketserver.rst index d06b2c2..51cab5e 100644 --- a/Doc/library/socketserver.rst +++ b/Doc/library/socketserver.rst @@ -1,19 +1,18 @@ -:mod:`socketserver` --- A framework for network servers + +:mod:`SocketServer` --- A framework for network servers ======================================================= .. module:: SocketServer - :synopsis: Old name for the socketserver module. - -.. module:: socketserver :synopsis: A framework for network servers. .. note:: - The :mod:`SocketServer` module has been renamed to :mod:`socketserver` in - Python 3.0. It is importable under both names in Python 2.6 and the rest of - the 2.x series. + + The :mod:`SocketServer` module has been renamed to `socketserver` in Python + 3.0. The :term:`2to3` tool will automatically adapt imports when converting + your sources to 3.0. -The :mod:`socketserver` module simplifies the task of writing network servers. +The :mod:`SocketServer` module simplifies the task of writing network servers. There are four basic server classes: :class:`TCPServer` uses the Internet TCP protocol, which provides for continuous streams of data between the client and @@ -220,7 +219,7 @@ server classes like :class:`TCPServer`; these methods aren't useful to external users of the server object. .. XXX should the default implementations of these be documented, or should - it be assumed that the user will look at socketserver.py? + it be assumed that the user will look at SocketServer.py? .. function:: finish_request() @@ -325,14 +324,14 @@ request. Examples -------- -:class:`socketserver.TCPServer` Example +:class:`SocketServer.TCPServer` Example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This is the server side:: - import socketserver + import SocketServer - class MyTCPHandler(socketserver.BaseRequestHandler): + class MyTCPHandler(SocketServer.BaseRequestHandler): """ The RequestHandler class for our server. @@ -353,7 +352,7 @@ This is the server side:: HOST, PORT = "localhost", 9999 # Create the server, binding to localhost on port 9999 - server = socketserver.TCPServer((HOST, PORT), MyTCPHandler) + server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) # Activate the server; this will keep running until you # interrupt the program with Ctrl-C @@ -362,7 +361,7 @@ This is the server side:: An alternative request handler class that makes use of streams (file-like objects that simplify communication by providing the standard file interface):: - class MyTCPHandler(socketserver.StreamRequestHandler): + class MyTCPHandler(SocketServer.StreamRequestHandler): def handle(self): # self.rfile is a file-like object created by the handler; @@ -423,14 +422,14 @@ Client:: Received: PYTHON IS NICE -:class:`socketserver.UDPServer` Example +:class:`SocketServer.UDPServer` Example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This is the server side:: - import socketserver + import SocketServer - class MyUDPHandler(socketserver.BaseRequestHandler): + class MyUDPHandler(SocketServer.BaseRequestHandler): """ This class works similar to the TCP handler class, except that self.request consists of a pair of data and client socket, and since @@ -447,7 +446,7 @@ This is the server side:: if __name__ == "__main__": HOST, PORT = "localhost", 9999 - server = socketserver.UDPServer((HOST, PORT), BaseUDPRequestHandler) + server = SocketServer.UDPServer((HOST, PORT), BaseUDPRequestHandler) server.serve_forever() This is the client side:: @@ -482,9 +481,9 @@ An example for the :class:`ThreadingMixIn` class:: import socket import threading - import socketserver + import SocketServer - class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler): + class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler): def handle(self): data = self.request.recv(1024) @@ -492,7 +491,7 @@ An example for the :class:`ThreadingMixIn` class:: response = "%s: %s" % (cur_thread.getName(), data) self.request.send(response) - class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): + class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): pass def client(ip, port, message): diff --git a/Doc/whatsnew/2.6.rst b/Doc/whatsnew/2.6.rst index 3145101..9337c23 100644 --- a/Doc/whatsnew/2.6.rst +++ b/Doc/whatsnew/2.6.rst @@ -1,5 +1,5 @@ **************************** - What's New in Python 2.6 + What's New in Python 2.6 **************************** .. XXX add trademark info for Apple, Microsoft, SourceForge. @@ -10,42 +10,42 @@ .. $Id: whatsnew26.tex 55746 2007-06-02 18:33:53Z neal.norwitz $ Rules for maintenance: - + * Anyone can add text to this document. Do not spend very much time on the wording of your changes, because your text will probably get rewritten to some degree. - + * The maintainer will go through Misc/NEWS periodically and add changes; it's therefore more important to add your changes to Misc/NEWS than to this file. - + * This is not a complete list of every single change; completeness is the purpose of Misc/NEWS. Some changes I consider too small or esoteric to include. If such a change is added to the text, I'll just remove it. (This is another reason you shouldn't spend too much time on writing your addition.) - + * If you want to draw your new text to the attention of the maintainer, add 'XXX' to the beginning of the paragraph or section. - + * It's OK to just add a fragmentary note about a change. For example: "XXX Describe the transmogrify() function added to the socket module." The maintainer will research the change and write the necessary text. - + * You can comment out your additions if you like, but it's not necessary (especially when a final release is some months away). - + * Credit the author of a patch or bugfix. Just the name is sufficient; the e-mail address isn't necessary. - + * It's helpful to add the bug/patch number in a parenthetical comment. - + XXX Describe the transmogrify() function added to the socket module. (Contributed by P.Y. Developer; :issue:`12345`.) - + This saves the maintainer some effort going through the SVN logs when researching a change. @@ -74,7 +74,7 @@ Python 3.0 ================ The development cycle for Python 2.6 also saw the release of the first -alphas of Python 3.0, and the development of 3.0 has influenced +alphas of Python 3.0, and the development of 3.0 has influenced a number of features in 2.6. Python 3.0 is a far-ranging redesign of Python that breaks @@ -83,7 +83,7 @@ code will need a certain amount of conversion in order to run on Python 3.0. However, not all the changes in 3.0 necessarily break compatibility. In cases where new features won't cause existing code to break, they've been backported to 2.6 and are described in this -document in the appropriate place. Some of the 3.0-derived features +document in the appropriate place. Some of the 3.0-derived features are: * A :meth:`__complex__` method for converting objects to a complex number. @@ -94,7 +94,7 @@ are: A new command-line switch, :option:`-3`, enables warnings about features that will be removed in Python 3.0. You can run code with this switch to see how much work will be necessary to port -code to 3.0. The value of this switch is available +code to 3.0. The value of this switch is available to Python code as the boolean variable :data:`sys.py3kwarning`, and to C extension code as :cdata:`Py_Py3kWarningFlag`. @@ -116,9 +116,9 @@ as necessary. Development Changes ================================================== -While 2.6 was being developed, the Python development process -underwent two significant changes: the developer group -switched from SourceForge's issue tracker to a customized +While 2.6 was being developed, the Python development process +underwent two significant changes: the developer group +switched from SourceForge's issue tracker to a customized Roundup installation, and the documentation was converted from LaTeX to reStructuredText. @@ -135,34 +135,34 @@ The infrastructure committee of the Python Software Foundation therefore posted a call for issue trackers, asking volunteers to set up different products and import some of the bugs and patches from SourceForge. Four different trackers were examined: Atlassian's `Jira -`__, -`Launchpad `__, -`Roundup `__, and -`Trac `__. +`__, +`Launchpad `__, +`Roundup `__, and +`Trac `__. The committee eventually settled on Jira and Roundup as the two candidates. Jira is a commercial product that -offers a no-cost hosted instance to free-software projects; Roundup +offers a no-cost hosted instance to free-software projects; Roundup is an open-source project that requires volunteers to administer it and a server to host it. After posting a call for volunteers, a new Roundup installation was set up at http://bugs.python.org. One installation of Roundup can host multiple trackers, and this server now also hosts issue trackers -for Jython and for the Python web site. It will surely find +for Jython and for the Python web site. It will surely find other uses in the future. Where possible, this edition of "What's New in Python" links to the bug/patch item for each change. -Hosting is kindly provided by -`Upfront Systems `__ +Hosting is kindly provided by +`Upfront Systems `__ of Stellenbosch, South Africa. Martin von Loewis put a lot of effort into importing existing bugs and patches from -SourceForge; his scripts for this import operation are at +SourceForge; his scripts for this import operation are at http://svn.python.org/view/tracker/importer/. .. seealso:: - http://bugs.python.org + http://bugs.python.org The Python bug tracker. http://bugs.jython.org: @@ -189,8 +189,8 @@ it online and HTML has become the most important format to support. Unfortunately, converting LaTeX to HTML is fairly complicated, and Fred L. Drake Jr., the Python documentation editor for many years, spent a lot of time wrestling the conversion process into shape. -Occasionally people would suggest converting the documentation into -SGML or, later, XML, but performing a good conversion is a major task +Occasionally people would suggest converting the documentation into +SGML or, later, XML, but performing a good conversion is a major task and no one pursued the task to completion. During the 2.6 development cycle, Georg Brandl put a substantial @@ -222,7 +222,7 @@ The previous version, Python 2.5, added the ':keyword:`with`' statement an optional feature, to be enabled by a ``from __future__ import with_statement`` directive. In 2.6 the statement no longer needs to be specially enabled; this means that :keyword:`with` is now always a -keyword. The rest of this section is a copy of the corresponding +keyword. The rest of this section is a copy of the corresponding section from "What's New in Python 2.5" document; if you read it back when Python 2.5 came out, you can skip the rest of this section. @@ -518,7 +518,7 @@ environment variable. :pep:`370` - Per-user ``site-packages`` Directory PEP written and implemented by Christian Heimes. - + .. ====================================================================== .. _pep-3101: @@ -539,7 +539,7 @@ The formatting template uses curly brackets (`{`, `}`) as special characters:: # Use the named keyword arguments uid = 'root' - + 'User ID: {uid} Last seen: {last_login}'.format(uid='root', last_login = '5 Mar 2008 07:20') -> 'User ID: root Last seen: 5 Mar 2008 07:20' @@ -548,8 +548,8 @@ Curly brackets can be escaped by doubling them:: format("Empty dict: {{}}") -> "Empty dict: {}" -Field names can be integers indicating positional arguments, such as -``{0}``, ``{1}``, etc. or names of keyword arguments. You can also +Field names can be integers indicating positional arguments, such as +``{0}``, ``{1}``, etc. or names of keyword arguments. You can also supply compound field names that read attributes or access dictionary keys:: import sys @@ -602,7 +602,7 @@ Character Effect = (For numeric types only) Pad after the sign. ================ ============================================ -Format specifiers can also include a presentation type, which +Format specifiers can also include a presentation type, which controls how the value is formatted. For example, floating-point numbers can be formatted as a general number or in exponential notation: @@ -660,10 +660,10 @@ PEP 3105: ``print`` As a Function ===================================================== The ``print`` statement becomes the :func:`print` function in Python 3.0. -Making :func:`print` a function makes it easier to change -by doing 'def print(...)' or importing a new function from somewhere else. +Making :func:`print` a function makes it easier to change +by doing 'def print(...)' or importing a new function from somewhere else. -Python 2.6 has a ``__future__`` import that removes ``print`` as language +Python 2.6 has a ``__future__`` import that removes ``print`` as language syntax, letting you use the functional form instead. For example:: from __future__ import print_function @@ -677,7 +677,7 @@ The parameters are: * **args**: positional arguments whose values will be printed out. * **sep**: the separator, which will be printed between arguments. - * **end**: the ending text, which will be printed after all of the + * **end**: the ending text, which will be printed after all of the arguments have been output. * **file**: the file object to which the output will be sent. @@ -693,7 +693,7 @@ The parameters are: PEP 3110: Exception-Handling Changes ===================================================== -One error that Python programmers occasionally make +One error that Python programmers occasionally make is the following:: try: @@ -701,11 +701,11 @@ is the following:: except TypeError, ValueError: ... -The author is probably trying to catch both +The author is probably trying to catch both :exc:`TypeError` and :exc:`ValueError` exceptions, but this code -actually does something different: it will catch +actually does something different: it will catch :exc:`TypeError` and bind the resulting exception object -to the local name ``"ValueError"``. The correct code +to the local name ``"ValueError"``. The correct code would have specified a tuple:: try: @@ -718,7 +718,7 @@ does it indicate two different nodes in the parse tree, or a single node that's a tuple. Python 3.0 changes the syntax to make this unambiguous by replacing -the comma with the word "as". To catch an exception and store the +the comma with the word "as". To catch an exception and store the exception object in the variable ``exc``, you must write:: try: @@ -744,13 +744,13 @@ PEP 3112: Byte Literals ===================================================== Python 3.0 adopts Unicode as the language's fundamental string type, and -denotes 8-bit literals differently, either as ``b'string'`` -or using a :class:`bytes` constructor. For future compatibility, +denotes 8-bit literals differently, either as ``b'string'`` +or using a :class:`bytes` constructor. For future compatibility, Python 2.6 adds :class:`bytes` as a synonym for the :class:`str` type, and it also supports the ``b''`` notation. There's also a ``__future__`` import that causes all string literals -to become Unicode strings. This means that ``\u`` escape sequences +to become Unicode strings. This means that ``\u`` escape sequences can be used to include Unicode characters:: @@ -786,7 +786,7 @@ There are three levels of abstract base classes provided by the :mod:`io` module: * :class:`RawIOBase`: defines raw I/O operations: :meth:`read`, - :meth:`readinto`, + :meth:`readinto`, :meth:`write`, :meth:`seek`, :meth:`tell`, :meth:`truncate`, and :meth:`close`. Most of the methods of this class will often map to a single system call. @@ -799,36 +799,36 @@ the :mod:`io` module: .. XXX should 2.6 register them in io.py? -* :class:`BufferedIOBase`: is an abstract base class that - buffers data in memory to reduce the number of +* :class:`BufferedIOBase`: is an abstract base class that + buffers data in memory to reduce the number of system calls used, making I/O processing more efficient. - It supports all of the methods of :class:`RawIOBase`, + It supports all of the methods of :class:`RawIOBase`, and adds a :attr:`raw` attribute holding the underlying raw object. There are four concrete classes implementing this ABC: - :class:`BufferedWriter` and + :class:`BufferedWriter` and :class:`BufferedReader` for objects that only support writing or reading and don't support random access, :class:`BufferedRandom` for objects that support the :meth:`seek` method for random access, - and :class:`BufferedRWPair` for objects such as TTYs that have + and :class:`BufferedRWPair` for objects such as TTYs that have both read and write operations that act upon unconnected streams of data. * :class:`TextIOBase`: Provides functions for reading and writing strings (remember, strings will be Unicode in Python 3.0), - and supporting universal newlines. :class:`TextIOBase` defines - the :meth:`readline` method and supports iteration upon - objects. + and supporting universal newlines. :class:`TextIOBase` defines + the :meth:`readline` method and supports iteration upon + objects. There are two concrete implementations. :class:`TextIOWrapper` wraps a buffered I/O object, supporting all of the methods for - text I/O and adding a :attr:`buffer` attribute for access + text I/O and adding a :attr:`buffer` attribute for access to the underlying object. :class:`StringIO` simply buffers everything in memory without ever writing anything to disk. (In current 2.6 alpha releases, :class:`io.StringIO` is implemented in - pure Python, so it's pretty slow. You should therefore stick with the - existing :mod:`StringIO` module or :mod:`cStringIO` for now. At some + pure Python, so it's pretty slow. You should therefore stick with the + existing :mod:`StringIO` module or :mod:`cStringIO` for now. At some point Python 3.0's :mod:`io` module will be rewritten into C for speed, and perhaps the C implementation will be backported to the 2.x releases.) @@ -836,7 +836,7 @@ the :mod:`io` module: In Python 2.6, the underlying implementations haven't been restructured to build on top of the :mod:`io` module's classes. The -module is being provided to make it easier to write code that's +module is being provided to make it easier to write code that's forward-compatible with 3.0, and to save developers the effort of writing their own implementations of buffering and text I/O. @@ -855,7 +855,7 @@ PEP 3118: Revised Buffer Protocol ===================================================== The buffer protocol is a C-level API that lets Python types -exchange pointers into their internal representations. A +exchange pointers into their internal representations. A memory-mapped file can be viewed as a buffer of characters, for example, and this lets another module such as :mod:`re` treat memory-mapped files as a string of characters to be searched. @@ -863,19 +863,19 @@ treat memory-mapped files as a string of characters to be searched. The primary users of the buffer protocol are numeric-processing packages such as NumPy, which can expose the internal representation of arrays so that callers can write data directly into an array instead -of going through a slower API. This PEP updates the buffer protocol in light of experience +of going through a slower API. This PEP updates the buffer protocol in light of experience from NumPy development, adding a number of new features -such as indicating the shape of an array, +such as indicating the shape of an array, locking memory . -The most important new C API function is +The most important new C API function is ``PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags)``, which takes an object and a set of flags, and fills in the -``Py_buffer`` structure with information +``Py_buffer`` structure with information about the object's memory representation. Objects -can use this operation to lock memory in place +can use this operation to lock memory in place while an external caller could be modifying the contents, -so there's a corresponding +so there's a corresponding ``PyObject_ReleaseBuffer(PyObject *obj, Py_buffer *view)`` to indicate that the external caller is done. @@ -883,7 +883,7 @@ The **flags** argument to :cfunc:`PyObject_GetBuffer` specifies constraints upon the memory returned. Some examples are: * :const:`PyBUF_WRITABLE` indicates that the memory must be writable. - + * :const:`PyBUF_LOCK` requests a read-only or exclusive lock on the memory. * :const:`PyBUF_C_CONTIGUOUS` and :const:`PyBUF_F_CONTIGUOUS` @@ -897,7 +897,7 @@ constraints upon the memory returned. Some examples are: :pep:`3118` - Revising the buffer protocol PEP written by Travis Oliphant and Carl Banks; implemented by Travis Oliphant. - + .. ====================================================================== @@ -909,16 +909,16 @@ PEP 3119: Abstract Base Classes Some object-oriented languages such as Java support interfaces: declarations that a class has a given set of methods or supports a given access protocol. Abstract Base Classes (or ABCs) are an equivalent feature for Python. The ABC -support consists of an :mod:`abc` module containing a metaclass called +support consists of an :mod:`abc` module containing a metaclass called :class:`ABCMeta`, special handling of this metaclass by the :func:`isinstance` and :func:`issubclass` built-ins, and a collection of basic ABCs that the Python developers think will be widely useful. -Let's say you have a particular class and wish to know whether it supports +Let's say you have a particular class and wish to know whether it supports dictionary-style access. The phrase "dictionary-style" is vague, however. -It probably means that accessing items with ``obj[1]`` works. -Does it imply that setting items with ``obj[2] = value`` works? +It probably means that accessing items with ``obj[1]`` works. +Does it imply that setting items with ``obj[2] = value`` works? Or that the object will have :meth:`keys`, :meth:`values`, and :meth:`items` methods? What about the iterative variants such as :meth:`iterkeys`? :meth:`copy` and :meth:`update`? Iterating over the object with :func:`iter`? @@ -927,7 +927,7 @@ Python 2.6 includes a number of different ABCs in the :mod:`collections` module. :class:`Iterable` indicates that a class defines :meth:`__iter__`, and :class:`Container` means the class supports ``x in y`` expressions by defining a :meth:`__contains__` method. The basic dictionary interface of -getting items, setting items, and +getting items, setting items, and :meth:`keys`, :meth:`values`, and :meth:`items`, is defined by the :class:`MutableMapping` ABC. @@ -935,22 +935,22 @@ You can derive your own classes from a particular ABC to indicate they support that ABC's interface:: import collections - + class Storage(collections.MutableMapping): ... -Alternatively, you could write the class without deriving from +Alternatively, you could write the class without deriving from the desired ABC and instead register the class by calling the ABC's :meth:`register` method:: import collections - + class Storage: ... - + collections.MutableMapping.register(Storage) - + For classes that you write, deriving from the ABC is probably clearer. The :meth:`register` method is useful when you've written a new ABC that can describe an existing type or class, or if you want @@ -963,8 +963,8 @@ it's legal to do: PrintableType.register(float) PrintableType.register(str) -Classes should obey the semantics specified by an ABC, but -Python can't check this; it's up to the class author to +Classes should obey the semantics specified by an ABC, but +Python can't check this; it's up to the class author to understand the ABC's requirements and to implement the code accordingly. To check whether an object supports a particular interface, you can @@ -972,11 +972,11 @@ now write:: def func(d): if not isinstance(d, collections.MutableMapping): - raise ValueError("Mapping object expected, not %r" % d) + raise ValueError("Mapping object expected, not %r" % d) -(Don't feel that you must now begin writing lots of checks as in the -above example. Python has a strong tradition of duck-typing, where -explicit type-checking isn't done and code simply calls methods on +(Don't feel that you must now begin writing lots of checks as in the +above example. Python has a strong tradition of duck-typing, where +explicit type-checking isn't done and code simply calls methods on an object, trusting that those methods will be there and raising an exception if they aren't. Be judicious in checking for ABCs and only do it where it helps.) @@ -988,46 +988,46 @@ metaclass in a class definition:: class Drawable(): __metaclass__ = ABCMeta - + def draw(self, x, y, scale=1.0): pass def draw_doubled(self, x, y): self.draw(x, y, scale=2.0) - + class Square(Drawable): def draw(self, x, y, scale): ... - + In the :class:`Drawable` ABC above, the :meth:`draw_doubled` method renders the object at twice its size and can be implemented in terms of other methods described in :class:`Drawable`. Classes implementing -this ABC therefore don't need to provide their own implementation +this ABC therefore don't need to provide their own implementation of :meth:`draw_doubled`, though they can do so. An implementation -of :meth:`draw` is necessary, though; the ABC can't provide -a useful generic implementation. You -can apply the ``@abstractmethod`` decorator to methods such as -:meth:`draw` that must be implemented; Python will -then raise an exception for classes that +of :meth:`draw` is necessary, though; the ABC can't provide +a useful generic implementation. You +can apply the ``@abstractmethod`` decorator to methods such as +:meth:`draw` that must be implemented; Python will +then raise an exception for classes that don't define the method:: class Drawable(): __metaclass__ = ABCMeta - + @abstractmethod def draw(self, x, y, scale): pass -Note that the exception is only raised when you actually +Note that the exception is only raised when you actually try to create an instance of a subclass without the method:: >>> s=Square() Traceback (most recent call last): File "", line 1, in TypeError: Can't instantiate abstract class Square with abstract methods draw - >>> + >>> Abstract data attributes can be declared using the ``@abstractproperty`` decorator:: @@ -1035,7 +1035,7 @@ Abstract data attributes can be declared using the ``@abstractproperty`` decorat def readonly(self): return self._x -Subclasses must then define a :meth:`readonly` property +Subclasses must then define a :meth:`readonly` property .. seealso:: @@ -1056,7 +1056,7 @@ which are now prefixed by "0o" or "0O" instead of a leading zero, and adds support for binary (base-2) integer literals, signalled by a "0b" or "0B" prefix. -Python 2.6 doesn't drop support for a leading 0 signalling +Python 2.6 doesn't drop support for a leading 0 signalling an octal number, but it does add support for "0o" and "0b":: >>> 0o21, 2*8 + 1 @@ -1064,8 +1064,8 @@ an octal number, but it does add support for "0o" and "0b":: >>> 0b101111 47 -The :func:`oct` built-in still returns numbers -prefixed with a leading zero, and a new :func:`bin` +The :func:`oct` built-in still returns numbers +prefixed with a leading zero, and a new :func:`bin` built-in returns the binary representation for a number:: >>> oct(42) @@ -1141,36 +1141,36 @@ Exact numbers can represent values precisely and operations never round off the results or introduce tiny errors that may break the commutativity and associativity properties; inexact numbers may perform such rounding or introduce small errors. Integers, long -integers, and rational numbers are exact, while floating-point +integers, and rational numbers are exact, while floating-point and complex numbers are inexact. :class:`Complex` is a subclass of :class:`Number`. Complex numbers can undergo the basic operations of addition, subtraction, multiplication, division, and exponentiation, and you can retrieve the -real and imaginary parts and obtain a number's conjugate. Python's built-in +real and imaginary parts and obtain a number's conjugate. Python's built-in complex type is an implementation of :class:`Complex`. -:class:`Real` further derives from :class:`Complex`, and adds -operations that only work on real numbers: :func:`floor`, :func:`trunc`, -rounding, taking the remainder mod N, floor division, -and comparisons. +:class:`Real` further derives from :class:`Complex`, and adds +operations that only work on real numbers: :func:`floor`, :func:`trunc`, +rounding, taking the remainder mod N, floor division, +and comparisons. :class:`Rational` numbers derive from :class:`Real`, have :attr:`numerator` and :attr:`denominator` properties, and can be converted to floats. Python 2.6 adds a simple rational-number class, -:class:`Fraction`, in the :mod:`fractions` module. (It's called -:class:`Fraction` instead of :class:`Rational` to avoid +:class:`Fraction`, in the :mod:`fractions` module. (It's called +:class:`Fraction` instead of :class:`Rational` to avoid a name clash with :class:`numbers.Rational`.) :class:`Integral` numbers derive from :class:`Rational`, and -can be shifted left and right with ``<<`` and ``>>``, -combined using bitwise operations such as ``&`` and ``|``, +can be shifted left and right with ``<<`` and ``>>``, +combined using bitwise operations such as ``&`` and ``|``, and can be used as array indexes and slice boundaries. In Python 3.0, the PEP slightly redefines the existing built-ins :func:`round`, :func:`math.floor`, :func:`math.ceil`, and adds a new -one, :func:`math.trunc`, that's been backported to Python 2.6. -:func:`math.trunc` rounds toward zero, returning the closest +one, :func:`math.trunc`, that's been backported to Python 2.6. +:func:`math.trunc` rounds toward zero, returning the closest :class:`Integral` that's between the function's argument and zero. .. seealso:: @@ -1181,7 +1181,7 @@ one, :func:`math.trunc`, that's been backported to Python 2.6. `Scheme's numerical tower `__, from the Guile manual. `Scheme's number datatypes `__ from the R5RS Scheme specification. - + The :mod:`fractions` Module -------------------------------------------------- @@ -1205,8 +1205,8 @@ that will be the numerator and denominator of the resulting fraction. :: >>> a/b Fraction(5, 3) -To help in converting floating-point numbers to rationals, -the float type now has a :meth:`as_integer_ratio()` method that returns +To help in converting floating-point numbers to rationals, +the float type now has a :meth:`as_integer_ratio()` method that returns the numerator and denominator for a fraction that evaluates to the same floating-point value:: @@ -1239,7 +1239,7 @@ Here are all of the changes that Python 2.6 makes to the core Python language. >>> def f(**kw): ... print sorted(kw) - ... + ... >>> ud=UserDict.UserDict() >>> ud['a'] = 1 >>> ud['b'] = 'string' @@ -1264,21 +1264,21 @@ Here are all of the changes that Python 2.6 makes to the core Python language. * Properties now have three attributes, :attr:`getter`, :attr:`setter` and :attr:`deleter`, that are useful shortcuts for - adding or modifying a getter, setter or deleter function to an + adding or modifying a getter, setter or deleter function to an existing property. You would use them like this:: class C(object): - @property - def x(self): - return self._x + @property + def x(self): + return self._x - @x.setter - def x(self, value): - self._x = value + @x.setter + def x(self, value): + self._x = value - @x.deleter - def x(self): - del self._x + @x.deleter + def x(self): + del self._x class D(C): @C.x.getter @@ -1290,22 +1290,22 @@ Here are all of the changes that Python 2.6 makes to the core Python language. self._x = value / 2 -* C functions and methods that use - :cfunc:`PyComplex_AsCComplex` will now accept arguments that - have a :meth:`__complex__` method. In particular, the functions in the +* C functions and methods that use + :cfunc:`PyComplex_AsCComplex` will now accept arguments that + have a :meth:`__complex__` method. In particular, the functions in the :mod:`cmath` module will now accept objects with this method. This is a backport of a Python 3.0 change. (Contributed by Mark Dickinson; :issue:`1675423`.) A numerical nicety: when creating a complex number from two floats - on systems that support signed zeros (-0 and +0), the - :func:`complex` constructor will now preserve the sign + on systems that support signed zeros (-0 and +0), the + :func:`complex` constructor will now preserve the sign of the zero. (:issue:`1507`) * More floating-point features were also added. The :func:`float` function will now turn the strings ``+nan`` and ``-nan`` into the corresponding - IEEE 754 Not A Number values, and ``+inf`` and ``-inf`` into - positive or negative infinity. This works on any platform with + IEEE 754 Not A Number values, and ``+inf`` and ``-inf`` into + positive or negative infinity. This works on any platform with IEEE 754 semantics. (Contributed by Christian Heimes; :issue:`1635`.) Other functions in the :mod:`math` module, :func:`isinf` and @@ -1316,11 +1316,11 @@ Here are all of the changes that Python 2.6 makes to the core Python language. functions have been improved to give more consistent behaviour across platforms, especially with respect to handling of floating-point exceptions and IEEE 754 special values. - The new functions are: + The new functions are: * :func:`isinf` and :func:`isnan` determine whether a given float is a (positive or negative) infinity or a NaN (Not a Number), - respectively. + respectively. * ``copysign(x, y)`` copies the sign bit of an IEEE 754 number, returning the absolute value of *x* combined with the sign bit of @@ -1350,31 +1350,31 @@ Here are all of the changes that Python 2.6 makes to the core Python language. (Contributed by Christian Heimes and Mark Dickinson.) * Changes to the :class:`Exception` interface - as dictated by :pep:`352` continue to be made. For 2.6, + as dictated by :pep:`352` continue to be made. For 2.6, the :attr:`message` attribute is being deprecated in favor of the :attr:`args` attribute. -* The :exc:`GeneratorExit` exception now subclasses - :exc:`BaseException` instead of :exc:`Exception`. This means +* The :exc:`GeneratorExit` exception now subclasses + :exc:`BaseException` instead of :exc:`Exception`. This means that an exception handler that does ``except Exception:`` - will not inadvertently catch :exc:`GeneratorExit`. + will not inadvertently catch :exc:`GeneratorExit`. (Contributed by Chad Austin; :issue:`1537`.) -* Generator objects now have a :attr:`gi_code` attribute that refers to - the original code object backing the generator. +* Generator objects now have a :attr:`gi_code` attribute that refers to + the original code object backing the generator. (Contributed by Collin Winter; :issue:`1473257`.) * The :func:`compile` built-in function now accepts keyword arguments as well as positional parameters. (Contributed by Thomas Wouters; :issue:`1444529`.) -* The :func:`complex` constructor now accepts strings containing +* The :func:`complex` constructor now accepts strings containing parenthesized complex numbers, letting ``complex(repr(cmplx))`` will now round-trip values. For example, ``complex('(3+4j)')`` now returns the value (3+4j). (:issue:`1491866`) -* The string :meth:`translate` method now accepts ``None`` as the - translation table parameter, which is treated as the identity +* The string :meth:`translate` method now accepts ``None`` as the + translation table parameter, which is treated as the identity transformation. This makes it easier to carry out operations that only delete characters. (Contributed by Bengt Richter; :issue:`1193128`.) @@ -1383,7 +1383,7 @@ Here are all of the changes that Python 2.6 makes to the core Python language. method on the objects it receives. This method must return a list of strings containing the names of valid attributes for the object, and lets the object control the value that :func:`dir` produces. - Objects that have :meth:`__getattr__` or :meth:`__getattribute__` + Objects that have :meth:`__getattr__` or :meth:`__getattribute__` methods can use this to advertise pseudo-attributes they will honor. (:issue:`1591665`) @@ -1411,12 +1411,12 @@ Optimizations * Type objects now have a cache of methods that can reduce the amount of work required to find the correct method implementation for a particular class; once cached, the interpreter doesn't need to - traverse base classes to figure out the right method to call. - The cache is cleared if a base class or the class itself is modified, - so the cache should remain correct even in the face of Python's dynamic + traverse base classes to figure out the right method to call. + The cache is cleared if a base class or the class itself is modified, + so the cache should remain correct even in the face of Python's dynamic nature. - (Original optimization implemented by Armin Rigo, updated for - Python 2.6 by Kevin Jacobs; :issue:`1700288`.) + (Original optimization implemented by Armin Rigo, updated for + Python 2.6 by Kevin Jacobs; :issue:`1700288`.) * All of the functions in the :mod:`struct` module have been rewritten in C, thanks to work at the Need For Speed sprint. @@ -1427,7 +1427,7 @@ Optimizations these types. (Contributed by Neal Norwitz.) * Unicode strings now use faster code for detecting - whitespace and line breaks; this speeds up the :meth:`split` method + whitespace and line breaks; this speeds up the :meth:`split` method by about 25% and :meth:`splitlines` by 35%. (Contributed by Antoine Pitrou.) Memory usage is reduced by using pymalloc for the Unicode string's data. @@ -1468,10 +1468,10 @@ by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more complete list of changes, or look through the Subversion logs for all the details. -* (3.0-warning mode) Python 3.0 will feature a reorganized standard +* (3.0-warning mode) Python 3.0 will feature a reorganized standard library; many outdated modules are being dropped, - and some modules are being renamed or moved into packages. - Python 2.6 running in 3.0-warning mode will warn about these modules + and some modules are being renamed or moved into packages. + Python 2.6 running in 3.0-warning mode will warn about these modules when they are imported. The modules that have been renamed are: @@ -1485,6 +1485,8 @@ details. * :mod:`Tkinter` has become the :mod:`tkinter` package. * :mod:`Queue` has become :mod:`queue`. + .. XXX no warnings anymore for renamed modules! + The list of deprecated modules is: :mod:`audiodev`, :mod:`bgenlocations`, @@ -1580,18 +1582,18 @@ details. thanks to Mark Dickinson and Christian Heimes, that added some new features and greatly improved the accuracy of the computations. - Five new functions were added: + Five new functions were added: * :func:`polar` converts a complex number to polar form, returning - the modulus and argument of that complex number. + the modulus and argument of that complex number. * :func:`rect` does the opposite, turning a (modulus, argument) pair back into the corresponding complex number. - * :func:`phase` returns the phase or argument of a complex number. + * :func:`phase` returns the phase or argument of a complex number. * :func:`isnan` returns True if either - the real or imaginary part of its argument is a NaN. + the real or imaginary part of its argument is a NaN. * :func:`isinf` returns True if either the real or imaginary part of its argument is infinite. @@ -1614,7 +1616,7 @@ details. fieldnames)` is a factory function that creates subclasses of the standard tuple whose fields are accessible by name as well as index. For example:: - >>> var_type = collections.namedtuple('variable', + >>> var_type = collections.namedtuple('variable', ... 'id name type size') # Names are separated by spaces or commas. # 'id, name, type, size' would also work. @@ -1633,15 +1635,15 @@ details. variable(id=1, name='amplitude', type='int', size=4) Where the new :class:`namedtuple` type proved suitable, the standard - library has been modified to return them. For example, - the :meth:`Decimal.as_tuple` method now returns a named tuple with + library has been modified to return them. For example, + the :meth:`Decimal.as_tuple` method now returns a named tuple with :attr:`sign`, :attr:`digits`, and :attr:`exponent` fields. (Contributed by Raymond Hettinger.) -* Another change to the :mod:`collections` module is that the +* Another change to the :mod:`collections` module is that the :class:`deque` type now supports an optional *maxlen* parameter; - if supplied, the deque's size will be restricted to no more + if supplied, the deque's size will be restricted to no more than *maxlen* items. Adding more items to a full deque causes old items to be discarded. @@ -1660,7 +1662,7 @@ details. (Contributed by Raymond Hettinger.) -* The :mod:`ctypes` module now supports a :class:`c_bool` datatype +* The :mod:`ctypes` module now supports a :class:`c_bool` datatype that represents the C99 ``bool`` type. (Contributed by David Remahl; :issue:`1649190`.) @@ -1676,9 +1678,9 @@ details. (Contributed by Fabian Kreutz.) :: - # Boldface text starting at y=0,x=21 + # Boldface text starting at y=0,x=21 # and affecting the rest of the line. - stdscr.chgat(0,21, curses.A_BOLD) + stdscr.chgat(0,21, curses.A_BOLD) The :class:`Textbox` class in the :mod:`curses.textpad` module now supports editing in insert mode as well as overwrite mode. @@ -1690,7 +1692,7 @@ details. object, zero-padded on the left to six places. (Contributed by Skip Montanaro; :issue:`1158`.) -* The :mod:`decimal` module was updated to version 1.66 of +* The :mod:`decimal` module was updated to version 1.66 of `the General Decimal Specification `__. New features include some methods for some basic mathematical functions such as :meth:`exp` and :meth:`log10`:: @@ -1702,14 +1704,14 @@ details. >>> Decimal(1000).log10() Decimal("3") - The :meth:`as_tuple` method of :class:`Decimal` objects now returns a + The :meth:`as_tuple` method of :class:`Decimal` objects now returns a named tuple with :attr:`sign`, :attr:`digits`, and :attr:`exponent` fields. - + (Implemented by Facundo Batista and Mark Dickinson. Named tuple support added by Raymond Hettinger.) -* The :mod:`difflib` module's :class:`SequenceMatcher` class - now returns named tuples representing matches. +* The :mod:`difflib` module's :class:`SequenceMatcher` class + now returns named tuples representing matches. In addition to behaving like tuples, the returned values also have :attr:`a`, :attr:`b`, and :attr:`size` attributes. (Contributed by Raymond Hettinger.) @@ -1717,25 +1719,25 @@ details. * An optional ``timeout`` parameter was added to the :class:`ftplib.FTP` class constructor as well as the :meth:`connect` method, specifying a timeout measured in seconds. (Added by Facundo - Batista.) Also, the :class:`FTP` class's + Batista.) Also, the :class:`FTP` class's :meth:`storbinary` and :meth:`storlines` - now take an optional *callback* parameter that will be called with + now take an optional *callback* parameter that will be called with each block of data after the data has been sent. (Contributed by Phil Schwartz; :issue:`1221598`.) -* The :func:`reduce` built-in function is also available in the +* The :func:`reduce` built-in function is also available in the :mod:`functools` module. In Python 3.0, the built-in is dropped and it's only available from :mod:`functools`; currently there are no plans - to drop the built-in in the 2.x series. (Patched by + to drop the built-in in the 2.x series. (Patched by Christian Heimes; :issue:`1739906`.) -* The :func:`glob.glob` function can now return Unicode filenames if +* The :func:`glob.glob` function can now return Unicode filenames if a Unicode path was used and Unicode filenames are matched within the directory. (:issue:`1001604`) * The :mod:`gopherlib` module has been removed. -* A new function in the :mod:`heapq` module: ``merge(iter1, iter2, ...)`` +* A new function in the :mod:`heapq` module: ``merge(iter1, iter2, ...)`` takes any number of iterables that return data *in sorted order*, and returns a new iterator that returns the contents of all the iterators, also in sorted order. For example:: @@ -1744,25 +1746,25 @@ details. [1, 2, 3, 5, 8, 9, 16] Another new function, ``heappushpop(heap, item)``, - pushes *item* onto *heap*, then pops off and returns the smallest item. + pushes *item* onto *heap*, then pops off and returns the smallest item. This is more efficient than making a call to :func:`heappush` and then :func:`heappop`. (Contributed by Raymond Hettinger.) * An optional ``timeout`` parameter was added to the - :class:`httplib.HTTPConnection` and :class:`HTTPSConnection` + :class:`httplib.HTTPConnection` and :class:`HTTPSConnection` class constructors, specifying a timeout measured in seconds. (Added by Facundo Batista.) -* Most of the :mod:`inspect` module's functions, such as - :func:`getmoduleinfo` and :func:`getargs`, now return named tuples. +* Most of the :mod:`inspect` module's functions, such as + :func:`getmoduleinfo` and :func:`getargs`, now return named tuples. In addition to behaving like tuples, the elements of the return value can also be accessed as attributes. (Contributed by Raymond Hettinger.) - Some new functions in the module include - :func:`isgenerator`, :func:`isgeneratorfunction`, + Some new functions in the module include + :func:`isgenerator`, :func:`isgeneratorfunction`, and :func:`isabstract`. * The :mod:`itertools` module gained several new functions. @@ -1779,25 +1781,25 @@ details. every possible combination of the elements returned from each iterable. :: itertools.product([1,2,3], [4,5,6]) -> - [(1, 4), (1, 5), (1, 6), - (2, 4), (2, 5), (2, 6), + [(1, 4), (1, 5), (1, 6), + (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)] The optional *repeat* keyword argument is used for taking the - product of an iterable or a set of iterables with themselves, + product of an iterable or a set of iterables with themselves, repeated *N* times. With a single iterable argument, *N*-tuples are returned:: itertools.product([1,2], repeat=3)) -> - [(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), + [(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)] With two iterables, *2N*-tuples are returned. :: itertools(product([1,2], [3,4], repeat=2) -> - [(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4), - (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4), - (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4), + [(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4), + (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4), + (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4), (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)] ``combinations(iterable, r)`` returns sub-sequences of length *r* from @@ -1810,35 +1812,35 @@ details. [('1', '2', '3')] itertools.combinations('1234', 3) -> - [('1', '2', '3'), ('1', '2', '4'), ('1', '3', '4'), + [('1', '2', '3'), ('1', '2', '4'), ('1', '3', '4'), ('2', '3', '4')] ``permutations(iter[, r])`` returns all the permutations of length *r* of - the iterable's elements. If *r* is not specified, it will default to the + the iterable's elements. If *r* is not specified, it will default to the number of elements produced by the iterable. :: itertools.permutations([1,2,3,4], 2) -> - [(1, 2), (1, 3), (1, 4), - (2, 1), (2, 3), (2, 4), - (3, 1), (3, 2), (3, 4), + [(1, 2), (1, 3), (1, 4), + (2, 1), (2, 3), (2, 4), + (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)] ``itertools.chain(*iterables)`` is an existing function in :mod:`itertools` that gained a new constructor in Python 2.6. - ``itertools.chain.from_iterable(iterable)`` takes a single + ``itertools.chain.from_iterable(iterable)`` takes a single iterable that should return other iterables. :func:`chain` will then return all the elements of the first iterable, then all the elements of the second, and so on. :: chain.from_iterable([[1,2,3], [4,5,6]]) -> [1, 2, 3, 4, 5, 6] - + (All contributed by Raymond Hettinger.) -* The :mod:`logging` module's :class:`FileHandler` class +* The :mod:`logging` module's :class:`FileHandler` class and its subclasses :class:`WatchedFileHandler`, :class:`RotatingFileHandler`, - and :class:`TimedRotatingFileHandler` now - have an optional *delay* parameter to its constructor. If *delay* + and :class:`TimedRotatingFileHandler` now + have an optional *delay* parameter to its constructor. If *delay* is true, opening of the log file is deferred until the first :meth:`emit` call is made. (Contributed by Vinay Sajip.) @@ -1853,16 +1855,16 @@ details. the forward search. (Contributed by John Lenton.) -* The :mod:`operator` module gained a - :func:`methodcaller` function that takes a name and an optional - set of arguments, returning a callable that will call +* The :mod:`operator` module gained a + :func:`methodcaller` function that takes a name and an optional + set of arguments, returning a callable that will call the named function on any arguments passed to it. For example:: >>> # Equivalent to lambda s: s.replace('old', 'new') >>> replacer = operator.methodcaller('replace', 'old', 'new') >>> replacer('old wine in old bottles') 'new wine in new bottles' - + (Contributed by Georg Brandl, after a suggestion by Gregory Petrosyan.) The :func:`attrgetter` function now accepts dotted names and performs @@ -1876,8 +1878,8 @@ details. (Contributed by Georg Brandl, after a suggestion by Barry Warsaw.) -* New functions in the :mod:`os` module include - ``fchmod(fd, mode)``, ``fchown(fd, uid, gid)``, +* New functions in the :mod:`os` module include + ``fchmod(fd, mode)``, ``fchown(fd, uid, gid)``, and ``lchmod(path, mode)``, on operating systems that support these functions. :func:`fchmod` and :func:`fchown` let you change the mode and ownership of an opened file, and :func:`lchmod` changes the mode @@ -1891,8 +1893,8 @@ details. parameter's default value is false. Note that the function can fall into an infinite recursion if there's a symlink that points to a parent directory. (:issue:`1273829`) - -* The ``os.environ`` object's :meth:`clear` method will now unset the + +* The ``os.environ`` object's :meth:`clear` method will now unset the environment variables using :func:`os.unsetenv` in addition to clearing the object's keys. (Contributed by Martin Horcicka; :issue:`1181`.) @@ -1908,23 +1910,23 @@ details. working directory to the destination ``path``. (Contributed by Richard Barran; :issue:`1339796`.) - On Windows, :func:`os.path.expandvars` will now expand environment variables - in the form "%var%", and "~user" will be expanded into the + On Windows, :func:`os.path.expandvars` will now expand environment variables + in the form "%var%", and "~user" will be expanded into the user's home directory path. (Contributed by Josiah Carlson; :issue:`957650`.) -* The Python debugger provided by the :mod:`pdb` module +* The Python debugger provided by the :mod:`pdb` module gained a new command: "run" restarts the Python program being debugged, and can optionally take new command-line arguments for the program. (Contributed by Rocky Bernstein; :issue:`1393667`.) - The :func:`post_mortem` function, used to enter debugging of a + The :func:`post_mortem` function, used to enter debugging of a traceback, will now use the traceback returned by :func:`sys.exc_info` if no traceback is supplied. (Contributed by Facundo Batista; :issue:`1106316`.) -* The :mod:`pickletools` module now has an :func:`optimize` function - that takes a string containing a pickle and removes some unused +* The :mod:`pickletools` module now has an :func:`optimize` function + that takes a string containing a pickle and removes some unused opcodes, returning a shorter pickle that contains the same data structure. (Contributed by Raymond Hettinger.) @@ -1942,7 +1944,7 @@ details. +-- StopIteration +-- StandardError ...' - >>> + >>> (Contributed by Paul Moore; :issue:`2439`.) @@ -1959,13 +1961,13 @@ details. processes faster. (Contributed by Georg Brandl; :issue:`1663329`.) * The :mod:`pyexpat` module's :class:`Parser` objects now allow setting - their :attr:`buffer_size` attribute to change the size of the buffer + their :attr:`buffer_size` attribute to change the size of the buffer used to hold character data. (Contributed by Achim Gaedke; :issue:`1137`.) * The :mod:`queue` module now provides queue classes that retrieve entries - in different orders. The :class:`PriorityQueue` class stores - queued items in a heap and retrieves them in priority order, + in different orders. The :class:`PriorityQueue` class stores + queued items in a heap and retrieves them in priority order, and :class:`LifoQueue` retrieves the most recently added entries first, meaning that it behaves like a stack. (Contributed by Raymond Hettinger.) @@ -1979,8 +1981,8 @@ details. The new ``triangular(low, high, mode)`` function returns random numbers following a triangular distribution. The returned values - are between *low* and *high*, not including *high* itself, and - with *mode* as the mode, the most frequently occurring value + are between *low* and *high*, not including *high* itself, and + with *mode* as the mode, the most frequently occurring value in the distribution. (Contributed by Wladmir van der Laan and Raymond Hettinger; :issue:`1681432`.) @@ -1991,8 +1993,8 @@ details. * The :mod:`rgbimg` module has been removed. -* The :mod:`sched` module's :class:`scheduler` instances now - have a read-only :attr:`queue` attribute that returns the +* The :mod:`sched` module's :class:`scheduler` instances now + have a read-only :attr:`queue` attribute that returns the contents of the scheduler's queue, represented as a list of named tuples with the fields ``(time, priority, action, argument)``. (Contributed by Raymond Hettinger; :issue:`1861`.) @@ -2001,19 +2003,19 @@ details. for the Linux :cfunc:`epoll` and BSD :cfunc:`kqueue` system calls. Also, a :meth:`modify` method was added to the existing :class:`poll` objects; ``pollobj.modify(fd, eventmask)`` takes a file descriptor - or file object and an event mask, - + or file object and an event mask, + (Contributed by Christian Heimes; :issue:`1657`.) -* The :mod:`sets` module has been deprecated; it's better to +* The :mod:`sets` module has been deprecated; it's better to use the built-in :class:`set` and :class:`frozenset` types. -* Integrating signal handling with GUI handling event loops +* Integrating signal handling with GUI handling event loops like those used by Tkinter or GTk+ has long been a problem; most software ends up polling, waking up every fraction of a second. The :mod:`signal` module can now make this more efficient. Calling ``signal.set_wakeup_fd(fd)`` sets a file descriptor - to be used; when a signal is received, a byte is written to that + to be used; when a signal is received, a byte is written to that file descriptor. There's also a C-level function, :cfunc:`PySignal_SetWakeupFd`, for setting the descriptor. @@ -2022,7 +2024,7 @@ details. will be passed to :func:`set_wakeup_fd`, and the readable descriptor will be added to the list of descriptors monitored by the event loop via :cfunc:`select` or :cfunc:`poll`. - On receiving a signal, a byte will be written and the main event loop + On receiving a signal, a byte will be written and the main event loop will be woken up, without the need to poll. (Contributed by Adam Olsen; :issue:`1583`.) @@ -2040,7 +2042,7 @@ details. * The :mod:`smtplib` module now supports SMTP over SSL thanks to the addition of the :class:`SMTP_SSL` class. This class supports an - interface identical to the existing :class:`SMTP` class. Both + interface identical to the existing :class:`SMTP` class. Both class constructors also have an optional ``timeout`` parameter that specifies a timeout for the initial connection attempt, measured in seconds. @@ -2063,35 +2065,35 @@ details. environments. TIPC addresses are 4- or 5-tuples. (Contributed by Alberto Bertogli; :issue:`1646`.) - A new function, :func:`create_connection`, takes an address - and connects to it using an optional timeout value, returning + A new function, :func:`create_connection`, takes an address + and connects to it using an optional timeout value, returning the connected socket object. -* The base classes in the :mod:`socketserver` module now support - calling a :meth:`handle_timeout` method after a span of inactivity - specified by the server's :attr:`timeout` attribute. (Contributed - by Michael Pomraning.) The :meth:`serve_forever` method +* The base classes in the :mod:`SocketServer` module now support + calling a :meth:`handle_timeout` method after a span of inactivity + specified by the server's :attr:`timeout` attribute. (Contributed + by Michael Pomraning.) The :meth:`serve_forever` method now takes an optional poll interval measured in seconds, controlling how often the server will check for a shutdown request. - (Contributed by Pedro Werneck and Jeffrey Yasskin; + (Contributed by Pedro Werneck and Jeffrey Yasskin; :issue:`742598`, :issue:`1193577`.) * The :mod:`struct` module now supports the C99 :ctype:`_Bool` type, - using the format character ``'?'``. + using the format character ``'?'``. (Contributed by David Remahl.) * The :class:`Popen` objects provided by the :mod:`subprocess` module now have :meth:`terminate`, :meth:`kill`, and :meth:`send_signal` methods. On Windows, :meth:`send_signal` only supports the :const:`SIGTERM` signal, and all these methods are aliases for the Win32 API function - :cfunc:`TerminateProcess`. + :cfunc:`TerminateProcess`. (Contributed by Christian Heimes.) - + * A new variable in the :mod:`sys` module, :attr:`float_info`, is an object containing information about the platform's floating-point support derived from the :file:`float.h` file. Attributes of this object - include + include :attr:`mant_dig` (number of digits in the mantissa), :attr:`epsilon` (smallest difference between 1.0 and the next largest value representable), and several others. (Contributed by Christian Heimes; @@ -2103,25 +2105,25 @@ details. variable is initially set on start-up by supplying the :option:`-B` switch to the Python interpreter, or by setting the :envvar:`PYTHONDONTWRITEBYTECODE` environment variable before - running the interpreter. Python code can subsequently + running the interpreter. Python code can subsequently change the value of this variable to control whether bytecode files are written or not. (Contributed by Neal Norwitz and Georg Brandl.) - Information about the command-line arguments supplied to the Python - interpreter are available as attributes of a ``sys.flags`` named - tuple. For example, the :attr:`verbose` attribute is true if Python + Information about the command-line arguments supplied to the Python + interpreter are available as attributes of a ``sys.flags`` named + tuple. For example, the :attr:`verbose` attribute is true if Python was executed in verbose mode, :attr:`debug` is true in debugging mode, etc. These attributes are all read-only. (Contributed by Christian Heimes.) It's now possible to determine the current profiler and tracer functions - by calling :func:`sys.getprofile` and :func:`sys.gettrace`. + by calling :func:`sys.getprofile` and :func:`sys.gettrace`. (Contributed by Georg Brandl; :issue:`1648`.) * The :mod:`tarfile` module now supports POSIX.1-2001 (pax) and POSIX.1-1988 (ustar) format tarfiles, in addition to the GNU tar - format that was already supported. The default format + format that was already supported. The default format is GNU tar; specify the ``format`` parameter to open a file using a different format:: @@ -2136,37 +2138,37 @@ details. The :meth:`TarFile.add` method now accepts a ``exclude`` argument that's a function that can be used to exclude certain filenames from - an archive. - The function must take a filename and return true if the file + an archive. + The function must take a filename and return true if the file should be excluded or false if it should be archived. The function is applied to both the name initially passed to :meth:`add` and to the names of files in recursively-added directories. - + (All changes contributed by Lars Gustäbel). * An optional ``timeout`` parameter was added to the :class:`telnetlib.Telnet` class constructor, specifying a timeout measured in seconds. (Added by Facundo Batista.) -* The :class:`tempfile.NamedTemporaryFile` class usually deletes - the temporary file it created when the file is closed. This - behaviour can now be changed by passing ``delete=False`` to the +* The :class:`tempfile.NamedTemporaryFile` class usually deletes + the temporary file it created when the file is closed. This + behaviour can now be changed by passing ``delete=False`` to the constructor. (Contributed by Damien Miller; :issue:`1537850`.) - A new class, :class:`SpooledTemporaryFile`, behaves like - a temporary file but stores its data in memory until a maximum size is - exceeded. On reaching that limit, the contents will be written to + A new class, :class:`SpooledTemporaryFile`, behaves like + a temporary file but stores its data in memory until a maximum size is + exceeded. On reaching that limit, the contents will be written to an on-disk temporary file. (Contributed by Dustin J. Mitchell.) The :class:`NamedTemporaryFile` and :class:`SpooledTemporaryFile` classes - both work as context managers, so you can write + both work as context managers, so you can write ``with tempfile.NamedTemporaryFile() as tmp: ...``. (Contributed by Alexander Belopolsky; :issue:`2021`.) * The :mod:`test.test_support` module now contains a :func:`EnvironmentVarGuard` context manager that supports temporarily changing environment variables and - automatically restores them to their old values. + automatically restores them to their old values. Another context manager, :class:`TransientResource`, can surround calls to resources that may or may not be available; it will catch and @@ -2175,12 +2177,12 @@ details. external web site:: with test_support.TransientResource(IOError, errno=errno.ETIMEDOUT): - f = urllib.urlopen('https://sf.net') + f = urllib.urlopen('https://sf.net') ... (Contributed by Brett Cannon.) -* The :mod:`textwrap` module can now preserve existing whitespace +* The :mod:`textwrap` module can now preserve existing whitespace at the beginnings and ends of the newly-created lines by specifying ``drop_whitespace=False`` as an argument:: @@ -2196,22 +2198,22 @@ details. has a bunch of extra whitespace. - >>> + >>> (Contributed by Dwayne Bailey; :issue:`1581073`.) -* The :mod:`timeit` module now accepts callables as well as strings +* The :mod:`timeit` module now accepts callables as well as strings for the statement being timed and for the setup code. - Two convenience functions were added for creating - :class:`Timer` instances: - ``repeat(stmt, setup, time, repeat, number)`` and + Two convenience functions were added for creating + :class:`Timer` instances: + ``repeat(stmt, setup, time, repeat, number)`` and ``timeit(stmt, setup, time, number)`` create an instance and call the corresponding method. (Contributed by Erik Demaine; :issue:`1533909`.) * An optional ``timeout`` parameter was added to the :func:`urllib.urlopen` function and the - :class:`urllib.ftpwrapper` class constructor, as well as the + :class:`urllib.ftpwrapper` class constructor, as well as the :func:`urllib2.urlopen` function. The parameter specifies a timeout measured in seconds. For example:: @@ -2219,11 +2221,11 @@ details. Traceback (most recent call last): ... urllib2.URLError: - >>> + >>> - (Added by Facundo Batista.) + (Added by Facundo Batista.) -* The :mod:`warnings` module's :func:`formatwarning` and :func:`showwarning` +* The :mod:`warnings` module's :func:`formatwarning` and :func:`showwarning` gained an optional *line* argument that can be used to supply the line of source code. (Added as part of :issue:`1631171`, which re-implemented part of the :mod:`warnings` module in C code.) @@ -2232,30 +2234,30 @@ details. classes can now be prevented from immediately opening and binding to their socket by passing True as the ``bind_and_activate`` constructor parameter. This can be used to modify the instance's - :attr:`allow_reuse_address` attribute before calling the - :meth:`server_bind` and :meth:`server_activate` methods to + :attr:`allow_reuse_address` attribute before calling the + :meth:`server_bind` and :meth:`server_activate` methods to open the socket and begin listening for connections. (Contributed by Peter Parente; :issue:`1599845`.) :class:`SimpleXMLRPCServer` also has a :attr:`_send_traceback_header` - attribute; if true, the exception and formatted traceback are returned - as HTTP headers "X-Exception" and "X-Traceback". This feature is + attribute; if true, the exception and formatted traceback are returned + as HTTP headers "X-Exception" and "X-Traceback". This feature is for debugging purposes only and should not be used on production servers because the tracebacks could possibly reveal passwords or other sensitive - information. (Contributed by Alan McIntyre as part of his + information. (Contributed by Alan McIntyre as part of his project for Google's Summer of Code 2007.) * The :mod:`xmlrpclib` module no longer automatically converts - :class:`datetime.date` and :class:`datetime.time` to the + :class:`datetime.date` and :class:`datetime.time` to the :class:`xmlrpclib.DateTime` type; the conversion semantics were not necessarily correct for all applications. Code using - :mod:`xmlrpclib` should convert :class:`date` and :class:`time` - instances. (:issue:`1330538`) The code can also handle + :mod:`xmlrpclib` should convert :class:`date` and :class:`time` + instances. (:issue:`1330538`) The code can also handle dates before 1900. (Contributed by Ralf Schmitt; :issue:`2014`.) -* The :mod:`zipfile` module's :class:`ZipFile` class now has - :meth:`extract` and :meth:`extractall` methods that will unpack - a single file or all the files in the archive to the current directory, or +* The :mod:`zipfile` module's :class:`ZipFile` class now has + :meth:`extract` and :meth:`extractall` methods that will unpack + a single file or all the files in the archive to the current directory, or to a specified directory:: z = zipfile.ZipFile('python-251.zip') @@ -2326,12 +2328,12 @@ obtain certificate info by calling the :meth:`getpeercert` method. plistlib: A Property-List Parser -------------------------------------------------- -A commonly-used format on MacOS X is the ``.plist`` format, -which stores basic data types (numbers, strings, lists, +A commonly-used format on MacOS X is the ``.plist`` format, +which stores basic data types (numbers, strings, lists, and dictionaries) and serializes them into an XML-based format. (It's a lot like the XML-RPC serialization of data types.) -Despite being primarily used on MacOS X, the format +Despite being primarily used on MacOS X, the format has nothing Mac-specific about it and the Python implementation works on any platform that Python supports, so the :mod:`plistlib` module has been promoted to the standard library. @@ -2359,7 +2361,7 @@ Using the module is simple:: # read/writePlist accepts file-like objects as well as paths. plistlib.writePlist(data_struct, sys.stdout) - + .. ====================================================================== @@ -2378,25 +2380,25 @@ Changes to Python's build process and to the C API include: own implementations of :cfunc:`memmove` and :cfunc:`strerror`, which are in the C89 standard library. -* The BerkeleyDB module now has a C API object, available as +* The BerkeleyDB module now has a C API object, available as ``bsddb.db.api``. This object can be used by other C extensions that wish to use the :mod:`bsddb` module for their own purposes. (Contributed by Duncan Grisby; :issue:`1551895`.) -* The new buffer interface, previously described in +* The new buffer interface, previously described in `the PEP 3118 section <#pep-3118-revised-buffer-protocol>`__, adds :cfunc:`PyObject_GetBuffer` and :cfunc:`PyObject_ReleaseBuffer`, as well as a few other functions. * Python's use of the C stdio library is now thread-safe, or at least as thread-safe as the underlying library is. A long-standing potential - bug occurred if one thread closed a file object while another thread - was reading from or writing to the object. In 2.6 file objects - have a reference count, manipulated by the + bug occurred if one thread closed a file object while another thread + was reading from or writing to the object. In 2.6 file objects + have a reference count, manipulated by the :cfunc:`PyFile_IncUseCount` and :cfunc:`PyFile_DecUseCount` - functions. File objects can't be closed unless the reference count - is zero. :cfunc:`PyFile_IncUseCount` should be called while the GIL - is still held, before carrying out an I/O operation using the + functions. File objects can't be closed unless the reference count + is zero. :cfunc:`PyFile_IncUseCount` should be called while the GIL + is still held, before carrying out an I/O operation using the ``FILE *`` pointer, and :cfunc:`PyFile_DecUseCount` should be called immediately after the GIL is re-acquired. (Contributed by Antoine Pitrou and Gregory P. Smith.) @@ -2409,11 +2411,11 @@ Changes to Python's build process and to the C API include: thread, the :exc:`ImportError` is raised. (Contributed by Christian Heimes.) -* Several functions return information about the platform's +* Several functions return information about the platform's floating-point support. :cfunc:`PyFloat_GetMax` returns the maximum representable floating point value, - and :cfunc:`PyFloat_GetMin` returns the minimum - positive value. :cfunc:`PyFloat_GetInfo` returns a dictionary + and :cfunc:`PyFloat_GetMin` returns the minimum + positive value. :cfunc:`PyFloat_GetInfo` returns a dictionary containing more information from the :file:`float.h` file, such as ``"mant_dig"`` (number of digits in the mantissa), ``"epsilon"`` (smallest difference between 1.0 and the next largest value @@ -2425,23 +2427,23 @@ Changes to Python's build process and to the C API include: and ``PyOS_strnicmp(char*, char*, Py_ssize_t)``. (Contributed by Christian Heimes; :issue:`1635`.) -* Many C extensions define their own little macro for adding - integers and strings to the module's dictionary in the - ``init*`` function. Python 2.6 finally defines standard macros +* Many C extensions define their own little macro for adding + integers and strings to the module's dictionary in the + ``init*`` function. Python 2.6 finally defines standard macros for adding values to a module, :cmacro:`PyModule_AddStringMacro` - and :cmacro:`PyModule_AddIntMacro()`. (Contributed by + and :cmacro:`PyModule_AddIntMacro()`. (Contributed by Christian Heimes.) * Some macros were renamed in both 3.0 and 2.6 to make it clearer that they are macros, not functions. :cmacro:`Py_Size()` became :cmacro:`Py_SIZE()`, :cmacro:`Py_Type()` became :cmacro:`Py_TYPE()`, and - :cmacro:`Py_Refcnt()` became :cmacro:`Py_REFCNT()`. + :cmacro:`Py_Refcnt()` became :cmacro:`Py_REFCNT()`. The mixed-case macros are still available in Python 2.6 for backward compatibility. (:issue:`1629`) -* Distutils now places C extensions it builds in a +* Distutils now places C extensions it builds in a different directory when running on a debug version of Python. (Contributed by Collin Winter; :issue:`1530959`.) @@ -2453,7 +2455,7 @@ Changes to Python's build process and to the C API include: always defined. * A new Makefile target, "make check", prepares the Python source tree - for making a patch: it fixes trailing whitespace in all modified + for making a patch: it fixes trailing whitespace in all modified ``.py`` files, checks whether the documentation has been changed, and reports whether the :file:`Misc/ACKS` and :file:`Misc/NEWS` files have been updated. @@ -2475,35 +2477,35 @@ Port-Specific Changes: Windows * The support for Windows 95, 98, ME and NT4 has been dropped. Python 2.6 requires at least Windows 2000 SP4. -* The :mod:`msvcrt` module now supports +* The :mod:`msvcrt` module now supports both the normal and wide char variants of the console I/O - API. The :func:`getwch` function reads a keypress and returns a Unicode + API. The :func:`getwch` function reads a keypress and returns a Unicode value, as does the :func:`getwche` function. The :func:`putwch` function takes a Unicode character and writes it to the console. (Contributed by Christian Heimes.) -* :func:`os.path.expandvars` will now expand environment variables - in the form "%var%", and "~user" will be expanded into the +* :func:`os.path.expandvars` will now expand environment variables + in the form "%var%", and "~user" will be expanded into the user's home directory path. (Contributed by Josiah Carlson.) -* The :mod:`socket` module's socket objects now have an - :meth:`ioctl` method that provides a limited interface to the +* The :mod:`socket` module's socket objects now have an + :meth:`ioctl` method that provides a limited interface to the :cfunc:`WSAIoctl` system interface. -* The :mod:`_winreg` module now has a function, - :func:`ExpandEnvironmentStrings`, +* The :mod:`_winreg` module now has a function, + :func:`ExpandEnvironmentStrings`, that expands environment variable references such as ``%NAME%`` in an input string. The handle objects provided by this - module now support the context protocol, so they can be used + module now support the context protocol, so they can be used in :keyword:`with` statements. (Contributed by Christian Heimes.) - :mod:`_winreg` also has better support for x64 systems, + :mod:`_winreg` also has better support for x64 systems, exposing the :func:`DisableReflectionKey`, :func:`EnableReflectionKey`, and :func:`QueryReflectionKey` functions, which enable and disable registry reflection for 32-bit processes running on 64-bit systems. (:issue:`1753245`) -* The new default compiler on Windows is Visual Studio 2008 (VS 9.0). The +* The new default compiler on Windows is Visual Studio 2008 (VS 9.0). The build directories for Visual Studio 2003 (VS7.1) and 2005 (VS8.0) were moved into the PC/ directory. The new PCbuild directory supports cross compilation for X64, debug builds and Profile Guided Optimization @@ -2526,7 +2528,7 @@ Python 2.5 and 2.6. Both figures are likely to be underestimates. Some of the more notable changes are: -* It's now possible to prevent Python from writing any :file:`.pyc` +* It's now possible to prevent Python from writing any :file:`.pyc` or :file:`.pyo` files by either supplying the :option:`-B` switch or setting the :envvar:`PYTHONDONTWRITEBYTECODE` environment variable to any non-empty string when running the Python interpreter. These @@ -2547,23 +2549,23 @@ that may require changes to your code: * The :meth:`__init__` method of :class:`collections.deque` now clears any existing contents of the deque before adding elements from the iterable. This change makes the - behavior match that of ``list.__init__()``. + behavior match that of ``list.__init__()``. -* The :class:`Decimal` constructor now accepts leading and trailing +* The :class:`Decimal` constructor now accepts leading and trailing whitespace when passed a string. Previously it would raise an :exc:`InvalidOperation` exception. On the other hand, the :meth:`create_decimal` method of :class:`Context` objects now - explicitly disallows extra whitespace, raising a + explicitly disallows extra whitespace, raising a :exc:`ConversionSyntax` exception. -* Due to an implementation accident, if you passed a file path to +* Due to an implementation accident, if you passed a file path to the built-in :func:`__import__` function, it would actually import - the specified file. This was never intended to work, however, and - the implementation now explicitly checks for this case and raises + the specified file. This was never intended to work, however, and + the implementation now explicitly checks for this case and raises an :exc:`ImportError`. * C API: the :cfunc:`PyImport_Import` and :cfunc:`PyImport_ImportModule` - functions now default to absolute imports, not relative imports. + functions now default to absolute imports, not relative imports. This will affect C extensions that import other modules. * The :mod:`socket` module exception :exc:`socket.error` now inherits @@ -2572,21 +2574,21 @@ that may require changes to your code: (Implemented by Gregory P. Smith; :issue:`1706815`.) * The :mod:`xmlrpclib` module no longer automatically converts - :class:`datetime.date` and :class:`datetime.time` to the + :class:`datetime.date` and :class:`datetime.time` to the :class:`xmlrpclib.DateTime` type; the conversion semantics were not necessarily correct for all applications. Code using - :mod:`xmlrpclib` should convert :class:`date` and :class:`time` + :mod:`xmlrpclib` should convert :class:`date` and :class:`time` instances. (:issue:`1330538`) -* (3.0-warning mode) The :class:`Exception` class now warns - when accessed using slicing or index access; having +* (3.0-warning mode) The :class:`Exception` class now warns + when accessed using slicing or index access; having :class:`Exception` behave like a tuple is being phased out. * (3.0-warning mode) inequality comparisons between two dictionaries or two objects that don't implement comparison methods are reported as warnings. ``dict1 == dict2`` still works, but ``dict1 < dict2`` is being phased out. - + Comparisons between cells, which are an implementation detail of Python's scoping rules, also cause warnings because such comparisons are forbidden entirely in 3.0. @@ -2600,6 +2602,6 @@ Acknowledgements ================ The author would like to thank the following people for offering suggestions, -corrections and assistance with various drafts of this article: +corrections and assistance with various drafts of this article: Georg Brandl, Jim Jewett. diff --git a/Lib/BaseHTTPServer.py b/Lib/BaseHTTPServer.py index cc244a7..5f2d558 100644 --- a/Lib/BaseHTTPServer.py +++ b/Lib/BaseHTTPServer.py @@ -74,7 +74,7 @@ import sys import time import socket # For gethostbyaddr() import mimetools -import socketserver +import SocketServer # Default error message template DEFAULT_ERROR_MESSAGE = """\ @@ -94,19 +94,19 @@ DEFAULT_ERROR_CONTENT_TYPE = "text/html" def _quote_html(html): return html.replace("&", "&").replace("<", "<").replace(">", ">") -class HTTPServer(socketserver.TCPServer): +class HTTPServer(SocketServer.TCPServer): allow_reuse_address = 1 # Seems to make sense in testing environment def server_bind(self): """Override server_bind to store the server name.""" - socketserver.TCPServer.server_bind(self) + SocketServer.TCPServer.server_bind(self) host, port = self.socket.getsockname()[:2] self.server_name = socket.getfqdn(host) self.server_port = port -class BaseHTTPRequestHandler(socketserver.StreamRequestHandler): +class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler): """HTTP request handler base class. diff --git a/Lib/SimpleXMLRPCServer.py b/Lib/SimpleXMLRPCServer.py index 353542a..5fad0af 100644 --- a/Lib/SimpleXMLRPCServer.py +++ b/Lib/SimpleXMLRPCServer.py @@ -101,7 +101,7 @@ server.handle_request() import xmlrpclib from xmlrpclib import Fault -import socketserver +import SocketServer import BaseHTTPServer import sys import os @@ -512,7 +512,7 @@ class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): if self.server.logRequests: BaseHTTPServer.BaseHTTPRequestHandler.log_request(self, code, size) -class SimpleXMLRPCServer(socketserver.TCPServer, +class SimpleXMLRPCServer(SocketServer.TCPServer, SimpleXMLRPCDispatcher): """Simple XML-RPC server. @@ -536,7 +536,7 @@ class SimpleXMLRPCServer(socketserver.TCPServer, self.logRequests = logRequests SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding) - socketserver.TCPServer.__init__(self, addr, requestHandler, bind_and_activate) + SocketServer.TCPServer.__init__(self, addr, requestHandler, bind_and_activate) # [Bug #1222790] If possible, set close-on-exec flag; if a # method spawns a subprocess, the subprocess shouldn't have diff --git a/Lib/SocketServer.py b/Lib/SocketServer.py new file mode 100644 index 0000000..2c41fbb --- /dev/null +++ b/Lib/SocketServer.py @@ -0,0 +1,681 @@ +"""Generic socket server classes. + +This module tries to capture the various aspects of defining a server: + +For socket-based servers: + +- address family: + - AF_INET{,6}: IP (Internet Protocol) sockets (default) + - AF_UNIX: Unix domain sockets + - others, e.g. AF_DECNET are conceivable (see +- socket type: + - SOCK_STREAM (reliable stream, e.g. TCP) + - SOCK_DGRAM (datagrams, e.g. UDP) + +For request-based servers (including socket-based): + +- client address verification before further looking at the request + (This is actually a hook for any processing that needs to look + at the request before anything else, e.g. logging) +- how to handle multiple requests: + - synchronous (one request is handled at a time) + - forking (each request is handled by a new process) + - threading (each request is handled by a new thread) + +The classes in this module favor the server type that is simplest to +write: a synchronous TCP/IP server. This is bad class design, but +save some typing. (There's also the issue that a deep class hierarchy +slows down method lookups.) + +There are five classes in an inheritance diagram, four of which represent +synchronous servers of four types: + + +------------+ + | BaseServer | + +------------+ + | + v + +-----------+ +------------------+ + | TCPServer |------->| UnixStreamServer | + +-----------+ +------------------+ + | + v + +-----------+ +--------------------+ + | UDPServer |------->| UnixDatagramServer | + +-----------+ +--------------------+ + +Note that UnixDatagramServer derives from UDPServer, not from +UnixStreamServer -- the only difference between an IP and a Unix +stream server is the address family, which is simply repeated in both +unix server classes. + +Forking and threading versions of each type of server can be created +using the ForkingMixIn and ThreadingMixIn mix-in classes. For +instance, a threading UDP server class is created as follows: + + class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass + +The Mix-in class must come first, since it overrides a method defined +in UDPServer! Setting the various member variables also changes +the behavior of the underlying server mechanism. + +To implement a service, you must derive a class from +BaseRequestHandler and redefine its handle() method. You can then run +various versions of the service by combining one of the server classes +with your request handler class. + +The request handler class must be different for datagram or stream +services. This can be hidden by using the request handler +subclasses StreamRequestHandler or DatagramRequestHandler. + +Of course, you still have to use your head! + +For instance, it makes no sense to use a forking server if the service +contains state in memory that can be modified by requests (since the +modifications in the child process would never reach the initial state +kept in the parent process and passed to each child). In this case, +you can use a threading server, but you will probably have to use +locks to avoid two requests that come in nearly simultaneous to apply +conflicting changes to the server state. + +On the other hand, if you are building e.g. an HTTP server, where all +data is stored externally (e.g. in the file system), a synchronous +class will essentially render the service "deaf" while one request is +being handled -- which may be for a very long time if a client is slow +to reqd all the data it has requested. Here a threading or forking +server is appropriate. + +In some cases, it may be appropriate to process part of a request +synchronously, but to finish processing in a forked child depending on +the request data. This can be implemented by using a synchronous +server and doing an explicit fork in the request handler class +handle() method. + +Another approach to handling multiple simultaneous requests in an +environment that supports neither threads nor fork (or where these are +too expensive or inappropriate for the service) is to maintain an +explicit table of partially finished requests and to use select() to +decide which request to work on next (or whether to handle a new +incoming request). This is particularly important for stream services +where each client can potentially be connected for a long time (if +threads or subprocesses cannot be used). + +Future work: +- Standard classes for Sun RPC (which uses either UDP or TCP) +- Standard mix-in classes to implement various authentication + and encryption schemes +- Standard framework for select-based multiplexing + +XXX Open problems: +- What to do with out-of-band data? + +BaseServer: +- split generic "request" functionality out into BaseServer class. + Copyright (C) 2000 Luke Kenneth Casson Leighton + + example: read entries from a SQL database (requires overriding + get_request() to return a table entry from the database). + entry is processed by a RequestHandlerClass. + +""" + +# Author of the BaseServer patch: Luke Kenneth Casson Leighton + +# XXX Warning! +# There is a test suite for this module, but it cannot be run by the +# standard regression test. +# To run it manually, run Lib/test/test_socketserver.py. + +__version__ = "0.4" + + +import socket +import select +import sys +import os +try: + import threading +except ImportError: + import dummy_threading as threading + +__all__ = ["TCPServer","UDPServer","ForkingUDPServer","ForkingTCPServer", + "ThreadingUDPServer","ThreadingTCPServer","BaseRequestHandler", + "StreamRequestHandler","DatagramRequestHandler", + "ThreadingMixIn", "ForkingMixIn"] +if hasattr(socket, "AF_UNIX"): + __all__.extend(["UnixStreamServer","UnixDatagramServer", + "ThreadingUnixStreamServer", + "ThreadingUnixDatagramServer"]) + +class BaseServer: + + """Base class for server classes. + + Methods for the caller: + + - __init__(server_address, RequestHandlerClass) + - serve_forever(poll_interval=0.5) + - shutdown() + - handle_request() # if you do not use serve_forever() + - fileno() -> int # for select() + + Methods that may be overridden: + + - server_bind() + - server_activate() + - get_request() -> request, client_address + - handle_timeout() + - verify_request(request, client_address) + - server_close() + - process_request(request, client_address) + - close_request(request) + - handle_error() + + Methods for derived classes: + + - finish_request(request, client_address) + + Class variables that may be overridden by derived classes or + instances: + + - timeout + - address_family + - socket_type + - allow_reuse_address + + Instance variables: + + - RequestHandlerClass + - socket + + """ + + timeout = None + + def __init__(self, server_address, RequestHandlerClass): + """Constructor. May be extended, do not override.""" + self.server_address = server_address + self.RequestHandlerClass = RequestHandlerClass + self.__is_shut_down = threading.Event() + self.__serving = False + + def server_activate(self): + """Called by constructor to activate the server. + + May be overridden. + + """ + pass + + def serve_forever(self, poll_interval=0.5): + """Handle one request at a time until shutdown. + + Polls for shutdown every poll_interval seconds. Ignores + self.timeout. If you need to do periodic tasks, do them in + another thread. + """ + self.__serving = True + self.__is_shut_down.clear() + while self.__serving: + # XXX: Consider using another file descriptor or + # connecting to the socket to wake this up instead of + # polling. Polling reduces our responsiveness to a + # shutdown request and wastes cpu at all other times. + r, w, e = select.select([self], [], [], poll_interval) + if r: + self._handle_request_noblock() + self.__is_shut_down.set() + + def shutdown(self): + """Stops the serve_forever loop. + + Blocks until the loop has finished. This must be called while + serve_forever() is running in another thread, or it will + deadlock. + """ + self.__serving = False + self.__is_shut_down.wait() + + # The distinction between handling, getting, processing and + # finishing a request is fairly arbitrary. Remember: + # + # - handle_request() is the top-level call. It calls + # select, get_request(), verify_request() and process_request() + # - get_request() is different for stream or datagram sockets + # - process_request() is the place that may fork a new process + # or create a new thread to finish the request + # - finish_request() instantiates the request handler class; + # this constructor will handle the request all by itself + + def handle_request(self): + """Handle one request, possibly blocking. + + Respects self.timeout. + """ + # Support people who used socket.settimeout() to escape + # handle_request before self.timeout was available. + timeout = self.socket.gettimeout() + if timeout is None: + timeout = self.timeout + elif self.timeout is not None: + timeout = min(timeout, self.timeout) + fd_sets = select.select([self], [], [], timeout) + if not fd_sets[0]: + self.handle_timeout() + return + self._handle_request_noblock() + + def _handle_request_noblock(self): + """Handle one request, without blocking. + + I assume that select.select has returned that the socket is + readable before this function was called, so there should be + no risk of blocking in get_request(). + """ + try: + request, client_address = self.get_request() + except socket.error: + return + if self.verify_request(request, client_address): + try: + self.process_request(request, client_address) + except: + self.handle_error(request, client_address) + self.close_request(request) + + def handle_timeout(self): + """Called if no new request arrives within self.timeout. + + Overridden by ForkingMixIn. + """ + pass + + def verify_request(self, request, client_address): + """Verify the request. May be overridden. + + Return True if we should proceed with this request. + + """ + return True + + def process_request(self, request, client_address): + """Call finish_request. + + Overridden by ForkingMixIn and ThreadingMixIn. + + """ + self.finish_request(request, client_address) + self.close_request(request) + + def server_close(self): + """Called to clean-up the server. + + May be overridden. + + """ + pass + + def finish_request(self, request, client_address): + """Finish one request by instantiating RequestHandlerClass.""" + self.RequestHandlerClass(request, client_address, self) + + def close_request(self, request): + """Called to clean up an individual request.""" + pass + + def handle_error(self, request, client_address): + """Handle an error gracefully. May be overridden. + + The default is to print a traceback and continue. + + """ + print '-'*40 + print 'Exception happened during processing of request from', + print client_address + import traceback + traceback.print_exc() # XXX But this goes to stderr! + print '-'*40 + + +class TCPServer(BaseServer): + + """Base class for various socket-based server classes. + + Defaults to synchronous IP stream (i.e., TCP). + + Methods for the caller: + + - __init__(server_address, RequestHandlerClass, bind_and_activate=True) + - serve_forever(poll_interval=0.5) + - shutdown() + - handle_request() # if you don't use serve_forever() + - fileno() -> int # for select() + + Methods that may be overridden: + + - server_bind() + - server_activate() + - get_request() -> request, client_address + - handle_timeout() + - verify_request(request, client_address) + - process_request(request, client_address) + - close_request(request) + - handle_error() + + Methods for derived classes: + + - finish_request(request, client_address) + + Class variables that may be overridden by derived classes or + instances: + + - timeout + - address_family + - socket_type + - request_queue_size (only for stream sockets) + - allow_reuse_address + + Instance variables: + + - server_address + - RequestHandlerClass + - socket + + """ + + address_family = socket.AF_INET + + socket_type = socket.SOCK_STREAM + + request_queue_size = 5 + + allow_reuse_address = False + + def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True): + """Constructor. May be extended, do not override.""" + BaseServer.__init__(self, server_address, RequestHandlerClass) + self.socket = socket.socket(self.address_family, + self.socket_type) + if bind_and_activate: + self.server_bind() + self.server_activate() + + def server_bind(self): + """Called by constructor to bind the socket. + + May be overridden. + + """ + if self.allow_reuse_address: + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.socket.bind(self.server_address) + self.server_address = self.socket.getsockname() + + def server_activate(self): + """Called by constructor to activate the server. + + May be overridden. + + """ + self.socket.listen(self.request_queue_size) + + def server_close(self): + """Called to clean-up the server. + + May be overridden. + + """ + self.socket.close() + + def fileno(self): + """Return socket file number. + + Interface required by select(). + + """ + return self.socket.fileno() + + def get_request(self): + """Get the request and client address from the socket. + + May be overridden. + + """ + return self.socket.accept() + + def close_request(self, request): + """Called to clean up an individual request.""" + request.close() + + +class UDPServer(TCPServer): + + """UDP server class.""" + + allow_reuse_address = False + + socket_type = socket.SOCK_DGRAM + + max_packet_size = 8192 + + def get_request(self): + data, client_addr = self.socket.recvfrom(self.max_packet_size) + return (data, self.socket), client_addr + + def server_activate(self): + # No need to call listen() for UDP. + pass + + def close_request(self, request): + # No need to close anything. + pass + +class ForkingMixIn: + + """Mix-in class to handle each request in a new process.""" + + timeout = 300 + active_children = None + max_children = 40 + + def collect_children(self): + """Internal routine to wait for children that have exited.""" + if self.active_children is None: return + while len(self.active_children) >= self.max_children: + # XXX: This will wait for any child process, not just ones + # spawned by this library. This could confuse other + # libraries that expect to be able to wait for their own + # children. + try: + pid, status = os.waitpid(0, options=0) + except os.error: + pid = None + if pid not in self.active_children: continue + self.active_children.remove(pid) + + # XXX: This loop runs more system calls than it ought + # to. There should be a way to put the active_children into a + # process group and then use os.waitpid(-pgid) to wait for any + # of that set, but I couldn't find a way to allocate pgids + # that couldn't collide. + for child in self.active_children: + try: + pid, status = os.waitpid(child, os.WNOHANG) + except os.error: + pid = None + if not pid: continue + try: + self.active_children.remove(pid) + except ValueError, e: + raise ValueError('%s. x=%d and list=%r' % (e.message, pid, + self.active_children)) + + def handle_timeout(self): + """Wait for zombies after self.timeout seconds of inactivity. + + May be extended, do not override. + """ + self.collect_children() + + def process_request(self, request, client_address): + """Fork a new subprocess to process the request.""" + self.collect_children() + pid = os.fork() + if pid: + # Parent process + if self.active_children is None: + self.active_children = [] + self.active_children.append(pid) + self.close_request(request) + return + else: + # Child process. + # This must never return, hence os._exit()! + try: + self.finish_request(request, client_address) + os._exit(0) + except: + try: + self.handle_error(request, client_address) + finally: + os._exit(1) + + +class ThreadingMixIn: + """Mix-in class to handle each request in a new thread.""" + + # Decides how threads will act upon termination of the + # main process + daemon_threads = False + + def process_request_thread(self, request, client_address): + """Same as in BaseServer but as a thread. + + In addition, exception handling is done here. + + """ + try: + self.finish_request(request, client_address) + self.close_request(request) + except: + self.handle_error(request, client_address) + self.close_request(request) + + def process_request(self, request, client_address): + """Start a new thread to process the request.""" + t = threading.Thread(target = self.process_request_thread, + args = (request, client_address)) + if self.daemon_threads: + t.setDaemon (1) + t.start() + + +class ForkingUDPServer(ForkingMixIn, UDPServer): pass +class ForkingTCPServer(ForkingMixIn, TCPServer): pass + +class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass +class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass + +if hasattr(socket, 'AF_UNIX'): + + class UnixStreamServer(TCPServer): + address_family = socket.AF_UNIX + + class UnixDatagramServer(UDPServer): + address_family = socket.AF_UNIX + + class ThreadingUnixStreamServer(ThreadingMixIn, UnixStreamServer): pass + + class ThreadingUnixDatagramServer(ThreadingMixIn, UnixDatagramServer): pass + +class BaseRequestHandler: + + """Base class for request handler classes. + + This class is instantiated for each request to be handled. The + constructor sets the instance variables request, client_address + and server, and then calls the handle() method. To implement a + specific service, all you need to do is to derive a class which + defines a handle() method. + + The handle() method can find the request as self.request, the + client address as self.client_address, and the server (in case it + needs access to per-server information) as self.server. Since a + separate instance is created for each request, the handle() method + can define arbitrary other instance variariables. + + """ + + def __init__(self, request, client_address, server): + self.request = request + self.client_address = client_address + self.server = server + try: + self.setup() + self.handle() + self.finish() + finally: + sys.exc_traceback = None # Help garbage collection + + def setup(self): + pass + + def handle(self): + pass + + def finish(self): + pass + + +# The following two classes make it possible to use the same service +# class for stream or datagram servers. +# Each class sets up these instance variables: +# - rfile: a file object from which receives the request is read +# - wfile: a file object to which the reply is written +# When the handle() method returns, wfile is flushed properly + + +class StreamRequestHandler(BaseRequestHandler): + + """Define self.rfile and self.wfile for stream sockets.""" + + # Default buffer sizes for rfile, wfile. + # We default rfile to buffered because otherwise it could be + # really slow for large data (a getc() call per byte); we make + # wfile unbuffered because (a) often after a write() we want to + # read and we need to flush the line; (b) big writes to unbuffered + # files are typically optimized by stdio even when big reads + # aren't. + rbufsize = -1 + wbufsize = 0 + + def setup(self): + self.connection = self.request + self.rfile = self.connection.makefile('rb', self.rbufsize) + self.wfile = self.connection.makefile('wb', self.wbufsize) + + def finish(self): + if not self.wfile.closed: + self.wfile.flush() + self.wfile.close() + self.rfile.close() + + +class DatagramRequestHandler(BaseRequestHandler): + + # XXX Regrettably, I cannot get this working on Linux; + # s.recvfrom() doesn't return a meaningful client address. + + """Define self.rfile and self.wfile for datagram sockets.""" + + def setup(self): + try: + from cStringIO import StringIO + except ImportError: + from StringIO import StringIO + self.packet, self.socket = self.request + self.rfile = StringIO(self.packet) + self.wfile = StringIO() + + def finish(self): + self.socket.sendto(self.wfile.getvalue(), self.client_address) diff --git a/Lib/idlelib/rpc.py b/Lib/idlelib/rpc.py index e0bcf2c..cdfdac4 100644 --- a/Lib/idlelib/rpc.py +++ b/Lib/idlelib/rpc.py @@ -5,7 +5,7 @@ connect to the Idle process, which listens for the connection. Since Idle has has only one client per server, this was not a limitation. +---------------------------------+ +-------------+ - | socketserver.BaseRequestHandler | | SocketIO | + | SocketServer.BaseRequestHandler | | SocketIO | +---------------------------------+ +-------------+ ^ | register() | | | unregister()| @@ -31,7 +31,7 @@ import sys import os import socket import select -import socketserver +import SocketServer import struct import cPickle as pickle import threading @@ -66,12 +66,12 @@ copy_reg.pickle(types.CodeType, pickle_code, unpickle_code) BUFSIZE = 8*1024 LOCALHOST = '127.0.0.1' -class RPCServer(socketserver.TCPServer): +class RPCServer(SocketServer.TCPServer): def __init__(self, addr, handlerclass=None): if handlerclass is None: handlerclass = RPCHandler - socketserver.TCPServer.__init__(self, addr, handlerclass) + SocketServer.TCPServer.__init__(self, addr, handlerclass) def server_bind(self): "Override TCPServer method, no bind() phase for connecting entity" @@ -492,7 +492,7 @@ class RemoteProxy(object): def __init__(self, oid): self.oid = oid -class RPCHandler(socketserver.BaseRequestHandler, SocketIO): +class RPCHandler(SocketServer.BaseRequestHandler, SocketIO): debugging = False location = "#S" # Server @@ -500,10 +500,10 @@ class RPCHandler(socketserver.BaseRequestHandler, SocketIO): def __init__(self, sock, addr, svr): svr.current_handler = self ## cgt xxx SocketIO.__init__(self, sock) - socketserver.BaseRequestHandler.__init__(self, sock, addr, svr) + SocketServer.BaseRequestHandler.__init__(self, sock, addr, svr) def handle(self): - "handle() method required by socketserver" + "handle() method required by SocketServer" self.mainloop() def get_remote_proxy(self, oid): diff --git a/Lib/lib-old/SocketServer.py b/Lib/lib-old/SocketServer.py deleted file mode 100644 index 2c41fbb..0000000 --- a/Lib/lib-old/SocketServer.py +++ /dev/null @@ -1,681 +0,0 @@ -"""Generic socket server classes. - -This module tries to capture the various aspects of defining a server: - -For socket-based servers: - -- address family: - - AF_INET{,6}: IP (Internet Protocol) sockets (default) - - AF_UNIX: Unix domain sockets - - others, e.g. AF_DECNET are conceivable (see -- socket type: - - SOCK_STREAM (reliable stream, e.g. TCP) - - SOCK_DGRAM (datagrams, e.g. UDP) - -For request-based servers (including socket-based): - -- client address verification before further looking at the request - (This is actually a hook for any processing that needs to look - at the request before anything else, e.g. logging) -- how to handle multiple requests: - - synchronous (one request is handled at a time) - - forking (each request is handled by a new process) - - threading (each request is handled by a new thread) - -The classes in this module favor the server type that is simplest to -write: a synchronous TCP/IP server. This is bad class design, but -save some typing. (There's also the issue that a deep class hierarchy -slows down method lookups.) - -There are five classes in an inheritance diagram, four of which represent -synchronous servers of four types: - - +------------+ - | BaseServer | - +------------+ - | - v - +-----------+ +------------------+ - | TCPServer |------->| UnixStreamServer | - +-----------+ +------------------+ - | - v - +-----------+ +--------------------+ - | UDPServer |------->| UnixDatagramServer | - +-----------+ +--------------------+ - -Note that UnixDatagramServer derives from UDPServer, not from -UnixStreamServer -- the only difference between an IP and a Unix -stream server is the address family, which is simply repeated in both -unix server classes. - -Forking and threading versions of each type of server can be created -using the ForkingMixIn and ThreadingMixIn mix-in classes. For -instance, a threading UDP server class is created as follows: - - class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass - -The Mix-in class must come first, since it overrides a method defined -in UDPServer! Setting the various member variables also changes -the behavior of the underlying server mechanism. - -To implement a service, you must derive a class from -BaseRequestHandler and redefine its handle() method. You can then run -various versions of the service by combining one of the server classes -with your request handler class. - -The request handler class must be different for datagram or stream -services. This can be hidden by using the request handler -subclasses StreamRequestHandler or DatagramRequestHandler. - -Of course, you still have to use your head! - -For instance, it makes no sense to use a forking server if the service -contains state in memory that can be modified by requests (since the -modifications in the child process would never reach the initial state -kept in the parent process and passed to each child). In this case, -you can use a threading server, but you will probably have to use -locks to avoid two requests that come in nearly simultaneous to apply -conflicting changes to the server state. - -On the other hand, if you are building e.g. an HTTP server, where all -data is stored externally (e.g. in the file system), a synchronous -class will essentially render the service "deaf" while one request is -being handled -- which may be for a very long time if a client is slow -to reqd all the data it has requested. Here a threading or forking -server is appropriate. - -In some cases, it may be appropriate to process part of a request -synchronously, but to finish processing in a forked child depending on -the request data. This can be implemented by using a synchronous -server and doing an explicit fork in the request handler class -handle() method. - -Another approach to handling multiple simultaneous requests in an -environment that supports neither threads nor fork (or where these are -too expensive or inappropriate for the service) is to maintain an -explicit table of partially finished requests and to use select() to -decide which request to work on next (or whether to handle a new -incoming request). This is particularly important for stream services -where each client can potentially be connected for a long time (if -threads or subprocesses cannot be used). - -Future work: -- Standard classes for Sun RPC (which uses either UDP or TCP) -- Standard mix-in classes to implement various authentication - and encryption schemes -- Standard framework for select-based multiplexing - -XXX Open problems: -- What to do with out-of-band data? - -BaseServer: -- split generic "request" functionality out into BaseServer class. - Copyright (C) 2000 Luke Kenneth Casson Leighton - - example: read entries from a SQL database (requires overriding - get_request() to return a table entry from the database). - entry is processed by a RequestHandlerClass. - -""" - -# Author of the BaseServer patch: Luke Kenneth Casson Leighton - -# XXX Warning! -# There is a test suite for this module, but it cannot be run by the -# standard regression test. -# To run it manually, run Lib/test/test_socketserver.py. - -__version__ = "0.4" - - -import socket -import select -import sys -import os -try: - import threading -except ImportError: - import dummy_threading as threading - -__all__ = ["TCPServer","UDPServer","ForkingUDPServer","ForkingTCPServer", - "ThreadingUDPServer","ThreadingTCPServer","BaseRequestHandler", - "StreamRequestHandler","DatagramRequestHandler", - "ThreadingMixIn", "ForkingMixIn"] -if hasattr(socket, "AF_UNIX"): - __all__.extend(["UnixStreamServer","UnixDatagramServer", - "ThreadingUnixStreamServer", - "ThreadingUnixDatagramServer"]) - -class BaseServer: - - """Base class for server classes. - - Methods for the caller: - - - __init__(server_address, RequestHandlerClass) - - serve_forever(poll_interval=0.5) - - shutdown() - - handle_request() # if you do not use serve_forever() - - fileno() -> int # for select() - - Methods that may be overridden: - - - server_bind() - - server_activate() - - get_request() -> request, client_address - - handle_timeout() - - verify_request(request, client_address) - - server_close() - - process_request(request, client_address) - - close_request(request) - - handle_error() - - Methods for derived classes: - - - finish_request(request, client_address) - - Class variables that may be overridden by derived classes or - instances: - - - timeout - - address_family - - socket_type - - allow_reuse_address - - Instance variables: - - - RequestHandlerClass - - socket - - """ - - timeout = None - - def __init__(self, server_address, RequestHandlerClass): - """Constructor. May be extended, do not override.""" - self.server_address = server_address - self.RequestHandlerClass = RequestHandlerClass - self.__is_shut_down = threading.Event() - self.__serving = False - - def server_activate(self): - """Called by constructor to activate the server. - - May be overridden. - - """ - pass - - def serve_forever(self, poll_interval=0.5): - """Handle one request at a time until shutdown. - - Polls for shutdown every poll_interval seconds. Ignores - self.timeout. If you need to do periodic tasks, do them in - another thread. - """ - self.__serving = True - self.__is_shut_down.clear() - while self.__serving: - # XXX: Consider using another file descriptor or - # connecting to the socket to wake this up instead of - # polling. Polling reduces our responsiveness to a - # shutdown request and wastes cpu at all other times. - r, w, e = select.select([self], [], [], poll_interval) - if r: - self._handle_request_noblock() - self.__is_shut_down.set() - - def shutdown(self): - """Stops the serve_forever loop. - - Blocks until the loop has finished. This must be called while - serve_forever() is running in another thread, or it will - deadlock. - """ - self.__serving = False - self.__is_shut_down.wait() - - # The distinction between handling, getting, processing and - # finishing a request is fairly arbitrary. Remember: - # - # - handle_request() is the top-level call. It calls - # select, get_request(), verify_request() and process_request() - # - get_request() is different for stream or datagram sockets - # - process_request() is the place that may fork a new process - # or create a new thread to finish the request - # - finish_request() instantiates the request handler class; - # this constructor will handle the request all by itself - - def handle_request(self): - """Handle one request, possibly blocking. - - Respects self.timeout. - """ - # Support people who used socket.settimeout() to escape - # handle_request before self.timeout was available. - timeout = self.socket.gettimeout() - if timeout is None: - timeout = self.timeout - elif self.timeout is not None: - timeout = min(timeout, self.timeout) - fd_sets = select.select([self], [], [], timeout) - if not fd_sets[0]: - self.handle_timeout() - return - self._handle_request_noblock() - - def _handle_request_noblock(self): - """Handle one request, without blocking. - - I assume that select.select has returned that the socket is - readable before this function was called, so there should be - no risk of blocking in get_request(). - """ - try: - request, client_address = self.get_request() - except socket.error: - return - if self.verify_request(request, client_address): - try: - self.process_request(request, client_address) - except: - self.handle_error(request, client_address) - self.close_request(request) - - def handle_timeout(self): - """Called if no new request arrives within self.timeout. - - Overridden by ForkingMixIn. - """ - pass - - def verify_request(self, request, client_address): - """Verify the request. May be overridden. - - Return True if we should proceed with this request. - - """ - return True - - def process_request(self, request, client_address): - """Call finish_request. - - Overridden by ForkingMixIn and ThreadingMixIn. - - """ - self.finish_request(request, client_address) - self.close_request(request) - - def server_close(self): - """Called to clean-up the server. - - May be overridden. - - """ - pass - - def finish_request(self, request, client_address): - """Finish one request by instantiating RequestHandlerClass.""" - self.RequestHandlerClass(request, client_address, self) - - def close_request(self, request): - """Called to clean up an individual request.""" - pass - - def handle_error(self, request, client_address): - """Handle an error gracefully. May be overridden. - - The default is to print a traceback and continue. - - """ - print '-'*40 - print 'Exception happened during processing of request from', - print client_address - import traceback - traceback.print_exc() # XXX But this goes to stderr! - print '-'*40 - - -class TCPServer(BaseServer): - - """Base class for various socket-based server classes. - - Defaults to synchronous IP stream (i.e., TCP). - - Methods for the caller: - - - __init__(server_address, RequestHandlerClass, bind_and_activate=True) - - serve_forever(poll_interval=0.5) - - shutdown() - - handle_request() # if you don't use serve_forever() - - fileno() -> int # for select() - - Methods that may be overridden: - - - server_bind() - - server_activate() - - get_request() -> request, client_address - - handle_timeout() - - verify_request(request, client_address) - - process_request(request, client_address) - - close_request(request) - - handle_error() - - Methods for derived classes: - - - finish_request(request, client_address) - - Class variables that may be overridden by derived classes or - instances: - - - timeout - - address_family - - socket_type - - request_queue_size (only for stream sockets) - - allow_reuse_address - - Instance variables: - - - server_address - - RequestHandlerClass - - socket - - """ - - address_family = socket.AF_INET - - socket_type = socket.SOCK_STREAM - - request_queue_size = 5 - - allow_reuse_address = False - - def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True): - """Constructor. May be extended, do not override.""" - BaseServer.__init__(self, server_address, RequestHandlerClass) - self.socket = socket.socket(self.address_family, - self.socket_type) - if bind_and_activate: - self.server_bind() - self.server_activate() - - def server_bind(self): - """Called by constructor to bind the socket. - - May be overridden. - - """ - if self.allow_reuse_address: - self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.socket.bind(self.server_address) - self.server_address = self.socket.getsockname() - - def server_activate(self): - """Called by constructor to activate the server. - - May be overridden. - - """ - self.socket.listen(self.request_queue_size) - - def server_close(self): - """Called to clean-up the server. - - May be overridden. - - """ - self.socket.close() - - def fileno(self): - """Return socket file number. - - Interface required by select(). - - """ - return self.socket.fileno() - - def get_request(self): - """Get the request and client address from the socket. - - May be overridden. - - """ - return self.socket.accept() - - def close_request(self, request): - """Called to clean up an individual request.""" - request.close() - - -class UDPServer(TCPServer): - - """UDP server class.""" - - allow_reuse_address = False - - socket_type = socket.SOCK_DGRAM - - max_packet_size = 8192 - - def get_request(self): - data, client_addr = self.socket.recvfrom(self.max_packet_size) - return (data, self.socket), client_addr - - def server_activate(self): - # No need to call listen() for UDP. - pass - - def close_request(self, request): - # No need to close anything. - pass - -class ForkingMixIn: - - """Mix-in class to handle each request in a new process.""" - - timeout = 300 - active_children = None - max_children = 40 - - def collect_children(self): - """Internal routine to wait for children that have exited.""" - if self.active_children is None: return - while len(self.active_children) >= self.max_children: - # XXX: This will wait for any child process, not just ones - # spawned by this library. This could confuse other - # libraries that expect to be able to wait for their own - # children. - try: - pid, status = os.waitpid(0, options=0) - except os.error: - pid = None - if pid not in self.active_children: continue - self.active_children.remove(pid) - - # XXX: This loop runs more system calls than it ought - # to. There should be a way to put the active_children into a - # process group and then use os.waitpid(-pgid) to wait for any - # of that set, but I couldn't find a way to allocate pgids - # that couldn't collide. - for child in self.active_children: - try: - pid, status = os.waitpid(child, os.WNOHANG) - except os.error: - pid = None - if not pid: continue - try: - self.active_children.remove(pid) - except ValueError, e: - raise ValueError('%s. x=%d and list=%r' % (e.message, pid, - self.active_children)) - - def handle_timeout(self): - """Wait for zombies after self.timeout seconds of inactivity. - - May be extended, do not override. - """ - self.collect_children() - - def process_request(self, request, client_address): - """Fork a new subprocess to process the request.""" - self.collect_children() - pid = os.fork() - if pid: - # Parent process - if self.active_children is None: - self.active_children = [] - self.active_children.append(pid) - self.close_request(request) - return - else: - # Child process. - # This must never return, hence os._exit()! - try: - self.finish_request(request, client_address) - os._exit(0) - except: - try: - self.handle_error(request, client_address) - finally: - os._exit(1) - - -class ThreadingMixIn: - """Mix-in class to handle each request in a new thread.""" - - # Decides how threads will act upon termination of the - # main process - daemon_threads = False - - def process_request_thread(self, request, client_address): - """Same as in BaseServer but as a thread. - - In addition, exception handling is done here. - - """ - try: - self.finish_request(request, client_address) - self.close_request(request) - except: - self.handle_error(request, client_address) - self.close_request(request) - - def process_request(self, request, client_address): - """Start a new thread to process the request.""" - t = threading.Thread(target = self.process_request_thread, - args = (request, client_address)) - if self.daemon_threads: - t.setDaemon (1) - t.start() - - -class ForkingUDPServer(ForkingMixIn, UDPServer): pass -class ForkingTCPServer(ForkingMixIn, TCPServer): pass - -class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass -class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass - -if hasattr(socket, 'AF_UNIX'): - - class UnixStreamServer(TCPServer): - address_family = socket.AF_UNIX - - class UnixDatagramServer(UDPServer): - address_family = socket.AF_UNIX - - class ThreadingUnixStreamServer(ThreadingMixIn, UnixStreamServer): pass - - class ThreadingUnixDatagramServer(ThreadingMixIn, UnixDatagramServer): pass - -class BaseRequestHandler: - - """Base class for request handler classes. - - This class is instantiated for each request to be handled. The - constructor sets the instance variables request, client_address - and server, and then calls the handle() method. To implement a - specific service, all you need to do is to derive a class which - defines a handle() method. - - The handle() method can find the request as self.request, the - client address as self.client_address, and the server (in case it - needs access to per-server information) as self.server. Since a - separate instance is created for each request, the handle() method - can define arbitrary other instance variariables. - - """ - - def __init__(self, request, client_address, server): - self.request = request - self.client_address = client_address - self.server = server - try: - self.setup() - self.handle() - self.finish() - finally: - sys.exc_traceback = None # Help garbage collection - - def setup(self): - pass - - def handle(self): - pass - - def finish(self): - pass - - -# The following two classes make it possible to use the same service -# class for stream or datagram servers. -# Each class sets up these instance variables: -# - rfile: a file object from which receives the request is read -# - wfile: a file object to which the reply is written -# When the handle() method returns, wfile is flushed properly - - -class StreamRequestHandler(BaseRequestHandler): - - """Define self.rfile and self.wfile for stream sockets.""" - - # Default buffer sizes for rfile, wfile. - # We default rfile to buffered because otherwise it could be - # really slow for large data (a getc() call per byte); we make - # wfile unbuffered because (a) often after a write() we want to - # read and we need to flush the line; (b) big writes to unbuffered - # files are typically optimized by stdio even when big reads - # aren't. - rbufsize = -1 - wbufsize = 0 - - def setup(self): - self.connection = self.request - self.rfile = self.connection.makefile('rb', self.rbufsize) - self.wfile = self.connection.makefile('wb', self.wbufsize) - - def finish(self): - if not self.wfile.closed: - self.wfile.flush() - self.wfile.close() - self.rfile.close() - - -class DatagramRequestHandler(BaseRequestHandler): - - # XXX Regrettably, I cannot get this working on Linux; - # s.recvfrom() doesn't return a meaningful client address. - - """Define self.rfile and self.wfile for datagram sockets.""" - - def setup(self): - try: - from cStringIO import StringIO - except ImportError: - from StringIO import StringIO - self.packet, self.socket = self.request - self.rfile = StringIO(self.packet) - self.wfile = StringIO() - - def finish(self): - self.socket.sendto(self.wfile.getvalue(), self.client_address) diff --git a/Lib/logging/config.py b/Lib/logging/config.py index b4c52b4..dc13723 100644 --- a/Lib/logging/config.py +++ b/Lib/logging/config.py @@ -35,7 +35,7 @@ try: except ImportError: thread = None -from socketserver import ThreadingTCPServer, StreamRequestHandler +from SocketServer import ThreadingTCPServer, StreamRequestHandler DEFAULT_LOGGING_CONFIG_PORT = 9030 diff --git a/Lib/test/test___all__.py b/Lib/test/test___all__.py index 846a2f6..ec58c14 100644 --- a/Lib/test/test___all__.py +++ b/Lib/test/test___all__.py @@ -42,7 +42,7 @@ class AllTest(unittest.TestCase): self.check_all("MimeWriter") self.check_all("queue") self.check_all("SimpleHTTPServer") - self.check_all("socketserver") + self.check_all("SocketServer") self.check_all("StringIO") self.check_all("UserString") self.check_all("aifc") diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 97e1af3..b937411 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -33,7 +33,7 @@ import os import re import select import socket -from socketserver import ThreadingTCPServer, StreamRequestHandler +from SocketServer import ThreadingTCPServer, StreamRequestHandler import string import struct import sys diff --git a/Lib/test/test_py3kwarn.py b/Lib/test/test_py3kwarn.py index b12d14d..5a9421f 100644 --- a/Lib/test/test_py3kwarn.py +++ b/Lib/test/test_py3kwarn.py @@ -216,7 +216,6 @@ class TestStdlibRemovals(unittest.TestCase): class TestStdlibRenames(unittest.TestCase): renames = {'Queue': 'queue', - 'SocketServer': 'socketserver', 'ConfigParser': 'configparser', } diff --git a/Lib/test/test_socketserver.py b/Lib/test/test_socketserver.py index 4e26e7b..8595e5a 100644 --- a/Lib/test/test_socketserver.py +++ b/Lib/test/test_socketserver.py @@ -1,5 +1,5 @@ """ -Test suite for socketserver. +Test suite for SocketServer.py. """ import contextlib @@ -13,7 +13,7 @@ import tempfile import threading import time import unittest -import socketserver +import SocketServer import test.test_support from test.test_support import reap_children, verbose, TestSkipped @@ -40,12 +40,12 @@ def receive(sock, n, timeout=20): raise RuntimeError, "timed out on %r" % (sock,) if HAVE_UNIX_SOCKETS: - class ForkingUnixStreamServer(socketserver.ForkingMixIn, - socketserver.UnixStreamServer): + class ForkingUnixStreamServer(SocketServer.ForkingMixIn, + SocketServer.UnixStreamServer): pass - class ForkingUnixDatagramServer(socketserver.ForkingMixIn, - socketserver.UnixDatagramServer): + class ForkingUnixDatagramServer(SocketServer.ForkingMixIn, + SocketServer.UnixDatagramServer): pass @@ -172,55 +172,55 @@ class SocketServerTest(unittest.TestCase): s.close() def test_TCPServer(self): - self.run_server(socketserver.TCPServer, - socketserver.StreamRequestHandler, + self.run_server(SocketServer.TCPServer, + SocketServer.StreamRequestHandler, self.stream_examine) def test_ThreadingTCPServer(self): - self.run_server(socketserver.ThreadingTCPServer, - socketserver.StreamRequestHandler, + self.run_server(SocketServer.ThreadingTCPServer, + SocketServer.StreamRequestHandler, self.stream_examine) if HAVE_FORKING: def test_ForkingTCPServer(self): with simple_subprocess(self): - self.run_server(socketserver.ForkingTCPServer, - socketserver.StreamRequestHandler, + self.run_server(SocketServer.ForkingTCPServer, + SocketServer.StreamRequestHandler, self.stream_examine) if HAVE_UNIX_SOCKETS: def test_UnixStreamServer(self): - self.run_server(socketserver.UnixStreamServer, - socketserver.StreamRequestHandler, + self.run_server(SocketServer.UnixStreamServer, + SocketServer.StreamRequestHandler, self.stream_examine) def test_ThreadingUnixStreamServer(self): - self.run_server(socketserver.ThreadingUnixStreamServer, - socketserver.StreamRequestHandler, + self.run_server(SocketServer.ThreadingUnixStreamServer, + SocketServer.StreamRequestHandler, self.stream_examine) if HAVE_FORKING: def test_ForkingUnixStreamServer(self): with simple_subprocess(self): self.run_server(ForkingUnixStreamServer, - socketserver.StreamRequestHandler, + SocketServer.StreamRequestHandler, self.stream_examine) def test_UDPServer(self): - self.run_server(socketserver.UDPServer, - socketserver.DatagramRequestHandler, + self.run_server(SocketServer.UDPServer, + SocketServer.DatagramRequestHandler, self.dgram_examine) def test_ThreadingUDPServer(self): - self.run_server(socketserver.ThreadingUDPServer, - socketserver.DatagramRequestHandler, + self.run_server(SocketServer.ThreadingUDPServer, + SocketServer.DatagramRequestHandler, self.dgram_examine) if HAVE_FORKING: def test_ForkingUDPServer(self): with simple_subprocess(self): - self.run_server(socketserver.ForkingUDPServer, - socketserver.DatagramRequestHandler, + self.run_server(SocketServer.ForkingUDPServer, + SocketServer.DatagramRequestHandler, self.dgram_examine) # Alas, on Linux (at least) recvfrom() doesn't return a meaningful @@ -228,19 +228,19 @@ class SocketServerTest(unittest.TestCase): # if HAVE_UNIX_SOCKETS: # def test_UnixDatagramServer(self): - # self.run_server(socketserver.UnixDatagramServer, - # socketserver.DatagramRequestHandler, + # self.run_server(SocketServer.UnixDatagramServer, + # SocketServer.DatagramRequestHandler, # self.dgram_examine) # # def test_ThreadingUnixDatagramServer(self): - # self.run_server(socketserver.ThreadingUnixDatagramServer, - # socketserver.DatagramRequestHandler, + # self.run_server(SocketServer.ThreadingUnixDatagramServer, + # SocketServer.DatagramRequestHandler, # self.dgram_examine) # # if HAVE_FORKING: # def test_ForkingUnixDatagramServer(self): - # self.run_server(socketserver.ForkingUnixDatagramServer, - # socketserver.DatagramRequestHandler, + # self.run_server(SocketServer.ForkingUnixDatagramServer, + # SocketServer.DatagramRequestHandler, # self.dgram_examine) diff --git a/Lib/test/test_wsgiref.py b/Lib/test/test_wsgiref.py index e30990e..b6d994b 100755 --- a/Lib/test/test_wsgiref.py +++ b/Lib/test/test_wsgiref.py @@ -8,7 +8,7 @@ from wsgiref.validate import validator from wsgiref.simple_server import WSGIServer, WSGIRequestHandler, demo_app from wsgiref.simple_server import make_server from StringIO import StringIO -from socketserver import BaseServer +from SocketServer import BaseServer import re, sys from test import test_support diff --git a/Misc/NEWS b/Misc/NEWS index c1f9555..b4b61e6 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -161,9 +161,6 @@ Library - The multifile module has been deprecated as per PEP 4. -- The SocketServer module has been renamed 'socketserver'. The old - name is now deprecated. - - The imageop module has been deprecated for removal in Python 3.0. - Issue #2250: Exceptions raised during evaluation of names in diff --git a/Misc/cheatsheet b/Misc/cheatsheet index e699359..8954e98 100644 --- a/Misc/cheatsheet +++ b/Misc/cheatsheet @@ -1973,7 +1973,7 @@ site Append module search paths for third-party packages to sys.path. smtplib SMTP Client class (RFC 821) sndhdr Several routines that help recognizing sound. -socketserver Generic socket server classes. +SocketServer Generic socket server classes. stat Constants and functions for interpreting stat/lstat struct. statcache Maintain a cache of file stats. statvfs Constants for interpreting statvfs struct as returned by -- cgit v0.12