blob: a2ce08a783f44894afe65e83a72a27467b8effc8 [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
Andrew M. Kuchling6edff592007-10-16 22:58:03 +0000518* The :mod:`bsddb.dbshelve` module now uses the highest pickling protocol
519 available, instead of restricting itself to protocol 1.
520 (Contributed by W. Barnes.)
521
522 .. % Patch 1551443
523
Andrew M. Kuchling4b3074c2007-10-08 23:23:03 +0000524* A new data type in the :mod:`collections` module: :class:`named_tuple(typename,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000525 fieldnames)` is a factory function that creates subclasses of the standard tuple
526 whose fields are accessible by name as well as index. For example::
527
Andrew M. Kuchling4b3074c2007-10-08 23:23:03 +0000528 >>> var_type = collections.named_tuple('variable',
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000529 ... 'id name type size')
530 # Names are separated by spaces or commas.
531 # 'id, name, type, size' would also work.
532 >>> var_type.__fields__
533 ('id', 'name', 'type', 'size')
Georg Brandl8ec7f652007-08-15 14:28:01 +0000534
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000535 >>> var = var_type(1, 'frequency', 'int', 4)
536 >>> print var[0], var.id # Equivalent
537 1 1
538 >>> print var[2], var.type # Equivalent
539 int int
Andrew M. Kuchling6edff592007-10-16 22:58:03 +0000540 >>> var.__asdict__()
541 {'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'}
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000542 >>> v2 = var.__replace__('name', 'amplitude')
543 >>> v2
544 variable(id=1, name='amplitude', type='int', size=4)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000545
546 (Contributed by Raymond Hettinger.)
547
Andrew M. Kuchling6edff592007-10-16 22:58:03 +0000548* Another change to the :mod:`collections` module is that the
549 :class:`deque` type now supports an optional `maxlen` parameter;
550 if supplied, the deque's size will be restricted to no more
551 than ``maxlen`` items. Adding more items to a full deque causes
552 old items to be discarded.
553
554 ::
555
556 >>> from collections import deque
557 >>> dq=deque(maxlen=3)
558 >>> dq
559 deque([], maxlen=3)
560 >>> dq.append(1) ; dq.append(2) ; dq.append(3)
561 >>> dq
562 deque([1, 2, 3], maxlen=3)
563 >>> dq.append(4)
564 >>> dq
565 deque([2, 3, 4], maxlen=3)
566
567 (Contributed by Raymond Hettinger.)
568
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000569* The :mod:`ctypes` module now supports a :class:`c_bool` datatype
570 that represents the C99 ``bool`` type. (Contributed by David Remahl.)
571
572 .. % Patch 1649190
573
Andrew M. Kuchling6edff592007-10-16 22:58:03 +0000574 The :mod:`ctypes` string, buffer and array types also have improved
575 support for extended slicing syntax,
576 where various combinations of ``(start, stop, step)`` are supplied.
577 (Implemented by Thomas Wouters.)
578
579 .. % Revision 57769
580
581
Georg Brandl8ec7f652007-08-15 14:28:01 +0000582* A new method in the :mod:`curses` module: for a window, :meth:`chgat` changes
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000583 the display characters for a certain number of characters on a single line.
584 ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000585
586 # Boldface text starting at y=0,x=21
587 # and affecting the rest of the line.
588 stdscr.chgat(0,21, curses.A_BOLD)
589
590 (Contributed by Fabian Kreutz.)
591
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000592* The :mod:`decimal` module was updated to version 1.66 of
593 `the General Decimal Specification <http://www2.hursley.ibm.com/decimal/decarith.html>`__. New features
594 include some methods for some basic mathematical functions such as
595 :meth:`exp` and :meth:`log10`::
596
597 >>> Decimal(1).exp()
598 Decimal("2.718281828459045235360287471")
599 >>> Decimal("2.7182818").ln()
600 Decimal("0.9999999895305022877376682436")
601 >>> Decimal(1000).log10()
602 Decimal("3")
603
604 (Implemented by Facundo Batista and Mark Dickinson.)
605
Andrew M. Kuchlingb4c62952007-09-01 21:18:31 +0000606* An optional ``timeout`` parameter was added to the
607 :class:`ftplib.FTP` class constructor as well as the :meth:`connect`
608 method, specifying a timeout measured in seconds. (Added by Facundo
609 Batista.)
610
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000611* The :func:`reduce` built-in function is also available in the
612 :mod:`functools` module. In Python 3.0, the built-in is dropped and it's
613 only available from :mod:`functools`; currently there are no plans
614 to drop the built-in in the 2.x series. (Patched by
615 Christian Heimes.)
616
617 .. % Patch 1739906
618
Georg Brandl8ec7f652007-08-15 14:28:01 +0000619* The :func:`glob.glob` function can now return Unicode filenames if
620 a Unicode path was used and Unicode filenames are matched within the directory.
621
622 .. % Patch #1001604
623
624* The :mod:`gopherlib` module has been removed.
625
626* A new function in the :mod:`heapq` module: ``merge(iter1, iter2, ...)``
627 takes any number of iterables that return data *in sorted order*, and returns
628 a new iterator that returns the contents of all the iterators, also in sorted
629 order. For example::
630
631 heapq.merge([1, 3, 5, 9], [2, 8, 16]) ->
632 [1, 2, 3, 5, 8, 9, 16]
633
634 (Contributed by Raymond Hettinger.)
635
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000636* An optional ``timeout`` parameter was added to the
637 :class:`httplib.HTTPConnection` and :class:`HTTPSConnection`
638 class constructors, specifying a timeout measured in seconds.
639 (Added by Facundo Batista.)
640
Georg Brandl8ec7f652007-08-15 14:28:01 +0000641* A new function in the :mod:`itertools` module: ``izip_longest(iter1, iter2,
642 ...[, fillvalue])`` makes tuples from each of the elements; if some of the
643 iterables are shorter than others, the missing values are set to *fillvalue*.
644 For example::
645
646 itertools.izip_longest([1,2,3], [1,2,3,4,5]) ->
647 [(1, 1), (2, 2), (3, 3), (None, 4), (None, 5)]
648
649 (Contributed by Raymond Hettinger.)
650
651* The :mod:`macfs` module has been removed. This in turn required the
652 :func:`macostools.touched` function to be removed because it depended on the
653 :mod:`macfs` module.
654
655 .. % Patch #1490190
656
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000657* The :func:`os.walk` function now has a ``followlinks`` parameter. If
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000658 set to True, it will follow symlinks pointing to directories and
659 visit the directory's contents. For backward compatibility, the
660 parameter's default value is false. Note that the function can fall
661 into an infinite recursion if there's a symlink that points to a
662 parent directory.
663
664 .. % Patch 1273829
665
Andrew M. Kuchling6edff592007-10-16 22:58:03 +0000666* The ``os.environ`` object's :meth:`clear` method will now unset the
667 environment variables using :func:`os.unsetenv` in addition to clearing
668 the object's keys. (Contributed by XXX.)
669
670 .. % Patch #1181
671
Andrew M. Kuchling6c066dd2007-09-01 20:43:36 +0000672* In the :mod:`os.path` module, the :func:`splitext` function
673 has been changed to not split on leading period characters.
674 This produces better results when operating on Unix's dot-files.
675 For example, ``os.path.splitext('.ipython')``
676 now returns ``('.ipython', '')`` instead of ``('', '.ipython')``.
677
678 .. % Bug #115886
679
Andrew M. Kuchlingb4c62952007-09-01 21:18:31 +0000680 A new function, :func:`relpath(path, start)` returns a relative path
681 from the ``start`` path, if it's supplied, or from the current
682 working directory to the destination ``path``. (Contributed by
683 Richard Barran.)
684
685 .. % Patch 1339796
686
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000687 On Windows, :func:`os.path.expandvars` will now expand environment variables
688 in the form "%var%", and "~user" will be expanded into the
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000689 user's home directory path. (Contributed by Josiah Carlson.)
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000690
691 .. % Patch 957650
692
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000693* The Python debugger provided by the :mod:`pdb` module
694 gained a new command: "run" restarts the Python program being debugged,
695 and can optionally take new command-line arguments for the program.
696 (Contributed by Rocky Bernstein.)
697
698 .. % Patch #1393667
699
Georg Brandl8ec7f652007-08-15 14:28:01 +0000700* New functions in the :mod:`posix` module: :func:`chflags` and :func:`lchflags`
701 are wrappers for the corresponding system calls (where they're available).
702 Constants for the flag values are defined in the :mod:`stat` module; some
703 possible values include :const:`UF_IMMUTABLE` to signal the file may not be
704 changed and :const:`UF_APPEND` to indicate that data can only be appended to the
705 file. (Contributed by M. Levinson.)
706
707* The :mod:`rgbimg` module has been removed.
708
Andrew M. Kuchling6c066dd2007-09-01 20:43:36 +0000709* The :mod:`smtplib` module now supports SMTP over SSL thanks to the
710 addition of the :class:`SMTP_SSL` class. This class supports an
Andrew M. Kuchlingb4c62952007-09-01 21:18:31 +0000711 interface identical to the existing :class:`SMTP` class. Both
712 class constructors also have an optional ``timeout`` parameter
713 that specifies a timeout for the initial connection attempt, measured in
714 seconds.
715
716 An implementation of the LMTP protocol (:rfc:`2033`) was also added to
Andrew M. Kuchling6c066dd2007-09-01 20:43:36 +0000717 the module. LMTP is used in place of SMTP when transferring e-mail
718 between agents that don't manage a mail queue.
Andrew M. Kuchlingb4c62952007-09-01 21:18:31 +0000719
720 (SMTP over SSL contributed by Monty Taylor; timeout parameter
721 added by Facundo Batista; LMTP implemented by Leif
Andrew M. Kuchling6c066dd2007-09-01 20:43:36 +0000722 Hedstrom.)
723
724 .. % Patch #957003
Georg Brandl8ec7f652007-08-15 14:28:01 +0000725
Andrew M. Kuchlingde37a8c2007-09-18 01:36:16 +0000726* The :mod:`tarfile` module now supports POSIX.1-2001 (pax) and
727 POSIX.1-1988 (ustar) format tarfiles, in addition to the GNU tar
728 format that was already supported. The default format
729 is GNU tar; specify the ``format`` parameter to open a file
730 using a different format::
731
732 tar = tarfile.open("output.tar", "w", format=tarfile.PAX_FORMAT)
733
734 The new ``errors`` parameter lets you specify an error handling
735 scheme for character conversions: the three standard ways Python can
736 handle errors ``'strict'``, ``'ignore'``, ``'replace'`` , or the
737 special value ``'utf-8'``, which replaces bad characters with their
738 UTF-8 representation. Character conversions occur because the PAX
739 format supports Unicode filenames, defaulting to UTF-8 encoding.
740
741 The :meth:`TarFile.add` method now accepts a ``exclude`` argument that's
742 a function that can be used to exclude certain filenames from
743 an archive.
744 The function must take a filename and return true if the file
745 should be excluded or false if it should be archived.
746 The function is applied to both the name initially passed to :meth:`add`
747 and to the names of files in recursively-added directories.
748
749 (All changes contributed by Lars Gustäbel).
750
751* An optional ``timeout`` parameter was added to the
752 :class:`telnetlib.Telnet` class constructor, specifying a timeout
753 measured in seconds. (Added by Facundo Batista.)
754
755* The :class:`tempfile.NamedTemporaryFile` class usually deletes
756 the temporary file it created when the file is closed. This
757 behaviour can now be changed by passing ``delete=False`` to the
758 constructor. (Contributed by Damien Miller.)
759
760 .. % Patch #1537850
761
762* The :mod:`test.test_support` module now contains a
763 :func:`EnvironmentVarGuard`
764 context manager that supports temporarily changing environment variables and
765 automatically restores them to their old values.
766
767 Another context manager, :class:`TransientResource`, can surround calls
768 to resources that may or may not be available; it will catch and
769 ignore a specified list of exceptions. For example,
770 a network test may ignore certain failures when connecting to an
771 external web site::
772
773 with test_support.TransientResource(IOError, errno=errno.ETIMEDOUT):
774 f = urllib.urlopen('https://sf.net')
775 ...
776
777 (Contributed by Brett Cannon.)
778
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000779* The :mod:`textwrap` module can now preserve existing whitespace
780 at the beginnings and ends of the newly-created lines
781 by specifying ``drop_whitespace=False``
782 as an argument::
783
784 >>> S = """This sentence has a bunch of extra whitespace."""
785 >>> print textwrap.fill(S, width=15)
786 This sentence
787 has a bunch
788 of extra
789 whitespace.
790 >>> print textwrap.fill(S, drop_whitespace=False, width=15)
791 This sentence
792 has a bunch
793 of extra
794 whitespace.
795 >>>
796
797 .. % Patch #1581073
798
Andrew M. Kuchling6c066dd2007-09-01 20:43:36 +0000799* The :mod:`timeit` module now accepts callables as well as strings
800 for the statement being timed and for the setup code.
801 Two convenience functions were added for creating
802 :class:`Timer` instances:
803 ``repeat(stmt, setup, time, repeat, number)`` and
804 ``timeit(stmt, setup, time, number)`` create an instance and call
805 the corresponding method. (Contributed by Erik Demaine.)
806
807 .. % Patch #1533909
808
Andrew M. Kuchlingf10878b2007-09-13 22:49:34 +0000809* An optional ``timeout`` parameter was added to the
810 :func:`urllib.urlopen` function and the
811 :class:`urllib.ftpwrapper` class constructor, as well as the
812 :func:`urllib2.urlopen` function. The parameter specifies a timeout
813 measured in seconds. For example::
814
815 >>> u = urllib2.urlopen("http://slow.example.com", timeout=3)
816 Traceback (most recent call last):
817 ...
818 urllib2.URLError: <urlopen error timed out>
819 >>>
820
821 (Added by Facundo Batista.)
822
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000823* The XML-RPC classes :class:`SimpleXMLRPCServer` and :class:`DocXMLRPCServer`
824 classes can now be preventing from immediately opening and binding to
825 their socket by passing True as the ``bind_and_activate``
826 constructor parameter. This can be used to modify the instance's
827 :attr:`allow_reuse_address` attribute before calling the
828 :meth:`server_bind` and :meth:`server_activate` methods to
829 open the socket and begin listening for connections.
830 (Contributed by Peter Parente.)
831
832 .. % Patch 1599845
833
Georg Brandl8ec7f652007-08-15 14:28:01 +0000834.. % ======================================================================
835.. % whole new modules get described in \subsections here
836
837.. % ======================================================================
838
839
840Build and C API Changes
841=======================
842
843Changes to Python's build process and to the C API include:
844
Andrew M. Kuchling6edff592007-10-16 22:58:03 +0000845* The BerkeleyDB module now has a C API object, available as
846 ``bsddb.db.api``. This object can be used by other C extensions
847 that wish to use the :mod:`bsddb` module for their own purposes.
848 (Contributed by Duncan Grisby.)
849
850 .. % Patch 1551895
851
Georg Brandl8ec7f652007-08-15 14:28:01 +0000852
853.. % ======================================================================
854
855
856Port-Specific Changes
857---------------------
858
859Platform-specific changes go here.
860
861.. % ======================================================================
862
863
864.. _section-other:
865
866Other Changes and Fixes
867=======================
868
869As usual, there were a bunch of other improvements and bugfixes scattered
870throughout the source tree. A search through the change logs finds there were
871XXX patches applied and YYY bugs fixed between Python 2.5 and 2.6. Both figures
872are likely to be underestimates.
873
874Some of the more notable changes are:
875
Andrew M. Kuchling99479eb2007-09-25 00:09:42 +0000876* Details will go here.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000877
878.. % ======================================================================
879
880
881Porting to Python 2.6
882=====================
883
884This section lists previously described changes that may require changes to your
885code:
886
Gregory P. Smithe9fef692007-09-09 23:36:46 +0000887* The :mod:`socket` module exception :exc:`socket.error` now inherits from
888 :exc:`IOError`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000889
890.. % ======================================================================
891
892
893.. _acks:
894
895Acknowledgements
896================
897
898The author would like to thank the following people for offering suggestions,
899corrections and assistance with various drafts of this article: .
900