blob: 856354a8ec6a6c6d57c50b51ff06fc0d53b0eedd [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. Kuchling71d5c282009-03-30 22:30:20 +00009.. Fix accents on Kristjan Valur Jonsson, Fuerstenau, Tarek Ziade.
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. Kuchling5a9c40b2009-10-05 22:30:22 +000052This article explains the new features in Python 2.7. No release
53schedule has been decided yet for 2.7; the schedule will eventually be
54described in :pep:`373`.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000055
56.. Compare with previous release in 2 - 3 sentences here.
57 add hyperlink when the documentation becomes available online.
58
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000059.. _whatsnew27-python31:
60
61Python 3.1 Features
62=======================
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +000063
64Much as Python 2.6 incorporated features from Python 3.0,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000065version 2.7 incorporates some of the new features
66in Python 3.1. The 2.x series continues to provide tools
67for migrating to the 3.x series.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +000068
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000069A partial list of 3.1 features that were backported to 2.7:
70
71* A version of the :mod:`io` library, rewritten in C for performance.
72* The ordered-dictionary type described in :ref:`pep-0372`.
73* The new format specified described in :ref:`pep-0378`.
74* The :class:`memoryview` object.
75* A small subset of the :mod:`importlib` module `described below <#importlib-section>`__.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +000076
77One porting change: the :option:`-3` switch now automatically
78enables the :option:`-Qwarn` switch that causes warnings
79about using classic division with integers and long integers.
80
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000081Other new Python3-mode warnings include:
82
83* :func:`operator.isCallable` and :func:`operator.sequenceIncludes`,
84 which are not supported in 3.x.
85
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000086.. ========================================================================
87.. Large, PEP-level features and changes should be described here.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +000088.. ========================================================================
89
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000090.. _pep-0372:
91
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +000092PEP 372: Adding an ordered dictionary to collections
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +000093====================================================
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +000094
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +000095Regular Python dictionaries iterate over key/value pairs in arbitrary order.
96Over the years, a number of authors have written alternative implementations
97that remember the order that the keys were originally inserted. Based on
98the experiences from those implementations, a new
99:class:`collections.OrderedDict` class has been introduced.
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000100
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000101The :class:`OrderedDict` API is substantially the same as regular dictionaries
102but will iterate over keys and values in a guaranteed order depending on
103when a key was first inserted::
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000104
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000105 >>> from collections import OrderedDict
106 >>> d = OrderedDict([('first', 1), ('second', 2),
107 ... ('third', 3)])
108 >>> d.items()
109 [('first', 1), ('second', 2), ('third', 3)]
110
111If a new entry overwrites an existing entry, the original insertion
112position is left unchanged::
113
114 >>> d['second'] = 4
115 >>> d.items()
116 [('first', 1), ('second', 4), ('third', 3)]
117
118Deleting an entry and reinserting it will move it to the end::
119
120 >>> del d['second']
121 >>> d['second'] = 5
122 >>> d.items()
123 [('first', 1), ('third', 3), ('second', 5)]
124
125The :meth:`popitem` method has an optional *last* argument
126that defaults to True. If *last* is True, the most recently
127added key is returned and removed; if it's False, the
128oldest key is selected::
129
130 >>> od = OrderedDict([(x,0) for x in range(20)])
131 >>> od.popitem()
132 (19, 0)
133 >>> od.popitem()
134 (18, 0)
135 >>> od.popitem(False)
136 (0, 0)
137 >>> od.popitem(False)
138 (1, 0)
139
140Comparing two ordered dictionaries checks both the keys and values,
141and requires that the insertion order was the same::
142
143 >>> od1 = OrderedDict([('first', 1), ('second', 2),
144 ... ('third', 3)])
145 >>> od2 = OrderedDict([('third', 3), ('first', 1),
146 ... ('second', 2)])
147 >>> od1==od2
148 False
149 >>> # Move 'third' key to the end
150 >>> del od2['third'] ; od2['third'] = 3
151 >>> od1==od2
152 True
153
154Comparing an :class:`OrderedDict` with a regular dictionary
155ignores the insertion order and just compares the keys and values.
156
157How does the :class:`OrderedDict` work? It maintains a doubly-linked
158list of keys, appending new keys to the list as they're inserted. A
159secondary dictionary maps keys to their corresponding list node, so
160deletion doesn't have to traverse the entire linked list and therefore
161remains O(1).
162
163.. XXX check O(1)-ness with Raymond
164
165The standard library now supports use of ordered dictionaries in several
166modules. The :mod:`configparser` module uses them by default. This lets
167configuration files be read, modified, and then written back in their original
168order. The *_asdict()* method for :func:`collections.namedtuple` now
169returns an ordered dictionary with the values appearing in the same order as
170the underlying tuple indicies. The :mod:`json` module is being built-out with
171an *object_pairs_hook* to allow OrderedDicts to be built by the decoder.
172Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_.
173
174
175.. _pep-0378:
176
177PEP 378: Format Specifier for Thousands Separator
178====================================================
179
180To make program output more readable, it can be useful to add
181separators to large numbers and render them as
18218,446,744,073,709,551,616 instead of 18446744073709551616.
183
184The fully general solution for doing this is the :mod:`locale` module,
185which can use different separators ("," in North America, "." in
186Europe) and different grouping sizes, but :mod:`locale` is complicated
187to use and unsuitable for multi-threaded applications where different
188threads are producing output for different locales.
189
190Therefore, a simple comma-grouping mechanism has been added to the
191mini-language used by the string :meth:`format` method. When
192formatting a floating-point number, simply include a comma between the
193width and the precision::
194
195 >>> '{:20,.2}'.format(f)
196 '18,446,744,073,709,551,616.00'
197
198This mechanism is not adaptable at all; commas are always used as the
199separator and the grouping is always into three-digit groups. The
200comma-formatting mechanism isn't as general as the :mod:`locale`
201module, but it's easier to use.
202
Andrew M. Kuchling85ea4bf2009-10-05 22:45:39 +0000203.. XXX "Format String Syntax" in string.rst could use many more examples.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000204
205.. seealso::
206
207 :pep:`378` - Format Specifier for Thousands Separator
208 PEP written by Raymond Hettinger; implemented by Eric Smith.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000209
210Other Language Changes
211======================
212
213Some smaller changes made to the core Python language are:
214
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000215* The :keyword:`with` statement can now use multiple context managers
216 in one statement. Context managers are processed from left to right
217 and each one is treated as beginning a new :keyword:`with` statement.
218 This means that::
219
220 with A() as a, B() as b:
221 ... suite of statements ...
222
223 is equivalent to::
224
225 with A() as a:
226 with B() as b:
227 ... suite of statements ...
228
229 The :func:`contextlib.nested` function provides a very similar
230 function, so it's no longer necessary and has been deprecated.
231
232 (Proposed in http://codereview.appspot.com/53094; implemented by
233 Georg Brandl.)
234
235* The :meth:`str.format` method now supports automatic numbering of the replacement
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +0000236 fields. This makes using :meth:`str.format` more closely resemble using
237 ``%s`` formatting::
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000238
239 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
240 '2009:4:Sunday'
241 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
242 '2009:4:Sunday'
243
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +0000244 The auto-numbering takes the fields from left to right, so the first ``{...}``
245 specifier will use the first argument to :meth:`str.format`, the next
246 specifier will use the next argument, and so on. You can't mix auto-numbering
247 and explicit numbering -- either number all of your specifier fields or none
248 of them -- but you can mix auto-numbering and named fields, as in the second
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000249 example above. (Contributed by Eric Smith; :issue:`5237`.)
250
251 Complex numbers now correctly support usage with :func:`format`.
252 Specifying a precision or comma-separation applies to both the real
253 and imaginary parts of the number, but a specified field width and
254 alignment is applied to the whole of the resulting ``1.5+3j``
255 output. (Contributed by Eric Smith; :issue:`1588`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000256
Mark Dickinson1a707982008-12-17 16:14:37 +0000257* The :func:`int` and :func:`long` types gained a ``bit_length``
Georg Brandl64e1c752009-04-11 18:19:27 +0000258 method that returns the number of bits necessary to represent
Mark Dickinson1a707982008-12-17 16:14:37 +0000259 its argument in binary::
260
261 >>> n = 37
262 >>> bin(37)
263 '0b100101'
264 >>> n.bit_length()
265 6
266 >>> n = 2**123-1
267 >>> n.bit_length()
268 123
269 >>> (n+1).bit_length()
270 124
271
272 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
273
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000274* Conversions from long integers and regular integers to floating
275 point now round differently, returning the floating-point number
276 closest to the number. This doesn't matter for small integers that
277 can be converted exactly, but for large numbers that will
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000278 unavoidably lose precision, Python 2.7 now approximates more
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000279 closely. For example, Python 2.6 computed the following::
280
281 >>> n = 295147905179352891391
282 >>> float(n)
283 2.9514790517935283e+20
284 >>> n - long(float(n))
285 65535L
286
287 Python 2.7's floating-point result is larger, but much closer to the
288 true value::
289
290 >>> n = 295147905179352891391
291 >>> float(n)
292 2.9514790517935289e+20
293 >>> n-long(float(n)
294 ... )
295 -1L
296
297 (Implemented by Mark Dickinson; :issue:`3166`.)
298
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000299* The :class:`bytearray` type's :meth:`translate` method now accepts
300 ``None`` as its first argument. (Fixed by Georg Brandl;
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000301 :issue:`4759`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000302
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000303* When using ``@classmethod`` and ``@staticmethod`` to wrap
304 methods as class or static methods, the wrapper object now
305 exposes the wrapped function as their :attr:`__func__` attribute.
306 (Contributed by Amaury Forgeot d'Arc, after a suggestion by
307 George Sakkis; :issue:`5982`.)
308
309* A new encoding named "cp720", used primarily for Arabic text, is now
310 supported. (Contributed by Alexander Belchenko and Amaury Forgeot
311 d'Arc; :issue:`1616979`.)
312
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000313.. ======================================================================
314
315
316Optimizations
317-------------
318
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000319Several performance enhancements have been added:
320
321.. * A new :program:`configure` option, :option:`--with-computed-gotos`,
322 compiles the main bytecode interpreter loop using a new dispatch
323 mechanism that gives speedups of up to 20%, depending on the system
324 and benchmark. The new mechanism is only supported on certain
325 compilers, such as gcc, SunPro, and icc.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000326
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000327* A new opcode was added to perform the initial setup for
328 :keyword:`with` statements, looking up the :meth:`__enter__` and
329 :meth:`__exit__` methods. (Contributed by Benjamin Peterson.)
330
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000331* The garbage collector now performs better when many objects are
332 being allocated without deallocating any. A full garbage collection
333 pass is only performed when the middle generation has been collected
334 10 times and when the number of survivor objects from the middle
335 generation exceeds 10% of the number of objects in the oldest
336 generation. The second condition was added to reduce the number
337 of full garbage collections as the number of objects on the heap grows,
338 avoiding quadratic performance when allocating very many objects.
339 (Suggested by Martin von Loewis and implemented by Antoine Pitrou;
340 :issue:`4074`.)
341
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000342* The garbage collector tries to avoid tracking simple containers
343 which can't be part of a cycle. In Python 2.7, this is now true for
344 tuples and dicts containing atomic types (such as ints, strings,
345 etc.). Transitively, a dict containing tuples of atomic types won't
346 be tracked either. This helps reduce the cost of each
347 garbage collection by decreasing the number of objects to be
348 considered and traversed by the collector.
Antoine Pitrouc18f6b02009-03-28 19:10:13 +0000349 (Contributed by Antoine Pitrou; :issue:`4688`.)
350
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000351* Long integers are now stored internally either in base 2**15 or in base
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000352 2**30, the base being determined at build time. Previously, they
353 were always stored in base 2**15. Using base 2**30 gives
354 significant performance improvements on 64-bit machines, but
355 benchmark results on 32-bit machines have been mixed. Therefore,
356 the default is to use base 2**30 on 64-bit machines and base 2**15
357 on 32-bit machines; on Unix, there's a new configure option
358 :option:`--enable-big-digits` that can be used to override this default.
359
360 Apart from the performance improvements this change should be
361 invisible to end users, with one exception: for testing and
362 debugging purposes there's a new structseq ``sys.long_info`` that
363 provides information about the internal format, giving the number of
364 bits per digit and the size in bytes of the C type used to store
365 each digit::
366
367 >>> import sys
368 >>> sys.long_info
369 sys.long_info(bits_per_digit=30, sizeof_digit=4)
370
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000371 (Contributed by Mark Dickinson; :issue:`4258`.)
372
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000373 Another set of changes made long objects a few bytes smaller: 2 bytes
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000374 smaller on 32-bit systems and 6 bytes on 64-bit.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000375 (Contributed by Mark Dickinson; :issue:`5260`.)
376
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000377* The division algorithm for long integers has been made faster
378 by tightening the inner loop, doing shifts instead of multiplications,
379 and fixing an unnecessary extra iteration.
380 Various benchmarks show speedups of between 50% and 150% for long
381 integer divisions and modulo operations.
382 (Contributed by Mark Dickinson; :issue:`5512`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000383
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000384* The implementation of ``%`` checks for the left-side operand being
385 a Python string and special-cases it; this results in a 1-3%
386 performance increase for applications that frequently use ``%``
387 with strings, such as templating libraries.
388 (Implemented by Collin Winter; :issue:`5176`.)
389
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000390* List comprehensions with an ``if`` condition are compiled into
391 faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
392 by Jeffrey Yasskin; :issue:`4715`.)
393
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000394* The :mod:`pickle` and :mod:`cPickle` modules now automatically
395 intern the strings used for attribute names, reducing memory usage
396 of the objects resulting from unpickling. (Contributed by Jake
397 McGuire; :issue:`5084`.)
398
399* The :mod:`cPickle` module now special-cases dictionaries,
400 nearly halving the time required to pickle them.
401 (Contributed by Collin Winter; :issue:`5670`.)
402
403* Converting an integer or long integer to a decimal string was made
404 faster by special-casing base 10 instead of using a generalized
405 conversion function that supports arbitrary bases.
406 (Patch by Gawain Bolton; :issue:`6713`.)
407
408
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000409.. ======================================================================
410
411New, Improved, and Deprecated Modules
412=====================================
413
414As in every release, Python's standard library received a number of
415enhancements and bug fixes. Here's a partial list of the most notable
416changes, sorted alphabetically by module name. Consult the
417:file:`Misc/NEWS` file in the source tree for a more complete list of
418changes, or look through the Subversion logs for all the details.
419
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000420* The :mod:`bdb` module's base debugging class :class:`Bdb`
421 gained a feature for skipping modules. The constructor
422 now takes an iterable containing glob-style patterns such as
423 ``django.*``; the debugger will not step into stack frames
424 from a module that matches one of these patterns.
425 (Contributed by Maru Newby after a suggestion by
426 Senthil Kumaran; :issue:`5142`.)
427
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000428* The :mod:`bz2` module's :class:`BZ2File` now supports the context
429 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
430 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
431
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000432* New class: the :class:`Counter` class in the :mod:`collections` module is
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000433 useful for tallying data. :class:`Counter` instances behave mostly
434 like dictionaries but return zero for missing keys instead of
Georg Brandlf6dab952009-04-28 21:48:35 +0000435 raising a :exc:`KeyError`:
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000436
Georg Brandlf6dab952009-04-28 21:48:35 +0000437 .. doctest::
438 :options: +NORMALIZE_WHITESPACE
439
440 >>> from collections import Counter
441 >>> c = Counter()
442 >>> for letter in 'here is a sample of english text':
443 ... c[letter] += 1
444 ...
445 >>> c
446 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
447 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
448 'p': 1, 'r': 1, 'x': 1})
449 >>> c['e']
450 5
451 >>> c['z']
452 0
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000453
454 There are two additional :class:`Counter` methods: :meth:`most_common`
455 returns the N most common elements and their counts, and :meth:`elements`
456 returns an iterator over the contained element, repeating each element
457 as many times as its count::
458
459 >>> c.most_common(5)
460 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
461 >>> c.elements() ->
462 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
463 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
464 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
Georg Brandlf6dab952009-04-28 21:48:35 +0000465 's', 's', 'r', 't', 't', 'x'
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000466
467 Contributed by Raymond Hettinger; :issue:`1696199`.
468
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000469 The new `OrderedDict` class is described in the earlier section
470 :ref:`pep-0372`.
471
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000472 The :class:`namedtuple` class now has an optional *rename* parameter.
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000473 If *rename* is true, field names that are invalid because they've
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000474 been repeated or that aren't legal Python identifiers will be
475 renamed to legal names that are derived from the field's
476 position within the list of fields:
477
Georg Brandlf6dab952009-04-28 21:48:35 +0000478 >>> from collections import namedtuple
479 >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000480 >>> T._fields
481 ('field1', '_1', '_2', 'field2')
482
483 (Added by Raymond Hettinger; :issue:`1818`.)
484
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000485 The :class:`deque` data type now exposes its maximum length as the
486 read-only :attr:`maxlen` attribute. (Added by Raymond Hettinger.)
487
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000488* The :mod:`ctypes` module now always converts ``None`` to a C NULL
489 pointer for arguments declared as pointers. (Changed by Thomas
490 Heller; :issue:`4606`.)
491
492* New method: the :class:`Decimal` class gained a
493 :meth:`from_float` class method that performs an exact conversion
494 of a floating-point number to a :class:`Decimal`.
495 Note that this is an **exact** conversion that strives for the
496 closest decimal approximation to the floating-point representation's value;
497 the resulting decimal value will therefore still include the inaccuracy,
498 if any.
499 For example, ``Decimal.from_float(0.1)`` returns
500 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
501 (Implemented by Raymond Hettinger; :issue:`4796`.)
502
503 The constructor for :class:`Decimal` now accepts non-European
504 Unicode characters, such as Arabic-Indic digits. (Contributed by
505 Mark Dickinson; :issue:`6595`.)
506
507 When using :class:`Decimal` instances with a string's
508 :meth:`format` method, the default alignment was previously
509 left-alignment. This has been changed to right-alignment, which seems
510 more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.)
511
512* Distutils is being more actively developed, thanks to Tarek Ziade
513 has taken over maintenance of the package. A new
514 :file:`setup.py` subcommand, ``check``, will
515 check that the arguments being passed to the :func:`setup` function
516 are complete and correct (:issue:`5732`).
517
518 :func:`distutils.sdist.add_defaults` now uses
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000519 *package_dir* and *data_files* to create the MANIFEST file.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000520 :mod:`distutils.sysconfig` now reads the :envvar:`AR` and
521 :envvar:`ARFLAGS` environment variables.
522
523 .. ARFLAGS done in #5941
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000524
525 It is no longer mandatory to store clear-text passwords in the
526 :file:`.pypirc` file when registering and uploading packages to PyPI. As long
527 as the username is present in that file, the :mod:`distutils` package will
528 prompt for the password if not present. (Added by Tarek Ziade,
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000529 based on an initial contribution by Nathan Van Gheem; :issue:`4394`.)
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000530
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000531 A Distutils setup can now specify that a C extension is optional by
532 setting the *optional* option setting to true. If this optional is
533 supplied, failure to build the extension will not abort the build
534 process, but instead simply not install the failing extension.
Georg Brandl64e1c752009-04-11 18:19:27 +0000535 (Contributed by Georg Brandl; :issue:`5583`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000536
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000537* The :class:`Fraction` class now accepts two rational numbers
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000538 as arguments to its constructor.
539 (Implemented by Mark Dickinson; :issue:`5812`.)
540
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000541* New function: the :mod:`gc` module's :func:`is_tracked` returns
542 true if a given instance is tracked by the garbage collector, false
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000543 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
544
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000545* The :mod:`gzip` module's :class:`GzipFile` now supports the context
546 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``.
547 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000548 It's now possible to override the modification time
549 recorded in a gzipped file by providing an optional timestamp to
550 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000551
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000552* The :mod:`hashlib` module was inconsistent about accepting
553 input as a Unicode object or an object that doesn't support
554 the buffer protocol. The behavior was different depending on
555 whether :mod:`hashlib` was using an external OpenSSL library
556 or its built-in implementations. Python 2.7 makes the
557 behavior consistent, always rejecting such objects by raising a
558 :exc:`TypeError`. (Fixed by Gregory P. Smith; :issue:`3745`.)
559
560* The default :class:`HTTPResponse` class used by the :mod:`httplib` module now
561 supports buffering, resulting in much faster reading of HTTP responses.
562 (Contributed by Kristjan Valur Jonsson; :issue:`4879`.)
563
564* The :mod:`imaplib` module now supports IPv6 addresses.
565 (Contributed by Derek Morr; :issue:`1655`.)
566
567* The :mod:`io` library has been upgraded to the version shipped with
568 Python 3.1. For 3.1, the I/O library was entirely rewritten in C
569 and is 2 to 20 times faster depending on the task at hand. The
570 original Python version was renamed to the :mod:`_pyio` module.
571
572 One minor resulting change: the :class:`io.TextIOBase` class now
573 has an :attr:`errors` attribute giving the error setting
574 used for encoding and decoding errors (one of ``'strict'``, ``'replace'``,
575 ``'ignore'``).
576
577 The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000578 an invalid file descriptor. (Implemented by Benjamin Peterson;
579 :issue:`4991`.)
580
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000581* New function: ``itertools.compress(*data*, *selectors*)`` takes two
582 iterators. Elements of *data* are returned if the corresponding
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000583 value in *selectors* is true::
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000584
585 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
586 A, C, E, F
587
588 New function: ``itertools.combinations_with_replacement(*iter*, *r*)``
589 returns all the possible *r*-length combinations of elements from the
590 iterable *iter*. Unlike :func:`combinations`, individual elements
591 can be repeated in the generated combinations::
592
593 itertools.combinations_with_replacement('abc', 2) =>
594 ('a', 'a'), ('a', 'b'), ('a', 'c'),
595 ('b', 'b'), ('b', 'c'), ('c', 'c')
596
597 Note that elements are treated as unique depending on their position
598 in the input, not their actual values.
599
600 The :class:`itertools.count` function now has a *step* argument that
601 allows incrementing by values other than 1. :func:`count` also
602 now allows keyword arguments, and using non-integer values such as
603 floats or :class:`Decimal` instances. (Implemented by Raymond
604 Hettinger; :issue:`5032`.)
605
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000606 :func:`itertools.combinations` and :func:`itertools.product` were
607 previously raising :exc:`ValueError` for values of *r* larger than
608 the input iterable. This was deemed a specification error, so they
609 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
610
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000611* The :mod:`json` module was upgraded to version 2.0.9 of the
612 simplejson package, which includes a C extension that makes
613 encoding and decoding faster.
614 (Contributed by Bob Ippolito; :issue:`4136`.)
615
616 To support the new :class:`OrderedDict` type, :func:`json.load`
617 now has an optional *object_pairs_hook* parameter that will be called
618 with any object literal that decodes to a list of pairs.
619 (Contributed by Raymond Hettinger; :issue:`5381`.)
620
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000621* New functions: the :mod:`math` module now has
622 a :func:`gamma` function.
623 (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.)
624
Andrew M. Kuchling24520b42009-04-09 11:22:47 +0000625* The :mod:`multiprocessing` module's :class:`Manager*` classes
626 can now be passed a callable that will be called whenever
627 a subprocess is started, along with a set of arguments that will be
628 passed to the callable.
629 (Contributed by lekma; :issue:`5585`.)
630
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000631* The :mod:`nntplib` module now supports IPv6 addresses.
632 (Contributed by Derek Morr; :issue:`1664`.)
633
Andrew M. Kuchling9cb42772009-01-21 02:15:43 +0000634* The :mod:`pydoc` module now has help for the various symbols that Python
635 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
636 (Contributed by David Laban; :issue:`4739`.)
637
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000638* The :mod:`re` module's :func:`split`, :func:`sub`, and :func:`subn`
639 now accept an optional *flags* argument, for consistency with the
640 other functions in the module. (Added by Gregory P. Smith.)
641
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000642* The :mod:`shutil` module's :func:`copyfile` and :func:`copytree`
643 functions now raises a :exc:`SpecialFileError` exception when
644 asked to copy a named pipe. Previously the code would treat
645 named pipes like a regular file by opening them for reading, and
646 this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.)
647
648* New functions: in the :mod:`site` module, three new functions
649 return various site- and user-specific paths.
650 :func:`getsitepackages` returns a list containing all
651 global site-packages directories, and
652 :func:`getusersitepackages` returns the path of the user's
653 site-packages directory.
654 :func:`getuserbase` returns the value of the :envvar:``USER_BASE``
655 environment variable, giving the path to a directory that can be used
656 to store data.
657 (Contributed by Tarek Ziade; :issue:`6693`.)
658
659* The :mod:`SocketServer` module's :class:`TCPServer` class now
660 has a :attr:`disable_nagle_algorithm` class attribute.
661 The default value is False; if overridden to be True,
662 new request connections will have the TCP_NODELAY option set to
663 prevent buffering many small sends into a single TCP packet.
664 (Contributed by Kristjan Valur Jonsson; :issue:`6192`.)
665
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000666* The :mod:`struct` module will no longer silently ignore overflow
667 errors when a value is too large for a particular integer format
668 code (one of ``bBhHiIlLqQ``); it now always raises a
669 :exc:`struct.error` exception. (Changed by Mark Dickinson;
670 :issue:`1523`.)
671
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000672* New function: the :mod:`subprocess` module's
673 :func:`check_output` runs a command with a specified set of arguments
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000674 and returns the command's output as a string when the command runs without
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +0000675 error, or raises a :exc:`CalledProcessError` exception otherwise.
676
677 ::
678
679 >>> subprocess.check_output(['df', '-h', '.'])
680 'Filesystem Size Used Avail Capacity Mounted on\n
681 /dev/disk0s2 52G 49G 3.0G 94% /\n'
682
683 >>> subprocess.check_output(['df', '-h', '/bogus'])
684 ...
685 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
686
687 (Contributed by Gregory P. Smith.)
688
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000689* New function: :func:`is_declared_global` in the :mod:`symtable` module
690 returns true for variables that are explicitly declared to be global,
691 false for ones that are implicitly global.
692 (Contributed by Jeremy Hylton.)
693
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000694* The ``sys.version_info`` value is now a named tuple, with attributes
695 named ``major``, ``minor``, ``micro``, ``releaselevel``, and ``serial``.
696 (Contributed by Ross Light; :issue:`4285`.)
697
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000698* The :mod:`tarfile` module now supports filtering the :class:`TarInfo`
699 objects being added to a tar file. When you call :meth:`TarFile.add`,
700 instance, you may supply an optional *filter* argument
701 that's a callable. The *filter* callable will be passed the
702 :class:`TarInfo` for every file being added, and can modify and return it.
703 If the callable returns ``None``, the file will be excluded from the
704 resulting archive. This is more powerful than the existing
705 *exclude* argument, which has therefore been deprecated.
706 (Added by Lars Gustaebel; :issue:`6856`.)
707
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000708* The :mod:`threading` module's :meth:`Event.wait` method now returns
709 the internal flag on exit. This means the method will usually
710 return true because :meth:`wait` is supposed to block until the
711 internal flag becomes true. The return value will only be false if
712 a timeout was provided and the operation timed out.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000713 (Contributed by Tim Lesher; :issue:`1674032`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000714
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000715* The :func:`is_zipfile` function in the :mod:`zipfile` module now
716 accepts a file object, in addition to the path names accepted in earlier
Andrew M. Kuchling9cb42772009-01-21 02:15:43 +0000717 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000718
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000719 :mod:`zipfile` now supports archiving empty directories and
720 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
721
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000722.. ======================================================================
723.. whole new modules get described in subsections here
724
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000725Unit Testing Enhancements
726---------------------------------
727
728The :mod:`unittest` module was enhanced in several ways.
729The progress messages now shows 'x' for expected failures
730and 'u' for unexpected successes when run in verbose mode.
731(Contributed by Benjamin Peterson.)
732Test cases can raise the :exc:`SkipTest` exception to skip a test.
733(:issue:`1034053`.)
734
735.. XXX describe test discovery (Contributed by Michael Foord; :issue:`6001`.)
736
737The error messages for :meth:`assertEqual`,
738:meth:`assertTrue`, and :meth:`assertFalse`
739failures now provide more information. If you set the
740:attr:`longMessage` attribute of your :class:`TestCase` classes to
741true, both the standard error message and any additional message you
742provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
743
744The :meth:`assertRaises` and :meth:`failUnlessRaises` methods now
745return a context handler when called without providing a callable
746object to run. For example, you can write this::
747
748 with self.assertRaises(KeyError):
749 raise ValueError
750
751(Implemented by Antoine Pitrou; :issue:`4444`.)
752
753The methods :meth:`addCleanup` and :meth:`doCleanups` were added.
754:meth:`addCleanup` allows you to add cleanup functions that
755will be called unconditionally (after :meth:`setUp` if
756:meth:`setUp` fails, otherwise after :meth:`tearDown`). This allows
757for much simpler resource allocation and deallocation during tests.
758:issue:`5679`
759
760A number of new methods were added that provide more specialized
761tests. Many of these methods were written by Google engineers
762for use in their test suites; Gregory P. Smith, Michael Foord, and
763GvR worked on merging them into Python's version of :mod:`unittest`.
764
765* :meth:`assertIsNone` and :meth:`assertIsNotNone` take one
766 expression and verify that the result is or is not ``None``.
767
768* :meth:`assertIs` and :meth:`assertIsNot` take two values and check
769 whether the two values evaluate to the same object or not.
770 (Added by Michael Foord; :issue:`2578`.)
771
772* :meth:`assertGreater`, :meth:`assertGreaterEqual`,
773 :meth:`assertLess`, and :meth:`assertLessEqual` compare
774 two quantities.
775
776* :meth:`assertMultiLineEqual` compares two strings, and if they're
777 not equal, displays a helpful comparison that highlights the
778 differences in the two strings.
779
780* :meth:`assertRegexpMatches` checks whether its first argument is a
781 string matching a regular expression provided as its second argument.
782
783* :meth:`assertRaisesRegexp` checks whether a particular exception
784 is raised, and then also checks that the string representation of
785 the exception matches the provided regular expression.
786
787* :meth:`assertIn` and :meth:`assertNotIn` tests whether
788 *first* is or is not in *second*.
789
790* :meth:`assertSameElements` tests whether two provided sequences
791 contain the same elements.
792
793* :meth:`assertSetEqual` compares whether two sets are equal, and
794 only reports the differences between the sets in case of error.
795
796* Similarly, :meth:`assertListEqual` and :meth:`assertTupleEqual`
797 compare the specified types and explain the differences.
798 More generally, :meth:`assertSequenceEqual` compares two sequences
799 and can optionally check whether both sequences are of a
800 particular type.
801
802* :meth:`assertDictEqual` compares two dictionaries and reports the
803 differences. :meth:`assertDictContainsSubset` checks whether
804 all of the key/value pairs in *first* are found in *second*.
805
806* :meth:`assertAlmostEqual` and :meth:`assertNotAlmostEqual` short-circuit
807 (automatically pass or fail without checking decimal places) if the objects
808 are equal.
809
810* :meth:`loadTestsFromName` properly honors the ``suiteClass`` attribute of
811 the :class:`TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.)
812
813* A new hook, :meth:`addTypeEqualityFunc` takes a type object and a
814 function. The :meth:`assertEqual` method will use the function
815 when both of the objects being compared are of the specified type.
816 This function should compare the two objects and raise an
817 exception if they don't match; it's a good idea for the function
818 to provide additional information about why the two objects are
819 matching, much as the new sequence comparison methods do.
820
821:func:`unittest.main` now takes an optional ``exit`` argument.
822If False ``main`` doesn't call :func:`sys.exit` allowing it to
823be used from the interactive interpreter. :issue:`3379`.
824
825:class:`TestResult` has new :meth:`startTestRun` and
826:meth:`stopTestRun` methods; called immediately before
827and after a test run. :issue:`5728` by Robert Collins.
828
829With all these changes, the :file:`unittest.py` was becoming awkwardly
830large, so the module was turned into a package and the code split into
831several files (by Benjamin Peterson). This doesn't affect how the
832module is imported.
833
834
835.. _importlib-section:
836
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000837importlib: Importing Modules
838------------------------------
839
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000840Python 3.1 includes the :mod:`importlib` package, a re-implementation
841of the logic underlying Python's :keyword:`import` statement.
842:mod:`importlib` is useful for implementors of Python interpreters and
843to user who wish to write new importers that can participate in the
844import process. Python 2.7 doesn't contain the complete
845:mod:`importlib` package, but instead has a tiny subset that contains
846a single function, :func:`import_module`.
847
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000848``import_module(name, package=None)`` imports a module. *name* is
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000849a string containing the module or package's name. It's possible to do
850relative imports by providing a string that begins with a ``.``
851character, such as ``..utils.errors``. For relative imports, the
852*package* argument must be provided and is the name of the package that
853will be used as the anchor for
854the relative import. :func:`import_module` both inserts the imported
855module into ``sys.modules`` and returns the module object.
856
857Here are some examples::
858
859 >>> from importlib import import_module
860 >>> anydbm = import_module('anydbm') # Standard absolute import
861 >>> anydbm
862 <module 'anydbm' from '/p/python/Lib/anydbm.py'>
863 >>> # Relative import
864 >>> sysconfig = import_module('..sysconfig', 'distutils.command')
865 >>> sysconfig
866 <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'>
867
868:mod:`importlib` was implemented by Brett Cannon and introduced in
869Python 3.1.
870
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000871
Andrew M. Kuchlinga17cd4a2009-01-31 02:50:09 +0000872ttk: Themed Widgets for Tk
873--------------------------
874
875Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
876widgets but have a more customizable appearance and can therefore more
877closely resemble the native platform's widgets. This widget
878set was originally called Tile, but was renamed to Ttk (for "themed Tk")
879on being added to Tcl/Tck release 8.5.
880
881XXX write a brief discussion and an example here.
882
883The :mod:`ttk` module was written by Guilherme Polo and added in
884:issue:`2983`. An alternate version called ``Tile.py``, written by
885Martin Franklin and maintained by Kevin Walzer, was proposed for
886inclusion in :issue:`2618`, but the authors argued that Guilherme
887Polo's work was more comprehensive.
888
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000889.. ======================================================================
890
891
892Build and C API Changes
893=======================
894
895Changes to Python's build process and to the C API include:
896
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +0000897* If you use the :file:`.gdbinit` file provided with Python,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000898 the "pyo" macro in the 2.7 version now works correctly when the thread being
899 debugged doesn't hold the GIL; the macro now acquires it before printing.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000900 (Contributed by Victor Stinner; :issue:`3632`.)
901
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000902* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000903 worker thread submit notifications to the main Python thread. This
904 is particularly useful for asynchronous IO operations.
905 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
906
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000907* New function: :cfunc:`PyCode_NewEmpty` creates an empty code object;
908 only the filename, function name, and first line number are required.
909 This is useful to extension modules that are attempting to
910 construct a more useful traceback stack. Previously such
911 extensions needed to call :cfunc:`PyCode_New`, which had many
912 more arguments. (Added by Jeffrey Yasskin.)
913
914* New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object
915 and returns the line number that the frame is currently executing.
916 Previously code would need to get the index of the bytecode
917 instruction currently executing, and then look up the line number
918 corresponding to that address. (Added by Jeffrey Yasskin.)
919
920* New macros: the Python header files now define the following macros:
921 :cmacro:`Py_ISALNUM`,
922 :cmacro:`Py_ISALPHA`,
923 :cmacro:`Py_ISDIGIT`,
924 :cmacro:`Py_ISLOWER`,
925 :cmacro:`Py_ISSPACE`,
926 :cmacro:`Py_ISUPPER`,
927 :cmacro:`Py_ISXDIGIT`,
928 and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`.
929 All of these functions are analogous to the C
930 standard macros for classifying characters, but ignore the current
931 locale setting, because in
932 several places Python needs to analyze characters in a
933 locale-independent way. (Added by Eric Smith;
934 :issue:`5793`.)
935
936 .. XXX these macros don't seem to be described in the c-api docs.
937
938* The complicated interaction between threads and process forking has
939 been changed. Previously, the child process created by
940 :func:`os.fork` might fail because the child is created with only a
941 single thread running, the thread performing the :func:`os.fork`.
942 If other threads were holding a lock, such as Python's import lock,
943 when the fork was performed, the lock would still be marked as
944 "held" in the new process. But in the child process nothing would
945 ever release the lock, since the other threads weren't replicated,
946 and the child process would no longer be able to perform imports.
947
948 Python 2.7 now acquires the import lock before performing an
949 :func:`os.fork`, and will also clean up any locks created using the
950 :mod:`threading` module. C extension modules that have internal
951 locks, or that call :cfunc:`fork()` themselves, will not benefit
952 from this clean-up.
953
954 (Fixed by Thomas Wouters; :issue:`1590864`.)
955
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000956* Global symbols defined by the :mod:`ctypes` module are now prefixed
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000957 with ``Py``, or with ``_ctypes``. (Implemented by Thomas
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000958 Heller; :issue:`3102`.)
959
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000960* The :program:`configure` script now checks for floating-point rounding bugs
961 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
962 preprocessor definition. No code currently uses this definition,
963 but it's available if anyone wishes to use it.
964 (Added by Mark Dickinson; :issue:`2937`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000965
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000966* The build process now creates the necessary files for pkg-config
967 support. (Contributed by Clinton Roy; :issue:`3585`.)
968
969* The build process now supports Subversion 1.7. (Contributed by
970 Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.)
971
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000972.. ======================================================================
973
974Port-Specific Changes: Windows
975-----------------------------------
976
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +0000977* The :mod:`msvcrt` module now contains some constants from
978 the :file:`crtassem.h` header file:
979 :data:`CRT_ASSEMBLY_VERSION`,
980 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
981 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000982 (Contributed by David Cournapeau; :issue:`4365`.)
983
984* The new :cfunc:`_beginthreadex` API is used to start threads, and
985 the native thread-local storage functions are now used.
986 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000987
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000988* The :func:`os.listdir` function now correctly fails
989 for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.)
990
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000991.. ======================================================================
992
993Port-Specific Changes: Mac OS X
994-----------------------------------
995
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000996* The path ``/Library/Python/2.7/site-packages`` is now appended to
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000997 ``sys.path``, in order to share added packages between the system
998 installation and a user-installed copy of the same version.
999 (Changed by Ronald Oussoren; :issue:`4865`.)
1000
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001001
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001002Other Changes and Fixes
1003=======================
1004
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001005* When importing a module from a :file:`.pyc` or :file:`.pyo` file
1006 with an existing :file:`.py` counterpart, the :attr:`co_filename`
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001007 attributes of the resulting code objects are overwritten when the
1008 original filename is obsolete. This can happen if the file has been
1009 renamed, moved, or is accessed through different paths. (Patch by
1010 Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.)
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001011
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001012* The :file:`regrtest.py` script now takes a :option:`--randseed=`
1013 switch that takes an integer that will be used as the random seed
1014 for the :option:`-r` option that executes tests in random order.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001015 The :option:`-r` option also reports the seed that was used
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001016 (Added by Collin Winter.)
1017
Antoine Pitrou4698d992009-05-31 14:20:14 +00001018* The :file:`regrtest.py` script now takes a :option:`-j` switch
1019 that takes an integer specifying how many tests run in parallel. This
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001020 allows reducing the total runtime on multi-core machines.
Antoine Pitrou4698d992009-05-31 14:20:14 +00001021 This option is compatible with several other options, including the
1022 :option:`-R` switch which is known to produce long runtimes.
1023 (Added by Antoine Pitrou, :issue:`6152`.)
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001024
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001025.. ======================================================================
1026
1027Porting to Python 2.7
1028=====================
1029
1030This section lists previously described changes and other bugfixes
1031that may require changes to your code:
1032
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001033* When using :class:`Decimal` instances with a string's
1034 :meth:`format` method, the default alignment was previously
1035 left-alignment. This has been changed to right-alignment, which might
1036 change the output of your programs.
1037 (Changed by Mark Dickinson; :issue:`6857`.)
1038
1039 Another :meth:`format`-related change: the default precision used
1040 for floating-point and complex numbers was changed from 6 decimal
1041 places to 12, which matches the precision used by :func:`str`.
1042 (Changed by Eric Smith; :issue:`5920`.)
1043
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00001044* Because of an optimization for the :keyword:`with` statement, the special
1045 methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's
1046 type, and cannot be directly attached to the object's instance. This
Amaury Forgeot d'Arcd81333c2009-06-10 20:30:19 +00001047 affects new-style classes (derived from :class:`object`) and C extension
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00001048 types. (:issue:`6101`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001049
1050.. ======================================================================
1051
1052
1053.. _acks27:
1054
1055Acknowledgements
1056================
1057
1058The author would like to thank the following people for offering
1059suggestions, corrections and assistance with various drafts of this
1060article: no one yet.
1061