blob: 717500ab79aad2bf35bcc56000495c620bd6524b [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001****************************
2 What's New in Python 2.6
3****************************
4
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +00005.. % XXX mention switch to reST for documentation
6.. % XXX mention switch to Roundup for bug tracking
7
Georg Brandl8ec7f652007-08-15 14:28:01 +00008:Author: A.M. Kuchling
9:Release: |release|
10:Date: |today|
11
12.. % $Id: whatsnew26.tex 55746 2007-06-02 18:33:53Z neal.norwitz $
13.. % Rules for maintenance:
14.. %
15.. % * Anyone can add text to this document. Do not spend very much time
16.. % on the wording of your changes, because your text will probably
17.. % get rewritten to some degree.
18.. %
19.. % * The maintainer will go through Misc/NEWS periodically and add
20.. % changes; it's therefore more important to add your changes to
21.. % Misc/NEWS than to this file.
22.. %
23.. % * This is not a complete list of every single change; completeness
24.. % is the purpose of Misc/NEWS. Some changes I consider too small
25.. % or esoteric to include. If such a change is added to the text,
26.. % I'll just remove it. (This is another reason you shouldn't spend
27.. % too much time on writing your addition.)
28.. %
29.. % * If you want to draw your new text to the attention of the
30.. % maintainer, add 'XXX' to the beginning of the paragraph or
31.. % section.
32.. %
33.. % * It's OK to just add a fragmentary note about a change. For
34.. % example: "XXX Describe the transmogrify() function added to the
35.. % socket module." The maintainer will research the change and
36.. % write the necessary text.
37.. %
38.. % * You can comment out your additions if you like, but it's not
39.. % necessary (especially when a final release is some months away).
40.. %
41.. % * Credit the author of a patch or bugfix. Just the name is
42.. % sufficient; the e-mail address isn't necessary.
43.. %
44.. % * It's helpful to add the bug/patch number as a comment:
45.. %
46.. % % Patch 12345
47.. % XXX Describe the transmogrify() function added to the socket
48.. % module.
49.. % (Contributed by P.Y. Developer.)
50.. %
51.. % This saves the maintainer the effort of going through the SVN log
52.. % when researching a change.
53
54This article explains the new features in Python 2.6. No release date for
55Python 2.6 has been set; it will probably be released in mid 2008.
56
57This article doesn't attempt to provide a complete specification of the new
58features, but instead provides a convenient overview. For full details, you
59should refer to the documentation for Python 2.6. If you want to understand the
60complete implementation and design rationale, refer to the PEP for a particular
61new feature.
62
63.. % Compare with previous release in 2 - 3 sentences here.
64.. % add hyperlink when the documentation becomes available online.
65
66.. % ======================================================================
67.. % Large, PEP-level features and changes should be described here.
68.. % Should there be a new section here for 3k migration?
69.. % Or perhaps a more general section describing module changes/deprecation?
70.. % sets module deprecated
71.. % ======================================================================
72
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +000073Python 3.0
74================
75
76.. % XXX add general comment about Python 3.0 features in 2.6
77
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +000078The development cycle for Python 2.6 also saw the release of the first
79alphas of Python 3.0, and the development of 3.0 has influenced
80a number of features in 2.6.
81
82Python 3.0 is a far-ranging redesign of Python that breaks
83compatibility with the 2.x series. This means that existing Python
84code will need a certain amount of conversion in order to run on
85Python 3.0. However, not all the changes in 3.0 necessarily break
86compatibility. In cases where new features won't cause existing code
87to break, they've been backported to 2.6 and are described in this
88document in the appropriate place. Some of the 3.0-derived features
89are:
90
91* A :meth:`__complex__` method for converting objects to a complex number.
92* Alternate syntax for catching exceptions: ``except TypeError as exc``.
93* The addition of :func:`functools.reduce` as a synonym for the built-in
94 :func:`reduce` function.
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +000095
96A new command-line switch, :option:`-3`, enables warnings
97about features that will be removed in Python 3.0. You can run code
98with this switch to see how much work will be necessary to port
99code to 3.0.
100
101.. seealso::
102
103 The 3xxx series of PEPs, which describes the development process for
104 Python 3.0 and various features that have been accepted, rejected,
105 or are still under consideration.
106
107PEP 343: The 'with' statement
108=============================
109
110The previous version, Python 2.5, added the ':keyword:`with`'
111statement an optional feature, to be enabled by a ``from __future__
112import generators`` directive. In 2.6 the statement no longer need to
113be specially enabled; this means that :keyword:`with` is now always a
114keyword. The rest of this section is a copy of the corresponding
115section from "What's New in Python 2.5" document; if you read
116it back when Python 2.5 came out, you can skip the rest of this
117section.
118
119The ':keyword:`with`' statement clarifies code that previously would use
120``try...finally`` blocks to ensure that clean-up code is executed. In this
121section, I'll discuss the statement as it will commonly be used. In the next
122section, I'll examine the implementation details and show how to write objects
123for use with this statement.
124
125The ':keyword:`with`' statement is a new control-flow structure whose basic
126structure is::
127
128 with expression [as variable]:
129 with-block
130
131The expression is evaluated, and it should result in an object that supports the
132context management protocol (that is, has :meth:`__enter__` and :meth:`__exit__`
133methods.
134
135The object's :meth:`__enter__` is called before *with-block* is executed and
136therefore can run set-up code. It also may return a value that is bound to the
137name *variable*, if given. (Note carefully that *variable* is *not* assigned
138the result of *expression*.)
139
140After execution of the *with-block* is finished, the object's :meth:`__exit__`
141method is called, even if the block raised an exception, and can therefore run
142clean-up code.
143
144Some standard Python objects now support the context management protocol and can
145be used with the ':keyword:`with`' statement. File objects are one example::
146
147 with open('/etc/passwd', 'r') as f:
148 for line in f:
149 print line
150 ... more processing code ...
151
152After this statement has executed, the file object in *f* will have been
153automatically closed, even if the :keyword:`for` loop raised an exception part-
154way through the block.
155
156.. note::
157
158 In this case, *f* is the same object created by :func:`open`, because
159 :meth:`file.__enter__` returns *self*.
160
161The :mod:`threading` module's locks and condition variables also support the
162':keyword:`with`' statement::
163
164 lock = threading.Lock()
165 with lock:
166 # Critical section of code
167 ...
168
169The lock is acquired before the block is executed and always released once the
170block is complete.
171
172The new :func:`localcontext` function in the :mod:`decimal` module makes it easy
173to save and restore the current decimal context, which encapsulates the desired
174precision and rounding characteristics for computations::
175
176 from decimal import Decimal, Context, localcontext
177
178 # Displays with default precision of 28 digits
179 v = Decimal('578')
180 print v.sqrt()
181
182 with localcontext(Context(prec=16)):
183 # All code in this block uses a precision of 16 digits.
184 # The original context is restored on exiting the block.
185 print v.sqrt()
186
187
188.. _new-26-context-managers:
189
190Writing Context Managers
191------------------------
192
193Under the hood, the ':keyword:`with`' statement is fairly complicated. Most
194people will only use ':keyword:`with`' in company with existing objects and
195don't need to know these details, so you can skip the rest of this section if
196you like. Authors of new objects will need to understand the details of the
197underlying implementation and should keep reading.
198
199A high-level explanation of the context management protocol is:
200
201* The expression is evaluated and should result in an object called a "context
202 manager". The context manager must have :meth:`__enter__` and :meth:`__exit__`
203 methods.
204
205* The context manager's :meth:`__enter__` method is called. The value returned
206 is assigned to *VAR*. If no ``'as VAR'`` clause is present, the value is simply
207 discarded.
208
209* The code in *BLOCK* is executed.
210
211* If *BLOCK* raises an exception, the :meth:`__exit__(type, value, traceback)`
212 is called with the exception details, the same values returned by
213 :func:`sys.exc_info`. The method's return value controls whether the exception
214 is re-raised: any false value re-raises the exception, and ``True`` will result
215 in suppressing it. You'll only rarely want to suppress the exception, because
216 if you do the author of the code containing the ':keyword:`with`' statement will
217 never realize anything went wrong.
218
219* If *BLOCK* didn't raise an exception, the :meth:`__exit__` method is still
220 called, but *type*, *value*, and *traceback* are all ``None``.
221
222Let's think through an example. I won't present detailed code but will only
223sketch the methods necessary for a database that supports transactions.
224
225(For people unfamiliar with database terminology: a set of changes to the
226database are grouped into a transaction. Transactions can be either committed,
227meaning that all the changes are written into the database, or rolled back,
228meaning that the changes are all discarded and the database is unchanged. See
229any database textbook for more information.)
230
231Let's assume there's an object representing a database connection. Our goal will
232be to let the user write code like this::
233
234 db_connection = DatabaseConnection()
235 with db_connection as cursor:
236 cursor.execute('insert into ...')
237 cursor.execute('delete from ...')
238 # ... more operations ...
239
240The transaction should be committed if the code in the block runs flawlessly or
241rolled back if there's an exception. Here's the basic interface for
242:class:`DatabaseConnection` that I'll assume::
243
244 class DatabaseConnection:
245 # Database interface
246 def cursor (self):
247 "Returns a cursor object and starts a new transaction"
248 def commit (self):
249 "Commits current transaction"
250 def rollback (self):
251 "Rolls back current transaction"
252
253The :meth:`__enter__` method is pretty easy, having only to start a new
254transaction. For this application the resulting cursor object would be a useful
255result, so the method will return it. The user can then add ``as cursor`` to
256their ':keyword:`with`' statement to bind the cursor to a variable name. ::
257
258 class DatabaseConnection:
259 ...
260 def __enter__ (self):
261 # Code to start a new transaction
262 cursor = self.cursor()
263 return cursor
264
265The :meth:`__exit__` method is the most complicated because it's where most of
266the work has to be done. The method has to check if an exception occurred. If
267there was no exception, the transaction is committed. The transaction is rolled
268back if there was an exception.
269
270In the code below, execution will just fall off the end of the function,
271returning the default value of ``None``. ``None`` is false, so the exception
272will be re-raised automatically. If you wished, you could be more explicit and
273add a :keyword:`return` statement at the marked location. ::
274
275 class DatabaseConnection:
276 ...
277 def __exit__ (self, type, value, tb):
278 if tb is None:
279 # No exception, so commit
280 self.commit()
281 else:
282 # Exception occurred, so rollback.
283 self.rollback()
284 # return False
285
286
287.. _module-contextlib:
288
289The contextlib module
290---------------------
291
292The new :mod:`contextlib` module provides some functions and a decorator that
293are useful for writing objects for use with the ':keyword:`with`' statement.
294
295The decorator is called :func:`contextmanager`, and lets you write a single
296generator function instead of defining a new class. The generator should yield
297exactly one value. The code up to the :keyword:`yield` will be executed as the
298:meth:`__enter__` method, and the value yielded will be the method's return
299value that will get bound to the variable in the ':keyword:`with`' statement's
300:keyword:`as` clause, if any. The code after the :keyword:`yield` will be
301executed in the :meth:`__exit__` method. Any exception raised in the block will
302be raised by the :keyword:`yield` statement.
303
304Our database example from the previous section could be written using this
305decorator as::
306
307 from contextlib import contextmanager
308
309 @contextmanager
310 def db_transaction (connection):
311 cursor = connection.cursor()
312 try:
313 yield cursor
314 except:
315 connection.rollback()
316 raise
317 else:
318 connection.commit()
319
320 db = DatabaseConnection()
321 with db_transaction(db) as cursor:
322 ...
323
324The :mod:`contextlib` module also has a :func:`nested(mgr1, mgr2, ...)` function
325that combines a number of context managers so you don't need to write nested
326':keyword:`with`' statements. In this example, the single ':keyword:`with`'
327statement both starts a database transaction and acquires a thread lock::
328
329 lock = threading.Lock()
330 with nested (db_transaction(db), lock) as (cursor, locked):
331 ...
332
333Finally, the :func:`closing(object)` function returns *object* so that it can be
334bound to a variable, and calls ``object.close`` at the end of the block. ::
335
336 import urllib, sys
337 from contextlib import closing
338
339 with closing(urllib.urlopen('http://www.yahoo.com')) as f:
340 for line in f:
341 sys.stdout.write(line)
342
343
344.. seealso::
345
346 :pep:`343` - The "with" statement
347 PEP written by Guido van Rossum and Nick Coghlan; implemented by Mike Bland,
348 Guido van Rossum, and Neal Norwitz. The PEP shows the code generated for a
349 ':keyword:`with`' statement, which can be helpful in learning how the statement
350 works.
351
352 The documentation for the :mod:`contextlib` module.
353
354.. % ======================================================================
355
356.. _pep-3110:
357
358PEP 3110: Exception-Handling Changes
359=====================================================
360
361One error that Python programmers occasionally make
362is the following::
363
364 try:
365 ...
366 except TypeError, ValueError:
367 ...
368
369The author is probably trying to catch both
370:exc:`TypeError` and :exc:`ValueError` exceptions, but this code
371actually does something different: it will catch
372:exc:`TypeError` and bind the resulting exception object
373to the local name ``"ValueError"``. The correct code
374would have specified a tuple::
375
376 try:
377 ...
378 except (TypeError, ValueError):
379 ...
380
381This error is possible because the use of the comma here is ambiguous:
382does it indicate two different nodes in the parse tree, or a single
383node that's a tuple.
384
385Python 3.0 changes the syntax to make this unambiguous by replacing
386the comma with the word "as". To catch an exception and store the
387exception object in the variable ``exc``, you must write::
388
389 try:
390 ...
391 except TypeError as exc:
392 ...
393
394Python 3.0 will only support the use of "as", and therefore interprets
395the first example as catching two different exceptions. Python 2.6
396supports both the comma and "as", so existing code will continue to
397work.
398
399.. seealso::
400
401 :pep:`3110` - Catching Exceptions in Python 3000
402 PEP written and implemented by Collin Winter.
403
404.. % ======================================================================
405
406.. _pep-3119:
407
408PEP 3119: Abstract Base Classes
409=====================================================
410
411XXX
412
413.. seealso::
414
415 :pep:`3119` - Introducing Abstract Base Classes
416 PEP written by Guido van Rossum and Talin.
417 Implemented by XXX.
418 Backported to 2.6 by Benjamin Aranguren (with Alex Martelli).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000419
420Other Language Changes
421======================
422
423Here are all of the changes that Python 2.6 makes to the core Python language.
424
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000425* When calling a function using the ``**`` syntax to provide keyword
426 arguments, you are no longer required to use a Python dictionary;
427 any mapping will now work::
428
429 >>> def f(**kw):
430 ... print sorted(kw)
431 ...
432 >>> ud=UserDict.UserDict()
433 >>> ud['a'] = 1
434 >>> ud['b'] = 'string'
435 >>> f(**ud)
436 ['a', 'b']
437
438 .. % Patch 1686487
439
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000440* The built-in types now have improved support for extended slicing syntax,
441 where various combinations of ``(start, stop, step)`` are supplied.
442 Previously, the support was partial and certain corner cases wouldn't work.
443 (Implemented by Thomas Wouters.)
444
445 .. % Revision 57619
446
447* C functions and methods that use
448 :cfunc:`PyComplex_AsCComplex` will now accept arguments that
449 have a :meth:`__complex__` method. In particular, the functions in the
450 :mod:`cmath` module will now accept objects with this method.
451 This is a backport of a Python 3.0 change.
452 (Contributed by Mark Dickinson.)
453
454 .. % Patch #1675423
455
456* Changes to the :class:`Exception` interface
457 as dictated by :pep:`352` continue to be made. For 2.6,
458 the :attr:`message` attribute is being deprecated in favor of the
459 :attr:`args` attribute.
460
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000461* The :func:`compile` built-in function now accepts keyword arguments
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000462 as well as positional parameters. (Contributed by Thomas Wouters.)
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000463
464 .. % Patch 1444529
465
Andrew M. Kuchling6c066dd2007-09-01 20:43:36 +0000466* The :func:`complex` constructor now accepts strings containing
467 parenthesized complex numbers, letting ``complex(repr(cmplx))``
468 will now round-trip values. For example, ``complex('(3+4j)')``
469 now returns the value (3+4j).
470
471 .. % Patch 1491866
472
Andrew M. Kuchlingb4c62952007-09-01 21:18:31 +0000473* The string :meth:`translate` method now accepts ``None`` as the
474 translation table parameter, which is treated as the identity
475 transformation. This makes it easier to carry out operations
476 that only delete characters. (Contributed by Bengt Richter.)
477
478 .. % Patch 1193128
479
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000480* The built-in :func:`dir` function now checks for a :meth:`__dir__`
481 method on the objects it receives. This method must return a list
482 of strings containing the names of valid attributes for the object,
483 and lets the object control the value that :func:`dir` produces.
484 Objects that have :meth:`__getattr__` or :meth:`__getattribute__`
485 methods.
486
487 .. % Patch 1591665
488
Georg Brandl8ec7f652007-08-15 14:28:01 +0000489* An obscure change: when you use the the :func:`locals` function inside a
490 :keyword:`class` statement, the resulting dictionary no longer returns free
491 variables. (Free variables, in this case, are variables referred to in the
492 :keyword:`class` statement that aren't attributes of the class.)
493
494.. % ======================================================================
495
496
497Optimizations
498-------------
499
500* Internally, a bit is now set in type objects to indicate some of the standard
501 built-in types. This speeds up checking if an object is a subclass of one of
502 these types. (Contributed by Neal Norwitz.)
503
504The net result of the 2.6 optimizations is that Python 2.6 runs the pystone
505benchmark around XX% faster than Python 2.5.
506
507.. % ======================================================================
508
509
510New, Improved, and Deprecated Modules
511=====================================
512
513As usual, Python's standard library received a number of enhancements and bug
514fixes. Here's a partial list of the most notable changes, sorted alphabetically
515by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more
516complete list of changes, or look through the CVS logs for all the details.
517
518* A new data type in the :mod:`collections` module: :class:`NamedTuple(typename,
519 fieldnames)` is a factory function that creates subclasses of the standard tuple
520 whose fields are accessible by name as well as index. For example::
521
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000522 >>> var_type = collections.NamedTuple('variable',
523 ... 'id name type size')
524 # Names are separated by spaces or commas.
525 # 'id, name, type, size' would also work.
526 >>> var_type.__fields__
527 ('id', 'name', 'type', 'size')
Georg Brandl8ec7f652007-08-15 14:28:01 +0000528
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000529 >>> var = var_type(1, 'frequency', 'int', 4)
530 >>> print var[0], var.id # Equivalent
531 1 1
532 >>> print var[2], var.type # Equivalent
533 int int
534 >>> v2 = var.__replace__('name', 'amplitude')
535 >>> v2
536 variable(id=1, name='amplitude', type='int', size=4)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000537
538 (Contributed by Raymond Hettinger.)
539
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000540* The :mod:`ctypes` module now supports a :class:`c_bool` datatype
541 that represents the C99 ``bool`` type. (Contributed by David Remahl.)
542
543 .. % Patch 1649190
544
Georg Brandl8ec7f652007-08-15 14:28:01 +0000545* A new method in the :mod:`curses` module: for a window, :meth:`chgat` changes
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000546 the display characters for a certain number of characters on a single line.
547 ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000548
549 # Boldface text starting at y=0,x=21
550 # and affecting the rest of the line.
551 stdscr.chgat(0,21, curses.A_BOLD)
552
553 (Contributed by Fabian Kreutz.)
554
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000555* The :mod:`decimal` module was updated to version 1.66 of
556 `the General Decimal Specification <http://www2.hursley.ibm.com/decimal/decarith.html>`__. New features
557 include some methods for some basic mathematical functions such as
558 :meth:`exp` and :meth:`log10`::
559
560 >>> Decimal(1).exp()
561 Decimal("2.718281828459045235360287471")
562 >>> Decimal("2.7182818").ln()
563 Decimal("0.9999999895305022877376682436")
564 >>> Decimal(1000).log10()
565 Decimal("3")
566
567 (Implemented by Facundo Batista and Mark Dickinson.)
568
Andrew M. Kuchlingb4c62952007-09-01 21:18:31 +0000569* An optional ``timeout`` parameter was added to the
570 :class:`ftplib.FTP` class constructor as well as the :meth:`connect`
571 method, specifying a timeout measured in seconds. (Added by Facundo
572 Batista.)
573
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000574* The :func:`reduce` built-in function is also available in the
575 :mod:`functools` module. In Python 3.0, the built-in is dropped and it's
576 only available from :mod:`functools`; currently there are no plans
577 to drop the built-in in the 2.x series. (Patched by
578 Christian Heimes.)
579
580 .. % Patch 1739906
581
Georg Brandl8ec7f652007-08-15 14:28:01 +0000582* The :func:`glob.glob` function can now return Unicode filenames if
583 a Unicode path was used and Unicode filenames are matched within the directory.
584
585 .. % Patch #1001604
586
587* The :mod:`gopherlib` module has been removed.
588
589* A new function in the :mod:`heapq` module: ``merge(iter1, iter2, ...)``
590 takes any number of iterables that return data *in sorted order*, and returns
591 a new iterator that returns the contents of all the iterators, also in sorted
592 order. For example::
593
594 heapq.merge([1, 3, 5, 9], [2, 8, 16]) ->
595 [1, 2, 3, 5, 8, 9, 16]
596
597 (Contributed by Raymond Hettinger.)
598
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000599* An optional ``timeout`` parameter was added to the
600 :class:`httplib.HTTPConnection` and :class:`HTTPSConnection`
601 class constructors, specifying a timeout measured in seconds.
602 (Added by Facundo Batista.)
603
Georg Brandl8ec7f652007-08-15 14:28:01 +0000604* A new function in the :mod:`itertools` module: ``izip_longest(iter1, iter2,
605 ...[, fillvalue])`` makes tuples from each of the elements; if some of the
606 iterables are shorter than others, the missing values are set to *fillvalue*.
607 For example::
608
609 itertools.izip_longest([1,2,3], [1,2,3,4,5]) ->
610 [(1, 1), (2, 2), (3, 3), (None, 4), (None, 5)]
611
612 (Contributed by Raymond Hettinger.)
613
614* The :mod:`macfs` module has been removed. This in turn required the
615 :func:`macostools.touched` function to be removed because it depended on the
616 :mod:`macfs` module.
617
618 .. % Patch #1490190
619
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000620* The :func:`os.walk` function now has a ``followlinks`` parameter. If
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000621 set to True, it will follow symlinks pointing to directories and
622 visit the directory's contents. For backward compatibility, the
623 parameter's default value is false. Note that the function can fall
624 into an infinite recursion if there's a symlink that points to a
625 parent directory.
626
627 .. % Patch 1273829
628
Andrew M. Kuchling6c066dd2007-09-01 20:43:36 +0000629* In the :mod:`os.path` module, the :func:`splitext` function
630 has been changed to not split on leading period characters.
631 This produces better results when operating on Unix's dot-files.
632 For example, ``os.path.splitext('.ipython')``
633 now returns ``('.ipython', '')`` instead of ``('', '.ipython')``.
634
635 .. % Bug #115886
636
Andrew M. Kuchlingb4c62952007-09-01 21:18:31 +0000637 A new function, :func:`relpath(path, start)` returns a relative path
638 from the ``start`` path, if it's supplied, or from the current
639 working directory to the destination ``path``. (Contributed by
640 Richard Barran.)
641
642 .. % Patch 1339796
643
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000644 On Windows, :func:`os.path.expandvars` will now expand environment variables
645 in the form "%var%", and "~user" will be expanded into the
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000646 user's home directory path. (Contributed by Josiah Carlson.)
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000647
648 .. % Patch 957650
649
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000650* The Python debugger provided by the :mod:`pdb` module
651 gained a new command: "run" restarts the Python program being debugged,
652 and can optionally take new command-line arguments for the program.
653 (Contributed by Rocky Bernstein.)
654
655 .. % Patch #1393667
656
Georg Brandl8ec7f652007-08-15 14:28:01 +0000657* New functions in the :mod:`posix` module: :func:`chflags` and :func:`lchflags`
658 are wrappers for the corresponding system calls (where they're available).
659 Constants for the flag values are defined in the :mod:`stat` module; some
660 possible values include :const:`UF_IMMUTABLE` to signal the file may not be
661 changed and :const:`UF_APPEND` to indicate that data can only be appended to the
662 file. (Contributed by M. Levinson.)
663
664* The :mod:`rgbimg` module has been removed.
665
Andrew M. Kuchling6c066dd2007-09-01 20:43:36 +0000666* The :mod:`smtplib` module now supports SMTP over SSL thanks to the
667 addition of the :class:`SMTP_SSL` class. This class supports an
Andrew M. Kuchlingb4c62952007-09-01 21:18:31 +0000668 interface identical to the existing :class:`SMTP` class. Both
669 class constructors also have an optional ``timeout`` parameter
670 that specifies a timeout for the initial connection attempt, measured in
671 seconds.
672
673 An implementation of the LMTP protocol (:rfc:`2033`) was also added to
Andrew M. Kuchling6c066dd2007-09-01 20:43:36 +0000674 the module. LMTP is used in place of SMTP when transferring e-mail
675 between agents that don't manage a mail queue.
Andrew M. Kuchlingb4c62952007-09-01 21:18:31 +0000676
677 (SMTP over SSL contributed by Monty Taylor; timeout parameter
678 added by Facundo Batista; LMTP implemented by Leif
Andrew M. Kuchling6c066dd2007-09-01 20:43:36 +0000679 Hedstrom.)
680
681 .. % Patch #957003
Georg Brandl8ec7f652007-08-15 14:28:01 +0000682
Andrew M. Kuchlingde37a8c2007-09-18 01:36:16 +0000683* The :mod:`tarfile` module now supports POSIX.1-2001 (pax) and
684 POSIX.1-1988 (ustar) format tarfiles, in addition to the GNU tar
685 format that was already supported. The default format
686 is GNU tar; specify the ``format`` parameter to open a file
687 using a different format::
688
689 tar = tarfile.open("output.tar", "w", format=tarfile.PAX_FORMAT)
690
691 The new ``errors`` parameter lets you specify an error handling
692 scheme for character conversions: the three standard ways Python can
693 handle errors ``'strict'``, ``'ignore'``, ``'replace'`` , or the
694 special value ``'utf-8'``, which replaces bad characters with their
695 UTF-8 representation. Character conversions occur because the PAX
696 format supports Unicode filenames, defaulting to UTF-8 encoding.
697
698 The :meth:`TarFile.add` method now accepts a ``exclude`` argument that's
699 a function that can be used to exclude certain filenames from
700 an archive.
701 The function must take a filename and return true if the file
702 should be excluded or false if it should be archived.
703 The function is applied to both the name initially passed to :meth:`add`
704 and to the names of files in recursively-added directories.
705
706 (All changes contributed by Lars Gustäbel).
707
708* An optional ``timeout`` parameter was added to the
709 :class:`telnetlib.Telnet` class constructor, specifying a timeout
710 measured in seconds. (Added by Facundo Batista.)
711
712* The :class:`tempfile.NamedTemporaryFile` class usually deletes
713 the temporary file it created when the file is closed. This
714 behaviour can now be changed by passing ``delete=False`` to the
715 constructor. (Contributed by Damien Miller.)
716
717 .. % Patch #1537850
718
719* The :mod:`test.test_support` module now contains a
720 :func:`EnvironmentVarGuard`
721 context manager that supports temporarily changing environment variables and
722 automatically restores them to their old values.
723
724 Another context manager, :class:`TransientResource`, can surround calls
725 to resources that may or may not be available; it will catch and
726 ignore a specified list of exceptions. For example,
727 a network test may ignore certain failures when connecting to an
728 external web site::
729
730 with test_support.TransientResource(IOError, errno=errno.ETIMEDOUT):
731 f = urllib.urlopen('https://sf.net')
732 ...
733
734 (Contributed by Brett Cannon.)
735
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000736* The :mod:`textwrap` module can now preserve existing whitespace
737 at the beginnings and ends of the newly-created lines
738 by specifying ``drop_whitespace=False``
739 as an argument::
740
741 >>> S = """This sentence has a bunch of extra whitespace."""
742 >>> print textwrap.fill(S, width=15)
743 This sentence
744 has a bunch
745 of extra
746 whitespace.
747 >>> print textwrap.fill(S, drop_whitespace=False, width=15)
748 This sentence
749 has a bunch
750 of extra
751 whitespace.
752 >>>
753
754 .. % Patch #1581073
755
Andrew M. Kuchling6c066dd2007-09-01 20:43:36 +0000756* The :mod:`timeit` module now accepts callables as well as strings
757 for the statement being timed and for the setup code.
758 Two convenience functions were added for creating
759 :class:`Timer` instances:
760 ``repeat(stmt, setup, time, repeat, number)`` and
761 ``timeit(stmt, setup, time, number)`` create an instance and call
762 the corresponding method. (Contributed by Erik Demaine.)
763
764 .. % Patch #1533909
765
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000766* An optional ``timeout`` parameter was added to the
767 :func:`urllib.urlopen` function and the
768 :class:`urllib.ftpwrapper` class constructor, as well as the
769 :func:`urllib2.urlopen` function. The parameter specifies a timeout
770 measured in seconds. For example::
771
772 >>> u = urllib2.urlopen("http://slow.example.com", timeout=3)
773 Traceback (most recent call last):
774 ...
775 urllib2.URLError: <urlopen error timed out>
776 >>>
777
778 (Added by Facundo Batista.)
779
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000780* The XML-RPC classes :class:`SimpleXMLRPCServer` and :class:`DocXMLRPCServer`
781 classes can now be preventing from immediately opening and binding to
782 their socket by passing True as the ``bind_and_activate``
783 constructor parameter. This can be used to modify the instance's
784 :attr:`allow_reuse_address` attribute before calling the
785 :meth:`server_bind` and :meth:`server_activate` methods to
786 open the socket and begin listening for connections.
787 (Contributed by Peter Parente.)
788
789 .. % Patch 1599845
790
Georg Brandl8ec7f652007-08-15 14:28:01 +0000791.. % ======================================================================
792.. % whole new modules get described in \subsections here
793
794.. % ======================================================================
795
796
797Build and C API Changes
798=======================
799
800Changes to Python's build process and to the C API include:
801
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000802* Detailed changes will be listed here.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000803
804.. % ======================================================================
805
806
807Port-Specific Changes
808---------------------
809
810Platform-specific changes go here.
811
812.. % ======================================================================
813
814
815.. _section-other:
816
817Other Changes and Fixes
818=======================
819
820As usual, there were a bunch of other improvements and bugfixes scattered
821throughout the source tree. A search through the change logs finds there were
822XXX patches applied and YYY bugs fixed between Python 2.5 and 2.6. Both figures
823are likely to be underestimates.
824
825Some of the more notable changes are:
826
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000827* Details will go here.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000828
829.. % ======================================================================
830
831
832Porting to Python 2.6
833=====================
834
835This section lists previously described changes that may require changes to your
836code:
837
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000838* The :mod:`socket` module exception :exc:`socket.error` now inherits from
839 :exc:`IOError`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000840
841.. % ======================================================================
842
843
844.. _acks:
845
846Acknowledgements
847================
848
849The author would like to thank the following people for offering suggestions,
850corrections and assistance with various drafts of this article: .
851