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 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 120 | XXX write this -- this section is currently just brief notes. |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 121 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 122 | The developers were growing increasingly annoyed by SourceForge's |
| 123 | bug tracker. (Discuss problems in a sentence or two.) |
| 124 | |
| 125 | Hosting provided by XXX. |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 126 | |
| 127 | New Documentation Format: ReStructured Text |
| 128 | -------------------------------------------------- |
| 129 | |
| 130 | Python's documentation had been written using LaTeX since the |
| 131 | project's inception around 1989. At that time, most documentation was |
| 132 | printed out for later study, not viewed online. LaTeX was widely used |
| 133 | because it provided attractive printed output while |
| 134 | remaining straightforward to write, once the basic rules |
| 135 | of the markup have been learned. |
| 136 | |
| 137 | LaTeX is still used today for writing technical publications destined |
| 138 | for printing, but the landscape for programming tools has shifted. We |
| 139 | no longer print out reams of documentation; instead, we browse through |
| 140 | it online and HTML is the most important format to support. |
| 141 | Unfortunately, converting LaTeX to HTML is fairly complicated, and |
| 142 | Fred L. Drake Jr., the Python documentation editor for many years, |
| 143 | spent a lot of time wrestling the conversion process into shape. |
| 144 | Occasionally people would suggest converting the documentation into |
| 145 | SGML or, later, XML, but performing a good conversion is a major task |
| 146 | and no one pursued the task to completion. |
| 147 | |
| 148 | During the 2.6 development cycle, Georg Brandl put a substantial |
| 149 | effort into building a new toolchain called Sphinx |
| 150 | for processing the documentation. |
| 151 | The input format is reStructured Text, |
| 152 | a markup commonly used in the Python community that supports |
| 153 | custom extensions and directives. Sphinx concentrates |
Andrew M. Kuchling | 2d60cf7 | 2007-12-22 17:27:02 +0000 | [diff] [blame] | 154 | on HTML output, producing attractively styled |
| 155 | and modern HTML, but printed output is still supported through |
| 156 | conversion to LaTeX as an output format. |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 157 | |
| 158 | .. seealso:: |
| 159 | |
| 160 | `Docutils <http://docutils.sf.net>`__: The fundamental |
| 161 | reStructured Text parser and toolset. |
| 162 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 163 | :ref:`documenting-index`: Describes how to write for |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 164 | Python's documentation. |
| 165 | |
| 166 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 167 | PEP 343: The 'with' statement |
| 168 | ============================= |
| 169 | |
| 170 | The previous version, Python 2.5, added the ':keyword:`with`' |
| 171 | statement an optional feature, to be enabled by a ``from __future__ |
Andrew M. Kuchling | 6e751f4 | 2007-12-03 21:28:41 +0000 | [diff] [blame] | 172 | 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] | 173 | be specially enabled; this means that :keyword:`with` is now always a |
| 174 | keyword. The rest of this section is a copy of the corresponding |
| 175 | section from "What's New in Python 2.5" document; if you read |
| 176 | it back when Python 2.5 came out, you can skip the rest of this |
| 177 | section. |
| 178 | |
| 179 | The ':keyword:`with`' statement clarifies code that previously would use |
| 180 | ``try...finally`` blocks to ensure that clean-up code is executed. In this |
| 181 | section, I'll discuss the statement as it will commonly be used. In the next |
| 182 | section, I'll examine the implementation details and show how to write objects |
| 183 | for use with this statement. |
| 184 | |
| 185 | The ':keyword:`with`' statement is a new control-flow structure whose basic |
| 186 | structure is:: |
| 187 | |
| 188 | with expression [as variable]: |
| 189 | with-block |
| 190 | |
| 191 | The expression is evaluated, and it should result in an object that supports the |
| 192 | context management protocol (that is, has :meth:`__enter__` and :meth:`__exit__` |
| 193 | methods. |
| 194 | |
| 195 | The object's :meth:`__enter__` is called before *with-block* is executed and |
| 196 | therefore can run set-up code. It also may return a value that is bound to the |
| 197 | name *variable*, if given. (Note carefully that *variable* is *not* assigned |
| 198 | the result of *expression*.) |
| 199 | |
| 200 | After execution of the *with-block* is finished, the object's :meth:`__exit__` |
| 201 | method is called, even if the block raised an exception, and can therefore run |
| 202 | clean-up code. |
| 203 | |
| 204 | Some standard Python objects now support the context management protocol and can |
| 205 | be used with the ':keyword:`with`' statement. File objects are one example:: |
| 206 | |
| 207 | with open('/etc/passwd', 'r') as f: |
| 208 | for line in f: |
| 209 | print line |
| 210 | ... more processing code ... |
| 211 | |
| 212 | After this statement has executed, the file object in *f* will have been |
| 213 | automatically closed, even if the :keyword:`for` loop raised an exception part- |
| 214 | way through the block. |
| 215 | |
| 216 | .. note:: |
| 217 | |
| 218 | In this case, *f* is the same object created by :func:`open`, because |
| 219 | :meth:`file.__enter__` returns *self*. |
| 220 | |
| 221 | The :mod:`threading` module's locks and condition variables also support the |
| 222 | ':keyword:`with`' statement:: |
| 223 | |
| 224 | lock = threading.Lock() |
| 225 | with lock: |
| 226 | # Critical section of code |
| 227 | ... |
| 228 | |
| 229 | The lock is acquired before the block is executed and always released once the |
| 230 | block is complete. |
| 231 | |
| 232 | The new :func:`localcontext` function in the :mod:`decimal` module makes it easy |
| 233 | to save and restore the current decimal context, which encapsulates the desired |
| 234 | precision and rounding characteristics for computations:: |
| 235 | |
| 236 | from decimal import Decimal, Context, localcontext |
| 237 | |
| 238 | # Displays with default precision of 28 digits |
| 239 | v = Decimal('578') |
| 240 | print v.sqrt() |
| 241 | |
| 242 | with localcontext(Context(prec=16)): |
| 243 | # All code in this block uses a precision of 16 digits. |
| 244 | # The original context is restored on exiting the block. |
| 245 | print v.sqrt() |
| 246 | |
| 247 | |
| 248 | .. _new-26-context-managers: |
| 249 | |
| 250 | Writing Context Managers |
| 251 | ------------------------ |
| 252 | |
| 253 | Under the hood, the ':keyword:`with`' statement is fairly complicated. Most |
| 254 | people will only use ':keyword:`with`' in company with existing objects and |
| 255 | don't need to know these details, so you can skip the rest of this section if |
| 256 | you like. Authors of new objects will need to understand the details of the |
| 257 | underlying implementation and should keep reading. |
| 258 | |
| 259 | A high-level explanation of the context management protocol is: |
| 260 | |
| 261 | * The expression is evaluated and should result in an object called a "context |
| 262 | manager". The context manager must have :meth:`__enter__` and :meth:`__exit__` |
| 263 | methods. |
| 264 | |
| 265 | * The context manager's :meth:`__enter__` method is called. The value returned |
Georg Brandl | d41b8dc | 2007-12-16 23:15:07 +0000 | [diff] [blame] | 266 | 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] | 267 | discarded. |
| 268 | |
| 269 | * The code in *BLOCK* is executed. |
| 270 | |
| 271 | * If *BLOCK* raises an exception, the :meth:`__exit__(type, value, traceback)` |
| 272 | is called with the exception details, the same values returned by |
| 273 | :func:`sys.exc_info`. The method's return value controls whether the exception |
| 274 | is re-raised: any false value re-raises the exception, and ``True`` will result |
| 275 | in suppressing it. You'll only rarely want to suppress the exception, because |
| 276 | if you do the author of the code containing the ':keyword:`with`' statement will |
| 277 | never realize anything went wrong. |
| 278 | |
| 279 | * If *BLOCK* didn't raise an exception, the :meth:`__exit__` method is still |
| 280 | called, but *type*, *value*, and *traceback* are all ``None``. |
| 281 | |
| 282 | Let's think through an example. I won't present detailed code but will only |
| 283 | sketch the methods necessary for a database that supports transactions. |
| 284 | |
| 285 | (For people unfamiliar with database terminology: a set of changes to the |
| 286 | database are grouped into a transaction. Transactions can be either committed, |
| 287 | meaning that all the changes are written into the database, or rolled back, |
| 288 | meaning that the changes are all discarded and the database is unchanged. See |
| 289 | any database textbook for more information.) |
| 290 | |
| 291 | Let's assume there's an object representing a database connection. Our goal will |
| 292 | be to let the user write code like this:: |
| 293 | |
| 294 | db_connection = DatabaseConnection() |
| 295 | with db_connection as cursor: |
| 296 | cursor.execute('insert into ...') |
| 297 | cursor.execute('delete from ...') |
| 298 | # ... more operations ... |
| 299 | |
| 300 | The transaction should be committed if the code in the block runs flawlessly or |
| 301 | rolled back if there's an exception. Here's the basic interface for |
| 302 | :class:`DatabaseConnection` that I'll assume:: |
| 303 | |
| 304 | class DatabaseConnection: |
| 305 | # Database interface |
Georg Brandl | 9f72d23 | 2007-12-16 23:13:29 +0000 | [diff] [blame] | 306 | def cursor(self): |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 307 | "Returns a cursor object and starts a new transaction" |
Georg Brandl | 9f72d23 | 2007-12-16 23:13:29 +0000 | [diff] [blame] | 308 | def commit(self): |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 309 | "Commits current transaction" |
Georg Brandl | 9f72d23 | 2007-12-16 23:13:29 +0000 | [diff] [blame] | 310 | def rollback(self): |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 311 | "Rolls back current transaction" |
| 312 | |
| 313 | The :meth:`__enter__` method is pretty easy, having only to start a new |
| 314 | transaction. For this application the resulting cursor object would be a useful |
| 315 | result, so the method will return it. The user can then add ``as cursor`` to |
| 316 | their ':keyword:`with`' statement to bind the cursor to a variable name. :: |
| 317 | |
| 318 | class DatabaseConnection: |
| 319 | ... |
Georg Brandl | 9f72d23 | 2007-12-16 23:13:29 +0000 | [diff] [blame] | 320 | def __enter__(self): |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 321 | # Code to start a new transaction |
| 322 | cursor = self.cursor() |
| 323 | return cursor |
| 324 | |
| 325 | The :meth:`__exit__` method is the most complicated because it's where most of |
| 326 | the work has to be done. The method has to check if an exception occurred. If |
| 327 | there was no exception, the transaction is committed. The transaction is rolled |
| 328 | back if there was an exception. |
| 329 | |
| 330 | In the code below, execution will just fall off the end of the function, |
| 331 | returning the default value of ``None``. ``None`` is false, so the exception |
| 332 | will be re-raised automatically. If you wished, you could be more explicit and |
| 333 | add a :keyword:`return` statement at the marked location. :: |
| 334 | |
| 335 | class DatabaseConnection: |
| 336 | ... |
Georg Brandl | 9f72d23 | 2007-12-16 23:13:29 +0000 | [diff] [blame] | 337 | def __exit__(self, type, value, tb): |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 338 | if tb is None: |
| 339 | # No exception, so commit |
| 340 | self.commit() |
| 341 | else: |
| 342 | # Exception occurred, so rollback. |
| 343 | self.rollback() |
| 344 | # return False |
| 345 | |
| 346 | |
| 347 | .. _module-contextlib: |
| 348 | |
| 349 | The contextlib module |
| 350 | --------------------- |
| 351 | |
| 352 | The new :mod:`contextlib` module provides some functions and a decorator that |
| 353 | are useful for writing objects for use with the ':keyword:`with`' statement. |
| 354 | |
| 355 | The decorator is called :func:`contextmanager`, and lets you write a single |
| 356 | generator function instead of defining a new class. The generator should yield |
| 357 | exactly one value. The code up to the :keyword:`yield` will be executed as the |
| 358 | :meth:`__enter__` method, and the value yielded will be the method's return |
| 359 | value that will get bound to the variable in the ':keyword:`with`' statement's |
| 360 | :keyword:`as` clause, if any. The code after the :keyword:`yield` will be |
| 361 | executed in the :meth:`__exit__` method. Any exception raised in the block will |
| 362 | be raised by the :keyword:`yield` statement. |
| 363 | |
| 364 | Our database example from the previous section could be written using this |
| 365 | decorator as:: |
| 366 | |
| 367 | from contextlib import contextmanager |
| 368 | |
| 369 | @contextmanager |
Georg Brandl | 9f72d23 | 2007-12-16 23:13:29 +0000 | [diff] [blame] | 370 | def db_transaction(connection): |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 371 | cursor = connection.cursor() |
| 372 | try: |
| 373 | yield cursor |
| 374 | except: |
| 375 | connection.rollback() |
| 376 | raise |
| 377 | else: |
| 378 | connection.commit() |
| 379 | |
| 380 | db = DatabaseConnection() |
| 381 | with db_transaction(db) as cursor: |
| 382 | ... |
| 383 | |
| 384 | The :mod:`contextlib` module also has a :func:`nested(mgr1, mgr2, ...)` function |
| 385 | that combines a number of context managers so you don't need to write nested |
| 386 | ':keyword:`with`' statements. In this example, the single ':keyword:`with`' |
| 387 | statement both starts a database transaction and acquires a thread lock:: |
| 388 | |
| 389 | lock = threading.Lock() |
| 390 | with nested (db_transaction(db), lock) as (cursor, locked): |
| 391 | ... |
| 392 | |
| 393 | Finally, the :func:`closing(object)` function returns *object* so that it can be |
| 394 | bound to a variable, and calls ``object.close`` at the end of the block. :: |
| 395 | |
| 396 | import urllib, sys |
| 397 | from contextlib import closing |
| 398 | |
| 399 | with closing(urllib.urlopen('http://www.yahoo.com')) as f: |
| 400 | for line in f: |
| 401 | sys.stdout.write(line) |
| 402 | |
| 403 | |
| 404 | .. seealso:: |
| 405 | |
| 406 | :pep:`343` - The "with" statement |
| 407 | PEP written by Guido van Rossum and Nick Coghlan; implemented by Mike Bland, |
| 408 | Guido van Rossum, and Neal Norwitz. The PEP shows the code generated for a |
| 409 | ':keyword:`with`' statement, which can be helpful in learning how the statement |
| 410 | works. |
| 411 | |
| 412 | The documentation for the :mod:`contextlib` module. |
| 413 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 414 | .. ====================================================================== |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 415 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 416 | .. _pep-0366: |
| 417 | |
| 418 | PEP 366: Explicit Relative Imports From a Main Module |
| 419 | ============================================================ |
| 420 | |
| 421 | Python's :option:`-m` switch allows running a module as a script. |
| 422 | When you ran a module that was located inside a package, relative |
| 423 | imports didn't work correctly. |
| 424 | |
| 425 | The fix in Python 2.6 adds a :attr:`__package__` attribute to modules. |
| 426 | When present, relative imports will be relative to the value of this |
| 427 | attribute instead of the :attr:`__name__` attribute. PEP 302-style |
| 428 | importers can then set :attr:`__package__`. The :mod:`runpy` module |
| 429 | that implements the :option:`-m` switch now does this, so relative imports |
| 430 | can now be used in scripts running from inside a package. |
| 431 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 432 | .. ====================================================================== |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 433 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 434 | .. :: |
| 435 | |
| 436 | .. _pep-0370: |
| 437 | |
| 438 | PEP 370: XXX |
| 439 | ===================================================== |
| 440 | |
| 441 | When you run Python, the module search page ``sys.modules`` usually |
| 442 | includes a directory whose path ends in ``"site-packages"``. This |
| 443 | directory is intended to hold locally-installed packages available to |
| 444 | all users on a machine or using a particular site installation. |
| 445 | |
| 446 | Python 2.6 introduces a convention for user-specific site directories. |
| 447 | |
| 448 | .. seealso:: |
| 449 | |
| 450 | :pep:`370` - XXX |
| 451 | |
| 452 | PEP written by XXX; implemented by Christian Heimes. |
| 453 | |
| 454 | |
| 455 | .. ====================================================================== |
| 456 | |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 457 | .. _pep-3101: |
| 458 | |
| 459 | PEP 3101: Advanced String Formatting |
| 460 | ===================================================== |
| 461 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 462 | XXX write this -- this section is currently just brief notes. |
| 463 | |
| 464 | 8-bit and Unicode strings have a .format() method that takes the arguments |
| 465 | to be formatted. |
| 466 | |
| 467 | .format() uses curly brackets ({, }) as special characters: |
| 468 | |
| 469 | format("User ID: {0}", "root") -> "User ID: root" |
| 470 | format("Empty dict: {{}}") -> "Empty dict: {}" |
| 471 | 0.name |
| 472 | 0[name] |
| 473 | |
| 474 | Format specifiers: |
| 475 | |
| 476 | 0:8 -> left-align, pad |
| 477 | 0:>8 -> right-align, pad |
| 478 | |
| 479 | Format data types:: |
| 480 | |
| 481 | ... take table from PEP 3101 |
| 482 | |
| 483 | Classes and types can define a __format__ method to control how it's |
| 484 | formatted. It receives a single argument, the format specifier:: |
| 485 | |
| 486 | def __format__(self, format_spec): |
| 487 | if isinstance(format_spec, unicode): |
| 488 | return unicode(str(self)) |
| 489 | else: |
| 490 | return str(self) |
| 491 | |
| 492 | There's also a format() built-in that will format a single value. It calls |
| 493 | the type's :meth:`__format__` method with the provided specifier:: |
| 494 | |
| 495 | >>> format(75.6564, '.2f') |
| 496 | '75.66' |
| 497 | |
| 498 | .. seealso:: |
| 499 | |
| 500 | :pep:`3101` - Advanced String Formatting |
| 501 | PEP written by Talin. |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 502 | |
| 503 | .. ====================================================================== |
| 504 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 505 | .. _pep-3110: |
| 506 | |
| 507 | PEP 3110: Exception-Handling Changes |
| 508 | ===================================================== |
| 509 | |
| 510 | One error that Python programmers occasionally make |
| 511 | is the following:: |
| 512 | |
| 513 | try: |
| 514 | ... |
| 515 | except TypeError, ValueError: |
| 516 | ... |
| 517 | |
| 518 | The author is probably trying to catch both |
| 519 | :exc:`TypeError` and :exc:`ValueError` exceptions, but this code |
| 520 | actually does something different: it will catch |
| 521 | :exc:`TypeError` and bind the resulting exception object |
| 522 | to the local name ``"ValueError"``. The correct code |
| 523 | would have specified a tuple:: |
| 524 | |
| 525 | try: |
| 526 | ... |
| 527 | except (TypeError, ValueError): |
| 528 | ... |
| 529 | |
| 530 | This error is possible because the use of the comma here is ambiguous: |
| 531 | does it indicate two different nodes in the parse tree, or a single |
| 532 | node that's a tuple. |
| 533 | |
| 534 | Python 3.0 changes the syntax to make this unambiguous by replacing |
| 535 | the comma with the word "as". To catch an exception and store the |
| 536 | exception object in the variable ``exc``, you must write:: |
| 537 | |
| 538 | try: |
| 539 | ... |
| 540 | except TypeError as exc: |
| 541 | ... |
| 542 | |
| 543 | Python 3.0 will only support the use of "as", and therefore interprets |
| 544 | the first example as catching two different exceptions. Python 2.6 |
| 545 | supports both the comma and "as", so existing code will continue to |
| 546 | work. |
| 547 | |
| 548 | .. seealso:: |
| 549 | |
| 550 | :pep:`3110` - Catching Exceptions in Python 3000 |
| 551 | PEP written and implemented by Collin Winter. |
| 552 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 553 | .. ====================================================================== |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 554 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 555 | .. _pep-3112: |
| 556 | |
| 557 | PEP 3112: Byte Literals |
| 558 | ===================================================== |
| 559 | |
| 560 | Python 3.0 adopts Unicode as the language's fundamental string type, and |
| 561 | denotes 8-bit literals differently, either as ``b'string'`` |
| 562 | or using a :class:`bytes` constructor. For future compatibility, |
| 563 | Python 2.6 adds :class:`bytes` as a synonym for the :class:`str` type, |
| 564 | and it also supports the ``b''`` notation. |
| 565 | |
| 566 | .. seealso:: |
| 567 | |
| 568 | :pep:`3112` - Bytes literals in Python 3000 |
| 569 | PEP written by Jason Orendorff; backported to 2.6 by Christian Heimes. |
| 570 | |
| 571 | .. ====================================================================== |
| 572 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 573 | .. _pep-3119: |
| 574 | |
| 575 | PEP 3119: Abstract Base Classes |
| 576 | ===================================================== |
| 577 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 578 | XXX write this -- this section is currently just brief notes. |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 579 | |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 580 | How to identify a file object? |
| 581 | |
| 582 | ABCs are a collection of classes describing various interfaces. |
| 583 | Classes can derive from an ABC to indicate they support that ABC's |
| 584 | interface. Concrete classes should obey the semantics specified by |
| 585 | an ABC, but Python can't check this; it's up to the implementor. |
| 586 | |
| 587 | A metaclass lets you declare that an existing class or type |
| 588 | derives from a particular ABC. You can even |
| 589 | |
| 590 | class AppendableSequence: |
| 591 | __metaclass__ = ABCMeta |
| 592 | |
| 593 | AppendableSequence.register(list) |
| 594 | assert issubclass(list, AppendableSequence) |
| 595 | assert isinstance([], AppendableSequence) |
| 596 | |
| 597 | @abstractmethod decorator -- you can't instantiate classes w/ |
| 598 | an abstract method. |
| 599 | |
Andrew M. Kuchling | 73835bd | 2008-01-04 18:24:41 +0000 | [diff] [blame] | 600 | :: |
| 601 | |
| 602 | @abstractproperty decorator |
| 603 | @abstractproperty |
| 604 | def readonly(self): |
| 605 | return self._x |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 606 | |
| 607 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 608 | .. seealso:: |
| 609 | |
| 610 | :pep:`3119` - Introducing Abstract Base Classes |
| 611 | PEP written by Guido van Rossum and Talin. |
| 612 | Implemented by XXX. |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 613 | Backported to 2.6 by Benjamin Aranguren, with Alex Martelli. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 614 | |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 615 | .. ====================================================================== |
| 616 | |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 617 | .. _pep-3127: |
| 618 | |
| 619 | PEP 3127: Integer Literal Support and Syntax |
| 620 | ===================================================== |
| 621 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 622 | XXX write this -- this section is currently just brief notes. |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 623 | |
| 624 | Python 3.0 changes the syntax for octal integer literals, and |
| 625 | adds supports for binary integers: 0o instad of 0, |
| 626 | and 0b for binary. Python 2.6 doesn't support this, but a bin() |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 627 | builtin was added. |
| 628 | |
| 629 | XXX changes to the hex/oct builtins |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 630 | |
| 631 | |
| 632 | New bin() built-in returns the binary form of a number. |
| 633 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 634 | .. seealso:: |
| 635 | |
| 636 | :pep:`3127` - Integer Literal Support and Syntax |
| 637 | PEP written by Patrick Maupin. |
| 638 | |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 639 | .. ====================================================================== |
| 640 | |
| 641 | .. _pep-3129: |
| 642 | |
| 643 | PEP 3129: Class Decorators |
| 644 | ===================================================== |
| 645 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 646 | XXX write this -- this section is currently just brief notes. |
| 647 | |
| 648 | Class decorators are analogous to function decorators. After defining a class, |
| 649 | it's passed through the specified series of decorator functions |
| 650 | and the ultimate return value is recorded as the class. |
| 651 | |
| 652 | :: |
| 653 | |
| 654 | class A: |
| 655 | pass |
| 656 | A = foo(bar(A)) |
| 657 | |
| 658 | |
| 659 | @foo |
| 660 | @bar |
| 661 | class A: |
| 662 | pass |
| 663 | |
| 664 | XXX need to find a good motivating example. |
| 665 | |
| 666 | .. seealso:: |
| 667 | |
| 668 | :pep:`3129` - Class Decorators |
| 669 | PEP written by Collin Winter. |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 670 | |
| 671 | .. ====================================================================== |
| 672 | |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 673 | .. _pep-3141: |
| 674 | |
| 675 | PEP 3141: A Type Hierarchy for Numbers |
| 676 | ===================================================== |
| 677 | |
| 678 | In Python 3.0, several abstract base classes for numeric types, |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 679 | inspired by Scheme's numeric tower, are being added. |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 680 | This change was backported to 2.6 as the :mod:`numbers` module. |
| 681 | |
| 682 | The most general ABC is :class:`Number`. It defines no operations at |
| 683 | all, and only exists to allow checking if an object is a number by |
| 684 | doing ``isinstance(obj, Number)``. |
| 685 | |
| 686 | Numbers are further divided into :class:`Exact` and :class:`Inexact`. |
| 687 | Exact numbers can represent values precisely and operations never |
| 688 | round off the results or introduce tiny errors that may break the |
Georg Brandl | 907a720 | 2008-02-22 12:31:45 +0000 | [diff] [blame] | 689 | commutativity and associativity properties; inexact numbers may |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 690 | perform such rounding or introduce small errors. Integers, long |
| 691 | integers, and rational numbers are exact, while floating-point |
| 692 | and complex numbers are inexact. |
| 693 | |
| 694 | :class:`Complex` is a subclass of :class:`Number`. Complex numbers |
| 695 | can undergo the basic operations of addition, subtraction, |
| 696 | multiplication, division, and exponentiation, and you can retrieve the |
| 697 | real and imaginary parts and obtain a number's conjugate. Python's built-in |
| 698 | complex type is an implementation of :class:`Complex`. |
| 699 | |
| 700 | :class:`Real` further derives from :class:`Complex`, and adds |
| 701 | operations that only work on real numbers: :func:`floor`, :func:`trunc`, |
| 702 | rounding, taking the remainder mod N, floor division, |
| 703 | and comparisons. |
| 704 | |
| 705 | :class:`Rational` numbers derive from :class:`Real`, have |
| 706 | :attr:`numerator` and :attr:`denominator` properties, and can be |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 707 | converted to floats. Python 2.6 adds a simple rational-number class, |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 708 | :class:`Fraction`, in the :mod:`fractions` module. (It's called |
| 709 | :class:`Fraction` instead of :class:`Rational` to avoid |
| 710 | a name clash with :class:`numbers.Rational`.) |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 711 | |
| 712 | :class:`Integral` numbers derive from :class:`Rational`, and |
| 713 | can be shifted left and right with ``<<`` and ``>>``, |
| 714 | combined using bitwise operations such as ``&`` and ``|``, |
| 715 | and can be used as array indexes and slice boundaries. |
| 716 | |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 717 | In Python 3.0, the PEP slightly redefines the existing built-ins |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 718 | :func:`round`, :func:`math.floor`, :func:`math.ceil`, and adds a new |
| 719 | one, :func:`math.trunc`, that's been backported to Python 2.6. |
| 720 | :func:`math.trunc` rounds toward zero, returning the closest |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 721 | :class:`Integral` that's between the function's argument and zero. |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 722 | |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 723 | .. seealso:: |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 724 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 725 | :pep:`3141` - A Type Hierarchy for Numbers |
| 726 | PEP written by Jeffrey Yasskin. |
| 727 | |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 728 | XXX link: Discusses Scheme's numeric tower. |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 729 | |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 730 | |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 731 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 732 | The :mod:`fractions` Module |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 733 | -------------------------------------------------- |
| 734 | |
| 735 | To fill out the hierarchy of numeric types, a rational-number class |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 736 | has been added as the :mod:`fractions` module. Rational numbers are |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 737 | represented as a fraction, and can exactly represent |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 738 | numbers such as two-thirds that floating-point numbers can only |
| 739 | approximate. |
| 740 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 741 | The :class:`Fraction` constructor takes two :class:`Integral` values |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 742 | that will be the numerator and denominator of the resulting fraction. :: |
| 743 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 744 | >>> from fractions import Fraction |
| 745 | >>> a = Fraction(2, 3) |
| 746 | >>> b = Fraction(2, 5) |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 747 | >>> float(a), float(b) |
| 748 | (0.66666666666666663, 0.40000000000000002) |
| 749 | >>> a+b |
Mark Dickinson | cd873fc | 2008-02-11 03:11:55 +0000 | [diff] [blame] | 750 | Fraction(16, 15) |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 751 | >>> a/b |
Mark Dickinson | cd873fc | 2008-02-11 03:11:55 +0000 | [diff] [blame] | 752 | Fraction(5, 3) |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 753 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 754 | To help in converting floating-point numbers to rationals, |
| 755 | the float type now has a :meth:`as_integer_ratio()` method that returns |
| 756 | the numerator and denominator for a fraction that evaluates to the same |
| 757 | floating-point value:: |
| 758 | |
| 759 | >>> (2.5) .as_integer_ratio() |
| 760 | (5, 2) |
| 761 | >>> (3.1415) .as_integer_ratio() |
| 762 | (7074029114692207L, 2251799813685248L) |
| 763 | >>> (1./3) .as_integer_ratio() |
| 764 | (6004799503160661L, 18014398509481984L) |
| 765 | |
| 766 | Note that values that can only be approximated by floating-point |
| 767 | numbers, such as 1./3, are not simplified to the number being |
| 768 | approximated; the fraction attempts to match the floating-point value |
| 769 | **exactly**. |
| 770 | |
Mark Dickinson | d058cd2 | 2008-02-10 21:29:51 +0000 | [diff] [blame] | 771 | The :mod:`fractions` module is based upon an implementation by Sjoerd |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 772 | Mullender that was in Python's :file:`Demo/classes/` directory for a |
| 773 | long time. This implementation was significantly updated by Jeffrey |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 774 | Yasskin. |
Andrew M. Kuchling | aa35554 | 2008-01-16 03:17:25 +0000 | [diff] [blame] | 775 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 776 | Other Language Changes |
| 777 | ====================== |
| 778 | |
| 779 | Here are all of the changes that Python 2.6 makes to the core Python language. |
| 780 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 781 | * When calling a function using the ``**`` syntax to provide keyword |
| 782 | arguments, you are no longer required to use a Python dictionary; |
| 783 | any mapping will now work:: |
| 784 | |
| 785 | >>> def f(**kw): |
| 786 | ... print sorted(kw) |
| 787 | ... |
| 788 | >>> ud=UserDict.UserDict() |
| 789 | >>> ud['a'] = 1 |
| 790 | >>> ud['b'] = 'string' |
| 791 | >>> f(**ud) |
| 792 | ['a', 'b'] |
| 793 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 794 | .. Patch 1686487 |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 795 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 796 | * The built-in types now have improved support for extended slicing syntax, |
| 797 | where various combinations of ``(start, stop, step)`` are supplied. |
| 798 | Previously, the support was partial and certain corner cases wouldn't work. |
| 799 | (Implemented by Thomas Wouters.) |
| 800 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 801 | .. Revision 57619 |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 802 | |
Christian Heimes | ff6cc6b | 2008-01-17 23:01:44 +0000 | [diff] [blame] | 803 | * Properties now have three attributes, :attr:`getter`, |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 804 | :attr:`setter` and :attr:`deleter`, that are useful shortcuts for |
Christian Heimes | ff6cc6b | 2008-01-17 23:01:44 +0000 | [diff] [blame] | 805 | adding or modifying a getter, setter or deleter function to an |
| 806 | existing property. You would use them like this:: |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 807 | |
| 808 | class C(object): |
| 809 | @property |
| 810 | def x(self): |
| 811 | return self._x |
| 812 | |
| 813 | @x.setter |
| 814 | def x(self, value): |
| 815 | self._x = value |
| 816 | |
| 817 | @x.deleter |
| 818 | def x(self): |
| 819 | del self._x |
| 820 | |
Christian Heimes | ff6cc6b | 2008-01-17 23:01:44 +0000 | [diff] [blame] | 821 | class D(C): |
| 822 | @C.x.getter |
| 823 | def x(self): |
| 824 | return self._x * 2 |
| 825 | |
| 826 | @x.setter |
| 827 | def x(self, value): |
| 828 | self._x = value / 2 |
| 829 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 830 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 831 | * C functions and methods that use |
| 832 | :cfunc:`PyComplex_AsCComplex` will now accept arguments that |
| 833 | have a :meth:`__complex__` method. In particular, the functions in the |
| 834 | :mod:`cmath` module will now accept objects with this method. |
| 835 | This is a backport of a Python 3.0 change. |
| 836 | (Contributed by Mark Dickinson.) |
| 837 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 838 | .. Patch #1675423 |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 839 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 840 | A numerical nicety: when creating a complex number from two floats |
| 841 | on systems that support signed zeros (-0 and +0), the |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 842 | :func:`complex` constructor will now preserve the sign |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 843 | of the zero. |
| 844 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 845 | .. Patch 1507 |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 846 | |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 847 | * More floating-point features were also added. The :func:`float` function |
| 848 | will now turn the strings ``+nan`` and ``-nan`` into the corresponding |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 849 | IEEE 754 Not A Number values, and ``+inf`` and ``-inf`` into |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 850 | positive or negative infinity. This works on any platform with |
Christian Heimes | d0d7d87 | 2008-01-04 02:03:25 +0000 | [diff] [blame] | 851 | IEEE 754 semantics. (Contributed by Christian Heimes.) |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 852 | |
Georg Brandl | 225163d | 2008-03-05 07:10:35 +0000 | [diff] [blame] | 853 | .. Patch 1635 |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 854 | |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 855 | Other functions in the :mod:`math` module, :func:`isinf` and |
| 856 | :func:`isnan`, return true if their floating-point argument is |
Georg Brandl | e1b8e9c | 2008-02-20 19:12:36 +0000 | [diff] [blame] | 857 | infinite or Not A Number. |
| 858 | |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 859 | .. Patch 1640 |
Georg Brandl | e1b8e9c | 2008-02-20 19:12:36 +0000 | [diff] [blame] | 860 | |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 861 | The ``math.copysign(x, y)`` function |
| 862 | copies the sign bit of an IEEE 754 number, returning the absolute |
| 863 | value of *x* combined with the sign bit of *y*. For example, |
| 864 | ``math.copysign(1, -0.0)`` returns -1.0. (Contributed by Christian |
| 865 | Heimes.) |
| 866 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 867 | * Changes to the :class:`Exception` interface |
| 868 | as dictated by :pep:`352` continue to be made. For 2.6, |
| 869 | the :attr:`message` attribute is being deprecated in favor of the |
| 870 | :attr:`args` attribute. |
| 871 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 872 | * The :exc:`GeneratorExit` exception now subclasses |
| 873 | :exc:`BaseException` instead of :exc:`Exception`. This means |
| 874 | that an exception handler that does ``except Exception:`` |
| 875 | will not inadvertently catch :exc:`GeneratorExit`. |
| 876 | (Contributed by Chad Austin.) |
| 877 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 878 | .. Patch #1537 |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 879 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 880 | * Generator objects now have a :attr:`gi_code` attribute that refers to |
| 881 | the original code object backing the generator. |
| 882 | (Contributed by Collin Winter.) |
| 883 | |
| 884 | .. Patch #1473257 |
| 885 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 886 | * The :func:`compile` built-in function now accepts keyword arguments |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 887 | as well as positional parameters. (Contributed by Thomas Wouters.) |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 888 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 889 | .. Patch 1444529 |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 890 | |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 891 | * The :func:`complex` constructor now accepts strings containing |
| 892 | parenthesized complex numbers, letting ``complex(repr(cmplx))`` |
| 893 | will now round-trip values. For example, ``complex('(3+4j)')`` |
| 894 | now returns the value (3+4j). |
| 895 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 896 | .. Patch 1491866 |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 897 | |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 898 | * The string :meth:`translate` method now accepts ``None`` as the |
| 899 | translation table parameter, which is treated as the identity |
| 900 | transformation. This makes it easier to carry out operations |
| 901 | that only delete characters. (Contributed by Bengt Richter.) |
| 902 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 903 | .. Patch 1193128 |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 904 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 905 | * The built-in :func:`dir` function now checks for a :meth:`__dir__` |
| 906 | method on the objects it receives. This method must return a list |
| 907 | of strings containing the names of valid attributes for the object, |
| 908 | and lets the object control the value that :func:`dir` produces. |
| 909 | Objects that have :meth:`__getattr__` or :meth:`__getattribute__` |
Facundo Batista | bd5b623 | 2007-12-03 19:49:54 +0000 | [diff] [blame] | 910 | methods can use this to advertise pseudo-attributes they will honor. |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 911 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 912 | .. Patch 1591665 |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 913 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 914 | * An obscure change: when you use the the :func:`locals` function inside a |
| 915 | :keyword:`class` statement, the resulting dictionary no longer returns free |
| 916 | variables. (Free variables, in this case, are variables referred to in the |
| 917 | :keyword:`class` statement that aren't attributes of the class.) |
| 918 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 919 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 920 | |
| 921 | |
| 922 | Optimizations |
| 923 | ------------- |
| 924 | |
Georg Brandl | af30b28 | 2008-01-15 06:55:56 +0000 | [diff] [blame] | 925 | * Type objects now have a cache of methods that can reduce |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 926 | the amount of work required to find the correct method implementation |
Andrew M. Kuchling | a01ed03 | 2008-01-15 01:55:32 +0000 | [diff] [blame] | 927 | for a particular class; once cached, the interpreter doesn't need to |
| 928 | traverse base classes to figure out the right method to call. |
| 929 | The cache is cleared if a base class or the class itself is modified, |
| 930 | so the cache should remain correct even in the face of Python's dynamic |
| 931 | nature. |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 932 | (Original optimization implemented by Armin Rigo, updated for |
| 933 | Python 2.6 by Kevin Jacobs.) |
| 934 | |
Georg Brandl | 225163d | 2008-03-05 07:10:35 +0000 | [diff] [blame] | 935 | .. Patch 1700288 |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 936 | |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 937 | * All of the functions in the :mod:`struct` module have been rewritten in |
| 938 | C, thanks to work at the Need For Speed sprint. |
| 939 | (Contributed by Raymond Hettinger.) |
| 940 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 941 | * Internally, a bit is now set in type objects to indicate some of the standard |
| 942 | built-in types. This speeds up checking if an object is a subclass of one of |
| 943 | these types. (Contributed by Neal Norwitz.) |
| 944 | |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 945 | * Unicode strings now uses faster code for detecting |
| 946 | whitespace and line breaks; this speeds up the :meth:`split` method |
| 947 | by about 25% and :meth:`splitlines` by 35%. |
| 948 | (Contributed by Antoine Pitrou.) |
| 949 | |
| 950 | * To reduce memory usage, the garbage collector will now clear internal |
| 951 | free lists when garbage-collecting the highest generation of objects. |
| 952 | This may return memory to the OS sooner. |
| 953 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 954 | The net result of the 2.6 optimizations is that Python 2.6 runs the pystone |
| 955 | benchmark around XX% faster than Python 2.5. |
| 956 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 957 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 958 | |
| 959 | |
| 960 | New, Improved, and Deprecated Modules |
| 961 | ===================================== |
| 962 | |
| 963 | As usual, Python's standard library received a number of enhancements and bug |
| 964 | fixes. Here's a partial list of the most notable changes, sorted alphabetically |
| 965 | by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more |
| 966 | complete list of changes, or look through the CVS logs for all the details. |
| 967 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 968 | * The :mod:`bsddb.dbshelve` module now uses the highest pickling protocol |
| 969 | available, instead of restricting itself to protocol 1. |
| 970 | (Contributed by W. Barnes.) |
| 971 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 972 | .. Patch 1551443 |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 973 | |
Andrew M. Kuchling | 6d57c82 | 2007-10-23 20:55:47 +0000 | [diff] [blame] | 974 | * A new data type in the :mod:`collections` module: :class:`namedtuple(typename, |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 975 | fieldnames)` is a factory function that creates subclasses of the standard tuple |
| 976 | whose fields are accessible by name as well as index. For example:: |
| 977 | |
Andrew M. Kuchling | 6d57c82 | 2007-10-23 20:55:47 +0000 | [diff] [blame] | 978 | >>> var_type = collections.namedtuple('variable', |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 979 | ... 'id name type size') |
| 980 | # Names are separated by spaces or commas. |
| 981 | # 'id, name, type, size' would also work. |
Raymond Hettinger | 366523c | 2007-12-14 18:12:21 +0000 | [diff] [blame] | 982 | >>> var_type._fields |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 983 | ('id', 'name', 'type', 'size') |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 984 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 985 | >>> var = var_type(1, 'frequency', 'int', 4) |
| 986 | >>> print var[0], var.id # Equivalent |
| 987 | 1 1 |
| 988 | >>> print var[2], var.type # Equivalent |
| 989 | int int |
Raymond Hettinger | 366523c | 2007-12-14 18:12:21 +0000 | [diff] [blame] | 990 | >>> var._asdict() |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 991 | {'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'} |
Raymond Hettinger | e9b9b35 | 2008-02-15 21:21:25 +0000 | [diff] [blame] | 992 | >>> v2 = var._replace(name='amplitude') |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 993 | >>> v2 |
| 994 | variable(id=1, name='amplitude', type='int', size=4) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 995 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 996 | Where the new :class:`namedtuple` type proved suitable, the standard |
| 997 | library has been modified to return them. For example, |
| 998 | the :meth:`Decimal.as_tuple` method now returns a named tuple with |
| 999 | :attr:`sign`, :attr:`digits`, and :attr:`exponent` fields. |
| 1000 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1001 | (Contributed by Raymond Hettinger.) |
| 1002 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1003 | * Another change to the :mod:`collections` module is that the |
Georg Brandl | e7d118a | 2007-12-08 11:05:05 +0000 | [diff] [blame] | 1004 | :class:`deque` type now supports an optional *maxlen* parameter; |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1005 | if supplied, the deque's size will be restricted to no more |
Georg Brandl | e7d118a | 2007-12-08 11:05:05 +0000 | [diff] [blame] | 1006 | than *maxlen* items. Adding more items to a full deque causes |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1007 | old items to be discarded. |
| 1008 | |
| 1009 | :: |
| 1010 | |
| 1011 | >>> from collections import deque |
| 1012 | >>> dq=deque(maxlen=3) |
| 1013 | >>> dq |
| 1014 | deque([], maxlen=3) |
| 1015 | >>> dq.append(1) ; dq.append(2) ; dq.append(3) |
| 1016 | >>> dq |
| 1017 | deque([1, 2, 3], maxlen=3) |
| 1018 | >>> dq.append(4) |
| 1019 | >>> dq |
| 1020 | deque([2, 3, 4], maxlen=3) |
| 1021 | |
| 1022 | (Contributed by Raymond Hettinger.) |
| 1023 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1024 | * The :mod:`ctypes` module now supports a :class:`c_bool` datatype |
| 1025 | that represents the C99 ``bool`` type. (Contributed by David Remahl.) |
| 1026 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1027 | .. Patch 1649190 |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1028 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1029 | The :mod:`ctypes` string, buffer and array types also have improved |
| 1030 | support for extended slicing syntax, |
| 1031 | where various combinations of ``(start, stop, step)`` are supplied. |
| 1032 | (Implemented by Thomas Wouters.) |
| 1033 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1034 | .. Revision 57769 |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1035 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1036 | * 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] | 1037 | 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] | 1038 | (Contributed by Fabian Kreutz.) |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1039 | :: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1040 | |
| 1041 | # Boldface text starting at y=0,x=21 |
| 1042 | # and affecting the rest of the line. |
| 1043 | stdscr.chgat(0,21, curses.A_BOLD) |
| 1044 | |
Andrew M. Kuchling | 4a2762d | 2008-01-20 00:00:38 +0000 | [diff] [blame] | 1045 | The :class:`Textbox` class in the :mod:`curses.textpad` module |
| 1046 | now supports editing in insert mode as well as overwrite mode. |
| 1047 | Insert mode is enabled by supplying a true value for the *insert_mode* |
| 1048 | parameter when creating the :class:`Textbox` instance. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1049 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1050 | * The :mod:`decimal` module was updated to version 1.66 of |
| 1051 | `the General Decimal Specification <http://www2.hursley.ibm.com/decimal/decarith.html>`__. New features |
| 1052 | include some methods for some basic mathematical functions such as |
| 1053 | :meth:`exp` and :meth:`log10`:: |
| 1054 | |
| 1055 | >>> Decimal(1).exp() |
| 1056 | Decimal("2.718281828459045235360287471") |
| 1057 | >>> Decimal("2.7182818").ln() |
| 1058 | Decimal("0.9999999895305022877376682436") |
| 1059 | >>> Decimal(1000).log10() |
| 1060 | Decimal("3") |
| 1061 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1062 | The :meth:`as_tuple` method of :class:`Decimal` objects now returns a |
| 1063 | named tuple with :attr:`sign`, :attr:`digits`, and :attr:`exponent` fields. |
| 1064 | |
| 1065 | (Implemented by Facundo Batista and Mark Dickinson. Named tuple |
| 1066 | support added by Raymond Hettinger.) |
| 1067 | |
| 1068 | * The :mod:`difflib` module's :class:`SequenceMatcher` class |
| 1069 | now returns named tuples representing matches. |
| 1070 | In addition to behaving like tuples, the returned values |
| 1071 | also have :attr:`a`, :attr:`b`, and :attr:`size` attributes. |
| 1072 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1073 | |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 1074 | * An optional ``timeout`` parameter was added to the |
| 1075 | :class:`ftplib.FTP` class constructor as well as the :meth:`connect` |
| 1076 | method, specifying a timeout measured in seconds. (Added by Facundo |
Andrew M. Kuchling | 0c3f168 | 2008-01-26 13:50:51 +0000 | [diff] [blame] | 1077 | Batista.) Also, the :class:`FTP` class's |
| 1078 | :meth:`storbinary` and :meth:`storlines` |
| 1079 | now take an optional *callback* parameter that will be called with |
| 1080 | each block of data after the data has been sent. |
| 1081 | (Contributed by Phil Schwartz.) |
| 1082 | |
| 1083 | .. Patch 1221598 |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 1084 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1085 | * The :func:`reduce` built-in function is also available in the |
| 1086 | :mod:`functools` module. In Python 3.0, the built-in is dropped and it's |
| 1087 | only available from :mod:`functools`; currently there are no plans |
| 1088 | to drop the built-in in the 2.x series. (Patched by |
| 1089 | Christian Heimes.) |
| 1090 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1091 | .. Patch 1739906 |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1092 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1093 | * The :func:`glob.glob` function can now return Unicode filenames if |
| 1094 | a Unicode path was used and Unicode filenames are matched within the directory. |
| 1095 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1096 | .. Patch #1001604 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1097 | |
| 1098 | * The :mod:`gopherlib` module has been removed. |
| 1099 | |
| 1100 | * A new function in the :mod:`heapq` module: ``merge(iter1, iter2, ...)`` |
| 1101 | takes any number of iterables that return data *in sorted order*, and returns |
| 1102 | a new iterator that returns the contents of all the iterators, also in sorted |
| 1103 | order. For example:: |
| 1104 | |
| 1105 | heapq.merge([1, 3, 5, 9], [2, 8, 16]) -> |
| 1106 | [1, 2, 3, 5, 8, 9, 16] |
| 1107 | |
| 1108 | (Contributed by Raymond Hettinger.) |
| 1109 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1110 | * An optional ``timeout`` parameter was added to the |
| 1111 | :class:`httplib.HTTPConnection` and :class:`HTTPSConnection` |
| 1112 | class constructors, specifying a timeout measured in seconds. |
| 1113 | (Added by Facundo Batista.) |
| 1114 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1115 | * Most of the :mod:`inspect` module's functions, such as |
| 1116 | :func:`getmoduleinfo` and :func:`getargs`, now return named tuples. |
| 1117 | In addition to behaving like tuples, the elements of the return value |
| 1118 | can also be accessed as attributes. |
| 1119 | (Contributed by Raymond Hettinger.) |
| 1120 | |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1121 | Some new functions in the module include |
| 1122 | :func:`isgenerator`, :func:`isgeneratorfunction`, |
| 1123 | and :func:`isabstract`. |
| 1124 | |
| 1125 | * The :mod:`itertools` module gained several new functions. |
| 1126 | |
| 1127 | ``izip_longest(iter1, iter2, ...[, fillvalue])`` makes tuples from |
| 1128 | each of the elements; if some of the iterables are shorter than |
| 1129 | others, the missing values are set to *fillvalue*. For example:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1130 | |
| 1131 | itertools.izip_longest([1,2,3], [1,2,3,4,5]) -> |
| 1132 | [(1, 1), (2, 2), (3, 3), (None, 4), (None, 5)] |
| 1133 | |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1134 | ``product(iter1, iter2, ..., [repeat=N])`` returns the Cartesian product |
| 1135 | of the supplied iterables, a set of tuples containing |
| 1136 | every possible combination of the elements returned from each iterable. :: |
| 1137 | |
| 1138 | itertools.product([1,2,3], [4,5,6]) -> |
| 1139 | [(1, 4), (1, 5), (1, 6), |
| 1140 | (2, 4), (2, 5), (2, 6), |
| 1141 | (3, 4), (3, 5), (3, 6)] |
| 1142 | |
| 1143 | The optional *repeat* keyword argument is used for taking the |
| 1144 | product of an iterable or a set of iterables with themselves, |
| 1145 | repeated *N* times. With a single iterable argument, *N*-tuples |
| 1146 | are returned:: |
| 1147 | |
| 1148 | itertools.product([1,2], repeat=3)) -> |
| 1149 | [(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), |
| 1150 | (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)] |
| 1151 | |
| 1152 | With two iterables, *2N*-tuples are returned. :: |
| 1153 | |
| 1154 | itertools(product([1,2], [3,4], repeat=2) -> |
| 1155 | [(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4), |
| 1156 | (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4), |
| 1157 | (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4), |
| 1158 | (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)] |
| 1159 | |
Andrew M. Kuchling | 1d136bb | 2008-03-06 01:36:27 +0000 | [diff] [blame^] | 1160 | ``combinations(iter, r)`` returns sub-sequences of length *r* from |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1161 | the elements of *iterable*. :: |
| 1162 | |
| 1163 | itertools.combinations('123', 2) -> |
| 1164 | [('1', '2'), ('1', '3'), ('2', '3')] |
| 1165 | |
| 1166 | itertools.combinations('123', 3) -> |
| 1167 | [('1', '2', '3')] |
| 1168 | |
| 1169 | itertools.combinations('1234', 3) -> |
| 1170 | [('1', '2', '3'), ('1', '2', '4'), ('1', '3', '4'), |
| 1171 | ('2', '3', '4')] |
| 1172 | |
Andrew M. Kuchling | 1d136bb | 2008-03-06 01:36:27 +0000 | [diff] [blame^] | 1173 | ``permutations(iter[, r])`` returns all the permutations of length *r* of |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 1174 | the iterable's elements. If *r* is not specified, it will default to the |
| 1175 | number of elements produced by the iterable. |
| 1176 | |
Andrew M. Kuchling | 1d136bb | 2008-03-06 01:36:27 +0000 | [diff] [blame^] | 1177 | itertools.permutations([1,2,3,4], 2) -> |
| 1178 | [(1, 2), (1, 3), (1, 4), |
| 1179 | (2, 1), (2, 3), (2, 4), |
| 1180 | (3, 1), (3, 2), (3, 4), |
| 1181 | (4, 1), (4, 2), (4, 3)] |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 1182 | |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1183 | ``itertools.chain(*iterables)` is an existing function in |
Andrew M. Kuchling | 1d136bb | 2008-03-06 01:36:27 +0000 | [diff] [blame^] | 1184 | :mod:`itertools` that gained a new constructor in Python 2.6. |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1185 | ``itertools.chain.from_iterable(iterable)`` takes a single |
| 1186 | iterable that should return other iterables. :func:`chain` will |
| 1187 | then return all the elements of the first iterable, then |
| 1188 | all the elements of the second, and so on. :: |
| 1189 | |
| 1190 | chain.from_iterable([[1,2,3], [4,5,6]]) -> |
| 1191 | [1, 2, 3, 4, 5, 6] |
| 1192 | |
| 1193 | (All contributed by Raymond Hettinger.) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1194 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 1195 | * The :mod:`logging` module's :class:`FileHandler` class |
| 1196 | and its subclasses :class:`WatchedFileHandler`, :class:`RotatingFileHandler`, |
| 1197 | and :class:`TimedRotatingFileHandler` now |
| 1198 | have an optional *delay* parameter to its constructor. If *delay* |
| 1199 | is true, opening of the log file is deferred until the first |
| 1200 | :meth:`emit` call is made. (Contributed by Vinay Sajip.) |
| 1201 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1202 | * The :mod:`macfs` module has been removed. This in turn required the |
| 1203 | :func:`macostools.touched` function to be removed because it depended on the |
| 1204 | :mod:`macfs` module. |
| 1205 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1206 | .. Patch #1490190 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1207 | |
Andrew M. Kuchling | 2686f4d | 2008-01-19 19:14:05 +0000 | [diff] [blame] | 1208 | * :class:`mmap` objects now have a :meth:`rfind` method that finds |
| 1209 | a substring, beginning at the end of the string and searching |
| 1210 | backwards. The :meth:`find` method |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1211 | also gained an *end* parameter containing the index at which to stop |
Andrew M. Kuchling | 2686f4d | 2008-01-19 19:14:05 +0000 | [diff] [blame] | 1212 | the forward search. |
| 1213 | (Contributed by John Lenton.) |
| 1214 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1215 | * The :mod:`new` module has been removed from Python 3.0. |
| 1216 | Importing it therefore |
| 1217 | triggers a warning message when Python is running in 3.0-warning |
| 1218 | mode. |
| 1219 | |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1220 | * The :mod:`operator` module gained a |
| 1221 | :func:`methodcaller` function that takes a name and an optional |
| 1222 | set of arguments, returning a callable that will call |
| 1223 | the named function on any arguments passed to it. For example:: |
| 1224 | |
| 1225 | >>> # Equivalent to lambda s: s.replace('old', 'new') |
| 1226 | >>> replacer = operator.methodcaller('replace', 'old', 'new') |
| 1227 | >>> replacer('old wine in old bottles') |
| 1228 | 'new wine in new bottles' |
| 1229 | |
Georg Brandl | 27504da | 2008-03-04 07:25:54 +0000 | [diff] [blame] | 1230 | (Contributed by Georg Brandl, after a suggestion by Gregory Petrosyan.) |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1231 | |
| 1232 | The :func:`attrgetter` function now accepts dotted names and performs |
| 1233 | the corresponding attribute lookups:: |
| 1234 | |
| 1235 | >>> inst_name = operator.attrgetter('__class__.__name__') |
| 1236 | >>> inst_name('') |
| 1237 | 'str' |
| 1238 | >>> inst_name(help) |
| 1239 | '_Helper' |
| 1240 | |
Georg Brandl | 27504da | 2008-03-04 07:25:54 +0000 | [diff] [blame] | 1241 | (Contributed by Georg Brandl, after a suggestion by Barry Warsaw.) |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1242 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1243 | * New functions in the :mod:`os` module include |
| 1244 | ``fchmod(fd, mode)``, ``fchown(fd, uid, gid)``, |
| 1245 | and ``lchmod(path, mode)``, on operating systems that support these |
| 1246 | functions. :func:`fchmod` and :func:`fchown` let you change the mode |
| 1247 | and ownership of an opened file, and :func:`lchmod` changes the mode |
| 1248 | of a symlink. |
| 1249 | |
| 1250 | (Contributed by Georg Brandl and Christian Heimes.) |
| 1251 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1252 | * The :func:`os.walk` function now has a ``followlinks`` parameter. If |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1253 | set to True, it will follow symlinks pointing to directories and |
| 1254 | visit the directory's contents. For backward compatibility, the |
| 1255 | parameter's default value is false. Note that the function can fall |
| 1256 | into an infinite recursion if there's a symlink that points to a |
| 1257 | parent directory. |
| 1258 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1259 | .. Patch 1273829 |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1260 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1261 | * The ``os.environ`` object's :meth:`clear` method will now unset the |
| 1262 | environment variables using :func:`os.unsetenv` in addition to clearing |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1263 | the object's keys. (Contributed by Martin Horcicka.) |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1264 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1265 | .. Patch #1181 |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1266 | |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 1267 | * In the :mod:`os.path` module, the :func:`splitext` function |
| 1268 | has been changed to not split on leading period characters. |
| 1269 | This produces better results when operating on Unix's dot-files. |
| 1270 | For example, ``os.path.splitext('.ipython')`` |
| 1271 | now returns ``('.ipython', '')`` instead of ``('', '.ipython')``. |
| 1272 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1273 | .. Bug #115886 |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 1274 | |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 1275 | A new function, :func:`relpath(path, start)` returns a relative path |
| 1276 | from the ``start`` path, if it's supplied, or from the current |
| 1277 | working directory to the destination ``path``. (Contributed by |
| 1278 | Richard Barran.) |
| 1279 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1280 | .. Patch 1339796 |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 1281 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1282 | On Windows, :func:`os.path.expandvars` will now expand environment variables |
| 1283 | in the form "%var%", and "~user" will be expanded into the |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1284 | user's home directory path. (Contributed by Josiah Carlson.) |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1285 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1286 | .. Patch 957650 |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1287 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1288 | * The Python debugger provided by the :mod:`pdb` module |
| 1289 | gained a new command: "run" restarts the Python program being debugged, |
| 1290 | and can optionally take new command-line arguments for the program. |
| 1291 | (Contributed by Rocky Bernstein.) |
| 1292 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1293 | .. Patch #1393667 |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1294 | |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1295 | * The :mod:`pickletools` module now has an :func:`optimize` function |
| 1296 | that takes a string containing a pickle and removes some unused |
| 1297 | opcodes, returning a shorter pickle that contains the same data structure. |
| 1298 | (Contributed by Raymond Hettinger.) |
| 1299 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1300 | * New functions in the :mod:`posix` module: :func:`chflags` and :func:`lchflags` |
| 1301 | are wrappers for the corresponding system calls (where they're available). |
| 1302 | Constants for the flag values are defined in the :mod:`stat` module; some |
| 1303 | possible values include :const:`UF_IMMUTABLE` to signal the file may not be |
| 1304 | changed and :const:`UF_APPEND` to indicate that data can only be appended to the |
| 1305 | file. (Contributed by M. Levinson.) |
| 1306 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 1307 | ``os.closerange(*low*, *high*)`` efficiently closes all file descriptors |
| 1308 | from *low* to *high*, ignoring any errors and not including *high* itself. |
| 1309 | This function is now used by the :mod:`subprocess` module to make starting |
| 1310 | processes faster. (Contributed by Georg Brandl.) |
| 1311 | |
| 1312 | .. Patch #1663329 |
| 1313 | |
Andrew M. Kuchling | e0a49b6 | 2008-01-08 14:30:55 +0000 | [diff] [blame] | 1314 | * The :mod:`pyexpat` module's :class:`Parser` objects now allow setting |
| 1315 | their :attr:`buffer_size` attribute to change the size of the buffer |
| 1316 | used to hold character data. |
| 1317 | (Contributed by Achim Gaedke.) |
| 1318 | |
| 1319 | .. Patch 1137 |
| 1320 | |
Andrew M. Kuchling | 0c3f168 | 2008-01-26 13:50:51 +0000 | [diff] [blame] | 1321 | * The :mod:`Queue` module now provides queue classes that retrieve entries |
| 1322 | in different orders. The :class:`PriorityQueue` class stores |
| 1323 | queued items in a heap and retrieves them in priority order, |
| 1324 | and :class:`LifoQueue` retrieves the most recently added entries first, |
| 1325 | meaning that it behaves like a stack. |
| 1326 | (Contributed by Raymond Hettinger.) |
| 1327 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1328 | * The :mod:`random` module's :class:`Random` objects can |
| 1329 | now be pickled on a 32-bit system and unpickled on a 64-bit |
| 1330 | system, and vice versa. Unfortunately, this change also means |
| 1331 | that Python 2.6's :class:`Random` objects can't be unpickled correctly |
| 1332 | on earlier versions of Python. |
| 1333 | (Contributed by Shawn Ligocki.) |
| 1334 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1335 | .. Issue 1727780 |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1336 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1337 | * Long regular expression searches carried out by the :mod:`re` |
| 1338 | module will now check for signals being delivered, so especially |
| 1339 | long searches can now be interrupted. |
| 1340 | (Contributed by Josh Hoyt and Ralf Schmitt.) |
| 1341 | |
Georg Brandl | 225163d | 2008-03-05 07:10:35 +0000 | [diff] [blame] | 1342 | .. Patch 846388 |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1343 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1344 | * The :mod:`rgbimg` module has been removed. |
| 1345 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 1346 | * The :mod:`sched` module's :class:`scheduler` instances now |
| 1347 | have a read-only :attr:`queue` attribute that returns the |
| 1348 | contents of the scheduler's queue, represented as a list of |
Georg Brandl | 225163d | 2008-03-05 07:10:35 +0000 | [diff] [blame] | 1349 | named tuples with the fields ``(time, priority, action, argument)``. |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 1350 | (Contributed by Raymond Hettinger XXX check.) |
Georg Brandl | 225163d | 2008-03-05 07:10:35 +0000 | [diff] [blame] | 1351 | |
| 1352 | .. Patch 1861 |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 1353 | |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1354 | * The :mod:`sets` module has been deprecated; it's better to |
| 1355 | use the built-in :class:`set` and :class:`frozenset` types. |
| 1356 | |
Andrew M. Kuchling | 2d60cf7 | 2007-12-22 17:27:02 +0000 | [diff] [blame] | 1357 | * Integrating signal handling with GUI handling event loops |
| 1358 | like those used by Tkinter or GTk+ has long been a problem; most |
Georg Brandl | e1b8e9c | 2008-02-20 19:12:36 +0000 | [diff] [blame] | 1359 | software ends up polling, waking up every fraction of a second. |
Andrew M. Kuchling | 2d60cf7 | 2007-12-22 17:27:02 +0000 | [diff] [blame] | 1360 | The :mod:`signal` module can now make this more efficient. |
| 1361 | Calling ``signal.set_wakeup_fd(fd)`` sets a file descriptor |
| 1362 | to be used; when a signal is received, a byte is written to that |
| 1363 | file descriptor. There's also a C-level function, |
| 1364 | :cfunc:`PySignal_SetWakeupFd`, for setting the descriptor. |
| 1365 | |
| 1366 | Event loops will use this by opening a pipe to create two descriptors, |
| 1367 | one for reading and one for writing. The writeable descriptor |
| 1368 | will be passed to :func:`set_wakeup_fd`, and the readable descriptor |
| 1369 | will be added to the list of descriptors monitored by the event loop via |
| 1370 | :cfunc:`select` or :cfunc:`poll`. |
| 1371 | On receiving a signal, a byte will be written and the main event loop |
| 1372 | will be woken up, without the need to poll. |
| 1373 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 1374 | (Contributed by Adam Olsen.) |
Andrew M. Kuchling | 2d60cf7 | 2007-12-22 17:27:02 +0000 | [diff] [blame] | 1375 | |
Georg Brandl | 225163d | 2008-03-05 07:10:35 +0000 | [diff] [blame] | 1376 | .. Patch 1583 |
Andrew M. Kuchling | 2d60cf7 | 2007-12-22 17:27:02 +0000 | [diff] [blame] | 1377 | |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1378 | The :func:`siginterrupt` function is now available from Python code, |
| 1379 | and allows changing whether signals can interrupt system calls or not. |
| 1380 | (Contributed by Ralf Schmitt.) |
| 1381 | |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 1382 | * The :mod:`smtplib` module now supports SMTP over SSL thanks to the |
| 1383 | addition of the :class:`SMTP_SSL` class. This class supports an |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 1384 | interface identical to the existing :class:`SMTP` class. Both |
| 1385 | class constructors also have an optional ``timeout`` parameter |
| 1386 | that specifies a timeout for the initial connection attempt, measured in |
| 1387 | seconds. |
| 1388 | |
| 1389 | 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] | 1390 | the module. LMTP is used in place of SMTP when transferring e-mail |
| 1391 | between agents that don't manage a mail queue. |
Andrew M. Kuchling | b4c6295 | 2007-09-01 21:18:31 +0000 | [diff] [blame] | 1392 | |
| 1393 | (SMTP over SSL contributed by Monty Taylor; timeout parameter |
| 1394 | added by Facundo Batista; LMTP implemented by Leif |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 1395 | Hedstrom.) |
| 1396 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1397 | .. Patch #957003 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1398 | |
Gregory P. Smith | 63bfc1d | 2008-01-17 07:43:20 +0000 | [diff] [blame] | 1399 | * In the :mod:`smtplib` module, SMTP.starttls() now complies with :rfc:`3207` |
| 1400 | and forgets any knowledge obtained from the server not obtained from |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 1401 | the TLS negotiation itself. (Patch contributed by Bill Fenner.) |
Gregory P. Smith | 63bfc1d | 2008-01-17 07:43:20 +0000 | [diff] [blame] | 1402 | |
| 1403 | .. Issue 829951 |
| 1404 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1405 | * The :mod:`socket` module now supports TIPC (http://tipc.sf.net), |
| 1406 | a high-performance non-IP-based protocol designed for use in clustered |
| 1407 | environments. TIPC addresses are 4- or 5-tuples. |
| 1408 | (Contributed by Alberto Bertogli.) |
| 1409 | |
| 1410 | .. Patch #1646 |
Andrew M. Kuchling | f60b641 | 2008-01-19 16:34:09 +0000 | [diff] [blame] | 1411 | |
| 1412 | * The base classes in the :mod:`SocketServer` module now support |
| 1413 | calling a :meth:`handle_timeout` method after a span of inactivity |
| 1414 | specified by the server's :attr:`timeout` attribute. (Contributed |
| 1415 | by Michael Pomraning.) |
| 1416 | |
| 1417 | .. Patch #742598 |
Andrew M. Kuchling | 1d136bb | 2008-03-06 01:36:27 +0000 | [diff] [blame^] | 1418 | |
| 1419 | * The :mod:`struct` module now supports the C99 :ctype:`_Bool` type, |
| 1420 | using the format character ``'?'``. |
| 1421 | (Contributed by David Remahl.) |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1422 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1423 | * A new variable in the :mod:`sys` module, |
Andrew M. Kuchling | 5d8b379 | 2008-01-14 14:48:43 +0000 | [diff] [blame] | 1424 | :attr:`float_info`, is an object |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1425 | containing information about the platform's floating-point support |
Andrew M. Kuchling | 5d8b379 | 2008-01-14 14:48:43 +0000 | [diff] [blame] | 1426 | derived from the :file:`float.h` file. Attributes of this object |
| 1427 | include |
| 1428 | :attr:`mant_dig` (number of digits in the mantissa), :attr:`epsilon` |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1429 | (smallest difference between 1.0 and the next largest value |
| 1430 | representable), and several others. (Contributed by Christian Heimes.) |
| 1431 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1432 | .. Patch 1534 |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1433 | |
Andrew M. Kuchling | 7b1e917 | 2008-01-15 14:38:05 +0000 | [diff] [blame] | 1434 | Another new variable, :attr:`dont_write_bytecode`, controls whether Python |
| 1435 | writes any :file:`.pyc` or :file:`.pyo` files on importing a module. |
| 1436 | If this variable is true, the compiled files are not written. The |
| 1437 | variable is initially set on start-up by supplying the :option:`-B` |
| 1438 | switch to the Python interpreter, or by setting the |
| 1439 | :envvar:`PYTHONDONTWRITEBYTECODE` environment variable before |
| 1440 | running the interpreter. Python code can subsequently |
| 1441 | change the value of this variable to control whether bytecode files |
| 1442 | are written or not. |
| 1443 | (Contributed by Neal Norwitz and Georg Brandl.) |
| 1444 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1445 | Information about the command-line arguments supplied to the Python |
| 1446 | interpreter are available as attributes of a ``sys.flags`` named |
| 1447 | tuple. For example, the :attr:`verbose` attribute is true if Python |
| 1448 | was executed in verbose mode, :attr:`debug` is true in debugging mode, etc. |
| 1449 | These attributes are all read-only. |
| 1450 | (Contributed by Christian Heimes.) |
| 1451 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 1452 | It's now possible to determine the current profiler and tracer functions |
| 1453 | by calling :func:`sys.getprofile` and :func:`sys.gettrace`. |
| 1454 | (Contributed by Georg Brandl.) |
| 1455 | |
| 1456 | .. Patch #1648 |
| 1457 | |
Andrew M. Kuchling | de37a8c | 2007-09-18 01:36:16 +0000 | [diff] [blame] | 1458 | * The :mod:`tarfile` module now supports POSIX.1-2001 (pax) and |
| 1459 | POSIX.1-1988 (ustar) format tarfiles, in addition to the GNU tar |
| 1460 | format that was already supported. The default format |
| 1461 | is GNU tar; specify the ``format`` parameter to open a file |
| 1462 | using a different format:: |
| 1463 | |
| 1464 | tar = tarfile.open("output.tar", "w", format=tarfile.PAX_FORMAT) |
| 1465 | |
| 1466 | The new ``errors`` parameter lets you specify an error handling |
| 1467 | scheme for character conversions: the three standard ways Python can |
| 1468 | handle errors ``'strict'``, ``'ignore'``, ``'replace'`` , or the |
| 1469 | special value ``'utf-8'``, which replaces bad characters with their |
| 1470 | UTF-8 representation. Character conversions occur because the PAX |
| 1471 | format supports Unicode filenames, defaulting to UTF-8 encoding. |
| 1472 | |
| 1473 | The :meth:`TarFile.add` method now accepts a ``exclude`` argument that's |
| 1474 | a function that can be used to exclude certain filenames from |
| 1475 | an archive. |
| 1476 | The function must take a filename and return true if the file |
| 1477 | should be excluded or false if it should be archived. |
| 1478 | The function is applied to both the name initially passed to :meth:`add` |
| 1479 | and to the names of files in recursively-added directories. |
| 1480 | |
| 1481 | (All changes contributed by Lars Gustäbel). |
| 1482 | |
| 1483 | * An optional ``timeout`` parameter was added to the |
| 1484 | :class:`telnetlib.Telnet` class constructor, specifying a timeout |
| 1485 | measured in seconds. (Added by Facundo Batista.) |
| 1486 | |
| 1487 | * The :class:`tempfile.NamedTemporaryFile` class usually deletes |
| 1488 | the temporary file it created when the file is closed. This |
| 1489 | behaviour can now be changed by passing ``delete=False`` to the |
| 1490 | constructor. (Contributed by Damien Miller.) |
| 1491 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1492 | .. Patch #1537850 |
Andrew M. Kuchling | de37a8c | 2007-09-18 01:36:16 +0000 | [diff] [blame] | 1493 | |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1494 | A new class, :class:`SpooledTemporaryFile`, behaves like |
| 1495 | a temporary file but stores its data in memory until a maximum size is |
| 1496 | exceeded. On reaching that limit, the contents will be written to |
| 1497 | an on-disk temporary file. (Contributed by Dustin J. Mitchell.) |
| 1498 | |
| 1499 | The :class:`NamedTemporaryFile` and :class:`SpooledTemporaryFile` classes |
| 1500 | both work as context managers, so you can write |
| 1501 | ``with tempfile.NamedTemporaryFile() as tmp: ...``. |
| 1502 | (Contributed by Alexander Belopolsky.) |
| 1503 | |
| 1504 | .. Issue #2021 |
| 1505 | |
Andrew M. Kuchling | de37a8c | 2007-09-18 01:36:16 +0000 | [diff] [blame] | 1506 | * The :mod:`test.test_support` module now contains a |
| 1507 | :func:`EnvironmentVarGuard` |
| 1508 | context manager that supports temporarily changing environment variables and |
| 1509 | automatically restores them to their old values. |
| 1510 | |
| 1511 | Another context manager, :class:`TransientResource`, can surround calls |
| 1512 | to resources that may or may not be available; it will catch and |
| 1513 | ignore a specified list of exceptions. For example, |
| 1514 | a network test may ignore certain failures when connecting to an |
| 1515 | external web site:: |
| 1516 | |
| 1517 | with test_support.TransientResource(IOError, errno=errno.ETIMEDOUT): |
| 1518 | f = urllib.urlopen('https://sf.net') |
| 1519 | ... |
| 1520 | |
| 1521 | (Contributed by Brett Cannon.) |
| 1522 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1523 | * The :mod:`textwrap` module can now preserve existing whitespace |
| 1524 | at the beginnings and ends of the newly-created lines |
| 1525 | by specifying ``drop_whitespace=False`` |
| 1526 | as an argument:: |
| 1527 | |
| 1528 | >>> S = """This sentence has a bunch of extra whitespace.""" |
| 1529 | >>> print textwrap.fill(S, width=15) |
| 1530 | This sentence |
| 1531 | has a bunch |
| 1532 | of extra |
| 1533 | whitespace. |
| 1534 | >>> print textwrap.fill(S, drop_whitespace=False, width=15) |
| 1535 | This sentence |
| 1536 | has a bunch |
| 1537 | of extra |
| 1538 | whitespace. |
| 1539 | >>> |
| 1540 | |
Georg Brandl | 27504da | 2008-03-04 07:25:54 +0000 | [diff] [blame] | 1541 | (Contributed by Dwayne Bailey.) |
| 1542 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1543 | .. Patch #1581073 |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1544 | |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 1545 | * The :mod:`timeit` module now accepts callables as well as strings |
| 1546 | for the statement being timed and for the setup code. |
| 1547 | Two convenience functions were added for creating |
| 1548 | :class:`Timer` instances: |
| 1549 | ``repeat(stmt, setup, time, repeat, number)`` and |
| 1550 | ``timeit(stmt, setup, time, number)`` create an instance and call |
| 1551 | the corresponding method. (Contributed by Erik Demaine.) |
| 1552 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1553 | .. Patch #1533909 |
Andrew M. Kuchling | 6c066dd | 2007-09-01 20:43:36 +0000 | [diff] [blame] | 1554 | |
Andrew M. Kuchling | f10878b | 2007-09-13 22:49:34 +0000 | [diff] [blame] | 1555 | * An optional ``timeout`` parameter was added to the |
| 1556 | :func:`urllib.urlopen` function and the |
| 1557 | :class:`urllib.ftpwrapper` class constructor, as well as the |
| 1558 | :func:`urllib2.urlopen` function. The parameter specifies a timeout |
| 1559 | measured in seconds. For example:: |
| 1560 | |
| 1561 | >>> u = urllib2.urlopen("http://slow.example.com", timeout=3) |
| 1562 | Traceback (most recent call last): |
| 1563 | ... |
| 1564 | urllib2.URLError: <urlopen error timed out> |
| 1565 | >>> |
| 1566 | |
| 1567 | (Added by Facundo Batista.) |
| 1568 | |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1569 | * The XML-RPC classes :class:`SimpleXMLRPCServer` and :class:`DocXMLRPCServer` |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1570 | classes can now be prevented from immediately opening and binding to |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1571 | their socket by passing True as the ``bind_and_activate`` |
| 1572 | constructor parameter. This can be used to modify the instance's |
| 1573 | :attr:`allow_reuse_address` attribute before calling the |
| 1574 | :meth:`server_bind` and :meth:`server_activate` methods to |
| 1575 | open the socket and begin listening for connections. |
| 1576 | (Contributed by Peter Parente.) |
| 1577 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1578 | .. Patch 1599845 |
Andrew M. Kuchling | 99479eb | 2007-09-25 00:09:42 +0000 | [diff] [blame] | 1579 | |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1580 | :class:`SimpleXMLRPCServer` also has a :attr:`_send_traceback_header` |
| 1581 | attribute; if true, the exception and formatted traceback are returned |
| 1582 | as HTTP headers "X-Exception" and "X-Traceback". This feature is |
| 1583 | for debugging purposes only and should not be used on production servers |
| 1584 | because the tracebacks could possibly reveal passwords or other sensitive |
| 1585 | information. (Contributed by Alan McIntyre as part of his |
| 1586 | project for Google's Summer of Code 2007.) |
| 1587 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1588 | * The :mod:`zipfile` module's :class:`ZipFile` class now has |
| 1589 | :meth:`extract` and :meth:`extractall` methods that will unpack |
| 1590 | a single file or all the files in the archive to the current directory, or |
| 1591 | to a specified directory:: |
| 1592 | |
| 1593 | z = zipfile.ZipFile('python-251.zip') |
| 1594 | |
| 1595 | # Unpack a single file, writing it relative to the /tmp directory. |
| 1596 | z.extract('Python/sysmodule.c', '/tmp') |
| 1597 | |
| 1598 | # Unpack all the files in the archive. |
| 1599 | z.extractall() |
| 1600 | |
| 1601 | (Contributed by Alan McIntyre.) |
Georg Brandl | e1b8e9c | 2008-02-20 19:12:36 +0000 | [diff] [blame] | 1602 | |
| 1603 | .. Patch 467924 |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1604 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1605 | .. ====================================================================== |
| 1606 | .. whole new modules get described in subsections here |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1607 | |
| 1608 | Improved SSL Support |
Andrew M. Kuchling | 27a4498 | 2007-10-20 19:39:35 +0000 | [diff] [blame] | 1609 | -------------------------------------------------- |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1610 | |
| 1611 | Bill Janssen made extensive improvements to Python 2.6's support for |
| 1612 | SSL. |
| 1613 | |
| 1614 | XXX use ssl.sslsocket - subclass of socket.socket. |
| 1615 | |
| 1616 | XXX Can specify if certificate is required, and obtain certificate info |
| 1617 | by calling getpeercert method. |
| 1618 | |
| 1619 | XXX sslwrap() behaves like socket.ssl |
| 1620 | |
| 1621 | XXX Certain features require the OpenSSL package to be installed, notably |
| 1622 | the 'openssl' binary. |
| 1623 | |
| 1624 | .. seealso:: |
| 1625 | |
| 1626 | SSL module documentation. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1627 | |
Andrew M. Kuchling | 0c3f168 | 2008-01-26 13:50:51 +0000 | [diff] [blame] | 1628 | |
| 1629 | .. ====================================================================== |
| 1630 | |
| 1631 | plistlib: A Property-List Parser |
| 1632 | -------------------------------------------------- |
| 1633 | |
| 1634 | A commonly-used format on MacOS X is the ``.plist`` format, |
| 1635 | which stores basic data types (numbers, strings, lists, |
| 1636 | and dictionaries) and serializes them into an XML-based format. |
| 1637 | (It's a lot like the XML-RPC serialization of data types.) |
| 1638 | |
| 1639 | Despite being primarily used on MacOS X, the format |
| 1640 | has nothing Mac-specific about it and the Python implementation works |
| 1641 | on any platform that Python supports, so the :mod:`plistlib` module |
| 1642 | has been promoted to the standard library. |
| 1643 | |
| 1644 | Using the module is simple:: |
| 1645 | |
| 1646 | import sys |
| 1647 | import plistlib |
| 1648 | import datetime |
| 1649 | |
| 1650 | # Create data structure |
| 1651 | data_struct = dict(lastAccessed=datetime.datetime.now(), |
| 1652 | version=1, |
| 1653 | categories=('Personal', 'Shared', 'Private')) |
| 1654 | |
| 1655 | # Create string containing XML. |
| 1656 | plist_str = plistlib.writePlistToString(data_struct) |
| 1657 | new_struct = plistlib.readPlistFromString(plist_str) |
| 1658 | print data_struct |
| 1659 | print new_struct |
| 1660 | |
| 1661 | # Write data structure to a file and read it back. |
| 1662 | plistlib.writePlist(data_struct, '/tmp/customizations.plist') |
| 1663 | new_struct = plistlib.readPlist('/tmp/customizations.plist') |
| 1664 | |
| 1665 | # read/writePlist accepts file-like objects as well as paths. |
| 1666 | plistlib.writePlist(data_struct, sys.stdout) |
| 1667 | |
| 1668 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1669 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1670 | |
| 1671 | |
| 1672 | Build and C API Changes |
| 1673 | ======================= |
| 1674 | |
| 1675 | Changes to Python's build process and to the C API include: |
| 1676 | |
Andrew M. Kuchling | f7b462f | 2007-11-23 13:37:39 +0000 | [diff] [blame] | 1677 | * Python 2.6 can be built with Microsoft Visual Studio 2008. |
| 1678 | See the :file:`PCbuild9` directory for the build files. |
| 1679 | (Implemented by Christian Heimes.) |
| 1680 | |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1681 | * The BerkeleyDB module now has a C API object, available as |
| 1682 | ``bsddb.db.api``. This object can be used by other C extensions |
| 1683 | that wish to use the :mod:`bsddb` module for their own purposes. |
| 1684 | (Contributed by Duncan Grisby.) |
| 1685 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1686 | .. Patch 1551895 |
Andrew M. Kuchling | 6edff59 | 2007-10-16 22:58:03 +0000 | [diff] [blame] | 1687 | |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1688 | * Several functions return information about the platform's |
| 1689 | floating-point support. :cfunc:`PyFloat_GetMax` returns |
| 1690 | the maximum representable floating point value, |
| 1691 | and :cfunc:`PyFloat_GetMin` returns the minimum |
| 1692 | positive value. :cfunc:`PyFloat_GetInfo` returns a dictionary |
| 1693 | containing more information from the :file:`float.h` file, such as |
| 1694 | ``"mant_dig"`` (number of digits in the mantissa), ``"epsilon"`` |
| 1695 | (smallest difference between 1.0 and the next largest value |
| 1696 | representable), and several others. |
Christian Heimes | d0d7d87 | 2008-01-04 02:03:25 +0000 | [diff] [blame] | 1697 | (Contributed by Christian Heimes.) |
Andrew M. Kuchling | d586559 | 2007-12-19 02:02:04 +0000 | [diff] [blame] | 1698 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1699 | .. Issue 1534 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1700 | |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1701 | * Python's C API now includes two functions for case-insensitive string |
Georg Brandl | 907a720 | 2008-02-22 12:31:45 +0000 | [diff] [blame] | 1702 | comparisons, ``PyOS_stricmp(char*, char*)`` |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1703 | and ``PyOS_strnicmp(char*, char*, Py_ssize_t)``. |
Christian Heimes | d0d7d87 | 2008-01-04 02:03:25 +0000 | [diff] [blame] | 1704 | (Contributed by Christian Heimes.) |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1705 | |
| 1706 | .. Issue 1635 |
| 1707 | |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 1708 | * Some macros were renamed in both 3.0 and 2.6 to make it clearer that |
| 1709 | they are macros, |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 1710 | not functions. :cmacro:`Py_Size()` became :cmacro:`Py_SIZE()`, |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1711 | :cmacro:`Py_Type()` became :cmacro:`Py_TYPE()`, and |
Andrew M. Kuchling | 3710a13 | 2008-03-05 00:44:41 +0000 | [diff] [blame] | 1712 | :cmacro:`Py_Refcnt()` became :cmacro:`Py_REFCNT()`. |
| 1713 | The mixed-case macros are still available |
| 1714 | in Python 2.6 for backward compatibility. |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1715 | |
Andrew M. Kuchling | 3b55470 | 2008-01-04 02:31:40 +0000 | [diff] [blame] | 1716 | .. Issue 1629 |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1717 | |
Andrew M. Kuchling | 0c3f168 | 2008-01-26 13:50:51 +0000 | [diff] [blame] | 1718 | * Distutils now places C extensions it builds in a |
| 1719 | different directory when running on a debug version of Python. |
| 1720 | (Contributed by Collin Winter.) |
| 1721 | |
| 1722 | .. Patch 1530959 |
| 1723 | |
Andrew M. Kuchling | 378586a | 2008-03-04 01:50:32 +0000 | [diff] [blame] | 1724 | * Several basic data types, such as integers and strings, maintain |
| 1725 | internal free lists of objects that can be re-used. The data |
| 1726 | structures for these free lists now follow a naming convention: the |
| 1727 | variable is always named ``free_list``, the counter is always named |
| 1728 | ``numfree``, and a macro :cmacro:`Py<typename>_MAXFREELIST` is |
| 1729 | always defined. |
Andrew M. Kuchling | 0c3f168 | 2008-01-26 13:50:51 +0000 | [diff] [blame] | 1730 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1731 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1732 | |
| 1733 | |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1734 | Port-Specific Changes: Windows |
| 1735 | ----------------------------------- |
| 1736 | |
| 1737 | * The :mod:`msvcrt` module now supports |
| 1738 | both the normal and wide char variants of the console I/O |
| 1739 | API. The :func:`getwch` function reads a keypress and returns a Unicode |
| 1740 | value, as does the :func:`getwche` function. The :func:`putwch` function |
| 1741 | takes a Unicode character and writes it to the console. |
Christian Heimes | ff6cc6b | 2008-01-17 23:01:44 +0000 | [diff] [blame] | 1742 | (Contributed by Christian Heimes.) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1743 | |
Andrew M. Kuchling | d221956 | 2008-01-17 12:00:15 +0000 | [diff] [blame] | 1744 | * :func:`os.path.expandvars` will now expand environment variables |
| 1745 | in the form "%var%", and "~user" will be expanded into the |
| 1746 | user's home directory path. (Contributed by Josiah Carlson.) |
| 1747 | |
| 1748 | * The :mod:`socket` module's socket objects now have an |
| 1749 | :meth:`ioctl` method that provides a limited interface to the |
| 1750 | :cfunc:`WSAIoctl` system interface. |
| 1751 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1752 | * The :mod:`_winreg` module now has a function, |
| 1753 | :func:`ExpandEnvironmentStrings`, |
| 1754 | that expands environment variable references such as ``%NAME%`` |
| 1755 | in an input string. The handle objects provided by this |
| 1756 | module now support the context protocol, so they can be used |
Christian Heimes | ff6cc6b | 2008-01-17 23:01:44 +0000 | [diff] [blame] | 1757 | in :keyword:`with` statements. (Contributed by Christian Heimes.) |
| 1758 | |
| 1759 | * The new default compiler on Windows is Visual Studio 2008 (VS 9.0). The |
| 1760 | build directories for Visual Studio 2003 (VS7.1) and 2005 (VS8.0) |
| 1761 | were moved into the PC/ directory. The new PCbuild directory supports |
| 1762 | cross compilation for X64, debug builds and Profile Guided Optimization |
| 1763 | (PGO). PGO builds are roughly 10% faster than normal builds. |
| 1764 | (Contributed by Christian Heimes with help from Amaury Forgeot d'Arc and |
| 1765 | Martin von Loewis.) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1766 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1767 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1768 | |
| 1769 | |
| 1770 | .. _section-other: |
| 1771 | |
| 1772 | Other Changes and Fixes |
| 1773 | ======================= |
| 1774 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1775 | As usual, there were a bunch of other improvements and bugfixes |
| 1776 | scattered throughout the source tree. A search through the change |
| 1777 | logs finds there were XXX patches applied and YYY bugs fixed between |
| 1778 | 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] | 1779 | |
| 1780 | Some of the more notable changes are: |
| 1781 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1782 | * It's now possible to prevent Python from writing any :file:`.pyc` |
| 1783 | or :file:`.pyo` files by either supplying the :option:`-B` switch |
| 1784 | or setting the :envvar:`PYTHONDONTWRITEBYTECODE` environment variable |
| 1785 | to any non-empty string when running the Python interpreter. These |
Georg Brandl | ca9c6e4 | 2008-01-15 06:58:15 +0000 | [diff] [blame] | 1786 | are also used to set the :data:`sys.dont_write_bytecode` attribute; |
| 1787 | Python code can change this variable to control whether bytecode |
| 1788 | files are subsequently written. |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1789 | (Contributed by Neal Norwitz and Georg Brandl.) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1790 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1791 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1792 | |
| 1793 | |
| 1794 | Porting to Python 2.6 |
| 1795 | ===================== |
| 1796 | |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1797 | This section lists previously described changes, and a few |
| 1798 | esoteric bugfixes, that may require changes to your |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1799 | code: |
| 1800 | |
Andrew M. Kuchling | 73835bd | 2008-01-04 18:24:41 +0000 | [diff] [blame] | 1801 | * The :meth:`__init__` method of :class:`collections.deque` |
Andrew M. Kuchling | 654ede7 | 2008-01-04 01:16:12 +0000 | [diff] [blame] | 1802 | now clears any existing contents of the deque |
| 1803 | before adding elements from the iterable. This change makes the |
| 1804 | behavior match that of ``list.__init__()``. |
| 1805 | |
Andrew M. Kuchling | 2e46355 | 2008-01-15 01:47:32 +0000 | [diff] [blame] | 1806 | * The :class:`Decimal` constructor now accepts leading and trailing |
| 1807 | whitespace when passed a string. Previously it would raise an |
| 1808 | :exc:`InvalidOperation` exception. On the other hand, the |
| 1809 | :meth:`create_decimal` method of :class:`Context` objects now |
| 1810 | explicitly disallows extra whitespace, raising a |
| 1811 | :exc:`ConversionSyntax` exception. |
| 1812 | |
| 1813 | * Due to an implementation accident, if you passed a file path to |
| 1814 | the built-in :func:`__import__` function, it would actually import |
| 1815 | the specified file. This was never intended to work, however, and |
| 1816 | the implementation now explicitly checks for this case and raises |
| 1817 | an :exc:`ImportError`. |
| 1818 | |
Andrew M. Kuchling | e34d289 | 2007-10-20 19:35:18 +0000 | [diff] [blame] | 1819 | * The :mod:`socket` module exception :exc:`socket.error` now inherits |
| 1820 | from :exc:`IOError`. Previously it wasn't a subclass of |
| 1821 | :exc:`StandardError` but now it is, through :exc:`IOError`. |
| 1822 | (Implemented by Gregory P. Smith.) |
| 1823 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1824 | .. Issue 1706815 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1825 | |
Andrew M. Kuchling | 085f75a | 2008-02-23 16:23:05 +0000 | [diff] [blame] | 1826 | * The :mod:`xmlrpclib` module no longer automatically converts |
| 1827 | :class:`datetime.date` and :class:`datetime.time` to the |
| 1828 | :class:`xmlrpclib.DateTime` type; the conversion semantics were |
| 1829 | not necessarily correct for all applications. Code using |
| 1830 | :mod:`xmlrpclib` should convert :class:`date` and :class:`time` |
| 1831 | instances. |
| 1832 | |
| 1833 | .. Issue 1330538 |
| 1834 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 1835 | .. ====================================================================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1836 | |
| 1837 | |
| 1838 | .. _acks: |
| 1839 | |
| 1840 | Acknowledgements |
| 1841 | ================ |
| 1842 | |
| 1843 | The author would like to thank the following people for offering suggestions, |
| 1844 | corrections and assistance with various drafts of this article: . |
| 1845 | |