blob: 2a4876442cd9efb87247951a7a32eb4dec162217 [file] [log] [blame]
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001****************************
2 What's New in Python 2.7
3****************************
4
5:Author: A.M. Kuchling (amk at amk.ca)
6:Release: |release|
7:Date: |today|
8
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00009.. Fix accents on Kristjan Valur Jonsson, Fuerstenau
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +000010
Andrew M. Kuchling85fffc32010-05-08 01:15:26 +000011.. Big jobs: pep 391 example
Andrew M. Kuchling837a5382010-05-06 17:21:59 +000012
Andrew M. Kuchling6d7dfa22010-04-11 12:49:37 +000013.. hyperlink all the methods & functions.
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +000014
Andrew M. Kuchling15c82d22010-04-29 00:22:16 +000015.. T_STRING_INPLACE not described in main docs
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +000016.. XXX "Format String Syntax" in string.rst could use many more examples.
Andrew M. Kuchling15c82d22010-04-29 00:22:16 +000017
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000018.. $Id$
19 Rules for maintenance:
20
21 * Anyone can add text to this document. Do not spend very much time
22 on the wording of your changes, because your text will probably
23 get rewritten to some degree.
24
25 * The maintainer will go through Misc/NEWS periodically and add
26 changes; it's therefore more important to add your changes to
27 Misc/NEWS than to this file.
28
29 * This is not a complete list of every single change; completeness
30 is the purpose of Misc/NEWS. Some changes I consider too small
31 or esoteric to include. If such a change is added to the text,
32 I'll just remove it. (This is another reason you shouldn't spend
33 too much time on writing your addition.)
34
35 * If you want to draw your new text to the attention of the
36 maintainer, add 'XXX' to the beginning of the paragraph or
37 section.
38
39 * It's OK to just add a fragmentary note about a change. For
40 example: "XXX Describe the transmogrify() function added to the
41 socket module." The maintainer will research the change and
42 write the necessary text.
43
44 * You can comment out your additions if you like, but it's not
45 necessary (especially when a final release is some months away).
46
Ezio Melotti021f3342010-04-06 03:26:49 +000047 * Credit the author of a patch or bugfix. Just the name is
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000048 sufficient; the e-mail address isn't necessary.
49
50 * It's helpful to add the bug/patch number in a parenthetical comment.
51
52 XXX Describe the transmogrify() function added to the socket
53 module.
54 (Contributed by P.Y. Developer; :issue:`12345`.)
55
56 This saves the maintainer some effort going through the SVN logs
57 when researching a change.
58
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +000059This article explains the new features in Python 2.7. The final
Andrew M. Kuchling837a5382010-05-06 17:21:59 +000060release of 2.7 is currently scheduled for July 2010; the detailed
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +000061schedule is described in :pep:`373`.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000062
63.. Compare with previous release in 2 - 3 sentences here.
64 add hyperlink when the documentation becomes available online.
65
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000066.. _whatsnew27-python31:
67
Andrew M. Kuchling02f7b992010-05-07 01:45:14 +000068The Future for Python 2.x
69=========================
70
71Python 2.7 is intended to be the last major release in the 2.x series.
Andrew M. Kuchlingd1e696b2010-05-07 11:30:47 +000072The Python maintainers are planning to focus their future efforts on
73the Python 3.x series.
Andrew M. Kuchling02f7b992010-05-07 01:45:14 +000074
75This means that 2.7 will remain in place for a long time, running
76production systems that have not been ported to Python 3.x.
77Two consequences of the long-term significance of 2.7 are:
78
79* It's very likely the 2.7 release will have a longer period of
80 maintenance compared to earlier 2.x versions. Python 2.7 will
Andrew M. Kuchlingd1e696b2010-05-07 11:30:47 +000081 continue to be maintained while the transition to 3.x continues.
82 Maintenance releases for Python 2.7 will probably be made for 5
83 years.
Andrew M. Kuchling02f7b992010-05-07 01:45:14 +000084
Andrew M. Kuchlingd1e696b2010-05-07 11:30:47 +000085* A policy decision was made to silence warnings only of interest to
86 developers by default. :exc:`DeprecationWarning` and its
87 descendants are now ignored unless otherwise requested, preventing
88 users from seeing warnings triggered by an application. (Carried
89 out in :issue:`7319`.)
90
91 In previous releases, :exc:`DeprecationWarning` messages were
92 enabled by default, providing Python developers with a clear
93 indication of where their code may break in a future major version
94 of Python.
95
96 However, there are increasingly many users of Python-based
97 applications who are not directly involved in the development of
98 those applications. :exc:`DeprecationWarning` messages are
99 irrelevant to such users, making them worry about an application
100 that's actually working correctly and burdening the developers of
101 these applications with responding to these concerns.
Andrew M. Kuchling02f7b992010-05-07 01:45:14 +0000102
103 You can re-enable display of :exc:`DeprecationWarning` messages by
104 running Python with the :option:`-Wdefault` (short form:
105 :option:`-Wd`) switch, or you can add
106 ``warnings.simplefilter('default')`` to your code.
107
108
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000109Python 3.1 Features
110=======================
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000111
112Much as Python 2.6 incorporated features from Python 3.0,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000113version 2.7 incorporates some of the new features
114in Python 3.1. The 2.x series continues to provide tools
115for migrating to the 3.x series.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000116
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000117A partial list of 3.1 features that were backported to 2.7:
118
Andrew M. Kuchling837a5382010-05-06 17:21:59 +0000119* A new version of the :mod:`io` library, rewritten in C for performance.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000120* The ordered-dictionary type described in :ref:`pep-0372`.
Andrew M. Kuchling8f254e72009-12-08 02:37:05 +0000121* The new format specifier described in :ref:`pep-0378`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000122* The :class:`memoryview` object.
123* A small subset of the :mod:`importlib` module `described below <#importlib-section>`__.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000124* Float-to-string and string-to-float conversions now round their
125 results more correctly. And :func:`repr` of a floating-point
126 number *x* returns a result that's guaranteed to round back to the
127 same number when converted back to a string.
Andrew M. Kuchling85fffc32010-05-08 01:15:26 +0000128* The :ctype:`PyCapsule` type, used to provide a C API for an extension module.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000129* The :cfunc:`PyLong_AsLongAndOverflow` C API function.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000130
131One porting change: the :option:`-3` switch now automatically
132enables the :option:`-Qwarn` switch that causes warnings
133about using classic division with integers and long integers.
134
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000135Other new Python3-mode warnings include:
136
137* :func:`operator.isCallable` and :func:`operator.sequenceIncludes`,
138 which are not supported in 3.x.
139
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000140.. ========================================================================
141.. Large, PEP-level features and changes should be described here.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000142.. ========================================================================
143
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000144.. _pep-0372:
145
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000146PEP 372: Adding an ordered dictionary to collections
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000147====================================================
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000148
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000149Regular Python dictionaries iterate over key/value pairs in arbitrary order.
150Over the years, a number of authors have written alternative implementations
151that remember the order that the keys were originally inserted. Based on
152the experiences from those implementations, a new
Ezio Melotti021f3342010-04-06 03:26:49 +0000153:class:`~collections.OrderedDict` class has been introduced in the
154:mod:`collections` module.
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000155
Ezio Melotti021f3342010-04-06 03:26:49 +0000156The :class:`~collections.OrderedDict` API is substantially the same as regular
157dictionaries but will iterate over keys and values in a guaranteed order
158depending on when a key was first inserted::
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000159
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000160 >>> from collections import OrderedDict
161 >>> d = OrderedDict([('first', 1), ('second', 2),
162 ... ('third', 3)])
163 >>> d.items()
164 [('first', 1), ('second', 2), ('third', 3)]
165
166If a new entry overwrites an existing entry, the original insertion
167position is left unchanged::
168
169 >>> d['second'] = 4
170 >>> d.items()
171 [('first', 1), ('second', 4), ('third', 3)]
172
173Deleting an entry and reinserting it will move it to the end::
174
175 >>> del d['second']
176 >>> d['second'] = 5
177 >>> d.items()
178 [('first', 1), ('third', 3), ('second', 5)]
179
Ezio Melotti021f3342010-04-06 03:26:49 +0000180The :meth:`~collections.OrderedDict.popitem` method has an optional *last*
181argument that defaults to True. If *last* is True, the most recently
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000182added key is returned and removed; if it's False, the
183oldest key is selected::
184
185 >>> od = OrderedDict([(x,0) for x in range(20)])
186 >>> od.popitem()
187 (19, 0)
188 >>> od.popitem()
189 (18, 0)
Ezio Melotti021f3342010-04-06 03:26:49 +0000190 >>> od.popitem(last=False)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000191 (0, 0)
Ezio Melotti021f3342010-04-06 03:26:49 +0000192 >>> od.popitem(last=False)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000193 (1, 0)
194
195Comparing two ordered dictionaries checks both the keys and values,
196and requires that the insertion order was the same::
197
198 >>> od1 = OrderedDict([('first', 1), ('second', 2),
199 ... ('third', 3)])
200 >>> od2 = OrderedDict([('third', 3), ('first', 1),
201 ... ('second', 2)])
Ezio Melotti021f3342010-04-06 03:26:49 +0000202 >>> od1 == od2
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000203 False
204 >>> # Move 'third' key to the end
Ezio Melotti021f3342010-04-06 03:26:49 +0000205 >>> del od2['third']; od2['third'] = 3
206 >>> od1 == od2
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000207 True
208
Ezio Melotti021f3342010-04-06 03:26:49 +0000209Comparing an :class:`~collections.OrderedDict` with a regular dictionary
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000210ignores the insertion order and just compares the keys and values.
211
Ezio Melotti021f3342010-04-06 03:26:49 +0000212How does the :class:`~collections.OrderedDict` work? It maintains a
213doubly-linked list of keys, appending new keys to the list as they're inserted.
214A secondary dictionary maps keys to their corresponding list node, so
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000215deletion doesn't have to traverse the entire linked list and therefore
216remains O(1).
217
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000218The standard library now supports use of ordered dictionaries in several
Andrew M. Kuchling363dbcc2010-04-14 23:55:17 +0000219modules.
220
221* The :mod:`ConfigParser` module uses them by default, letting
222 configuration files be read, modified, and then written back in their original
223 order.
224
225* The :meth:`~collections.somenamedtuple._asdict()` method for
226 :func:`collections.namedtuple` now returns an ordered dictionary with the
227 values appearing in the same order as the underlying tuple indices.
228
229* The :mod:`json` module's :class:`~json.JSONDecoder` class
230 constructor was extended with an *object_pairs_hook* parameter to
231 allow :class:`OrderedDict` instances to be built by the decoder.
232 Support was also added for third-party tools like
233 `PyYAML <http://pyyaml.org/>`_.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000234
Andrew M. Kuchling7fe65a02009-10-13 15:49:33 +0000235.. seealso::
236
237 :pep:`372` - Adding an ordered dictionary to collections
238 PEP written by Armin Ronacher and Raymond Hettinger;
239 implemented by Raymond Hettinger.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000240
241.. _pep-0378:
242
243PEP 378: Format Specifier for Thousands Separator
Ezio Melotti021f3342010-04-06 03:26:49 +0000244=================================================
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000245
246To make program output more readable, it can be useful to add
247separators to large numbers and render them as
24818,446,744,073,709,551,616 instead of 18446744073709551616.
249
250The fully general solution for doing this is the :mod:`locale` module,
251which can use different separators ("," in North America, "." in
252Europe) and different grouping sizes, but :mod:`locale` is complicated
253to use and unsuitable for multi-threaded applications where different
254threads are producing output for different locales.
255
256Therefore, a simple comma-grouping mechanism has been added to the
Ezio Melotti021f3342010-04-06 03:26:49 +0000257mini-language used by the :meth:`str.format` method. When
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000258formatting a floating-point number, simply include a comma between the
259width and the precision::
260
Eric Smithc4663852010-04-06 14:30:15 +0000261 >>> '{:20,.2f}'.format(18446744073709551616.0)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000262 '18,446,744,073,709,551,616.00'
263
Eric Smith6a928602010-04-06 15:17:33 +0000264When formatting an integer, include the comma after the width:
265
266 >>> '{:20,d}'.format(18446744073709551616)
267 '18,446,744,073,709,551,616'
268
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000269This mechanism is not adaptable at all; commas are always used as the
270separator and the grouping is always into three-digit groups. The
271comma-formatting mechanism isn't as general as the :mod:`locale`
272module, but it's easier to use.
273
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000274.. seealso::
275
276 :pep:`378` - Format Specifier for Thousands Separator
277 PEP written by Raymond Hettinger; implemented by Eric Smith.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000278
Andrew M. Kuchlingab21f752010-03-02 13:55:33 +0000279PEP 389: The argparse Module for Parsing Command Lines
280======================================================
281
Andrew M. Kuchlingec6393f2010-04-11 01:40:30 +0000282The :mod:`argparse` module for parsing command-line arguments was
283added, intended as a more powerful replacement for the
284:mod:`optparse` module.
285
286This means Python now supports three different modules for parsing
287command-line arguments: :mod:`getopt`, :mod:`optparse`, and
288:mod:`argparse`. The :mod:`getopt` module closely resembles the C
289:cfunc:`getopt` function, so it remains useful if you're writing a
290Python prototype that will eventually be rewritten in C.
291:mod:`optparse` becomes redundant, but there are no plans to remove it
292because there are many scripts still using it, and there's no
293automated way to update these scripts. (Making the :mod:`argparse`
294API consistent with :mod:`optparse`'s interface was discussed but
295rejected as too messy and difficult.)
296
Andrew M. Kuchlingf03641a2010-04-14 01:14:59 +0000297In short, if you're writing a new script and don't need to worry
Andrew M. Kuchlingec6393f2010-04-11 01:40:30 +0000298about compatibility with earlier versions of Python, use
299:mod:`argparse` instead of :mod:`optparse`.
300
Andrew M. Kuchlingf03641a2010-04-14 01:14:59 +0000301Here's an example::
302
303 import argparse
304
305 parser = argparse.ArgumentParser(description='Command-line example.')
306
307 # Add optional switches
308 parser.add_argument('-v', action='store_true', dest='is_verbose',
309 help='produce verbose output')
310 parser.add_argument('-o', action='store', dest='output',
311 metavar='FILE',
312 help='direct output to FILE instead of stdout')
313 parser.add_argument('-C', action='store', type=int, dest='context',
314 metavar='NUM', default=0,
315 help='display NUM lines of added context')
316
317 # Allow any number of additional arguments.
318 parser.add_argument(nargs='*', action='store', dest='inputs',
319 help='input filenames (default is stdin)')
320
321 args = parser.parse_args()
322 print args.__dict__
323
324Unless you override it, :option:`-h` and :option:`--help` switches
325are automatically added, and produce neatly formatted output::
326
327 -> ./python.exe argparse-example.py --help
Andrew M. Kuchling363dbcc2010-04-14 23:55:17 +0000328 usage: argparse-example.py [-h] [-v] [-o FILE] [-C NUM] [inputs [inputs ...]]
Andrew M. Kuchlingf03641a2010-04-14 01:14:59 +0000329
330 Command-line example.
331
332 positional arguments:
333 inputs input filenames (default is stdin)
334
335 optional arguments:
336 -h, --help show this help message and exit
337 -v produce verbose output
338 -o FILE direct output to FILE instead of stdout
339 -C NUM display NUM lines of added context
340
341Similarly to :mod:`optparse`, the command-line switches and arguments
342are returned as an object with attributes named by the *dest* parameters::
343
344 -> ./python.exe argparse-example.py -v
345 {'output': None, 'is_verbose': True, 'context': 0, 'inputs': []}
346
347 -> ./python.exe argparse-example.py -v -o /tmp/output -C 4 file1 file2
348 {'output': '/tmp/output', 'is_verbose': True, 'context': 4,
349 'inputs': ['file1', 'file2']}
350
351:mod:`argparse` has much fancier validation than :mod:`optparse`; you
352can specify an exact number of arguments as an integer, 0 or more
353arguments by passing ``'*'``, 1 or more by passing ``'+'``, or an
354optional argument with ``'?'``. A top-level parser can contain
355sub-parsers, so you can define subcommands that have different sets of
356switches, as in ``svn commit``, ``svn checkout``, etc. You can
357specify an argument type as :class:`~argparse.FileType`, which will
358automatically open files for you and understands that ``'-'`` means
359standard input or output.
Andrew M. Kuchlingab21f752010-03-02 13:55:33 +0000360
361.. seealso::
362
Andrew M. Kuchlingec6393f2010-04-11 01:40:30 +0000363 `argparse module documentation <http://docs.python.org/dev/library/argparse.html>`__
364
365 `Upgrading optparse code to use argparse <http://docs.python.org/dev/library/argparse.html#upgrading-optparse-code>`__
366
Andrew M. Kuchlingab21f752010-03-02 13:55:33 +0000367 :pep:`389` - argparse - New Command Line Parsing Module
368 PEP written and implemented by Steven Bethard.
369
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000370PEP 391: Dictionary-Based Configuration For Logging
371====================================================
372
Andrew M. Kuchlingb6c1aeb2010-04-14 14:28:31 +0000373.. not documented in library reference yet.
374
375The :mod:`logging` module is very flexible; an application can define
376a tree of logging subsystems, and each logger in this tree can filter
377out certain messages, format them differently, and direct messages to
378a varying number of handlers.
379
380All this flexibility can require a lot of configuration. You can
381write Python statements to create objects and set their properties,
382but a complex set-up would require verbose but boring code.
383:mod:`logging` also supports a :func:`~logging.config.fileConfig`
384function that parses a file, but the file format doesn't support
385configuring filters, and it's messier to generate programmatically.
386
387Python 2.7 adds a :func:`~logging.config.dictConfig` function that
388uses a dictionary, and there are many ways to produce a dictionary
389from different sources. You can construct one with code, of course.
390Python's standard library now includes a JSON parser, so you could
391parse a file containing JSON, or you could use a YAML parsing library
392if one is installed.
393
394XXX describe an example.
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000395
Andrew M. Kuchling02f7b992010-05-07 01:45:14 +0000396Three smaller enhancements to the :mod:`logging` module, all
397implemented by Vinay Sajip, are:
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000398
399.. rev79293
400
Andrew M. Kuchling02f7b992010-05-07 01:45:14 +0000401* The :class:`~logging.handlers.SysLogHandler` class now supports
402 syslogging over TCP. The constructor has a *socktype* parameter
403 giving the type of socket to use, either :const:`socket.SOCK_DGRAM`
404 for UDP or :const:`socket.SOCK_STREAM` for TCP. The default
405 protocol remains UDP.
406
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +0000407* :class:`Logger` instances gained a :meth:`getChild` method that retrieves a
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000408 descendant logger using a relative path. For example,
409 once you retrieve a logger by doing ``log = getLogger('app')``,
410 calling ``log.getChild('network.listen')`` is equivalent to
411 ``getLogger('app.network.listen')``.
412
413* The :class:`LoggerAdapter` class gained a :meth:`isEnabledFor` method
414 that takes a *level* and returns whether the underlying logger would
415 process a message of that level of importance.
416
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000417.. seealso::
418
419 :pep:`391` - Dictionary-Based Configuration For Logging
420 PEP written and implemented by Vinay Sajip.
421
422PEP 3106: Dictionary Views
423====================================================
424
Andrew M. Kuchling85f928a2010-04-15 01:42:27 +0000425The dictionary methods :meth:`keys`, :meth:`values`, and :meth:`items`
426are different in Python 3.x. They return an object called a :dfn:`view`
427instead of a fully materialized list.
428
Andrew M. Kuchling85f928a2010-04-15 01:42:27 +0000429It's not possible to change the return values of :meth:`keys`,
430:meth:`values`, and :meth:`items` in Python 2.7 because too much code
431would break. Instead the 3.x versions were added under the new names
432of :meth:`viewkeys`, :meth:`viewvalues`, and :meth:`viewitems`.
433
434::
435
436 >>> d = dict((i*10, chr(65+i)) for i in range(26))
437 >>> d
438 {0: 'A', 130: 'N', 10: 'B', 140: 'O', 20: ..., 250: 'Z'}
439 >>> d.viewkeys()
440 dict_keys([0, 130, 10, 140, 20, 150, 30, ..., 250])
441
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +0000442Views can be iterated over, but they also behave like sets. The ``&``
443operator performs intersection, and ``|`` performs a union::
444
445 >>> d1 = dict((i*10, chr(65+i)) for i in range(26))
446 >>> d2 = dict((i**.5, i) for i in range(1000))
447 >>> d1.viewkeys() & d2.viewkeys()
448 set([0.0, 10.0, 20.0, 30.0])
449 >>> d1.viewkeys() | range(0, 30)
450 set([0, 1, 130, 3, 4, 5, 6, ..., 120, 250])
451
Andrew M. Kuchling85f928a2010-04-15 01:42:27 +0000452The view keeps track of the dictionary and its contents change as the
453dictionary is modified::
454
455 >>> vk = d.viewkeys()
456 >>> vk
457 dict_keys([0, 130, 10, ..., 250])
458 >>> d[260] = '&'
459 >>> vk
460 dict_keys([0, 130, 260, 10, ..., 250])
461
462However, note that you can't add or remove keys while you're iterating
463over the view::
464
465 >>> for k in vk:
466 ... d[k*2] = k
467 ...
468 Traceback (most recent call last):
469 File "<stdin>", line 1, in <module>
470 RuntimeError: dictionary changed size during iteration
471
472You can use the view methods in Python 2.x code, and the 2to3
473converter will change them to the standard :meth:`keys`,
474:meth:`values`, and :meth:`items` methods.
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000475
476.. seealso::
477
478 :pep:`3106` - Revamping dict.keys(), .values() and .items()
479 PEP written by Guido van Rossum.
480 Backported to 2.7 by Alexandre Vassalotti; :issue:`1967`.
481
482
Andrew M. Kuchling9fbbd3b2010-05-01 12:06:51 +0000483PEP 3137: The memoryview Object
484====================================================
485
486The :class:`memoryview` object provides a view of another object's
487memory content that matches the :class:`bytes` type's interface.
488
489 >>> import string
490 >>> m = memoryview(string.letters)
491 >>> m
492 <memory at 0x37f850>
493 >>> len(m) # Returns length of underlying object
494 52
495 >>> m[0], m[25], m[26] # Indexing returns one byte
496 ('a', 'z', 'A')
497 >>> m2 = m[0:26] # Slicing returns another memoryview
498 >>> m2
499 <memory at 0x37f080>
500
501The content of the view can be converted to a string of bytes or to
502a list of integers:
503
504 >>> m2.tobytes()
505 'abcdefghijklmnopqrstuvwxyz'
506 >>> m2.tolist()
507 [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122]
508 >>>
509
510:class:`memoryview` objects allow modifying the underlying object if
511it's a mutable object.
512
513 >>> m2[0] = 75
514 Traceback (most recent call last):
515 File "<stdin>", line 1, in <module>
516 TypeError: cannot modify read-only memory
517 >>> b = bytearray(string.letters) # Creating a mutable object
518 >>> b
519 bytearray(b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
520 >>> mb = memoryview(b)
521 >>> mb[0] = '*' # Assign to view, changing the bytearray.
522 >>> b[0:5] # The bytearray has been changed.
523 bytearray(b'*bcde')
524 >>>
525
526.. seealso::
527
528 :pep:`3137` - Immutable Bytes and Mutable Buffer
529 PEP written by Guido van Rossum.
Antoine Pitrou5cace782010-05-01 12:16:39 +0000530 Implemented by Travis Oliphant, Antoine Pitrou and others.
Andrew M. Kuchling9fbbd3b2010-05-01 12:06:51 +0000531 Backported to 2.7 by Antoine Pitrou; :issue:`2396`.
532
533
534
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000535Other Language Changes
536======================
537
538Some smaller changes made to the core Python language are:
539
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000540* The syntax for set literals has been backported from Python 3.x.
541 Curly brackets are used to surround the contents of the resulting
542 mutable set; set literals are
543 distinguished from dictionaries by not containing colons and values.
544 ``{}`` continues to represent an empty dictionary; use
545 ``set()`` for an empty set.
546
547 >>> {1,2,3,4,5}
548 set([1, 2, 3, 4, 5])
Ezio Melotti021f3342010-04-06 03:26:49 +0000549 >>> set() # empty set
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000550 set([])
Ezio Melotti021f3342010-04-06 03:26:49 +0000551 >>> {} # empty dict
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000552 {}
553
554 Backported by Alexandre Vassalotti; :issue:`2335`.
555
556* Dictionary and set comprehensions are another feature backported from
557 3.x, generalizing list/generator comprehensions to use
558 the literal syntax for sets and dictionaries.
559
560 >>> {x:x*x for x in range(6)}
561 {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
562 >>> {'a'*x for x in range(6)}
563 set(['', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa'])
564
565 Backported by Alexandre Vassalotti; :issue:`2333`.
566
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000567* The :keyword:`with` statement can now use multiple context managers
568 in one statement. Context managers are processed from left to right
569 and each one is treated as beginning a new :keyword:`with` statement.
570 This means that::
571
572 with A() as a, B() as b:
573 ... suite of statements ...
574
575 is equivalent to::
576
577 with A() as a:
578 with B() as b:
579 ... suite of statements ...
580
581 The :func:`contextlib.nested` function provides a very similar
582 function, so it's no longer necessary and has been deprecated.
583
584 (Proposed in http://codereview.appspot.com/53094; implemented by
585 Georg Brandl.)
586
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000587* Conversions between floating-point numbers and strings are
588 now correctly rounded on most platforms. These conversions occur
589 in many different places: :func:`str` on
590 floats and complex numbers; the :class:`float` and :class:`complex`
591 constructors;
592 numeric formatting; serialization and
593 deserialization of floats and complex numbers using the
594 :mod:`marshal`, :mod:`pickle`
595 and :mod:`json` modules;
596 parsing of float and imaginary literals in Python code;
Ezio Melotti021f3342010-04-06 03:26:49 +0000597 and :class:`~decimal.Decimal`-to-float conversion.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000598
599 Related to this, the :func:`repr` of a floating-point number *x*
600 now returns a result based on the shortest decimal string that's
601 guaranteed to round back to *x* under correct rounding (with
602 round-half-to-even rounding mode). Previously it gave a string
603 based on rounding x to 17 decimal digits.
604
Ezio Melotti021f3342010-04-06 03:26:49 +0000605 .. maybe add an example?
606
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000607 The rounding library responsible for this improvement works on
608 Windows, and on Unix platforms using the gcc, icc, or suncc
609 compilers. There may be a small number of platforms where correct
610 operation of this code cannot be guaranteed, so the code is not
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +0000611 used on such systems. You can find out which code is being used
612 by checking :data:`sys.float_repr_style`, which will be ``short``
613 if the new code is in use and ``legacy`` if it isn't.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000614
Mark Dickinsonbdd863d2010-01-07 09:28:29 +0000615 Implemented by Eric Smith and Mark Dickinson, using David Gay's
616 :file:`dtoa.c` library; :issue:`7117`.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000617
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000618* The :meth:`str.format` method now supports automatic numbering of the replacement
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +0000619 fields. This makes using :meth:`str.format` more closely resemble using
620 ``%s`` formatting::
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000621
622 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
623 '2009:4:Sunday'
624 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
625 '2009:4:Sunday'
626
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +0000627 The auto-numbering takes the fields from left to right, so the first ``{...}``
628 specifier will use the first argument to :meth:`str.format`, the next
629 specifier will use the next argument, and so on. You can't mix auto-numbering
630 and explicit numbering -- either number all of your specifier fields or none
631 of them -- but you can mix auto-numbering and named fields, as in the second
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000632 example above. (Contributed by Eric Smith; :issue:`5237`.)
633
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000634 Complex numbers now correctly support usage with :func:`format`,
635 and default to being right-aligned.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000636 Specifying a precision or comma-separation applies to both the real
637 and imaginary parts of the number, but a specified field width and
638 alignment is applied to the whole of the resulting ``1.5+3j``
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000639 output. (Contributed by Eric Smith; :issue:`1588` and :issue:`7988`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000640
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000641 The 'F' format code now always formats its output using uppercase characters,
642 so it will now produce 'INF' and 'NAN'.
643 (Contributed by Eric Smith; :issue:`3382`.)
644
Andrew M. Kuchlingc4ae73e2010-04-30 13:47:34 +0000645 A low-level change: the :meth:`object.__format__` method now triggers
646 a :exc:`PendingDeprecationWarning` if it's passed a format string,
647 because the :meth:`__format__` method for :class:`object` converts
648 the object to a string representation and formats that. The method
649 used to silently apply the format string to the string
650 representation, but that could hide mistakes in Python code. If
651 you're supplying formatting information such as an alignment or
652 precision, presumably you're expecting the formatting to be applied
653 in some object-specific way. (Fixed by Eric Smith; :issue:`7994`.)
654
Mark Dickinson1a707982008-12-17 16:14:37 +0000655* The :func:`int` and :func:`long` types gained a ``bit_length``
Georg Brandl64e1c752009-04-11 18:19:27 +0000656 method that returns the number of bits necessary to represent
Mark Dickinson1a707982008-12-17 16:14:37 +0000657 its argument in binary::
658
659 >>> n = 37
Ezio Melotti021f3342010-04-06 03:26:49 +0000660 >>> bin(n)
Mark Dickinson1a707982008-12-17 16:14:37 +0000661 '0b100101'
662 >>> n.bit_length()
663 6
664 >>> n = 2**123-1
665 >>> n.bit_length()
666 123
667 >>> (n+1).bit_length()
668 124
669
670 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
671
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000672* Conversions from long integers and regular integers to floating
673 point now round differently, returning the floating-point number
674 closest to the number. This doesn't matter for small integers that
675 can be converted exactly, but for large numbers that will
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000676 unavoidably lose precision, Python 2.7 now approximates more
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000677 closely. For example, Python 2.6 computed the following::
678
679 >>> n = 295147905179352891391
680 >>> float(n)
681 2.9514790517935283e+20
682 >>> n - long(float(n))
683 65535L
684
685 Python 2.7's floating-point result is larger, but much closer to the
686 true value::
687
688 >>> n = 295147905179352891391
689 >>> float(n)
690 2.9514790517935289e+20
Ezio Melotti021f3342010-04-06 03:26:49 +0000691 >>> n - long(float(n))
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000692 -1L
693
694 (Implemented by Mark Dickinson; :issue:`3166`.)
695
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000696 Integer division is also more accurate in its rounding behaviours. (Also
697 implemented by Mark Dickinson; :issue:`1811`.)
698
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000699* It's now possible for a subclass of the built-in :class:`unicode` type
700 to override the :meth:`__unicode__` method. (Implemented by
701 Victor Stinner; :issue:`1583863`.)
702
Ezio Melotti021f3342010-04-06 03:26:49 +0000703* The :class:`bytearray` type's :meth:`~bytearray.translate` method now accepts
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000704 ``None`` as its first argument. (Fixed by Georg Brandl;
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000705 :issue:`4759`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000706
Ezio Melotti021f3342010-04-06 03:26:49 +0000707 .. bytearray doesn't seem to be documented
708
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000709* When using ``@classmethod`` and ``@staticmethod`` to wrap
710 methods as class or static methods, the wrapper object now
711 exposes the wrapped function as their :attr:`__func__` attribute.
712 (Contributed by Amaury Forgeot d'Arc, after a suggestion by
713 George Sakkis; :issue:`5982`.)
714
Andrew M. Kuchlingd3b60222010-05-01 01:19:16 +0000715* When a restricted set of attributes were set using ``__slots__``,
716 deleting an unset attribute would not raise :exc:`AttributeError`
717 as you would expect. Fixed by Benjamin Peterson; :issue:`7604`.)
718
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000719* A new encoding named "cp720", used primarily for Arabic text, is now
720 supported. (Contributed by Alexander Belchenko and Amaury Forgeot
721 d'Arc; :issue:`1616979`.)
722
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000723* The :class:`file` object will now set the :attr:`filename` attribute
724 on the :exc:`IOError` exception when trying to open a directory
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +0000725 on POSIX platforms (noted by Jan Kaliszewski; :issue:`4764`), and
726 now explicitly checks for and forbids writing to read-only file objects
727 instead of trusting the C library to catch and report the error
728 (fixed by Stefan Krah; :issue:`5677`).
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000729
Benjamin Petersonae9a0a02009-12-31 16:49:37 +0000730* The Python tokenizer now translates line endings itself, so the
731 :func:`compile` built-in function can now accept code using any
732 line-ending convention. Additionally, it no longer requires that the
733 code end in a newline.
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +0000734
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000735* Extra parentheses in function definitions are illegal in Python 3.x,
736 meaning that you get a syntax error from ``def f((x)): pass``. In
737 Python3-warning mode, Python 2.7 will now warn about this odd usage.
738 (Noted by James Lingard; :issue:`7362`.)
739
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +0000740* It's now possible to create weak references to old-style class
741 objects. New-style classes were always weak-referenceable. (Fixed
742 by Antoine Pitrou; :issue:`8268`.)
743
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000744* When a module object is garbage-collected, the module's dictionary is
745 now only cleared if no one else is holding a reference to the
746 dictionary (:issue:`7140`).
747
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000748.. ======================================================================
749
Andrew M. Kuchlingec6393f2010-04-11 01:40:30 +0000750.. _new-27-interpreter:
751
752Interpreter Changes
753-------------------------------
754
755A new environment variable, :envvar:`PYTHONWARNINGS`,
756allows controlling warnings. It should be set to a string
757containing warning settings, equivalent to those
758used with the :option:`-W` switch, separated by commas.
759(Contributed by Brian Curtin; :issue:`7301`.)
760
761For example, the following setting will print warnings every time
762they occur, but turn warnings from the :mod:`Cookie` module into an
763error. (The exact syntax for setting an environment variable varies
764across operating systems and shells, so it may be different for you.)
765
766::
767
768 export PYTHONWARNINGS=all,error:::Cookie:0
769
Andrew M. Kuchling15c82d22010-04-29 00:22:16 +0000770When running a module using the interpreter's :option:`-m` switch,
771``sys.argv[0]`` will now be set to the string ``'-m'`` while the
Andrew M. Kuchlingc121f132010-04-30 01:33:40 +0000772module is being located, while executing the :file:`__init__.py` files
773for any parent packages of the module to be executed.
774(Suggested by Michael Foord; implemented by Nick Coghlan;
775:issue:`8202`.)
Andrew M. Kuchlingec6393f2010-04-11 01:40:30 +0000776
777.. ======================================================================
778
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000779
780Optimizations
781-------------
782
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000783Several performance enhancements have been added:
784
785.. * A new :program:`configure` option, :option:`--with-computed-gotos`,
786 compiles the main bytecode interpreter loop using a new dispatch
787 mechanism that gives speedups of up to 20%, depending on the system
788 and benchmark. The new mechanism is only supported on certain
789 compilers, such as gcc, SunPro, and icc.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000790
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000791* A new opcode was added to perform the initial setup for
792 :keyword:`with` statements, looking up the :meth:`__enter__` and
793 :meth:`__exit__` methods. (Contributed by Benjamin Peterson.)
794
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000795* The garbage collector now performs better for one common usage
796 pattern: when many objects are being allocated without deallocating
797 any of them. This would previously take quadratic
798 time for garbage collection, but now the number of full garbage collections
799 is reduced as the number of objects on the heap grows.
800 The new logic is to only perform a full garbage collection pass when
801 the middle generation has been collected 10 times and when the
802 number of survivor objects from the middle generation exceeds 10% of
803 the number of objects in the oldest generation. (Suggested by Martin
Ezio Melotti021f3342010-04-06 03:26:49 +0000804 von Löwis and implemented by Antoine Pitrou; :issue:`4074`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000805
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000806* The garbage collector tries to avoid tracking simple containers
807 which can't be part of a cycle. In Python 2.7, this is now true for
808 tuples and dicts containing atomic types (such as ints, strings,
809 etc.). Transitively, a dict containing tuples of atomic types won't
810 be tracked either. This helps reduce the cost of each
811 garbage collection by decreasing the number of objects to be
812 considered and traversed by the collector.
Antoine Pitrouc18f6b02009-03-28 19:10:13 +0000813 (Contributed by Antoine Pitrou; :issue:`4688`.)
814
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000815* Long integers are now stored internally either in base 2**15 or in base
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000816 2**30, the base being determined at build time. Previously, they
817 were always stored in base 2**15. Using base 2**30 gives
818 significant performance improvements on 64-bit machines, but
819 benchmark results on 32-bit machines have been mixed. Therefore,
820 the default is to use base 2**30 on 64-bit machines and base 2**15
821 on 32-bit machines; on Unix, there's a new configure option
822 :option:`--enable-big-digits` that can be used to override this default.
823
824 Apart from the performance improvements this change should be
825 invisible to end users, with one exception: for testing and
Ezio Melotti021f3342010-04-06 03:26:49 +0000826 debugging purposes there's a new structseq :data:`sys.long_info` that
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000827 provides information about the internal format, giving the number of
828 bits per digit and the size in bytes of the C type used to store
829 each digit::
830
831 >>> import sys
832 >>> sys.long_info
833 sys.long_info(bits_per_digit=30, sizeof_digit=4)
834
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000835 (Contributed by Mark Dickinson; :issue:`4258`.)
836
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000837 Another set of changes made long objects a few bytes smaller: 2 bytes
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000838 smaller on 32-bit systems and 6 bytes on 64-bit.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000839 (Contributed by Mark Dickinson; :issue:`5260`.)
840
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000841* The division algorithm for long integers has been made faster
842 by tightening the inner loop, doing shifts instead of multiplications,
843 and fixing an unnecessary extra iteration.
844 Various benchmarks show speedups of between 50% and 150% for long
845 integer divisions and modulo operations.
846 (Contributed by Mark Dickinson; :issue:`5512`.)
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +0000847 Bitwise operations are also significantly faster (initial patch by
848 Gregory Smith; :issue:`1087418`).
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000849
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000850* The implementation of ``%`` checks for the left-side operand being
851 a Python string and special-cases it; this results in a 1-3%
852 performance increase for applications that frequently use ``%``
853 with strings, such as templating libraries.
854 (Implemented by Collin Winter; :issue:`5176`.)
855
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000856* List comprehensions with an ``if`` condition are compiled into
857 faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
858 by Jeffrey Yasskin; :issue:`4715`.)
859
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +0000860* Converting an integer or long integer to a decimal string was made
861 faster by special-casing base 10 instead of using a generalized
862 conversion function that supports arbitrary bases.
863 (Patch by Gawain Bolton; :issue:`6713`.)
864
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000865* The :meth:`split`, :meth:`replace`, :meth:`rindex`,
866 :meth:`rpartition`, and :meth:`rsplit` methods of string-like types
867 (strings, Unicode strings, and :class:`bytearray` objects) now use a
868 fast reverse-search algorithm instead of a character-by-character
869 scan. This is sometimes faster by a factor of 10. (Added by
870 Florent Xicluna; :issue:`7462` and :issue:`7622`.)
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +0000871
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000872* The :mod:`pickle` and :mod:`cPickle` modules now automatically
873 intern the strings used for attribute names, reducing memory usage
874 of the objects resulting from unpickling. (Contributed by Jake
875 McGuire; :issue:`5084`.)
876
877* The :mod:`cPickle` module now special-cases dictionaries,
878 nearly halving the time required to pickle them.
879 (Contributed by Collin Winter; :issue:`5670`.)
880
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000881.. ======================================================================
882
Georg Brandl0516f812009-11-18 18:52:35 +0000883New and Improved Modules
884========================
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000885
886As in every release, Python's standard library received a number of
887enhancements and bug fixes. Here's a partial list of the most notable
888changes, sorted alphabetically by module name. Consult the
889:file:`Misc/NEWS` file in the source tree for a more complete list of
890changes, or look through the Subversion logs for all the details.
891
Ezio Melotti021f3342010-04-06 03:26:49 +0000892* The :mod:`bdb` module's base debugging class :class:`~bdb.Bdb`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000893 gained a feature for skipping modules. The constructor
894 now takes an iterable containing glob-style patterns such as
895 ``django.*``; the debugger will not step into stack frames
896 from a module that matches one of these patterns.
897 (Contributed by Maru Newby after a suggestion by
898 Senthil Kumaran; :issue:`5142`.)
899
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +0000900* The :mod:`binascii` module now supports the buffer API, so it can be
901 used with :class:`memoryview` instances and other similar buffer objects.
902 (Backported from 3.x by Florent Xicluna; :issue:`7703`.)
903
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000904* Updated module: the :mod:`bsddb` module has been updated from 4.7.2devel9
905 to version 4.8.4 of
906 `the pybsddb package <http://www.jcea.es/programacion/pybsddb.htm>`__.
907 The new version features better Python 3.x compatibility, various bug fixes,
908 and adds several new BerkeleyDB flags and methods.
Ezio Melotti021f3342010-04-06 03:26:49 +0000909 (Updated by Jesús Cea Avión; :issue:`8156`. The pybsddb
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000910 changelog can be browsed at http://hg.jcea.es/pybsddb/file/tip/ChangeLog.)
911
Ezio Melotti021f3342010-04-06 03:26:49 +0000912* The :mod:`bz2` module's :class:`~bz2.BZ2File` now supports the context
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000913 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
914 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
915
Ezio Melotti021f3342010-04-06 03:26:49 +0000916* New class: the :class:`~collections.Counter` class in the :mod:`collections`
917 module is useful for tallying data. :class:`~collections.Counter` instances
918 behave mostly like dictionaries but return zero for missing keys instead of
Georg Brandlf6dab952009-04-28 21:48:35 +0000919 raising a :exc:`KeyError`:
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000920
Georg Brandlf6dab952009-04-28 21:48:35 +0000921 .. doctest::
922 :options: +NORMALIZE_WHITESPACE
923
924 >>> from collections import Counter
925 >>> c = Counter()
926 >>> for letter in 'here is a sample of english text':
927 ... c[letter] += 1
928 ...
929 >>> c
930 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
931 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
932 'p': 1, 'r': 1, 'x': 1})
933 >>> c['e']
934 5
935 >>> c['z']
936 0
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000937
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +0000938 There are three additional :class:`~collections.Counter` methods:
939 :meth:`~collections.Counter.most_common` returns the N most common
940 elements and their counts. :meth:`~collections.Counter.elements`
941 returns an iterator over the contained elements, repeating each
942 element as many times as its count.
943 :meth:`~collections.Counter.subtract` takes an iterable and
944 subtracts one for each element instead of adding; if the argument is
945 a dictionary or another :class:`Counter`, the counts are
946 subtracted. ::
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000947
948 >>> c.most_common(5)
949 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
950 >>> c.elements() ->
951 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
952 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
953 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
Georg Brandlf6dab952009-04-28 21:48:35 +0000954 's', 's', 'r', 't', 't', 'x'
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +0000955 >>> c['e']
956 5
957 >>> c.subtract('very heavy on the letter e')
958 >>> c['e'] # Count is now lower
959 -1
Ezio Melotti021f3342010-04-06 03:26:49 +0000960
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000961 Contributed by Raymond Hettinger; :issue:`1696199`.
962
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +0000963 .. revision 79660
964
Ezio Melotti021f3342010-04-06 03:26:49 +0000965 The new :class:`~collections.OrderedDict` class is described in the earlier
966 section :ref:`pep-0372`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000967
Ezio Melotti021f3342010-04-06 03:26:49 +0000968 The :class:`~collections.namedtuple` class now has an optional *rename* parameter.
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000969 If *rename* is true, field names that are invalid because they've
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000970 been repeated or that aren't legal Python identifiers will be
971 renamed to legal names that are derived from the field's
972 position within the list of fields:
973
Georg Brandlf6dab952009-04-28 21:48:35 +0000974 >>> from collections import namedtuple
975 >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000976 >>> T._fields
977 ('field1', '_1', '_2', 'field2')
978
979 (Added by Raymond Hettinger; :issue:`1818`.)
980
Andrew M. Kuchling6d7dfa22010-04-11 12:49:37 +0000981 The :class:`~collections.deque` data type now has a
982 :meth:`~collections.deque.count` method that returns the number of
983 contained elements equal to the supplied argument *x*, and a
984 :meth:`~collections.deque.reverse` method that reverses the elements
985 of the deque in-place. :class:`deque` also exposes its maximum
986 length as the read-only :attr:`~collections.deque.maxlen` attribute.
987 (Both features added by Raymond Hettinger.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000988
Andrew M. Kuchling02f7b992010-05-07 01:45:14 +0000989* Constructors for the parsing classes in the :mod:`ConfigParser` module now
990 take a *allow_no_value* parameter, defaulting to false; if true,
991 options without values will be allowed. For example::
992
993 >>> import ConfigParser, StringIO
994 >>> sample_config = """
995 ... [mysqld]
996 ... user = mysql
997 ... pid-file = /var/run/mysqld/mysqld.pid
998 ... skip-bdb
999 ... """
1000 >>> config = ConfigParser.RawConfigParser(allow_no_value=True)
1001 >>> config.readfp(StringIO.StringIO(sample_config))
1002 >>> config.get('mysqld', 'user')
1003 'mysql'
1004 >>> print config.get('mysqld', 'skip-bdb')
1005 None
1006 >>> print config.get('mysqld', 'unknown')
1007 Traceback (most recent call last):
1008 ...
1009 ConfigParser.NoOptionError: No option 'unknown' in section: 'mysqld'
1010
1011 (Contributed by Mats Kindahl; :issue:`7005`.)
1012
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001013* Deprecated function: :func:`contextlib.nested`, which allows
1014 handling more than one context manager with a single :keyword:`with`
1015 statement, has been deprecated, because :keyword:`with` supports
1016 multiple context managers syntactically now.
1017
Ezio Melotti021f3342010-04-06 03:26:49 +00001018* The :mod:`copy` module's :func:`~copy.deepcopy` function will now
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001019 correctly copy bound instance methods. (Implemented by
1020 Robert Collins; :issue:`1515`.)
1021
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001022* The :mod:`ctypes` module now always converts ``None`` to a C NULL
1023 pointer for arguments declared as pointers. (Changed by Thomas
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001024 Heller; :issue:`4606`.) The underlying `libffi library
1025 <http://sourceware.org/libffi/>`__ has been updated to version
1026 3.0.9, containing various fixes for different platforms. (Updated
1027 by Matthias Klose; :issue:`8142`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001028
Ezio Melotti021f3342010-04-06 03:26:49 +00001029* New method: the :mod:`datetime` module's :class:`~datetime.timedelta` class
1030 gained a :meth:`~datetime.timedelta.total_seconds` method that returns the
1031 number of seconds in the duration. (Contributed by Brian Quinlan; :issue:`5788`.)
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001032
Ezio Melotti021f3342010-04-06 03:26:49 +00001033* New method: the :class:`~decimal.Decimal` class gained a
1034 :meth:`~decimal.Decimal.from_float` class method that performs an exact
1035 conversion of a floating-point number to a :class:`~decimal.Decimal`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001036 Note that this is an **exact** conversion that strives for the
1037 closest decimal approximation to the floating-point representation's value;
1038 the resulting decimal value will therefore still include the inaccuracy,
1039 if any.
1040 For example, ``Decimal.from_float(0.1)`` returns
1041 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
1042 (Implemented by Raymond Hettinger; :issue:`4796`.)
1043
Andrew M. Kuchling04b99cc2010-05-04 01:24:22 +00001044 Comparing instances of :class:`Decimal` with floating-point
1045 numbers now produces sensible results based on the numeric values
1046 of the operands. Previously such comparisons would fall back to
1047 Python's default rules for comparing objects, which produced arbitrary
1048 results based on their type. Note that you still cannot combine
1049 :class:`Decimal` and floating-point in other operations such as addition,
1050 since you should be explicitly choosing how to convert between float and
1051 :class:`Decimal`.
1052 (Fixed by Mark Dickinson; :issue:`2531`.)
1053
Ezio Melotti021f3342010-04-06 03:26:49 +00001054 Most of the methods of the :class:`~decimal.Context` class now accept integers
1055 as well as :class:`~decimal.Decimal` instances; the only exceptions are the
1056 :meth:`~decimal.Context.canonical` and :meth:`~decimal.Context.is_canonical`
1057 methods. (Patch by Juan José Conti; :issue:`7633`.)
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001058
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +00001059 The constructor for :class:`~decimal.Decimal` now accepts
1060 floating-point numbers (added by Raymond Hettinger; :issue:`8257`)
1061 and non-European Unicode characters such as Arabic-Indic digits
1062 (contributed by Mark Dickinson; :issue:`6595`).
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001063
Ezio Melotti021f3342010-04-06 03:26:49 +00001064 When using :class:`~decimal.Decimal` instances with a string's
1065 :meth:`~str.format` method, the default alignment was previously
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001066 left-alignment. This has been changed to right-alignment, which seems
1067 more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.)
1068
Andrew M. Kuchlingc121f132010-04-30 01:33:40 +00001069 Comparisons involving a signaling NaN value (or ``sNAN``) now signal
1070 :const:`InvalidOperation` instead of silently returning a true or
1071 false value depending on the comparison operator. Quiet NaN values
1072 (or ``NaN``) are now hashable. (Fixed by Mark Dickinson;
1073 :issue:`7279`.)
1074
Andrew M. Kuchling363dbcc2010-04-14 23:55:17 +00001075* The :mod:`difflib` module now produces output that is more
Andrew M. Kuchling15c82d22010-04-29 00:22:16 +00001076 compatible with modern :command:`diff`/:command:`patch` tools
1077 through one small change, using a tab character instead of spaces as
1078 a separator in the header giving the filename. (Fixed by Anatoly
1079 Techtonik; :issue:`7585`.)
1080
1081* The :mod:`doctest` module's :const:`IGNORE_EXCEPTION_DETAIL` flag
1082 will now ignore the name of the module containing the exception
1083 being tested. (Patch by Lennart Regebro; :issue:`7490`.)
Andrew M. Kuchling363dbcc2010-04-14 23:55:17 +00001084
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +00001085* The :class:`~fractions.Fraction` class now accepts a single float or
1086 :class:`~decimal.Decimal` instance, or two rational numbers, as
1087 arguments to its constructor. (Implemented by Mark Dickinson;
1088 rationals added in :issue:`5812`, and float/decimal in
1089 :issue:`8294`.)
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001090
Andrew M. Kuchlingec6393f2010-04-11 01:40:30 +00001091 An oversight was fixed, making the :class:`Fraction` match the other
1092 numeric types; ordering comparisons (``<``, ``<=``, ``>``, ``>=``) between
1093 fractions and complex numbers now raise a :exc:`TypeError`.
1094
1095 .. revision 79455
1096
Ezio Melotti021f3342010-04-06 03:26:49 +00001097* New class: a new :class:`~ftplib.FTP_TLS` class in
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +00001098 the :mod:`ftplib` module provides secure FTP
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001099 connections using TLS encapsulation of authentication as well as
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +00001100 subsequent control and data transfers.
1101 (Contributed by Giampaolo Rodola', :issue:`2054`.)
1102
Ezio Melotti021f3342010-04-06 03:26:49 +00001103 The :meth:`~ftplib.FTP.storbinary` method for binary uploads can now restart
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +00001104 uploads thanks to an added *rest* parameter (patch by Pablo Mouzo;
1105 :issue:`6845`.)
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001106
Andrew M. Kuchling6d7dfa22010-04-11 12:49:37 +00001107* New class decorator: :func:`total_ordering` in the :mod:`functools`
1108 module takes a class that defines an :meth:`__eq__` method and one of
1109 :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, or :meth:`__ge__`,
1110 and generates the missing comparison methods. Since the
1111 :meth:`__cmp__` method is being deprecated in Python 3.x,
1112 this decorator makes it easier to define ordered classes.
1113 (Added by Raymond Hettinger; :issue:`5479`.)
1114
1115 New function: :func:`cmp_to_key` will take an old-style comparison
1116 function that expects two arguments and return a new callable that
1117 can be used as the *key* parameter to functions such as
1118 :func:`sorted`, :func:`min` and :func:`max`, etc. The primary
1119 intended use is to help with making code compatible with Python 3.x.
1120 (Added by Raymond Hettinger.)
1121
Ezio Melotti021f3342010-04-06 03:26:49 +00001122* New function: the :mod:`gc` module's :func:`~gc.is_tracked` returns
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001123 true if a given instance is tracked by the garbage collector, false
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001124 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
1125
Ezio Melotti021f3342010-04-06 03:26:49 +00001126* The :mod:`gzip` module's :class:`~gzip.GzipFile` now supports the context
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +00001127 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``
1128 (contributed by Hagen Fuerstenau; :issue:`3860`), and it now implements
1129 the :class:`io.BufferedIOBase` ABC, so you can wrap it with
1130 :class:`io.BufferedReader` for faster processing
1131 (contributed by Nir Aides; :issue:`7471`).
1132 It's also now possible to override the modification time
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001133 recorded in a gzipped file by providing an optional timestamp to
1134 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001135
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001136 Files in gzip format can be padded with trailing zero bytes; the
1137 :mod:`gzip` module will now consume these trailing bytes. (Fixed by
1138 Tadek Pietraszek and Brian Curtin; :issue:`2846`.)
1139
Ezio Melotti021f3342010-04-06 03:26:49 +00001140* New attribute: the :mod:`hashlib` module now has an :attr:`~hashlib.hashlib.algorithms`
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001141 attribute containing a tuple naming the supported algorithms.
1142 In Python 2.7, ``hashlib.algorithms`` contains
1143 ``('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')``
1144 (Contributed by Carl Chenet; :issue:`7418`.)
1145
Ezio Melotti021f3342010-04-06 03:26:49 +00001146* The default :class:`~httplib.HTTPResponse` class used by the :mod:`httplib` module now
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001147 supports buffering, resulting in much faster reading of HTTP responses.
1148 (Contributed by Kristjan Valur Jonsson; :issue:`4879`.)
1149
Ezio Melotti021f3342010-04-06 03:26:49 +00001150 The :class:`~httplib.HTTPConnection` and :class:`~httplib.HTTPSConnection` classes
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001151 now support a *source_address* parameter, a ``(host, port)`` 2-tuple
1152 giving the source address that will be used for the connection.
1153 (Contributed by Eldon Ziegler; :issue:`3972`.)
1154
Andrew M. Kuchlingd3b60222010-05-01 01:19:16 +00001155* The :mod:`ihooks` module now supports relative imports. Note that
1156 :mod:`ihooks` is an older module used to support customizing imports,
1157 superseded by the :mod:`imputil` module added in Python 2.0.
1158 (Relative import support added by Neil Schemenauer.)
1159
1160 .. revision 75423
1161
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001162* The :mod:`imaplib` module now supports IPv6 addresses.
1163 (Contributed by Derek Morr; :issue:`1655`.)
1164
Andrew M. Kuchlingce690522010-04-13 01:32:51 +00001165* New function: the :mod:`inspect` module's :func:`~inspect.getcallargs`
1166 takes a callable and its positional and keyword arguments,
1167 and figures out which of the callable's parameters will receive each argument,
1168 returning a dictionary mapping argument names to their values. For example::
1169
1170 >>> from inspect import getcallargs
1171 >>> def f(a, b=1, *pos, **named):
1172 ... pass
1173 >>> getcallargs(f, 1, 2, 3)
1174 {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}
1175 >>> getcallargs(f, a=2, x=4)
1176 {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}
1177 >>> getcallargs(f)
1178 Traceback (most recent call last):
1179 ...
1180 TypeError: f() takes at least 1 argument (0 given)
1181
1182 Contributed by George Sakkis; :issue:`3135`.
1183
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +00001184* Updated module: The :mod:`io` library has been upgraded to the version shipped with
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001185 Python 3.1. For 3.1, the I/O library was entirely rewritten in C
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +00001186 and is 2 to 20 times faster depending on the task being performed. The
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001187 original Python version was renamed to the :mod:`_pyio` module.
1188
1189 One minor resulting change: the :class:`io.TextIOBase` class now
1190 has an :attr:`errors` attribute giving the error setting
1191 used for encoding and decoding errors (one of ``'strict'``, ``'replace'``,
1192 ``'ignore'``).
1193
1194 The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001195 an invalid file descriptor. (Implemented by Benjamin Peterson;
Ezio Melotti021f3342010-04-06 03:26:49 +00001196 :issue:`4991`.) The :meth:`~io.IOBase.truncate` method now preserves the
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001197 file position; previously it would change the file position to the
1198 end of the new file. (Fixed by Pascal Chambon; :issue:`6939`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001199
Andrew M. Kuchling5a73ff82009-12-02 14:27:11 +00001200* New function: ``itertools.compress(data, selectors)`` takes two
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +00001201 iterators. Elements of *data* are returned if the corresponding
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001202 value in *selectors* is true::
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +00001203
1204 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
1205 A, C, E, F
1206
Ezio Melotti021f3342010-04-06 03:26:49 +00001207 .. maybe here is better to use >>> list(itertools.compress(...)) instead
1208
Andrew M. Kuchling5a73ff82009-12-02 14:27:11 +00001209 New function: ``itertools.combinations_with_replacement(iter, r)``
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +00001210 returns all the possible *r*-length combinations of elements from the
Ezio Melotti021f3342010-04-06 03:26:49 +00001211 iterable *iter*. Unlike :func:`~itertools.combinations`, individual elements
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +00001212 can be repeated in the generated combinations::
1213
1214 itertools.combinations_with_replacement('abc', 2) =>
1215 ('a', 'a'), ('a', 'b'), ('a', 'c'),
1216 ('b', 'b'), ('b', 'c'), ('c', 'c')
1217
1218 Note that elements are treated as unique depending on their position
1219 in the input, not their actual values.
1220
Ezio Melotti021f3342010-04-06 03:26:49 +00001221 The :func:`itertools.count` function now has a *step* argument that
1222 allows incrementing by values other than 1. :func:`~itertools.count` also
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +00001223 now allows keyword arguments, and using non-integer values such as
Ezio Melotti021f3342010-04-06 03:26:49 +00001224 floats or :class:`~decimal.Decimal` instances. (Implemented by Raymond
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +00001225 Hettinger; :issue:`5032`.)
1226
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001227 :func:`itertools.combinations` and :func:`itertools.product` were
1228 previously raising :exc:`ValueError` for values of *r* larger than
1229 the input iterable. This was deemed a specification error, so they
1230 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
1231
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +00001232* Updated module: The :mod:`json` module was upgraded to version 2.0.9 of the
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001233 simplejson package, which includes a C extension that makes
1234 encoding and decoding faster.
1235 (Contributed by Bob Ippolito; :issue:`4136`.)
1236
Ezio Melotti021f3342010-04-06 03:26:49 +00001237 To support the new :class:`collections.OrderedDict` type, :func:`json.load`
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001238 now has an optional *object_pairs_hook* parameter that will be called
1239 with any object literal that decodes to a list of pairs.
1240 (Contributed by Raymond Hettinger; :issue:`5381`.)
1241
Andrew M. Kuchling837a5382010-05-06 17:21:59 +00001242* The :mod:`mailbox` module's :class:`Maildir` class now records the
1243 timestamp on the directories it reads, and only re-reads them if the
1244 modification time has subsequently changed. This improves
1245 performance by avoiding unneeded directory scans. (Fixed by
1246 A.M. Kuchling and Antoine Pitrou; :issue:`1607951`, :issue:`6896`.)
1247
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001248* New functions: the :mod:`math` module gained
Ezio Melotti021f3342010-04-06 03:26:49 +00001249 :func:`~math.erf` and :func:`~math.erfc` for the error function and the complementary error function,
1250 :func:`~math.expm1` which computes ``e**x - 1`` with more precision than
1251 using :func:`~math.exp` and subtracting 1,
1252 :func:`~math.gamma` for the Gamma function, and
1253 :func:`~math.lgamma` for the natural log of the Gamma function.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001254 (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.)
1255
Andrew M. Kuchling24520b42009-04-09 11:22:47 +00001256* The :mod:`multiprocessing` module's :class:`Manager*` classes
1257 can now be passed a callable that will be called whenever
1258 a subprocess is started, along with a set of arguments that will be
1259 passed to the callable.
1260 (Contributed by lekma; :issue:`5585`.)
1261
Ezio Melotti021f3342010-04-06 03:26:49 +00001262 The :class:`~multiprocessing.Pool` class, which controls a pool of worker processes,
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001263 now has an optional *maxtasksperchild* parameter. Worker processes
1264 will perform the specified number of tasks and then exit, causing the
Ezio Melotti021f3342010-04-06 03:26:49 +00001265 :class:`~multiprocessing.Pool` to start a new worker. This is useful if tasks may leak
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001266 memory or other resources, or if some tasks will cause the worker to
1267 become very large.
1268 (Contributed by Charles Cazabon; :issue:`6963`.)
1269
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001270* The :mod:`nntplib` module now supports IPv6 addresses.
1271 (Contributed by Derek Morr; :issue:`1664`.)
1272
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001273* New functions: the :mod:`os` module wraps the following POSIX system
Ezio Melotti021f3342010-04-06 03:26:49 +00001274 calls: :func:`~os.getresgid` and :func:`~os.getresuid`, which return the
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001275 real, effective, and saved GIDs and UIDs;
Ezio Melotti021f3342010-04-06 03:26:49 +00001276 :func:`~os.setresgid` and :func:`~os.setresuid`, which set
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001277 real, effective, and saved GIDs and UIDs to new values;
Ezio Melotti021f3342010-04-06 03:26:49 +00001278 :func:`~os.initgroups`. (GID/UID functions
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001279 contributed by Travis H.; :issue:`6508`. Support for initgroups added
1280 by Jean-Paul Calderone; :issue:`7333`.)
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001281
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001282 The :func:`os.fork` function now re-initializes the import lock in
Ezio Melotti021f3342010-04-06 03:26:49 +00001283 the child process; this fixes problems on Solaris when :func:`~os.fork`
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001284 is called from a thread. (Fixed by Zsolt Cserna; :issue:`7242`.)
1285
Ezio Melotti021f3342010-04-06 03:26:49 +00001286* In the :mod:`os.path` module, the :func:`~os.path.normpath` and
1287 :func:`~os.path.abspath` functions now preserve Unicode; if their input path
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001288 is a Unicode string, the return value is also a Unicode string.
Ezio Melotti021f3342010-04-06 03:26:49 +00001289 (:meth:`~os.path.normpath` fixed by Matt Giuca in :issue:`5827`;
1290 :meth:`~os.path.abspath` fixed by Ezio Melotti in :issue:`3426`.)
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001291
Andrew M. Kuchling9cb42772009-01-21 02:15:43 +00001292* The :mod:`pydoc` module now has help for the various symbols that Python
1293 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
1294 (Contributed by David Laban; :issue:`4739`.)
1295
Ezio Melotti021f3342010-04-06 03:26:49 +00001296* The :mod:`re` module's :func:`~re.split`, :func:`~re.sub`, and :func:`~re.subn`
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001297 now accept an optional *flags* argument, for consistency with the
1298 other functions in the module. (Added by Gregory P. Smith.)
1299
Andrew M. Kuchlingca485622010-05-06 01:10:56 +00001300* New function: :func:`~runpy.run_path` in the :mod:`runpy` module
1301 will execute the code at a provided *path* argument. *path* can be
1302 the path of a Python source file (:file:`example.py`), a compiled
1303 bytecode file (:file:`example.pyc`), a directory
1304 (:file:`./package/'), or a zip archive (:file:`example.zip`). If a
1305 directory or zip path is provided, it will be added to the front of
1306 ``sys.path`` and the module :mod:`__main__` will be imported. It's
1307 expected that the directory or zip contains a :file:`__main__.py`;
1308 if it doesn't, some other :file:`__main__.py` might be imported from
1309 a location later in ``sys.path``. This makes some of the machinery
Andrew M. Kuchling837a5382010-05-06 17:21:59 +00001310 of :mod:`runpy` available to scripts that want to mimic the way
1311 Python's :option:`-m` processes an explicit path name.
1312 (Added by Nick Coghlan; :issue:`6816`.)
Andrew M. Kuchlingca485622010-05-06 01:10:56 +00001313
Ezio Melotti021f3342010-04-06 03:26:49 +00001314* New function: in the :mod:`shutil` module, :func:`~shutil.make_archive`
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +00001315 takes a filename, archive type (zip or tar-format), and a directory
1316 path, and creates an archive containing the directory's contents.
1317 (Added by Tarek Ziadé.)
1318
Ezio Melotti021f3342010-04-06 03:26:49 +00001319 :mod:`shutil`'s :func:`~shutil.copyfile` and :func:`~shutil.copytree`
1320 functions now raise a :exc:`~shutil.SpecialFileError` exception when
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001321 asked to copy a named pipe. Previously the code would treat
1322 named pipes like a regular file by opening them for reading, and
1323 this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.)
1324
1325* New functions: in the :mod:`site` module, three new functions
1326 return various site- and user-specific paths.
Ezio Melotti021f3342010-04-06 03:26:49 +00001327 :func:`~site.getsitepackages` returns a list containing all
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001328 global site-packages directories, and
Ezio Melotti021f3342010-04-06 03:26:49 +00001329 :func:`~site.getusersitepackages` returns the path of the user's
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001330 site-packages directory.
Ezio Melotti021f3342010-04-06 03:26:49 +00001331 :func:`~site.getuserbase` returns the value of the :envvar:`USER_BASE`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001332 environment variable, giving the path to a directory that can be used
1333 to store data.
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00001334 (Contributed by Tarek Ziadé; :issue:`6693`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001335
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001336 The :mod:`site` module now reports exceptions occurring
1337 when the :mod:`sitecustomize` module is imported, and will no longer
Florent Xiclunaad598332010-03-31 21:40:32 +00001338 catch and swallow the :exc:`KeyboardInterrupt` exception. (Fixed by
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001339 Victor Stinner; :issue:`3137`.)
1340
Andrew M. Kuchlingc121f132010-04-30 01:33:40 +00001341* The :func:`~socket.create_connection` function
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +00001342 gained a *source_address* parameter, a ``(host, port)`` 2-tuple
1343 giving the source address that will be used for the connection.
1344 (Contributed by Eldon Ziegler; :issue:`3972`.)
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001345
Ezio Melotti021f3342010-04-06 03:26:49 +00001346 The :meth:`~socket.socket.recv_into` and :meth:`~socket.socket.recvfrom_into`
1347 methods will now write into objects that support the buffer API, most usefully
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001348 the :class:`bytearray` and :class:`memoryview` objects. (Implemented by
1349 Antoine Pitrou; :issue:`8104`.)
1350
Ezio Melotti021f3342010-04-06 03:26:49 +00001351* The :mod:`SocketServer` module's :class:`~SocketServer.TCPServer` class now
Andrew M. Kuchling85fffc32010-05-08 01:15:26 +00001352 supports socket timeouts and disabling the Nagle algorithm.
1353 The :attr:`~SocketServer.TCPServer.disable_nagle_algorithm` class attribute
1354 defaults to False; if overridden to be True,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001355 new request connections will have the TCP_NODELAY option set to
1356 prevent buffering many small sends into a single TCP packet.
Andrew M. Kuchling85fffc32010-05-08 01:15:26 +00001357 The :attr:`~SocketServer.TCPServer.timeout` class attribute can hold
1358 a timeout in seconds that will be applied to the request socket; if
1359 no request is received within that time, :meth:`handle_timeout`
1360 will be called and :meth:`handle_request` will return.
1361 (Contributed by Kristjan Valur Jonsson; :issue:`6192` and :issue:`6267`.)
1362
1363* The XML-RPC client and server, provided by the :mod:`xmlrpclib` and
1364 :mod:`SimpleXMLRPCServer` modules, have improved performance by
1365 supporting HTTP/1.1 keep-alive and by optionally using gzip encoding
1366 to compress the XML being exchanged. The gzip compression is
1367 controlled by the :attr:`encode_threshold` attribute of
1368 :class:`SimpleXMLRPCRequestHandler`, which contains a size in bytes;
1369 responses larger than this will be compressed.
1370 (Contributed by Kristjan Valur Jonsson; :issue:`6267`.)
1371
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001372
Ezio Melotti021f3342010-04-06 03:26:49 +00001373* Updated module: the :mod:`sqlite3` module has been updated to
Andrew M. Kuchlingfed15762010-03-08 12:00:39 +00001374 version 2.6.0 of the `pysqlite package <http://code.google.com/p/pysqlite/>`__. Version 2.6.0 includes a number of bugfixes, and adds
1375 the ability to load SQLite extensions from shared libraries.
1376 Call the ``enable_load_extension(True)`` method to enable extensions,
Ezio Melotti021f3342010-04-06 03:26:49 +00001377 and then call :meth:`~sqlite3.Connection.load_extension` to load a particular shared library.
Andrew M. Kuchlingfed15762010-03-08 12:00:39 +00001378 (Updated by Gerhard Häring.)
1379
Antoine Pitroud69e6ee2010-05-07 10:15:51 +00001380* The :mod:`ssl` module's :class:`ssl.SSLSocket` objects now support the
Andrew M. Kuchlingc121f132010-04-30 01:33:40 +00001381 buffer API, which fixed a test suite failure (fix by Antoine Pitrou;
1382 :issue:`7133`) and automatically set
1383 OpenSSL's :cmacro:`SSL_MODE_AUTO_RETRY`, which will prevent an error
1384 code being returned from :meth:`recv` operations that trigger an SSL
1385 renegotiation (fix by Antoine Pitrou; :issue:`8222`).
1386
Antoine Pitroud69e6ee2010-05-07 10:15:51 +00001387 The :func:`ssl.wrap_socket` constructor function now takes a
Andrew M. Kuchlingc121f132010-04-30 01:33:40 +00001388 *ciphers* argument that's a string listing the encryption algorithms
1389 to be allowed; the format of the string is described
Antoine Pitroud69e6ee2010-05-07 10:15:51 +00001390 `in the OpenSSL documentation
1391 <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`__.
1392 (Added by Antoine Pitrou; :issue:`8322`.)
Andrew M. Kuchlingc121f132010-04-30 01:33:40 +00001393
1394 Another change makes the extension load all of OpenSSL's ciphers and
1395 digest algorithms so that they're all available. Some SSL
1396 certificates couldn't be verified, reporting an 'unknown algorithm'
1397 error. (Reported by Beda Kosata, and fixed by Antoine Pitrou;
1398 :issue:`8484`.)
1399
1400 The version of OpenSSL being used is now available as the module
Antoine Pitroud69e6ee2010-05-07 10:15:51 +00001401 attributes :data:`ssl.OPENSSL_VERSION` (a string),
1402 :data:`ssl.OPENSSL_VERSION_INFO` (a 5-tuple), and
1403 :data:`ssl.OPENSSL_VERSION_NUMBER` (an integer). (Added by Antoine
Andrew M. Kuchlingc121f132010-04-30 01:33:40 +00001404 Pitrou; :issue:`8321`.)
1405
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001406* The :mod:`struct` module will no longer silently ignore overflow
1407 errors when a value is too large for a particular integer format
1408 code (one of ``bBhHiIlLqQ``); it now always raises a
1409 :exc:`struct.error` exception. (Changed by Mark Dickinson;
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +00001410 :issue:`1523`.) The :func:`~struct.pack` function will also
1411 attempt to use :meth:`__index__` to convert and pack non-integers
1412 before trying the :meth:`__int__` method or reporting an error.
1413 (Changed by Mark Dickinson; :issue:`8300`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001414
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001415* New function: the :mod:`subprocess` module's
Ezio Melotti021f3342010-04-06 03:26:49 +00001416 :func:`~subprocess.check_output` runs a command with a specified set of arguments
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +00001417 and returns the command's output as a string when the command runs without
Ezio Melotti021f3342010-04-06 03:26:49 +00001418 error, or raises a :exc:`~subprocess.CalledProcessError` exception otherwise.
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +00001419
1420 ::
1421
1422 >>> subprocess.check_output(['df', '-h', '.'])
1423 'Filesystem Size Used Avail Capacity Mounted on\n
1424 /dev/disk0s2 52G 49G 3.0G 94% /\n'
1425
1426 >>> subprocess.check_output(['df', '-h', '/bogus'])
1427 ...
1428 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
1429
1430 (Contributed by Gregory P. Smith.)
1431
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +00001432 The :mod:`subprocess` module will now retry its internal system calls
1433 on receiving an :const:`EINTR` signal. (Reported by several people; final
1434 patch by Gregory P. Smith in :issue:`1068268`.)
1435
Ezio Melotti021f3342010-04-06 03:26:49 +00001436* New function: :func:`~symtable.is_declared_global` in the :mod:`symtable` module
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001437 returns true for variables that are explicitly declared to be global,
1438 false for ones that are implicitly global.
1439 (Contributed by Jeremy Hylton.)
1440
Andrew M. Kuchling15c82d22010-04-29 00:22:16 +00001441* The :mod:`syslog` module will now use the value of ``sys.argv[0]`` as the
1442 identifier instead of the previous default value of ``'python'``.
1443 (Changed by Sean Reifschneider; :issue:`8451`.)
1444
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +00001445* The ``sys.version_info`` value is now a named tuple, with attributes
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001446 named :attr:`major`, :attr:`minor`, :attr:`micro`,
1447 :attr:`releaselevel`, and :attr:`serial`. (Contributed by Ross
1448 Light; :issue:`4285`.)
1449
1450 :func:`sys.getwindowsversion` also returns a named tuple,
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +00001451 with attributes named :attr:`major`, :attr:`minor`, :attr:`build`,
Ezio Melotti12477752010-02-08 22:22:41 +00001452 :attr:`platform`, :attr:`service_pack`, :attr:`service_pack_major`,
Eric Smithb3c54882010-02-03 14:17:50 +00001453 :attr:`service_pack_minor`, :attr:`suite_mask`, and
1454 :attr:`product_type`. (Contributed by Brian Curtin; :issue:`7766`.)
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +00001455
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00001456* The :mod:`tarfile` module's default error handling has changed, to
1457 no longer suppress fatal errors. The default error level was previously 0,
1458 which meant that errors would only result in a message being written to the
1459 debug log, but because the debug log is not activated by default,
1460 these errors go unnoticed. The default error level is now 1,
1461 which raises an exception if there's an error.
1462 (Changed by Lars Gustäbel; :issue:`7357`.)
1463
Ezio Melotti021f3342010-04-06 03:26:49 +00001464 :mod:`tarfile` now supports filtering the :class:`~tarfile.TarInfo`
1465 objects being added to a tar file. When you call :meth:`~tarfile.TarFile.add`,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001466 instance, you may supply an optional *filter* argument
1467 that's a callable. The *filter* callable will be passed the
Ezio Melotti021f3342010-04-06 03:26:49 +00001468 :class:`~tarfile.TarInfo` for every file being added, and can modify and return it.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001469 If the callable returns ``None``, the file will be excluded from the
1470 resulting archive. This is more powerful than the existing
1471 *exclude* argument, which has therefore been deprecated.
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00001472 (Added by Lars Gustäbel; :issue:`6856`.)
Ezio Melotti021f3342010-04-06 03:26:49 +00001473 The :class:`~tarfile.TarFile` class also now supports the context manager protocol.
Andrew M. Kuchlingfed15762010-03-08 12:00:39 +00001474 (Added by Lars Gustäbel; :issue:`7232`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001475
Ezio Melotti021f3342010-04-06 03:26:49 +00001476* The :meth:`~threading.Event.wait` method of the :class:`threading.Event` class
1477 now returns the internal flag on exit. This means the method will usually
1478 return true because :meth:`~threading.Event.wait` is supposed to block until the
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001479 internal flag becomes true. The return value will only be false if
1480 a timeout was provided and the operation timed out.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001481 (Contributed by Tim Lesher; :issue:`1674032`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001482
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001483* The Unicode database provided by the :mod:`unicodedata` module is
1484 now used internally to determine which characters are numeric,
1485 whitespace, or represent line breaks. The database also
1486 includes information from the :file:`Unihan.txt` data file (patch
1487 by Anders Chrigström and Amaury Forgeot d'Arc; :issue:`1571184`)
1488 and has been updated to version 5.2.0 (updated by
1489 Florent Xicluna; :issue:`8024`).
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001490
Andrew M. Kuchling04b99cc2010-05-04 01:24:22 +00001491* The :mod:`urlparse` module's :func:`~urlparse.urlsplit` now handles
1492 unknown URL schemes in a fashion compliant with :rfc:`3986`: if the
1493 URL is of the form ``"<something>://..."``, the text before the
1494 ``://`` is treated as the scheme, even if it's a made-up scheme that
1495 the module doesn't know about. This change may break code that
1496 worked around the old behaviour. For example, Python 2.6.4 or 2.5
1497 will return the following:
1498
1499 >>> import urlparse
1500 >>> urlparse.urlsplit('invented://host/filename?query')
1501 ('invented', '', '//host/filename?query', '', '')
1502
1503 Python 2.7 (and Python 2.6.5) will return:
1504
1505 >>> import urlparse
1506 >>> urlparse.urlsplit('invented://host/filename?query')
1507 ('invented', 'host', '/filename?query', '', '')
1508
1509 (Python 2.7 actually produces slightly different output, since it
1510 returns a named tuple instead of a standard tuple.)
1511
1512 The :mod:`urlparse` module also supports IPv6 literal addresses as defined by
Andrew M. Kuchlingc121f132010-04-30 01:33:40 +00001513 :rfc:`2732` (contributed by Senthil Kumaran; :issue:`2987`). ::
1514
1515 >>> urlparse.urlparse('http://[1080::8:800:200C:417A]/foo')
1516 ParseResult(scheme='http', netloc='[1080::8:800:200C:417A]',
1517 path='/foo', params='', query='', fragment='')
1518
Ezio Melotti021f3342010-04-06 03:26:49 +00001519* The :class:`~UserDict.UserDict` class is now a new-style class. (Changed by
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001520 Benjamin Peterson.)
1521
Andrew M. Kuchlingd3b60222010-05-01 01:19:16 +00001522* New class: the :class:`~weakref.WeakSet` class in the :mod:`weakref`
1523 module is a set that only holds weak references to its elements; elements
1524 will be removed once there are no references pointing to them.
1525 (Originally implemented in Python 3.x by Raymond Hettinger, and backported
1526 to 2.7 by Michael Foord.)
1527
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001528* The ElementTree library, :mod:`xml.etree`, no longer escapes
1529 ampersands and angle brackets when outputting an XML processing
Ezio Melotti021f3342010-04-06 03:26:49 +00001530 instruction (which looks like ``<?xml-stylesheet href="#style1"?>``)
1531 or comment (which looks like ``<!-- comment -->``).
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001532 (Patch by Neil Muller; :issue:`2746`.)
1533
Andrew M. Kuchling85fffc32010-05-08 01:15:26 +00001534* The XML-RPC client and server, provided by the :mod:`xmlrpclib` and
1535 :mod:`SimpleXMLRPCServer` modules, have improved performance by
1536 supporting HTTP/1.1 keep-alive and by optionally using gzip encoding
1537 to compress the XML being exchanged. The gzip compression is
1538 controlled by the :attr:`encode_threshold` attribute of
1539 :class:`SimpleXMLRPCRequestHandler`, which contains a size in bytes;
1540 responses larger than this will be compressed.
1541 (Contributed by Kristjan Valur Jonsson; :issue:`6267`.)
1542
Ezio Melotti021f3342010-04-06 03:26:49 +00001543* The :mod:`zipfile` module's :class:`~zipfile.ZipFile` now supports the context
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00001544 management protocol, so you can write ``with zipfile.ZipFile(...) as f: ...``.
1545 (Contributed by Brian Curtin; :issue:`5511`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001546
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001547 :mod:`zipfile` now also supports archiving empty directories and
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +00001548 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001549 Reading files out of an archive is faster, and interleaving
Ezio Melotti021f3342010-04-06 03:26:49 +00001550 :meth:`~zipfile.ZipFile.read` and :meth:`~zipfile.ZipFile.readline` now works correctly.
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001551 (Contributed by Nir Aides; :issue:`7610`.)
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +00001552
Ezio Melotti021f3342010-04-06 03:26:49 +00001553 The :func:`~zipfile.is_zipfile` function now
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00001554 accepts a file object, in addition to the path names accepted in earlier
1555 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
1556
Ezio Melotti021f3342010-04-06 03:26:49 +00001557 The :meth:`~zipfile.ZipFile.writestr` method now has an optional *compress_type* parameter
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001558 that lets you override the default compression method specified in the
Ezio Melotti021f3342010-04-06 03:26:49 +00001559 :class:`~zipfile.ZipFile` constructor. (Contributed by Ronald Oussoren;
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001560 :issue:`6003`.)
1561
1562
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001563.. ======================================================================
1564.. whole new modules get described in subsections here
1565
1566
1567.. _importlib-section:
1568
1569New module: importlib
1570------------------------------
1571
1572Python 3.1 includes the :mod:`importlib` package, a re-implementation
1573of the logic underlying Python's :keyword:`import` statement.
1574:mod:`importlib` is useful for implementors of Python interpreters and
1575to users who wish to write new importers that can participate in the
1576import process. Python 2.7 doesn't contain the complete
1577:mod:`importlib` package, but instead has a tiny subset that contains
1578a single function, :func:`~importlib.import_module`.
1579
1580``import_module(name, package=None)`` imports a module. *name* is
1581a string containing the module or package's name. It's possible to do
1582relative imports by providing a string that begins with a ``.``
1583character, such as ``..utils.errors``. For relative imports, the
1584*package* argument must be provided and is the name of the package that
1585will be used as the anchor for
1586the relative import. :func:`~importlib.import_module` both inserts the imported
1587module into ``sys.modules`` and returns the module object.
1588
1589Here are some examples::
1590
1591 >>> from importlib import import_module
1592 >>> anydbm = import_module('anydbm') # Standard absolute import
1593 >>> anydbm
1594 <module 'anydbm' from '/p/python/Lib/anydbm.py'>
1595 >>> # Relative import
1596 >>> sysconfig = import_module('..sysconfig', 'distutils.command')
1597 >>> sysconfig
1598 <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'>
1599
1600:mod:`importlib` was implemented by Brett Cannon and introduced in
1601Python 3.1.
1602
1603
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001604New module: sysconfig
1605---------------------------------
1606
Andrew M. Kuchlingca485622010-05-06 01:10:56 +00001607The :mod:`sysconfig` module has been pulled out of the Distutils
1608package, becoming a new top-level module in its own right.
1609:mod:`sysconfig` provides functions for getting information about
1610Python's build process: compiler switches, installation paths, the
1611platform name, and whether Python is running from its source
1612directory.
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001613
Andrew M. Kuchlingca485622010-05-06 01:10:56 +00001614Some of the functions in the module are:
1615
1616* :func:`~sysconfig.get_config_var` returns variables from Python's
1617 Makefile and the :file:`pyconfig.h` file.
1618* :func:`~sysconfig.get_config_vars` returns a dictionary containing
1619 all of the configuration variables.
1620* :func:`~sysconfig.getpath` returns the configured path for
1621 a particular type of module: the standard library,
1622 site-specific modules, platform-specific modules, etc.
1623* :func:`~sysconfig.is_python_build` returns true if you're running a
1624 binary from a Python source tree, and false otherwise.
1625
1626Consult the :mod:`sysconfig` documentation for more details and for
1627a complete list of functions.
1628
1629The Distutils package and :mod:`sysconfig` are now maintained and
1630renamed by Tarek Ziadé.
1631
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001632
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001633ttk: Themed Widgets for Tk
1634--------------------------
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001635
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001636Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
1637widgets but have a more customizable appearance and can therefore more
1638closely resemble the native platform's widgets. This widget
1639set was originally called Tile, but was renamed to Ttk (for "themed Tk")
1640on being added to Tcl/Tck release 8.5.
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001641
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001642XXX write a brief discussion and an example here.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001643
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001644The :mod:`ttk` module was written by Guilherme Polo and added in
1645:issue:`2983`. An alternate version called ``Tile.py``, written by
1646Martin Franklin and maintained by Kevin Walzer, was proposed for
1647inclusion in :issue:`2618`, but the authors argued that Guilherme
1648Polo's work was more comprehensive.
1649
1650
1651.. _unittest-section:
Tarek Ziadé2b210692010-02-02 23:39:40 +00001652
Andrew M. Kuchlingacab9402010-05-06 17:27:57 +00001653Updated module: unittest
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001654---------------------------------
1655
Andrew M. Kuchlingb2454b22010-04-29 01:45:41 +00001656The :mod:`unittest` module was greatly enhanced; many
1657new features were added. Most of these features were implemented
Andrew M. Kuchlingacab9402010-05-06 17:27:57 +00001658by Michael Foord, unless otherwise noted. The enhanced version of
1659the module is downloadable separately for use with Python versions 2.4 to 2.6,
1660packaged as the :mod:`unittest2` package, from
1661http://pypi.python.org/pypi/unittest2.
Andrew M. Kuchlingb2454b22010-04-29 01:45:41 +00001662
Andrew M. Kuchlingc121f132010-04-30 01:33:40 +00001663When used from the command line, the module can automatically discover
1664tests. It's not as fancy as `py.test <http://pytest.org>`__ or
1665`nose <http://code.google.com/p/python-nose/>`__, but provides a simple way
1666to run tests kept within a set of package directories. For example,
1667the following command will search the :file:`test/` subdirectory for
1668any importable test files named ``test*.py``::
1669
1670 python -m unittest discover -s test
1671
1672Consult the :mod:`unittest` module documentation for more details.
1673(Developed in :issue:`6001`.)
1674
1675The :func:`main` function supports some other new options:
1676
1677* :option:`-b` or :option:`--buffer` will buffer the standard output
1678 and standard error streams during each test. If the test passes,
1679 any resulting output will be discard; on failure, the buffered
1680 output will be displayed.
1681
1682* :option:`-c` or :option:`--catch` will cause the control-C interrupt
1683 to be handled more gracefully. Instead of interrupting the test
1684 process immediately, the currently running test will be completed
1685 and then the resulting partial results will be reported. If you're
1686 impatient, a second press of control-C will cause an immediate
1687 interruption.
1688
1689 This control-C handler tries to avoid interfering when the code
1690 being tested or the tests being run have defined a signal handler of
1691 their own, by noticing that a signal handler was already set and
1692 calling it. If this doesn't work for you, there's a
1693 :func:`removeHandler` decorator that can be used to mark tests that
1694 should have the control-C handling disabled.
1695
1696* :option:`-f` or :option:`--failfast` makes
1697 test execution stop immediately when a test fails instead of
1698 continuing to execute further tests. (Suggested by Cliff Dyer and
1699 implemented by Michael Foord; :issue:`8074`.)
1700
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001701The progress messages now shows 'x' for expected failures
1702and 'u' for unexpected successes when run in verbose mode.
1703(Contributed by Benjamin Peterson.)
Andrew M. Kuchlingb2454b22010-04-29 01:45:41 +00001704
1705Test cases can raise the :exc:`~unittest.SkipTest` exception to skip a
1706test. (:issue:`1034053`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001707
Ezio Melotti021f3342010-04-06 03:26:49 +00001708The error messages for :meth:`~unittest.TestCase.assertEqual`,
1709:meth:`~unittest.TestCase.assertTrue`, and :meth:`~unittest.TestCase.assertFalse`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001710failures now provide more information. If you set the
Ezio Melotti021f3342010-04-06 03:26:49 +00001711:attr:`~unittest.TestCase.longMessage` attribute of your :class:`~unittest.TestCase` classes to
1712True, both the standard error message and any additional message you
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001713provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
1714
Ezio Melotti021f3342010-04-06 03:26:49 +00001715The :meth:`~unittest.TestCase.assertRaises` method now
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001716return a context handler when called without providing a callable
1717object to run. For example, you can write this::
1718
1719 with self.assertRaises(KeyError):
Ezio Melotti021f3342010-04-06 03:26:49 +00001720 {}['foo']
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001721
1722(Implemented by Antoine Pitrou; :issue:`4444`.)
1723
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001724.. rev 78774
1725
1726Module- and class-level setup and teardown fixtures are now supported.
Ezio Melotti021f3342010-04-06 03:26:49 +00001727Modules can contain :func:`~unittest.setUpModule` and :func:`~unittest.tearDownModule`
1728functions. Classes can have :meth:`~unittest.TestCase.setUpClass` and
1729:meth:`~unittest.TestCase.tearDownClass` methods that must be defined as class methods
1730(using ``@classmethod`` or equivalent). These functions and
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001731methods are invoked when the test runner switches to a test case in a
1732different module or class.
1733
Ezio Melotti021f3342010-04-06 03:26:49 +00001734The methods :meth:`~unittest.TestCase.addCleanup` and
1735:meth:`~unittest.TestCase.doCleanups` were added.
1736:meth:`~unittest.TestCase.addCleanup` allows you to add cleanup functions that
1737will be called unconditionally (after :meth:`~unittest.TestCase.setUp` if
1738:meth:`~unittest.TestCase.setUp` fails, otherwise after :meth:`~unittest.TestCase.tearDown`). This allows
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001739for much simpler resource allocation and deallocation during tests
1740(:issue:`5679`).
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001741
1742A number of new methods were added that provide more specialized
1743tests. Many of these methods were written by Google engineers
1744for use in their test suites; Gregory P. Smith, Michael Foord, and
1745GvR worked on merging them into Python's version of :mod:`unittest`.
1746
Ezio Melotti021f3342010-04-06 03:26:49 +00001747* :meth:`~unittest.TestCase.assertIsNone` and :meth:`~unittest.TestCase.assertIsNotNone` take one
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001748 expression and verify that the result is or is not ``None``.
1749
Ezio Melotti021f3342010-04-06 03:26:49 +00001750* :meth:`~unittest.TestCase.assertIs` and :meth:`~unittest.TestCase.assertIsNot`
1751 take two values and check whether the two values evaluate to the same object or not.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001752 (Added by Michael Foord; :issue:`2578`.)
1753
Ezio Melotti021f3342010-04-06 03:26:49 +00001754* :meth:`~unittest.TestCase.assertIsInstance` and
1755 :meth:`~unittest.TestCase.assertNotIsInstance` check whether
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001756 the resulting object is an instance of a particular class, or of
1757 one of a tuple of classes. (Added by Georg Brandl; :issue:`7031`.)
1758
Ezio Melotti021f3342010-04-06 03:26:49 +00001759* :meth:`~unittest.TestCase.assertGreater`, :meth:`~unittest.TestCase.assertGreaterEqual`,
1760 :meth:`~unittest.TestCase.assertLess`, and :meth:`~unittest.TestCase.assertLessEqual` compare
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001761 two quantities.
1762
Ezio Melotti021f3342010-04-06 03:26:49 +00001763* :meth:`~unittest.TestCase.assertMultiLineEqual` compares two strings, and if they're
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001764 not equal, displays a helpful comparison that highlights the
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001765 differences in the two strings. This comparison is now used by
Ezio Melotti021f3342010-04-06 03:26:49 +00001766 default when Unicode strings are compared with :meth:`~unittest.TestCase.assertEqual`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001767
Andrew M. Kuchlingb2454b22010-04-29 01:45:41 +00001768* :meth:`~unittest.TestCase.assertRegexpMatches` and
1769 :meth:`~unittest.TestCase.assertNotRegexpMatches` checks whether the
1770 first argument is a string matching or not matching the regular
1771 expression provided as the second argument (:issue:`8038`).
Ezio Melotti021f3342010-04-06 03:26:49 +00001772
1773* :meth:`~unittest.TestCase.assertRaisesRegexp` checks whether a particular exception
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001774 is raised, and then also checks that the string representation of
1775 the exception matches the provided regular expression.
1776
Ezio Melotti021f3342010-04-06 03:26:49 +00001777* :meth:`~unittest.TestCase.assertIn` and :meth:`~unittest.TestCase.assertNotIn`
1778 tests whether *first* is or is not in *second*.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001779
Ezio Melotti021f3342010-04-06 03:26:49 +00001780* :meth:`~unittest.TestCase.assertItemsEqual` tests whether two provided sequences
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001781 contain the same elements.
1782
Ezio Melotti021f3342010-04-06 03:26:49 +00001783* :meth:`~unittest.TestCase.assertSetEqual` compares whether two sets are equal, and
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001784 only reports the differences between the sets in case of error.
1785
Ezio Melotti021f3342010-04-06 03:26:49 +00001786* Similarly, :meth:`~unittest.TestCase.assertListEqual` and :meth:`~unittest.TestCase.assertTupleEqual`
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001787 compare the specified types and explain any differences without necessarily
1788 printing their full values; these methods are now used by default
Ezio Melotti021f3342010-04-06 03:26:49 +00001789 when comparing lists and tuples using :meth:`~unittest.TestCase.assertEqual`.
1790 More generally, :meth:`~unittest.TestCase.assertSequenceEqual` compares two sequences
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001791 and can optionally check whether both sequences are of a
1792 particular type.
1793
Ezio Melotti021f3342010-04-06 03:26:49 +00001794* :meth:`~unittest.TestCase.assertDictEqual` compares two dictionaries and reports the
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001795 differences; it's now used by default when you compare two dictionaries
Ezio Melotti021f3342010-04-06 03:26:49 +00001796 using :meth:`~unittest.TestCase.assertEqual`. :meth:`~unittest.TestCase.assertDictContainsSubset` checks whether
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001797 all of the key/value pairs in *first* are found in *second*.
1798
Ezio Melotti021f3342010-04-06 03:26:49 +00001799* :meth:`~unittest.TestCase.assertAlmostEqual` and :meth:`~unittest.TestCase.assertNotAlmostEqual` test
Andrew M. Kuchlingb2454b22010-04-29 01:45:41 +00001800 whether *first* and *second* are approximately equal. This method
1801 can either round their difference to an optionally-specified number
1802 of *places* (the default is 7) and compare it to zero, or require
1803 the difference to be smaller than a supplied *delta* value.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001804
Ezio Melotti021f3342010-04-06 03:26:49 +00001805* :meth:`~unittest.TestLoader.loadTestsFromName` properly honors the
1806 :attr:`~unittest.TestLoader.suiteClass` attribute of
1807 the :class:`~unittest.TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001808
Ezio Melotti021f3342010-04-06 03:26:49 +00001809* A new hook lets you extend the :meth:`~unittest.TestCase.assertEqual` method to handle
1810 new data types. The :meth:`~unittest.TestCase.addTypeEqualityFunc` method takes a type
Andrew M. Kuchling9858f632010-03-23 18:39:24 +00001811 object and a function. The function will be used when both of the
1812 objects being compared are of the specified type. This function
1813 should compare the two objects and raise an exception if they don't
1814 match; it's a good idea for the function to provide additional
1815 information about why the two objects are matching, much as the new
1816 sequence comparison methods do.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001817
Andrew M. Kuchling9858f632010-03-23 18:39:24 +00001818:func:`unittest.main` now takes an optional ``exit`` argument. If
Ezio Melotti021f3342010-04-06 03:26:49 +00001819False, :func:`~unittest.main` doesn't call :func:`sys.exit`, allowing it to be
Andrew M. Kuchling9858f632010-03-23 18:39:24 +00001820used from the interactive interpreter. (Contributed by J. Pablo
1821Fernández; :issue:`3379`.)
1822
Ezio Melotti021f3342010-04-06 03:26:49 +00001823:class:`~unittest.TestResult` has new :meth:`~unittest.TestResult.startTestRun` and
1824:meth:`~unittest.TestResult.stopTestRun` methods that are called immediately before
Andrew M. Kuchling9858f632010-03-23 18:39:24 +00001825and after a test run. (Contributed by Robert Collins; :issue:`5728`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001826
1827With all these changes, the :file:`unittest.py` was becoming awkwardly
1828large, so the module was turned into a package and the code split into
1829several files (by Benjamin Peterson). This doesn't affect how the
Andrew M. Kuchlingb2454b22010-04-29 01:45:41 +00001830module is imported or used.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001831
1832
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001833.. _elementtree-section:
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001834
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001835Updated module: ElementTree 1.3
1836---------------------------------
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001837
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001838The version of the ElementTree library included with Python was updated to
1839version 1.3. Some of the new features in ElementTree 1.3 are:
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001840
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001841* The various parsing functions now take a *parser* keyword argument
1842 that can be used to provide an :class:`XMLParser` instance that will
1843 be used. This makes it possible to override the file's internal encoding:
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001844
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001845 p = ET.XMLParser(encoding='utf-8')
1846 t = ET.XML("""<root/>""", parser=p)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001847
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001848 Errors in parsing XML now raise a :exc:`ParseError` exception.
1849 Instances of :exc:`ParseError` have a :attr:`position` attribute
1850 containing a (*line*, *column*) tuple giving the location of the problem.
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001851
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001852* ElementTree's code for converting trees to a string has been
1853 significantly reworked, making it roughly twice as fast in many
1854 cases. The :class:`ElementTree` :meth:`write` and :class:`Element`
1855 :meth:`write` methods now have a *method* parameter that can be
1856 "xml" (the default), "html", or "text". HTML mode will output empty
1857 elements as ``<empty></empty>`` instead of ``<empty/>``, and text
1858 mode will skip over elements and only output the text chunks. If
1859 you set the :attr:`tag` attribute of an element to ``None`` but
1860 leaves its children in place, the element will be omitted when the
1861 tree is written out, so you don't need to do more extensive rearrangement
1862 to remove a single element.
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001863
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001864 Namespace aspects have also been improved. All the ``xmlns:<whatever>``
1865 declarations are now put on the root element and not scattered throughout
1866 the resulting output. You can set the default namespace for a tree
1867 by setting the :attr:`default_namespace` attribute and can
1868 register new prefixes with :meth:`regsiter_namespace`. In XML mode,
1869 you can use the true/false *xml_declaration* parameter to suppress the
1870 XML declaration.
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001871
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001872* New :class:`Element` method: :meth:`extend` appends the items from a
1873 sequence to the element's children. Elements themselves behave like
1874 sequences, so it's easy to move children from one element to
1875 another::
Andrew M. Kuchlinga17cd4a2009-01-31 02:50:09 +00001876
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001877 from xml.etree import ElementTree as ET
Andrew M. Kuchlinga17cd4a2009-01-31 02:50:09 +00001878
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001879 t = ET.XML("""<list>
1880 <item>1</item> <item>2</item> <item>3</item>
1881 </list>""")
1882 new = ET.XML('<root/>')
1883 new.extend(t)
Andrew M. Kuchlinga17cd4a2009-01-31 02:50:09 +00001884
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001885 # Outputs <root><item>1</item>...</root>
1886 print ET.tostring(new)
Andrew M. Kuchlinga17cd4a2009-01-31 02:50:09 +00001887
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001888* New :class:`Element` method: :meth:`iter` yields the children of the
1889 element as a generator. It's also possible to write ``for child in
1890 elem: ...`` to loop over an element's children. The existing method
1891 :meth:`getiterator` is now deprecated. :meth:`getchildren` is
1892 another similar method that constructs and returns a list of
1893 children; it's also deprecated.
Georg Brandl0516f812009-11-18 18:52:35 +00001894
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001895* New :class:`Element` method: :meth:`itertext` yields all chunks of
1896 text that are descendants of the element. For example::
Georg Brandl0516f812009-11-18 18:52:35 +00001897
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00001898 t = ET.XML("""<list>
1899 <item>1</item> <item>2</item> <item>3</item>
1900 </list>""")
1901
1902 # Outputs ['\n ', '1', ' ', '2', ' ', '3', '\n']
1903 print list(t.itertext())
1904
1905* Deprecated: using an element as a Boolean (i.e., ``if elem: ...``)
1906 would return true if the element had any children, or false if
1907 there were no children. This behaviour will eventually change or be removed
1908 because it's confusing (``None`` is false, but so is a childless element?),
1909 so it will now trigger a :exc:`FutureWarning`. In your code,
1910 you should be explicit: write ``len(elem) != 0`` if you're interested in
1911 the number of children, or ``elem is not None`` Instead,
1912
1913Fredrik Lundh develops ElementTree and produced the 1.3 version;
1914you can read his article describing 1.3 at
1915http://effbot.org/zone/elementtree-13-intro.htm.
1916Florent Xicluna updated the version included with
1917Python, after discussions on python-dev and in :issue:`6472`.)
Georg Brandl0516f812009-11-18 18:52:35 +00001918
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001919.. ======================================================================
1920
1921
1922Build and C API Changes
1923=======================
1924
1925Changes to Python's build process and to the C API include:
1926
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +00001927* The latest release of the GNU Debugger, GDB 7, can be `scripted
1928 using Python
1929 <http://sourceware.org/gdb/current/onlinedocs/gdb/Python.html>`__.
1930 When you begin debugging an executable program P, GDB will look for
1931 a file named ``P-gdb.py`` and automatically read it. Dave Malcolm
1932 contributed a :file:`python-gdb.py` that adds a number of useful
1933 commands when debugging Python itself. For example, there are
1934 ``py-up`` and ``py-down`` that go up or down one Python stack frame,
1935 which usually corresponds to several C stack frames. ``py-print``
1936 prints the value of a Python variable, and ``py-bt`` prints the
1937 Python stack trace. (Added as a result of :issue:`8032`.)
1938
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +00001939* If you use the :file:`.gdbinit` file provided with Python,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001940 the "pyo" macro in the 2.7 version now works correctly when the thread being
1941 debugged doesn't hold the GIL; the macro now acquires it before printing.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001942 (Contributed by Victor Stinner; :issue:`3632`.)
1943
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +00001944* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001945 worker thread submit notifications to the main Python thread. This
1946 is particularly useful for asynchronous IO operations.
1947 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
1948
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001949* New function: :cfunc:`PyCode_NewEmpty` creates an empty code object;
1950 only the filename, function name, and first line number are required.
1951 This is useful to extension modules that are attempting to
1952 construct a more useful traceback stack. Previously such
1953 extensions needed to call :cfunc:`PyCode_New`, which had many
1954 more arguments. (Added by Jeffrey Yasskin.)
1955
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001956* New function: :cfunc:`PyErr_NewExceptionWithDoc` creates a new
1957 exception class, just as the existing :cfunc:`PyErr_NewException` does,
1958 but takes an extra ``char *`` argument containing the docstring for the
1959 new exception class. (Added by the 'lekma' user on the Python bug tracker;
1960 :issue:`7033`.)
1961
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001962* New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object
1963 and returns the line number that the frame is currently executing.
1964 Previously code would need to get the index of the bytecode
1965 instruction currently executing, and then look up the line number
1966 corresponding to that address. (Added by Jeffrey Yasskin.)
1967
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001968* New functions: :cfunc:`PyLong_AsLongAndOverflow` and
1969 :cfunc:`PyLong_AsLongLongAndOverflow` approximates a Python long
1970 integer as a C :ctype:`long` or :ctype:`long long`.
1971 If the number is too large to fit into
1972 the output type, an *overflow* flag is set and returned to the caller.
1973 (Contributed by Case Van Horsen; :issue:`7528` and :issue:`7767`.)
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001974
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001975* New function: stemming from the rewrite of string-to-float conversion,
1976 a new :cfunc:`PyOS_string_to_double` function was added. The old
1977 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions
1978 are now deprecated.
1979
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001980* New macros: the Python header files now define the following macros:
1981 :cmacro:`Py_ISALNUM`,
1982 :cmacro:`Py_ISALPHA`,
1983 :cmacro:`Py_ISDIGIT`,
1984 :cmacro:`Py_ISLOWER`,
1985 :cmacro:`Py_ISSPACE`,
1986 :cmacro:`Py_ISUPPER`,
1987 :cmacro:`Py_ISXDIGIT`,
1988 and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`.
1989 All of these functions are analogous to the C
1990 standard macros for classifying characters, but ignore the current
1991 locale setting, because in
1992 several places Python needs to analyze characters in a
1993 locale-independent way. (Added by Eric Smith;
1994 :issue:`5793`.)
1995
1996 .. XXX these macros don't seem to be described in the c-api docs.
1997
Andrew M. Kuchlingd3b60222010-05-01 01:19:16 +00001998* Removed function: :cmacro:`PyEval_CallObject` is now only available
1999 as a macro. A function version was being kept around to preserve
2000 ABI linking compatibility, but that was in 1997; it can certainly be
2001 deleted. (Removed by Antoine Pitrou; :issue:`8276`.)
2002
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00002003* New format codes: the :cfunc:`PyFormat_FromString`,
2004 :cfunc:`PyFormat_FromStringV`, and :cfunc:`PyErr_Format` now
2005 accepts ``%lld`` and ``%llu`` format codes for displaying values of
2006 C's :ctype:`long long` types.
2007 (Contributed by Mark Dickinson; :issue:`7228`.)
2008
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00002009* The complicated interaction between threads and process forking has
2010 been changed. Previously, the child process created by
2011 :func:`os.fork` might fail because the child is created with only a
2012 single thread running, the thread performing the :func:`os.fork`.
2013 If other threads were holding a lock, such as Python's import lock,
2014 when the fork was performed, the lock would still be marked as
2015 "held" in the new process. But in the child process nothing would
2016 ever release the lock, since the other threads weren't replicated,
2017 and the child process would no longer be able to perform imports.
2018
2019 Python 2.7 now acquires the import lock before performing an
2020 :func:`os.fork`, and will also clean up any locks created using the
2021 :mod:`threading` module. C extension modules that have internal
2022 locks, or that call :cfunc:`fork()` themselves, will not benefit
2023 from this clean-up.
2024
2025 (Fixed by Thomas Wouters; :issue:`1590864`.)
2026
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00002027* The :cfunc:`Py_Finalize` function now calls the internal
2028 :func:`threading._shutdown` function; this prevents some exceptions from
2029 being raised when an interpreter shuts down.
2030 (Patch by Adam Olsen; :issue:`1722344`.)
2031
Andrew M. Kuchling15c82d22010-04-29 00:22:16 +00002032* When using the :ctype:`PyMemberDef` structure to define attributes
2033 of a type, Python will no longer let you try to delete or set a
2034 :const:`T_STRING_INPLACE` attribute.
2035
2036 .. rev 79644
2037
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00002038* Global symbols defined by the :mod:`ctypes` module are now prefixed
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00002039 with ``Py``, or with ``_ctypes``. (Implemented by Thomas
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00002040 Heller; :issue:`3102`.)
2041
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00002042* New configure option: the :option:`--with-system-expat` switch allows
2043 building the :mod:`pyexpat` module to use the system Expat library.
2044 (Contributed by Arfrever Frehtes Taifersar Arahesis; :issue:`7609`.)
2045
Andrew M. Kuchlingce690522010-04-13 01:32:51 +00002046* New configure option: compiling Python with the
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00002047 :option:`--with-valgrind` option will now disable the pymalloc
Andrew M. Kuchlingce690522010-04-13 01:32:51 +00002048 allocator, which is difficult for the Valgrind memory-error detector
2049 to analyze correctly.
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00002050 Valgrind will therefore be better at detecting memory leaks and
2051 overruns. (Contributed by James Henstridge; :issue:`2422`.)
2052
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00002053* New configure option: you can now supply no arguments to
2054 :option:`--with-dbmliborder=` in order to build none of the various
2055 DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis;
2056 :issue:`6491`.)
2057
Andrew M. Kuchling77069572009-03-31 01:21:01 +00002058* The :program:`configure` script now checks for floating-point rounding bugs
2059 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
2060 preprocessor definition. No code currently uses this definition,
2061 but it's available if anyone wishes to use it.
2062 (Added by Mark Dickinson; :issue:`2937`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00002063
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +00002064 :program:`configure` also now sets a :envvar:`LDCXXSHARED` Makefile
2065 variable for supporting C++ linking. (Contributed by Arfrever
2066 Frehtes Taifersar Arahesis; :issue:`1222585`.)
2067
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00002068* The build process now creates the necessary files for pkg-config
2069 support. (Contributed by Clinton Roy; :issue:`3585`.)
2070
2071* The build process now supports Subversion 1.7. (Contributed by
2072 Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.)
2073
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00002074
Andrew M. Kuchling85fffc32010-05-08 01:15:26 +00002075.. _whatsnew27-capsules:
2076
2077Capsules
2078-------------------
2079
2080Python 3.1 adds a new C datatype, :ctype:`PyCapsule`, for providing a
2081C API to an extension module. A capsule is essentially the holder for
2082a C ``void *`` pointer, and is bound to a module attribute; for
2083example, the :mod:`socket` module's API is exposed as ``socket.CAPI`,
2084and :mod:`unicodedata` calls it ``ucnhash_CAPI``. Other extensions
2085can import the module, access its dictionary to get the capsule
2086object, and then get the ``void *`` pointer, which will usually point
2087to an array of pointers to the various API functions.
2088
2089There is an existing data type that already does this,
2090:ctype:`PyCObject`, but it doesn't provide type safety. Evil code
2091written in pure Python could cause a segmentation fault by taking a
2092:ctype:`PyCObject` from module A and somehow substituting it for the
2093:ctype:`PyCObject` in module B. Capsules know their own name,
2094and getting the pointer requires providing the name::
2095
2096 void *vtable;
2097
2098 if (!PyCapsule_IsValid(capsule, "mymodule.CAPI") {
2099 PyErr_SetString(PyExc_ValueError, "argument type invalid");
2100 return NULL;
2101 }
2102
2103 vtable = PyCapsule_GetPointer(capsule, "mymodule.CAPI");
2104
2105You are assured that ``vtable`` points to whatever you're expecting.
2106If a different capsule was passed in, :cfunc:`PyCapsule_IsValid` would
2107detect the mismatched name and return false. Refer to
2108:ref:`using-capsules` for more information on using these objects.
2109
2110Python 2.7 now uses capsules internally to provide various
2111extension-module APIs, but the :cfunc:`PyCObject_AsVoidPtr` was
2112modified to handle capsules, preserving compile-time compatibility
2113with the :ctype:`CObject` interface. Use of
2114:cfunc:`PyCObject_AsVoidPtr` will signal a
2115:exc:`PendingDeprecationWarning`, which is silent by default.
2116
2117Implemented in Python 3.1 and backported to 2.7 by Larry Hastings;
2118discussed in :issue:`5630`.
2119
2120
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00002121.. ======================================================================
2122
2123Port-Specific Changes: Windows
2124-----------------------------------
2125
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +00002126* The :mod:`msvcrt` module now contains some constants from
2127 the :file:`crtassem.h` header file:
2128 :data:`CRT_ASSEMBLY_VERSION`,
2129 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
2130 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00002131 (Contributed by David Cournapeau; :issue:`4365`.)
2132
Andrew M. Kuchling15c82d22010-04-29 00:22:16 +00002133* The :mod:`_winreg` module for accessing the registry now implements
2134 the :func:`CreateKeyEx` and :func:`DeleteKeyEx` functions, extended
2135 versions of previously-supported functions that take several extra
2136 arguments. The :func:`DisableReflectionKey`,
2137 :func:`EnableReflectionKey`, and :func:`QueryReflectionKey` were also
2138 tested and documented.
2139 (Implemented by Brian Curtin: :issue:`7347`.)
2140
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00002141* The new :cfunc:`_beginthreadex` API is used to start threads, and
2142 the native thread-local storage functions are now used.
2143 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00002144
Andrew M. Kuchling363dbcc2010-04-14 23:55:17 +00002145* The :func:`os.kill` function now works on Windows. The signal value
2146 can be the constants :const:`CTRL_C_EVENT`,
2147 :const:`CTRL_BREAK_EVENT`, or any integer. The Control-C and
2148 Control-Break keystroke events can be sent to subprocesses; any
2149 other value will use the :cfunc:`TerminateProcess` API.
2150 (Contributed by Miki Tebeka; :issue:`1220212`.)
2151
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00002152* The :func:`os.listdir` function now correctly fails
2153 for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.)
2154
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00002155* The :mod:`mimelib` module will now read the MIME database from
2156 the Windows registry when initializing.
2157 (Patch by Gabriel Genellina; :issue:`4969`.)
2158
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00002159.. ======================================================================
2160
2161Port-Specific Changes: Mac OS X
2162-----------------------------------
2163
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00002164* The path ``/Library/Python/2.7/site-packages`` is now appended to
Andrew M. Kuchling77069572009-03-31 01:21:01 +00002165 ``sys.path``, in order to share added packages between the system
2166 installation and a user-installed copy of the same version.
2167 (Changed by Ronald Oussoren; :issue:`4865`.)
2168
Andrew M. Kuchling04b99cc2010-05-04 01:24:22 +00002169Port-Specific Changes: FreeBSD
2170-----------------------------------
2171
2172* FreeBSD 7.1's :const:`SO_SETFIB` constant, used with
2173 :func:`~socket.getsockopt`/:func:`~socket.setsockopt` to select an
2174 alternate routing table, is now available in the :mod:`socket`
2175 module. (Added by Kyle VanderBeek; :issue:`8235`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00002176
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00002177Other Changes and Fixes
2178=======================
2179
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00002180* Two benchmark scripts, :file:`iobench` and :file:`ccbench`, were
2181 added to the :file:`Tools` directory. :file:`iobench` measures the
Antoine Pitroudde96e62010-02-08 20:25:47 +00002182 speed of built-in file I/O objects (as returned by :func:`open`)
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00002183 while performing various operations, and :file:`ccbench` is a
2184 concurrency benchmark that tries to measure computing throughput,
2185 thread switching latency, and IO processing bandwidth when
2186 performing several tasks using a varying number of threads.
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00002187
Andrew M. Kuchling77069572009-03-31 01:21:01 +00002188* When importing a module from a :file:`.pyc` or :file:`.pyo` file
2189 with an existing :file:`.py` counterpart, the :attr:`co_filename`
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00002190 attributes of the resulting code objects are overwritten when the
2191 original filename is obsolete. This can happen if the file has been
2192 renamed, moved, or is accessed through different paths. (Patch by
2193 Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.)
Andrew M. Kuchling77069572009-03-31 01:21:01 +00002194
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00002195* The :file:`regrtest.py` script now takes a :option:`--randseed=`
2196 switch that takes an integer that will be used as the random seed
2197 for the :option:`-r` option that executes tests in random order.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00002198 The :option:`-r` option also reports the seed that was used
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00002199 (Added by Collin Winter.)
2200
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00002201* Another :file:`regrtest.py` switch is :option:`-j`, which
2202 takes an integer specifying how many tests run in parallel. This
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00002203 allows reducing the total runtime on multi-core machines.
Antoine Pitrou4698d992009-05-31 14:20:14 +00002204 This option is compatible with several other options, including the
2205 :option:`-R` switch which is known to produce long runtimes.
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00002206 (Added by Antoine Pitrou, :issue:`6152`.) This can also be used
2207 with a new :option:`-F` switch that runs selected tests in a loop
2208 until they fail. (Added by Antoine Pitrou; :issue:`7312`.)
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00002209
Andrew M. Kuchling85f928a2010-04-15 01:42:27 +00002210* When executed as a script, the :file:`py_compile.py` module now
2211 accepts ``'-'`` as an argument, which will read standard input for
2212 the list of filenames to be compiled. (Contributed by Piotr
2213 Ożarowski; :issue:`8233`.)
2214
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00002215.. ======================================================================
2216
2217Porting to Python 2.7
2218=====================
2219
2220This section lists previously described changes and other bugfixes
2221that may require changes to your code:
2222
Andrew M. Kuchlinge86b7fe2010-05-06 14:14:09 +00002223* The :func:`range` function processes its arguments more
2224 consistently; it will now call :meth:`__int__` on non-float,
2225 non-integer arguments that are supplied to it. (Fixed by Alexander
2226 Belopolsky; :issue:`1533`.)
2227
Andrew M. Kuchling15c82d22010-04-29 00:22:16 +00002228* The string :meth:`format` method changed the default precision used
2229 for floating-point and complex numbers from 6 decimal
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00002230 places to 12, which matches the precision used by :func:`str`.
2231 (Changed by Eric Smith; :issue:`5920`.)
2232
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00002233* Because of an optimization for the :keyword:`with` statement, the special
2234 methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's
2235 type, and cannot be directly attached to the object's instance. This
Amaury Forgeot d'Arcd81333c2009-06-10 20:30:19 +00002236 affects new-style classes (derived from :class:`object`) and C extension
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00002237 types. (:issue:`6101`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00002238
Andrew M. Kuchling837a5382010-05-06 17:21:59 +00002239* Due to a bug in Python 2.6, the *exc_value* parameter to
2240 :meth:`__exit__` methods was often the string representation of the
2241 exception, not an instance. This was fixed in 2.7, so *exc_value*
2242 will be an instance as expected. (Fixed by Florent Xicluna;
2243 :issue:`7853`.)
2244
Andrew M. Kuchlingd3b60222010-05-01 01:19:16 +00002245* When a restricted set of attributes were set using ``__slots__``,
2246 deleting an unset attribute would not raise :exc:`AttributeError`
2247 as you would expect. Fixed by Benjamin Peterson; :issue:`7604`.)
Andrew M. Kuchling15c82d22010-04-29 00:22:16 +00002248
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00002249In the standard library:
2250
Andrew M. Kuchling15c82d22010-04-29 00:22:16 +00002251* When using :class:`Decimal` instances with a string's
2252 :meth:`format` method, the default alignment was previously
2253 left-alignment. This has been changed to right-alignment, which might
2254 change the output of your programs.
2255 (Changed by Mark Dickinson; :issue:`6857`.)
2256
Andrew M. Kuchlingc121f132010-04-30 01:33:40 +00002257 Comparisons involving a signaling NaN value (or ``sNAN``) now signal
2258 :const:`InvalidOperation` instead of silently returning a true or
2259 false value depending on the comparison operator. Quiet NaN values
2260 (or ``NaN``) are now hashable. (Fixed by Mark Dickinson;
2261 :issue:`7279`.)
2262
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00002263* The ElementTree library, :mod:`xml.etree`, no longer escapes
2264 ampersands and angle brackets when outputting an XML processing
2265 instruction (which looks like `<?xml-stylesheet href="#style1"?>`)
2266 or comment (which looks like `<!-- comment -->`).
2267 (Patch by Neil Muller; :issue:`2746`.)
2268
Andrew M. Kuchlingd3b60222010-05-01 01:19:16 +00002269* The :meth:`readline` method of :class:`StringIO` objects now does
2270 nothing when a negative length is requested, as other file-like
2271 objects do. (:issue:`7348`).
2272
Andrew M. Kuchling15c82d22010-04-29 00:22:16 +00002273* The :mod:`syslog` module will now use the value of ``sys.argv[0]`` as the
2274 identifier instead of the previous default value of ``'python'``.
2275 (Changed by Sean Reifschneider; :issue:`8451`.)
2276
Andrew M. Kuchling04b99cc2010-05-04 01:24:22 +00002277* The :mod:`urlparse` module's :func:`~urlparse.urlsplit` now handles
2278 unknown URL schemes in a fashion compliant with :rfc:`3986`: if the
2279 URL is of the form ``"<something>://..."``, the text before the
2280 ``://`` is treated as the scheme, even if it's a made-up scheme that
2281 the module doesn't know about. This change may break code that
2282 worked around the old behaviour. For example, Python 2.6.4 or 2.5
2283 will return the following:
2284
2285 >>> import urlparse
2286 >>> urlparse.urlsplit('invented://host/filename?query')
2287 ('invented', '', '//host/filename?query', '', '')
2288
2289 Python 2.7 (and Python 2.6.5) will return:
2290
2291 >>> import urlparse
2292 >>> urlparse.urlsplit('invented://host/filename?query')
2293 ('invented', 'host', '/filename?query', '', '')
2294
2295 (Python 2.7 actually produces slightly different output, since it
2296 returns a named tuple instead of a standard tuple.)
2297
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00002298For C extensions:
2299
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00002300* C extensions that use integer format codes with the ``PyArg_Parse*``
2301 family of functions will now raise a :exc:`TypeError` exception
2302 instead of triggering a :exc:`DeprecationWarning` (:issue:`5080`).
2303
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00002304* Use the new :cfunc:`PyOS_string_to_double` function instead of the old
2305 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions,
2306 which are now deprecated.
2307
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00002308
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00002309.. ======================================================================
2310
2311
2312.. _acks27:
2313
2314Acknowledgements
2315================
2316
2317The author would like to thank the following people for offering
2318suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchlingc121f132010-04-30 01:33:40 +00002319article: Nick Coghlan, Ryan Lovett, R. David Murray, Hugh Secker-Walker.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00002320