blob: 8a46ff44fda1c6232ef286292545265c89a3e6ee [file] [log] [blame]
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001****************************
2 What's New in Python 2.7
3****************************
4
5:Author: A.M. Kuchling (amk at amk.ca)
6:Release: |release|
7:Date: |today|
8
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00009.. Fix accents on Kristjan Valur Jonsson, Fuerstenau
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +000010
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +000011.. Big jobs: argparse, ElementTree 1.3, pep 391, 3106, sysconfig
12.. unittest test discovery
13
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +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 Melotti021f3342010-04-06 03:26:49 +000043 * Credit the author of a patch or bugfix. Just the name is
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +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
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +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`.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000058
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +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
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000064.. Compare with previous release in 2 - 3 sentences here.
65 add hyperlink when the documentation becomes available online.
66
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000067.. _whatsnew27-python31:
68
69Python 3.1 Features
70=======================
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +000071
72Much as Python 2.6 incorporated features from Python 3.0,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +000076
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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`.
Andrew M. Kuchling8f254e72009-12-08 02:37:05 +000081* The new format specifier described in :ref:`pep-0378`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000082* The :class:`memoryview` object.
83* A small subset of the :mod:`importlib` module `described below <#importlib-section>`__.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +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.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000094Other new Python3-mode warnings include:
95
96* :func:`operator.isCallable` and :func:`operator.sequenceIncludes`,
97 which are not supported in 3.x.
98
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000099.. ========================================================================
100.. Large, PEP-level features and changes should be described here.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000101.. ========================================================================
102
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000103.. _pep-0372:
104
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000105PEP 372: Adding an ordered dictionary to collections
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000106====================================================
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000107
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +0000112:class:`~collections.OrderedDict` class has been introduced in the
113:mod:`collections` module.
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000114
Ezio Melotti021f3342010-04-06 03:26:49 +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::
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000118
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +0000139The :meth:`~collections.OrderedDict.popitem` method has an optional *last*
140argument that defaults to True. If *last* is True, the most recently
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +0000149 >>> od.popitem(last=False)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000150 (0, 0)
Ezio Melotti021f3342010-04-06 03:26:49 +0000151 >>> od.popitem(last=False)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +0000161 >>> od1 == od2
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000162 False
163 >>> # Move 'third' key to the end
Ezio Melotti021f3342010-04-06 03:26:49 +0000164 >>> del od2['third']; od2['third'] = 3
165 >>> od1 == od2
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000166 True
167
Ezio Melotti021f3342010-04-06 03:26:49 +0000168Comparing an :class:`~collections.OrderedDict` with a regular dictionary
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000169ignores the insertion order and just compares the keys and values.
170
Ezio Melotti021f3342010-04-06 03:26:49 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +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)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000181
182The standard library now supports use of ordered dictionaries in several
Ezio Melotti021f3342010-04-06 03:26:49 +0000183modules. The :mod:`ConfigParser` module uses them by default. This lets
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000184configuration files be read, modified, and then written back in their original
Ezio Melotti021f3342010-04-06 03:26:49 +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.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000190Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_.
191
Andrew M. Kuchling7fe65a02009-10-13 15:49:33 +0000192.. 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.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000197
198.. _pep-0378:
199
200PEP 378: Format Specifier for Thousands Separator
Ezio Melotti021f3342010-04-06 03:26:49 +0000201=================================================
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +0000214mini-language used by the :meth:`str.format` method. When
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000215formatting a floating-point number, simply include a comma between the
216width and the precision::
217
218 >>> '{:20,.2}'.format(f)
219 '18,446,744,073,709,551,616.00'
220
Ezio Melotti021f3342010-04-06 03:26:49 +0000221.. XXX this example seems wrong:
222.. >>> f = 18446744073709551616.0
223.. >>> '{:20,.2}'.format(f)
224.. ' 1.8e+19'
225
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchling85ea4bf2009-10-05 22:45:39 +0000231.. XXX "Format String Syntax" in string.rst could use many more examples.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000232
233.. seealso::
234
235 :pep:`378` - Format Specifier for Thousands Separator
236 PEP written by Raymond Hettinger; implemented by Eric Smith.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000237
Andrew M. Kuchlingab21f752010-03-02 13:55:33 +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
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000248PEP 391: Dictionary-Based Configuration For Logging
249====================================================
250
251XXX write this section.
252
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +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
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +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
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000284Other Language Changes
285======================
286
287Some smaller changes made to the core Python language are:
288
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +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 Melotti021f3342010-04-06 03:26:49 +0000298 >>> set() # empty set
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000299 set([])
Ezio Melotti021f3342010-04-06 03:26:49 +0000300 >>> {} # empty dict
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +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 Melotti021f3342010-04-06 03:26:49 +0000346 and :class:`~decimal.Decimal`-to-float conversion.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +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 Melotti021f3342010-04-06 03:26:49 +0000354 .. maybe add an example?
355
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +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
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +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.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000363
Mark Dickinsonbdd863d2010-01-07 09:28:29 +0000364 Implemented by Eric Smith and Mark Dickinson, using David Gay's
365 :file:`dtoa.c` library; :issue:`7117`.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000366
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000367* The :meth:`str.format` method now supports automatic numbering of the replacement
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +0000368 fields. This makes using :meth:`str.format` more closely resemble using
369 ``%s`` formatting::
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000370
371 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
372 '2009:4:Sunday'
373 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
374 '2009:4:Sunday'
375
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000381 example above. (Contributed by Eric Smith; :issue:`5237`.)
382
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000383 Complex numbers now correctly support usage with :func:`format`,
384 and default to being right-aligned.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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``
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000388 output. (Contributed by Eric Smith; :issue:`1588` and :issue:`7988`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000389
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +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 Dickinson1a707982008-12-17 16:14:37 +0000394* The :func:`int` and :func:`long` types gained a ``bit_length``
Georg Brandl64e1c752009-04-11 18:19:27 +0000395 method that returns the number of bits necessary to represent
Mark Dickinson1a707982008-12-17 16:14:37 +0000396 its argument in binary::
397
398 >>> n = 37
Ezio Melotti021f3342010-04-06 03:26:49 +0000399 >>> bin(n)
Mark Dickinson1a707982008-12-17 16:14:37 +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
Andrew M. Kuchling92b97002009-05-02 17:12:15 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000415 unavoidably lose precision, Python 2.7 now approximates more
Andrew M. Kuchling92b97002009-05-02 17:12:15 +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 Melotti021f3342010-04-06 03:26:49 +0000430 >>> n - long(float(n))
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000431 -1L
432
433 (Implemented by Mark Dickinson; :issue:`3166`.)
434
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000435 Integer division is also more accurate in its rounding behaviours. (Also
436 implemented by Mark Dickinson; :issue:`1811`.)
437
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +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
Ezio Melotti021f3342010-04-06 03:26:49 +0000442* The :class:`bytearray` type's :meth:`~bytearray.translate` method now accepts
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000443 ``None`` as its first argument. (Fixed by Georg Brandl;
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000444 :issue:`4759`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000445
Ezio Melotti021f3342010-04-06 03:26:49 +0000446 .. bytearray doesn't seem to be documented
447
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000458* The :class:`file` object will now set the :attr:`filename` attribute
459 on the :exc:`IOError` exception when trying to open a directory
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +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`).
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000464
Benjamin Petersonae9a0a02009-12-31 16:49:37 +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.
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +0000469
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +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
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +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
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000479.. ======================================================================
480
481
482Optimizations
483-------------
484
Andrew M. Kuchling77069572009-03-31 01:21:01 +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.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000492
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +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 Melotti021f3342010-04-06 03:26:49 +0000506 von Löwis and implemented by Antoine Pitrou; :issue:`4074`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000507
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +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 Pitrouc18f6b02009-03-28 19:10:13 +0000515 (Contributed by Antoine Pitrou; :issue:`4688`.)
516
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000517* Long integers are now stored internally either in base 2**15 or in base
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +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 Melotti021f3342010-04-06 03:26:49 +0000528 debugging purposes there's a new structseq :data:`sys.long_info` that
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +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
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000537 (Contributed by Mark Dickinson; :issue:`4258`.)
538
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000539 Another set of changes made long objects a few bytes smaller: 2 bytes
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000540 smaller on 32-bit systems and 6 bytes on 64-bit.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000541 (Contributed by Mark Dickinson; :issue:`5260`.)
542
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000543* 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`.)
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +0000549 Bitwise operations are also significantly faster (initial patch by
550 Gregory Smith; :issue:`1087418`).
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000551
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000552* 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
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000558* 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`.)
561
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +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
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +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`.)
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +0000573
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000583.. ======================================================================
584
Georg Brandl0516f812009-11-18 18:52:35 +0000585New and Improved Modules
586========================
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +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 Melotti021f3342010-04-06 03:26:49 +0000594* The :mod:`bdb` module's base debugging class :class:`~bdb.Bdb`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +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
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +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.
Ezio Melotti021f3342010-04-06 03:26:49 +0000611 (Updated by Jesús Cea Avión; :issue:`8156`. The pybsddb
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000612 changelog can be browsed at http://hg.jcea.es/pybsddb/file/tip/ChangeLog.)
613
Ezio Melotti021f3342010-04-06 03:26:49 +0000614* The :mod:`bz2` module's :class:`~bz2.BZ2File` now supports the context
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000615 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
616 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
617
Ezio Melotti021f3342010-04-06 03:26:49 +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
Georg Brandlf6dab952009-04-28 21:48:35 +0000621 raising a :exc:`KeyError`:
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000622
Georg Brandlf6dab952009-04-28 21:48:35 +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
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000639
Ezio Melotti021f3342010-04-06 03:26:49 +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`
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +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',
Georg Brandlf6dab952009-04-28 21:48:35 +0000652 's', 's', 'r', 't', 't', 'x'
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000653
Ezio Melotti021f3342010-04-06 03:26:49 +0000654 .. maybe it's better to use list(c.elements()) here
655
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000656 Contributed by Raymond Hettinger; :issue:`1696199`.
657
Ezio Melotti021f3342010-04-06 03:26:49 +0000658 The new :class:`~collections.OrderedDict` class is described in the earlier
659 section :ref:`pep-0372`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000660
Ezio Melotti021f3342010-04-06 03:26:49 +0000661 The :class:`~collections.namedtuple` class now has an optional *rename* parameter.
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000662 If *rename* is true, field names that are invalid because they've
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +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
Georg Brandlf6dab952009-04-28 21:48:35 +0000667 >>> from collections import namedtuple
668 >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000669 >>> T._fields
670 ('field1', '_1', '_2', 'field2')
671
672 (Added by Raymond Hettinger; :issue:`1818`.)
673
Ezio Melotti021f3342010-04-06 03:26:49 +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.
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000677 (Added by Raymond Hettinger.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000678
Ezio Melotti021f3342010-04-06 03:26:49 +0000679* The :mod:`copy` module's :func:`~copy.deepcopy` function will now
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000680 correctly copy bound instance methods. (Implemented by
681 Robert Collins; :issue:`1515`.)
682
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000683* The :mod:`ctypes` module now always converts ``None`` to a C NULL
684 pointer for arguments declared as pointers. (Changed by Thomas
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +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`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000689
Ezio Melotti021f3342010-04-06 03:26:49 +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`.)
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000693
Ezio Melotti021f3342010-04-06 03:26:49 +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`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +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`.)
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000709
Ezio Melotti021f3342010-04-06 03:26:49 +0000710 The constructor for :class:`~decimal.Decimal` now accepts non-European
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000711 Unicode characters, such as Arabic-Indic digits. (Contributed by
712 Mark Dickinson; :issue:`6595`.)
713
Ezio Melotti021f3342010-04-06 03:26:49 +0000714 When using :class:`~decimal.Decimal` instances with a string's
715 :meth:`~str.format` method, the default alignment was previously
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +0000719* The :class:`~fractions.Fraction` class now accepts two rational numbers
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000720 as arguments to its constructor.
721 (Implemented by Mark Dickinson; :issue:`5812`.)
722
Ezio Melotti021f3342010-04-06 03:26:49 +0000723* New class: a new :class:`~ftplib.FTP_TLS` class in
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000724 the :mod:`ftplib` module provides secure FTP
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000725 connections using TLS encapsulation of authentication as well as
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000726 subsequent control and data transfers.
727 (Contributed by Giampaolo Rodola', :issue:`2054`.)
728
Ezio Melotti021f3342010-04-06 03:26:49 +0000729 The :meth:`~ftplib.FTP.storbinary` method for binary uploads can now restart
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000730 uploads thanks to an added *rest* parameter (patch by Pablo Mouzo;
731 :issue:`6845`.)
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000732
Ezio Melotti021f3342010-04-06 03:26:49 +0000733* New function: the :mod:`gc` module's :func:`~gc.is_tracked` returns
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000734 true if a given instance is tracked by the garbage collector, false
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000735 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
736
Ezio Melotti021f3342010-04-06 03:26:49 +0000737* The :mod:`gzip` module's :class:`~gzip.GzipFile` now supports the context
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +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
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000744 recorded in a gzipped file by providing an optional timestamp to
745 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000746
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +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 Melotti021f3342010-04-06 03:26:49 +0000751* New attribute: the :mod:`hashlib` module now has an :attr:`~hashlib.hashlib.algorithms`
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000752 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
Ezio Melotti021f3342010-04-06 03:26:49 +0000757* The default :class:`~httplib.HTTPResponse` class used by the :mod:`httplib` module now
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000758 supports buffering, resulting in much faster reading of HTTP responses.
759 (Contributed by Kristjan Valur Jonsson; :issue:`4879`.)
760
Ezio Melotti021f3342010-04-06 03:26:49 +0000761 The :class:`~httplib.HTTPConnection` and :class:`~httplib.HTTPSConnection` classes
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000766* The :mod:`imaplib` module now supports IPv6 addresses.
767 (Contributed by Derek Morr; :issue:`1655`.)
768
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000769* Updated module: The :mod:`io` library has been upgraded to the version shipped with
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000770 Python 3.1. For 3.1, the I/O library was entirely rewritten in C
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000771 and is 2 to 20 times faster depending on the task being performed. The
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000780 an invalid file descriptor. (Implemented by Benjamin Peterson;
Ezio Melotti021f3342010-04-06 03:26:49 +0000781 :issue:`4991`.) The :meth:`~io.IOBase.truncate` method now preserves the
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000782 file position; previously it would change the file position to the
783 end of the new file. (Fixed by Pascal Chambon; :issue:`6939`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000784
Andrew M. Kuchling5a73ff82009-12-02 14:27:11 +0000785* New function: ``itertools.compress(data, selectors)`` takes two
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000786 iterators. Elements of *data* are returned if the corresponding
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000787 value in *selectors* is true::
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000788
789 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
790 A, C, E, F
791
Ezio Melotti021f3342010-04-06 03:26:49 +0000792 .. maybe here is better to use >>> list(itertools.compress(...)) instead
793
Andrew M. Kuchling5a73ff82009-12-02 14:27:11 +0000794 New function: ``itertools.combinations_with_replacement(iter, r)``
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000795 returns all the possible *r*-length combinations of elements from the
Ezio Melotti021f3342010-04-06 03:26:49 +0000796 iterable *iter*. Unlike :func:`~itertools.combinations`, individual elements
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +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 Melotti021f3342010-04-06 03:26:49 +0000806 The :func:`itertools.count` function now has a *step* argument that
807 allows incrementing by values other than 1. :func:`~itertools.count` also
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000808 now allows keyword arguments, and using non-integer values such as
Ezio Melotti021f3342010-04-06 03:26:49 +0000809 floats or :class:`~decimal.Decimal` instances. (Implemented by Raymond
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000810 Hettinger; :issue:`5032`.)
811
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000812 :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
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000817* Updated module: The :mod:`json` module was upgraded to version 2.0.9 of the
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000818 simplejson package, which includes a C extension that makes
819 encoding and decoding faster.
820 (Contributed by Bob Ippolito; :issue:`4136`.)
821
Ezio Melotti021f3342010-04-06 03:26:49 +0000822 To support the new :class:`collections.OrderedDict` type, :func:`json.load`
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +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
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000827* New functions: the :mod:`math` module gained
Ezio Melotti021f3342010-04-06 03:26:49 +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.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000833 (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.)
834
Andrew M. Kuchling24520b42009-04-09 11:22:47 +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 Melotti021f3342010-04-06 03:26:49 +0000841 The :class:`~multiprocessing.Pool` class, which controls a pool of worker processes,
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000842 now has an optional *maxtasksperchild* parameter. Worker processes
843 will perform the specified number of tasks and then exit, causing the
Ezio Melotti021f3342010-04-06 03:26:49 +0000844 :class:`~multiprocessing.Pool` to start a new worker. This is useful if tasks may leak
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000849* The :mod:`nntplib` module now supports IPv6 addresses.
850 (Contributed by Derek Morr; :issue:`1664`.)
851
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000852* New functions: the :mod:`os` module wraps the following POSIX system
Ezio Melotti021f3342010-04-06 03:26:49 +0000853 calls: :func:`~os.getresgid` and :func:`~os.getresuid`, which return the
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000854 real, effective, and saved GIDs and UIDs;
Ezio Melotti021f3342010-04-06 03:26:49 +0000855 :func:`~os.setresgid` and :func:`~os.setresuid`, which set
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000856 real, effective, and saved GIDs and UIDs to new values;
Ezio Melotti021f3342010-04-06 03:26:49 +0000857 :func:`~os.initgroups`. (GID/UID functions
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000858 contributed by Travis H.; :issue:`6508`. Support for initgroups added
859 by Jean-Paul Calderone; :issue:`7333`.)
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000860
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +0000861 The :func:`os.fork` function now re-initializes the import lock in
Ezio Melotti021f3342010-04-06 03:26:49 +0000862 the child process; this fixes problems on Solaris when :func:`~os.fork`
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +0000863 is called from a thread. (Fixed by Zsolt Cserna; :issue:`7242`.)
864
Ezio Melotti021f3342010-04-06 03:26:49 +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
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000867 is a Unicode string, the return value is also a Unicode string.
Ezio Melotti021f3342010-04-06 03:26:49 +0000868 (:meth:`~os.path.normpath` fixed by Matt Giuca in :issue:`5827`;
869 :meth:`~os.path.abspath` fixed by Ezio Melotti in :issue:`3426`.)
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000870
Andrew M. Kuchling9cb42772009-01-21 02:15:43 +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 Melotti021f3342010-04-06 03:26:49 +0000875* The :mod:`re` module's :func:`~re.split`, :func:`~re.sub`, and :func:`~re.subn`
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000876 now accept an optional *flags* argument, for consistency with the
877 other functions in the module. (Added by Gregory P. Smith.)
878
Ezio Melotti021f3342010-04-06 03:26:49 +0000879* New function: in the :mod:`shutil` module, :func:`~shutil.make_archive`
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000880 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
Ezio Melotti021f3342010-04-06 03:26:49 +0000884 :mod:`shutil`'s :func:`~shutil.copyfile` and :func:`~shutil.copytree`
885 functions now raise a :exc:`~shutil.SpecialFileError` exception when
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +0000892 :func:`~site.getsitepackages` returns a list containing all
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000893 global site-packages directories, and
Ezio Melotti021f3342010-04-06 03:26:49 +0000894 :func:`~site.getusersitepackages` returns the path of the user's
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000895 site-packages directory.
Ezio Melotti021f3342010-04-06 03:26:49 +0000896 :func:`~site.getuserbase` returns the value of the :envvar:`USER_BASE`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000897 environment variable, giving the path to a directory that can be used
898 to store data.
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000899 (Contributed by Tarek Ziadé; :issue:`6693`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000900
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +0000901 The :mod:`site` module now reports exceptions occurring
902 when the :mod:`sitecustomize` module is imported, and will no longer
Florent Xiclunaad598332010-03-31 21:40:32 +0000903 catch and swallow the :exc:`KeyboardInterrupt` exception. (Fixed by
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +0000904 Victor Stinner; :issue:`3137`.)
905
Ezio Melotti021f3342010-04-06 03:26:49 +0000906* The :mod:`socket` module's :class:`~ssl.SSL` objects now support the
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000907 buffer API, which fixed a test suite failure. (Fixed by Antoine
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +0000908 Pitrou; :issue:`7133`.)
909
Ezio Melotti021f3342010-04-06 03:26:49 +0000910 The :func:`~socket.create_connection` function
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +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`.)
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +0000914
Ezio Melotti021f3342010-04-06 03:26:49 +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
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +0000917 the :class:`bytearray` and :class:`memoryview` objects. (Implemented by
918 Antoine Pitrou; :issue:`8104`.)
919
Ezio Melotti021f3342010-04-06 03:26:49 +0000920* The :mod:`SocketServer` module's :class:`~SocketServer.TCPServer` class now
921 has a :attr:`~SocketServer.TCPServer.disable_nagle_algorithm` class attribute.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +0000927* Updated module: the :mod:`sqlite3` module has been updated to
Andrew M. Kuchlingfed15762010-03-08 12:00:39 +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 Melotti021f3342010-04-06 03:26:49 +0000931 and then call :meth:`~sqlite3.Connection.load_extension` to load a particular shared library.
Andrew M. Kuchlingfed15762010-03-08 12:00:39 +0000932 (Updated by Gerhard Häring.)
933
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000940* New function: the :mod:`subprocess` module's
Ezio Melotti021f3342010-04-06 03:26:49 +0000941 :func:`~subprocess.check_output` runs a command with a specified set of arguments
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000942 and returns the command's output as a string when the command runs without
Ezio Melotti021f3342010-04-06 03:26:49 +0000943 error, or raises a :exc:`~subprocess.CalledProcessError` exception otherwise.
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +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
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +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
Ezio Melotti021f3342010-04-06 03:26:49 +0000961* New function: :func:`~symtable.is_declared_global` in the :mod:`symtable` module
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +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
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000966* The ``sys.version_info`` value is now a named tuple, with attributes
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000967 named :attr:`major`, :attr:`minor`, :attr:`micro`,
968 :attr:`releaselevel`, and :attr:`serial`. (Contributed by Ross
969 Light; :issue:`4285`.)
970
971 :func:`sys.getwindowsversion` also returns a named tuple,
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000972 with attributes named :attr:`major`, :attr:`minor`, :attr:`build`,
Ezio Melotti12477752010-02-08 22:22:41 +0000973 :attr:`platform`, :attr:`service_pack`, :attr:`service_pack_major`,
Eric Smithb3c54882010-02-03 14:17:50 +0000974 :attr:`service_pack_minor`, :attr:`suite_mask`, and
975 :attr:`product_type`. (Contributed by Brian Curtin; :issue:`7766`.)
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000976
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000977* 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 Melotti021f3342010-04-06 03:26:49 +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`,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000987 instance, you may supply an optional *filter* argument
988 that's a callable. The *filter* callable will be passed the
Ezio Melotti021f3342010-04-06 03:26:49 +0000989 :class:`~tarfile.TarInfo` for every file being added, and can modify and return it.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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.
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000993 (Added by Lars Gustäbel; :issue:`6856`.)
Ezio Melotti021f3342010-04-06 03:26:49 +0000994 The :class:`~tarfile.TarFile` class also now supports the context manager protocol.
Andrew M. Kuchlingfed15762010-03-08 12:00:39 +0000995 (Added by Lars Gustäbel; :issue:`7232`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000996
Ezio Melotti021f3342010-04-06 03:26:49 +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
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001000 internal flag becomes true. The return value will only be false if
1001 a timeout was provided and the operation timed out.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001002 (Contributed by Tim Lesher; :issue:`1674032`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001003
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +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`).
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001011
Ezio Melotti021f3342010-04-06 03:26:49 +00001012* The :class:`~UserDict.UserDict` class is now a new-style class. (Changed by
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001013 Benjamin Peterson.)
1014
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001015* The ElementTree library, :mod:`xml.etree`, no longer escapes
1016 ampersands and angle brackets when outputting an XML processing
Ezio Melotti021f3342010-04-06 03:26:49 +00001017 instruction (which looks like ``<?xml-stylesheet href="#style1"?>``)
1018 or comment (which looks like ``<!-- comment -->``).
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001019 (Patch by Neil Muller; :issue:`2746`.)
1020
Ezio Melotti021f3342010-04-06 03:26:49 +00001021* The :mod:`zipfile` module's :class:`~zipfile.ZipFile` now supports the context
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00001022 management protocol, so you can write ``with zipfile.ZipFile(...) as f: ...``.
1023 (Contributed by Brian Curtin; :issue:`5511`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001024
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +00001025 :mod:`zipfile` now supports archiving empty directories and
1026 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001027 Reading files out of an archive is now faster, and interleaving
Ezio Melotti021f3342010-04-06 03:26:49 +00001028 :meth:`~zipfile.ZipFile.read` and :meth:`~zipfile.ZipFile.readline` now works correctly.
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001029 (Contributed by Nir Aides; :issue:`7610`.)
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +00001030
Ezio Melotti021f3342010-04-06 03:26:49 +00001031 The :func:`~zipfile.is_zipfile` function now
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00001032 accepts a file object, in addition to the path names accepted in earlier
1033 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
1034
Ezio Melotti021f3342010-04-06 03:26:49 +00001035 The :meth:`~zipfile.ZipFile.writestr` method now has an optional *compress_type* parameter
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001036 that lets you override the default compression method specified in the
Ezio Melotti021f3342010-04-06 03:26:49 +00001037 :class:`~zipfile.ZipFile` constructor. (Contributed by Ronald Oussoren;
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001038 :issue:`6003`.)
1039
1040
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001041New 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
Tarek Ziadé1e069ee2010-02-23 05:20:22 +00001049variables relevant for the current platform. (contributed by Tarek)
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001050
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001051Updated module: ElementTree 1.3
1052---------------------------------
1053
1054XXX write this.
1055
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001056.. ======================================================================
1057.. whole new modules get described in subsections here
1058
Tarek Ziadé2b210692010-02-02 23:39:40 +00001059
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +00001060Distutils Enhancements
1061---------------------------------
1062
Tarek Ziadé0b93a6b2010-04-05 14:58:14 +00001063XXX all of this work has been moved to Distutils2
1064XXX Not sure what we should say here
1065
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +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`).
1110
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001111
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +00001119Test cases can raise the :exc:`~unittest.SkipTest` exception to skip a test.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001120(:issue:`1034053`.)
1121
1122.. XXX describe test discovery (Contributed by Michael Foord; :issue:`6001`.)
1123
Ezio Melotti021f3342010-04-06 03:26:49 +00001124The error messages for :meth:`~unittest.TestCase.assertEqual`,
1125:meth:`~unittest.TestCase.assertTrue`, and :meth:`~unittest.TestCase.assertFalse`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001126failures now provide more information. If you set the
Ezio Melotti021f3342010-04-06 03:26:49 +00001127:attr:`~unittest.TestCase.longMessage` attribute of your :class:`~unittest.TestCase` classes to
1128True, both the standard error message and any additional message you
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001129provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
1130
Ezio Melotti021f3342010-04-06 03:26:49 +00001131The :meth:`~unittest.TestCase.assertRaises` method now
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +00001136 {}['foo']
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001137
1138(Implemented by Antoine Pitrou; :issue:`4444`.)
1139
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001140.. rev 78774
1141
1142Module- and class-level setup and teardown fixtures are now supported.
Ezio Melotti021f3342010-04-06 03:26:49 +00001143Modules 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
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001147methods are invoked when the test runner switches to a test case in a
1148different module or class.
1149
Ezio Melotti021f3342010-04-06 03:26:49 +00001150The 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
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001155for much simpler resource allocation and deallocation during tests
1156(:issue:`5679`).
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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 Melotti021f3342010-04-06 03:26:49 +00001163* :meth:`~unittest.TestCase.assertIsNone` and :meth:`~unittest.TestCase.assertIsNotNone` take one
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001164 expression and verify that the result is or is not ``None``.
1165
Ezio Melotti021f3342010-04-06 03:26:49 +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.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001168 (Added by Michael Foord; :issue:`2578`.)
1169
Ezio Melotti021f3342010-04-06 03:26:49 +00001170* :meth:`~unittest.TestCase.assertIsInstance` and
1171 :meth:`~unittest.TestCase.assertNotIsInstance` check whether
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +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 Melotti021f3342010-04-06 03:26:49 +00001175* :meth:`~unittest.TestCase.assertGreater`, :meth:`~unittest.TestCase.assertGreaterEqual`,
1176 :meth:`~unittest.TestCase.assertLess`, and :meth:`~unittest.TestCase.assertLessEqual` compare
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001177 two quantities.
1178
Ezio Melotti021f3342010-04-06 03:26:49 +00001179* :meth:`~unittest.TestCase.assertMultiLineEqual` compares two strings, and if they're
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001180 not equal, displays a helpful comparison that highlights the
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001181 differences in the two strings. This comparison is now used by
Ezio Melotti021f3342010-04-06 03:26:49 +00001182 default when Unicode strings are compared with :meth:`~unittest.TestCase.assertEqual`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001183
Ezio Melotti021f3342010-04-06 03:26:49 +00001184* :meth:`~unittest.TestCase.assertRegexpMatches` checks whether its first argument is a
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001185 string matching a regular expression provided as its second argument.
1186
Ezio Melotti021f3342010-04-06 03:26:49 +00001187 .. XXX add assertNotRegexpMatches see issue 8038
1188
1189* :meth:`~unittest.TestCase.assertRaisesRegexp` checks whether a particular exception
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001190 is raised, and then also checks that the string representation of
1191 the exception matches the provided regular expression.
1192
Ezio Melotti021f3342010-04-06 03:26:49 +00001193* :meth:`~unittest.TestCase.assertIn` and :meth:`~unittest.TestCase.assertNotIn`
1194 tests whether *first* is or is not in *second*.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001195
Ezio Melotti021f3342010-04-06 03:26:49 +00001196* :meth:`~unittest.TestCase.assertItemsEqual` tests whether two provided sequences
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001197 contain the same elements.
1198
Ezio Melotti021f3342010-04-06 03:26:49 +00001199* :meth:`~unittest.TestCase.assertSetEqual` compares whether two sets are equal, and
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001200 only reports the differences between the sets in case of error.
1201
Ezio Melotti021f3342010-04-06 03:26:49 +00001202* Similarly, :meth:`~unittest.TestCase.assertListEqual` and :meth:`~unittest.TestCase.assertTupleEqual`
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001203 compare the specified types and explain any differences without necessarily
1204 printing their full values; these methods are now used by default
Ezio Melotti021f3342010-04-06 03:26:49 +00001205 when comparing lists and tuples using :meth:`~unittest.TestCase.assertEqual`.
1206 More generally, :meth:`~unittest.TestCase.assertSequenceEqual` compares two sequences
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001207 and can optionally check whether both sequences are of a
1208 particular type.
1209
Ezio Melotti021f3342010-04-06 03:26:49 +00001210* :meth:`~unittest.TestCase.assertDictEqual` compares two dictionaries and reports the
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001211 differences; it's now used by default when you compare two dictionaries
Ezio Melotti021f3342010-04-06 03:26:49 +00001212 using :meth:`~unittest.TestCase.assertEqual`. :meth:`~unittest.TestCase.assertDictContainsSubset` checks whether
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001213 all of the key/value pairs in *first* are found in *second*.
1214
Ezio Melotti021f3342010-04-06 03:26:49 +00001215* :meth:`~unittest.TestCase.assertAlmostEqual` and :meth:`~unittest.TestCase.assertNotAlmostEqual` test
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +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.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001219
Ezio Melotti021f3342010-04-06 03:26:49 +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`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001223
Ezio Melotti021f3342010-04-06 03:26:49 +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
Andrew M. Kuchling9858f632010-03-23 18:39:24 +00001226 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.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001232
Andrew M. Kuchling9858f632010-03-23 18:39:24 +00001233:func:`unittest.main` now takes an optional ``exit`` argument. If
Ezio Melotti021f3342010-04-06 03:26:49 +00001234False, :func:`~unittest.main` doesn't call :func:`sys.exit`, allowing it to be
Andrew M. Kuchling9858f632010-03-23 18:39:24 +00001235used from the interactive interpreter. (Contributed by J. Pablo
1236Fernández; :issue:`3379`.)
1237
1238A 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`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001242
Ezio Melotti021f3342010-04-06 03:26:49 +00001243.. 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
Andrew M. Kuchling9858f632010-03-23 18:39:24 +00001247and after a test run. (Contributed by Robert Collins; :issue:`5728`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001257importlib: Importing Modules
1258------------------------------
1259
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +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
Brett Cannonca2dc472009-12-22 02:37:37 +00001263to users who wish to write new importers that can participate in the
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001264import process. Python 2.7 doesn't contain the complete
1265:mod:`importlib` package, but instead has a tiny subset that contains
Ezio Melotti021f3342010-04-06 03:26:49 +00001266a single function, :func:`~importlib.import_module`.
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001267
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001268``import_module(name, package=None)`` imports a module. *name* is
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +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 Melotti021f3342010-04-06 03:26:49 +00001274the relative import. :func:`~importlib.import_module` both inserts the imported
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +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
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001291
Andrew M. Kuchlinga17cd4a2009-01-31 02:50:09 +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 Brandl0516f812009-11-18 18:52:35 +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
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001317.. ======================================================================
1318
1319
1320Build and C API Changes
1321=======================
1322
1323Changes to Python's build process and to the C API include:
1324
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +00001325* If you use the :file:`.gdbinit` file provided with Python,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001328 (Contributed by Victor Stinner; :issue:`3632`.)
1329
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +00001330* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +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`.)
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001360
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +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
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001413* Global symbols defined by the :mod:`ctypes` module are now prefixed
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001414 with ``Py``, or with ``_ctypes``. (Implemented by Thomas
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001415 Heller; :issue:`3102`.)
1416
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +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
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00001427* 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
Andrew M. Kuchling77069572009-03-31 01:21:01 +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`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001437
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001444
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001445.. ======================================================================
1446
1447Port-Specific Changes: Windows
1448-----------------------------------
1449
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +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`.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +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`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001460
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001461* The :func:`os.listdir` function now correctly fails
1462 for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.)
1463
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +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
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001468.. ======================================================================
1469
1470Port-Specific Changes: Mac OS X
1471-----------------------------------
1472
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001473* The path ``/Library/Python/2.7/site-packages`` is now appended to
Andrew M. Kuchling77069572009-03-31 01:21:01 +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
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001478
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001479Other Changes and Fixes
1480=======================
1481
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001482* Two benchmark scripts, :file:`iobench` and :file:`ccbench`, were
1483 added to the :file:`Tools` directory. :file:`iobench` measures the
Antoine Pitroudde96e62010-02-08 20:25:47 +00001484 speed of built-in file I/O objects (as returned by :func:`open`)
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001485 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.
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001489
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001490* When importing a module from a :file:`.pyc` or :file:`.pyo` file
1491 with an existing :file:`.py` counterpart, the :attr:`co_filename`
Andrew M. Kuchling92b97002009-05-02 17:12:15 +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`.)
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001496
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001497* 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.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001500 The :option:`-r` option also reports the seed that was used
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001501 (Added by Collin Winter.)
1502
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001503* Another :file:`regrtest.py` switch is :option:`-j`, which
1504 takes an integer specifying how many tests run in parallel. This
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001505 allows reducing the total runtime on multi-core machines.
Antoine Pitrou4698d992009-05-31 14:20:14 +00001506 This option is compatible with several other options, including the
1507 :option:`-R` switch which is known to produce long runtimes.
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +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`.)
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001511
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001512.. ======================================================================
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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +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
Amaury Forgeot d'Arcd81333c2009-06-10 20:30:19 +00001534 affects new-style classes (derived from :class:`object`) and C extension
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00001535 types. (:issue:`6101`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001536
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +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
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +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
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001549For C extensions:
1550
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00001551* 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
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001555* 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
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00001559
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +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
Andrew M. Kuchling8f254e72009-12-08 02:37:05 +00001570article: Ryan Lovett, Hugh Secker-Walker.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001571