blob: f611e05a8d52c8501d8818dff5163ec3f5a22e6d [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
Andrew M. Kuchling7fe65a02009-10-13 15:49:33 +0000174.. seealso::
175
176 :pep:`372` - Adding an ordered dictionary to collections
177 PEP written by Armin Ronacher and Raymond Hettinger;
178 implemented by Raymond Hettinger.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000179
180.. _pep-0378:
181
182PEP 378: Format Specifier for Thousands Separator
183====================================================
184
185To make program output more readable, it can be useful to add
186separators to large numbers and render them as
18718,446,744,073,709,551,616 instead of 18446744073709551616.
188
189The fully general solution for doing this is the :mod:`locale` module,
190which can use different separators ("," in North America, "." in
191Europe) and different grouping sizes, but :mod:`locale` is complicated
192to use and unsuitable for multi-threaded applications where different
193threads are producing output for different locales.
194
195Therefore, a simple comma-grouping mechanism has been added to the
196mini-language used by the string :meth:`format` method. When
197formatting a floating-point number, simply include a comma between the
198width and the precision::
199
200 >>> '{:20,.2}'.format(f)
201 '18,446,744,073,709,551,616.00'
202
203This mechanism is not adaptable at all; commas are always used as the
204separator and the grouping is always into three-digit groups. The
205comma-formatting mechanism isn't as general as the :mod:`locale`
206module, but it's easier to use.
207
Andrew M. Kuchling85ea4bf2009-10-05 22:45:39 +0000208.. XXX "Format String Syntax" in string.rst could use many more examples.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000209
210.. seealso::
211
212 :pep:`378` - Format Specifier for Thousands Separator
213 PEP written by Raymond Hettinger; implemented by Eric Smith.
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000214
215Other Language Changes
216======================
217
218Some smaller changes made to the core Python language are:
219
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000220* The :keyword:`with` statement can now use multiple context managers
221 in one statement. Context managers are processed from left to right
222 and each one is treated as beginning a new :keyword:`with` statement.
223 This means that::
224
225 with A() as a, B() as b:
226 ... suite of statements ...
227
228 is equivalent to::
229
230 with A() as a:
231 with B() as b:
232 ... suite of statements ...
233
234 The :func:`contextlib.nested` function provides a very similar
235 function, so it's no longer necessary and has been deprecated.
236
237 (Proposed in http://codereview.appspot.com/53094; implemented by
238 Georg Brandl.)
239
240* The :meth:`str.format` method now supports automatic numbering of the replacement
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +0000241 fields. This makes using :meth:`str.format` more closely resemble using
242 ``%s`` formatting::
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000243
244 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
245 '2009:4:Sunday'
246 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
247 '2009:4:Sunday'
248
Benjamin Petersonaa0a0b92009-04-11 20:27:15 +0000249 The auto-numbering takes the fields from left to right, so the first ``{...}``
250 specifier will use the first argument to :meth:`str.format`, the next
251 specifier will use the next argument, and so on. You can't mix auto-numbering
252 and explicit numbering -- either number all of your specifier fields or none
253 of them -- but you can mix auto-numbering and named fields, as in the second
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000254 example above. (Contributed by Eric Smith; :issue:`5237`.)
255
256 Complex numbers now correctly support usage with :func:`format`.
257 Specifying a precision or comma-separation applies to both the real
258 and imaginary parts of the number, but a specified field width and
259 alignment is applied to the whole of the resulting ``1.5+3j``
260 output. (Contributed by Eric Smith; :issue:`1588`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000261
Mark Dickinson1a707982008-12-17 16:14:37 +0000262* The :func:`int` and :func:`long` types gained a ``bit_length``
Georg Brandl64e1c752009-04-11 18:19:27 +0000263 method that returns the number of bits necessary to represent
Mark Dickinson1a707982008-12-17 16:14:37 +0000264 its argument in binary::
265
266 >>> n = 37
267 >>> bin(37)
268 '0b100101'
269 >>> n.bit_length()
270 6
271 >>> n = 2**123-1
272 >>> n.bit_length()
273 123
274 >>> (n+1).bit_length()
275 124
276
277 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
278
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000279* Conversions from long integers and regular integers to floating
280 point now round differently, returning the floating-point number
281 closest to the number. This doesn't matter for small integers that
282 can be converted exactly, but for large numbers that will
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000283 unavoidably lose precision, Python 2.7 now approximates more
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000284 closely. For example, Python 2.6 computed the following::
285
286 >>> n = 295147905179352891391
287 >>> float(n)
288 2.9514790517935283e+20
289 >>> n - long(float(n))
290 65535L
291
292 Python 2.7's floating-point result is larger, but much closer to the
293 true value::
294
295 >>> n = 295147905179352891391
296 >>> float(n)
297 2.9514790517935289e+20
298 >>> n-long(float(n)
299 ... )
300 -1L
301
302 (Implemented by Mark Dickinson; :issue:`3166`.)
303
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000304* The :class:`bytearray` type's :meth:`translate` method now accepts
305 ``None`` as its first argument. (Fixed by Georg Brandl;
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000306 :issue:`4759`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000307
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000308* When using ``@classmethod`` and ``@staticmethod`` to wrap
309 methods as class or static methods, the wrapper object now
310 exposes the wrapped function as their :attr:`__func__` attribute.
311 (Contributed by Amaury Forgeot d'Arc, after a suggestion by
312 George Sakkis; :issue:`5982`.)
313
314* A new encoding named "cp720", used primarily for Arabic text, is now
315 supported. (Contributed by Alexander Belchenko and Amaury Forgeot
316 d'Arc; :issue:`1616979`.)
317
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000318.. ======================================================================
319
320
321Optimizations
322-------------
323
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000324Several performance enhancements have been added:
325
326.. * A new :program:`configure` option, :option:`--with-computed-gotos`,
327 compiles the main bytecode interpreter loop using a new dispatch
328 mechanism that gives speedups of up to 20%, depending on the system
329 and benchmark. The new mechanism is only supported on certain
330 compilers, such as gcc, SunPro, and icc.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000331
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000332* A new opcode was added to perform the initial setup for
333 :keyword:`with` statements, looking up the :meth:`__enter__` and
334 :meth:`__exit__` methods. (Contributed by Benjamin Peterson.)
335
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000336* The garbage collector now performs better when many objects are
337 being allocated without deallocating any. A full garbage collection
338 pass is only performed when the middle generation has been collected
339 10 times and when the number of survivor objects from the middle
340 generation exceeds 10% of the number of objects in the oldest
341 generation. The second condition was added to reduce the number
342 of full garbage collections as the number of objects on the heap grows,
343 avoiding quadratic performance when allocating very many objects.
344 (Suggested by Martin von Loewis and implemented by Antoine Pitrou;
345 :issue:`4074`.)
346
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000347* The garbage collector tries to avoid tracking simple containers
348 which can't be part of a cycle. In Python 2.7, this is now true for
349 tuples and dicts containing atomic types (such as ints, strings,
350 etc.). Transitively, a dict containing tuples of atomic types won't
351 be tracked either. This helps reduce the cost of each
352 garbage collection by decreasing the number of objects to be
353 considered and traversed by the collector.
Antoine Pitrouc18f6b02009-03-28 19:10:13 +0000354 (Contributed by Antoine Pitrou; :issue:`4688`.)
355
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000356* Long integers are now stored internally either in base 2**15 or in base
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000357 2**30, the base being determined at build time. Previously, they
358 were always stored in base 2**15. Using base 2**30 gives
359 significant performance improvements on 64-bit machines, but
360 benchmark results on 32-bit machines have been mixed. Therefore,
361 the default is to use base 2**30 on 64-bit machines and base 2**15
362 on 32-bit machines; on Unix, there's a new configure option
363 :option:`--enable-big-digits` that can be used to override this default.
364
365 Apart from the performance improvements this change should be
366 invisible to end users, with one exception: for testing and
367 debugging purposes there's a new structseq ``sys.long_info`` that
368 provides information about the internal format, giving the number of
369 bits per digit and the size in bytes of the C type used to store
370 each digit::
371
372 >>> import sys
373 >>> sys.long_info
374 sys.long_info(bits_per_digit=30, sizeof_digit=4)
375
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000376 (Contributed by Mark Dickinson; :issue:`4258`.)
377
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000378 Another set of changes made long objects a few bytes smaller: 2 bytes
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000379 smaller on 32-bit systems and 6 bytes on 64-bit.
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000380 (Contributed by Mark Dickinson; :issue:`5260`.)
381
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000382* The division algorithm for long integers has been made faster
383 by tightening the inner loop, doing shifts instead of multiplications,
384 and fixing an unnecessary extra iteration.
385 Various benchmarks show speedups of between 50% and 150% for long
386 integer divisions and modulo operations.
387 (Contributed by Mark Dickinson; :issue:`5512`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000388
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000389* The implementation of ``%`` checks for the left-side operand being
390 a Python string and special-cases it; this results in a 1-3%
391 performance increase for applications that frequently use ``%``
392 with strings, such as templating libraries.
393 (Implemented by Collin Winter; :issue:`5176`.)
394
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000395* List comprehensions with an ``if`` condition are compiled into
396 faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
397 by Jeffrey Yasskin; :issue:`4715`.)
398
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000399* The :mod:`pickle` and :mod:`cPickle` modules now automatically
400 intern the strings used for attribute names, reducing memory usage
401 of the objects resulting from unpickling. (Contributed by Jake
402 McGuire; :issue:`5084`.)
403
404* The :mod:`cPickle` module now special-cases dictionaries,
405 nearly halving the time required to pickle them.
406 (Contributed by Collin Winter; :issue:`5670`.)
407
408* Converting an integer or long integer to a decimal string was made
409 faster by special-casing base 10 instead of using a generalized
410 conversion function that supports arbitrary bases.
411 (Patch by Gawain Bolton; :issue:`6713`.)
412
413
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000414.. ======================================================================
415
416New, Improved, and Deprecated Modules
417=====================================
418
419As in every release, Python's standard library received a number of
420enhancements and bug fixes. Here's a partial list of the most notable
421changes, sorted alphabetically by module name. Consult the
422:file:`Misc/NEWS` file in the source tree for a more complete list of
423changes, or look through the Subversion logs for all the details.
424
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000425* The :mod:`bdb` module's base debugging class :class:`Bdb`
426 gained a feature for skipping modules. The constructor
427 now takes an iterable containing glob-style patterns such as
428 ``django.*``; the debugger will not step into stack frames
429 from a module that matches one of these patterns.
430 (Contributed by Maru Newby after a suggestion by
431 Senthil Kumaran; :issue:`5142`.)
432
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000433* The :mod:`bz2` module's :class:`BZ2File` now supports the context
434 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
435 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
436
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000437* New class: the :class:`Counter` class in the :mod:`collections` module is
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000438 useful for tallying data. :class:`Counter` instances behave mostly
439 like dictionaries but return zero for missing keys instead of
Georg Brandlf6dab952009-04-28 21:48:35 +0000440 raising a :exc:`KeyError`:
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000441
Georg Brandlf6dab952009-04-28 21:48:35 +0000442 .. doctest::
443 :options: +NORMALIZE_WHITESPACE
444
445 >>> from collections import Counter
446 >>> c = Counter()
447 >>> for letter in 'here is a sample of english text':
448 ... c[letter] += 1
449 ...
450 >>> c
451 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
452 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
453 'p': 1, 'r': 1, 'x': 1})
454 >>> c['e']
455 5
456 >>> c['z']
457 0
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000458
459 There are two additional :class:`Counter` methods: :meth:`most_common`
460 returns the N most common elements and their counts, and :meth:`elements`
461 returns an iterator over the contained element, repeating each element
462 as many times as its count::
463
464 >>> c.most_common(5)
465 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
466 >>> c.elements() ->
467 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
468 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
469 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
Georg Brandlf6dab952009-04-28 21:48:35 +0000470 's', 's', 'r', 't', 't', 'x'
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000471
472 Contributed by Raymond Hettinger; :issue:`1696199`.
473
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000474 The new `OrderedDict` class is described in the earlier section
475 :ref:`pep-0372`.
476
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000477 The :class:`namedtuple` class now has an optional *rename* parameter.
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000478 If *rename* is true, field names that are invalid because they've
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000479 been repeated or that aren't legal Python identifiers will be
480 renamed to legal names that are derived from the field's
481 position within the list of fields:
482
Georg Brandlf6dab952009-04-28 21:48:35 +0000483 >>> from collections import namedtuple
484 >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000485 >>> T._fields
486 ('field1', '_1', '_2', 'field2')
487
488 (Added by Raymond Hettinger; :issue:`1818`.)
489
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000490 The :class:`deque` data type now exposes its maximum length as the
491 read-only :attr:`maxlen` attribute. (Added by Raymond Hettinger.)
492
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000493* The :mod:`ctypes` module now always converts ``None`` to a C NULL
494 pointer for arguments declared as pointers. (Changed by Thomas
495 Heller; :issue:`4606`.)
496
497* New method: the :class:`Decimal` class gained a
498 :meth:`from_float` class method that performs an exact conversion
499 of a floating-point number to a :class:`Decimal`.
500 Note that this is an **exact** conversion that strives for the
501 closest decimal approximation to the floating-point representation's value;
502 the resulting decimal value will therefore still include the inaccuracy,
503 if any.
504 For example, ``Decimal.from_float(0.1)`` returns
505 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
506 (Implemented by Raymond Hettinger; :issue:`4796`.)
507
508 The constructor for :class:`Decimal` now accepts non-European
509 Unicode characters, such as Arabic-Indic digits. (Contributed by
510 Mark Dickinson; :issue:`6595`.)
511
512 When using :class:`Decimal` instances with a string's
513 :meth:`format` method, the default alignment was previously
514 left-alignment. This has been changed to right-alignment, which seems
515 more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.)
516
517* Distutils is being more actively developed, thanks to Tarek Ziade
518 has taken over maintenance of the package. A new
519 :file:`setup.py` subcommand, ``check``, will
520 check that the arguments being passed to the :func:`setup` function
521 are complete and correct (:issue:`5732`).
522
523 :func:`distutils.sdist.add_defaults` now uses
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000524 *package_dir* and *data_files* to create the MANIFEST file.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000525 :mod:`distutils.sysconfig` now reads the :envvar:`AR` and
526 :envvar:`ARFLAGS` environment variables.
527
528 .. ARFLAGS done in #5941
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000529
530 It is no longer mandatory to store clear-text passwords in the
531 :file:`.pypirc` file when registering and uploading packages to PyPI. As long
532 as the username is present in that file, the :mod:`distutils` package will
533 prompt for the password if not present. (Added by Tarek Ziade,
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000534 based on an initial contribution by Nathan Van Gheem; :issue:`4394`.)
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000535
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000536 A Distutils setup can now specify that a C extension is optional by
537 setting the *optional* option setting to true. If this optional is
538 supplied, failure to build the extension will not abort the build
539 process, but instead simply not install the failing extension.
Georg Brandl64e1c752009-04-11 18:19:27 +0000540 (Contributed by Georg Brandl; :issue:`5583`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000541
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000542* The :class:`Fraction` class now accepts two rational numbers
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000543 as arguments to its constructor.
544 (Implemented by Mark Dickinson; :issue:`5812`.)
545
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000546* New function: the :mod:`gc` module's :func:`is_tracked` returns
547 true if a given instance is tracked by the garbage collector, false
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000548 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
549
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000550* The :mod:`gzip` module's :class:`GzipFile` now supports the context
551 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``.
552 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000553 It's now possible to override the modification time
554 recorded in a gzipped file by providing an optional timestamp to
555 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000556
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000557* The :mod:`hashlib` module was inconsistent about accepting
558 input as a Unicode object or an object that doesn't support
559 the buffer protocol. The behavior was different depending on
560 whether :mod:`hashlib` was using an external OpenSSL library
561 or its built-in implementations. Python 2.7 makes the
562 behavior consistent, always rejecting such objects by raising a
563 :exc:`TypeError`. (Fixed by Gregory P. Smith; :issue:`3745`.)
564
565* The default :class:`HTTPResponse` class used by the :mod:`httplib` module now
566 supports buffering, resulting in much faster reading of HTTP responses.
567 (Contributed by Kristjan Valur Jonsson; :issue:`4879`.)
568
569* The :mod:`imaplib` module now supports IPv6 addresses.
570 (Contributed by Derek Morr; :issue:`1655`.)
571
572* The :mod:`io` library has been upgraded to the version shipped with
573 Python 3.1. For 3.1, the I/O library was entirely rewritten in C
574 and is 2 to 20 times faster depending on the task at hand. The
575 original Python version was renamed to the :mod:`_pyio` module.
576
577 One minor resulting change: the :class:`io.TextIOBase` class now
578 has an :attr:`errors` attribute giving the error setting
579 used for encoding and decoding errors (one of ``'strict'``, ``'replace'``,
580 ``'ignore'``).
581
582 The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000583 an invalid file descriptor. (Implemented by Benjamin Peterson;
584 :issue:`4991`.)
585
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000586* New function: ``itertools.compress(*data*, *selectors*)`` takes two
587 iterators. Elements of *data* are returned if the corresponding
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000588 value in *selectors* is true::
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000589
590 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
591 A, C, E, F
592
593 New function: ``itertools.combinations_with_replacement(*iter*, *r*)``
594 returns all the possible *r*-length combinations of elements from the
595 iterable *iter*. Unlike :func:`combinations`, individual elements
596 can be repeated in the generated combinations::
597
598 itertools.combinations_with_replacement('abc', 2) =>
599 ('a', 'a'), ('a', 'b'), ('a', 'c'),
600 ('b', 'b'), ('b', 'c'), ('c', 'c')
601
602 Note that elements are treated as unique depending on their position
603 in the input, not their actual values.
604
605 The :class:`itertools.count` function now has a *step* argument that
606 allows incrementing by values other than 1. :func:`count` also
607 now allows keyword arguments, and using non-integer values such as
608 floats or :class:`Decimal` instances. (Implemented by Raymond
609 Hettinger; :issue:`5032`.)
610
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000611 :func:`itertools.combinations` and :func:`itertools.product` were
612 previously raising :exc:`ValueError` for values of *r* larger than
613 the input iterable. This was deemed a specification error, so they
614 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
615
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000616* The :mod:`json` module was upgraded to version 2.0.9 of the
617 simplejson package, which includes a C extension that makes
618 encoding and decoding faster.
619 (Contributed by Bob Ippolito; :issue:`4136`.)
620
621 To support the new :class:`OrderedDict` type, :func:`json.load`
622 now has an optional *object_pairs_hook* parameter that will be called
623 with any object literal that decodes to a list of pairs.
624 (Contributed by Raymond Hettinger; :issue:`5381`.)
625
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000626* New functions: the :mod:`math` module now has
627 a :func:`gamma` function.
628 (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.)
629
Andrew M. Kuchling24520b42009-04-09 11:22:47 +0000630* The :mod:`multiprocessing` module's :class:`Manager*` classes
631 can now be passed a callable that will be called whenever
632 a subprocess is started, along with a set of arguments that will be
633 passed to the callable.
634 (Contributed by lekma; :issue:`5585`.)
635
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000636* The :mod:`nntplib` module now supports IPv6 addresses.
637 (Contributed by Derek Morr; :issue:`1664`.)
638
Andrew M. Kuchling9cb42772009-01-21 02:15:43 +0000639* The :mod:`pydoc` module now has help for the various symbols that Python
640 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
641 (Contributed by David Laban; :issue:`4739`.)
642
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000643* The :mod:`re` module's :func:`split`, :func:`sub`, and :func:`subn`
644 now accept an optional *flags* argument, for consistency with the
645 other functions in the module. (Added by Gregory P. Smith.)
646
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000647* The :mod:`shutil` module's :func:`copyfile` and :func:`copytree`
648 functions now raises a :exc:`SpecialFileError` exception when
649 asked to copy a named pipe. Previously the code would treat
650 named pipes like a regular file by opening them for reading, and
651 this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.)
652
653* New functions: in the :mod:`site` module, three new functions
654 return various site- and user-specific paths.
655 :func:`getsitepackages` returns a list containing all
656 global site-packages directories, and
657 :func:`getusersitepackages` returns the path of the user's
658 site-packages directory.
659 :func:`getuserbase` returns the value of the :envvar:``USER_BASE``
660 environment variable, giving the path to a directory that can be used
661 to store data.
662 (Contributed by Tarek Ziade; :issue:`6693`.)
663
664* The :mod:`SocketServer` module's :class:`TCPServer` class now
665 has a :attr:`disable_nagle_algorithm` class attribute.
666 The default value is False; if overridden to be True,
667 new request connections will have the TCP_NODELAY option set to
668 prevent buffering many small sends into a single TCP packet.
669 (Contributed by Kristjan Valur Jonsson; :issue:`6192`.)
670
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000671* The :mod:`struct` module will no longer silently ignore overflow
672 errors when a value is too large for a particular integer format
673 code (one of ``bBhHiIlLqQ``); it now always raises a
674 :exc:`struct.error` exception. (Changed by Mark Dickinson;
675 :issue:`1523`.)
676
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000677* New function: the :mod:`subprocess` module's
678 :func:`check_output` runs a command with a specified set of arguments
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000679 and returns the command's output as a string when the command runs without
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +0000680 error, or raises a :exc:`CalledProcessError` exception otherwise.
681
682 ::
683
684 >>> subprocess.check_output(['df', '-h', '.'])
685 'Filesystem Size Used Avail Capacity Mounted on\n
686 /dev/disk0s2 52G 49G 3.0G 94% /\n'
687
688 >>> subprocess.check_output(['df', '-h', '/bogus'])
689 ...
690 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
691
692 (Contributed by Gregory P. Smith.)
693
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000694* New function: :func:`is_declared_global` in the :mod:`symtable` module
695 returns true for variables that are explicitly declared to be global,
696 false for ones that are implicitly global.
697 (Contributed by Jeremy Hylton.)
698
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000699* The ``sys.version_info`` value is now a named tuple, with attributes
700 named ``major``, ``minor``, ``micro``, ``releaselevel``, and ``serial``.
701 (Contributed by Ross Light; :issue:`4285`.)
702
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000703* The :mod:`tarfile` module now supports filtering the :class:`TarInfo`
704 objects being added to a tar file. When you call :meth:`TarFile.add`,
705 instance, you may supply an optional *filter* argument
706 that's a callable. The *filter* callable will be passed the
707 :class:`TarInfo` for every file being added, and can modify and return it.
708 If the callable returns ``None``, the file will be excluded from the
709 resulting archive. This is more powerful than the existing
710 *exclude* argument, which has therefore been deprecated.
711 (Added by Lars Gustaebel; :issue:`6856`.)
712
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000713* The :mod:`threading` module's :meth:`Event.wait` method now returns
714 the internal flag on exit. This means the method will usually
715 return true because :meth:`wait` is supposed to block until the
716 internal flag becomes true. The return value will only be false if
717 a timeout was provided and the operation timed out.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000718 (Contributed by Tim Lesher; :issue:`1674032`.)
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000719
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000720* The :func:`is_zipfile` function in the :mod:`zipfile` module now
721 accepts a file object, in addition to the path names accepted in earlier
Andrew M. Kuchling9cb42772009-01-21 02:15:43 +0000722 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000723
Andrew M. Kuchling6c2633e2009-03-30 23:09:46 +0000724 :mod:`zipfile` now supports archiving empty directories and
725 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
726
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000727.. ======================================================================
728.. whole new modules get described in subsections here
729
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000730Unit Testing Enhancements
731---------------------------------
732
733The :mod:`unittest` module was enhanced in several ways.
734The progress messages now shows 'x' for expected failures
735and 'u' for unexpected successes when run in verbose mode.
736(Contributed by Benjamin Peterson.)
737Test cases can raise the :exc:`SkipTest` exception to skip a test.
738(:issue:`1034053`.)
739
740.. XXX describe test discovery (Contributed by Michael Foord; :issue:`6001`.)
741
742The error messages for :meth:`assertEqual`,
743:meth:`assertTrue`, and :meth:`assertFalse`
744failures now provide more information. If you set the
745:attr:`longMessage` attribute of your :class:`TestCase` classes to
746true, both the standard error message and any additional message you
747provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
748
749The :meth:`assertRaises` and :meth:`failUnlessRaises` methods now
750return a context handler when called without providing a callable
751object to run. For example, you can write this::
752
753 with self.assertRaises(KeyError):
754 raise ValueError
755
756(Implemented by Antoine Pitrou; :issue:`4444`.)
757
758The methods :meth:`addCleanup` and :meth:`doCleanups` were added.
759:meth:`addCleanup` allows you to add cleanup functions that
760will be called unconditionally (after :meth:`setUp` if
761:meth:`setUp` fails, otherwise after :meth:`tearDown`). This allows
762for much simpler resource allocation and deallocation during tests.
763:issue:`5679`
764
765A number of new methods were added that provide more specialized
766tests. Many of these methods were written by Google engineers
767for use in their test suites; Gregory P. Smith, Michael Foord, and
768GvR worked on merging them into Python's version of :mod:`unittest`.
769
770* :meth:`assertIsNone` and :meth:`assertIsNotNone` take one
771 expression and verify that the result is or is not ``None``.
772
773* :meth:`assertIs` and :meth:`assertIsNot` take two values and check
774 whether the two values evaluate to the same object or not.
775 (Added by Michael Foord; :issue:`2578`.)
776
777* :meth:`assertGreater`, :meth:`assertGreaterEqual`,
778 :meth:`assertLess`, and :meth:`assertLessEqual` compare
779 two quantities.
780
781* :meth:`assertMultiLineEqual` compares two strings, and if they're
782 not equal, displays a helpful comparison that highlights the
783 differences in the two strings.
784
785* :meth:`assertRegexpMatches` checks whether its first argument is a
786 string matching a regular expression provided as its second argument.
787
788* :meth:`assertRaisesRegexp` checks whether a particular exception
789 is raised, and then also checks that the string representation of
790 the exception matches the provided regular expression.
791
792* :meth:`assertIn` and :meth:`assertNotIn` tests whether
793 *first* is or is not in *second*.
794
795* :meth:`assertSameElements` tests whether two provided sequences
796 contain the same elements.
797
798* :meth:`assertSetEqual` compares whether two sets are equal, and
799 only reports the differences between the sets in case of error.
800
801* Similarly, :meth:`assertListEqual` and :meth:`assertTupleEqual`
802 compare the specified types and explain the differences.
803 More generally, :meth:`assertSequenceEqual` compares two sequences
804 and can optionally check whether both sequences are of a
805 particular type.
806
807* :meth:`assertDictEqual` compares two dictionaries and reports the
808 differences. :meth:`assertDictContainsSubset` checks whether
809 all of the key/value pairs in *first* are found in *second*.
810
811* :meth:`assertAlmostEqual` and :meth:`assertNotAlmostEqual` short-circuit
812 (automatically pass or fail without checking decimal places) if the objects
813 are equal.
814
815* :meth:`loadTestsFromName` properly honors the ``suiteClass`` attribute of
816 the :class:`TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.)
817
818* A new hook, :meth:`addTypeEqualityFunc` takes a type object and a
819 function. The :meth:`assertEqual` method will use the function
820 when both of the objects being compared are of the specified type.
821 This function should compare the two objects and raise an
822 exception if they don't match; it's a good idea for the function
823 to provide additional information about why the two objects are
824 matching, much as the new sequence comparison methods do.
825
826:func:`unittest.main` now takes an optional ``exit`` argument.
827If False ``main`` doesn't call :func:`sys.exit` allowing it to
828be used from the interactive interpreter. :issue:`3379`.
829
830:class:`TestResult` has new :meth:`startTestRun` and
831:meth:`stopTestRun` methods; called immediately before
832and after a test run. :issue:`5728` by Robert Collins.
833
834With all these changes, the :file:`unittest.py` was becoming awkwardly
835large, so the module was turned into a package and the code split into
836several files (by Benjamin Peterson). This doesn't affect how the
837module is imported.
838
839
840.. _importlib-section:
841
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000842importlib: Importing Modules
843------------------------------
844
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000845Python 3.1 includes the :mod:`importlib` package, a re-implementation
846of the logic underlying Python's :keyword:`import` statement.
847:mod:`importlib` is useful for implementors of Python interpreters and
848to user who wish to write new importers that can participate in the
849import process. Python 2.7 doesn't contain the complete
850:mod:`importlib` package, but instead has a tiny subset that contains
851a single function, :func:`import_module`.
852
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000853``import_module(name, package=None)`` imports a module. *name* is
Andrew M. Kuchling2c130b62009-04-11 16:12:23 +0000854a string containing the module or package's name. It's possible to do
855relative imports by providing a string that begins with a ``.``
856character, such as ``..utils.errors``. For relative imports, the
857*package* argument must be provided and is the name of the package that
858will be used as the anchor for
859the relative import. :func:`import_module` both inserts the imported
860module into ``sys.modules`` and returns the module object.
861
862Here are some examples::
863
864 >>> from importlib import import_module
865 >>> anydbm = import_module('anydbm') # Standard absolute import
866 >>> anydbm
867 <module 'anydbm' from '/p/python/Lib/anydbm.py'>
868 >>> # Relative import
869 >>> sysconfig = import_module('..sysconfig', 'distutils.command')
870 >>> sysconfig
871 <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'>
872
873:mod:`importlib` was implemented by Brett Cannon and introduced in
874Python 3.1.
875
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +0000876
Andrew M. Kuchlinga17cd4a2009-01-31 02:50:09 +0000877ttk: Themed Widgets for Tk
878--------------------------
879
880Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
881widgets but have a more customizable appearance and can therefore more
882closely resemble the native platform's widgets. This widget
883set was originally called Tile, but was renamed to Ttk (for "themed Tk")
884on being added to Tcl/Tck release 8.5.
885
886XXX write a brief discussion and an example here.
887
888The :mod:`ttk` module was written by Guilherme Polo and added in
889:issue:`2983`. An alternate version called ``Tile.py``, written by
890Martin Franklin and maintained by Kevin Walzer, was proposed for
891inclusion in :issue:`2618`, but the authors argued that Guilherme
892Polo's work was more comprehensive.
893
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000894.. ======================================================================
895
896
897Build and C API Changes
898=======================
899
900Changes to Python's build process and to the C API include:
901
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +0000902* If you use the :file:`.gdbinit` file provided with Python,
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000903 the "pyo" macro in the 2.7 version now works correctly when the thread being
904 debugged doesn't hold the GIL; the macro now acquires it before printing.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000905 (Contributed by Victor Stinner; :issue:`3632`.)
906
Andrew M. Kuchling9a4b94c2009-04-03 21:43:00 +0000907* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000908 worker thread submit notifications to the main Python thread. This
909 is particularly useful for asynchronous IO operations.
910 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
911
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000912* New function: :cfunc:`PyCode_NewEmpty` creates an empty code object;
913 only the filename, function name, and first line number are required.
914 This is useful to extension modules that are attempting to
915 construct a more useful traceback stack. Previously such
916 extensions needed to call :cfunc:`PyCode_New`, which had many
917 more arguments. (Added by Jeffrey Yasskin.)
918
919* New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object
920 and returns the line number that the frame is currently executing.
921 Previously code would need to get the index of the bytecode
922 instruction currently executing, and then look up the line number
923 corresponding to that address. (Added by Jeffrey Yasskin.)
924
925* New macros: the Python header files now define the following macros:
926 :cmacro:`Py_ISALNUM`,
927 :cmacro:`Py_ISALPHA`,
928 :cmacro:`Py_ISDIGIT`,
929 :cmacro:`Py_ISLOWER`,
930 :cmacro:`Py_ISSPACE`,
931 :cmacro:`Py_ISUPPER`,
932 :cmacro:`Py_ISXDIGIT`,
933 and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`.
934 All of these functions are analogous to the C
935 standard macros for classifying characters, but ignore the current
936 locale setting, because in
937 several places Python needs to analyze characters in a
938 locale-independent way. (Added by Eric Smith;
939 :issue:`5793`.)
940
941 .. XXX these macros don't seem to be described in the c-api docs.
942
943* The complicated interaction between threads and process forking has
944 been changed. Previously, the child process created by
945 :func:`os.fork` might fail because the child is created with only a
946 single thread running, the thread performing the :func:`os.fork`.
947 If other threads were holding a lock, such as Python's import lock,
948 when the fork was performed, the lock would still be marked as
949 "held" in the new process. But in the child process nothing would
950 ever release the lock, since the other threads weren't replicated,
951 and the child process would no longer be able to perform imports.
952
953 Python 2.7 now acquires the import lock before performing an
954 :func:`os.fork`, and will also clean up any locks created using the
955 :mod:`threading` module. C extension modules that have internal
956 locks, or that call :cfunc:`fork()` themselves, will not benefit
957 from this clean-up.
958
959 (Fixed by Thomas Wouters; :issue:`1590864`.)
960
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000961* Global symbols defined by the :mod:`ctypes` module are now prefixed
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000962 with ``Py``, or with ``_ctypes``. (Implemented by Thomas
Andrew M. Kuchling92b97002009-05-02 17:12:15 +0000963 Heller; :issue:`3102`.)
964
Andrew M. Kuchling77069572009-03-31 01:21:01 +0000965* The :program:`configure` script now checks for floating-point rounding bugs
966 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
967 preprocessor definition. No code currently uses this definition,
968 but it's available if anyone wishes to use it.
969 (Added by Mark Dickinson; :issue:`2937`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000970
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000971* The build process now creates the necessary files for pkg-config
972 support. (Contributed by Clinton Roy; :issue:`3585`.)
973
974* The build process now supports Subversion 1.7. (Contributed by
975 Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.)
976
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000977.. ======================================================================
978
979Port-Specific Changes: Windows
980-----------------------------------
981
Andrew M. Kuchling10b1ec92009-01-02 21:00:35 +0000982* The :mod:`msvcrt` module now contains some constants from
983 the :file:`crtassem.h` header file:
984 :data:`CRT_ASSEMBLY_VERSION`,
985 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
986 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Andrew M. Kuchling466bd9d2009-01-24 03:28:18 +0000987 (Contributed by David Cournapeau; :issue:`4365`.)
988
989* The new :cfunc:`_beginthreadex` API is used to start threads, and
990 the native thread-local storage functions are now used.
991 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000992
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +0000993* The :func:`os.listdir` function now correctly fails
994 for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.)
995
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +0000996.. ======================================================================
997
998Port-Specific Changes: Mac OS X
999-----------------------------------
1000
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001001* The path ``/Library/Python/2.7/site-packages`` is now appended to
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001002 ``sys.path``, in order to share added packages between the system
1003 installation and a user-installed copy of the same version.
1004 (Changed by Ronald Oussoren; :issue:`4865`.)
1005
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001006
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001007Other Changes and Fixes
1008=======================
1009
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001010* When importing a module from a :file:`.pyc` or :file:`.pyo` file
1011 with an existing :file:`.py` counterpart, the :attr:`co_filename`
Andrew M. Kuchling92b97002009-05-02 17:12:15 +00001012 attributes of the resulting code objects are overwritten when the
1013 original filename is obsolete. This can happen if the file has been
1014 renamed, moved, or is accessed through different paths. (Patch by
1015 Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.)
Andrew M. Kuchling77069572009-03-31 01:21:01 +00001016
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001017* The :file:`regrtest.py` script now takes a :option:`--randseed=`
1018 switch that takes an integer that will be used as the random seed
1019 for the :option:`-r` option that executes tests in random order.
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001020 The :option:`-r` option also reports the seed that was used
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001021 (Added by Collin Winter.)
1022
Antoine Pitrou4698d992009-05-31 14:20:14 +00001023* The :file:`regrtest.py` script now takes a :option:`-j` switch
1024 that takes an integer specifying how many tests run in parallel. This
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001025 allows reducing the total runtime on multi-core machines.
Antoine Pitrou4698d992009-05-31 14:20:14 +00001026 This option is compatible with several other options, including the
1027 :option:`-R` switch which is known to produce long runtimes.
1028 (Added by Antoine Pitrou, :issue:`6152`.)
Andrew M. Kuchling71d5c282009-03-30 22:30:20 +00001029
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001030.. ======================================================================
1031
1032Porting to Python 2.7
1033=====================
1034
1035This section lists previously described changes and other bugfixes
1036that may require changes to your code:
1037
Andrew M. Kuchling5a9c40b2009-10-05 22:30:22 +00001038* When using :class:`Decimal` instances with a string's
1039 :meth:`format` method, the default alignment was previously
1040 left-alignment. This has been changed to right-alignment, which might
1041 change the output of your programs.
1042 (Changed by Mark Dickinson; :issue:`6857`.)
1043
1044 Another :meth:`format`-related change: the default precision used
1045 for floating-point and complex numbers was changed from 6 decimal
1046 places to 12, which matches the precision used by :func:`str`.
1047 (Changed by Eric Smith; :issue:`5920`.)
1048
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00001049* Because of an optimization for the :keyword:`with` statement, the special
1050 methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's
1051 type, and cannot be directly attached to the object's instance. This
Amaury Forgeot d'Arcd81333c2009-06-10 20:30:19 +00001052 affects new-style classes (derived from :class:`object`) and C extension
Amaury Forgeot d'Arc901f2002009-06-09 23:08:13 +00001053 types. (:issue:`6101`.)
Andrew M. Kuchlingce1882b2008-10-04 16:52:31 +00001054
1055.. ======================================================================
1056
1057
1058.. _acks27:
1059
1060Acknowledgements
1061================
1062
1063The author would like to thank the following people for offering
1064suggestions, corrections and assistance with various drafts of this
1065article: no one yet.
1066