blob: e20e2043c4a52e84c34027451f21b55f37b09307 [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
Andrew M. Kuchling6d7dfa22010-04-11 12:49:37 +000013.. hyperlink all the methods & functions.
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +000014
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000015.. $Id$
16 Rules for maintenance:
17
18 * Anyone can add text to this document. Do not spend very much time
19 on the wording of your changes, because your text will probably
20 get rewritten to some degree.
21
22 * The maintainer will go through Misc/NEWS periodically and add
23 changes; it's therefore more important to add your changes to
24 Misc/NEWS than to this file.
25
26 * This is not a complete list of every single change; completeness
27 is the purpose of Misc/NEWS. Some changes I consider too small
28 or esoteric to include. If such a change is added to the text,
29 I'll just remove it. (This is another reason you shouldn't spend
30 too much time on writing your addition.)
31
32 * If you want to draw your new text to the attention of the
33 maintainer, add 'XXX' to the beginning of the paragraph or
34 section.
35
36 * It's OK to just add a fragmentary note about a change. For
37 example: "XXX Describe the transmogrify() function added to the
38 socket module." The maintainer will research the change and
39 write the necessary text.
40
41 * You can comment out your additions if you like, but it's not
42 necessary (especially when a final release is some months away).
43
Ezio Melotti021f3342010-04-06 03:26:49 +000044 * Credit the author of a patch or bugfix. Just the name is
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000045 sufficient; the e-mail address isn't necessary.
46
47 * It's helpful to add the bug/patch number in a parenthetical comment.
48
49 XXX Describe the transmogrify() function added to the socket
50 module.
51 (Contributed by P.Y. Developer; :issue:`12345`.)
52
53 This saves the maintainer some effort going through the SVN logs
54 when researching a change.
55
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +000056This article explains the new features in Python 2.7. The final
57release of 2.7 is currently scheduled for June 2010; the detailed
58schedule is described in :pep:`373`.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000059
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +000060Python 2.7 is planned to be the last major release in the 2.x series.
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +000061Though more major releases have not been absolutely ruled out, the
62Python maintainers are planning to focus more on Python 3.x. Despite
63that, it's likely that the 2.7 release will have a longer period of
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +000064maintenance compared to earlier 2.x versions.
65
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000066.. Compare with previous release in 2 - 3 sentences here.
67 add hyperlink when the documentation becomes available online.
68
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000069.. _whatsnew27-python31:
70
71Python 3.1 Features
72=======================
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +000073
74Much as Python 2.6 incorporated features from Python 3.0,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000075version 2.7 incorporates some of the new features
76in Python 3.1. The 2.x series continues to provide tools
77for migrating to the 3.x series.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +000078
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000079A partial list of 3.1 features that were backported to 2.7:
80
81* A version of the :mod:`io` library, rewritten in C for performance.
82* The ordered-dictionary type described in :ref:`pep-0372`.
Andrew M. Kuchling8f254e72009-12-08 02:37:05 +000083* The new format specifier described in :ref:`pep-0378`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000084* The :class:`memoryview` object.
85* A small subset of the :mod:`importlib` module `described below <#importlib-section>`__.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +000086* Float-to-string and string-to-float conversions now round their
87 results more correctly. And :func:`repr` of a floating-point
88 number *x* returns a result that's guaranteed to round back to the
89 same number when converted back to a string.
90* The :cfunc:`PyLong_AsLongAndOverflow` C API function.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +000091
92One porting change: the :option:`-3` switch now automatically
93enables the :option:`-Qwarn` switch that causes warnings
94about using classic division with integers and long integers.
95
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000096Other new Python3-mode warnings include:
97
98* :func:`operator.isCallable` and :func:`operator.sequenceIncludes`,
99 which are not supported in 3.x.
100
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000101.. ========================================================================
102.. Large, PEP-level features and changes should be described here.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000103.. ========================================================================
104
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000105.. _pep-0372:
106
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000107PEP 372: Adding an ordered dictionary to collections
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000108====================================================
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000109
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000110Regular Python dictionaries iterate over key/value pairs in arbitrary order.
111Over the years, a number of authors have written alternative implementations
112that remember the order that the keys were originally inserted. Based on
113the experiences from those implementations, a new
Ezio Melotti021f3342010-04-06 03:26:49 +0000114:class:`~collections.OrderedDict` class has been introduced in the
115:mod:`collections` module.
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000116
Ezio Melotti021f3342010-04-06 03:26:49 +0000117The :class:`~collections.OrderedDict` API is substantially the same as regular
118dictionaries but will iterate over keys and values in a guaranteed order
119depending on when a key was first inserted::
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000120
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000121 >>> from collections import OrderedDict
122 >>> d = OrderedDict([('first', 1), ('second', 2),
123 ... ('third', 3)])
124 >>> d.items()
125 [('first', 1), ('second', 2), ('third', 3)]
126
127If a new entry overwrites an existing entry, the original insertion
128position is left unchanged::
129
130 >>> d['second'] = 4
131 >>> d.items()
132 [('first', 1), ('second', 4), ('third', 3)]
133
134Deleting an entry and reinserting it will move it to the end::
135
136 >>> del d['second']
137 >>> d['second'] = 5
138 >>> d.items()
139 [('first', 1), ('third', 3), ('second', 5)]
140
Ezio Melotti021f3342010-04-06 03:26:49 +0000141The :meth:`~collections.OrderedDict.popitem` method has an optional *last*
142argument that defaults to True. If *last* is True, the most recently
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000143added key is returned and removed; if it's False, the
144oldest key is selected::
145
146 >>> od = OrderedDict([(x,0) for x in range(20)])
147 >>> od.popitem()
148 (19, 0)
149 >>> od.popitem()
150 (18, 0)
Ezio Melotti021f3342010-04-06 03:26:49 +0000151 >>> od.popitem(last=False)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000152 (0, 0)
Ezio Melotti021f3342010-04-06 03:26:49 +0000153 >>> od.popitem(last=False)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000154 (1, 0)
155
156Comparing two ordered dictionaries checks both the keys and values,
157and requires that the insertion order was the same::
158
159 >>> od1 = OrderedDict([('first', 1), ('second', 2),
160 ... ('third', 3)])
161 >>> od2 = OrderedDict([('third', 3), ('first', 1),
162 ... ('second', 2)])
Ezio Melotti021f3342010-04-06 03:26:49 +0000163 >>> od1 == od2
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000164 False
165 >>> # Move 'third' key to the end
Ezio Melotti021f3342010-04-06 03:26:49 +0000166 >>> del od2['third']; od2['third'] = 3
167 >>> od1 == od2
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000168 True
169
Ezio Melotti021f3342010-04-06 03:26:49 +0000170Comparing an :class:`~collections.OrderedDict` with a regular dictionary
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000171ignores the insertion order and just compares the keys and values.
172
Ezio Melotti021f3342010-04-06 03:26:49 +0000173How does the :class:`~collections.OrderedDict` work? It maintains a
174doubly-linked list of keys, appending new keys to the list as they're inserted.
175A secondary dictionary maps keys to their corresponding list node, so
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000176deletion doesn't have to traverse the entire linked list and therefore
177remains O(1).
178
179.. XXX check O(1)-ness with Raymond
Ezio Melotti021f3342010-04-06 03:26:49 +0000180.. Also check if the 'somenamedtuple' in the collection module should
181.. be replaced/removed in order to use
182.. :meth:`~collections.namedtuple._asdict()` (see below)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000183
184The standard library now supports use of ordered dictionaries in several
Ezio Melotti021f3342010-04-06 03:26:49 +0000185modules. The :mod:`ConfigParser` module uses them by default. This lets
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000186configuration files be read, modified, and then written back in their original
Ezio Melotti021f3342010-04-06 03:26:49 +0000187order. The :meth:`~collections.somenamedtuple._asdict()` method for
188:func:`collections.namedtuple` now returns an ordered dictionary with the
189values appearing in the same order as the underlying tuple indices.
190The :mod:`json` module is being built-out with an *object_pairs_hook* to allow
191OrderedDicts to be built by the decoder.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000192Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_.
193
Andrew M. Kuchling7fe65a02009-10-13 15:49:33 +0000194.. seealso::
195
196 :pep:`372` - Adding an ordered dictionary to collections
197 PEP written by Armin Ronacher and Raymond Hettinger;
198 implemented by Raymond Hettinger.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000199
200.. _pep-0378:
201
202PEP 378: Format Specifier for Thousands Separator
Ezio Melotti021f3342010-04-06 03:26:49 +0000203=================================================
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000204
205To make program output more readable, it can be useful to add
206separators to large numbers and render them as
20718,446,744,073,709,551,616 instead of 18446744073709551616.
208
209The fully general solution for doing this is the :mod:`locale` module,
210which can use different separators ("," in North America, "." in
211Europe) and different grouping sizes, but :mod:`locale` is complicated
212to use and unsuitable for multi-threaded applications where different
213threads are producing output for different locales.
214
215Therefore, a simple comma-grouping mechanism has been added to the
Ezio Melotti021f3342010-04-06 03:26:49 +0000216mini-language used by the :meth:`str.format` method. When
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000217formatting a floating-point number, simply include a comma between the
218width and the precision::
219
Eric Smithc4663852010-04-06 14:30:15 +0000220 >>> '{:20,.2f}'.format(18446744073709551616.0)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000221 '18,446,744,073,709,551,616.00'
222
Eric Smith6a928602010-04-06 15:17:33 +0000223When formatting an integer, include the comma after the width:
224
225 >>> '{:20,d}'.format(18446744073709551616)
226 '18,446,744,073,709,551,616'
227
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000228This mechanism is not adaptable at all; commas are always used as the
229separator and the grouping is always into three-digit groups. The
230comma-formatting mechanism isn't as general as the :mod:`locale`
231module, but it's easier to use.
232
Andrew M. Kuchling85ea4bf2009-10-05 22:45:39 +0000233.. XXX "Format String Syntax" in string.rst could use many more examples.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000234
235.. seealso::
236
237 :pep:`378` - Format Specifier for Thousands Separator
238 PEP written by Raymond Hettinger; implemented by Eric Smith.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000239
Andrew M. Kuchlingab21f752010-03-02 13:55:33 +0000240PEP 389: The argparse Module for Parsing Command Lines
241======================================================
242
Andrew M. Kuchlingec6393f2010-04-11 01:40:30 +0000243The :mod:`argparse` module for parsing command-line arguments was
244added, intended as a more powerful replacement for the
245:mod:`optparse` module.
246
247This means Python now supports three different modules for parsing
248command-line arguments: :mod:`getopt`, :mod:`optparse`, and
249:mod:`argparse`. The :mod:`getopt` module closely resembles the C
250:cfunc:`getopt` function, so it remains useful if you're writing a
251Python prototype that will eventually be rewritten in C.
252:mod:`optparse` becomes redundant, but there are no plans to remove it
253because there are many scripts still using it, and there's no
254automated way to update these scripts. (Making the :mod:`argparse`
255API consistent with :mod:`optparse`'s interface was discussed but
256rejected as too messy and difficult.)
257
258To summarize, if you're writing a new script and don't need to worry
259about compatibility with earlier versions of Python, use
260:mod:`argparse` instead of :mod:`optparse`.
261
262XXX need an example
Andrew M. Kuchlingab21f752010-03-02 13:55:33 +0000263
264.. seealso::
265
Andrew M. Kuchlingec6393f2010-04-11 01:40:30 +0000266 `argparse module documentation <http://docs.python.org/dev/library/argparse.html>`__
267
268 `Upgrading optparse code to use argparse <http://docs.python.org/dev/library/argparse.html#upgrading-optparse-code>`__
269
Andrew M. Kuchlingab21f752010-03-02 13:55:33 +0000270 :pep:`389` - argparse - New Command Line Parsing Module
271 PEP written and implemented by Steven Bethard.
272
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000273PEP 391: Dictionary-Based Configuration For Logging
274====================================================
275
276XXX write this section.
277
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000278Two smaller enhancements to the logging module are:
279
280.. rev79293
281
282* :class:`Logger` instances gained a :meth:`getChild` that retrieves a
283 descendant logger using a relative path. For example,
284 once you retrieve a logger by doing ``log = getLogger('app')``,
285 calling ``log.getChild('network.listen')`` is equivalent to
286 ``getLogger('app.network.listen')``.
287
288* The :class:`LoggerAdapter` class gained a :meth:`isEnabledFor` method
289 that takes a *level* and returns whether the underlying logger would
290 process a message of that level of importance.
291
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000292.. seealso::
293
294 :pep:`391` - Dictionary-Based Configuration For Logging
295 PEP written and implemented by Vinay Sajip.
296
297PEP 3106: Dictionary Views
298====================================================
299
300XXX write this section.
301
302.. seealso::
303
304 :pep:`3106` - Revamping dict.keys(), .values() and .items()
305 PEP written by Guido van Rossum.
306 Backported to 2.7 by Alexandre Vassalotti; :issue:`1967`.
307
308
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000309Other Language Changes
310======================
311
312Some smaller changes made to the core Python language are:
313
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000314* The syntax for set literals has been backported from Python 3.x.
315 Curly brackets are used to surround the contents of the resulting
316 mutable set; set literals are
317 distinguished from dictionaries by not containing colons and values.
318 ``{}`` continues to represent an empty dictionary; use
319 ``set()`` for an empty set.
320
321 >>> {1,2,3,4,5}
322 set([1, 2, 3, 4, 5])
Ezio Melotti021f3342010-04-06 03:26:49 +0000323 >>> set() # empty set
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000324 set([])
Ezio Melotti021f3342010-04-06 03:26:49 +0000325 >>> {} # empty dict
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000326 {}
327
328 Backported by Alexandre Vassalotti; :issue:`2335`.
329
330* Dictionary and set comprehensions are another feature backported from
331 3.x, generalizing list/generator comprehensions to use
332 the literal syntax for sets and dictionaries.
333
334 >>> {x:x*x for x in range(6)}
335 {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
336 >>> {'a'*x for x in range(6)}
337 set(['', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa'])
338
339 Backported by Alexandre Vassalotti; :issue:`2333`.
340
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000341* The :keyword:`with` statement can now use multiple context managers
342 in one statement. Context managers are processed from left to right
343 and each one is treated as beginning a new :keyword:`with` statement.
344 This means that::
345
346 with A() as a, B() as b:
347 ... suite of statements ...
348
349 is equivalent to::
350
351 with A() as a:
352 with B() as b:
353 ... suite of statements ...
354
355 The :func:`contextlib.nested` function provides a very similar
356 function, so it's no longer necessary and has been deprecated.
357
358 (Proposed in http://codereview.appspot.com/53094; implemented by
359 Georg Brandl.)
360
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000361* Conversions between floating-point numbers and strings are
362 now correctly rounded on most platforms. These conversions occur
363 in many different places: :func:`str` on
364 floats and complex numbers; the :class:`float` and :class:`complex`
365 constructors;
366 numeric formatting; serialization and
367 deserialization of floats and complex numbers using the
368 :mod:`marshal`, :mod:`pickle`
369 and :mod:`json` modules;
370 parsing of float and imaginary literals in Python code;
Ezio Melotti021f3342010-04-06 03:26:49 +0000371 and :class:`~decimal.Decimal`-to-float conversion.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000372
373 Related to this, the :func:`repr` of a floating-point number *x*
374 now returns a result based on the shortest decimal string that's
375 guaranteed to round back to *x* under correct rounding (with
376 round-half-to-even rounding mode). Previously it gave a string
377 based on rounding x to 17 decimal digits.
378
Ezio Melotti021f3342010-04-06 03:26:49 +0000379 .. maybe add an example?
380
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000381 The rounding library responsible for this improvement works on
382 Windows, and on Unix platforms using the gcc, icc, or suncc
383 compilers. There may be a small number of platforms where correct
384 operation of this code cannot be guaranteed, so the code is not
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +0000385 used on such systems. You can find out which code is being used
386 by checking :data:`sys.float_repr_style`, which will be ``short``
387 if the new code is in use and ``legacy`` if it isn't.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000388
Mark Dickinsonbdd863d2010-01-07 09:28:29 +0000389 Implemented by Eric Smith and Mark Dickinson, using David Gay's
390 :file:`dtoa.c` library; :issue:`7117`.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000391
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000392* The :meth:`str.format` method now supports automatic numbering of the replacement
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +0000393 fields. This makes using :meth:`str.format` more closely resemble using
394 ``%s`` formatting::
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000395
396 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
397 '2009:4:Sunday'
398 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
399 '2009:4:Sunday'
400
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +0000401 The auto-numbering takes the fields from left to right, so the first ``{...}``
402 specifier will use the first argument to :meth:`str.format`, the next
403 specifier will use the next argument, and so on. You can't mix auto-numbering
404 and explicit numbering -- either number all of your specifier fields or none
405 of them -- but you can mix auto-numbering and named fields, as in the second
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000406 example above. (Contributed by Eric Smith; :issue:`5237`.)
407
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000408 Complex numbers now correctly support usage with :func:`format`,
409 and default to being right-aligned.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000410 Specifying a precision or comma-separation applies to both the real
411 and imaginary parts of the number, but a specified field width and
412 alignment is applied to the whole of the resulting ``1.5+3j``
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000413 output. (Contributed by Eric Smith; :issue:`1588` and :issue:`7988`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000414
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000415 The 'F' format code now always formats its output using uppercase characters,
416 so it will now produce 'INF' and 'NAN'.
417 (Contributed by Eric Smith; :issue:`3382`.)
418
Mark Dickinson1a707982008-12-17 16:14:37 +0000419* The :func:`int` and :func:`long` types gained a ``bit_length``
Georg Brandl64e1c752009-04-11 18:19:27 +0000420 method that returns the number of bits necessary to represent
Mark Dickinson1a707982008-12-17 16:14:37 +0000421 its argument in binary::
422
423 >>> n = 37
Ezio Melotti021f3342010-04-06 03:26:49 +0000424 >>> bin(n)
Mark Dickinson1a707982008-12-17 16:14:37 +0000425 '0b100101'
426 >>> n.bit_length()
427 6
428 >>> n = 2**123-1
429 >>> n.bit_length()
430 123
431 >>> (n+1).bit_length()
432 124
433
434 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
435
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000436* Conversions from long integers and regular integers to floating
437 point now round differently, returning the floating-point number
438 closest to the number. This doesn't matter for small integers that
439 can be converted exactly, but for large numbers that will
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000440 unavoidably lose precision, Python 2.7 now approximates more
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000441 closely. For example, Python 2.6 computed the following::
442
443 >>> n = 295147905179352891391
444 >>> float(n)
445 2.9514790517935283e+20
446 >>> n - long(float(n))
447 65535L
448
449 Python 2.7's floating-point result is larger, but much closer to the
450 true value::
451
452 >>> n = 295147905179352891391
453 >>> float(n)
454 2.9514790517935289e+20
Ezio Melotti021f3342010-04-06 03:26:49 +0000455 >>> n - long(float(n))
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000456 -1L
457
458 (Implemented by Mark Dickinson; :issue:`3166`.)
459
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000460 Integer division is also more accurate in its rounding behaviours. (Also
461 implemented by Mark Dickinson; :issue:`1811`.)
462
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000463* It's now possible for a subclass of the built-in :class:`unicode` type
464 to override the :meth:`__unicode__` method. (Implemented by
465 Victor Stinner; :issue:`1583863`.)
466
Ezio Melotti021f3342010-04-06 03:26:49 +0000467* The :class:`bytearray` type's :meth:`~bytearray.translate` method now accepts
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000468 ``None`` as its first argument. (Fixed by Georg Brandl;
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000469 :issue:`4759`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000470
Ezio Melotti021f3342010-04-06 03:26:49 +0000471 .. bytearray doesn't seem to be documented
472
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000473* When using ``@classmethod`` and ``@staticmethod`` to wrap
474 methods as class or static methods, the wrapper object now
475 exposes the wrapped function as their :attr:`__func__` attribute.
476 (Contributed by Amaury Forgeot d'Arc, after a suggestion by
477 George Sakkis; :issue:`5982`.)
478
479* A new encoding named "cp720", used primarily for Arabic text, is now
480 supported. (Contributed by Alexander Belchenko and Amaury Forgeot
481 d'Arc; :issue:`1616979`.)
482
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000483* The :class:`file` object will now set the :attr:`filename` attribute
484 on the :exc:`IOError` exception when trying to open a directory
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +0000485 on POSIX platforms (noted by Jan Kaliszewski; :issue:`4764`), and
486 now explicitly checks for and forbids writing to read-only file objects
487 instead of trusting the C library to catch and report the error
488 (fixed by Stefan Krah; :issue:`5677`).
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000489
Benjamin Petersonae9a0a02009-12-31 16:49:37 +0000490* The Python tokenizer now translates line endings itself, so the
491 :func:`compile` built-in function can now accept code using any
492 line-ending convention. Additionally, it no longer requires that the
493 code end in a newline.
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +0000494
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000495* Extra parentheses in function definitions are illegal in Python 3.x,
496 meaning that you get a syntax error from ``def f((x)): pass``. In
497 Python3-warning mode, Python 2.7 will now warn about this odd usage.
498 (Noted by James Lingard; :issue:`7362`.)
499
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +0000500* It's now possible to create weak references to old-style class
501 objects. New-style classes were always weak-referenceable. (Fixed
502 by Antoine Pitrou; :issue:`8268`.)
503
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000504* When a module object is garbage-collected, the module's dictionary is
505 now only cleared if no one else is holding a reference to the
506 dictionary (:issue:`7140`).
507
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000508.. ======================================================================
509
Andrew M. Kuchlingec6393f2010-04-11 01:40:30 +0000510.. _new-27-interpreter:
511
512Interpreter Changes
513-------------------------------
514
515A new environment variable, :envvar:`PYTHONWARNINGS`,
516allows controlling warnings. It should be set to a string
517containing warning settings, equivalent to those
518used with the :option:`-W` switch, separated by commas.
519(Contributed by Brian Curtin; :issue:`7301`.)
520
521For example, the following setting will print warnings every time
522they occur, but turn warnings from the :mod:`Cookie` module into an
523error. (The exact syntax for setting an environment variable varies
524across operating systems and shells, so it may be different for you.)
525
526::
527
528 export PYTHONWARNINGS=all,error:::Cookie:0
529
530
531.. ======================================================================
532
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000533
534Optimizations
535-------------
536
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000537Several performance enhancements have been added:
538
539.. * A new :program:`configure` option, :option:`--with-computed-gotos`,
540 compiles the main bytecode interpreter loop using a new dispatch
541 mechanism that gives speedups of up to 20%, depending on the system
542 and benchmark. The new mechanism is only supported on certain
543 compilers, such as gcc, SunPro, and icc.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000544
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000545* A new opcode was added to perform the initial setup for
546 :keyword:`with` statements, looking up the :meth:`__enter__` and
547 :meth:`__exit__` methods. (Contributed by Benjamin Peterson.)
548
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000549* The garbage collector now performs better for one common usage
550 pattern: when many objects are being allocated without deallocating
551 any of them. This would previously take quadratic
552 time for garbage collection, but now the number of full garbage collections
553 is reduced as the number of objects on the heap grows.
554 The new logic is to only perform a full garbage collection pass when
555 the middle generation has been collected 10 times and when the
556 number of survivor objects from the middle generation exceeds 10% of
557 the number of objects in the oldest generation. (Suggested by Martin
Ezio Melotti021f3342010-04-06 03:26:49 +0000558 von Löwis and implemented by Antoine Pitrou; :issue:`4074`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000559
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000560* The garbage collector tries to avoid tracking simple containers
561 which can't be part of a cycle. In Python 2.7, this is now true for
562 tuples and dicts containing atomic types (such as ints, strings,
563 etc.). Transitively, a dict containing tuples of atomic types won't
564 be tracked either. This helps reduce the cost of each
565 garbage collection by decreasing the number of objects to be
566 considered and traversed by the collector.
Antoine Pitrouc18f6b02009-03-28 19:10:13 +0000567 (Contributed by Antoine Pitrou; :issue:`4688`.)
568
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000569* Long integers are now stored internally either in base 2**15 or in base
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000570 2**30, the base being determined at build time. Previously, they
571 were always stored in base 2**15. Using base 2**30 gives
572 significant performance improvements on 64-bit machines, but
573 benchmark results on 32-bit machines have been mixed. Therefore,
574 the default is to use base 2**30 on 64-bit machines and base 2**15
575 on 32-bit machines; on Unix, there's a new configure option
576 :option:`--enable-big-digits` that can be used to override this default.
577
578 Apart from the performance improvements this change should be
579 invisible to end users, with one exception: for testing and
Ezio Melotti021f3342010-04-06 03:26:49 +0000580 debugging purposes there's a new structseq :data:`sys.long_info` that
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000581 provides information about the internal format, giving the number of
582 bits per digit and the size in bytes of the C type used to store
583 each digit::
584
585 >>> import sys
586 >>> sys.long_info
587 sys.long_info(bits_per_digit=30, sizeof_digit=4)
588
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000589 (Contributed by Mark Dickinson; :issue:`4258`.)
590
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000591 Another set of changes made long objects a few bytes smaller: 2 bytes
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000592 smaller on 32-bit systems and 6 bytes on 64-bit.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000593 (Contributed by Mark Dickinson; :issue:`5260`.)
594
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000595* The division algorithm for long integers has been made faster
596 by tightening the inner loop, doing shifts instead of multiplications,
597 and fixing an unnecessary extra iteration.
598 Various benchmarks show speedups of between 50% and 150% for long
599 integer divisions and modulo operations.
600 (Contributed by Mark Dickinson; :issue:`5512`.)
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +0000601 Bitwise operations are also significantly faster (initial patch by
602 Gregory Smith; :issue:`1087418`).
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000603
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000604* The implementation of ``%`` checks for the left-side operand being
605 a Python string and special-cases it; this results in a 1-3%
606 performance increase for applications that frequently use ``%``
607 with strings, such as templating libraries.
608 (Implemented by Collin Winter; :issue:`5176`.)
609
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000610* List comprehensions with an ``if`` condition are compiled into
611 faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
612 by Jeffrey Yasskin; :issue:`4715`.)
613
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +0000614* Converting an integer or long integer to a decimal string was made
615 faster by special-casing base 10 instead of using a generalized
616 conversion function that supports arbitrary bases.
617 (Patch by Gawain Bolton; :issue:`6713`.)
618
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000619* The :meth:`split`, :meth:`replace`, :meth:`rindex`,
620 :meth:`rpartition`, and :meth:`rsplit` methods of string-like types
621 (strings, Unicode strings, and :class:`bytearray` objects) now use a
622 fast reverse-search algorithm instead of a character-by-character
623 scan. This is sometimes faster by a factor of 10. (Added by
624 Florent Xicluna; :issue:`7462` and :issue:`7622`.)
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +0000625
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000626* The :mod:`pickle` and :mod:`cPickle` modules now automatically
627 intern the strings used for attribute names, reducing memory usage
628 of the objects resulting from unpickling. (Contributed by Jake
629 McGuire; :issue:`5084`.)
630
631* The :mod:`cPickle` module now special-cases dictionaries,
632 nearly halving the time required to pickle them.
633 (Contributed by Collin Winter; :issue:`5670`.)
634
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000635.. ======================================================================
636
Georg Brandl0516f812009-11-18 18:52:35 +0000637New and Improved Modules
638========================
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000639
640As in every release, Python's standard library received a number of
641enhancements and bug fixes. Here's a partial list of the most notable
642changes, sorted alphabetically by module name. Consult the
643:file:`Misc/NEWS` file in the source tree for a more complete list of
644changes, or look through the Subversion logs for all the details.
645
Ezio Melotti021f3342010-04-06 03:26:49 +0000646* The :mod:`bdb` module's base debugging class :class:`~bdb.Bdb`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000647 gained a feature for skipping modules. The constructor
648 now takes an iterable containing glob-style patterns such as
649 ``django.*``; the debugger will not step into stack frames
650 from a module that matches one of these patterns.
651 (Contributed by Maru Newby after a suggestion by
652 Senthil Kumaran; :issue:`5142`.)
653
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +0000654* The :mod:`binascii` module now supports the buffer API, so it can be
655 used with :class:`memoryview` instances and other similar buffer objects.
656 (Backported from 3.x by Florent Xicluna; :issue:`7703`.)
657
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000658* Updated module: the :mod:`bsddb` module has been updated from 4.7.2devel9
659 to version 4.8.4 of
660 `the pybsddb package <http://www.jcea.es/programacion/pybsddb.htm>`__.
661 The new version features better Python 3.x compatibility, various bug fixes,
662 and adds several new BerkeleyDB flags and methods.
Ezio Melotti021f3342010-04-06 03:26:49 +0000663 (Updated by Jesús Cea Avión; :issue:`8156`. The pybsddb
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000664 changelog can be browsed at http://hg.jcea.es/pybsddb/file/tip/ChangeLog.)
665
Ezio Melotti021f3342010-04-06 03:26:49 +0000666* The :mod:`bz2` module's :class:`~bz2.BZ2File` now supports the context
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000667 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
668 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
669
Ezio Melotti021f3342010-04-06 03:26:49 +0000670* New class: the :class:`~collections.Counter` class in the :mod:`collections`
671 module is useful for tallying data. :class:`~collections.Counter` instances
672 behave mostly like dictionaries but return zero for missing keys instead of
Georg Brandlf6dab952009-04-28 21:48:35 +0000673 raising a :exc:`KeyError`:
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000674
Georg Brandlf6dab952009-04-28 21:48:35 +0000675 .. doctest::
676 :options: +NORMALIZE_WHITESPACE
677
678 >>> from collections import Counter
679 >>> c = Counter()
680 >>> for letter in 'here is a sample of english text':
681 ... c[letter] += 1
682 ...
683 >>> c
684 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
685 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
686 'p': 1, 'r': 1, 'x': 1})
687 >>> c['e']
688 5
689 >>> c['z']
690 0
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000691
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +0000692 There are three additional :class:`~collections.Counter` methods:
693 :meth:`~collections.Counter.most_common` returns the N most common
694 elements and their counts. :meth:`~collections.Counter.elements`
695 returns an iterator over the contained elements, repeating each
696 element as many times as its count.
697 :meth:`~collections.Counter.subtract` takes an iterable and
698 subtracts one for each element instead of adding; if the argument is
699 a dictionary or another :class:`Counter`, the counts are
700 subtracted. ::
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000701
702 >>> c.most_common(5)
703 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
704 >>> c.elements() ->
705 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
706 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
707 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
Georg Brandlf6dab952009-04-28 21:48:35 +0000708 's', 's', 'r', 't', 't', 'x'
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +0000709 >>> c['e']
710 5
711 >>> c.subtract('very heavy on the letter e')
712 >>> c['e'] # Count is now lower
713 -1
Ezio Melotti021f3342010-04-06 03:26:49 +0000714
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000715 Contributed by Raymond Hettinger; :issue:`1696199`.
716
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +0000717 .. revision 79660
718
Ezio Melotti021f3342010-04-06 03:26:49 +0000719 The new :class:`~collections.OrderedDict` class is described in the earlier
720 section :ref:`pep-0372`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000721
Ezio Melotti021f3342010-04-06 03:26:49 +0000722 The :class:`~collections.namedtuple` class now has an optional *rename* parameter.
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000723 If *rename* is true, field names that are invalid because they've
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000724 been repeated or that aren't legal Python identifiers will be
725 renamed to legal names that are derived from the field's
726 position within the list of fields:
727
Georg Brandlf6dab952009-04-28 21:48:35 +0000728 >>> from collections import namedtuple
729 >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000730 >>> T._fields
731 ('field1', '_1', '_2', 'field2')
732
733 (Added by Raymond Hettinger; :issue:`1818`.)
734
Andrew M. Kuchling6d7dfa22010-04-11 12:49:37 +0000735 The :class:`~collections.deque` data type now has a
736 :meth:`~collections.deque.count` method that returns the number of
737 contained elements equal to the supplied argument *x*, and a
738 :meth:`~collections.deque.reverse` method that reverses the elements
739 of the deque in-place. :class:`deque` also exposes its maximum
740 length as the read-only :attr:`~collections.deque.maxlen` attribute.
741 (Both features added by Raymond Hettinger.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000742
Ezio Melotti021f3342010-04-06 03:26:49 +0000743* The :mod:`copy` module's :func:`~copy.deepcopy` function will now
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000744 correctly copy bound instance methods. (Implemented by
745 Robert Collins; :issue:`1515`.)
746
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000747* The :mod:`ctypes` module now always converts ``None`` to a C NULL
748 pointer for arguments declared as pointers. (Changed by Thomas
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +0000749 Heller; :issue:`4606`.) The underlying `libffi library
750 <http://sourceware.org/libffi/>`__ has been updated to version
751 3.0.9, containing various fixes for different platforms. (Updated
752 by Matthias Klose; :issue:`8142`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000753
Ezio Melotti021f3342010-04-06 03:26:49 +0000754* New method: the :mod:`datetime` module's :class:`~datetime.timedelta` class
755 gained a :meth:`~datetime.timedelta.total_seconds` method that returns the
756 number of seconds in the duration. (Contributed by Brian Quinlan; :issue:`5788`.)
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000757
Ezio Melotti021f3342010-04-06 03:26:49 +0000758* New method: the :class:`~decimal.Decimal` class gained a
759 :meth:`~decimal.Decimal.from_float` class method that performs an exact
760 conversion of a floating-point number to a :class:`~decimal.Decimal`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000761 Note that this is an **exact** conversion that strives for the
762 closest decimal approximation to the floating-point representation's value;
763 the resulting decimal value will therefore still include the inaccuracy,
764 if any.
765 For example, ``Decimal.from_float(0.1)`` returns
766 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
767 (Implemented by Raymond Hettinger; :issue:`4796`.)
768
Ezio Melotti021f3342010-04-06 03:26:49 +0000769 Most of the methods of the :class:`~decimal.Context` class now accept integers
770 as well as :class:`~decimal.Decimal` instances; the only exceptions are the
771 :meth:`~decimal.Context.canonical` and :meth:`~decimal.Context.is_canonical`
772 methods. (Patch by Juan José Conti; :issue:`7633`.)
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000773
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +0000774 The constructor for :class:`~decimal.Decimal` now accepts
775 floating-point numbers (added by Raymond Hettinger; :issue:`8257`)
776 and non-European Unicode characters such as Arabic-Indic digits
777 (contributed by Mark Dickinson; :issue:`6595`).
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000778
Ezio Melotti021f3342010-04-06 03:26:49 +0000779 When using :class:`~decimal.Decimal` instances with a string's
780 :meth:`~str.format` method, the default alignment was previously
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000781 left-alignment. This has been changed to right-alignment, which seems
782 more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.)
783
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +0000784* The :class:`~fractions.Fraction` class now accepts a single float or
785 :class:`~decimal.Decimal` instance, or two rational numbers, as
786 arguments to its constructor. (Implemented by Mark Dickinson;
787 rationals added in :issue:`5812`, and float/decimal in
788 :issue:`8294`.)
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000789
Andrew M. Kuchlingec6393f2010-04-11 01:40:30 +0000790 An oversight was fixed, making the :class:`Fraction` match the other
791 numeric types; ordering comparisons (``<``, ``<=``, ``>``, ``>=``) between
792 fractions and complex numbers now raise a :exc:`TypeError`.
793
794 .. revision 79455
795
Ezio Melotti021f3342010-04-06 03:26:49 +0000796* New class: a new :class:`~ftplib.FTP_TLS` class in
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000797 the :mod:`ftplib` module provides secure FTP
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000798 connections using TLS encapsulation of authentication as well as
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000799 subsequent control and data transfers.
800 (Contributed by Giampaolo Rodola', :issue:`2054`.)
801
Ezio Melotti021f3342010-04-06 03:26:49 +0000802 The :meth:`~ftplib.FTP.storbinary` method for binary uploads can now restart
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000803 uploads thanks to an added *rest* parameter (patch by Pablo Mouzo;
804 :issue:`6845`.)
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000805
Andrew M. Kuchling6d7dfa22010-04-11 12:49:37 +0000806* New class decorator: :func:`total_ordering` in the :mod:`functools`
807 module takes a class that defines an :meth:`__eq__` method and one of
808 :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, or :meth:`__ge__`,
809 and generates the missing comparison methods. Since the
810 :meth:`__cmp__` method is being deprecated in Python 3.x,
811 this decorator makes it easier to define ordered classes.
812 (Added by Raymond Hettinger; :issue:`5479`.)
813
814 New function: :func:`cmp_to_key` will take an old-style comparison
815 function that expects two arguments and return a new callable that
816 can be used as the *key* parameter to functions such as
817 :func:`sorted`, :func:`min` and :func:`max`, etc. The primary
818 intended use is to help with making code compatible with Python 3.x.
819 (Added by Raymond Hettinger.)
820
Ezio Melotti021f3342010-04-06 03:26:49 +0000821* New function: the :mod:`gc` module's :func:`~gc.is_tracked` returns
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000822 true if a given instance is tracked by the garbage collector, false
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000823 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
824
Ezio Melotti021f3342010-04-06 03:26:49 +0000825* The :mod:`gzip` module's :class:`~gzip.GzipFile` now supports the context
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000826 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``
827 (contributed by Hagen Fuerstenau; :issue:`3860`), and it now implements
828 the :class:`io.BufferedIOBase` ABC, so you can wrap it with
829 :class:`io.BufferedReader` for faster processing
830 (contributed by Nir Aides; :issue:`7471`).
831 It's also now possible to override the modification time
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000832 recorded in a gzipped file by providing an optional timestamp to
833 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000834
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000835 Files in gzip format can be padded with trailing zero bytes; the
836 :mod:`gzip` module will now consume these trailing bytes. (Fixed by
837 Tadek Pietraszek and Brian Curtin; :issue:`2846`.)
838
Ezio Melotti021f3342010-04-06 03:26:49 +0000839* New attribute: the :mod:`hashlib` module now has an :attr:`~hashlib.hashlib.algorithms`
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +0000840 attribute containing a tuple naming the supported algorithms.
841 In Python 2.7, ``hashlib.algorithms`` contains
842 ``('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')``
843 (Contributed by Carl Chenet; :issue:`7418`.)
844
Ezio Melotti021f3342010-04-06 03:26:49 +0000845* The default :class:`~httplib.HTTPResponse` class used by the :mod:`httplib` module now
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000846 supports buffering, resulting in much faster reading of HTTP responses.
847 (Contributed by Kristjan Valur Jonsson; :issue:`4879`.)
848
Ezio Melotti021f3342010-04-06 03:26:49 +0000849 The :class:`~httplib.HTTPConnection` and :class:`~httplib.HTTPSConnection` classes
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +0000850 now support a *source_address* parameter, a ``(host, port)`` 2-tuple
851 giving the source address that will be used for the connection.
852 (Contributed by Eldon Ziegler; :issue:`3972`.)
853
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000854* The :mod:`imaplib` module now supports IPv6 addresses.
855 (Contributed by Derek Morr; :issue:`1655`.)
856
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000857* Updated module: The :mod:`io` library has been upgraded to the version shipped with
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000858 Python 3.1. For 3.1, the I/O library was entirely rewritten in C
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000859 and is 2 to 20 times faster depending on the task being performed. The
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000860 original Python version was renamed to the :mod:`_pyio` module.
861
862 One minor resulting change: the :class:`io.TextIOBase` class now
863 has an :attr:`errors` attribute giving the error setting
864 used for encoding and decoding errors (one of ``'strict'``, ``'replace'``,
865 ``'ignore'``).
866
867 The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000868 an invalid file descriptor. (Implemented by Benjamin Peterson;
Ezio Melotti021f3342010-04-06 03:26:49 +0000869 :issue:`4991`.) The :meth:`~io.IOBase.truncate` method now preserves the
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000870 file position; previously it would change the file position to the
871 end of the new file. (Fixed by Pascal Chambon; :issue:`6939`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000872
Andrew M. Kuchling5a73ff82009-12-02 14:27:11 +0000873* New function: ``itertools.compress(data, selectors)`` takes two
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000874 iterators. Elements of *data* are returned if the corresponding
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000875 value in *selectors* is true::
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000876
877 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
878 A, C, E, F
879
Ezio Melotti021f3342010-04-06 03:26:49 +0000880 .. maybe here is better to use >>> list(itertools.compress(...)) instead
881
Andrew M. Kuchling5a73ff82009-12-02 14:27:11 +0000882 New function: ``itertools.combinations_with_replacement(iter, r)``
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000883 returns all the possible *r*-length combinations of elements from the
Ezio Melotti021f3342010-04-06 03:26:49 +0000884 iterable *iter*. Unlike :func:`~itertools.combinations`, individual elements
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000885 can be repeated in the generated combinations::
886
887 itertools.combinations_with_replacement('abc', 2) =>
888 ('a', 'a'), ('a', 'b'), ('a', 'c'),
889 ('b', 'b'), ('b', 'c'), ('c', 'c')
890
891 Note that elements are treated as unique depending on their position
892 in the input, not their actual values.
893
Ezio Melotti021f3342010-04-06 03:26:49 +0000894 The :func:`itertools.count` function now has a *step* argument that
895 allows incrementing by values other than 1. :func:`~itertools.count` also
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000896 now allows keyword arguments, and using non-integer values such as
Ezio Melotti021f3342010-04-06 03:26:49 +0000897 floats or :class:`~decimal.Decimal` instances. (Implemented by Raymond
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000898 Hettinger; :issue:`5032`.)
899
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000900 :func:`itertools.combinations` and :func:`itertools.product` were
901 previously raising :exc:`ValueError` for values of *r* larger than
902 the input iterable. This was deemed a specification error, so they
903 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
904
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000905* Updated module: The :mod:`json` module was upgraded to version 2.0.9 of the
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000906 simplejson package, which includes a C extension that makes
907 encoding and decoding faster.
908 (Contributed by Bob Ippolito; :issue:`4136`.)
909
Ezio Melotti021f3342010-04-06 03:26:49 +0000910 To support the new :class:`collections.OrderedDict` type, :func:`json.load`
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000911 now has an optional *object_pairs_hook* parameter that will be called
912 with any object literal that decodes to a list of pairs.
913 (Contributed by Raymond Hettinger; :issue:`5381`.)
914
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000915* New functions: the :mod:`math` module gained
Ezio Melotti021f3342010-04-06 03:26:49 +0000916 :func:`~math.erf` and :func:`~math.erfc` for the error function and the complementary error function,
917 :func:`~math.expm1` which computes ``e**x - 1`` with more precision than
918 using :func:`~math.exp` and subtracting 1,
919 :func:`~math.gamma` for the Gamma function, and
920 :func:`~math.lgamma` for the natural log of the Gamma function.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000921 (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.)
922
Andrew M. Kuchling24520b42009-04-09 11:22:47 +0000923* The :mod:`multiprocessing` module's :class:`Manager*` classes
924 can now be passed a callable that will be called whenever
925 a subprocess is started, along with a set of arguments that will be
926 passed to the callable.
927 (Contributed by lekma; :issue:`5585`.)
928
Ezio Melotti021f3342010-04-06 03:26:49 +0000929 The :class:`~multiprocessing.Pool` class, which controls a pool of worker processes,
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000930 now has an optional *maxtasksperchild* parameter. Worker processes
931 will perform the specified number of tasks and then exit, causing the
Ezio Melotti021f3342010-04-06 03:26:49 +0000932 :class:`~multiprocessing.Pool` to start a new worker. This is useful if tasks may leak
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000933 memory or other resources, or if some tasks will cause the worker to
934 become very large.
935 (Contributed by Charles Cazabon; :issue:`6963`.)
936
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000937* The :mod:`nntplib` module now supports IPv6 addresses.
938 (Contributed by Derek Morr; :issue:`1664`.)
939
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000940* New functions: the :mod:`os` module wraps the following POSIX system
Ezio Melotti021f3342010-04-06 03:26:49 +0000941 calls: :func:`~os.getresgid` and :func:`~os.getresuid`, which return the
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000942 real, effective, and saved GIDs and UIDs;
Ezio Melotti021f3342010-04-06 03:26:49 +0000943 :func:`~os.setresgid` and :func:`~os.setresuid`, which set
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000944 real, effective, and saved GIDs and UIDs to new values;
Ezio Melotti021f3342010-04-06 03:26:49 +0000945 :func:`~os.initgroups`. (GID/UID functions
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000946 contributed by Travis H.; :issue:`6508`. Support for initgroups added
947 by Jean-Paul Calderone; :issue:`7333`.)
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000948
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +0000949 The :func:`os.fork` function now re-initializes the import lock in
Ezio Melotti021f3342010-04-06 03:26:49 +0000950 the child process; this fixes problems on Solaris when :func:`~os.fork`
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +0000951 is called from a thread. (Fixed by Zsolt Cserna; :issue:`7242`.)
952
Ezio Melotti021f3342010-04-06 03:26:49 +0000953* In the :mod:`os.path` module, the :func:`~os.path.normpath` and
954 :func:`~os.path.abspath` functions now preserve Unicode; if their input path
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000955 is a Unicode string, the return value is also a Unicode string.
Ezio Melotti021f3342010-04-06 03:26:49 +0000956 (:meth:`~os.path.normpath` fixed by Matt Giuca in :issue:`5827`;
957 :meth:`~os.path.abspath` fixed by Ezio Melotti in :issue:`3426`.)
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000958
Andrew M. Kuchling9cb42772009-01-21 02:15:43 +0000959* The :mod:`pydoc` module now has help for the various symbols that Python
960 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
961 (Contributed by David Laban; :issue:`4739`.)
962
Ezio Melotti021f3342010-04-06 03:26:49 +0000963* The :mod:`re` module's :func:`~re.split`, :func:`~re.sub`, and :func:`~re.subn`
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000964 now accept an optional *flags* argument, for consistency with the
965 other functions in the module. (Added by Gregory P. Smith.)
966
Ezio Melotti021f3342010-04-06 03:26:49 +0000967* New function: in the :mod:`shutil` module, :func:`~shutil.make_archive`
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +0000968 takes a filename, archive type (zip or tar-format), and a directory
969 path, and creates an archive containing the directory's contents.
970 (Added by Tarek Ziadé.)
971
Ezio Melotti021f3342010-04-06 03:26:49 +0000972 :mod:`shutil`'s :func:`~shutil.copyfile` and :func:`~shutil.copytree`
973 functions now raise a :exc:`~shutil.SpecialFileError` exception when
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000974 asked to copy a named pipe. Previously the code would treat
975 named pipes like a regular file by opening them for reading, and
976 this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.)
977
978* New functions: in the :mod:`site` module, three new functions
979 return various site- and user-specific paths.
Ezio Melotti021f3342010-04-06 03:26:49 +0000980 :func:`~site.getsitepackages` returns a list containing all
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000981 global site-packages directories, and
Ezio Melotti021f3342010-04-06 03:26:49 +0000982 :func:`~site.getusersitepackages` returns the path of the user's
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000983 site-packages directory.
Ezio Melotti021f3342010-04-06 03:26:49 +0000984 :func:`~site.getuserbase` returns the value of the :envvar:`USER_BASE`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000985 environment variable, giving the path to a directory that can be used
986 to store data.
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000987 (Contributed by Tarek Ziadé; :issue:`6693`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000988
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +0000989 The :mod:`site` module now reports exceptions occurring
990 when the :mod:`sitecustomize` module is imported, and will no longer
Florent Xiclunaad598332010-03-31 21:40:32 +0000991 catch and swallow the :exc:`KeyboardInterrupt` exception. (Fixed by
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +0000992 Victor Stinner; :issue:`3137`.)
993
Ezio Melotti021f3342010-04-06 03:26:49 +0000994* The :mod:`socket` module's :class:`~ssl.SSL` objects now support the
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +0000995 buffer API, which fixed a test suite failure (fix by Antoine Pitrou;
996 :issue:`7133`). :class:`SSL` objects also now automatically set
997 OpenSSL's :cmacro:`SSL_MODE_AUTO_RETRY`, which will prevent an error
998 code being returned from :meth:`recv` operations that trigger an SSL
999 renegotiation (fix by Antoine Pitrou; :issue:`8222`).
1000
1001 The version of OpenSSL being used is now available as the module
1002 attributes :attr:`OPENSSL_VERSION` (a string),
Andrew M. Kuchlingec6393f2010-04-11 01:40:30 +00001003 :attr:`OPENSSL_VERSION_INFO` (a 5-tuple), and
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +00001004 :attr:`OPENSSL_VERSION_NUMBER` (an integer). (Added by Antoine
1005 Pitrou; :issue:`8321`.)
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001006
Ezio Melotti021f3342010-04-06 03:26:49 +00001007 The :func:`~socket.create_connection` function
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +00001008 gained a *source_address* parameter, a ``(host, port)`` 2-tuple
1009 giving the source address that will be used for the connection.
1010 (Contributed by Eldon Ziegler; :issue:`3972`.)
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001011
Ezio Melotti021f3342010-04-06 03:26:49 +00001012 The :meth:`~socket.socket.recv_into` and :meth:`~socket.socket.recvfrom_into`
1013 methods will now write into objects that support the buffer API, most usefully
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001014 the :class:`bytearray` and :class:`memoryview` objects. (Implemented by
1015 Antoine Pitrou; :issue:`8104`.)
1016
Ezio Melotti021f3342010-04-06 03:26:49 +00001017* The :mod:`SocketServer` module's :class:`~SocketServer.TCPServer` class now
1018 has a :attr:`~SocketServer.TCPServer.disable_nagle_algorithm` class attribute.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001019 The default value is False; if overridden to be True,
1020 new request connections will have the TCP_NODELAY option set to
1021 prevent buffering many small sends into a single TCP packet.
1022 (Contributed by Kristjan Valur Jonsson; :issue:`6192`.)
1023
Ezio Melotti021f3342010-04-06 03:26:49 +00001024* Updated module: the :mod:`sqlite3` module has been updated to
Andrew M. Kuchlingfed15762010-03-08 12:00:39 +00001025 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
1026 the ability to load SQLite extensions from shared libraries.
1027 Call the ``enable_load_extension(True)`` method to enable extensions,
Ezio Melotti021f3342010-04-06 03:26:49 +00001028 and then call :meth:`~sqlite3.Connection.load_extension` to load a particular shared library.
Andrew M. Kuchlingfed15762010-03-08 12:00:39 +00001029 (Updated by Gerhard Häring.)
1030
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001031* The :mod:`struct` module will no longer silently ignore overflow
1032 errors when a value is too large for a particular integer format
1033 code (one of ``bBhHiIlLqQ``); it now always raises a
1034 :exc:`struct.error` exception. (Changed by Mark Dickinson;
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +00001035 :issue:`1523`.) The :func:`~struct.pack` function will also
1036 attempt to use :meth:`__index__` to convert and pack non-integers
1037 before trying the :meth:`__int__` method or reporting an error.
1038 (Changed by Mark Dickinson; :issue:`8300`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001039
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001040* New function: the :mod:`subprocess` module's
Ezio Melotti021f3342010-04-06 03:26:49 +00001041 :func:`~subprocess.check_output` runs a command with a specified set of arguments
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +00001042 and returns the command's output as a string when the command runs without
Ezio Melotti021f3342010-04-06 03:26:49 +00001043 error, or raises a :exc:`~subprocess.CalledProcessError` exception otherwise.
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +00001044
1045 ::
1046
1047 >>> subprocess.check_output(['df', '-h', '.'])
1048 'Filesystem Size Used Avail Capacity Mounted on\n
1049 /dev/disk0s2 52G 49G 3.0G 94% /\n'
1050
1051 >>> subprocess.check_output(['df', '-h', '/bogus'])
1052 ...
1053 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
1054
1055 (Contributed by Gregory P. Smith.)
1056
Andrew M. Kuchlingf91a6792010-03-24 18:07:43 +00001057 The :mod:`subprocess` module will now retry its internal system calls
1058 on receiving an :const:`EINTR` signal. (Reported by several people; final
1059 patch by Gregory P. Smith in :issue:`1068268`.)
1060
Ezio Melotti021f3342010-04-06 03:26:49 +00001061* New function: :func:`~symtable.is_declared_global` in the :mod:`symtable` module
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001062 returns true for variables that are explicitly declared to be global,
1063 false for ones that are implicitly global.
1064 (Contributed by Jeremy Hylton.)
1065
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +00001066* The ``sys.version_info`` value is now a named tuple, with attributes
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001067 named :attr:`major`, :attr:`minor`, :attr:`micro`,
1068 :attr:`releaselevel`, and :attr:`serial`. (Contributed by Ross
1069 Light; :issue:`4285`.)
1070
1071 :func:`sys.getwindowsversion` also returns a named tuple,
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +00001072 with attributes named :attr:`major`, :attr:`minor`, :attr:`build`,
Ezio Melotti12477752010-02-08 22:22:41 +00001073 :attr:`platform`, :attr:`service_pack`, :attr:`service_pack_major`,
Eric Smithb3c54882010-02-03 14:17:50 +00001074 :attr:`service_pack_minor`, :attr:`suite_mask`, and
1075 :attr:`product_type`. (Contributed by Brian Curtin; :issue:`7766`.)
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +00001076
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00001077* The :mod:`tarfile` module's default error handling has changed, to
1078 no longer suppress fatal errors. The default error level was previously 0,
1079 which meant that errors would only result in a message being written to the
1080 debug log, but because the debug log is not activated by default,
1081 these errors go unnoticed. The default error level is now 1,
1082 which raises an exception if there's an error.
1083 (Changed by Lars Gustäbel; :issue:`7357`.)
1084
Ezio Melotti021f3342010-04-06 03:26:49 +00001085 :mod:`tarfile` now supports filtering the :class:`~tarfile.TarInfo`
1086 objects being added to a tar file. When you call :meth:`~tarfile.TarFile.add`,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001087 instance, you may supply an optional *filter* argument
1088 that's a callable. The *filter* callable will be passed the
Ezio Melotti021f3342010-04-06 03:26:49 +00001089 :class:`~tarfile.TarInfo` for every file being added, and can modify and return it.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001090 If the callable returns ``None``, the file will be excluded from the
1091 resulting archive. This is more powerful than the existing
1092 *exclude* argument, which has therefore been deprecated.
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00001093 (Added by Lars Gustäbel; :issue:`6856`.)
Ezio Melotti021f3342010-04-06 03:26:49 +00001094 The :class:`~tarfile.TarFile` class also now supports the context manager protocol.
Andrew M. Kuchlingfed15762010-03-08 12:00:39 +00001095 (Added by Lars Gustäbel; :issue:`7232`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001096
Ezio Melotti021f3342010-04-06 03:26:49 +00001097* The :meth:`~threading.Event.wait` method of the :class:`threading.Event` class
1098 now returns the internal flag on exit. This means the method will usually
1099 return true because :meth:`~threading.Event.wait` is supposed to block until the
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001100 internal flag becomes true. The return value will only be false if
1101 a timeout was provided and the operation timed out.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001102 (Contributed by Tim Lesher; :issue:`1674032`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001103
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001104* The Unicode database provided by the :mod:`unicodedata` module is
1105 now used internally to determine which characters are numeric,
1106 whitespace, or represent line breaks. The database also
1107 includes information from the :file:`Unihan.txt` data file (patch
1108 by Anders Chrigström and Amaury Forgeot d'Arc; :issue:`1571184`)
1109 and has been updated to version 5.2.0 (updated by
1110 Florent Xicluna; :issue:`8024`).
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001111
Ezio Melotti021f3342010-04-06 03:26:49 +00001112* The :class:`~UserDict.UserDict` class is now a new-style class. (Changed by
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001113 Benjamin Peterson.)
1114
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001115* The ElementTree library, :mod:`xml.etree`, no longer escapes
1116 ampersands and angle brackets when outputting an XML processing
Ezio Melotti021f3342010-04-06 03:26:49 +00001117 instruction (which looks like ``<?xml-stylesheet href="#style1"?>``)
1118 or comment (which looks like ``<!-- comment -->``).
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001119 (Patch by Neil Muller; :issue:`2746`.)
1120
Ezio Melotti021f3342010-04-06 03:26:49 +00001121* The :mod:`zipfile` module's :class:`~zipfile.ZipFile` now supports the context
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00001122 management protocol, so you can write ``with zipfile.ZipFile(...) as f: ...``.
1123 (Contributed by Brian Curtin; :issue:`5511`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001124
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +00001125 :mod:`zipfile` now supports archiving empty directories and
1126 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001127 Reading files out of an archive is now faster, and interleaving
Ezio Melotti021f3342010-04-06 03:26:49 +00001128 :meth:`~zipfile.ZipFile.read` and :meth:`~zipfile.ZipFile.readline` now works correctly.
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001129 (Contributed by Nir Aides; :issue:`7610`.)
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +00001130
Ezio Melotti021f3342010-04-06 03:26:49 +00001131 The :func:`~zipfile.is_zipfile` function now
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00001132 accepts a file object, in addition to the path names accepted in earlier
1133 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
1134
Ezio Melotti021f3342010-04-06 03:26:49 +00001135 The :meth:`~zipfile.ZipFile.writestr` method now has an optional *compress_type* parameter
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001136 that lets you override the default compression method specified in the
Ezio Melotti021f3342010-04-06 03:26:49 +00001137 :class:`~zipfile.ZipFile` constructor. (Contributed by Ronald Oussoren;
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001138 :issue:`6003`.)
1139
1140
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001141New module: sysconfig
1142---------------------------------
1143
1144XXX A new :mod:`sysconfig` module has been extracted from
1145:mod:`distutils` and put in the standard library.
1146
1147The :mod:`sysconfig` module provides access to Python's configuration
1148information like the list of installation paths and the configuration
Tarek Ziadé1e069ee2010-02-23 05:20:22 +00001149variables relevant for the current platform. (contributed by Tarek)
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001150
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001151Updated module: ElementTree 1.3
1152---------------------------------
1153
1154XXX write this.
1155
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001156.. ======================================================================
1157.. whole new modules get described in subsections here
1158
Tarek Ziadé2b210692010-02-02 23:39:40 +00001159
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001160Unit Testing Enhancements
1161---------------------------------
1162
1163The :mod:`unittest` module was enhanced in several ways.
1164The progress messages now shows 'x' for expected failures
1165and 'u' for unexpected successes when run in verbose mode.
1166(Contributed by Benjamin Peterson.)
Ezio Melotti021f3342010-04-06 03:26:49 +00001167Test cases can raise the :exc:`~unittest.SkipTest` exception to skip a test.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001168(:issue:`1034053`.)
1169
1170.. XXX describe test discovery (Contributed by Michael Foord; :issue:`6001`.)
1171
Ezio Melotti021f3342010-04-06 03:26:49 +00001172The error messages for :meth:`~unittest.TestCase.assertEqual`,
1173:meth:`~unittest.TestCase.assertTrue`, and :meth:`~unittest.TestCase.assertFalse`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001174failures now provide more information. If you set the
Ezio Melotti021f3342010-04-06 03:26:49 +00001175:attr:`~unittest.TestCase.longMessage` attribute of your :class:`~unittest.TestCase` classes to
1176True, both the standard error message and any additional message you
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001177provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
1178
Ezio Melotti021f3342010-04-06 03:26:49 +00001179The :meth:`~unittest.TestCase.assertRaises` method now
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001180return a context handler when called without providing a callable
1181object to run. For example, you can write this::
1182
1183 with self.assertRaises(KeyError):
Ezio Melotti021f3342010-04-06 03:26:49 +00001184 {}['foo']
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001185
1186(Implemented by Antoine Pitrou; :issue:`4444`.)
1187
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001188.. rev 78774
1189
1190Module- and class-level setup and teardown fixtures are now supported.
Ezio Melotti021f3342010-04-06 03:26:49 +00001191Modules can contain :func:`~unittest.setUpModule` and :func:`~unittest.tearDownModule`
1192functions. Classes can have :meth:`~unittest.TestCase.setUpClass` and
1193:meth:`~unittest.TestCase.tearDownClass` methods that must be defined as class methods
1194(using ``@classmethod`` or equivalent). These functions and
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001195methods are invoked when the test runner switches to a test case in a
1196different module or class.
1197
Ezio Melotti021f3342010-04-06 03:26:49 +00001198The methods :meth:`~unittest.TestCase.addCleanup` and
1199:meth:`~unittest.TestCase.doCleanups` were added.
1200:meth:`~unittest.TestCase.addCleanup` allows you to add cleanup functions that
1201will be called unconditionally (after :meth:`~unittest.TestCase.setUp` if
1202:meth:`~unittest.TestCase.setUp` fails, otherwise after :meth:`~unittest.TestCase.tearDown`). This allows
Andrew M. Kuchling4a0661b2010-03-25 01:35:51 +00001203for much simpler resource allocation and deallocation during tests
1204(:issue:`5679`).
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001205
1206A number of new methods were added that provide more specialized
1207tests. Many of these methods were written by Google engineers
1208for use in their test suites; Gregory P. Smith, Michael Foord, and
1209GvR worked on merging them into Python's version of :mod:`unittest`.
1210
Ezio Melotti021f3342010-04-06 03:26:49 +00001211* :meth:`~unittest.TestCase.assertIsNone` and :meth:`~unittest.TestCase.assertIsNotNone` take one
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001212 expression and verify that the result is or is not ``None``.
1213
Ezio Melotti021f3342010-04-06 03:26:49 +00001214* :meth:`~unittest.TestCase.assertIs` and :meth:`~unittest.TestCase.assertIsNot`
1215 take two values and check whether the two values evaluate to the same object or not.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001216 (Added by Michael Foord; :issue:`2578`.)
1217
Ezio Melotti021f3342010-04-06 03:26:49 +00001218* :meth:`~unittest.TestCase.assertIsInstance` and
1219 :meth:`~unittest.TestCase.assertNotIsInstance` check whether
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001220 the resulting object is an instance of a particular class, or of
1221 one of a tuple of classes. (Added by Georg Brandl; :issue:`7031`.)
1222
Ezio Melotti021f3342010-04-06 03:26:49 +00001223* :meth:`~unittest.TestCase.assertGreater`, :meth:`~unittest.TestCase.assertGreaterEqual`,
1224 :meth:`~unittest.TestCase.assertLess`, and :meth:`~unittest.TestCase.assertLessEqual` compare
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001225 two quantities.
1226
Ezio Melotti021f3342010-04-06 03:26:49 +00001227* :meth:`~unittest.TestCase.assertMultiLineEqual` compares two strings, and if they're
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001228 not equal, displays a helpful comparison that highlights the
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001229 differences in the two strings. This comparison is now used by
Ezio Melotti021f3342010-04-06 03:26:49 +00001230 default when Unicode strings are compared with :meth:`~unittest.TestCase.assertEqual`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001231
Ezio Melotti021f3342010-04-06 03:26:49 +00001232* :meth:`~unittest.TestCase.assertRegexpMatches` checks whether its first argument is a
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001233 string matching a regular expression provided as its second argument.
1234
Ezio Melotti021f3342010-04-06 03:26:49 +00001235 .. XXX add assertNotRegexpMatches see issue 8038
1236
1237* :meth:`~unittest.TestCase.assertRaisesRegexp` checks whether a particular exception
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001238 is raised, and then also checks that the string representation of
1239 the exception matches the provided regular expression.
1240
Ezio Melotti021f3342010-04-06 03:26:49 +00001241* :meth:`~unittest.TestCase.assertIn` and :meth:`~unittest.TestCase.assertNotIn`
1242 tests whether *first* is or is not in *second*.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001243
Ezio Melotti021f3342010-04-06 03:26:49 +00001244* :meth:`~unittest.TestCase.assertItemsEqual` tests whether two provided sequences
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001245 contain the same elements.
1246
Ezio Melotti021f3342010-04-06 03:26:49 +00001247* :meth:`~unittest.TestCase.assertSetEqual` compares whether two sets are equal, and
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001248 only reports the differences between the sets in case of error.
1249
Ezio Melotti021f3342010-04-06 03:26:49 +00001250* Similarly, :meth:`~unittest.TestCase.assertListEqual` and :meth:`~unittest.TestCase.assertTupleEqual`
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001251 compare the specified types and explain any differences without necessarily
1252 printing their full values; these methods are now used by default
Ezio Melotti021f3342010-04-06 03:26:49 +00001253 when comparing lists and tuples using :meth:`~unittest.TestCase.assertEqual`.
1254 More generally, :meth:`~unittest.TestCase.assertSequenceEqual` compares two sequences
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001255 and can optionally check whether both sequences are of a
1256 particular type.
1257
Ezio Melotti021f3342010-04-06 03:26:49 +00001258* :meth:`~unittest.TestCase.assertDictEqual` compares two dictionaries and reports the
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001259 differences; it's now used by default when you compare two dictionaries
Ezio Melotti021f3342010-04-06 03:26:49 +00001260 using :meth:`~unittest.TestCase.assertEqual`. :meth:`~unittest.TestCase.assertDictContainsSubset` checks whether
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001261 all of the key/value pairs in *first* are found in *second*.
1262
Ezio Melotti021f3342010-04-06 03:26:49 +00001263* :meth:`~unittest.TestCase.assertAlmostEqual` and :meth:`~unittest.TestCase.assertNotAlmostEqual` test
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001264 whether *first* and *second* are approximately equal by computing
1265 their difference, rounding the result to an optionally-specified number
1266 of *places* (the default is 7), and comparing to zero.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001267
Ezio Melotti021f3342010-04-06 03:26:49 +00001268* :meth:`~unittest.TestLoader.loadTestsFromName` properly honors the
1269 :attr:`~unittest.TestLoader.suiteClass` attribute of
1270 the :class:`~unittest.TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001271
Ezio Melotti021f3342010-04-06 03:26:49 +00001272* A new hook lets you extend the :meth:`~unittest.TestCase.assertEqual` method to handle
1273 new data types. The :meth:`~unittest.TestCase.addTypeEqualityFunc` method takes a type
Andrew M. Kuchling9858f632010-03-23 18:39:24 +00001274 object and a function. The function will be used when both of the
1275 objects being compared are of the specified type. This function
1276 should compare the two objects and raise an exception if they don't
1277 match; it's a good idea for the function to provide additional
1278 information about why the two objects are matching, much as the new
1279 sequence comparison methods do.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001280
Andrew M. Kuchling9858f632010-03-23 18:39:24 +00001281:func:`unittest.main` now takes an optional ``exit`` argument. If
Ezio Melotti021f3342010-04-06 03:26:49 +00001282False, :func:`~unittest.main` doesn't call :func:`sys.exit`, allowing it to be
Andrew M. Kuchling9858f632010-03-23 18:39:24 +00001283used from the interactive interpreter. (Contributed by J. Pablo
1284Fernández; :issue:`3379`.)
1285
1286A new command-line switch, :option:`-f` or :option:`--failfast`, makes
1287test execution stop immediately when a test fails instead of
1288continuing to execute further tests. (Suggested by Cliff Dyer and
1289implemented by Michael Foord; :issue:`8074`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001290
Ezio Melotti021f3342010-04-06 03:26:49 +00001291.. XXX document the other new switches
1292
1293:class:`~unittest.TestResult` has new :meth:`~unittest.TestResult.startTestRun` and
1294:meth:`~unittest.TestResult.stopTestRun` methods that are called immediately before
Andrew M. Kuchling9858f632010-03-23 18:39:24 +00001295and after a test run. (Contributed by Robert Collins; :issue:`5728`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001296
1297With all these changes, the :file:`unittest.py` was becoming awkwardly
1298large, so the module was turned into a package and the code split into
1299several files (by Benjamin Peterson). This doesn't affect how the
1300module is imported.
1301
1302
1303.. _importlib-section:
1304
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001305importlib: Importing Modules
1306------------------------------
1307
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001308Python 3.1 includes the :mod:`importlib` package, a re-implementation
1309of the logic underlying Python's :keyword:`import` statement.
1310:mod:`importlib` is useful for implementors of Python interpreters and
Brett Cannonca2dc472009-12-22 02:37:37 +00001311to users who wish to write new importers that can participate in the
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001312import process. Python 2.7 doesn't contain the complete
1313:mod:`importlib` package, but instead has a tiny subset that contains
Ezio Melotti021f3342010-04-06 03:26:49 +00001314a single function, :func:`~importlib.import_module`.
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001315
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001316``import_module(name, package=None)`` imports a module. *name* is
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001317a string containing the module or package's name. It's possible to do
1318relative imports by providing a string that begins with a ``.``
1319character, such as ``..utils.errors``. For relative imports, the
1320*package* argument must be provided and is the name of the package that
1321will be used as the anchor for
Ezio Melotti021f3342010-04-06 03:26:49 +00001322the relative import. :func:`~importlib.import_module` both inserts the imported
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001323module into ``sys.modules`` and returns the module object.
1324
1325Here are some examples::
1326
1327 >>> from importlib import import_module
1328 >>> anydbm = import_module('anydbm') # Standard absolute import
1329 >>> anydbm
1330 <module 'anydbm' from '/p/python/Lib/anydbm.py'>
1331 >>> # Relative import
1332 >>> sysconfig = import_module('..sysconfig', 'distutils.command')
1333 >>> sysconfig
1334 <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'>
1335
1336:mod:`importlib` was implemented by Brett Cannon and introduced in
1337Python 3.1.
1338
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001339
Andrew M. Kuchlinga17cd4a2009-01-31 02:50:09 +00001340ttk: Themed Widgets for Tk
1341--------------------------
1342
1343Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
1344widgets but have a more customizable appearance and can therefore more
1345closely resemble the native platform's widgets. This widget
1346set was originally called Tile, but was renamed to Ttk (for "themed Tk")
1347on being added to Tcl/Tck release 8.5.
1348
1349XXX write a brief discussion and an example here.
1350
1351The :mod:`ttk` module was written by Guilherme Polo and added in
1352:issue:`2983`. An alternate version called ``Tile.py``, written by
1353Martin Franklin and maintained by Kevin Walzer, was proposed for
1354inclusion in :issue:`2618`, but the authors argued that Guilherme
1355Polo's work was more comprehensive.
1356
Georg Brandl0516f812009-11-18 18:52:35 +00001357
1358Deprecations and Removals
1359=========================
1360
1361* :func:`contextlib.nested`, which allows handling more than one context manager
1362 with one :keyword:`with` statement, has been deprecated; :keyword:`with`
1363 supports multiple context managers syntactically now.
1364
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001365.. ======================================================================
1366
1367
1368Build and C API Changes
1369=======================
1370
1371Changes to Python's build process and to the C API include:
1372
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +00001373* The latest release of the GNU Debugger, GDB 7, can be `scripted
1374 using Python
1375 <http://sourceware.org/gdb/current/onlinedocs/gdb/Python.html>`__.
1376 When you begin debugging an executable program P, GDB will look for
1377 a file named ``P-gdb.py`` and automatically read it. Dave Malcolm
1378 contributed a :file:`python-gdb.py` that adds a number of useful
1379 commands when debugging Python itself. For example, there are
1380 ``py-up`` and ``py-down`` that go up or down one Python stack frame,
1381 which usually corresponds to several C stack frames. ``py-print``
1382 prints the value of a Python variable, and ``py-bt`` prints the
1383 Python stack trace. (Added as a result of :issue:`8032`.)
1384
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +00001385* If you use the :file:`.gdbinit` file provided with Python,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001386 the "pyo" macro in the 2.7 version now works correctly when the thread being
1387 debugged doesn't hold the GIL; the macro now acquires it before printing.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001388 (Contributed by Victor Stinner; :issue:`3632`.)
1389
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +00001390* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001391 worker thread submit notifications to the main Python thread. This
1392 is particularly useful for asynchronous IO operations.
1393 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
1394
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001395* New function: :cfunc:`PyCode_NewEmpty` creates an empty code object;
1396 only the filename, function name, and first line number are required.
1397 This is useful to extension modules that are attempting to
1398 construct a more useful traceback stack. Previously such
1399 extensions needed to call :cfunc:`PyCode_New`, which had many
1400 more arguments. (Added by Jeffrey Yasskin.)
1401
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001402* New function: :cfunc:`PyErr_NewExceptionWithDoc` creates a new
1403 exception class, just as the existing :cfunc:`PyErr_NewException` does,
1404 but takes an extra ``char *`` argument containing the docstring for the
1405 new exception class. (Added by the 'lekma' user on the Python bug tracker;
1406 :issue:`7033`.)
1407
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001408* New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object
1409 and returns the line number that the frame is currently executing.
1410 Previously code would need to get the index of the bytecode
1411 instruction currently executing, and then look up the line number
1412 corresponding to that address. (Added by Jeffrey Yasskin.)
1413
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001414* New functions: :cfunc:`PyLong_AsLongAndOverflow` and
1415 :cfunc:`PyLong_AsLongLongAndOverflow` approximates a Python long
1416 integer as a C :ctype:`long` or :ctype:`long long`.
1417 If the number is too large to fit into
1418 the output type, an *overflow* flag is set and returned to the caller.
1419 (Contributed by Case Van Horsen; :issue:`7528` and :issue:`7767`.)
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001420
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001421* New function: stemming from the rewrite of string-to-float conversion,
1422 a new :cfunc:`PyOS_string_to_double` function was added. The old
1423 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions
1424 are now deprecated.
1425
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001426* New macros: the Python header files now define the following macros:
1427 :cmacro:`Py_ISALNUM`,
1428 :cmacro:`Py_ISALPHA`,
1429 :cmacro:`Py_ISDIGIT`,
1430 :cmacro:`Py_ISLOWER`,
1431 :cmacro:`Py_ISSPACE`,
1432 :cmacro:`Py_ISUPPER`,
1433 :cmacro:`Py_ISXDIGIT`,
1434 and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`.
1435 All of these functions are analogous to the C
1436 standard macros for classifying characters, but ignore the current
1437 locale setting, because in
1438 several places Python needs to analyze characters in a
1439 locale-independent way. (Added by Eric Smith;
1440 :issue:`5793`.)
1441
1442 .. XXX these macros don't seem to be described in the c-api docs.
1443
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001444* New format codes: the :cfunc:`PyFormat_FromString`,
1445 :cfunc:`PyFormat_FromStringV`, and :cfunc:`PyErr_Format` now
1446 accepts ``%lld`` and ``%llu`` format codes for displaying values of
1447 C's :ctype:`long long` types.
1448 (Contributed by Mark Dickinson; :issue:`7228`.)
1449
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001450* The complicated interaction between threads and process forking has
1451 been changed. Previously, the child process created by
1452 :func:`os.fork` might fail because the child is created with only a
1453 single thread running, the thread performing the :func:`os.fork`.
1454 If other threads were holding a lock, such as Python's import lock,
1455 when the fork was performed, the lock would still be marked as
1456 "held" in the new process. But in the child process nothing would
1457 ever release the lock, since the other threads weren't replicated,
1458 and the child process would no longer be able to perform imports.
1459
1460 Python 2.7 now acquires the import lock before performing an
1461 :func:`os.fork`, and will also clean up any locks created using the
1462 :mod:`threading` module. C extension modules that have internal
1463 locks, or that call :cfunc:`fork()` themselves, will not benefit
1464 from this clean-up.
1465
1466 (Fixed by Thomas Wouters; :issue:`1590864`.)
1467
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001468* The :cfunc:`Py_Finalize` function now calls the internal
1469 :func:`threading._shutdown` function; this prevents some exceptions from
1470 being raised when an interpreter shuts down.
1471 (Patch by Adam Olsen; :issue:`1722344`.)
1472
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001473* Global symbols defined by the :mod:`ctypes` module are now prefixed
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001474 with ``Py``, or with ``_ctypes``. (Implemented by Thomas
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001475 Heller; :issue:`3102`.)
1476
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001477* New configure option: the :option:`--with-system-expat` switch allows
1478 building the :mod:`pyexpat` module to use the system Expat library.
1479 (Contributed by Arfrever Frehtes Taifersar Arahesis; :issue:`7609`.)
1480
1481* New configure option: Compiling Python with the
1482 :option:`--with-valgrind` option will now disable the pymalloc
1483 allocator, which is difficult for the Valgrind to analyze correctly.
1484 Valgrind will therefore be better at detecting memory leaks and
1485 overruns. (Contributed by James Henstridge; :issue:`2422`.)
1486
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00001487* New configure option: you can now supply no arguments to
1488 :option:`--with-dbmliborder=` in order to build none of the various
1489 DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis;
1490 :issue:`6491`.)
1491
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001492* The :program:`configure` script now checks for floating-point rounding bugs
1493 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
1494 preprocessor definition. No code currently uses this definition,
1495 but it's available if anyone wishes to use it.
1496 (Added by Mark Dickinson; :issue:`2937`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001497
Andrew M. Kuchling4515f0d2010-04-11 20:40:09 +00001498 :program:`configure` also now sets a :envvar:`LDCXXSHARED` Makefile
1499 variable for supporting C++ linking. (Contributed by Arfrever
1500 Frehtes Taifersar Arahesis; :issue:`1222585`.)
1501
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001502* The build process now creates the necessary files for pkg-config
1503 support. (Contributed by Clinton Roy; :issue:`3585`.)
1504
1505* The build process now supports Subversion 1.7. (Contributed by
1506 Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.)
1507
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001508
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001509.. ======================================================================
1510
1511Port-Specific Changes: Windows
1512-----------------------------------
1513
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +00001514* The :mod:`msvcrt` module now contains some constants from
1515 the :file:`crtassem.h` header file:
1516 :data:`CRT_ASSEMBLY_VERSION`,
1517 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
1518 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001519 (Contributed by David Cournapeau; :issue:`4365`.)
1520
1521* The new :cfunc:`_beginthreadex` API is used to start threads, and
1522 the native thread-local storage functions are now used.
1523 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001524
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001525* The :func:`os.listdir` function now correctly fails
1526 for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.)
1527
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001528* The :mod:`mimelib` module will now read the MIME database from
1529 the Windows registry when initializing.
1530 (Patch by Gabriel Genellina; :issue:`4969`.)
1531
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001532.. ======================================================================
1533
1534Port-Specific Changes: Mac OS X
1535-----------------------------------
1536
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001537* The path ``/Library/Python/2.7/site-packages`` is now appended to
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001538 ``sys.path``, in order to share added packages between the system
1539 installation and a user-installed copy of the same version.
1540 (Changed by Ronald Oussoren; :issue:`4865`.)
1541
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001542
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001543Other Changes and Fixes
1544=======================
1545
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001546* Two benchmark scripts, :file:`iobench` and :file:`ccbench`, were
1547 added to the :file:`Tools` directory. :file:`iobench` measures the
Antoine Pitroudde96e62010-02-08 20:25:47 +00001548 speed of built-in file I/O objects (as returned by :func:`open`)
Andrew M. Kuchling46c2db52010-03-21 18:47:12 +00001549 while performing various operations, and :file:`ccbench` is a
1550 concurrency benchmark that tries to measure computing throughput,
1551 thread switching latency, and IO processing bandwidth when
1552 performing several tasks using a varying number of threads.
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001553
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001554* When importing a module from a :file:`.pyc` or :file:`.pyo` file
1555 with an existing :file:`.py` counterpart, the :attr:`co_filename`
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001556 attributes of the resulting code objects are overwritten when the
1557 original filename is obsolete. This can happen if the file has been
1558 renamed, moved, or is accessed through different paths. (Patch by
1559 Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.)
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001560
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001561* The :file:`regrtest.py` script now takes a :option:`--randseed=`
1562 switch that takes an integer that will be used as the random seed
1563 for the :option:`-r` option that executes tests in random order.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001564 The :option:`-r` option also reports the seed that was used
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001565 (Added by Collin Winter.)
1566
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001567* Another :file:`regrtest.py` switch is :option:`-j`, which
1568 takes an integer specifying how many tests run in parallel. This
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001569 allows reducing the total runtime on multi-core machines.
Antoine Pitrou4698d992009-05-31 14:20:14 +00001570 This option is compatible with several other options, including the
1571 :option:`-R` switch which is known to produce long runtimes.
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001572 (Added by Antoine Pitrou, :issue:`6152`.) This can also be used
1573 with a new :option:`-F` switch that runs selected tests in a loop
1574 until they fail. (Added by Antoine Pitrou; :issue:`7312`.)
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001575
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001576.. ======================================================================
1577
1578Porting to Python 2.7
1579=====================
1580
1581This section lists previously described changes and other bugfixes
1582that may require changes to your code:
1583
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001584* When using :class:`Decimal` instances with a string's
1585 :meth:`format` method, the default alignment was previously
1586 left-alignment. This has been changed to right-alignment, which might
1587 change the output of your programs.
1588 (Changed by Mark Dickinson; :issue:`6857`.)
1589
1590 Another :meth:`format`-related change: the default precision used
1591 for floating-point and complex numbers was changed from 6 decimal
1592 places to 12, which matches the precision used by :func:`str`.
1593 (Changed by Eric Smith; :issue:`5920`.)
1594
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00001595* Because of an optimization for the :keyword:`with` statement, the special
1596 methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's
1597 type, and cannot be directly attached to the object's instance. This
Amaury Forgeot d'Arcd81333c2009-06-10 20:30:19 +00001598 affects new-style classes (derived from :class:`object`) and C extension
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00001599 types. (:issue:`6101`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001600
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001601* The :meth:`readline` method of :class:`StringIO` objects now does
1602 nothing when a negative length is requested, as other file-like
1603 objects do. (:issue:`7348`).
1604
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001605In the standard library:
1606
1607* The ElementTree library, :mod:`xml.etree`, no longer escapes
1608 ampersands and angle brackets when outputting an XML processing
1609 instruction (which looks like `<?xml-stylesheet href="#style1"?>`)
1610 or comment (which looks like `<!-- comment -->`).
1611 (Patch by Neil Muller; :issue:`2746`.)
1612
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001613For C extensions:
1614
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00001615* C extensions that use integer format codes with the ``PyArg_Parse*``
1616 family of functions will now raise a :exc:`TypeError` exception
1617 instead of triggering a :exc:`DeprecationWarning` (:issue:`5080`).
1618
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001619* Use the new :cfunc:`PyOS_string_to_double` function instead of the old
1620 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions,
1621 which are now deprecated.
1622
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00001623
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001624.. ======================================================================
1625
1626
1627.. _acks27:
1628
1629Acknowledgements
1630================
1631
1632The author would like to thank the following people for offering
1633suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchling8f254e72009-12-08 02:37:05 +00001634article: Ryan Lovett, Hugh Secker-Walker.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001635