blob: 9148743dc05002100fd8da71d0a8a80d4306243c [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 Peterson1010bf32009-01-30 04:00:29 +00009.. Fix accents on Kristjan Valur Jonsson, Fuerstenau.
10
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
58.. ========================================================================
59.. Large, PEP-level features and changes should be described here.
60.. Should there be a new section here for 3k migration?
61.. Or perhaps a more general section describing module changes/deprecation?
62.. ========================================================================
63
64
65
66Other Language Changes
67======================
68
69Some smaller changes made to the core Python language are:
70
Mark Dickinson54bc1ec2008-12-17 16:19:07 +000071* The :func:`int` and :func:`long` types gained a ``bit_length``
72 method that returns the number of bits necessary to represent
73 its argument in binary::
74
75 >>> n = 37
76 >>> bin(37)
77 '0b100101'
78 >>> n.bit_length()
79 6
80 >>> n = 2**123-1
81 >>> n.bit_length()
82 123
83 >>> (n+1).bit_length()
84 124
85
86 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
87
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000088
Mark Dickinsond72c7b62009-03-20 16:00:49 +000089* Integers are now stored internally either in base 2**15 or in base
90 2**30, the base being determined at build time. Previously, they
91 were always stored in base 2**15. Using base 2**30 gives
92 significant performance improvements on 64-bit machines, but
93 benchmark results on 32-bit machines have been mixed. Therefore,
94 the default is to use base 2**30 on 64-bit machines and base 2**15
95 on 32-bit machines; on Unix, there's a new configure option
96 --enable-big-digits that can be used to override this default.
97
98 Apart from the performance improvements this change should be
99 invisible to end users, with one exception: for testing and
100 debugging purposes there's a new structseq ``sys.long_info`` that
101 provides information about the internal format, giving the number of
102 bits per digit and the size in bytes of the C type used to store
103 each digit::
104
105 >>> import sys
106 >>> sys.long_info
107 sys.long_info(bits_per_digit=30, sizeof_digit=4)
108
109
110 (Contributed by Mark Dickinson; :issue:`4258`.)
111
112
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000113.. ======================================================================
114
115
116Optimizations
117-------------
118
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000119A few performance enhancements have been added:
120
121* The garbage collector now performs better when many objects are
122 being allocated without deallocating any. A full garbage collection
123 pass is only performed when the middle generation has been collected
124 10 times and when the number of survivor objects from the middle
125 generation exceeds 10% of the number of objects in the oldest
126 generation. The second condition was added to reduce the number
127 of full garbage collections as the number of objects on the heap grows,
128 avoiding quadratic performance when allocating very many objects.
129 (Suggested by Martin von Loewis and implemented by Antoine Pitrou;
130 :issue:`4074`.)
131
Antoine Pitrou9d81def2009-03-28 19:20:09 +0000132* The garbage collector tries to avoid tracking simple containers which
133 can't be part of a cycle. As of now, this is true for tuples and dicts
134 containing atomic types (such as ints, strings, etc.). Transitively, a dict
135 containing tuples of atomic types won't be tracked either. This helps bring
136 down the individual cost of each garbage collection, since it decreases the
137 number of objects to be considered and traversed by the collector.
138
139 To help diagnosing this optimization, a new function in the :mod:`gc`
140 module, :func:`is_tracked`, returns True if a given instance is tracked
141 by the garbage collector, False otherwise.
142 (Contributed by Antoine Pitrou; :issue:`4688`.)
143
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000144
145.. ======================================================================
146
147New, Improved, and Deprecated Modules
148=====================================
149
150As in every release, Python's standard library received a number of
151enhancements and bug fixes. Here's a partial list of the most notable
152changes, sorted alphabetically by module name. Consult the
153:file:`Misc/NEWS` file in the source tree for a more complete list of
154changes, or look through the Subversion logs for all the details.
155
Tarek Ziadé555f0e92009-02-16 22:42:39 +0000156* In Distutils, distutils.sdist.add_defaults now uses package_dir and data_files
157 to feed MANIFEST.
158
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000159* It is not mandatory anymore to store clear text passwords in the
160 :file:`.pypirc` file when registering and uploading packages to PyPI. As long
161 as the username is present in that file, the :mod:`distutils` package will
162 prompt for the password if not present. (Added by tarek, with the initial
163 contribution of Nathan Van Gheem; :issue:`4394`.)
164
165* The :mod:`bz2` module's :class:`BZ2File` now supports the context
166 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
167 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
168
169* A new :class:`Counter` class in the :mod:`collections` module is
170 useful for tallying data. :class:`Counter` instances behave mostly
171 like dictionaries but return zero for missing keys instead of
172 raising a :exc:`KeyError`::
173
174 >>> from collections import Counter
175 >>> c=Counter()
176 >>> for letter in 'here is a sample of english text':
177 ... c[letter] += 1
178 ...
179 >>> c
180 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
181 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
182 'p': 1, 'r': 1, 'x': 1})
183 >>> c['e']
184 5
185 >>> c['z']
186 0
187
188 There are two additional :class:`Counter` methods: :meth:`most_common`
189 returns the N most common elements and their counts, and :meth:`elements`
190 returns an iterator over the contained element, repeating each element
191 as many times as its count::
192
193 >>> c.most_common(5)
194 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
195 >>> c.elements() ->
196 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
197 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
198 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
199 's', 's', 'r', 't', 't', 'x']
200
201 Contributed by Raymond Hettinger; :issue:`1696199`.
202
203* The :mod:`gzip` module's :class:`GzipFile` now supports the context
204 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``.
205 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
206
207* The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
208 an invalid file descriptor. (Implemented by Benjamin Peterson;
209 :issue:`4991`.)
210
211* The :mod:`pydoc` module now has help for the various symbols that Python
212 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
213 (Contributed by David Laban; :issue:`4739`.)
214
Georg Brandl1f01deb2009-01-03 22:47:39 +0000215* A new function in the :mod:`subprocess` module,
216 :func:`check_output`, runs a command with a specified set of arguments
217 and returns the command's output as a string if the command runs without
218 error, or raises a :exc:`CalledProcessError` exception otherwise.
219
220 ::
221
222 >>> subprocess.check_output(['df', '-h', '.'])
223 'Filesystem Size Used Avail Capacity Mounted on\n
224 /dev/disk0s2 52G 49G 3.0G 94% /\n'
225
226 >>> subprocess.check_output(['df', '-h', '/bogus'])
227 ...
228 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
229
230 (Contributed by Gregory P. Smith.)
231
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000232* The :func:`is_zipfile` function in the :mod:`zipfile` module will now
233 accept a file object, in addition to the path names accepted in earlier
234 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000235
236.. ======================================================================
237.. whole new modules get described in subsections here
238
Benjamin Peterson5c6d7872009-02-06 02:40:07 +0000239ttk: Themed Widgets for Tk
240--------------------------
241
242Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
243widgets but have a more customizable appearance and can therefore more
244closely resemble the native platform's widgets. This widget
245set was originally called Tile, but was renamed to Ttk (for "themed Tk")
246on being added to Tcl/Tck release 8.5.
247
248XXX write a brief discussion and an example here.
249
250The :mod:`ttk` module was written by Guilherme Polo and added in
251:issue:`2983`. An alternate version called ``Tile.py``, written by
252Martin Franklin and maintained by Kevin Walzer, was proposed for
253inclusion in :issue:`2618`, but the authors argued that Guilherme
254Polo's work was more comprehensive.
255
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000256.. ======================================================================
257
258
259Build and C API Changes
260=======================
261
262Changes to Python's build process and to the C API include:
263
Georg Brandl1f01deb2009-01-03 22:47:39 +0000264* If you use the :file:`.gdbinit` file provided with Python,
265 the "pyo" macro in the 2.7 version will now work when the thread being
266 debugged doesn't hold the GIL; the macro will now acquire it before printing.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000267 (Contributed by Victor Stinner; :issue:`3632`.)
268
269* :cfunc:`Py_AddPendingCall` is now thread safe, letting any
270 worker thread submit notifications to the main Python thread. This
271 is particularly useful for asynchronous IO operations.
272 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
273
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000274
275.. ======================================================================
276
277Port-Specific Changes: Windows
278-----------------------------------
279
Georg Brandl1f01deb2009-01-03 22:47:39 +0000280* The :mod:`msvcrt` module now contains some constants from
281 the :file:`crtassem.h` header file:
282 :data:`CRT_ASSEMBLY_VERSION`,
283 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
284 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000285 (Contributed by David Cournapeau; :issue:`4365`.)
286
287* The new :cfunc:`_beginthreadex` API is used to start threads, and
288 the native thread-local storage functions are now used.
289 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000290
291.. ======================================================================
292
293Port-Specific Changes: Mac OS X
294-----------------------------------
295
296
297.. ======================================================================
298
299Porting to Python 2.7
300=====================
301
302This section lists previously described changes and other bugfixes
303that may require changes to your code:
304
305To be written.
306
307.. ======================================================================
308
309
310.. _acks27:
311
312Acknowledgements
313================
314
315The author would like to thank the following people for offering
316suggestions, corrections and assistance with various drafts of this
317article: no one yet.
318