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