Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 1 | **************************** |
| 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 Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 9 | .. Fix accents on Kristjan Valur Jonsson, Fuerstenau. |
| 10 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11 | .. $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 | |
| 52 | This article explains the new features in Python 2.7. |
| 53 | No 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 | |
| 66 | Other Language Changes |
| 67 | ====================== |
| 68 | |
| 69 | Some smaller changes made to the core Python language are: |
| 70 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 71 | * 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 Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 88 | |
| 89 | .. ====================================================================== |
| 90 | |
| 91 | |
| 92 | Optimizations |
| 93 | ------------- |
| 94 | |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 95 | A few performance enhancements have been added: |
| 96 | |
| 97 | * The garbage collector now performs better when many objects are |
| 98 | being allocated without deallocating any. A full garbage collection |
| 99 | pass is only performed when the middle generation has been collected |
| 100 | 10 times and when the number of survivor objects from the middle |
| 101 | generation exceeds 10% of the number of objects in the oldest |
| 102 | generation. The second condition was added to reduce the number |
| 103 | of full garbage collections as the number of objects on the heap grows, |
| 104 | avoiding quadratic performance when allocating very many objects. |
| 105 | (Suggested by Martin von Loewis and implemented by Antoine Pitrou; |
| 106 | :issue:`4074`.) |
| 107 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 108 | |
| 109 | .. ====================================================================== |
| 110 | |
| 111 | New, Improved, and Deprecated Modules |
| 112 | ===================================== |
| 113 | |
| 114 | As in every release, Python's standard library received a number of |
| 115 | enhancements and bug fixes. Here's a partial list of the most notable |
| 116 | changes, sorted alphabetically by module name. Consult the |
| 117 | :file:`Misc/NEWS` file in the source tree for a more complete list of |
| 118 | changes, or look through the Subversion logs for all the details. |
| 119 | |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 120 | * It is not mandatory anymore to store clear text passwords in the |
| 121 | :file:`.pypirc` file when registering and uploading packages to PyPI. As long |
| 122 | as the username is present in that file, the :mod:`distutils` package will |
| 123 | prompt for the password if not present. (Added by tarek, with the initial |
| 124 | contribution of Nathan Van Gheem; :issue:`4394`.) |
| 125 | |
| 126 | * The :mod:`bz2` module's :class:`BZ2File` now supports the context |
| 127 | management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``. |
| 128 | (Contributed by Hagen Fuerstenau; :issue:`3860`.) |
| 129 | |
| 130 | * A new :class:`Counter` class in the :mod:`collections` module is |
| 131 | useful for tallying data. :class:`Counter` instances behave mostly |
| 132 | like dictionaries but return zero for missing keys instead of |
| 133 | raising a :exc:`KeyError`:: |
| 134 | |
| 135 | >>> from collections import Counter |
| 136 | >>> c=Counter() |
| 137 | >>> for letter in 'here is a sample of english text': |
| 138 | ... c[letter] += 1 |
| 139 | ... |
| 140 | >>> c |
| 141 | Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2, |
| 142 | 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1, |
| 143 | 'p': 1, 'r': 1, 'x': 1}) |
| 144 | >>> c['e'] |
| 145 | 5 |
| 146 | >>> c['z'] |
| 147 | 0 |
| 148 | |
| 149 | There are two additional :class:`Counter` methods: :meth:`most_common` |
| 150 | returns the N most common elements and their counts, and :meth:`elements` |
| 151 | returns an iterator over the contained element, repeating each element |
| 152 | as many times as its count:: |
| 153 | |
| 154 | >>> c.most_common(5) |
| 155 | [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)] |
| 156 | >>> c.elements() -> |
| 157 | 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ', |
| 158 | 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i', |
| 159 | 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's', |
| 160 | 's', 's', 'r', 't', 't', 'x'] |
| 161 | |
| 162 | Contributed by Raymond Hettinger; :issue:`1696199`. |
| 163 | |
| 164 | * The :mod:`gzip` module's :class:`GzipFile` now supports the context |
| 165 | management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``. |
| 166 | (Contributed by Hagen Fuerstenau; :issue:`3860`.) |
| 167 | |
| 168 | * The :class:`io.FileIO` class now raises an :exc:`OSError` when passed |
| 169 | an invalid file descriptor. (Implemented by Benjamin Peterson; |
| 170 | :issue:`4991`.) |
| 171 | |
| 172 | * The :mod:`pydoc` module now has help for the various symbols that Python |
| 173 | uses. You can now do ``help('<<')`` or ``help('@')``, for example. |
| 174 | (Contributed by David Laban; :issue:`4739`.) |
| 175 | |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 176 | * A new function in the :mod:`subprocess` module, |
| 177 | :func:`check_output`, runs a command with a specified set of arguments |
| 178 | and returns the command's output as a string if the command runs without |
| 179 | error, or raises a :exc:`CalledProcessError` exception otherwise. |
| 180 | |
| 181 | :: |
| 182 | |
| 183 | >>> subprocess.check_output(['df', '-h', '.']) |
| 184 | 'Filesystem Size Used Avail Capacity Mounted on\n |
| 185 | /dev/disk0s2 52G 49G 3.0G 94% /\n' |
| 186 | |
| 187 | >>> subprocess.check_output(['df', '-h', '/bogus']) |
| 188 | ... |
| 189 | subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1 |
| 190 | |
| 191 | (Contributed by Gregory P. Smith.) |
| 192 | |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 193 | * The :func:`is_zipfile` function in the :mod:`zipfile` module will now |
| 194 | accept a file object, in addition to the path names accepted in earlier |
| 195 | versions. (Contributed by Gabriel Genellina; :issue:`4756`.) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 196 | |
| 197 | .. ====================================================================== |
| 198 | .. whole new modules get described in subsections here |
| 199 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame^] | 200 | ttk: Themed Widgets for Tk |
| 201 | -------------------------- |
| 202 | |
| 203 | Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk |
| 204 | widgets but have a more customizable appearance and can therefore more |
| 205 | closely resemble the native platform's widgets. This widget |
| 206 | set was originally called Tile, but was renamed to Ttk (for "themed Tk") |
| 207 | on being added to Tcl/Tck release 8.5. |
| 208 | |
| 209 | XXX write a brief discussion and an example here. |
| 210 | |
| 211 | The :mod:`ttk` module was written by Guilherme Polo and added in |
| 212 | :issue:`2983`. An alternate version called ``Tile.py``, written by |
| 213 | Martin Franklin and maintained by Kevin Walzer, was proposed for |
| 214 | inclusion in :issue:`2618`, but the authors argued that Guilherme |
| 215 | Polo's work was more comprehensive. |
| 216 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 217 | .. ====================================================================== |
| 218 | |
| 219 | |
| 220 | Build and C API Changes |
| 221 | ======================= |
| 222 | |
| 223 | Changes to Python's build process and to the C API include: |
| 224 | |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 225 | * If you use the :file:`.gdbinit` file provided with Python, |
| 226 | the "pyo" macro in the 2.7 version will now work when the thread being |
| 227 | debugged doesn't hold the GIL; the macro will now acquire it before printing. |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 228 | (Contributed by Victor Stinner; :issue:`3632`.) |
| 229 | |
| 230 | * :cfunc:`Py_AddPendingCall` is now thread safe, letting any |
| 231 | worker thread submit notifications to the main Python thread. This |
| 232 | is particularly useful for asynchronous IO operations. |
| 233 | (Contributed by Kristjan Valur Jonsson; :issue:`4293`.) |
| 234 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 235 | |
| 236 | .. ====================================================================== |
| 237 | |
| 238 | Port-Specific Changes: Windows |
| 239 | ----------------------------------- |
| 240 | |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 241 | * The :mod:`msvcrt` module now contains some constants from |
| 242 | the :file:`crtassem.h` header file: |
| 243 | :data:`CRT_ASSEMBLY_VERSION`, |
| 244 | :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`, |
| 245 | and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`. |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 246 | (Contributed by David Cournapeau; :issue:`4365`.) |
| 247 | |
| 248 | * The new :cfunc:`_beginthreadex` API is used to start threads, and |
| 249 | the native thread-local storage functions are now used. |
| 250 | (Contributed by Kristjan Valur Jonsson; :issue:`3582`.) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 251 | |
| 252 | .. ====================================================================== |
| 253 | |
| 254 | Port-Specific Changes: Mac OS X |
| 255 | ----------------------------------- |
| 256 | |
| 257 | |
| 258 | .. ====================================================================== |
| 259 | |
| 260 | Porting to Python 2.7 |
| 261 | ===================== |
| 262 | |
| 263 | This section lists previously described changes and other bugfixes |
| 264 | that may require changes to your code: |
| 265 | |
| 266 | To be written. |
| 267 | |
| 268 | .. ====================================================================== |
| 269 | |
| 270 | |
| 271 | .. _acks27: |
| 272 | |
| 273 | Acknowledgements |
| 274 | ================ |
| 275 | |
| 276 | The author would like to thank the following people for offering |
| 277 | suggestions, corrections and assistance with various drafts of this |
| 278 | article: no one yet. |
| 279 | |