blob: 102cd4279a1568de4d6153dc93f9f13fc6c6e59f [file] [log] [blame]
Fred Drake03e10312002-03-26 19:17:43 +00001\documentclass{howto}
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00002% $Id$
3
4\title{What's New in Python 2.3}
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +00005\release{0.03}
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00006\author{A.M. Kuchling}
Andrew M. Kuchlingbc5e3cc2002-11-05 00:26:33 +00007\authoraddress{\email{amk@amk.ca}}
Fred Drake03e10312002-03-26 19:17:43 +00008
9\begin{document}
10\maketitle
11\tableofcontents
12
Andrew M. Kuchlingc61ec522002-08-04 01:20:05 +000013% MacOS framework-related changes (section of its own, probably)
14%
Andrew M. Kuchling90e9a792002-08-15 00:40:21 +000015% xreadlines obsolete; files are their own iterator
Andrew M. Kuchlingf70a0a82002-06-10 13:22:46 +000016
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +000017%\section{Introduction \label{intro}}
18
19{\large This article is a draft, and is currently up to date for some
Andrew M. Kuchlingbc5e3cc2002-11-05 00:26:33 +000020random version of the CVS tree from early November 2002. Please send any
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +000021additions, comments or errata to the author.}
22
23This article explains the new features in Python 2.3. The tentative
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +000024release date of Python 2.3 is currently scheduled for some undefined
25time before the end of 2002.
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +000026
27This article doesn't attempt to provide a complete specification of
28the new features, but instead provides a convenient overview. For
29full details, you should refer to the documentation for Python 2.3,
30such as the
31\citetitle[http://www.python.org/doc/2.3/lib/lib.html]{Python Library
32Reference} and the
33\citetitle[http://www.python.org/doc/2.3/ref/ref.html]{Python
34Reference Manual}. If you want to understand the complete
35implementation and design rationale for a change, refer to the PEP for
36a particular new feature.
Fred Drake03e10312002-03-26 19:17:43 +000037
38
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +000039%======================================================================
Andrew M. Kuchlingbc465102002-08-20 01:34:06 +000040\section{PEP 218: A Standard Set Datatype}
41
42The new \module{sets} module contains an implementation of a set
43datatype. The \class{Set} class is for mutable sets, sets that can
44have members added and removed. The \class{ImmutableSet} class is for
45sets that can't be modified, and can be used as dictionary keys. Sets
46are built on top of dictionaries, so the elements within a set must be
47hashable.
48
Fred Drake5c4cf152002-11-13 14:59:06 +000049As a simple example,
Andrew M. Kuchlingbc465102002-08-20 01:34:06 +000050
51\begin{verbatim}
52>>> import sets
53>>> S = sets.Set([1,2,3])
54>>> S
55Set([1, 2, 3])
56>>> 1 in S
57True
58>>> 0 in S
59False
60>>> S.add(5)
61>>> S.remove(3)
62>>> S
63Set([1, 2, 5])
Fred Drake5c4cf152002-11-13 14:59:06 +000064>>>
Andrew M. Kuchlingbc465102002-08-20 01:34:06 +000065\end{verbatim}
66
67The union and intersection of sets can be computed with the
68\method{union()} and \method{intersection()} methods, or,
Fred Drake5c4cf152002-11-13 14:59:06 +000069alternatively, using the bitwise operators \code{\&} and \code{|}.
Andrew M. Kuchlingbc465102002-08-20 01:34:06 +000070Mutable sets also have in-place versions of these methods,
71\method{union_update()} and \method{intersection_update()}.
72
73\begin{verbatim}
74>>> S1 = sets.Set([1,2,3])
75>>> S2 = sets.Set([4,5,6])
76>>> S1.union(S2)
77Set([1, 2, 3, 4, 5, 6])
78>>> S1 | S2 # Alternative notation
79Set([1, 2, 3, 4, 5, 6])
Fred Drake5c4cf152002-11-13 14:59:06 +000080>>> S1.intersection(S2)
Andrew M. Kuchlingbc465102002-08-20 01:34:06 +000081Set([])
82>>> S1 & S2 # Alternative notation
83Set([])
84>>> S1.union_update(S2)
85Set([1, 2, 3, 4, 5, 6])
86>>> S1
87Set([1, 2, 3, 4, 5, 6])
Fred Drake5c4cf152002-11-13 14:59:06 +000088>>>
Andrew M. Kuchlingbc465102002-08-20 01:34:06 +000089\end{verbatim}
90
91It's also possible to take the symmetric difference of two sets. This
92is the set of all elements in the union that aren't in the
93intersection. An alternative way of expressing the symmetric
94difference is that it contains all elements that are in exactly one
95set. Again, there's an in-place version, with the ungainly name
96\method{symmetric_difference_update()}.
97
98\begin{verbatim}
99>>> S1 = sets.Set([1,2,3,4])
100>>> S2 = sets.Set([3,4,5,6])
101>>> S1.symmetric_difference(S2)
102Set([1, 2, 5, 6])
103>>> S1 ^ S2
104Set([1, 2, 5, 6])
105>>>
106\end{verbatim}
107
108There are also methods, \method{issubset()} and \method{issuperset()},
109for checking whether one set is a strict subset or superset of
110another:
111
112\begin{verbatim}
113>>> S1 = sets.Set([1,2,3])
114>>> S2 = sets.Set([2,3])
115>>> S2.issubset(S1)
116True
117>>> S1.issubset(S2)
118False
119>>> S1.issuperset(S2)
120True
121>>>
122\end{verbatim}
123
124
125\begin{seealso}
126
127\seepep{218}{Adding a Built-In Set Object Type}{PEP written by Greg V. Wilson.
128Implemented by Greg V. Wilson, Alex Martelli, and GvR.}
129
130\end{seealso}
131
132
133
134%======================================================================
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000135\section{PEP 255: Simple Generators\label{section-generators}}
Andrew M. Kuchlingf4dd65d2002-04-01 19:28:09 +0000136
137In Python 2.2, generators were added as an optional feature, to be
138enabled by a \code{from __future__ import generators} directive. In
1392.3 generators no longer need to be specially enabled, and are now
140always present; this means that \keyword{yield} is now always a
141keyword. The rest of this section is a copy of the description of
142generators from the ``What's New in Python 2.2'' document; if you read
143it when 2.2 came out, you can skip the rest of this section.
144
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000145You're doubtless familiar with how function calls work in Python or C.
146When you call a function, it gets a private namespace where its local
Andrew M. Kuchlingf4dd65d2002-04-01 19:28:09 +0000147variables are created. When the function reaches a \keyword{return}
148statement, the local variables are destroyed and the resulting value
149is returned to the caller. A later call to the same function will get
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000150a fresh new set of local variables. But, what if the local variables
Andrew M. Kuchlingf4dd65d2002-04-01 19:28:09 +0000151weren't thrown away on exiting a function? What if you could later
152resume the function where it left off? This is what generators
153provide; they can be thought of as resumable functions.
154
155Here's the simplest example of a generator function:
156
157\begin{verbatim}
158def generate_ints(N):
159 for i in range(N):
160 yield i
161\end{verbatim}
162
163A new keyword, \keyword{yield}, was introduced for generators. Any
164function containing a \keyword{yield} statement is a generator
165function; this is detected by Python's bytecode compiler which
Fred Drake5c4cf152002-11-13 14:59:06 +0000166compiles the function specially as a result.
Andrew M. Kuchlingf4dd65d2002-04-01 19:28:09 +0000167
168When you call a generator function, it doesn't return a single value;
169instead it returns a generator object that supports the iterator
170protocol. On executing the \keyword{yield} statement, the generator
171outputs the value of \code{i}, similar to a \keyword{return}
172statement. The big difference between \keyword{yield} and a
173\keyword{return} statement is that on reaching a \keyword{yield} the
174generator's state of execution is suspended and local variables are
175preserved. On the next call to the generator's \code{.next()} method,
176the function will resume executing immediately after the
177\keyword{yield} statement. (For complicated reasons, the
178\keyword{yield} statement isn't allowed inside the \keyword{try} block
179of a \code{try...finally} statement; read \pep{255} for a full
180explanation of the interaction between \keyword{yield} and
181exceptions.)
182
183Here's a sample usage of the \function{generate_ints} generator:
184
185\begin{verbatim}
186>>> gen = generate_ints(3)
187>>> gen
188<generator object at 0x8117f90>
189>>> gen.next()
1900
191>>> gen.next()
1921
193>>> gen.next()
1942
195>>> gen.next()
196Traceback (most recent call last):
Andrew M. Kuchling9f6e1042002-06-17 13:40:04 +0000197 File "stdin", line 1, in ?
198 File "stdin", line 2, in generate_ints
Andrew M. Kuchlingf4dd65d2002-04-01 19:28:09 +0000199StopIteration
200\end{verbatim}
201
202You could equally write \code{for i in generate_ints(5)}, or
203\code{a,b,c = generate_ints(3)}.
204
205Inside a generator function, the \keyword{return} statement can only
206be used without a value, and signals the end of the procession of
207values; afterwards the generator cannot return any further values.
208\keyword{return} with a value, such as \code{return 5}, is a syntax
209error inside a generator function. The end of the generator's results
210can also be indicated by raising \exception{StopIteration} manually,
211or by just letting the flow of execution fall off the bottom of the
212function.
213
214You could achieve the effect of generators manually by writing your
215own class and storing all the local variables of the generator as
216instance variables. For example, returning a list of integers could
217be done by setting \code{self.count} to 0, and having the
218\method{next()} method increment \code{self.count} and return it.
219However, for a moderately complicated generator, writing a
220corresponding class would be much messier.
221\file{Lib/test/test_generators.py} contains a number of more
222interesting examples. The simplest one implements an in-order
223traversal of a tree using generators recursively.
224
225\begin{verbatim}
226# A recursive generator that generates Tree leaves in in-order.
227def inorder(t):
228 if t:
229 for x in inorder(t.left):
230 yield x
231 yield t.label
232 for x in inorder(t.right):
233 yield x
234\end{verbatim}
235
236Two other examples in \file{Lib/test/test_generators.py} produce
237solutions for the N-Queens problem (placing $N$ queens on an $NxN$
238chess board so that no queen threatens another) and the Knight's Tour
239(a route that takes a knight to every square of an $NxN$ chessboard
Fred Drake5c4cf152002-11-13 14:59:06 +0000240without visiting any square twice).
Andrew M. Kuchlingf4dd65d2002-04-01 19:28:09 +0000241
242The idea of generators comes from other programming languages,
243especially Icon (\url{http://www.cs.arizona.edu/icon/}), where the
244idea of generators is central. In Icon, every
245expression and function call behaves like a generator. One example
246from ``An Overview of the Icon Programming Language'' at
247\url{http://www.cs.arizona.edu/icon/docs/ipd266.htm} gives an idea of
248what this looks like:
249
250\begin{verbatim}
251sentence := "Store it in the neighboring harbor"
252if (i := find("or", sentence)) > 5 then write(i)
253\end{verbatim}
254
255In Icon the \function{find()} function returns the indexes at which the
256substring ``or'' is found: 3, 23, 33. In the \keyword{if} statement,
257\code{i} is first assigned a value of 3, but 3 is less than 5, so the
258comparison fails, and Icon retries it with the second value of 23. 23
259is greater than 5, so the comparison now succeeds, and the code prints
260the value 23 to the screen.
261
262Python doesn't go nearly as far as Icon in adopting generators as a
263central concept. Generators are considered a new part of the core
264Python language, but learning or using them isn't compulsory; if they
265don't solve any problems that you have, feel free to ignore them.
266One novel feature of Python's interface as compared to
267Icon's is that a generator's state is represented as a concrete object
268(the iterator) that can be passed around to other functions or stored
269in a data structure.
270
271\begin{seealso}
272
273\seepep{255}{Simple Generators}{Written by Neil Schemenauer, Tim
274Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer
275and Tim Peters, with other fixes from the Python Labs crew.}
276
277\end{seealso}
278
279
280%======================================================================
Fred Drake13090e12002-08-22 16:51:08 +0000281\section{PEP 263: Source Code Encodings \label{section-encodings}}
Andrew M. Kuchling950725f2002-08-06 01:40:48 +0000282
283Python source files can now be declared as being in different
284character set encodings. Encodings are declared by including a
285specially formatted comment in the first or second line of the source
286file. For example, a UTF-8 file can be declared with:
287
288\begin{verbatim}
289#!/usr/bin/env python
290# -*- coding: UTF-8 -*-
291\end{verbatim}
292
293Without such an encoding declaration, the default encoding used is
Fred Drake5c4cf152002-11-13 14:59:06 +0000294ISO-8859-1, also known as Latin1.
Andrew M. Kuchling950725f2002-08-06 01:40:48 +0000295
296The encoding declaration only affects Unicode string literals; the
297text in the source code will be converted to Unicode using the
298specified encoding. Note that Python identifiers are still restricted
299to ASCII characters, so you can't have variable names that use
300characters outside of the usual alphanumerics.
301
302\begin{seealso}
303
304\seepep{263}{Defining Python Source Code Encodings}{Written by
Martin v. Löwisbd5e38d2002-10-07 18:52:29 +0000305Marc-Andr\'e Lemburg and Martin von L\"owis; implemented by SUZUKI
306Hisao and Martin von L\"owis.}
Andrew M. Kuchling950725f2002-08-06 01:40:48 +0000307
308\end{seealso}
309
310
311%======================================================================
Martin v. Löwisbd5e38d2002-10-07 18:52:29 +0000312\section{PEP 277: Unicode file name support for Windows NT}
Andrew M. Kuchling0f345562002-10-04 22:34:11 +0000313
Martin v. Löwisbd5e38d2002-10-07 18:52:29 +0000314On Windows NT, 2000, and XP, the system stores file names as Unicode
Andrew M. Kuchling0a6fa962002-10-09 12:11:10 +0000315strings. Traditionally, Python has represented file names as byte
316strings, which is inadequate because it renders some file names
Martin v. Löwisbd5e38d2002-10-07 18:52:29 +0000317inaccessible.
318
Andrew M. Kuchling0a6fa962002-10-09 12:11:10 +0000319Python now allows using arbitrary Unicode strings (within the
320limitations of the file system) for all functions that expect file
321names, in particular the \function{open()} built-in. If a Unicode
322string is passed to \function{os.listdir}, Python now returns a list
323of Unicode strings. A new function, \function{os.getcwdu()}, returns
324the current directory as a Unicode string.
Martin v. Löwisbd5e38d2002-10-07 18:52:29 +0000325
Andrew M. Kuchling0a6fa962002-10-09 12:11:10 +0000326Byte strings still work as file names, and Python will transparently
327convert them to Unicode using the \code{mbcs} encoding.
Martin v. Löwisbd5e38d2002-10-07 18:52:29 +0000328
Andrew M. Kuchling0a6fa962002-10-09 12:11:10 +0000329Other systems also allow Unicode strings as file names, but convert
330them to byte strings before passing them to the system which may cause
331a \exception{UnicodeError} to be raised. Applications can test whether
332arbitrary Unicode strings are supported as file names by checking
333\member{os.path.unicode_file_names}, a Boolean value.
Martin v. Löwisbd5e38d2002-10-07 18:52:29 +0000334
335\begin{seealso}
336
337\seepep{277}{Unicode file name support for Windows NT}{Written by Neil
338Hodgson; implemented by Neil Hodgson, Martin von L\"owis, and Mark
339Hammond.}
340
341\end{seealso}
Andrew M. Kuchling0f345562002-10-04 22:34:11 +0000342
343
344%======================================================================
Andrew M. Kuchlingf3676512002-04-15 02:27:55 +0000345\section{PEP 278: Universal Newline Support}
346
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000347The three major operating systems used today are Microsoft Windows,
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000348Apple's Macintosh OS, and the various \UNIX\ derivatives. A minor
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000349irritation is that these three platforms all use different characters
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000350to mark the ends of lines in text files. \UNIX\ uses character 10,
351the ASCII linefeed, while MacOS uses character 13, the ASCII carriage
352return, and Windows uses a two-character sequence of a carriage return
353plus a newline.
Andrew M. Kuchlingf3676512002-04-15 02:27:55 +0000354
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000355Python's file objects can now support end of line conventions other
356than the one followed by the platform on which Python is running.
Fred Drake5c4cf152002-11-13 14:59:06 +0000357Opening a file with the mode \code{'U'} or \code{'rU'} will open a file
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000358for reading in universal newline mode. All three line ending
Fred Drake5c4cf152002-11-13 14:59:06 +0000359conventions will be translated to a \character{\e n} in the strings
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000360returned by the various file methods such as \method{read()} and
Fred Drake5c4cf152002-11-13 14:59:06 +0000361\method{readline()}.
Andrew M. Kuchlingf3676512002-04-15 02:27:55 +0000362
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000363Universal newline support is also used when importing modules and when
364executing a file with the \function{execfile()} function. This means
365that Python modules can be shared between all three operating systems
366without needing to convert the line-endings.
367
Fred Drake5c4cf152002-11-13 14:59:06 +0000368This feature can be disabled at compile-time by specifying
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000369\longprogramopt{without-universal-newlines} when running Python's
Fred Drake5c4cf152002-11-13 14:59:06 +0000370\program{configure} script.
Andrew M. Kuchlingf3676512002-04-15 02:27:55 +0000371
372\begin{seealso}
373
Fred Drake5c4cf152002-11-13 14:59:06 +0000374\seepep{278}{Universal Newline Support}{Written
Andrew M. Kuchlingf3676512002-04-15 02:27:55 +0000375and implemented by Jack Jansen.}
376
377\end{seealso}
378
Andrew M. Kuchlingfad2f592002-05-10 21:00:05 +0000379
380%======================================================================
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000381\section{PEP 279: The \function{enumerate()} Built-in Function\label{section-enumerate}}
Andrew M. Kuchlingfad2f592002-05-10 21:00:05 +0000382
383A new built-in function, \function{enumerate()}, will make
384certain loops a bit clearer. \code{enumerate(thing)}, where
385\var{thing} is either an iterator or a sequence, returns a iterator
386that will return \code{(0, \var{thing[0]})}, \code{(1,
387\var{thing[1]})}, \code{(2, \var{thing[2]})}, and so forth. Fairly
388often you'll see code to change every element of a list that looks
389like this:
390
391\begin{verbatim}
392for i in range(len(L)):
393 item = L[i]
394 # ... compute some result based on item ...
395 L[i] = result
396\end{verbatim}
397
398This can be rewritten using \function{enumerate()} as:
399
400\begin{verbatim}
401for i, item in enumerate(L):
402 # ... compute some result based on item ...
403 L[i] = result
404\end{verbatim}
405
406
407\begin{seealso}
408
Fred Drake5c4cf152002-11-13 14:59:06 +0000409\seepep{279}{The enumerate() built-in function}{Written
Andrew M. Kuchlingfad2f592002-05-10 21:00:05 +0000410by Raymond D. Hettinger.}
411
412\end{seealso}
413
414
Andrew M. Kuchlingf3676512002-04-15 02:27:55 +0000415%======================================================================
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +0000416\section{PEP 282: The \module{logging} Package}
417
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +0000418A standard package for writing logs called \module{logging} has been
419added to Python 2.3. It provides a powerful and flexible way for
420components to generate logging output which can then be filtered and
421processed in various ways. A standard configuration file format can
422be used to control the logging behaviour of a program. Python comes
423with handlers that will write log records to standard error or to a
424file or socket, send them to the system log, or even e-mail them to a
425particular address, and of course it's also possible to write your own
426handler classes.
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +0000427
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +0000428Most application code will deal with one or more \class{Logger}
429objects, each one used by a particular subsystem of the application.
430Each \class{Logger} is identified by a name, and names are organized
431into a hierarchy using \samp{.} as the component separator. For
432example, you might have \class{Logger} instances named \samp{server},
433\samp{server.auth} and \samp{server.network}. The latter two
434instances fall under the \samp{server} \class{Logger} in the
435hierarchy. This means that if you turn up the verbosity for
436\samp{server} or direct \samp{server} messages to a different handler,
437the changes will also apply to records logged to \samp{server.auth}
438and \samp{server.network}. There's also a root \class{Logger} with
439the name \samp{root} that's the parent of all other loggers.
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +0000440
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +0000441For simple uses, the \module{logging} package contains some
442convenience functions that always use the root log:
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +0000443
444\begin{verbatim}
445import logging
446
447logging.debug('Debugging information')
448logging.info('Informational message')
449logging.warn('Warning: config file %s not found', 'server.conf')
450logging.error('Error occurred')
451logging.critical('Critical error -- shutting down')
452\end{verbatim}
453
454This produces the following output:
455
456\begin{verbatim}
457WARN:root:Warning: config file not found
458ERROR:root:Error occurred
459CRITICAL:root:Critical error -- shutting down
460\end{verbatim}
461
462In the default configuration, informational and debugging messages are
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +0000463suppressed and the output is sent to standard error; you can change
464this by calling the \method{setLevel()} method on the root logger.
465
466Notice the \function{warn()} call's use of string formatting
467operators; all of the functions for logging messages take the
468arguments \code{(\var{msg}, \var{arg1}, \var{arg2}, ...)} and log the
469string resulting from \code{\var{msg} \% (\var{arg1}, \var{arg2},
470...)}.
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +0000471
472There's also an \function{exception()} function that records the most
473recent traceback. Any of the other functions will also record the
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +0000474traceback if you specify a true value for the keyword argument
475\code{exc_info}.
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +0000476
477\begin{verbatim}
478def f():
479 try: 1/0
480 except: logging.exception('Problem recorded')
481
482f()
483\end{verbatim}
484
485This produces the following output:
486
487\begin{verbatim}
488ERROR:root:Problem recorded
489Traceback (most recent call last):
490 File "t.py", line 6, in f
491 1/0
492ZeroDivisionError: integer division or modulo by zero
493\end{verbatim}
494
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +0000495Slightly more advanced programs will use a logger other than the root
496logger. The \function{getLogger(\var{name})} is used to get a
497particular log, creating it if it doesn't exist yet.
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +0000498
499\begin{verbatim}
500log = logging.getLogger('server')
501 ...
502log.info('Listening on port %i', port)
503 ...
504log.critical('Disk full')
505 ...
506\end{verbatim}
507
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +0000508There are more classes that can be customized. When a \class{Logger}
509instance is told to log a message, it creates a \class{LogRecord}
510instance that is sent to any number of different \class{Handler}
511instances. Loggers and handlers can also have an attached list of
512filters, and each filter can cause the \class{LogRecord} to be ignored
513or can modify the record before passing it along. \class{LogRecord}
514instances are converted to text by a \class{Formatter} class.
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +0000515
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +0000516Log records are usually propagated up the hierarchy, so a message
517logged to \samp{server.auth} is also seen by \samp{server} and
518\samp{root}, but a handler can prevent this by setting its
519\member{propagate} attribute to \code{True}.
520
521With all of these features the \module{logging} package should provide
522enough flexibility for even the most complicated applications. This
523is only a partial overview of the \module{logging} package's features,
524so please see the
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +0000525\citetitle[http://www.python.org/dev/doc/devel/lib/module-logging.html]{\module{logging}
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +0000526package's reference documentation} for all of the details. Reading
527\pep{282} will also be helpful.
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +0000528
529
530\begin{seealso}
531
532\seepep{282}{A Logging System}{Written by Vinay Sajip and Trent Mick;
533implemented by Vinay Sajip.}
534
535\end{seealso}
536
537
538%======================================================================
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000539\section{PEP 285: The \class{bool} Type\label{section-bool}}
540
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000541A Boolean type was added to Python 2.3. Two new constants were added
542to the \module{__builtin__} module, \constant{True} and
543\constant{False}. The type object for this new type is named
544\class{bool}; the constructor for it takes any Python value and
545converts it to \constant{True} or \constant{False}.
546
547\begin{verbatim}
548>>> bool(1)
549True
550>>> bool(0)
551False
552>>> bool([])
553False
554>>> bool( (1,) )
555True
556\end{verbatim}
557
558Most of the standard library modules and built-in functions have been
559changed to return Booleans.
560
561\begin{verbatim}
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000562>>> obj = []
563>>> hasattr(obj, 'append')
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000564True
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000565>>> isinstance(obj, list)
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000566True
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000567>>> isinstance(obj, tuple)
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000568False
569\end{verbatim}
570
571Python's Booleans were added with the primary goal of making code
572clearer. For example, if you're reading a function and encounter the
Fred Drake5c4cf152002-11-13 14:59:06 +0000573statement \code{return 1}, you might wonder whether the \code{1}
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000574represents a truth value, or whether it's an index, or whether it's a
575coefficient that multiplies some other quantity. If the statement is
576\code{return True}, however, the meaning of the return value is quite
577clearly a truth value.
578
579Python's Booleans were not added for the sake of strict type-checking.
Andrew M. Kuchlinga2a206b2002-05-24 21:08:58 +0000580A very strict language such as Pascal would also prevent you
581performing arithmetic with Booleans, and would require that the
582expression in an \keyword{if} statement always evaluate to a Boolean.
583Python is not this strict, and it never will be. (\pep{285}
584explicitly says so.) So you can still use any expression in an
585\keyword{if}, even ones that evaluate to a list or tuple or some
586random object, and the Boolean type is a subclass of the
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000587\class{int} class, so arithmetic using a Boolean still works.
588
589\begin{verbatim}
590>>> True + 1
5912
592>>> False + 1
5931
594>>> False * 75
5950
596>>> True * 75
59775
598\end{verbatim}
599
600To sum up \constant{True} and \constant{False} in a sentence: they're
601alternative ways to spell the integer values 1 and 0, with the single
602difference that \function{str()} and \function{repr()} return the
Fred Drake5c4cf152002-11-13 14:59:06 +0000603strings \code{'True'} and \code{'False'} instead of \code{'1'} and
604\code{'0'}.
Andrew M. Kuchling3a52ff62002-04-03 22:44:47 +0000605
606\begin{seealso}
607
608\seepep{285}{Adding a bool type}{Written and implemented by GvR.}
609
610\end{seealso}
611
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000612
Andrew M. Kuchling65b72822002-09-03 00:53:21 +0000613%======================================================================
614\section{PEP 293: Codec Error Handling Callbacks}
615
Martin v. Löwis20eae692002-10-07 19:01:07 +0000616When encoding a Unicode string into a byte string, unencodable
Andrew M. Kuchling0a6fa962002-10-09 12:11:10 +0000617characters may be encountered. So far, Python has allowed specifying
618the error processing as either ``strict'' (raising
619\exception{UnicodeError}), ``ignore'' (skip the character), or
620``replace'' (with question mark), defaulting to ``strict''. It may be
621desirable to specify an alternative processing of the error, e.g. by
622inserting an XML character reference or HTML entity reference into the
623converted string.
Martin v. Löwis20eae692002-10-07 19:01:07 +0000624
625Python now has a flexible framework to add additional processing
Andrew M. Kuchling0a6fa962002-10-09 12:11:10 +0000626strategies. New error handlers can be added with
Martin v. Löwis20eae692002-10-07 19:01:07 +0000627\function{codecs.register_error}. Codecs then can access the error
Andrew M. Kuchling0a6fa962002-10-09 12:11:10 +0000628handler with \function{codecs.lookup_error}. An equivalent C API has
629been added for codecs written in C. The error handler gets the
630necessary state information, such as the string being converted, the
631position in the string where the error was detected, and the target
632encoding. The handler can then either raise an exception, or return a
633replacement string.
Martin v. Löwis20eae692002-10-07 19:01:07 +0000634
635Two additional error handlers have been implemented using this
Andrew M. Kuchling0a6fa962002-10-09 12:11:10 +0000636framework: ``backslashreplace'' uses Python backslash quoting to
Martin v. Löwis20eae692002-10-07 19:01:07 +0000637represent the unencodable character, and ``xmlcharrefreplace'' emits
638XML character references.
Andrew M. Kuchling65b72822002-09-03 00:53:21 +0000639
640\begin{seealso}
641
Fred Drake5c4cf152002-11-13 14:59:06 +0000642\seepep{293}{Codec Error Handling Callbacks}{Written and implemented by
Andrew M. Kuchling0a6fa962002-10-09 12:11:10 +0000643Walter D\"orwald.}
Andrew M. Kuchling65b72822002-09-03 00:53:21 +0000644
645\end{seealso}
646
647
648%======================================================================
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000649\section{Extended Slices\label{section-slices}}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000650
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000651Ever since Python 1.4, the slicing syntax has supported an optional
652third ``step'' or ``stride'' argument. For example, these are all
653legal Python syntax: \code{L[1:10:2]}, \code{L[:-1:1]},
654\code{L[::-1]}. This was added to Python included at the request of
655the developers of Numerical Python. However, the built-in sequence
656types of lists, tuples, and strings have never supported this feature,
657and you got a \exception{TypeError} if you tried it. Michael Hudson
Fred Drake5c4cf152002-11-13 14:59:06 +0000658contributed a patch that was applied to Python 2.3 and fixed this
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000659shortcoming.
660
661For example, you can now easily extract the elements of a list that
662have even indexes:
Fred Drakedf872a22002-07-03 12:02:01 +0000663
664\begin{verbatim}
665>>> L = range(10)
666>>> L[::2]
667[0, 2, 4, 6, 8]
668\end{verbatim}
669
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000670Negative values also work, so you can make a copy of the same list in
671reverse order:
Fred Drakedf872a22002-07-03 12:02:01 +0000672
673\begin{verbatim}
674>>> L[::-1]
675[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
676\end{verbatim}
Andrew M. Kuchling3a52ff62002-04-03 22:44:47 +0000677
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000678This also works for strings:
679
680\begin{verbatim}
681>>> s='abcd'
682>>> s[::2]
683'ac'
684>>> s[::-1]
685'dcba'
686\end{verbatim}
687
Michael W. Hudson4da01ed2002-07-19 15:48:56 +0000688as well as tuples and arrays.
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000689
Michael W. Hudson4da01ed2002-07-19 15:48:56 +0000690If you have a mutable sequence (i.e. a list or an array) you can
691assign to or delete an extended slice, but there are some differences
692in assignment to extended and regular slices. Assignment to a regular
693slice can be used to change the length of the sequence:
694
695\begin{verbatim}
696>>> a = range(3)
697>>> a
698[0, 1, 2]
699>>> a[1:3] = [4, 5, 6]
700>>> a
701[0, 4, 5, 6]
702\end{verbatim}
703
704but when assigning to an extended slice the list on the right hand
705side of the statement must contain the same number of items as the
706slice it is replacing:
707
708\begin{verbatim}
709>>> a = range(4)
710>>> a
711[0, 1, 2, 3]
712>>> a[::2]
713[0, 2]
714>>> a[::2] = range(0, -2, -1)
715>>> a
716[0, 1, -1, 3]
717>>> a[::2] = range(3)
718Traceback (most recent call last):
719 File "<stdin>", line 1, in ?
720ValueError: attempt to assign list of size 3 to extended slice of size 2
721\end{verbatim}
722
723Deletion is more straightforward:
724
725\begin{verbatim}
726>>> a = range(4)
727>>> a[::2]
728[0, 2]
729>>> del a[::2]
730>>> a
731[1, 3]
732\end{verbatim}
733
734One can also now pass slice objects to builtin sequences
735\method{__getitem__} methods:
736
737\begin{verbatim}
738>>> range(10).__getitem__(slice(0, 5, 2))
739[0, 2, 4]
740\end{verbatim}
741
742or use them directly in subscripts:
743
744\begin{verbatim}
745>>> range(10)[slice(0, 5, 2)]
746[0, 2, 4]
747\end{verbatim}
748
749To make implementing sequences that support extended slicing in Python
750easier, slice ojects now have a method \method{indices} which given
751the length of a sequence returns \code{(start, stop, step)} handling
752omitted and out-of-bounds indices in a manner consistent with regular
753slices (and this innocuous phrase hides a welter of confusing
754details!). The method is intended to be used like this:
755
756\begin{verbatim}
757class FakeSeq:
758 ...
759 def calc_item(self, i):
760 ...
761 def __getitem__(self, item):
762 if isinstance(item, slice):
Fred Drake5c4cf152002-11-13 14:59:06 +0000763 return FakeSeq([self.calc_item(i)
Michael W. Hudson4da01ed2002-07-19 15:48:56 +0000764 in range(*item.indices(len(self)))])
Fred Drake5c4cf152002-11-13 14:59:06 +0000765 else:
Michael W. Hudson4da01ed2002-07-19 15:48:56 +0000766 return self.calc_item(i)
767\end{verbatim}
768
Andrew M. Kuchling90e9a792002-08-15 00:40:21 +0000769From this example you can also see that the builtin ``\class{slice}''
770object is now the type object for the slice type, and is no longer a
771function. This is consistent with Python 2.2, where \class{int},
772\class{str}, etc., underwent the same change.
773
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000774
Andrew M. Kuchling3a52ff62002-04-03 22:44:47 +0000775%======================================================================
Fred Drakedf872a22002-07-03 12:02:01 +0000776\section{Other Language Changes}
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000777
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000778Here are all of the changes that Python 2.3 makes to the core Python
779language.
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000780
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000781\begin{itemize}
782\item The \keyword{yield} statement is now always a keyword, as
783described in section~\ref{section-generators} of this document.
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000784
Fred Drake5c4cf152002-11-13 14:59:06 +0000785\item A new built-in function \function{enumerate()}
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000786was added, as described in section~\ref{section-enumerate} of this
787document.
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000788
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000789\item Two new constants, \constant{True} and \constant{False} were
790added along with the built-in \class{bool} type, as described in
791section~\ref{section-bool} of this document.
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000792
Fred Drake5c4cf152002-11-13 14:59:06 +0000793\item Built-in types now support the extended slicing syntax,
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000794as described in section~\ref{section-slices} of this document.
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000795
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000796\item Dictionaries have a new method, \method{pop(\var{key})}, that
797returns the value corresponding to \var{key} and removes that
798key/value pair from the dictionary. \method{pop()} will raise a
799\exception{KeyError} if the requested key isn't present in the
800dictionary:
801
802\begin{verbatim}
803>>> d = {1:2}
804>>> d
805{1: 2}
806>>> d.pop(4)
807Traceback (most recent call last):
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +0000808 File "stdin", line 1, in ?
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000809KeyError: 4
810>>> d.pop(1)
8112
812>>> d.pop(1)
813Traceback (most recent call last):
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +0000814 File "stdin", line 1, in ?
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000815KeyError: pop(): dictionary is empty
816>>> d
817{}
818>>>
819\end{verbatim}
820
821(Patch contributed by Raymond Hettinger.)
822
Andrew M. Kuchling7a82b8c2002-11-04 20:17:24 +0000823\item The \keyword{assert} statement no longer checks the \code{__debug__}
Andrew M. Kuchling6974aa92002-08-20 00:54:36 +0000824flag, so you can no longer disable assertions by assigning to \code{__debug__}.
Fred Drake5c4cf152002-11-13 14:59:06 +0000825Running Python with the \programopt{-O} switch will still generate
Andrew M. Kuchling6974aa92002-08-20 00:54:36 +0000826code that doesn't execute any assertions.
827
828\item Most type objects are now callable, so you can use them
829to create new objects such as functions, classes, and modules. (This
830means that the \module{new} module can be deprecated in a future
831Python version, because you can now use the type objects available
832in the \module{types} module.)
833% XXX should new.py use PendingDeprecationWarning?
834For example, you can create a new module object with the following code:
835
836\begin{verbatim}
837>>> import types
838>>> m = types.ModuleType('abc','docstring')
839>>> m
840<module 'abc' (built-in)>
841>>> m.__doc__
842'docstring'
843\end{verbatim}
844
Fred Drake5c4cf152002-11-13 14:59:06 +0000845\item
Andrew M. Kuchling6974aa92002-08-20 00:54:36 +0000846A new warning, \exception{PendingDeprecationWarning} was added to
847indicate features which are in the process of being
848deprecated. The warning will \emph{not} be printed by default. To
849check for use of features that will be deprecated in the future,
850supply \programopt{-Walways::PendingDeprecationWarning::} on the
851command line or use \function{warnings.filterwarnings()}.
852
853\item Using \code{None} as a variable name will now result in a
854\exception{SyntaxWarning} warning. In a future version of Python,
855\code{None} may finally become a keyword.
856
Andrew M. Kuchlingb60ea3f2002-11-15 14:37:10 +0000857\item The method resolution order used by new-style classes has
858changed, though you'll only notice the difference if you have a really
859complicated inheritance hierarchy. (Classic classes are unaffected by
860this change.) Python 2.2 originally used a topological sort of a
861class's ancestors, but 2.3 now uses the C3 algorithm as described in
862\citetitle[http://www.webcom.com/haahr/dylan/linearization-oopsla96.html]{``A
863Monotonic Superclass Linearization for Dylan''}. To understand the
864motivation for this change, read the thread on python-dev starting
865with the message at
866\url{http://mail.python.org/pipermail/python-dev/2002-October/029035.html}.
867Samuele Pedroni first pointed out the problem and also implemented the
868fix by coding the C3 algorithm.
869
Andrew M. Kuchlingdcfd8252002-09-13 22:21:42 +0000870\item Python runs multithreaded programs by switching between threads
871after executing N bytecodes. The default value for N has been
872increased from 10 to 100 bytecodes, speeding up single-threaded
873applications by reducing the switching overhead. Some multithreaded
874applications may suffer slower response time, but that's easily fixed
875by setting the limit back to a lower number by calling
876\function{sys.setcheckinterval(\var{N})}.
877
Andrew M. Kuchling6974aa92002-08-20 00:54:36 +0000878\item One minor but far-reaching change is that the names of extension
879types defined by the modules included with Python now contain the
Fred Drake5c4cf152002-11-13 14:59:06 +0000880module and a \character{.} in front of the type name. For example, in
Andrew M. Kuchling6974aa92002-08-20 00:54:36 +0000881Python 2.2, if you created a socket and printed its
882\member{__class__}, you'd get this output:
883
884\begin{verbatim}
885>>> s = socket.socket()
886>>> s.__class__
887<type 'socket'>
888\end{verbatim}
889
890In 2.3, you get this:
891\begin{verbatim}
892>>> s.__class__
893<type '_socket.socket'>
894\end{verbatim}
895
896\end{itemize}
897
898
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +0000899%======================================================================
Andrew M. Kuchling6974aa92002-08-20 00:54:36 +0000900\subsection{String Changes}
901
902\begin{itemize}
903
904\item The \code{in} operator now works differently for strings.
905Previously, when evaluating \code{\var{X} in \var{Y}} where \var{X}
906and \var{Y} are strings, \var{X} could only be a single character.
907That's now changed; \var{X} can be a string of any length, and
908\code{\var{X} in \var{Y}} will return \constant{True} if \var{X} is a
909substring of \var{Y}. If \var{X} is the empty string, the result is
910always \constant{True}.
911
912\begin{verbatim}
913>>> 'ab' in 'abcd'
914True
915>>> 'ad' in 'abcd'
916False
917>>> '' in 'abcd'
918True
919\end{verbatim}
920
921Note that this doesn't tell you where the substring starts; the
922\method{find()} method is still necessary to figure that out.
923
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000924\item The \method{strip()}, \method{lstrip()}, and \method{rstrip()}
925string methods now have an optional argument for specifying the
926characters to strip. The default is still to remove all whitespace
927characters:
928
929\begin{verbatim}
930>>> ' abc '.strip()
931'abc'
932>>> '><><abc<><><>'.strip('<>')
933'abc'
934>>> '><><abc<><><>\n'.strip('<>')
935'abc<><><>\n'
936>>> u'\u4000\u4001abc\u4000'.strip(u'\u4000')
937u'\u4001abc'
938>>>
939\end{verbatim}
940
Andrew M. Kuchling7a82b8c2002-11-04 20:17:24 +0000941(Suggested by Simon Brunning, and implemented by Walter D\"orwald.)
Andrew M. Kuchling346386f2002-07-12 20:24:42 +0000942
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000943\item The \method{startswith()} and \method{endswith()}
944string methods now accept negative numbers for the start and end
945parameters.
946
947\item Another new string method is \method{zfill()}, originally a
948function in the \module{string} module. \method{zfill()} pads a
949numeric string with zeros on the left until it's the specified width.
950Note that the \code{\%} operator is still more flexible and powerful
951than \method{zfill()}.
952
953\begin{verbatim}
954>>> '45'.zfill(4)
955'0045'
956>>> '12345'.zfill(4)
957'12345'
958>>> 'goofy'.zfill(6)
959'0goofy'
960\end{verbatim}
961
Andrew M. Kuchling346386f2002-07-12 20:24:42 +0000962(Contributed by Walter D\"orwald.)
963
Fred Drake5c4cf152002-11-13 14:59:06 +0000964\item A new type object, \class{basestring}, has been added.
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +0000965 Both 8-bit strings and Unicode strings inherit from this type, so
966 \code{isinstance(obj, basestring)} will return \constant{True} for
967 either kind of string. It's a completely abstract type, so you
968 can't create \class{basestring} instances.
969
Andrew M. Kuchling6974aa92002-08-20 00:54:36 +0000970\item Interned strings are no longer immortal. Interned will now be
971garbage-collected in the usual way when the only reference to them is
972from the internal dictionary of interned strings. (Implemented by
973Oren Tirosh.)
974
975\end{itemize}
976
977
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +0000978%======================================================================
Andrew M. Kuchling6974aa92002-08-20 00:54:36 +0000979\subsection{Optimizations}
980
981\begin{itemize}
982
Andrew M. Kuchling950725f2002-08-06 01:40:48 +0000983\item The \method{sort()} method of list objects has been extensively
984rewritten by Tim Peters, and the implementation is significantly
985faster.
986
Andrew M. Kuchling6974aa92002-08-20 00:54:36 +0000987\item Multiplication of large long integers is now much faster thanks
988to an implementation of Karatsuba multiplication, an algorithm that
989scales better than the O(n*n) required for the grade-school
990multiplication algorithm. (Original patch by Christopher A. Craig,
991and significantly reworked by Tim Peters.)
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +0000992
Andrew M. Kuchling6974aa92002-08-20 00:54:36 +0000993\item The \code{SET_LINENO} opcode is now gone. This may provide a
994small speed increase, subject to your compiler's idiosyncrasies.
995(Removed by Michael Hudson.)
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +0000996
Andrew M. Kuchling6974aa92002-08-20 00:54:36 +0000997\item A number of small rearrangements have been made in various
998hotspots to improve performance, inlining a function here, removing
999some code there. (Implemented mostly by GvR, but lots of people have
1000contributed to one change or another.)
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +00001001
1002\end{itemize}
Neal Norwitzd68f5172002-05-29 15:54:55 +00001003
Andrew M. Kuchling6974aa92002-08-20 00:54:36 +00001004
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001005%======================================================================
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00001006\section{New and Improved Modules}
1007
Andrew M. Kuchling821013e2002-05-06 17:46:39 +00001008As usual, Python's standard modules had a number of enhancements and
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001009bug fixes. Here's a partial list of the most notable changes, sorted
1010alphabetically by module name. Consult the
1011\file{Misc/NEWS} file in the source tree for a more
1012complete list of changes, or look through the CVS logs for all the
1013details.
Andrew M. Kuchling821013e2002-05-06 17:46:39 +00001014
1015\begin{itemize}
1016
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001017\item The \module{array} module now supports arrays of Unicode
Fred Drake5c4cf152002-11-13 14:59:06 +00001018characters using the \character{u} format character. Arrays also now
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001019support using the \code{+=} assignment operator to add another array's
1020contents, and the \code{*=} assignment operator to repeat an array.
1021(Contributed by Jason Orendorff.)
1022
Fred Drake5c4cf152002-11-13 14:59:06 +00001023\item The Distutils \class{Extension} class now supports
1024an extra constructor argument named \var{depends} for listing
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001025additional source files that an extension depends on. This lets
1026Distutils recompile the module if any of the dependency files are
Fred Drake5c4cf152002-11-13 14:59:06 +00001027modified. For example, if \file{sampmodule.c} includes the header
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001028file \file{sample.h}, you would create the \class{Extension} object like
1029this:
1030
1031\begin{verbatim}
1032ext = Extension("samp",
1033 sources=["sampmodule.c"],
1034 depends=["sample.h"])
1035\end{verbatim}
1036
1037Modifying \file{sample.h} would then cause the module to be recompiled.
1038(Contributed by Jeremy Hylton.)
1039
Andrew M. Kuchlingdc3f7e12002-11-04 20:05:10 +00001040\item Other minor changes to Distutils:
1041it now checks for the \envvar{CC}, \envvar{CFLAGS}, \envvar{CPP},
1042\envvar{LDFLAGS}, and \envvar{CPPFLAGS} environment variables, using
1043them to override the settings in Python's configuration (contributed
1044by Robert Weber); the \function{get_distutils_option()} method lists
1045recently-added extensions to Distutils.
1046
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001047\item The \module{getopt} module gained a new function,
1048\function{gnu_getopt()}, that supports the same arguments as the existing
Fred Drake5c4cf152002-11-13 14:59:06 +00001049\function{getopt()} function but uses GNU-style scanning mode.
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001050The existing \function{getopt()} stops processing options as soon as a
1051non-option argument is encountered, but in GNU-style mode processing
1052continues, meaning that options and arguments can be mixed. For
1053example:
1054
1055\begin{verbatim}
1056>>> getopt.getopt(['-f', 'filename', 'output', '-v'], 'f:v')
1057([('-f', 'filename')], ['output', '-v'])
1058>>> getopt.gnu_getopt(['-f', 'filename', 'output', '-v'], 'f:v')
1059([('-f', 'filename'), ('-v', '')], ['output'])
1060\end{verbatim}
1061
1062(Contributed by Peter \AA{strand}.)
1063
1064\item The \module{grp}, \module{pwd}, and \module{resource} modules
Fred Drake5c4cf152002-11-13 14:59:06 +00001065now return enhanced tuples:
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001066
1067\begin{verbatim}
1068>>> import grp
1069>>> g = grp.getgrnam('amk')
1070>>> g.gr_name, g.gr_gid
1071('amk', 500)
1072\end{verbatim}
1073
Andrew M. Kuchling950725f2002-08-06 01:40:48 +00001074\item The new \module{heapq} module contains an implementation of a
1075heap queue algorithm. A heap is an array-like data structure that
1076keeps items in a sorted order such that, for every index k, heap[k] <=
1077heap[2*k+1] and heap[k] <= heap[2*k+2]. This makes it quick to remove
1078the smallest item, and inserting a new item while maintaining the heap
1079property is O(lg~n). (See
1080\url{http://www.nist.gov/dads/HTML/priorityque.html} for more
1081information about the priority queue data structure.)
1082
Andrew M. Kuchling8a61f492002-11-13 13:24:41 +00001083The \module{heapq} module provides \function{heappush()} and
Andrew M. Kuchling950725f2002-08-06 01:40:48 +00001084\function{heappop()} functions for adding and removing items while
1085maintaining the heap property on top of some other mutable Python
1086sequence type. For example:
1087
1088\begin{verbatim}
1089>>> import heapq
1090>>> heap = []
1091>>> for item in [3, 7, 5, 11, 1]:
1092... heapq.heappush(heap, item)
1093...
1094>>> heap
1095[1, 3, 5, 11, 7]
1096>>> heapq.heappop(heap)
10971
1098>>> heapq.heappop(heap)
10993
1100>>> heap
1101[5, 7, 11]
1102>>>
1103>>> heapq.heappush(heap, 5)
1104>>> heap = []
1105>>> for item in [3, 7, 5, 11, 1]:
1106... heapq.heappush(heap, item)
1107...
1108>>> heap
1109[1, 3, 5, 11, 7]
1110>>> heapq.heappop(heap)
11111
1112>>> heapq.heappop(heap)
11133
1114>>> heap
1115[5, 7, 11]
1116>>>
1117\end{verbatim}
1118
1119(Contributed by Kevin O'Connor.)
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001120
Fred Drake5c4cf152002-11-13 14:59:06 +00001121\item Two new functions in the \module{math} module,
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001122\function{degrees(\var{rads})} and \function{radians(\var{degs})},
Fred Drake5c4cf152002-11-13 14:59:06 +00001123convert between radians and degrees. Other functions in the
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001124\module{math} module such as
1125\function{math.sin()} and \function{math.cos()} have always required
1126input values measured in radians. (Contributed by Raymond Hettinger.)
1127
Andrew M. Kuchlingc309cca2002-10-10 16:04:08 +00001128\item Seven new functions, \function{getpgid()}, \function{killpg()},
1129\function{lchown()}, \function{major()}, \function{makedev()},
1130\function{minor()}, and \function{mknod()}, were added to the
1131\module{posix} module that underlies the \module{os} module.
1132(Contributed by Gustavo Niemeyer and Geert Jansen.)
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001133
Fred Drake5c4cf152002-11-13 14:59:06 +00001134\item The parser objects provided by the \module{pyexpat} module
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001135can now optionally buffer character data, resulting in fewer calls to
1136your character data handler and therefore faster performance. Setting
Fred Drake5c4cf152002-11-13 14:59:06 +00001137the parser object's \member{buffer_text} attribute to \constant{True}
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001138will enable buffering.
1139
Andrew M. Kuchling8a61f492002-11-13 13:24:41 +00001140\item The \function{sample(\var{population}, \var{k})} function was
1141added to the \module{random} module. \var{population} is a sequence
1142containing the elements of a population, and \function{sample()}
1143chooses \var{k} elements from the population without replacing chosen
1144elements. \var{k} can be any value up to \code{len(\var{population})}.
1145For example:
1146
1147\begin{verbatim}
1148>>> pop = range(6) ; pop
1149[0, 1, 2, 3, 4, 5]
1150>>> random.sample(pop, 3) # Choose three elements
1151[0, 4, 3]
1152>>> random.sample(pop, 6) # Choose all six elements
1153[4, 5, 0, 3, 2, 1]
1154>>> random.sample(pop, 6) # Choose six again
1155[4, 2, 3, 0, 5, 1]
1156>>> random.sample(pop, 7) # Can't choose more than six
1157Traceback (most recent call last):
Andrew M. Kuchling28f2f882002-11-14 14:14:16 +00001158 File "<stdin>", line 1, in ?
1159 File "random.py", line 396, in sample
1160 raise ValueError, "sample larger than population"
Andrew M. Kuchling8a61f492002-11-13 13:24:41 +00001161ValueError: sample larger than population
1162>>>
1163\end{verbatim}
1164
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001165\item The \module{readline} module also gained a number of new
1166functions: \function{get_history_item()},
1167\function{get_current_history_length()}, and \function{redisplay()}.
1168
1169\item Support for more advanced POSIX signal handling was added
1170to the \module{signal} module by adding the \function{sigpending},
1171\function{sigprocmask} and \function{sigsuspend} functions, where supported
1172by the platform. These functions make it possible to avoid some previously
1173unavoidable race conditions.
1174
1175\item The \module{socket} module now supports timeouts. You
1176can call the \method{settimeout(\var{t})} method on a socket object to
1177set a timeout of \var{t} seconds. Subsequent socket operations that
1178take longer than \var{t} seconds to complete will abort and raise a
Fred Drake5c4cf152002-11-13 14:59:06 +00001179\exception{socket.error} exception.
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +00001180
1181The original timeout implementation was by Tim O'Malley. Michael
1182Gilfix integrated it into the Python \module{socket} module, after the
1183patch had undergone a lengthy review. After it was checked in, Guido
1184van~Rossum rewrote parts of it. This is a good example of the free
1185software development process in action.
1186
Fred Drake5c4cf152002-11-13 14:59:06 +00001187\item The value of the C \constant{PYTHON_API_VERSION} macro is now exposed
Fred Drake583db0d2002-09-14 02:03:25 +00001188at the Python level as \code{sys.api_version}.
Andrew M. Kuchlingdcfd8252002-09-13 22:21:42 +00001189
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +00001190\item The new \module{textwrap} module contains functions for wrapping
Andrew M. Kuchlingd003a2a2002-06-26 13:23:55 +00001191strings containing paragraphs of text. The \function{wrap(\var{text},
1192\var{width})} function takes a string and returns a list containing
1193the text split into lines of no more than the chosen width. The
1194\function{fill(\var{text}, \var{width})} function returns a single
1195string, reformatted to fit into lines no longer than the chosen width.
1196(As you can guess, \function{fill()} is built on top of
1197\function{wrap()}. For example:
1198
1199\begin{verbatim}
1200>>> import textwrap
1201>>> paragraph = "Not a whit, we defy augury: ... more text ..."
1202>>> textwrap.wrap(paragraph, 60)
Fred Drake5c4cf152002-11-13 14:59:06 +00001203["Not a whit, we defy augury: there's a special providence in",
1204 "the fall of a sparrow. If it be now, 'tis not to come; if it",
Andrew M. Kuchlingd003a2a2002-06-26 13:23:55 +00001205 ...]
1206>>> print textwrap.fill(paragraph, 35)
1207Not a whit, we defy augury: there's
1208a special providence in the fall of
1209a sparrow. If it be now, 'tis not
1210to come; if it be not to come, it
1211will be now; if it be not now, yet
1212it will come: the readiness is all.
Fred Drake5c4cf152002-11-13 14:59:06 +00001213>>>
Andrew M. Kuchlingd003a2a2002-06-26 13:23:55 +00001214\end{verbatim}
1215
1216The module also contains a \class{TextWrapper} class that actually
Fred Drake5c4cf152002-11-13 14:59:06 +00001217implements the text wrapping strategy. Both the
Andrew M. Kuchlingd003a2a2002-06-26 13:23:55 +00001218\class{TextWrapper} class and the \function{wrap()} and
1219\function{fill()} functions support a number of additional keyword
1220arguments for fine-tuning the formatting; consult the module's
Fred Drake5c4cf152002-11-13 14:59:06 +00001221documentation for details.
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +00001222%XXX add a link to the module docs?
Andrew M. Kuchlingd003a2a2002-06-26 13:23:55 +00001223(Contributed by Greg Ward.)
1224
Andrew M. Kuchlingef5d06b2002-07-22 19:21:06 +00001225\item The \module{time} module's \function{strptime()} function has
Fred Drake5c4cf152002-11-13 14:59:06 +00001226long been an annoyance because it uses the platform C library's
Andrew M. Kuchlingef5d06b2002-07-22 19:21:06 +00001227\function{strptime()} implementation, and different platforms
1228sometimes have odd bugs. Brett Cannon contributed a portable
1229implementation that's written in pure Python, which should behave
1230identically on all platforms.
1231
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +00001232\item The DOM implementation
1233in \module{xml.dom.minidom} can now generate XML output in a
1234particular encoding, by specifying an optional encoding argument to
1235the \method{toxml()} and \method{toprettyxml()} methods of DOM nodes.
1236
Andrew M. Kuchlingbc5e3cc2002-11-05 00:26:33 +00001237\item The \function{*stat()} family of functions can now report
1238fractions of a second in a timestamp. Such time stamps are
1239represented as floats, similar to \function{time.time()}.
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001240
Andrew M. Kuchlingbc5e3cc2002-11-05 00:26:33 +00001241During testing, it was found that some applications will break if time
1242stamps are floats. For compatibility, when using the tuple interface
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001243of the \class{stat_result}, time stamps are represented as integers.
Andrew M. Kuchlingbc5e3cc2002-11-05 00:26:33 +00001244When using named fields (a feature first introduced in Python 2.2),
1245time stamps are still represented as ints, unless
1246\function{os.stat_float_times()} is invoked to enable float return
1247values:
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001248
1249\begin{verbatim}
Andrew M. Kuchlingbc5e3cc2002-11-05 00:26:33 +00001250>>> os.stat("/tmp").st_mtime
12511034791200
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001252>>> os.stat_float_times(True)
1253>>> os.stat("/tmp").st_mtime
12541034791200.6335014
1255\end{verbatim}
1256
Andrew M. Kuchlingbc5e3cc2002-11-05 00:26:33 +00001257In Python 2.4, the default will change to always returning floats.
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001258
1259Application developers should use this feature only if all their
1260libraries work properly when confronted with floating point time
Andrew M. Kuchlingbc5e3cc2002-11-05 00:26:33 +00001261stamps, or if they use the tuple API. If used, the feature should be
1262activated on an application level instead of trying to enable it on a
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001263per-use basis.
1264
Andrew M. Kuchling821013e2002-05-06 17:46:39 +00001265\end{itemize}
1266
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00001267
Andrew M. Kuchlingef5d06b2002-07-22 19:21:06 +00001268%======================================================================
Andrew M. Kuchling24d5a522002-11-14 23:40:42 +00001269\subsection{The \module{optparse} Module}
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +00001270
Andrew M. Kuchling24d5a522002-11-14 23:40:42 +00001271The \module{getopt} module provides simple parsing of command-line
1272arguments. The new \module{optparse} module (originally named Optik)
1273provides more elaborate command-line parsing that follows the Unix
1274conventions, automatically creates the output for \longprogramopt{help},
1275and can perform different actions
1276
1277You start by creating an instance of \class{OptionParser} and telling
1278it what your program's options are.
1279
1280\begin{verbatim}
1281from optparse import OptionParser
1282
1283op = OptionParser()
1284op.add_option('-i', '--input',
1285 action='store', type='string', dest='input',
1286 help='set input filename')
1287op.add_option('-l', '--length',
1288 action='store', type='int', dest='length',
1289 help='set maximum length of output')
1290\end{verbatim}
1291
1292Parsing a command line is then done by calling the \method{parse_args()}
1293method.
1294
1295\begin{verbatim}
1296options, args = op.parse_args(sys.argv[1:])
1297print options
1298print args
1299\end{verbatim}
1300
1301This returns an object containing all of the option values,
1302and a list of strings containing the remaining arguments.
1303
1304Invoking the script with the various arguments now works as you'd
1305expect it to. Note that the length argument is automatically
1306converted to an integer.
1307
1308\begin{verbatim}
1309$ ./python opt.py -i data arg1
1310<Values at 0x400cad4c: {'input': 'data', 'length': None}>
1311['arg1']
1312$ ./python opt.py --input=data --length=4
1313<Values at 0x400cad2c: {'input': 'data', 'length': 4}>
1314['arg1']
1315$
1316\end{verbatim}
1317
1318The help message is automatically generated for you:
1319
1320\begin{verbatim}
1321$ ./python opt.py --help
1322usage: opt.py [options]
1323
1324options:
1325 -h, --help show this help message and exit
1326 -iINPUT, --input=INPUT
1327 set input filename
1328 -lLENGTH, --length=LENGTH
1329 set maximum length of output
1330$
1331\end{verbatim}
1332
1333Optik was written by Greg Ward, with suggestions from the readers of
1334the Getopt SIG.
1335
1336\begin{seealso}
1337\seeurl{http://optik.sourceforge.net}
1338{The Optik site has tutorial and reference documentation for
1339\module{optparse}.
1340% XXX change to point to Python docs, when those docs get written.
1341}
1342\end{seealso}
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +00001343
1344
1345%======================================================================
Andrew M. Kuchlingef5d06b2002-07-22 19:21:06 +00001346\section{Specialized Object Allocator (pymalloc)\label{section-pymalloc}}
1347
1348An experimental feature added to Python 2.1 was a specialized object
1349allocator called pymalloc, written by Vladimir Marangozov. Pymalloc
1350was intended to be faster than the system \cfunction{malloc()} and have
1351less memory overhead for typical allocation patterns of Python
1352programs. The allocator uses C's \cfunction{malloc()} function to get
1353large pools of memory, and then fulfills smaller memory requests from
1354these pools.
1355
1356In 2.1 and 2.2, pymalloc was an experimental feature and wasn't
1357enabled by default; you had to explicitly turn it on by providing the
1358\longprogramopt{with-pymalloc} option to the \program{configure}
1359script. In 2.3, pymalloc has had further enhancements and is now
1360enabled by default; you'll have to supply
1361\longprogramopt{without-pymalloc} to disable it.
1362
1363This change is transparent to code written in Python; however,
1364pymalloc may expose bugs in C extensions. Authors of C extension
1365modules should test their code with the object allocator enabled,
1366because some incorrect code may cause core dumps at runtime. There
1367are a bunch of memory allocation functions in Python's C API that have
1368previously been just aliases for the C library's \cfunction{malloc()}
1369and \cfunction{free()}, meaning that if you accidentally called
1370mismatched functions, the error wouldn't be noticeable. When the
1371object allocator is enabled, these functions aren't aliases of
1372\cfunction{malloc()} and \cfunction{free()} any more, and calling the
1373wrong function to free memory may get you a core dump. For example,
1374if memory was allocated using \cfunction{PyObject_Malloc()}, it has to
1375be freed using \cfunction{PyObject_Free()}, not \cfunction{free()}. A
1376few modules included with Python fell afoul of this and had to be
1377fixed; doubtless there are more third-party modules that will have the
1378same problem.
1379
1380As part of this change, the confusing multiple interfaces for
1381allocating memory have been consolidated down into two API families.
1382Memory allocated with one family must not be manipulated with
1383functions from the other family.
1384
1385There is another family of functions specifically for allocating
1386Python \emph{objects} (as opposed to memory).
1387
1388\begin{itemize}
1389 \item To allocate and free an undistinguished chunk of memory use
1390 the ``raw memory'' family: \cfunction{PyMem_Malloc()},
1391 \cfunction{PyMem_Realloc()}, and \cfunction{PyMem_Free()}.
1392
1393 \item The ``object memory'' family is the interface to the pymalloc
1394 facility described above and is biased towards a large number of
1395 ``small'' allocations: \cfunction{PyObject_Malloc},
1396 \cfunction{PyObject_Realloc}, and \cfunction{PyObject_Free}.
1397
1398 \item To allocate and free Python objects, use the ``object'' family
1399 \cfunction{PyObject_New()}, \cfunction{PyObject_NewVar()}, and
1400 \cfunction{PyObject_Del()}.
1401\end{itemize}
1402
1403Thanks to lots of work by Tim Peters, pymalloc in 2.3 also provides
1404debugging features to catch memory overwrites and doubled frees in
1405both extension modules and in the interpreter itself. To enable this
1406support, turn on the Python interpreter's debugging code by running
Fred Drake5c4cf152002-11-13 14:59:06 +00001407\program{configure} with \longprogramopt{with-pydebug}.
Andrew M. Kuchlingef5d06b2002-07-22 19:21:06 +00001408
1409To aid extension writers, a header file \file{Misc/pymemcompat.h} is
1410distributed with the source to Python 2.3 that allows Python
1411extensions to use the 2.3 interfaces to memory allocation and compile
1412against any version of Python since 1.5.2. You would copy the file
1413from Python's source distribution and bundle it with the source of
1414your extension.
1415
1416\begin{seealso}
1417
1418\seeurl{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/python/python/dist/src/Objects/obmalloc.c}
1419{For the full details of the pymalloc implementation, see
1420the comments at the top of the file \file{Objects/obmalloc.c} in the
1421Python source code. The above link points to the file within the
1422SourceForge CVS browser.}
1423
1424\end{seealso}
1425
1426
Andrew M. Kuchling821013e2002-05-06 17:46:39 +00001427% ======================================================================
1428\section{Build and C API Changes}
1429
Andrew M. Kuchling3c305d92002-07-22 18:50:11 +00001430Changes to Python's build process and to the C API include:
Andrew M. Kuchling821013e2002-05-06 17:46:39 +00001431
1432\begin{itemize}
1433
Andrew M. Kuchlingef5d06b2002-07-22 19:21:06 +00001434\item The C-level interface to the garbage collector has been changed,
1435to make it easier to write extension types that support garbage
1436collection, and to make it easier to debug misuses of the functions.
1437Various functions have slightly different semantics, so a bunch of
1438functions had to be renamed. Extensions that use the old API will
1439still compile but will \emph{not} participate in garbage collection,
1440so updating them for 2.3 should be considered fairly high priority.
1441
1442To upgrade an extension module to the new API, perform the following
1443steps:
1444
1445\begin{itemize}
1446
1447\item Rename \cfunction{Py_TPFLAGS_GC} to \cfunction{PyTPFLAGS_HAVE_GC}.
1448
1449\item Use \cfunction{PyObject_GC_New} or \cfunction{PyObject_GC_NewVar} to
1450allocate objects, and \cfunction{PyObject_GC_Del} to deallocate them.
1451
1452\item Rename \cfunction{PyObject_GC_Init} to \cfunction{PyObject_GC_Track} and
1453\cfunction{PyObject_GC_Fini} to \cfunction{PyObject_GC_UnTrack}.
1454
1455\item Remove \cfunction{PyGC_HEAD_SIZE} from object size calculations.
1456
1457\item Remove calls to \cfunction{PyObject_AS_GC} and \cfunction{PyObject_FROM_GC}.
1458
1459\end{itemize}
1460
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001461\item Python can now optionally be built as a shared library
1462(\file{libpython2.3.so}) by supplying \longprogramopt{enable-shared}
Fred Drake5c4cf152002-11-13 14:59:06 +00001463when running Python's \program{configure} script. (Contributed by Ondrej
Andrew M. Kuchlingfad2f592002-05-10 21:00:05 +00001464Palkovsky.)
Andrew M. Kuchlingf4dd65d2002-04-01 19:28:09 +00001465
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001466\item The \csimplemacro{DL_EXPORT} and \csimplemacro{DL_IMPORT} macros
1467are now deprecated. Initialization functions for Python extension
1468modules should now be declared using the new macro
Andrew M. Kuchling3c305d92002-07-22 18:50:11 +00001469\csimplemacro{PyMODINIT_FUNC}, while the Python core will generally
1470use the \csimplemacro{PyAPI_FUNC} and \csimplemacro{PyAPI_DATA}
1471macros.
Neal Norwitzbba23a82002-07-22 13:18:59 +00001472
Fred Drake5c4cf152002-11-13 14:59:06 +00001473\item The interpreter can be compiled without any docstrings for
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +00001474the built-in functions and modules by supplying
Fred Drake5c4cf152002-11-13 14:59:06 +00001475\longprogramopt{without-doc-strings} to the \program{configure} script.
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +00001476This makes the Python executable about 10\% smaller, but will also
1477mean that you can't get help for Python's built-ins. (Contributed by
1478Gustavo Niemeyer.)
1479
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +00001480\item The cycle detection implementation used by the garbage collection
1481has proven to be stable, so it's now being made mandatory; you can no
1482longer compile Python without it, and the
Fred Drake5c4cf152002-11-13 14:59:06 +00001483\longprogramopt{with-cycle-gc} switch to \program{configure} has been removed.
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +00001484
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001485\item The \cfunction{PyArg_NoArgs()} macro is now deprecated, and code
Andrew M. Kuchling7845e7c2002-07-11 19:27:46 +00001486that uses it should be changed. For Python 2.2 and later, the method
1487definition table can specify the
Fred Drake5c4cf152002-11-13 14:59:06 +00001488\constant{METH_NOARGS} flag, signalling that there are no arguments, and
Andrew M. Kuchling7845e7c2002-07-11 19:27:46 +00001489the argument checking can then be removed. If compatibility with
1490pre-2.2 versions of Python is important, the code could use
Fred Drake5c4cf152002-11-13 14:59:06 +00001491\code{PyArg_ParseTuple(args, "")} instead, but this will be slower
Andrew M. Kuchling7845e7c2002-07-11 19:27:46 +00001492than using \constant{METH_NOARGS}.
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00001493
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001494\item A new function, \cfunction{PyObject_DelItemString(\var{mapping},
1495char *\var{key})} was added
Fred Drake5c4cf152002-11-13 14:59:06 +00001496as shorthand for
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001497\code{PyObject_DelItem(\var{mapping}, PyString_New(\var{key})}.
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00001498
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +00001499\item The \method{xreadlines()} method of file objects, introduced in
1500Python 2.1, is no longer necessary because files now behave as their
1501own iterator. \method{xreadlines()} was originally introduced as a
1502faster way to loop over all the lines in a file, but now you can
1503simply write \code{for line in file_obj}.
1504
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001505\item File objects now manage their internal string buffer
Fred Drake5c4cf152002-11-13 14:59:06 +00001506differently by increasing it exponentially when needed.
1507This results in the benchmark tests in \file{Lib/test/test_bufio.py}
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001508speeding up from 57 seconds to 1.7 seconds, according to one
1509measurement.
1510
Andrew M. Kuchling72b58e02002-05-29 17:30:34 +00001511\item It's now possible to define class and static methods for a C
1512extension type by setting either the \constant{METH_CLASS} or
1513\constant{METH_STATIC} flags in a method's \ctype{PyMethodDef}
1514structure.
Andrew M. Kuchling45afd542002-04-02 14:25:25 +00001515
Andrew M. Kuchling346386f2002-07-12 20:24:42 +00001516\item Python now includes a copy of the Expat XML parser's source code,
1517removing any dependence on a system version or local installation of
Fred Drake5c4cf152002-11-13 14:59:06 +00001518Expat.
Andrew M. Kuchling346386f2002-07-12 20:24:42 +00001519
Andrew M. Kuchling821013e2002-05-06 17:46:39 +00001520\end{itemize}
1521
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +00001522
1523%======================================================================
Andrew M. Kuchling821013e2002-05-06 17:46:39 +00001524\subsection{Port-Specific Changes}
1525
Andrew M. Kuchling187b1d82002-05-29 19:20:57 +00001526Support for a port to IBM's OS/2 using the EMX runtime environment was
1527merged into the main Python source tree. EMX is a POSIX emulation
1528layer over the OS/2 system APIs. The Python port for EMX tries to
1529support all the POSIX-like capability exposed by the EMX runtime, and
1530mostly succeeds; \function{fork()} and \function{fcntl()} are
1531restricted by the limitations of the underlying emulation layer. The
1532standard OS/2 port, which uses IBM's Visual Age compiler, also gained
1533support for case-sensitive import semantics as part of the integration
1534of the EMX port into CVS. (Contributed by Andrew MacIntyre.)
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00001535
Andrew M. Kuchling72b58e02002-05-29 17:30:34 +00001536On MacOS, most toolbox modules have been weaklinked to improve
1537backward compatibility. This means that modules will no longer fail
1538to load if a single routine is missing on the curent OS version.
Andrew M. Kuchling187b1d82002-05-29 19:20:57 +00001539Instead calling the missing routine will raise an exception.
1540(Contributed by Jack Jansen.)
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00001541
Andrew M. Kuchling187b1d82002-05-29 19:20:57 +00001542The RPM spec files, found in the \file{Misc/RPM/} directory in the
1543Python source distribution, were updated for 2.3. (Contributed by
1544Sean Reifschneider.)
Fred Drake03e10312002-03-26 19:17:43 +00001545
Andrew M. Kuchling3e3e1292002-10-10 11:32:30 +00001546Python now supports AtheOS (\url{http://www.atheos.cx}) and GNU/Hurd.
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +00001547
Fred Drake03e10312002-03-26 19:17:43 +00001548
1549%======================================================================
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001550\section{Other Changes and Fixes}
1551
Andrew M. Kuchling7a82b8c2002-11-04 20:17:24 +00001552As usual, there were a bunch of other improvements and bugfixes
1553scattered throughout the source tree. A search through the CVS change
1554logs finds there were 289 patches applied and 323 bugs fixed between
1555Python 2.2 and 2.3. Both figures are likely to be underestimates.
1556
1557Some of the more notable changes are:
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001558
1559\begin{itemize}
1560
1561\item The tools used to build the documentation now work under Cygwin
1562as well as \UNIX.
1563
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001564\item The \code{SET_LINENO} opcode has been removed. Back in the
1565mists of time, this opcode was needed to produce line numbers in
1566tracebacks and support trace functions (for, e.g., \module{pdb}).
1567Since Python 1.5, the line numbers in tracebacks have been computed
1568using a different mechanism that works with ``python -O''. For Python
15692.3 Michael Hudson implemented a similar scheme to determine when to
1570call the trace function, removing the need for \code{SET_LINENO}
1571entirely.
1572
Andrew M. Kuchling7a82b8c2002-11-04 20:17:24 +00001573It would be difficult to detect any resulting difference from Python
1574code, apart from a slight speed up when Python is run without
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001575\programopt{-O}.
1576
1577C extensions that access the \member{f_lineno} field of frame objects
1578should instead call \code{PyCode_Addr2Line(f->f_code, f->f_lasti)}.
1579This will have the added effect of making the code work as desired
1580under ``python -O'' in earlier versions of Python.
1581
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001582\end{itemize}
1583
Andrew M. Kuchling187b1d82002-05-29 19:20:57 +00001584
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001585%======================================================================
Andrew M. Kuchling950725f2002-08-06 01:40:48 +00001586\section{Porting to Python 2.3}
1587
Andrew M. Kuchling8a61f492002-11-13 13:24:41 +00001588This section lists changes that may actually require changes to your code:
1589
1590\begin{itemize}
1591
1592\item \keyword{yield} is now always a keyword; if it's used as a
1593variable name in your code, a different name must be chosen.
1594
1595\item You can no longer disable assertions by assigning to \code{__debug__}.
1596
1597\item Using \code{None} as a variable name will now result in a
Fred Drake5c4cf152002-11-13 14:59:06 +00001598\exception{SyntaxWarning} warning.
Andrew M. Kuchling8a61f492002-11-13 13:24:41 +00001599
1600\item Names of extension types defined by the modules included with
Fred Drake5c4cf152002-11-13 14:59:06 +00001601Python now contain the module and a \character{.} in front of the type
1602name.
Andrew M. Kuchling8a61f492002-11-13 13:24:41 +00001603
1604\item For strings \var{X} and \var{Y}, \code{\var{X} in \var{Y}} now works
1605if \var{X} is more than one character long.
1606
1607\item The Distutils \function{setup()} function has gained various new
Fred Drake5c4cf152002-11-13 14:59:06 +00001608keyword arguments such as \var{depends}. Old versions of the
Andrew M. Kuchling8a61f492002-11-13 13:24:41 +00001609Distutils will abort if passed unknown keywords. The fix is to check
1610for the presence of the new \function{get_distutil_options()} function
1611in your \file{setup.py} if you want to only support the new keywords
1612with a version of the Distutils that supports them:
1613
1614\begin{verbatim}
1615from distutils import core
1616
1617kw = {'sources': 'foo.c', ...}
1618if hasattr(core, 'get_distutil_options'):
1619 kw['depends'] = ['foo.h']
Fred Drake5c4cf152002-11-13 14:59:06 +00001620ext = Extension(**kw)
Andrew M. Kuchling8a61f492002-11-13 13:24:41 +00001621\end{verbatim}
1622
1623\end{itemize}
Andrew M. Kuchling950725f2002-08-06 01:40:48 +00001624
1625
1626%======================================================================
Fred Drake03e10312002-03-26 19:17:43 +00001627\section{Acknowledgements \label{acks}}
1628
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00001629The author would like to thank the following people for offering
1630suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchling366c10c2002-11-14 23:07:57 +00001631article: Simon Brunning, Michael Chermside, Scott David Daniels,
1632Fred~L. Drake, Jr., Michael Hudson, Detlef Lannert, Martin von
1633L\"owis, Andrew MacIntyre, Lalo Martins, Gustavo Niemeyer, Neal
1634Norwitz, Neil Schemenauer, Jason Tishler.
Fred Drake03e10312002-03-26 19:17:43 +00001635
1636\end{document}