blob: af247a45bf70941122e5df72d9680fdf19302c81 [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. Kuchling38f85072006-04-02 01:46:32 +00006% The easy_install stuff
Andrew M. Kuchling075e0232006-04-11 13:14:56 +00007% Stateful codec changes
Andrew M. Kuchling75ba2442006-04-14 10:29:55 +00008% cProfile
Andrew M. Kuchling5f445bf2006-04-12 18:54:00 +00009% Count up the patches and bugs
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. Kuchling6a67e4e2006-04-12 13:03:35 +000039\section{PEP 243: Uploading Modules to PyPI}
40
41PEP 243 describes an HTTP-based protocol for submitting software
42packages to a central archive. The Python package index at
43\url{http://cheeseshop.python.org} now supports package uploads, and
44the new \command{upload} Distutils command will upload a package to the
45repository.
46
47Before a package can be uploaded, you must be able to build a
48distribution using the \command{sdist} Distutils command. Once that
49works, you can run \code{python setup.py upload} to add your package
50to the PyPI archive. Optionally you can GPG-sign the package by
51supplying the \programopt{--sign} and
52\programopt{--identity} options.
53
54\begin{seealso}
55
56\seepep{243}{Module Repository Upload Mechanism}{PEP written by
Andrew M. Kuchling5f445bf2006-04-12 18:54:00 +000057Sean Reifschneider; implemented by Martin von~L\"owis
Andrew M. Kuchling6a67e4e2006-04-12 13:03:35 +000058and Richard Jones. Note that the PEP doesn't exactly
59describe what's implemented in PyPI.}
60
61\end{seealso}
62
63
64%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +000065\section{PEP 308: Conditional Expressions}
66
Andrew M. Kuchlinge362d932006-03-09 13:56:25 +000067For a long time, people have been requesting a way to write
68conditional expressions, expressions that return value A or value B
69depending on whether a Boolean value is true or false. A conditional
70expression lets you write a single assignment statement that has the
71same effect as the following:
72
73\begin{verbatim}
74if condition:
75 x = true_value
76else:
77 x = false_value
78\end{verbatim}
79
80There have been endless tedious discussions of syntax on both
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +000081python-dev and comp.lang.python. A vote was even held that found the
82majority of voters wanted conditional expressions in some form,
83but there was no syntax that was preferred by a clear majority.
84Candidates included C's \code{cond ? true_v : false_v},
Andrew M. Kuchlinge362d932006-03-09 13:56:25 +000085\code{if cond then true_v else false_v}, and 16 other variations.
86
87GvR eventually chose a surprising syntax:
88
89\begin{verbatim}
90x = true_value if condition else false_value
91\end{verbatim}
92
Andrew M. Kuchling38f85072006-04-02 01:46:32 +000093Evaluation is still lazy as in existing Boolean expressions, so the
94order of evaluation jumps around a bit. The \var{condition}
95expression in the middle is evaluated first, and the \var{true_value}
96expression is evaluated only if the condition was true. Similarly,
97the \var{false_value} expression is only evaluated when the condition
98is false.
Andrew M. Kuchlinge362d932006-03-09 13:56:25 +000099
100This syntax may seem strange and backwards; why does the condition go
101in the \emph{middle} of the expression, and not in the front as in C's
102\code{c ? x : y}? The decision was checked by applying the new syntax
103to the modules in the standard library and seeing how the resulting
104code read. In many cases where a conditional expression is used, one
105value seems to be the 'common case' and one value is an 'exceptional
106case', used only on rarer occasions when the condition isn't met. The
107conditional syntax makes this pattern a bit more obvious:
108
109\begin{verbatim}
110contents = ((doc + '\n') if doc else '')
111\end{verbatim}
112
113I read the above statement as meaning ``here \var{contents} is
Andrew M. Kuchlingd0fcc022006-03-09 13:57:28 +0000114usually assigned a value of \code{doc+'\e n'}; sometimes
Andrew M. Kuchlinge362d932006-03-09 13:56:25 +0000115\var{doc} is empty, in which special case an empty string is returned.''
116I doubt I will use conditional expressions very often where there
117isn't a clear common and uncommon case.
118
119There was some discussion of whether the language should require
120surrounding conditional expressions with parentheses. The decision
121was made to \emph{not} require parentheses in the Python language's
122grammar, but as a matter of style I think you should always use them.
123Consider these two statements:
124
125\begin{verbatim}
126# First version -- no parens
127level = 1 if logging else 0
128
129# Second version -- with parens
130level = (1 if logging else 0)
131\end{verbatim}
132
133In the first version, I think a reader's eye might group the statement
134into 'level = 1', 'if logging', 'else 0', and think that the condition
135decides whether the assignment to \var{level} is performed. The
136second version reads better, in my opinion, because it makes it clear
137that the assignment is always performed and the choice is being made
138between two values.
139
140Another reason for including the brackets: a few odd combinations of
141list comprehensions and lambdas could look like incorrect conditional
142expressions. See \pep{308} for some examples. If you put parentheses
143around your conditional expressions, you won't run into this case.
144
145
146\begin{seealso}
147
148\seepep{308}{Conditional Expressions}{PEP written by
149Guido van Rossum and Raymond D. Hettinger; implemented by Thomas
150Wouters.}
151
152\end{seealso}
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000153
154
155%======================================================================
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000156\section{PEP 309: Partial Function Application}
Fred Drake2db76802004-12-01 05:05:47 +0000157
Andrew M. Kuchlingb1c96fd2005-03-20 21:42:04 +0000158The \module{functional} module is intended to contain tools for
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000159functional-style programming. Currently it only contains a
160\class{partial()} function, but new functions will probably be added
161in future versions of Python.
Andrew M. Kuchlingb1c96fd2005-03-20 21:42:04 +0000162
Andrew M. Kuchling4b000cd2005-04-09 15:51:44 +0000163For programs written in a functional style, it can be useful to
164construct variants of existing functions that have some of the
165parameters filled in. Consider a Python function \code{f(a, b, c)};
166you could create a new function \code{g(b, c)} that was equivalent to
167\code{f(1, b, c)}. This is called ``partial function application'',
168and is provided by the \class{partial} class in the new
169\module{functional} module.
170
171The constructor for \class{partial} takes the arguments
172\code{(\var{function}, \var{arg1}, \var{arg2}, ...
173\var{kwarg1}=\var{value1}, \var{kwarg2}=\var{value2})}. The resulting
174object is callable, so you can just call it to invoke \var{function}
175with the filled-in arguments.
176
177Here's a small but realistic example:
178
179\begin{verbatim}
180import functional
181
182def log (message, subsystem):
183 "Write the contents of 'message' to the specified subsystem."
184 print '%s: %s' % (subsystem, message)
185 ...
186
187server_log = functional.partial(log, subsystem='server')
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000188server_log('Unable to open socket')
Andrew M. Kuchling4b000cd2005-04-09 15:51:44 +0000189\end{verbatim}
190
Andrew M. Kuchling6af7fe02005-08-02 17:20:36 +0000191Here's another example, from a program that uses PyGTk. Here a
192context-sensitive pop-up menu is being constructed dynamically. The
193callback provided for the menu option is a partially applied version
194of the \method{open_item()} method, where the first argument has been
195provided.
Andrew M. Kuchling4b000cd2005-04-09 15:51:44 +0000196
Andrew M. Kuchling6af7fe02005-08-02 17:20:36 +0000197\begin{verbatim}
198...
199class Application:
200 def open_item(self, path):
201 ...
202 def init (self):
203 open_func = functional.partial(self.open_item, item_path)
204 popup_menu.append( ("Open", open_func, 1) )
205\end{verbatim}
Andrew M. Kuchlingb1c96fd2005-03-20 21:42:04 +0000206
207
208\begin{seealso}
209
210\seepep{309}{Partial Function Application}{PEP proposed and written by
211Peter Harris; implemented by Hye-Shik Chang, with adaptations by
212Raymond Hettinger.}
213
214\end{seealso}
Fred Drake2db76802004-12-01 05:05:47 +0000215
216
217%======================================================================
Fred Drakedb7b0022005-03-20 22:19:47 +0000218\section{PEP 314: Metadata for Python Software Packages v1.1}
219
Andrew M. Kuchlingd8d732e2005-04-09 23:59:41 +0000220Some simple dependency support was added to Distutils. The
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000221\function{setup()} function now has \code{requires}, \code{provides},
222and \code{obsoletes} keyword parameters. When you build a source
223distribution using the \code{sdist} command, the dependency
224information will be recorded in the \file{PKG-INFO} file.
Andrew M. Kuchlingd8d732e2005-04-09 23:59:41 +0000225
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000226Another new keyword parameter is \code{download_url}, which should be
227set to a URL for the package's source code. This means it's now
228possible to look up an entry in the package index, determine the
229dependencies for a package, and download the required packages.
Andrew M. Kuchlingd8d732e2005-04-09 23:59:41 +0000230
Andrew M. Kuchling61434b62006-04-13 11:51:07 +0000231\begin{verbatim}
232VERSION = '1.0'
233setup(name='PyPackage',
234 version=VERSION,
235 requires=['numarray', 'zlib (>=1.1.4)'],
236 obsoletes=['OldPackage']
237 download_url=('http://www.example.com/pypackage/dist/pkg-%s.tar.gz'
238 % VERSION),
239 )
240\end{verbatim}
Andrew M. Kuchlingd8d732e2005-04-09 23:59:41 +0000241
242\begin{seealso}
243
244\seepep{314}{Metadata for Python Software Packages v1.1}{PEP proposed
245and written by A.M. Kuchling, Richard Jones, and Fred Drake;
246implemented by Richard Jones and Fred Drake.}
247
248\end{seealso}
Fred Drakedb7b0022005-03-20 22:19:47 +0000249
250
251%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000252\section{PEP 328: Absolute and Relative Imports}
253
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000254The simpler part of PEP 328 was implemented in Python 2.4: parentheses
255could now be used to enclose the names imported from a module using
256the \code{from ... import ...} statement, making it easier to import
257many different names.
258
259The more complicated part has been implemented in Python 2.5:
260importing a module can be specified to use absolute or
261package-relative imports. The plan is to move toward making absolute
262imports the default in future versions of Python.
263
264Let's say you have a package directory like this:
265\begin{verbatim}
266pkg/
267pkg/__init__.py
268pkg/main.py
269pkg/string.py
270\end{verbatim}
271
272This defines a package named \module{pkg} containing the
273\module{pkg.main} and \module{pkg.string} submodules.
274
275Consider the code in the \file{main.py} module. What happens if it
276executes the statement \code{import string}? In Python 2.4 and
277earlier, it will first look in the package's directory to perform a
278relative import, finds \file{pkg/string.py}, imports the contents of
279that file as the \module{pkg.string} module, and that module is bound
280to the name \samp{string} in the \module{pkg.main} module's namespace.
281
282That's fine if \module{pkg.string} was what you wanted. But what if
283you wanted Python's standard \module{string} module? There's no clean
284way to ignore \module{pkg.string} and look for the standard module;
285generally you had to look at the contents of \code{sys.modules}, which
286is slightly unclean.
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000287Holger Krekel's \module{py.std} package provides a tidier way to perform
288imports from the standard library, \code{import py ; py.std.string.join()},
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000289but that package isn't available on all Python installations.
290
291Reading code which relies on relative imports is also less clear,
292because a reader may be confused about which module, \module{string}
293or \module{pkg.string}, is intended to be used. Python users soon
294learned not to duplicate the names of standard library modules in the
295names of their packages' submodules, but you can't protect against
296having your submodule's name being used for a new module added in a
297future version of Python.
298
299In Python 2.5, you can switch \keyword{import}'s behaviour to
300absolute imports using a \code{from __future__ import absolute_import}
301directive. This absolute-import behaviour will become the default in
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000302a future version (probably Python 2.7). Once absolute imports
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000303are the default, \code{import string} will
304always find the standard library's version.
305It's suggested that users should begin using absolute imports as much
306as possible, so it's preferable to begin writing \code{from pkg import
307string} in your code.
308
309Relative imports are still possible by adding a leading period
310to the module name when using the \code{from ... import} form:
311
312\begin{verbatim}
313# Import names from pkg.string
314from .string import name1, name2
315# Import pkg.string
316from . import string
317\end{verbatim}
318
319This imports the \module{string} module relative to the current
320package, so in \module{pkg.main} this will import \var{name1} and
321\var{name2} from \module{pkg.string}. Additional leading periods
322perform the relative import starting from the parent of the current
323package. For example, code in the \module{A.B.C} module can do:
324
325\begin{verbatim}
326from . import D # Imports A.B.D
327from .. import E # Imports A.E
328from ..F import G # Imports A.F.G
329\end{verbatim}
330
331Leading periods cannot be used with the \code{import \var{modname}}
332form of the import statement, only the \code{from ... import} form.
333
334\begin{seealso}
335
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000336\seepep{328}{Imports: Multi-Line and Absolute/Relative}
337{PEP written by Aahz; implemented by Thomas Wouters.}
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000338
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000339\seeurl{http://codespeak.net/py/current/doc/index.html}
340{The py library by Holger Krekel, which contains the \module{py.std} package.}
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000341
342\end{seealso}
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000343
344
345%======================================================================
Andrew M. Kuchling21d3a7c2006-03-15 11:53:09 +0000346\section{PEP 338: Executing Modules as Scripts}
347
Andrew M. Kuchlingb182db42006-03-17 21:48:46 +0000348The \programopt{-m} switch added in Python 2.4 to execute a module as
349a script gained a few more abilities. Instead of being implemented in
350C code inside the Python interpreter, the switch now uses an
351implementation in a new module, \module{runpy}.
352
353The \module{runpy} module implements a more sophisticated import
354mechanism so that it's now possible to run modules in a package such
355as \module{pychecker.checker}. The module also supports alternative
Andrew M. Kuchling5d4cf5e2006-04-13 13:02:42 +0000356import mechanisms such as the \module{zipimport} module. This means
Andrew M. Kuchlingb182db42006-03-17 21:48:46 +0000357you can add a .zip archive's path to \code{sys.path} and then use the
358\programopt{-m} switch to execute code from the archive.
359
360
361\begin{seealso}
362
363\seepep{338}{Executing modules as scripts}{PEP written and
364implemented by Nick Coghlan.}
365
366\end{seealso}
Andrew M. Kuchling21d3a7c2006-03-15 11:53:09 +0000367
368
369%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000370\section{PEP 341: Unified try/except/finally}
371
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000372Until Python 2.5, the \keyword{try} statement came in two
373flavours. You could use a \keyword{finally} block to ensure that code
Andrew M. Kuchling0f1955d2006-04-13 12:09:08 +0000374is always executed, or one or more \keyword{except} blocks to catch
375specific exceptions. You couldn't combine both \keyword{except} blocks and a
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000376\keyword{finally} block, because generating the right bytecode for the
377combined version was complicated and it wasn't clear what the
378semantics of the combined should be.
379
380GvR spent some time working with Java, which does support the
381equivalent of combining \keyword{except} blocks and a
382\keyword{finally} block, and this clarified what the statement should
383mean. In Python 2.5, you can now write:
384
385\begin{verbatim}
386try:
387 block-1 ...
388except Exception1:
389 handler-1 ...
390except Exception2:
391 handler-2 ...
392else:
393 else-block
394finally:
395 final-block
396\end{verbatim}
397
398The code in \var{block-1} is executed. If the code raises an
399exception, the handlers are tried in order: \var{handler-1},
400\var{handler-2}, ... If no exception is raised, the \var{else-block}
401is executed. No matter what happened previously, the
402\var{final-block} is executed once the code block is complete and any
403raised exceptions handled. Even if there's an error in an exception
404handler or the \var{else-block} and a new exception is raised, the
405\var{final-block} is still executed.
406
407\begin{seealso}
408
409\seepep{341}{Unifying try-except and try-finally}{PEP written by Georg Brandl;
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +0000410implementation by Thomas Lee.}
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000411
412\end{seealso}
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000413
414
415%======================================================================
Andrew M. Kuchling3b4fb042006-04-13 12:49:39 +0000416\section{PEP 342: New Generator Features\label{section-generators}}
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000417
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000418Python 2.5 adds a simple way to pass values \emph{into} a generator.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000419As introduced in Python 2.3, generators only produce output; once a
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000420generator's code is invoked to create an iterator, there's no way to
421pass any new information into the function when its execution is
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000422resumed. Sometimes the ability to pass in some information would be
423useful. Hackish solutions to this include making the generator's code
424look at a global variable and then changing the global variable's
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000425value, or passing in some mutable object that callers then modify.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000426
427To refresh your memory of basic generators, here's a simple example:
428
429\begin{verbatim}
430def counter (maximum):
431 i = 0
432 while i < maximum:
433 yield i
434 i += 1
435\end{verbatim}
436
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000437When you call \code{counter(10)}, the result is an iterator that
438returns the values from 0 up to 9. On encountering the
439\keyword{yield} statement, the iterator returns the provided value and
440suspends the function's execution, preserving the local variables.
441Execution resumes on the following call to the iterator's
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000442\method{next()} method, picking up after the \keyword{yield} statement.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000443
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000444In Python 2.3, \keyword{yield} was a statement; it didn't return any
445value. In 2.5, \keyword{yield} is now an expression, returning a
446value that can be assigned to a variable or otherwise operated on:
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000447
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000448\begin{verbatim}
449val = (yield i)
450\end{verbatim}
451
452I recommend that you always put parentheses around a \keyword{yield}
453expression when you're doing something with the returned value, as in
454the above example. The parentheses aren't always necessary, but it's
455easier to always add them instead of having to remember when they're
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000456needed.\footnote{The exact rules are that a \keyword{yield}-expression must
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000457always be parenthesized except when it occurs at the top-level
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000458expression on the right-hand side of an assignment, meaning you can
459write \code{val = yield i} but have to use parentheses when there's an
460operation, as in \code{val = (yield i) + 12}.}
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000461
462Values are sent into a generator by calling its
463\method{send(\var{value})} method. The generator's code is then
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000464resumed and the \keyword{yield} expression returns the specified
465\var{value}. If the regular \method{next()} method is called, the
466\keyword{yield} returns \constant{None}.
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000467
468Here's the previous example, modified to allow changing the value of
469the internal counter.
470
471\begin{verbatim}
472def counter (maximum):
473 i = 0
474 while i < maximum:
475 val = (yield i)
476 # If value provided, change counter
477 if val is not None:
478 i = val
479 else:
480 i += 1
481\end{verbatim}
482
483And here's an example of changing the counter:
484
485\begin{verbatim}
486>>> it = counter(10)
487>>> print it.next()
4880
489>>> print it.next()
4901
491>>> print it.send(8)
4928
493>>> print it.next()
4949
495>>> print it.next()
496Traceback (most recent call last):
497 File ``t.py'', line 15, in ?
498 print it.next()
499StopIteration
Andrew M. Kuchlingc2033702005-08-29 13:30:12 +0000500\end{verbatim}
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000501
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000502Because \keyword{yield} will often be returning \constant{None}, you
503should always check for this case. Don't just use its value in
504expressions unless you're sure that the \method{send()} method
505will be the only method used resume your generator function.
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000506
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000507In addition to \method{send()}, there are two other new methods on
508generators:
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000509
510\begin{itemize}
511
512 \item \method{throw(\var{type}, \var{value}=None,
513 \var{traceback}=None)} is used to raise an exception inside the
514 generator; the exception is raised by the \keyword{yield} expression
515 where the generator's execution is paused.
516
517 \item \method{close()} raises a new \exception{GeneratorExit}
518 exception inside the generator to terminate the iteration.
519 On receiving this
520 exception, the generator's code must either raise
521 \exception{GeneratorExit} or \exception{StopIteration}; catching the
522 exception and doing anything else is illegal and will trigger
523 a \exception{RuntimeError}. \method{close()} will also be called by
524 Python's garbage collection when the generator is garbage-collected.
525
526 If you need to run cleanup code in case of a \exception{GeneratorExit},
527 I suggest using a \code{try: ... finally:} suite instead of
528 catching \exception{GeneratorExit}.
529
530\end{itemize}
531
532The cumulative effect of these changes is to turn generators from
533one-way producers of information into both producers and consumers.
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000534
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000535Generators also become \emph{coroutines}, a more generalized form of
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000536subroutines. Subroutines are entered at one point and exited at
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000537another point (the top of the function, and a \keyword{return
538statement}), but coroutines can be entered, exited, and resumed at
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000539many different points (the \keyword{yield} statements). We'll have to
540figure out patterns for using coroutines effectively in Python.
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000541
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000542The addition of the \method{close()} method has one side effect that
543isn't obvious. \method{close()} is called when a generator is
544garbage-collected, so this means the generator's code gets one last
Andrew M. Kuchling3b4fb042006-04-13 12:49:39 +0000545chance to run before the generator is destroyed. This last chance
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000546means that \code{try...finally} statements in generators can now be
547guaranteed to work; the \keyword{finally} clause will now always get a
548chance to run. The syntactic restriction that you couldn't mix
549\keyword{yield} statements with a \code{try...finally} suite has
550therefore been removed. This seems like a minor bit of language
551trivia, but using generators and \code{try...finally} is actually
552necessary in order to implement the \keyword{with} statement
553described by PEP 343. We'll look at this new statement in the following
554section.
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000555
Andrew M. Kuchling3b4fb042006-04-13 12:49:39 +0000556Another even more esoteric effect of this change: previously, the
557\member{gi_frame} attribute of a generator was always a frame object.
558It's now possible for \member{gi_frame} to be \code{None}
559once the generator has been exhausted.
560
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000561\begin{seealso}
562
563\seepep{342}{Coroutines via Enhanced Generators}{PEP written by
564Guido van Rossum and Phillip J. Eby;
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000565implemented by Phillip J. Eby. Includes examples of
566some fancier uses of generators as coroutines.}
567
568\seeurl{http://en.wikipedia.org/wiki/Coroutine}{The Wikipedia entry for
569coroutines.}
570
Neal Norwitz09179882006-03-04 23:31:45 +0000571\seeurl{http://www.sidhe.org/\~{}dan/blog/archives/000178.html}{An
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000572explanation of coroutines from a Perl point of view, written by Dan
573Sugalski.}
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000574
575\end{seealso}
576
577
578%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000579\section{PEP 343: The 'with' statement}
580
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000581The \keyword{with} statement allows a clearer
582version of code that uses \code{try...finally} blocks
583
584First, I'll discuss the statement as it will commonly be used, and
585then I'll discuss the detailed implementation and how to write objects
586(called ``context managers'') that can be used with this statement.
587Most people, who will only use \keyword{with} in company with an
Andrew M. Kuchlinga4d651f2006-04-06 13:24:58 +0000588existing object, don't need to know these details and can
589just use objects that are documented to work as context managers.
590Authors of new context managers will need to understand the details of
591the underlying implementation.
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000592
593The \keyword{with} statement is a new control-flow structure whose
594basic structure is:
595
596\begin{verbatim}
597with expression as variable:
598 with-block
599\end{verbatim}
600
601The expression is evaluated, and it should result in a type of object
602that's called a context manager. The context manager can return a
603value that will be bound to the name \var{variable}. (Note carefully:
604\var{variable} is \emph{not} assigned the result of \var{expression}.
605One method of the context manager is run before \var{with-block} is
606executed, and another method is run after the block is done, even if
607the block raised an exception.
608
609To enable the statement in Python 2.5, you need
610to add the following directive to your module:
611
612\begin{verbatim}
613from __future__ import with_statement
614\end{verbatim}
615
616Some standard Python objects can now behave as context managers. For
617example, file objects:
618
619\begin{verbatim}
620with open('/etc/passwd', 'r') as f:
621 for line in f:
622 print line
623
624# f has been automatically closed at this point.
625\end{verbatim}
626
627The \module{threading} module's locks and condition variables
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +0000628also support the \keyword{with} statement:
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000629
630\begin{verbatim}
631lock = threading.Lock()
632with lock:
633 # Critical section of code
634 ...
635\end{verbatim}
636
637The lock is acquired before the block is executed, and released once
638the block is complete.
639
640The \module{decimal} module's contexts, which encapsulate the desired
641precision and rounding characteristics for computations, can also be
642used as context managers.
643
644\begin{verbatim}
645import decimal
646
647v1 = decimal.Decimal('578')
648
649# Displays with default precision of 28 digits
650print v1.sqrt()
651
652with decimal.Context(prec=16):
653 # All code in this block uses a precision of 16 digits.
654 # The original context is restored on exiting the block.
655 print v1.sqrt()
656\end{verbatim}
657
658\subsection{Writing Context Managers}
659
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000660% XXX write this
661
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +0000662This section still needs to be written.
663
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000664The new \module{contextlib} module provides some functions and a
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +0000665decorator that are useful for writing context managers.
666Future versions will go into more detail.
667
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000668% XXX describe further
669
670\begin{seealso}
671
672\seepep{343}{The ``with'' statement}{PEP written by
673Guido van Rossum and Nick Coghlan. }
674
675\end{seealso}
676
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000677
678%======================================================================
Andrew M. Kuchling8f4d2552006-03-08 01:50:20 +0000679\section{PEP 352: Exceptions as New-Style Classes}
680
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000681Exception classes can now be new-style classes, not just classic
682classes, and the built-in \exception{Exception} class and all the
683standard built-in exceptions (\exception{NameError},
684\exception{ValueError}, etc.) are now new-style classes.
Andrew M. Kuchlingaeadf952006-03-09 19:06:05 +0000685
686The inheritance hierarchy for exceptions has been rearranged a bit.
687In 2.5, the inheritance relationships are:
688
689\begin{verbatim}
690BaseException # New in Python 2.5
691|- KeyboardInterrupt
692|- SystemExit
693|- Exception
694 |- (all other current built-in exceptions)
695\end{verbatim}
696
697This rearrangement was done because people often want to catch all
698exceptions that indicate program errors. \exception{KeyboardInterrupt} and
699\exception{SystemExit} aren't errors, though, and usually represent an explicit
700action such as the user hitting Control-C or code calling
701\function{sys.exit()}. A bare \code{except:} will catch all exceptions,
702so you commonly need to list \exception{KeyboardInterrupt} and
703\exception{SystemExit} in order to re-raise them. The usual pattern is:
704
705\begin{verbatim}
706try:
707 ...
708except (KeyboardInterrupt, SystemExit):
709 raise
710except:
711 # Log error...
712 # Continue running program...
713\end{verbatim}
714
715In Python 2.5, you can now write \code{except Exception} to achieve
716the same result, catching all the exceptions that usually indicate errors
717but leaving \exception{KeyboardInterrupt} and
718\exception{SystemExit} alone. As in previous versions,
719a bare \code{except:} still catches all exceptions.
720
721The goal for Python 3.0 is to require any class raised as an exception
722to derive from \exception{BaseException} or some descendant of
723\exception{BaseException}, and future releases in the
724Python 2.x series may begin to enforce this constraint. Therefore, I
725suggest you begin making all your exception classes derive from
726\exception{Exception} now. It's been suggested that the bare
727\code{except:} form should be removed in Python 3.0, but Guido van~Rossum
728hasn't decided whether to do this or not.
729
730Raising of strings as exceptions, as in the statement \code{raise
731"Error occurred"}, is deprecated in Python 2.5 and will trigger a
732warning. The aim is to be able to remove the string-exception feature
733in a few releases.
734
735
736\begin{seealso}
737
Andrew M. Kuchlingc3749a92006-04-04 19:14:41 +0000738\seepep{352}{Required Superclass for Exceptions}{PEP written by
Andrew M. Kuchlingaeadf952006-03-09 19:06:05 +0000739Brett Cannon and Guido van Rossum; implemented by Brett Cannon.}
740
741\end{seealso}
Andrew M. Kuchling8f4d2552006-03-08 01:50:20 +0000742
743
744%======================================================================
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000745\section{PEP 353: Using ssize_t as the index type\label{section-353}}
Andrew M. Kuchlingc3749a92006-04-04 19:14:41 +0000746
747A wide-ranging change to Python's C API, using a new
748\ctype{Py_ssize_t} type definition instead of \ctype{int},
749will permit the interpreter to handle more data on 64-bit platforms.
750This change doesn't affect Python's capacity on 32-bit platforms.
751
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000752Various pieces of the Python interpreter used C's \ctype{int} type to
753store sizes or counts; for example, the number of items in a list or
754tuple were stored in an \ctype{int}. The C compilers for most 64-bit
755platforms still define \ctype{int} as a 32-bit type, so that meant
756that lists could only hold up to \code{2**31 - 1} = 2147483647 items.
757(There are actually a few different programming models that 64-bit C
758compilers can use -- see
759\url{http://www.unix.org/version2/whatsnew/lp64_wp.html} for a
760discussion -- but the most commonly available model leaves \ctype{int}
761as 32 bits.)
762
763A limit of 2147483647 items doesn't really matter on a 32-bit platform
764because you'll run out of memory before hitting the length limit.
765Each list item requires space for a pointer, which is 4 bytes, plus
766space for a \ctype{PyObject} representing the item. 2147483647*4 is
767already more bytes than a 32-bit address space can contain.
768
769It's possible to address that much memory on a 64-bit platform,
770however. The pointers for a list that size would only require 16GiB
771of space, so it's not unreasonable that Python programmers might
772construct lists that large. Therefore, the Python interpreter had to
773be changed to use some type other than \ctype{int}, and this will be a
77464-bit type on 64-bit platforms. The change will cause
775incompatibilities on 64-bit machines, so it was deemed worth making
776the transition now, while the number of 64-bit users is still
777relatively small. (In 5 or 10 years, we may \emph{all} be on 64-bit
778machines, and the transition would be more painful then.)
779
780This change most strongly affects authors of C extension modules.
781Python strings and container types such as lists and tuples
782now use \ctype{Py_ssize_t} to store their size.
783Functions such as \cfunction{PyList_Size()}
784now return \ctype{Py_ssize_t}. Code in extension modules
785may therefore need to have some variables changed to
786\ctype{Py_ssize_t}.
787
788The \cfunction{PyArg_ParseTuple()} and \cfunction{Py_BuildValue()} functions
789have a new conversion code, \samp{n}, for \ctype{Py_ssize_t}.
Andrew M. Kuchlinga4d651f2006-04-06 13:24:58 +0000790\cfunction{PyArg_ParseTuple()}'s \samp{s\#} and \samp{t\#} still output
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000791\ctype{int} by default, but you can define the macro
792\csimplemacro{PY_SSIZE_T_CLEAN} before including \file{Python.h}
793to make them return \ctype{Py_ssize_t}.
794
795\pep{353} has a section on conversion guidelines that
796extension authors should read to learn about supporting 64-bit
797platforms.
Andrew M. Kuchlingc3749a92006-04-04 19:14:41 +0000798
799\begin{seealso}
800
Andrew M. Kuchling5f445bf2006-04-12 18:54:00 +0000801\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 +0000802
803\end{seealso}
804
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +0000805
Andrew M. Kuchlingc3749a92006-04-04 19:14:41 +0000806%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000807\section{PEP 357: The '__index__' method}
808
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000809The NumPy developers had a problem that could only be solved by adding
810a new special method, \method{__index__}. When using slice notation,
Fred Drake1c0e3282006-04-02 03:30:06 +0000811as in \code{[\var{start}:\var{stop}:\var{step}]}, the values of the
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000812\var{start}, \var{stop}, and \var{step} indexes must all be either
813integers or long integers. NumPy defines a variety of specialized
814integer types corresponding to unsigned and signed integers of 8, 16,
81532, and 64 bits, but there was no way to signal that these types could
816be used as slice indexes.
817
818Slicing can't just use the existing \method{__int__} method because
819that method is also used to implement coercion to integers. If
820slicing used \method{__int__}, floating-point numbers would also
821become legal slice indexes and that's clearly an undesirable
822behaviour.
823
824Instead, a new special method called \method{__index__} was added. It
825takes no arguments and returns an integer giving the slice index to
826use. For example:
827
828\begin{verbatim}
829class C:
830 def __index__ (self):
831 return self.value
832\end{verbatim}
833
834The return value must be either a Python integer or long integer.
835The interpreter will check that the type returned is correct, and
836raises a \exception{TypeError} if this requirement isn't met.
837
838A corresponding \member{nb_index} slot was added to the C-level
839\ctype{PyNumberMethods} structure to let C extensions implement this
840protocol. \cfunction{PyNumber_Index(\var{obj})} can be used in
841extension code to call the \method{__index__} function and retrieve
842its result.
843
844\begin{seealso}
845
846\seepep{357}{Allowing Any Object to be Used for Slicing}{PEP written
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +0000847and implemented by Travis Oliphant.}
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000848
849\end{seealso}
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000850
851
852%======================================================================
Fred Drake2db76802004-12-01 05:05:47 +0000853\section{Other Language Changes}
854
855Here are all of the changes that Python 2.5 makes to the core Python
856language.
857
858\begin{itemize}
Andrew M. Kuchling1cae3f52004-12-03 14:57:21 +0000859
860\item The \function{min()} and \function{max()} built-in functions
861gained a \code{key} keyword argument analogous to the \code{key}
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000862argument for \method{sort()}. This argument supplies a function
Andrew M. Kuchling1cae3f52004-12-03 14:57:21 +0000863that takes a single argument and is called for every value in the list;
864\function{min()}/\function{max()} will return the element with the
865smallest/largest return value from this function.
866For example, to find the longest string in a list, you can do:
867
868\begin{verbatim}
869L = ['medium', 'longest', 'short']
870# Prints 'longest'
871print max(L, key=len)
872# Prints 'short', because lexicographically 'short' has the largest value
873print max(L)
874\end{verbatim}
875
876(Contributed by Steven Bethard and Raymond Hettinger.)
Fred Drake2db76802004-12-01 05:05:47 +0000877
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000878\item Two new built-in functions, \function{any()} and
879\function{all()}, evaluate whether an iterator contains any true or
880false values. \function{any()} returns \constant{True} if any value
881returned by the iterator is true; otherwise it will return
882\constant{False}. \function{all()} returns \constant{True} only if
883all of the values returned by the iterator evaluate as being true.
Andrew M. Kuchling6e3a66d2006-04-07 12:46:06 +0000884(Suggested by GvR, and implemented by Raymond Hettinger.)
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000885
Andrew M. Kuchling5f445bf2006-04-12 18:54:00 +0000886\item ASCII is now the default encoding for modules. It's now
887a syntax error if a module contains string literals with 8-bit
888characters but doesn't have an encoding declaration. In Python 2.4
889this triggered a warning, not a syntax error. See \pep{263}
890for how to declare a module's encoding; for example, you might add
891a line like this near the top of the source file:
892
893\begin{verbatim}
894# -*- coding: latin1 -*-
895\end{verbatim}
896
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000897\item The list of base classes in a class definition can now be empty.
898As an example, this is now legal:
899
900\begin{verbatim}
901class C():
902 pass
903\end{verbatim}
904(Implemented by Brett Cannon.)
905
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000906% XXX __missing__ hook in dictionaries
907
Fred Drake2db76802004-12-01 05:05:47 +0000908\end{itemize}
909
910
911%======================================================================
Andrew M. Kuchlingda376042006-03-17 15:56:41 +0000912\subsection{Interactive Interpreter Changes}
913
914In the interactive interpreter, \code{quit} and \code{exit}
915have long been strings so that new users get a somewhat helpful message
916when they try to quit:
917
918\begin{verbatim}
919>>> quit
920'Use Ctrl-D (i.e. EOF) to exit.'
921\end{verbatim}
922
923In Python 2.5, \code{quit} and \code{exit} are now objects that still
924produce string representations of themselves, but are also callable.
925Newbies who try \code{quit()} or \code{exit()} will now exit the
926interpreter as they expect. (Implemented by Georg Brandl.)
927
928
929%======================================================================
Fred Drake2db76802004-12-01 05:05:47 +0000930\subsection{Optimizations}
931
932\begin{itemize}
933
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000934\item When they were introduced
935in Python 2.4, the built-in \class{set} and \class{frozenset} types
936were built on top of Python's dictionary type.
937In 2.5 the internal data structure has been customized for implementing sets,
938and as a result sets will use a third less memory and are somewhat faster.
939(Implemented by Raymond Hettinger.)
Fred Drake2db76802004-12-01 05:05:47 +0000940
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000941\item The performance of some Unicode operations has been improved.
942% XXX provide details?
943
944\item The code generator's peephole optimizer now performs
945simple constant folding in expressions. If you write something like
946\code{a = 2+3}, the code generator will do the arithmetic and produce
947code corresponding to \code{a = 5}.
948
Fred Drake2db76802004-12-01 05:05:47 +0000949\end{itemize}
950
951The net result of the 2.5 optimizations is that Python 2.5 runs the
Andrew M. Kuchling9c67ee02006-04-04 19:07:27 +0000952pystone benchmark around XXX\% faster than Python 2.4.
Fred Drake2db76802004-12-01 05:05:47 +0000953
954
955%======================================================================
956\section{New, Improved, and Deprecated Modules}
957
Andrew M. Kuchling0f1955d2006-04-13 12:09:08 +0000958As usual, Python's standard library received many enhancements and
Fred Drake2db76802004-12-01 05:05:47 +0000959bug fixes. Here's a partial list of the most notable changes, sorted
960alphabetically by module name. Consult the
961\file{Misc/NEWS} file in the source tree for a more
Andrew M. Kuchlingf688cc52006-03-10 18:50:08 +0000962complete list of changes, or look through the SVN logs for all the
Fred Drake2db76802004-12-01 05:05:47 +0000963details.
964
965\begin{itemize}
966
Andrew M. Kuchling6fc69762006-04-13 12:37:21 +0000967% XXX collections.deque now has .remove()
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000968% collections.defaultdict
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000969
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000970% the cPickle module no longer accepts the deprecated None option in the
971% args tuple returned by __reduce__().
972
Andrew M. Kuchling6fc69762006-04-13 12:37:21 +0000973% XXX csv module improvements
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000974
Andrew M. Kuchling6fc69762006-04-13 12:37:21 +0000975% XXX datetime.datetime() now has a strptime class method which can be used to
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000976% create datetime object using a string and format.
977
Andrew M. Kuchling6fc69762006-04-13 12:37:21 +0000978% XXX fileinput: opening hook used to control how files are opened.
Andrew M. Kuchling38f85072006-04-02 01:46:32 +0000979% .input() now has a mode parameter
980% now has a fileno() function
981% accepts Unicode filenames
982
Andrew M. Kuchling6fc69762006-04-13 12:37:21 +0000983\item The \module{audioop} module now supports the a-LAW encoding,
984and the code for u-LAW encoding has been improved. (Contributed by
985Lars Immisch.)
986
Andrew M. Kuchlingda376042006-03-17 15:56:41 +0000987\item In the \module{gc} module, the new \function{get_count()} function
988returns a 3-tuple containing the current collection counts for the
989three GC generations. This is accounting information for the garbage
990collector; when these counts reach a specified threshold, a garbage
991collection sweep will be made. The existing \function{gc.collect()}
992function now takes an optional \var{generation} argument of 0, 1, or 2
993to specify which generation to collect.
994
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000995\item The \function{nsmallest()} and
996\function{nlargest()} functions in the \module{heapq} module
997now support a \code{key} keyword argument similar to the one
998provided by the \function{min()}/\function{max()} functions
999and the \method{sort()} methods. For example:
1000Example:
1001
1002\begin{verbatim}
1003>>> import heapq
1004>>> L = ["short", 'medium', 'longest', 'longer still']
1005>>> heapq.nsmallest(2, L) # Return two lowest elements, lexicographically
1006['longer still', 'longest']
1007>>> heapq.nsmallest(2, L, key=len) # Return two shortest elements
1008['short', 'medium']
1009\end{verbatim}
1010
1011(Contributed by Raymond Hettinger.)
1012
Andrew M. Kuchling511a3a82005-03-20 19:52:18 +00001013\item The \function{itertools.islice()} function now accepts
1014\code{None} for the start and step arguments. This makes it more
1015compatible with the attributes of slice objects, so that you can now write
1016the following:
1017
1018\begin{verbatim}
1019s = slice(5) # Create slice object
1020itertools.islice(iterable, s.start, s.stop, s.step)
1021\end{verbatim}
1022
1023(Contributed by Raymond Hettinger.)
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +00001024
Andrew M. Kuchling75ba2442006-04-14 10:29:55 +00001025\item The \module{nis} module now supports accessing domains other
1026than the system default domain by supplying a \var{domain} argument to
1027the \function{nis.match()} and \function{nis.maps()} functions.
1028(Contributed by Ben Bell.)
1029
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001030\item The \module{operator} module's \function{itemgetter()}
1031and \function{attrgetter()} functions now support multiple fields.
1032A call such as \code{operator.attrgetter('a', 'b')}
1033will return a function
1034that retrieves the \member{a} and \member{b} attributes. Combining
1035this new feature with the \method{sort()} method's \code{key} parameter
1036lets you easily sort lists using multiple fields.
Andrew M. Kuchling6e3a66d2006-04-07 12:46:06 +00001037(Contributed by Raymond Hettinger.)
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001038
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +00001039
Andrew M. Kuchling0f1955d2006-04-13 12:09:08 +00001040\item The \module{os} module underwent several changes. The
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +00001041\member{stat_float_times} variable now defaults to true, meaning that
1042\function{os.stat()} will now return time values as floats. (This
1043doesn't necessarily mean that \function{os.stat()} will return times
1044that are precise to fractions of a second; not all systems support
1045such precision.)
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +00001046
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001047Constants named \member{os.SEEK_SET}, \member{os.SEEK_CUR}, and
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +00001048\member{os.SEEK_END} have been added; these are the parameters to the
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001049\function{os.lseek()} function. Two new constants for locking are
1050\member{os.O_SHLOCK} and \member{os.O_EXLOCK}.
1051
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001052Two new functions, \function{wait3()} and \function{wait4()}, were
1053added. They're similar the \function{waitpid()} function which waits
1054for a child process to exit and returns a tuple of the process ID and
1055its exit status, but \function{wait3()} and \function{wait4()} return
1056additional information. \function{wait3()} doesn't take a process ID
1057as input, so it waits for any child process to exit and returns a
10583-tuple of \var{process-id}, \var{exit-status}, \var{resource-usage}
1059as returned from the \function{resource.getrusage()} function.
1060\function{wait4(\var{pid})} does take a process ID.
Andrew M. Kuchling6e3a66d2006-04-07 12:46:06 +00001061(Contributed by Chad J. Schroeder.)
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001062
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001063On FreeBSD, the \function{os.stat()} function now returns
1064times with nanosecond resolution, and the returned object
1065now has \member{st_gen} and \member{st_birthtime}.
1066The \member{st_flags} member is also available, if the platform supports it.
Andrew M. Kuchling6e3a66d2006-04-07 12:46:06 +00001067(Contributed by Antti Louko and Diego Petten\`o.)
1068% (Patch 1180695, 1212117)
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001069
Andrew M. Kuchling01e3d262006-03-17 15:38:39 +00001070\item The old \module{regex} and \module{regsub} modules, which have been
1071deprecated ever since Python 2.0, have finally been deleted.
Andrew M. Kuchlingf4b06602006-03-17 15:39:52 +00001072Other deleted modules: \module{statcache}, \module{tzparse},
1073\module{whrandom}.
Andrew M. Kuchling01e3d262006-03-17 15:38:39 +00001074
1075\item The \file{lib-old} directory,
1076which includes ancient modules such as \module{dircmp} and
1077\module{ni}, was also deleted. \file{lib-old} wasn't on the default
1078\code{sys.path}, so unless your programs explicitly added the directory to
1079\code{sys.path}, this removal shouldn't affect your code.
1080
Andrew M. Kuchling4678dc82006-01-15 16:11:28 +00001081\item The \module{socket} module now supports \constant{AF_NETLINK}
1082sockets on Linux, thanks to a patch from Philippe Biondi.
1083Netlink sockets are a Linux-specific mechanism for communications
1084between a user-space process and kernel code; an introductory
1085article about them is at \url{http://www.linuxjournal.com/article/7356}.
1086In Python code, netlink addresses are represented as a tuple of 2 integers,
1087\code{(\var{pid}, \var{group_mask})}.
1088
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001089Socket objects also gained accessor methods \method{getfamily()},
1090\method{gettype()}, and \method{getproto()} methods to retrieve the
1091family, type, and protocol values for the socket.
1092
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001093\item New module: \module{spwd} provides functions for accessing the
Andrew M. Kuchling5f445bf2006-04-12 18:54:00 +00001094shadow password database on systems that support it.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001095% XXX give example
Fred Drake2db76802004-12-01 05:05:47 +00001096
Andrew M. Kuchling61434b62006-04-13 11:51:07 +00001097\item The Python developers switched from CVS to Subversion during the 2.5
1098development process. Information about the exact build version is
1099available as the \code{sys.subversion} variable, a 3-tuple
1100of \code{(\var{interpreter-name}, \var{branch-name}, \var{revision-range})}.
1101For example, at the time of writing
1102my copy of 2.5 was reporting \code{('CPython', 'trunk', '45313:45315')}.
1103
1104This information is also available to C extensions via the
1105\cfunction{Py_GetBuildInfo()} function that returns a
1106string of build information like this:
1107\code{"trunk:45355:45356M, Apr 13 2006, 07:42:19"}.
1108(Contributed by Barry Warsaw.)
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001109
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +00001110\item The \class{TarFile} class in the \module{tarfile} module now has
Georg Brandl08c02db2005-07-22 18:39:19 +00001111an \method{extractall()} method that extracts all members from the
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +00001112archive into the current working directory. It's also possible to set
1113a different directory as the extraction target, and to unpack only a
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001114subset of the archive's members.
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +00001115
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001116A tarfile's compression can be autodetected by
1117using the mode \code{'r|*'}.
1118% patch 918101
1119(Contributed by Lars Gust\"abel.)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001120
Andrew M. Kuchlingf688cc52006-03-10 18:50:08 +00001121\item The \module{unicodedata} module has been updated to use version 4.1.0
1122of the Unicode character database. Version 3.2.0 is required
1123by some specifications, so it's still available as
1124\member{unicodedata.db_3_2_0}.
1125
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001126% patch #754022: Greatly enhanced webbrowser.py (by Oleg Broytmann).
1127
Fredrik Lundh7e0aef02005-12-12 18:54:55 +00001128
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001129\item The \module{xmlrpclib} module now supports returning
1130 \class{datetime} objects for the XML-RPC date type. Supply
1131 \code{use_datetime=True} to the \function{loads()} function
1132 or the \class{Unmarshaller} class to enable this feature.
Andrew M. Kuchling6e3a66d2006-04-07 12:46:06 +00001133 (Contributed by Skip Montanaro.)
1134% Patch 1120353
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001135
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001136
Fred Drake114b8ca2005-03-21 05:47:11 +00001137\end{itemize}
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +00001138
Fred Drake2db76802004-12-01 05:05:47 +00001139
1140
1141%======================================================================
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001142% whole new modules get described in subsections here
Fred Drake2db76802004-12-01 05:05:47 +00001143
Andrew M. Kuchling61434b62006-04-13 11:51:07 +00001144%======================================================================
Andrew M. Kuchling75ba2442006-04-14 10:29:55 +00001145%\subsection{The cProfile module}
1146
1147%======================================================================
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001148\subsection{The ctypes package}
1149
1150The \module{ctypes} package, written by Thomas Heller, has been added
1151to the standard library. \module{ctypes} lets you call arbitrary functions
Andrew M. Kuchling28c5f1f2006-04-13 02:04:42 +00001152in shared libraries or DLLs. Long-time users may remember the \module{dl} module, which
1153provides functions for loading shared libraries and calling functions in them. The \module{ctypes} package is much fancier.
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001154
Andrew M. Kuchling28c5f1f2006-04-13 02:04:42 +00001155To load a shared library or DLL, you must create an instance of the
1156\class{CDLL} class and provide the name or path of the shared library
1157or DLL. Once that's done, you can call arbitrary functions
1158by accessing them as attributes of the \class{CDLL} object.
1159
1160\begin{verbatim}
1161import ctypes
1162
1163libc = ctypes.CDLL('libc.so.6')
1164result = libc.printf("Line of output\n")
1165\end{verbatim}
1166
1167Type constructors for the various C types are provided: \function{c_int},
1168\function{c_float}, \function{c_double}, \function{c_char_p} (equivalent to \ctype{char *}), and so forth. Unlike Python's types, the C versions are all mutable; you can assign to their \member{value} attribute
1169to change the wrapped value. Python integers and strings will be automatically
1170converted to the corresponding C types, but for other types you
1171must call the correct type constructor. (And I mean \emph{must};
1172getting it wrong will often result in the interpreter crashing
1173with a segmentation fault.)
1174
1175You shouldn't use \function{c_char_p} with a Python string when the C function will be modifying the memory area, because Python strings are
1176supposed to be immutable; breaking this rule will cause puzzling bugs. When you need a modifiable memory area,
Neal Norwitz5f5a69b2006-04-13 03:41:04 +00001177use \function{create_string_buffer()}:
Andrew M. Kuchling28c5f1f2006-04-13 02:04:42 +00001178
1179\begin{verbatim}
1180s = "this is a string"
1181buf = ctypes.create_string_buffer(s)
1182libc.strfry(buf)
1183\end{verbatim}
1184
1185C functions are assumed to return integers, but you can set
1186the \member{restype} attribute of the function object to
1187change this:
1188
1189\begin{verbatim}
1190>>> libc.atof('2.71828')
1191-1783957616
1192>>> libc.atof.restype = ctypes.c_double
1193>>> libc.atof('2.71828')
11942.71828
1195\end{verbatim}
1196
1197\module{ctypes} also provides a wrapper for Python's C API
1198as the \code{ctypes.pythonapi} object. This object does \emph{not}
1199release the global interpreter lock before calling a function, because the lock must be held when calling into the interpreter's code.
1200There's a \class{py_object()} type constructor that will create a
1201\ctype{PyObject *} pointer. A simple usage:
1202
1203\begin{verbatim}
1204import ctypes
1205
1206d = {}
1207ctypes.pythonapi.PyObject_SetItem(ctypes.py_object(d),
1208 ctypes.py_object("abc"), ctypes.py_object(1))
1209# d is now {'abc', 1}.
1210\end{verbatim}
1211
1212Don't forget to use \class{py_object()}; if it's omitted you end
1213up with a segmentation fault.
1214
1215\module{ctypes} has been around for a while, but people still write
1216and distribution hand-coded extension modules because you can't rely on \module{ctypes} being present.
1217Perhaps developers will begin to write
1218Python wrappers atop a library accessed through \module{ctypes} instead
1219of extension modules, now that \module{ctypes} is included with core Python.
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001220
Andrew M. Kuchling28c5f1f2006-04-13 02:04:42 +00001221\begin{seealso}
1222
1223\seeurl{http://starship.python.net/crew/theller/ctypes/}
1224{The ctypes web page, with a tutorial, reference, and FAQ.}
1225
1226\end{seealso}
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001227
Andrew M. Kuchling61434b62006-04-13 11:51:07 +00001228
1229%======================================================================
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001230\subsection{The ElementTree package}
1231
1232A subset of Fredrik Lundh's ElementTree library for processing XML has
Andrew M. Kuchling16ed5212006-04-10 22:28:11 +00001233been added to the standard library as \module{xmlcore.etree}. The
Georg Brandlce27a062006-04-11 06:27:12 +00001234available modules are
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001235\module{ElementTree}, \module{ElementPath}, and
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +00001236\module{ElementInclude} from ElementTree 1.2.6.
1237The \module{cElementTree} accelerator module is also included.
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001238
Andrew M. Kuchling16ed5212006-04-10 22:28:11 +00001239The rest of this section will provide a brief overview of using
1240ElementTree. Full documentation for ElementTree is available at
1241\url{http://effbot.org/zone/element-index.htm}.
1242
1243ElementTree represents an XML document as a tree of element nodes.
1244The text content of the document is stored as the \member{.text}
1245and \member{.tail} attributes of
1246(This is one of the major differences between ElementTree and
1247the Document Object Model; in the DOM there are many different
1248types of node, including \class{TextNode}.)
1249
1250The most commonly used parsing function is \function{parse()}, that
1251takes either a string (assumed to contain a filename) or a file-like
1252object and returns an \class{ElementTree} instance:
1253
1254\begin{verbatim}
1255from xmlcore.etree import ElementTree as ET
1256
1257tree = ET.parse('ex-1.xml')
1258
1259feed = urllib.urlopen(
1260 'http://planet.python.org/rss10.xml')
1261tree = ET.parse(feed)
1262\end{verbatim}
1263
1264Once you have an \class{ElementTree} instance, you
1265can call its \method{getroot()} method to get the root \class{Element} node.
1266
1267There's also an \function{XML()} function that takes a string literal
1268and returns an \class{Element} node (not an \class{ElementTree}).
1269This function provides a tidy way to incorporate XML fragments,
1270approaching the convenience of an XML literal:
1271
1272\begin{verbatim}
1273svg = et.XML("""<svg width="10px" version="1.0">
1274 </svg>""")
1275svg.set('height', '320px')
1276svg.append(elem1)
1277\end{verbatim}
1278
1279Each XML element supports some dictionary-like and some list-like
Andrew M. Kuchling075e0232006-04-11 13:14:56 +00001280access methods. Dictionary-like operations are used to access attribute
1281values, and list-like operations are used to access child nodes.
Andrew M. Kuchling16ed5212006-04-10 22:28:11 +00001282
Andrew M. Kuchling075e0232006-04-11 13:14:56 +00001283\begin{tableii}{c|l}{code}{Operation}{Result}
1284 \lineii{elem[n]}{Returns n'th child element.}
1285 \lineii{elem[m:n]}{Returns list of m'th through n'th child elements.}
1286 \lineii{len(elem)}{Returns number of child elements.}
1287 \lineii{elem.getchildren()}{Returns list of child elements.}
1288 \lineii{elem.append(elem2)}{Adds \var{elem2} as a child.}
1289 \lineii{elem.insert(index, elem2)}{Inserts \var{elem2} at the specified location.}
1290 \lineii{del elem[n]}{Deletes n'th child element.}
1291 \lineii{elem.keys()}{Returns list of attribute names.}
1292 \lineii{elem.get(name)}{Returns value of attribute \var{name}.}
1293 \lineii{elem.set(name, value)}{Sets new value for attribute \var{name}.}
1294 \lineii{elem.attrib}{Retrieves the dictionary containing attributes.}
1295 \lineii{del elem.attrib[name]}{Deletes attribute \var{name}.}
1296\end{tableii}
1297
1298Comments and processing instructions are also represented as
1299\class{Element} nodes. To check if a node is a comment or processing
1300instructions:
1301
1302\begin{verbatim}
1303if elem.tag is ET.Comment:
1304 ...
1305elif elem.tag is ET.ProcessingInstruction:
1306 ...
1307\end{verbatim}
Andrew M. Kuchling16ed5212006-04-10 22:28:11 +00001308
1309To generate XML output, you should call the
1310\method{ElementTree.write()} method. Like \function{parse()},
1311it can take either a string or a file-like object:
1312
1313\begin{verbatim}
1314# Encoding is US-ASCII
1315tree.write('output.xml')
1316
1317# Encoding is UTF-8
1318f = open('output.xml', 'w')
1319tree.write(f, 'utf-8')
1320\end{verbatim}
1321
1322(Caution: the default encoding used for output is ASCII, which isn't
1323very useful for general XML work, raising an exception if there are
1324any characters with values greater than 127. You should always
1325specify a different encoding such as UTF-8 that can handle any Unicode
1326character.)
1327
Andrew M. Kuchling075e0232006-04-11 13:14:56 +00001328This section is only a partial description of the ElementTree interfaces.
1329Please read the package's official documentation for more details.
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001330
Andrew M. Kuchling16ed5212006-04-10 22:28:11 +00001331\begin{seealso}
1332
1333\seeurl{http://effbot.org/zone/element-index.htm}
1334{Official documentation for ElementTree.}
1335
1336
1337\end{seealso}
1338
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001339
Andrew M. Kuchling61434b62006-04-13 11:51:07 +00001340%======================================================================
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001341\subsection{The hashlib package}
1342
1343A new \module{hashlib} module has been added to replace the
1344\module{md5} and \module{sha} modules. \module{hashlib} adds support
1345for additional secure hashes (SHA-224, SHA-256, SHA-384, and SHA-512).
1346When available, the module uses OpenSSL for fast platform optimized
1347implementations of algorithms.
1348
1349The old \module{md5} and \module{sha} modules still exist as wrappers
1350around hashlib to preserve backwards compatibility. The new module's
1351interface is very close to that of the old modules, but not identical.
1352The most significant difference is that the constructor functions
1353for creating new hashing objects are named differently.
1354
1355\begin{verbatim}
1356# Old versions
1357h = md5.md5()
1358h = md5.new()
1359
1360# New version
1361h = hashlib.md5()
1362
1363# Old versions
1364h = sha.sha()
1365h = sha.new()
1366
1367# New version
1368h = hashlib.sha1()
1369
1370# Hash that weren't previously available
1371h = hashlib.sha224()
1372h = hashlib.sha256()
1373h = hashlib.sha384()
1374h = hashlib.sha512()
1375
1376# Alternative form
1377h = hashlib.new('md5') # Provide algorithm as a string
1378\end{verbatim}
1379
1380Once a hash object has been created, its methods are the same as before:
1381\method{update(\var{string})} hashes the specified string into the
1382current digest state, \method{digest()} and \method{hexdigest()}
1383return the digest value as a binary string or a string of hex digits,
1384and \method{copy()} returns a new hashing object with the same digest state.
1385
1386This module was contributed by Gregory P. Smith.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001387
1388
Andrew M. Kuchling61434b62006-04-13 11:51:07 +00001389%======================================================================
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001390\subsection{The sqlite3 package}
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001391
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001392The pysqlite module (\url{http://www.pysqlite.org}), a wrapper for the
1393SQLite embedded database, has been added to the standard library under
1394the package name \module{sqlite3}. SQLite is a C library that
1395provides a SQL-language database that stores data in disk files
1396without requiring a separate server process. pysqlite was written by
1397Gerhard H\"aring, and provides a SQL interface that complies with the
Andrew M. Kuchlingd58baf82006-04-10 21:40:16 +00001398DB-API 2.0 specification described by \pep{249}. This means that it
1399should be possible to write the first version of your applications
1400using SQLite for data storage and, if switching to a larger database
1401such as PostgreSQL or Oracle is necessary, the switch should be
1402relatively easy.
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001403
1404If you're compiling the Python source yourself, note that the source
1405tree doesn't include the SQLite code itself, only the wrapper module.
1406You'll need to have the SQLite libraries and headers installed before
1407compiling Python, and the build process will compile the module when
1408the necessary headers are available.
1409
Andrew M. Kuchlingd58baf82006-04-10 21:40:16 +00001410To use the module, you must first create a \class{Connection} object
1411that represents the database. Here the data will be stored in the
1412\file{/tmp/example} file:
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001413
Andrew M. Kuchlingd58baf82006-04-10 21:40:16 +00001414\begin{verbatim}
1415conn = sqlite3.connect('/tmp/example')
1416\end{verbatim}
1417
1418You can also supply the special name \samp{:memory:} to create
1419a database in RAM.
1420
1421Once you have a \class{Connection}, you can create a \class{Cursor}
1422object and call its \method{execute()} method to perform SQL commands:
1423
1424\begin{verbatim}
1425c = conn.cursor()
1426
1427# Create table
1428c.execute('''create table stocks
1429(date timestamp, trans varchar, symbol varchar,
1430 qty decimal, price decimal)''')
1431
1432# Insert a row of data
1433c.execute("""insert into stocks
1434 values ('2006-01-05','BUY','RHAT',100, 35.14)""")
1435\end{verbatim}
1436
1437Usually your SQL queries will need to reflect the value of Python
1438variables. You shouldn't assemble your query using Python's string
1439operations because doing so is insecure; it makes your program
1440vulnerable to what's called an SQL injection attack. Instead, use
1441SQLite's parameter substitution, putting \samp{?} as a placeholder
1442wherever you want to use a value, and then provide a tuple of values
1443as the second argument to the cursor's \method{execute()} method. For
1444example:
1445
1446\begin{verbatim}
1447# Never do this -- insecure!
1448symbol = 'IBM'
1449c.execute("... where symbol = '%s'" % symbol)
1450
1451# Do this instead
1452t = (symbol,)
1453c.execute("... where symbol = '?'", t)
1454
1455# Larger example
1456for t in (('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
1457 ('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
1458 ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
1459 ):
1460 c.execute('insert into stocks values (?,?,?,?,?)', t)
1461\end{verbatim}
1462
1463To retrieve data after executing a SELECT statement, you can either
1464treat the cursor as an iterator, call the cursor's \method{fetchone()}
1465method to retrieve a single matching row,
1466or call \method{fetchall()} to get a list of the matching rows.
1467
1468This example uses the iterator form:
1469
1470\begin{verbatim}
1471>>> c = conn.cursor()
1472>>> c.execute('select * from stocks order by price')
1473>>> for row in c:
1474... print row
1475...
1476(u'2006-01-05', u'BUY', u'RHAT', 100, 35.140000000000001)
1477(u'2006-03-28', u'BUY', u'IBM', 1000, 45.0)
1478(u'2006-04-06', u'SELL', u'IBM', 500, 53.0)
1479(u'2006-04-05', u'BUY', u'MSOFT', 1000, 72.0)
1480>>>
1481\end{verbatim}
1482
1483You should also use parameter substitution with SELECT statements:
1484
1485\begin{verbatim}
1486>>> c.execute('select * from stocks where symbol=?', ('IBM',))
1487>>> print c.fetchall()
1488[(u'2006-03-28', u'BUY', u'IBM', 1000, 45.0),
1489 (u'2006-04-06', u'SELL', u'IBM', 500, 53.0)]
1490\end{verbatim}
1491
1492For more information about the SQL dialect supported by SQLite, see
1493\url{http://www.sqlite.org}.
1494
1495\begin{seealso}
1496
1497\seeurl{http://www.pysqlite.org}
1498{The pysqlite web page.}
1499
1500\seeurl{http://www.sqlite.org}
1501{The SQLite web page; the documentation describes the syntax and the
1502available data types for the supported SQL dialect.}
1503
1504\seepep{249}{Database API Specification 2.0}{PEP written by
1505Marc-Andr\'e Lemburg.}
1506
1507\end{seealso}
Andrew M. Kuchlingaf7ee992006-04-03 12:41:37 +00001508
Fred Drake2db76802004-12-01 05:05:47 +00001509
1510% ======================================================================
1511\section{Build and C API Changes}
1512
1513Changes to Python's build process and to the C API include:
1514
1515\begin{itemize}
1516
Andrew M. Kuchling4d8cd892006-04-06 13:03:04 +00001517\item The largest change to the C API came from \pep{353},
1518which modifies the interpreter to use a \ctype{Py_ssize_t} type
1519definition instead of \ctype{int}. See the earlier
1520section~ref{section-353} for a discussion of this change.
1521
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001522\item The design of the bytecode compiler has changed a great deal, to
1523no longer generate bytecode by traversing the parse tree. Instead
Andrew M. Kuchlingdb85ed52005-10-23 21:52:59 +00001524the parse tree is converted to an abstract syntax tree (or AST), and it is
1525the abstract syntax tree that's traversed to produce the bytecode.
1526
Andrew M. Kuchling4e861952006-04-12 12:16:31 +00001527It's possible for Python code to obtain AST objects by using the
Andrew M. Kuchling5f445bf2006-04-12 18:54:00 +00001528\function{compile()} built-in and specifying \code{_ast.PyCF_ONLY_AST}
1529as the value of the
Andrew M. Kuchling4e861952006-04-12 12:16:31 +00001530\var{flags} parameter:
1531
1532\begin{verbatim}
Andrew M. Kuchling5f445bf2006-04-12 18:54:00 +00001533from _ast import PyCF_ONLY_AST
Andrew M. Kuchling4e861952006-04-12 12:16:31 +00001534ast = compile("""a=0
1535for i in range(10):
1536 a += i
Andrew M. Kuchling5f445bf2006-04-12 18:54:00 +00001537""", "<string>", 'exec', PyCF_ONLY_AST)
Andrew M. Kuchling4e861952006-04-12 12:16:31 +00001538
1539assignment = ast.body[0]
1540for_loop = ast.body[1]
1541\end{verbatim}
1542
Andrew M. Kuchlingdb85ed52005-10-23 21:52:59 +00001543No documentation has been written for the AST code yet. To start
1544learning about it, read the definition of the various AST nodes in
1545\file{Parser/Python.asdl}. A Python script reads this file and
1546generates a set of C structure definitions in
1547\file{Include/Python-ast.h}. The \cfunction{PyParser_ASTFromString()}
1548and \cfunction{PyParser_ASTFromFile()}, defined in
1549\file{Include/pythonrun.h}, take Python source as input and return the
1550root of an AST representing the contents. This AST can then be turned
1551into a code object by \cfunction{PyAST_Compile()}. For more
1552information, read the source code, and then ask questions on
1553python-dev.
1554
1555% List of names taken from Jeremy's python-dev post at
1556% http://mail.python.org/pipermail/python-dev/2005-October/057500.html
1557The AST code was developed under Jeremy Hylton's management, and
1558implemented by (in alphabetical order) Brett Cannon, Nick Coghlan,
1559Grant Edwards, John Ehresman, Kurt Kaiser, Neal Norwitz, Tim Peters,
1560Armin Rigo, and Neil Schemenauer, plus the participants in a number of
1561AST sprints at conferences such as PyCon.
1562
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001563\item The built-in set types now have an official C API. Call
1564\cfunction{PySet_New()} and \cfunction{PyFrozenSet_New()} to create a
1565new set, \cfunction{PySet_Add()} and \cfunction{PySet_Discard()} to
1566add and remove elements, and \cfunction{PySet_Contains} and
1567\cfunction{PySet_Size} to examine the set's state.
1568
Andrew M. Kuchling61434b62006-04-13 11:51:07 +00001569\item C code can now obtain information about the exact revision
1570of the Python interpreter by calling the
1571\cfunction{Py_GetBuildInfo()} function that returns a
1572string of build information like this:
1573\code{"trunk:45355:45356M, Apr 13 2006, 07:42:19"}.
1574(Contributed by Barry Warsaw.)
1575
Andrew M. Kuchling150e3492005-08-23 00:56:06 +00001576\item The \cfunction{PyRange_New()} function was removed. It was
1577never documented, never used in the core code, and had dangerously lax
1578error checking.
Fred Drake2db76802004-12-01 05:05:47 +00001579
1580\end{itemize}
1581
1582
1583%======================================================================
Andrew M. Kuchling6fc69762006-04-13 12:37:21 +00001584\subsection{Port-Specific Changes}
Fred Drake2db76802004-12-01 05:05:47 +00001585
Andrew M. Kuchling6fc69762006-04-13 12:37:21 +00001586\begin{itemize}
1587
1588\item MacOS X (10.3 and higher): dynamic loading of modules
1589now uses the \cfunction{dlopen()} function instead of MacOS-specific
1590functions.
1591
1592\end{itemize}
Fred Drake2db76802004-12-01 05:05:47 +00001593
1594
1595%======================================================================
1596\section{Other Changes and Fixes \label{section-other}}
1597
1598As usual, there were a bunch of other improvements and bugfixes
Andrew M. Kuchlingf688cc52006-03-10 18:50:08 +00001599scattered throughout the source tree. A search through the SVN change
Fred Drake2db76802004-12-01 05:05:47 +00001600logs finds there were XXX patches applied and YYY bugs fixed between
Andrew M. Kuchling92e24952004-12-03 13:54:09 +00001601Python 2.4 and 2.5. Both figures are likely to be underestimates.
Fred Drake2db76802004-12-01 05:05:47 +00001602
1603Some of the more notable changes are:
1604
1605\begin{itemize}
1606
Andrew M. Kuchling01e3d262006-03-17 15:38:39 +00001607\item Evan Jones's patch to obmalloc, first described in a talk
1608at PyCon DC 2005, was applied. Python 2.4 allocated small objects in
1609256K-sized arenas, but never freed arenas. With this patch, Python
1610will free arenas when they're empty. The net effect is that on some
1611platforms, when you allocate many objects, Python's memory usage may
1612actually drop when you delete them, and the memory may be returned to
1613the operating system. (Implemented by Evan Jones, and reworked by Tim
1614Peters.)
Fred Drake2db76802004-12-01 05:05:47 +00001615
Andrew M. Kuchlingf7c62902006-04-12 12:27:50 +00001616Note that this change means extension modules need to be more careful
Andrew M. Kuchling0f1955d2006-04-13 12:09:08 +00001617with how they allocate memory. Python's API has many different
Andrew M. Kuchlingf7c62902006-04-12 12:27:50 +00001618functions for allocating memory that are grouped into families. For
1619example, \cfunction{PyMem_Malloc()}, \cfunction{PyMem_Realloc()}, and
1620\cfunction{PyMem_Free()} are one family that allocates raw memory,
1621while \cfunction{PyObject_Malloc()}, \cfunction{PyObject_Realloc()},
1622and \cfunction{PyObject_Free()} are another family that's supposed to
1623be used for creating Python objects.
1624
1625Previously these different families all reduced to the platform's
1626\cfunction{malloc()} and \cfunction{free()} functions. This meant
1627it didn't matter if you got things wrong and allocated memory with the
1628\cfunction{PyMem} function but freed it with the \cfunction{PyObject}
1629function. With the obmalloc change, these families now do different
1630things, and mismatches will probably result in a segfault. You should
1631carefully test your C extension modules with Python 2.5.
1632
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001633\item Coverity, a company that markets a source code analysis tool
1634 called Prevent, provided the results of their examination of the Python
Andrew M. Kuchling0f1955d2006-04-13 12:09:08 +00001635 source code. The analysis found about 60 bugs that
1636 were quickly fixed. Many of the bugs were refcounting problems, often
1637 occurring in error-handling code. See
1638 \url{http://scan.coverity.com} for the statistics.
Andrew M. Kuchling38f85072006-04-02 01:46:32 +00001639
Fred Drake2db76802004-12-01 05:05:47 +00001640\end{itemize}
1641
1642
1643%======================================================================
1644\section{Porting to Python 2.5}
1645
1646This section lists previously described changes that may require
1647changes to your code:
1648
1649\begin{itemize}
1650
Andrew M. Kuchling5f445bf2006-04-12 18:54:00 +00001651\item ASCII is now the default encoding for modules. It's now
1652a syntax error if a module contains string literals with 8-bit
1653characters but doesn't have an encoding declaration. In Python 2.4
1654this triggered a warning, not a syntax error.
1655
Andrew M. Kuchlingc3749a92006-04-04 19:14:41 +00001656\item The \module{pickle} module no longer uses the deprecated \var{bin} parameter.
Fred Drake2db76802004-12-01 05:05:47 +00001657
Andrew M. Kuchling3b4fb042006-04-13 12:49:39 +00001658\item Previously, the \member{gi_frame} attribute of a generator
1659was always a frame object. Because of the \pep{342} changes
1660described in section~\ref{section-generators}, it's now possible
1661for \member{gi_frame} to be \code{None}.
1662
Andrew M. Kuchlingf7c62902006-04-12 12:27:50 +00001663\item C API: Many functions now use \ctype{Py_ssize_t}
1664instead of \ctype{int} to allow processing more data
1665on 64-bit machines. Extension code may need to make
1666the same change to avoid warnings and to support 64-bit machines.
1667See the earlier
1668section~ref{section-353} for a discussion of this change.
1669
1670\item C API:
1671The obmalloc changes mean that
1672you must be careful to not mix usage
1673of the \cfunction{PyMem_*()} and \cfunction{PyObject_*()}
1674families of functions. Memory allocated with
1675one family's \cfunction{*_Malloc()} must be
1676freed with the corresponding family's \cfunction{*_Free()} function.
1677
Fred Drake2db76802004-12-01 05:05:47 +00001678\end{itemize}
1679
1680
1681%======================================================================
1682\section{Acknowledgements \label{acks}}
1683
1684The author would like to thank the following people for offering
1685suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchling5f445bf2006-04-12 18:54:00 +00001686article: Martin von~L\"owis, Mike Rovner, Thomas Wouters.
Fred Drake2db76802004-12-01 05:05:47 +00001687
1688\end{document}