blob: f919d7ee518ceb8b248a5a087874f80f9f734f81 [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}
7\authoraddress{\email{akuchlin@mems-exchange.org}}
Fred Drake03e10312002-03-26 19:17:43 +00008
9\begin{document}
10\maketitle
11\tableofcontents
12
Andrew M. Kuchlingf70a0a82002-06-10 13:22:46 +000013% Optik (or whatever it gets called)
14%
Andrew M. Kuchlingc61ec522002-08-04 01:20:05 +000015% MacOS framework-related changes (section of its own, probably)
16%
Andrew M. Kuchling950725f2002-08-06 01:40:48 +000017% New sorting code
Andrew M. Kuchling90e9a792002-08-15 00:40:21 +000018%
19% Karatsuba multiplication for long ints (#560379)
20%
21% xreadlines obsolete; files are their own iterator
Andrew M. Kuchlingf70a0a82002-06-10 13:22:46 +000022
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +000023%\section{Introduction \label{intro}}
24
25{\large This article is a draft, and is currently up to date for some
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +000026random version of the CVS tree around mid-July 2002. Please send any
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +000027additions, comments or errata to the author.}
28
29This article explains the new features in Python 2.3. The tentative
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +000030release date of Python 2.3 is currently scheduled for some undefined
31time before the end of 2002.
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +000032
33This article doesn't attempt to provide a complete specification of
34the new features, but instead provides a convenient overview. For
35full details, you should refer to the documentation for Python 2.3,
36such as the
37\citetitle[http://www.python.org/doc/2.3/lib/lib.html]{Python Library
38Reference} and the
39\citetitle[http://www.python.org/doc/2.3/ref/ref.html]{Python
40Reference Manual}. If you want to understand the complete
41implementation and design rationale for a change, refer to the PEP for
42a particular new feature.
Fred Drake03e10312002-03-26 19:17:43 +000043
44
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +000045%======================================================================
Andrew M. Kuchling517109b2002-05-07 21:01:16 +000046\section{PEP 255: Simple Generators\label{section-generators}}
Andrew M. Kuchlingf4dd65d2002-04-01 19:28:09 +000047
48In Python 2.2, generators were added as an optional feature, to be
49enabled by a \code{from __future__ import generators} directive. In
502.3 generators no longer need to be specially enabled, and are now
51always present; this means that \keyword{yield} is now always a
52keyword. The rest of this section is a copy of the description of
53generators from the ``What's New in Python 2.2'' document; if you read
54it when 2.2 came out, you can skip the rest of this section.
55
Andrew M. Kuchling517109b2002-05-07 21:01:16 +000056You're doubtless familiar with how function calls work in Python or C.
57When you call a function, it gets a private namespace where its local
Andrew M. Kuchlingf4dd65d2002-04-01 19:28:09 +000058variables are created. When the function reaches a \keyword{return}
59statement, the local variables are destroyed and the resulting value
60is returned to the caller. A later call to the same function will get
Andrew M. Kuchling517109b2002-05-07 21:01:16 +000061a fresh new set of local variables. But, what if the local variables
Andrew M. Kuchlingf4dd65d2002-04-01 19:28:09 +000062weren't thrown away on exiting a function? What if you could later
63resume the function where it left off? This is what generators
64provide; they can be thought of as resumable functions.
65
66Here's the simplest example of a generator function:
67
68\begin{verbatim}
69def generate_ints(N):
70 for i in range(N):
71 yield i
72\end{verbatim}
73
74A new keyword, \keyword{yield}, was introduced for generators. Any
75function containing a \keyword{yield} statement is a generator
76function; this is detected by Python's bytecode compiler which
77compiles the function specially as a result.
78
79When you call a generator function, it doesn't return a single value;
80instead it returns a generator object that supports the iterator
81protocol. On executing the \keyword{yield} statement, the generator
82outputs the value of \code{i}, similar to a \keyword{return}
83statement. The big difference between \keyword{yield} and a
84\keyword{return} statement is that on reaching a \keyword{yield} the
85generator's state of execution is suspended and local variables are
86preserved. On the next call to the generator's \code{.next()} method,
87the function will resume executing immediately after the
88\keyword{yield} statement. (For complicated reasons, the
89\keyword{yield} statement isn't allowed inside the \keyword{try} block
90of a \code{try...finally} statement; read \pep{255} for a full
91explanation of the interaction between \keyword{yield} and
92exceptions.)
93
94Here's a sample usage of the \function{generate_ints} generator:
95
96\begin{verbatim}
97>>> gen = generate_ints(3)
98>>> gen
99<generator object at 0x8117f90>
100>>> gen.next()
1010
102>>> gen.next()
1031
104>>> gen.next()
1052
106>>> gen.next()
107Traceback (most recent call last):
Andrew M. Kuchling9f6e1042002-06-17 13:40:04 +0000108 File "stdin", line 1, in ?
109 File "stdin", line 2, in generate_ints
Andrew M. Kuchlingf4dd65d2002-04-01 19:28:09 +0000110StopIteration
111\end{verbatim}
112
113You could equally write \code{for i in generate_ints(5)}, or
114\code{a,b,c = generate_ints(3)}.
115
116Inside a generator function, the \keyword{return} statement can only
117be used without a value, and signals the end of the procession of
118values; afterwards the generator cannot return any further values.
119\keyword{return} with a value, such as \code{return 5}, is a syntax
120error inside a generator function. The end of the generator's results
121can also be indicated by raising \exception{StopIteration} manually,
122or by just letting the flow of execution fall off the bottom of the
123function.
124
125You could achieve the effect of generators manually by writing your
126own class and storing all the local variables of the generator as
127instance variables. For example, returning a list of integers could
128be done by setting \code{self.count} to 0, and having the
129\method{next()} method increment \code{self.count} and return it.
130However, for a moderately complicated generator, writing a
131corresponding class would be much messier.
132\file{Lib/test/test_generators.py} contains a number of more
133interesting examples. The simplest one implements an in-order
134traversal of a tree using generators recursively.
135
136\begin{verbatim}
137# A recursive generator that generates Tree leaves in in-order.
138def inorder(t):
139 if t:
140 for x in inorder(t.left):
141 yield x
142 yield t.label
143 for x in inorder(t.right):
144 yield x
145\end{verbatim}
146
147Two other examples in \file{Lib/test/test_generators.py} produce
148solutions for the N-Queens problem (placing $N$ queens on an $NxN$
149chess board so that no queen threatens another) and the Knight's Tour
150(a route that takes a knight to every square of an $NxN$ chessboard
151without visiting any square twice).
152
153The idea of generators comes from other programming languages,
154especially Icon (\url{http://www.cs.arizona.edu/icon/}), where the
155idea of generators is central. In Icon, every
156expression and function call behaves like a generator. One example
157from ``An Overview of the Icon Programming Language'' at
158\url{http://www.cs.arizona.edu/icon/docs/ipd266.htm} gives an idea of
159what this looks like:
160
161\begin{verbatim}
162sentence := "Store it in the neighboring harbor"
163if (i := find("or", sentence)) > 5 then write(i)
164\end{verbatim}
165
166In Icon the \function{find()} function returns the indexes at which the
167substring ``or'' is found: 3, 23, 33. In the \keyword{if} statement,
168\code{i} is first assigned a value of 3, but 3 is less than 5, so the
169comparison fails, and Icon retries it with the second value of 23. 23
170is greater than 5, so the comparison now succeeds, and the code prints
171the value 23 to the screen.
172
173Python doesn't go nearly as far as Icon in adopting generators as a
174central concept. Generators are considered a new part of the core
175Python language, but learning or using them isn't compulsory; if they
176don't solve any problems that you have, feel free to ignore them.
177One novel feature of Python's interface as compared to
178Icon's is that a generator's state is represented as a concrete object
179(the iterator) that can be passed around to other functions or stored
180in a data structure.
181
182\begin{seealso}
183
184\seepep{255}{Simple Generators}{Written by Neil Schemenauer, Tim
185Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer
186and Tim Peters, with other fixes from the Python Labs crew.}
187
188\end{seealso}
189
190
191%======================================================================
Andrew M. Kuchling950725f2002-08-06 01:40:48 +0000192\section{PEP 263: \label{section-encodings}}
193
194Python source files can now be declared as being in different
195character set encodings. Encodings are declared by including a
196specially formatted comment in the first or second line of the source
197file. For example, a UTF-8 file can be declared with:
198
199\begin{verbatim}
200#!/usr/bin/env python
201# -*- coding: UTF-8 -*-
202\end{verbatim}
203
204Without such an encoding declaration, the default encoding used is
205ISO-8859-1, also known as Latin1.
206
207The encoding declaration only affects Unicode string literals; the
208text in the source code will be converted to Unicode using the
209specified encoding. Note that Python identifiers are still restricted
210to ASCII characters, so you can't have variable names that use
211characters outside of the usual alphanumerics.
212
213\begin{seealso}
214
215\seepep{263}{Defining Python Source Code Encodings}{Written by
216Marc-Andr\'e Lemburg and Martin von L\"owis; implemented by Martin von
217L\"owis.}
218
219\end{seealso}
220
221
222%======================================================================
Andrew M. Kuchlingf3676512002-04-15 02:27:55 +0000223\section{PEP 278: Universal Newline Support}
224
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000225The three major operating systems used today are Microsoft Windows,
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000226Apple's Macintosh OS, and the various \UNIX\ derivatives. A minor
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000227irritation is that these three platforms all use different characters
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000228to mark the ends of lines in text files. \UNIX\ uses character 10,
229the ASCII linefeed, while MacOS uses character 13, the ASCII carriage
230return, and Windows uses a two-character sequence of a carriage return
231plus a newline.
Andrew M. Kuchlingf3676512002-04-15 02:27:55 +0000232
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000233Python's file objects can now support end of line conventions other
234than the one followed by the platform on which Python is running.
235Opening a file with the mode \samp{U} or \samp{rU} will open a file
236for reading in universal newline mode. All three line ending
237conventions will be translated to a \samp{\e n} in the strings
238returned by the various file methods such as \method{read()} and
239\method{readline()}.
Andrew M. Kuchlingf3676512002-04-15 02:27:55 +0000240
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000241Universal newline support is also used when importing modules and when
242executing a file with the \function{execfile()} function. This means
243that Python modules can be shared between all three operating systems
244without needing to convert the line-endings.
245
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000246This feature can be disabled at compile-time by specifying
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000247\longprogramopt{without-universal-newlines} when running Python's
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000248\file{configure} script.
Andrew M. Kuchlingf3676512002-04-15 02:27:55 +0000249
250\begin{seealso}
251
252\seepep{278}{Universal Newline Support}{Written
253and implemented by Jack Jansen.}
254
255\end{seealso}
256
Andrew M. Kuchlingfad2f592002-05-10 21:00:05 +0000257
258%======================================================================
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000259\section{PEP 279: The \function{enumerate()} Built-in Function\label{section-enumerate}}
Andrew M. Kuchlingfad2f592002-05-10 21:00:05 +0000260
261A new built-in function, \function{enumerate()}, will make
262certain loops a bit clearer. \code{enumerate(thing)}, where
263\var{thing} is either an iterator or a sequence, returns a iterator
264that will return \code{(0, \var{thing[0]})}, \code{(1,
265\var{thing[1]})}, \code{(2, \var{thing[2]})}, and so forth. Fairly
266often you'll see code to change every element of a list that looks
267like this:
268
269\begin{verbatim}
270for i in range(len(L)):
271 item = L[i]
272 # ... compute some result based on item ...
273 L[i] = result
274\end{verbatim}
275
276This can be rewritten using \function{enumerate()} as:
277
278\begin{verbatim}
279for i, item in enumerate(L):
280 # ... compute some result based on item ...
281 L[i] = result
282\end{verbatim}
283
284
285\begin{seealso}
286
287\seepep{279}{The enumerate() built-in function}{Written
288by Raymond D. Hettinger.}
289
290\end{seealso}
291
292
Andrew M. Kuchlingf3676512002-04-15 02:27:55 +0000293%======================================================================
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000294\section{PEP 285: The \class{bool} Type\label{section-bool}}
295
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000296A Boolean type was added to Python 2.3. Two new constants were added
297to the \module{__builtin__} module, \constant{True} and
298\constant{False}. The type object for this new type is named
299\class{bool}; the constructor for it takes any Python value and
300converts it to \constant{True} or \constant{False}.
301
302\begin{verbatim}
303>>> bool(1)
304True
305>>> bool(0)
306False
307>>> bool([])
308False
309>>> bool( (1,) )
310True
311\end{verbatim}
312
313Most of the standard library modules and built-in functions have been
314changed to return Booleans.
315
316\begin{verbatim}
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000317>>> obj = []
318>>> hasattr(obj, 'append')
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000319True
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000320>>> isinstance(obj, list)
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000321True
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000322>>> isinstance(obj, tuple)
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000323False
324\end{verbatim}
325
326Python's Booleans were added with the primary goal of making code
327clearer. For example, if you're reading a function and encounter the
328statement \code{return 1}, you might wonder whether the \samp{1}
329represents a truth value, or whether it's an index, or whether it's a
330coefficient that multiplies some other quantity. If the statement is
331\code{return True}, however, the meaning of the return value is quite
332clearly a truth value.
333
334Python's Booleans were not added for the sake of strict type-checking.
Andrew M. Kuchlinga2a206b2002-05-24 21:08:58 +0000335A very strict language such as Pascal would also prevent you
336performing arithmetic with Booleans, and would require that the
337expression in an \keyword{if} statement always evaluate to a Boolean.
338Python is not this strict, and it never will be. (\pep{285}
339explicitly says so.) So you can still use any expression in an
340\keyword{if}, even ones that evaluate to a list or tuple or some
341random object, and the Boolean type is a subclass of the
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000342\class{int} class, so arithmetic using a Boolean still works.
343
344\begin{verbatim}
345>>> True + 1
3462
347>>> False + 1
3481
349>>> False * 75
3500
351>>> True * 75
35275
353\end{verbatim}
354
355To sum up \constant{True} and \constant{False} in a sentence: they're
356alternative ways to spell the integer values 1 and 0, with the single
357difference that \function{str()} and \function{repr()} return the
358strings \samp{True} and \samp{False} instead of \samp{1} and \samp{0}.
Andrew M. Kuchling3a52ff62002-04-03 22:44:47 +0000359
360\begin{seealso}
361
362\seepep{285}{Adding a bool type}{Written and implemented by GvR.}
363
364\end{seealso}
365
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000366
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000367\section{Extended Slices\label{section-slices}}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +0000368
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000369Ever since Python 1.4, the slicing syntax has supported an optional
370third ``step'' or ``stride'' argument. For example, these are all
371legal Python syntax: \code{L[1:10:2]}, \code{L[:-1:1]},
372\code{L[::-1]}. This was added to Python included at the request of
373the developers of Numerical Python. However, the built-in sequence
374types of lists, tuples, and strings have never supported this feature,
375and you got a \exception{TypeError} if you tried it. Michael Hudson
376contributed a patch that was applied to Python 2.3 and fixed this
377shortcoming.
378
379For example, you can now easily extract the elements of a list that
380have even indexes:
Fred Drakedf872a22002-07-03 12:02:01 +0000381
382\begin{verbatim}
383>>> L = range(10)
384>>> L[::2]
385[0, 2, 4, 6, 8]
386\end{verbatim}
387
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000388Negative values also work, so you can make a copy of the same list in
389reverse order:
Fred Drakedf872a22002-07-03 12:02:01 +0000390
391\begin{verbatim}
392>>> L[::-1]
393[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
394\end{verbatim}
Andrew M. Kuchling3a52ff62002-04-03 22:44:47 +0000395
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000396This also works for strings:
397
398\begin{verbatim}
399>>> s='abcd'
400>>> s[::2]
401'ac'
402>>> s[::-1]
403'dcba'
404\end{verbatim}
405
Michael W. Hudson4da01ed2002-07-19 15:48:56 +0000406as well as tuples and arrays.
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000407
Michael W. Hudson4da01ed2002-07-19 15:48:56 +0000408If you have a mutable sequence (i.e. a list or an array) you can
409assign to or delete an extended slice, but there are some differences
410in assignment to extended and regular slices. Assignment to a regular
411slice can be used to change the length of the sequence:
412
413\begin{verbatim}
414>>> a = range(3)
415>>> a
416[0, 1, 2]
417>>> a[1:3] = [4, 5, 6]
418>>> a
419[0, 4, 5, 6]
420\end{verbatim}
421
422but when assigning to an extended slice the list on the right hand
423side of the statement must contain the same number of items as the
424slice it is replacing:
425
426\begin{verbatim}
427>>> a = range(4)
428>>> a
429[0, 1, 2, 3]
430>>> a[::2]
431[0, 2]
432>>> a[::2] = range(0, -2, -1)
433>>> a
434[0, 1, -1, 3]
435>>> a[::2] = range(3)
436Traceback (most recent call last):
437 File "<stdin>", line 1, in ?
438ValueError: attempt to assign list of size 3 to extended slice of size 2
439\end{verbatim}
440
441Deletion is more straightforward:
442
443\begin{verbatim}
444>>> a = range(4)
445>>> a[::2]
446[0, 2]
447>>> del a[::2]
448>>> a
449[1, 3]
450\end{verbatim}
451
452One can also now pass slice objects to builtin sequences
453\method{__getitem__} methods:
454
455\begin{verbatim}
456>>> range(10).__getitem__(slice(0, 5, 2))
457[0, 2, 4]
458\end{verbatim}
459
460or use them directly in subscripts:
461
462\begin{verbatim}
463>>> range(10)[slice(0, 5, 2)]
464[0, 2, 4]
465\end{verbatim}
466
467To make implementing sequences that support extended slicing in Python
468easier, slice ojects now have a method \method{indices} which given
469the length of a sequence returns \code{(start, stop, step)} handling
470omitted and out-of-bounds indices in a manner consistent with regular
471slices (and this innocuous phrase hides a welter of confusing
472details!). The method is intended to be used like this:
473
474\begin{verbatim}
475class FakeSeq:
476 ...
477 def calc_item(self, i):
478 ...
479 def __getitem__(self, item):
480 if isinstance(item, slice):
481 return FakeSeq([self.calc_item(i)
482 in range(*item.indices(len(self)))])
483 else:
484 return self.calc_item(i)
485\end{verbatim}
486
Andrew M. Kuchling90e9a792002-08-15 00:40:21 +0000487From this example you can also see that the builtin ``\class{slice}''
488object is now the type object for the slice type, and is no longer a
489function. This is consistent with Python 2.2, where \class{int},
490\class{str}, etc., underwent the same change.
491
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000492
Andrew M. Kuchling3a52ff62002-04-03 22:44:47 +0000493%======================================================================
Fred Drakedf872a22002-07-03 12:02:01 +0000494\section{Other Language Changes}
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000495
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000496Here are all of the changes that Python 2.3 makes to the core Python
497language.
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000498
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000499\begin{itemize}
500\item The \keyword{yield} statement is now always a keyword, as
501described in section~\ref{section-generators} of this document.
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000502
Andrew M. Kuchling90e9a792002-08-15 00:40:21 +0000503\item The \code{in} operator now works differently for strings.
504Previously, when evaluating \code{\var{X} in \var{Y}} where \var{X}
505and \var{Y} are strings, \var{X} could only be a single character.
506That's now changed; \var{X} can be a string of any length, and
507\code{\var{X} in \var{Y}} will return \constant{True} if \var{X} is a
508substring of \var{Y}. If \var{X} is the empty string, the result is
509always \constant{True}.
510
511\begin{verbatim}
512>>> 'ab' in 'abcd'
513True
514>>> 'ad' in 'abcd'
515False
516>>> '' in 'abcd'
517True
518\end{verbatim}
519
520Note that this doesn't tell you where the substring starts; the
521\method{find()} method is still necessary to figure that out.
522
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000523\item A new built-in function \function{enumerate()}
524was added, as described in section~\ref{section-enumerate} of this
525document.
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000526
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000527\item Two new constants, \constant{True} and \constant{False} were
528added along with the built-in \class{bool} type, as described in
529section~\ref{section-bool} of this document.
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000530
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000531\item Built-in types now support the extended slicing syntax,
532as described in section~\ref{section-slices} of this document.
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000533
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000534\item Dictionaries have a new method, \method{pop(\var{key})}, that
535returns the value corresponding to \var{key} and removes that
536key/value pair from the dictionary. \method{pop()} will raise a
537\exception{KeyError} if the requested key isn't present in the
538dictionary:
539
540\begin{verbatim}
541>>> d = {1:2}
542>>> d
543{1: 2}
544>>> d.pop(4)
545Traceback (most recent call last):
546 File ``stdin'', line 1, in ?
547KeyError: 4
548>>> d.pop(1)
5492
550>>> d.pop(1)
551Traceback (most recent call last):
552 File ``stdin'', line 1, in ?
553KeyError: pop(): dictionary is empty
554>>> d
555{}
556>>>
557\end{verbatim}
558
559(Patch contributed by Raymond Hettinger.)
560
561\item The \method{strip()}, \method{lstrip()}, and \method{rstrip()}
562string methods now have an optional argument for specifying the
563characters to strip. The default is still to remove all whitespace
564characters:
565
566\begin{verbatim}
567>>> ' abc '.strip()
568'abc'
569>>> '><><abc<><><>'.strip('<>')
570'abc'
571>>> '><><abc<><><>\n'.strip('<>')
572'abc<><><>\n'
573>>> u'\u4000\u4001abc\u4000'.strip(u'\u4000')
574u'\u4001abc'
575>>>
576\end{verbatim}
577
Andrew M. Kuchling346386f2002-07-12 20:24:42 +0000578(Contributed by Simon Brunning.)
579
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000580\item The \method{startswith()} and \method{endswith()}
581string methods now accept negative numbers for the start and end
582parameters.
583
584\item Another new string method is \method{zfill()}, originally a
585function in the \module{string} module. \method{zfill()} pads a
586numeric string with zeros on the left until it's the specified width.
587Note that the \code{\%} operator is still more flexible and powerful
588than \method{zfill()}.
589
590\begin{verbatim}
591>>> '45'.zfill(4)
592'0045'
593>>> '12345'.zfill(4)
594'12345'
595>>> 'goofy'.zfill(6)
596'0goofy'
597\end{verbatim}
598
Andrew M. Kuchling346386f2002-07-12 20:24:42 +0000599(Contributed by Walter D\"orwald.)
600
601\item The \keyword{assert} statement no longer checks the \code{__debug__}
602flag, so you can no longer disable assertions by assigning to \code{__debug__}.
603Running Python with the \programopt{-O} switch will still generate
604code that doesn't execute any assertions.
605
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +0000606\item A new type object, \class{basestring}, has been added.
607 Both 8-bit strings and Unicode strings inherit from this type, so
608 \code{isinstance(obj, basestring)} will return \constant{True} for
609 either kind of string. It's a completely abstract type, so you
610 can't create \class{basestring} instances.
611
Andrew M. Kuchling950725f2002-08-06 01:40:48 +0000612\item The \method{sort()} method of list objects has been extensively
613rewritten by Tim Peters, and the implementation is significantly
614faster.
615
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +0000616\item Most type objects are now callable, so you can use them
617to create new objects such as functions, classes, and modules. (This
618means that the \module{new} module can be deprecated in a future
619Python version, because you can now use the type objects available
620in the \module{types} module.)
621% XXX should new.py use PendingDeprecationWarning?
622For example, you can create a new module object with the following code:
623
624\begin{verbatim}
625>>> import types
626>>> m = types.ModuleType('abc','docstring')
627>>> m
628<module 'abc' (built-in)>
629>>> m.__doc__
630'docstring'
631\end{verbatim}
632
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000633\item
Fred Drakedf872a22002-07-03 12:02:01 +0000634A new warning, \exception{PendingDeprecationWarning} was added to
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +0000635indicate features which are in the process of being
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000636deprecated. The warning will \emph{not} be printed by default. To
637check for use of features that will be deprecated in the future,
638supply \programopt{-Walways::PendingDeprecationWarning::} on the
639command line or use \function{warnings.filterwarnings()}.
640
641\item One minor but far-reaching change is that the names of extension
642types defined by the modules included with Python now contain the
643module and a \samp{.} in front of the type name. For example, in
644Python 2.2, if you created a socket and printed its
645\member{__class__}, you'd get this output:
646
647\begin{verbatim}
648>>> s = socket.socket()
649>>> s.__class__
650<type 'socket'>
651\end{verbatim}
652
653In 2.3, you get this:
654\begin{verbatim}
655>>> s.__class__
656<type '_socket.socket'>
657\end{verbatim}
658
659\end{itemize}
Neal Norwitzd68f5172002-05-29 15:54:55 +0000660
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000661%======================================================================
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +0000662\section{New and Improved Modules}
663
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000664As usual, Python's standard modules had a number of enhancements and
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +0000665bug fixes. Here's a partial list of the most notable changes, sorted
666alphabetically by module name. Consult the
667\file{Misc/NEWS} file in the source tree for a more
668complete list of changes, or look through the CVS logs for all the
669details.
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000670
671\begin{itemize}
672
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +0000673\item The \module{array} module now supports arrays of Unicode
674characters using the \samp{u} format character. Arrays also now
675support using the \code{+=} assignment operator to add another array's
676contents, and the \code{*=} assignment operator to repeat an array.
677(Contributed by Jason Orendorff.)
678
679\item The Distutils \class{Extension} class now supports
680an extra constructor argument named \samp{depends} for listing
681additional source files that an extension depends on. This lets
682Distutils recompile the module if any of the dependency files are
683modified. For example, if \samp{sampmodule.c} includes the header
684file \file{sample.h}, you would create the \class{Extension} object like
685this:
686
687\begin{verbatim}
688ext = Extension("samp",
689 sources=["sampmodule.c"],
690 depends=["sample.h"])
691\end{verbatim}
692
693Modifying \file{sample.h} would then cause the module to be recompiled.
694(Contributed by Jeremy Hylton.)
695
696\item Two new binary packagers were added to the Distutils.
697\code{bdist_pkgtool} builds \file{.pkg} files to use with Solaris
698\program{pkgtool}, and \code{bdist_sdux} builds \program{swinstall}
699packages for use on HP-UX.
700An abstract binary packager class,
701\module{distutils.command.bdist_packager}, was added; this may make it
702easier to write binary packaging commands. (Contributed by Mark
703Alexander.)
704
705\item The \module{getopt} module gained a new function,
706\function{gnu_getopt()}, that supports the same arguments as the existing
707\function{getopt()} function but uses GNU-style scanning mode.
708The existing \function{getopt()} stops processing options as soon as a
709non-option argument is encountered, but in GNU-style mode processing
710continues, meaning that options and arguments can be mixed. For
711example:
712
713\begin{verbatim}
714>>> getopt.getopt(['-f', 'filename', 'output', '-v'], 'f:v')
715([('-f', 'filename')], ['output', '-v'])
716>>> getopt.gnu_getopt(['-f', 'filename', 'output', '-v'], 'f:v')
717([('-f', 'filename'), ('-v', '')], ['output'])
718\end{verbatim}
719
720(Contributed by Peter \AA{strand}.)
721
722\item The \module{grp}, \module{pwd}, and \module{resource} modules
723now return enhanced tuples:
724
725\begin{verbatim}
726>>> import grp
727>>> g = grp.getgrnam('amk')
728>>> g.gr_name, g.gr_gid
729('amk', 500)
730\end{verbatim}
731
Andrew M. Kuchling950725f2002-08-06 01:40:48 +0000732\item The new \module{heapq} module contains an implementation of a
733heap queue algorithm. A heap is an array-like data structure that
734keeps items in a sorted order such that, for every index k, heap[k] <=
735heap[2*k+1] and heap[k] <= heap[2*k+2]. This makes it quick to remove
736the smallest item, and inserting a new item while maintaining the heap
737property is O(lg~n). (See
738\url{http://www.nist.gov/dads/HTML/priorityque.html} for more
739information about the priority queue data structure.)
740
741The Python \module{heapq} module provides \function{heappush()} and
742\function{heappop()} functions for adding and removing items while
743maintaining the heap property on top of some other mutable Python
744sequence type. For example:
745
746\begin{verbatim}
747>>> import heapq
748>>> heap = []
749>>> for item in [3, 7, 5, 11, 1]:
750... heapq.heappush(heap, item)
751...
752>>> heap
753[1, 3, 5, 11, 7]
754>>> heapq.heappop(heap)
7551
756>>> heapq.heappop(heap)
7573
758>>> heap
759[5, 7, 11]
760>>>
761>>> heapq.heappush(heap, 5)
762>>> heap = []
763>>> for item in [3, 7, 5, 11, 1]:
764... heapq.heappush(heap, item)
765...
766>>> heap
767[1, 3, 5, 11, 7]
768>>> heapq.heappop(heap)
7691
770>>> heapq.heappop(heap)
7713
772>>> heap
773[5, 7, 11]
774>>>
775\end{verbatim}
776
777(Contributed by Kevin O'Connor.)
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +0000778
779\item Two new functions in the \module{math} module,
780\function{degrees(\var{rads})} and \function{radians(\var{degs})},
781convert between radians and degrees. Other functions in the
782\module{math} module such as
783\function{math.sin()} and \function{math.cos()} have always required
784input values measured in radians. (Contributed by Raymond Hettinger.)
785
Andrew M. Kuchling52f1b762002-07-28 20:29:03 +0000786\item Four new functions, \function{getpgid()}, \function{killpg()}, \function{lchown()}, and \function{mknod()}, were added to the \module{posix} module that
Andrew M. Kuchlinga982eb12002-07-22 18:57:36 +0000787underlies the \module{os} module. (Contributed by Gustavo Niemeyer
788and Geert Jansen.)
789
790\item The parser objects provided by the \module{pyexpat} module
791can now optionally buffer character data, resulting in fewer calls to
792your character data handler and therefore faster performance. Setting
793the parser object's \member{buffer_text} attribute to \constant{True}
794will enable buffering.
795
796\item The \module{readline} module also gained a number of new
797functions: \function{get_history_item()},
798\function{get_current_history_length()}, and \function{redisplay()}.
799
800\item Support for more advanced POSIX signal handling was added
801to the \module{signal} module by adding the \function{sigpending},
802\function{sigprocmask} and \function{sigsuspend} functions, where supported
803by the platform. These functions make it possible to avoid some previously
804unavoidable race conditions.
805
806\item The \module{socket} module now supports timeouts. You
807can call the \method{settimeout(\var{t})} method on a socket object to
808set a timeout of \var{t} seconds. Subsequent socket operations that
809take longer than \var{t} seconds to complete will abort and raise a
810\exception{socket.error} exception.
811
812The original timeout implementation was by Tim O'Malley. Michael
813Gilfix integrated it into the Python \module{socket} module, after the
814patch had undergone a lengthy review. After it was checked in, Guido
815van~Rossum rewrote parts of it. This is a good example of the free
816software development process in action.
817
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +0000818\item The new \module{textwrap} module contains functions for wrapping
Andrew M. Kuchlingd003a2a2002-06-26 13:23:55 +0000819strings containing paragraphs of text. The \function{wrap(\var{text},
820\var{width})} function takes a string and returns a list containing
821the text split into lines of no more than the chosen width. The
822\function{fill(\var{text}, \var{width})} function returns a single
823string, reformatted to fit into lines no longer than the chosen width.
824(As you can guess, \function{fill()} is built on top of
825\function{wrap()}. For example:
826
827\begin{verbatim}
828>>> import textwrap
829>>> paragraph = "Not a whit, we defy augury: ... more text ..."
830>>> textwrap.wrap(paragraph, 60)
831["Not a whit, we defy augury: there's a special providence in",
832 "the fall of a sparrow. If it be now, 'tis not to come; if it",
833 ...]
834>>> print textwrap.fill(paragraph, 35)
835Not a whit, we defy augury: there's
836a special providence in the fall of
837a sparrow. If it be now, 'tis not
838to come; if it be not to come, it
839will be now; if it be not now, yet
840it will come: the readiness is all.
841>>>
842\end{verbatim}
843
844The module also contains a \class{TextWrapper} class that actually
845implements the text wrapping strategy. Both the
846\class{TextWrapper} class and the \function{wrap()} and
847\function{fill()} functions support a number of additional keyword
848arguments for fine-tuning the formatting; consult the module's
849documentation for details.
850% XXX add a link to the module docs?
851(Contributed by Greg Ward.)
852
Andrew M. Kuchlingef5d06b2002-07-22 19:21:06 +0000853\item The \module{time} module's \function{strptime()} function has
854long been an annoyance because it uses the platform C library's
855\function{strptime()} implementation, and different platforms
856sometimes have odd bugs. Brett Cannon contributed a portable
857implementation that's written in pure Python, which should behave
858identically on all platforms.
859
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +0000860\item The DOM implementation
861in \module{xml.dom.minidom} can now generate XML output in a
862particular encoding, by specifying an optional encoding argument to
863the \method{toxml()} and \method{toprettyxml()} methods of DOM nodes.
864
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000865\end{itemize}
866
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +0000867
Andrew M. Kuchlingef5d06b2002-07-22 19:21:06 +0000868%======================================================================
869\section{Specialized Object Allocator (pymalloc)\label{section-pymalloc}}
870
871An experimental feature added to Python 2.1 was a specialized object
872allocator called pymalloc, written by Vladimir Marangozov. Pymalloc
873was intended to be faster than the system \cfunction{malloc()} and have
874less memory overhead for typical allocation patterns of Python
875programs. The allocator uses C's \cfunction{malloc()} function to get
876large pools of memory, and then fulfills smaller memory requests from
877these pools.
878
879In 2.1 and 2.2, pymalloc was an experimental feature and wasn't
880enabled by default; you had to explicitly turn it on by providing the
881\longprogramopt{with-pymalloc} option to the \program{configure}
882script. In 2.3, pymalloc has had further enhancements and is now
883enabled by default; you'll have to supply
884\longprogramopt{without-pymalloc} to disable it.
885
886This change is transparent to code written in Python; however,
887pymalloc may expose bugs in C extensions. Authors of C extension
888modules should test their code with the object allocator enabled,
889because some incorrect code may cause core dumps at runtime. There
890are a bunch of memory allocation functions in Python's C API that have
891previously been just aliases for the C library's \cfunction{malloc()}
892and \cfunction{free()}, meaning that if you accidentally called
893mismatched functions, the error wouldn't be noticeable. When the
894object allocator is enabled, these functions aren't aliases of
895\cfunction{malloc()} and \cfunction{free()} any more, and calling the
896wrong function to free memory may get you a core dump. For example,
897if memory was allocated using \cfunction{PyObject_Malloc()}, it has to
898be freed using \cfunction{PyObject_Free()}, not \cfunction{free()}. A
899few modules included with Python fell afoul of this and had to be
900fixed; doubtless there are more third-party modules that will have the
901same problem.
902
903As part of this change, the confusing multiple interfaces for
904allocating memory have been consolidated down into two API families.
905Memory allocated with one family must not be manipulated with
906functions from the other family.
907
908There is another family of functions specifically for allocating
909Python \emph{objects} (as opposed to memory).
910
911\begin{itemize}
912 \item To allocate and free an undistinguished chunk of memory use
913 the ``raw memory'' family: \cfunction{PyMem_Malloc()},
914 \cfunction{PyMem_Realloc()}, and \cfunction{PyMem_Free()}.
915
916 \item The ``object memory'' family is the interface to the pymalloc
917 facility described above and is biased towards a large number of
918 ``small'' allocations: \cfunction{PyObject_Malloc},
919 \cfunction{PyObject_Realloc}, and \cfunction{PyObject_Free}.
920
921 \item To allocate and free Python objects, use the ``object'' family
922 \cfunction{PyObject_New()}, \cfunction{PyObject_NewVar()}, and
923 \cfunction{PyObject_Del()}.
924\end{itemize}
925
926Thanks to lots of work by Tim Peters, pymalloc in 2.3 also provides
927debugging features to catch memory overwrites and doubled frees in
928both extension modules and in the interpreter itself. To enable this
929support, turn on the Python interpreter's debugging code by running
930\program{configure} with \longprogramopt{with-pydebug}.
931
932To aid extension writers, a header file \file{Misc/pymemcompat.h} is
933distributed with the source to Python 2.3 that allows Python
934extensions to use the 2.3 interfaces to memory allocation and compile
935against any version of Python since 1.5.2. You would copy the file
936from Python's source distribution and bundle it with the source of
937your extension.
938
939\begin{seealso}
940
941\seeurl{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/python/python/dist/src/Objects/obmalloc.c}
942{For the full details of the pymalloc implementation, see
943the comments at the top of the file \file{Objects/obmalloc.c} in the
944Python source code. The above link points to the file within the
945SourceForge CVS browser.}
946
947\end{seealso}
948
949
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000950% ======================================================================
951\section{Build and C API Changes}
952
Andrew M. Kuchling3c305d92002-07-22 18:50:11 +0000953Changes to Python's build process and to the C API include:
Andrew M. Kuchling821013e2002-05-06 17:46:39 +0000954
955\begin{itemize}
956
Andrew M. Kuchlingef5d06b2002-07-22 19:21:06 +0000957\item The C-level interface to the garbage collector has been changed,
958to make it easier to write extension types that support garbage
959collection, and to make it easier to debug misuses of the functions.
960Various functions have slightly different semantics, so a bunch of
961functions had to be renamed. Extensions that use the old API will
962still compile but will \emph{not} participate in garbage collection,
963so updating them for 2.3 should be considered fairly high priority.
964
965To upgrade an extension module to the new API, perform the following
966steps:
967
968\begin{itemize}
969
970\item Rename \cfunction{Py_TPFLAGS_GC} to \cfunction{PyTPFLAGS_HAVE_GC}.
971
972\item Use \cfunction{PyObject_GC_New} or \cfunction{PyObject_GC_NewVar} to
973allocate objects, and \cfunction{PyObject_GC_Del} to deallocate them.
974
975\item Rename \cfunction{PyObject_GC_Init} to \cfunction{PyObject_GC_Track} and
976\cfunction{PyObject_GC_Fini} to \cfunction{PyObject_GC_UnTrack}.
977
978\item Remove \cfunction{PyGC_HEAD_SIZE} from object size calculations.
979
980\item Remove calls to \cfunction{PyObject_AS_GC} and \cfunction{PyObject_FROM_GC}.
981
982\end{itemize}
983
Andrew M. Kuchling517109b2002-05-07 21:01:16 +0000984\item Python can now optionally be built as a shared library
985(\file{libpython2.3.so}) by supplying \longprogramopt{enable-shared}
Andrew M. Kuchlingfad2f592002-05-10 21:00:05 +0000986when running Python's \file{configure} script. (Contributed by Ondrej
987Palkovsky.)
Andrew M. Kuchlingf4dd65d2002-04-01 19:28:09 +0000988
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000989\item The \csimplemacro{DL_EXPORT} and \csimplemacro{DL_IMPORT} macros
990are now deprecated. Initialization functions for Python extension
991modules should now be declared using the new macro
Andrew M. Kuchling3c305d92002-07-22 18:50:11 +0000992\csimplemacro{PyMODINIT_FUNC}, while the Python core will generally
993use the \csimplemacro{PyAPI_FUNC} and \csimplemacro{PyAPI_DATA}
994macros.
Neal Norwitzbba23a82002-07-22 13:18:59 +0000995
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000996\item The interpreter can be compiled without any docstrings for
997the built-in functions and modules by supplying
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +0000998\longprogramopt{without-doc-strings} to the \file{configure} script.
Andrew M. Kuchlinge995d162002-07-11 20:09:50 +0000999This makes the Python executable about 10\% smaller, but will also
1000mean that you can't get help for Python's built-ins. (Contributed by
1001Gustavo Niemeyer.)
1002
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +00001003\item The cycle detection implementation used by the garbage collection
1004has proven to be stable, so it's now being made mandatory; you can no
1005longer compile Python without it, and the
1006\longprogramopt{with-cycle-gc} switch to \file{configure} has been removed.
1007
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001008\item The \cfunction{PyArg_NoArgs()} macro is now deprecated, and code
Andrew M. Kuchling7845e7c2002-07-11 19:27:46 +00001009that uses it should be changed. For Python 2.2 and later, the method
1010definition table can specify the
1011\constant{METH_NOARGS} flag, signalling that there are no arguments, and
1012the argument checking can then be removed. If compatibility with
1013pre-2.2 versions of Python is important, the code could use
1014\code{PyArg_ParseTuple(args, "")} instead, but this will be slower
1015than using \constant{METH_NOARGS}.
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00001016
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001017\item A new function, \cfunction{PyObject_DelItemString(\var{mapping},
1018char *\var{key})} was added
1019as shorthand for
1020\code{PyObject_DelItem(\var{mapping}, PyString_New(\var{key})}.
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00001021
Andrew M. Kuchling821013e2002-05-06 17:46:39 +00001022\item The source code for the Expat XML parser is now included with
1023the Python source, so the \module{pyexpat} module is no longer
1024dependent on having a system library containing Expat.
1025
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001026\item File objects now manage their internal string buffer
1027differently by increasing it exponentially when needed.
1028This results in the benchmark tests in \file{Lib/test/test_bufio.py}
1029speeding up from 57 seconds to 1.7 seconds, according to one
1030measurement.
1031
Andrew M. Kuchling72b58e02002-05-29 17:30:34 +00001032\item It's now possible to define class and static methods for a C
1033extension type by setting either the \constant{METH_CLASS} or
1034\constant{METH_STATIC} flags in a method's \ctype{PyMethodDef}
1035structure.
Andrew M. Kuchling45afd542002-04-02 14:25:25 +00001036
Andrew M. Kuchling346386f2002-07-12 20:24:42 +00001037\item Python now includes a copy of the Expat XML parser's source code,
1038removing any dependence on a system version or local installation of
1039Expat.
1040
Andrew M. Kuchling821013e2002-05-06 17:46:39 +00001041\end{itemize}
1042
1043\subsection{Port-Specific Changes}
1044
Andrew M. Kuchling187b1d82002-05-29 19:20:57 +00001045Support for a port to IBM's OS/2 using the EMX runtime environment was
1046merged into the main Python source tree. EMX is a POSIX emulation
1047layer over the OS/2 system APIs. The Python port for EMX tries to
1048support all the POSIX-like capability exposed by the EMX runtime, and
1049mostly succeeds; \function{fork()} and \function{fcntl()} are
1050restricted by the limitations of the underlying emulation layer. The
1051standard OS/2 port, which uses IBM's Visual Age compiler, also gained
1052support for case-sensitive import semantics as part of the integration
1053of the EMX port into CVS. (Contributed by Andrew MacIntyre.)
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00001054
Andrew M. Kuchling72b58e02002-05-29 17:30:34 +00001055On MacOS, most toolbox modules have been weaklinked to improve
1056backward compatibility. This means that modules will no longer fail
1057to load if a single routine is missing on the curent OS version.
Andrew M. Kuchling187b1d82002-05-29 19:20:57 +00001058Instead calling the missing routine will raise an exception.
1059(Contributed by Jack Jansen.)
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00001060
Andrew M. Kuchling187b1d82002-05-29 19:20:57 +00001061The RPM spec files, found in the \file{Misc/RPM/} directory in the
1062Python source distribution, were updated for 2.3. (Contributed by
1063Sean Reifschneider.)
Fred Drake03e10312002-03-26 19:17:43 +00001064
Andrew M. Kuchling20e5abc2002-07-11 20:50:34 +00001065Python now supports AtheOS (\url{www.atheos.cx}) and GNU/Hurd.
1066
Fred Drake03e10312002-03-26 19:17:43 +00001067
1068%======================================================================
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001069\section{Other Changes and Fixes}
1070
1071Finally, there are various miscellaneous fixes:
1072
1073\begin{itemize}
1074
1075\item The tools used to build the documentation now work under Cygwin
1076as well as \UNIX.
1077
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001078\item The \code{SET_LINENO} opcode has been removed. Back in the
1079mists of time, this opcode was needed to produce line numbers in
1080tracebacks and support trace functions (for, e.g., \module{pdb}).
1081Since Python 1.5, the line numbers in tracebacks have been computed
1082using a different mechanism that works with ``python -O''. For Python
10832.3 Michael Hudson implemented a similar scheme to determine when to
1084call the trace function, removing the need for \code{SET_LINENO}
1085entirely.
1086
1087Python code will be hard pushed to notice a difference from this
1088change, apart from a slight speed up when python is run without
1089\programopt{-O}.
1090
1091C extensions that access the \member{f_lineno} field of frame objects
1092should instead call \code{PyCode_Addr2Line(f->f_code, f->f_lasti)}.
1093This will have the added effect of making the code work as desired
1094under ``python -O'' in earlier versions of Python.
1095
1096To make tracing work as expected, it was found necessary to add a new
1097opcode, \cdata{RETURN_NONE}, to the VM. If you want to know why, read
1098the comments in the function \cfunction{maybe_call_line_trace} in
1099\file{Python/ceval.c}.
1100
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001101\end{itemize}
1102
Andrew M. Kuchling187b1d82002-05-29 19:20:57 +00001103
Andrew M. Kuchling517109b2002-05-07 21:01:16 +00001104%======================================================================
Andrew M. Kuchling950725f2002-08-06 01:40:48 +00001105\section{Porting to Python 2.3}
1106
1107XXX write this
1108
1109
1110%======================================================================
Fred Drake03e10312002-03-26 19:17:43 +00001111\section{Acknowledgements \label{acks}}
1112
Andrew M. Kuchling03594bb2002-03-27 02:29:48 +00001113The author would like to thank the following people for offering
1114suggestions, corrections and assistance with various drafts of this
Andrew M. Kuchling7f147a72002-06-10 18:58:19 +00001115article: Michael Chermside, Scott David Daniels, Fred~L. Drake, Jr.,
Andrew M. Kuchling7845e7c2002-07-11 19:27:46 +00001116Michael Hudson, Detlef Lannert, Martin von L\"owis, Andrew MacIntyre,
Andrew M. Kuchling950725f2002-08-06 01:40:48 +00001117Gustavo Niemeyer, Neal Norwitz, Jason Tishler.
Fred Drake03e10312002-03-26 19:17:43 +00001118
1119\end{document}