blob: b61ab478a45cfdfa7f269ded7c338c23d272c295 [file] [log] [blame]
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001\documentclass{howto}
2\usepackage{distutils}
3% $Id$
4
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +00005% Don't write extensive text for new sections; I'll do that.
6% Feel free to add commented-out reminders of things that need
7% to be covered. --amk
8
Fred Drakeed0fa3d2003-07-30 19:14:09 +00009\title{What's New in Python 2.4}
Andrew M. Kuchling74666592004-11-19 14:26:23 +000010\release{1.0}
Fred Drakeed0fa3d2003-07-30 19:14:09 +000011\author{A.M.\ Kuchling}
Fred Drakeb914ef02004-01-02 06:57:50 +000012\authoraddress{
13 \strong{Python Software Foundation}\\
14 Email: \email{amk@amk.ca}
15}
Fred Drakeed0fa3d2003-07-30 19:14:09 +000016
17\begin{document}
18\maketitle
19\tableofcontents
20
Andrew M. Kuchling74666592004-11-19 14:26:23 +000021This article explains the new features in Python 2.4, released in December
222004.
Fred Drakeed0fa3d2003-07-30 19:14:09 +000023
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +000024Python 2.4 is a medium-sized release. It doesn't introduce as many
Andrew M. Kuchling3b790912004-07-04 16:39:40 +000025changes as the radical Python 2.2, but introduces more features than
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +000026the conservative 2.3 release. The most significant new language
27features are function decorators and generator expressions; most other
28changes are to the standard library.
29
Andrew M. Kuchling74666592004-11-19 14:26:23 +000030According to the CVS change logs, there were 481 patches applied and
31502 bugs fixed between Python 2.3 and 2.4. Both figures are likely to
Andrew M. Kuchlinga331e862004-09-10 13:05:22 +000032be underestimates.
33
Fred Drakeed0fa3d2003-07-30 19:14:09 +000034This article doesn't attempt to provide a complete specification of
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +000035every single new feature, but instead provides a brief introduction to
36each feature. For full details, you should refer to the documentation
37for Python 2.4, such as the \citetitle[../lib/lib.html]{Python Library
38Reference} and the \citetitle[../ref/ref.html]{Python Reference
39Manual}. Often you will be referred to the PEP for a particular new
40feature for explanations of the implementation and design rationale.
Fred Drakeed0fa3d2003-07-30 19:14:09 +000041
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +000042
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000043%======================================================================
44\section{PEP 218: Built-In Set Objects}
45
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +000046Python 2.3 introduced the \module{sets} module. C implementations of
47set data types have now been added to the Python core as two new
48built-in types, \function{set(\var{iterable})} and
49\function{frozenset(\var{iterable})}. They provide high speed
50operations for membership testing, for eliminating duplicates from
51sequences, and for mathematical operations like unions, intersections,
52differences, and symmetric differences.
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000053
54\begin{verbatim}
55>>> a = set('abracadabra') # form a set from a string
56>>> 'z' in a # fast membership testing
57False
58>>> a # unique letters in a
59set(['a', 'r', 'b', 'c', 'd'])
60>>> ''.join(a) # convert back into a string
61'arbcd'
Raymond Hettingerd4462302003-11-26 17:52:45 +000062
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000063>>> b = set('alacazam') # form a second set
64>>> a - b # letters in a but not in b
65set(['r', 'd', 'b'])
66>>> a | b # letters in either a or b
67set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
68>>> a & b # letters in both a and b
69set(['a', 'c'])
70>>> a ^ b # letters in a or b but not both
71set(['r', 'd', 'b', 'm', 'z', 'l'])
Raymond Hettingerd4462302003-11-26 17:52:45 +000072
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000073>>> a.add('z') # add a new element
74>>> a.update('wxy') # add multiple new elements
75>>> a
76set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z'])
77>>> a.remove('x') # take one element out
78>>> a
79set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z'])
80\end{verbatim}
81
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +000082The \function{frozenset} type is an immutable version of \function{set}.
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000083Since it is immutable and hashable, it may be used as a dictionary key or
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +000084as a member of another set.
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000085
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +000086The \module{sets} module remains in the standard library, and may be
87useful if you wish to subclass the \class{Set} or \class{ImmutableSet}
88classes. There are currently no plans to deprecate the module.
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +000089
Raymond Hettinger7e0282f2003-11-24 07:14:54 +000090\begin{seealso}
91\seepep{218}{Adding a Built-In Set Object Type}{Originally proposed by
92Greg Wilson and ultimately implemented by Raymond Hettinger.}
93\end{seealso}
Fred Drakeed0fa3d2003-07-30 19:14:09 +000094
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +000095
Fred Drakeed0fa3d2003-07-30 19:14:09 +000096%======================================================================
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +000097\section{PEP 237: Unifying Long Integers and Integers}
98
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +000099The lengthy transition process for this PEP, begun in Python 2.2,
Andrew M. Kuchlingd4be86c2004-07-04 01:44:04 +0000100takes another step forward in Python 2.4. In 2.3, certain integer
101operations that would behave differently after int/long unification
102triggered \exception{FutureWarning} warnings and returned values
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +0000103limited to 32 or 64 bits (depending on your platform). In 2.4, these
104expressions no longer produce a warning and instead produce a
105different result that's usually a long integer.
Andrew M. Kuchlingd4be86c2004-07-04 01:44:04 +0000106
107The problematic expressions are primarily left shifts and lengthy
Raymond Hettingerca1a7752004-07-12 13:00:45 +0000108hexadecimal and octal constants. For example,
109\code{2 \textless{}\textless{} 32} results
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +0000110in a warning in 2.3, evaluating to 0 on 32-bit platforms. In Python
1112.4, this expression now returns the correct answer, 8589934592.
Andrew M. Kuchlingd4be86c2004-07-04 01:44:04 +0000112
113\begin{seealso}
114\seepep{237}{Unifying Long Integers and Integers}{Original PEP
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +0000115written by Moshe Zadka and GvR. The changes for 2.4 were implemented by
Andrew M. Kuchlingd4be86c2004-07-04 01:44:04 +0000116Kalle Svensson.}
117\end{seealso}
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000118
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000119
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000120%======================================================================
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000121\section{PEP 289: Generator Expressions}
Raymond Hettinger354433a2004-05-19 08:20:33 +0000122
Andrew M. Kuchling38dc2a62004-08-07 13:24:12 +0000123The iterator feature introduced in Python 2.2 and the
124\module{itertools} module make it easier to write programs that loop
125through large data sets without having the entire data set in memory
126at one time. List comprehensions don't fit into this picture very
127well because they produce a Python list object containing all of the
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000128items. This unavoidably pulls all of the objects into memory, which
129can be a problem if your data set is very large. When trying to write
Andrew M. Kuchling38dc2a62004-08-07 13:24:12 +0000130a functionally-styled program, it would be natural to write something
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +0000131like:
Raymond Hettinger354433a2004-05-19 08:20:33 +0000132
133\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000134links = [link for link in get_all_links() if not link.followed]
135for link in links:
136 ...
Raymond Hettinger354433a2004-05-19 08:20:33 +0000137\end{verbatim}
138
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000139instead of
Raymond Hettinger354433a2004-05-19 08:20:33 +0000140
141\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000142for link in get_all_links():
143 if link.followed:
144 continue
145 ...
146\end{verbatim}
Raymond Hettinger354433a2004-05-19 08:20:33 +0000147
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000148The first form is more concise and perhaps more readable, but if
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000149you're dealing with a large number of link objects you'd have to write
150the second form to avoid having all link objects in memory at the same
151time.
Raymond Hettinger354433a2004-05-19 08:20:33 +0000152
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000153Generator expressions work similarly to list comprehensions but don't
154materialize the entire list; instead they create a generator that will
155return elements one by one. The above example could be written as:
Raymond Hettinger354433a2004-05-19 08:20:33 +0000156
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000157\begin{verbatim}
158links = (link for link in get_all_links() if not link.followed)
159for link in links:
160 ...
161\end{verbatim}
Raymond Hettinger170a6222004-05-19 19:45:19 +0000162
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000163Generator expressions always have to be written inside parentheses, as
164in the above example. The parentheses signalling a function call also
165count, so if you want to create a iterator that will be immediately
166passed to a function you could write:
Raymond Hettinger170a6222004-05-19 19:45:19 +0000167
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000168\begin{verbatim}
169print sum(obj.count for obj in list_all_objects())
170\end{verbatim}
Raymond Hettinger170a6222004-05-19 19:45:19 +0000171
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +0000172Generator expressions differ from list comprehensions in various small
173ways. Most notably, the loop variable (\var{obj} in the above
174example) is not accessible outside of the generator expression. List
175comprehensions leave the variable assigned to its last value; future
176versions of Python will change this, making list comprehensions match
177generator expressions in this respect.
Raymond Hettinger354433a2004-05-19 08:20:33 +0000178
179\begin{seealso}
180\seepep{289}{Generator Expressions}{Proposed by Raymond Hettinger and
181implemented by Jiwon Seo with early efforts steered by Hye-Shik Chang.}
182\end{seealso}
183
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +0000184
185%======================================================================
186\section{PEP 292: Simpler String Substitutions}
187
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000188Some new classes in the standard library provide an alternative
189mechanism for substituting variables into strings; this style of
190substitution may be better for applications where untrained
191users need to edit templates.
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +0000192
193The usual way of substituting variables by name is the \code{\%}
194operator:
195
196\begin{verbatim}
197>>> '%(page)i: %(title)s' % {'page':2, 'title': 'The Best of Times'}
198'2: The Best of Times'
199\end{verbatim}
200
201When writing the template string, it can be easy to forget the
202\samp{i} or \samp{s} after the closing parenthesis. This isn't a big
203problem if the template is in a Python module, because you run the
204code, get an ``Unsupported format character'' \exception{ValueError},
205and fix the problem. However, consider an application such as Mailman
206where template strings or translations are being edited by users who
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000207aren't aware of the Python language. The format string's syntax is
208complicated to explain to such users, and if they make a mistake, it's
209difficult to provide helpful feedback to them.
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +0000210
211PEP 292 adds a \class{Template} class to the \module{string} module
212that uses \samp{\$} to indicate a substitution. \class{Template} is a
213subclass of the built-in Unicode type, so the result is always a
214Unicode string:
215
216\begin{verbatim}
217>>> import string
218>>> t = string.Template('$page: $title')
Andrew M. Kuchlinga79ec222004-09-10 11:34:39 +0000219>>> t.substitute({'page':2, 'title': 'The Best of Times'})
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +0000220u'2: The Best of Times'
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +0000221\end{verbatim}
222
223% $ Terminate $-mode for Emacs
224
Andrew M. Kuchlinga79ec222004-09-10 11:34:39 +0000225If a key is missing from the dictionary, the \method{substitute} method
226will raise a \exception{KeyError}. There's also a \method{safe_substitute}
227method that ignores missing keys:
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +0000228
229\begin{verbatim}
230>>> t = string.SafeTemplate('$page: $title')
Andrew M. Kuchlinga79ec222004-09-10 11:34:39 +0000231>>> t.safe_substitute({'page':3})
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +0000232u'3: $title'
233\end{verbatim}
234
Andrew M. Kuchlinga331e862004-09-10 13:05:22 +0000235% $ Terminate math-mode for Emacs
236
237
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +0000238\begin{seealso}
239\seepep{292}{Simpler String Substitutions}{Written and implemented
240by Barry Warsaw.}
241\end{seealso}
242
243
Raymond Hettinger354433a2004-05-19 08:20:33 +0000244%======================================================================
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000245\section{PEP 318: Decorators for Functions and Methods}
Andrew M. Kuchlingd91fcbe2004-08-02 12:44:28 +0000246
Andrew M. Kuchling77a602f2004-08-02 13:48:18 +0000247Python 2.2 extended Python's object model by adding static methods and
248class methods, but it didn't extend Python's syntax to provide any new
249way of defining static or class methods. Instead, you had to write a
250\keyword{def} statement in the usual way, and pass the resulting
251method to a \function{staticmethod()} or \function{classmethod()}
252function that would wrap up the function as a method of the new type.
253Your code would look like this:
254
255\begin{verbatim}
256class C:
257 def meth (cls):
258 ...
259
260 meth = classmethod(meth) # Rebind name to wrapped-up class method
261\end{verbatim}
262
263If the method was very long, it would be easy to miss or forget the
264\function{classmethod()} invocation after the function body.
265
266The intention was always to add some syntax to make such definitions
267more readable, but at the time of 2.2's release a good syntax was not
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000268obvious. Today a good syntax \emph{still} isn't obvious but users are
269asking for easier access to the feature; a new syntactic feature has
270been added to meet this need.
Andrew M. Kuchling77a602f2004-08-02 13:48:18 +0000271
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000272The new feature is called ``function decorators''. The name comes
273from the idea that \function{classmethod}, \function{staticmethod},
274and friends are storing additional information on a function object;
275they're \emph{decorating} functions with more details.
Andrew M. Kuchling77a602f2004-08-02 13:48:18 +0000276
Fred Drake3f5c6542004-08-06 03:34:20 +0000277The notation borrows from Java and uses the \character{@} character as an
Andrew M. Kuchling77a602f2004-08-02 13:48:18 +0000278indicator. Using the new syntax, the example above would be written:
279
280\begin{verbatim}
281class C:
282
283 @classmethod
284 def meth (cls):
285 ...
286
287\end{verbatim}
288
289The \code{@classmethod} is shorthand for the
Fred Drake3f5c6542004-08-06 03:34:20 +0000290\code{meth=classmethod(meth)} assignment. More generally, if you have
Andrew M. Kuchling77a602f2004-08-02 13:48:18 +0000291the following:
292
293\begin{verbatim}
294@A @B @C
295def f ():
296 ...
297\end{verbatim}
298
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000299It's equivalent to the following pre-decorator code:
Andrew M. Kuchling77a602f2004-08-02 13:48:18 +0000300
301\begin{verbatim}
302def f(): ...
Andrew M. Kuchlingcebdd3c2004-10-08 18:29:29 +0000303f = A(B(C(f)))
Andrew M. Kuchling77a602f2004-08-02 13:48:18 +0000304\end{verbatim}
305
306Decorators must come on the line before a function definition, and
307can't be on the same line, meaning that \code{@A def f(): ...} is
308illegal. You can only decorate function definitions, either at the
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000309module level or inside a class; you can't decorate class definitions.
Andrew M. Kuchling77a602f2004-08-02 13:48:18 +0000310
311A decorator is just a function that takes the function to be decorated
312as an argument and returns either the same function or some new
313callable thing. It's easy to write your own decorators. The
314following simple example just sets an attribute on the function
315object:
316
317\begin{verbatim}
318>>> def deco(func):
319... func.attr = 'decorated'
320... return func
321...
322>>> @deco
323... def f(): pass
324...
325>>> f
326<function f at 0x402ef0d4>
327>>> f.attr
328'decorated'
329>>>
330\end{verbatim}
331
332As a slightly more realistic example, the following decorator checks
333that the supplied argument is an integer:
334
335\begin{verbatim}
336def require_int (func):
337 def wrapper (arg):
338 assert isinstance(arg, int)
339 return func(arg)
340
341 return wrapper
342
343@require_int
344def p1 (arg):
345 print arg
346
347@require_int
348def p2(arg):
349 print arg*2
350\end{verbatim}
351
352An example in \pep{318} contains a fancier version of this idea that
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000353lets you both specify the required type and check the returned type.
Andrew M. Kuchling77a602f2004-08-02 13:48:18 +0000354
355Decorator functions can take arguments. If arguments are supplied,
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000356your decorator function is called with only those arguments and must
357return a new decorator function; this function must take a single
Andrew M. Kuchling77a602f2004-08-02 13:48:18 +0000358function and return a function, as previously described. In other
359words, \code{@A @B @C(args)} becomes:
360
361\begin{verbatim}
362def f(): ...
363_deco = C(args)
Andrew M. Kuchlingcebdd3c2004-10-08 18:29:29 +0000364f = A(B(_deco(f)))
Andrew M. Kuchling77a602f2004-08-02 13:48:18 +0000365\end{verbatim}
366
367Getting this right can be slightly brain-bending, but it's not too
368difficult.
369
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +0000370A small related change makes the \member{func_name} attribute of
371functions writable. This attribute is used to display function names
372in tracebacks, so decorators should change the name of any new
373function that's constructed and returned.
374
Andrew M. Kuchlingd91fcbe2004-08-02 12:44:28 +0000375\begin{seealso}
376\seepep{318}{Decorators for Functions, Methods and Classes}{Written
Andrew M. Kuchling77a602f2004-08-02 13:48:18 +0000377by Kevin D. Smith, Jim Jewett, and Skip Montanaro. Several people
378wrote patches implementing function decorators, but the one that was
Fred Drakee72bd4d2004-08-02 21:50:26 +0000379actually checked in was patch \#979728, written by Mark Russell.}
Andrew M. Kuchlingd91fcbe2004-08-02 12:44:28 +0000380\end{seealso}
381
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000382% XXX add link to decorators module in Wiki
383
384
Andrew M. Kuchlingd91fcbe2004-08-02 12:44:28 +0000385%======================================================================
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000386\section{PEP 322: Reverse Iteration}
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000387
Fred Drake56fcc232004-05-06 02:55:35 +0000388A new built-in function, \function{reversed(\var{seq})}, takes a sequence
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +0000389and returns an iterator that loops over the elements of the sequence
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000390in reverse order.
391
392\begin{verbatim}
Raymond Hettingerbc3cba22003-11-12 16:39:30 +0000393>>> for i in reversed(xrange(1,4)):
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000394... print i
395...
3963
3972
3981
399\end{verbatim}
400
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +0000401Compared to extended slicing, such as \code{range(1,4)[::-1]},
402\function{reversed()} is easier to read, runs faster, and uses
403substantially less memory.
Raymond Hettingerbc3cba22003-11-12 16:39:30 +0000404
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000405Note that \function{reversed()} only accepts sequences, not arbitrary
Raymond Hettingerbc3cba22003-11-12 16:39:30 +0000406iterators. If you want to reverse an iterator, first convert it to
407a list with \function{list()}.
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000408
409\begin{verbatim}
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000410>>> input = open('/etc/passwd', 'r')
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000411>>> for line in reversed(list(input)):
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000412... print line
413...
414root:*:0:0:System Administrator:/var/root:/bin/tcsh
415 ...
416\end{verbatim}
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000417
Andrew M. Kuchlingf7a6b672003-11-08 16:05:37 +0000418\begin{seealso}
419\seepep{322}{Reverse Iteration}{Written and implemented by Raymond Hettinger.}
420
421\end{seealso}
422
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000423
424%======================================================================
Andrew M. Kuchlingc9e7d772004-10-12 15:58:02 +0000425\section{PEP 324: New subprocess Module}
426
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000427The standard library provides a number of ways to execute a
428subprocess, offering different features and different levels of
429complexity. \function{os.system(\var{command})} is easy to use, but
430slow (it runs a shell process which executes the command) and
431dangerous (you have to be careful about escaping the shell's
432metacharacters). The \module{popen2} module offers classes that can
433capture standard output and standard error from the subprocess, but
434the naming is confusing. The \module{subprocess} module cleans
435this up, providing a unified interface that offers all the features
436you might need.
Andrew M. Kuchlingc9e7d772004-10-12 15:58:02 +0000437
Andrew M. Kuchlingb6ffc272004-10-12 16:36:57 +0000438Instead of \module{popen2}'s collection of classes,
439\module{subprocess} contains a single class called \class{Popen}
440whose constructor supports a number of different keyword arguments.
Andrew M. Kuchlingc9e7d772004-10-12 15:58:02 +0000441
Andrew M. Kuchlingb6ffc272004-10-12 16:36:57 +0000442\begin{verbatim}
443class Popen(args, bufsize=0, executable=None,
444 stdin=None, stdout=None, stderr=None,
445 preexec_fn=None, close_fds=False, shell=False,
446 cwd=None, env=None, universal_newlines=False,
447 startupinfo=None, creationflags=0):
448\end{verbatim}
Andrew M. Kuchlingc9e7d772004-10-12 15:58:02 +0000449
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000450\var{args} is commonly a sequence of strings that will be the
451arguments to the program executed as the subprocess. (If the
452\var{shell} argument is true, \var{args} can be a string which will
453then be passed on to the shell for interpretation, just as
454\function{os.system()} does.)
Andrew M. Kuchlingb6ffc272004-10-12 16:36:57 +0000455
456\var{stdin}, \var{stdout}, and \var{stderr} specify what the
457subprocess's input, output, and error streams will be. You can
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000458provide a file object or a file descriptor, or you can use the
459constant \code{subprocess.PIPE} to create a pipe between the
460subprocess and the parent.
Andrew M. Kuchlingb6ffc272004-10-12 16:36:57 +0000461
462The constructor has a number of handy options:
463
464\begin{itemize}
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000465 \item \var{close_fds} requests that all file descriptors be closed
466 before running the subprocess.
467
468 \item \var{cwd} specifies the working directory in which the
469 subprocess will be executed (defaulting to whatever the parent's
470 working directory is).
471
Andrew M. Kuchlingb6ffc272004-10-12 16:36:57 +0000472 \item \var{env} is a dictionary specifying environment variables.
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000473
474 \item \var{preexec_fn} is a function that gets called before the
475 child is started.
476
477 \item \var{universal_newlines} opens the child's input and output
478 using Python's universal newline feature.
479
Andrew M. Kuchlingb6ffc272004-10-12 16:36:57 +0000480\end{itemize}
481
482Once you've created the \class{Popen} instance,
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000483you can call its \method{wait()} method to pause until the subprocess
484has exited, \method{poll()} to check if it's exited without pausing,
485or \method{communicate(\var{data})} to send the string \var{data} to
Andrew M. Kuchlingb6ffc272004-10-12 16:36:57 +0000486the subprocess's standard input. \method{communicate(\var{data})}
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000487then reads any data that the subprocess has sent to its standard output
488or standard error, returning a tuple \code{(\var{stdout_data},
489\var{stderr_data})}.
Andrew M. Kuchlingb6ffc272004-10-12 16:36:57 +0000490
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000491\function{call()} is a shortcut that passes its arguments along to the
492\class{Popen} constructor, waits for the command to complete, and
493returns the status code of the subprocess. It can serve as a safer
494analog to \function{os.system()}:
Andrew M. Kuchlingb6ffc272004-10-12 16:36:57 +0000495
496\begin{verbatim}
497sts = subprocess.call(['dpkg', '-i', '/tmp/new-package.deb'])
498if sts == 0:
499 # Success
500 ...
501else:
502 # dpkg returned an error
503 ...
504\end{verbatim}
505
506The command is invoked without use of the shell. If you really do want to
507use the shell, you can add \code{shell=True} as a keyword argument and provide
508a string instead of a sequence:
509
510\begin{verbatim}
511sts = subprocess.call('dpkg -i /tmp/new-package.deb', shell=True)
512\end{verbatim}
513
514The PEP takes various examples of shell and Python code and shows how
515they'd be translated into Python code that uses \module{subprocess}.
516Reading this section of the PEP is highly recommended.
Andrew M. Kuchlingc9e7d772004-10-12 15:58:02 +0000517
518\begin{seealso}
Andrew M. Kuchlingb6ffc272004-10-12 16:36:57 +0000519\seepep{324}{subprocess - New process module}{Written and implemented by Peter {\AA}strand, with assistance from Fredrik Lundh and others.}
Andrew M. Kuchlingc9e7d772004-10-12 15:58:02 +0000520\end{seealso}
521
Andrew M. Kuchlingb6ffc272004-10-12 16:36:57 +0000522
Andrew M. Kuchlingc9e7d772004-10-12 15:58:02 +0000523%======================================================================
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000524\section{PEP 327: Decimal Data Type}
525
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000526Python has always supported floating-point (FP) numbers, based on the
527underlying C \ctype{double} type, as a data type. However, while most
528programming languages provide a floating-point type, most people (even
529programmers) are unaware that computing with floating-point numbers
530entails certain unavoidable inaccuracies. The new decimal type
531provides a way to avoid these inaccuracies.
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000532
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000533\subsection{Why is Decimal needed?}
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000534
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000535The limitations arise from the representation used for floating-point numbers.
536FP numbers are made up of three components:
537
538\begin{itemize}
Andrew M. Kuchlinge34c3bd2004-08-31 12:21:44 +0000539\item The sign, which is positive or negative.
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000540\item The mantissa, which is a single-digit binary number
541followed by a fractional part. For example, \code{1.01} in base-2 notation
542is \code{1 + 0/2 + 1/4}, or 1.25 in decimal notation.
543\item The exponent, which tells where the decimal point is located in the number represented.
544\end{itemize}
545
Andrew M. Kuchlinge34c3bd2004-08-31 12:21:44 +0000546For example, the number 1.25 has positive sign, a mantissa value of
5471.01 (in binary), and an exponent of 0 (the decimal point doesn't need
548to be shifted). The number 5 has the same sign and mantissa, but the
549exponent is 2 because the mantissa is multiplied by 4 (2 to the power
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000550of the exponent 2); 1.25 * 4 equals 5.
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000551
552Modern systems usually provide floating-point support that conforms to
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000553a standard called IEEE 754. C's \ctype{double} type is usually
554implemented as a 64-bit IEEE 754 number, which uses 52 bits of space
555for the mantissa. This means that numbers can only be specified to 52
556bits of precision. If you're trying to represent numbers whose
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000557expansion repeats endlessly, the expansion is cut off after 52 bits.
558Unfortunately, most software needs to produce output in base 10, and
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000559common fractions in base 10 are often repeating decimals in binary.
560For example, 1.1 decimal is binary \code{1.0001100110011 ...}; .1 =
5611/16 + 1/32 + 1/256 plus an infinite number of additional terms. IEEE
562754 has to chop off that infinitely repeated decimal after 52 digits,
563so the representation is slightly inaccurate.
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000564
565Sometimes you can see this inaccuracy when the number is printed:
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000566\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000567>>> 1.1
5681.1000000000000001
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000569\end{verbatim}
570
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000571The inaccuracy isn't always visible when you print the number because
Andrew M. Kuchlinge34c3bd2004-08-31 12:21:44 +0000572the FP-to-decimal-string conversion is provided by the C library, and
573most C libraries try to produce sensible output. Even if it's not
574displayed, however, the inaccuracy is still there and subsequent
575operations can magnify the error.
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000576
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000577For many applications this doesn't matter. If I'm plotting points and
578displaying them on my monitor, the difference between 1.1 and
5791.1000000000000001 is too small to be visible. Reports often limit
580output to a certain number of decimal places, and if you round the
581number to two or three or even eight decimal places, the error is
582never apparent. However, for applications where it does matter,
583it's a lot of work to implement your own custom arithmetic routines.
584
Andrew M. Kuchlinge34c3bd2004-08-31 12:21:44 +0000585Hence, the \class{Decimal} type was created.
586
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000587\subsection{The \class{Decimal} type}
588
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000589A new module, \module{decimal}, was added to Python's standard
590library. It contains two classes, \class{Decimal} and
591\class{Context}. \class{Decimal} instances represent numbers, and
592\class{Context} instances are used to wrap up various settings such as
593the precision and default rounding mode.
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000594
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000595\class{Decimal} instances are immutable, like regular Python integers
596and FP numbers; once it's been created, you can't change the value an
597instance represents. \class{Decimal} instances can be created from
598integers or strings:
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000599
600\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000601>>> import decimal
602>>> decimal.Decimal(1972)
603Decimal("1972")
604>>> decimal.Decimal("1.1")
605Decimal("1.1")
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000606\end{verbatim}
607
Andrew M. Kuchlinge34c3bd2004-08-31 12:21:44 +0000608You can also provide tuples containing the sign, the mantissa represented
609as a tuple of decimal digits, and the exponent:
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000610
611\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000612>>> decimal.Decimal((1, (1, 4, 7, 5), -2))
613Decimal("-14.75")
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000614\end{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000615
Andrew M. Kuchlinge34c3bd2004-08-31 12:21:44 +0000616Cautionary note: the sign bit is a Boolean value, so 0 is positive and
6171 is negative.
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000618
Andrew M. Kuchlinge34c3bd2004-08-31 12:21:44 +0000619Converting from floating-point numbers poses a bit of a problem:
620should the FP number representing 1.1 turn into the decimal number for
621exactly 1.1, or for 1.1 plus whatever inaccuracies are introduced?
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000622The decision was to dodge the issue and leave such a conversion out of
623the API. Instead, you should convert the floating-point number into a
624string using the desired precision and pass the string to the
625\class{Decimal} constructor:
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000626
627\begin{verbatim}
628>>> f = 1.1
629>>> decimal.Decimal(str(f))
630Decimal("1.1")
Andrew M. Kuchling0ad20f12004-07-21 13:00:06 +0000631>>> decimal.Decimal('%.12f' % f)
632Decimal("1.100000000000")
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000633\end{verbatim}
634
635Once you have \class{Decimal} instances, you can perform the usual
636mathematical operations on them. One limitation: exponentiation
637requires an integer exponent:
638
639\begin{verbatim}
640>>> a = decimal.Decimal('35.72')
641>>> b = decimal.Decimal('1.73')
642>>> a+b
643Decimal("37.45")
644>>> a-b
645Decimal("33.99")
646>>> a*b
647Decimal("61.7956")
648>>> a/b
Andrew M. Kuchling0ad20f12004-07-21 13:00:06 +0000649Decimal("20.64739884393063583815028902")
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000650>>> a ** 2
651Decimal("1275.9184")
Andrew M. Kuchling0ad20f12004-07-21 13:00:06 +0000652>>> a**b
653Traceback (most recent call last):
654 ...
655decimal.InvalidOperation: x ** (non-integer)
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000656\end{verbatim}
657
658You can combine \class{Decimal} instances with integers, but not with
659floating-point numbers:
660
661\begin{verbatim}
662>>> a + 4
663Decimal("39.72")
664>>> a + 4.5
665Traceback (most recent call last):
666 ...
667TypeError: You can interact Decimal only with int, long or Decimal data types.
668>>>
669\end{verbatim}
670
671\class{Decimal} numbers can be used with the \module{math} and
Andrew M. Kuchling0ad20f12004-07-21 13:00:06 +0000672\module{cmath} modules, but note that they'll be immediately converted to
673floating-point numbers before the operation is performed, resulting in
674a possible loss of precision and accuracy. You'll also get back a
675regular floating-point number and not a \class{Decimal}.
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000676
677\begin{verbatim}
678>>> import math, cmath
679>>> d = decimal.Decimal('123456789012.345')
680>>> math.sqrt(d)
681351364.18288201344
682>>> cmath.sqrt(-d)
683351364.18288201344j
Andrew M. Kuchling0ad20f12004-07-21 13:00:06 +0000684\end{verbatim}
685
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000686\class{Decimal} instances have a \method{sqrt()} method that
687returns a \class{Decimal}, but if you need other things such as
688trigonometric functions you'll have to implement them.
Andrew M. Kuchling0ad20f12004-07-21 13:00:06 +0000689
690\begin{verbatim}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000691>>> d.sqrt()
Raymond Hettingerca1a7752004-07-12 13:00:45 +0000692Decimal("351364.1828820134592177245001")
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000693\end{verbatim}
694
695
696\subsection{The \class{Context} type}
697
698Instances of the \class{Context} class encapsulate several settings for
699decimal operations:
700
701\begin{itemize}
702 \item \member{prec} is the precision, the number of decimal places.
703 \item \member{rounding} specifies the rounding mode. The \module{decimal}
704 module has constants for the various possibilities:
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000705 \constant{ROUND_DOWN}, \constant{ROUND_CEILING},
706 \constant{ROUND_HALF_EVEN}, and various others.
Andrew M. Kuchling0ad20f12004-07-21 13:00:06 +0000707 \item \member{traps} is a dictionary specifying what happens on
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000708encountering certain error conditions: either an exception is raised or
709a value is returned. Some examples of error conditions are
710division by zero, loss of precision, and overflow.
711\end{itemize}
712
713There's a thread-local default context available by calling
714\function{getcontext()}; you can change the properties of this context
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000715to alter the default precision, rounding, or trap handling. The
716following example shows the effect of changing the precision of the default
717context:
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000718
719\begin{verbatim}
720>>> decimal.getcontext().prec
72128
722>>> decimal.Decimal(1) / decimal.Decimal(7)
Raymond Hettingerca1a7752004-07-12 13:00:45 +0000723Decimal("0.1428571428571428571428571429")
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000724>>> decimal.getcontext().prec = 9
725>>> decimal.Decimal(1) / decimal.Decimal(7)
Raymond Hettingerca1a7752004-07-12 13:00:45 +0000726Decimal("0.142857143")
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000727\end{verbatim}
728
Andrew M. Kuchling0ad20f12004-07-21 13:00:06 +0000729The default action for error conditions is selectable; the module can
730either return a special value such as infinity or not-a-number, or
731exceptions can be raised:
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000732
733\begin{verbatim}
734>>> decimal.Decimal(1) / decimal.Decimal(0)
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000735Traceback (most recent call last):
736 ...
737decimal.DivisionByZero: x / 0
Andrew M. Kuchling0ad20f12004-07-21 13:00:06 +0000738>>> decimal.getcontext().traps[decimal.DivisionByZero] = False
739>>> decimal.Decimal(1) / decimal.Decimal(0)
740Decimal("Infinity")
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000741>>>
742\end{verbatim}
743
744The \class{Context} instance also has various methods for formatting
745numbers such as \method{to_eng_string()} and \method{to_sci_string()}.
746
Andrew M. Kuchling0ad20f12004-07-21 13:00:06 +0000747For more information, see the documentation for the \module{decimal}
748module, which includes a quick-start tutorial and a reference.
749
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000750\begin{seealso}
751\seepep{327}{Decimal Data Type}{Written by Facundo Batista and implemented
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000752 by Facundo Batista, Eric Price, Raymond Hettinger, Aahz, and Tim Peters.}
753
Raymond Hettingerca1a7752004-07-12 13:00:45 +0000754\seeurl{http://research.microsoft.com/\textasciitilde hollasch/cgindex/coding/ieeefloat.html}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000755{A more detailed overview of the IEEE-754 representation.}
756
757\seeurl{http://www.lahey.com/float.htm}
758{The article uses Fortran code to illustrate many of the problems
759that floating-point inaccuracy can cause.}
760
761\seeurl{http://www2.hursley.ibm.com/decimal/}
762{A description of a decimal-based representation. This representation
763is being proposed as a standard, and underlies the new Python decimal
764type. Much of this material was written by Mike Cowlishaw, designer of the
Raymond Hettingerca1a7752004-07-12 13:00:45 +0000765Rexx language.}
Andrew M. Kuchlingc8f8a812004-07-04 01:26:42 +0000766
Raymond Hettinger0fff62f2004-07-01 11:52:15 +0000767\end{seealso}
768
769
770%======================================================================
Andrew M. Kuchling3294e9d2004-08-31 11:26:23 +0000771\section{PEP 328: Multi-line Imports}
772
773One language change is a small syntactic tweak aimed at making it
774easier to import many names from a module. In a
775\code{from \var{module} import \var{names}} statement,
776\var{names} is a sequence of names separated by commas. If the sequence is
777very long, you can either write multiple imports from the same module,
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000778or you can use backslashes to escape the line endings like this:
Andrew M. Kuchling3294e9d2004-08-31 11:26:23 +0000779
780\begin{verbatim}
781from SimpleXMLRPCServer import SimpleXMLRPCServer,\
782 SimpleXMLRPCRequestHandler,\
783 CGIXMLRPCRequestHandler,\
784 resolve_dotted_attribute
785\end{verbatim}
786
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000787The syntactic change in Python 2.4 simply allows putting the names
788within parentheses. Python ignores newlines within a parenthesized
Andrew M. Kuchling3294e9d2004-08-31 11:26:23 +0000789expression, so the backslashes are no longer needed:
790
791\begin{verbatim}
792from SimpleXMLRPCServer import (SimpleXMLRPCServer,
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000793 SimpleXMLRPCRequestHandler,
794 CGIXMLRPCRequestHandler,
795 resolve_dotted_attribute)
Andrew M. Kuchling3294e9d2004-08-31 11:26:23 +0000796\end{verbatim}
797
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000798The PEP also proposes that all \keyword{import} statements be absolute
799imports, with a leading \samp{.} character to indicate a relative
800import. This part of the PEP is not yet implemented, and will have to
801wait for Python 2.5 or some other future version.
Andrew M. Kuchling3294e9d2004-08-31 11:26:23 +0000802
803\begin{seealso}
Fred Drake410eb842004-09-01 04:05:08 +0000804\seepep{328}{Imports: Multi-Line and Absolute/Relative}
805 {Written by Aahz. Multi-line imports were implemented by
806 Dima Dorfman.}
807\end{seealso}
Andrew M. Kuchling3294e9d2004-08-31 11:26:23 +0000808
809
810%======================================================================
Andrew M. Kuchling65a33322004-07-21 12:41:38 +0000811\section{PEP 331: Locale-Independent Float/String Conversions}
812
813The \module{locale} modules lets Python software select various
814conversions and display conventions that are localized to a particular
815country or language. However, the module was careful to not change
816the numeric locale because various functions in Python's
817implementation required that the numeric locale remain set to the
818\code{'C'} locale. Often this was because the code was using the C library's
819\cfunction{atof()} function.
820
821Not setting the numeric locale caused trouble for extensions that used
822third-party C libraries, however, because they wouldn't have the
823correct locale set. The motivating example was GTK+, whose user
824interface widgets weren't displaying numbers in the current locale.
825
826The solution described in the PEP is to add three new functions to the
827Python API that perform ASCII-only conversions, ignoring the locale
828setting:
829
830\begin{itemize}
831 \item \cfunction{PyOS_ascii_strtod(\var{str}, \var{ptr})}
832and \cfunction{PyOS_ascii_atof(\var{str}, \var{ptr})}
833both convert a string to a C \ctype{double}.
834 \item \cfunction{PyOS_ascii_formatd(\var{buffer}, \var{buf_len}, \var{format}, \var{d})} converts a \ctype{double} to an ASCII string.
835\end{itemize}
836
837The code for these functions came from the GLib library
838(\url{http://developer.gnome.org/arch/gtk/glib.html}), whose
839developers kindly relicensed the relevant functions and donated them
840to the Python Software Foundation. The \module{locale} module
841can now change the numeric locale, letting extensions such as GTK+
842produce the correct results.
843
844\begin{seealso}
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000845\seepep{331}{Locale-Independent Float/String Conversions}
846{Written by Christian R. Reis, and implemented by Gustavo Carneiro.}
Andrew M. Kuchling65a33322004-07-21 12:41:38 +0000847\end{seealso}
848
849%======================================================================
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000850\section{Other Language Changes}
851
852Here are all of the changes that Python 2.4 makes to the core Python
853language.
854
855\begin{itemize}
Raymond Hettingerd4462302003-11-26 17:52:45 +0000856
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000857\item Decorators for functions and methods were added (\pep{318}).
858
859\item Built-in \function{set} and \function{frozenset} types were
860added (\pep{218}). Other new built-ins include the \function{reversed(\var{seq})} function (\pep{322}).
861
862\item Generator expressions were added (\pep{289}).
863
864\item Certain numeric expressions no longer return values restricted to 32 or 64 bits (\pep{237}).
865
866\item You can now put parentheses around the list of names in a
867\code{from \var{module} import \var{names}} statement (\pep{328}).
868
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000869\item The \method{dict.update()} method now accepts the same
870argument forms as the \class{dict} constructor. This includes any
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +0000871mapping, any iterable of key/value pairs, and keyword arguments.
872(Contributed by Raymond Hettinger.)
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000873
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +0000874\item The string methods \method{ljust()}, \method{rjust()}, and
Andrew M. Kuchling67087562003-11-26 18:03:48 +0000875\method{center()} now take an optional argument for specifying a
Raymond Hettingerd4462302003-11-26 17:52:45 +0000876fill character other than a space.
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +0000877(Contributed by Raymond Hettinger.)
Raymond Hettingerd4462302003-11-26 17:52:45 +0000878
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000879\item Strings also gained an \method{rsplit()} method that
Raymond Hettingered54d912003-12-31 01:59:18 +0000880works like the \method{split()} method but splits from the end of
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +0000881the string.
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +0000882
883\begin{verbatim}
Raymond Hettinger7a6d2972004-02-13 19:00:07 +0000884>>> 'www.python.org'.split('.', 1)
885['www', 'python.org']
886'www.python.org'.rsplit('.', 1)
887['www.python', 'org']
888\end{verbatim}
Raymond Hettinger97ef8de2004-01-05 00:29:57 +0000889
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000890\item Three keyword parameters, \var{cmp}, \var{key}, and
891\var{reverse}, were added to the \method{sort()} method of lists.
892These parameters make some common usages of \method{sort()} simpler.
893All of these parameters are optional.
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000894
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000895For the \var{cmp} parameter, the value should be a comparison function
896that takes two parameters and returns -1, 0, or +1 depending on how
897the parameters compare. This function will then be used to sort the
898list. Previously this was the only parameter that could be provided
899to \method{sort()}.
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000900
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000901\var{key} should be a single-parameter function that takes a list
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000902element and returns a comparison key for the element. The list is
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000903then sorted using the comparison keys. The following example sorts a
904list case-insensitively:
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000905
906\begin{verbatim}
907>>> L = ['A', 'b', 'c', 'D']
908>>> L.sort() # Case-sensitive sort
909>>> L
910['A', 'D', 'b', 'c']
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000911>>> # Using 'key' parameter to sort list
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000912>>> L.sort(key=lambda x: x.lower())
913>>> L
914['A', 'b', 'c', 'D']
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000915>>> # Old-fashioned way
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000916>>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
917>>> L
918['A', 'b', 'c', 'D']
919\end{verbatim}
920
921The last example, which uses the \var{cmp} parameter, is the old way
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000922to perform a case-insensitive sort. It works but is slower than using
923a \var{key} parameter. Using \var{key} calls \method{lower()} method
924once for each element in the list while using \var{cmp} will call it
925twice for each comparison, so using \var{key} saves on invocations of
926the \method{lower()} method.
Andrew M. Kuchling2fb4d512003-10-21 12:31:16 +0000927
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000928For simple key functions and comparison functions, it is often
929possible to avoid a \keyword{lambda} expression by using an unbound
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000930method instead. For example, the above case-insensitive sort is best
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000931written as:
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000932
933\begin{verbatim}
934>>> L.sort(key=str.lower)
935>>> L
936['A', 'b', 'c', 'D']
937\end{verbatim}
938
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000939Finally, the \var{reverse} parameter takes a Boolean value. If the
940value is true, the list will be sorted into reverse order.
941Instead of \code{L.sort() ; L.reverse()}, you can now write
942\code{L.sort(reverse=True)}.
Fred Drakeed0fa3d2003-07-30 19:14:09 +0000943
Andrew M. Kuchling981a9182003-11-13 21:33:26 +0000944The results of sorting are now guaranteed to be stable. This means
945that two entries with equal keys will be returned in the same order as
946they were input. For example, you can sort a list of people by name,
947and then sort the list by age, resulting in a list sorted by age where
948people with the same age are in name-sorted order.
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000949
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000950(All changes to \method{sort()} contributed by Raymond Hettinger.)
951
Fred Drake56fcc232004-05-06 02:55:35 +0000952\item There is a new built-in function
953\function{sorted(\var{iterable})} that works like the in-place
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +0000954\method{list.sort()} method but can be used in
Fred Drake56fcc232004-05-06 02:55:35 +0000955expressions. The differences are:
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000956 \begin{itemize}
Raymond Hettinger7d1dd042003-11-12 16:42:10 +0000957 \item the input may be any iterable;
958 \item a newly formed copy is sorted, leaving the original intact; and
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000959 \item the expression returns the new sorted copy
960 \end{itemize}
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000961
962\begin{verbatim}
963>>> L = [9,7,8,3,2,4,1,6,5]
Raymond Hettinger64958a12003-12-17 20:43:33 +0000964>>> [10+i for i in sorted(L)] # usable in a list comprehension
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000965[11, 12, 13, 14, 15, 16, 17, 18, 19]
Hye-Shik Chang2b052482004-07-17 13:53:48 +0000966>>> L # original is left unchanged
Andrew M. Kuchlinge3e1eca2004-07-26 18:52:48 +0000967[9,7,8,3,2,4,1,6,5]
968>>> sorted('Monty Python') # any iterable may be an input
969[' ', 'M', 'P', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y', 'y']
Raymond Hettingerd4462302003-11-26 17:52:45 +0000970
971>>> # List the contents of a dict sorted by key values
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000972>>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5)
Raymond Hettinger64958a12003-12-17 20:43:33 +0000973>>> for k, v in sorted(colormap.iteritems()):
Raymond Hettinger607c00f2003-11-12 16:27:50 +0000974... print k, v
975...
976black 4
977blue 2
978green 3
979red 1
980yellow 5
Andrew M. Kuchling1a420252003-11-08 15:58:49 +0000981\end{verbatim}
982
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +0000983(Contributed by Raymond Hettinger.)
984
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +0000985\item Integer operations will no longer trigger an \exception{OverflowWarning}.
986The \exception{OverflowWarning} warning will disappear in Python 2.5.
987
Andrew M. Kuchling5e3f9232004-10-07 12:00:33 +0000988\item The interpreter gained a new switch, \programopt{-m}, that
989takes a name, searches for the corresponding module on \code{sys.path},
990and runs the module as a script. For example,
991you can now run the Python profiler with \code{python -m profile}.
992(Contributed by Nick Coghlan.)
993
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000994\item The \function{eval(\var{expr}, \var{globals}, \var{locals})}
Andrew M. Kuchling1455f792004-08-02 12:09:58 +0000995and \function{execfile(\var{filename}, \var{globals}, \var{locals})}
996functions and the \keyword{exec} statement now accept any mapping type
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +0000997for the \var{locals} parameter. Previously this had to be a regular
Andrew M. Kuchling1455f792004-08-02 12:09:58 +0000998Python dictionary. (Contributed by Raymond Hettinger.)
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +0000999
Raymond Hettinger607c00f2003-11-12 16:27:50 +00001000\item The \function{zip()} built-in function and \function{itertools.izip()}
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +00001001 now return an empty list if called with no arguments.
1002 Previously they raised a \exception{TypeError}
1003 exception. This makes them more
Raymond Hettinger607c00f2003-11-12 16:27:50 +00001004 suitable for use with variable length argument lists:
1005
1006\begin{verbatim}
1007>>> def transpose(array):
1008... return zip(*array)
1009...
1010>>> transpose([(1,2,3), (4,5,6)])
1011[(1, 4), (2, 5), (3, 6)]
1012>>> transpose([])
1013[]
1014\end{verbatim}
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001015(Contributed by Raymond Hettinger.)
1016
Andrew M. Kuchlingd91fcbe2004-08-02 12:44:28 +00001017\item Encountering a failure while importing a module no longer leaves
1018a partially-initialized module object in \code{sys.modules}. The
1019incomplete module object left behind would fool further imports of the
1020same module into succeeding, leading to confusing errors.
Andrew M. Kuchling067947e2004-11-19 14:43:36 +00001021(Fixed by Tim Peters.)
Andrew M. Kuchlingd91fcbe2004-08-02 12:44:28 +00001022
Andrew M. Kuchling65a33322004-07-21 12:41:38 +00001023\item \constant{None} is now a constant; code that binds a new value to
1024the name \samp{None} is now a syntax error.
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001025(Contributed by Raymond Hettinger.)
Andrew M. Kuchling65a33322004-07-21 12:41:38 +00001026
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001027\end{itemize}
1028
1029
1030%======================================================================
1031\subsection{Optimizations}
1032
1033\begin{itemize}
1034
Raymond Hettingerca1a7752004-07-12 13:00:45 +00001035\item The inner loops for list and tuple slicing
Andrew M. Kuchling65a33322004-07-21 12:41:38 +00001036 were optimized and now run about one-third faster. The inner loops
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001037 for dictionaries were also optimized , resulting in performance boosts for
Andrew M. Kuchling65a33322004-07-21 12:41:38 +00001038 \method{keys()}, \method{values()}, \method{items()},
1039 \method{iterkeys()}, \method{itervalues()}, and \method{iteritems()}.
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001040 (Contributed by Raymond Hettinger.)
Raymond Hettingerb7d05db2004-03-08 07:25:05 +00001041
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +00001042\item The machinery for growing and shrinking lists was optimized for
1043 speed and for space efficiency. Appending and popping from lists now
1044 runs faster due to more efficient code paths and less frequent use of
1045 the underlying system \cfunction{realloc()}. List comprehensions
1046 also benefit. \method{list.extend()} was also optimized and no
1047 longer converts its argument into a temporary list before extending
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001048 the base list. (Contributed by Raymond Hettinger.)
Raymond Hettinger7a6d2972004-02-13 19:00:07 +00001049
Raymond Hettinger97ef8de2004-01-05 00:29:57 +00001050\item \function{list()}, \function{tuple()}, \function{map()},
1051 \function{filter()}, and \function{zip()} now run several times
1052 faster with non-sequence arguments that supply a \method{__len__()}
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001053 method. (Contributed by Raymond Hettinger.)
Raymond Hettinger97ef8de2004-01-05 00:29:57 +00001054
Raymond Hettinger23a0f4e2004-01-05 08:15:20 +00001055\item The methods \method{list.__getitem__()},
Raymond Hettinger97ef8de2004-01-05 00:29:57 +00001056 \method{dict.__getitem__()}, and \method{dict.__contains__()} are
1057 are now implemented as \class{method_descriptor} objects rather
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001058 than \class{wrapper_descriptor} objects. This form of
Raymond Hettinger97ef8de2004-01-05 00:29:57 +00001059 access doubles their performance and makes them more suitable for
Raymond Hettinger23a0f4e2004-01-05 08:15:20 +00001060 use as arguments to functionals:
1061 \samp{map(mydict.__getitem__, keylist)}.
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001062 (Contributed by Raymond Hettinger.)
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001063
Fred Draked6d35d92004-06-03 13:31:22 +00001064\item Added a new opcode, \code{LIST_APPEND}, that simplifies
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001065 the generated bytecode for list comprehensions and speeds them up
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001066 by about a third. (Contributed by Raymond Hettinger.)
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001067
Andrew M. Kuchling0c789562004-09-23 20:15:41 +00001068\item The peephole bytecode optimizer has been improved to
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001069produce shorter, faster bytecode; remarkably, the resulting bytecode is
Andrew M. Kuchling0c789562004-09-23 20:15:41 +00001070more readable. (Enhanced by Raymond Hettinger.)
1071
Andrew M. Kuchlingac642872004-08-07 13:13:31 +00001072\item String concatenations in statements of the form \code{s = s +
1073"abc"} and \code{s += "abc"} are now performed more efficiently in
1074certain circumstances. This optimization won't be present in other
1075Python implementations such as Jython, so you shouldn't rely on it;
1076using the \method{join()} method of strings is still recommended when
1077you want to efficiently glue a large number of strings together.
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001078(Contributed by Armin Rigo.)
Andrew M. Kuchlingac642872004-08-07 13:13:31 +00001079
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001080\end{itemize}
1081
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001082% XXX fill in these figures
Raymond Hettingerb2d5a8e2004-11-18 05:51:53 +00001083% pystone is almost useless for comparing different versions of Python;
1084% instead, it excels at predicting relative Python performance on
1085% different machines.
1086% So, this section would be more informative if it used other tools
1087% such as pybench and parrotbench. For a more application oriented
1088% benchmark, try comparing the timings of test_decimal.py under 2.3
1089% and 2.4.
1090
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001091The net result of the 2.4 optimizations is that Python 2.4 runs the
1092pystone benchmark around XX\% faster than Python 2.3 and YY\% faster
1093than Python 2.2.
1094
1095
1096%======================================================================
1097\section{New, Improved, and Deprecated Modules}
1098
1099As usual, Python's standard library received a number of enhancements and
1100bug fixes. Here's a partial list of the most notable changes, sorted
1101alphabetically by module name. Consult the
1102\file{Misc/NEWS} file in the source tree for a more
1103complete list of changes, or look through the CVS logs for all the
1104details.
1105
1106\begin{itemize}
1107
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001108\item The \module{asyncore} module's \function{loop()} function now
1109 has a \var{count} parameter that lets you perform a limited number
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +00001110 of passes through the polling loop. The default is still to loop
1111 forever.
1112
Andrew M. Kuchlinga331e862004-09-10 13:05:22 +00001113\item The \module{base64} module now has more complete RFC 3548 support
1114 for Base64, Base32, and Base16 encoding and decoding, including
1115 optional case folding and optional alternative alphabets.
1116 (Contributed by Barry Warsaw.)
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +00001117
Raymond Hettinger0c410272004-01-05 10:13:35 +00001118\item The \module{bisect} module now has an underlying C implementation
1119 for improved performance.
1120 (Contributed by Dmitry Vasiliev.)
1121
Andrew M. Kuchling5303a962004-01-18 15:55:51 +00001122\item The CJKCodecs collections of East Asian codecs, maintained
1123by Hye-Shik Chang, was integrated into 2.4.
1124The new encodings are:
1125
1126\begin{itemize}
Andrew M. Kuchling671c5062004-07-28 15:29:39 +00001127 \item Chinese (PRC): gb2312, gbk, gb18030, big5hkscs, hz
Andrew M. Kuchling5303a962004-01-18 15:55:51 +00001128 \item Chinese (ROC): big5, cp950
Andrew M. Kuchling671c5062004-07-28 15:29:39 +00001129 \item Japanese: cp932, euc-jis-2004, euc-jp,
Andrew M. Kuchling5303a962004-01-18 15:55:51 +00001130euc-jisx0213, iso-2022-jp, iso-2022-jp-1, iso-2022-jp-2,
Andrew M. Kuchling671c5062004-07-28 15:29:39 +00001131 iso-2022-jp-3, iso-2022-jp-ext, iso-2022-jp-2004,
1132 shift-jis, shift-jisx0213, shift-jis-2004
Andrew M. Kuchling5303a962004-01-18 15:55:51 +00001133 \item Korean: cp949, euc-kr, johab, iso-2022-kr
1134\end{itemize}
1135
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001136\item Some other new encodings were added: HP Roman8,
1137ISO_8859-11, ISO_8859-16, PCTP-154, and TIS-620.
1138
Andrew M. Kuchling579b3e22004-10-05 20:23:34 +00001139\item The UTF-8 and UTF-16 codecs now cope better with receiving partial input.
1140Previously the \class{StreamReader} class would try to read more data,
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001141making it impossible to resume decoding from the stream. The
Andrew M. Kuchling579b3e22004-10-05 20:23:34 +00001142\method{read()} method will now return as much data as it can and future
1143calls will resume decoding where previous ones left off.
1144(Implemented by Walter D\"orwald.)
1145
Andrew M. Kuchlingfd0e4942004-02-09 13:23:34 +00001146\item There is a new \module{collections} module for
1147 various specialized collection datatypes.
1148 Currently it contains just one type, \class{deque},
1149 a double-ended queue that supports efficiently adding and removing
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001150 elements from either end:
Raymond Hettinger756b3f32004-01-29 06:37:52 +00001151
1152\begin{verbatim}
1153>>> from collections import deque
1154>>> d = deque('ghi') # make a new deque with three items
1155>>> d.append('j') # add a new entry to the right side
1156>>> d.appendleft('f') # add a new entry to the left side
1157>>> d # show the representation of the deque
1158deque(['f', 'g', 'h', 'i', 'j'])
1159>>> d.pop() # return and remove the rightmost item
1160'j'
1161>>> d.popleft() # return and remove the leftmost item
1162'f'
1163>>> list(d) # list the contents of the deque
1164['g', 'h', 'i']
1165>>> 'h' in d # search the deque
1166True
1167\end{verbatim}
1168
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001169Several modules, such as the \module{Queue} and \module{threading}
1170modules, now take advantage of \class{collections.deque} for improved
1171performance. (Contributed by Raymond Hettinger.)
Andrew M. Kuchling5303a962004-01-18 15:55:51 +00001172
Fred Drake9f15b5c2004-05-18 04:30:00 +00001173\item The \module{ConfigParser} classes have been enhanced slightly.
1174 The \method{read()} method now returns a list of the files that
1175 were successfully parsed, and the \method{set()} method raises
1176 \exception{TypeError} if passed a \var{value} argument that isn't a
Andrew M. Kuchling067947e2004-11-19 14:43:36 +00001177 string. (Contributed by John Belmonte and David Goodger.)
Fred Drake9f15b5c2004-05-18 04:30:00 +00001178
Andrew M. Kuchlinga331e862004-09-10 13:05:22 +00001179\item The \module{curses} module now supports the ncurses extension
1180 \function{use_default_colors()}. On platforms where the terminal
1181 supports transparency, this makes it possible to use a transparent
1182 background. (Contributed by J\"org Lehmann.)
1183
1184\item The \module{difflib} module now includes an \class{HtmlDiff} class
1185that creates an HTML table showing a side by side comparison
1186of two versions of a text. (Contributed by Dan Gass.)
1187
Andrew M. Kuchling579b3e22004-10-05 20:23:34 +00001188\item The \module{email} package was updated to version 3.0,
1189which dropped various deprecated APIs and removes support for Python
1190versions earlier than 2.3. The 3.0 version of the package uses a new
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001191incremental parser for MIME messages, available in the
Andrew M. Kuchling579b3e22004-10-05 20:23:34 +00001192\module{email.FeedParser} module. The new parser doesn't require
1193reading the entire message into memory, and doesn't throw exceptions
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001194if a message is malformed; instead it records any problems in the
Andrew M. Kuchling579b3e22004-10-05 20:23:34 +00001195\member{defect} attribute of the message. (Developed by Anthony
1196Baxter, Barry Warsaw, Thomas Wouters, and others.)
Andrew M. Kuchlinga331e862004-09-10 13:05:22 +00001197
Raymond Hettinger607c00f2003-11-12 16:27:50 +00001198\item The \module{heapq} module has been converted to C. The resulting
Andrew M. Kuchlingfd0e4942004-02-09 13:23:34 +00001199 tenfold improvement in speed makes the module suitable for handling
Raymond Hettinger33ecffb2004-06-10 05:03:17 +00001200 high volumes of data. In addition, the module has two new functions
1201 \function{nlargest()} and \function{nsmallest()} that use heaps to
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +00001202 find the N largest or smallest values in a dataset without the
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001203 expense of a full sort. (Contributed by Raymond Hettinger.)
Andrew M. Kuchling1a420252003-11-08 15:58:49 +00001204
Andrew M. Kuchling0c789562004-09-23 20:15:41 +00001205\item The \module{httplib} module now contains constants for HTTP
1206status codes defined in various HTTP-related RFC documents. Constants
1207have names such as \constant{OK}, \constant{CREATED},
1208\constant{CONTINUE}, and \constant{MOVED_PERMANENTLY}; use pydoc to
1209get a full list. (Contributed by Andrew Eland.)
1210
Andrew M. Kuchlingce4bae62004-07-27 12:13:25 +00001211\item The \module{imaplib} module now supports IMAP's THREAD command
1212(contributed by Yves Dionne) and new \method{deleteacl()} and
1213\method{myrights()} methods (contributed by Arnaud Mazin).
Andrew M. Kuchlingdff9dbd2003-11-20 22:22:19 +00001214
Andrew M. Kuchlingad809552003-12-06 23:19:23 +00001215\item The \module{itertools} module gained a
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +00001216 \function{groupby(\var{iterable}\optional{, \var{func}})} function.
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001217 \var{iterable} is something that can be iterated over to return a
1218 stream of elements, and the optional \var{func} parameter is a
1219 function that takes an element and returns a key value; if omitted,
1220 the key is simply the element itself. \function{groupby()} then
1221 groups the elements into subsequences which have matching values of
1222 the key, and returns a series of 2-tuples containing the key value
1223 and an iterator over the subsequence.
Andrew M. Kuchlingad809552003-12-06 23:19:23 +00001224
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001225Here's an example to make this clearer. The \var{key} function simply
1226returns whether a number is even or odd, so the result of
1227\function{groupby()} is to return consecutive runs of odd or even
1228numbers.
Andrew M. Kuchlingad809552003-12-06 23:19:23 +00001229
1230\begin{verbatim}
1231>>> import itertools
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001232>>> L = [2, 4, 6, 7, 8, 9, 11, 12, 14]
Andrew M. Kuchlingad809552003-12-06 23:19:23 +00001233>>> for key_val, it in itertools.groupby(L, lambda x: x % 2):
1234... print key_val, list(it)
1235...
12360 [2, 4, 6]
12371 [7]
12380 [8]
12391 [9, 11]
12400 [12, 14]
1241>>>
1242\end{verbatim}
1243
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +00001244\function{groupby()} is typically used with sorted input. The logic
1245for \function{groupby()} is similar to the \UNIX{} \code{uniq} filter
1246which makes it handy for eliminating, counting, or identifying
1247duplicate elements:
Raymond Hettingerfeb78c92003-12-12 13:13:47 +00001248
1249\begin{verbatim}
1250>>> word = 'abracadabra'
Raymond Hettingered54d912003-12-31 01:59:18 +00001251>>> letters = sorted(word) # Turn string into a sorted list of letters
Raymond Hettinger64958a12003-12-17 20:43:33 +00001252>>> letters
Andrew M. Kuchling4612bc52003-12-16 20:59:37 +00001253['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r']
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +00001254>>> for k, g in itertools.groupby(letters):
1255... print k, list(g)
1256...
1257a ['a', 'a', 'a', 'a', 'a']
1258b ['b', 'b']
1259c ['c']
1260d ['d']
1261r ['r', 'r']
1262>>> # List unique letters
1263>>> [k for k, g in groupby(letters)]
Raymond Hettingerfeb78c92003-12-12 13:13:47 +00001264['a', 'b', 'c', 'd', 'r']
Johannes Gijsbersd3452252004-09-11 16:50:06 +00001265>>> # Count letter occurrences
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +00001266>>> [(k, len(list(g))) for k, g in groupby(letters)]
Raymond Hettingerfeb78c92003-12-12 13:13:47 +00001267[('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]
Raymond Hettingerfeb78c92003-12-12 13:13:47 +00001268\end{verbatim}
1269
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001270(Contributed by Hye-Shik Chang.)
1271
Raymond Hettingered54d912003-12-31 01:59:18 +00001272\item \module{itertools} also gained a function named
1273\function{tee(\var{iterator}, \var{N})} that returns \var{N} independent
1274iterators that replicate \var{iterator}. If \var{N} is omitted, the
1275default is 2.
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +00001276
1277\begin{verbatim}
1278>>> L = [1,2,3]
1279>>> i1, i2 = itertools.tee(L)
1280>>> i1,i2
1281(<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>)
Raymond Hettingered54d912003-12-31 01:59:18 +00001282>>> list(i1) # Run the first iterator to exhaustion
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +00001283[1, 2, 3]
Raymond Hettingered54d912003-12-31 01:59:18 +00001284>>> list(i2) # Run the second iterator to exhaustion
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +00001285[1, 2, 3]
1286>\end{verbatim}
1287
1288Note that \function{tee()} has to keep copies of the values returned
Raymond Hettingered54d912003-12-31 01:59:18 +00001289by the iterator; in the worst case, it may need to keep all of them.
Andrew M. Kuchling44a31e12004-01-01 18:33:34 +00001290This should therefore be used carefully if the leading iterator
Raymond Hettingered54d912003-12-31 01:59:18 +00001291can run far ahead of the trailing iterator in a long stream of inputs.
Andrew M. Kuchling3bf85f12004-07-05 01:37:07 +00001292If the separation is large, then you might as well use
Raymond Hettingered54d912003-12-31 01:59:18 +00001293\function{list()} instead. When the iterators track closely with one
1294another, \function{tee()} is ideal. Possible applications include
1295bookmarking, windowing, or lookahead iterators.
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001296(Contributed by Raymond Hettinger.)
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +00001297
Andrew M. Kuchling5785a132004-07-26 19:28:46 +00001298\item A number of functions were added to the \module{locale}
1299module, such as \function{bind_textdomain_codeset()} to specify a
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001300particular encoding and a family of \function{l*gettext()} functions
Andrew M. Kuchling5785a132004-07-26 19:28:46 +00001301that return messages in the chosen encoding.
1302(Contributed by Gustavo Niemeyer.)
1303
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001304\item Some keyword arguments were added to the \module{logging}
1305package's \function{basicConfig} function to simplify log
1306configuration. The default behavior is to log messages to standard
1307error, but various keyword arguments can be specified to log to a
1308particular file, change the logging format, or set the logging level.
1309For example:
Andrew M. Kuchlingbcefe692004-07-07 13:01:53 +00001310
1311\begin{verbatim}
1312import logging
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001313logging.basicConfig(filename='/var/log/application.log',
1314 level=0, # Log all messages
Andrew M. Kuchlingbcefe692004-07-07 13:01:53 +00001315 format='%(levelname):%(process):%(thread):%(message)')
1316\end{verbatim}
1317
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001318Other additions to the \module{logging} package include a
1319\method{log(\var{level}, \var{msg})} convenience method, as well as a
1320\class{TimedRotatingFileHandler} class that rotates its log files at a
1321timed interval. The module already had \class{RotatingFileHandler},
Andrew M. Kuchlingbcefe692004-07-07 13:01:53 +00001322which rotated logs once the file exceeded a certain size. Both
1323classes derive from a new \class{BaseRotatingHandler} class that can
1324be used to implement other rotating handlers.
1325
Andrew M. Kuchling579b3e22004-10-05 20:23:34 +00001326(Changes implemented by Vinay Sajip.)
1327
Andrew M. Kuchling0c789562004-09-23 20:15:41 +00001328\item The \module{marshal} module now shares interned strings on unpacking a
1329data structure. This may shrink the size of certain pickle strings,
1330but the primary effect is to make \file{.pyc} files significantly smaller.
1331(Contributed by Martin von Loewis.)
1332
Andrew M. Kuchling5785a132004-07-26 19:28:46 +00001333\item The \module{nntplib} module's \class{NNTP} class gained
1334\method{description()} and \method{descriptions()} methods to retrieve
1335newsgroup descriptions for a single group or for a range of groups.
1336(Contributed by J\"urgen A. Erhard.)
1337
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001338\item Two new functions were added to the \module{operator} module,
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +00001339\function{attrgetter(\var{attr})} and \function{itemgetter(\var{index})}.
1340Both functions return callables that take a single argument and return
Raymond Hettingered54d912003-12-31 01:59:18 +00001341the corresponding attribute or item; these callables make excellent
Andrew M. Kuchlingbcefe692004-07-07 13:01:53 +00001342data extractors when used with \function{map()} or
1343\function{sorted()}. For example:
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +00001344
1345\begin{verbatim}
Raymond Hettingered54d912003-12-31 01:59:18 +00001346>>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +00001347>>> map(operator.itemgetter(0), L)
1348['c', 'd', 'a', 'b']
1349>>> map(operator.itemgetter(1), L)
Raymond Hettingered54d912003-12-31 01:59:18 +00001350[2, 1, 4, 3]
1351>>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item
1352[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
Andrew M. Kuchling35f2b052003-12-18 13:28:13 +00001353\end{verbatim}
1354
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001355(Contributed by Raymond Hettinger.)
1356
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001357\item The \module{optparse} module was updated in various ways. The
1358module now passes its messages through \function{gettext.gettext()},
1359making it possible to internationalize Optik's help and error
1360messages. Help messages for options can now include the string
1361\code{'\%default'}, which will be replaced by the option's default
1362value. (Contributed by Greg Ward.)
Andrew M. Kuchlinge30c4d42004-08-07 13:58:02 +00001363
Andrew M. Kuchlingf3958f12004-10-11 19:20:06 +00001364\item The long-term plan is to deprecate the \module{rfc822} module
1365in some future Python release in favor of the \module{email} package.
1366To this end, the \function{email.Utils.formatdate()} function has been
1367changed to make it usable as a replacement for
1368\function{rfc822.formatdate()}. You may want to write new e-mail
1369processing code with this in mind. (Change implemented by Anthony
1370Baxter.)
1371
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001372\item A new \function{urandom(\var{n})} function was added to the
1373\module{os} module, returning a string containing \var{n} bytes of
1374random data. This function provides access to platform-specific
1375sources of randomness such as \file{/dev/urandom} on Linux or the
1376Windows CryptoAPI. (Contributed by Trevor Perrin.)
Andrew M. Kuchlingcb7b3f32004-08-30 11:58:04 +00001377
1378\item Another new function: \function{os.path.lexists(\var{path})}
1379returns true if the file specified by \var{path} exists, whether or
1380not it's a symbolic link. This differs from the existing
1381\function{os.path.exists(\var{path})} function, which returns false if
1382\var{path} is a symlink that points to a destination that doesn't exist.
1383(Contributed by Beni Cherniavsky.)
1384
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +00001385\item A new \function{getsid()} function was added to the
1386\module{posix} module that underlies the \module{os} module.
1387(Contributed by J. Raynor.)
1388
Andrew M. Kuchling067947e2004-11-19 14:43:36 +00001389\item The \module{poplib} module now supports POP over SSL. (Contributed by
1390Hector Urtubia.)
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +00001391
1392\item The \module{profile} module can now profile C extension functions.
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001393(Contributed by Nick Bastin.)
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +00001394
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001395\item The \module{random} module has a new method called
1396 \method{getrandbits(\var{N})} that returns a long integer \var{N}
1397 bits in length. The existing \method{randrange()} method now uses
1398 \method{getrandbits()} where appropriate, making generation of
1399 arbitrarily large random numbers more efficient. (Contributed by
1400 Raymond Hettinger.)
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +00001401
1402\item The regular expression language accepted by the \module{re} module
1403 was extended with simple conditional expressions, written as
Andrew M. Kuchlingab778222004-08-31 12:07:43 +00001404 \regexp{(?(\var{group})\var{A}|\var{B})}. \var{group} is either a
1405 numeric group ID or a group name defined with \regexp{(?P<group>...)}
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +00001406 earlier in the expression. If the specified group matched, the
1407 regular expression pattern \var{A} will be tested against the string; if
1408 the group didn't match, the pattern \var{B} will be used instead.
Andrew M. Kuchling067947e2004-11-19 14:43:36 +00001409 (Contributed by Gustavo Niemeyer.)
Raymond Hettinger874ebd52004-05-31 03:15:02 +00001410
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001411\item The \module{re} module is also no longer recursive, thanks to a
1412massive amount of work by Gustavo Niemeyer. In a recursive regular
1413expression engine, certain patterns result in a large amount of C
1414stack space being consumed, and it was possible to overflow the stack.
1415For example, if you matched a 30000-byte string of \samp{a} characters
1416against the expression \regexp{(a|b)+}, one stack frame was consumed
1417per character. Python 2.3 tried to check for stack overflow and raise
1418a \exception{RuntimeError} exception, but certain patterns could
1419sidestep the checking and if you were unlucky Python could segfault.
1420Python 2.4's regular expression engine can match this pattern without
1421problems.
Andrew M. Kuchlingab778222004-08-31 12:07:43 +00001422
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001423\item A new \function{socketpair()} function, returning a pair of
1424connected sockets, was added to the \module{socket} module.
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +00001425(Contributed by Dave Cole.)
Andrew M. Kuchling7f203b82004-08-09 14:48:28 +00001426
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +00001427\item The \function{sys.exitfunc()} function has been deprecated. Code
1428should be using the existing \module{atexit} module, which correctly
1429handles calling multiple exit functions. Eventually
1430\function{sys.exitfunc()} will become a purely internal interface,
1431accessed only by \module{atexit}.
1432
1433\item The \module{tarfile} module now generates GNU-format tar files
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001434by default. (Contributed by Lars Gustaebel.)
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +00001435
Andrew M. Kuchling00457172004-07-15 11:52:40 +00001436\item The \module{threading} module now has an elegantly simple way to support
1437thread-local data. The module contains a \class{local} class whose
1438attribute values are local to different threads.
1439
1440\begin{verbatim}
1441import threading
1442
1443data = threading.local()
1444data.number = 42
1445data.url = ('www.python.org', 80)
1446\end{verbatim}
1447
1448Other threads can assign and retrieve their own values for the
1449\member{number} and \member{url} attributes. You can subclass
1450\class{local} to initialize attributes or to add methods.
1451(Contributed by Jim Fulton.)
1452
Andrew M. Kuchlinga331e862004-09-10 13:05:22 +00001453\item The \module{timeit} module now automatically disables periodic
1454 garbarge collection during the timing loop. This change makes
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001455 consecutive timings more comparable. (Contributed by Raymond Hettinger.)
Andrew M. Kuchlinga331e862004-09-10 13:05:22 +00001456
Raymond Hettinger874ebd52004-05-31 03:15:02 +00001457\item The \module{weakref} module now supports a wider variety of objects
1458 including Python functions, class instances, sets, frozensets, deques,
1459 arrays, files, sockets, and regular expression pattern objects.
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001460 (Contributed by Raymond Hettinger.)
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +00001461
1462\item The \module{xmlrpclib} module now supports a multi-call extension for
Andrew M. Kuchling00457172004-07-15 11:52:40 +00001463transmitting multiple XML-RPC calls in a single HTTP operation.
Andrew M. Kuchling067947e2004-11-19 14:43:36 +00001464(Contributed by Brian Quinlan.)
Andrew M. Kuchling3d3db962004-08-31 13:57:02 +00001465
1466\item The \module{mpz}, \module{rotor}, and \module{xreadlines} modules have
1467been removed.
Andrew M. Kuchling69f31eb2003-08-13 23:11:04 +00001468
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001469\end{itemize}
1470
1471
1472%======================================================================
Raymond Hettingerca1a7752004-07-12 13:00:45 +00001473% whole new modules get described in subsections here
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001474
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001475%=====================
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001476\subsection{cookielib}
1477
1478The \module{cookielib} library supports client-side handling for HTTP
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001479cookies, mirroring the \module{Cookie} module's server-side cookie
1480support. Cookies are stored in cookie jars; the library transparently
1481stores cookies offered by the web server in the cookie jar, and
1482fetches the cookie from the jar when connecting to the server. As in
1483web browsers, policy objects control whether cookies are accepted or
1484not.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001485
1486In order to store cookies across sessions, two implementations of
1487cookie jars are provided: one that stores cookies in the Netscape
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001488format so applications can use the Mozilla or Lynx cookie files, and
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001489one that stores cookies in the same format as the Perl libwww libary.
1490
1491\module{urllib2} has been changed to interact with \module{cookielib}:
1492\class{HTTPCookieProcessor} manages a cookie jar that is used when
1493accessing URLs.
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001494
Andrew M. Kuchling067947e2004-11-19 14:43:36 +00001495This module was contributed by John J. Lee.
1496
1497
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001498% ==================
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +00001499\subsection{doctest}
1500
1501The \module{doctest} module underwent considerable refactoring thanks
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001502to Edward Loper and Tim Peters. Testing can still be as simple as
1503running \function{doctest.testmod()}, but the refactorings allow
1504customizing the module's operation in various ways
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +00001505
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001506The new \class{DocTestFinder} class extracts the tests from a given
1507object's docstrings:
1508
1509\begin{verbatim}
1510def f (x, y):
1511 """>>> f(2,2)
15124
1513>>> f(3,2)
15146
1515 """
1516 return x*y
1517
1518finder = doctest.DocTestFinder()
1519
1520# Get list of DocTest instances
1521tests = finder.find(f)
1522\end{verbatim}
1523
1524The new \class{DocTestRunner} class then runs individual tests and can
1525produce a summary of the results:
1526
1527\begin{verbatim}
1528runner = doctest.DocTestRunner()
1529for t in tests:
1530 tried, failed = runner.run(t)
1531
1532runner.summarize(verbose=1)
1533\end{verbatim}
1534
1535The above example produces the following output:
1536
1537\begin{verbatim}
15381 items passed all tests:
1539 2 tests in f
15402 tests in 1 items.
15412 passed and 0 failed.
1542Test passed.
1543\end{verbatim}
1544
1545\class{DocTestRunner} uses an instance of the \class{OutputChecker}
1546class to compare the expected output with the actual output. This
1547class takes a number of different flags that customize its behaviour;
1548ambitious users can also write a completely new subclass of
1549\class{OutputChecker}.
1550
1551The default output checker provides a number of handy features.
1552For example, with the \constant{doctest.ELLIPSIS} option flag,
1553an ellipsis (\samp{...}) in the expected output matches any substring,
1554making it easier to accommodate outputs that vary in minor ways:
1555
1556\begin{verbatim}
1557def o (n):
1558 """>>> o(1)
1559<__main__.C instance at 0x...>
1560>>>
1561"""
1562\end{verbatim}
1563
1564Another special string, \samp{<BLANKLINE>}, matches a blank line:
1565
1566\begin{verbatim}
1567def p (n):
1568 """>>> p(1)
1569<BLANKLINE>
1570>>>
1571"""
1572\end{verbatim}
1573
1574Another new capability is producing a diff-style display of the output
1575by specifying the \constant{doctest.REPORT_UDIFF} (unified diffs),
1576\constant{doctest.REPORT_CDIFF} (context diffs), or
1577\constant{doctest.REPORT_NDIFF} (delta-style) option flags. For example:
1578
1579\begin{verbatim}
1580def g (n):
1581 """>>> g(4)
1582here
1583is
1584a
1585lengthy
1586>>>"""
1587 L = 'here is a rather lengthy list of words'.split()
1588 for word in L[:n]:
1589 print word
1590\end{verbatim}
1591
1592Running the above function's tests with
1593\constant{doctest.REPORT_UDIFF} specified, you get the following output:
1594
1595\begin{verbatim}
1596**********************************************************************
1597File ``t.py'', line 15, in g
1598Failed example:
1599 g(4)
1600Differences (unified diff with -expected +actual):
1601 @@ -2,3 +2,3 @@
1602 is
1603 a
1604 -lengthy
1605 +rather
1606**********************************************************************
1607\end{verbatim}
1608
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +00001609
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001610% ======================================================================
1611\section{Build and C API Changes}
1612
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001613Some of the changes to Python's build process and to the C API are:
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001614
1615\begin{itemize}
1616
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +00001617 \item Three new convenience macros were added for common return
1618 values from extension functions: \csimplemacro{Py_RETURN_NONE},
1619 \csimplemacro{Py_RETURN_TRUE}, and \csimplemacro{Py_RETURN_FALSE}.
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001620 (Contributed by Brett Cannon.)
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +00001621
Andrew M. Kuchling5785a132004-07-26 19:28:46 +00001622 \item Another new macro, \csimplemacro{Py_CLEAR(\var{obj})},
1623 decreases the reference count of \var{obj} and sets \var{obj} to the
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001624 null pointer. (Contributed by Jim Fulton.)
Andrew M. Kuchling5785a132004-07-26 19:28:46 +00001625
Fred Drakece3caf22004-02-12 18:13:12 +00001626 \item A new function, \cfunction{PyTuple_Pack(\var{N}, \var{obj1},
1627 \var{obj2}, ..., \var{objN})}, constructs tuples from a variable
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001628 length argument list of Python objects. (Contributed by Raymond Hettinger.)
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001629
Fred Drakece3caf22004-02-12 18:13:12 +00001630 \item A new function, \cfunction{PyDict_Contains(\var{d}, \var{k})},
1631 implements fast dictionary lookups without masking exceptions raised
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001632 during the look-up process. (Contributed by Raymond Hettinger.)
Raymond Hettingerd4462302003-11-26 17:52:45 +00001633
Andrew M. Kuchling0c789562004-09-23 20:15:41 +00001634 \item The \csimplemacro{Py_IS_NAN(\var{X})} macro returns 1 if
1635 its float or double argument \var{X} is a NaN.
1636 (Contributed by Tim Peters.)
1637
Andrew M. Kuchlingf3958f12004-10-11 19:20:06 +00001638 \item C code can avoid unnecessary locking by using the new
1639 \cfunction{PyEval_ThreadsInitialized()} function to tell
1640 if any thread operations have been performed. If this function
1641 returns false, no lock operations are needed.
1642 (Contributed by Nick Coghlan.)
1643
Andrew M. Kuchlinge30c4d42004-08-07 13:58:02 +00001644 \item A new function, \cfunction{PyArg_VaParseTupleAndKeywords()},
1645 is the same as \cfunction{PyArg_ParseTupleAndKeywords()} but takes a
1646 \ctype{va_list} instead of a number of arguments.
1647 (Contributed by Greg Chapman.)
1648
Fred Drakece3caf22004-02-12 18:13:12 +00001649 \item A new method flag, \constant{METH_COEXISTS}, allows a function
Andrew M. Kuchling71432f12004-07-05 01:40:07 +00001650 defined in slots to co-exist with a \ctype{PyCFunction} having the
1651 same name. This can halve the access time for a method such as
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001652 \method{set.__contains__()}. (Contributed by Raymond Hettinger.)
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +00001653
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +00001654 \item Python can now be built with additional profiling for the
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001655 interpreter itself, intended as an aid to people developing the
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +00001656 Python core. Providing \longprogramopt{--enable-profiling} to the
1657 \program{configure} script will let you profile the interpreter with
1658 \program{gprof}, and providing the \longprogramopt{--with-tsc}
1659 switch enables profiling using the Pentium's Time-Stamp-Counter
Andrew M. Kuchling067947e2004-11-19 14:43:36 +00001660 register. Note that the \longprogramopt{--with-tsc} switch is slightly
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001661 misnamed, because the profiling feature also works on the PowerPC
1662 platform, though that processor architecture doesn't call that
Andrew M. Kuchling067947e2004-11-19 14:43:36 +00001663 register ``the TSC register''. (Contributed by Jeremy Hylton.)
1664
Andrew M. Kuchlingd0b6d9d2004-07-04 15:35:00 +00001665 \item The \ctype{tracebackobject} type has been renamed to \ctype{PyTracebackObject}.
Raymond Hettinger97ef8de2004-01-05 00:29:57 +00001666
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001667\end{itemize}
1668
1669
1670%======================================================================
1671\subsection{Port-Specific Changes}
1672
Raymond Hettinger97ef8de2004-01-05 00:29:57 +00001673\begin{itemize}
1674
1675\item The Windows port now builds under MSVC++ 7.1 as well as version 6.
Andrew M. Kuchling7642f7a2004-09-13 15:06:50 +00001676 (Contributed by Martin von Loewis.)
Raymond Hettinger97ef8de2004-01-05 00:29:57 +00001677
1678\end{itemize}
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001679
1680
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001681
1682%======================================================================
1683\section{Porting to Python 2.4}
1684
1685This section lists previously described changes that may require
1686changes to your code:
1687
1688\begin{itemize}
1689
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001690\item Left shifts and hexadecimal/octal constants that are too
1691 large no longer trigger a \exception{FutureWarning} and return
1692 a value limited to 32 or 64 bits; instead they return a long integer.
1693
1694\item Integer operations will no longer trigger an \exception{OverflowWarning}.
1695The \exception{OverflowWarning} warning will disappear in Python 2.5.
1696
Raymond Hettinger607c00f2003-11-12 16:27:50 +00001697\item The \function{zip()} built-in function and \function{itertools.izip()}
1698 now return an empty list instead of raising a \exception{TypeError}
1699 exception if called with no arguments.
Andrew M. Kuchling6aedcfc2003-10-21 12:48:23 +00001700
1701\item \function{dircache.listdir()} now passes exceptions to the caller
1702 instead of returning empty lists.
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001703
Andrew M. Kuchling71432f12004-07-05 01:40:07 +00001704\item \function{LexicalHandler.startDTD()} used to receive the public and
1705 system IDs in the wrong order. This has been corrected; applications
Fred Drake56fcc232004-05-06 02:55:35 +00001706 relying on the wrong order need to be fixed.
Martin v. Löwis456ab1d2004-05-06 01:54:36 +00001707
Andrew M. Kuchling71432f12004-07-05 01:40:07 +00001708\item \function{fcntl.ioctl} now warns if the \var{mutate}
1709 argument is omitted and relevant.
Martin v. Löwis77ca6c42004-06-03 12:47:26 +00001710
Andrew M. Kuchling87c98b22004-08-25 13:38:46 +00001711\item The \module{tarfile} module now generates GNU-format tar files
1712by default.
1713
Andrew M. Kuchlingf8c075c2004-11-09 02:58:02 +00001714\item Encountering a failure while importing a module no longer leaves
1715a partially-initialized module object in \code{sys.modules}.
1716
1717\item \constant{None} is now a constant; code that binds a new value to
1718the name \samp{None} is now a syntax error.
1719
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001720\end{itemize}
1721
1722
1723%======================================================================
1724\section{Acknowledgements \label{acks}}
1725
1726The author would like to thank the following people for offering
1727suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchlingb6ffc272004-10-12 16:36:57 +00001728article: Hye-Shik Chang, Michael Dyck, Raymond Hettinger, Hamish Lawson,
1729Fredrik Lundh.
Fred Drakeed0fa3d2003-07-30 19:14:09 +00001730
1731\end{document}