blob: fa041907f4b01f27ff3b89bf61a9755f8f64cf8b [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
213This mechanism is not adaptable at all; commas are always used as the
214separator and the grouping is always into three-digit groups. The
215comma-formatting mechanism isn't as general as the :mod:`locale`
216module, but it's easier to use.
217
218.. XXX "Format String Syntax" in string.rst could use many more examples.
219
220.. seealso::
221
222 :pep:`378` - Format Specifier for Thousands Separator
223 PEP written by Raymond Hettinger; implemented by Eric Smith.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000224
Benjamin Peterson9895f912010-03-21 22:05:32 +0000225PEP 389: The argparse Module for Parsing Command Lines
226======================================================
227
228XXX write this section.
229
230.. seealso::
231
232 :pep:`389` - argparse - New Command Line Parsing Module
233 PEP written and implemented by Steven Bethard.
234
235PEP 391: Dictionary-Based Configuration For Logging
236====================================================
237
238XXX write this section.
239
240.. seealso::
241
242 :pep:`391` - Dictionary-Based Configuration For Logging
243 PEP written and implemented by Vinay Sajip.
244
245PEP 3106: Dictionary Views
246====================================================
247
248XXX write this section.
249
250.. seealso::
251
252 :pep:`3106` - Revamping dict.keys(), .values() and .items()
253 PEP written by Guido van Rossum.
254 Backported to 2.7 by Alexandre Vassalotti; :issue:`1967`.
255
256
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000257Other Language Changes
258======================
259
260Some smaller changes made to the core Python language are:
261
Benjamin Peterson9895f912010-03-21 22:05:32 +0000262* The syntax for set literals has been backported from Python 3.x.
263 Curly brackets are used to surround the contents of the resulting
264 mutable set; set literals are
265 distinguished from dictionaries by not containing colons and values.
266 ``{}`` continues to represent an empty dictionary; use
267 ``set()`` for an empty set.
268
269 >>> {1,2,3,4,5}
270 set([1, 2, 3, 4, 5])
271 >>> set()
272 set([])
273 >>> {}
274 {}
275
276 Backported by Alexandre Vassalotti; :issue:`2335`.
277
278* Dictionary and set comprehensions are another feature backported from
279 3.x, generalizing list/generator comprehensions to use
280 the literal syntax for sets and dictionaries.
281
282 >>> {x:x*x for x in range(6)}
283 {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
284 >>> {'a'*x for x in range(6)}
285 set(['', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa'])
286
287 Backported by Alexandre Vassalotti; :issue:`2333`.
288
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000289* The :keyword:`with` statement can now use multiple context managers
290 in one statement. Context managers are processed from left to right
291 and each one is treated as beginning a new :keyword:`with` statement.
292 This means that::
293
294 with A() as a, B() as b:
295 ... suite of statements ...
296
297 is equivalent to::
298
299 with A() as a:
300 with B() as b:
301 ... suite of statements ...
302
303 The :func:`contextlib.nested` function provides a very similar
304 function, so it's no longer necessary and has been deprecated.
305
306 (Proposed in http://codereview.appspot.com/53094; implemented by
307 Georg Brandl.)
308
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000309* Conversions between floating-point numbers and strings are
310 now correctly rounded on most platforms. These conversions occur
311 in many different places: :func:`str` on
312 floats and complex numbers; the :class:`float` and :class:`complex`
313 constructors;
314 numeric formatting; serialization and
315 deserialization of floats and complex numbers using the
316 :mod:`marshal`, :mod:`pickle`
317 and :mod:`json` modules;
318 parsing of float and imaginary literals in Python code;
319 and :class:`Decimal`-to-float conversion.
320
321 Related to this, the :func:`repr` of a floating-point number *x*
322 now returns a result based on the shortest decimal string that's
323 guaranteed to round back to *x* under correct rounding (with
324 round-half-to-even rounding mode). Previously it gave a string
325 based on rounding x to 17 decimal digits.
326
327 The rounding library responsible for this improvement works on
328 Windows, and on Unix platforms using the gcc, icc, or suncc
329 compilers. There may be a small number of platforms where correct
330 operation of this code cannot be guaranteed, so the code is not
Benjamin Petersona28e7022010-01-09 18:53:06 +0000331 used on such systems. You can find out which code is being used
332 by checking :data:`sys.float_repr_style`, which will be ``short``
333 if the new code is in use and ``legacy`` if it isn't.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000334
Mark Dickinson0bc8f902010-01-07 09:31:48 +0000335 Implemented by Eric Smith and Mark Dickinson, using David Gay's
336 :file:`dtoa.c` library; :issue:`7117`.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000337
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000338* The :meth:`str.format` method now supports automatic numbering of the replacement
Benjamin Peterson3f96a872009-04-11 20:58:12 +0000339 fields. This makes using :meth:`str.format` more closely resemble using
340 ``%s`` formatting::
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000341
342 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
343 '2009:4:Sunday'
344 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
345 '2009:4:Sunday'
346
Benjamin Peterson3f96a872009-04-11 20:58:12 +0000347 The auto-numbering takes the fields from left to right, so the first ``{...}``
348 specifier will use the first argument to :meth:`str.format`, the next
349 specifier will use the next argument, and so on. You can't mix auto-numbering
350 and explicit numbering -- either number all of your specifier fields or none
351 of them -- but you can mix auto-numbering and named fields, as in the second
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000352 example above. (Contributed by Eric Smith; :issue:`5237`.)
353
354 Complex numbers now correctly support usage with :func:`format`.
355 Specifying a precision or comma-separation applies to both the real
356 and imaginary parts of the number, but a specified field width and
357 alignment is applied to the whole of the resulting ``1.5+3j``
358 output. (Contributed by Eric Smith; :issue:`1588`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000359
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000360 The 'F' format code now always formats its output using uppercase characters,
361 so it will now produce 'INF' and 'NAN'.
362 (Contributed by Eric Smith; :issue:`3382`.)
363
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000364* The :func:`int` and :func:`long` types gained a ``bit_length``
365 method that returns the number of bits necessary to represent
366 its argument in binary::
367
368 >>> n = 37
369 >>> bin(37)
370 '0b100101'
371 >>> n.bit_length()
372 6
373 >>> n = 2**123-1
374 >>> n.bit_length()
375 123
376 >>> (n+1).bit_length()
377 124
378
379 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
380
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000381* Conversions from long integers and regular integers to floating
382 point now round differently, returning the floating-point number
383 closest to the number. This doesn't matter for small integers that
384 can be converted exactly, but for large numbers that will
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000385 unavoidably lose precision, Python 2.7 now approximates more
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000386 closely. For example, Python 2.6 computed the following::
387
388 >>> n = 295147905179352891391
389 >>> float(n)
390 2.9514790517935283e+20
391 >>> n - long(float(n))
392 65535L
393
394 Python 2.7's floating-point result is larger, but much closer to the
395 true value::
396
397 >>> n = 295147905179352891391
398 >>> float(n)
399 2.9514790517935289e+20
400 >>> n-long(float(n)
401 ... )
402 -1L
403
404 (Implemented by Mark Dickinson; :issue:`3166`.)
405
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000406 Integer division is also more accurate in its rounding behaviours. (Also
407 implemented by Mark Dickinson; :issue:`1811`.)
408
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000409* The :class:`bytearray` type's :meth:`translate` method now accepts
410 ``None`` as its first argument. (Fixed by Georg Brandl;
Benjamin Petersond23f8222009-04-05 19:13:16 +0000411 :issue:`4759`.)
Mark Dickinsond72c7b62009-03-20 16:00:49 +0000412
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000413* When using ``@classmethod`` and ``@staticmethod`` to wrap
414 methods as class or static methods, the wrapper object now
415 exposes the wrapped function as their :attr:`__func__` attribute.
416 (Contributed by Amaury Forgeot d'Arc, after a suggestion by
417 George Sakkis; :issue:`5982`.)
418
419* A new encoding named "cp720", used primarily for Arabic text, is now
420 supported. (Contributed by Alexander Belchenko and Amaury Forgeot
421 d'Arc; :issue:`1616979`.)
422
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000423* The :class:`file` object will now set the :attr:`filename` attribute
424 on the :exc:`IOError` exception when trying to open a directory
Benjamin Peterson9895f912010-03-21 22:05:32 +0000425 on POSIX platforms (noted by Jan Kaliszewski; :issue:`4764`), and
426 now explicitly checks for and forbids writing to read-only file objects
427 instead of trusting the C library to catch and report the error
428 (fixed by Stefan Krah; :issue:`5677`).
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000429
Benjamin Petersona28e7022010-01-09 18:53:06 +0000430* The Python tokenizer now translates line endings itself, so the
431 :func:`compile` built-in function can now accept code using any
432 line-ending convention. Additionally, it no longer requires that the
433 code end in a newline.
434
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000435* Extra parentheses in function definitions are illegal in Python 3.x,
436 meaning that you get a syntax error from ``def f((x)): pass``. In
437 Python3-warning mode, Python 2.7 will now warn about this odd usage.
438 (Noted by James Lingard; :issue:`7362`.)
439
Benjamin Peterson9895f912010-03-21 22:05:32 +0000440* When a module object is garbage-collected, the module's dictionary is
441 now only cleared if no one else is holding a reference to the
442 dictionary (:issue:`7140`).
443
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000444.. ======================================================================
445
446
447Optimizations
448-------------
449
Benjamin Petersond23f8222009-04-05 19:13:16 +0000450Several performance enhancements have been added:
451
452.. * A new :program:`configure` option, :option:`--with-computed-gotos`,
453 compiles the main bytecode interpreter loop using a new dispatch
454 mechanism that gives speedups of up to 20%, depending on the system
455 and benchmark. The new mechanism is only supported on certain
456 compilers, such as gcc, SunPro, and icc.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000457
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000458* A new opcode was added to perform the initial setup for
459 :keyword:`with` statements, looking up the :meth:`__enter__` and
460 :meth:`__exit__` methods. (Contributed by Benjamin Peterson.)
461
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000462* The garbage collector now performs better for one common usage
463 pattern: when many objects are being allocated without deallocating
464 any of them. This would previously take quadratic
465 time for garbage collection, but now the number of full garbage collections
466 is reduced as the number of objects on the heap grows.
467 The new logic is to only perform a full garbage collection pass when
468 the middle generation has been collected 10 times and when the
469 number of survivor objects from the middle generation exceeds 10% of
470 the number of objects in the oldest generation. (Suggested by Martin
471 von Loewis and implemented by Antoine Pitrou; :issue:`4074`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000472
Benjamin Petersond23f8222009-04-05 19:13:16 +0000473* The garbage collector tries to avoid tracking simple containers
474 which can't be part of a cycle. In Python 2.7, this is now true for
475 tuples and dicts containing atomic types (such as ints, strings,
476 etc.). Transitively, a dict containing tuples of atomic types won't
477 be tracked either. This helps reduce the cost of each
478 garbage collection by decreasing the number of objects to be
479 considered and traversed by the collector.
Antoine Pitrou9d81def2009-03-28 19:20:09 +0000480 (Contributed by Antoine Pitrou; :issue:`4688`.)
481
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000482* Long integers are now stored internally either in base 2**15 or in base
Benjamin Petersond23f8222009-04-05 19:13:16 +0000483 2**30, the base being determined at build time. Previously, they
484 were always stored in base 2**15. Using base 2**30 gives
485 significant performance improvements on 64-bit machines, but
486 benchmark results on 32-bit machines have been mixed. Therefore,
487 the default is to use base 2**30 on 64-bit machines and base 2**15
488 on 32-bit machines; on Unix, there's a new configure option
489 :option:`--enable-big-digits` that can be used to override this default.
490
491 Apart from the performance improvements this change should be
492 invisible to end users, with one exception: for testing and
493 debugging purposes there's a new structseq ``sys.long_info`` that
494 provides information about the internal format, giving the number of
495 bits per digit and the size in bytes of the C type used to store
496 each digit::
497
498 >>> import sys
499 >>> sys.long_info
500 sys.long_info(bits_per_digit=30, sizeof_digit=4)
501
502 (Contributed by Mark Dickinson; :issue:`4258`.)
503
504 Another set of changes made long objects a few bytes smaller: 2 bytes
505 smaller on 32-bit systems and 6 bytes on 64-bit.
506 (Contributed by Mark Dickinson; :issue:`5260`.)
507
508* The division algorithm for long integers has been made faster
509 by tightening the inner loop, doing shifts instead of multiplications,
510 and fixing an unnecessary extra iteration.
511 Various benchmarks show speedups of between 50% and 150% for long
512 integer divisions and modulo operations.
513 (Contributed by Mark Dickinson; :issue:`5512`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000514 Bitwise operations are also significantly faster (initial patch by
515 Gregory Smith; :issue:`1087418`).
Benjamin Petersond23f8222009-04-05 19:13:16 +0000516
517* The implementation of ``%`` checks for the left-side operand being
518 a Python string and special-cases it; this results in a 1-3%
519 performance increase for applications that frequently use ``%``
520 with strings, such as templating libraries.
521 (Implemented by Collin Winter; :issue:`5176`.)
522
523* List comprehensions with an ``if`` condition are compiled into
524 faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
525 by Jeffrey Yasskin; :issue:`4715`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000526
Benjamin Petersona28e7022010-01-09 18:53:06 +0000527* Converting an integer or long integer to a decimal string was made
528 faster by special-casing base 10 instead of using a generalized
529 conversion function that supports arbitrary bases.
530 (Patch by Gawain Bolton; :issue:`6713`.)
531
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000532* The :meth:`split`, :meth:`replace`, :meth:`rindex`,
533 :meth:`rpartition`, and :meth:`rsplit` methods of string-like types
534 (strings, Unicode strings, and :class:`bytearray` objects) now use a
535 fast reverse-search algorithm instead of a character-by-character
536 scan. This is sometimes faster by a factor of 10. (Added by
537 Florent Xicluna; :issue:`7462` and :issue:`7622`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000538
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000539* The :mod:`pickle` and :mod:`cPickle` modules now automatically
540 intern the strings used for attribute names, reducing memory usage
541 of the objects resulting from unpickling. (Contributed by Jake
542 McGuire; :issue:`5084`.)
543
544* The :mod:`cPickle` module now special-cases dictionaries,
545 nearly halving the time required to pickle them.
546 (Contributed by Collin Winter; :issue:`5670`.)
547
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000548.. ======================================================================
549
Georg Brandl4d131ee2009-11-18 18:53:14 +0000550New and Improved Modules
551========================
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000552
553As in every release, Python's standard library received a number of
554enhancements and bug fixes. Here's a partial list of the most notable
555changes, sorted alphabetically by module name. Consult the
556:file:`Misc/NEWS` file in the source tree for a more complete list of
557changes, or look through the Subversion logs for all the details.
558
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000559* The :mod:`bdb` module's base debugging class :class:`Bdb`
560 gained a feature for skipping modules. The constructor
561 now takes an iterable containing glob-style patterns such as
562 ``django.*``; the debugger will not step into stack frames
563 from a module that matches one of these patterns.
564 (Contributed by Maru Newby after a suggestion by
565 Senthil Kumaran; :issue:`5142`.)
566
Benjamin Peterson9895f912010-03-21 22:05:32 +0000567* The :mod:`binascii` module now supports the buffer API, so it can be
568 used with :class:`memoryview` instances and other similar buffer objects.
569 (Backported from 3.x by Florent Xicluna; :issue:`7703`.)
570
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000571* The :mod:`bz2` module's :class:`BZ2File` now supports the context
572 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
573 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
574
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000575* New class: the :class:`Counter` class in the :mod:`collections` module is
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000576 useful for tallying data. :class:`Counter` instances behave mostly
577 like dictionaries but return zero for missing keys instead of
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000578 raising a :exc:`KeyError`:
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000579
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000580 .. doctest::
581 :options: +NORMALIZE_WHITESPACE
582
583 >>> from collections import Counter
584 >>> c = Counter()
585 >>> for letter in 'here is a sample of english text':
586 ... c[letter] += 1
587 ...
588 >>> c
589 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
590 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
591 'p': 1, 'r': 1, 'x': 1})
592 >>> c['e']
593 5
594 >>> c['z']
595 0
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000596
597 There are two additional :class:`Counter` methods: :meth:`most_common`
598 returns the N most common elements and their counts, and :meth:`elements`
599 returns an iterator over the contained element, repeating each element
600 as many times as its count::
601
602 >>> c.most_common(5)
603 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
604 >>> c.elements() ->
605 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
606 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
607 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000608 's', 's', 'r', 't', 't', 'x'
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000609
610 Contributed by Raymond Hettinger; :issue:`1696199`.
611
Georg Brandlef871f62010-03-12 10:06:40 +0000612 The new `~collections.OrderedDict` class is described in the earlier section
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000613 :ref:`pep-0372`.
614
Benjamin Petersond23f8222009-04-05 19:13:16 +0000615 The :class:`namedtuple` class now has an optional *rename* parameter.
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000616 If *rename* is true, field names that are invalid because they've
Benjamin Petersond23f8222009-04-05 19:13:16 +0000617 been repeated or that aren't legal Python identifiers will be
618 renamed to legal names that are derived from the field's
619 position within the list of fields:
620
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000621 >>> from collections import namedtuple
622 >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000623 >>> T._fields
624 ('field1', '_1', '_2', 'field2')
625
626 (Added by Raymond Hettinger; :issue:`1818`.)
627
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000628 The :class:`deque` data type now exposes its maximum length as the
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000629 read-only :attr:`maxlen` attribute, and has a
630 :meth:`reverse` method that reverses the elements of the deque in-place.
631 (Added by Raymond Hettinger.)
632
633* The :mod:`copy` module's :func:`deepcopy` function will now
634 correctly copy bound instance methods. (Implemented by
635 Robert Collins; :issue:`1515`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000636
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000637* The :mod:`ctypes` module now always converts ``None`` to a C NULL
638 pointer for arguments declared as pointers. (Changed by Thomas
Benjamin Peterson9895f912010-03-21 22:05:32 +0000639 Heller; :issue:`4606`.) The underlying `libffi library
640 <http://sourceware.org/libffi/>`__ has been updated to version
641 3.0.9, containing various fixes for different platforms. (Updated
642 by Matthias Klose; :issue:`8142`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000643
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000644* New method: the :mod:`datetime` module's :class:`timedelta` class
645 gained a :meth:`total_seconds` method that returns the number of seconds
646 in the duration. (Contributed by Brian Quinlan; :issue:`5788`.)
647
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000648* New method: the :class:`Decimal` class gained a
649 :meth:`from_float` class method that performs an exact conversion
650 of a floating-point number to a :class:`Decimal`.
651 Note that this is an **exact** conversion that strives for the
652 closest decimal approximation to the floating-point representation's value;
653 the resulting decimal value will therefore still include the inaccuracy,
654 if any.
655 For example, ``Decimal.from_float(0.1)`` returns
656 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
657 (Implemented by Raymond Hettinger; :issue:`4796`.)
658
659 The constructor for :class:`Decimal` now accepts non-European
660 Unicode characters, such as Arabic-Indic digits. (Contributed by
661 Mark Dickinson; :issue:`6595`.)
662
663 When using :class:`Decimal` instances with a string's
664 :meth:`format` method, the default alignment was previously
665 left-alignment. This has been changed to right-alignment, which seems
666 more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.)
667
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000668* The :class:`Fraction` class now accepts two rational numbers
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000669 as arguments to its constructor.
670 (Implemented by Mark Dickinson; :issue:`5812`.)
671
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000672* The :mod:`ftplib` module gained the ability to establish secure FTP
673 connections using TLS encapsulation of authentication as well as
674 subsequent control and data transfers. This is provided by the new
675 :class:`ftplib.FTP_TLS` class.
676 (Contributed by Giampaolo Rodola', :issue:`2054`.) The :meth:`storbinary`
677 method for binary uploads can now restart uploads thanks to an added
678 *rest* parameter (patch by Pablo Mouzo; :issue:`6845`.)
679
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000680* New function: the :mod:`gc` module's :func:`is_tracked` returns
681 true if a given instance is tracked by the garbage collector, false
Benjamin Petersond23f8222009-04-05 19:13:16 +0000682 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
683
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000684* The :mod:`gzip` module's :class:`GzipFile` now supports the context
Benjamin Peterson9895f912010-03-21 22:05:32 +0000685 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``
686 (contributed by Hagen Fuerstenau; :issue:`3860`), and it now implements
687 the :class:`io.BufferedIOBase` ABC, so you can wrap it with
688 :class:`io.BufferedReader` for faster processing
689 (contributed by Nir Aides; :issue:`7471`).
690 It's also now possible to override the modification time
Benjamin Petersond23f8222009-04-05 19:13:16 +0000691 recorded in a gzipped file by providing an optional timestamp to
692 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000693
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000694 Files in gzip format can be padded with trailing zero bytes; the
695 :mod:`gzip` module will now consume these trailing bytes. (Fixed by
696 Tadek Pietraszek and Brian Curtin; :issue:`2846`.)
697
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000698* The default :class:`HTTPResponse` class used by the :mod:`httplib` module now
699 supports buffering, resulting in much faster reading of HTTP responses.
700 (Contributed by Kristjan Valur Jonsson; :issue:`4879`.)
701
Benjamin Peterson9895f912010-03-21 22:05:32 +0000702 The :class:`HTTPConnection` and :class:`HTTPSConnection` classes
703 now support a *source_address* parameter, a ``(host, port)`` 2-tuple
704 giving the source address that will be used for the connection.
705 (Contributed by Eldon Ziegler; :issue:`3972`.)
706
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000707* The :mod:`imaplib` module now supports IPv6 addresses.
708 (Contributed by Derek Morr; :issue:`1655`.)
709
710* The :mod:`io` library has been upgraded to the version shipped with
711 Python 3.1. For 3.1, the I/O library was entirely rewritten in C
712 and is 2 to 20 times faster depending on the task at hand. The
713 original Python version was renamed to the :mod:`_pyio` module.
714
715 One minor resulting change: the :class:`io.TextIOBase` class now
716 has an :attr:`errors` attribute giving the error setting
717 used for encoding and decoding errors (one of ``'strict'``, ``'replace'``,
718 ``'ignore'``).
719
720 The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000721 an invalid file descriptor. (Implemented by Benjamin Peterson;
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000722 :issue:`4991`.) The :meth:`truncate` method now preserves the
723 file position; previously it would change the file position to the
724 end of the new file. (Fixed by Pascal Chambon; :issue:`6939`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000725
Benjamin Peterson97dd9872009-12-13 01:23:39 +0000726* New function: ``itertools.compress(data, selectors)`` takes two
Benjamin Petersond23f8222009-04-05 19:13:16 +0000727 iterators. Elements of *data* are returned if the corresponding
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000728 value in *selectors* is true::
Benjamin Petersond23f8222009-04-05 19:13:16 +0000729
730 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
731 A, C, E, F
732
Benjamin Peterson97dd9872009-12-13 01:23:39 +0000733 New function: ``itertools.combinations_with_replacement(iter, r)``
Benjamin Petersond23f8222009-04-05 19:13:16 +0000734 returns all the possible *r*-length combinations of elements from the
735 iterable *iter*. Unlike :func:`combinations`, individual elements
736 can be repeated in the generated combinations::
737
738 itertools.combinations_with_replacement('abc', 2) =>
739 ('a', 'a'), ('a', 'b'), ('a', 'c'),
740 ('b', 'b'), ('b', 'c'), ('c', 'c')
741
742 Note that elements are treated as unique depending on their position
743 in the input, not their actual values.
744
745 The :class:`itertools.count` function now has a *step* argument that
746 allows incrementing by values other than 1. :func:`count` also
747 now allows keyword arguments, and using non-integer values such as
748 floats or :class:`Decimal` instances. (Implemented by Raymond
749 Hettinger; :issue:`5032`.)
750
751 :func:`itertools.combinations` and :func:`itertools.product` were
752 previously raising :exc:`ValueError` for values of *r* larger than
753 the input iterable. This was deemed a specification error, so they
754 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
755
756* The :mod:`json` module was upgraded to version 2.0.9 of the
757 simplejson package, which includes a C extension that makes
758 encoding and decoding faster.
759 (Contributed by Bob Ippolito; :issue:`4136`.)
760
761 To support the new :class:`OrderedDict` type, :func:`json.load`
762 now has an optional *object_pairs_hook* parameter that will be called
763 with any object literal that decodes to a list of pairs.
764 (Contributed by Raymond Hettinger; :issue:`5381`.)
765
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000766* New functions: the :mod:`math` module gained
767 :func:`erf` and :func:`erfc` for the error function and the complementary error function,
768 :func:`expm1` which computes ``e**x - 1`` with more precision than
769 using :func:`exp` and subtracting 1,
770 :func:`gamma` for the Gamma function, and
771 :func:`lgamma` for the natural log of the Gamma function.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000772 (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.)
773
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000774* The :mod:`multiprocessing` module's :class:`Manager*` classes
775 can now be passed a callable that will be called whenever
776 a subprocess is started, along with a set of arguments that will be
777 passed to the callable.
778 (Contributed by lekma; :issue:`5585`.)
779
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000780 The :class:`Pool` class, which controls a pool of worker processes,
781 now has an optional *maxtasksperchild* parameter. Worker processes
782 will perform the specified number of tasks and then exit, causing the
783 :class:`Pool` to start a new worker. This is useful if tasks may leak
784 memory or other resources, or if some tasks will cause the worker to
785 become very large.
786 (Contributed by Charles Cazabon; :issue:`6963`.)
787
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000788* The :mod:`nntplib` module now supports IPv6 addresses.
789 (Contributed by Derek Morr; :issue:`1664`.)
790
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000791* New functions: the :mod:`os` module wraps the following POSIX system
792 calls: :func:`getresgid` and :func:`getresuid`, which return the
793 real, effective, and saved GIDs and UIDs;
794 :func:`setresgid` and :func:`setresuid`, which set
795 real, effective, and saved GIDs and UIDs to new values;
796 :func:`initgroups`. (GID/UID functions
797 contributed by Travis H.; :issue:`6508`. Support for initgroups added
798 by Jean-Paul Calderone; :issue:`7333`.)
799
Benjamin Peterson9895f912010-03-21 22:05:32 +0000800 The :func:`os.fork` function now re-initializes the import lock in
801 the child process; this fixes problems on Solaris when :func:`fork`
802 is called from a thread. (Fixed by Zsolt Cserna; :issue:`7242`.)
803
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000804 The :func:`normpath` function now preserves Unicode; if its input path
805 is a Unicode string, the return value is also a Unicode string.
806 (Fixed by Matt Giuca; :issue:`5827`.)
807
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000808* The :mod:`pydoc` module now has help for the various symbols that Python
809 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
810 (Contributed by David Laban; :issue:`4739`.)
811
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000812* The :mod:`re` module's :func:`split`, :func:`sub`, and :func:`subn`
813 now accept an optional *flags* argument, for consistency with the
814 other functions in the module. (Added by Gregory P. Smith.)
815
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000816* The :mod:`shutil` module's :func:`copyfile` and :func:`copytree`
817 functions now raises a :exc:`SpecialFileError` exception when
818 asked to copy a named pipe. Previously the code would treat
819 named pipes like a regular file by opening them for reading, and
820 this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.)
821
Benjamin Peterson9895f912010-03-21 22:05:32 +0000822 New function: :func:`make_archive` takes a filename, archive type
823 (zip or tar-format), and a directory path, and creates an archive
824 containing the directory's contents. (Added by Tarek Ziadé.)
825
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000826* New functions: in the :mod:`site` module, three new functions
827 return various site- and user-specific paths.
828 :func:`getsitepackages` returns a list containing all
829 global site-packages directories, and
830 :func:`getusersitepackages` returns the path of the user's
831 site-packages directory.
Ezio Melotti6e40e272010-01-04 09:29:10 +0000832 :func:`getuserbase` returns the value of the :envvar:`USER_BASE`
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000833 environment variable, giving the path to a directory that can be used
834 to store data.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000835 (Contributed by Tarek Ziadé; :issue:`6693`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000836
Benjamin Peterson9895f912010-03-21 22:05:32 +0000837 The :mod:`site` module now reports exceptions occurring
838 when the :mod:`sitecustomize` module is imported, and will no longer
Florent Xicluna41fe6152010-04-02 18:52:12 +0000839 catch and swallow the :exc:`KeyboardInterrupt` exception. (Fixed by
Benjamin Peterson9895f912010-03-21 22:05:32 +0000840 Victor Stinner; :issue:`3137`.)
841
Benjamin Petersona28e7022010-01-09 18:53:06 +0000842* The :mod:`socket` module's :class:`SSL` objects now support the
Benjamin Peterson9895f912010-03-21 22:05:32 +0000843 buffer API, which fixed a test suite failure. (Fixed by Antoine
844 Pitrou; :issue:`7133`.)
845
846 The :func:`create_connection` function
847 gained a *source_address* parameter, a ``(host, port)`` 2-tuple
848 giving the source address that will be used for the connection.
849 (Contributed by Eldon Ziegler; :issue:`3972`.)
850
Ezio Melotti9de5a412010-04-05 08:21:29 +0000851 The :meth:`recv_into` and :meth:`recvfrom_into` methods will now write
Benjamin Peterson9895f912010-03-21 22:05:32 +0000852 into objects that support the buffer API, most usefully
853 the :class:`bytearray` and :class:`memoryview` objects. (Implemented by
854 Antoine Pitrou; :issue:`8104`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000855
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000856* The :mod:`SocketServer` module's :class:`TCPServer` class now
857 has a :attr:`disable_nagle_algorithm` class attribute.
858 The default value is False; if overridden to be True,
859 new request connections will have the TCP_NODELAY option set to
860 prevent buffering many small sends into a single TCP packet.
861 (Contributed by Kristjan Valur Jonsson; :issue:`6192`.)
862
Benjamin Peterson9895f912010-03-21 22:05:32 +0000863* Updated module: the :mod:`sqlite` module has been updated to
864 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
865 the ability to load SQLite extensions from shared libraries.
866 Call the ``enable_load_extension(True)`` method to enable extensions,
867 and then call :meth:`load_extension` to load a particular shared library.
868 (Updated by Gerhard Häring.)
869
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000870* The :mod:`struct` module will no longer silently ignore overflow
871 errors when a value is too large for a particular integer format
872 code (one of ``bBhHiIlLqQ``); it now always raises a
873 :exc:`struct.error` exception. (Changed by Mark Dickinson;
874 :issue:`1523`.)
875
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000876* New function: the :mod:`subprocess` module's
877 :func:`check_output` runs a command with a specified set of arguments
Benjamin Petersond23f8222009-04-05 19:13:16 +0000878 and returns the command's output as a string when the command runs without
Georg Brandl1f01deb2009-01-03 22:47:39 +0000879 error, or raises a :exc:`CalledProcessError` exception otherwise.
880
881 ::
882
883 >>> subprocess.check_output(['df', '-h', '.'])
884 'Filesystem Size Used Avail Capacity Mounted on\n
885 /dev/disk0s2 52G 49G 3.0G 94% /\n'
886
887 >>> subprocess.check_output(['df', '-h', '/bogus'])
888 ...
889 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
890
891 (Contributed by Gregory P. Smith.)
892
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000893* New function: :func:`is_declared_global` in the :mod:`symtable` module
894 returns true for variables that are explicitly declared to be global,
895 false for ones that are implicitly global.
896 (Contributed by Jeremy Hylton.)
897
Benjamin Petersond23f8222009-04-05 19:13:16 +0000898* The ``sys.version_info`` value is now a named tuple, with attributes
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000899 named :attr:`major`, :attr:`minor`, :attr:`micro`,
900 :attr:`releaselevel`, and :attr:`serial`. (Contributed by Ross
901 Light; :issue:`4285`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000902
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000903 :func:`sys.getwindowsversion` also returns a named tuple,
Ezio Melotti0d85e412010-03-13 00:39:49 +0000904 with attributes named :attr:`major`, :attr:`minor`, :attr:`build`,
905 :attr:`platform`, :attr:`service_pack`, :attr:`service_pack_major`,
Eric Smithb0869402010-02-03 14:25:10 +0000906 :attr:`service_pack_minor`, :attr:`suite_mask`, and
907 :attr:`product_type`. (Contributed by Brian Curtin; :issue:`7766`.)
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000908
909* The :mod:`tarfile` module's default error handling has changed, to
910 no longer suppress fatal errors. The default error level was previously 0,
911 which meant that errors would only result in a message being written to the
912 debug log, but because the debug log is not activated by default,
913 these errors go unnoticed. The default error level is now 1,
914 which raises an exception if there's an error.
915 (Changed by Lars Gustäbel; :issue:`7357`.)
916
917 :mod:`tarfile` now supports filtering the :class:`TarInfo`
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000918 objects being added to a tar file. When you call :meth:`TarFile.add`,
919 instance, you may supply an optional *filter* argument
920 that's a callable. The *filter* callable will be passed the
921 :class:`TarInfo` for every file being added, and can modify and return it.
922 If the callable returns ``None``, the file will be excluded from the
923 resulting archive. This is more powerful than the existing
924 *exclude* argument, which has therefore been deprecated.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000925 (Added by Lars Gustäbel; :issue:`6856`.)
Benjamin Peterson9895f912010-03-21 22:05:32 +0000926 The :class:`TarFile` class also now supports the context manager protocol.
927 (Added by Lars Gustäbel; :issue:`7232`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000928
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000929* The :mod:`threading` module's :meth:`Event.wait` method now returns
930 the internal flag on exit. This means the method will usually
931 return true because :meth:`wait` is supposed to block until the
932 internal flag becomes true. The return value will only be false if
933 a timeout was provided and the operation timed out.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000934 (Contributed by Tim Lesher; :issue:`1674032`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000935
Ezio Melotti4c5475d2010-03-22 23:16:42 +0000936* The Unicode database has been updated to the version 5.2.0.
937 (Updated by Florent Xicluna; :issue:`8024`.)
938
939* The Unicode database provided by the :mod:`unicodedata` is used
940 internally to determine which characters are numeric, whitespace,
941 or represent line breaks. The database also now includes information
942 from the :file:`Unihan.txt` data file. (Patch by Anders Chrigström
Benjamin Peterson9895f912010-03-21 22:05:32 +0000943 and Amaury Forgeot d'Arc; :issue:`1571184`.)
944
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000945* The :class:`UserDict` class is now a new-style class. (Changed by
946 Benjamin Peterson.)
947
Benjamin Peterson9895f912010-03-21 22:05:32 +0000948* The ElementTree library, :mod:`xml.etree`, no longer escapes
949 ampersands and angle brackets when outputting an XML processing
950 instruction (which looks like `<?xml-stylesheet href="#style1"?>`)
951 or comment (which looks like `<!-- comment -->`).
952 (Patch by Neil Muller; :issue:`2746`.)
953
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000954* The :mod:`zipfile` module's :class:`ZipFile` now supports the context
955 management protocol, so you can write ``with zipfile.ZipFile(...) as f: ...``.
956 (Contributed by Brian Curtin; :issue:`5511`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000957
Benjamin Petersond23f8222009-04-05 19:13:16 +0000958 :mod:`zipfile` now supports archiving empty directories and
959 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000960 Reading files out of an archive is now faster, and interleaving
961 :meth:`read` and :meth:`readline` now works correctly.
962 (Contributed by Nir Aides; :issue:`7610`.)
963
964 The :func:`is_zipfile` function in the module now
965 accepts a file object, in addition to the path names accepted in earlier
966 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000967
Benjamin Peterson9895f912010-03-21 22:05:32 +0000968 The :meth:`writestr` method now has an optional *compress_type* parameter
969 that lets you override the default compression method specified in the
970 :class:`ZipFile` constructor. (Contributed by Ronald Oussoren;
971 :issue:`6003`.)
972
Tarek Ziadé396fad72010-02-23 05:30:31 +0000973* XXX the :mod:`shutil` module has now a :func:`make_archive` function
Benjamin Peterson9895f912010-03-21 22:05:32 +0000974 (see the module doc, contributed by Tarek)
975
976
977New module: sysconfig
978---------------------------------
979
980XXX A new :mod:`sysconfig` module has been extracted from
981:mod:`distutils` and put in the standard library.
982
983The :mod:`sysconfig` module provides access to Python's configuration
984information like the list of installation paths and the configuration
985variables relevant for the current platform. (contributed by Tarek)
986
987Updated module: ElementTree 1.3
988---------------------------------
989
990XXX write this.
Tarek Ziadé396fad72010-02-23 05:30:31 +0000991
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000992.. ======================================================================
993.. whole new modules get described in subsections here
994
Tarek Ziadéba0eacf2010-02-02 23:43:21 +0000995
Benjamin Peterson9895f912010-03-21 22:05:32 +0000996Distutils Enhancements
997---------------------------------
998
999Distutils is being more actively developed, thanks to Tarek Ziadé
1000who has taken over maintenance of the package, so there are a number
1001of fixes and improvements.
1002
1003A new :file:`setup.py` subcommand, ``check``, will check that the
1004arguments being passed to the :func:`setup` function are complete
1005and correct (:issue:`5732`).
1006
1007Byte-compilation by the ``install_lib`` subcommand is now only done
1008if the ``sys.dont_write_bytecode`` setting allows it (:issue:`7071`).
1009
1010:func:`distutils.sdist.add_defaults` now uses
1011*package_dir* and *data_files* to create the MANIFEST file.
1012:mod:`distutils.sysconfig` now reads the :envvar:`AR` and
1013:envvar:`ARFLAGS` environment variables.
1014
1015.. ARFLAGS done in #5941
1016
1017It is no longer mandatory to store clear-text passwords in the
1018:file:`.pypirc` file when registering and uploading packages to PyPI. As long
1019as the username is present in that file, the :mod:`distutils` package will
1020prompt for the password if not present. (Added by Tarek Ziadé,
1021based on an initial contribution by Nathan Van Gheem; :issue:`4394`.)
1022
1023A Distutils setup can now specify that a C extension is optional by
1024setting the *optional* option setting to true. If this optional is
1025supplied, failure to build the extension will not abort the build
1026process, but instead simply not install the failing extension.
1027(Contributed by Georg Brandl; :issue:`5583`.)
1028
1029The :class:`distutils.dist.DistributionMetadata` class'
1030:meth:`read_pkg_file` method will read the contents of a package's
1031:file:`PKG-INFO` metadata file. For an example of its use, see
1032:ref:`reading-metadata`.
1033(Contributed by Tarek Ziadé; :issue:`7457`.)
1034
1035:file:`setup.py` files will now accept a :option:`--no-user-cfg` switch
1036to skip reading the :file:`~/.pydistutils.cfg` file. (Suggested by
1037by Michael Hoffman, and implemented by Paul Winkler; :issue:`1180`.)
1038
1039When creating a tar-format archive, the ``sdist`` subcommand now
1040allows specifying the user id and group that will own the files in the
1041archives using the :option:`--owner` and :option:`--group` switches
1042(:issue:`6516`).
Tarek Ziadéba0eacf2010-02-02 23:43:21 +00001043
1044
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001045Unit Testing Enhancements
1046---------------------------------
1047
1048The :mod:`unittest` module was enhanced in several ways.
1049The progress messages now shows 'x' for expected failures
1050and 'u' for unexpected successes when run in verbose mode.
1051(Contributed by Benjamin Peterson.)
1052Test cases can raise the :exc:`SkipTest` exception to skip a test.
1053(:issue:`1034053`.)
1054
1055.. XXX describe test discovery (Contributed by Michael Foord; :issue:`6001`.)
1056
1057The error messages for :meth:`assertEqual`,
1058:meth:`assertTrue`, and :meth:`assertFalse`
1059failures now provide more information. If you set the
1060:attr:`longMessage` attribute of your :class:`TestCase` classes to
1061true, both the standard error message and any additional message you
1062provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
1063
1064The :meth:`assertRaises` and :meth:`failUnlessRaises` methods now
1065return a context handler when called without providing a callable
1066object to run. For example, you can write this::
1067
1068 with self.assertRaises(KeyError):
1069 raise ValueError
1070
1071(Implemented by Antoine Pitrou; :issue:`4444`.)
1072
1073The methods :meth:`addCleanup` and :meth:`doCleanups` were added.
1074:meth:`addCleanup` allows you to add cleanup functions that
1075will be called unconditionally (after :meth:`setUp` if
1076:meth:`setUp` fails, otherwise after :meth:`tearDown`). This allows
1077for much simpler resource allocation and deallocation during tests.
1078:issue:`5679`
1079
1080A number of new methods were added that provide more specialized
1081tests. Many of these methods were written by Google engineers
1082for use in their test suites; Gregory P. Smith, Michael Foord, and
1083GvR worked on merging them into Python's version of :mod:`unittest`.
1084
1085* :meth:`assertIsNone` and :meth:`assertIsNotNone` take one
1086 expression and verify that the result is or is not ``None``.
1087
1088* :meth:`assertIs` and :meth:`assertIsNot` take two values and check
1089 whether the two values evaluate to the same object or not.
1090 (Added by Michael Foord; :issue:`2578`.)
1091
Benjamin Petersona28e7022010-01-09 18:53:06 +00001092* :meth:`assertIsInstance` and :meth:`assertNotIsInstance` check whether
1093 the resulting object is an instance of a particular class, or of
1094 one of a tuple of classes. (Added by Georg Brandl; :issue:`7031`.)
1095
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001096* :meth:`assertGreater`, :meth:`assertGreaterEqual`,
1097 :meth:`assertLess`, and :meth:`assertLessEqual` compare
1098 two quantities.
1099
1100* :meth:`assertMultiLineEqual` compares two strings, and if they're
1101 not equal, displays a helpful comparison that highlights the
Benjamin Peterson9895f912010-03-21 22:05:32 +00001102 differences in the two strings. This comparison is now used by
1103 default when Unicode strings are compared with :meth:`assertEqual`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001104
1105* :meth:`assertRegexpMatches` checks whether its first argument is a
1106 string matching a regular expression provided as its second argument.
1107
1108* :meth:`assertRaisesRegexp` checks whether a particular exception
1109 is raised, and then also checks that the string representation of
1110 the exception matches the provided regular expression.
1111
1112* :meth:`assertIn` and :meth:`assertNotIn` tests whether
1113 *first* is or is not in *second*.
1114
Michael Foordabd91d52010-03-20 18:09:14 +00001115* :meth:`assertItemsEqual` tests whether two provided sequences
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001116 contain the same elements.
1117
1118* :meth:`assertSetEqual` compares whether two sets are equal, and
1119 only reports the differences between the sets in case of error.
1120
1121* Similarly, :meth:`assertListEqual` and :meth:`assertTupleEqual`
Benjamin Peterson9895f912010-03-21 22:05:32 +00001122 compare the specified types and explain any differences without necessarily
1123 printing their full values; these methods are now used by default
1124 when comparing lists and tuples using :meth:`assertEqual`.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001125 More generally, :meth:`assertSequenceEqual` compares two sequences
1126 and can optionally check whether both sequences are of a
1127 particular type.
1128
1129* :meth:`assertDictEqual` compares two dictionaries and reports the
Benjamin Peterson9895f912010-03-21 22:05:32 +00001130 differences; it's now used by default when you compare two dictionaries
1131 using :meth:`assertEqual`. :meth:`assertDictContainsSubset` checks whether
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001132 all of the key/value pairs in *first* are found in *second*.
1133
Benjamin Peterson9895f912010-03-21 22:05:32 +00001134* :meth:`assertAlmostEqual` and :meth:`assertNotAlmostEqual` test
1135 whether *first* and *second* are approximately equal by computing
1136 their difference, rounding the result to an optionally-specified number
1137 of *places* (the default is 7), and comparing to zero.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001138
1139* :meth:`loadTestsFromName` properly honors the ``suiteClass`` attribute of
1140 the :class:`TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.)
1141
1142* A new hook, :meth:`addTypeEqualityFunc` takes a type object and a
1143 function. The :meth:`assertEqual` method will use the function
1144 when both of the objects being compared are of the specified type.
1145 This function should compare the two objects and raise an
1146 exception if they don't match; it's a good idea for the function
1147 to provide additional information about why the two objects are
1148 matching, much as the new sequence comparison methods do.
1149
1150:func:`unittest.main` now takes an optional ``exit`` argument.
1151If False ``main`` doesn't call :func:`sys.exit` allowing it to
1152be used from the interactive interpreter. :issue:`3379`.
1153
1154:class:`TestResult` has new :meth:`startTestRun` and
1155:meth:`stopTestRun` methods; called immediately before
1156and after a test run. :issue:`5728` by Robert Collins.
1157
1158With all these changes, the :file:`unittest.py` was becoming awkwardly
1159large, so the module was turned into a package and the code split into
1160several files (by Benjamin Peterson). This doesn't affect how the
1161module is imported.
1162
1163
1164.. _importlib-section:
1165
Benjamin Petersond23f8222009-04-05 19:13:16 +00001166importlib: Importing Modules
1167------------------------------
1168
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001169Python 3.1 includes the :mod:`importlib` package, a re-implementation
1170of the logic underlying Python's :keyword:`import` statement.
1171:mod:`importlib` is useful for implementors of Python interpreters and
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001172to users who wish to write new importers that can participate in the
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001173import process. Python 2.7 doesn't contain the complete
1174:mod:`importlib` package, but instead has a tiny subset that contains
1175a single function, :func:`import_module`.
1176
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001177``import_module(name, package=None)`` imports a module. *name* is
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001178a string containing the module or package's name. It's possible to do
1179relative imports by providing a string that begins with a ``.``
1180character, such as ``..utils.errors``. For relative imports, the
1181*package* argument must be provided and is the name of the package that
1182will be used as the anchor for
1183the relative import. :func:`import_module` both inserts the imported
1184module into ``sys.modules`` and returns the module object.
1185
1186Here are some examples::
1187
1188 >>> from importlib import import_module
1189 >>> anydbm = import_module('anydbm') # Standard absolute import
1190 >>> anydbm
1191 <module 'anydbm' from '/p/python/Lib/anydbm.py'>
1192 >>> # Relative import
1193 >>> sysconfig = import_module('..sysconfig', 'distutils.command')
1194 >>> sysconfig
1195 <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'>
1196
1197:mod:`importlib` was implemented by Brett Cannon and introduced in
1198Python 3.1.
1199
Benjamin Petersond23f8222009-04-05 19:13:16 +00001200
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00001201ttk: Themed Widgets for Tk
1202--------------------------
1203
1204Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
1205widgets but have a more customizable appearance and can therefore more
1206closely resemble the native platform's widgets. This widget
1207set was originally called Tile, but was renamed to Ttk (for "themed Tk")
1208on being added to Tcl/Tck release 8.5.
1209
1210XXX write a brief discussion and an example here.
1211
1212The :mod:`ttk` module was written by Guilherme Polo and added in
1213:issue:`2983`. An alternate version called ``Tile.py``, written by
1214Martin Franklin and maintained by Kevin Walzer, was proposed for
1215inclusion in :issue:`2618`, but the authors argued that Guilherme
1216Polo's work was more comprehensive.
1217
Georg Brandl4d131ee2009-11-18 18:53:14 +00001218
1219Deprecations and Removals
1220=========================
1221
1222* :func:`contextlib.nested`, which allows handling more than one context manager
1223 with one :keyword:`with` statement, has been deprecated; :keyword:`with`
1224 supports multiple context managers syntactically now.
1225
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001226.. ======================================================================
1227
1228
1229Build and C API Changes
1230=======================
1231
1232Changes to Python's build process and to the C API include:
1233
Georg Brandl1f01deb2009-01-03 22:47:39 +00001234* If you use the :file:`.gdbinit` file provided with Python,
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001235 the "pyo" macro in the 2.7 version now works correctly when the thread being
1236 debugged doesn't hold the GIL; the macro now acquires it before printing.
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001237 (Contributed by Victor Stinner; :issue:`3632`.)
1238
Benjamin Petersond23f8222009-04-05 19:13:16 +00001239* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001240 worker thread submit notifications to the main Python thread. This
1241 is particularly useful for asynchronous IO operations.
1242 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
1243
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001244* New function: :cfunc:`PyCode_NewEmpty` creates an empty code object;
1245 only the filename, function name, and first line number are required.
1246 This is useful to extension modules that are attempting to
1247 construct a more useful traceback stack. Previously such
1248 extensions needed to call :cfunc:`PyCode_New`, which had many
1249 more arguments. (Added by Jeffrey Yasskin.)
1250
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001251* New function: :cfunc:`PyErr_NewExceptionWithDoc` creates a new
1252 exception class, just as the existing :cfunc:`PyErr_NewException` does,
1253 but takes an extra ``char *`` argument containing the docstring for the
1254 new exception class. (Added by the 'lekma' user on the Python bug tracker;
1255 :issue:`7033`.)
1256
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001257* New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object
1258 and returns the line number that the frame is currently executing.
1259 Previously code would need to get the index of the bytecode
1260 instruction currently executing, and then look up the line number
1261 corresponding to that address. (Added by Jeffrey Yasskin.)
1262
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001263* New functions: :cfunc:`PyLong_AsLongAndOverflow` and
1264 :cfunc:`PyLong_AsLongLongAndOverflow` approximates a Python long
1265 integer as a C :ctype:`long` or :ctype:`long long`.
1266 If the number is too large to fit into
1267 the output type, an *overflow* flag is set and returned to the caller.
1268 (Contributed by Case Van Horsen; :issue:`7528` and :issue:`7767`.)
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001269
Benjamin Petersona28e7022010-01-09 18:53:06 +00001270* New function: stemming from the rewrite of string-to-float conversion,
1271 a new :cfunc:`PyOS_string_to_double` function was added. The old
1272 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions
1273 are now deprecated.
1274
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001275* New macros: the Python header files now define the following macros:
1276 :cmacro:`Py_ISALNUM`,
1277 :cmacro:`Py_ISALPHA`,
1278 :cmacro:`Py_ISDIGIT`,
1279 :cmacro:`Py_ISLOWER`,
1280 :cmacro:`Py_ISSPACE`,
1281 :cmacro:`Py_ISUPPER`,
1282 :cmacro:`Py_ISXDIGIT`,
1283 and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`.
1284 All of these functions are analogous to the C
1285 standard macros for classifying characters, but ignore the current
1286 locale setting, because in
1287 several places Python needs to analyze characters in a
1288 locale-independent way. (Added by Eric Smith;
1289 :issue:`5793`.)
1290
1291 .. XXX these macros don't seem to be described in the c-api docs.
1292
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001293* New format codes: the :cfunc:`PyFormat_FromString`,
1294 :cfunc:`PyFormat_FromStringV`, and :cfunc:`PyErr_Format` now
1295 accepts ``%lld`` and ``%llu`` format codes for displaying values of
1296 C's :ctype:`long long` types.
1297 (Contributed by Mark Dickinson; :issue:`7228`.)
1298
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001299* The complicated interaction between threads and process forking has
1300 been changed. Previously, the child process created by
1301 :func:`os.fork` might fail because the child is created with only a
1302 single thread running, the thread performing the :func:`os.fork`.
1303 If other threads were holding a lock, such as Python's import lock,
1304 when the fork was performed, the lock would still be marked as
1305 "held" in the new process. But in the child process nothing would
1306 ever release the lock, since the other threads weren't replicated,
1307 and the child process would no longer be able to perform imports.
1308
1309 Python 2.7 now acquires the import lock before performing an
1310 :func:`os.fork`, and will also clean up any locks created using the
1311 :mod:`threading` module. C extension modules that have internal
1312 locks, or that call :cfunc:`fork()` themselves, will not benefit
1313 from this clean-up.
1314
1315 (Fixed by Thomas Wouters; :issue:`1590864`.)
1316
Benjamin Petersona28e7022010-01-09 18:53:06 +00001317* The :cfunc:`Py_Finalize` function now calls the internal
1318 :func:`threading._shutdown` function; this prevents some exceptions from
1319 being raised when an interpreter shuts down.
1320 (Patch by Adam Olsen; :issue:`1722344`.)
1321
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001322* Global symbols defined by the :mod:`ctypes` module are now prefixed
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001323 with ``Py``, or with ``_ctypes``. (Implemented by Thomas
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001324 Heller; :issue:`3102`.)
1325
Benjamin Petersona28e7022010-01-09 18:53:06 +00001326* New configure option: the :option:`--with-system-expat` switch allows
1327 building the :mod:`pyexpat` module to use the system Expat library.
1328 (Contributed by Arfrever Frehtes Taifersar Arahesis; :issue:`7609`.)
1329
1330* New configure option: Compiling Python with the
1331 :option:`--with-valgrind` option will now disable the pymalloc
1332 allocator, which is difficult for the Valgrind to analyze correctly.
1333 Valgrind will therefore be better at detecting memory leaks and
1334 overruns. (Contributed by James Henstridge; :issue:`2422`.)
1335
1336* New configure option: you can now supply no arguments to
1337 :option:`--with-dbmliborder=` in order to build none of the various
1338 DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis;
1339 :issue:`6491`.)
1340
Benjamin Petersond23f8222009-04-05 19:13:16 +00001341* The :program:`configure` script now checks for floating-point rounding bugs
1342 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
1343 preprocessor definition. No code currently uses this definition,
1344 but it's available if anyone wishes to use it.
1345 (Added by Mark Dickinson; :issue:`2937`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001346
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001347* The build process now creates the necessary files for pkg-config
1348 support. (Contributed by Clinton Roy; :issue:`3585`.)
1349
1350* The build process now supports Subversion 1.7. (Contributed by
1351 Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.)
1352
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001353
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001354.. ======================================================================
1355
1356Port-Specific Changes: Windows
1357-----------------------------------
1358
Georg Brandl1f01deb2009-01-03 22:47:39 +00001359* The :mod:`msvcrt` module now contains some constants from
1360 the :file:`crtassem.h` header file:
1361 :data:`CRT_ASSEMBLY_VERSION`,
1362 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
1363 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001364 (Contributed by David Cournapeau; :issue:`4365`.)
1365
1366* The new :cfunc:`_beginthreadex` API is used to start threads, and
1367 the native thread-local storage functions are now used.
1368 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001369
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001370* The :func:`os.listdir` function now correctly fails
1371 for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.)
1372
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001373* The :mod:`mimelib` module will now read the MIME database from
1374 the Windows registry when initializing.
1375 (Patch by Gabriel Genellina; :issue:`4969`.)
1376
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001377.. ======================================================================
1378
1379Port-Specific Changes: Mac OS X
1380-----------------------------------
1381
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001382* The path ``/Library/Python/2.7/site-packages`` is now appended to
Benjamin Petersond23f8222009-04-05 19:13:16 +00001383 ``sys.path``, in order to share added packages between the system
1384 installation and a user-installed copy of the same version.
1385 (Changed by Ronald Oussoren; :issue:`4865`.)
1386
1387
1388Other Changes and Fixes
1389=======================
1390
Benjamin Peterson9895f912010-03-21 22:05:32 +00001391* Two benchmark scripts, :file:`iobench` and :file:`ccbench`, were
1392 added to the :file:`Tools` directory. :file:`iobench` measures the
1393 speed of built-in file I/O objects (as returned by :func:`open`)
1394 while performing various operations, and :file:`ccbench` is a
1395 concurrency benchmark that tries to measure computing throughput,
1396 thread switching latency, and IO processing bandwidth when
1397 performing several tasks using a varying number of threads.
1398
Benjamin Petersond23f8222009-04-05 19:13:16 +00001399* When importing a module from a :file:`.pyc` or :file:`.pyo` file
1400 with an existing :file:`.py` counterpart, the :attr:`co_filename`
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001401 attributes of the resulting code objects are overwritten when the
1402 original filename is obsolete. This can happen if the file has been
1403 renamed, moved, or is accessed through different paths. (Patch by
1404 Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +00001405
1406* The :file:`regrtest.py` script now takes a :option:`--randseed=`
1407 switch that takes an integer that will be used as the random seed
1408 for the :option:`-r` option that executes tests in random order.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001409 The :option:`-r` option also reports the seed that was used
Benjamin Petersond23f8222009-04-05 19:13:16 +00001410 (Added by Collin Winter.)
1411
Benjamin Petersona28e7022010-01-09 18:53:06 +00001412* Another :file:`regrtest.py` switch is :option:`-j`, which
1413 takes an integer specifying how many tests run in parallel. This
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001414 allows reducing the total runtime on multi-core machines.
Antoine Pitrou88909542009-06-29 13:54:42 +00001415 This option is compatible with several other options, including the
1416 :option:`-R` switch which is known to produce long runtimes.
Benjamin Petersona28e7022010-01-09 18:53:06 +00001417 (Added by Antoine Pitrou, :issue:`6152`.) This can also be used
1418 with a new :option:`-F` switch that runs selected tests in a loop
1419 until they fail. (Added by Antoine Pitrou; :issue:`7312`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001420
1421.. ======================================================================
1422
1423Porting to Python 2.7
1424=====================
1425
1426This section lists previously described changes and other bugfixes
1427that may require changes to your code:
1428
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001429* When using :class:`Decimal` instances with a string's
1430 :meth:`format` method, the default alignment was previously
1431 left-alignment. This has been changed to right-alignment, which might
1432 change the output of your programs.
1433 (Changed by Mark Dickinson; :issue:`6857`.)
1434
1435 Another :meth:`format`-related change: the default precision used
1436 for floating-point and complex numbers was changed from 6 decimal
1437 places to 12, which matches the precision used by :func:`str`.
1438 (Changed by Eric Smith; :issue:`5920`.)
1439
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001440* Because of an optimization for the :keyword:`with` statement, the special
1441 methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's
1442 type, and cannot be directly attached to the object's instance. This
1443 affects new-style classes (derived from :class:`object`) and C extension
1444 types. (:issue:`6101`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001445
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001446* The :meth:`readline` method of :class:`StringIO` objects now does
1447 nothing when a negative length is requested, as other file-like
1448 objects do. (:issue:`7348`).
1449
Benjamin Peterson9895f912010-03-21 22:05:32 +00001450In the standard library:
1451
1452* The ElementTree library, :mod:`xml.etree`, no longer escapes
1453 ampersands and angle brackets when outputting an XML processing
1454 instruction (which looks like `<?xml-stylesheet href="#style1"?>`)
1455 or comment (which looks like `<!-- comment -->`).
1456 (Patch by Neil Muller; :issue:`2746`.)
1457
Benjamin Petersona28e7022010-01-09 18:53:06 +00001458For C extensions:
1459
1460* C extensions that use integer format codes with the ``PyArg_Parse*``
1461 family of functions will now raise a :exc:`TypeError` exception
1462 instead of triggering a :exc:`DeprecationWarning` (:issue:`5080`).
1463
1464* Use the new :cfunc:`PyOS_string_to_double` function instead of the old
1465 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions,
1466 which are now deprecated.
1467
1468
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001469.. ======================================================================
1470
1471
1472.. _acks27:
1473
1474Acknowledgements
1475================
1476
1477The author would like to thank the following people for offering
1478suggestions, corrections and assistance with various drafts of this
Benjamin Peterson97dd9872009-12-13 01:23:39 +00001479article: Ryan Lovett, Hugh Secker-Walker.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001480