blob: 580790f5a6b9b23c990f899d8e0d9ab2b0faf0c7 [file] [log] [blame]
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001****************************
2 What's New in Python 2.7
3****************************
4
5:Author: A.M. Kuchling (amk at amk.ca)
6:Release: |release|
7:Date: |today|
8
Benjamin Petersond23f8222009-04-05 19:13:16 +00009.. Fix accents on Kristjan Valur Jonsson, Fuerstenau, Tarek Ziade.
Benjamin Peterson1010bf32009-01-30 04:00:29 +000010
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000011.. $Id$
12 Rules for maintenance:
13
14 * Anyone can add text to this document. Do not spend very much time
15 on the wording of your changes, because your text will probably
16 get rewritten to some degree.
17
18 * The maintainer will go through Misc/NEWS periodically and add
19 changes; it's therefore more important to add your changes to
20 Misc/NEWS than to this file.
21
22 * This is not a complete list of every single change; completeness
23 is the purpose of Misc/NEWS. Some changes I consider too small
24 or esoteric to include. If such a change is added to the text,
25 I'll just remove it. (This is another reason you shouldn't spend
26 too much time on writing your addition.)
27
28 * If you want to draw your new text to the attention of the
29 maintainer, add 'XXX' to the beginning of the paragraph or
30 section.
31
32 * It's OK to just add a fragmentary note about a change. For
33 example: "XXX Describe the transmogrify() function added to the
34 socket module." The maintainer will research the change and
35 write the necessary text.
36
37 * You can comment out your additions if you like, but it's not
38 necessary (especially when a final release is some months away).
39
40 * Credit the author of a patch or bugfix. Just the name is
41 sufficient; the e-mail address isn't necessary.
42
43 * It's helpful to add the bug/patch number in a parenthetical comment.
44
45 XXX Describe the transmogrify() function added to the socket
46 module.
47 (Contributed by P.Y. Developer; :issue:`12345`.)
48
49 This saves the maintainer some effort going through the SVN logs
50 when researching a change.
51
52This article explains the new features in Python 2.7.
53No release schedule has been decided yet for 2.7.
54
55.. Compare with previous release in 2 - 3 sentences here.
56 add hyperlink when the documentation becomes available online.
57
Benjamin Petersond23f8222009-04-05 19:13:16 +000058Python 3.1
59================
60
61Much as Python 2.6 incorporated features from Python 3.0,
62version 2.7 is influenced by features from 3.1.
63
64XXX mention importlib; anything else?
65
66One porting change: the :option:`-3` switch now automatically
67enables the :option:`-Qwarn` switch that causes warnings
68about using classic division with integers and long integers.
69
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000070.. ========================================================================
71.. Large, PEP-level features and changes should be described here.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000072.. ========================================================================
73
Benjamin Petersond23f8222009-04-05 19:13:16 +000074PEP 372: Adding an ordered dictionary to collections
75====================================================
76
77XXX write this
78
79Several modules will now use :class:`OrderedDict` by default. The
80:mod:`ConfigParser` module uses :class:`OrderedDict` for the list
81of sections and the options within a section.
82The :meth:`namedtuple._asdict` method returns an :class:`OrderedDict`
83as well.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000084
85
86Other Language Changes
87======================
88
89Some smaller changes made to the core Python language are:
90
Mark Dickinson54bc1ec2008-12-17 16:19:07 +000091* The :func:`int` and :func:`long` types gained a ``bit_length``
92 method that returns the number of bits necessary to represent
93 its argument in binary::
94
95 >>> n = 37
96 >>> bin(37)
97 '0b100101'
98 >>> n.bit_length()
99 6
100 >>> n = 2**123-1
101 >>> n.bit_length()
102 123
103 >>> (n+1).bit_length()
104 124
105
106 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
107
Benjamin Petersond23f8222009-04-05 19:13:16 +0000108* The :class:`bytearray` type's :meth:`translate` method will
109 now accept None as its first argument. (Fixed by Georg Brandl;
110 :issue:`4759`.)
Mark Dickinsond72c7b62009-03-20 16:00:49 +0000111
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000112.. ======================================================================
113
114
115Optimizations
116-------------
117
Benjamin Petersond23f8222009-04-05 19:13:16 +0000118Several performance enhancements have been added:
119
120.. * A new :program:`configure` option, :option:`--with-computed-gotos`,
121 compiles the main bytecode interpreter loop using a new dispatch
122 mechanism that gives speedups of up to 20%, depending on the system
123 and benchmark. The new mechanism is only supported on certain
124 compilers, such as gcc, SunPro, and icc.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000125
126* The garbage collector now performs better when many objects are
127 being allocated without deallocating any. A full garbage collection
128 pass is only performed when the middle generation has been collected
129 10 times and when the number of survivor objects from the middle
130 generation exceeds 10% of the number of objects in the oldest
131 generation. The second condition was added to reduce the number
132 of full garbage collections as the number of objects on the heap grows,
133 avoiding quadratic performance when allocating very many objects.
134 (Suggested by Martin von Loewis and implemented by Antoine Pitrou;
135 :issue:`4074`.)
136
Benjamin Petersond23f8222009-04-05 19:13:16 +0000137* The garbage collector tries to avoid tracking simple containers
138 which can't be part of a cycle. In Python 2.7, this is now true for
139 tuples and dicts containing atomic types (such as ints, strings,
140 etc.). Transitively, a dict containing tuples of atomic types won't
141 be tracked either. This helps reduce the cost of each
142 garbage collection by decreasing the number of objects to be
143 considered and traversed by the collector.
Antoine Pitrou9d81def2009-03-28 19:20:09 +0000144 (Contributed by Antoine Pitrou; :issue:`4688`.)
145
Benjamin Petersond23f8222009-04-05 19:13:16 +0000146* Integers are now stored internally either in base 2**15 or in base
147 2**30, the base being determined at build time. Previously, they
148 were always stored in base 2**15. Using base 2**30 gives
149 significant performance improvements on 64-bit machines, but
150 benchmark results on 32-bit machines have been mixed. Therefore,
151 the default is to use base 2**30 on 64-bit machines and base 2**15
152 on 32-bit machines; on Unix, there's a new configure option
153 :option:`--enable-big-digits` that can be used to override this default.
154
155 Apart from the performance improvements this change should be
156 invisible to end users, with one exception: for testing and
157 debugging purposes there's a new structseq ``sys.long_info`` that
158 provides information about the internal format, giving the number of
159 bits per digit and the size in bytes of the C type used to store
160 each digit::
161
162 >>> import sys
163 >>> sys.long_info
164 sys.long_info(bits_per_digit=30, sizeof_digit=4)
165
166 (Contributed by Mark Dickinson; :issue:`4258`.)
167
168 Another set of changes made long objects a few bytes smaller: 2 bytes
169 smaller on 32-bit systems and 6 bytes on 64-bit.
170 (Contributed by Mark Dickinson; :issue:`5260`.)
171
172* The division algorithm for long integers has been made faster
173 by tightening the inner loop, doing shifts instead of multiplications,
174 and fixing an unnecessary extra iteration.
175 Various benchmarks show speedups of between 50% and 150% for long
176 integer divisions and modulo operations.
177 (Contributed by Mark Dickinson; :issue:`5512`.)
178
179* The implementation of ``%`` checks for the left-side operand being
180 a Python string and special-cases it; this results in a 1-3%
181 performance increase for applications that frequently use ``%``
182 with strings, such as templating libraries.
183 (Implemented by Collin Winter; :issue:`5176`.)
184
185* List comprehensions with an ``if`` condition are compiled into
186 faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
187 by Jeffrey Yasskin; :issue:`4715`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000188
189.. ======================================================================
190
191New, Improved, and Deprecated Modules
192=====================================
193
194As in every release, Python's standard library received a number of
195enhancements and bug fixes. Here's a partial list of the most notable
196changes, sorted alphabetically by module name. Consult the
197:file:`Misc/NEWS` file in the source tree for a more complete list of
198changes, or look through the Subversion logs for all the details.
199
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000200* The :mod:`bz2` module's :class:`BZ2File` now supports the context
201 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
202 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
203
204* A new :class:`Counter` class in the :mod:`collections` module is
205 useful for tallying data. :class:`Counter` instances behave mostly
206 like dictionaries but return zero for missing keys instead of
207 raising a :exc:`KeyError`::
208
209 >>> from collections import Counter
210 >>> c=Counter()
211 >>> for letter in 'here is a sample of english text':
212 ... c[letter] += 1
213 ...
214 >>> c
215 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
216 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
217 'p': 1, 'r': 1, 'x': 1})
218 >>> c['e']
219 5
220 >>> c['z']
221 0
222
223 There are two additional :class:`Counter` methods: :meth:`most_common`
224 returns the N most common elements and their counts, and :meth:`elements`
225 returns an iterator over the contained element, repeating each element
226 as many times as its count::
227
228 >>> c.most_common(5)
229 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
230 >>> c.elements() ->
231 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
232 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
233 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
234 's', 's', 'r', 't', 't', 'x']
235
236 Contributed by Raymond Hettinger; :issue:`1696199`.
237
Benjamin Petersond23f8222009-04-05 19:13:16 +0000238 The :class:`namedtuple` class now has an optional *rename* parameter.
239 If *rename* is True, field names that are invalid because they've
240 been repeated or that aren't legal Python identifiers will be
241 renamed to legal names that are derived from the field's
242 position within the list of fields:
243
244 >>> T=namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
245 >>> T._fields
246 ('field1', '_1', '_2', 'field2')
247
248 (Added by Raymond Hettinger; :issue:`1818`.)
249
250* In Distutils, :func:`distutils.sdist.add_defaults` now uses
251 *package_dir* and *data_files* to create the MANIFEST file.
252
253 It is no longer mandatory to store clear-text passwords in the
254 :file:`.pypirc` file when registering and uploading packages to PyPI. As long
255 as the username is present in that file, the :mod:`distutils` package will
256 prompt for the password if not present. (Added by Tarek Ziade,
257 based on an initial contribution by Nathan Van Gheem; :issue:`4394`.)
258
259* New method: the :class:`Decimal` class gained a
260 :meth:`from_float` class method that performs an exact conversion
261 of a floating-point number to a :class:`Decimal`.
262 Note that this is an **exact** conversion that strives for the
263 closest decimal approximation to the floating-point representation's value;
264 the resulting decimal value will therefore still include the inaccuracy,
265 if any.
266 For example, ``Decimal.from_float(0.1)`` returns
267 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
268 (Implemented by Raymond Hettinger; :issue:`4796`.)
269
270* A new function in the :mod:`gc` module, :func:`is_tracked`, returns
271 True if a given instance is tracked by the garbage collector, False
272 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
273
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000274* The :mod:`gzip` module's :class:`GzipFile` now supports the context
275 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``.
276 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000277 It's now possible to override the modification time
278 recorded in a gzipped file by providing an optional timestamp to
279 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000280
281* The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
282 an invalid file descriptor. (Implemented by Benjamin Peterson;
283 :issue:`4991`.)
284
Benjamin Petersond23f8222009-04-05 19:13:16 +0000285* New function: ``itertools.compress(*data*, *selectors*)`` takes two
286 iterators. Elements of *data* are returned if the corresponding
287 value in *selectors* is True::
288
289 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
290 A, C, E, F
291
292 New function: ``itertools.combinations_with_replacement(*iter*, *r*)``
293 returns all the possible *r*-length combinations of elements from the
294 iterable *iter*. Unlike :func:`combinations`, individual elements
295 can be repeated in the generated combinations::
296
297 itertools.combinations_with_replacement('abc', 2) =>
298 ('a', 'a'), ('a', 'b'), ('a', 'c'),
299 ('b', 'b'), ('b', 'c'), ('c', 'c')
300
301 Note that elements are treated as unique depending on their position
302 in the input, not their actual values.
303
304 The :class:`itertools.count` function now has a *step* argument that
305 allows incrementing by values other than 1. :func:`count` also
306 now allows keyword arguments, and using non-integer values such as
307 floats or :class:`Decimal` instances. (Implemented by Raymond
308 Hettinger; :issue:`5032`.)
309
310 :func:`itertools.combinations` and :func:`itertools.product` were
311 previously raising :exc:`ValueError` for values of *r* larger than
312 the input iterable. This was deemed a specification error, so they
313 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
314
315* The :mod:`json` module was upgraded to version 2.0.9 of the
316 simplejson package, which includes a C extension that makes
317 encoding and decoding faster.
318 (Contributed by Bob Ippolito; :issue:`4136`.)
319
320 To support the new :class:`OrderedDict` type, :func:`json.load`
321 now has an optional *object_pairs_hook* parameter that will be called
322 with any object literal that decodes to a list of pairs.
323 (Contributed by Raymond Hettinger; :issue:`5381`.)
324
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000325* The :mod:`pydoc` module now has help for the various symbols that Python
326 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
327 (Contributed by David Laban; :issue:`4739`.)
328
Georg Brandl1f01deb2009-01-03 22:47:39 +0000329* A new function in the :mod:`subprocess` module,
330 :func:`check_output`, runs a command with a specified set of arguments
Benjamin Petersond23f8222009-04-05 19:13:16 +0000331 and returns the command's output as a string when the command runs without
Georg Brandl1f01deb2009-01-03 22:47:39 +0000332 error, or raises a :exc:`CalledProcessError` exception otherwise.
333
334 ::
335
336 >>> subprocess.check_output(['df', '-h', '.'])
337 'Filesystem Size Used Avail Capacity Mounted on\n
338 /dev/disk0s2 52G 49G 3.0G 94% /\n'
339
340 >>> subprocess.check_output(['df', '-h', '/bogus'])
341 ...
342 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
343
344 (Contributed by Gregory P. Smith.)
345
Benjamin Petersond23f8222009-04-05 19:13:16 +0000346* The ``sys.version_info`` value is now a named tuple, with attributes
347 named ``major``, ``minor``, ``micro``, ``releaselevel``, and ``serial``.
348 (Contributed by Ross Light; :issue:`4285`.)
349
350* The :mod:`unittest` module was enhanced in several ways.
351 Test cases can raise the :exc:`SkipTest` exception to skip a test.
352 (:issue:`1034053`.)
353 It will now use 'x' for expected failures
354 and 'u' for unexpected successes when run in its verbose mode.
355 (Contributed by Benjamin Peterson.)
356
357 The :meth:`assertRaises` and :meth:`failUnlessRaises` methods now
358 return a context handler when called without providing a callable
359 object to run. For example, you can write this::
360
361 with self.assertRaises(KeyError):
362 raise ValueError
363
364 (Implemented by Antoine Pitrou; :issue:`4444`.)
365
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000366* The :func:`is_zipfile` function in the :mod:`zipfile` module will now
367 accept a file object, in addition to the path names accepted in earlier
368 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000369
Benjamin Petersond23f8222009-04-05 19:13:16 +0000370 :mod:`zipfile` now supports archiving empty directories and
371 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
372
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000373.. ======================================================================
374.. whole new modules get described in subsections here
375
Benjamin Petersond23f8222009-04-05 19:13:16 +0000376importlib: Importing Modules
377------------------------------
378
379XXX write this
380
Benjamin Peterson5c6d7872009-02-06 02:40:07 +0000381ttk: Themed Widgets for Tk
382--------------------------
383
384Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
385widgets but have a more customizable appearance and can therefore more
386closely resemble the native platform's widgets. This widget
387set was originally called Tile, but was renamed to Ttk (for "themed Tk")
388on being added to Tcl/Tck release 8.5.
389
390XXX write a brief discussion and an example here.
391
392The :mod:`ttk` module was written by Guilherme Polo and added in
393:issue:`2983`. An alternate version called ``Tile.py``, written by
394Martin Franklin and maintained by Kevin Walzer, was proposed for
395inclusion in :issue:`2618`, but the authors argued that Guilherme
396Polo's work was more comprehensive.
397
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000398.. ======================================================================
399
400
401Build and C API Changes
402=======================
403
404Changes to Python's build process and to the C API include:
405
Georg Brandl1f01deb2009-01-03 22:47:39 +0000406* If you use the :file:`.gdbinit` file provided with Python,
407 the "pyo" macro in the 2.7 version will now work when the thread being
408 debugged doesn't hold the GIL; the macro will now acquire it before printing.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000409 (Contributed by Victor Stinner; :issue:`3632`.)
410
Benjamin Petersond23f8222009-04-05 19:13:16 +0000411* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000412 worker thread submit notifications to the main Python thread. This
413 is particularly useful for asynchronous IO operations.
414 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
415
Benjamin Petersond23f8222009-04-05 19:13:16 +0000416* The :program:`configure` script now checks for floating-point rounding bugs
417 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
418 preprocessor definition. No code currently uses this definition,
419 but it's available if anyone wishes to use it.
420 (Added by Mark Dickinson; :issue:`2937`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000421
422.. ======================================================================
423
424Port-Specific Changes: Windows
425-----------------------------------
426
Georg Brandl1f01deb2009-01-03 22:47:39 +0000427* The :mod:`msvcrt` module now contains some constants from
428 the :file:`crtassem.h` header file:
429 :data:`CRT_ASSEMBLY_VERSION`,
430 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
431 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000432 (Contributed by David Cournapeau; :issue:`4365`.)
433
434* The new :cfunc:`_beginthreadex` API is used to start threads, and
435 the native thread-local storage functions are now used.
436 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000437
438.. ======================================================================
439
440Port-Specific Changes: Mac OS X
441-----------------------------------
442
Benjamin Petersond23f8222009-04-05 19:13:16 +0000443* The ``/Library/Python/2.7/site-packages`` is now appended to
444 ``sys.path``, in order to share added packages between the system
445 installation and a user-installed copy of the same version.
446 (Changed by Ronald Oussoren; :issue:`4865`.)
447
448
449Other Changes and Fixes
450=======================
451
452* When importing a module from a :file:`.pyc` or :file:`.pyo` file
453 with an existing :file:`.py` counterpart, the :attr:`co_filename`
454 attributes of all code objects if the original filename is obsolete,
455 which can happen if the file has been renamed, moved, or is accessed
456 through different paths. (Patch by Ziga Seilnacht and Jean-Paul
457 Calderone; :issue:`1180193`.)
458
459* The :file:`regrtest.py` script now takes a :option:`--randseed=`
460 switch that takes an integer that will be used as the random seed
461 for the :option:`-r` option that executes tests in random order.
462 The :option:`-r` option also now reports the seed that was used
463 (Added by Collin Winter.)
464
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000465
466.. ======================================================================
467
468Porting to Python 2.7
469=====================
470
471This section lists previously described changes and other bugfixes
472that may require changes to your code:
473
474To be written.
475
476.. ======================================================================
477
478
479.. _acks27:
480
481Acknowledgements
482================
483
484The author would like to thank the following people for offering
485suggestions, corrections and assistance with various drafts of this
486article: no one yet.
487