Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | **************************** |
| 2 | What's New in Python 2.6 |
| 3 | **************************** |
| 4 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 5 | .. XXX mention switch to Roundup for bug tracking |
Andrew M. Kuchling | 0c3f168 | 2008-01-26 13:50:51 +0000 | [diff] [blame] | 6 | .. XXX add trademark info for Apple, Microsoft. |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 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 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 63 | .. Compare with previous release in 2 - 3 sentences here. |
| 64 | add hyperlink when the documentation becomes available online. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 65 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 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? |
| 70 | .. ======================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 159 | :ref:`documenting-index`: 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 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 410 | .. ====================================================================== |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 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 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 428 | .. ====================================================================== |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 429 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 430 | .. :: |
| 431 | |
| 432 | .. _pep-0370: |
| 433 | |
| 434 | PEP 370: XXX |
| 435 | ===================================================== |
| 436 | |
| 437 | When you run Python, the module search page ``sys.modules`` usually |
| 438 | includes a directory whose path ends in ``"site-packages"``. This |
| 439 | directory is intended to hold locally-installed packages available to |
| 440 | all users on a machine or using a particular site installation. |
| 441 | |
| 442 | Python 2.6 introduces a convention for user-specific site directories. |
| 443 | |
| 444 | .. seealso:: |
| 445 | |
| 446 | :pep:`370` - XXX |
| 447 | |
| 448 | PEP written by XXX; implemented by Christian Heimes. |
| 449 | |
| 450 | |
| 451 | .. ====================================================================== |
| 452 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 453 | .. _pep-3110: |
| 454 | |
| 455 | PEP 3110: Exception-Handling Changes |
| 456 | ===================================================== |
| 457 | |
| 458 | One error that Python programmers occasionally make |
| 459 | is the following:: |
| 460 | |
| 461 | try: |
| 462 | ... |
| 463 | except TypeError, ValueError: |
| 464 | ... |
| 465 | |
| 466 | The author is probably trying to catch both |
| 467 | :exc:`TypeError` and :exc:`ValueError` exceptions, but this code |
| 468 | actually does something different: it will catch |
| 469 | :exc:`TypeError` and bind the resulting exception object |
| 470 | to the local name ``"ValueError"``. The correct code |
| 471 | would have specified a tuple:: |
| 472 | |
| 473 | try: |
| 474 | ... |
| 475 | except (TypeError, ValueError): |
| 476 | ... |
| 477 | |
| 478 | This error is possible because the use of the comma here is ambiguous: |
| 479 | does it indicate two different nodes in the parse tree, or a single |
| 480 | node that's a tuple. |
| 481 | |
| 482 | Python 3.0 changes the syntax to make this unambiguous by replacing |
| 483 | the comma with the word "as". To catch an exception and store the |
| 484 | exception object in the variable ``exc``, you must write:: |
| 485 | |
| 486 | try: |
| 487 | ... |
| 488 | except TypeError as exc: |
| 489 | ... |
| 490 | |
| 491 | Python 3.0 will only support the use of "as", and therefore interprets |
| 492 | the first example as catching two different exceptions. Python 2.6 |
| 493 | supports both the comma and "as", so existing code will continue to |
| 494 | work. |
| 495 | |
| 496 | .. seealso:: |
| 497 | |
| 498 | :pep:`3110` - Catching Exceptions in Python 3000 |
| 499 | PEP written and implemented by Collin Winter. |
| 500 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 501 | .. ====================================================================== |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 502 | |
| 503 | .. _pep-3119: |
| 504 | |
| 505 | PEP 3119: Abstract Base Classes |
| 506 | ===================================================== |
| 507 | |
| 508 | XXX |
| 509 | |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 510 | How to identify a file object? |
| 511 | |
| 512 | ABCs are a collection of classes describing various interfaces. |
| 513 | Classes can derive from an ABC to indicate they support that ABC's |
| 514 | interface. Concrete classes should obey the semantics specified by |
| 515 | an ABC, but Python can't check this; it's up to the implementor. |
| 516 | |
| 517 | A metaclass lets you declare that an existing class or type |
| 518 | derives from a particular ABC. You can even |
| 519 | |
| 520 | class AppendableSequence: |
| 521 | __metaclass__ = ABCMeta |
| 522 | |
| 523 | AppendableSequence.register(list) |
| 524 | assert issubclass(list, AppendableSequence) |
| 525 | assert isinstance([], AppendableSequence) |
| 526 | |
| 527 | @abstractmethod decorator -- you can't instantiate classes w/ |
| 528 | an abstract method. |
| 529 | |
Andrew M. Kuchling | 73835bd | 2008-01-04 18:24:41 +0000 | [diff] [blame] | 530 | :: |
| 531 | |
| 532 | @abstractproperty decorator |
| 533 | @abstractproperty |
| 534 | def readonly(self): |
| 535 | return self._x |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 536 | |
| 537 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 538 | .. seealso:: |
| 539 | |
| 540 | :pep:`3119` - Introducing Abstract Base Classes |
| 541 | PEP written by Guido van Rossum and Talin. |
| 542 | Implemented by XXX. |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 543 | Backported to 2.6 by Benjamin Aranguren, with Alex Martelli. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 544 | |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 545 | .. ====================================================================== |
| 546 | |
| 547 | .. _pep-3141: |
| 548 | |
| 549 | PEP 3141: A Type Hierarchy for Numbers |
| 550 | ===================================================== |
| 551 | |
| 552 | In Python 3.0, several abstract base classes for numeric types, |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 553 | inspired by Scheme's numeric tower, are being added. |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 554 | This change was backported to 2.6 as the :mod:`numbers` module. |
| 555 | |
| 556 | The most general ABC is :class:`Number`. It defines no operations at |
| 557 | all, and only exists to allow checking if an object is a number by |
| 558 | doing ``isinstance(obj, Number)``. |
| 559 | |
| 560 | Numbers are further divided into :class:`Exact` and :class:`Inexact`. |
| 561 | Exact numbers can represent values precisely and operations never |
| 562 | round off the results or introduce tiny errors that may break the |
| 563 | communtativity and associativity properties; inexact numbers may |
| 564 | perform such rounding or introduce small errors. Integers, long |
| 565 | integers, and rational numbers are exact, while floating-point |
| 566 | and complex numbers are inexact. |
| 567 | |
| 568 | :class:`Complex` is a subclass of :class:`Number`. Complex numbers |
| 569 | can undergo the basic operations of addition, subtraction, |
| 570 | multiplication, division, and exponentiation, and you can retrieve the |
| 571 | real and imaginary parts and obtain a number's conjugate. Python's built-in |
| 572 | complex type is an implementation of :class:`Complex`. |
| 573 | |
| 574 | :class:`Real` further derives from :class:`Complex`, and adds |
| 575 | operations that only work on real numbers: :func:`floor`, :func:`trunc`, |
| 576 | rounding, taking the remainder mod N, floor division, |
| 577 | and comparisons. |
| 578 | |
| 579 | :class:`Rational` numbers derive from :class:`Real`, have |
| 580 | :attr:`numerator` and :attr:`denominator` properties, and can be |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 581 | converted to floats. Python 2.6 adds a simple rational-number class, |
| 582 | :class:`Fraction`, in the :mod:`fractions` module. |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 583 | |
| 584 | :class:`Integral` numbers derive from :class:`Rational`, and |
| 585 | can be shifted left and right with ``<<`` and ``>>``, |
| 586 | combined using bitwise operations such as ``&`` and ``|``, |
| 587 | and can be used as array indexes and slice boundaries. |
| 588 | |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 589 | In Python 3.0, the PEP slightly redefines the existing built-ins |
| 590 | :func:`math.floor`, :func:`math.ceil`, :func:`round`, and adds a new |
| 591 | one, :func:`trunc`, that's been backported to Python 2.6. |
| 592 | :func:`trunc` rounds toward zero, returning the closest |
| 593 | :class:`Integral` that's between the function's argument and zero. |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 594 | |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 595 | .. seealso:: |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 596 | |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 597 | XXX link: Discusses Scheme's numeric tower. |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 598 | |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 599 | |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 600 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 601 | The Fraction Module |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 602 | -------------------------------------------------- |
| 603 | |
| 604 | To fill out the hierarchy of numeric types, a rational-number class |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 605 | has been added as the :mod:`fractions` module. Rational numbers are |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 606 | represented as a fraction; rational numbers can exactly represent |
| 607 | numbers such as two-thirds that floating-point numbers can only |
| 608 | approximate. |
| 609 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 610 | The :class:`Fraction` constructor takes two :class:`Integral` values |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 611 | that will be the numerator and denominator of the resulting fraction. :: |
| 612 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 613 | >>> from fractions import Fraction |
| 614 | >>> a = Fraction(2, 3) |
| 615 | >>> b = Fraction(2, 5) |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 616 | >>> float(a), float(b) |
| 617 | (0.66666666666666663, 0.40000000000000002) |
| 618 | >>> a+b |
Mark Dickinson | cd873fc | 2008-02-11 03:11:55 +0000 | [diff] [blame^] | 619 | Fraction(16, 15) |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 620 | >>> a/b |
Mark Dickinson | cd873fc | 2008-02-11 03:11:55 +0000 | [diff] [blame^] | 621 | Fraction(5, 3) |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 622 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 623 | The :mod:`fractions` module is based upon an implementation by Sjoerd |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 624 | Mullender that was in Python's :file:`Demo/classes/` directory for a |
| 625 | long time. This implementation was significantly updated by Jeffrey |
| 626 | Yaskin. |
| 627 | |
| 628 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 629 | Other Language Changes |
| 630 | ====================== |
| 631 | |
| 632 | Here are all of the changes that Python 2.6 makes to the core Python language. |
| 633 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 634 | * When calling a function using the ``**`` syntax to provide keyword |
| 635 | arguments, you are no longer required to use a Python dictionary; |
| 636 | any mapping will now work:: |
| 637 | |
| 638 | >>> def f(**kw): |
| 639 | ... print sorted(kw) |
| 640 | ... |
| 641 | >>> ud=UserDict.UserDict() |
| 642 | >>> ud['a'] = 1 |
| 643 | >>> ud['b'] = 'string' |
| 644 | >>> f(**ud) |
| 645 | ['a', 'b'] |
| 646 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 647 | .. Patch 1686487 |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 648 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 649 | * The built-in types now have improved support for extended slicing syntax, |
| 650 | where various combinations of ``(start, stop, step)`` are supplied. |
| 651 | Previously, the support was partial and certain corner cases wouldn't work. |
| 652 | (Implemented by Thomas Wouters.) |
| 653 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 654 | .. Revision 57619 |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 655 | |
Christian Heimes | ff6cc6b | 2008-01-17 23:01:44 +0000 | [diff] [blame] | 656 | * Properties now have three attributes, :attr:`getter`, |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 657 | :attr:`setter` and :attr:`deleter`, that are useful shortcuts for |
Christian Heimes | ff6cc6b | 2008-01-17 23:01:44 +0000 | [diff] [blame] | 658 | adding or modifying a getter, setter or deleter function to an |
| 659 | existing property. You would use them like this:: |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 660 | |
| 661 | class C(object): |
| 662 | @property |
| 663 | def x(self): |
| 664 | return self._x |
| 665 | |
| 666 | @x.setter |
| 667 | def x(self, value): |
| 668 | self._x = value |
| 669 | |
| 670 | @x.deleter |
| 671 | def x(self): |
| 672 | del self._x |
| 673 | |
Christian Heimes | ff6cc6b | 2008-01-17 23:01:44 +0000 | [diff] [blame] | 674 | class D(C): |
| 675 | @C.x.getter |
| 676 | def x(self): |
| 677 | return self._x * 2 |
| 678 | |
| 679 | @x.setter |
| 680 | def x(self, value): |
| 681 | self._x = value / 2 |
| 682 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 683 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 684 | * C functions and methods that use |
| 685 | :cfunc:`PyComplex_AsCComplex` will now accept arguments that |
| 686 | have a :meth:`__complex__` method. In particular, the functions in the |
| 687 | :mod:`cmath` module will now accept objects with this method. |
| 688 | This is a backport of a Python 3.0 change. |
| 689 | (Contributed by Mark Dickinson.) |
| 690 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 691 | .. Patch #1675423 |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 692 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 693 | A numerical nicety: when creating a complex number from two floats |
| 694 | on systems that support signed zeros (-0 and +0), the |
| 695 | :func:`complex()` constructor will now preserve the sign |
| 696 | of the zero. |
| 697 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 698 | .. Patch 1507 |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 699 | |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 700 | * More floating-point features were also added. The :func:`float` function |
| 701 | will now turn the strings ``+nan`` and ``-nan`` into the corresponding |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 702 | IEEE 754 Not A Number values, and ``+inf`` and ``-inf`` into |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 703 | positive or negative infinity. This works on any platform with |
Christian Heimes | d0d7d87 | 2008-01-04 02:03:25 +0000 | [diff] [blame] | 704 | IEEE 754 semantics. (Contributed by Christian Heimes.) |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 705 | |
| 706 | .. Patch 1635. |
| 707 | |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 708 | Other functions in the :mod:`math` module, :func:`isinf` and |
| 709 | :func:`isnan`, return true if their floating-point argument is |
| 710 | infinite or Not A Number. |
| 711 | .. Patch 1640 |
| 712 | The ``math.copysign(x, y)`` function |
| 713 | copies the sign bit of an IEEE 754 number, returning the absolute |
| 714 | value of *x* combined with the sign bit of *y*. For example, |
| 715 | ``math.copysign(1, -0.0)`` returns -1.0. (Contributed by Christian |
| 716 | Heimes.) |
| 717 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 718 | * Changes to the :class:`Exception` interface |
| 719 | as dictated by :pep:`352` continue to be made. For 2.6, |
| 720 | the :attr:`message` attribute is being deprecated in favor of the |
| 721 | :attr:`args` attribute. |
| 722 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 723 | * The :exc:`GeneratorExit` exception now subclasses |
| 724 | :exc:`BaseException` instead of :exc:`Exception`. This means |
| 725 | that an exception handler that does ``except Exception:`` |
| 726 | will not inadvertently catch :exc:`GeneratorExit`. |
| 727 | (Contributed by Chad Austin.) |
| 728 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 729 | .. Patch #1537 |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 730 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 731 | * The :func:`compile` built-in function now accepts keyword arguments |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 732 | as well as positional parameters. (Contributed by Thomas Wouters.) |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 733 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 734 | .. Patch 1444529 |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 735 | |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 736 | * The :func:`complex` constructor now accepts strings containing |
| 737 | parenthesized complex numbers, letting ``complex(repr(cmplx))`` |
| 738 | will now round-trip values. For example, ``complex('(3+4j)')`` |
| 739 | now returns the value (3+4j). |
| 740 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 741 | .. Patch 1491866 |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 742 | |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 743 | * The string :meth:`translate` method now accepts ``None`` as the |
| 744 | translation table parameter, which is treated as the identity |
| 745 | transformation. This makes it easier to carry out operations |
| 746 | that only delete characters. (Contributed by Bengt Richter.) |
| 747 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 748 | .. Patch 1193128 |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 749 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 750 | * The built-in :func:`dir` function now checks for a :meth:`__dir__` |
| 751 | method on the objects it receives. This method must return a list |
| 752 | of strings containing the names of valid attributes for the object, |
| 753 | and lets the object control the value that :func:`dir` produces. |
| 754 | Objects that have :meth:`__getattr__` or :meth:`__getattribute__` |
Facundo Batista | bd5b623 | 2007-12-03 19:49:54 +0000 | [diff] [blame] | 755 | methods can use this to advertise pseudo-attributes they will honor. |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 756 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 757 | .. Patch 1591665 |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 758 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 759 | * An obscure change: when you use the the :func:`locals` function inside a |
| 760 | :keyword:`class` statement, the resulting dictionary no longer returns free |
| 761 | variables. (Free variables, in this case, are variables referred to in the |
| 762 | :keyword:`class` statement that aren't attributes of the class.) |
| 763 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 764 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 765 | |
| 766 | |
| 767 | Optimizations |
| 768 | ------------- |
| 769 | |
Georg Brandl | af30b28 | 2008-01-15 06:55:56 +0000 | [diff] [blame] | 770 | * Type objects now have a cache of methods that can reduce |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 771 | the amount of work required to find the correct method implementation |
Andrew M. Kuchling | a01ed03 | 2008-01-15 01:55:32 +0000 | [diff] [blame] | 772 | for a particular class; once cached, the interpreter doesn't need to |
| 773 | traverse base classes to figure out the right method to call. |
| 774 | The cache is cleared if a base class or the class itself is modified, |
| 775 | so the cache should remain correct even in the face of Python's dynamic |
| 776 | nature. |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 777 | (Original optimization implemented by Armin Rigo, updated for |
| 778 | Python 2.6 by Kevin Jacobs.) |
| 779 | |
| 780 | .. % Patch 1700288 |
| 781 | |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 782 | * All of the functions in the :mod:`struct` module have been rewritten in |
| 783 | C, thanks to work at the Need For Speed sprint. |
| 784 | (Contributed by Raymond Hettinger.) |
| 785 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 786 | * Internally, a bit is now set in type objects to indicate some of the standard |
| 787 | built-in types. This speeds up checking if an object is a subclass of one of |
| 788 | these types. (Contributed by Neal Norwitz.) |
| 789 | |
| 790 | The net result of the 2.6 optimizations is that Python 2.6 runs the pystone |
| 791 | benchmark around XX% faster than Python 2.5. |
| 792 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 793 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 794 | |
| 795 | |
| 796 | New, Improved, and Deprecated Modules |
| 797 | ===================================== |
| 798 | |
| 799 | As usual, Python's standard library received a number of enhancements and bug |
| 800 | fixes. Here's a partial list of the most notable changes, sorted alphabetically |
| 801 | by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more |
| 802 | complete list of changes, or look through the CVS logs for all the details. |
| 803 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 804 | * The :mod:`bsddb.dbshelve` module now uses the highest pickling protocol |
| 805 | available, instead of restricting itself to protocol 1. |
| 806 | (Contributed by W. Barnes.) |
| 807 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 808 | .. Patch 1551443 |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 809 | |
Andrew M. Kuchling | 6d57c82 | 2007-10-23 20:55:47 +0000 | [diff] [blame] | 810 | * A new data type in the :mod:`collections` module: :class:`namedtuple(typename, |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 811 | fieldnames)` is a factory function that creates subclasses of the standard tuple |
| 812 | whose fields are accessible by name as well as index. For example:: |
| 813 | |
Andrew M. Kuchling | 6d57c82 | 2007-10-23 20:55:47 +0000 | [diff] [blame] | 814 | >>> var_type = collections.namedtuple('variable', |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 815 | ... 'id name type size') |
| 816 | # Names are separated by spaces or commas. |
| 817 | # 'id, name, type, size' would also work. |
Raymond Hettinger | 366523c | 2007-12-14 18:12:21 +0000 | [diff] [blame] | 818 | >>> var_type._fields |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 819 | ('id', 'name', 'type', 'size') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 820 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 821 | >>> var = var_type(1, 'frequency', 'int', 4) |
| 822 | >>> print var[0], var.id # Equivalent |
| 823 | 1 1 |
| 824 | >>> print var[2], var.type # Equivalent |
| 825 | int int |
Raymond Hettinger | 366523c | 2007-12-14 18:12:21 +0000 | [diff] [blame] | 826 | >>> var._asdict() |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 827 | {'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'} |
Raymond Hettinger | 366523c | 2007-12-14 18:12:21 +0000 | [diff] [blame] | 828 | >>> v2 = var._replace('name', 'amplitude') |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 829 | >>> v2 |
| 830 | variable(id=1, name='amplitude', type='int', size=4) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 831 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 832 | Where the new :class:`namedtuple` type proved suitable, the standard |
| 833 | library has been modified to return them. For example, |
| 834 | the :meth:`Decimal.as_tuple` method now returns a named tuple with |
| 835 | :attr:`sign`, :attr:`digits`, and :attr:`exponent` fields. |
| 836 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 837 | (Contributed by Raymond Hettinger.) |
| 838 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 839 | * Another change to the :mod:`collections` module is that the |
Georg Brandl | e7d118a | 2007-12-08 11:05:05 +0000 | [diff] [blame] | 840 | :class:`deque` type now supports an optional *maxlen* parameter; |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 841 | if supplied, the deque's size will be restricted to no more |
Georg Brandl | e7d118a | 2007-12-08 11:05:05 +0000 | [diff] [blame] | 842 | than *maxlen* items. Adding more items to a full deque causes |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 843 | old items to be discarded. |
| 844 | |
| 845 | :: |
| 846 | |
| 847 | >>> from collections import deque |
| 848 | >>> dq=deque(maxlen=3) |
| 849 | >>> dq |
| 850 | deque([], maxlen=3) |
| 851 | >>> dq.append(1) ; dq.append(2) ; dq.append(3) |
| 852 | >>> dq |
| 853 | deque([1, 2, 3], maxlen=3) |
| 854 | >>> dq.append(4) |
| 855 | >>> dq |
| 856 | deque([2, 3, 4], maxlen=3) |
| 857 | |
| 858 | (Contributed by Raymond Hettinger.) |
| 859 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 860 | * The :mod:`ctypes` module now supports a :class:`c_bool` datatype |
| 861 | that represents the C99 ``bool`` type. (Contributed by David Remahl.) |
| 862 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 863 | .. Patch 1649190 |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 864 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 865 | The :mod:`ctypes` string, buffer and array types also have improved |
| 866 | support for extended slicing syntax, |
| 867 | where various combinations of ``(start, stop, step)`` are supplied. |
| 868 | (Implemented by Thomas Wouters.) |
| 869 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 870 | .. Revision 57769 |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 871 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 872 | * 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] | 873 | the display characters for a certain number of characters on a single line. |
Andrew M. Kuchling | 4a2762d | 2008-01-20 00:00:38 +0000 | [diff] [blame] | 874 | (Contributed by Fabian Kreutz.) |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 875 | :: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 876 | |
| 877 | # Boldface text starting at y=0,x=21 |
| 878 | # and affecting the rest of the line. |
| 879 | stdscr.chgat(0,21, curses.A_BOLD) |
| 880 | |
Andrew M. Kuchling | 4a2762d | 2008-01-20 00:00:38 +0000 | [diff] [blame] | 881 | The :class:`Textbox` class in the :mod:`curses.textpad` module |
| 882 | now supports editing in insert mode as well as overwrite mode. |
| 883 | Insert mode is enabled by supplying a true value for the *insert_mode* |
| 884 | parameter when creating the :class:`Textbox` instance. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 885 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 886 | * The :mod:`decimal` module was updated to version 1.66 of |
| 887 | `the General Decimal Specification <http://www2.hursley.ibm.com/decimal/decarith.html>`__. New features |
| 888 | include some methods for some basic mathematical functions such as |
| 889 | :meth:`exp` and :meth:`log10`:: |
| 890 | |
| 891 | >>> Decimal(1).exp() |
| 892 | Decimal("2.718281828459045235360287471") |
| 893 | >>> Decimal("2.7182818").ln() |
| 894 | Decimal("0.9999999895305022877376682436") |
| 895 | >>> Decimal(1000).log10() |
| 896 | Decimal("3") |
| 897 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 898 | The :meth:`as_tuple` method of :class:`Decimal` objects now returns a |
| 899 | named tuple with :attr:`sign`, :attr:`digits`, and :attr:`exponent` fields. |
| 900 | |
| 901 | (Implemented by Facundo Batista and Mark Dickinson. Named tuple |
| 902 | support added by Raymond Hettinger.) |
| 903 | |
| 904 | * The :mod:`difflib` module's :class:`SequenceMatcher` class |
| 905 | now returns named tuples representing matches. |
| 906 | In addition to behaving like tuples, the returned values |
| 907 | also have :attr:`a`, :attr:`b`, and :attr:`size` attributes. |
| 908 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 909 | |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 910 | * An optional ``timeout`` parameter was added to the |
| 911 | :class:`ftplib.FTP` class constructor as well as the :meth:`connect` |
| 912 | method, specifying a timeout measured in seconds. (Added by Facundo |
Andrew M. Kuchling | 0c3f168 | 2008-01-26 13:50:51 +0000 | [diff] [blame] | 913 | Batista.) Also, the :class:`FTP` class's |
| 914 | :meth:`storbinary` and :meth:`storlines` |
| 915 | now take an optional *callback* parameter that will be called with |
| 916 | each block of data after the data has been sent. |
| 917 | (Contributed by Phil Schwartz.) |
| 918 | |
| 919 | .. Patch 1221598 |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 920 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 921 | * The :func:`reduce` built-in function is also available in the |
| 922 | :mod:`functools` module. In Python 3.0, the built-in is dropped and it's |
| 923 | only available from :mod:`functools`; currently there are no plans |
| 924 | to drop the built-in in the 2.x series. (Patched by |
| 925 | Christian Heimes.) |
| 926 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 927 | .. Patch 1739906 |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 928 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 929 | * The :func:`glob.glob` function can now return Unicode filenames if |
| 930 | a Unicode path was used and Unicode filenames are matched within the directory. |
| 931 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 932 | .. Patch #1001604 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 933 | |
| 934 | * The :mod:`gopherlib` module has been removed. |
| 935 | |
| 936 | * A new function in the :mod:`heapq` module: ``merge(iter1, iter2, ...)`` |
| 937 | takes any number of iterables that return data *in sorted order*, and returns |
| 938 | a new iterator that returns the contents of all the iterators, also in sorted |
| 939 | order. For example:: |
| 940 | |
| 941 | heapq.merge([1, 3, 5, 9], [2, 8, 16]) -> |
| 942 | [1, 2, 3, 5, 8, 9, 16] |
| 943 | |
| 944 | (Contributed by Raymond Hettinger.) |
| 945 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 946 | * An optional ``timeout`` parameter was added to the |
| 947 | :class:`httplib.HTTPConnection` and :class:`HTTPSConnection` |
| 948 | class constructors, specifying a timeout measured in seconds. |
| 949 | (Added by Facundo Batista.) |
| 950 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 951 | * Most of the :mod:`inspect` module's functions, such as |
| 952 | :func:`getmoduleinfo` and :func:`getargs`, now return named tuples. |
| 953 | In addition to behaving like tuples, the elements of the return value |
| 954 | can also be accessed as attributes. |
| 955 | (Contributed by Raymond Hettinger.) |
| 956 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 957 | * A new function in the :mod:`itertools` module: ``izip_longest(iter1, iter2, |
| 958 | ...[, fillvalue])`` makes tuples from each of the elements; if some of the |
| 959 | iterables are shorter than others, the missing values are set to *fillvalue*. |
| 960 | For example:: |
| 961 | |
| 962 | itertools.izip_longest([1,2,3], [1,2,3,4,5]) -> |
| 963 | [(1, 1), (2, 2), (3, 3), (None, 4), (None, 5)] |
| 964 | |
| 965 | (Contributed by Raymond Hettinger.) |
| 966 | |
| 967 | * The :mod:`macfs` module has been removed. This in turn required the |
| 968 | :func:`macostools.touched` function to be removed because it depended on the |
| 969 | :mod:`macfs` module. |
| 970 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 971 | .. Patch #1490190 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 972 | |
Andrew M. Kuchling | 2686f4d | 2008-01-19 19:14:05 +0000 | [diff] [blame] | 973 | * :class:`mmap` objects now have a :meth:`rfind` method that finds |
| 974 | a substring, beginning at the end of the string and searching |
| 975 | backwards. The :meth:`find` method |
| 976 | also gained a *end* parameter containing the index at which to stop |
| 977 | the forward search. |
| 978 | (Contributed by John Lenton.) |
| 979 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 980 | * The :mod:`new` module has been removed from Python 3.0. |
| 981 | Importing it therefore |
| 982 | triggers a warning message when Python is running in 3.0-warning |
| 983 | mode. |
| 984 | |
| 985 | * New functions in the :mod:`os` module include |
| 986 | ``fchmod(fd, mode)``, ``fchown(fd, uid, gid)``, |
| 987 | and ``lchmod(path, mode)``, on operating systems that support these |
| 988 | functions. :func:`fchmod` and :func:`fchown` let you change the mode |
| 989 | and ownership of an opened file, and :func:`lchmod` changes the mode |
| 990 | of a symlink. |
| 991 | |
| 992 | (Contributed by Georg Brandl and Christian Heimes.) |
| 993 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 994 | * The :func:`os.walk` function now has a ``followlinks`` parameter. If |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 995 | set to True, it will follow symlinks pointing to directories and |
| 996 | visit the directory's contents. For backward compatibility, the |
| 997 | parameter's default value is false. Note that the function can fall |
| 998 | into an infinite recursion if there's a symlink that points to a |
| 999 | parent directory. |
| 1000 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1001 | .. Patch 1273829 |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1002 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1003 | * The ``os.environ`` object's :meth:`clear` method will now unset the |
| 1004 | environment variables using :func:`os.unsetenv` in addition to clearing |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1005 | the object's keys. (Contributed by Martin Horcicka.) |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1006 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1007 | .. Patch #1181 |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1008 | |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 1009 | * In the :mod:`os.path` module, the :func:`splitext` function |
| 1010 | has been changed to not split on leading period characters. |
| 1011 | This produces better results when operating on Unix's dot-files. |
| 1012 | For example, ``os.path.splitext('.ipython')`` |
| 1013 | now returns ``('.ipython', '')`` instead of ``('', '.ipython')``. |
| 1014 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1015 | .. Bug #115886 |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 1016 | |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 1017 | A new function, :func:`relpath(path, start)` returns a relative path |
| 1018 | from the ``start`` path, if it's supplied, or from the current |
| 1019 | working directory to the destination ``path``. (Contributed by |
| 1020 | Richard Barran.) |
| 1021 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1022 | .. Patch 1339796 |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 1023 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1024 | On Windows, :func:`os.path.expandvars` will now expand environment variables |
| 1025 | in the form "%var%", and "~user" will be expanded into the |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1026 | user's home directory path. (Contributed by Josiah Carlson.) |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1027 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1028 | .. Patch 957650 |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1029 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1030 | * The Python debugger provided by the :mod:`pdb` module |
| 1031 | gained a new command: "run" restarts the Python program being debugged, |
| 1032 | and can optionally take new command-line arguments for the program. |
| 1033 | (Contributed by Rocky Bernstein.) |
| 1034 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1035 | .. Patch #1393667 |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1036 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1037 | * New functions in the :mod:`posix` module: :func:`chflags` and :func:`lchflags` |
| 1038 | are wrappers for the corresponding system calls (where they're available). |
| 1039 | Constants for the flag values are defined in the :mod:`stat` module; some |
| 1040 | possible values include :const:`UF_IMMUTABLE` to signal the file may not be |
| 1041 | changed and :const:`UF_APPEND` to indicate that data can only be appended to the |
| 1042 | file. (Contributed by M. Levinson.) |
| 1043 | |
Andrew M. Kuchling | e0a49b6 | 2008-01-08 14:30:55 +0000 | [diff] [blame] | 1044 | * The :mod:`pyexpat` module's :class:`Parser` objects now allow setting |
| 1045 | their :attr:`buffer_size` attribute to change the size of the buffer |
| 1046 | used to hold character data. |
| 1047 | (Contributed by Achim Gaedke.) |
| 1048 | |
| 1049 | .. Patch 1137 |
| 1050 | |
Andrew M. Kuchling | 0c3f168 | 2008-01-26 13:50:51 +0000 | [diff] [blame] | 1051 | * The :mod:`Queue` module now provides queue classes that retrieve entries |
| 1052 | in different orders. The :class:`PriorityQueue` class stores |
| 1053 | queued items in a heap and retrieves them in priority order, |
| 1054 | and :class:`LifoQueue` retrieves the most recently added entries first, |
| 1055 | meaning that it behaves like a stack. |
| 1056 | (Contributed by Raymond Hettinger.) |
| 1057 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1058 | * The :mod:`random` module's :class:`Random` objects can |
| 1059 | now be pickled on a 32-bit system and unpickled on a 64-bit |
| 1060 | system, and vice versa. Unfortunately, this change also means |
| 1061 | that Python 2.6's :class:`Random` objects can't be unpickled correctly |
| 1062 | on earlier versions of Python. |
| 1063 | (Contributed by Shawn Ligocki.) |
| 1064 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1065 | .. Issue 1727780 |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1066 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1067 | * Long regular expression searches carried out by the :mod:`re` |
| 1068 | module will now check for signals being delivered, so especially |
| 1069 | long searches can now be interrupted. |
| 1070 | (Contributed by Josh Hoyt and Ralf Schmitt.) |
| 1071 | |
| 1072 | .. % Patch 846388 |
| 1073 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1074 | * The :mod:`rgbimg` module has been removed. |
| 1075 | |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1076 | * The :mod:`sets` module has been deprecated; it's better to |
| 1077 | use the built-in :class:`set` and :class:`frozenset` types. |
| 1078 | |
Andrew M. Kuchling | 2d60cf7 | 2007-12-22 17:27:02 +0000 | [diff] [blame] | 1079 | * Integrating signal handling with GUI handling event loops |
| 1080 | like those used by Tkinter or GTk+ has long been a problem; most |
| 1081 | software ends up polling, waking up every fraction of a second. Thi |
| 1082 | The :mod:`signal` module can now make this more efficient. |
| 1083 | Calling ``signal.set_wakeup_fd(fd)`` sets a file descriptor |
| 1084 | to be used; when a signal is received, a byte is written to that |
| 1085 | file descriptor. There's also a C-level function, |
| 1086 | :cfunc:`PySignal_SetWakeupFd`, for setting the descriptor. |
| 1087 | |
| 1088 | Event loops will use this by opening a pipe to create two descriptors, |
| 1089 | one for reading and one for writing. The writeable descriptor |
| 1090 | will be passed to :func:`set_wakeup_fd`, and the readable descriptor |
| 1091 | will be added to the list of descriptors monitored by the event loop via |
| 1092 | :cfunc:`select` or :cfunc:`poll`. |
| 1093 | On receiving a signal, a byte will be written and the main event loop |
| 1094 | will be woken up, without the need to poll. |
| 1095 | |
| 1096 | Contributed by Adam Olsen. |
| 1097 | |
| 1098 | .. % Patch 1583 |
| 1099 | |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 1100 | * The :mod:`smtplib` module now supports SMTP over SSL thanks to the |
| 1101 | addition of the :class:`SMTP_SSL` class. This class supports an |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 1102 | interface identical to the existing :class:`SMTP` class. Both |
| 1103 | class constructors also have an optional ``timeout`` parameter |
| 1104 | that specifies a timeout for the initial connection attempt, measured in |
| 1105 | seconds. |
| 1106 | |
| 1107 | 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] | 1108 | the module. LMTP is used in place of SMTP when transferring e-mail |
| 1109 | between agents that don't manage a mail queue. |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 1110 | |
| 1111 | (SMTP over SSL contributed by Monty Taylor; timeout parameter |
| 1112 | added by Facundo Batista; LMTP implemented by Leif |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 1113 | Hedstrom.) |
| 1114 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1115 | .. Patch #957003 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1116 | |
Gregory P. Smith | 63bfc1d | 2008-01-17 07:43:20 +0000 | [diff] [blame] | 1117 | * In the :mod:`smtplib` module, SMTP.starttls() now complies with :rfc:`3207` |
| 1118 | and forgets any knowledge obtained from the server not obtained from |
| 1119 | the TLS negotiation itself. Patch contributed by Bill Fenner. |
| 1120 | |
| 1121 | .. Issue 829951 |
| 1122 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1123 | * The :mod:`socket` module now supports TIPC (http://tipc.sf.net), |
| 1124 | a high-performance non-IP-based protocol designed for use in clustered |
| 1125 | environments. TIPC addresses are 4- or 5-tuples. |
| 1126 | (Contributed by Alberto Bertogli.) |
| 1127 | |
| 1128 | .. Patch #1646 |
Andrew M. Kuchling | f60b641 | 2008-01-19 16:34:09 +0000 | [diff] [blame] | 1129 | |
| 1130 | * The base classes in the :mod:`SocketServer` module now support |
| 1131 | calling a :meth:`handle_timeout` method after a span of inactivity |
| 1132 | specified by the server's :attr:`timeout` attribute. (Contributed |
| 1133 | by Michael Pomraning.) |
| 1134 | |
| 1135 | .. Patch #742598 |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1136 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1137 | * A new variable in the :mod:`sys` module, |
Andrew M. Kuchling | 5d8b379 | 2008-01-14 14:48:43 +0000 | [diff] [blame] | 1138 | :attr:`float_info`, is an object |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1139 | containing information about the platform's floating-point support |
Andrew M. Kuchling | 5d8b379 | 2008-01-14 14:48:43 +0000 | [diff] [blame] | 1140 | derived from the :file:`float.h` file. Attributes of this object |
| 1141 | include |
| 1142 | :attr:`mant_dig` (number of digits in the mantissa), :attr:`epsilon` |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1143 | (smallest difference between 1.0 and the next largest value |
| 1144 | representable), and several others. (Contributed by Christian Heimes.) |
| 1145 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1146 | .. Patch 1534 |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1147 | |
Andrew M. Kuchling | 7b1e917 | 2008-01-15 14:38:05 +0000 | [diff] [blame] | 1148 | Another new variable, :attr:`dont_write_bytecode`, controls whether Python |
| 1149 | writes any :file:`.pyc` or :file:`.pyo` files on importing a module. |
| 1150 | If this variable is true, the compiled files are not written. The |
| 1151 | variable is initially set on start-up by supplying the :option:`-B` |
| 1152 | switch to the Python interpreter, or by setting the |
| 1153 | :envvar:`PYTHONDONTWRITEBYTECODE` environment variable before |
| 1154 | running the interpreter. Python code can subsequently |
| 1155 | change the value of this variable to control whether bytecode files |
| 1156 | are written or not. |
| 1157 | (Contributed by Neal Norwitz and Georg Brandl.) |
| 1158 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1159 | Information about the command-line arguments supplied to the Python |
| 1160 | interpreter are available as attributes of a ``sys.flags`` named |
| 1161 | tuple. For example, the :attr:`verbose` attribute is true if Python |
| 1162 | was executed in verbose mode, :attr:`debug` is true in debugging mode, etc. |
| 1163 | These attributes are all read-only. |
| 1164 | (Contributed by Christian Heimes.) |
| 1165 | |
Andrew M. Kuchling | de37a8c | 2007-09-18 01:36:16 +0000 | [diff] [blame] | 1166 | * The :mod:`tarfile` module now supports POSIX.1-2001 (pax) and |
| 1167 | POSIX.1-1988 (ustar) format tarfiles, in addition to the GNU tar |
| 1168 | format that was already supported. The default format |
| 1169 | is GNU tar; specify the ``format`` parameter to open a file |
| 1170 | using a different format:: |
| 1171 | |
| 1172 | tar = tarfile.open("output.tar", "w", format=tarfile.PAX_FORMAT) |
| 1173 | |
| 1174 | The new ``errors`` parameter lets you specify an error handling |
| 1175 | scheme for character conversions: the three standard ways Python can |
| 1176 | handle errors ``'strict'``, ``'ignore'``, ``'replace'`` , or the |
| 1177 | special value ``'utf-8'``, which replaces bad characters with their |
| 1178 | UTF-8 representation. Character conversions occur because the PAX |
| 1179 | format supports Unicode filenames, defaulting to UTF-8 encoding. |
| 1180 | |
| 1181 | The :meth:`TarFile.add` method now accepts a ``exclude`` argument that's |
| 1182 | a function that can be used to exclude certain filenames from |
| 1183 | an archive. |
| 1184 | The function must take a filename and return true if the file |
| 1185 | should be excluded or false if it should be archived. |
| 1186 | The function is applied to both the name initially passed to :meth:`add` |
| 1187 | and to the names of files in recursively-added directories. |
| 1188 | |
| 1189 | (All changes contributed by Lars Gustäbel). |
| 1190 | |
| 1191 | * An optional ``timeout`` parameter was added to the |
| 1192 | :class:`telnetlib.Telnet` class constructor, specifying a timeout |
| 1193 | measured in seconds. (Added by Facundo Batista.) |
| 1194 | |
| 1195 | * The :class:`tempfile.NamedTemporaryFile` class usually deletes |
| 1196 | the temporary file it created when the file is closed. This |
| 1197 | behaviour can now be changed by passing ``delete=False`` to the |
| 1198 | constructor. (Contributed by Damien Miller.) |
| 1199 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1200 | .. Patch #1537850 |
Andrew M. Kuchling | de37a8c | 2007-09-18 01:36:16 +0000 | [diff] [blame] | 1201 | |
| 1202 | * The :mod:`test.test_support` module now contains a |
| 1203 | :func:`EnvironmentVarGuard` |
| 1204 | context manager that supports temporarily changing environment variables and |
| 1205 | automatically restores them to their old values. |
| 1206 | |
| 1207 | Another context manager, :class:`TransientResource`, can surround calls |
| 1208 | to resources that may or may not be available; it will catch and |
| 1209 | ignore a specified list of exceptions. For example, |
| 1210 | a network test may ignore certain failures when connecting to an |
| 1211 | external web site:: |
| 1212 | |
| 1213 | with test_support.TransientResource(IOError, errno=errno.ETIMEDOUT): |
| 1214 | f = urllib.urlopen('https://sf.net') |
| 1215 | ... |
| 1216 | |
| 1217 | (Contributed by Brett Cannon.) |
| 1218 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1219 | * The :mod:`textwrap` module can now preserve existing whitespace |
| 1220 | at the beginnings and ends of the newly-created lines |
| 1221 | by specifying ``drop_whitespace=False`` |
| 1222 | as an argument:: |
| 1223 | |
| 1224 | >>> S = """This sentence has a bunch of extra whitespace.""" |
| 1225 | >>> print textwrap.fill(S, width=15) |
| 1226 | This sentence |
| 1227 | has a bunch |
| 1228 | of extra |
| 1229 | whitespace. |
| 1230 | >>> print textwrap.fill(S, drop_whitespace=False, width=15) |
| 1231 | This sentence |
| 1232 | has a bunch |
| 1233 | of extra |
| 1234 | whitespace. |
| 1235 | >>> |
| 1236 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1237 | .. Patch #1581073 |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1238 | |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 1239 | * The :mod:`timeit` module now accepts callables as well as strings |
| 1240 | for the statement being timed and for the setup code. |
| 1241 | Two convenience functions were added for creating |
| 1242 | :class:`Timer` instances: |
| 1243 | ``repeat(stmt, setup, time, repeat, number)`` and |
| 1244 | ``timeit(stmt, setup, time, number)`` create an instance and call |
| 1245 | the corresponding method. (Contributed by Erik Demaine.) |
| 1246 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1247 | .. Patch #1533909 |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 1248 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1249 | * An optional ``timeout`` parameter was added to the |
| 1250 | :func:`urllib.urlopen` function and the |
| 1251 | :class:`urllib.ftpwrapper` class constructor, as well as the |
| 1252 | :func:`urllib2.urlopen` function. The parameter specifies a timeout |
| 1253 | measured in seconds. For example:: |
| 1254 | |
| 1255 | >>> u = urllib2.urlopen("http://slow.example.com", timeout=3) |
| 1256 | Traceback (most recent call last): |
| 1257 | ... |
| 1258 | urllib2.URLError: <urlopen error timed out> |
| 1259 | >>> |
| 1260 | |
| 1261 | (Added by Facundo Batista.) |
| 1262 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1263 | * The XML-RPC classes :class:`SimpleXMLRPCServer` and :class:`DocXMLRPCServer` |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1264 | classes can now be prevented from immediately opening and binding to |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1265 | their socket by passing True as the ``bind_and_activate`` |
| 1266 | constructor parameter. This can be used to modify the instance's |
| 1267 | :attr:`allow_reuse_address` attribute before calling the |
| 1268 | :meth:`server_bind` and :meth:`server_activate` methods to |
| 1269 | open the socket and begin listening for connections. |
| 1270 | (Contributed by Peter Parente.) |
| 1271 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1272 | .. Patch 1599845 |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1273 | |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1274 | :class:`SimpleXMLRPCServer` also has a :attr:`_send_traceback_header` |
| 1275 | attribute; if true, the exception and formatted traceback are returned |
| 1276 | as HTTP headers "X-Exception" and "X-Traceback". This feature is |
| 1277 | for debugging purposes only and should not be used on production servers |
| 1278 | because the tracebacks could possibly reveal passwords or other sensitive |
| 1279 | information. (Contributed by Alan McIntyre as part of his |
| 1280 | project for Google's Summer of Code 2007.) |
| 1281 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1282 | * The :mod:`zipfile` module's :class:`ZipFile` class now has |
| 1283 | :meth:`extract` and :meth:`extractall` methods that will unpack |
| 1284 | a single file or all the files in the archive to the current directory, or |
| 1285 | to a specified directory:: |
| 1286 | |
| 1287 | z = zipfile.ZipFile('python-251.zip') |
| 1288 | |
| 1289 | # Unpack a single file, writing it relative to the /tmp directory. |
| 1290 | z.extract('Python/sysmodule.c', '/tmp') |
| 1291 | |
| 1292 | # Unpack all the files in the archive. |
| 1293 | z.extractall() |
| 1294 | |
| 1295 | (Contributed by Alan McIntyre.) |
| 1296 | .. % Patch 467924 |
| 1297 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1298 | .. ====================================================================== |
| 1299 | .. whole new modules get described in subsections here |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1300 | |
| 1301 | Improved SSL Support |
Andrew M. Kuchling | 27a4498 | 2007-10-20 19:39:35 +0000 | [diff] [blame] | 1302 | -------------------------------------------------- |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1303 | |
| 1304 | Bill Janssen made extensive improvements to Python 2.6's support for |
| 1305 | SSL. |
| 1306 | |
| 1307 | XXX use ssl.sslsocket - subclass of socket.socket. |
| 1308 | |
| 1309 | XXX Can specify if certificate is required, and obtain certificate info |
| 1310 | by calling getpeercert method. |
| 1311 | |
| 1312 | XXX sslwrap() behaves like socket.ssl |
| 1313 | |
| 1314 | XXX Certain features require the OpenSSL package to be installed, notably |
| 1315 | the 'openssl' binary. |
| 1316 | |
| 1317 | .. seealso:: |
| 1318 | |
| 1319 | SSL module documentation. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1320 | |
Andrew M. Kuchling | 0c3f168 | 2008-01-26 13:50:51 +0000 | [diff] [blame] | 1321 | |
| 1322 | .. ====================================================================== |
| 1323 | |
| 1324 | plistlib: A Property-List Parser |
| 1325 | -------------------------------------------------- |
| 1326 | |
| 1327 | A commonly-used format on MacOS X is the ``.plist`` format, |
| 1328 | which stores basic data types (numbers, strings, lists, |
| 1329 | and dictionaries) and serializes them into an XML-based format. |
| 1330 | (It's a lot like the XML-RPC serialization of data types.) |
| 1331 | |
| 1332 | Despite being primarily used on MacOS X, the format |
| 1333 | has nothing Mac-specific about it and the Python implementation works |
| 1334 | on any platform that Python supports, so the :mod:`plistlib` module |
| 1335 | has been promoted to the standard library. |
| 1336 | |
| 1337 | Using the module is simple:: |
| 1338 | |
| 1339 | import sys |
| 1340 | import plistlib |
| 1341 | import datetime |
| 1342 | |
| 1343 | # Create data structure |
| 1344 | data_struct = dict(lastAccessed=datetime.datetime.now(), |
| 1345 | version=1, |
| 1346 | categories=('Personal', 'Shared', 'Private')) |
| 1347 | |
| 1348 | # Create string containing XML. |
| 1349 | plist_str = plistlib.writePlistToString(data_struct) |
| 1350 | new_struct = plistlib.readPlistFromString(plist_str) |
| 1351 | print data_struct |
| 1352 | print new_struct |
| 1353 | |
| 1354 | # Write data structure to a file and read it back. |
| 1355 | plistlib.writePlist(data_struct, '/tmp/customizations.plist') |
| 1356 | new_struct = plistlib.readPlist('/tmp/customizations.plist') |
| 1357 | |
| 1358 | # read/writePlist accepts file-like objects as well as paths. |
| 1359 | plistlib.writePlist(data_struct, sys.stdout) |
| 1360 | |
| 1361 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1362 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1363 | |
| 1364 | |
| 1365 | Build and C API Changes |
| 1366 | ======================= |
| 1367 | |
| 1368 | Changes to Python's build process and to the C API include: |
| 1369 | |
Andrew M. Kuchling | f7b462f | 2007-11-23 13:37:39 +0000 | [diff] [blame] | 1370 | * Python 2.6 can be built with Microsoft Visual Studio 2008. |
| 1371 | See the :file:`PCbuild9` directory for the build files. |
| 1372 | (Implemented by Christian Heimes.) |
| 1373 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1374 | * The BerkeleyDB module now has a C API object, available as |
| 1375 | ``bsddb.db.api``. This object can be used by other C extensions |
| 1376 | that wish to use the :mod:`bsddb` module for their own purposes. |
| 1377 | (Contributed by Duncan Grisby.) |
| 1378 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1379 | .. Patch 1551895 |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1380 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1381 | * Several functions return information about the platform's |
| 1382 | floating-point support. :cfunc:`PyFloat_GetMax` returns |
| 1383 | the maximum representable floating point value, |
| 1384 | and :cfunc:`PyFloat_GetMin` returns the minimum |
| 1385 | positive value. :cfunc:`PyFloat_GetInfo` returns a dictionary |
| 1386 | containing more information from the :file:`float.h` file, such as |
| 1387 | ``"mant_dig"`` (number of digits in the mantissa), ``"epsilon"`` |
| 1388 | (smallest difference between 1.0 and the next largest value |
| 1389 | representable), and several others. |
Christian Heimes | d0d7d87 | 2008-01-04 02:03:25 +0000 | [diff] [blame] | 1390 | (Contributed by Christian Heimes.) |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1391 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1392 | .. Issue 1534 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1393 | |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1394 | * Python's C API now includes two functions for case-insensitive string |
| 1395 | comparisions, ``PyOS_stricmp(char*, char*)`` |
| 1396 | and ``PyOS_strnicmp(char*, char*, Py_ssize_t)``. |
Christian Heimes | d0d7d87 | 2008-01-04 02:03:25 +0000 | [diff] [blame] | 1397 | (Contributed by Christian Heimes.) |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1398 | |
| 1399 | .. Issue 1635 |
| 1400 | |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 1401 | * Some macros were renamed to make it clearer that they are macros, |
| 1402 | not functions. :cmacro:`Py_Size()` became :cmacro:`Py_SIZE()`, |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1403 | :cmacro:`Py_Type()` became :cmacro:`Py_TYPE()`, and |
| 1404 | :cmacro:`Py_Refcnt()` became :cmacro:`Py_REFCNT()`. Macros for backward |
Christian Heimes | d0d7d87 | 2008-01-04 02:03:25 +0000 | [diff] [blame] | 1405 | compatibility are still available for Python 2.6. |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1406 | |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 1407 | .. Issue 1629 |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1408 | |
Andrew M. Kuchling | 0c3f168 | 2008-01-26 13:50:51 +0000 | [diff] [blame] | 1409 | * Distutils now places C extensions it builds in a |
| 1410 | different directory when running on a debug version of Python. |
| 1411 | (Contributed by Collin Winter.) |
| 1412 | |
| 1413 | .. Patch 1530959 |
| 1414 | |
| 1415 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1416 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1417 | |
| 1418 | |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1419 | Port-Specific Changes: Windows |
| 1420 | ----------------------------------- |
| 1421 | |
| 1422 | * The :mod:`msvcrt` module now supports |
| 1423 | both the normal and wide char variants of the console I/O |
| 1424 | API. The :func:`getwch` function reads a keypress and returns a Unicode |
| 1425 | value, as does the :func:`getwche` function. The :func:`putwch` function |
| 1426 | takes a Unicode character and writes it to the console. |
Christian Heimes | ff6cc6b | 2008-01-17 23:01:44 +0000 | [diff] [blame] | 1427 | (Contributed by Christian Heimes.) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1428 | |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 1429 | * :func:`os.path.expandvars` will now expand environment variables |
| 1430 | in the form "%var%", and "~user" will be expanded into the |
| 1431 | user's home directory path. (Contributed by Josiah Carlson.) |
| 1432 | |
| 1433 | * The :mod:`socket` module's socket objects now have an |
| 1434 | :meth:`ioctl` method that provides a limited interface to the |
| 1435 | :cfunc:`WSAIoctl` system interface. |
| 1436 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1437 | * The :mod:`_winreg` module now has a function, |
| 1438 | :func:`ExpandEnvironmentStrings`, |
| 1439 | that expands environment variable references such as ``%NAME%`` |
| 1440 | in an input string. The handle objects provided by this |
| 1441 | module now support the context protocol, so they can be used |
Christian Heimes | ff6cc6b | 2008-01-17 23:01:44 +0000 | [diff] [blame] | 1442 | in :keyword:`with` statements. (Contributed by Christian Heimes.) |
| 1443 | |
| 1444 | * The new default compiler on Windows is Visual Studio 2008 (VS 9.0). The |
| 1445 | build directories for Visual Studio 2003 (VS7.1) and 2005 (VS8.0) |
| 1446 | were moved into the PC/ directory. The new PCbuild directory supports |
| 1447 | cross compilation for X64, debug builds and Profile Guided Optimization |
| 1448 | (PGO). PGO builds are roughly 10% faster than normal builds. |
| 1449 | (Contributed by Christian Heimes with help from Amaury Forgeot d'Arc and |
| 1450 | Martin von Loewis.) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1451 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1452 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1453 | |
| 1454 | |
| 1455 | .. _section-other: |
| 1456 | |
| 1457 | Other Changes and Fixes |
| 1458 | ======================= |
| 1459 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1460 | As usual, there were a bunch of other improvements and bugfixes |
| 1461 | scattered throughout the source tree. A search through the change |
| 1462 | logs finds there were XXX patches applied and YYY bugs fixed between |
| 1463 | Python 2.5 and 2.6. Both figures are likely to be underestimates. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1464 | |
| 1465 | Some of the more notable changes are: |
| 1466 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1467 | * It's now possible to prevent Python from writing any :file:`.pyc` |
| 1468 | or :file:`.pyo` files by either supplying the :option:`-B` switch |
| 1469 | or setting the :envvar:`PYTHONDONTWRITEBYTECODE` environment variable |
| 1470 | to any non-empty string when running the Python interpreter. These |
Georg Brandl | ca9c6e4 | 2008-01-15 06:58:15 +0000 | [diff] [blame] | 1471 | are also used to set the :data:`sys.dont_write_bytecode` attribute; |
| 1472 | Python code can change this variable to control whether bytecode |
| 1473 | files are subsequently written. |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1474 | (Contributed by Neal Norwitz and Georg Brandl.) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1475 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1476 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1477 | |
| 1478 | |
| 1479 | Porting to Python 2.6 |
| 1480 | ===================== |
| 1481 | |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1482 | This section lists previously described changes, and a few |
| 1483 | esoteric bugfixes, that may require changes to your |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1484 | code: |
| 1485 | |
Andrew M. Kuchling | 73835bd | 2008-01-04 18:24:41 +0000 | [diff] [blame] | 1486 | * The :meth:`__init__` method of :class:`collections.deque` |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1487 | now clears any existing contents of the deque |
| 1488 | before adding elements from the iterable. This change makes the |
| 1489 | behavior match that of ``list.__init__()``. |
| 1490 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1491 | * The :class:`Decimal` constructor now accepts leading and trailing |
| 1492 | whitespace when passed a string. Previously it would raise an |
| 1493 | :exc:`InvalidOperation` exception. On the other hand, the |
| 1494 | :meth:`create_decimal` method of :class:`Context` objects now |
| 1495 | explicitly disallows extra whitespace, raising a |
| 1496 | :exc:`ConversionSyntax` exception. |
| 1497 | |
| 1498 | * Due to an implementation accident, if you passed a file path to |
| 1499 | the built-in :func:`__import__` function, it would actually import |
| 1500 | the specified file. This was never intended to work, however, and |
| 1501 | the implementation now explicitly checks for this case and raises |
| 1502 | an :exc:`ImportError`. |
| 1503 | |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1504 | * The :mod:`socket` module exception :exc:`socket.error` now inherits |
| 1505 | from :exc:`IOError`. Previously it wasn't a subclass of |
| 1506 | :exc:`StandardError` but now it is, through :exc:`IOError`. |
| 1507 | (Implemented by Gregory P. Smith.) |
| 1508 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1509 | .. Issue 1706815 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1510 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1511 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1512 | |
| 1513 | |
| 1514 | .. _acks: |
| 1515 | |
| 1516 | Acknowledgements |
| 1517 | ================ |
| 1518 | |
| 1519 | The author would like to thank the following people for offering suggestions, |
| 1520 | corrections and assistance with various drafts of this article: . |
| 1521 | |