blob: 329518dc6df3c3a8756a7458db0b0af6ff46b771 [file] [log] [blame]
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001****************************
2 What's New in Python 2.7
3****************************
4
5:Author: A.M. Kuchling (amk at amk.ca)
6:Release: |release|
7:Date: |today|
8
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00009.. Fix accents on Kristjan Valur Jonsson, Fuerstenau
Benjamin Peterson1010bf32009-01-30 04:00:29 +000010
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000011.. $Id$
12 Rules for maintenance:
13
14 * Anyone can add text to this document. Do not spend very much time
15 on the wording of your changes, because your text will probably
16 get rewritten to some degree.
17
18 * The maintainer will go through Misc/NEWS periodically and add
19 changes; it's therefore more important to add your changes to
20 Misc/NEWS than to this file.
21
22 * This is not a complete list of every single change; completeness
23 is the purpose of Misc/NEWS. Some changes I consider too small
24 or esoteric to include. If such a change is added to the text,
25 I'll just remove it. (This is another reason you shouldn't spend
26 too much time on writing your addition.)
27
28 * If you want to draw your new text to the attention of the
29 maintainer, add 'XXX' to the beginning of the paragraph or
30 section.
31
32 * It's OK to just add a fragmentary note about a change. For
33 example: "XXX Describe the transmogrify() function added to the
34 socket module." The maintainer will research the change and
35 write the necessary text.
36
37 * You can comment out your additions if you like, but it's not
38 necessary (especially when a final release is some months away).
39
40 * Credit the author of a patch or bugfix. Just the name is
41 sufficient; the e-mail address isn't necessary.
42
43 * It's helpful to add the bug/patch number in a parenthetical comment.
44
45 XXX Describe the transmogrify() function added to the socket
46 module.
47 (Contributed by P.Y. Developer; :issue:`12345`.)
48
49 This saves the maintainer some effort going through the SVN logs
50 when researching a change.
51
Benjamin Peterson9eea4802009-12-31 03:31:15 +000052This article explains the new features in Python 2.7. The final
53release of 2.7 is currently scheduled for June 2010; the detailed
54schedule is described in :pep:`373`.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000055
Benjamin Petersond69fe2a2010-02-03 02:59:43 +000056Python 2.7 is planned to be the last major release in the 2.x series.
57Though more major releases have not been absolutely ruled out, it's
58likely that the 2.7 release will have an extended period of
59maintenance compared to earlier 2.x versions.
60
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000061.. Compare with previous release in 2 - 3 sentences here.
62 add hyperlink when the documentation becomes available online.
63
Benjamin Petersonf6489f92009-11-25 17:46:26 +000064.. _whatsnew27-python31:
65
66Python 3.1 Features
67=======================
Benjamin Petersond23f8222009-04-05 19:13:16 +000068
69Much as Python 2.6 incorporated features from Python 3.0,
Benjamin Petersonf6489f92009-11-25 17:46:26 +000070version 2.7 incorporates some of the new features
71in Python 3.1. The 2.x series continues to provide tools
72for migrating to the 3.x series.
Benjamin Petersond23f8222009-04-05 19:13:16 +000073
Benjamin Petersonf6489f92009-11-25 17:46:26 +000074A partial list of 3.1 features that were backported to 2.7:
75
76* A version of the :mod:`io` library, rewritten in C for performance.
77* The ordered-dictionary type described in :ref:`pep-0372`.
Benjamin Peterson97dd9872009-12-13 01:23:39 +000078* The new format specifier described in :ref:`pep-0378`.
Benjamin Petersonf6489f92009-11-25 17:46:26 +000079* The :class:`memoryview` object.
80* A small subset of the :mod:`importlib` module `described below <#importlib-section>`__.
Benjamin Peterson9eea4802009-12-31 03:31:15 +000081* Float-to-string and string-to-float conversions now round their
82 results more correctly. And :func:`repr` of a floating-point
83 number *x* returns a result that's guaranteed to round back to the
84 same number when converted back to a string.
85* The :cfunc:`PyLong_AsLongAndOverflow` C API function.
Benjamin Petersond23f8222009-04-05 19:13:16 +000086
87One porting change: the :option:`-3` switch now automatically
88enables the :option:`-Qwarn` switch that causes warnings
89about using classic division with integers and long integers.
90
Benjamin Petersonf6489f92009-11-25 17:46:26 +000091Other new Python3-mode warnings include:
92
93* :func:`operator.isCallable` and :func:`operator.sequenceIncludes`,
94 which are not supported in 3.x.
95
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000096.. ========================================================================
97.. Large, PEP-level features and changes should be described here.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000098.. ========================================================================
99
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000100.. _pep-0372:
101
Benjamin Petersond23f8222009-04-05 19:13:16 +0000102PEP 372: Adding an ordered dictionary to collections
103====================================================
104
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000105Regular Python dictionaries iterate over key/value pairs in arbitrary order.
106Over the years, a number of authors have written alternative implementations
107that remember the order that the keys were originally inserted. Based on
108the experiences from those implementations, a new
109:class:`collections.OrderedDict` class has been introduced.
Benjamin Petersond23f8222009-04-05 19:13:16 +0000110
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000111The :class:`OrderedDict` API is substantially the same as regular dictionaries
112but will iterate over keys and values in a guaranteed order depending on
113when a key was first inserted::
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000114
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000115 >>> from collections import OrderedDict
116 >>> d = OrderedDict([('first', 1), ('second', 2),
117 ... ('third', 3)])
118 >>> d.items()
119 [('first', 1), ('second', 2), ('third', 3)]
120
121If a new entry overwrites an existing entry, the original insertion
122position is left unchanged::
123
124 >>> d['second'] = 4
125 >>> d.items()
126 [('first', 1), ('second', 4), ('third', 3)]
127
128Deleting an entry and reinserting it will move it to the end::
129
130 >>> del d['second']
131 >>> d['second'] = 5
132 >>> d.items()
133 [('first', 1), ('third', 3), ('second', 5)]
134
135The :meth:`popitem` method has an optional *last* argument
136that defaults to True. If *last* is True, the most recently
137added key is returned and removed; if it's False, the
138oldest key is selected::
139
140 >>> od = OrderedDict([(x,0) for x in range(20)])
141 >>> od.popitem()
142 (19, 0)
143 >>> od.popitem()
144 (18, 0)
145 >>> od.popitem(False)
146 (0, 0)
147 >>> od.popitem(False)
148 (1, 0)
149
150Comparing two ordered dictionaries checks both the keys and values,
151and requires that the insertion order was the same::
152
153 >>> od1 = OrderedDict([('first', 1), ('second', 2),
154 ... ('third', 3)])
155 >>> od2 = OrderedDict([('third', 3), ('first', 1),
156 ... ('second', 2)])
157 >>> od1==od2
158 False
159 >>> # Move 'third' key to the end
160 >>> del od2['third'] ; od2['third'] = 3
161 >>> od1==od2
162 True
163
164Comparing an :class:`OrderedDict` with a regular dictionary
165ignores the insertion order and just compares the keys and values.
166
167How does the :class:`OrderedDict` work? It maintains a doubly-linked
168list of keys, appending new keys to the list as they're inserted. A
169secondary dictionary maps keys to their corresponding list node, so
170deletion doesn't have to traverse the entire linked list and therefore
171remains O(1).
172
173.. XXX check O(1)-ness with Raymond
174
175The standard library now supports use of ordered dictionaries in several
176modules. The :mod:`configparser` module uses them by default. This lets
177configuration files be read, modified, and then written back in their original
178order. The *_asdict()* method for :func:`collections.namedtuple` now
179returns an ordered dictionary with the values appearing in the same order as
180the underlying tuple indicies. The :mod:`json` module is being built-out with
181an *object_pairs_hook* to allow OrderedDicts to be built by the decoder.
182Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_.
183
184.. seealso::
185
186 :pep:`372` - Adding an ordered dictionary to collections
187 PEP written by Armin Ronacher and Raymond Hettinger;
188 implemented by Raymond Hettinger.
189
190.. _pep-0378:
191
192PEP 378: Format Specifier for Thousands Separator
193====================================================
194
195To make program output more readable, it can be useful to add
196separators to large numbers and render them as
19718,446,744,073,709,551,616 instead of 18446744073709551616.
198
199The fully general solution for doing this is the :mod:`locale` module,
200which can use different separators ("," in North America, "." in
201Europe) and different grouping sizes, but :mod:`locale` is complicated
202to use and unsuitable for multi-threaded applications where different
203threads are producing output for different locales.
204
205Therefore, a simple comma-grouping mechanism has been added to the
206mini-language used by the string :meth:`format` method. When
207formatting a floating-point number, simply include a comma between the
208width and the precision::
209
Eric Smith2b1a1162010-04-06 14:57:57 +0000210 >>> '{:20,.2f}'.format(18446744073709551616.0)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000211 '18,446,744,073,709,551,616.00'
212
Eric Smith21e85c72010-04-06 15:21:59 +0000213When formatting an integer, include the comma after the width:
214
215 >>> '{:20,d}'.format(18446744073709551616)
216 '18,446,744,073,709,551,616'
217
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000218This mechanism is not adaptable at all; commas are always used as the
219separator and the grouping is always into three-digit groups. The
220comma-formatting mechanism isn't as general as the :mod:`locale`
221module, but it's easier to use.
222
223.. XXX "Format String Syntax" in string.rst could use many more examples.
224
225.. seealso::
226
227 :pep:`378` - Format Specifier for Thousands Separator
228 PEP written by Raymond Hettinger; implemented by Eric Smith.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000229
Benjamin Peterson9895f912010-03-21 22:05:32 +0000230PEP 389: The argparse Module for Parsing Command Lines
231======================================================
232
233XXX write this section.
234
235.. seealso::
236
237 :pep:`389` - argparse - New Command Line Parsing Module
238 PEP written and implemented by Steven Bethard.
239
240PEP 391: Dictionary-Based Configuration For Logging
241====================================================
242
243XXX write this section.
244
245.. seealso::
246
247 :pep:`391` - Dictionary-Based Configuration For Logging
248 PEP written and implemented by Vinay Sajip.
249
250PEP 3106: Dictionary Views
251====================================================
252
253XXX write this section.
254
255.. seealso::
256
257 :pep:`3106` - Revamping dict.keys(), .values() and .items()
258 PEP written by Guido van Rossum.
259 Backported to 2.7 by Alexandre Vassalotti; :issue:`1967`.
260
261
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000262Other Language Changes
263======================
264
265Some smaller changes made to the core Python language are:
266
Benjamin Peterson9895f912010-03-21 22:05:32 +0000267* The syntax for set literals has been backported from Python 3.x.
268 Curly brackets are used to surround the contents of the resulting
269 mutable set; set literals are
270 distinguished from dictionaries by not containing colons and values.
271 ``{}`` continues to represent an empty dictionary; use
272 ``set()`` for an empty set.
273
274 >>> {1,2,3,4,5}
275 set([1, 2, 3, 4, 5])
276 >>> set()
277 set([])
278 >>> {}
279 {}
280
281 Backported by Alexandre Vassalotti; :issue:`2335`.
282
283* Dictionary and set comprehensions are another feature backported from
284 3.x, generalizing list/generator comprehensions to use
285 the literal syntax for sets and dictionaries.
286
287 >>> {x:x*x for x in range(6)}
288 {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
289 >>> {'a'*x for x in range(6)}
290 set(['', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa'])
291
292 Backported by Alexandre Vassalotti; :issue:`2333`.
293
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000294* The :keyword:`with` statement can now use multiple context managers
295 in one statement. Context managers are processed from left to right
296 and each one is treated as beginning a new :keyword:`with` statement.
297 This means that::
298
299 with A() as a, B() as b:
300 ... suite of statements ...
301
302 is equivalent to::
303
304 with A() as a:
305 with B() as b:
306 ... suite of statements ...
307
308 The :func:`contextlib.nested` function provides a very similar
309 function, so it's no longer necessary and has been deprecated.
310
311 (Proposed in http://codereview.appspot.com/53094; implemented by
312 Georg Brandl.)
313
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000314* Conversions between floating-point numbers and strings are
315 now correctly rounded on most platforms. These conversions occur
316 in many different places: :func:`str` on
317 floats and complex numbers; the :class:`float` and :class:`complex`
318 constructors;
319 numeric formatting; serialization and
320 deserialization of floats and complex numbers using the
321 :mod:`marshal`, :mod:`pickle`
322 and :mod:`json` modules;
323 parsing of float and imaginary literals in Python code;
324 and :class:`Decimal`-to-float conversion.
325
326 Related to this, the :func:`repr` of a floating-point number *x*
327 now returns a result based on the shortest decimal string that's
328 guaranteed to round back to *x* under correct rounding (with
329 round-half-to-even rounding mode). Previously it gave a string
330 based on rounding x to 17 decimal digits.
331
332 The rounding library responsible for this improvement works on
333 Windows, and on Unix platforms using the gcc, icc, or suncc
334 compilers. There may be a small number of platforms where correct
335 operation of this code cannot be guaranteed, so the code is not
Benjamin Petersona28e7022010-01-09 18:53:06 +0000336 used on such systems. You can find out which code is being used
337 by checking :data:`sys.float_repr_style`, which will be ``short``
338 if the new code is in use and ``legacy`` if it isn't.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000339
Mark Dickinson0bc8f902010-01-07 09:31:48 +0000340 Implemented by Eric Smith and Mark Dickinson, using David Gay's
341 :file:`dtoa.c` library; :issue:`7117`.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000342
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000343* The :meth:`str.format` method now supports automatic numbering of the replacement
Benjamin Peterson3f96a872009-04-11 20:58:12 +0000344 fields. This makes using :meth:`str.format` more closely resemble using
345 ``%s`` formatting::
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000346
347 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
348 '2009:4:Sunday'
349 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
350 '2009:4:Sunday'
351
Benjamin Peterson3f96a872009-04-11 20:58:12 +0000352 The auto-numbering takes the fields from left to right, so the first ``{...}``
353 specifier will use the first argument to :meth:`str.format`, the next
354 specifier will use the next argument, and so on. You can't mix auto-numbering
355 and explicit numbering -- either number all of your specifier fields or none
356 of them -- but you can mix auto-numbering and named fields, as in the second
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000357 example above. (Contributed by Eric Smith; :issue:`5237`.)
358
359 Complex numbers now correctly support usage with :func:`format`.
360 Specifying a precision or comma-separation applies to both the real
361 and imaginary parts of the number, but a specified field width and
362 alignment is applied to the whole of the resulting ``1.5+3j``
363 output. (Contributed by Eric Smith; :issue:`1588`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000364
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000365 The 'F' format code now always formats its output using uppercase characters,
366 so it will now produce 'INF' and 'NAN'.
367 (Contributed by Eric Smith; :issue:`3382`.)
368
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000369* The :func:`int` and :func:`long` types gained a ``bit_length``
370 method that returns the number of bits necessary to represent
371 its argument in binary::
372
373 >>> n = 37
374 >>> bin(37)
375 '0b100101'
376 >>> n.bit_length()
377 6
378 >>> n = 2**123-1
379 >>> n.bit_length()
380 123
381 >>> (n+1).bit_length()
382 124
383
384 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
385
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000386* Conversions from long integers and regular integers to floating
387 point now round differently, returning the floating-point number
388 closest to the number. This doesn't matter for small integers that
389 can be converted exactly, but for large numbers that will
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000390 unavoidably lose precision, Python 2.7 now approximates more
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000391 closely. For example, Python 2.6 computed the following::
392
393 >>> n = 295147905179352891391
394 >>> float(n)
395 2.9514790517935283e+20
396 >>> n - long(float(n))
397 65535L
398
399 Python 2.7's floating-point result is larger, but much closer to the
400 true value::
401
402 >>> n = 295147905179352891391
403 >>> float(n)
404 2.9514790517935289e+20
405 >>> n-long(float(n)
406 ... )
407 -1L
408
409 (Implemented by Mark Dickinson; :issue:`3166`.)
410
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000411 Integer division is also more accurate in its rounding behaviours. (Also
412 implemented by Mark Dickinson; :issue:`1811`.)
413
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000414* The :class:`bytearray` type's :meth:`translate` method now accepts
415 ``None`` as its first argument. (Fixed by Georg Brandl;
Benjamin Petersond23f8222009-04-05 19:13:16 +0000416 :issue:`4759`.)
Mark Dickinsond72c7b62009-03-20 16:00:49 +0000417
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000418* When using ``@classmethod`` and ``@staticmethod`` to wrap
419 methods as class or static methods, the wrapper object now
420 exposes the wrapped function as their :attr:`__func__` attribute.
421 (Contributed by Amaury Forgeot d'Arc, after a suggestion by
422 George Sakkis; :issue:`5982`.)
423
424* A new encoding named "cp720", used primarily for Arabic text, is now
425 supported. (Contributed by Alexander Belchenko and Amaury Forgeot
426 d'Arc; :issue:`1616979`.)
427
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000428* The :class:`file` object will now set the :attr:`filename` attribute
429 on the :exc:`IOError` exception when trying to open a directory
Benjamin Peterson9895f912010-03-21 22:05:32 +0000430 on POSIX platforms (noted by Jan Kaliszewski; :issue:`4764`), and
431 now explicitly checks for and forbids writing to read-only file objects
432 instead of trusting the C library to catch and report the error
433 (fixed by Stefan Krah; :issue:`5677`).
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000434
Benjamin Petersona28e7022010-01-09 18:53:06 +0000435* The Python tokenizer now translates line endings itself, so the
436 :func:`compile` built-in function can now accept code using any
437 line-ending convention. Additionally, it no longer requires that the
438 code end in a newline.
439
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000440* Extra parentheses in function definitions are illegal in Python 3.x,
441 meaning that you get a syntax error from ``def f((x)): pass``. In
442 Python3-warning mode, Python 2.7 will now warn about this odd usage.
443 (Noted by James Lingard; :issue:`7362`.)
444
Benjamin Peterson9895f912010-03-21 22:05:32 +0000445* When a module object is garbage-collected, the module's dictionary is
446 now only cleared if no one else is holding a reference to the
447 dictionary (:issue:`7140`).
448
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000449.. ======================================================================
450
451
452Optimizations
453-------------
454
Benjamin Petersond23f8222009-04-05 19:13:16 +0000455Several performance enhancements have been added:
456
457.. * A new :program:`configure` option, :option:`--with-computed-gotos`,
458 compiles the main bytecode interpreter loop using a new dispatch
459 mechanism that gives speedups of up to 20%, depending on the system
460 and benchmark. The new mechanism is only supported on certain
461 compilers, such as gcc, SunPro, and icc.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000462
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000463* A new opcode was added to perform the initial setup for
464 :keyword:`with` statements, looking up the :meth:`__enter__` and
465 :meth:`__exit__` methods. (Contributed by Benjamin Peterson.)
466
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000467* The garbage collector now performs better for one common usage
468 pattern: when many objects are being allocated without deallocating
469 any of them. This would previously take quadratic
470 time for garbage collection, but now the number of full garbage collections
471 is reduced as the number of objects on the heap grows.
472 The new logic is to only perform a full garbage collection pass when
473 the middle generation has been collected 10 times and when the
474 number of survivor objects from the middle generation exceeds 10% of
475 the number of objects in the oldest generation. (Suggested by Martin
476 von Loewis and implemented by Antoine Pitrou; :issue:`4074`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000477
Benjamin Petersond23f8222009-04-05 19:13:16 +0000478* The garbage collector tries to avoid tracking simple containers
479 which can't be part of a cycle. In Python 2.7, this is now true for
480 tuples and dicts containing atomic types (such as ints, strings,
481 etc.). Transitively, a dict containing tuples of atomic types won't
482 be tracked either. This helps reduce the cost of each
483 garbage collection by decreasing the number of objects to be
484 considered and traversed by the collector.
Antoine Pitrou9d81def2009-03-28 19:20:09 +0000485 (Contributed by Antoine Pitrou; :issue:`4688`.)
486
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000487* Long integers are now stored internally either in base 2**15 or in base
Benjamin Petersond23f8222009-04-05 19:13:16 +0000488 2**30, the base being determined at build time. Previously, they
489 were always stored in base 2**15. Using base 2**30 gives
490 significant performance improvements on 64-bit machines, but
491 benchmark results on 32-bit machines have been mixed. Therefore,
492 the default is to use base 2**30 on 64-bit machines and base 2**15
493 on 32-bit machines; on Unix, there's a new configure option
494 :option:`--enable-big-digits` that can be used to override this default.
495
496 Apart from the performance improvements this change should be
497 invisible to end users, with one exception: for testing and
498 debugging purposes there's a new structseq ``sys.long_info`` that
499 provides information about the internal format, giving the number of
500 bits per digit and the size in bytes of the C type used to store
501 each digit::
502
503 >>> import sys
504 >>> sys.long_info
505 sys.long_info(bits_per_digit=30, sizeof_digit=4)
506
507 (Contributed by Mark Dickinson; :issue:`4258`.)
508
509 Another set of changes made long objects a few bytes smaller: 2 bytes
510 smaller on 32-bit systems and 6 bytes on 64-bit.
511 (Contributed by Mark Dickinson; :issue:`5260`.)
512
513* The division algorithm for long integers has been made faster
514 by tightening the inner loop, doing shifts instead of multiplications,
515 and fixing an unnecessary extra iteration.
516 Various benchmarks show speedups of between 50% and 150% for long
517 integer divisions and modulo operations.
518 (Contributed by Mark Dickinson; :issue:`5512`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000519 Bitwise operations are also significantly faster (initial patch by
520 Gregory Smith; :issue:`1087418`).
Benjamin Petersond23f8222009-04-05 19:13:16 +0000521
522* The implementation of ``%`` checks for the left-side operand being
523 a Python string and special-cases it; this results in a 1-3%
524 performance increase for applications that frequently use ``%``
525 with strings, such as templating libraries.
526 (Implemented by Collin Winter; :issue:`5176`.)
527
528* List comprehensions with an ``if`` condition are compiled into
529 faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
530 by Jeffrey Yasskin; :issue:`4715`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000531
Benjamin Petersona28e7022010-01-09 18:53:06 +0000532* Converting an integer or long integer to a decimal string was made
533 faster by special-casing base 10 instead of using a generalized
534 conversion function that supports arbitrary bases.
535 (Patch by Gawain Bolton; :issue:`6713`.)
536
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000537* The :meth:`split`, :meth:`replace`, :meth:`rindex`,
538 :meth:`rpartition`, and :meth:`rsplit` methods of string-like types
539 (strings, Unicode strings, and :class:`bytearray` objects) now use a
540 fast reverse-search algorithm instead of a character-by-character
541 scan. This is sometimes faster by a factor of 10. (Added by
542 Florent Xicluna; :issue:`7462` and :issue:`7622`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000543
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000544* The :mod:`pickle` and :mod:`cPickle` modules now automatically
545 intern the strings used for attribute names, reducing memory usage
546 of the objects resulting from unpickling. (Contributed by Jake
547 McGuire; :issue:`5084`.)
548
549* The :mod:`cPickle` module now special-cases dictionaries,
550 nearly halving the time required to pickle them.
551 (Contributed by Collin Winter; :issue:`5670`.)
552
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000553.. ======================================================================
554
Georg Brandl4d131ee2009-11-18 18:53:14 +0000555New and Improved Modules
556========================
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000557
558As in every release, Python's standard library received a number of
559enhancements and bug fixes. Here's a partial list of the most notable
560changes, sorted alphabetically by module name. Consult the
561:file:`Misc/NEWS` file in the source tree for a more complete list of
562changes, or look through the Subversion logs for all the details.
563
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000564* The :mod:`bdb` module's base debugging class :class:`Bdb`
565 gained a feature for skipping modules. The constructor
566 now takes an iterable containing glob-style patterns such as
567 ``django.*``; the debugger will not step into stack frames
568 from a module that matches one of these patterns.
569 (Contributed by Maru Newby after a suggestion by
570 Senthil Kumaran; :issue:`5142`.)
571
Benjamin Peterson9895f912010-03-21 22:05:32 +0000572* The :mod:`binascii` module now supports the buffer API, so it can be
573 used with :class:`memoryview` instances and other similar buffer objects.
574 (Backported from 3.x by Florent Xicluna; :issue:`7703`.)
575
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000576* The :mod:`bz2` module's :class:`BZ2File` now supports the context
577 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
578 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
579
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000580* New class: the :class:`Counter` class in the :mod:`collections` module is
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000581 useful for tallying data. :class:`Counter` instances behave mostly
582 like dictionaries but return zero for missing keys instead of
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000583 raising a :exc:`KeyError`:
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000584
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000585 .. doctest::
586 :options: +NORMALIZE_WHITESPACE
587
588 >>> from collections import Counter
589 >>> c = Counter()
590 >>> for letter in 'here is a sample of english text':
591 ... c[letter] += 1
592 ...
593 >>> c
594 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
595 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
596 'p': 1, 'r': 1, 'x': 1})
597 >>> c['e']
598 5
599 >>> c['z']
600 0
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000601
602 There are two additional :class:`Counter` methods: :meth:`most_common`
603 returns the N most common elements and their counts, and :meth:`elements`
604 returns an iterator over the contained element, repeating each element
605 as many times as its count::
606
607 >>> c.most_common(5)
608 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
609 >>> c.elements() ->
610 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
611 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
612 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000613 's', 's', 'r', 't', 't', 'x'
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000614
615 Contributed by Raymond Hettinger; :issue:`1696199`.
616
Georg Brandlef871f62010-03-12 10:06:40 +0000617 The new `~collections.OrderedDict` class is described in the earlier section
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000618 :ref:`pep-0372`.
619
Benjamin Petersond23f8222009-04-05 19:13:16 +0000620 The :class:`namedtuple` class now has an optional *rename* parameter.
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000621 If *rename* is true, field names that are invalid because they've
Benjamin Petersond23f8222009-04-05 19:13:16 +0000622 been repeated or that aren't legal Python identifiers will be
623 renamed to legal names that are derived from the field's
624 position within the list of fields:
625
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000626 >>> from collections import namedtuple
627 >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000628 >>> T._fields
629 ('field1', '_1', '_2', 'field2')
630
631 (Added by Raymond Hettinger; :issue:`1818`.)
632
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000633 The :class:`deque` data type now exposes its maximum length as the
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000634 read-only :attr:`maxlen` attribute, and has a
635 :meth:`reverse` method that reverses the elements of the deque in-place.
636 (Added by Raymond Hettinger.)
637
638* The :mod:`copy` module's :func:`deepcopy` function will now
639 correctly copy bound instance methods. (Implemented by
640 Robert Collins; :issue:`1515`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000641
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000642* The :mod:`ctypes` module now always converts ``None`` to a C NULL
643 pointer for arguments declared as pointers. (Changed by Thomas
Benjamin Peterson9895f912010-03-21 22:05:32 +0000644 Heller; :issue:`4606`.) The underlying `libffi library
645 <http://sourceware.org/libffi/>`__ has been updated to version
646 3.0.9, containing various fixes for different platforms. (Updated
647 by Matthias Klose; :issue:`8142`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000648
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000649* New method: the :mod:`datetime` module's :class:`timedelta` class
650 gained a :meth:`total_seconds` method that returns the number of seconds
651 in the duration. (Contributed by Brian Quinlan; :issue:`5788`.)
652
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000653* New method: the :class:`Decimal` class gained a
654 :meth:`from_float` class method that performs an exact conversion
655 of a floating-point number to a :class:`Decimal`.
656 Note that this is an **exact** conversion that strives for the
657 closest decimal approximation to the floating-point representation's value;
658 the resulting decimal value will therefore still include the inaccuracy,
659 if any.
660 For example, ``Decimal.from_float(0.1)`` returns
661 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
662 (Implemented by Raymond Hettinger; :issue:`4796`.)
663
664 The constructor for :class:`Decimal` now accepts non-European
665 Unicode characters, such as Arabic-Indic digits. (Contributed by
666 Mark Dickinson; :issue:`6595`.)
667
668 When using :class:`Decimal` instances with a string's
669 :meth:`format` method, the default alignment was previously
670 left-alignment. This has been changed to right-alignment, which seems
671 more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.)
672
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000673* The :class:`Fraction` class now accepts two rational numbers
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000674 as arguments to its constructor.
675 (Implemented by Mark Dickinson; :issue:`5812`.)
676
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000677* The :mod:`ftplib` module gained the ability to establish secure FTP
678 connections using TLS encapsulation of authentication as well as
679 subsequent control and data transfers. This is provided by the new
680 :class:`ftplib.FTP_TLS` class.
681 (Contributed by Giampaolo Rodola', :issue:`2054`.) The :meth:`storbinary`
682 method for binary uploads can now restart uploads thanks to an added
683 *rest* parameter (patch by Pablo Mouzo; :issue:`6845`.)
684
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000685* New function: the :mod:`gc` module's :func:`is_tracked` returns
686 true if a given instance is tracked by the garbage collector, false
Benjamin Petersond23f8222009-04-05 19:13:16 +0000687 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
688
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000689* The :mod:`gzip` module's :class:`GzipFile` now supports the context
Benjamin Peterson9895f912010-03-21 22:05:32 +0000690 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``
691 (contributed by Hagen Fuerstenau; :issue:`3860`), and it now implements
692 the :class:`io.BufferedIOBase` ABC, so you can wrap it with
693 :class:`io.BufferedReader` for faster processing
694 (contributed by Nir Aides; :issue:`7471`).
695 It's also now possible to override the modification time
Benjamin Petersond23f8222009-04-05 19:13:16 +0000696 recorded in a gzipped file by providing an optional timestamp to
697 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000698
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000699 Files in gzip format can be padded with trailing zero bytes; the
700 :mod:`gzip` module will now consume these trailing bytes. (Fixed by
701 Tadek Pietraszek and Brian Curtin; :issue:`2846`.)
702
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000703* The default :class:`HTTPResponse` class used by the :mod:`httplib` module now
704 supports buffering, resulting in much faster reading of HTTP responses.
705 (Contributed by Kristjan Valur Jonsson; :issue:`4879`.)
706
Benjamin Peterson9895f912010-03-21 22:05:32 +0000707 The :class:`HTTPConnection` and :class:`HTTPSConnection` classes
708 now support a *source_address* parameter, a ``(host, port)`` 2-tuple
709 giving the source address that will be used for the connection.
710 (Contributed by Eldon Ziegler; :issue:`3972`.)
711
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000712* The :mod:`imaplib` module now supports IPv6 addresses.
713 (Contributed by Derek Morr; :issue:`1655`.)
714
715* The :mod:`io` library has been upgraded to the version shipped with
716 Python 3.1. For 3.1, the I/O library was entirely rewritten in C
717 and is 2 to 20 times faster depending on the task at hand. The
718 original Python version was renamed to the :mod:`_pyio` module.
719
720 One minor resulting change: the :class:`io.TextIOBase` class now
721 has an :attr:`errors` attribute giving the error setting
722 used for encoding and decoding errors (one of ``'strict'``, ``'replace'``,
723 ``'ignore'``).
724
725 The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000726 an invalid file descriptor. (Implemented by Benjamin Peterson;
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000727 :issue:`4991`.) The :meth:`truncate` method now preserves the
728 file position; previously it would change the file position to the
729 end of the new file. (Fixed by Pascal Chambon; :issue:`6939`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000730
Benjamin Peterson97dd9872009-12-13 01:23:39 +0000731* New function: ``itertools.compress(data, selectors)`` takes two
Benjamin Petersond23f8222009-04-05 19:13:16 +0000732 iterators. Elements of *data* are returned if the corresponding
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000733 value in *selectors* is true::
Benjamin Petersond23f8222009-04-05 19:13:16 +0000734
735 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
736 A, C, E, F
737
Benjamin Peterson97dd9872009-12-13 01:23:39 +0000738 New function: ``itertools.combinations_with_replacement(iter, r)``
Benjamin Petersond23f8222009-04-05 19:13:16 +0000739 returns all the possible *r*-length combinations of elements from the
740 iterable *iter*. Unlike :func:`combinations`, individual elements
741 can be repeated in the generated combinations::
742
743 itertools.combinations_with_replacement('abc', 2) =>
744 ('a', 'a'), ('a', 'b'), ('a', 'c'),
745 ('b', 'b'), ('b', 'c'), ('c', 'c')
746
747 Note that elements are treated as unique depending on their position
748 in the input, not their actual values.
749
750 The :class:`itertools.count` function now has a *step* argument that
751 allows incrementing by values other than 1. :func:`count` also
752 now allows keyword arguments, and using non-integer values such as
753 floats or :class:`Decimal` instances. (Implemented by Raymond
754 Hettinger; :issue:`5032`.)
755
756 :func:`itertools.combinations` and :func:`itertools.product` were
757 previously raising :exc:`ValueError` for values of *r* larger than
758 the input iterable. This was deemed a specification error, so they
759 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
760
761* The :mod:`json` module was upgraded to version 2.0.9 of the
762 simplejson package, which includes a C extension that makes
763 encoding and decoding faster.
764 (Contributed by Bob Ippolito; :issue:`4136`.)
765
766 To support the new :class:`OrderedDict` type, :func:`json.load`
767 now has an optional *object_pairs_hook* parameter that will be called
768 with any object literal that decodes to a list of pairs.
769 (Contributed by Raymond Hettinger; :issue:`5381`.)
770
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000771* New functions: the :mod:`math` module gained
772 :func:`erf` and :func:`erfc` for the error function and the complementary error function,
773 :func:`expm1` which computes ``e**x - 1`` with more precision than
774 using :func:`exp` and subtracting 1,
775 :func:`gamma` for the Gamma function, and
776 :func:`lgamma` for the natural log of the Gamma function.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000777 (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.)
778
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000779* The :mod:`multiprocessing` module's :class:`Manager*` classes
780 can now be passed a callable that will be called whenever
781 a subprocess is started, along with a set of arguments that will be
782 passed to the callable.
783 (Contributed by lekma; :issue:`5585`.)
784
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000785 The :class:`Pool` class, which controls a pool of worker processes,
786 now has an optional *maxtasksperchild* parameter. Worker processes
787 will perform the specified number of tasks and then exit, causing the
788 :class:`Pool` to start a new worker. This is useful if tasks may leak
789 memory or other resources, or if some tasks will cause the worker to
790 become very large.
791 (Contributed by Charles Cazabon; :issue:`6963`.)
792
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000793* The :mod:`nntplib` module now supports IPv6 addresses.
794 (Contributed by Derek Morr; :issue:`1664`.)
795
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000796* New functions: the :mod:`os` module wraps the following POSIX system
797 calls: :func:`getresgid` and :func:`getresuid`, which return the
798 real, effective, and saved GIDs and UIDs;
799 :func:`setresgid` and :func:`setresuid`, which set
800 real, effective, and saved GIDs and UIDs to new values;
801 :func:`initgroups`. (GID/UID functions
802 contributed by Travis H.; :issue:`6508`. Support for initgroups added
803 by Jean-Paul Calderone; :issue:`7333`.)
804
Benjamin Peterson9895f912010-03-21 22:05:32 +0000805 The :func:`os.fork` function now re-initializes the import lock in
806 the child process; this fixes problems on Solaris when :func:`fork`
807 is called from a thread. (Fixed by Zsolt Cserna; :issue:`7242`.)
808
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000809 The :func:`normpath` function now preserves Unicode; if its input path
810 is a Unicode string, the return value is also a Unicode string.
811 (Fixed by Matt Giuca; :issue:`5827`.)
812
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000813* The :mod:`pydoc` module now has help for the various symbols that Python
814 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
815 (Contributed by David Laban; :issue:`4739`.)
816
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000817* The :mod:`re` module's :func:`split`, :func:`sub`, and :func:`subn`
818 now accept an optional *flags* argument, for consistency with the
819 other functions in the module. (Added by Gregory P. Smith.)
820
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000821* The :mod:`shutil` module's :func:`copyfile` and :func:`copytree`
822 functions now raises a :exc:`SpecialFileError` exception when
823 asked to copy a named pipe. Previously the code would treat
824 named pipes like a regular file by opening them for reading, and
825 this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.)
826
Benjamin Peterson9895f912010-03-21 22:05:32 +0000827 New function: :func:`make_archive` takes a filename, archive type
828 (zip or tar-format), and a directory path, and creates an archive
829 containing the directory's contents. (Added by Tarek Ziadé.)
830
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000831* New functions: in the :mod:`site` module, three new functions
832 return various site- and user-specific paths.
833 :func:`getsitepackages` returns a list containing all
834 global site-packages directories, and
835 :func:`getusersitepackages` returns the path of the user's
836 site-packages directory.
Ezio Melotti6e40e272010-01-04 09:29:10 +0000837 :func:`getuserbase` returns the value of the :envvar:`USER_BASE`
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000838 environment variable, giving the path to a directory that can be used
839 to store data.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000840 (Contributed by Tarek Ziadé; :issue:`6693`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000841
Benjamin Peterson9895f912010-03-21 22:05:32 +0000842 The :mod:`site` module now reports exceptions occurring
843 when the :mod:`sitecustomize` module is imported, and will no longer
Florent Xicluna41fe6152010-04-02 18:52:12 +0000844 catch and swallow the :exc:`KeyboardInterrupt` exception. (Fixed by
Benjamin Peterson9895f912010-03-21 22:05:32 +0000845 Victor Stinner; :issue:`3137`.)
846
Benjamin Petersona28e7022010-01-09 18:53:06 +0000847* The :mod:`socket` module's :class:`SSL` objects now support the
Benjamin Peterson9895f912010-03-21 22:05:32 +0000848 buffer API, which fixed a test suite failure. (Fixed by Antoine
849 Pitrou; :issue:`7133`.)
850
851 The :func:`create_connection` function
852 gained a *source_address* parameter, a ``(host, port)`` 2-tuple
853 giving the source address that will be used for the connection.
854 (Contributed by Eldon Ziegler; :issue:`3972`.)
855
Ezio Melotti9de5a412010-04-05 08:21:29 +0000856 The :meth:`recv_into` and :meth:`recvfrom_into` methods will now write
Benjamin Peterson9895f912010-03-21 22:05:32 +0000857 into objects that support the buffer API, most usefully
858 the :class:`bytearray` and :class:`memoryview` objects. (Implemented by
859 Antoine Pitrou; :issue:`8104`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000860
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000861* The :mod:`SocketServer` module's :class:`TCPServer` class now
862 has a :attr:`disable_nagle_algorithm` class attribute.
863 The default value is False; if overridden to be True,
864 new request connections will have the TCP_NODELAY option set to
865 prevent buffering many small sends into a single TCP packet.
866 (Contributed by Kristjan Valur Jonsson; :issue:`6192`.)
867
Benjamin Peterson9895f912010-03-21 22:05:32 +0000868* Updated module: the :mod:`sqlite` module has been updated to
869 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
870 the ability to load SQLite extensions from shared libraries.
871 Call the ``enable_load_extension(True)`` method to enable extensions,
872 and then call :meth:`load_extension` to load a particular shared library.
873 (Updated by Gerhard Häring.)
874
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000875* The :mod:`struct` module will no longer silently ignore overflow
876 errors when a value is too large for a particular integer format
877 code (one of ``bBhHiIlLqQ``); it now always raises a
878 :exc:`struct.error` exception. (Changed by Mark Dickinson;
879 :issue:`1523`.)
880
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000881* New function: the :mod:`subprocess` module's
882 :func:`check_output` runs a command with a specified set of arguments
Benjamin Petersond23f8222009-04-05 19:13:16 +0000883 and returns the command's output as a string when the command runs without
Georg Brandl1f01deb2009-01-03 22:47:39 +0000884 error, or raises a :exc:`CalledProcessError` exception otherwise.
885
886 ::
887
888 >>> subprocess.check_output(['df', '-h', '.'])
889 'Filesystem Size Used Avail Capacity Mounted on\n
890 /dev/disk0s2 52G 49G 3.0G 94% /\n'
891
892 >>> subprocess.check_output(['df', '-h', '/bogus'])
893 ...
894 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
895
896 (Contributed by Gregory P. Smith.)
897
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000898* New function: :func:`is_declared_global` in the :mod:`symtable` module
899 returns true for variables that are explicitly declared to be global,
900 false for ones that are implicitly global.
901 (Contributed by Jeremy Hylton.)
902
Benjamin Petersond23f8222009-04-05 19:13:16 +0000903* The ``sys.version_info`` value is now a named tuple, with attributes
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000904 named :attr:`major`, :attr:`minor`, :attr:`micro`,
905 :attr:`releaselevel`, and :attr:`serial`. (Contributed by Ross
906 Light; :issue:`4285`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000907
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000908 :func:`sys.getwindowsversion` also returns a named tuple,
Ezio Melotti0d85e412010-03-13 00:39:49 +0000909 with attributes named :attr:`major`, :attr:`minor`, :attr:`build`,
910 :attr:`platform`, :attr:`service_pack`, :attr:`service_pack_major`,
Eric Smithb0869402010-02-03 14:25:10 +0000911 :attr:`service_pack_minor`, :attr:`suite_mask`, and
912 :attr:`product_type`. (Contributed by Brian Curtin; :issue:`7766`.)
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000913
914* The :mod:`tarfile` module's default error handling has changed, to
915 no longer suppress fatal errors. The default error level was previously 0,
916 which meant that errors would only result in a message being written to the
917 debug log, but because the debug log is not activated by default,
918 these errors go unnoticed. The default error level is now 1,
919 which raises an exception if there's an error.
920 (Changed by Lars Gustäbel; :issue:`7357`.)
921
922 :mod:`tarfile` now supports filtering the :class:`TarInfo`
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000923 objects being added to a tar file. When you call :meth:`TarFile.add`,
924 instance, you may supply an optional *filter* argument
925 that's a callable. The *filter* callable will be passed the
926 :class:`TarInfo` for every file being added, and can modify and return it.
927 If the callable returns ``None``, the file will be excluded from the
928 resulting archive. This is more powerful than the existing
929 *exclude* argument, which has therefore been deprecated.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000930 (Added by Lars Gustäbel; :issue:`6856`.)
Benjamin Peterson9895f912010-03-21 22:05:32 +0000931 The :class:`TarFile` class also now supports the context manager protocol.
932 (Added by Lars Gustäbel; :issue:`7232`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000933
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000934* The :mod:`threading` module's :meth:`Event.wait` method now returns
935 the internal flag on exit. This means the method will usually
936 return true because :meth:`wait` is supposed to block until the
937 internal flag becomes true. The return value will only be false if
938 a timeout was provided and the operation timed out.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000939 (Contributed by Tim Lesher; :issue:`1674032`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000940
Ezio Melotti4c5475d2010-03-22 23:16:42 +0000941* The Unicode database has been updated to the version 5.2.0.
942 (Updated by Florent Xicluna; :issue:`8024`.)
943
944* The Unicode database provided by the :mod:`unicodedata` is used
945 internally to determine which characters are numeric, whitespace,
946 or represent line breaks. The database also now includes information
947 from the :file:`Unihan.txt` data file. (Patch by Anders Chrigström
Benjamin Peterson9895f912010-03-21 22:05:32 +0000948 and Amaury Forgeot d'Arc; :issue:`1571184`.)
949
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000950* The :class:`UserDict` class is now a new-style class. (Changed by
951 Benjamin Peterson.)
952
Benjamin Peterson9895f912010-03-21 22:05:32 +0000953* The ElementTree library, :mod:`xml.etree`, no longer escapes
954 ampersands and angle brackets when outputting an XML processing
955 instruction (which looks like `<?xml-stylesheet href="#style1"?>`)
956 or comment (which looks like `<!-- comment -->`).
957 (Patch by Neil Muller; :issue:`2746`.)
958
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000959* The :mod:`zipfile` module's :class:`ZipFile` now supports the context
960 management protocol, so you can write ``with zipfile.ZipFile(...) as f: ...``.
961 (Contributed by Brian Curtin; :issue:`5511`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000962
Benjamin Petersond23f8222009-04-05 19:13:16 +0000963 :mod:`zipfile` now supports archiving empty directories and
964 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000965 Reading files out of an archive is now faster, and interleaving
966 :meth:`read` and :meth:`readline` now works correctly.
967 (Contributed by Nir Aides; :issue:`7610`.)
968
969 The :func:`is_zipfile` function in the module now
970 accepts a file object, in addition to the path names accepted in earlier
971 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000972
Benjamin Peterson9895f912010-03-21 22:05:32 +0000973 The :meth:`writestr` method now has an optional *compress_type* parameter
974 that lets you override the default compression method specified in the
975 :class:`ZipFile` constructor. (Contributed by Ronald Oussoren;
976 :issue:`6003`.)
977
Tarek Ziadé396fad72010-02-23 05:30:31 +0000978* XXX the :mod:`shutil` module has now a :func:`make_archive` function
Benjamin Peterson9895f912010-03-21 22:05:32 +0000979 (see the module doc, contributed by Tarek)
980
981
982New module: sysconfig
983---------------------------------
984
985XXX A new :mod:`sysconfig` module has been extracted from
986:mod:`distutils` and put in the standard library.
987
988The :mod:`sysconfig` module provides access to Python's configuration
989information like the list of installation paths and the configuration
990variables relevant for the current platform. (contributed by Tarek)
991
992Updated module: ElementTree 1.3
993---------------------------------
994
995XXX write this.
Tarek Ziadé396fad72010-02-23 05:30:31 +0000996
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000997.. ======================================================================
998.. whole new modules get described in subsections here
999
Tarek Ziadéba0eacf2010-02-02 23:43:21 +00001000
Benjamin Peterson9895f912010-03-21 22:05:32 +00001001Distutils Enhancements
1002---------------------------------
1003
1004Distutils is being more actively developed, thanks to Tarek Ziadé
1005who has taken over maintenance of the package, so there are a number
1006of fixes and improvements.
1007
1008A new :file:`setup.py` subcommand, ``check``, will check that the
1009arguments being passed to the :func:`setup` function are complete
1010and correct (:issue:`5732`).
1011
1012Byte-compilation by the ``install_lib`` subcommand is now only done
1013if the ``sys.dont_write_bytecode`` setting allows it (:issue:`7071`).
1014
1015:func:`distutils.sdist.add_defaults` now uses
1016*package_dir* and *data_files* to create the MANIFEST file.
1017:mod:`distutils.sysconfig` now reads the :envvar:`AR` and
1018:envvar:`ARFLAGS` environment variables.
1019
1020.. ARFLAGS done in #5941
1021
1022It is no longer mandatory to store clear-text passwords in the
1023:file:`.pypirc` file when registering and uploading packages to PyPI. As long
1024as the username is present in that file, the :mod:`distutils` package will
1025prompt for the password if not present. (Added by Tarek Ziadé,
1026based on an initial contribution by Nathan Van Gheem; :issue:`4394`.)
1027
1028A Distutils setup can now specify that a C extension is optional by
1029setting the *optional* option setting to true. If this optional is
1030supplied, failure to build the extension will not abort the build
1031process, but instead simply not install the failing extension.
1032(Contributed by Georg Brandl; :issue:`5583`.)
1033
1034The :class:`distutils.dist.DistributionMetadata` class'
1035:meth:`read_pkg_file` method will read the contents of a package's
1036:file:`PKG-INFO` metadata file. For an example of its use, see
1037:ref:`reading-metadata`.
1038(Contributed by Tarek Ziadé; :issue:`7457`.)
1039
1040:file:`setup.py` files will now accept a :option:`--no-user-cfg` switch
1041to skip reading the :file:`~/.pydistutils.cfg` file. (Suggested by
1042by Michael Hoffman, and implemented by Paul Winkler; :issue:`1180`.)
1043
1044When creating a tar-format archive, the ``sdist`` subcommand now
1045allows specifying the user id and group that will own the files in the
1046archives using the :option:`--owner` and :option:`--group` switches
1047(:issue:`6516`).
Tarek Ziadéba0eacf2010-02-02 23:43:21 +00001048
1049
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001050Unit Testing Enhancements
1051---------------------------------
1052
1053The :mod:`unittest` module was enhanced in several ways.
1054The progress messages now shows 'x' for expected failures
1055and 'u' for unexpected successes when run in verbose mode.
1056(Contributed by Benjamin Peterson.)
1057Test cases can raise the :exc:`SkipTest` exception to skip a test.
1058(:issue:`1034053`.)
1059
1060.. XXX describe test discovery (Contributed by Michael Foord; :issue:`6001`.)
1061
1062The error messages for :meth:`assertEqual`,
1063:meth:`assertTrue`, and :meth:`assertFalse`
1064failures now provide more information. If you set the
1065:attr:`longMessage` attribute of your :class:`TestCase` classes to
1066true, both the standard error message and any additional message you
1067provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
1068
1069The :meth:`assertRaises` and :meth:`failUnlessRaises` methods now
1070return a context handler when called without providing a callable
1071object to run. For example, you can write this::
1072
1073 with self.assertRaises(KeyError):
1074 raise ValueError
1075
1076(Implemented by Antoine Pitrou; :issue:`4444`.)
1077
1078The methods :meth:`addCleanup` and :meth:`doCleanups` were added.
1079:meth:`addCleanup` allows you to add cleanup functions that
1080will be called unconditionally (after :meth:`setUp` if
1081:meth:`setUp` fails, otherwise after :meth:`tearDown`). This allows
1082for much simpler resource allocation and deallocation during tests.
1083:issue:`5679`
1084
1085A number of new methods were added that provide more specialized
1086tests. Many of these methods were written by Google engineers
1087for use in their test suites; Gregory P. Smith, Michael Foord, and
1088GvR worked on merging them into Python's version of :mod:`unittest`.
1089
1090* :meth:`assertIsNone` and :meth:`assertIsNotNone` take one
1091 expression and verify that the result is or is not ``None``.
1092
1093* :meth:`assertIs` and :meth:`assertIsNot` take two values and check
1094 whether the two values evaluate to the same object or not.
1095 (Added by Michael Foord; :issue:`2578`.)
1096
Benjamin Petersona28e7022010-01-09 18:53:06 +00001097* :meth:`assertIsInstance` and :meth:`assertNotIsInstance` check whether
1098 the resulting object is an instance of a particular class, or of
1099 one of a tuple of classes. (Added by Georg Brandl; :issue:`7031`.)
1100
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001101* :meth:`assertGreater`, :meth:`assertGreaterEqual`,
1102 :meth:`assertLess`, and :meth:`assertLessEqual` compare
1103 two quantities.
1104
1105* :meth:`assertMultiLineEqual` compares two strings, and if they're
1106 not equal, displays a helpful comparison that highlights the
Benjamin Peterson9895f912010-03-21 22:05:32 +00001107 differences in the two strings. This comparison is now used by
1108 default when Unicode strings are compared with :meth:`assertEqual`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001109
1110* :meth:`assertRegexpMatches` checks whether its first argument is a
1111 string matching a regular expression provided as its second argument.
1112
1113* :meth:`assertRaisesRegexp` checks whether a particular exception
1114 is raised, and then also checks that the string representation of
1115 the exception matches the provided regular expression.
1116
1117* :meth:`assertIn` and :meth:`assertNotIn` tests whether
1118 *first* is or is not in *second*.
1119
Michael Foordabd91d52010-03-20 18:09:14 +00001120* :meth:`assertItemsEqual` tests whether two provided sequences
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001121 contain the same elements.
1122
1123* :meth:`assertSetEqual` compares whether two sets are equal, and
1124 only reports the differences between the sets in case of error.
1125
1126* Similarly, :meth:`assertListEqual` and :meth:`assertTupleEqual`
Benjamin Peterson9895f912010-03-21 22:05:32 +00001127 compare the specified types and explain any differences without necessarily
1128 printing their full values; these methods are now used by default
1129 when comparing lists and tuples using :meth:`assertEqual`.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001130 More generally, :meth:`assertSequenceEqual` compares two sequences
1131 and can optionally check whether both sequences are of a
1132 particular type.
1133
1134* :meth:`assertDictEqual` compares two dictionaries and reports the
Benjamin Peterson9895f912010-03-21 22:05:32 +00001135 differences; it's now used by default when you compare two dictionaries
1136 using :meth:`assertEqual`. :meth:`assertDictContainsSubset` checks whether
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001137 all of the key/value pairs in *first* are found in *second*.
1138
Benjamin Peterson9895f912010-03-21 22:05:32 +00001139* :meth:`assertAlmostEqual` and :meth:`assertNotAlmostEqual` test
1140 whether *first* and *second* are approximately equal by computing
1141 their difference, rounding the result to an optionally-specified number
1142 of *places* (the default is 7), and comparing to zero.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001143
1144* :meth:`loadTestsFromName` properly honors the ``suiteClass`` attribute of
1145 the :class:`TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.)
1146
1147* A new hook, :meth:`addTypeEqualityFunc` takes a type object and a
1148 function. The :meth:`assertEqual` method will use the function
1149 when both of the objects being compared are of the specified type.
1150 This function should compare the two objects and raise an
1151 exception if they don't match; it's a good idea for the function
1152 to provide additional information about why the two objects are
1153 matching, much as the new sequence comparison methods do.
1154
1155:func:`unittest.main` now takes an optional ``exit`` argument.
1156If False ``main`` doesn't call :func:`sys.exit` allowing it to
1157be used from the interactive interpreter. :issue:`3379`.
1158
1159:class:`TestResult` has new :meth:`startTestRun` and
1160:meth:`stopTestRun` methods; called immediately before
1161and after a test run. :issue:`5728` by Robert Collins.
1162
1163With all these changes, the :file:`unittest.py` was becoming awkwardly
1164large, so the module was turned into a package and the code split into
1165several files (by Benjamin Peterson). This doesn't affect how the
1166module is imported.
1167
1168
1169.. _importlib-section:
1170
Benjamin Petersond23f8222009-04-05 19:13:16 +00001171importlib: Importing Modules
1172------------------------------
1173
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001174Python 3.1 includes the :mod:`importlib` package, a re-implementation
1175of the logic underlying Python's :keyword:`import` statement.
1176:mod:`importlib` is useful for implementors of Python interpreters and
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001177to users who wish to write new importers that can participate in the
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001178import process. Python 2.7 doesn't contain the complete
1179:mod:`importlib` package, but instead has a tiny subset that contains
1180a single function, :func:`import_module`.
1181
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001182``import_module(name, package=None)`` imports a module. *name* is
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001183a string containing the module or package's name. It's possible to do
1184relative imports by providing a string that begins with a ``.``
1185character, such as ``..utils.errors``. For relative imports, the
1186*package* argument must be provided and is the name of the package that
1187will be used as the anchor for
1188the relative import. :func:`import_module` both inserts the imported
1189module into ``sys.modules`` and returns the module object.
1190
1191Here are some examples::
1192
1193 >>> from importlib import import_module
1194 >>> anydbm = import_module('anydbm') # Standard absolute import
1195 >>> anydbm
1196 <module 'anydbm' from '/p/python/Lib/anydbm.py'>
1197 >>> # Relative import
1198 >>> sysconfig = import_module('..sysconfig', 'distutils.command')
1199 >>> sysconfig
1200 <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'>
1201
1202:mod:`importlib` was implemented by Brett Cannon and introduced in
1203Python 3.1.
1204
Benjamin Petersond23f8222009-04-05 19:13:16 +00001205
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00001206ttk: Themed Widgets for Tk
1207--------------------------
1208
1209Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
1210widgets but have a more customizable appearance and can therefore more
1211closely resemble the native platform's widgets. This widget
1212set was originally called Tile, but was renamed to Ttk (for "themed Tk")
1213on being added to Tcl/Tck release 8.5.
1214
1215XXX write a brief discussion and an example here.
1216
1217The :mod:`ttk` module was written by Guilherme Polo and added in
1218:issue:`2983`. An alternate version called ``Tile.py``, written by
1219Martin Franklin and maintained by Kevin Walzer, was proposed for
1220inclusion in :issue:`2618`, but the authors argued that Guilherme
1221Polo's work was more comprehensive.
1222
Georg Brandl4d131ee2009-11-18 18:53:14 +00001223
1224Deprecations and Removals
1225=========================
1226
1227* :func:`contextlib.nested`, which allows handling more than one context manager
1228 with one :keyword:`with` statement, has been deprecated; :keyword:`with`
1229 supports multiple context managers syntactically now.
1230
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001231.. ======================================================================
1232
1233
1234Build and C API Changes
1235=======================
1236
1237Changes to Python's build process and to the C API include:
1238
Georg Brandl1f01deb2009-01-03 22:47:39 +00001239* If you use the :file:`.gdbinit` file provided with Python,
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001240 the "pyo" macro in the 2.7 version now works correctly when the thread being
1241 debugged doesn't hold the GIL; the macro now acquires it before printing.
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001242 (Contributed by Victor Stinner; :issue:`3632`.)
1243
Benjamin Petersond23f8222009-04-05 19:13:16 +00001244* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001245 worker thread submit notifications to the main Python thread. This
1246 is particularly useful for asynchronous IO operations.
1247 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
1248
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001249* New function: :cfunc:`PyCode_NewEmpty` creates an empty code object;
1250 only the filename, function name, and first line number are required.
1251 This is useful to extension modules that are attempting to
1252 construct a more useful traceback stack. Previously such
1253 extensions needed to call :cfunc:`PyCode_New`, which had many
1254 more arguments. (Added by Jeffrey Yasskin.)
1255
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001256* New function: :cfunc:`PyErr_NewExceptionWithDoc` creates a new
1257 exception class, just as the existing :cfunc:`PyErr_NewException` does,
1258 but takes an extra ``char *`` argument containing the docstring for the
1259 new exception class. (Added by the 'lekma' user on the Python bug tracker;
1260 :issue:`7033`.)
1261
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001262* New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object
1263 and returns the line number that the frame is currently executing.
1264 Previously code would need to get the index of the bytecode
1265 instruction currently executing, and then look up the line number
1266 corresponding to that address. (Added by Jeffrey Yasskin.)
1267
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001268* New functions: :cfunc:`PyLong_AsLongAndOverflow` and
1269 :cfunc:`PyLong_AsLongLongAndOverflow` approximates a Python long
1270 integer as a C :ctype:`long` or :ctype:`long long`.
1271 If the number is too large to fit into
1272 the output type, an *overflow* flag is set and returned to the caller.
1273 (Contributed by Case Van Horsen; :issue:`7528` and :issue:`7767`.)
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001274
Benjamin Petersona28e7022010-01-09 18:53:06 +00001275* New function: stemming from the rewrite of string-to-float conversion,
1276 a new :cfunc:`PyOS_string_to_double` function was added. The old
1277 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions
1278 are now deprecated.
1279
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001280* New macros: the Python header files now define the following macros:
1281 :cmacro:`Py_ISALNUM`,
1282 :cmacro:`Py_ISALPHA`,
1283 :cmacro:`Py_ISDIGIT`,
1284 :cmacro:`Py_ISLOWER`,
1285 :cmacro:`Py_ISSPACE`,
1286 :cmacro:`Py_ISUPPER`,
1287 :cmacro:`Py_ISXDIGIT`,
1288 and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`.
1289 All of these functions are analogous to the C
1290 standard macros for classifying characters, but ignore the current
1291 locale setting, because in
1292 several places Python needs to analyze characters in a
1293 locale-independent way. (Added by Eric Smith;
1294 :issue:`5793`.)
1295
1296 .. XXX these macros don't seem to be described in the c-api docs.
1297
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001298* New format codes: the :cfunc:`PyFormat_FromString`,
1299 :cfunc:`PyFormat_FromStringV`, and :cfunc:`PyErr_Format` now
1300 accepts ``%lld`` and ``%llu`` format codes for displaying values of
1301 C's :ctype:`long long` types.
1302 (Contributed by Mark Dickinson; :issue:`7228`.)
1303
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001304* The complicated interaction between threads and process forking has
1305 been changed. Previously, the child process created by
1306 :func:`os.fork` might fail because the child is created with only a
1307 single thread running, the thread performing the :func:`os.fork`.
1308 If other threads were holding a lock, such as Python's import lock,
1309 when the fork was performed, the lock would still be marked as
1310 "held" in the new process. But in the child process nothing would
1311 ever release the lock, since the other threads weren't replicated,
1312 and the child process would no longer be able to perform imports.
1313
1314 Python 2.7 now acquires the import lock before performing an
1315 :func:`os.fork`, and will also clean up any locks created using the
1316 :mod:`threading` module. C extension modules that have internal
1317 locks, or that call :cfunc:`fork()` themselves, will not benefit
1318 from this clean-up.
1319
1320 (Fixed by Thomas Wouters; :issue:`1590864`.)
1321
Benjamin Petersona28e7022010-01-09 18:53:06 +00001322* The :cfunc:`Py_Finalize` function now calls the internal
1323 :func:`threading._shutdown` function; this prevents some exceptions from
1324 being raised when an interpreter shuts down.
1325 (Patch by Adam Olsen; :issue:`1722344`.)
1326
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001327* Global symbols defined by the :mod:`ctypes` module are now prefixed
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001328 with ``Py``, or with ``_ctypes``. (Implemented by Thomas
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001329 Heller; :issue:`3102`.)
1330
Benjamin Petersona28e7022010-01-09 18:53:06 +00001331* New configure option: the :option:`--with-system-expat` switch allows
1332 building the :mod:`pyexpat` module to use the system Expat library.
1333 (Contributed by Arfrever Frehtes Taifersar Arahesis; :issue:`7609`.)
1334
1335* New configure option: Compiling Python with the
1336 :option:`--with-valgrind` option will now disable the pymalloc
1337 allocator, which is difficult for the Valgrind to analyze correctly.
1338 Valgrind will therefore be better at detecting memory leaks and
1339 overruns. (Contributed by James Henstridge; :issue:`2422`.)
1340
1341* New configure option: you can now supply no arguments to
1342 :option:`--with-dbmliborder=` in order to build none of the various
1343 DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis;
1344 :issue:`6491`.)
1345
Benjamin Petersond23f8222009-04-05 19:13:16 +00001346* The :program:`configure` script now checks for floating-point rounding bugs
1347 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
1348 preprocessor definition. No code currently uses this definition,
1349 but it's available if anyone wishes to use it.
1350 (Added by Mark Dickinson; :issue:`2937`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001351
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001352* The build process now creates the necessary files for pkg-config
1353 support. (Contributed by Clinton Roy; :issue:`3585`.)
1354
1355* The build process now supports Subversion 1.7. (Contributed by
1356 Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.)
1357
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001358
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001359.. ======================================================================
1360
1361Port-Specific Changes: Windows
1362-----------------------------------
1363
Georg Brandl1f01deb2009-01-03 22:47:39 +00001364* The :mod:`msvcrt` module now contains some constants from
1365 the :file:`crtassem.h` header file:
1366 :data:`CRT_ASSEMBLY_VERSION`,
1367 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
1368 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001369 (Contributed by David Cournapeau; :issue:`4365`.)
1370
1371* The new :cfunc:`_beginthreadex` API is used to start threads, and
1372 the native thread-local storage functions are now used.
1373 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001374
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001375* The :func:`os.listdir` function now correctly fails
1376 for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.)
1377
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001378* The :mod:`mimelib` module will now read the MIME database from
1379 the Windows registry when initializing.
1380 (Patch by Gabriel Genellina; :issue:`4969`.)
1381
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001382.. ======================================================================
1383
1384Port-Specific Changes: Mac OS X
1385-----------------------------------
1386
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001387* The path ``/Library/Python/2.7/site-packages`` is now appended to
Benjamin Petersond23f8222009-04-05 19:13:16 +00001388 ``sys.path``, in order to share added packages between the system
1389 installation and a user-installed copy of the same version.
1390 (Changed by Ronald Oussoren; :issue:`4865`.)
1391
1392
1393Other Changes and Fixes
1394=======================
1395
Benjamin Peterson9895f912010-03-21 22:05:32 +00001396* Two benchmark scripts, :file:`iobench` and :file:`ccbench`, were
1397 added to the :file:`Tools` directory. :file:`iobench` measures the
1398 speed of built-in file I/O objects (as returned by :func:`open`)
1399 while performing various operations, and :file:`ccbench` is a
1400 concurrency benchmark that tries to measure computing throughput,
1401 thread switching latency, and IO processing bandwidth when
1402 performing several tasks using a varying number of threads.
1403
Benjamin Petersond23f8222009-04-05 19:13:16 +00001404* When importing a module from a :file:`.pyc` or :file:`.pyo` file
1405 with an existing :file:`.py` counterpart, the :attr:`co_filename`
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001406 attributes of the resulting code objects are overwritten when the
1407 original filename is obsolete. This can happen if the file has been
1408 renamed, moved, or is accessed through different paths. (Patch by
1409 Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +00001410
1411* The :file:`regrtest.py` script now takes a :option:`--randseed=`
1412 switch that takes an integer that will be used as the random seed
1413 for the :option:`-r` option that executes tests in random order.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001414 The :option:`-r` option also reports the seed that was used
Benjamin Petersond23f8222009-04-05 19:13:16 +00001415 (Added by Collin Winter.)
1416
Benjamin Petersona28e7022010-01-09 18:53:06 +00001417* Another :file:`regrtest.py` switch is :option:`-j`, which
1418 takes an integer specifying how many tests run in parallel. This
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001419 allows reducing the total runtime on multi-core machines.
Antoine Pitrou88909542009-06-29 13:54:42 +00001420 This option is compatible with several other options, including the
1421 :option:`-R` switch which is known to produce long runtimes.
Benjamin Petersona28e7022010-01-09 18:53:06 +00001422 (Added by Antoine Pitrou, :issue:`6152`.) This can also be used
1423 with a new :option:`-F` switch that runs selected tests in a loop
1424 until they fail. (Added by Antoine Pitrou; :issue:`7312`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001425
1426.. ======================================================================
1427
1428Porting to Python 2.7
1429=====================
1430
1431This section lists previously described changes and other bugfixes
1432that may require changes to your code:
1433
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001434* When using :class:`Decimal` instances with a string's
1435 :meth:`format` method, the default alignment was previously
1436 left-alignment. This has been changed to right-alignment, which might
1437 change the output of your programs.
1438 (Changed by Mark Dickinson; :issue:`6857`.)
1439
1440 Another :meth:`format`-related change: the default precision used
1441 for floating-point and complex numbers was changed from 6 decimal
1442 places to 12, which matches the precision used by :func:`str`.
1443 (Changed by Eric Smith; :issue:`5920`.)
1444
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001445* Because of an optimization for the :keyword:`with` statement, the special
1446 methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's
1447 type, and cannot be directly attached to the object's instance. This
1448 affects new-style classes (derived from :class:`object`) and C extension
1449 types. (:issue:`6101`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001450
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001451* The :meth:`readline` method of :class:`StringIO` objects now does
1452 nothing when a negative length is requested, as other file-like
1453 objects do. (:issue:`7348`).
1454
Benjamin Peterson9895f912010-03-21 22:05:32 +00001455In the standard library:
1456
1457* The ElementTree library, :mod:`xml.etree`, no longer escapes
1458 ampersands and angle brackets when outputting an XML processing
1459 instruction (which looks like `<?xml-stylesheet href="#style1"?>`)
1460 or comment (which looks like `<!-- comment -->`).
1461 (Patch by Neil Muller; :issue:`2746`.)
1462
Benjamin Petersona28e7022010-01-09 18:53:06 +00001463For C extensions:
1464
1465* C extensions that use integer format codes with the ``PyArg_Parse*``
1466 family of functions will now raise a :exc:`TypeError` exception
1467 instead of triggering a :exc:`DeprecationWarning` (:issue:`5080`).
1468
1469* Use the new :cfunc:`PyOS_string_to_double` function instead of the old
1470 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions,
1471 which are now deprecated.
1472
1473
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001474.. ======================================================================
1475
1476
1477.. _acks27:
1478
1479Acknowledgements
1480================
1481
1482The author would like to thank the following people for offering
1483suggestions, corrections and assistance with various drafts of this
Benjamin Peterson97dd9872009-12-13 01:23:39 +00001484article: Ryan Lovett, Hugh Secker-Walker.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001485