blob: 8645e326ec0f7e74788193825ddc6f0d864c310c [file] [log] [blame]
Fred Drake2db76802004-12-01 05:05:47 +00001\documentclass{howto}
2\usepackage{distutils}
3% $Id$
4
Fred Drake2db76802004-12-01 05:05:47 +00005
6\title{What's New in Python 2.5}
7\release{0.0}
Andrew M. Kuchling92e24952004-12-03 13:54:09 +00008\author{A.M. Kuchling}
9\authoraddress{\email{amk@amk.ca}}
Fred Drake2db76802004-12-01 05:05:47 +000010
11\begin{document}
12\maketitle
13\tableofcontents
14
15This article explains the new features in Python 2.5. No release date
Andrew M. Kuchling5eefdca2006-02-08 11:36:09 +000016for Python 2.5 has been set; it will probably be released in the
17autumn of 2006.
Fred Drake2db76802004-12-01 05:05:47 +000018
Andrew M. Kuchling437567c2006-03-07 20:48:55 +000019% XXX Compare with previous release in 2 - 3 sentences here.
Fred Drake2db76802004-12-01 05:05:47 +000020
21This article doesn't attempt to provide a complete specification of
22the new features, but instead provides a convenient overview. For
23full details, you should refer to the documentation for Python 2.5.
Andrew M. Kuchling437567c2006-03-07 20:48:55 +000024% XXX add hyperlink when the documentation becomes available online.
Fred Drake2db76802004-12-01 05:05:47 +000025If you want to understand the complete implementation and design
26rationale, refer to the PEP for a particular new feature.
27
28
29%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +000030\section{PEP 308: Conditional Expressions}
31
Andrew M. Kuchlinge362d932006-03-09 13:56:25 +000032For a long time, people have been requesting a way to write
33conditional expressions, expressions that return value A or value B
34depending on whether a Boolean value is true or false. A conditional
35expression lets you write a single assignment statement that has the
36same effect as the following:
37
38\begin{verbatim}
39if condition:
40 x = true_value
41else:
42 x = false_value
43\end{verbatim}
44
45There have been endless tedious discussions of syntax on both
46python-dev and comp.lang.python, and even a vote that found the
47majority of voters wanted some way to write conditional expressions,
48but there was no syntax that was clearly preferred by a majority.
49Candidates include C's \code{cond ? true_v : false_v},
50\code{if cond then true_v else false_v}, and 16 other variations.
51
52GvR eventually chose a surprising syntax:
53
54\begin{verbatim}
55x = true_value if condition else false_value
56\end{verbatim}
57
58Evaluation is still lazy as in existing Boolean expression, so the
59evaluation jumps around a bit. The \var{condition} expression is
60evaluated first, and the \var{true_value} expression is evaluated only
61if the condition was true. Similarly, the \var{false_value}
62expression is only evaluated when the condition is false.
63
64This syntax may seem strange and backwards; why does the condition go
65in the \emph{middle} of the expression, and not in the front as in C's
66\code{c ? x : y}? The decision was checked by applying the new syntax
67to the modules in the standard library and seeing how the resulting
68code read. In many cases where a conditional expression is used, one
69value seems to be the 'common case' and one value is an 'exceptional
70case', used only on rarer occasions when the condition isn't met. The
71conditional syntax makes this pattern a bit more obvious:
72
73\begin{verbatim}
74contents = ((doc + '\n') if doc else '')
75\end{verbatim}
76
77I read the above statement as meaning ``here \var{contents} is
78usually assigned a value of \code{doc+'\n'}; sometimes
79\var{doc} is empty, in which special case an empty string is returned.''
80I doubt I will use conditional expressions very often where there
81isn't a clear common and uncommon case.
82
83There was some discussion of whether the language should require
84surrounding conditional expressions with parentheses. The decision
85was made to \emph{not} require parentheses in the Python language's
86grammar, but as a matter of style I think you should always use them.
87Consider these two statements:
88
89\begin{verbatim}
90# First version -- no parens
91level = 1 if logging else 0
92
93# Second version -- with parens
94level = (1 if logging else 0)
95\end{verbatim}
96
97In the first version, I think a reader's eye might group the statement
98into 'level = 1', 'if logging', 'else 0', and think that the condition
99decides whether the assignment to \var{level} is performed. The
100second version reads better, in my opinion, because it makes it clear
101that the assignment is always performed and the choice is being made
102between two values.
103
104Another reason for including the brackets: a few odd combinations of
105list comprehensions and lambdas could look like incorrect conditional
106expressions. See \pep{308} for some examples. If you put parentheses
107around your conditional expressions, you won't run into this case.
108
109
110\begin{seealso}
111
112\seepep{308}{Conditional Expressions}{PEP written by
113Guido van Rossum and Raymond D. Hettinger; implemented by Thomas
114Wouters.}
115
116\end{seealso}
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000117
118
119%======================================================================
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000120\section{PEP 309: Partial Function Application}
Fred Drake2db76802004-12-01 05:05:47 +0000121
Andrew M. Kuchlingb1c96fd2005-03-20 21:42:04 +0000122The \module{functional} module is intended to contain tools for
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000123functional-style programming. Currently it only contains a
124\class{partial()} function, but new functions will probably be added
125in future versions of Python.
Andrew M. Kuchlingb1c96fd2005-03-20 21:42:04 +0000126
Andrew M. Kuchling4b000cd2005-04-09 15:51:44 +0000127For programs written in a functional style, it can be useful to
128construct variants of existing functions that have some of the
129parameters filled in. Consider a Python function \code{f(a, b, c)};
130you could create a new function \code{g(b, c)} that was equivalent to
131\code{f(1, b, c)}. This is called ``partial function application'',
132and is provided by the \class{partial} class in the new
133\module{functional} module.
134
135The constructor for \class{partial} takes the arguments
136\code{(\var{function}, \var{arg1}, \var{arg2}, ...
137\var{kwarg1}=\var{value1}, \var{kwarg2}=\var{value2})}. The resulting
138object is callable, so you can just call it to invoke \var{function}
139with the filled-in arguments.
140
141Here's a small but realistic example:
142
143\begin{verbatim}
144import functional
145
146def log (message, subsystem):
147 "Write the contents of 'message' to the specified subsystem."
148 print '%s: %s' % (subsystem, message)
149 ...
150
151server_log = functional.partial(log, subsystem='server')
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000152server_log('Unable to open socket')
Andrew M. Kuchling4b000cd2005-04-09 15:51:44 +0000153\end{verbatim}
154
Andrew M. Kuchling6af7fe02005-08-02 17:20:36 +0000155Here's another example, from a program that uses PyGTk. Here a
156context-sensitive pop-up menu is being constructed dynamically. The
157callback provided for the menu option is a partially applied version
158of the \method{open_item()} method, where the first argument has been
159provided.
Andrew M. Kuchling4b000cd2005-04-09 15:51:44 +0000160
Andrew M. Kuchling6af7fe02005-08-02 17:20:36 +0000161\begin{verbatim}
162...
163class Application:
164 def open_item(self, path):
165 ...
166 def init (self):
167 open_func = functional.partial(self.open_item, item_path)
168 popup_menu.append( ("Open", open_func, 1) )
169\end{verbatim}
Andrew M. Kuchlingb1c96fd2005-03-20 21:42:04 +0000170
171
172\begin{seealso}
173
174\seepep{309}{Partial Function Application}{PEP proposed and written by
175Peter Harris; implemented by Hye-Shik Chang, with adaptations by
176Raymond Hettinger.}
177
178\end{seealso}
Fred Drake2db76802004-12-01 05:05:47 +0000179
180
181%======================================================================
Fred Drakedb7b0022005-03-20 22:19:47 +0000182\section{PEP 314: Metadata for Python Software Packages v1.1}
183
Andrew M. Kuchlingd8d732e2005-04-09 23:59:41 +0000184Some simple dependency support was added to Distutils. The
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000185\function{setup()} function now has \code{requires}, \code{provides},
186and \code{obsoletes} keyword parameters. When you build a source
187distribution using the \code{sdist} command, the dependency
188information will be recorded in the \file{PKG-INFO} file.
Andrew M. Kuchlingd8d732e2005-04-09 23:59:41 +0000189
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000190Another new keyword parameter is \code{download_url}, which should be
191set to a URL for the package's source code. This means it's now
192possible to look up an entry in the package index, determine the
193dependencies for a package, and download the required packages.
Andrew M. Kuchlingd8d732e2005-04-09 23:59:41 +0000194
195% XXX put example here
196
197\begin{seealso}
198
199\seepep{314}{Metadata for Python Software Packages v1.1}{PEP proposed
200and written by A.M. Kuchling, Richard Jones, and Fred Drake;
201implemented by Richard Jones and Fred Drake.}
202
203\end{seealso}
Fred Drakedb7b0022005-03-20 22:19:47 +0000204
205
206%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000207\section{PEP 328: Absolute and Relative Imports}
208
209% XXX write this
210
211
212%======================================================================
213\section{PEP 341: Unified try/except/finally}
214
215% XXX write this
216
217
218%======================================================================
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000219\section{PEP 342: New Generator Features}
220
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000221As introduced in Python 2.3, generators only produce output; once a
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000222generator's code is invoked to create an iterator, there's no way to
223pass any new information into the function when its execution is
224resumed. Hackish solutions to this include making the generator's
225code look at a global variable and then changing the global variable's
226value, or passing in some mutable object that callers then modify.
227Python 2.5 adds the ability to pass values \emph{into} a generator.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000228
229To refresh your memory of basic generators, here's a simple example:
230
231\begin{verbatim}
232def counter (maximum):
233 i = 0
234 while i < maximum:
235 yield i
236 i += 1
237\end{verbatim}
238
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000239When you call \code{counter(10)}, the result is an iterator that
240returns the values from 0 up to 9. On encountering the
241\keyword{yield} statement, the iterator returns the provided value and
242suspends the function's execution, preserving the local variables.
243Execution resumes on the following call to the iterator's
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000244\method{next()} method, picking up after the \keyword{yield} statement.
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000245
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000246In Python 2.3, \keyword{yield} was a statement; it didn't return any
247value. In 2.5, \keyword{yield} is now an expression, returning a
248value that can be assigned to a variable or otherwise operated on:
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000249
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000250\begin{verbatim}
251val = (yield i)
252\end{verbatim}
253
254I recommend that you always put parentheses around a \keyword{yield}
255expression when you're doing something with the returned value, as in
256the above example. The parentheses aren't always necessary, but it's
257easier to always add them instead of having to remember when they're
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000258needed.\footnote{The exact rules are that a \keyword{yield}-expression must
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000259always be parenthesized except when it occurs at the top-level
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000260expression on the right-hand side of an assignment, meaning you can
261write \code{val = yield i} but have to use parentheses when there's an
262operation, as in \code{val = (yield i) + 12}.}
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000263
264Values are sent into a generator by calling its
265\method{send(\var{value})} method. The generator's code is then
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000266resumed and the \keyword{yield} expression returns the specified
267\var{value}. If the regular \method{next()} method is called, the
268\keyword{yield} returns \constant{None}.
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000269
270Here's the previous example, modified to allow changing the value of
271the internal counter.
272
273\begin{verbatim}
274def counter (maximum):
275 i = 0
276 while i < maximum:
277 val = (yield i)
278 # If value provided, change counter
279 if val is not None:
280 i = val
281 else:
282 i += 1
283\end{verbatim}
284
285And here's an example of changing the counter:
286
287\begin{verbatim}
288>>> it = counter(10)
289>>> print it.next()
2900
291>>> print it.next()
2921
293>>> print it.send(8)
2948
295>>> print it.next()
2969
297>>> print it.next()
298Traceback (most recent call last):
299 File ``t.py'', line 15, in ?
300 print it.next()
301StopIteration
Andrew M. Kuchlingc2033702005-08-29 13:30:12 +0000302\end{verbatim}
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000303
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000304Because \keyword{yield} will often be returning \constant{None}, you
305should always check for this case. Don't just use its value in
306expressions unless you're sure that the \method{send()} method
307will be the only method used resume your generator function.
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000308
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000309In addition to \method{send()}, there are two other new methods on
310generators:
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000311
312\begin{itemize}
313
314 \item \method{throw(\var{type}, \var{value}=None,
315 \var{traceback}=None)} is used to raise an exception inside the
316 generator; the exception is raised by the \keyword{yield} expression
317 where the generator's execution is paused.
318
319 \item \method{close()} raises a new \exception{GeneratorExit}
320 exception inside the generator to terminate the iteration.
321 On receiving this
322 exception, the generator's code must either raise
323 \exception{GeneratorExit} or \exception{StopIteration}; catching the
324 exception and doing anything else is illegal and will trigger
325 a \exception{RuntimeError}. \method{close()} will also be called by
326 Python's garbage collection when the generator is garbage-collected.
327
328 If you need to run cleanup code in case of a \exception{GeneratorExit},
329 I suggest using a \code{try: ... finally:} suite instead of
330 catching \exception{GeneratorExit}.
331
332\end{itemize}
333
334The cumulative effect of these changes is to turn generators from
335one-way producers of information into both producers and consumers.
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000336
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000337Generators also become \emph{coroutines}, a more generalized form of
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000338subroutines. Subroutines are entered at one point and exited at
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000339another point (the top of the function, and a \keyword{return
340statement}), but coroutines can be entered, exited, and resumed at
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000341many different points (the \keyword{yield} statements).
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000342
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000343
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000344\begin{seealso}
345
346\seepep{342}{Coroutines via Enhanced Generators}{PEP written by
347Guido van Rossum and Phillip J. Eby;
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000348implemented by Phillip J. Eby. Includes examples of
349some fancier uses of generators as coroutines.}
350
351\seeurl{http://en.wikipedia.org/wiki/Coroutine}{The Wikipedia entry for
352coroutines.}
353
Neal Norwitz09179882006-03-04 23:31:45 +0000354\seeurl{http://www.sidhe.org/\~{}dan/blog/archives/000178.html}{An
Andrew M. Kuchling07382062005-08-27 18:45:47 +0000355explanation of coroutines from a Perl point of view, written by Dan
356Sugalski.}
Andrew M. Kuchlinga2e21cb2005-08-02 17:13:21 +0000357
358\end{seealso}
359
360
361%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000362\section{PEP 343: The 'with' statement}
363
364% XXX write this
365
366
367%======================================================================
Andrew M. Kuchling8f4d2552006-03-08 01:50:20 +0000368\section{PEP 352: Exceptions as New-Style Classes}
369
370% XXX write this
371
372
373%======================================================================
Andrew M. Kuchling437567c2006-03-07 20:48:55 +0000374\section{PEP 357: The '__index__' method}
375
376% XXX write this
377
378
379%======================================================================
Fred Drake2db76802004-12-01 05:05:47 +0000380\section{Other Language Changes}
381
382Here are all of the changes that Python 2.5 makes to the core Python
383language.
384
385\begin{itemize}
Andrew M. Kuchling1cae3f52004-12-03 14:57:21 +0000386
387\item The \function{min()} and \function{max()} built-in functions
388gained a \code{key} keyword argument analogous to the \code{key}
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000389argument for \method{sort()}. This argument supplies a function
Andrew M. Kuchling1cae3f52004-12-03 14:57:21 +0000390that takes a single argument and is called for every value in the list;
391\function{min()}/\function{max()} will return the element with the
392smallest/largest return value from this function.
393For example, to find the longest string in a list, you can do:
394
395\begin{verbatim}
396L = ['medium', 'longest', 'short']
397# Prints 'longest'
398print max(L, key=len)
399# Prints 'short', because lexicographically 'short' has the largest value
400print max(L)
401\end{verbatim}
402
403(Contributed by Steven Bethard and Raymond Hettinger.)
Fred Drake2db76802004-12-01 05:05:47 +0000404
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000405\item Two new built-in functions, \function{any()} and
406\function{all()}, evaluate whether an iterator contains any true or
407false values. \function{any()} returns \constant{True} if any value
408returned by the iterator is true; otherwise it will return
409\constant{False}. \function{all()} returns \constant{True} only if
410all of the values returned by the iterator evaluate as being true.
411
412% XXX who added?
413
414
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000415\item The list of base classes in a class definition can now be empty.
416As an example, this is now legal:
417
418\begin{verbatim}
419class C():
420 pass
421\end{verbatim}
422(Implemented by Brett Cannon.)
423
Fred Drake2db76802004-12-01 05:05:47 +0000424\end{itemize}
425
426
427%======================================================================
428\subsection{Optimizations}
429
430\begin{itemize}
431
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000432\item When they were introduced
433in Python 2.4, the built-in \class{set} and \class{frozenset} types
434were built on top of Python's dictionary type.
435In 2.5 the internal data structure has been customized for implementing sets,
436and as a result sets will use a third less memory and are somewhat faster.
437(Implemented by Raymond Hettinger.)
Fred Drake2db76802004-12-01 05:05:47 +0000438
439\end{itemize}
440
441The net result of the 2.5 optimizations is that Python 2.5 runs the
Andrew M. Kuchling92e24952004-12-03 13:54:09 +0000442pystone benchmark around XX\% faster than Python 2.4.
Fred Drake2db76802004-12-01 05:05:47 +0000443
444
445%======================================================================
446\section{New, Improved, and Deprecated Modules}
447
448As usual, Python's standard library received a number of enhancements and
449bug fixes. Here's a partial list of the most notable changes, sorted
450alphabetically by module name. Consult the
451\file{Misc/NEWS} file in the source tree for a more
452complete list of changes, or look through the CVS logs for all the
453details.
454
455\begin{itemize}
456
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000457% collections.deque now has .remove()
458
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000459% the cPickle module no longer accepts the deprecated None option in the
460% args tuple returned by __reduce__().
461
462% csv module improvements
463
464% datetime.datetime() now has a strptime class method which can be used to
465% create datetime object using a string and format.
466
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000467\item A new \module{hashlib} module has been added to replace the
468\module{md5} and \module{sha} modules. \module{hashlib} adds support
469for additional secure hashes (SHA-224, SHA-256, SHA-384, and SHA-512).
470When available, the module uses OpenSSL for fast platform optimized
471implementations of algorithms. The old \module{md5} and \module{sha}
472modules still exist as wrappers around hashlib to preserve backwards
473compatibility. (Contributed by Gregory P. Smith.)
474
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000475\item The \function{nsmallest()} and
476\function{nlargest()} functions in the \module{heapq} module
477now support a \code{key} keyword argument similar to the one
478provided by the \function{min()}/\function{max()} functions
479and the \method{sort()} methods. For example:
480Example:
481
482\begin{verbatim}
483>>> import heapq
484>>> L = ["short", 'medium', 'longest', 'longer still']
485>>> heapq.nsmallest(2, L) # Return two lowest elements, lexicographically
486['longer still', 'longest']
487>>> heapq.nsmallest(2, L, key=len) # Return two shortest elements
488['short', 'medium']
489\end{verbatim}
490
491(Contributed by Raymond Hettinger.)
492
Andrew M. Kuchling511a3a82005-03-20 19:52:18 +0000493\item The \function{itertools.islice()} function now accepts
494\code{None} for the start and step arguments. This makes it more
495compatible with the attributes of slice objects, so that you can now write
496the following:
497
498\begin{verbatim}
499s = slice(5) # Create slice object
500itertools.islice(iterable, s.start, s.stop, s.step)
501\end{verbatim}
502
503(Contributed by Raymond Hettinger.)
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000504
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000505\item The \module{operator} module's \function{itemgetter()}
506and \function{attrgetter()} functions now support multiple fields.
507A call such as \code{operator.attrgetter('a', 'b')}
508will return a function
509that retrieves the \member{a} and \member{b} attributes. Combining
510this new feature with the \method{sort()} method's \code{key} parameter
511lets you easily sort lists using multiple fields.
512
513% XXX who added?
514
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000515
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000516\item The \module{os} module underwent a number of changes. The
517\member{stat_float_times} variable now defaults to true, meaning that
518\function{os.stat()} will now return time values as floats. (This
519doesn't necessarily mean that \function{os.stat()} will return times
520that are precise to fractions of a second; not all systems support
521such precision.)
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000522
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000523Constants named \member{os.SEEK_SET}, \member{os.SEEK_CUR}, and
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000524\member{os.SEEK_END} have been added; these are the parameters to the
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000525\function{os.lseek()} function. Two new constants for locking are
526\member{os.O_SHLOCK} and \member{os.O_EXLOCK}.
527
528On FreeBSD, the \function{os.stat()} function now returns
529times with nanosecond resolution, and the returned object
530now has \member{st_gen} and \member{st_birthtime}.
531The \member{st_flags} member is also available, if the platform supports it.
532% XXX patch 1180695, 1212117
533
Andrew M. Kuchling4678dc82006-01-15 16:11:28 +0000534\item The \module{socket} module now supports \constant{AF_NETLINK}
535sockets on Linux, thanks to a patch from Philippe Biondi.
536Netlink sockets are a Linux-specific mechanism for communications
537between a user-space process and kernel code; an introductory
538article about them is at \url{http://www.linuxjournal.com/article/7356}.
539In Python code, netlink addresses are represented as a tuple of 2 integers,
540\code{(\var{pid}, \var{group_mask})}.
541
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000542\item New module: \module{spwd} provides functions for accessing the
543shadow password database on systems that support it.
544% XXX give example
Fred Drake2db76802004-12-01 05:05:47 +0000545
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000546\item The \class{TarFile} class in the \module{tarfile} module now has
Georg Brandl08c02db2005-07-22 18:39:19 +0000547an \method{extractall()} method that extracts all members from the
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000548archive into the current working directory. It's also possible to set
549a different directory as the extraction target, and to unpack only a
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000550subset of the archive's members.
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000551
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000552A tarfile's compression can be autodetected by
553using the mode \code{'r|*'}.
554% patch 918101
555(Contributed by Lars Gust\"abel.)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000556
Fredrik Lundh7e0aef02005-12-12 18:54:55 +0000557\item A new package \module{xml.etree} has been added, which contains
558a subset of the ElementTree XML library. Available modules are
559\module{ElementTree}, \module{ElementPath}, and
560\module{ElementInclude}, from ElementTree 1.2.6. (Contributed by
561Fredrik Lundh.)
562
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000563\item The \module{xmlrpclib} module now supports returning
564 \class{datetime} objects for the XML-RPC date type. Supply
565 \code{use_datetime=True} to the \function{loads()} function
566 or the \class{Unmarshaller} class to enable this feature.
567% XXX patch 1120353
568
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000569
Fred Drake114b8ca2005-03-21 05:47:11 +0000570\end{itemize}
Andrew M. Kuchlinge9b1bf42005-03-20 19:26:30 +0000571
Fred Drake2db76802004-12-01 05:05:47 +0000572
573
574%======================================================================
575% whole new modules get described in \subsections here
576
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000577% XXX new distutils features: upload
578
Fredrik Lundh7e0aef02005-12-12 18:54:55 +0000579% XXX should hashlib perhaps be described here instead?
580% XXX should xml.etree perhaps be described here instead?
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000581
582
Fred Drake2db76802004-12-01 05:05:47 +0000583
584% ======================================================================
585\section{Build and C API Changes}
586
587Changes to Python's build process and to the C API include:
588
589\begin{itemize}
590
Andrew M. Kuchlingdb85ed52005-10-23 21:52:59 +0000591\item The design of the bytecode compiler has changed a great deal, no
592longer generating bytecode by traversing the parse tree. Instead
593the parse tree is converted to an abstract syntax tree (or AST), and it is
594the abstract syntax tree that's traversed to produce the bytecode.
595
596No documentation has been written for the AST code yet. To start
597learning about it, read the definition of the various AST nodes in
598\file{Parser/Python.asdl}. A Python script reads this file and
599generates a set of C structure definitions in
600\file{Include/Python-ast.h}. The \cfunction{PyParser_ASTFromString()}
601and \cfunction{PyParser_ASTFromFile()}, defined in
602\file{Include/pythonrun.h}, take Python source as input and return the
603root of an AST representing the contents. This AST can then be turned
604into a code object by \cfunction{PyAST_Compile()}. For more
605information, read the source code, and then ask questions on
606python-dev.
607
608% List of names taken from Jeremy's python-dev post at
609% http://mail.python.org/pipermail/python-dev/2005-October/057500.html
610The AST code was developed under Jeremy Hylton's management, and
611implemented by (in alphabetical order) Brett Cannon, Nick Coghlan,
612Grant Edwards, John Ehresman, Kurt Kaiser, Neal Norwitz, Tim Peters,
613Armin Rigo, and Neil Schemenauer, plus the participants in a number of
614AST sprints at conferences such as PyCon.
615
Andrew M. Kuchling150e3492005-08-23 00:56:06 +0000616\item The built-in set types now have an official C API. Call
617\cfunction{PySet_New()} and \cfunction{PyFrozenSet_New()} to create a
618new set, \cfunction{PySet_Add()} and \cfunction{PySet_Discard()} to
619add and remove elements, and \cfunction{PySet_Contains} and
620\cfunction{PySet_Size} to examine the set's state.
621
622\item The \cfunction{PyRange_New()} function was removed. It was
623never documented, never used in the core code, and had dangerously lax
624error checking.
Fred Drake2db76802004-12-01 05:05:47 +0000625
626\end{itemize}
627
628
629%======================================================================
630\subsection{Port-Specific Changes}
631
632Platform-specific changes go here.
633
634
635%======================================================================
636\section{Other Changes and Fixes \label{section-other}}
637
638As usual, there were a bunch of other improvements and bugfixes
639scattered throughout the source tree. A search through the CVS change
640logs finds there were XXX patches applied and YYY bugs fixed between
Andrew M. Kuchling92e24952004-12-03 13:54:09 +0000641Python 2.4 and 2.5. Both figures are likely to be underestimates.
Fred Drake2db76802004-12-01 05:05:47 +0000642
643Some of the more notable changes are:
644
645\begin{itemize}
646
647\item Details go here.
648
649\end{itemize}
650
651
652%======================================================================
653\section{Porting to Python 2.5}
654
655This section lists previously described changes that may require
656changes to your code:
657
658\begin{itemize}
659
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000660\item Some old deprecated modules (\module{statcache}, \module{tzparse},
661 \module{whrandom}) have been moved to \file{Lib/lib-old}.
Andrew M. Kuchling0c35db92005-03-20 20:06:49 +0000662You can get access to these modules again by adding the directory
663to your \code{sys.path}:
664
665\begin{verbatim}
666import os
667from distutils import sysconfig
668
669lib_dir = sysconfig.get_python_lib(standard_lib=True)
670old_dir = os.path.join(lib_dir, 'lib-old')
671sys.path.append(old_dir)
672\end{verbatim}
673
674Doing so is discouraged, however; it's better to update any code that
675still uses these modules.
Andrew M. Kuchling3e41b052005-03-01 00:53:46 +0000676
677% the pickle module no longer uses the deprecated bin parameter.
Fred Drake2db76802004-12-01 05:05:47 +0000678
679\end{itemize}
680
681
682%======================================================================
683\section{Acknowledgements \label{acks}}
684
685The author would like to thank the following people for offering
686suggestions, corrections and assistance with various drafts of this
687article: .
688
689\end{document}