blob: dcea4f20674e97925c3c235f99f49c21e38d6367 [file] [log] [blame]
Benjamin Petersonf10a79a2008-10-11 00:49:57 +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
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00009.. Fix accents on Kristjan Valur Jonsson, Fuerstenau
Benjamin Peterson1010bf32009-01-30 04:00:29 +000010
Ezio Melotti6c96ffe2010-04-07 04:27:14 +000011.. Big jobs: argparse, ElementTree 1.3, pep 391, 3106, sysconfig
12.. unittest test discovery
13
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000014.. $Id$
15 Rules for maintenance:
16
17 * Anyone can add text to this document. Do not spend very much time
18 on the wording of your changes, because your text will probably
19 get rewritten to some degree.
20
21 * The maintainer will go through Misc/NEWS periodically and add
22 changes; it's therefore more important to add your changes to
23 Misc/NEWS than to this file.
24
25 * This is not a complete list of every single change; completeness
26 is the purpose of Misc/NEWS. Some changes I consider too small
27 or esoteric to include. If such a change is added to the text,
28 I'll just remove it. (This is another reason you shouldn't spend
29 too much time on writing your addition.)
30
31 * If you want to draw your new text to the attention of the
32 maintainer, add 'XXX' to the beginning of the paragraph or
33 section.
34
35 * It's OK to just add a fragmentary note about a change. For
36 example: "XXX Describe the transmogrify() function added to the
37 socket module." The maintainer will research the change and
38 write the necessary text.
39
40 * You can comment out your additions if you like, but it's not
41 necessary (especially when a final release is some months away).
42
Ezio Melotti6c96ffe2010-04-07 04:27:14 +000043 * Credit the author of a patch or bugfix. Just the name is
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000044 sufficient; the e-mail address isn't necessary.
45
46 * It's helpful to add the bug/patch number in a parenthetical comment.
47
48 XXX Describe the transmogrify() function added to the socket
49 module.
50 (Contributed by P.Y. Developer; :issue:`12345`.)
51
52 This saves the maintainer some effort going through the SVN logs
53 when researching a change.
54
Benjamin Peterson9eea4802009-12-31 03:31:15 +000055This article explains the new features in Python 2.7. The final
56release of 2.7 is currently scheduled for June 2010; the detailed
57schedule is described in :pep:`373`.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000058
Benjamin Petersond69fe2a2010-02-03 02:59:43 +000059Python 2.7 is planned to be the last major release in the 2.x series.
60Though more major releases have not been absolutely ruled out, it's
61likely that the 2.7 release will have an extended period of
62maintenance compared to earlier 2.x versions.
63
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000064.. Compare with previous release in 2 - 3 sentences here.
65 add hyperlink when the documentation becomes available online.
66
Benjamin Petersonf6489f92009-11-25 17:46:26 +000067.. _whatsnew27-python31:
68
69Python 3.1 Features
70=======================
Benjamin Petersond23f8222009-04-05 19:13:16 +000071
72Much as Python 2.6 incorporated features from Python 3.0,
Benjamin Petersonf6489f92009-11-25 17:46:26 +000073version 2.7 incorporates some of the new features
74in Python 3.1. The 2.x series continues to provide tools
75for migrating to the 3.x series.
Benjamin Petersond23f8222009-04-05 19:13:16 +000076
Benjamin Petersonf6489f92009-11-25 17:46:26 +000077A partial list of 3.1 features that were backported to 2.7:
78
79* A version of the :mod:`io` library, rewritten in C for performance.
80* The ordered-dictionary type described in :ref:`pep-0372`.
Benjamin Peterson97dd9872009-12-13 01:23:39 +000081* The new format specifier described in :ref:`pep-0378`.
Benjamin Petersonf6489f92009-11-25 17:46:26 +000082* The :class:`memoryview` object.
83* A small subset of the :mod:`importlib` module `described below <#importlib-section>`__.
Benjamin Peterson9eea4802009-12-31 03:31:15 +000084* Float-to-string and string-to-float conversions now round their
85 results more correctly. And :func:`repr` of a floating-point
86 number *x* returns a result that's guaranteed to round back to the
87 same number when converted back to a string.
88* The :cfunc:`PyLong_AsLongAndOverflow` C API function.
Benjamin Petersond23f8222009-04-05 19:13:16 +000089
90One porting change: the :option:`-3` switch now automatically
91enables the :option:`-Qwarn` switch that causes warnings
92about using classic division with integers and long integers.
93
Benjamin Petersonf6489f92009-11-25 17:46:26 +000094Other new Python3-mode warnings include:
95
96* :func:`operator.isCallable` and :func:`operator.sequenceIncludes`,
97 which are not supported in 3.x.
98
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000099.. ========================================================================
100.. Large, PEP-level features and changes should be described here.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000101.. ========================================================================
102
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000103.. _pep-0372:
104
Benjamin Petersond23f8222009-04-05 19:13:16 +0000105PEP 372: Adding an ordered dictionary to collections
106====================================================
107
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000108Regular Python dictionaries iterate over key/value pairs in arbitrary order.
109Over the years, a number of authors have written alternative implementations
110that remember the order that the keys were originally inserted. Based on
111the experiences from those implementations, a new
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000112:class:`~collections.OrderedDict` class has been introduced in the
113:mod:`collections` module.
Benjamin Petersond23f8222009-04-05 19:13:16 +0000114
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000115The :class:`~collections.OrderedDict` API is substantially the same as regular
116dictionaries but will iterate over keys and values in a guaranteed order
117depending on when a key was first inserted::
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000118
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000119 >>> from collections import OrderedDict
120 >>> d = OrderedDict([('first', 1), ('second', 2),
121 ... ('third', 3)])
122 >>> d.items()
123 [('first', 1), ('second', 2), ('third', 3)]
124
125If a new entry overwrites an existing entry, the original insertion
126position is left unchanged::
127
128 >>> d['second'] = 4
129 >>> d.items()
130 [('first', 1), ('second', 4), ('third', 3)]
131
132Deleting an entry and reinserting it will move it to the end::
133
134 >>> del d['second']
135 >>> d['second'] = 5
136 >>> d.items()
137 [('first', 1), ('third', 3), ('second', 5)]
138
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000139The :meth:`~collections.OrderedDict.popitem` method has an optional *last*
140argument that defaults to True. If *last* is True, the most recently
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000141added key is returned and removed; if it's False, the
142oldest key is selected::
143
144 >>> od = OrderedDict([(x,0) for x in range(20)])
145 >>> od.popitem()
146 (19, 0)
147 >>> od.popitem()
148 (18, 0)
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000149 >>> od.popitem(last=False)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000150 (0, 0)
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000151 >>> od.popitem(last=False)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000152 (1, 0)
153
154Comparing two ordered dictionaries checks both the keys and values,
155and requires that the insertion order was the same::
156
157 >>> od1 = OrderedDict([('first', 1), ('second', 2),
158 ... ('third', 3)])
159 >>> od2 = OrderedDict([('third', 3), ('first', 1),
160 ... ('second', 2)])
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000161 >>> od1 == od2
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000162 False
163 >>> # Move 'third' key to the end
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000164 >>> del od2['third']; od2['third'] = 3
165 >>> od1 == od2
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000166 True
167
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000168Comparing an :class:`~collections.OrderedDict` with a regular dictionary
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000169ignores the insertion order and just compares the keys and values.
170
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000171How does the :class:`~collections.OrderedDict` work? It maintains a
172doubly-linked list of keys, appending new keys to the list as they're inserted.
173A secondary dictionary maps keys to their corresponding list node, so
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000174deletion doesn't have to traverse the entire linked list and therefore
175remains O(1).
176
177.. XXX check O(1)-ness with Raymond
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000178.. Also check if the 'somenamedtuple' in the collection module should
179.. be replaced/removed in order to use
180.. :meth:`~collections.namedtuple._asdict()` (see below)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000181
182The standard library now supports use of ordered dictionaries in several
183modules. The :mod:`configparser` module uses them by default. This lets
184configuration files be read, modified, and then written back in their original
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000185order. The :meth:`~collections.somenamedtuple._asdict()` method for
186:func:`collections.namedtuple` now returns an ordered dictionary with the
187values appearing in the same order as the underlying tuple indices.
188The :mod:`json` module is being built-out with an *object_pairs_hook* to allow
189OrderedDicts to be built by the decoder.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000190Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_.
191
192.. seealso::
193
194 :pep:`372` - Adding an ordered dictionary to collections
195 PEP written by Armin Ronacher and Raymond Hettinger;
196 implemented by Raymond Hettinger.
197
198.. _pep-0378:
199
200PEP 378: Format Specifier for Thousands Separator
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000201=================================================
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000202
203To make program output more readable, it can be useful to add
204separators to large numbers and render them as
20518,446,744,073,709,551,616 instead of 18446744073709551616.
206
207The fully general solution for doing this is the :mod:`locale` module,
208which can use different separators ("," in North America, "." in
209Europe) and different grouping sizes, but :mod:`locale` is complicated
210to use and unsuitable for multi-threaded applications where different
211threads are producing output for different locales.
212
213Therefore, a simple comma-grouping mechanism has been added to the
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000214mini-language used by the :meth:`str.format` method. When
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000215formatting a floating-point number, simply include a comma between the
216width and the precision::
217
Eric Smith2b1a1162010-04-06 14:57:57 +0000218 >>> '{:20,.2f}'.format(18446744073709551616.0)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000219 '18,446,744,073,709,551,616.00'
220
Eric Smith21e85c72010-04-06 15:21:59 +0000221When formatting an integer, include the comma after the width:
222
223 >>> '{:20,d}'.format(18446744073709551616)
224 '18,446,744,073,709,551,616'
225
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000226This mechanism is not adaptable at all; commas are always used as the
227separator and the grouping is always into three-digit groups. The
228comma-formatting mechanism isn't as general as the :mod:`locale`
229module, but it's easier to use.
230
231.. XXX "Format String Syntax" in string.rst could use many more examples.
232
233.. seealso::
234
235 :pep:`378` - Format Specifier for Thousands Separator
236 PEP written by Raymond Hettinger; implemented by Eric Smith.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000237
Benjamin Peterson9895f912010-03-21 22:05:32 +0000238PEP 389: The argparse Module for Parsing Command Lines
239======================================================
240
241XXX write this section.
242
243.. seealso::
244
245 :pep:`389` - argparse - New Command Line Parsing Module
246 PEP written and implemented by Steven Bethard.
247
248PEP 391: Dictionary-Based Configuration For Logging
249====================================================
250
251XXX write this section.
252
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000253Two smaller enhancements to the logging module are:
254
255.. rev79293
256
257* :class:`Logger` instances gained a :meth:`getChild` that retrieves a
258 descendant logger using a relative path. For example,
259 once you retrieve a logger by doing ``log = getLogger('app')``,
260 calling ``log.getChild('network.listen')`` is equivalent to
261 ``getLogger('app.network.listen')``.
262
263* The :class:`LoggerAdapter` class gained a :meth:`isEnabledFor` method
264 that takes a *level* and returns whether the underlying logger would
265 process a message of that level of importance.
266
Benjamin Peterson9895f912010-03-21 22:05:32 +0000267.. seealso::
268
269 :pep:`391` - Dictionary-Based Configuration For Logging
270 PEP written and implemented by Vinay Sajip.
271
272PEP 3106: Dictionary Views
273====================================================
274
275XXX write this section.
276
277.. seealso::
278
279 :pep:`3106` - Revamping dict.keys(), .values() and .items()
280 PEP written by Guido van Rossum.
281 Backported to 2.7 by Alexandre Vassalotti; :issue:`1967`.
282
283
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000284Other Language Changes
285======================
286
287Some smaller changes made to the core Python language are:
288
Benjamin Peterson9895f912010-03-21 22:05:32 +0000289* The syntax for set literals has been backported from Python 3.x.
290 Curly brackets are used to surround the contents of the resulting
291 mutable set; set literals are
292 distinguished from dictionaries by not containing colons and values.
293 ``{}`` continues to represent an empty dictionary; use
294 ``set()`` for an empty set.
295
296 >>> {1,2,3,4,5}
297 set([1, 2, 3, 4, 5])
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000298 >>> set() # empty set
Benjamin Peterson9895f912010-03-21 22:05:32 +0000299 set([])
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000300 >>> {} # empty dict
Benjamin Peterson9895f912010-03-21 22:05:32 +0000301 {}
302
303 Backported by Alexandre Vassalotti; :issue:`2335`.
304
305* Dictionary and set comprehensions are another feature backported from
306 3.x, generalizing list/generator comprehensions to use
307 the literal syntax for sets and dictionaries.
308
309 >>> {x:x*x for x in range(6)}
310 {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
311 >>> {'a'*x for x in range(6)}
312 set(['', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa'])
313
314 Backported by Alexandre Vassalotti; :issue:`2333`.
315
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000316* The :keyword:`with` statement can now use multiple context managers
317 in one statement. Context managers are processed from left to right
318 and each one is treated as beginning a new :keyword:`with` statement.
319 This means that::
320
321 with A() as a, B() as b:
322 ... suite of statements ...
323
324 is equivalent to::
325
326 with A() as a:
327 with B() as b:
328 ... suite of statements ...
329
330 The :func:`contextlib.nested` function provides a very similar
331 function, so it's no longer necessary and has been deprecated.
332
333 (Proposed in http://codereview.appspot.com/53094; implemented by
334 Georg Brandl.)
335
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000336* Conversions between floating-point numbers and strings are
337 now correctly rounded on most platforms. These conversions occur
338 in many different places: :func:`str` on
339 floats and complex numbers; the :class:`float` and :class:`complex`
340 constructors;
341 numeric formatting; serialization and
342 deserialization of floats and complex numbers using the
343 :mod:`marshal`, :mod:`pickle`
344 and :mod:`json` modules;
345 parsing of float and imaginary literals in Python code;
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000346 and :class:`~decimal.Decimal`-to-float conversion.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000347
348 Related to this, the :func:`repr` of a floating-point number *x*
349 now returns a result based on the shortest decimal string that's
350 guaranteed to round back to *x* under correct rounding (with
351 round-half-to-even rounding mode). Previously it gave a string
352 based on rounding x to 17 decimal digits.
353
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000354 .. maybe add an example?
355
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000356 The rounding library responsible for this improvement works on
357 Windows, and on Unix platforms using the gcc, icc, or suncc
358 compilers. There may be a small number of platforms where correct
359 operation of this code cannot be guaranteed, so the code is not
Benjamin Petersona28e7022010-01-09 18:53:06 +0000360 used on such systems. You can find out which code is being used
361 by checking :data:`sys.float_repr_style`, which will be ``short``
362 if the new code is in use and ``legacy`` if it isn't.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000363
Mark Dickinson0bc8f902010-01-07 09:31:48 +0000364 Implemented by Eric Smith and Mark Dickinson, using David Gay's
365 :file:`dtoa.c` library; :issue:`7117`.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000366
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000367* The :meth:`str.format` method now supports automatic numbering of the replacement
Benjamin Peterson3f96a872009-04-11 20:58:12 +0000368 fields. This makes using :meth:`str.format` more closely resemble using
369 ``%s`` formatting::
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000370
371 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
372 '2009:4:Sunday'
373 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
374 '2009:4:Sunday'
375
Benjamin Peterson3f96a872009-04-11 20:58:12 +0000376 The auto-numbering takes the fields from left to right, so the first ``{...}``
377 specifier will use the first argument to :meth:`str.format`, the next
378 specifier will use the next argument, and so on. You can't mix auto-numbering
379 and explicit numbering -- either number all of your specifier fields or none
380 of them -- but you can mix auto-numbering and named fields, as in the second
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000381 example above. (Contributed by Eric Smith; :issue:`5237`.)
382
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000383 Complex numbers now correctly support usage with :func:`format`,
384 and default to being right-aligned.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000385 Specifying a precision or comma-separation applies to both the real
386 and imaginary parts of the number, but a specified field width and
387 alignment is applied to the whole of the resulting ``1.5+3j``
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000388 output. (Contributed by Eric Smith; :issue:`1588` and :issue:`7988`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000389
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000390 The 'F' format code now always formats its output using uppercase characters,
391 so it will now produce 'INF' and 'NAN'.
392 (Contributed by Eric Smith; :issue:`3382`.)
393
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000394* The :func:`int` and :func:`long` types gained a ``bit_length``
395 method that returns the number of bits necessary to represent
396 its argument in binary::
397
398 >>> n = 37
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000399 >>> bin(n)
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000400 '0b100101'
401 >>> n.bit_length()
402 6
403 >>> n = 2**123-1
404 >>> n.bit_length()
405 123
406 >>> (n+1).bit_length()
407 124
408
409 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
410
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000411* Conversions from long integers and regular integers to floating
412 point now round differently, returning the floating-point number
413 closest to the number. This doesn't matter for small integers that
414 can be converted exactly, but for large numbers that will
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000415 unavoidably lose precision, Python 2.7 now approximates more
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000416 closely. For example, Python 2.6 computed the following::
417
418 >>> n = 295147905179352891391
419 >>> float(n)
420 2.9514790517935283e+20
421 >>> n - long(float(n))
422 65535L
423
424 Python 2.7's floating-point result is larger, but much closer to the
425 true value::
426
427 >>> n = 295147905179352891391
428 >>> float(n)
429 2.9514790517935289e+20
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000430 >>> n - long(float(n))
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000431 -1L
432
433 (Implemented by Mark Dickinson; :issue:`3166`.)
434
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000435 Integer division is also more accurate in its rounding behaviours. (Also
436 implemented by Mark Dickinson; :issue:`1811`.)
437
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000438* It's now possible for a subclass of the built-in :class:`unicode` type
439 to override the :meth:`__unicode__` method. (Implemented by
440 Victor Stinner; :issue:`1583863`.)
441
442* The :class:`bytearray` type's :meth:`~bytearray.translate` method now accepts
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000443 ``None`` as its first argument. (Fixed by Georg Brandl;
Benjamin Petersond23f8222009-04-05 19:13:16 +0000444 :issue:`4759`.)
Mark Dickinsond72c7b62009-03-20 16:00:49 +0000445
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000446 .. bytearray doesn't seem to be documented
447
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000448* When using ``@classmethod`` and ``@staticmethod`` to wrap
449 methods as class or static methods, the wrapper object now
450 exposes the wrapped function as their :attr:`__func__` attribute.
451 (Contributed by Amaury Forgeot d'Arc, after a suggestion by
452 George Sakkis; :issue:`5982`.)
453
454* A new encoding named "cp720", used primarily for Arabic text, is now
455 supported. (Contributed by Alexander Belchenko and Amaury Forgeot
456 d'Arc; :issue:`1616979`.)
457
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000458* The :class:`file` object will now set the :attr:`filename` attribute
459 on the :exc:`IOError` exception when trying to open a directory
Benjamin Peterson9895f912010-03-21 22:05:32 +0000460 on POSIX platforms (noted by Jan Kaliszewski; :issue:`4764`), and
461 now explicitly checks for and forbids writing to read-only file objects
462 instead of trusting the C library to catch and report the error
463 (fixed by Stefan Krah; :issue:`5677`).
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000464
Benjamin Petersona28e7022010-01-09 18:53:06 +0000465* The Python tokenizer now translates line endings itself, so the
466 :func:`compile` built-in function can now accept code using any
467 line-ending convention. Additionally, it no longer requires that the
468 code end in a newline.
469
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000470* Extra parentheses in function definitions are illegal in Python 3.x,
471 meaning that you get a syntax error from ``def f((x)): pass``. In
472 Python3-warning mode, Python 2.7 will now warn about this odd usage.
473 (Noted by James Lingard; :issue:`7362`.)
474
Benjamin Peterson9895f912010-03-21 22:05:32 +0000475* When a module object is garbage-collected, the module's dictionary is
476 now only cleared if no one else is holding a reference to the
477 dictionary (:issue:`7140`).
478
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000479.. ======================================================================
480
481
482Optimizations
483-------------
484
Benjamin Petersond23f8222009-04-05 19:13:16 +0000485Several performance enhancements have been added:
486
487.. * A new :program:`configure` option, :option:`--with-computed-gotos`,
488 compiles the main bytecode interpreter loop using a new dispatch
489 mechanism that gives speedups of up to 20%, depending on the system
490 and benchmark. The new mechanism is only supported on certain
491 compilers, such as gcc, SunPro, and icc.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000492
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000493* A new opcode was added to perform the initial setup for
494 :keyword:`with` statements, looking up the :meth:`__enter__` and
495 :meth:`__exit__` methods. (Contributed by Benjamin Peterson.)
496
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000497* The garbage collector now performs better for one common usage
498 pattern: when many objects are being allocated without deallocating
499 any of them. This would previously take quadratic
500 time for garbage collection, but now the number of full garbage collections
501 is reduced as the number of objects on the heap grows.
502 The new logic is to only perform a full garbage collection pass when
503 the middle generation has been collected 10 times and when the
504 number of survivor objects from the middle generation exceeds 10% of
505 the number of objects in the oldest generation. (Suggested by Martin
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000506 von Löwis and implemented by Antoine Pitrou; :issue:`4074`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000507
Benjamin Petersond23f8222009-04-05 19:13:16 +0000508* The garbage collector tries to avoid tracking simple containers
509 which can't be part of a cycle. In Python 2.7, this is now true for
510 tuples and dicts containing atomic types (such as ints, strings,
511 etc.). Transitively, a dict containing tuples of atomic types won't
512 be tracked either. This helps reduce the cost of each
513 garbage collection by decreasing the number of objects to be
514 considered and traversed by the collector.
Antoine Pitrou9d81def2009-03-28 19:20:09 +0000515 (Contributed by Antoine Pitrou; :issue:`4688`.)
516
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000517* Long integers are now stored internally either in base 2**15 or in base
Benjamin Petersond23f8222009-04-05 19:13:16 +0000518 2**30, the base being determined at build time. Previously, they
519 were always stored in base 2**15. Using base 2**30 gives
520 significant performance improvements on 64-bit machines, but
521 benchmark results on 32-bit machines have been mixed. Therefore,
522 the default is to use base 2**30 on 64-bit machines and base 2**15
523 on 32-bit machines; on Unix, there's a new configure option
524 :option:`--enable-big-digits` that can be used to override this default.
525
526 Apart from the performance improvements this change should be
527 invisible to end users, with one exception: for testing and
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000528 debugging purposes there's a new structseq :data:`sys.long_info` that
Benjamin Petersond23f8222009-04-05 19:13:16 +0000529 provides information about the internal format, giving the number of
530 bits per digit and the size in bytes of the C type used to store
531 each digit::
532
533 >>> import sys
534 >>> sys.long_info
535 sys.long_info(bits_per_digit=30, sizeof_digit=4)
536
537 (Contributed by Mark Dickinson; :issue:`4258`.)
538
539 Another set of changes made long objects a few bytes smaller: 2 bytes
540 smaller on 32-bit systems and 6 bytes on 64-bit.
541 (Contributed by Mark Dickinson; :issue:`5260`.)
542
543* The division algorithm for long integers has been made faster
544 by tightening the inner loop, doing shifts instead of multiplications,
545 and fixing an unnecessary extra iteration.
546 Various benchmarks show speedups of between 50% and 150% for long
547 integer divisions and modulo operations.
548 (Contributed by Mark Dickinson; :issue:`5512`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000549 Bitwise operations are also significantly faster (initial patch by
550 Gregory Smith; :issue:`1087418`).
Benjamin Petersond23f8222009-04-05 19:13:16 +0000551
552* The implementation of ``%`` checks for the left-side operand being
553 a Python string and special-cases it; this results in a 1-3%
554 performance increase for applications that frequently use ``%``
555 with strings, such as templating libraries.
556 (Implemented by Collin Winter; :issue:`5176`.)
557
558* List comprehensions with an ``if`` condition are compiled into
559 faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
560 by Jeffrey Yasskin; :issue:`4715`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000561
Benjamin Petersona28e7022010-01-09 18:53:06 +0000562* Converting an integer or long integer to a decimal string was made
563 faster by special-casing base 10 instead of using a generalized
564 conversion function that supports arbitrary bases.
565 (Patch by Gawain Bolton; :issue:`6713`.)
566
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000567* The :meth:`split`, :meth:`replace`, :meth:`rindex`,
568 :meth:`rpartition`, and :meth:`rsplit` methods of string-like types
569 (strings, Unicode strings, and :class:`bytearray` objects) now use a
570 fast reverse-search algorithm instead of a character-by-character
571 scan. This is sometimes faster by a factor of 10. (Added by
572 Florent Xicluna; :issue:`7462` and :issue:`7622`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000573
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000574* The :mod:`pickle` and :mod:`cPickle` modules now automatically
575 intern the strings used for attribute names, reducing memory usage
576 of the objects resulting from unpickling. (Contributed by Jake
577 McGuire; :issue:`5084`.)
578
579* The :mod:`cPickle` module now special-cases dictionaries,
580 nearly halving the time required to pickle them.
581 (Contributed by Collin Winter; :issue:`5670`.)
582
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000583.. ======================================================================
584
Georg Brandl4d131ee2009-11-18 18:53:14 +0000585New and Improved Modules
586========================
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000587
588As in every release, Python's standard library received a number of
589enhancements and bug fixes. Here's a partial list of the most notable
590changes, sorted alphabetically by module name. Consult the
591:file:`Misc/NEWS` file in the source tree for a more complete list of
592changes, or look through the Subversion logs for all the details.
593
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000594* The :mod:`bdb` module's base debugging class :class:`~bdb.Bdb`
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000595 gained a feature for skipping modules. The constructor
596 now takes an iterable containing glob-style patterns such as
597 ``django.*``; the debugger will not step into stack frames
598 from a module that matches one of these patterns.
599 (Contributed by Maru Newby after a suggestion by
600 Senthil Kumaran; :issue:`5142`.)
601
Benjamin Peterson9895f912010-03-21 22:05:32 +0000602* The :mod:`binascii` module now supports the buffer API, so it can be
603 used with :class:`memoryview` instances and other similar buffer objects.
604 (Backported from 3.x by Florent Xicluna; :issue:`7703`.)
605
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000606* Updated module: the :mod:`bsddb` module has been updated from 4.7.2devel9
607 to version 4.8.4 of
608 `the pybsddb package <http://www.jcea.es/programacion/pybsddb.htm>`__.
609 The new version features better Python 3.x compatibility, various bug fixes,
610 and adds several new BerkeleyDB flags and methods.
611 (Updated by Jesús Cea Avión; :issue:`8156`. The pybsddb
612 changelog can be browsed at http://hg.jcea.es/pybsddb/file/tip/ChangeLog.)
613
614* The :mod:`bz2` module's :class:`~bz2.BZ2File` now supports the context
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000615 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
616 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
617
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000618* New class: the :class:`~collections.Counter` class in the :mod:`collections`
619 module is useful for tallying data. :class:`~collections.Counter` instances
620 behave mostly like dictionaries but return zero for missing keys instead of
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000621 raising a :exc:`KeyError`:
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000622
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000623 .. doctest::
624 :options: +NORMALIZE_WHITESPACE
625
626 >>> from collections import Counter
627 >>> c = Counter()
628 >>> for letter in 'here is a sample of english text':
629 ... c[letter] += 1
630 ...
631 >>> c
632 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
633 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
634 'p': 1, 'r': 1, 'x': 1})
635 >>> c['e']
636 5
637 >>> c['z']
638 0
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000639
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000640 There are two additional :class:`~collections.Counter` methods:
641 :meth:`~collections.Counter.most_common` returns the N most common elements
642 and their counts, and :meth:`~collections.Counter.elements`
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000643 returns an iterator over the contained element, repeating each element
644 as many times as its count::
645
646 >>> c.most_common(5)
647 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
648 >>> c.elements() ->
649 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
650 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
651 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000652 's', 's', 'r', 't', 't', 'x'
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000653
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000654 .. maybe it's better to use list(c.elements()) here
655
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000656 Contributed by Raymond Hettinger; :issue:`1696199`.
657
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000658 The new :class:`~collections.OrderedDict` class is described in the earlier
659 section :ref:`pep-0372`.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000660
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000661 The :class:`~collections.namedtuple` class now has an optional *rename* parameter.
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000662 If *rename* is true, field names that are invalid because they've
Benjamin Petersond23f8222009-04-05 19:13:16 +0000663 been repeated or that aren't legal Python identifiers will be
664 renamed to legal names that are derived from the field's
665 position within the list of fields:
666
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000667 >>> from collections import namedtuple
668 >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000669 >>> T._fields
670 ('field1', '_1', '_2', 'field2')
671
672 (Added by Raymond Hettinger; :issue:`1818`.)
673
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000674 The :class:`~collections.deque` data type now exposes its maximum length as the
675 read-only :attr:`~collections.deque.maxlen` attribute, and has a
676 :meth:`~collections.deque.reverse` method that reverses the elements of the deque in-place.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000677 (Added by Raymond Hettinger.)
678
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000679* The :mod:`copy` module's :func:`~copy.deepcopy` function will now
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000680 correctly copy bound instance methods. (Implemented by
681 Robert Collins; :issue:`1515`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000682
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000683* The :mod:`ctypes` module now always converts ``None`` to a C NULL
684 pointer for arguments declared as pointers. (Changed by Thomas
Benjamin Peterson9895f912010-03-21 22:05:32 +0000685 Heller; :issue:`4606`.) The underlying `libffi library
686 <http://sourceware.org/libffi/>`__ has been updated to version
687 3.0.9, containing various fixes for different platforms. (Updated
688 by Matthias Klose; :issue:`8142`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000689
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000690* New method: the :mod:`datetime` module's :class:`~datetime.timedelta` class
691 gained a :meth:`~datetime.timedelta.total_seconds` method that returns the
692 number of seconds in the duration. (Contributed by Brian Quinlan; :issue:`5788`.)
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000693
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000694* New method: the :class:`~decimal.Decimal` class gained a
695 :meth:`~decimal.Decimal.from_float` class method that performs an exact
696 conversion of a floating-point number to a :class:`~decimal.Decimal`.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000697 Note that this is an **exact** conversion that strives for the
698 closest decimal approximation to the floating-point representation's value;
699 the resulting decimal value will therefore still include the inaccuracy,
700 if any.
701 For example, ``Decimal.from_float(0.1)`` returns
702 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
703 (Implemented by Raymond Hettinger; :issue:`4796`.)
704
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000705 Most of the methods of the :class:`~decimal.Context` class now accept integers
706 as well as :class:`~decimal.Decimal` instances; the only exceptions are the
707 :meth:`~decimal.Context.canonical` and :meth:`~decimal.Context.is_canonical`
708 methods. (Patch by Juan José Conti; :issue:`7633`.)
709
710 The constructor for :class:`~decimal.Decimal` now accepts non-European
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000711 Unicode characters, such as Arabic-Indic digits. (Contributed by
712 Mark Dickinson; :issue:`6595`.)
713
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000714 When using :class:`~decimal.Decimal` instances with a string's
715 :meth:`~str.format` method, the default alignment was previously
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000716 left-alignment. This has been changed to right-alignment, which seems
717 more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.)
718
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000719* The :class:`~fractions.Fraction` class now accepts two rational numbers
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000720 as arguments to its constructor.
721 (Implemented by Mark Dickinson; :issue:`5812`.)
722
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000723* New class: a new :class:`~ftplib.FTP_TLS` class in
724 the :mod:`ftplib` module provides secure FTP
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000725 connections using TLS encapsulation of authentication as well as
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000726 subsequent control and data transfers.
727 (Contributed by Giampaolo Rodola', :issue:`2054`.)
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000728
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000729 The :meth:`~ftplib.FTP.storbinary` method for binary uploads can now restart
730 uploads thanks to an added *rest* parameter (patch by Pablo Mouzo;
731 :issue:`6845`.)
732
733* New function: the :mod:`gc` module's :func:`~gc.is_tracked` returns
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000734 true if a given instance is tracked by the garbage collector, false
Benjamin Petersond23f8222009-04-05 19:13:16 +0000735 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
736
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000737* The :mod:`gzip` module's :class:`~gzip.GzipFile` now supports the context
Benjamin Peterson9895f912010-03-21 22:05:32 +0000738 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``
739 (contributed by Hagen Fuerstenau; :issue:`3860`), and it now implements
740 the :class:`io.BufferedIOBase` ABC, so you can wrap it with
741 :class:`io.BufferedReader` for faster processing
742 (contributed by Nir Aides; :issue:`7471`).
743 It's also now possible to override the modification time
Benjamin Petersond23f8222009-04-05 19:13:16 +0000744 recorded in a gzipped file by providing an optional timestamp to
745 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000746
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000747 Files in gzip format can be padded with trailing zero bytes; the
748 :mod:`gzip` module will now consume these trailing bytes. (Fixed by
749 Tadek Pietraszek and Brian Curtin; :issue:`2846`.)
750
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000751* New attribute: the :mod:`hashlib` module now has an :attr:`~hashlib.hashlib.algorithms`
752 attribute containing a tuple naming the supported algorithms.
753 In Python 2.7, ``hashlib.algorithms`` contains
754 ``('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')``
755 (Contributed by Carl Chenet; :issue:`7418`.)
756
757* The default :class:`~httplib.HTTPResponse` class used by the :mod:`httplib` module now
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000758 supports buffering, resulting in much faster reading of HTTP responses.
759 (Contributed by Kristjan Valur Jonsson; :issue:`4879`.)
760
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000761 The :class:`~httplib.HTTPConnection` and :class:`~httplib.HTTPSConnection` classes
Benjamin Peterson9895f912010-03-21 22:05:32 +0000762 now support a *source_address* parameter, a ``(host, port)`` 2-tuple
763 giving the source address that will be used for the connection.
764 (Contributed by Eldon Ziegler; :issue:`3972`.)
765
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000766* The :mod:`imaplib` module now supports IPv6 addresses.
767 (Contributed by Derek Morr; :issue:`1655`.)
768
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000769* Updated module: The :mod:`io` library has been upgraded to the version shipped with
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000770 Python 3.1. For 3.1, the I/O library was entirely rewritten in C
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000771 and is 2 to 20 times faster depending on the task being performed. The
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000772 original Python version was renamed to the :mod:`_pyio` module.
773
774 One minor resulting change: the :class:`io.TextIOBase` class now
775 has an :attr:`errors` attribute giving the error setting
776 used for encoding and decoding errors (one of ``'strict'``, ``'replace'``,
777 ``'ignore'``).
778
779 The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000780 an invalid file descriptor. (Implemented by Benjamin Peterson;
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000781 :issue:`4991`.) The :meth:`~io.IOBase.truncate` method now preserves the
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000782 file position; previously it would change the file position to the
783 end of the new file. (Fixed by Pascal Chambon; :issue:`6939`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000784
Benjamin Peterson97dd9872009-12-13 01:23:39 +0000785* New function: ``itertools.compress(data, selectors)`` takes two
Benjamin Petersond23f8222009-04-05 19:13:16 +0000786 iterators. Elements of *data* are returned if the corresponding
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000787 value in *selectors* is true::
Benjamin Petersond23f8222009-04-05 19:13:16 +0000788
789 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
790 A, C, E, F
791
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000792 .. maybe here is better to use >>> list(itertools.compress(...)) instead
793
Benjamin Peterson97dd9872009-12-13 01:23:39 +0000794 New function: ``itertools.combinations_with_replacement(iter, r)``
Benjamin Petersond23f8222009-04-05 19:13:16 +0000795 returns all the possible *r*-length combinations of elements from the
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000796 iterable *iter*. Unlike :func:`~itertools.combinations`, individual elements
Benjamin Petersond23f8222009-04-05 19:13:16 +0000797 can be repeated in the generated combinations::
798
799 itertools.combinations_with_replacement('abc', 2) =>
800 ('a', 'a'), ('a', 'b'), ('a', 'c'),
801 ('b', 'b'), ('b', 'c'), ('c', 'c')
802
803 Note that elements are treated as unique depending on their position
804 in the input, not their actual values.
805
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000806 The :func:`itertools.count` function now has a *step* argument that
807 allows incrementing by values other than 1. :func:`~itertools.count` also
Benjamin Petersond23f8222009-04-05 19:13:16 +0000808 now allows keyword arguments, and using non-integer values such as
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000809 floats or :class:`~decimal.Decimal` instances. (Implemented by Raymond
Benjamin Petersond23f8222009-04-05 19:13:16 +0000810 Hettinger; :issue:`5032`.)
811
812 :func:`itertools.combinations` and :func:`itertools.product` were
813 previously raising :exc:`ValueError` for values of *r* larger than
814 the input iterable. This was deemed a specification error, so they
815 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
816
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000817* Updated module: The :mod:`json` module was upgraded to version 2.0.9 of the
Benjamin Petersond23f8222009-04-05 19:13:16 +0000818 simplejson package, which includes a C extension that makes
819 encoding and decoding faster.
820 (Contributed by Bob Ippolito; :issue:`4136`.)
821
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000822 To support the new :class:`collections.OrderedDict` type, :func:`json.load`
Benjamin Petersond23f8222009-04-05 19:13:16 +0000823 now has an optional *object_pairs_hook* parameter that will be called
824 with any object literal that decodes to a list of pairs.
825 (Contributed by Raymond Hettinger; :issue:`5381`.)
826
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000827* New functions: the :mod:`math` module gained
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000828 :func:`~math.erf` and :func:`~math.erfc` for the error function and the complementary error function,
829 :func:`~math.expm1` which computes ``e**x - 1`` with more precision than
830 using :func:`~math.exp` and subtracting 1,
831 :func:`~math.gamma` for the Gamma function, and
832 :func:`~math.lgamma` for the natural log of the Gamma function.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000833 (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.)
834
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000835* The :mod:`multiprocessing` module's :class:`Manager*` classes
836 can now be passed a callable that will be called whenever
837 a subprocess is started, along with a set of arguments that will be
838 passed to the callable.
839 (Contributed by lekma; :issue:`5585`.)
840
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000841 The :class:`~multiprocessing.Pool` class, which controls a pool of worker processes,
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000842 now has an optional *maxtasksperchild* parameter. Worker processes
843 will perform the specified number of tasks and then exit, causing the
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000844 :class:`~multiprocessing.Pool` to start a new worker. This is useful if tasks may leak
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000845 memory or other resources, or if some tasks will cause the worker to
846 become very large.
847 (Contributed by Charles Cazabon; :issue:`6963`.)
848
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000849* The :mod:`nntplib` module now supports IPv6 addresses.
850 (Contributed by Derek Morr; :issue:`1664`.)
851
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000852* New functions: the :mod:`os` module wraps the following POSIX system
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000853 calls: :func:`~os.getresgid` and :func:`~os.getresuid`, which return the
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000854 real, effective, and saved GIDs and UIDs;
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000855 :func:`~os.setresgid` and :func:`~os.setresuid`, which set
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000856 real, effective, and saved GIDs and UIDs to new values;
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000857 :func:`~os.initgroups`. (GID/UID functions
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000858 contributed by Travis H.; :issue:`6508`. Support for initgroups added
859 by Jean-Paul Calderone; :issue:`7333`.)
860
Benjamin Peterson9895f912010-03-21 22:05:32 +0000861 The :func:`os.fork` function now re-initializes the import lock in
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000862 the child process; this fixes problems on Solaris when :func:`~os.fork`
Benjamin Peterson9895f912010-03-21 22:05:32 +0000863 is called from a thread. (Fixed by Zsolt Cserna; :issue:`7242`.)
864
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000865* In the :mod:`os.path` module, the :func:`~os.path.normpath` and
866 :func:`~os.path.abspath` functions now preserve Unicode; if their input path
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000867 is a Unicode string, the return value is also a Unicode string.
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000868 (:meth:`~os.path.normpath` fixed by Matt Giuca in :issue:`5827`;
869 :meth:`~os.path.abspath` fixed by Ezio Melotti in :issue:`3426`.)
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000870
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000871* The :mod:`pydoc` module now has help for the various symbols that Python
872 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
873 (Contributed by David Laban; :issue:`4739`.)
874
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000875* The :mod:`re` module's :func:`~re.split`, :func:`~re.sub`, and :func:`~re.subn`
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000876 now accept an optional *flags* argument, for consistency with the
877 other functions in the module. (Added by Gregory P. Smith.)
878
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000879* New function: in the :mod:`shutil` module, :func:`~shutil.make_archive`
880 takes a filename, archive type (zip or tar-format), and a directory
881 path, and creates an archive containing the directory's contents.
882 (Added by Tarek Ziadé.)
883
884 :mod:`shutil`'s :func:`~shutil.copyfile` and :func:`~shutil.copytree`
885 functions now raise a :exc:`~shutil.SpecialFileError` exception when
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000886 asked to copy a named pipe. Previously the code would treat
887 named pipes like a regular file by opening them for reading, and
888 this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.)
889
890* New functions: in the :mod:`site` module, three new functions
891 return various site- and user-specific paths.
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000892 :func:`~site.getsitepackages` returns a list containing all
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000893 global site-packages directories, and
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000894 :func:`~site.getusersitepackages` returns the path of the user's
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000895 site-packages directory.
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000896 :func:`~site.getuserbase` returns the value of the :envvar:`USER_BASE`
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000897 environment variable, giving the path to a directory that can be used
898 to store data.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000899 (Contributed by Tarek Ziadé; :issue:`6693`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000900
Benjamin Peterson9895f912010-03-21 22:05:32 +0000901 The :mod:`site` module now reports exceptions occurring
902 when the :mod:`sitecustomize` module is imported, and will no longer
Florent Xicluna41fe6152010-04-02 18:52:12 +0000903 catch and swallow the :exc:`KeyboardInterrupt` exception. (Fixed by
Benjamin Peterson9895f912010-03-21 22:05:32 +0000904 Victor Stinner; :issue:`3137`.)
905
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000906* The :mod:`socket` module's :class:`~ssl.SSL` objects now support the
Benjamin Peterson9895f912010-03-21 22:05:32 +0000907 buffer API, which fixed a test suite failure. (Fixed by Antoine
908 Pitrou; :issue:`7133`.)
909
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000910 The :func:`~socket.create_connection` function
Benjamin Peterson9895f912010-03-21 22:05:32 +0000911 gained a *source_address* parameter, a ``(host, port)`` 2-tuple
912 giving the source address that will be used for the connection.
913 (Contributed by Eldon Ziegler; :issue:`3972`.)
914
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000915 The :meth:`~socket.socket.recv_into` and :meth:`~socket.socket.recvfrom_into`
916 methods will now write into objects that support the buffer API, most usefully
Benjamin Peterson9895f912010-03-21 22:05:32 +0000917 the :class:`bytearray` and :class:`memoryview` objects. (Implemented by
918 Antoine Pitrou; :issue:`8104`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000919
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000920* The :mod:`SocketServer` module's :class:`~SocketServer.TCPServer` class now
921 has a :attr:`~SocketServer.TCPServer.disable_nagle_algorithm` class attribute.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000922 The default value is False; if overridden to be True,
923 new request connections will have the TCP_NODELAY option set to
924 prevent buffering many small sends into a single TCP packet.
925 (Contributed by Kristjan Valur Jonsson; :issue:`6192`.)
926
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000927* Updated module: the :mod:`sqlite3` module has been updated to
Benjamin Peterson9895f912010-03-21 22:05:32 +0000928 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
929 the ability to load SQLite extensions from shared libraries.
930 Call the ``enable_load_extension(True)`` method to enable extensions,
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000931 and then call :meth:`~sqlite3.Connection.load_extension` to load a particular shared library.
Benjamin Peterson9895f912010-03-21 22:05:32 +0000932 (Updated by Gerhard Häring.)
933
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000934* The :mod:`struct` module will no longer silently ignore overflow
935 errors when a value is too large for a particular integer format
936 code (one of ``bBhHiIlLqQ``); it now always raises a
937 :exc:`struct.error` exception. (Changed by Mark Dickinson;
938 :issue:`1523`.)
939
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000940* New function: the :mod:`subprocess` module's
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000941 :func:`~subprocess.check_output` runs a command with a specified set of arguments
Benjamin Petersond23f8222009-04-05 19:13:16 +0000942 and returns the command's output as a string when the command runs without
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000943 error, or raises a :exc:`~subprocess.CalledProcessError` exception otherwise.
Georg Brandl1f01deb2009-01-03 22:47:39 +0000944
945 ::
946
947 >>> subprocess.check_output(['df', '-h', '.'])
948 'Filesystem Size Used Avail Capacity Mounted on\n
949 /dev/disk0s2 52G 49G 3.0G 94% /\n'
950
951 >>> subprocess.check_output(['df', '-h', '/bogus'])
952 ...
953 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
954
955 (Contributed by Gregory P. Smith.)
956
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000957 The :mod:`subprocess` module will now retry its internal system calls
958 on receiving an :const:`EINTR` signal. (Reported by several people; final
959 patch by Gregory P. Smith in :issue:`1068268`.)
960
961* New function: :func:`~symtable.is_declared_global` in the :mod:`symtable` module
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000962 returns true for variables that are explicitly declared to be global,
963 false for ones that are implicitly global.
964 (Contributed by Jeremy Hylton.)
965
Benjamin Petersond23f8222009-04-05 19:13:16 +0000966* The ``sys.version_info`` value is now a named tuple, with attributes
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000967 named :attr:`major`, :attr:`minor`, :attr:`micro`,
968 :attr:`releaselevel`, and :attr:`serial`. (Contributed by Ross
969 Light; :issue:`4285`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000970
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000971 :func:`sys.getwindowsversion` also returns a named tuple,
Ezio Melotti0d85e412010-03-13 00:39:49 +0000972 with attributes named :attr:`major`, :attr:`minor`, :attr:`build`,
973 :attr:`platform`, :attr:`service_pack`, :attr:`service_pack_major`,
Eric Smithb0869402010-02-03 14:25:10 +0000974 :attr:`service_pack_minor`, :attr:`suite_mask`, and
975 :attr:`product_type`. (Contributed by Brian Curtin; :issue:`7766`.)
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000976
977* The :mod:`tarfile` module's default error handling has changed, to
978 no longer suppress fatal errors. The default error level was previously 0,
979 which meant that errors would only result in a message being written to the
980 debug log, but because the debug log is not activated by default,
981 these errors go unnoticed. The default error level is now 1,
982 which raises an exception if there's an error.
983 (Changed by Lars Gustäbel; :issue:`7357`.)
984
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000985 :mod:`tarfile` now supports filtering the :class:`~tarfile.TarInfo`
986 objects being added to a tar file. When you call :meth:`~tarfile.TarFile.add`,
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000987 instance, you may supply an optional *filter* argument
988 that's a callable. The *filter* callable will be passed the
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000989 :class:`~tarfile.TarInfo` for every file being added, and can modify and return it.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000990 If the callable returns ``None``, the file will be excluded from the
991 resulting archive. This is more powerful than the existing
992 *exclude* argument, which has therefore been deprecated.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000993 (Added by Lars Gustäbel; :issue:`6856`.)
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000994 The :class:`~tarfile.TarFile` class also now supports the context manager protocol.
Benjamin Peterson9895f912010-03-21 22:05:32 +0000995 (Added by Lars Gustäbel; :issue:`7232`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000996
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000997* The :meth:`~threading.Event.wait` method of the :class:`threading.Event` class
998 now returns the internal flag on exit. This means the method will usually
999 return true because :meth:`~threading.Event.wait` is supposed to block until the
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001000 internal flag becomes true. The return value will only be false if
1001 a timeout was provided and the operation timed out.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001002 (Contributed by Tim Lesher; :issue:`1674032`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001003
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001004* The Unicode database provided by the :mod:`unicodedata` module is
1005 now used internally to determine which characters are numeric,
1006 whitespace, or represent line breaks. The database also
1007 includes information from the :file:`Unihan.txt` data file (patch
1008 by Anders Chrigström and Amaury Forgeot d'Arc; :issue:`1571184`)
1009 and has been updated to version 5.2.0 (updated by
1010 Florent Xicluna; :issue:`8024`).
Ezio Melotti4c5475d2010-03-22 23:16:42 +00001011
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001012* The :class:`~UserDict.UserDict` class is now a new-style class. (Changed by
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001013 Benjamin Peterson.)
1014
Benjamin Peterson9895f912010-03-21 22:05:32 +00001015* The ElementTree library, :mod:`xml.etree`, no longer escapes
1016 ampersands and angle brackets when outputting an XML processing
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001017 instruction (which looks like ``<?xml-stylesheet href="#style1"?>``)
1018 or comment (which looks like ``<!-- comment -->``).
Benjamin Peterson9895f912010-03-21 22:05:32 +00001019 (Patch by Neil Muller; :issue:`2746`.)
1020
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001021* The :mod:`zipfile` module's :class:`~zipfile.ZipFile` now supports the context
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001022 management protocol, so you can write ``with zipfile.ZipFile(...) as f: ...``.
1023 (Contributed by Brian Curtin; :issue:`5511`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001024
Benjamin Petersond23f8222009-04-05 19:13:16 +00001025 :mod:`zipfile` now supports archiving empty directories and
1026 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001027 Reading files out of an archive is now faster, and interleaving
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001028 :meth:`~zipfile.ZipFile.read` and :meth:`~zipfile.ZipFile.readline` now works correctly.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001029 (Contributed by Nir Aides; :issue:`7610`.)
1030
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001031 The :func:`~zipfile.is_zipfile` function now
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001032 accepts a file object, in addition to the path names accepted in earlier
1033 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +00001034
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001035 The :meth:`~zipfile.ZipFile.writestr` method now has an optional *compress_type* parameter
Benjamin Peterson9895f912010-03-21 22:05:32 +00001036 that lets you override the default compression method specified in the
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001037 :class:`~zipfile.ZipFile` constructor. (Contributed by Ronald Oussoren;
Benjamin Peterson9895f912010-03-21 22:05:32 +00001038 :issue:`6003`.)
1039
Benjamin Peterson9895f912010-03-21 22:05:32 +00001040
1041New module: sysconfig
1042---------------------------------
1043
1044XXX A new :mod:`sysconfig` module has been extracted from
1045:mod:`distutils` and put in the standard library.
1046
1047The :mod:`sysconfig` module provides access to Python's configuration
1048information like the list of installation paths and the configuration
1049variables relevant for the current platform. (contributed by Tarek)
1050
1051Updated module: ElementTree 1.3
1052---------------------------------
1053
1054XXX write this.
Tarek Ziadé396fad72010-02-23 05:30:31 +00001055
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001056.. ======================================================================
1057.. whole new modules get described in subsections here
1058
Tarek Ziadéba0eacf2010-02-02 23:43:21 +00001059
Benjamin Peterson9895f912010-03-21 22:05:32 +00001060Distutils Enhancements
1061---------------------------------
1062
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001063XXX all of this work has been moved to Distutils2
1064XXX Not sure what we should say here
1065
Benjamin Peterson9895f912010-03-21 22:05:32 +00001066Distutils is being more actively developed, thanks to Tarek Ziadé
1067who has taken over maintenance of the package, so there are a number
1068of fixes and improvements.
1069
1070A new :file:`setup.py` subcommand, ``check``, will check that the
1071arguments being passed to the :func:`setup` function are complete
1072and correct (:issue:`5732`).
1073
1074Byte-compilation by the ``install_lib`` subcommand is now only done
1075if the ``sys.dont_write_bytecode`` setting allows it (:issue:`7071`).
1076
1077:func:`distutils.sdist.add_defaults` now uses
1078*package_dir* and *data_files* to create the MANIFEST file.
1079:mod:`distutils.sysconfig` now reads the :envvar:`AR` and
1080:envvar:`ARFLAGS` environment variables.
1081
1082.. ARFLAGS done in #5941
1083
1084It is no longer mandatory to store clear-text passwords in the
1085:file:`.pypirc` file when registering and uploading packages to PyPI. As long
1086as the username is present in that file, the :mod:`distutils` package will
1087prompt for the password if not present. (Added by Tarek Ziadé,
1088based on an initial contribution by Nathan Van Gheem; :issue:`4394`.)
1089
1090A Distutils setup can now specify that a C extension is optional by
1091setting the *optional* option setting to true. If this optional is
1092supplied, failure to build the extension will not abort the build
1093process, but instead simply not install the failing extension.
1094(Contributed by Georg Brandl; :issue:`5583`.)
1095
1096The :class:`distutils.dist.DistributionMetadata` class'
1097:meth:`read_pkg_file` method will read the contents of a package's
1098:file:`PKG-INFO` metadata file. For an example of its use, see
1099:ref:`reading-metadata`.
1100(Contributed by Tarek Ziadé; :issue:`7457`.)
1101
1102:file:`setup.py` files will now accept a :option:`--no-user-cfg` switch
1103to skip reading the :file:`~/.pydistutils.cfg` file. (Suggested by
1104by Michael Hoffman, and implemented by Paul Winkler; :issue:`1180`.)
1105
1106When creating a tar-format archive, the ``sdist`` subcommand now
1107allows specifying the user id and group that will own the files in the
1108archives using the :option:`--owner` and :option:`--group` switches
1109(:issue:`6516`).
Tarek Ziadéba0eacf2010-02-02 23:43:21 +00001110
1111
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001112Unit Testing Enhancements
1113---------------------------------
1114
1115The :mod:`unittest` module was enhanced in several ways.
1116The progress messages now shows 'x' for expected failures
1117and 'u' for unexpected successes when run in verbose mode.
1118(Contributed by Benjamin Peterson.)
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001119Test cases can raise the :exc:`~unittest.SkipTest` exception to skip a test.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001120(:issue:`1034053`.)
1121
1122.. XXX describe test discovery (Contributed by Michael Foord; :issue:`6001`.)
1123
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001124The error messages for :meth:`~unittest.TestCase.assertEqual`,
1125:meth:`~unittest.TestCase.assertTrue`, and :meth:`~unittest.TestCase.assertFalse`
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001126failures now provide more information. If you set the
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001127:attr:`~unittest.TestCase.longMessage` attribute of your :class:`~unittest.TestCase` classes to
1128True, both the standard error message and any additional message you
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001129provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
1130
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001131The :meth:`~unittest.TestCase.assertRaises` method now
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001132return a context handler when called without providing a callable
1133object to run. For example, you can write this::
1134
1135 with self.assertRaises(KeyError):
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001136 {}['foo']
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001137
1138(Implemented by Antoine Pitrou; :issue:`4444`.)
1139
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001140.. rev 78774
1141
1142Module- and class-level setup and teardown fixtures are now supported.
1143Modules can contain :func:`~unittest.setUpModule` and :func:`~unittest.tearDownModule`
1144functions. Classes can have :meth:`~unittest.TestCase.setUpClass` and
1145:meth:`~unittest.TestCase.tearDownClass` methods that must be defined as class methods
1146(using ``@classmethod`` or equivalent). These functions and
1147methods are invoked when the test runner switches to a test case in a
1148different module or class.
1149
1150The methods :meth:`~unittest.TestCase.addCleanup` and
1151:meth:`~unittest.TestCase.doCleanups` were added.
1152:meth:`~unittest.TestCase.addCleanup` allows you to add cleanup functions that
1153will be called unconditionally (after :meth:`~unittest.TestCase.setUp` if
1154:meth:`~unittest.TestCase.setUp` fails, otherwise after :meth:`~unittest.TestCase.tearDown`). This allows
1155for much simpler resource allocation and deallocation during tests
1156(:issue:`5679`).
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001157
1158A number of new methods were added that provide more specialized
1159tests. Many of these methods were written by Google engineers
1160for use in their test suites; Gregory P. Smith, Michael Foord, and
1161GvR worked on merging them into Python's version of :mod:`unittest`.
1162
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001163* :meth:`~unittest.TestCase.assertIsNone` and :meth:`~unittest.TestCase.assertIsNotNone` take one
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001164 expression and verify that the result is or is not ``None``.
1165
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001166* :meth:`~unittest.TestCase.assertIs` and :meth:`~unittest.TestCase.assertIsNot`
1167 take two values and check whether the two values evaluate to the same object or not.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001168 (Added by Michael Foord; :issue:`2578`.)
1169
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001170* :meth:`~unittest.TestCase.assertIsInstance` and
1171 :meth:`~unittest.TestCase.assertNotIsInstance` check whether
Benjamin Petersona28e7022010-01-09 18:53:06 +00001172 the resulting object is an instance of a particular class, or of
1173 one of a tuple of classes. (Added by Georg Brandl; :issue:`7031`.)
1174
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001175* :meth:`~unittest.TestCase.assertGreater`, :meth:`~unittest.TestCase.assertGreaterEqual`,
1176 :meth:`~unittest.TestCase.assertLess`, and :meth:`~unittest.TestCase.assertLessEqual` compare
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001177 two quantities.
1178
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001179* :meth:`~unittest.TestCase.assertMultiLineEqual` compares two strings, and if they're
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001180 not equal, displays a helpful comparison that highlights the
Benjamin Peterson9895f912010-03-21 22:05:32 +00001181 differences in the two strings. This comparison is now used by
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001182 default when Unicode strings are compared with :meth:`~unittest.TestCase.assertEqual`.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001183
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001184* :meth:`~unittest.TestCase.assertRegexpMatches` checks whether its first argument is a
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001185 string matching a regular expression provided as its second argument.
1186
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001187 .. XXX add assertNotRegexpMatches see issue 8038
1188
1189* :meth:`~unittest.TestCase.assertRaisesRegexp` checks whether a particular exception
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001190 is raised, and then also checks that the string representation of
1191 the exception matches the provided regular expression.
1192
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001193* :meth:`~unittest.TestCase.assertIn` and :meth:`~unittest.TestCase.assertNotIn`
1194 tests whether *first* is or is not in *second*.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001195
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001196* :meth:`~unittest.TestCase.assertItemsEqual` tests whether two provided sequences
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001197 contain the same elements.
1198
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001199* :meth:`~unittest.TestCase.assertSetEqual` compares whether two sets are equal, and
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001200 only reports the differences between the sets in case of error.
1201
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001202* Similarly, :meth:`~unittest.TestCase.assertListEqual` and :meth:`~unittest.TestCase.assertTupleEqual`
Benjamin Peterson9895f912010-03-21 22:05:32 +00001203 compare the specified types and explain any differences without necessarily
1204 printing their full values; these methods are now used by default
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001205 when comparing lists and tuples using :meth:`~unittest.TestCase.assertEqual`.
1206 More generally, :meth:`~unittest.TestCase.assertSequenceEqual` compares two sequences
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001207 and can optionally check whether both sequences are of a
1208 particular type.
1209
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001210* :meth:`~unittest.TestCase.assertDictEqual` compares two dictionaries and reports the
Benjamin Peterson9895f912010-03-21 22:05:32 +00001211 differences; it's now used by default when you compare two dictionaries
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001212 using :meth:`~unittest.TestCase.assertEqual`. :meth:`~unittest.TestCase.assertDictContainsSubset` checks whether
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001213 all of the key/value pairs in *first* are found in *second*.
1214
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001215* :meth:`~unittest.TestCase.assertAlmostEqual` and :meth:`~unittest.TestCase.assertNotAlmostEqual` test
Benjamin Peterson9895f912010-03-21 22:05:32 +00001216 whether *first* and *second* are approximately equal by computing
1217 their difference, rounding the result to an optionally-specified number
1218 of *places* (the default is 7), and comparing to zero.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001219
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001220* :meth:`~unittest.TestLoader.loadTestsFromName` properly honors the
1221 :attr:`~unittest.TestLoader.suiteClass` attribute of
1222 the :class:`~unittest.TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001223
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001224* A new hook lets you extend the :meth:`~unittest.TestCase.assertEqual` method to handle
1225 new data types. The :meth:`~unittest.TestCase.addTypeEqualityFunc` method takes a type
1226 object and a function. The function will be used when both of the
1227 objects being compared are of the specified type. This function
1228 should compare the two objects and raise an exception if they don't
1229 match; it's a good idea for the function to provide additional
1230 information about why the two objects are matching, much as the new
1231 sequence comparison methods do.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001232
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001233:func:`unittest.main` now takes an optional ``exit`` argument. If
1234False, :func:`~unittest.main` doesn't call :func:`sys.exit`, allowing it to be
1235used from the interactive interpreter. (Contributed by J. Pablo
1236Fernández; :issue:`3379`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001237
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001238A new command-line switch, :option:`-f` or :option:`--failfast`, makes
1239test execution stop immediately when a test fails instead of
1240continuing to execute further tests. (Suggested by Cliff Dyer and
1241implemented by Michael Foord; :issue:`8074`.)
1242
1243.. XXX document the other new switches
1244
1245:class:`~unittest.TestResult` has new :meth:`~unittest.TestResult.startTestRun` and
1246:meth:`~unittest.TestResult.stopTestRun` methods that are called immediately before
1247and after a test run. (Contributed by Robert Collins; :issue:`5728`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001248
1249With all these changes, the :file:`unittest.py` was becoming awkwardly
1250large, so the module was turned into a package and the code split into
1251several files (by Benjamin Peterson). This doesn't affect how the
1252module is imported.
1253
1254
1255.. _importlib-section:
1256
Benjamin Petersond23f8222009-04-05 19:13:16 +00001257importlib: Importing Modules
1258------------------------------
1259
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001260Python 3.1 includes the :mod:`importlib` package, a re-implementation
1261of the logic underlying Python's :keyword:`import` statement.
1262:mod:`importlib` is useful for implementors of Python interpreters and
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001263to users who wish to write new importers that can participate in the
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001264import process. Python 2.7 doesn't contain the complete
1265:mod:`importlib` package, but instead has a tiny subset that contains
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001266a single function, :func:`~importlib.import_module`.
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001267
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001268``import_module(name, package=None)`` imports a module. *name* is
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001269a string containing the module or package's name. It's possible to do
1270relative imports by providing a string that begins with a ``.``
1271character, such as ``..utils.errors``. For relative imports, the
1272*package* argument must be provided and is the name of the package that
1273will be used as the anchor for
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001274the relative import. :func:`~importlib.import_module` both inserts the imported
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001275module into ``sys.modules`` and returns the module object.
1276
1277Here are some examples::
1278
1279 >>> from importlib import import_module
1280 >>> anydbm = import_module('anydbm') # Standard absolute import
1281 >>> anydbm
1282 <module 'anydbm' from '/p/python/Lib/anydbm.py'>
1283 >>> # Relative import
1284 >>> sysconfig = import_module('..sysconfig', 'distutils.command')
1285 >>> sysconfig
1286 <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'>
1287
1288:mod:`importlib` was implemented by Brett Cannon and introduced in
1289Python 3.1.
1290
Benjamin Petersond23f8222009-04-05 19:13:16 +00001291
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00001292ttk: Themed Widgets for Tk
1293--------------------------
1294
1295Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
1296widgets but have a more customizable appearance and can therefore more
1297closely resemble the native platform's widgets. This widget
1298set was originally called Tile, but was renamed to Ttk (for "themed Tk")
1299on being added to Tcl/Tck release 8.5.
1300
1301XXX write a brief discussion and an example here.
1302
1303The :mod:`ttk` module was written by Guilherme Polo and added in
1304:issue:`2983`. An alternate version called ``Tile.py``, written by
1305Martin Franklin and maintained by Kevin Walzer, was proposed for
1306inclusion in :issue:`2618`, but the authors argued that Guilherme
1307Polo's work was more comprehensive.
1308
Georg Brandl4d131ee2009-11-18 18:53:14 +00001309
1310Deprecations and Removals
1311=========================
1312
1313* :func:`contextlib.nested`, which allows handling more than one context manager
1314 with one :keyword:`with` statement, has been deprecated; :keyword:`with`
1315 supports multiple context managers syntactically now.
1316
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001317.. ======================================================================
1318
1319
1320Build and C API Changes
1321=======================
1322
1323Changes to Python's build process and to the C API include:
1324
Georg Brandl1f01deb2009-01-03 22:47:39 +00001325* If you use the :file:`.gdbinit` file provided with Python,
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001326 the "pyo" macro in the 2.7 version now works correctly when the thread being
1327 debugged doesn't hold the GIL; the macro now acquires it before printing.
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001328 (Contributed by Victor Stinner; :issue:`3632`.)
1329
Benjamin Petersond23f8222009-04-05 19:13:16 +00001330* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001331 worker thread submit notifications to the main Python thread. This
1332 is particularly useful for asynchronous IO operations.
1333 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
1334
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001335* New function: :cfunc:`PyCode_NewEmpty` creates an empty code object;
1336 only the filename, function name, and first line number are required.
1337 This is useful to extension modules that are attempting to
1338 construct a more useful traceback stack. Previously such
1339 extensions needed to call :cfunc:`PyCode_New`, which had many
1340 more arguments. (Added by Jeffrey Yasskin.)
1341
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001342* New function: :cfunc:`PyErr_NewExceptionWithDoc` creates a new
1343 exception class, just as the existing :cfunc:`PyErr_NewException` does,
1344 but takes an extra ``char *`` argument containing the docstring for the
1345 new exception class. (Added by the 'lekma' user on the Python bug tracker;
1346 :issue:`7033`.)
1347
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001348* New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object
1349 and returns the line number that the frame is currently executing.
1350 Previously code would need to get the index of the bytecode
1351 instruction currently executing, and then look up the line number
1352 corresponding to that address. (Added by Jeffrey Yasskin.)
1353
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001354* New functions: :cfunc:`PyLong_AsLongAndOverflow` and
1355 :cfunc:`PyLong_AsLongLongAndOverflow` approximates a Python long
1356 integer as a C :ctype:`long` or :ctype:`long long`.
1357 If the number is too large to fit into
1358 the output type, an *overflow* flag is set and returned to the caller.
1359 (Contributed by Case Van Horsen; :issue:`7528` and :issue:`7767`.)
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001360
Benjamin Petersona28e7022010-01-09 18:53:06 +00001361* New function: stemming from the rewrite of string-to-float conversion,
1362 a new :cfunc:`PyOS_string_to_double` function was added. The old
1363 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions
1364 are now deprecated.
1365
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001366* New macros: the Python header files now define the following macros:
1367 :cmacro:`Py_ISALNUM`,
1368 :cmacro:`Py_ISALPHA`,
1369 :cmacro:`Py_ISDIGIT`,
1370 :cmacro:`Py_ISLOWER`,
1371 :cmacro:`Py_ISSPACE`,
1372 :cmacro:`Py_ISUPPER`,
1373 :cmacro:`Py_ISXDIGIT`,
1374 and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`.
1375 All of these functions are analogous to the C
1376 standard macros for classifying characters, but ignore the current
1377 locale setting, because in
1378 several places Python needs to analyze characters in a
1379 locale-independent way. (Added by Eric Smith;
1380 :issue:`5793`.)
1381
1382 .. XXX these macros don't seem to be described in the c-api docs.
1383
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001384* New format codes: the :cfunc:`PyFormat_FromString`,
1385 :cfunc:`PyFormat_FromStringV`, and :cfunc:`PyErr_Format` now
1386 accepts ``%lld`` and ``%llu`` format codes for displaying values of
1387 C's :ctype:`long long` types.
1388 (Contributed by Mark Dickinson; :issue:`7228`.)
1389
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001390* The complicated interaction between threads and process forking has
1391 been changed. Previously, the child process created by
1392 :func:`os.fork` might fail because the child is created with only a
1393 single thread running, the thread performing the :func:`os.fork`.
1394 If other threads were holding a lock, such as Python's import lock,
1395 when the fork was performed, the lock would still be marked as
1396 "held" in the new process. But in the child process nothing would
1397 ever release the lock, since the other threads weren't replicated,
1398 and the child process would no longer be able to perform imports.
1399
1400 Python 2.7 now acquires the import lock before performing an
1401 :func:`os.fork`, and will also clean up any locks created using the
1402 :mod:`threading` module. C extension modules that have internal
1403 locks, or that call :cfunc:`fork()` themselves, will not benefit
1404 from this clean-up.
1405
1406 (Fixed by Thomas Wouters; :issue:`1590864`.)
1407
Benjamin Petersona28e7022010-01-09 18:53:06 +00001408* The :cfunc:`Py_Finalize` function now calls the internal
1409 :func:`threading._shutdown` function; this prevents some exceptions from
1410 being raised when an interpreter shuts down.
1411 (Patch by Adam Olsen; :issue:`1722344`.)
1412
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001413* Global symbols defined by the :mod:`ctypes` module are now prefixed
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001414 with ``Py``, or with ``_ctypes``. (Implemented by Thomas
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001415 Heller; :issue:`3102`.)
1416
Benjamin Petersona28e7022010-01-09 18:53:06 +00001417* New configure option: the :option:`--with-system-expat` switch allows
1418 building the :mod:`pyexpat` module to use the system Expat library.
1419 (Contributed by Arfrever Frehtes Taifersar Arahesis; :issue:`7609`.)
1420
1421* New configure option: Compiling Python with the
1422 :option:`--with-valgrind` option will now disable the pymalloc
1423 allocator, which is difficult for the Valgrind to analyze correctly.
1424 Valgrind will therefore be better at detecting memory leaks and
1425 overruns. (Contributed by James Henstridge; :issue:`2422`.)
1426
1427* New configure option: you can now supply no arguments to
1428 :option:`--with-dbmliborder=` in order to build none of the various
1429 DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis;
1430 :issue:`6491`.)
1431
Benjamin Petersond23f8222009-04-05 19:13:16 +00001432* The :program:`configure` script now checks for floating-point rounding bugs
1433 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
1434 preprocessor definition. No code currently uses this definition,
1435 but it's available if anyone wishes to use it.
1436 (Added by Mark Dickinson; :issue:`2937`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001437
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001438* The build process now creates the necessary files for pkg-config
1439 support. (Contributed by Clinton Roy; :issue:`3585`.)
1440
1441* The build process now supports Subversion 1.7. (Contributed by
1442 Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.)
1443
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001444
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001445.. ======================================================================
1446
1447Port-Specific Changes: Windows
1448-----------------------------------
1449
Georg Brandl1f01deb2009-01-03 22:47:39 +00001450* The :mod:`msvcrt` module now contains some constants from
1451 the :file:`crtassem.h` header file:
1452 :data:`CRT_ASSEMBLY_VERSION`,
1453 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
1454 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001455 (Contributed by David Cournapeau; :issue:`4365`.)
1456
1457* The new :cfunc:`_beginthreadex` API is used to start threads, and
1458 the native thread-local storage functions are now used.
1459 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001460
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001461* The :func:`os.listdir` function now correctly fails
1462 for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.)
1463
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001464* The :mod:`mimelib` module will now read the MIME database from
1465 the Windows registry when initializing.
1466 (Patch by Gabriel Genellina; :issue:`4969`.)
1467
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001468.. ======================================================================
1469
1470Port-Specific Changes: Mac OS X
1471-----------------------------------
1472
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001473* The path ``/Library/Python/2.7/site-packages`` is now appended to
Benjamin Petersond23f8222009-04-05 19:13:16 +00001474 ``sys.path``, in order to share added packages between the system
1475 installation and a user-installed copy of the same version.
1476 (Changed by Ronald Oussoren; :issue:`4865`.)
1477
1478
1479Other Changes and Fixes
1480=======================
1481
Benjamin Peterson9895f912010-03-21 22:05:32 +00001482* Two benchmark scripts, :file:`iobench` and :file:`ccbench`, were
1483 added to the :file:`Tools` directory. :file:`iobench` measures the
1484 speed of built-in file I/O objects (as returned by :func:`open`)
1485 while performing various operations, and :file:`ccbench` is a
1486 concurrency benchmark that tries to measure computing throughput,
1487 thread switching latency, and IO processing bandwidth when
1488 performing several tasks using a varying number of threads.
1489
Benjamin Petersond23f8222009-04-05 19:13:16 +00001490* When importing a module from a :file:`.pyc` or :file:`.pyo` file
1491 with an existing :file:`.py` counterpart, the :attr:`co_filename`
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001492 attributes of the resulting code objects are overwritten when the
1493 original filename is obsolete. This can happen if the file has been
1494 renamed, moved, or is accessed through different paths. (Patch by
1495 Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +00001496
1497* The :file:`regrtest.py` script now takes a :option:`--randseed=`
1498 switch that takes an integer that will be used as the random seed
1499 for the :option:`-r` option that executes tests in random order.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001500 The :option:`-r` option also reports the seed that was used
Benjamin Petersond23f8222009-04-05 19:13:16 +00001501 (Added by Collin Winter.)
1502
Benjamin Petersona28e7022010-01-09 18:53:06 +00001503* Another :file:`regrtest.py` switch is :option:`-j`, which
1504 takes an integer specifying how many tests run in parallel. This
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001505 allows reducing the total runtime on multi-core machines.
Antoine Pitrou88909542009-06-29 13:54:42 +00001506 This option is compatible with several other options, including the
1507 :option:`-R` switch which is known to produce long runtimes.
Benjamin Petersona28e7022010-01-09 18:53:06 +00001508 (Added by Antoine Pitrou, :issue:`6152`.) This can also be used
1509 with a new :option:`-F` switch that runs selected tests in a loop
1510 until they fail. (Added by Antoine Pitrou; :issue:`7312`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001511
1512.. ======================================================================
1513
1514Porting to Python 2.7
1515=====================
1516
1517This section lists previously described changes and other bugfixes
1518that may require changes to your code:
1519
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001520* When using :class:`Decimal` instances with a string's
1521 :meth:`format` method, the default alignment was previously
1522 left-alignment. This has been changed to right-alignment, which might
1523 change the output of your programs.
1524 (Changed by Mark Dickinson; :issue:`6857`.)
1525
1526 Another :meth:`format`-related change: the default precision used
1527 for floating-point and complex numbers was changed from 6 decimal
1528 places to 12, which matches the precision used by :func:`str`.
1529 (Changed by Eric Smith; :issue:`5920`.)
1530
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001531* Because of an optimization for the :keyword:`with` statement, the special
1532 methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's
1533 type, and cannot be directly attached to the object's instance. This
1534 affects new-style classes (derived from :class:`object`) and C extension
1535 types. (:issue:`6101`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001536
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001537* The :meth:`readline` method of :class:`StringIO` objects now does
1538 nothing when a negative length is requested, as other file-like
1539 objects do. (:issue:`7348`).
1540
Benjamin Peterson9895f912010-03-21 22:05:32 +00001541In the standard library:
1542
1543* The ElementTree library, :mod:`xml.etree`, no longer escapes
1544 ampersands and angle brackets when outputting an XML processing
1545 instruction (which looks like `<?xml-stylesheet href="#style1"?>`)
1546 or comment (which looks like `<!-- comment -->`).
1547 (Patch by Neil Muller; :issue:`2746`.)
1548
Benjamin Petersona28e7022010-01-09 18:53:06 +00001549For C extensions:
1550
1551* C extensions that use integer format codes with the ``PyArg_Parse*``
1552 family of functions will now raise a :exc:`TypeError` exception
1553 instead of triggering a :exc:`DeprecationWarning` (:issue:`5080`).
1554
1555* Use the new :cfunc:`PyOS_string_to_double` function instead of the old
1556 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions,
1557 which are now deprecated.
1558
1559
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001560.. ======================================================================
1561
1562
1563.. _acks27:
1564
1565Acknowledgements
1566================
1567
1568The author would like to thank the following people for offering
1569suggestions, corrections and assistance with various drafts of this
Benjamin Peterson97dd9872009-12-13 01:23:39 +00001570article: Ryan Lovett, Hugh Secker-Walker.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001571