blob: 20a4b56fc06e020e161faa727dd57eb0c95310d8 [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. Kuchlingab21f752010-03-02 13:55:33 +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
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000235PEP 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
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000257Other Language Changes
258======================
259
260Some smaller changes made to the core Python language are:
261
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +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
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +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.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000334
Mark Dickinsonbdd863d2010-01-07 09:28:29 +0000335 Implemented by Eric Smith and Mark Dickinson, using David Gay's
336 :file:`dtoa.c` library; :issue:`7117`.
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000337
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000338* The :meth:`str.format` method now supports automatic numbering of the replacement
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +0000339 fields. This makes using :meth:`str.format` more closely resemble using
340 ``%s`` formatting::
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000341
342 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
343 '2009:4:Sunday'
344 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
345 '2009:4:Sunday'
346
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000359
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +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 Dickinson1a707982008-12-17 16:14:37 +0000364* The :func:`int` and :func:`long` types gained a ``bit_length``
Georg Brandl64e1c752009-04-11 18:19:27 +0000365 method that returns the number of bits necessary to represent
Mark Dickinson1a707982008-12-17 16:14:37 +0000366 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
Andrew M. Kuchling92b97002009-05-02 17:12:15 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000385 unavoidably lose precision, Python 2.7 now approximates more
Andrew M. Kuchling92b97002009-05-02 17:12:15 +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
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000406 Integer division is also more accurate in its rounding behaviours. (Also
407 implemented by Mark Dickinson; :issue:`1811`.)
408
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000409* The :class:`bytearray` type's :meth:`translate` method now accepts
410 ``None`` as its first argument. (Fixed by Georg Brandl;
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000411 :issue:`4759`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000412
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000423* The :class:`file` object will now set the :attr:`filename` attribute
424 on the :exc:`IOError` exception when trying to open a directory
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +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`).
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000429
Benjamin Petersonae9a0a02009-12-31 16:49:37 +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.
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +0000434
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +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
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +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
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000444.. ======================================================================
445
446
447Optimizations
448-------------
449
Andrew M. Kuchling77069572009-03-31 01:21:01 +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.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000457
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +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`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000472
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +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 Pitrouc18f6b02009-03-28 19:10:13 +0000480 (Contributed by Antoine Pitrou; :issue:`4688`.)
481
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000482* Long integers are now stored internally either in base 2**15 or in base
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +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
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000502 (Contributed by Mark Dickinson; :issue:`4258`.)
503
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000504 Another set of changes made long objects a few bytes smaller: 2 bytes
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000505 smaller on 32-bit systems and 6 bytes on 64-bit.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000506 (Contributed by Mark Dickinson; :issue:`5260`.)
507
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000508* 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`.)
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +0000514 Bitwise operations are also significantly faster (initial patch by
515 Gregory Smith; :issue:`1087418`).
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000516
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000517* 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
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000523* 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`.)
526
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +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
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +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`.)
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +0000538
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000548.. ======================================================================
549
Georg Brandl0516f812009-11-18 18:52:35 +0000550New and Improved Modules
551========================
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +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
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +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
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +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
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +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
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000575* New class: the :class:`Counter` class in the :mod:`collections` module is
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000576 useful for tallying data. :class:`Counter` instances behave mostly
577 like dictionaries but return zero for missing keys instead of
Georg Brandlf6dab952009-04-28 21:48:35 +0000578 raising a :exc:`KeyError`:
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000579
Georg Brandlf6dab952009-04-28 21:48:35 +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
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +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',
Georg Brandlf6dab952009-04-28 21:48:35 +0000608 's', 's', 'r', 't', 't', 'x'
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000609
610 Contributed by Raymond Hettinger; :issue:`1696199`.
611
Georg Brandlf6d367452010-03-12 10:02:03 +0000612 The new `~collections.OrderedDict` class is described in the earlier section
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000613 :ref:`pep-0372`.
614
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000615 The :class:`namedtuple` class now has an optional *rename* parameter.
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000616 If *rename* is true, field names that are invalid because they've
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +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
Georg Brandlf6dab952009-04-28 21:48:35 +0000621 >>> from collections import namedtuple
622 >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000623 >>> T._fields
624 ('field1', '_1', '_2', 'field2')
625
626 (Added by Raymond Hettinger; :issue:`1818`.)
627
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000628 The :class:`deque` data type now exposes its maximum length as the
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +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.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000632
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000633* The :mod:`copy` module's :func:`deepcopy` function will now
634 correctly copy bound instance methods. (Implemented by
635 Robert Collins; :issue:`1515`.)
636
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000637* The :mod:`ctypes` module now always converts ``None`` to a C NULL
638 pointer for arguments declared as pointers. (Changed by Thomas
639 Heller; :issue:`4606`.)
640
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000641* New method: the :mod:`datetime` module's :class:`timedelta` class
642 gained a :meth:`total_seconds` method that returns the number of seconds
643 in the duration. (Contributed by Brian Quinlan; :issue:`5788`.)
644
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000645* New method: the :class:`Decimal` class gained a
646 :meth:`from_float` class method that performs an exact conversion
647 of a floating-point number to a :class:`Decimal`.
648 Note that this is an **exact** conversion that strives for the
649 closest decimal approximation to the floating-point representation's value;
650 the resulting decimal value will therefore still include the inaccuracy,
651 if any.
652 For example, ``Decimal.from_float(0.1)`` returns
653 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
654 (Implemented by Raymond Hettinger; :issue:`4796`.)
655
656 The constructor for :class:`Decimal` now accepts non-European
657 Unicode characters, such as Arabic-Indic digits. (Contributed by
658 Mark Dickinson; :issue:`6595`.)
659
660 When using :class:`Decimal` instances with a string's
661 :meth:`format` method, the default alignment was previously
662 left-alignment. This has been changed to right-alignment, which seems
663 more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.)
664
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000665* The :class:`Fraction` class now accepts two rational numbers
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000666 as arguments to its constructor.
667 (Implemented by Mark Dickinson; :issue:`5812`.)
668
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000669* The :mod:`ftplib` module gained the ability to establish secure FTP
670 connections using TLS encapsulation of authentication as well as
671 subsequent control and data transfers. This is provided by the new
672 :class:`ftplib.FTP_TLS` class.
673 (Contributed by Giampaolo Rodola', :issue:`2054`.) The :meth:`storbinary`
674 method for binary uploads can now restart uploads thanks to an added
675 *rest* parameter (patch by Pablo Mouzo; :issue:`6845`.)
676
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000677* New function: the :mod:`gc` module's :func:`is_tracked` returns
678 true if a given instance is tracked by the garbage collector, false
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000679 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
680
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000681* The :mod:`gzip` module's :class:`GzipFile` now supports the context
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000682 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``
683 (contributed by Hagen Fuerstenau; :issue:`3860`), and it now implements
684 the :class:`io.BufferedIOBase` ABC, so you can wrap it with
685 :class:`io.BufferedReader` for faster processing
686 (contributed by Nir Aides; :issue:`7471`).
687 It's also now possible to override the modification time
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000688 recorded in a gzipped file by providing an optional timestamp to
689 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000690
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000691 Files in gzip format can be padded with trailing zero bytes; the
692 :mod:`gzip` module will now consume these trailing bytes. (Fixed by
693 Tadek Pietraszek and Brian Curtin; :issue:`2846`.)
694
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000695* The default :class:`HTTPResponse` class used by the :mod:`httplib` module now
696 supports buffering, resulting in much faster reading of HTTP responses.
697 (Contributed by Kristjan Valur Jonsson; :issue:`4879`.)
698
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +0000699 The :class:`HTTPConnection` and :class:`HTTPSConnection` classes
700 now support a *source_address* parameter, a ``(host, port)`` 2-tuple
701 giving the source address that will be used for the connection.
702 (Contributed by Eldon Ziegler; :issue:`3972`.)
703
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000704* The :mod:`imaplib` module now supports IPv6 addresses.
705 (Contributed by Derek Morr; :issue:`1655`.)
706
707* The :mod:`io` library has been upgraded to the version shipped with
708 Python 3.1. For 3.1, the I/O library was entirely rewritten in C
709 and is 2 to 20 times faster depending on the task at hand. The
710 original Python version was renamed to the :mod:`_pyio` module.
711
712 One minor resulting change: the :class:`io.TextIOBase` class now
713 has an :attr:`errors` attribute giving the error setting
714 used for encoding and decoding errors (one of ``'strict'``, ``'replace'``,
715 ``'ignore'``).
716
717 The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000718 an invalid file descriptor. (Implemented by Benjamin Peterson;
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000719 :issue:`4991`.) The :meth:`truncate` method now preserves the
720 file position; previously it would change the file position to the
721 end of the new file. (Fixed by Pascal Chambon; :issue:`6939`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000722
Andrew M. Kuchling5a73ff82009-12-02 14:27:11 +0000723* New function: ``itertools.compress(data, selectors)`` takes two
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000724 iterators. Elements of *data* are returned if the corresponding
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000725 value in *selectors* is true::
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000726
727 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
728 A, C, E, F
729
Andrew M. Kuchling5a73ff82009-12-02 14:27:11 +0000730 New function: ``itertools.combinations_with_replacement(iter, r)``
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000731 returns all the possible *r*-length combinations of elements from the
732 iterable *iter*. Unlike :func:`combinations`, individual elements
733 can be repeated in the generated combinations::
734
735 itertools.combinations_with_replacement('abc', 2) =>
736 ('a', 'a'), ('a', 'b'), ('a', 'c'),
737 ('b', 'b'), ('b', 'c'), ('c', 'c')
738
739 Note that elements are treated as unique depending on their position
740 in the input, not their actual values.
741
742 The :class:`itertools.count` function now has a *step* argument that
743 allows incrementing by values other than 1. :func:`count` also
744 now allows keyword arguments, and using non-integer values such as
745 floats or :class:`Decimal` instances. (Implemented by Raymond
746 Hettinger; :issue:`5032`.)
747
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000748 :func:`itertools.combinations` and :func:`itertools.product` were
749 previously raising :exc:`ValueError` for values of *r* larger than
750 the input iterable. This was deemed a specification error, so they
751 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
752
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000753* The :mod:`json` module was upgraded to version 2.0.9 of the
754 simplejson package, which includes a C extension that makes
755 encoding and decoding faster.
756 (Contributed by Bob Ippolito; :issue:`4136`.)
757
758 To support the new :class:`OrderedDict` type, :func:`json.load`
759 now has an optional *object_pairs_hook* parameter that will be called
760 with any object literal that decodes to a list of pairs.
761 (Contributed by Raymond Hettinger; :issue:`5381`.)
762
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000763* New functions: the :mod:`math` module gained
764 :func:`erf` and :func:`erfc` for the error function and the complementary error function,
765 :func:`expm1` which computes ``e**x - 1`` with more precision than
766 using :func:`exp` and subtracting 1,
767 :func:`gamma` for the Gamma function, and
768 :func:`lgamma` for the natural log of the Gamma function.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000769 (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.)
770
Andrew M. Kuchling24520b42009-04-09 11:22:47 +0000771* The :mod:`multiprocessing` module's :class:`Manager*` classes
772 can now be passed a callable that will be called whenever
773 a subprocess is started, along with a set of arguments that will be
774 passed to the callable.
775 (Contributed by lekma; :issue:`5585`.)
776
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000777 The :class:`Pool` class, which controls a pool of worker processes,
778 now has an optional *maxtasksperchild* parameter. Worker processes
779 will perform the specified number of tasks and then exit, causing the
780 :class:`Pool` to start a new worker. This is useful if tasks may leak
781 memory or other resources, or if some tasks will cause the worker to
782 become very large.
783 (Contributed by Charles Cazabon; :issue:`6963`.)
784
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000785* The :mod:`nntplib` module now supports IPv6 addresses.
786 (Contributed by Derek Morr; :issue:`1664`.)
787
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000788* New functions: the :mod:`os` module wraps the following POSIX system
789 calls: :func:`getresgid` and :func:`getresuid`, which return the
790 real, effective, and saved GIDs and UIDs;
791 :func:`setresgid` and :func:`setresuid`, which set
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +0000792 real, effective, and saved GIDs and UIDs to new values;
793 :func:`initgroups`. (GID/UID functions
794 contributed by Travis H.; :issue:`6508`. Support for initgroups added
795 by Jean-Paul Calderone; :issue:`7333`.)
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +0000796
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000797 The :func:`normpath` function now preserves Unicode; if its input path
798 is a Unicode string, the return value is also a Unicode string.
799 (Fixed by Matt Giuca; :issue:`5827`.)
800
Andrew M. Kuchling9cb42772009-01-21 02:15:43 +0000801* The :mod:`pydoc` module now has help for the various symbols that Python
802 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
803 (Contributed by David Laban; :issue:`4739`.)
804
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000805* The :mod:`re` module's :func:`split`, :func:`sub`, and :func:`subn`
806 now accept an optional *flags* argument, for consistency with the
807 other functions in the module. (Added by Gregory P. Smith.)
808
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000809* The :mod:`shutil` module's :func:`copyfile` and :func:`copytree`
810 functions now raises a :exc:`SpecialFileError` exception when
811 asked to copy a named pipe. Previously the code would treat
812 named pipes like a regular file by opening them for reading, and
813 this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.)
814
Andrew M. Kuchlingfed15762010-03-08 12:00:39 +0000815 New function: :func:`make_archive` takes a filename, archive type
816 (zip or tar-format), and a directory path, and creates an archive
817 containing the directory's contents. (Added by Tarek Ziadé.)
818
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000819* New functions: in the :mod:`site` module, three new functions
820 return various site- and user-specific paths.
821 :func:`getsitepackages` returns a list containing all
822 global site-packages directories, and
823 :func:`getusersitepackages` returns the path of the user's
824 site-packages directory.
Ezio Melotti81982562010-01-04 09:00:11 +0000825 :func:`getuserbase` returns the value of the :envvar:`USER_BASE`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000826 environment variable, giving the path to a directory that can be used
827 to store data.
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000828 (Contributed by Tarek Ziadé; :issue:`6693`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000829
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +0000830* The :mod:`socket` module's :class:`SSL` objects now support the
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000831 buffer API, which fixed a test suite failure. (Fixed by Antoine
832 Pitrou; :issue:`7133`.) The :func:`create_connection` function
833 gained a *source_address* parameter, a ``(host, port)`` 2-tuple
834 giving the source address that will be used for the connection.
835 (Contributed by Eldon Ziegler; :issue:`3972`.)
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +0000836
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000837* The :mod:`SocketServer` module's :class:`TCPServer` class now
838 has a :attr:`disable_nagle_algorithm` class attribute.
839 The default value is False; if overridden to be True,
840 new request connections will have the TCP_NODELAY option set to
841 prevent buffering many small sends into a single TCP packet.
842 (Contributed by Kristjan Valur Jonsson; :issue:`6192`.)
843
Andrew M. Kuchlingfed15762010-03-08 12:00:39 +0000844* Updated module: the :mod:`sqlite` module has been updated to
845 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
846 the ability to load SQLite extensions from shared libraries.
847 Call the ``enable_load_extension(True)`` method to enable extensions,
848 and then call :meth:`load_extension` to load a particular shared library.
849 (Updated by Gerhard Häring.)
850
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000851* The :mod:`struct` module will no longer silently ignore overflow
852 errors when a value is too large for a particular integer format
853 code (one of ``bBhHiIlLqQ``); it now always raises a
854 :exc:`struct.error` exception. (Changed by Mark Dickinson;
855 :issue:`1523`.)
856
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000857* New function: the :mod:`subprocess` module's
858 :func:`check_output` runs a command with a specified set of arguments
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000859 and returns the command's output as a string when the command runs without
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +0000860 error, or raises a :exc:`CalledProcessError` exception otherwise.
861
862 ::
863
864 >>> subprocess.check_output(['df', '-h', '.'])
865 'Filesystem Size Used Avail Capacity Mounted on\n
866 /dev/disk0s2 52G 49G 3.0G 94% /\n'
867
868 >>> subprocess.check_output(['df', '-h', '/bogus'])
869 ...
870 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
871
872 (Contributed by Gregory P. Smith.)
873
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000874* New function: :func:`is_declared_global` in the :mod:`symtable` module
875 returns true for variables that are explicitly declared to be global,
876 false for ones that are implicitly global.
877 (Contributed by Jeremy Hylton.)
878
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000879* The ``sys.version_info`` value is now a named tuple, with attributes
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000880 named :attr:`major`, :attr:`minor`, :attr:`micro`,
881 :attr:`releaselevel`, and :attr:`serial`. (Contributed by Ross
882 Light; :issue:`4285`.)
883
884 :func:`sys.getwindowsversion` also returns a named tuple,
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000885 with attributes named :attr:`major`, :attr:`minor`, :attr:`build`,
Ezio Melotti12477752010-02-08 22:22:41 +0000886 :attr:`platform`, :attr:`service_pack`, :attr:`service_pack_major`,
Eric Smithb3c54882010-02-03 14:17:50 +0000887 :attr:`service_pack_minor`, :attr:`suite_mask`, and
888 :attr:`product_type`. (Contributed by Brian Curtin; :issue:`7766`.)
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000889
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000890* The :mod:`tarfile` module's default error handling has changed, to
891 no longer suppress fatal errors. The default error level was previously 0,
892 which meant that errors would only result in a message being written to the
893 debug log, but because the debug log is not activated by default,
894 these errors go unnoticed. The default error level is now 1,
895 which raises an exception if there's an error.
896 (Changed by Lars Gustäbel; :issue:`7357`.)
897
898 :mod:`tarfile` now supports filtering the :class:`TarInfo`
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000899 objects being added to a tar file. When you call :meth:`TarFile.add`,
900 instance, you may supply an optional *filter* argument
901 that's a callable. The *filter* callable will be passed the
902 :class:`TarInfo` for every file being added, and can modify and return it.
903 If the callable returns ``None``, the file will be excluded from the
904 resulting archive. This is more powerful than the existing
905 *exclude* argument, which has therefore been deprecated.
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000906 (Added by Lars Gustäbel; :issue:`6856`.)
Andrew M. Kuchlingfed15762010-03-08 12:00:39 +0000907 The :class:`TarFile` class also now supports the context manager protocol.
908 (Added by Lars Gustäbel; :issue:`7232`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000909
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000910* The :mod:`threading` module's :meth:`Event.wait` method now returns
911 the internal flag on exit. This means the method will usually
912 return true because :meth:`wait` is supposed to block until the
913 internal flag becomes true. The return value will only be false if
914 a timeout was provided and the operation timed out.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000915 (Contributed by Tim Lesher; :issue:`1674032`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000916
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +0000917* The Unicode database provided by the :mod:`unicodedata` module
918 remains at version 5.1.0, but Python now uses it internally to
919 determine which characters are numeric, whitespace, or represent
920 line breaks. The database also now includes information from the
921 :file:`Unihan.txt` data file. (Patch by Anders Chrigström
922 and Amaury Forgeot d'Arc; :issue:`1571184`.)
923
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000924* The :class:`UserDict` class is now a new-style class. (Changed by
925 Benjamin Peterson.)
926
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +0000927* The ElementTree library, :mod:`xml.etree`, no longer escapes
928 ampersands and angle brackets when outputting an XML processing
929 instruction (which looks like `<?xml-stylesheet href="#style1"?>`)
930 or comment (which looks like `<!-- comment -->`).
931 (Patch by Neil Muller; :issue:`2746`.)
932
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000933* The :mod:`zipfile` module's :class:`ZipFile` now supports the context
934 management protocol, so you can write ``with zipfile.ZipFile(...) as f: ...``.
935 (Contributed by Brian Curtin; :issue:`5511`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000936
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000937 :mod:`zipfile` now supports archiving empty directories and
938 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000939 Reading files out of an archive is now faster, and interleaving
940 :meth:`read` and :meth:`readline` now works correctly.
941 (Contributed by Nir Aides; :issue:`7610`.)
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000942
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +0000943 The :func:`is_zipfile` function in the module now
Andrew M. Kuchling039c8992010-02-01 02:04:26 +0000944 accepts a file object, in addition to the path names accepted in earlier
945 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
946
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +0000947 The :meth:`writestr` method now has an optional *compress_type* parameter
948 that lets you override the default compression method specified in the
949 :class:`ZipFile` constructor. (Contributed by Ronald Oussoren;
950 :issue:`6003`.)
951
Tarek Ziadé1e069ee2010-02-23 05:20:22 +0000952* XXX the :mod:`shutil` module has now a :func:`make_archive` function
953 (see the module doc, contributed by Tarek)
954
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +0000955
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +0000956New module: sysconfig
957---------------------------------
958
959XXX A new :mod:`sysconfig` module has been extracted from
960:mod:`distutils` and put in the standard library.
961
962The :mod:`sysconfig` module provides access to Python's configuration
963information like the list of installation paths and the configuration
Tarek Ziadé1e069ee2010-02-23 05:20:22 +0000964variables relevant for the current platform. (contributed by Tarek)
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +0000965
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000966.. ======================================================================
967.. whole new modules get described in subsections here
968
Tarek Ziadé2b210692010-02-02 23:39:40 +0000969
Andrew M. Kuchling9e483ef2010-02-08 01:35:35 +0000970Distutils Enhancements
971---------------------------------
972
973Distutils is being more actively developed, thanks to Tarek Ziadé
974who has taken over maintenance of the package, so there are a number
975of fixes and improvements.
976
977A new :file:`setup.py` subcommand, ``check``, will check that the
978arguments being passed to the :func:`setup` function are complete
979and correct (:issue:`5732`).
980
981Byte-compilation by the ``install_lib`` subcommand is now only done
982if the ``sys.dont_write_bytecode`` setting allows it (:issue:`7071`).
983
984:func:`distutils.sdist.add_defaults` now uses
985*package_dir* and *data_files* to create the MANIFEST file.
986:mod:`distutils.sysconfig` now reads the :envvar:`AR` and
987:envvar:`ARFLAGS` environment variables.
988
989.. ARFLAGS done in #5941
990
991It is no longer mandatory to store clear-text passwords in the
992:file:`.pypirc` file when registering and uploading packages to PyPI. As long
993as the username is present in that file, the :mod:`distutils` package will
994prompt for the password if not present. (Added by Tarek Ziadé,
995based on an initial contribution by Nathan Van Gheem; :issue:`4394`.)
996
997A Distutils setup can now specify that a C extension is optional by
998setting the *optional* option setting to true. If this optional is
999supplied, failure to build the extension will not abort the build
1000process, but instead simply not install the failing extension.
1001(Contributed by Georg Brandl; :issue:`5583`.)
1002
1003The :class:`distutils.dist.DistributionMetadata` class'
1004:meth:`read_pkg_file` method will read the contents of a package's
1005:file:`PKG-INFO` metadata file. For an example of its use, see
1006:ref:`reading-metadata`.
1007(Contributed by Tarek Ziadé; :issue:`7457`.)
1008
1009:file:`setup.py` files will now accept a :option:`--no-user-cfg` switch
1010to skip reading the :file:`~/.pydistutils.cfg` file. (Suggested by
1011by Michael Hoffman, and implemented by Paul Winkler; :issue:`1180`.)
1012
1013When creating a tar-format archive, the ``sdist`` subcommand now
1014allows specifying the user id and group that will own the files in the
1015archives using the :option:`--owner` and :option:`--group` switches
1016(:issue:`6516`).
1017
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001018
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001019Unit Testing Enhancements
1020---------------------------------
1021
1022The :mod:`unittest` module was enhanced in several ways.
1023The progress messages now shows 'x' for expected failures
1024and 'u' for unexpected successes when run in verbose mode.
1025(Contributed by Benjamin Peterson.)
1026Test cases can raise the :exc:`SkipTest` exception to skip a test.
1027(:issue:`1034053`.)
1028
1029.. XXX describe test discovery (Contributed by Michael Foord; :issue:`6001`.)
1030
1031The error messages for :meth:`assertEqual`,
1032:meth:`assertTrue`, and :meth:`assertFalse`
1033failures now provide more information. If you set the
1034:attr:`longMessage` attribute of your :class:`TestCase` classes to
1035true, both the standard error message and any additional message you
1036provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
1037
1038The :meth:`assertRaises` and :meth:`failUnlessRaises` methods now
1039return a context handler when called without providing a callable
1040object to run. For example, you can write this::
1041
1042 with self.assertRaises(KeyError):
1043 raise ValueError
1044
1045(Implemented by Antoine Pitrou; :issue:`4444`.)
1046
1047The methods :meth:`addCleanup` and :meth:`doCleanups` were added.
1048:meth:`addCleanup` allows you to add cleanup functions that
1049will be called unconditionally (after :meth:`setUp` if
1050:meth:`setUp` fails, otherwise after :meth:`tearDown`). This allows
1051for much simpler resource allocation and deallocation during tests.
1052:issue:`5679`
1053
1054A number of new methods were added that provide more specialized
1055tests. Many of these methods were written by Google engineers
1056for use in their test suites; Gregory P. Smith, Michael Foord, and
1057GvR worked on merging them into Python's version of :mod:`unittest`.
1058
1059* :meth:`assertIsNone` and :meth:`assertIsNotNone` take one
1060 expression and verify that the result is or is not ``None``.
1061
1062* :meth:`assertIs` and :meth:`assertIsNot` take two values and check
1063 whether the two values evaluate to the same object or not.
1064 (Added by Michael Foord; :issue:`2578`.)
1065
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001066* :meth:`assertIsInstance` and :meth:`assertNotIsInstance` check whether
1067 the resulting object is an instance of a particular class, or of
1068 one of a tuple of classes. (Added by Georg Brandl; :issue:`7031`.)
1069
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001070* :meth:`assertGreater`, :meth:`assertGreaterEqual`,
1071 :meth:`assertLess`, and :meth:`assertLessEqual` compare
1072 two quantities.
1073
1074* :meth:`assertMultiLineEqual` compares two strings, and if they're
1075 not equal, displays a helpful comparison that highlights the
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001076 differences in the two strings. This comparison is now used by
1077 default when Unicode strings are compared with :meth:`assertEqual`.)
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001078
1079* :meth:`assertRegexpMatches` checks whether its first argument is a
1080 string matching a regular expression provided as its second argument.
1081
1082* :meth:`assertRaisesRegexp` checks whether a particular exception
1083 is raised, and then also checks that the string representation of
1084 the exception matches the provided regular expression.
1085
1086* :meth:`assertIn` and :meth:`assertNotIn` tests whether
1087 *first* is or is not in *second*.
1088
Michael Foord98e7b762010-03-20 03:00:34 +00001089* :meth:`assertItemsEqual` tests whether two provided sequences
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001090 contain the same elements.
1091
1092* :meth:`assertSetEqual` compares whether two sets are equal, and
1093 only reports the differences between the sets in case of error.
1094
1095* Similarly, :meth:`assertListEqual` and :meth:`assertTupleEqual`
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001096 compare the specified types and explain any differences without necessarily
1097 printing their full values; these methods are now used by default
1098 when comparing lists and tuples using :meth:`assertEqual`.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001099 More generally, :meth:`assertSequenceEqual` compares two sequences
1100 and can optionally check whether both sequences are of a
1101 particular type.
1102
1103* :meth:`assertDictEqual` compares two dictionaries and reports the
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001104 differences; it's now used by default when you compare two dictionaries
1105 using :meth:`assertEqual`. :meth:`assertDictContainsSubset` checks whether
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001106 all of the key/value pairs in *first* are found in *second*.
1107
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001108* :meth:`assertAlmostEqual` and :meth:`assertNotAlmostEqual` test
1109 whether *first* and *second* are approximately equal by computing
1110 their difference, rounding the result to an optionally-specified number
1111 of *places* (the default is 7), and comparing to zero.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001112
1113* :meth:`loadTestsFromName` properly honors the ``suiteClass`` attribute of
1114 the :class:`TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.)
1115
1116* A new hook, :meth:`addTypeEqualityFunc` takes a type object and a
1117 function. The :meth:`assertEqual` method will use the function
1118 when both of the objects being compared are of the specified type.
1119 This function should compare the two objects and raise an
1120 exception if they don't match; it's a good idea for the function
1121 to provide additional information about why the two objects are
1122 matching, much as the new sequence comparison methods do.
1123
1124:func:`unittest.main` now takes an optional ``exit`` argument.
1125If False ``main`` doesn't call :func:`sys.exit` allowing it to
1126be used from the interactive interpreter. :issue:`3379`.
1127
1128:class:`TestResult` has new :meth:`startTestRun` and
1129:meth:`stopTestRun` methods; called immediately before
1130and after a test run. :issue:`5728` by Robert Collins.
1131
1132With all these changes, the :file:`unittest.py` was becoming awkwardly
1133large, so the module was turned into a package and the code split into
1134several files (by Benjamin Peterson). This doesn't affect how the
1135module is imported.
1136
1137
1138.. _importlib-section:
1139
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001140importlib: Importing Modules
1141------------------------------
1142
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001143Python 3.1 includes the :mod:`importlib` package, a re-implementation
1144of the logic underlying Python's :keyword:`import` statement.
1145:mod:`importlib` is useful for implementors of Python interpreters and
Brett Cannonca2dc472009-12-22 02:37:37 +00001146to users who wish to write new importers that can participate in the
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001147import process. Python 2.7 doesn't contain the complete
1148:mod:`importlib` package, but instead has a tiny subset that contains
1149a single function, :func:`import_module`.
1150
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001151``import_module(name, package=None)`` imports a module. *name* is
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +00001152a string containing the module or package's name. It's possible to do
1153relative imports by providing a string that begins with a ``.``
1154character, such as ``..utils.errors``. For relative imports, the
1155*package* argument must be provided and is the name of the package that
1156will be used as the anchor for
1157the relative import. :func:`import_module` both inserts the imported
1158module into ``sys.modules`` and returns the module object.
1159
1160Here are some examples::
1161
1162 >>> from importlib import import_module
1163 >>> anydbm = import_module('anydbm') # Standard absolute import
1164 >>> anydbm
1165 <module 'anydbm' from '/p/python/Lib/anydbm.py'>
1166 >>> # Relative import
1167 >>> sysconfig = import_module('..sysconfig', 'distutils.command')
1168 >>> sysconfig
1169 <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'>
1170
1171:mod:`importlib` was implemented by Brett Cannon and introduced in
1172Python 3.1.
1173
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001174
Andrew M. Kuchlinga17cd4a2009-01-31 02:50:09 +00001175ttk: Themed Widgets for Tk
1176--------------------------
1177
1178Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
1179widgets but have a more customizable appearance and can therefore more
1180closely resemble the native platform's widgets. This widget
1181set was originally called Tile, but was renamed to Ttk (for "themed Tk")
1182on being added to Tcl/Tck release 8.5.
1183
1184XXX write a brief discussion and an example here.
1185
1186The :mod:`ttk` module was written by Guilherme Polo and added in
1187:issue:`2983`. An alternate version called ``Tile.py``, written by
1188Martin Franklin and maintained by Kevin Walzer, was proposed for
1189inclusion in :issue:`2618`, but the authors argued that Guilherme
1190Polo's work was more comprehensive.
1191
Georg Brandl0516f812009-11-18 18:52:35 +00001192
1193Deprecations and Removals
1194=========================
1195
1196* :func:`contextlib.nested`, which allows handling more than one context manager
1197 with one :keyword:`with` statement, has been deprecated; :keyword:`with`
1198 supports multiple context managers syntactically now.
1199
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001200.. ======================================================================
1201
1202
1203Build and C API Changes
1204=======================
1205
1206Changes to Python's build process and to the C API include:
1207
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +00001208* If you use the :file:`.gdbinit` file provided with Python,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001209 the "pyo" macro in the 2.7 version now works correctly when the thread being
1210 debugged doesn't hold the GIL; the macro now acquires it before printing.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001211 (Contributed by Victor Stinner; :issue:`3632`.)
1212
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +00001213* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001214 worker thread submit notifications to the main Python thread. This
1215 is particularly useful for asynchronous IO operations.
1216 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
1217
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001218* New function: :cfunc:`PyCode_NewEmpty` creates an empty code object;
1219 only the filename, function name, and first line number are required.
1220 This is useful to extension modules that are attempting to
1221 construct a more useful traceback stack. Previously such
1222 extensions needed to call :cfunc:`PyCode_New`, which had many
1223 more arguments. (Added by Jeffrey Yasskin.)
1224
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001225* New function: :cfunc:`PyErr_NewExceptionWithDoc` creates a new
1226 exception class, just as the existing :cfunc:`PyErr_NewException` does,
1227 but takes an extra ``char *`` argument containing the docstring for the
1228 new exception class. (Added by the 'lekma' user on the Python bug tracker;
1229 :issue:`7033`.)
1230
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001231* New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object
1232 and returns the line number that the frame is currently executing.
1233 Previously code would need to get the index of the bytecode
1234 instruction currently executing, and then look up the line number
1235 corresponding to that address. (Added by Jeffrey Yasskin.)
1236
Andrew M. Kuchling17ae2ba2010-02-03 02:19:14 +00001237* New functions: :cfunc:`PyLong_AsLongAndOverflow` and
1238 :cfunc:`PyLong_AsLongLongAndOverflow` approximates a Python long
1239 integer as a C :ctype:`long` or :ctype:`long long`.
1240 If the number is too large to fit into
1241 the output type, an *overflow* flag is set and returned to the caller.
1242 (Contributed by Case Van Horsen; :issue:`7528` and :issue:`7767`.)
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001243
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001244* New function: stemming from the rewrite of string-to-float conversion,
1245 a new :cfunc:`PyOS_string_to_double` function was added. The old
1246 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions
1247 are now deprecated.
1248
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001249* New macros: the Python header files now define the following macros:
1250 :cmacro:`Py_ISALNUM`,
1251 :cmacro:`Py_ISALPHA`,
1252 :cmacro:`Py_ISDIGIT`,
1253 :cmacro:`Py_ISLOWER`,
1254 :cmacro:`Py_ISSPACE`,
1255 :cmacro:`Py_ISUPPER`,
1256 :cmacro:`Py_ISXDIGIT`,
1257 and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`.
1258 All of these functions are analogous to the C
1259 standard macros for classifying characters, but ignore the current
1260 locale setting, because in
1261 several places Python needs to analyze characters in a
1262 locale-independent way. (Added by Eric Smith;
1263 :issue:`5793`.)
1264
1265 .. XXX these macros don't seem to be described in the c-api docs.
1266
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001267* New format codes: the :cfunc:`PyFormat_FromString`,
1268 :cfunc:`PyFormat_FromStringV`, and :cfunc:`PyErr_Format` now
1269 accepts ``%lld`` and ``%llu`` format codes for displaying values of
1270 C's :ctype:`long long` types.
1271 (Contributed by Mark Dickinson; :issue:`7228`.)
1272
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001273* The complicated interaction between threads and process forking has
1274 been changed. Previously, the child process created by
1275 :func:`os.fork` might fail because the child is created with only a
1276 single thread running, the thread performing the :func:`os.fork`.
1277 If other threads were holding a lock, such as Python's import lock,
1278 when the fork was performed, the lock would still be marked as
1279 "held" in the new process. But in the child process nothing would
1280 ever release the lock, since the other threads weren't replicated,
1281 and the child process would no longer be able to perform imports.
1282
1283 Python 2.7 now acquires the import lock before performing an
1284 :func:`os.fork`, and will also clean up any locks created using the
1285 :mod:`threading` module. C extension modules that have internal
1286 locks, or that call :cfunc:`fork()` themselves, will not benefit
1287 from this clean-up.
1288
1289 (Fixed by Thomas Wouters; :issue:`1590864`.)
1290
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001291* The :cfunc:`Py_Finalize` function now calls the internal
1292 :func:`threading._shutdown` function; this prevents some exceptions from
1293 being raised when an interpreter shuts down.
1294 (Patch by Adam Olsen; :issue:`1722344`.)
1295
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001296* Global symbols defined by the :mod:`ctypes` module are now prefixed
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001297 with ``Py``, or with ``_ctypes``. (Implemented by Thomas
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001298 Heller; :issue:`3102`.)
1299
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001300* New configure option: the :option:`--with-system-expat` switch allows
1301 building the :mod:`pyexpat` module to use the system Expat library.
1302 (Contributed by Arfrever Frehtes Taifersar Arahesis; :issue:`7609`.)
1303
1304* New configure option: Compiling Python with the
1305 :option:`--with-valgrind` option will now disable the pymalloc
1306 allocator, which is difficult for the Valgrind to analyze correctly.
1307 Valgrind will therefore be better at detecting memory leaks and
1308 overruns. (Contributed by James Henstridge; :issue:`2422`.)
1309
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00001310* New configure option: you can now supply no arguments to
1311 :option:`--with-dbmliborder=` in order to build none of the various
1312 DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis;
1313 :issue:`6491`.)
1314
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001315* The :program:`configure` script now checks for floating-point rounding bugs
1316 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
1317 preprocessor definition. No code currently uses this definition,
1318 but it's available if anyone wishes to use it.
1319 (Added by Mark Dickinson; :issue:`2937`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001320
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001321* The build process now creates the necessary files for pkg-config
1322 support. (Contributed by Clinton Roy; :issue:`3585`.)
1323
1324* The build process now supports Subversion 1.7. (Contributed by
1325 Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.)
1326
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001327
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001328.. ======================================================================
1329
1330Port-Specific Changes: Windows
1331-----------------------------------
1332
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +00001333* The :mod:`msvcrt` module now contains some constants from
1334 the :file:`crtassem.h` header file:
1335 :data:`CRT_ASSEMBLY_VERSION`,
1336 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
1337 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +00001338 (Contributed by David Cournapeau; :issue:`4365`.)
1339
1340* The new :cfunc:`_beginthreadex` API is used to start threads, and
1341 the native thread-local storage functions are now used.
1342 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001343
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001344* The :func:`os.listdir` function now correctly fails
1345 for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.)
1346
Andrew M. Kuchling3c8a24e2009-12-29 23:41:04 +00001347* The :mod:`mimelib` module will now read the MIME database from
1348 the Windows registry when initializing.
1349 (Patch by Gabriel Genellina; :issue:`4969`.)
1350
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001351.. ======================================================================
1352
1353Port-Specific Changes: Mac OS X
1354-----------------------------------
1355
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001356* The path ``/Library/Python/2.7/site-packages`` is now appended to
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001357 ``sys.path``, in order to share added packages between the system
1358 installation and a user-installed copy of the same version.
1359 (Changed by Ronald Oussoren; :issue:`4865`.)
1360
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001361
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001362Other Changes and Fixes
1363=======================
1364
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001365* Two benchmark scripts, :file:`iobench` and :file:`ccbench`, were
1366 added to the :file:`Tools` directory. :file:`iobench` measures the
Antoine Pitroudde96e62010-02-08 20:25:47 +00001367 speed of built-in file I/O objects (as returned by :func:`open`)
1368 while performing various operations, and :file:`ccbench` is a concurrency
1369 benchmark that tries to measure computing throughput and thread switching
1370 latency when performing several tasks using a varying number of threads.
Andrew M. Kuchling0e7123f2010-02-08 13:22:24 +00001371
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001372* When importing a module from a :file:`.pyc` or :file:`.pyo` file
1373 with an existing :file:`.py` counterpart, the :attr:`co_filename`
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001374 attributes of the resulting code objects are overwritten when the
1375 original filename is obsolete. This can happen if the file has been
1376 renamed, moved, or is accessed through different paths. (Patch by
1377 Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.)
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001378
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001379* The :file:`regrtest.py` script now takes a :option:`--randseed=`
1380 switch that takes an integer that will be used as the random seed
1381 for the :option:`-r` option that executes tests in random order.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001382 The :option:`-r` option also reports the seed that was used
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001383 (Added by Collin Winter.)
1384
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001385* Another :file:`regrtest.py` switch is :option:`-j`, which
1386 takes an integer specifying how many tests run in parallel. This
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001387 allows reducing the total runtime on multi-core machines.
Antoine Pitrou4698d992009-05-31 14:20:14 +00001388 This option is compatible with several other options, including the
1389 :option:`-R` switch which is known to produce long runtimes.
Andrew M. Kuchling91e0db82009-12-31 16:17:05 +00001390 (Added by Antoine Pitrou, :issue:`6152`.) This can also be used
1391 with a new :option:`-F` switch that runs selected tests in a loop
1392 until they fail. (Added by Antoine Pitrou; :issue:`7312`.)
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001393
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001394.. ======================================================================
1395
1396Porting to Python 2.7
1397=====================
1398
1399This section lists previously described changes and other bugfixes
1400that may require changes to your code:
1401
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001402* When using :class:`Decimal` instances with a string's
1403 :meth:`format` method, the default alignment was previously
1404 left-alignment. This has been changed to right-alignment, which might
1405 change the output of your programs.
1406 (Changed by Mark Dickinson; :issue:`6857`.)
1407
1408 Another :meth:`format`-related change: the default precision used
1409 for floating-point and complex numbers was changed from 6 decimal
1410 places to 12, which matches the precision used by :func:`str`.
1411 (Changed by Eric Smith; :issue:`5920`.)
1412
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00001413* Because of an optimization for the :keyword:`with` statement, the special
1414 methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's
1415 type, and cannot be directly attached to the object's instance. This
Amaury Forgeot d'Arcd81333c2009-06-10 20:30:19 +00001416 affects new-style classes (derived from :class:`object`) and C extension
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00001417 types. (:issue:`6101`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001418
Andrew M. Kuchlingb4a4f512009-12-29 20:10:16 +00001419* The :meth:`readline` method of :class:`StringIO` objects now does
1420 nothing when a negative length is requested, as other file-like
1421 objects do. (:issue:`7348`).
1422
Andrew M. Kuchlinge41e4db2010-02-18 14:16:48 +00001423In the standard library:
1424
1425* The ElementTree library, :mod:`xml.etree`, no longer escapes
1426 ampersands and angle brackets when outputting an XML processing
1427 instruction (which looks like `<?xml-stylesheet href="#style1"?>`)
1428 or comment (which looks like `<!-- comment -->`).
1429 (Patch by Neil Muller; :issue:`2746`.)
1430
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001431For C extensions:
1432
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00001433* C extensions that use integer format codes with the ``PyArg_Parse*``
1434 family of functions will now raise a :exc:`TypeError` exception
1435 instead of triggering a :exc:`DeprecationWarning` (:issue:`5080`).
1436
Andrew M. Kuchlinga7f59472009-12-31 16:38:53 +00001437* Use the new :cfunc:`PyOS_string_to_double` function instead of the old
1438 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions,
1439 which are now deprecated.
1440
Andrew M. Kuchling7f8ebdb2010-01-03 01:15:21 +00001441
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001442.. ======================================================================
1443
1444
1445.. _acks27:
1446
1447Acknowledgements
1448================
1449
1450The author would like to thank the following people for offering
1451suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchling8f254e72009-12-08 02:37:05 +00001452article: Ryan Lovett, Hugh Secker-Walker.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001453