blob: edc1f0d5a9cee18a9357fc21b55deb24729d8325 [file] [log] [blame]
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001****************************
2 What's New in Python 2.7
3****************************
4
5:Author: A.M. Kuchling (amk at amk.ca)
6:Release: |release|
7:Date: |today|
8
Andrew M. Kuchling039c8992010-02-01 02:04:26 +00009.. Fix accents on Kristjan Valur Jonsson, Fuerstenau
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +000010
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +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
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +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`.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000055
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +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
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000061.. Compare with previous release in 2 - 3 sentences here.
62 add hyperlink when the documentation becomes available online.
63
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000064.. _whatsnew27-python31:
65
66Python 3.1 Features
67=======================
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +000068
69Much as Python 2.6 incorporated features from Python 3.0,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +000073
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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`.
Andrew M. Kuchling8f254e72009-12-08 02:37:05 +000078* The new format specifier described in :ref:`pep-0378`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000079* The :class:`memoryview` object.
80* A small subset of the :mod:`importlib` module `described below <#importlib-section>`__.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +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.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000091Other new Python3-mode warnings include:
92
93* :func:`operator.isCallable` and :func:`operator.sequenceIncludes`,
94 which are not supported in 3.x.
95
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000096.. ========================================================================
97.. Large, PEP-level features and changes should be described here.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000098.. ========================================================================
99
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000100.. _pep-0372:
101
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000102PEP 372: Adding an ordered dictionary to collections
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000103====================================================
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000104
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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.
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000110
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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::
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000114
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchling7fe65a02009-10-13 15:49:33 +0000184.. 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.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000189
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
210 >>> '{:20,.2}'.format(f)
211 '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
Andrew M. Kuchling85ea4bf2009-10-05 22:45:39 +0000218.. XXX "Format String Syntax" in string.rst could use many more examples.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000219
220.. seealso::
221
222 :pep:`378` - Format Specifier for Thousands Separator
223 PEP written by Raymond Hettinger; implemented by Eric Smith.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000224
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000225PEP 391: Dictionary-Based Configuration For Logging
226====================================================
227
228XXX write this section.
229
230.. seealso::
231
232 :pep:`391` - Dictionary-Based Configuration For Logging
233 PEP written and implemented by Vinay Sajip.
234
235PEP 3106: Dictionary Views
236====================================================
237
238XXX write this section.
239
240.. seealso::
241
242 :pep:`3106` - Revamping dict.keys(), .values() and .items()
243 PEP written by Guido van Rossum.
244 Backported to 2.7 by Alexandre Vassalotti; :issue:`1967`.
245
246
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000247Other Language Changes
248======================
249
250Some smaller changes made to the core Python language are:
251
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000252* The syntax for set literals has been backported from Python 3.x.
253 Curly brackets are used to surround the contents of the resulting
254 mutable set; set literals are
255 distinguished from dictionaries by not containing colons and values.
256 ``{}`` continues to represent an empty dictionary; use
257 ``set()`` for an empty set.
258
259 >>> {1,2,3,4,5}
260 set([1, 2, 3, 4, 5])
261 >>> set()
262 set([])
263 >>> {}
264 {}
265
266 Backported by Alexandre Vassalotti; :issue:`2335`.
267
268* Dictionary and set comprehensions are another feature backported from
269 3.x, generalizing list/generator comprehensions to use
270 the literal syntax for sets and dictionaries.
271
272 >>> {x:x*x for x in range(6)}
273 {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
274 >>> {'a'*x for x in range(6)}
275 set(['', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa'])
276
277 Backported by Alexandre Vassalotti; :issue:`2333`.
278
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000279* The :keyword:`with` statement can now use multiple context managers
280 in one statement. Context managers are processed from left to right
281 and each one is treated as beginning a new :keyword:`with` statement.
282 This means that::
283
284 with A() as a, B() as b:
285 ... suite of statements ...
286
287 is equivalent to::
288
289 with A() as a:
290 with B() as b:
291 ... suite of statements ...
292
293 The :func:`contextlib.nested` function provides a very similar
294 function, so it's no longer necessary and has been deprecated.
295
296 (Proposed in http://codereview.appspot.com/53094; implemented by
297 Georg Brandl.)
298
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000299* Conversions between floating-point numbers and strings are
300 now correctly rounded on most platforms. These conversions occur
301 in many different places: :func:`str` on
302 floats and complex numbers; the :class:`float` and :class:`complex`
303 constructors;
304 numeric formatting; serialization and
305 deserialization of floats and complex numbers using the
306 :mod:`marshal`, :mod:`pickle`
307 and :mod:`json` modules;
308 parsing of float and imaginary literals in Python code;
309 and :class:`Decimal`-to-float conversion.
310
311 Related to this, the :func:`repr` of a floating-point number *x*
312 now returns a result based on the shortest decimal string that's
313 guaranteed to round back to *x* under correct rounding (with
314 round-half-to-even rounding mode). Previously it gave a string
315 based on rounding x to 17 decimal digits.
316
317 The rounding library responsible for this improvement works on
318 Windows, and on Unix platforms using the gcc, icc, or suncc
319 compilers. There may be a small number of platforms where correct
320 operation of this code cannot be guaranteed, so the code is not
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +0000321 used on such systems. You can find out which code is being used
322 by checking :data:`sys.float_repr_style`, which will be ``short``
323 if the new code is in use and ``legacy`` if it isn't.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000324
Mark Dickinsonbdd863d2010-01-07 09:28:29 +0000325 Implemented by Eric Smith and Mark Dickinson, using David Gay's
326 :file:`dtoa.c` library; :issue:`7117`.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000327
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000328* The :meth:`str.format` method now supports automatic numbering of the replacement
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +0000329 fields. This makes using :meth:`str.format` more closely resemble using
330 ``%s`` formatting::
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000331
332 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
333 '2009:4:Sunday'
334 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
335 '2009:4:Sunday'
336
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +0000337 The auto-numbering takes the fields from left to right, so the first ``{...}``
338 specifier will use the first argument to :meth:`str.format`, the next
339 specifier will use the next argument, and so on. You can't mix auto-numbering
340 and explicit numbering -- either number all of your specifier fields or none
341 of them -- but you can mix auto-numbering and named fields, as in the second
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000342 example above. (Contributed by Eric Smith; :issue:`5237`.)
343
344 Complex numbers now correctly support usage with :func:`format`.
345 Specifying a precision or comma-separation applies to both the real
346 and imaginary parts of the number, but a specified field width and
347 alignment is applied to the whole of the resulting ``1.5+3j``
348 output. (Contributed by Eric Smith; :issue:`1588`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000349
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000350 The 'F' format code now always formats its output using uppercase characters,
351 so it will now produce 'INF' and 'NAN'.
352 (Contributed by Eric Smith; :issue:`3382`.)
353
Mark Dickinson1a707982008-12-17 16:14:37 +0000354* The :func:`int` and :func:`long` types gained a ``bit_length``
Georg Brandl64e1c752009-04-11 18:19:27 +0000355 method that returns the number of bits necessary to represent
Mark Dickinson1a707982008-12-17 16:14:37 +0000356 its argument in binary::
357
358 >>> n = 37
359 >>> bin(37)
360 '0b100101'
361 >>> n.bit_length()
362 6
363 >>> n = 2**123-1
364 >>> n.bit_length()
365 123
366 >>> (n+1).bit_length()
367 124
368
369 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
370
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000371* Conversions from long integers and regular integers to floating
372 point now round differently, returning the floating-point number
373 closest to the number. This doesn't matter for small integers that
374 can be converted exactly, but for large numbers that will
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000375 unavoidably lose precision, Python 2.7 now approximates more
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000376 closely. For example, Python 2.6 computed the following::
377
378 >>> n = 295147905179352891391
379 >>> float(n)
380 2.9514790517935283e+20
381 >>> n - long(float(n))
382 65535L
383
384 Python 2.7's floating-point result is larger, but much closer to the
385 true value::
386
387 >>> n = 295147905179352891391
388 >>> float(n)
389 2.9514790517935289e+20
390 >>> n-long(float(n)
391 ... )
392 -1L
393
394 (Implemented by Mark Dickinson; :issue:`3166`.)
395
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000396 Integer division is also more accurate in its rounding behaviours. (Also
397 implemented by Mark Dickinson; :issue:`1811`.)
398
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000399* The :class:`bytearray` type's :meth:`translate` method now accepts
400 ``None`` as its first argument. (Fixed by Georg Brandl;
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000401 :issue:`4759`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000402
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000403* When using ``@classmethod`` and ``@staticmethod`` to wrap
404 methods as class or static methods, the wrapper object now
405 exposes the wrapped function as their :attr:`__func__` attribute.
406 (Contributed by Amaury Forgeot d'Arc, after a suggestion by
407 George Sakkis; :issue:`5982`.)
408
409* A new encoding named "cp720", used primarily for Arabic text, is now
410 supported. (Contributed by Alexander Belchenko and Amaury Forgeot
411 d'Arc; :issue:`1616979`.)
412
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000413* The :class:`file` object will now set the :attr:`filename` attribute
414 on the :exc:`IOError` exception when trying to open a directory
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +0000415 on POSIX platforms (noted by Jan Kaliszewski; :issue:`4764`), and
416 now explicitly checks for and forbids writing to read-only file objects
417 instead of trusting the C library to catch and report the error
418 (fixed by Stefan Krah; :issue:`5677`).
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000419
Benjamin Petersonae9a0a02009-12-31 16:49:37 +0000420* The Python tokenizer now translates line endings itself, so the
421 :func:`compile` built-in function can now accept code using any
422 line-ending convention. Additionally, it no longer requires that the
423 code end in a newline.
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +0000424
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000425* Extra parentheses in function definitions are illegal in Python 3.x,
426 meaning that you get a syntax error from ``def f((x)): pass``. In
427 Python3-warning mode, Python 2.7 will now warn about this odd usage.
428 (Noted by James Lingard; :issue:`7362`.)
429
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000430* When a module object is garbage-collected, the module's dictionary is
431 now only cleared if no one else is holding a reference to the
432 dictionary (:issue:`7140`).
433
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000434.. ======================================================================
435
436
437Optimizations
438-------------
439
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000440Several performance enhancements have been added:
441
442.. * A new :program:`configure` option, :option:`--with-computed-gotos`,
443 compiles the main bytecode interpreter loop using a new dispatch
444 mechanism that gives speedups of up to 20%, depending on the system
445 and benchmark. The new mechanism is only supported on certain
446 compilers, such as gcc, SunPro, and icc.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000447
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000448* A new opcode was added to perform the initial setup for
449 :keyword:`with` statements, looking up the :meth:`__enter__` and
450 :meth:`__exit__` methods. (Contributed by Benjamin Peterson.)
451
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000452* The garbage collector now performs better for one common usage
453 pattern: when many objects are being allocated without deallocating
454 any of them. This would previously take quadratic
455 time for garbage collection, but now the number of full garbage collections
456 is reduced as the number of objects on the heap grows.
457 The new logic is to only perform a full garbage collection pass when
458 the middle generation has been collected 10 times and when the
459 number of survivor objects from the middle generation exceeds 10% of
460 the number of objects in the oldest generation. (Suggested by Martin
461 von Loewis and implemented by Antoine Pitrou; :issue:`4074`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000462
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000463* The garbage collector tries to avoid tracking simple containers
464 which can't be part of a cycle. In Python 2.7, this is now true for
465 tuples and dicts containing atomic types (such as ints, strings,
466 etc.). Transitively, a dict containing tuples of atomic types won't
467 be tracked either. This helps reduce the cost of each
468 garbage collection by decreasing the number of objects to be
469 considered and traversed by the collector.
Antoine Pitrouc18f6b02009-03-28 19:10:13 +0000470 (Contributed by Antoine Pitrou; :issue:`4688`.)
471
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000472* Long integers are now stored internally either in base 2**15 or in base
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000473 2**30, the base being determined at build time. Previously, they
474 were always stored in base 2**15. Using base 2**30 gives
475 significant performance improvements on 64-bit machines, but
476 benchmark results on 32-bit machines have been mixed. Therefore,
477 the default is to use base 2**30 on 64-bit machines and base 2**15
478 on 32-bit machines; on Unix, there's a new configure option
479 :option:`--enable-big-digits` that can be used to override this default.
480
481 Apart from the performance improvements this change should be
482 invisible to end users, with one exception: for testing and
483 debugging purposes there's a new structseq ``sys.long_info`` that
484 provides information about the internal format, giving the number of
485 bits per digit and the size in bytes of the C type used to store
486 each digit::
487
488 >>> import sys
489 >>> sys.long_info
490 sys.long_info(bits_per_digit=30, sizeof_digit=4)
491
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000492 (Contributed by Mark Dickinson; :issue:`4258`.)
493
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000494 Another set of changes made long objects a few bytes smaller: 2 bytes
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000495 smaller on 32-bit systems and 6 bytes on 64-bit.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000496 (Contributed by Mark Dickinson; :issue:`5260`.)
497
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000498* The division algorithm for long integers has been made faster
499 by tightening the inner loop, doing shifts instead of multiplications,
500 and fixing an unnecessary extra iteration.
501 Various benchmarks show speedups of between 50% and 150% for long
502 integer divisions and modulo operations.
503 (Contributed by Mark Dickinson; :issue:`5512`.)
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +0000504 Bitwise operations are also significantly faster (initial patch by
505 Gregory Smith; :issue:`1087418`).
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000506
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000507* The implementation of ``%`` checks for the left-side operand being
508 a Python string and special-cases it; this results in a 1-3%
509 performance increase for applications that frequently use ``%``
510 with strings, such as templating libraries.
511 (Implemented by Collin Winter; :issue:`5176`.)
512
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000513* List comprehensions with an ``if`` condition are compiled into
514 faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
515 by Jeffrey Yasskin; :issue:`4715`.)
516
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +0000517* Converting an integer or long integer to a decimal string was made
518 faster by special-casing base 10 instead of using a generalized
519 conversion function that supports arbitrary bases.
520 (Patch by Gawain Bolton; :issue:`6713`.)
521
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000522* The :meth:`split`, :meth:`replace`, :meth:`rindex`,
523 :meth:`rpartition`, and :meth:`rsplit` methods of string-like types
524 (strings, Unicode strings, and :class:`bytearray` objects) now use a
525 fast reverse-search algorithm instead of a character-by-character
526 scan. This is sometimes faster by a factor of 10. (Added by
527 Florent Xicluna; :issue:`7462` and :issue:`7622`.)
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +0000528
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000529* The :mod:`pickle` and :mod:`cPickle` modules now automatically
530 intern the strings used for attribute names, reducing memory usage
531 of the objects resulting from unpickling. (Contributed by Jake
532 McGuire; :issue:`5084`.)
533
534* The :mod:`cPickle` module now special-cases dictionaries,
535 nearly halving the time required to pickle them.
536 (Contributed by Collin Winter; :issue:`5670`.)
537
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000538.. ======================================================================
539
Georg Brandl0516f812009-11-18 18:52:35 +0000540New and Improved Modules
541========================
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000542
543As in every release, Python's standard library received a number of
544enhancements and bug fixes. Here's a partial list of the most notable
545changes, sorted alphabetically by module name. Consult the
546:file:`Misc/NEWS` file in the source tree for a more complete list of
547changes, or look through the Subversion logs for all the details.
548
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000549* The :mod:`bdb` module's base debugging class :class:`Bdb`
550 gained a feature for skipping modules. The constructor
551 now takes an iterable containing glob-style patterns such as
552 ``django.*``; the debugger will not step into stack frames
553 from a module that matches one of these patterns.
554 (Contributed by Maru Newby after a suggestion by
555 Senthil Kumaran; :issue:`5142`.)
556
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000557* The :mod:`bz2` module's :class:`BZ2File` now supports the context
558 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
559 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
560
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000561* New class: the :class:`Counter` class in the :mod:`collections` module is
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000562 useful for tallying data. :class:`Counter` instances behave mostly
563 like dictionaries but return zero for missing keys instead of
Georg Brandlf6dab952009-04-28 21:48:35 +0000564 raising a :exc:`KeyError`:
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000565
Georg Brandlf6dab952009-04-28 21:48:35 +0000566 .. doctest::
567 :options: +NORMALIZE_WHITESPACE
568
569 >>> from collections import Counter
570 >>> c = Counter()
571 >>> for letter in 'here is a sample of english text':
572 ... c[letter] += 1
573 ...
574 >>> c
575 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
576 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
577 'p': 1, 'r': 1, 'x': 1})
578 >>> c['e']
579 5
580 >>> c['z']
581 0
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000582
583 There are two additional :class:`Counter` methods: :meth:`most_common`
584 returns the N most common elements and their counts, and :meth:`elements`
585 returns an iterator over the contained element, repeating each element
586 as many times as its count::
587
588 >>> c.most_common(5)
589 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
590 >>> c.elements() ->
591 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
592 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
593 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
Georg Brandlf6dab952009-04-28 21:48:35 +0000594 's', 's', 'r', 't', 't', 'x'
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000595
596 Contributed by Raymond Hettinger; :issue:`1696199`.
597
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000598 The new `OrderedDict` class is described in the earlier section
599 :ref:`pep-0372`.
600
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000601 The :class:`namedtuple` class now has an optional *rename* parameter.
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000602 If *rename* is true, field names that are invalid because they've
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000603 been repeated or that aren't legal Python identifiers will be
604 renamed to legal names that are derived from the field's
605 position within the list of fields:
606
Georg Brandlf6dab952009-04-28 21:48:35 +0000607 >>> from collections import namedtuple
608 >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000609 >>> T._fields
610 ('field1', '_1', '_2', 'field2')
611
612 (Added by Raymond Hettinger; :issue:`1818`.)
613
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000614 The :class:`deque` data type now exposes its maximum length as the
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000615 read-only :attr:`maxlen` attribute, and has a
616 :meth:`reverse` method that reverses the elements of the deque in-place.
617 (Added by Raymond Hettinger.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000618
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000619* The :mod:`copy` module's :func:`deepcopy` function will now
620 correctly copy bound instance methods. (Implemented by
621 Robert Collins; :issue:`1515`.)
622
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000623* The :mod:`ctypes` module now always converts ``None`` to a C NULL
624 pointer for arguments declared as pointers. (Changed by Thomas
625 Heller; :issue:`4606`.)
626
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000627* New method: the :mod:`datetime` module's :class:`timedelta` class
628 gained a :meth:`total_seconds` method that returns the number of seconds
629 in the duration. (Contributed by Brian Quinlan; :issue:`5788`.)
630
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000631* New method: the :class:`Decimal` class gained a
632 :meth:`from_float` class method that performs an exact conversion
633 of a floating-point number to a :class:`Decimal`.
634 Note that this is an **exact** conversion that strives for the
635 closest decimal approximation to the floating-point representation's value;
636 the resulting decimal value will therefore still include the inaccuracy,
637 if any.
638 For example, ``Decimal.from_float(0.1)`` returns
639 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
640 (Implemented by Raymond Hettinger; :issue:`4796`.)
641
642 The constructor for :class:`Decimal` now accepts non-European
643 Unicode characters, such as Arabic-Indic digits. (Contributed by
644 Mark Dickinson; :issue:`6595`.)
645
646 When using :class:`Decimal` instances with a string's
647 :meth:`format` method, the default alignment was previously
648 left-alignment. This has been changed to right-alignment, which seems
649 more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.)
650
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000651* The :class:`Fraction` class now accepts two rational numbers
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000652 as arguments to its constructor.
653 (Implemented by Mark Dickinson; :issue:`5812`.)
654
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000655* The :mod:`ftplib` module gained the ability to establish secure FTP
656 connections using TLS encapsulation of authentication as well as
657 subsequent control and data transfers. This is provided by the new
658 :class:`ftplib.FTP_TLS` class.
659 (Contributed by Giampaolo Rodola', :issue:`2054`.) The :meth:`storbinary`
660 method for binary uploads can now restart uploads thanks to an added
661 *rest* parameter (patch by Pablo Mouzo; :issue:`6845`.)
662
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000663* New function: the :mod:`gc` module's :func:`is_tracked` returns
664 true if a given instance is tracked by the garbage collector, false
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000665 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
666
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000667* The :mod:`gzip` module's :class:`GzipFile` now supports the context
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000668 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``
669 (contributed by Hagen Fuerstenau; :issue:`3860`), and it now implements
670 the :class:`io.BufferedIOBase` ABC, so you can wrap it with
671 :class:`io.BufferedReader` for faster processing
672 (contributed by Nir Aides; :issue:`7471`).
673 It's also now possible to override the modification time
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000674 recorded in a gzipped file by providing an optional timestamp to
675 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000676
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000677 Files in gzip format can be padded with trailing zero bytes; the
678 :mod:`gzip` module will now consume these trailing bytes. (Fixed by
679 Tadek Pietraszek and Brian Curtin; :issue:`2846`.)
680
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000681* The default :class:`HTTPResponse` class used by the :mod:`httplib` module now
682 supports buffering, resulting in much faster reading of HTTP responses.
683 (Contributed by Kristjan Valur Jonsson; :issue:`4879`.)
684
685* The :mod:`imaplib` module now supports IPv6 addresses.
686 (Contributed by Derek Morr; :issue:`1655`.)
687
688* The :mod:`io` library has been upgraded to the version shipped with
689 Python 3.1. For 3.1, the I/O library was entirely rewritten in C
690 and is 2 to 20 times faster depending on the task at hand. The
691 original Python version was renamed to the :mod:`_pyio` module.
692
693 One minor resulting change: the :class:`io.TextIOBase` class now
694 has an :attr:`errors` attribute giving the error setting
695 used for encoding and decoding errors (one of ``'strict'``, ``'replace'``,
696 ``'ignore'``).
697
698 The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000699 an invalid file descriptor. (Implemented by Benjamin Peterson;
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000700 :issue:`4991`.) The :meth:`truncate` method now preserves the
701 file position; previously it would change the file position to the
702 end of the new file. (Fixed by Pascal Chambon; :issue:`6939`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000703
Andrew M. Kuchling5a73ff82009-12-02 14:27:11 +0000704* New function: ``itertools.compress(data, selectors)`` takes two
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000705 iterators. Elements of *data* are returned if the corresponding
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000706 value in *selectors* is true::
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000707
708 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
709 A, C, E, F
710
Andrew M. Kuchling5a73ff82009-12-02 14:27:11 +0000711 New function: ``itertools.combinations_with_replacement(iter, r)``
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000712 returns all the possible *r*-length combinations of elements from the
713 iterable *iter*. Unlike :func:`combinations`, individual elements
714 can be repeated in the generated combinations::
715
716 itertools.combinations_with_replacement('abc', 2) =>
717 ('a', 'a'), ('a', 'b'), ('a', 'c'),
718 ('b', 'b'), ('b', 'c'), ('c', 'c')
719
720 Note that elements are treated as unique depending on their position
721 in the input, not their actual values.
722
723 The :class:`itertools.count` function now has a *step* argument that
724 allows incrementing by values other than 1. :func:`count` also
725 now allows keyword arguments, and using non-integer values such as
726 floats or :class:`Decimal` instances. (Implemented by Raymond
727 Hettinger; :issue:`5032`.)
728
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000729 :func:`itertools.combinations` and :func:`itertools.product` were
730 previously raising :exc:`ValueError` for values of *r* larger than
731 the input iterable. This was deemed a specification error, so they
732 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
733
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000734* The :mod:`json` module was upgraded to version 2.0.9 of the
735 simplejson package, which includes a C extension that makes
736 encoding and decoding faster.
737 (Contributed by Bob Ippolito; :issue:`4136`.)
738
739 To support the new :class:`OrderedDict` type, :func:`json.load`
740 now has an optional *object_pairs_hook* parameter that will be called
741 with any object literal that decodes to a list of pairs.
742 (Contributed by Raymond Hettinger; :issue:`5381`.)
743
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000744* New functions: the :mod:`math` module gained
745 :func:`erf` and :func:`erfc` for the error function and the complementary error function,
746 :func:`expm1` which computes ``e**x - 1`` with more precision than
747 using :func:`exp` and subtracting 1,
748 :func:`gamma` for the Gamma function, and
749 :func:`lgamma` for the natural log of the Gamma function.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000750 (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.)
751
Andrew M. Kuchling24520b42009-04-09 11:22:47 +0000752* The :mod:`multiprocessing` module's :class:`Manager*` classes
753 can now be passed a callable that will be called whenever
754 a subprocess is started, along with a set of arguments that will be
755 passed to the callable.
756 (Contributed by lekma; :issue:`5585`.)
757
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000758 The :class:`Pool` class, which controls a pool of worker processes,
759 now has an optional *maxtasksperchild* parameter. Worker processes
760 will perform the specified number of tasks and then exit, causing the
761 :class:`Pool` to start a new worker. This is useful if tasks may leak
762 memory or other resources, or if some tasks will cause the worker to
763 become very large.
764 (Contributed by Charles Cazabon; :issue:`6963`.)
765
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000766* The :mod:`nntplib` module now supports IPv6 addresses.
767 (Contributed by Derek Morr; :issue:`1664`.)
768
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000769* New functions: the :mod:`os` module wraps the following POSIX system
770 calls: :func:`getresgid` and :func:`getresuid`, which return the
771 real, effective, and saved GIDs and UIDs;
772 :func:`setresgid` and :func:`setresuid`, which set
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000773 real, effective, and saved GIDs and UIDs to new values;
774 :func:`initgroups`. (GID/UID functions
775 contributed by Travis H.; :issue:`6508`. Support for initgroups added
776 by Jean-Paul Calderone; :issue:`7333`.)
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000777
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000778 The :func:`normpath` function now preserves Unicode; if its input path
779 is a Unicode string, the return value is also a Unicode string.
780 (Fixed by Matt Giuca; :issue:`5827`.)
781
Andrew M. Kuchling9cb42772009-01-21 02:15:43 +0000782* The :mod:`pydoc` module now has help for the various symbols that Python
783 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
784 (Contributed by David Laban; :issue:`4739`.)
785
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000786* The :mod:`re` module's :func:`split`, :func:`sub`, and :func:`subn`
787 now accept an optional *flags* argument, for consistency with the
788 other functions in the module. (Added by Gregory P. Smith.)
789
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000790* The :mod:`shutil` module's :func:`copyfile` and :func:`copytree`
791 functions now raises a :exc:`SpecialFileError` exception when
792 asked to copy a named pipe. Previously the code would treat
793 named pipes like a regular file by opening them for reading, and
794 this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.)
795
796* New functions: in the :mod:`site` module, three new functions
797 return various site- and user-specific paths.
798 :func:`getsitepackages` returns a list containing all
799 global site-packages directories, and
800 :func:`getusersitepackages` returns the path of the user's
801 site-packages directory.
Ezio Melotti81982562010-01-04 09:00:11 +0000802 :func:`getuserbase` returns the value of the :envvar:`USER_BASE`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000803 environment variable, giving the path to a directory that can be used
804 to store data.
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000805 (Contributed by Tarek Ziadé; :issue:`6693`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000806
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +0000807* The :mod:`socket` module's :class:`SSL` objects now support the
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000808 buffer API, which fixed a test suite failure. (Fixed by Antoine
809 Pitrou; :issue:`7133`.) The :func:`create_connection` function
810 gained a *source_address* parameter, a ``(host, port)`` 2-tuple
811 giving the source address that will be used for the connection.
812 (Contributed by Eldon Ziegler; :issue:`3972`.)
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +0000813
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000814* The :mod:`SocketServer` module's :class:`TCPServer` class now
815 has a :attr:`disable_nagle_algorithm` class attribute.
816 The default value is False; if overridden to be True,
817 new request connections will have the TCP_NODELAY option set to
818 prevent buffering many small sends into a single TCP packet.
819 (Contributed by Kristjan Valur Jonsson; :issue:`6192`.)
820
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000821* The :mod:`struct` module will no longer silently ignore overflow
822 errors when a value is too large for a particular integer format
823 code (one of ``bBhHiIlLqQ``); it now always raises a
824 :exc:`struct.error` exception. (Changed by Mark Dickinson;
825 :issue:`1523`.)
826
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000827* New function: the :mod:`subprocess` module's
828 :func:`check_output` runs a command with a specified set of arguments
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000829 and returns the command's output as a string when the command runs without
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +0000830 error, or raises a :exc:`CalledProcessError` exception otherwise.
831
832 ::
833
834 >>> subprocess.check_output(['df', '-h', '.'])
835 'Filesystem Size Used Avail Capacity Mounted on\n
836 /dev/disk0s2 52G 49G 3.0G 94% /\n'
837
838 >>> subprocess.check_output(['df', '-h', '/bogus'])
839 ...
840 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
841
842 (Contributed by Gregory P. Smith.)
843
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000844* New function: :func:`is_declared_global` in the :mod:`symtable` module
845 returns true for variables that are explicitly declared to be global,
846 false for ones that are implicitly global.
847 (Contributed by Jeremy Hylton.)
848
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000849* The ``sys.version_info`` value is now a named tuple, with attributes
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000850 named :attr:`major`, :attr:`minor`, :attr:`micro`,
851 :attr:`releaselevel`, and :attr:`serial`. (Contributed by Ross
852 Light; :issue:`4285`.)
853
854 :func:`sys.getwindowsversion` also returns a named tuple,
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000855 with attributes named :attr:`major`, :attr:`minor`, :attr:`build`,
Eric Smithb3c54882010-02-03 14:17:50 +0000856 :attr:`platform`:, :attr:`service_pack`, :attr:`service_pack_major`,
857 :attr:`service_pack_minor`, :attr:`suite_mask`, and
858 :attr:`product_type`. (Contributed by Brian Curtin; :issue:`7766`.)
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000859
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000860* The :mod:`tarfile` module's default error handling has changed, to
861 no longer suppress fatal errors. The default error level was previously 0,
862 which meant that errors would only result in a message being written to the
863 debug log, but because the debug log is not activated by default,
864 these errors go unnoticed. The default error level is now 1,
865 which raises an exception if there's an error.
866 (Changed by Lars Gustäbel; :issue:`7357`.)
867
868 :mod:`tarfile` now supports filtering the :class:`TarInfo`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000869 objects being added to a tar file. When you call :meth:`TarFile.add`,
870 instance, you may supply an optional *filter* argument
871 that's a callable. The *filter* callable will be passed the
872 :class:`TarInfo` for every file being added, and can modify and return it.
873 If the callable returns ``None``, the file will be excluded from the
874 resulting archive. This is more powerful than the existing
875 *exclude* argument, which has therefore been deprecated.
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000876 (Added by Lars Gustäbel; :issue:`6856`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000877
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000878* The :mod:`threading` module's :meth:`Event.wait` method now returns
879 the internal flag on exit. This means the method will usually
880 return true because :meth:`wait` is supposed to block until the
881 internal flag becomes true. The return value will only be false if
882 a timeout was provided and the operation timed out.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000883 (Contributed by Tim Lesher; :issue:`1674032`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000884
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000885* The :class:`UserDict` class is now a new-style class. (Changed by
886 Benjamin Peterson.)
887
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000888* The :mod:`zipfile` module's :class:`ZipFile` now supports the context
889 management protocol, so you can write ``with zipfile.ZipFile(...) as f: ...``.
890 (Contributed by Brian Curtin; :issue:`5511`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000891
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000892 :mod:`zipfile` now supports archiving empty directories and
893 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000894 Reading files out of an archive is now faster, and interleaving
895 :meth:`read` and :meth:`readline` now works correctly.
896 (Contributed by Nir Aides; :issue:`7610`.)
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000897
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000898 The :func:`is_zipfile` function in the module now
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000899 accepts a file object, in addition to the path names accepted in earlier
900 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
901
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +0000902New module: sysconfig
903---------------------------------
904
905XXX A new :mod:`sysconfig` module has been extracted from
906:mod:`distutils` and put in the standard library.
907
908The :mod:`sysconfig` module provides access to Python's configuration
909information like the list of installation paths and the configuration
910variables relevant for the current platform.
911
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000912.. ======================================================================
913.. whole new modules get described in subsections here
914
Tarek Ziadé2b210692010-02-02 23:39:40 +0000915
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000916Distutils Enhancements
917---------------------------------
918
919Distutils is being more actively developed, thanks to Tarek Ziadé
920who has taken over maintenance of the package, so there are a number
921of fixes and improvements.
922
923A new :file:`setup.py` subcommand, ``check``, will check that the
924arguments being passed to the :func:`setup` function are complete
925and correct (:issue:`5732`).
926
927Byte-compilation by the ``install_lib`` subcommand is now only done
928if the ``sys.dont_write_bytecode`` setting allows it (:issue:`7071`).
929
930:func:`distutils.sdist.add_defaults` now uses
931*package_dir* and *data_files* to create the MANIFEST file.
932:mod:`distutils.sysconfig` now reads the :envvar:`AR` and
933:envvar:`ARFLAGS` environment variables.
934
935.. ARFLAGS done in #5941
936
937It is no longer mandatory to store clear-text passwords in the
938:file:`.pypirc` file when registering and uploading packages to PyPI. As long
939as the username is present in that file, the :mod:`distutils` package will
940prompt for the password if not present. (Added by Tarek Ziadé,
941based on an initial contribution by Nathan Van Gheem; :issue:`4394`.)
942
943A Distutils setup can now specify that a C extension is optional by
944setting the *optional* option setting to true. If this optional is
945supplied, failure to build the extension will not abort the build
946process, but instead simply not install the failing extension.
947(Contributed by Georg Brandl; :issue:`5583`.)
948
949The :class:`distutils.dist.DistributionMetadata` class'
950:meth:`read_pkg_file` method will read the contents of a package's
951:file:`PKG-INFO` metadata file. For an example of its use, see
952:ref:`reading-metadata`.
953(Contributed by Tarek Ziadé; :issue:`7457`.)
954
955:file:`setup.py` files will now accept a :option:`--no-user-cfg` switch
956to skip reading the :file:`~/.pydistutils.cfg` file. (Suggested by
957by Michael Hoffman, and implemented by Paul Winkler; :issue:`1180`.)
958
959When creating a tar-format archive, the ``sdist`` subcommand now
960allows specifying the user id and group that will own the files in the
961archives using the :option:`--owner` and :option:`--group` switches
962(:issue:`6516`).
963
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +0000964
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000965Unit Testing Enhancements
966---------------------------------
967
968The :mod:`unittest` module was enhanced in several ways.
969The progress messages now shows 'x' for expected failures
970and 'u' for unexpected successes when run in verbose mode.
971(Contributed by Benjamin Peterson.)
972Test cases can raise the :exc:`SkipTest` exception to skip a test.
973(:issue:`1034053`.)
974
975.. XXX describe test discovery (Contributed by Michael Foord; :issue:`6001`.)
976
977The error messages for :meth:`assertEqual`,
978:meth:`assertTrue`, and :meth:`assertFalse`
979failures now provide more information. If you set the
980:attr:`longMessage` attribute of your :class:`TestCase` classes to
981true, both the standard error message and any additional message you
982provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
983
984The :meth:`assertRaises` and :meth:`failUnlessRaises` methods now
985return a context handler when called without providing a callable
986object to run. For example, you can write this::
987
988 with self.assertRaises(KeyError):
989 raise ValueError
990
991(Implemented by Antoine Pitrou; :issue:`4444`.)
992
993The methods :meth:`addCleanup` and :meth:`doCleanups` were added.
994:meth:`addCleanup` allows you to add cleanup functions that
995will be called unconditionally (after :meth:`setUp` if
996:meth:`setUp` fails, otherwise after :meth:`tearDown`). This allows
997for much simpler resource allocation and deallocation during tests.
998:issue:`5679`
999
1000A number of new methods were added that provide more specialized
1001tests. Many of these methods were written by Google engineers
1002for use in their test suites; Gregory P. Smith, Michael Foord, and
1003GvR worked on merging them into Python's version of :mod:`unittest`.
1004
1005* :meth:`assertIsNone` and :meth:`assertIsNotNone` take one
1006 expression and verify that the result is or is not ``None``.
1007
1008* :meth:`assertIs` and :meth:`assertIsNot` take two values and check
1009 whether the two values evaluate to the same object or not.
1010 (Added by Michael Foord; :issue:`2578`.)
1011
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001012* :meth:`assertIsInstance` and :meth:`assertNotIsInstance` check whether
1013 the resulting object is an instance of a particular class, or of
1014 one of a tuple of classes. (Added by Georg Brandl; :issue:`7031`.)
1015
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001016* :meth:`assertGreater`, :meth:`assertGreaterEqual`,
1017 :meth:`assertLess`, and :meth:`assertLessEqual` compare
1018 two quantities.
1019
1020* :meth:`assertMultiLineEqual` compares two strings, and if they're
1021 not equal, displays a helpful comparison that highlights the
1022 differences in the two strings.
1023
1024* :meth:`assertRegexpMatches` checks whether its first argument is a
1025 string matching a regular expression provided as its second argument.
1026
1027* :meth:`assertRaisesRegexp` checks whether a particular exception
1028 is raised, and then also checks that the string representation of
1029 the exception matches the provided regular expression.
1030
1031* :meth:`assertIn` and :meth:`assertNotIn` tests whether
1032 *first* is or is not in *second*.
1033
1034* :meth:`assertSameElements` tests whether two provided sequences
1035 contain the same elements.
1036
1037* :meth:`assertSetEqual` compares whether two sets are equal, and
1038 only reports the differences between the sets in case of error.
1039
1040* Similarly, :meth:`assertListEqual` and :meth:`assertTupleEqual`
1041 compare the specified types and explain the differences.
1042 More generally, :meth:`assertSequenceEqual` compares two sequences
1043 and can optionally check whether both sequences are of a
1044 particular type.
1045
1046* :meth:`assertDictEqual` compares two dictionaries and reports the
1047 differences. :meth:`assertDictContainsSubset` checks whether
1048 all of the key/value pairs in *first* are found in *second*.
1049
1050* :meth:`assertAlmostEqual` and :meth:`assertNotAlmostEqual` short-circuit
1051 (automatically pass or fail without checking decimal places) if the objects
1052 are equal.
1053
1054* :meth:`loadTestsFromName` properly honors the ``suiteClass`` attribute of
1055 the :class:`TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.)
1056
1057* A new hook, :meth:`addTypeEqualityFunc` takes a type object and a
1058 function. The :meth:`assertEqual` method will use the function
1059 when both of the objects being compared are of the specified type.
1060 This function should compare the two objects and raise an
1061 exception if they don't match; it's a good idea for the function
1062 to provide additional information about why the two objects are
1063 matching, much as the new sequence comparison methods do.
1064
1065:func:`unittest.main` now takes an optional ``exit`` argument.
1066If False ``main`` doesn't call :func:`sys.exit` allowing it to
1067be used from the interactive interpreter. :issue:`3379`.
1068
1069:class:`TestResult` has new :meth:`startTestRun` and
1070:meth:`stopTestRun` methods; called immediately before
1071and after a test run. :issue:`5728` by Robert Collins.
1072
1073With all these changes, the :file:`unittest.py` was becoming awkwardly
1074large, so the module was turned into a package and the code split into
1075several files (by Benjamin Peterson). This doesn't affect how the
1076module is imported.
1077
1078
1079.. _importlib-section:
1080
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001081importlib: Importing Modules
1082------------------------------
1083
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001084Python 3.1 includes the :mod:`importlib` package, a re-implementation
1085of the logic underlying Python's :keyword:`import` statement.
1086:mod:`importlib` is useful for implementors of Python interpreters and
Brett Cannonca2dc472009-12-22 02:37:37 +00001087to users who wish to write new importers that can participate in the
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001088import process. Python 2.7 doesn't contain the complete
1089:mod:`importlib` package, but instead has a tiny subset that contains
1090a single function, :func:`import_module`.
1091
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001092``import_module(name, package=None)`` imports a module. *name* is
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001093a string containing the module or package's name. It's possible to do
1094relative imports by providing a string that begins with a ``.``
1095character, such as ``..utils.errors``. For relative imports, the
1096*package* argument must be provided and is the name of the package that
1097will be used as the anchor for
1098the relative import. :func:`import_module` both inserts the imported
1099module into ``sys.modules`` and returns the module object.
1100
1101Here are some examples::
1102
1103 >>> from importlib import import_module
1104 >>> anydbm = import_module('anydbm') # Standard absolute import
1105 >>> anydbm
1106 <module 'anydbm' from '/p/python/Lib/anydbm.py'>
1107 >>> # Relative import
1108 >>> sysconfig = import_module('..sysconfig', 'distutils.command')
1109 >>> sysconfig
1110 <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'>
1111
1112:mod:`importlib` was implemented by Brett Cannon and introduced in
1113Python 3.1.
1114
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001115
Andrew M. Kuchlinga17cd4a2009-01-31 02:50:09 +00001116ttk: Themed Widgets for Tk
1117--------------------------
1118
1119Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
1120widgets but have a more customizable appearance and can therefore more
1121closely resemble the native platform's widgets. This widget
1122set was originally called Tile, but was renamed to Ttk (for "themed Tk")
1123on being added to Tcl/Tck release 8.5.
1124
1125XXX write a brief discussion and an example here.
1126
1127The :mod:`ttk` module was written by Guilherme Polo and added in
1128:issue:`2983`. An alternate version called ``Tile.py``, written by
1129Martin Franklin and maintained by Kevin Walzer, was proposed for
1130inclusion in :issue:`2618`, but the authors argued that Guilherme
1131Polo's work was more comprehensive.
1132
Georg Brandl0516f812009-11-18 18:52:35 +00001133
1134Deprecations and Removals
1135=========================
1136
1137* :func:`contextlib.nested`, which allows handling more than one context manager
1138 with one :keyword:`with` statement, has been deprecated; :keyword:`with`
1139 supports multiple context managers syntactically now.
1140
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001141.. ======================================================================
1142
1143
1144Build and C API Changes
1145=======================
1146
1147Changes to Python's build process and to the C API include:
1148
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +00001149* If you use the :file:`.gdbinit` file provided with Python,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001150 the "pyo" macro in the 2.7 version now works correctly when the thread being
1151 debugged doesn't hold the GIL; the macro now acquires it before printing.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001152 (Contributed by Victor Stinner; :issue:`3632`.)
1153
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +00001154* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001155 worker thread submit notifications to the main Python thread. This
1156 is particularly useful for asynchronous IO operations.
1157 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
1158
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001159* New function: :cfunc:`PyCode_NewEmpty` creates an empty code object;
1160 only the filename, function name, and first line number are required.
1161 This is useful to extension modules that are attempting to
1162 construct a more useful traceback stack. Previously such
1163 extensions needed to call :cfunc:`PyCode_New`, which had many
1164 more arguments. (Added by Jeffrey Yasskin.)
1165
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001166* New function: :cfunc:`PyErr_NewExceptionWithDoc` creates a new
1167 exception class, just as the existing :cfunc:`PyErr_NewException` does,
1168 but takes an extra ``char *`` argument containing the docstring for the
1169 new exception class. (Added by the 'lekma' user on the Python bug tracker;
1170 :issue:`7033`.)
1171
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001172* New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object
1173 and returns the line number that the frame is currently executing.
1174 Previously code would need to get the index of the bytecode
1175 instruction currently executing, and then look up the line number
1176 corresponding to that address. (Added by Jeffrey Yasskin.)
1177
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001178* New functions: :cfunc:`PyLong_AsLongAndOverflow` and
1179 :cfunc:`PyLong_AsLongLongAndOverflow` approximates a Python long
1180 integer as a C :ctype:`long` or :ctype:`long long`.
1181 If the number is too large to fit into
1182 the output type, an *overflow* flag is set and returned to the caller.
1183 (Contributed by Case Van Horsen; :issue:`7528` and :issue:`7767`.)
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001184
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001185* New function: stemming from the rewrite of string-to-float conversion,
1186 a new :cfunc:`PyOS_string_to_double` function was added. The old
1187 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions
1188 are now deprecated.
1189
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001190* New macros: the Python header files now define the following macros:
1191 :cmacro:`Py_ISALNUM`,
1192 :cmacro:`Py_ISALPHA`,
1193 :cmacro:`Py_ISDIGIT`,
1194 :cmacro:`Py_ISLOWER`,
1195 :cmacro:`Py_ISSPACE`,
1196 :cmacro:`Py_ISUPPER`,
1197 :cmacro:`Py_ISXDIGIT`,
1198 and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`.
1199 All of these functions are analogous to the C
1200 standard macros for classifying characters, but ignore the current
1201 locale setting, because in
1202 several places Python needs to analyze characters in a
1203 locale-independent way. (Added by Eric Smith;
1204 :issue:`5793`.)
1205
1206 .. XXX these macros don't seem to be described in the c-api docs.
1207
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001208* New format codes: the :cfunc:`PyFormat_FromString`,
1209 :cfunc:`PyFormat_FromStringV`, and :cfunc:`PyErr_Format` now
1210 accepts ``%lld`` and ``%llu`` format codes for displaying values of
1211 C's :ctype:`long long` types.
1212 (Contributed by Mark Dickinson; :issue:`7228`.)
1213
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001214* The complicated interaction between threads and process forking has
1215 been changed. Previously, the child process created by
1216 :func:`os.fork` might fail because the child is created with only a
1217 single thread running, the thread performing the :func:`os.fork`.
1218 If other threads were holding a lock, such as Python's import lock,
1219 when the fork was performed, the lock would still be marked as
1220 "held" in the new process. But in the child process nothing would
1221 ever release the lock, since the other threads weren't replicated,
1222 and the child process would no longer be able to perform imports.
1223
1224 Python 2.7 now acquires the import lock before performing an
1225 :func:`os.fork`, and will also clean up any locks created using the
1226 :mod:`threading` module. C extension modules that have internal
1227 locks, or that call :cfunc:`fork()` themselves, will not benefit
1228 from this clean-up.
1229
1230 (Fixed by Thomas Wouters; :issue:`1590864`.)
1231
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001232* The :cfunc:`Py_Finalize` function now calls the internal
1233 :func:`threading._shutdown` function; this prevents some exceptions from
1234 being raised when an interpreter shuts down.
1235 (Patch by Adam Olsen; :issue:`1722344`.)
1236
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001237* Global symbols defined by the :mod:`ctypes` module are now prefixed
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001238 with ``Py``, or with ``_ctypes``. (Implemented by Thomas
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001239 Heller; :issue:`3102`.)
1240
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001241* New configure option: the :option:`--with-system-expat` switch allows
1242 building the :mod:`pyexpat` module to use the system Expat library.
1243 (Contributed by Arfrever Frehtes Taifersar Arahesis; :issue:`7609`.)
1244
1245* New configure option: Compiling Python with the
1246 :option:`--with-valgrind` option will now disable the pymalloc
1247 allocator, which is difficult for the Valgrind to analyze correctly.
1248 Valgrind will therefore be better at detecting memory leaks and
1249 overruns. (Contributed by James Henstridge; :issue:`2422`.)
1250
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00001251* New configure option: you can now supply no arguments to
1252 :option:`--with-dbmliborder=` in order to build none of the various
1253 DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis;
1254 :issue:`6491`.)
1255
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001256* The :program:`configure` script now checks for floating-point rounding bugs
1257 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
1258 preprocessor definition. No code currently uses this definition,
1259 but it's available if anyone wishes to use it.
1260 (Added by Mark Dickinson; :issue:`2937`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001261
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001262* The build process now creates the necessary files for pkg-config
1263 support. (Contributed by Clinton Roy; :issue:`3585`.)
1264
1265* The build process now supports Subversion 1.7. (Contributed by
1266 Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.)
1267
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001268
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001269.. ======================================================================
1270
1271Port-Specific Changes: Windows
1272-----------------------------------
1273
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +00001274* The :mod:`msvcrt` module now contains some constants from
1275 the :file:`crtassem.h` header file:
1276 :data:`CRT_ASSEMBLY_VERSION`,
1277 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
1278 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001279 (Contributed by David Cournapeau; :issue:`4365`.)
1280
1281* The new :cfunc:`_beginthreadex` API is used to start threads, and
1282 the native thread-local storage functions are now used.
1283 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001284
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001285* The :func:`os.listdir` function now correctly fails
1286 for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.)
1287
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001288* The :mod:`mimelib` module will now read the MIME database from
1289 the Windows registry when initializing.
1290 (Patch by Gabriel Genellina; :issue:`4969`.)
1291
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001292.. ======================================================================
1293
1294Port-Specific Changes: Mac OS X
1295-----------------------------------
1296
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001297* The path ``/Library/Python/2.7/site-packages`` is now appended to
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001298 ``sys.path``, in order to share added packages between the system
1299 installation and a user-installed copy of the same version.
1300 (Changed by Ronald Oussoren; :issue:`4865`.)
1301
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001302
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001303Other Changes and Fixes
1304=======================
1305
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001306* Two benchmark scripts, :file:`iobench` and :file:`ccbench`, were
1307 added to the :file:`Tools` directory. :file:`iobench` measures the
1308 speed of the built-in :class:`file` objects while performing various
1309 operations, and :file:`ccbench` is a concurrency that performs
1310 several tasks using a varying number of threads.
1311
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001312* When importing a module from a :file:`.pyc` or :file:`.pyo` file
1313 with an existing :file:`.py` counterpart, the :attr:`co_filename`
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001314 attributes of the resulting code objects are overwritten when the
1315 original filename is obsolete. This can happen if the file has been
1316 renamed, moved, or is accessed through different paths. (Patch by
1317 Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.)
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001318
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001319* The :file:`regrtest.py` script now takes a :option:`--randseed=`
1320 switch that takes an integer that will be used as the random seed
1321 for the :option:`-r` option that executes tests in random order.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001322 The :option:`-r` option also reports the seed that was used
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001323 (Added by Collin Winter.)
1324
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001325* Another :file:`regrtest.py` switch is :option:`-j`, which
1326 takes an integer specifying how many tests run in parallel. This
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001327 allows reducing the total runtime on multi-core machines.
Antoine Pitrou4698d992009-05-31 14:20:14 +00001328 This option is compatible with several other options, including the
1329 :option:`-R` switch which is known to produce long runtimes.
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001330 (Added by Antoine Pitrou, :issue:`6152`.) This can also be used
1331 with a new :option:`-F` switch that runs selected tests in a loop
1332 until they fail. (Added by Antoine Pitrou; :issue:`7312`.)
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001333
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001334.. ======================================================================
1335
1336Porting to Python 2.7
1337=====================
1338
1339This section lists previously described changes and other bugfixes
1340that may require changes to your code:
1341
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001342* When using :class:`Decimal` instances with a string's
1343 :meth:`format` method, the default alignment was previously
1344 left-alignment. This has been changed to right-alignment, which might
1345 change the output of your programs.
1346 (Changed by Mark Dickinson; :issue:`6857`.)
1347
1348 Another :meth:`format`-related change: the default precision used
1349 for floating-point and complex numbers was changed from 6 decimal
1350 places to 12, which matches the precision used by :func:`str`.
1351 (Changed by Eric Smith; :issue:`5920`.)
1352
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00001353* Because of an optimization for the :keyword:`with` statement, the special
1354 methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's
1355 type, and cannot be directly attached to the object's instance. This
Amaury Forgeot d'Arcd81333c2009-06-10 20:30:19 +00001356 affects new-style classes (derived from :class:`object`) and C extension
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00001357 types. (:issue:`6101`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001358
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001359* The :meth:`readline` method of :class:`StringIO` objects now does
1360 nothing when a negative length is requested, as other file-like
1361 objects do. (:issue:`7348`).
1362
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001363For C extensions:
1364
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00001365* C extensions that use integer format codes with the ``PyArg_Parse*``
1366 family of functions will now raise a :exc:`TypeError` exception
1367 instead of triggering a :exc:`DeprecationWarning` (:issue:`5080`).
1368
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001369* Use the new :cfunc:`PyOS_string_to_double` function instead of the old
1370 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions,
1371 which are now deprecated.
1372
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00001373
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001374.. ======================================================================
1375
1376
1377.. _acks27:
1378
1379Acknowledgements
1380================
1381
1382The author would like to thank the following people for offering
1383suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchling8f254e72009-12-08 02:37:05 +00001384article: Ryan Lovett, Hugh Secker-Walker.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001385