Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | **************************** |
| 2 | What's New in Python 2.6 |
| 3 | **************************** |
| 4 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 5 | .. % XXX mention switch to reST for documentation |
| 6 | .. % XXX mention switch to Roundup for bug tracking |
| 7 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 8 | :Author: A.M. Kuchling |
| 9 | :Release: |release| |
| 10 | :Date: |today| |
| 11 | |
| 12 | .. % $Id: whatsnew26.tex 55746 2007-06-02 18:33:53Z neal.norwitz $ |
| 13 | .. % Rules for maintenance: |
| 14 | .. % |
| 15 | .. % * Anyone can add text to this document. Do not spend very much time |
| 16 | .. % on the wording of your changes, because your text will probably |
| 17 | .. % get rewritten to some degree. |
| 18 | .. % |
| 19 | .. % * The maintainer will go through Misc/NEWS periodically and add |
| 20 | .. % changes; it's therefore more important to add your changes to |
| 21 | .. % Misc/NEWS than to this file. |
| 22 | .. % |
| 23 | .. % * This is not a complete list of every single change; completeness |
| 24 | .. % is the purpose of Misc/NEWS. Some changes I consider too small |
| 25 | .. % or esoteric to include. If such a change is added to the text, |
| 26 | .. % I'll just remove it. (This is another reason you shouldn't spend |
| 27 | .. % too much time on writing your addition.) |
| 28 | .. % |
| 29 | .. % * If you want to draw your new text to the attention of the |
| 30 | .. % maintainer, add 'XXX' to the beginning of the paragraph or |
| 31 | .. % section. |
| 32 | .. % |
| 33 | .. % * It's OK to just add a fragmentary note about a change. For |
| 34 | .. % example: "XXX Describe the transmogrify() function added to the |
| 35 | .. % socket module." The maintainer will research the change and |
| 36 | .. % write the necessary text. |
| 37 | .. % |
| 38 | .. % * You can comment out your additions if you like, but it's not |
| 39 | .. % necessary (especially when a final release is some months away). |
| 40 | .. % |
| 41 | .. % * Credit the author of a patch or bugfix. Just the name is |
| 42 | .. % sufficient; the e-mail address isn't necessary. |
| 43 | .. % |
| 44 | .. % * It's helpful to add the bug/patch number as a comment: |
| 45 | .. % |
| 46 | .. % % Patch 12345 |
| 47 | .. % XXX Describe the transmogrify() function added to the socket |
| 48 | .. % module. |
| 49 | .. % (Contributed by P.Y. Developer.) |
| 50 | .. % |
| 51 | .. % This saves the maintainer the effort of going through the SVN log |
| 52 | .. % when researching a change. |
| 53 | |
| 54 | This article explains the new features in Python 2.6. No release date for |
| 55 | Python 2.6 has been set; it will probably be released in mid 2008. |
| 56 | |
| 57 | This article doesn't attempt to provide a complete specification of the new |
| 58 | features, but instead provides a convenient overview. For full details, you |
| 59 | should refer to the documentation for Python 2.6. If you want to understand the |
| 60 | complete implementation and design rationale, refer to the PEP for a particular |
| 61 | new feature. |
| 62 | |
| 63 | .. % Compare with previous release in 2 - 3 sentences here. |
| 64 | .. % add hyperlink when the documentation becomes available online. |
| 65 | |
| 66 | .. % ====================================================================== |
| 67 | .. % Large, PEP-level features and changes should be described here. |
| 68 | .. % Should there be a new section here for 3k migration? |
| 69 | .. % Or perhaps a more general section describing module changes/deprecation? |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 70 | .. % ====================================================================== |
| 71 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 72 | Python 3.0 |
| 73 | ================ |
| 74 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 75 | The development cycle for Python 2.6 also saw the release of the first |
| 76 | alphas of Python 3.0, and the development of 3.0 has influenced |
| 77 | a number of features in 2.6. |
| 78 | |
| 79 | Python 3.0 is a far-ranging redesign of Python that breaks |
| 80 | compatibility with the 2.x series. This means that existing Python |
| 81 | code will need a certain amount of conversion in order to run on |
| 82 | Python 3.0. However, not all the changes in 3.0 necessarily break |
| 83 | compatibility. In cases where new features won't cause existing code |
| 84 | to break, they've been backported to 2.6 and are described in this |
| 85 | document in the appropriate place. Some of the 3.0-derived features |
| 86 | are: |
| 87 | |
| 88 | * A :meth:`__complex__` method for converting objects to a complex number. |
| 89 | * Alternate syntax for catching exceptions: ``except TypeError as exc``. |
| 90 | * The addition of :func:`functools.reduce` as a synonym for the built-in |
| 91 | :func:`reduce` function. |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 92 | |
| 93 | A new command-line switch, :option:`-3`, enables warnings |
| 94 | about features that will be removed in Python 3.0. You can run code |
| 95 | with this switch to see how much work will be necessary to port |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 96 | code to 3.0. The value of this switch is available |
| 97 | to Python code as the boolean variable ``sys.py3kwarning``, |
| 98 | and to C extension code as :cdata:`Py_Py3kWarningFlag`. |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 99 | |
| 100 | .. seealso:: |
| 101 | |
| 102 | The 3xxx series of PEPs, which describes the development process for |
| 103 | Python 3.0 and various features that have been accepted, rejected, |
| 104 | or are still under consideration. |
| 105 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 106 | |
| 107 | Development Changes |
| 108 | ================================================== |
| 109 | |
| 110 | While 2.6 was being developed, the Python development process |
| 111 | underwent two significant changes: the developer group |
| 112 | switched from SourceForge's issue tracker to a customized |
| 113 | Roundup installation, and the documentation was converted from |
| 114 | LaTeX to reStructured Text. |
| 115 | |
| 116 | |
| 117 | New Issue Tracker: Roundup |
| 118 | -------------------------------------------------- |
| 119 | |
| 120 | XXX write this. |
| 121 | |
| 122 | |
| 123 | New Documentation Format: ReStructured Text |
| 124 | -------------------------------------------------- |
| 125 | |
| 126 | Python's documentation had been written using LaTeX since the |
| 127 | project's inception around 1989. At that time, most documentation was |
| 128 | printed out for later study, not viewed online. LaTeX was widely used |
| 129 | because it provided attractive printed output while |
| 130 | remaining straightforward to write, once the basic rules |
| 131 | of the markup have been learned. |
| 132 | |
| 133 | LaTeX is still used today for writing technical publications destined |
| 134 | for printing, but the landscape for programming tools has shifted. We |
| 135 | no longer print out reams of documentation; instead, we browse through |
| 136 | it online and HTML is the most important format to support. |
| 137 | Unfortunately, converting LaTeX to HTML is fairly complicated, and |
| 138 | Fred L. Drake Jr., the Python documentation editor for many years, |
| 139 | spent a lot of time wrestling the conversion process into shape. |
| 140 | Occasionally people would suggest converting the documentation into |
| 141 | SGML or, later, XML, but performing a good conversion is a major task |
| 142 | and no one pursued the task to completion. |
| 143 | |
| 144 | During the 2.6 development cycle, Georg Brandl put a substantial |
| 145 | effort into building a new toolchain called Sphinx |
| 146 | for processing the documentation. |
| 147 | The input format is reStructured Text, |
| 148 | a markup commonly used in the Python community that supports |
| 149 | custom extensions and directives. Sphinx concentrates |
Andrew M. Kuchling | 2d60cf7 | 2007-12-22 17:27:02 +0000 | [diff] [blame] | 150 | on HTML output, producing attractively styled |
| 151 | and modern HTML, but printed output is still supported through |
| 152 | conversion to LaTeX as an output format. |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 153 | |
| 154 | .. seealso:: |
| 155 | |
| 156 | `Docutils <http://docutils.sf.net>`__: The fundamental |
| 157 | reStructured Text parser and toolset. |
| 158 | |
Andrew M. Kuchling | 2d60cf7 | 2007-12-22 17:27:02 +0000 | [diff] [blame] | 159 | `Documenting Python <http://docs.python.org/dev/documenting/>`__: Describes how to write for |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 160 | Python's documentation. |
| 161 | |
| 162 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 163 | PEP 343: The 'with' statement |
| 164 | ============================= |
| 165 | |
| 166 | The previous version, Python 2.5, added the ':keyword:`with`' |
| 167 | statement an optional feature, to be enabled by a ``from __future__ |
Andrew M. Kuchling | 6e751f4 | 2007-12-03 21:28:41 +0000 | [diff] [blame] | 168 | import with_statement`` directive. In 2.6 the statement no longer needs to |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 169 | be specially enabled; this means that :keyword:`with` is now always a |
| 170 | keyword. The rest of this section is a copy of the corresponding |
| 171 | section from "What's New in Python 2.5" document; if you read |
| 172 | it back when Python 2.5 came out, you can skip the rest of this |
| 173 | section. |
| 174 | |
| 175 | The ':keyword:`with`' statement clarifies code that previously would use |
| 176 | ``try...finally`` blocks to ensure that clean-up code is executed. In this |
| 177 | section, I'll discuss the statement as it will commonly be used. In the next |
| 178 | section, I'll examine the implementation details and show how to write objects |
| 179 | for use with this statement. |
| 180 | |
| 181 | The ':keyword:`with`' statement is a new control-flow structure whose basic |
| 182 | structure is:: |
| 183 | |
| 184 | with expression [as variable]: |
| 185 | with-block |
| 186 | |
| 187 | The expression is evaluated, and it should result in an object that supports the |
| 188 | context management protocol (that is, has :meth:`__enter__` and :meth:`__exit__` |
| 189 | methods. |
| 190 | |
| 191 | The object's :meth:`__enter__` is called before *with-block* is executed and |
| 192 | therefore can run set-up code. It also may return a value that is bound to the |
| 193 | name *variable*, if given. (Note carefully that *variable* is *not* assigned |
| 194 | the result of *expression*.) |
| 195 | |
| 196 | After execution of the *with-block* is finished, the object's :meth:`__exit__` |
| 197 | method is called, even if the block raised an exception, and can therefore run |
| 198 | clean-up code. |
| 199 | |
| 200 | Some standard Python objects now support the context management protocol and can |
| 201 | be used with the ':keyword:`with`' statement. File objects are one example:: |
| 202 | |
| 203 | with open('/etc/passwd', 'r') as f: |
| 204 | for line in f: |
| 205 | print line |
| 206 | ... more processing code ... |
| 207 | |
| 208 | After this statement has executed, the file object in *f* will have been |
| 209 | automatically closed, even if the :keyword:`for` loop raised an exception part- |
| 210 | way through the block. |
| 211 | |
| 212 | .. note:: |
| 213 | |
| 214 | In this case, *f* is the same object created by :func:`open`, because |
| 215 | :meth:`file.__enter__` returns *self*. |
| 216 | |
| 217 | The :mod:`threading` module's locks and condition variables also support the |
| 218 | ':keyword:`with`' statement:: |
| 219 | |
| 220 | lock = threading.Lock() |
| 221 | with lock: |
| 222 | # Critical section of code |
| 223 | ... |
| 224 | |
| 225 | The lock is acquired before the block is executed and always released once the |
| 226 | block is complete. |
| 227 | |
| 228 | The new :func:`localcontext` function in the :mod:`decimal` module makes it easy |
| 229 | to save and restore the current decimal context, which encapsulates the desired |
| 230 | precision and rounding characteristics for computations:: |
| 231 | |
| 232 | from decimal import Decimal, Context, localcontext |
| 233 | |
| 234 | # Displays with default precision of 28 digits |
| 235 | v = Decimal('578') |
| 236 | print v.sqrt() |
| 237 | |
| 238 | with localcontext(Context(prec=16)): |
| 239 | # All code in this block uses a precision of 16 digits. |
| 240 | # The original context is restored on exiting the block. |
| 241 | print v.sqrt() |
| 242 | |
| 243 | |
| 244 | .. _new-26-context-managers: |
| 245 | |
| 246 | Writing Context Managers |
| 247 | ------------------------ |
| 248 | |
| 249 | Under the hood, the ':keyword:`with`' statement is fairly complicated. Most |
| 250 | people will only use ':keyword:`with`' in company with existing objects and |
| 251 | don't need to know these details, so you can skip the rest of this section if |
| 252 | you like. Authors of new objects will need to understand the details of the |
| 253 | underlying implementation and should keep reading. |
| 254 | |
| 255 | A high-level explanation of the context management protocol is: |
| 256 | |
| 257 | * The expression is evaluated and should result in an object called a "context |
| 258 | manager". The context manager must have :meth:`__enter__` and :meth:`__exit__` |
| 259 | methods. |
| 260 | |
| 261 | * The context manager's :meth:`__enter__` method is called. The value returned |
Georg Brandl | d41b8dc | 2007-12-16 23:15:07 +0000 | [diff] [blame] | 262 | is assigned to *VAR*. If no ``as VAR`` clause is present, the value is simply |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 263 | discarded. |
| 264 | |
| 265 | * The code in *BLOCK* is executed. |
| 266 | |
| 267 | * If *BLOCK* raises an exception, the :meth:`__exit__(type, value, traceback)` |
| 268 | is called with the exception details, the same values returned by |
| 269 | :func:`sys.exc_info`. The method's return value controls whether the exception |
| 270 | is re-raised: any false value re-raises the exception, and ``True`` will result |
| 271 | in suppressing it. You'll only rarely want to suppress the exception, because |
| 272 | if you do the author of the code containing the ':keyword:`with`' statement will |
| 273 | never realize anything went wrong. |
| 274 | |
| 275 | * If *BLOCK* didn't raise an exception, the :meth:`__exit__` method is still |
| 276 | called, but *type*, *value*, and *traceback* are all ``None``. |
| 277 | |
| 278 | Let's think through an example. I won't present detailed code but will only |
| 279 | sketch the methods necessary for a database that supports transactions. |
| 280 | |
| 281 | (For people unfamiliar with database terminology: a set of changes to the |
| 282 | database are grouped into a transaction. Transactions can be either committed, |
| 283 | meaning that all the changes are written into the database, or rolled back, |
| 284 | meaning that the changes are all discarded and the database is unchanged. See |
| 285 | any database textbook for more information.) |
| 286 | |
| 287 | Let's assume there's an object representing a database connection. Our goal will |
| 288 | be to let the user write code like this:: |
| 289 | |
| 290 | db_connection = DatabaseConnection() |
| 291 | with db_connection as cursor: |
| 292 | cursor.execute('insert into ...') |
| 293 | cursor.execute('delete from ...') |
| 294 | # ... more operations ... |
| 295 | |
| 296 | The transaction should be committed if the code in the block runs flawlessly or |
| 297 | rolled back if there's an exception. Here's the basic interface for |
| 298 | :class:`DatabaseConnection` that I'll assume:: |
| 299 | |
| 300 | class DatabaseConnection: |
| 301 | # Database interface |
Georg Brandl | 9f72d23 | 2007-12-16 23:13:29 +0000 | [diff] [blame] | 302 | def cursor(self): |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 303 | "Returns a cursor object and starts a new transaction" |
Georg Brandl | 9f72d23 | 2007-12-16 23:13:29 +0000 | [diff] [blame] | 304 | def commit(self): |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 305 | "Commits current transaction" |
Georg Brandl | 9f72d23 | 2007-12-16 23:13:29 +0000 | [diff] [blame] | 306 | def rollback(self): |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 307 | "Rolls back current transaction" |
| 308 | |
| 309 | The :meth:`__enter__` method is pretty easy, having only to start a new |
| 310 | transaction. For this application the resulting cursor object would be a useful |
| 311 | result, so the method will return it. The user can then add ``as cursor`` to |
| 312 | their ':keyword:`with`' statement to bind the cursor to a variable name. :: |
| 313 | |
| 314 | class DatabaseConnection: |
| 315 | ... |
Georg Brandl | 9f72d23 | 2007-12-16 23:13:29 +0000 | [diff] [blame] | 316 | def __enter__(self): |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 317 | # Code to start a new transaction |
| 318 | cursor = self.cursor() |
| 319 | return cursor |
| 320 | |
| 321 | The :meth:`__exit__` method is the most complicated because it's where most of |
| 322 | the work has to be done. The method has to check if an exception occurred. If |
| 323 | there was no exception, the transaction is committed. The transaction is rolled |
| 324 | back if there was an exception. |
| 325 | |
| 326 | In the code below, execution will just fall off the end of the function, |
| 327 | returning the default value of ``None``. ``None`` is false, so the exception |
| 328 | will be re-raised automatically. If you wished, you could be more explicit and |
| 329 | add a :keyword:`return` statement at the marked location. :: |
| 330 | |
| 331 | class DatabaseConnection: |
| 332 | ... |
Georg Brandl | 9f72d23 | 2007-12-16 23:13:29 +0000 | [diff] [blame] | 333 | def __exit__(self, type, value, tb): |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 334 | if tb is None: |
| 335 | # No exception, so commit |
| 336 | self.commit() |
| 337 | else: |
| 338 | # Exception occurred, so rollback. |
| 339 | self.rollback() |
| 340 | # return False |
| 341 | |
| 342 | |
| 343 | .. _module-contextlib: |
| 344 | |
| 345 | The contextlib module |
| 346 | --------------------- |
| 347 | |
| 348 | The new :mod:`contextlib` module provides some functions and a decorator that |
| 349 | are useful for writing objects for use with the ':keyword:`with`' statement. |
| 350 | |
| 351 | The decorator is called :func:`contextmanager`, and lets you write a single |
| 352 | generator function instead of defining a new class. The generator should yield |
| 353 | exactly one value. The code up to the :keyword:`yield` will be executed as the |
| 354 | :meth:`__enter__` method, and the value yielded will be the method's return |
| 355 | value that will get bound to the variable in the ':keyword:`with`' statement's |
| 356 | :keyword:`as` clause, if any. The code after the :keyword:`yield` will be |
| 357 | executed in the :meth:`__exit__` method. Any exception raised in the block will |
| 358 | be raised by the :keyword:`yield` statement. |
| 359 | |
| 360 | Our database example from the previous section could be written using this |
| 361 | decorator as:: |
| 362 | |
| 363 | from contextlib import contextmanager |
| 364 | |
| 365 | @contextmanager |
Georg Brandl | 9f72d23 | 2007-12-16 23:13:29 +0000 | [diff] [blame] | 366 | def db_transaction(connection): |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 367 | cursor = connection.cursor() |
| 368 | try: |
| 369 | yield cursor |
| 370 | except: |
| 371 | connection.rollback() |
| 372 | raise |
| 373 | else: |
| 374 | connection.commit() |
| 375 | |
| 376 | db = DatabaseConnection() |
| 377 | with db_transaction(db) as cursor: |
| 378 | ... |
| 379 | |
| 380 | The :mod:`contextlib` module also has a :func:`nested(mgr1, mgr2, ...)` function |
| 381 | that combines a number of context managers so you don't need to write nested |
| 382 | ':keyword:`with`' statements. In this example, the single ':keyword:`with`' |
| 383 | statement both starts a database transaction and acquires a thread lock:: |
| 384 | |
| 385 | lock = threading.Lock() |
| 386 | with nested (db_transaction(db), lock) as (cursor, locked): |
| 387 | ... |
| 388 | |
| 389 | Finally, the :func:`closing(object)` function returns *object* so that it can be |
| 390 | bound to a variable, and calls ``object.close`` at the end of the block. :: |
| 391 | |
| 392 | import urllib, sys |
| 393 | from contextlib import closing |
| 394 | |
| 395 | with closing(urllib.urlopen('http://www.yahoo.com')) as f: |
| 396 | for line in f: |
| 397 | sys.stdout.write(line) |
| 398 | |
| 399 | |
| 400 | .. seealso:: |
| 401 | |
| 402 | :pep:`343` - The "with" statement |
| 403 | PEP written by Guido van Rossum and Nick Coghlan; implemented by Mike Bland, |
| 404 | Guido van Rossum, and Neal Norwitz. The PEP shows the code generated for a |
| 405 | ':keyword:`with`' statement, which can be helpful in learning how the statement |
| 406 | works. |
| 407 | |
| 408 | The documentation for the :mod:`contextlib` module. |
| 409 | |
| 410 | .. % ====================================================================== |
| 411 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 412 | .. _pep-0366: |
| 413 | |
| 414 | PEP 366: Explicit Relative Imports From a Main Module |
| 415 | ============================================================ |
| 416 | |
| 417 | Python's :option:`-m` switch allows running a module as a script. |
| 418 | When you ran a module that was located inside a package, relative |
| 419 | imports didn't work correctly. |
| 420 | |
| 421 | The fix in Python 2.6 adds a :attr:`__package__` attribute to modules. |
| 422 | When present, relative imports will be relative to the value of this |
| 423 | attribute instead of the :attr:`__name__` attribute. PEP 302-style |
| 424 | importers can then set :attr:`__package__`. The :mod:`runpy` module |
| 425 | that implements the :option:`-m` switch now does this, so relative imports |
| 426 | can now be used in scripts running from inside a package. |
| 427 | |
| 428 | .. % ====================================================================== |
| 429 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 430 | .. _pep-3110: |
| 431 | |
| 432 | PEP 3110: Exception-Handling Changes |
| 433 | ===================================================== |
| 434 | |
| 435 | One error that Python programmers occasionally make |
| 436 | is the following:: |
| 437 | |
| 438 | try: |
| 439 | ... |
| 440 | except TypeError, ValueError: |
| 441 | ... |
| 442 | |
| 443 | The author is probably trying to catch both |
| 444 | :exc:`TypeError` and :exc:`ValueError` exceptions, but this code |
| 445 | actually does something different: it will catch |
| 446 | :exc:`TypeError` and bind the resulting exception object |
| 447 | to the local name ``"ValueError"``. The correct code |
| 448 | would have specified a tuple:: |
| 449 | |
| 450 | try: |
| 451 | ... |
| 452 | except (TypeError, ValueError): |
| 453 | ... |
| 454 | |
| 455 | This error is possible because the use of the comma here is ambiguous: |
| 456 | does it indicate two different nodes in the parse tree, or a single |
| 457 | node that's a tuple. |
| 458 | |
| 459 | Python 3.0 changes the syntax to make this unambiguous by replacing |
| 460 | the comma with the word "as". To catch an exception and store the |
| 461 | exception object in the variable ``exc``, you must write:: |
| 462 | |
| 463 | try: |
| 464 | ... |
| 465 | except TypeError as exc: |
| 466 | ... |
| 467 | |
| 468 | Python 3.0 will only support the use of "as", and therefore interprets |
| 469 | the first example as catching two different exceptions. Python 2.6 |
| 470 | supports both the comma and "as", so existing code will continue to |
| 471 | work. |
| 472 | |
| 473 | .. seealso:: |
| 474 | |
| 475 | :pep:`3110` - Catching Exceptions in Python 3000 |
| 476 | PEP written and implemented by Collin Winter. |
| 477 | |
| 478 | .. % ====================================================================== |
| 479 | |
| 480 | .. _pep-3119: |
| 481 | |
| 482 | PEP 3119: Abstract Base Classes |
| 483 | ===================================================== |
| 484 | |
| 485 | XXX |
| 486 | |
| 487 | .. seealso:: |
| 488 | |
| 489 | :pep:`3119` - Introducing Abstract Base Classes |
| 490 | PEP written by Guido van Rossum and Talin. |
| 491 | Implemented by XXX. |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 492 | Backported to 2.6 by Benjamin Aranguren, with Alex Martelli. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 493 | |
| 494 | Other Language Changes |
| 495 | ====================== |
| 496 | |
| 497 | Here are all of the changes that Python 2.6 makes to the core Python language. |
| 498 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 499 | * When calling a function using the ``**`` syntax to provide keyword |
| 500 | arguments, you are no longer required to use a Python dictionary; |
| 501 | any mapping will now work:: |
| 502 | |
| 503 | >>> def f(**kw): |
| 504 | ... print sorted(kw) |
| 505 | ... |
| 506 | >>> ud=UserDict.UserDict() |
| 507 | >>> ud['a'] = 1 |
| 508 | >>> ud['b'] = 'string' |
| 509 | >>> f(**ud) |
| 510 | ['a', 'b'] |
| 511 | |
| 512 | .. % Patch 1686487 |
| 513 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 514 | * The built-in types now have improved support for extended slicing syntax, |
| 515 | where various combinations of ``(start, stop, step)`` are supplied. |
| 516 | Previously, the support was partial and certain corner cases wouldn't work. |
| 517 | (Implemented by Thomas Wouters.) |
| 518 | |
| 519 | .. % Revision 57619 |
| 520 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 521 | * Properties now have two attributes, |
| 522 | :attr:`setter` and :attr:`deleter`, that are useful shortcuts for |
| 523 | adding a setter or deleter function to an existing property. |
| 524 | You would use them like this:: |
| 525 | |
| 526 | class C(object): |
| 527 | @property |
| 528 | def x(self): |
| 529 | return self._x |
| 530 | |
| 531 | @x.setter |
| 532 | def x(self, value): |
| 533 | self._x = value |
| 534 | |
| 535 | @x.deleter |
| 536 | def x(self): |
| 537 | del self._x |
| 538 | |
| 539 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 540 | * C functions and methods that use |
| 541 | :cfunc:`PyComplex_AsCComplex` will now accept arguments that |
| 542 | have a :meth:`__complex__` method. In particular, the functions in the |
| 543 | :mod:`cmath` module will now accept objects with this method. |
| 544 | This is a backport of a Python 3.0 change. |
| 545 | (Contributed by Mark Dickinson.) |
| 546 | |
| 547 | .. % Patch #1675423 |
| 548 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 549 | A numerical nicety: when creating a complex number from two floats |
| 550 | on systems that support signed zeros (-0 and +0), the |
| 551 | :func:`complex()` constructor will now preserve the sign |
| 552 | of the zero. |
| 553 | |
| 554 | .. % Patch 1507 |
| 555 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 556 | * Changes to the :class:`Exception` interface |
| 557 | as dictated by :pep:`352` continue to be made. For 2.6, |
| 558 | the :attr:`message` attribute is being deprecated in favor of the |
| 559 | :attr:`args` attribute. |
| 560 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 561 | * The :exc:`GeneratorExit` exception now subclasses |
| 562 | :exc:`BaseException` instead of :exc:`Exception`. This means |
| 563 | that an exception handler that does ``except Exception:`` |
| 564 | will not inadvertently catch :exc:`GeneratorExit`. |
| 565 | (Contributed by Chad Austin.) |
| 566 | |
| 567 | .. % Patch #1537 |
| 568 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 569 | * The :func:`compile` built-in function now accepts keyword arguments |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 570 | as well as positional parameters. (Contributed by Thomas Wouters.) |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 571 | |
| 572 | .. % Patch 1444529 |
| 573 | |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 574 | * The :func:`complex` constructor now accepts strings containing |
| 575 | parenthesized complex numbers, letting ``complex(repr(cmplx))`` |
| 576 | will now round-trip values. For example, ``complex('(3+4j)')`` |
| 577 | now returns the value (3+4j). |
| 578 | |
| 579 | .. % Patch 1491866 |
| 580 | |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 581 | * The string :meth:`translate` method now accepts ``None`` as the |
| 582 | translation table parameter, which is treated as the identity |
| 583 | transformation. This makes it easier to carry out operations |
| 584 | that only delete characters. (Contributed by Bengt Richter.) |
| 585 | |
| 586 | .. % Patch 1193128 |
| 587 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 588 | * The built-in :func:`dir` function now checks for a :meth:`__dir__` |
| 589 | method on the objects it receives. This method must return a list |
| 590 | of strings containing the names of valid attributes for the object, |
| 591 | and lets the object control the value that :func:`dir` produces. |
| 592 | Objects that have :meth:`__getattr__` or :meth:`__getattribute__` |
Facundo Batista | bd5b623 | 2007-12-03 19:49:54 +0000 | [diff] [blame] | 593 | methods can use this to advertise pseudo-attributes they will honor. |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 594 | |
| 595 | .. % Patch 1591665 |
| 596 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 597 | * An obscure change: when you use the the :func:`locals` function inside a |
| 598 | :keyword:`class` statement, the resulting dictionary no longer returns free |
| 599 | variables. (Free variables, in this case, are variables referred to in the |
| 600 | :keyword:`class` statement that aren't attributes of the class.) |
| 601 | |
| 602 | .. % ====================================================================== |
| 603 | |
| 604 | |
| 605 | Optimizations |
| 606 | ------------- |
| 607 | |
| 608 | * Internally, a bit is now set in type objects to indicate some of the standard |
| 609 | built-in types. This speeds up checking if an object is a subclass of one of |
| 610 | these types. (Contributed by Neal Norwitz.) |
| 611 | |
| 612 | The net result of the 2.6 optimizations is that Python 2.6 runs the pystone |
| 613 | benchmark around XX% faster than Python 2.5. |
| 614 | |
| 615 | .. % ====================================================================== |
| 616 | |
| 617 | |
| 618 | New, Improved, and Deprecated Modules |
| 619 | ===================================== |
| 620 | |
| 621 | As usual, Python's standard library received a number of enhancements and bug |
| 622 | fixes. Here's a partial list of the most notable changes, sorted alphabetically |
| 623 | by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more |
| 624 | complete list of changes, or look through the CVS logs for all the details. |
| 625 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 626 | * The :mod:`bsddb.dbshelve` module now uses the highest pickling protocol |
| 627 | available, instead of restricting itself to protocol 1. |
| 628 | (Contributed by W. Barnes.) |
| 629 | |
| 630 | .. % Patch 1551443 |
| 631 | |
Andrew M. Kuchling | 6d57c82 | 2007-10-23 20:55:47 +0000 | [diff] [blame] | 632 | * A new data type in the :mod:`collections` module: :class:`namedtuple(typename, |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 633 | fieldnames)` is a factory function that creates subclasses of the standard tuple |
| 634 | whose fields are accessible by name as well as index. For example:: |
| 635 | |
Andrew M. Kuchling | 6d57c82 | 2007-10-23 20:55:47 +0000 | [diff] [blame] | 636 | >>> var_type = collections.namedtuple('variable', |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 637 | ... 'id name type size') |
| 638 | # Names are separated by spaces or commas. |
| 639 | # 'id, name, type, size' would also work. |
Raymond Hettinger | 366523c | 2007-12-14 18:12:21 +0000 | [diff] [blame] | 640 | >>> var_type._fields |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 641 | ('id', 'name', 'type', 'size') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 642 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 643 | >>> var = var_type(1, 'frequency', 'int', 4) |
| 644 | >>> print var[0], var.id # Equivalent |
| 645 | 1 1 |
| 646 | >>> print var[2], var.type # Equivalent |
| 647 | int int |
Raymond Hettinger | 366523c | 2007-12-14 18:12:21 +0000 | [diff] [blame] | 648 | >>> var._asdict() |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 649 | {'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'} |
Raymond Hettinger | 366523c | 2007-12-14 18:12:21 +0000 | [diff] [blame] | 650 | >>> v2 = var._replace('name', 'amplitude') |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 651 | >>> v2 |
| 652 | variable(id=1, name='amplitude', type='int', size=4) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 653 | |
| 654 | (Contributed by Raymond Hettinger.) |
| 655 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 656 | * Another change to the :mod:`collections` module is that the |
Georg Brandl | e7d118a | 2007-12-08 11:05:05 +0000 | [diff] [blame] | 657 | :class:`deque` type now supports an optional *maxlen* parameter; |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 658 | if supplied, the deque's size will be restricted to no more |
Georg Brandl | e7d118a | 2007-12-08 11:05:05 +0000 | [diff] [blame] | 659 | than *maxlen* items. Adding more items to a full deque causes |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 660 | old items to be discarded. |
| 661 | |
| 662 | :: |
| 663 | |
| 664 | >>> from collections import deque |
| 665 | >>> dq=deque(maxlen=3) |
| 666 | >>> dq |
| 667 | deque([], maxlen=3) |
| 668 | >>> dq.append(1) ; dq.append(2) ; dq.append(3) |
| 669 | >>> dq |
| 670 | deque([1, 2, 3], maxlen=3) |
| 671 | >>> dq.append(4) |
| 672 | >>> dq |
| 673 | deque([2, 3, 4], maxlen=3) |
| 674 | |
| 675 | (Contributed by Raymond Hettinger.) |
| 676 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 677 | * The :mod:`ctypes` module now supports a :class:`c_bool` datatype |
| 678 | that represents the C99 ``bool`` type. (Contributed by David Remahl.) |
| 679 | |
| 680 | .. % Patch 1649190 |
| 681 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 682 | The :mod:`ctypes` string, buffer and array types also have improved |
| 683 | support for extended slicing syntax, |
| 684 | where various combinations of ``(start, stop, step)`` are supplied. |
| 685 | (Implemented by Thomas Wouters.) |
| 686 | |
| 687 | .. % Revision 57769 |
| 688 | |
| 689 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 690 | * A new method in the :mod:`curses` module: for a window, :meth:`chgat` changes |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 691 | the display characters for a certain number of characters on a single line. |
| 692 | :: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 693 | |
| 694 | # Boldface text starting at y=0,x=21 |
| 695 | # and affecting the rest of the line. |
| 696 | stdscr.chgat(0,21, curses.A_BOLD) |
| 697 | |
| 698 | (Contributed by Fabian Kreutz.) |
| 699 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 700 | * The :mod:`decimal` module was updated to version 1.66 of |
| 701 | `the General Decimal Specification <http://www2.hursley.ibm.com/decimal/decarith.html>`__. New features |
| 702 | include some methods for some basic mathematical functions such as |
| 703 | :meth:`exp` and :meth:`log10`:: |
| 704 | |
| 705 | >>> Decimal(1).exp() |
| 706 | Decimal("2.718281828459045235360287471") |
| 707 | >>> Decimal("2.7182818").ln() |
| 708 | Decimal("0.9999999895305022877376682436") |
| 709 | >>> Decimal(1000).log10() |
| 710 | Decimal("3") |
| 711 | |
| 712 | (Implemented by Facundo Batista and Mark Dickinson.) |
| 713 | |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 714 | * An optional ``timeout`` parameter was added to the |
| 715 | :class:`ftplib.FTP` class constructor as well as the :meth:`connect` |
| 716 | method, specifying a timeout measured in seconds. (Added by Facundo |
| 717 | Batista.) |
| 718 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 719 | * The :func:`reduce` built-in function is also available in the |
| 720 | :mod:`functools` module. In Python 3.0, the built-in is dropped and it's |
| 721 | only available from :mod:`functools`; currently there are no plans |
| 722 | to drop the built-in in the 2.x series. (Patched by |
| 723 | Christian Heimes.) |
| 724 | |
| 725 | .. % Patch 1739906 |
| 726 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 727 | * The :func:`glob.glob` function can now return Unicode filenames if |
| 728 | a Unicode path was used and Unicode filenames are matched within the directory. |
| 729 | |
| 730 | .. % Patch #1001604 |
| 731 | |
| 732 | * The :mod:`gopherlib` module has been removed. |
| 733 | |
| 734 | * A new function in the :mod:`heapq` module: ``merge(iter1, iter2, ...)`` |
| 735 | takes any number of iterables that return data *in sorted order*, and returns |
| 736 | a new iterator that returns the contents of all the iterators, also in sorted |
| 737 | order. For example:: |
| 738 | |
| 739 | heapq.merge([1, 3, 5, 9], [2, 8, 16]) -> |
| 740 | [1, 2, 3, 5, 8, 9, 16] |
| 741 | |
| 742 | (Contributed by Raymond Hettinger.) |
| 743 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 744 | * An optional ``timeout`` parameter was added to the |
| 745 | :class:`httplib.HTTPConnection` and :class:`HTTPSConnection` |
| 746 | class constructors, specifying a timeout measured in seconds. |
| 747 | (Added by Facundo Batista.) |
| 748 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 749 | * A new function in the :mod:`itertools` module: ``izip_longest(iter1, iter2, |
| 750 | ...[, fillvalue])`` makes tuples from each of the elements; if some of the |
| 751 | iterables are shorter than others, the missing values are set to *fillvalue*. |
| 752 | For example:: |
| 753 | |
| 754 | itertools.izip_longest([1,2,3], [1,2,3,4,5]) -> |
| 755 | [(1, 1), (2, 2), (3, 3), (None, 4), (None, 5)] |
| 756 | |
| 757 | (Contributed by Raymond Hettinger.) |
| 758 | |
| 759 | * The :mod:`macfs` module has been removed. This in turn required the |
| 760 | :func:`macostools.touched` function to be removed because it depended on the |
| 761 | :mod:`macfs` module. |
| 762 | |
| 763 | .. % Patch #1490190 |
| 764 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 765 | * The :mod:`new` module has been removed from Python 3.0. |
| 766 | Importing it therefore |
| 767 | triggers a warning message when Python is running in 3.0-warning |
| 768 | mode. |
| 769 | |
| 770 | * New functions in the :mod:`os` module include |
| 771 | ``fchmod(fd, mode)``, ``fchown(fd, uid, gid)``, |
| 772 | and ``lchmod(path, mode)``, on operating systems that support these |
| 773 | functions. :func:`fchmod` and :func:`fchown` let you change the mode |
| 774 | and ownership of an opened file, and :func:`lchmod` changes the mode |
| 775 | of a symlink. |
| 776 | |
| 777 | (Contributed by Georg Brandl and Christian Heimes.) |
| 778 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 779 | * The :func:`os.walk` function now has a ``followlinks`` parameter. If |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 780 | set to True, it will follow symlinks pointing to directories and |
| 781 | visit the directory's contents. For backward compatibility, the |
| 782 | parameter's default value is false. Note that the function can fall |
| 783 | into an infinite recursion if there's a symlink that points to a |
| 784 | parent directory. |
| 785 | |
| 786 | .. % Patch 1273829 |
| 787 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 788 | * The ``os.environ`` object's :meth:`clear` method will now unset the |
| 789 | environment variables using :func:`os.unsetenv` in addition to clearing |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 790 | the object's keys. (Contributed by Martin Horcicka.) |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 791 | |
| 792 | .. % Patch #1181 |
| 793 | |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 794 | * In the :mod:`os.path` module, the :func:`splitext` function |
| 795 | has been changed to not split on leading period characters. |
| 796 | This produces better results when operating on Unix's dot-files. |
| 797 | For example, ``os.path.splitext('.ipython')`` |
| 798 | now returns ``('.ipython', '')`` instead of ``('', '.ipython')``. |
| 799 | |
| 800 | .. % Bug #115886 |
| 801 | |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 802 | A new function, :func:`relpath(path, start)` returns a relative path |
| 803 | from the ``start`` path, if it's supplied, or from the current |
| 804 | working directory to the destination ``path``. (Contributed by |
| 805 | Richard Barran.) |
| 806 | |
| 807 | .. % Patch 1339796 |
| 808 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 809 | On Windows, :func:`os.path.expandvars` will now expand environment variables |
| 810 | in the form "%var%", and "~user" will be expanded into the |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 811 | user's home directory path. (Contributed by Josiah Carlson.) |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 812 | |
| 813 | .. % Patch 957650 |
| 814 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 815 | * The Python debugger provided by the :mod:`pdb` module |
| 816 | gained a new command: "run" restarts the Python program being debugged, |
| 817 | and can optionally take new command-line arguments for the program. |
| 818 | (Contributed by Rocky Bernstein.) |
| 819 | |
| 820 | .. % Patch #1393667 |
| 821 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 822 | * New functions in the :mod:`posix` module: :func:`chflags` and :func:`lchflags` |
| 823 | are wrappers for the corresponding system calls (where they're available). |
| 824 | Constants for the flag values are defined in the :mod:`stat` module; some |
| 825 | possible values include :const:`UF_IMMUTABLE` to signal the file may not be |
| 826 | changed and :const:`UF_APPEND` to indicate that data can only be appended to the |
| 827 | file. (Contributed by M. Levinson.) |
| 828 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 829 | * The :mod:`random` module's :class:`Random` objects can |
| 830 | now be pickled on a 32-bit system and unpickled on a 64-bit |
| 831 | system, and vice versa. Unfortunately, this change also means |
| 832 | that Python 2.6's :class:`Random` objects can't be unpickled correctly |
| 833 | on earlier versions of Python. |
| 834 | (Contributed by Shawn Ligocki.) |
| 835 | |
| 836 | .. % Issue 1727780 |
| 837 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 838 | * The :mod:`rgbimg` module has been removed. |
| 839 | |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 840 | * The :mod:`sets` module has been deprecated; it's better to |
| 841 | use the built-in :class:`set` and :class:`frozenset` types. |
| 842 | |
Andrew M. Kuchling | 2d60cf7 | 2007-12-22 17:27:02 +0000 | [diff] [blame] | 843 | * Integrating signal handling with GUI handling event loops |
| 844 | like those used by Tkinter or GTk+ has long been a problem; most |
| 845 | software ends up polling, waking up every fraction of a second. Thi |
| 846 | The :mod:`signal` module can now make this more efficient. |
| 847 | Calling ``signal.set_wakeup_fd(fd)`` sets a file descriptor |
| 848 | to be used; when a signal is received, a byte is written to that |
| 849 | file descriptor. There's also a C-level function, |
| 850 | :cfunc:`PySignal_SetWakeupFd`, for setting the descriptor. |
| 851 | |
| 852 | Event loops will use this by opening a pipe to create two descriptors, |
| 853 | one for reading and one for writing. The writeable descriptor |
| 854 | will be passed to :func:`set_wakeup_fd`, and the readable descriptor |
| 855 | will be added to the list of descriptors monitored by the event loop via |
| 856 | :cfunc:`select` or :cfunc:`poll`. |
| 857 | On receiving a signal, a byte will be written and the main event loop |
| 858 | will be woken up, without the need to poll. |
| 859 | |
| 860 | Contributed by Adam Olsen. |
| 861 | |
| 862 | .. % Patch 1583 |
| 863 | |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 864 | * The :mod:`smtplib` module now supports SMTP over SSL thanks to the |
| 865 | addition of the :class:`SMTP_SSL` class. This class supports an |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 866 | interface identical to the existing :class:`SMTP` class. Both |
| 867 | class constructors also have an optional ``timeout`` parameter |
| 868 | that specifies a timeout for the initial connection attempt, measured in |
| 869 | seconds. |
| 870 | |
| 871 | An implementation of the LMTP protocol (:rfc:`2033`) was also added to |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 872 | the module. LMTP is used in place of SMTP when transferring e-mail |
| 873 | between agents that don't manage a mail queue. |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 874 | |
| 875 | (SMTP over SSL contributed by Monty Taylor; timeout parameter |
| 876 | added by Facundo Batista; LMTP implemented by Leif |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 877 | Hedstrom.) |
| 878 | |
| 879 | .. % Patch #957003 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 880 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 881 | * A new variable in the :mod:`sys` module, |
| 882 | :attr:`float_info`, is a dictionary |
| 883 | containing information about the platform's floating-point support |
| 884 | derived from the :file:`float.h` file. Key/value pairs |
| 885 | in this dictionary include |
| 886 | ``"mant_dig"`` (number of digits in the mantissa), ``"epsilon"`` |
| 887 | (smallest difference between 1.0 and the next largest value |
| 888 | representable), and several others. (Contributed by Christian Heimes.) |
| 889 | |
| 890 | .. % Patch 1534 |
| 891 | |
Andrew M. Kuchling | de37a8c | 2007-09-18 01:36:16 +0000 | [diff] [blame] | 892 | * The :mod:`tarfile` module now supports POSIX.1-2001 (pax) and |
| 893 | POSIX.1-1988 (ustar) format tarfiles, in addition to the GNU tar |
| 894 | format that was already supported. The default format |
| 895 | is GNU tar; specify the ``format`` parameter to open a file |
| 896 | using a different format:: |
| 897 | |
| 898 | tar = tarfile.open("output.tar", "w", format=tarfile.PAX_FORMAT) |
| 899 | |
| 900 | The new ``errors`` parameter lets you specify an error handling |
| 901 | scheme for character conversions: the three standard ways Python can |
| 902 | handle errors ``'strict'``, ``'ignore'``, ``'replace'`` , or the |
| 903 | special value ``'utf-8'``, which replaces bad characters with their |
| 904 | UTF-8 representation. Character conversions occur because the PAX |
| 905 | format supports Unicode filenames, defaulting to UTF-8 encoding. |
| 906 | |
| 907 | The :meth:`TarFile.add` method now accepts a ``exclude`` argument that's |
| 908 | a function that can be used to exclude certain filenames from |
| 909 | an archive. |
| 910 | The function must take a filename and return true if the file |
| 911 | should be excluded or false if it should be archived. |
| 912 | The function is applied to both the name initially passed to :meth:`add` |
| 913 | and to the names of files in recursively-added directories. |
| 914 | |
| 915 | (All changes contributed by Lars Gustäbel). |
| 916 | |
| 917 | * An optional ``timeout`` parameter was added to the |
| 918 | :class:`telnetlib.Telnet` class constructor, specifying a timeout |
| 919 | measured in seconds. (Added by Facundo Batista.) |
| 920 | |
| 921 | * The :class:`tempfile.NamedTemporaryFile` class usually deletes |
| 922 | the temporary file it created when the file is closed. This |
| 923 | behaviour can now be changed by passing ``delete=False`` to the |
| 924 | constructor. (Contributed by Damien Miller.) |
| 925 | |
| 926 | .. % Patch #1537850 |
| 927 | |
| 928 | * The :mod:`test.test_support` module now contains a |
| 929 | :func:`EnvironmentVarGuard` |
| 930 | context manager that supports temporarily changing environment variables and |
| 931 | automatically restores them to their old values. |
| 932 | |
| 933 | Another context manager, :class:`TransientResource`, can surround calls |
| 934 | to resources that may or may not be available; it will catch and |
| 935 | ignore a specified list of exceptions. For example, |
| 936 | a network test may ignore certain failures when connecting to an |
| 937 | external web site:: |
| 938 | |
| 939 | with test_support.TransientResource(IOError, errno=errno.ETIMEDOUT): |
| 940 | f = urllib.urlopen('https://sf.net') |
| 941 | ... |
| 942 | |
| 943 | (Contributed by Brett Cannon.) |
| 944 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 945 | * The :mod:`textwrap` module can now preserve existing whitespace |
| 946 | at the beginnings and ends of the newly-created lines |
| 947 | by specifying ``drop_whitespace=False`` |
| 948 | as an argument:: |
| 949 | |
| 950 | >>> S = """This sentence has a bunch of extra whitespace.""" |
| 951 | >>> print textwrap.fill(S, width=15) |
| 952 | This sentence |
| 953 | has a bunch |
| 954 | of extra |
| 955 | whitespace. |
| 956 | >>> print textwrap.fill(S, drop_whitespace=False, width=15) |
| 957 | This sentence |
| 958 | has a bunch |
| 959 | of extra |
| 960 | whitespace. |
| 961 | >>> |
| 962 | |
| 963 | .. % Patch #1581073 |
| 964 | |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 965 | * The :mod:`timeit` module now accepts callables as well as strings |
| 966 | for the statement being timed and for the setup code. |
| 967 | Two convenience functions were added for creating |
| 968 | :class:`Timer` instances: |
| 969 | ``repeat(stmt, setup, time, repeat, number)`` and |
| 970 | ``timeit(stmt, setup, time, number)`` create an instance and call |
| 971 | the corresponding method. (Contributed by Erik Demaine.) |
| 972 | |
| 973 | .. % Patch #1533909 |
| 974 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 975 | * An optional ``timeout`` parameter was added to the |
| 976 | :func:`urllib.urlopen` function and the |
| 977 | :class:`urllib.ftpwrapper` class constructor, as well as the |
| 978 | :func:`urllib2.urlopen` function. The parameter specifies a timeout |
| 979 | measured in seconds. For example:: |
| 980 | |
| 981 | >>> u = urllib2.urlopen("http://slow.example.com", timeout=3) |
| 982 | Traceback (most recent call last): |
| 983 | ... |
| 984 | urllib2.URLError: <urlopen error timed out> |
| 985 | >>> |
| 986 | |
| 987 | (Added by Facundo Batista.) |
| 988 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 989 | * The XML-RPC classes :class:`SimpleXMLRPCServer` and :class:`DocXMLRPCServer` |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 990 | classes can now be prevented from immediately opening and binding to |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 991 | their socket by passing True as the ``bind_and_activate`` |
| 992 | constructor parameter. This can be used to modify the instance's |
| 993 | :attr:`allow_reuse_address` attribute before calling the |
| 994 | :meth:`server_bind` and :meth:`server_activate` methods to |
| 995 | open the socket and begin listening for connections. |
| 996 | (Contributed by Peter Parente.) |
| 997 | |
| 998 | .. % Patch 1599845 |
| 999 | |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1000 | :class:`SimpleXMLRPCServer` also has a :attr:`_send_traceback_header` |
| 1001 | attribute; if true, the exception and formatted traceback are returned |
| 1002 | as HTTP headers "X-Exception" and "X-Traceback". This feature is |
| 1003 | for debugging purposes only and should not be used on production servers |
| 1004 | because the tracebacks could possibly reveal passwords or other sensitive |
| 1005 | information. (Contributed by Alan McIntyre as part of his |
| 1006 | project for Google's Summer of Code 2007.) |
| 1007 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1008 | .. % ====================================================================== |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1009 | .. % whole new modules get described in subsections here |
| 1010 | |
| 1011 | Improved SSL Support |
Andrew M. Kuchling | 27a4498 | 2007-10-20 19:39:35 +0000 | [diff] [blame] | 1012 | -------------------------------------------------- |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1013 | |
| 1014 | Bill Janssen made extensive improvements to Python 2.6's support for |
| 1015 | SSL. |
| 1016 | |
| 1017 | XXX use ssl.sslsocket - subclass of socket.socket. |
| 1018 | |
| 1019 | XXX Can specify if certificate is required, and obtain certificate info |
| 1020 | by calling getpeercert method. |
| 1021 | |
| 1022 | XXX sslwrap() behaves like socket.ssl |
| 1023 | |
| 1024 | XXX Certain features require the OpenSSL package to be installed, notably |
| 1025 | the 'openssl' binary. |
| 1026 | |
| 1027 | .. seealso:: |
| 1028 | |
| 1029 | SSL module documentation. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1030 | |
| 1031 | .. % ====================================================================== |
| 1032 | |
| 1033 | |
| 1034 | Build and C API Changes |
| 1035 | ======================= |
| 1036 | |
| 1037 | Changes to Python's build process and to the C API include: |
| 1038 | |
Andrew M. Kuchling | f7b462f | 2007-11-23 13:37:39 +0000 | [diff] [blame] | 1039 | * Python 2.6 can be built with Microsoft Visual Studio 2008. |
| 1040 | See the :file:`PCbuild9` directory for the build files. |
| 1041 | (Implemented by Christian Heimes.) |
| 1042 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1043 | * The BerkeleyDB module now has a C API object, available as |
| 1044 | ``bsddb.db.api``. This object can be used by other C extensions |
| 1045 | that wish to use the :mod:`bsddb` module for their own purposes. |
| 1046 | (Contributed by Duncan Grisby.) |
| 1047 | |
| 1048 | .. % Patch 1551895 |
| 1049 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1050 | * Several functions return information about the platform's |
| 1051 | floating-point support. :cfunc:`PyFloat_GetMax` returns |
| 1052 | the maximum representable floating point value, |
| 1053 | and :cfunc:`PyFloat_GetMin` returns the minimum |
| 1054 | positive value. :cfunc:`PyFloat_GetInfo` returns a dictionary |
| 1055 | containing more information from the :file:`float.h` file, such as |
| 1056 | ``"mant_dig"`` (number of digits in the mantissa), ``"epsilon"`` |
| 1057 | (smallest difference between 1.0 and the next largest value |
| 1058 | representable), and several others. |
| 1059 | |
| 1060 | .. % Issue 1534 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1061 | |
| 1062 | .. % ====================================================================== |
| 1063 | |
| 1064 | |
| 1065 | Port-Specific Changes |
| 1066 | --------------------- |
| 1067 | |
| 1068 | Platform-specific changes go here. |
| 1069 | |
| 1070 | .. % ====================================================================== |
| 1071 | |
| 1072 | |
| 1073 | .. _section-other: |
| 1074 | |
| 1075 | Other Changes and Fixes |
| 1076 | ======================= |
| 1077 | |
| 1078 | As usual, there were a bunch of other improvements and bugfixes scattered |
| 1079 | throughout the source tree. A search through the change logs finds there were |
| 1080 | XXX patches applied and YYY bugs fixed between Python 2.5 and 2.6. Both figures |
| 1081 | are likely to be underestimates. |
| 1082 | |
| 1083 | Some of the more notable changes are: |
| 1084 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1085 | * Details will go here. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1086 | |
| 1087 | .. % ====================================================================== |
| 1088 | |
| 1089 | |
| 1090 | Porting to Python 2.6 |
| 1091 | ===================== |
| 1092 | |
| 1093 | This section lists previously described changes that may require changes to your |
| 1094 | code: |
| 1095 | |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1096 | * The :mod:`socket` module exception :exc:`socket.error` now inherits |
| 1097 | from :exc:`IOError`. Previously it wasn't a subclass of |
| 1098 | :exc:`StandardError` but now it is, through :exc:`IOError`. |
| 1099 | (Implemented by Gregory P. Smith.) |
| 1100 | |
| 1101 | .. % http://bugs.python.org/issue1706815 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1102 | |
| 1103 | .. % ====================================================================== |
| 1104 | |
| 1105 | |
| 1106 | .. _acks: |
| 1107 | |
| 1108 | Acknowledgements |
| 1109 | ================ |
| 1110 | |
| 1111 | The author would like to thank the following people for offering suggestions, |
| 1112 | corrections and assistance with various drafts of this article: . |
| 1113 | |