blob: 91ade015ae710da719927afd3710ae6532f8c552 [file] [log] [blame]
Fred Drake2db76802004-12-01 05:05:47 +00001\documentclass{howto}
2\usepackage{distutils}
3% $Id$
4
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00005% Fix XXX comments
Andrew M. Kuchlinga4d651f2006-04-06 13:24:58 +00006% Distutils upload (PEP 243)
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00007% The easy_install stuff
8% xml.etree section
9% added sqlite3
Fred Drake2db76802004-12-01 05:05:47 +000010
11\title{What's New in Python 2.5}
Andrew M. Kuchling2cdb23e2006-04-05 13:59:01 +000012\release{0.1}
Andrew M. Kuchling92e24952004-12-03 13:54:09 +000013\author{A.M. Kuchling}
14\authoraddress{\email{amk@amk.ca}}
Fred Drake2db76802004-12-01 05:05:47 +000015
16\begin{document}
17\maketitle
18\tableofcontents
19
20This article explains the new features in Python 2.5. No release date
Andrew M. Kuchling5eefdca2006-02-08 11:36:09 +000021for Python 2.5 has been set; it will probably be released in the
Andrew M. Kuchlingd96a6ac2006-04-04 19:17:34 +000022autumn of 2006. \pep{356} describes the planned release schedule.
Fred Drake2db76802004-12-01 05:05:47 +000023
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +000024(This is still an early draft, and some sections are still skeletal or
25completely missing. Comments on the present material will still be
26welcomed.)
27
Andrew M. Kuchling437567c2006-03-07 20:48:55 +000028% XXX Compare with previous release in 2 - 3 sentences here.
Fred Drake2db76802004-12-01 05:05:47 +000029
30This article doesn't attempt to provide a complete specification of
31the new features, but instead provides a convenient overview. For
32full details, you should refer to the documentation for Python 2.5.
Andrew M. Kuchling437567c2006-03-07 20:48:55 +000033% XXX add hyperlink when the documentation becomes available online.
Fred Drake2db76802004-12-01 05:05:47 +000034If you want to understand the complete implementation and design
35rationale, refer to the PEP for a particular new feature.
36
37
38%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +000039\section{PEP 308: Conditional Expressions}
40
Andrew M. Kuchlinge362d932006-03-09 13:56:25 +000041For a long time, people have been requesting a way to write
42conditional expressions, expressions that return value A or value B
43depending on whether a Boolean value is true or false. A conditional
44expression lets you write a single assignment statement that has the
45same effect as the following:
46
47\begin{verbatim}
48if condition:
49 x = true_value
50else:
51 x = false_value
52\end{verbatim}
53
54There have been endless tedious discussions of syntax on both
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +000055python-dev and comp.lang.python. A vote was even held that found the
56majority of voters wanted conditional expressions in some form,
57but there was no syntax that was preferred by a clear majority.
58Candidates included C's \code{cond ? true_v : false_v},
Andrew M. Kuchlinge362d932006-03-09 13:56:25 +000059\code{if cond then true_v else false_v}, and 16 other variations.
60
61GvR eventually chose a surprising syntax:
62
63\begin{verbatim}
64x = true_value if condition else false_value
65\end{verbatim}
66
Andrew M. Kuchling38f85072006-04-02 01:46:32 +000067Evaluation is still lazy as in existing Boolean expressions, so the
68order of evaluation jumps around a bit. The \var{condition}
69expression in the middle is evaluated first, and the \var{true_value}
70expression is evaluated only if the condition was true. Similarly,
71the \var{false_value} expression is only evaluated when the condition
72is false.
Andrew M. Kuchlinge362d932006-03-09 13:56:25 +000073
74This syntax may seem strange and backwards; why does the condition go
75in the \emph{middle} of the expression, and not in the front as in C's
76\code{c ? x : y}? The decision was checked by applying the new syntax
77to the modules in the standard library and seeing how the resulting
78code read. In many cases where a conditional expression is used, one
79value seems to be the 'common case' and one value is an 'exceptional
80case', used only on rarer occasions when the condition isn't met. The
81conditional syntax makes this pattern a bit more obvious:
82
83\begin{verbatim}
84contents = ((doc + '\n') if doc else '')
85\end{verbatim}
86
87I read the above statement as meaning ``here \var{contents} is
Andrew M. Kuchlingd0fcc022006-03-09 13:57:28 +000088usually assigned a value of \code{doc+'\e n'}; sometimes
Andrew M. Kuchlinge362d932006-03-09 13:56:25 +000089\var{doc} is empty, in which special case an empty string is returned.''
90I doubt I will use conditional expressions very often where there
91isn't a clear common and uncommon case.
92
93There was some discussion of whether the language should require
94surrounding conditional expressions with parentheses. The decision
95was made to \emph{not} require parentheses in the Python language's
96grammar, but as a matter of style I think you should always use them.
97Consider these two statements:
98
99\begin{verbatim}
100# First version -- no parens
101level = 1 if logging else 0
102
103# Second version -- with parens
104level = (1 if logging else 0)
105\end{verbatim}
106
107In the first version, I think a reader's eye might group the statement
108into 'level = 1', 'if logging', 'else 0', and think that the condition
109decides whether the assignment to \var{level} is performed. The
110second version reads better, in my opinion, because it makes it clear
111that the assignment is always performed and the choice is being made
112between two values.
113
114Another reason for including the brackets: a few odd combinations of
115list comprehensions and lambdas could look like incorrect conditional
116expressions. See \pep{308} for some examples. If you put parentheses
117around your conditional expressions, you won't run into this case.
118
119
120\begin{seealso}
121
122\seepep{308}{Conditional Expressions}{PEP written by
123Guido van Rossum and Raymond D. Hettinger; implemented by Thomas
124Wouters.}
125
126\end{seealso}
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000127
128
129%======================================================================
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000130\section{PEP 309: Partial Function Application}
Fred Drake2db76802004-12-01 05:05:47 +0000131
Andrew M. Kuchlingb1c96fd2005-03-20 21:42:04 +0000132The \module{functional} module is intended to contain tools for
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000133functional-style programming. Currently it only contains a
134\class{partial()} function, but new functions will probably be added
135in future versions of Python.
Andrew M. Kuchlingb1c96fd2005-03-20 21:42:04 +0000136
Andrew M. Kuchling4b000cd2005-04-09 15:51:44 +0000137For programs written in a functional style, it can be useful to
138construct variants of existing functions that have some of the
139parameters filled in. Consider a Python function \code{f(a, b, c)};
140you could create a new function \code{g(b, c)} that was equivalent to
141\code{f(1, b, c)}. This is called ``partial function application'',
142and is provided by the \class{partial} class in the new
143\module{functional} module.
144
145The constructor for \class{partial} takes the arguments
146\code{(\var{function}, \var{arg1}, \var{arg2}, ...
147\var{kwarg1}=\var{value1}, \var{kwarg2}=\var{value2})}. The resulting
148object is callable, so you can just call it to invoke \var{function}
149with the filled-in arguments.
150
151Here's a small but realistic example:
152
153\begin{verbatim}
154import functional
155
156def log (message, subsystem):
157 "Write the contents of 'message' to the specified subsystem."
158 print '%s: %s' % (subsystem, message)
159 ...
160
161server_log = functional.partial(log, subsystem='server')
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000162server_log('Unable to open socket')
Andrew M. Kuchling4b000cd2005-04-09 15:51:44 +0000163\end{verbatim}
164
Andrew M. Kuchling6af7fe02005-08-02 17:20:36 +0000165Here's another example, from a program that uses PyGTk. Here a
166context-sensitive pop-up menu is being constructed dynamically. The
167callback provided for the menu option is a partially applied version
168of the \method{open_item()} method, where the first argument has been
169provided.
Andrew M. Kuchling4b000cd2005-04-09 15:51:44 +0000170
Andrew M. Kuchling6af7fe02005-08-02 17:20:36 +0000171\begin{verbatim}
172...
173class Application:
174 def open_item(self, path):
175 ...
176 def init (self):
177 open_func = functional.partial(self.open_item, item_path)
178 popup_menu.append( ("Open", open_func, 1) )
179\end{verbatim}
Andrew M. Kuchlingb1c96fd2005-03-20 21:42:04 +0000180
181
182\begin{seealso}
183
184\seepep{309}{Partial Function Application}{PEP proposed and written by
185Peter Harris; implemented by Hye-Shik Chang, with adaptations by
186Raymond Hettinger.}
187
188\end{seealso}
Fred Drake2db76802004-12-01 05:05:47 +0000189
190
191%======================================================================
Fred Drakedb7b0022005-03-20 22:19:47 +0000192\section{PEP 314: Metadata for Python Software Packages v1.1}
193
Andrew M. Kuchlingd8d732e2005-04-09 23:59:41 +0000194Some simple dependency support was added to Distutils. The
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000195\function{setup()} function now has \code{requires}, \code{provides},
196and \code{obsoletes} keyword parameters. When you build a source
197distribution using the \code{sdist} command, the dependency
198information will be recorded in the \file{PKG-INFO} file.
Andrew M. Kuchlingd8d732e2005-04-09 23:59:41 +0000199
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000200Another new keyword parameter is \code{download_url}, which should be
201set to a URL for the package's source code. This means it's now
202possible to look up an entry in the package index, determine the
203dependencies for a package, and download the required packages.
Andrew M. Kuchlingd8d732e2005-04-09 23:59:41 +0000204
205% XXX put example here
206
207\begin{seealso}
208
209\seepep{314}{Metadata for Python Software Packages v1.1}{PEP proposed
210and written by A.M. Kuchling, Richard Jones, and Fred Drake;
211implemented by Richard Jones and Fred Drake.}
212
213\end{seealso}
Fred Drakedb7b0022005-03-20 22:19:47 +0000214
215
216%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000217\section{PEP 328: Absolute and Relative Imports}
218
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000219The simpler part of PEP 328 was implemented in Python 2.4: parentheses
220could now be used to enclose the names imported from a module using
221the \code{from ... import ...} statement, making it easier to import
222many different names.
223
224The more complicated part has been implemented in Python 2.5:
225importing a module can be specified to use absolute or
226package-relative imports. The plan is to move toward making absolute
227imports the default in future versions of Python.
228
229Let's say you have a package directory like this:
230\begin{verbatim}
231pkg/
232pkg/__init__.py
233pkg/main.py
234pkg/string.py
235\end{verbatim}
236
237This defines a package named \module{pkg} containing the
238\module{pkg.main} and \module{pkg.string} submodules.
239
240Consider the code in the \file{main.py} module. What happens if it
241executes the statement \code{import string}? In Python 2.4 and
242earlier, it will first look in the package's directory to perform a
243relative import, finds \file{pkg/string.py}, imports the contents of
244that file as the \module{pkg.string} module, and that module is bound
245to the name \samp{string} in the \module{pkg.main} module's namespace.
246
247That's fine if \module{pkg.string} was what you wanted. But what if
248you wanted Python's standard \module{string} module? There's no clean
249way to ignore \module{pkg.string} and look for the standard module;
250generally you had to look at the contents of \code{sys.modules}, which
251is slightly unclean.
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000252Holger Krekel's \module{py.std} package provides a tidier way to perform
253imports from the standard library, \code{import py ; py.std.string.join()},
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000254but that package isn't available on all Python installations.
255
256Reading code which relies on relative imports is also less clear,
257because a reader may be confused about which module, \module{string}
258or \module{pkg.string}, is intended to be used. Python users soon
259learned not to duplicate the names of standard library modules in the
260names of their packages' submodules, but you can't protect against
261having your submodule's name being used for a new module added in a
262future version of Python.
263
264In Python 2.5, you can switch \keyword{import}'s behaviour to
265absolute imports using a \code{from __future__ import absolute_import}
266directive. This absolute-import behaviour will become the default in
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000267a future version (probably Python 2.7). Once absolute imports
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000268are the default, \code{import string} will
269always find the standard library's version.
270It's suggested that users should begin using absolute imports as much
271as possible, so it's preferable to begin writing \code{from pkg import
272string} in your code.
273
274Relative imports are still possible by adding a leading period
275to the module name when using the \code{from ... import} form:
276
277\begin{verbatim}
278# Import names from pkg.string
279from .string import name1, name2
280# Import pkg.string
281from . import string
282\end{verbatim}
283
284This imports the \module{string} module relative to the current
285package, so in \module{pkg.main} this will import \var{name1} and
286\var{name2} from \module{pkg.string}. Additional leading periods
287perform the relative import starting from the parent of the current
288package. For example, code in the \module{A.B.C} module can do:
289
290\begin{verbatim}
291from . import D # Imports A.B.D
292from .. import E # Imports A.E
293from ..F import G # Imports A.F.G
294\end{verbatim}
295
296Leading periods cannot be used with the \code{import \var{modname}}
297form of the import statement, only the \code{from ... import} form.
298
299\begin{seealso}
300
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000301\seepep{328}{Imports: Multi-Line and Absolute/Relative}
302{PEP written by Aahz; implemented by Thomas Wouters.}
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000303
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000304\seeurl{http://codespeak.net/py/current/doc/index.html}
305{The py library by Holger Krekel, which contains the \module{py.std} package.}
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000306
307\end{seealso}
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000308
309
310%======================================================================
Andrew M. Kuchling21d3a7c2006-03-15 11:53:09 +0000311\section{PEP 338: Executing Modules as Scripts}
312
Andrew M. Kuchlingb182db42006-03-17 21:48:46 +0000313The \programopt{-m} switch added in Python 2.4 to execute a module as
314a script gained a few more abilities. Instead of being implemented in
315C code inside the Python interpreter, the switch now uses an
316implementation in a new module, \module{runpy}.
317
318The \module{runpy} module implements a more sophisticated import
319mechanism so that it's now possible to run modules in a package such
320as \module{pychecker.checker}. The module also supports alternative
321import mechanisms such as the \module{zipimport} module. (This means
322you can add a .zip archive's path to \code{sys.path} and then use the
323\programopt{-m} switch to execute code from the archive.
324
325
326\begin{seealso}
327
328\seepep{338}{Executing modules as scripts}{PEP written and
329implemented by Nick Coghlan.}
330
331\end{seealso}
Andrew M. Kuchling21d3a7c2006-03-15 11:53:09 +0000332
333
334%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000335\section{PEP 341: Unified try/except/finally}
336
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000337Until Python 2.5, the \keyword{try} statement came in two
338flavours. You could use a \keyword{finally} block to ensure that code
339is always executed, or a number of \keyword{except} blocks to catch an
340exception. You couldn't combine both \keyword{except} blocks and a
341\keyword{finally} block, because generating the right bytecode for the
342combined version was complicated and it wasn't clear what the
343semantics of the combined should be.
344
345GvR spent some time working with Java, which does support the
346equivalent of combining \keyword{except} blocks and a
347\keyword{finally} block, and this clarified what the statement should
348mean. In Python 2.5, you can now write:
349
350\begin{verbatim}
351try:
352 block-1 ...
353except Exception1:
354 handler-1 ...
355except Exception2:
356 handler-2 ...
357else:
358 else-block
359finally:
360 final-block
361\end{verbatim}
362
363The code in \var{block-1} is executed. If the code raises an
364exception, the handlers are tried in order: \var{handler-1},
365\var{handler-2}, ... If no exception is raised, the \var{else-block}
366is executed. No matter what happened previously, the
367\var{final-block} is executed once the code block is complete and any
368raised exceptions handled. Even if there's an error in an exception
369handler or the \var{else-block} and a new exception is raised, the
370\var{final-block} is still executed.
371
372\begin{seealso}
373
374\seepep{341}{Unifying try-except and try-finally}{PEP written by Georg Brandl;
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +0000375implementation by Thomas Lee.}
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000376
377\end{seealso}
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000378
379
380%======================================================================
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000381\section{PEP 342: New Generator Features}
382
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000383Python 2.5 adds a simple way to pass values \emph{into} a generator.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000384As introduced in Python 2.3, generators only produce output; once a
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000385generator's code is invoked to create an iterator, there's no way to
386pass any new information into the function when its execution is
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000387resumed. Sometimes the ability to pass in some information would be
388useful. Hackish solutions to this include making the generator's code
389look at a global variable and then changing the global variable's
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000390value, or passing in some mutable object that callers then modify.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000391
392To refresh your memory of basic generators, here's a simple example:
393
394\begin{verbatim}
395def counter (maximum):
396 i = 0
397 while i < maximum:
398 yield i
399 i += 1
400\end{verbatim}
401
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000402When you call \code{counter(10)}, the result is an iterator that
403returns the values from 0 up to 9. On encountering the
404\keyword{yield} statement, the iterator returns the provided value and
405suspends the function's execution, preserving the local variables.
406Execution resumes on the following call to the iterator's
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000407\method{next()} method, picking up after the \keyword{yield} statement.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000408
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000409In Python 2.3, \keyword{yield} was a statement; it didn't return any
410value. In 2.5, \keyword{yield} is now an expression, returning a
411value that can be assigned to a variable or otherwise operated on:
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000412
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000413\begin{verbatim}
414val = (yield i)
415\end{verbatim}
416
417I recommend that you always put parentheses around a \keyword{yield}
418expression when you're doing something with the returned value, as in
419the above example. The parentheses aren't always necessary, but it's
420easier to always add them instead of having to remember when they're
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000421needed.\footnote{The exact rules are that a \keyword{yield}-expression must
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000422always be parenthesized except when it occurs at the top-level
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000423expression on the right-hand side of an assignment, meaning you can
424write \code{val = yield i} but have to use parentheses when there's an
425operation, as in \code{val = (yield i) + 12}.}
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000426
427Values are sent into a generator by calling its
428\method{send(\var{value})} method. The generator's code is then
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000429resumed and the \keyword{yield} expression returns the specified
430\var{value}. If the regular \method{next()} method is called, the
431\keyword{yield} returns \constant{None}.
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000432
433Here's the previous example, modified to allow changing the value of
434the internal counter.
435
436\begin{verbatim}
437def counter (maximum):
438 i = 0
439 while i < maximum:
440 val = (yield i)
441 # If value provided, change counter
442 if val is not None:
443 i = val
444 else:
445 i += 1
446\end{verbatim}
447
448And here's an example of changing the counter:
449
450\begin{verbatim}
451>>> it = counter(10)
452>>> print it.next()
4530
454>>> print it.next()
4551
456>>> print it.send(8)
4578
458>>> print it.next()
4599
460>>> print it.next()
461Traceback (most recent call last):
462 File ``t.py'', line 15, in ?
463 print it.next()
464StopIteration
Andrew M. Kuchlingc2033702005-08-29 13:30:12 +0000465\end{verbatim}
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000466
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000467Because \keyword{yield} will often be returning \constant{None}, you
468should always check for this case. Don't just use its value in
469expressions unless you're sure that the \method{send()} method
470will be the only method used resume your generator function.
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000471
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000472In addition to \method{send()}, there are two other new methods on
473generators:
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000474
475\begin{itemize}
476
477 \item \method{throw(\var{type}, \var{value}=None,
478 \var{traceback}=None)} is used to raise an exception inside the
479 generator; the exception is raised by the \keyword{yield} expression
480 where the generator's execution is paused.
481
482 \item \method{close()} raises a new \exception{GeneratorExit}
483 exception inside the generator to terminate the iteration.
484 On receiving this
485 exception, the generator's code must either raise
486 \exception{GeneratorExit} or \exception{StopIteration}; catching the
487 exception and doing anything else is illegal and will trigger
488 a \exception{RuntimeError}. \method{close()} will also be called by
489 Python's garbage collection when the generator is garbage-collected.
490
491 If you need to run cleanup code in case of a \exception{GeneratorExit},
492 I suggest using a \code{try: ... finally:} suite instead of
493 catching \exception{GeneratorExit}.
494
495\end{itemize}
496
497The cumulative effect of these changes is to turn generators from
498one-way producers of information into both producers and consumers.
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000499
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000500Generators also become \emph{coroutines}, a more generalized form of
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000501subroutines. Subroutines are entered at one point and exited at
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000502another point (the top of the function, and a \keyword{return
503statement}), but coroutines can be entered, exited, and resumed at
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000504many different points (the \keyword{yield} statements). We'll have to
505figure out patterns for using coroutines effectively in Python.
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000506
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000507The addition of the \method{close()} method has one side effect that
508isn't obvious. \method{close()} is called when a generator is
509garbage-collected, so this means the generator's code gets one last
510chance to run before the generator is destroyed, and this last chance
511means that \code{try...finally} statements in generators can now be
512guaranteed to work; the \keyword{finally} clause will now always get a
513chance to run. The syntactic restriction that you couldn't mix
514\keyword{yield} statements with a \code{try...finally} suite has
515therefore been removed. This seems like a minor bit of language
516trivia, but using generators and \code{try...finally} is actually
517necessary in order to implement the \keyword{with} statement
518described by PEP 343. We'll look at this new statement in the following
519section.
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000520
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000521\begin{seealso}
522
523\seepep{342}{Coroutines via Enhanced Generators}{PEP written by
524Guido van Rossum and Phillip J. Eby;
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000525implemented by Phillip J. Eby. Includes examples of
526some fancier uses of generators as coroutines.}
527
528\seeurl{http://en.wikipedia.org/wiki/Coroutine}{The Wikipedia entry for
529coroutines.}
530
Neal Norwitz09179882006-03-04 23:31:45 +0000531\seeurl{http://www.sidhe.org/\~{}dan/blog/archives/000178.html}{An
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000532explanation of coroutines from a Perl point of view, written by Dan
533Sugalski.}
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000534
535\end{seealso}
536
537
538%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000539\section{PEP 343: The 'with' statement}
540
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000541The \keyword{with} statement allows a clearer
542version of code that uses \code{try...finally} blocks
543
544First, I'll discuss the statement as it will commonly be used, and
545then I'll discuss the detailed implementation and how to write objects
546(called ``context managers'') that can be used with this statement.
547Most people, who will only use \keyword{with} in company with an
Andrew M. Kuchlinga4d651f2006-04-06 13:24:58 +0000548existing object, don't need to know these details and can
549just use objects that are documented to work as context managers.
550Authors of new context managers will need to understand the details of
551the underlying implementation.
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000552
553The \keyword{with} statement is a new control-flow structure whose
554basic structure is:
555
556\begin{verbatim}
557with expression as variable:
558 with-block
559\end{verbatim}
560
561The expression is evaluated, and it should result in a type of object
562that's called a context manager. The context manager can return a
563value that will be bound to the name \var{variable}. (Note carefully:
564\var{variable} is \emph{not} assigned the result of \var{expression}.
565One method of the context manager is run before \var{with-block} is
566executed, and another method is run after the block is done, even if
567the block raised an exception.
568
569To enable the statement in Python 2.5, you need
570to add the following directive to your module:
571
572\begin{verbatim}
573from __future__ import with_statement
574\end{verbatim}
575
576Some standard Python objects can now behave as context managers. For
577example, file objects:
578
579\begin{verbatim}
580with open('/etc/passwd', 'r') as f:
581 for line in f:
582 print line
583
584# f has been automatically closed at this point.
585\end{verbatim}
586
587The \module{threading} module's locks and condition variables
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +0000588also support the \keyword{with} statement:
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000589
590\begin{verbatim}
591lock = threading.Lock()
592with lock:
593 # Critical section of code
594 ...
595\end{verbatim}
596
597The lock is acquired before the block is executed, and released once
598the block is complete.
599
600The \module{decimal} module's contexts, which encapsulate the desired
601precision and rounding characteristics for computations, can also be
602used as context managers.
603
604\begin{verbatim}
605import decimal
606
607v1 = decimal.Decimal('578')
608
609# Displays with default precision of 28 digits
610print v1.sqrt()
611
612with decimal.Context(prec=16):
613 # All code in this block uses a precision of 16 digits.
614 # The original context is restored on exiting the block.
615 print v1.sqrt()
616\end{verbatim}
617
618\subsection{Writing Context Managers}
619
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000620% XXX write this
621
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +0000622This section still needs to be written.
623
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000624The new \module{contextlib} module provides some functions and a
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +0000625decorator that are useful for writing context managers.
626Future versions will go into more detail.
627
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000628% XXX describe further
629
630\begin{seealso}
631
632\seepep{343}{The ``with'' statement}{PEP written by
633Guido van Rossum and Nick Coghlan. }
634
635\end{seealso}
636
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000637
638%======================================================================
Andrew M. Kuchling8f4d2552006-03-08 01:50:20 +0000639\section{PEP 352: Exceptions as New-Style Classes}
640
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000641Exception classes can now be new-style classes, not just classic
642classes, and the built-in \exception{Exception} class and all the
643standard built-in exceptions (\exception{NameError},
644\exception{ValueError}, etc.) are now new-style classes.
Andrew M. Kuchlingaeadf952006-03-09 19:06:05 +0000645
646The inheritance hierarchy for exceptions has been rearranged a bit.
647In 2.5, the inheritance relationships are:
648
649\begin{verbatim}
650BaseException # New in Python 2.5
651|- KeyboardInterrupt
652|- SystemExit
653|- Exception
654 |- (all other current built-in exceptions)
655\end{verbatim}
656
657This rearrangement was done because people often want to catch all
658exceptions that indicate program errors. \exception{KeyboardInterrupt} and
659\exception{SystemExit} aren't errors, though, and usually represent an explicit
660action such as the user hitting Control-C or code calling
661\function{sys.exit()}. A bare \code{except:} will catch all exceptions,
662so you commonly need to list \exception{KeyboardInterrupt} and
663\exception{SystemExit} in order to re-raise them. The usual pattern is:
664
665\begin{verbatim}
666try:
667 ...
668except (KeyboardInterrupt, SystemExit):
669 raise
670except:
671 # Log error...
672 # Continue running program...
673\end{verbatim}
674
675In Python 2.5, you can now write \code{except Exception} to achieve
676the same result, catching all the exceptions that usually indicate errors
677but leaving \exception{KeyboardInterrupt} and
678\exception{SystemExit} alone. As in previous versions,
679a bare \code{except:} still catches all exceptions.
680
681The goal for Python 3.0 is to require any class raised as an exception
682to derive from \exception{BaseException} or some descendant of
683\exception{BaseException}, and future releases in the
684Python 2.x series may begin to enforce this constraint. Therefore, I
685suggest you begin making all your exception classes derive from
686\exception{Exception} now. It's been suggested that the bare
687\code{except:} form should be removed in Python 3.0, but Guido van~Rossum
688hasn't decided whether to do this or not.
689
690Raising of strings as exceptions, as in the statement \code{raise
691"Error occurred"}, is deprecated in Python 2.5 and will trigger a
692warning. The aim is to be able to remove the string-exception feature
693in a few releases.
694
695
696\begin{seealso}
697
Andrew M. Kuchlingc3749a92006-04-04 19:14:41 +0000698\seepep{352}{Required Superclass for Exceptions}{PEP written by
Andrew M. Kuchlingaeadf952006-03-09 19:06:05 +0000699Brett Cannon and Guido van Rossum; implemented by Brett Cannon.}
700
701\end{seealso}
Andrew M. Kuchling8f4d2552006-03-08 01:50:20 +0000702
703
704%======================================================================
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000705\section{PEP 353: Using ssize_t as the index type\label{section-353}}
Andrew M. Kuchlingc3749a92006-04-04 19:14:41 +0000706
707A wide-ranging change to Python's C API, using a new
708\ctype{Py_ssize_t} type definition instead of \ctype{int},
709will permit the interpreter to handle more data on 64-bit platforms.
710This change doesn't affect Python's capacity on 32-bit platforms.
711
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000712Various pieces of the Python interpreter used C's \ctype{int} type to
713store sizes or counts; for example, the number of items in a list or
714tuple were stored in an \ctype{int}. The C compilers for most 64-bit
715platforms still define \ctype{int} as a 32-bit type, so that meant
716that lists could only hold up to \code{2**31 - 1} = 2147483647 items.
717(There are actually a few different programming models that 64-bit C
718compilers can use -- see
719\url{http://www.unix.org/version2/whatsnew/lp64_wp.html} for a
720discussion -- but the most commonly available model leaves \ctype{int}
721as 32 bits.)
722
723A limit of 2147483647 items doesn't really matter on a 32-bit platform
724because you'll run out of memory before hitting the length limit.
725Each list item requires space for a pointer, which is 4 bytes, plus
726space for a \ctype{PyObject} representing the item. 2147483647*4 is
727already more bytes than a 32-bit address space can contain.
728
729It's possible to address that much memory on a 64-bit platform,
730however. The pointers for a list that size would only require 16GiB
731of space, so it's not unreasonable that Python programmers might
732construct lists that large. Therefore, the Python interpreter had to
733be changed to use some type other than \ctype{int}, and this will be a
73464-bit type on 64-bit platforms. The change will cause
735incompatibilities on 64-bit machines, so it was deemed worth making
736the transition now, while the number of 64-bit users is still
737relatively small. (In 5 or 10 years, we may \emph{all} be on 64-bit
738machines, and the transition would be more painful then.)
739
740This change most strongly affects authors of C extension modules.
741Python strings and container types such as lists and tuples
742now use \ctype{Py_ssize_t} to store their size.
743Functions such as \cfunction{PyList_Size()}
744now return \ctype{Py_ssize_t}. Code in extension modules
745may therefore need to have some variables changed to
746\ctype{Py_ssize_t}.
747
748The \cfunction{PyArg_ParseTuple()} and \cfunction{Py_BuildValue()} functions
749have a new conversion code, \samp{n}, for \ctype{Py_ssize_t}.
Andrew M. Kuchlinga4d651f2006-04-06 13:24:58 +0000750\cfunction{PyArg_ParseTuple()}'s \samp{s\#} and \samp{t\#} still output
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000751\ctype{int} by default, but you can define the macro
752\csimplemacro{PY_SSIZE_T_CLEAN} before including \file{Python.h}
753to make them return \ctype{Py_ssize_t}.
754
755\pep{353} has a section on conversion guidelines that
756extension authors should read to learn about supporting 64-bit
757platforms.
Andrew M. Kuchlingc3749a92006-04-04 19:14:41 +0000758
759\begin{seealso}
760
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000761\seepep{353}{Using ssize_t as the index type}{PEP written and implemented by Martin von L\"owis.}
Andrew M. Kuchlingc3749a92006-04-04 19:14:41 +0000762
763\end{seealso}
764
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000765
Andrew M. Kuchlingc3749a92006-04-04 19:14:41 +0000766%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000767\section{PEP 357: The '__index__' method}
768
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000769The NumPy developers had a problem that could only be solved by adding
770a new special method, \method{__index__}. When using slice notation,
Fred Drake1c0e3282006-04-02 03:30:06 +0000771as in \code{[\var{start}:\var{stop}:\var{step}]}, the values of the
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000772\var{start}, \var{stop}, and \var{step} indexes must all be either
773integers or long integers. NumPy defines a variety of specialized
774integer types corresponding to unsigned and signed integers of 8, 16,
77532, and 64 bits, but there was no way to signal that these types could
776be used as slice indexes.
777
778Slicing can't just use the existing \method{__int__} method because
779that method is also used to implement coercion to integers. If
780slicing used \method{__int__}, floating-point numbers would also
781become legal slice indexes and that's clearly an undesirable
782behaviour.
783
784Instead, a new special method called \method{__index__} was added. It
785takes no arguments and returns an integer giving the slice index to
786use. For example:
787
788\begin{verbatim}
789class C:
790 def __index__ (self):
791 return self.value
792\end{verbatim}
793
794The return value must be either a Python integer or long integer.
795The interpreter will check that the type returned is correct, and
796raises a \exception{TypeError} if this requirement isn't met.
797
798A corresponding \member{nb_index} slot was added to the C-level
799\ctype{PyNumberMethods} structure to let C extensions implement this
800protocol. \cfunction{PyNumber_Index(\var{obj})} can be used in
801extension code to call the \method{__index__} function and retrieve
802its result.
803
804\begin{seealso}
805
806\seepep{357}{Allowing Any Object to be Used for Slicing}{PEP written
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +0000807and implemented by Travis Oliphant.}
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000808
809\end{seealso}
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000810
811
812%======================================================================
Fred Drake2db76802004-12-01 05:05:47 +0000813\section{Other Language Changes}
814
815Here are all of the changes that Python 2.5 makes to the core Python
816language.
817
818\begin{itemize}
Andrew M. Kuchling1cae3f52004-12-03 14:57:21 +0000819
820\item The \function{min()} and \function{max()} built-in functions
821gained a \code{key} keyword argument analogous to the \code{key}
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000822argument for \method{sort()}. This argument supplies a function
Andrew M. Kuchling1cae3f52004-12-03 14:57:21 +0000823that takes a single argument and is called for every value in the list;
824\function{min()}/\function{max()} will return the element with the
825smallest/largest return value from this function.
826For example, to find the longest string in a list, you can do:
827
828\begin{verbatim}
829L = ['medium', 'longest', 'short']
830# Prints 'longest'
831print max(L, key=len)
832# Prints 'short', because lexicographically 'short' has the largest value
833print max(L)
834\end{verbatim}
835
836(Contributed by Steven Bethard and Raymond Hettinger.)
Fred Drake2db76802004-12-01 05:05:47 +0000837
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000838\item Two new built-in functions, \function{any()} and
839\function{all()}, evaluate whether an iterator contains any true or
840false values. \function{any()} returns \constant{True} if any value
841returned by the iterator is true; otherwise it will return
842\constant{False}. \function{all()} returns \constant{True} only if
843all of the values returned by the iterator evaluate as being true.
844
845% XXX who added?
846
847
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000848\item The list of base classes in a class definition can now be empty.
849As an example, this is now legal:
850
851\begin{verbatim}
852class C():
853 pass
854\end{verbatim}
855(Implemented by Brett Cannon.)
856
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000857% XXX __missing__ hook in dictionaries
858
Fred Drake2db76802004-12-01 05:05:47 +0000859\end{itemize}
860
861
862%======================================================================
Andrew M. Kuchlingda376042006-03-17 15:56:41 +0000863\subsection{Interactive Interpreter Changes}
864
865In the interactive interpreter, \code{quit} and \code{exit}
866have long been strings so that new users get a somewhat helpful message
867when they try to quit:
868
869\begin{verbatim}
870>>> quit
871'Use Ctrl-D (i.e. EOF) to exit.'
872\end{verbatim}
873
874In Python 2.5, \code{quit} and \code{exit} are now objects that still
875produce string representations of themselves, but are also callable.
876Newbies who try \code{quit()} or \code{exit()} will now exit the
877interpreter as they expect. (Implemented by Georg Brandl.)
878
879
880%======================================================================
Fred Drake2db76802004-12-01 05:05:47 +0000881\subsection{Optimizations}
882
883\begin{itemize}
884
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000885\item When they were introduced
886in Python 2.4, the built-in \class{set} and \class{frozenset} types
887were built on top of Python's dictionary type.
888In 2.5 the internal data structure has been customized for implementing sets,
889and as a result sets will use a third less memory and are somewhat faster.
890(Implemented by Raymond Hettinger.)
Fred Drake2db76802004-12-01 05:05:47 +0000891
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000892\item The performance of some Unicode operations has been improved.
893% XXX provide details?
894
895\item The code generator's peephole optimizer now performs
896simple constant folding in expressions. If you write something like
897\code{a = 2+3}, the code generator will do the arithmetic and produce
898code corresponding to \code{a = 5}.
899
Fred Drake2db76802004-12-01 05:05:47 +0000900\end{itemize}
901
902The net result of the 2.5 optimizations is that Python 2.5 runs the
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +0000903pystone benchmark around XXX\% faster than Python 2.4.
Fred Drake2db76802004-12-01 05:05:47 +0000904
905
906%======================================================================
907\section{New, Improved, and Deprecated Modules}
908
909As usual, Python's standard library received a number of enhancements and
910bug fixes. Here's a partial list of the most notable changes, sorted
911alphabetically by module name. Consult the
912\file{Misc/NEWS} file in the source tree for a more
Andrew M. Kuchlingf688cc52006-03-10 18:50:08 +0000913complete list of changes, or look through the SVN logs for all the
Fred Drake2db76802004-12-01 05:05:47 +0000914details.
915
916\begin{itemize}
917
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000918% collections.deque now has .remove()
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000919% collections.defaultdict
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000920
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000921% the cPickle module no longer accepts the deprecated None option in the
922% args tuple returned by __reduce__().
923
924% csv module improvements
925
926% datetime.datetime() now has a strptime class method which can be used to
927% create datetime object using a string and format.
928
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000929% fileinput: opening hook used to control how files are opened.
930% .input() now has a mode parameter
931% now has a fileno() function
932% accepts Unicode filenames
933
Andrew M. Kuchlingda376042006-03-17 15:56:41 +0000934\item In the \module{gc} module, the new \function{get_count()} function
935returns a 3-tuple containing the current collection counts for the
936three GC generations. This is accounting information for the garbage
937collector; when these counts reach a specified threshold, a garbage
938collection sweep will be made. The existing \function{gc.collect()}
939function now takes an optional \var{generation} argument of 0, 1, or 2
940to specify which generation to collect.
941
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000942\item The \function{nsmallest()} and
943\function{nlargest()} functions in the \module{heapq} module
944now support a \code{key} keyword argument similar to the one
945provided by the \function{min()}/\function{max()} functions
946and the \method{sort()} methods. For example:
947Example:
948
949\begin{verbatim}
950>>> import heapq
951>>> L = ["short", 'medium', 'longest', 'longer still']
952>>> heapq.nsmallest(2, L) # Return two lowest elements, lexicographically
953['longer still', 'longest']
954>>> heapq.nsmallest(2, L, key=len) # Return two shortest elements
955['short', 'medium']
956\end{verbatim}
957
958(Contributed by Raymond Hettinger.)
959
Andrew M. Kuchling511a3a82005-03-20 19:52:18 +0000960\item The \function{itertools.islice()} function now accepts
961\code{None} for the start and step arguments. This makes it more
962compatible with the attributes of slice objects, so that you can now write
963the following:
964
965\begin{verbatim}
966s = slice(5) # Create slice object
967itertools.islice(iterable, s.start, s.stop, s.step)
968\end{verbatim}
969
970(Contributed by Raymond Hettinger.)
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000971
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000972\item The \module{operator} module's \function{itemgetter()}
973and \function{attrgetter()} functions now support multiple fields.
974A call such as \code{operator.attrgetter('a', 'b')}
975will return a function
976that retrieves the \member{a} and \member{b} attributes. Combining
977this new feature with the \method{sort()} method's \code{key} parameter
978lets you easily sort lists using multiple fields.
979
980% XXX who added?
981
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000982
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000983\item The \module{os} module underwent a number of changes. The
984\member{stat_float_times} variable now defaults to true, meaning that
985\function{os.stat()} will now return time values as floats. (This
986doesn't necessarily mean that \function{os.stat()} will return times
987that are precise to fractions of a second; not all systems support
988such precision.)
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000989
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000990Constants named \member{os.SEEK_SET}, \member{os.SEEK_CUR}, and
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000991\member{os.SEEK_END} have been added; these are the parameters to the
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000992\function{os.lseek()} function. Two new constants for locking are
993\member{os.O_SHLOCK} and \member{os.O_EXLOCK}.
994
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000995Two new functions, \function{wait3()} and \function{wait4()}, were
996added. They're similar the \function{waitpid()} function which waits
997for a child process to exit and returns a tuple of the process ID and
998its exit status, but \function{wait3()} and \function{wait4()} return
999additional information. \function{wait3()} doesn't take a process ID
1000as input, so it waits for any child process to exit and returns a
10013-tuple of \var{process-id}, \var{exit-status}, \var{resource-usage}
1002as returned from the \function{resource.getrusage()} function.
1003\function{wait4(\var{pid})} does take a process ID.
1004(Contributed by XXX.)
1005
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001006On FreeBSD, the \function{os.stat()} function now returns
1007times with nanosecond resolution, and the returned object
1008now has \member{st_gen} and \member{st_birthtime}.
1009The \member{st_flags} member is also available, if the platform supports it.
1010% XXX patch 1180695, 1212117
1011
Andrew M. Kuchling01e3d262006-03-17 15:38:39 +00001012\item The old \module{regex} and \module{regsub} modules, which have been
1013deprecated ever since Python 2.0, have finally been deleted.
Andrew M. Kuchlingf4b06602006-03-17 15:39:52 +00001014Other deleted modules: \module{statcache}, \module{tzparse},
1015\module{whrandom}.
Andrew M. Kuchling01e3d262006-03-17 15:38:39 +00001016
1017\item The \file{lib-old} directory,
1018which includes ancient modules such as \module{dircmp} and
1019\module{ni}, was also deleted. \file{lib-old} wasn't on the default
1020\code{sys.path}, so unless your programs explicitly added the directory to
1021\code{sys.path}, this removal shouldn't affect your code.
1022
Andrew M. Kuchling4678dc82006-01-15 16:11:28 +00001023\item The \module{socket} module now supports \constant{AF_NETLINK}
1024sockets on Linux, thanks to a patch from Philippe Biondi.
1025Netlink sockets are a Linux-specific mechanism for communications
1026between a user-space process and kernel code; an introductory
1027article about them is at \url{http://www.linuxjournal.com/article/7356}.
1028In Python code, netlink addresses are represented as a tuple of 2 integers,
1029\code{(\var{pid}, \var{group_mask})}.
1030
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001031Socket objects also gained accessor methods \method{getfamily()},
1032\method{gettype()}, and \method{getproto()} methods to retrieve the
1033family, type, and protocol values for the socket.
1034
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001035\item New module: \module{spwd} provides functions for accessing the
1036shadow password database on systems that support it.
1037% XXX give example
Fred Drake2db76802004-12-01 05:05:47 +00001038
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001039% XXX patch #1382163: sys.subversion, Py_GetBuildNumber()
1040
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +00001041\item The \class{TarFile} class in the \module{tarfile} module now has
Georg Brandl08c02db2005-07-22 18:39:19 +00001042an \method{extractall()} method that extracts all members from the
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +00001043archive into the current working directory. It's also possible to set
1044a different directory as the extraction target, and to unpack only a
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001045subset of the archive's members.
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +00001046
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001047A tarfile's compression can be autodetected by
1048using the mode \code{'r|*'}.
1049% patch 918101
1050(Contributed by Lars Gust\"abel.)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001051
Andrew M. Kuchlingf688cc52006-03-10 18:50:08 +00001052\item The \module{unicodedata} module has been updated to use version 4.1.0
1053of the Unicode character database. Version 3.2.0 is required
1054by some specifications, so it's still available as
1055\member{unicodedata.db_3_2_0}.
1056
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001057% patch #754022: Greatly enhanced webbrowser.py (by Oleg Broytmann).
1058
Fredrik Lundh7e0aef02005-12-12 18:54:55 +00001059
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001060\item The \module{xmlrpclib} module now supports returning
1061 \class{datetime} objects for the XML-RPC date type. Supply
1062 \code{use_datetime=True} to the \function{loads()} function
1063 or the \class{Unmarshaller} class to enable this feature.
1064% XXX patch 1120353
1065
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001066
Fred Drake114b8ca2005-03-21 05:47:11 +00001067\end{itemize}
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +00001068
Fred Drake2db76802004-12-01 05:05:47 +00001069
1070
1071%======================================================================
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001072% whole new modules get described in subsections here
Fred Drake2db76802004-12-01 05:05:47 +00001073
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001074% XXX new distutils features: upload
1075
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001076\subsection{The ctypes package}
1077
1078The \module{ctypes} package, written by Thomas Heller, has been added
1079to the standard library. \module{ctypes} lets you call arbitrary functions
1080in shared libraries or DLLs.
1081
1082In subsequent alpha releases of Python 2.5, I'll add a brief
1083introduction that shows some basic usage of the module.
1084
1085% XXX write introduction
1086
1087
1088\subsection{The ElementTree package}
1089
1090A subset of Fredrik Lundh's ElementTree library for processing XML has
1091been added to the standard library as \module{xml.etree}. The
1092vailable modules are
1093\module{ElementTree}, \module{ElementPath}, and
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +00001094\module{ElementInclude} from ElementTree 1.2.6.
1095The \module{cElementTree} accelerator module is also included.
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001096
1097In subsequent alpha releases of Python 2.5, I'll add a brief
1098introduction that will provide a page-long overview of using
1099ElementTree. Full documentation for
1100ElementTree is available at \url{http://effbot.org/zone/element-index.htm}.
1101
1102% XXX write introduction
1103
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001104
1105\subsection{The hashlib package}
1106
1107A new \module{hashlib} module has been added to replace the
1108\module{md5} and \module{sha} modules. \module{hashlib} adds support
1109for additional secure hashes (SHA-224, SHA-256, SHA-384, and SHA-512).
1110When available, the module uses OpenSSL for fast platform optimized
1111implementations of algorithms.
1112
1113The old \module{md5} and \module{sha} modules still exist as wrappers
1114around hashlib to preserve backwards compatibility. The new module's
1115interface is very close to that of the old modules, but not identical.
1116The most significant difference is that the constructor functions
1117for creating new hashing objects are named differently.
1118
1119\begin{verbatim}
1120# Old versions
1121h = md5.md5()
1122h = md5.new()
1123
1124# New version
1125h = hashlib.md5()
1126
1127# Old versions
1128h = sha.sha()
1129h = sha.new()
1130
1131# New version
1132h = hashlib.sha1()
1133
1134# Hash that weren't previously available
1135h = hashlib.sha224()
1136h = hashlib.sha256()
1137h = hashlib.sha384()
1138h = hashlib.sha512()
1139
1140# Alternative form
1141h = hashlib.new('md5') # Provide algorithm as a string
1142\end{verbatim}
1143
1144Once a hash object has been created, its methods are the same as before:
1145\method{update(\var{string})} hashes the specified string into the
1146current digest state, \method{digest()} and \method{hexdigest()}
1147return the digest value as a binary string or a string of hex digits,
1148and \method{copy()} returns a new hashing object with the same digest state.
1149
1150This module was contributed by Gregory P. Smith.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001151
1152
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001153\subsection{The sqlite3 package}
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001154
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001155The pysqlite module (\url{http://www.pysqlite.org}), a wrapper for the
1156SQLite embedded database, has been added to the standard library under
1157the package name \module{sqlite3}. SQLite is a C library that
1158provides a SQL-language database that stores data in disk files
1159without requiring a separate server process. pysqlite was written by
1160Gerhard H\"aring, and provides a SQL interface that complies with the
1161DB-API 2.0 specification. This means that it should be possible to
1162write the first version of your applications using SQLite for data
1163storage and, if switching to a larger database such as PostgreSQL or
1164Oracle is necessary, the switch should be relatively easy.
1165
1166If you're compiling the Python source yourself, note that the source
1167tree doesn't include the SQLite code itself, only the wrapper module.
1168You'll need to have the SQLite libraries and headers installed before
1169compiling Python, and the build process will compile the module when
1170the necessary headers are available.
1171
1172In subsequent alpha releases of Python 2.5, I'll add a brief
1173introduction that shows some basic usage of the module.
1174
1175% XXX write introduction
1176
Fred Drake2db76802004-12-01 05:05:47 +00001177
1178% ======================================================================
1179\section{Build and C API Changes}
1180
1181Changes to Python's build process and to the C API include:
1182
1183\begin{itemize}
1184
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +00001185\item The largest change to the C API came from \pep{353},
1186which modifies the interpreter to use a \ctype{Py_ssize_t} type
1187definition instead of \ctype{int}. See the earlier
1188section~ref{section-353} for a discussion of this change.
1189
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001190\item The design of the bytecode compiler has changed a great deal, to
1191no longer generate bytecode by traversing the parse tree. Instead
Andrew M. Kuchlingdb85ed52005-10-23 21:52:59 +00001192the parse tree is converted to an abstract syntax tree (or AST), and it is
1193the abstract syntax tree that's traversed to produce the bytecode.
1194
1195No documentation has been written for the AST code yet. To start
1196learning about it, read the definition of the various AST nodes in
1197\file{Parser/Python.asdl}. A Python script reads this file and
1198generates a set of C structure definitions in
1199\file{Include/Python-ast.h}. The \cfunction{PyParser_ASTFromString()}
1200and \cfunction{PyParser_ASTFromFile()}, defined in
1201\file{Include/pythonrun.h}, take Python source as input and return the
1202root of an AST representing the contents. This AST can then be turned
1203into a code object by \cfunction{PyAST_Compile()}. For more
1204information, read the source code, and then ask questions on
1205python-dev.
1206
1207% List of names taken from Jeremy's python-dev post at
1208% http://mail.python.org/pipermail/python-dev/2005-October/057500.html
1209The AST code was developed under Jeremy Hylton's management, and
1210implemented by (in alphabetical order) Brett Cannon, Nick Coghlan,
1211Grant Edwards, John Ehresman, Kurt Kaiser, Neal Norwitz, Tim Peters,
1212Armin Rigo, and Neil Schemenauer, plus the participants in a number of
1213AST sprints at conferences such as PyCon.
1214
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001215\item The built-in set types now have an official C API. Call
1216\cfunction{PySet_New()} and \cfunction{PyFrozenSet_New()} to create a
1217new set, \cfunction{PySet_Add()} and \cfunction{PySet_Discard()} to
1218add and remove elements, and \cfunction{PySet_Contains} and
1219\cfunction{PySet_Size} to examine the set's state.
1220
1221\item The \cfunction{PyRange_New()} function was removed. It was
1222never documented, never used in the core code, and had dangerously lax
1223error checking.
Fred Drake2db76802004-12-01 05:05:47 +00001224
1225\end{itemize}
1226
1227
1228%======================================================================
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001229%\subsection{Port-Specific Changes}
Fred Drake2db76802004-12-01 05:05:47 +00001230
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001231%Platform-specific changes go here.
Fred Drake2db76802004-12-01 05:05:47 +00001232
1233
1234%======================================================================
1235\section{Other Changes and Fixes \label{section-other}}
1236
1237As usual, there were a bunch of other improvements and bugfixes
Andrew M. Kuchlingf688cc52006-03-10 18:50:08 +00001238scattered throughout the source tree. A search through the SVN change
Fred Drake2db76802004-12-01 05:05:47 +00001239logs finds there were XXX patches applied and YYY bugs fixed between
Andrew M. Kuchling92e24952004-12-03 13:54:09 +00001240Python 2.4 and 2.5. Both figures are likely to be underestimates.
Fred Drake2db76802004-12-01 05:05:47 +00001241
1242Some of the more notable changes are:
1243
1244\begin{itemize}
1245
Andrew M. Kuchling01e3d262006-03-17 15:38:39 +00001246\item Evan Jones's patch to obmalloc, first described in a talk
1247at PyCon DC 2005, was applied. Python 2.4 allocated small objects in
1248256K-sized arenas, but never freed arenas. With this patch, Python
1249will free arenas when they're empty. The net effect is that on some
1250platforms, when you allocate many objects, Python's memory usage may
1251actually drop when you delete them, and the memory may be returned to
1252the operating system. (Implemented by Evan Jones, and reworked by Tim
1253Peters.)
Fred Drake2db76802004-12-01 05:05:47 +00001254
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001255\item Coverity, a company that markets a source code analysis tool
1256 called Prevent, provided the results of their examination of the Python
1257 source code. The analysis found a number of refcounting bugs, often
1258 in error-handling code. These bugs have been fixed.
1259 % XXX provide reference?
1260
Fred Drake2db76802004-12-01 05:05:47 +00001261\end{itemize}
1262
1263
1264%======================================================================
1265\section{Porting to Python 2.5}
1266
1267This section lists previously described changes that may require
1268changes to your code:
1269
1270\begin{itemize}
1271
Andrew M. Kuchlingc3749a92006-04-04 19:14:41 +00001272\item The \module{pickle} module no longer uses the deprecated \var{bin} parameter.
Fred Drake2db76802004-12-01 05:05:47 +00001273
1274\end{itemize}
1275
1276
1277%======================================================================
1278\section{Acknowledgements \label{acks}}
1279
1280The author would like to thank the following people for offering
1281suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +00001282article: Thomas Wouters.
Fred Drake2db76802004-12-01 05:05:47 +00001283
1284\end{document}