blob: fe5378be5440e462ce2b203d7f7090631daad3da [file] [log] [blame]
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +00001\documentclass{howto}
2
Andrew M. Kuchling3ad4e742000-09-27 01:33:41 +00003% $Id$
4
Andrew M. Kuchling730067e2000-06-30 01:44:05 +00005\title{What's New in Python 2.0}
Andrew M. Kuchling95467722002-05-02 14:48:26 +00006\release{1.02}
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +00007\author{A.M. Kuchling and Moshe Zadka}
Fred Drakeb914ef02004-01-02 06:57:50 +00008\authoraddress{
9 \strong{Python Software Foundation}\\
10 Email: \email{amk@amk.ca}, \email{moshez@twistedmatrix.com}
11}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000012\begin{document}
13\maketitle\tableofcontents
14
15\section{Introduction}
16
Andrew M. Kuchling91bae442002-04-18 02:18:27 +000017A new release of Python, version 2.0, was released on October 16, 2000. This
18article covers the exciting new features in 2.0, highlights some other
19useful changes, and points out a few incompatible changes that may require
Andrew M. Kuchling70ba3822000-07-01 00:13:30 +000020rewriting code.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000021
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000022Python's development never completely stops between releases, and a
23steady flow of bug fixes and improvements are always being submitted.
24A host of minor fixes, a few optimizations, additional docstrings, and
Andrew M. Kuchling730067e2000-06-30 01:44:05 +000025better error messages went into 2.0; to list them all would be
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000026impossible, but they're certainly significant. Consult the
Andrew M. Kuchlingec1722e2000-10-12 03:04:22 +000027publicly-available CVS logs if you want to see the full list. This
28progress is due to the five developers working for
29PythonLabs are now getting paid to spend their days fixing bugs,
30and also due to the improved communication resulting
31from moving to SourceForge.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000032
33% ======================================================================
Andrew M. Kuchling4d46d382000-09-06 17:58:49 +000034\section{What About Python 1.6?}
35
36Python 1.6 can be thought of as the Contractual Obligations Python
37release. After the core development team left CNRI in May 2000, CNRI
38requested that a 1.6 release be created, containing all the work on
39Python that had been performed at CNRI. Python 1.6 therefore
40represents the state of the CVS tree as of May 2000, with the most
41significant new feature being Unicode support. Development continued
42after May, of course, so the 1.6 tree received a few fixes to ensure
43that it's forward-compatible with Python 2.0. 1.6 is therefore part
44of Python's evolution, and not a side branch.
45
46So, should you take much interest in Python 1.6? Probably not. The
471.6final and 2.0beta1 releases were made on the same day (September 5,
482000), the plan being to finalize Python 2.0 within a month or so. If
49you have applications to maintain, there seems little point in
50breaking things by moving to 1.6, fixing them, and then having another
51round of breakage within a month by moving to 2.0; you're better off
52just going straight to 2.0. Most of the really interesting features
53described in this document are only in 2.0, because a lot of work was
54done between May and September.
55
56% ======================================================================
Andrew M. Kuchlingbe870dd2000-09-27 02:36:10 +000057\section{New Development Process}
58
59The most important change in Python 2.0 may not be to the code at all,
Andrew M. Kuchlingd44dc3c2000-10-04 12:40:44 +000060but to how Python is developed: in May 2000 the Python developers
61began using the tools made available by SourceForge for storing
62source code, tracking bug reports, and managing the queue of patch
63submissions. To report bugs or submit patches for Python 2.0, use the
64bug tracking and patch manager tools available from Python's project
65page, located at \url{http://sourceforge.net/projects/python/}.
Andrew M. Kuchlingbe870dd2000-09-27 02:36:10 +000066
Andrew M. Kuchlingd44dc3c2000-10-04 12:40:44 +000067The most important of the services now hosted at SourceForge is the
68Python CVS tree, the version-controlled repository containing the
69source code for Python. Previously, there were roughly 7 or so people
70who had write access to the CVS tree, and all patches had to be
71inspected and checked in by one of the people on this short list.
72Obviously, this wasn't very scalable. By moving the CVS tree to
73SourceForge, it became possible to grant write access to more people;
74as of September 2000 there were 27 people able to check in changes, a
75fourfold increase. This makes possible large-scale changes that
76wouldn't be attempted if they'd have to be filtered through the small
77group of core developers. For example, one day Peter Schneider-Kamp
78took it into his head to drop K\&R C compatibility and convert the C
79source for Python to ANSI C. After getting approval on the python-dev
80mailing list, he launched into a flurry of checkins that lasted about
81a week, other developers joined in to help, and the job was done. If
82there were only 5 people with write access, probably that task would
83have been viewed as ``nice, but not worth the time and effort needed''
84and it would never have gotten done.
Andrew M. Kuchlingbe870dd2000-09-27 02:36:10 +000085
Andrew M. Kuchlingd44dc3c2000-10-04 12:40:44 +000086The shift to using SourceForge's services has resulted in a remarkable
87increase in the speed of development. Patches now get submitted,
88commented on, revised by people other than the original submitter, and
89bounced back and forth between people until the patch is deemed worth
Andrew M. Kuchlingec1722e2000-10-12 03:04:22 +000090checking in. Bugs are tracked in one central location and can be
91assigned to a specific person for fixing, and we can count the number
92of open bugs to measure progress. This didn't come without a cost:
93developers now have more e-mail to deal with, more mailing lists to
94follow, and special tools had to be written for the new environment.
95For example, SourceForge sends default patch and bug notification
96e-mail messages that are completely unhelpful, so Ka-Ping Yee wrote an
97HTML screen-scraper that sends more useful messages.
Andrew M. Kuchlingbe870dd2000-09-27 02:36:10 +000098
99The ease of adding code caused a few initial growing pains, such as
100code was checked in before it was ready or without getting clear
101agreement from the developer group. The approval process that has
102emerged is somewhat similar to that used by the Apache group.
103Developers can vote +1, +0, -0, or -1 on a patch; +1 and -1 denote
104acceptance or rejection, while +0 and -0 mean the developer is mostly
105indifferent to the change, though with a slight positive or negative
Andrew M. Kuchlingec1722e2000-10-12 03:04:22 +0000106slant. The most significant change from the Apache model is that the
107voting is essentially advisory, letting Guido van Rossum, who has
108Benevolent Dictator For Life status, know what the general opinion is.
109He can still ignore the result of a vote, and approve or
110reject a change even if the community disagrees with him.
Andrew M. Kuchlingbe870dd2000-09-27 02:36:10 +0000111
112Producing an actual patch is the last step in adding a new feature,
113and is usually easy compared to the earlier task of coming up with a
114good design. Discussions of new features can often explode into
115lengthy mailing list threads, making the discussion hard to follow,
116and no one can read every posting to python-dev. Therefore, a
117relatively formal process has been set up to write Python Enhancement
118Proposals (PEPs), modelled on the Internet RFC process. PEPs are
119draft documents that describe a proposed new feature, and are
120continually revised until the community reaches a consensus, either
121accepting or rejecting the proposal. Quoting from the introduction to
122PEP 1, ``PEP Purpose and Guidelines'':
123
124\begin{quotation}
125 PEP stands for Python Enhancement Proposal. A PEP is a design
126 document providing information to the Python community, or
127 describing a new feature for Python. The PEP should provide a
128 concise technical specification of the feature and a rationale for
129 the feature.
130
131 We intend PEPs to be the primary mechanisms for proposing new
132 features, for collecting community input on an issue, and for
133 documenting the design decisions that have gone into Python. The
134 PEP author is responsible for building consensus within the
135 community and documenting dissenting opinions.
136\end{quotation}
137
138Read the rest of PEP 1 for the details of the PEP editorial process,
139style, and format. PEPs are kept in the Python CVS tree on
140SourceForge, though they're not part of the Python 2.0 distribution,
141and are also available in HTML form from
Fred Drakeb81fbad2002-04-03 02:52:50 +0000142\url{http://www.python.org/peps/}. As of September 2000,
Andrew M. Kuchlingbe870dd2000-09-27 02:36:10 +0000143there are 25 PEPS, ranging from PEP 201, ``Lockstep Iteration'', to
144PEP 225, ``Elementwise/Objectwise Operators''.
145
Andrew M. Kuchlingbe870dd2000-09-27 02:36:10 +0000146% ======================================================================
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000147\section{Unicode}
148
Andrew M. Kuchling730067e2000-06-30 01:44:05 +0000149The largest new feature in Python 2.0 is a new fundamental data type:
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000150Unicode strings. Unicode uses 16-bit numbers to represent characters
151instead of the 8-bit number used by ASCII, meaning that 65,536
152distinct characters can be supported.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000153
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000154The final interface for Unicode support was arrived at through
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000155countless often-stormy discussions on the python-dev mailing list, and
Andrew M. Kuchling62cdd962000-06-30 12:46:41 +0000156mostly implemented by Marc-Andr\'e Lemburg, based on a Unicode string
157type implementation by Fredrik Lundh. A detailed explanation of the
Andrew M. Kuchling95467722002-05-02 14:48:26 +0000158interface was written up as \pep{100}, ``Python Unicode Integration''.
159This article will simply cover the most significant points about the
160Unicode interfaces.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000161
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000162In Python source code, Unicode strings are written as
163\code{u"string"}. Arbitrary Unicode characters can be written using a
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000164new escape sequence, \code{\e u\var{HHHH}}, where \var{HHHH} is a
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +00001654-digit hexadecimal number from 0000 to FFFF. The existing
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000166\code{\e x\var{HHHH}} escape sequence can also be used, and octal
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000167escapes can be used for characters up to U+01FF, which is represented
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000168by \code{\e 777}.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000169
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000170Unicode strings, just like regular strings, are an immutable sequence
Andrew M. Kuchling662d76e2000-06-25 14:32:48 +0000171type. They can be indexed and sliced, but not modified in place.
Andrew M. Kuchling62cdd962000-06-30 12:46:41 +0000172Unicode strings have an \method{encode( \optional{encoding} )} method
Andrew M. Kuchling662d76e2000-06-25 14:32:48 +0000173that returns an 8-bit string in the desired encoding. Encodings are
174named by strings, such as \code{'ascii'}, \code{'utf-8'},
175\code{'iso-8859-1'}, or whatever. A codec API is defined for
176implementing and registering new encodings that are then available
177throughout a Python program. If an encoding isn't specified, the
178default encoding is usually 7-bit ASCII, though it can be changed for
179your Python installation by calling the
Andrew M. Kuchlingc0328f02000-06-10 15:11:20 +0000180\function{sys.setdefaultencoding(\var{encoding})} function in a
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +0000181customised version of \file{site.py}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000182
183Combining 8-bit and Unicode strings always coerces to Unicode, using
184the default ASCII encoding; the result of \code{'a' + u'bc'} is
Andrew M. Kuchling7f6270d2000-06-09 02:48:18 +0000185\code{u'abc'}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000186
187New built-in functions have been added, and existing built-ins
188modified to support Unicode:
189
190\begin{itemize}
191\item \code{unichr(\var{ch})} returns a Unicode string 1 character
192long, containing the character \var{ch}.
193
194\item \code{ord(\var{u})}, where \var{u} is a 1-character regular or Unicode string, returns the number of the character as an integer.
195
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +0000196\item \code{unicode(\var{string} \optional{, \var{encoding}}
197\optional{, \var{errors}} ) } creates a Unicode string from an 8-bit
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000198string. \code{encoding} is a string naming the encoding to use.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000199The \code{errors} parameter specifies the treatment of characters that
200are invalid for the current encoding; passing \code{'strict'} as the
201value causes an exception to be raised on any encoding error, while
202\code{'ignore'} causes errors to be silently ignored and
203\code{'replace'} uses U+FFFD, the official replacement character, in
204case of any problems.
205
Andrew M. Kuchling3ad4e742000-09-27 01:33:41 +0000206\item The \keyword{exec} statement, and various built-ins such as
207\code{eval()}, \code{getattr()}, and \code{setattr()} will also
208accept Unicode strings as well as regular strings. (It's possible
209that the process of fixing this missed some built-ins; if you find a
210built-in function that accepts strings but doesn't accept Unicode
211strings at all, please report it as a bug.)
212
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000213\end{itemize}
214
215A new module, \module{unicodedata}, provides an interface to Unicode
216character properties. For example, \code{unicodedata.category(u'A')}
217returns the 2-character string 'Lu', the 'L' denoting it's a letter,
218and 'u' meaning that it's uppercase.
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000219\code{u.bidirectional(u'\e x0660')} returns 'AN', meaning that U+0660 is
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000220an Arabic number.
221
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000222The \module{codecs} module contains functions to look up existing encodings
223and register new ones. Unless you want to implement a
224new encoding, you'll most often use the
225\function{codecs.lookup(\var{encoding})} function, which returns a
2264-element tuple: \code{(\var{encode_func},
227\var{decode_func}, \var{stream_reader}, \var{stream_writer})}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000228
229\begin{itemize}
230\item \var{encode_func} is a function that takes a Unicode string, and
231returns a 2-tuple \code{(\var{string}, \var{length})}. \var{string}
232is an 8-bit string containing a portion (perhaps all) of the Unicode
Andrew M. Kuchling2d2dc9f2000-08-17 00:27:06 +0000233string converted into the given encoding, and \var{length} tells you
234how much of the Unicode string was converted.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000235
Andrew M. Kuchling118ee962000-09-27 01:01:18 +0000236\item \var{decode_func} is the opposite of \var{encode_func}, taking
237an 8-bit string and returning a 2-tuple \code{(\var{ustring},
238\var{length})}, consisting of the resulting Unicode string
239\var{ustring} and the integer \var{length} telling how much of the
Andrew M. Kuchling3ad4e742000-09-27 01:33:41 +00002408-bit string was consumed.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000241
242\item \var{stream_reader} is a class that supports decoding input from
243a stream. \var{stream_reader(\var{file_obj})} returns an object that
244supports the \method{read()}, \method{readline()}, and
245\method{readlines()} methods. These methods will all translate from
246the given encoding and return Unicode strings.
247
248\item \var{stream_writer}, similarly, is a class that supports
249encoding output to a stream. \var{stream_writer(\var{file_obj})}
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +0000250returns an object that supports the \method{write()} and
251\method{writelines()} methods. These methods expect Unicode strings,
252translating them to the given encoding on output.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000253\end{itemize}
254
255For example, the following code writes a Unicode string into a file,
256encoding it as UTF-8:
257
258\begin{verbatim}
259import codecs
260
261unistr = u'\u0660\u2000ab ...'
262
263(UTF8_encode, UTF8_decode,
264 UTF8_streamreader, UTF8_streamwriter) = codecs.lookup('UTF-8')
265
266output = UTF8_streamwriter( open( '/tmp/output', 'wb') )
267output.write( unistr )
268output.close()
269\end{verbatim}
270
271The following code would then read UTF-8 input from the file:
272
273\begin{verbatim}
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +0000274input = UTF8_streamreader( open( '/tmp/output', 'rb') )
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000275print repr(input.read())
276input.close()
277\end{verbatim}
278
279Unicode-aware regular expressions are available through the
280\module{re} module, which has a new underlying implementation called
281SRE written by Fredrik Lundh of Secret Labs AB.
282
Andrew M. Kuchlingc0328f02000-06-10 15:11:20 +0000283A \code{-U} command line option was added which causes the Python
284compiler to interpret all string literals as Unicode string literals.
285This is intended to be used in testing and future-proofing your Python
286code, since some future version of Python may drop support for 8-bit
287strings and provide only Unicode strings.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000288
289% ======================================================================
Andrew M. Kuchling2d2dc9f2000-08-17 00:27:06 +0000290\section{List Comprehensions}
291
292Lists are a workhorse data type in Python, and many programs
293manipulate a list at some point. Two common operations on lists are
294to loop over them, and either pick out the elements that meet a
295certain criterion, or apply some function to each element. For
296example, given a list of strings, you might want to pull out all the
297strings containing a given substring, or strip off trailing whitespace
298from each line.
299
300The existing \function{map()} and \function{filter()} functions can be
301used for this purpose, but they require a function as one of their
302arguments. This is fine if there's an existing built-in function that
303can be passed directly, but if there isn't, you have to create a
304little function to do the required work, and Python's scoping rules
305make the result ugly if the little function needs additional
306information. Take the first example in the previous paragraph,
307finding all the strings in the list containing a given substring. You
308could write the following to do it:
309
310\begin{verbatim}
311# Given the list L, make a list of all strings
312# containing the substring S.
313sublist = filter( lambda s, substring=S:
314 string.find(s, substring) != -1,
315 L)
316\end{verbatim}
317
318Because of Python's scoping rules, a default argument is used so that
319the anonymous function created by the \keyword{lambda} statement knows
320what substring is being searched for. List comprehensions make this
321cleaner:
322
323\begin{verbatim}
324sublist = [ s for s in L if string.find(s, S) != -1 ]
325\end{verbatim}
326
327List comprehensions have the form:
328
329\begin{verbatim}
330[ expression for expr in sequence1
331 for expr2 in sequence2 ...
332 for exprN in sequenceN
333 if condition
334\end{verbatim}
335
336The \keyword{for}...\keyword{in} clauses contain the sequences to be
337iterated over. The sequences do not have to be the same length,
338because they are \emph{not} iterated over in parallel, but
339from left to right; this is explained more clearly in the following
340paragraphs. The elements of the generated list will be the successive
341values of \var{expression}. The final \keyword{if} clause is
342optional; if present, \var{expression} is only evaluated and added to
343the result if \var{condition} is true.
344
345To make the semantics very clear, a list comprehension is equivalent
346to the following Python code:
347
348\begin{verbatim}
349for expr1 in sequence1:
350 for expr2 in sequence2:
351 ...
352 for exprN in sequenceN:
353 if (condition):
354 # Append the value of
355 # the expression to the
356 # resulting list.
357\end{verbatim}
358
359This means that when there are \keyword{for}...\keyword{in} clauses,
360the resulting list will be equal to the product of the lengths of all
361the sequences. If you have two lists of length 3, the output list is
3629 elements long:
363
364\begin{verbatim}
365seq1 = 'abc'
366seq2 = (1,2,3)
367>>> [ (x,y) for x in seq1 for y in seq2]
368[('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1),
369('c', 2), ('c', 3)]
370\end{verbatim}
371
372To avoid introducing an ambiguity into Python's grammar, if
373\var{expression} is creating a tuple, it must be surrounded with
374parentheses. The first list comprehension below is a syntax error,
375while the second one is correct:
376
377\begin{verbatim}
378# Syntax error
379[ x,y for x in seq1 for y in seq2]
380# Correct
381[ (x,y) for x in seq1 for y in seq2]
382\end{verbatim}
383
Andrew M. Kuchling2d2dc9f2000-08-17 00:27:06 +0000384The idea of list comprehensions originally comes from the functional
385programming language Haskell (\url{http://www.haskell.org}). Greg
386Ewing argued most effectively for adding them to Python and wrote the
387initial list comprehension patch, which was then discussed for a
388seemingly endless time on the python-dev mailing list and kept
389up-to-date by Skip Montanaro.
390
Andrew M. Kuchling2d2dc9f2000-08-17 00:27:06 +0000391% ======================================================================
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000392\section{Augmented Assignment}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000393
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000394Augmented assignment operators, another long-requested feature, have
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +0000395been added to Python 2.0. Augmented assignment operators include
396\code{+=}, \code{-=}, \code{*=}, and so forth. For example, the
397statement \code{a += 2} increments the value of the variable
398\code{a} by 2, equivalent to the slightly lengthier \code{a = a + 2}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000399
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000400The full list of supported assignment operators is \code{+=},
401\code{-=}, \code{*=}, \code{/=}, \code{\%=}, \code{**=}, \code{\&=},
Andrew M. Kuchling3cdb5762000-08-30 12:55:42 +0000402\code{|=}, \verb|^=|, \code{>>=}, and \code{<<=}. Python classes can
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000403override the augmented assignment operators by defining methods named
404\method{__iadd__}, \method{__isub__}, etc. For example, the following
405\class{Number} class stores a number and supports using += to create a
406new instance with an incremented value.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000407
408\begin{verbatim}
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000409class Number:
410 def __init__(self, value):
411 self.value = value
412 def __iadd__(self, increment):
413 return Number( self.value + increment)
414
415n = Number(5)
416n += 3
417print n.value
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000418\end{verbatim}
419
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000420The \method{__iadd__} special method is called with the value of the
421increment, and should return a new instance with an appropriately
422modified value; this return value is bound as the new value of the
423variable on the left-hand side.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000424
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000425Augmented assignment operators were first introduced in the C
426programming language, and most C-derived languages, such as
Fred Drake2884d6d2003-07-02 12:27:43 +0000427\program{awk}, \Cpp, Java, Perl, and PHP also support them. The augmented
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000428assignment patch was implemented by Thomas Wouters.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000429
430% ======================================================================
431\section{String Methods}
432
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000433Until now string-manipulation functionality was in the \module{string}
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +0000434module, which was usually a front-end for the \module{strop}
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000435module written in C. The addition of Unicode posed a difficulty for
436the \module{strop} module, because the functions would all need to be
437rewritten in order to accept either 8-bit or Unicode strings. For
438functions such as \function{string.replace()}, which takes 3 string
439arguments, that means eight possible permutations, and correspondingly
440complicated code.
441
Andrew M. Kuchling730067e2000-06-30 01:44:05 +0000442Instead, Python 2.0 pushes the problem onto the string type, making
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000443string manipulation functionality available through methods on both
4448-bit strings and Unicode strings.
445
446\begin{verbatim}
447>>> 'andrew'.capitalize()
448'Andrew'
449>>> 'hostname'.replace('os', 'linux')
450'hlinuxtname'
451>>> 'moshe'.find('sh')
4522
453\end{verbatim}
454
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000455One thing that hasn't changed, a noteworthy April Fools' joke
456notwithstanding, is that Python strings are immutable. Thus, the
457string methods return new strings, and do not modify the string on
458which they operate.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000459
460The old \module{string} module is still around for backwards
461compatibility, but it mostly acts as a front-end to the new string
462methods.
463
Andrew M. Kuchling730067e2000-06-30 01:44:05 +0000464Two methods which have no parallel in pre-2.0 versions, although they
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000465did exist in JPython for quite some time, are \method{startswith()}
466and \method{endswith}. \code{s.startswith(t)} is equivalent to \code{s[:len(t)]
467== t}, while \code{s.endswith(t)} is equivalent to \code{s[-len(t):] == t}.
468
Andrew M. Kuchlingfed4f1e2000-07-01 12:33:43 +0000469One other method which deserves special mention is \method{join}. The
470\method{join} method of a string receives one parameter, a sequence of
471strings, and is equivalent to the \function{string.join} function from
472the old \module{string} module, with the arguments reversed. In other
473words, \code{s.join(seq)} is equivalent to the old
474\code{string.join(seq, s)}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000475
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000476% ======================================================================
Andrew M. Kuchlingec1722e2000-10-12 03:04:22 +0000477\section{Garbage Collection of Cycles}
Andrew M. Kuchling35e8afb2000-07-08 12:06:31 +0000478
479The C implementation of Python uses reference counting to implement
480garbage collection. Every Python object maintains a count of the
481number of references pointing to itself, and adjusts the count as
482references are created or destroyed. Once the reference count reaches
483zero, the object is no longer accessible, since you need to have a
484reference to an object to access it, and if the count is zero, no
485references exist any longer.
486
487Reference counting has some pleasant properties: it's easy to
488understand and implement, and the resulting implementation is
489portable, fairly fast, and reacts well with other libraries that
490implement their own memory handling schemes. The major problem with
491reference counting is that it sometimes doesn't realise that objects
492are no longer accessible, resulting in a memory leak. This happens
493when there are cycles of references.
494
495Consider the simplest possible cycle,
496a class instance which has a reference to itself:
497
498\begin{verbatim}
499instance = SomeClass()
500instance.myself = instance
501\end{verbatim}
502
503After the above two lines of code have been executed, the reference
504count of \code{instance} is 2; one reference is from the variable
505named \samp{'instance'}, and the other is from the \samp{myself}
506attribute of the instance.
507
508If the next line of code is \code{del instance}, what happens? The
509reference count of \code{instance} is decreased by 1, so it has a
510reference count of 1; the reference in the \samp{myself} attribute
511still exists. Yet the instance is no longer accessible through Python
512code, and it could be deleted. Several objects can participate in a
513cycle if they have references to each other, causing all of the
514objects to be leaked.
515
Andrew M. Kuchlingec1722e2000-10-12 03:04:22 +0000516Python 2.0 fixes this problem by periodically executing a cycle
517detection algorithm which looks for inaccessible cycles and deletes
518the objects involved. A new \module{gc} module provides functions to
519perform a garbage collection, obtain debugging statistics, and tuning
520the collector's parameters.
Andrew M. Kuchling35e8afb2000-07-08 12:06:31 +0000521
Andrew M. Kuchlingec1722e2000-10-12 03:04:22 +0000522Running the cycle detection algorithm takes some time, and therefore
523will result in some additional overhead. It is hoped that after we've
524gotten experience with the cycle collection from using 2.0, Python 2.1
525will be able to minimize the overhead with careful tuning. It's not
526yet obvious how much performance is lost, because benchmarking this is
527tricky and depends crucially on how often the program creates and
528destroys objects. The detection of cycles can be disabled when Python
529is compiled, if you can't afford even a tiny speed penalty or suspect
530that the cycle collection is buggy, by specifying the
Fred Drake7c67cb82003-12-30 17:17:17 +0000531\longprogramopt{without-cycle-gc} switch when running the
532\program{configure} script.
Andrew M. Kuchling35e8afb2000-07-08 12:06:31 +0000533
534Several people tackled this problem and contributed to a solution. An
535early implementation of the cycle detection approach was written by
536Toby Kelsey. The current algorithm was suggested by Eric Tiedemann
537during a visit to CNRI, and Guido van Rossum and Neil Schemenauer
538wrote two different implementations, which were later integrated by
539Neil. Lots of other people offered suggestions along the way; the
540March 2000 archives of the python-dev mailing list contain most of the
541relevant discussion, especially in the threads titled ``Reference
542cycle collection for Python'' and ``Finalization again''.
543
Andrew M. Kuchling35e8afb2000-07-08 12:06:31 +0000544% ======================================================================
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000545\section{Other Core Changes}
Andrew M. Kuchling35e8afb2000-07-08 12:06:31 +0000546
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000547Various minor changes have been made to Python's syntax and built-in
548functions. None of the changes are very far-reaching, but they're
549handy conveniences.
550
551\subsection{Minor Language Changes}
552
553A new syntax makes it more convenient to call a given function
554with a tuple of arguments and/or a dictionary of keyword arguments.
555In Python 1.5 and earlier, you'd use the \function{apply()}
556built-in function: \code{apply(f, \var{args}, \var{kw})} calls the
557function \function{f()} with the argument tuple \var{args} and the
558keyword arguments in the dictionary \var{kw}. \function{apply()}
559is the same in 2.0, but thanks to a patch from
560Greg Ewing, \code{f(*\var{args}, **\var{kw})} as a shorter
561and clearer way to achieve the same effect. This syntax is
562symmetrical with the syntax for defining functions:
563
564\begin{verbatim}
565def f(*args, **kw):
566 # args is a tuple of positional args,
567 # kw is a dictionary of keyword args
568 ...
569\end{verbatim}
570
571The \keyword{print} statement can now have its output directed to a
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +0000572file-like object by following the \keyword{print} with
573\verb|>> file|, similar to the redirection operator in Unix shells.
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000574Previously you'd either have to use the \method{write()} method of the
575file-like object, which lacks the convenience and simplicity of
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +0000576\keyword{print}, or you could assign a new value to
577\code{sys.stdout} and then restore the old value. For sending output to standard error,
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000578it's much easier to write this:
579
580\begin{verbatim}
581print >> sys.stderr, "Warning: action field not supplied"
582\end{verbatim}
583
584Modules can now be renamed on importing them, using the syntax
585\code{import \var{module} as \var{name}} or \code{from \var{module}
586import \var{name} as \var{othername}}. The patch was submitted by
587Thomas Wouters.
588
589A new format style is available when using the \code{\%} operator;
590'\%r' will insert the \function{repr()} of its argument. This was
591also added from symmetry considerations, this time for symmetry with
592the existing '\%s' format style, which inserts the \function{str()} of
593its argument. For example, \code{'\%r \%s' \% ('abc', 'abc')} returns a
594string containing \verb|'abc' abc|.
595
596Previously there was no way to implement a class that overrode
597Python's built-in \keyword{in} operator and implemented a custom
598version. \code{\var{obj} in \var{seq}} returns true if \var{obj} is
599present in the sequence \var{seq}; Python computes this by simply
600trying every index of the sequence until either \var{obj} is found or
601an \exception{IndexError} is encountered. Moshe Zadka contributed a
602patch which adds a \method{__contains__} magic method for providing a
603custom implementation for \keyword{in}. Additionally, new built-in
604objects written in C can define what \keyword{in} means for them via a
605new slot in the sequence protocol.
606
607Earlier versions of Python used a recursive algorithm for deleting
608objects. Deeply nested data structures could cause the interpreter to
609fill up the C stack and crash; Christian Tismer rewrote the deletion
610logic to fix this problem. On a related note, comparing recursive
611objects recursed infinitely and crashed; Jeremy Hylton rewrote the
612code to no longer crash, producing a useful result instead. For
613example, after this code:
614
615\begin{verbatim}
616a = []
617b = []
618a.append(a)
619b.append(b)
620\end{verbatim}
621
622The comparison \code{a==b} returns true, because the two recursive
Andrew M. Kuchling6032c482000-10-12 02:37:14 +0000623data structures are isomorphic. See the thread ``trashcan
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000624and PR\#7'' in the April 2000 archives of the python-dev mailing list
625for the discussion leading up to this implementation, and some useful
Andrew M. Kuchlingec1722e2000-10-12 03:04:22 +0000626relevant links.
Andrew M. Kuchling6032c482000-10-12 02:37:14 +0000627% Starting URL:
628% http://www.python.org/pipermail/python-dev/2000-April/004834.html
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000629
Andrew M. Kuchlingec1722e2000-10-12 03:04:22 +0000630Note that comparisons can now also raise exceptions. In earlier
631versions of Python, a comparison operation such as \code{cmp(a,b)}
632would always produce an answer, even if a user-defined
633\method{__cmp__} method encountered an error, since the resulting
634exception would simply be silently swallowed.
635
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000636Work has been done on porting Python to 64-bit Windows on the Itanium
637processor, mostly by Trent Mick of ActiveState. (Confusingly,
638\code{sys.platform} is still \code{'win32'} on Win64 because it seems
Fred Drake2884d6d2003-07-02 12:27:43 +0000639that for ease of porting, MS Visual \Cpp{} treats code as 32 bit on Itanium.)
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000640PythonWin also supports Windows CE; see the Python CE page at
641\url{http://starship.python.net/crew/mhammond/ce/} for more
642information.
643
Andrew M. Kuchlingec1722e2000-10-12 03:04:22 +0000644Another new platform is Darwin/MacOS X; inital support for it is in
645Python 2.0. Dynamic loading works, if you specify ``configure
646--with-dyld --with-suffix=.x''. Consult the README in the Python
647source distribution for more instructions.
648
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000649An attempt has been made to alleviate one of Python's warts, the
650often-confusing \exception{NameError} exception when code refers to a
651local variable before the variable has been assigned a value. For
652example, the following code raises an exception on the \keyword{print}
653statement in both 1.5.2 and 2.0; in 1.5.2 a \exception{NameError}
654exception is raised, while 2.0 raises a new
655\exception{UnboundLocalError} exception.
656\exception{UnboundLocalError} is a subclass of \exception{NameError},
657so any existing code that expects \exception{NameError} to be raised
658should still work.
659
660\begin{verbatim}
661def f():
662 print "i=",i
663 i = i + 1
664f()
665\end{verbatim}
666
Andrew M. Kuchling4d46d382000-09-06 17:58:49 +0000667Two new exceptions, \exception{TabError} and
668\exception{IndentationError}, have been introduced. They're both
669subclasses of \exception{SyntaxError}, and are raised when Python code
670is found to be improperly indented.
671
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000672\subsection{Changes to Built-in Functions}
673
674A new built-in, \function{zip(\var{seq1}, \var{seq2}, ...)}, has been
675added. \function{zip()} returns a list of tuples where each tuple
676contains the i-th element from each of the argument sequences. The
677difference between \function{zip()} and \code{map(None, \var{seq1},
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +0000678\var{seq2})} is that \function{map()} pads the sequences with
679\code{None} if the sequences aren't all of the same length, while
680\function{zip()} truncates the returned list to the length of the
681shortest argument sequence.
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000682
683The \function{int()} and \function{long()} functions now accept an
684optional ``base'' parameter when the first argument is a string.
685\code{int('123', 10)} returns 123, while \code{int('123', 16)} returns
686291. \code{int(123, 16)} raises a \exception{TypeError} exception
687with the message ``can't convert non-string with explicit base''.
688
689A new variable holding more detailed version information has been
690added to the \module{sys} module. \code{sys.version_info} is a tuple
691\code{(\var{major}, \var{minor}, \var{micro}, \var{level},
692\var{serial})} For example, in a hypothetical 2.0.1beta1,
693\code{sys.version_info} would be \code{(2, 0, 1, 'beta', 1)}.
694\var{level} is a string such as \code{"alpha"}, \code{"beta"}, or
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +0000695\code{"final"} for a final release.
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000696
697Dictionaries have an odd new method, \method{setdefault(\var{key},
698\var{default})}, which behaves similarly to the existing
699\method{get()} method. However, if the key is missing,
700\method{setdefault()} both returns the value of \var{default} as
701\method{get()} would do, and also inserts it into the dictionary as
702the value for \var{key}. Thus, the following lines of code:
703
704\begin{verbatim}
705if dict.has_key( key ): return dict[key]
706else:
707 dict[key] = []
708 return dict[key]
709\end{verbatim}
710
711can be reduced to a single \code{return dict.setdefault(key, [])} statement.
712
Andrew M. Kuchling4d46d382000-09-06 17:58:49 +0000713The interpreter sets a maximum recursion depth in order to catch
714runaway recursion before filling the C stack and causing a core dump
715or GPF.. Previously this limit was fixed when you compiled Python,
716but in 2.0 the maximum recursion depth can be read and modified using
717\function{sys.getrecursionlimit} and \function{sys.setrecursionlimit}.
718The default value is 1000, and a rough maximum value for a given
719platform can be found by running a new script,
720\file{Misc/find_recursionlimit.py}.
Andrew M. Kuchling35e8afb2000-07-08 12:06:31 +0000721
722% ======================================================================
Andrew M. Kuchling730067e2000-06-30 01:44:05 +0000723\section{Porting to 2.0}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000724
725New Python releases try hard to be compatible with previous releases,
726and the record has been pretty good. However, some changes are
Andrew M. Kuchling4d46d382000-09-06 17:58:49 +0000727considered useful enough, usually because they fix initial design decisions that
728turned out to be actively mistaken, that breaking backward compatibility
Andrew M. Kuchling730067e2000-06-30 01:44:05 +0000729can't always be avoided. This section lists the changes in Python 2.0
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000730that may cause old Python code to break.
731
732The change which will probably break the most code is tightening up
733the arguments accepted by some methods. Some methods would take
734multiple arguments and treat them as a tuple, particularly various
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000735list methods such as \method{.append()} and \method{.insert()}.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000736In earlier versions of Python, if \code{L} is a list, \code{L.append(
Andrew M. Kuchling730067e2000-06-30 01:44:05 +00007371,2 )} appends the tuple \code{(1,2)} to the list. In Python 2.0 this
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000738causes a \exception{TypeError} exception to be raised, with the
739message: 'append requires exactly 1 argument; 2 given'. The fix is to
740simply add an extra set of parentheses to pass both values as a tuple:
741\code{L.append( (1,2) )}.
742
743The earlier versions of these methods were more forgiving because they
744used an old function in Python's C interface to parse their arguments;
Andrew M. Kuchling730067e2000-06-30 01:44:05 +00007452.0 modernizes them to use \function{PyArg_ParseTuple}, the current
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000746argument parsing function, which provides more helpful error messages
747and treats multi-argument calls as errors. If you absolutely must use
Andrew M. Kuchling730067e2000-06-30 01:44:05 +00007482.0 but can't fix your code, you can edit \file{Objects/listobject.c}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000749and define the preprocessor symbol \code{NO_STRICT_LIST_APPEND} to
750preserve the old behaviour; this isn't recommended.
751
752Some of the functions in the \module{socket} module are still
753forgiving in this way. For example, \function{socket.connect(
754('hostname', 25) )} is the correct form, passing a tuple representing
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000755an IP address, but \function{socket.connect( 'hostname', 25 )} also
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000756works. \function{socket.connect_ex()} and \function{socket.bind()} are
Andrew M. Kuchling730067e2000-06-30 01:44:05 +0000757similarly easy-going. 2.0alpha1 tightened these functions up, but
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000758because the documentation actually used the erroneous multiple
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000759argument form, many people wrote code which would break with the
760stricter checking. GvR backed out the changes in the face of public
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +0000761reaction, so for the \module{socket} module, the documentation was
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000762fixed and the multiple argument form is simply marked as deprecated;
763it \emph{will} be tightened up again in a future Python version.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000764
Andrew M. Kuchling4d46d382000-09-06 17:58:49 +0000765The \code{\e x} escape in string literals now takes exactly 2 hex
766digits. Previously it would consume all the hex digits following the
767'x' and take the lowest 8 bits of the result, so \code{\e x123456} was
768equivalent to \code{\e x56}.
769
Andrew M. Kuchling2a159802002-05-02 14:37:14 +0000770The \exception{AttributeError} and \exception{NameError} exceptions
771have a more friendly error message, whose text will be something like
772\code{'Spam' instance has no attribute 'eggs'} or \code{name 'eggs' is
773not defined}. Previously the error message was just the missing
774attribute name \code{eggs}, and code written to take advantage of this
775fact will break in 2.0.
Andrew M. Kuchling4d46d382000-09-06 17:58:49 +0000776
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000777Some work has been done to make integers and long integers a bit more
778interchangeable. In 1.5.2, large-file support was added for Solaris,
779to allow reading files larger than 2Gb; this made the \method{tell()}
780method of file objects return a long integer instead of a regular
781integer. Some code would subtract two file offsets and attempt to use
782the result to multiply a sequence or slice a string, but this raised a
Andrew M. Kuchling730067e2000-06-30 01:44:05 +0000783\exception{TypeError}. In 2.0, long integers can be used to multiply
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000784or slice a sequence, and it'll behave as you'd intuitively expect it
785to; \code{3L * 'abc'} produces 'abcabcabc', and \code{
786(0,1,2,3)[2L:4L]} produces (2,3). Long integers can also be used in
Andrew M. Kuchling3ad4e742000-09-27 01:33:41 +0000787various contexts where previously only integers were accepted, such
788as in the \method{seek()} method of file objects, and in the formats
789supported by the \verb|%| operator (\verb|%d|, \verb|%i|, \verb|%x|,
790etc.). For example, \code{"\%d" \% 2L**64} will produce the string
791\samp{18446744073709551616}.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000792
793The subtlest long integer change of all is that the \function{str()}
794of a long integer no longer has a trailing 'L' character, though
795\function{repr()} still includes it. The 'L' annoyed many people who
796wanted to print long integers that looked just like regular integers,
797since they had to go out of their way to chop off the character. This
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +0000798is no longer a problem in 2.0, but code which does \code{str(longval)[:-1]} and assumes the 'L' is there, will now lose
799the final digit.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000800
801Taking the \function{repr()} of a float now uses a different
802formatting precision than \function{str()}. \function{repr()} uses
Andrew M. Kuchling662d76e2000-06-25 14:32:48 +0000803\code{\%.17g} format string for C's \function{sprintf()}, while
804\function{str()} uses \code{\%.12g} as before. The effect is that
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000805\function{repr()} may occasionally show more decimal places than
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +0000806\function{str()}, for certain numbers.
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +0000807For example, the number 8.1 can't be represented exactly in binary, so
808\code{repr(8.1)} is \code{'8.0999999999999996'}, while str(8.1) is
809\code{'8.1'}.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000810
Andrew M. Kuchling730067e2000-06-30 01:44:05 +0000811The \code{-X} command-line option, which turned all standard
Andrew M. Kuchling62cdd962000-06-30 12:46:41 +0000812exceptions into strings instead of classes, has been removed; the
813standard exceptions will now always be classes. The
814\module{exceptions} module containing the standard exceptions was
815translated from Python to a built-in C module, written by Barry Warsaw
816and Fredrik Lundh.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000817
Andrew M. Kuchling791b3662000-07-01 15:04:18 +0000818% Commented out for now -- I don't think anyone will care.
819%The pattern and match objects provided by SRE are C types, not Python
820%class instances as in 1.5. This means you can no longer inherit from
821%\class{RegexObject} or \class{MatchObject}, but that shouldn't be much
822%of a problem since no one should have been doing that in the first
823%place.
824
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000825% ======================================================================
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000826\section{Extending/Embedding Changes}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000827
828Some of the changes are under the covers, and will only be apparent to
Andrew M. Kuchling8357c4c2000-07-01 00:14:43 +0000829people writing C extension modules or embedding a Python interpreter
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000830in a larger application. If you aren't dealing with Python's C API,
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000831you can safely skip this section.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000832
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +0000833The version number of the Python C API was incremented, so C
834extensions compiled for 1.5.2 must be recompiled in order to work with
Andrew M. Kuchlinga8d10782000-10-19 01:42:33 +00008352.0. On Windows, it's not possible for Python 2.0 to import a third
836party extension built for Python 1.5.x due to how Windows DLLs work,
837so Python will raise an exception and the import will fail.
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +0000838
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000839Users of Jim Fulton's ExtensionClass module will be pleased to find
840out that hooks have been added so that ExtensionClasses are now
841supported by \function{isinstance()} and \function{issubclass()}.
842This means you no longer have to remember to write code such as
843\code{if type(obj) == myExtensionClass}, but can use the more natural
844\code{if isinstance(obj, myExtensionClass)}.
845
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000846The \file{Python/importdl.c} file, which was a mass of \#ifdefs to
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000847support dynamic loading on many different platforms, was cleaned up
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +0000848and reorganised by Greg Stein. \file{importdl.c} is now quite small,
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000849and platform-specific code has been moved into a bunch of
Andrew M. Kuchlingb9fb1f22000-08-04 12:40:35 +0000850\file{Python/dynload_*.c} files. Another cleanup: there were also a
851number of \file{my*.h} files in the Include/ directory that held
852various portability hacks; they've been merged into a single file,
853\file{Include/pyport.h}.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000854
855Vladimir Marangozov's long-awaited malloc restructuring was completed,
856to make it easy to have the Python interpreter use a custom allocator
857instead of C's standard \function{malloc()}. For documentation, read
Andrew M. Kuchling2d2dc9f2000-08-17 00:27:06 +0000858the comments in \file{Include/pymem.h} and
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000859\file{Include/objimpl.h}. For the lengthy discussions during which
860the interface was hammered out, see the Web archives of the 'patches'
861and 'python-dev' lists at python.org.
862
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000863Recent versions of the GUSI development environment for MacOS support
864POSIX threads. Therefore, Python's POSIX threading support now works
865on the Macintosh. Threading support using the user-space GNU \texttt{pth}
866library was also contributed.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000867
868Threading support on Windows was enhanced, too. Windows supports
869thread locks that use kernel objects only in case of contention; in
870the common case when there's no contention, they use simpler functions
871which are an order of magnitude faster. A threaded version of Python
Andrew M. Kuchling730067e2000-06-30 01:44:05 +00008721.5.2 on NT is twice as slow as an unthreaded version; with the 2.0
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000873changes, the difference is only 10\%. These improvements were
874contributed by Yakov Markovitch.
875
Andrew M. Kuchling08d87c62000-07-09 15:05:15 +0000876Python 2.0's source now uses only ANSI C prototypes, so compiling Python now
877requires an ANSI C compiler, and can no longer be done using a compiler that
878only supports K\&R C.
879
Andrew M. Kuchling4d46d382000-09-06 17:58:49 +0000880Previously the Python virtual machine used 16-bit numbers in its
881bytecode, limiting the size of source files. In particular, this
882affected the maximum size of literal lists and dictionaries in Python
Andrew M. Kuchling3ad4e742000-09-27 01:33:41 +0000883source; occasionally people who are generating Python code would run
884into this limit. A patch by Charles G. Waldman raises the limit from
885\verb|2^16| to \verb|2^{32}|.
Andrew M. Kuchling4d46d382000-09-06 17:58:49 +0000886
Andrew M. Kuchling3ad4e742000-09-27 01:33:41 +0000887Three new convenience functions intended for adding constants to a
888module's dictionary at module initialization time were added:
889\function{PyModule_AddObject()}, \function{PyModule_AddIntConstant()},
890and \function{PyModule_AddStringConstant()}. Each of these functions
891takes a module object, a null-terminated C string containing the name
892to be added, and a third argument for the value to be assigned to the
893name. This third argument is, respectively, a Python object, a C
894long, or a C string.
895
896A wrapper API was added for Unix-style signal handlers.
897\function{PyOS_getsig()} gets a signal handler and
898\function{PyOS_setsig()} will set a new handler.
Andrew M. Kuchling4d46d382000-09-06 17:58:49 +0000899
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000900% ======================================================================
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000901\section{Distutils: Making Modules Easy to Install}
902
903Before Python 2.0, installing modules was a tedious affair -- there
904was no way to figure out automatically where Python is installed, or
905what compiler options to use for extension modules. Software authors
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +0000906had to go through an arduous ritual of editing Makefiles and
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000907configuration files, which only really work on Unix and leave Windows
Andrew M. Kuchling3ad4e742000-09-27 01:33:41 +0000908and MacOS unsupported. Python users faced wildly differing
909installation instructions which varied between different extension
910packages, which made adminstering a Python installation something of a
911chore.
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000912
913The SIG for distribution utilities, shepherded by Greg Ward, has
914created the Distutils, a system to make package installation much
915easier. They form the \module{distutils} package, a new part of
916Python's standard library. In the best case, installing a Python
917module from source will require the same steps: first you simply mean
918unpack the tarball or zip archive, and the run ``\code{python setup.py
919install}''. The platform will be automatically detected, the compiler
920will be recognized, C extension modules will be compiled, and the
921distribution installed into the proper directory. Optional
922command-line arguments provide more control over the installation
923process, the distutils package offers many places to override defaults
924-- separating the build from the install, building or installing in
925non-default directories, and more.
926
927In order to use the Distutils, you need to write a \file{setup.py}
928script. For the simple case, when the software contains only .py
929files, a minimal \file{setup.py} can be just a few lines long:
930
931\begin{verbatim}
932from distutils.core import setup
933setup (name = "foo", version = "1.0",
934 py_modules = ["module1", "module2"])
935\end{verbatim}
936
937The \file{setup.py} file isn't much more complicated if the software
938consists of a few packages:
939
940\begin{verbatim}
941from distutils.core import setup
942setup (name = "foo", version = "1.0",
943 packages = ["package", "package.subpackage"])
944\end{verbatim}
945
946A C extension can be the most complicated case; here's an example taken from
947the PyXML package:
948
949
950\begin{verbatim}
951from distutils.core import setup, Extension
952
953expat_extension = Extension('xml.parsers.pyexpat',
954 define_macros = [('XML_NS', None)],
955 include_dirs = [ 'extensions/expat/xmltok',
956 'extensions/expat/xmlparse' ],
957 sources = [ 'extensions/pyexpat.c',
958 'extensions/expat/xmltok/xmltok.c',
959 'extensions/expat/xmltok/xmlrole.c',
960 ]
961 )
962setup (name = "PyXML", version = "0.5.4",
963 ext_modules =[ expat_extension ] )
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000964\end{verbatim}
965
966The Distutils can also take care of creating source and binary
967distributions. The ``sdist'' command, run by ``\code{python setup.py
968sdist}', builds a source distribution such as \file{foo-1.0.tar.gz}.
969Adding new commands isn't difficult, ``bdist_rpm'' and
970``bdist_wininst'' commands have already been contributed to create an
971RPM distribution and a Windows installer for the software,
972respectively. Commands to create other distribution formats such as
973Debian packages and Solaris \file{.pkg} files are in various stages of
974development.
975
976All this is documented in a new manual, \textit{Distributing Python
977Modules}, that joins the basic set of Python documentation.
978
Fred Drake7486c6b2000-10-12 02:49:12 +0000979% ======================================================================
Andrew M. Kuchling6032c482000-10-12 02:37:14 +0000980\section{XML Modules}
Andrew M. Kuchling43737642000-08-30 00:51:02 +0000981
Andrew M. Kuchling6032c482000-10-12 02:37:14 +0000982Python 1.5.2 included a simple XML parser in the form of the
983\module{xmllib} module, contributed by Sjoerd Mullender. Since
9841.5.2's release, two different interfaces for processing XML have
985become common: SAX2 (version 2 of the Simple API for XML) provides an
986event-driven interface with some similarities to \module{xmllib}, and
987the DOM (Document Object Model) provides a tree-based interface,
988transforming an XML document into a tree of nodes that can be
989traversed and modified. Python 2.0 includes a SAX2 interface and a
990stripped-down DOM interface as part of the \module{xml} package.
991Here we will give a brief overview of these new interfaces; consult
992the Python documentation or the source code for complete details.
993The Python XML SIG is also working on improved documentation.
994
995\subsection{SAX2 Support}
996
997SAX defines an event-driven interface for parsing XML. To use SAX,
998you must write a SAX handler class. Handler classes inherit from
999various classes provided by SAX, and override various methods that
1000will then be called by the XML parser. For example, the
1001\method{startElement} and \method{endElement} methods are called for
1002every starting and end tag encountered by the parser, the
1003\method{characters()} method is called for every chunk of character
1004data, and so forth.
1005
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +00001006The advantage of the event-driven approach is that the whole
Andrew M. Kuchling6032c482000-10-12 02:37:14 +00001007document doesn't have to be resident in memory at any one time, which
1008matters if you are processing really huge documents. However, writing
1009the SAX handler class can get very complicated if you're trying to
1010modify the document structure in some elaborate way.
1011
1012For example, this little example program defines a handler that prints
1013a message for every starting and ending tag, and then parses the file
1014\file{hamlet.xml} using it:
1015
1016\begin{verbatim}
1017from xml import sax
1018
1019class SimpleHandler(sax.ContentHandler):
1020 def startElement(self, name, attrs):
1021 print 'Start of element:', name, attrs.keys()
1022
1023 def endElement(self, name):
1024 print 'End of element:', name
1025
1026# Create a parser object
1027parser = sax.make_parser()
1028
1029# Tell it what handler to use
1030handler = SimpleHandler()
1031parser.setContentHandler( handler )
1032
1033# Parse a file!
1034parser.parse( 'hamlet.xml' )
1035\end{verbatim}
1036
1037For more information, consult the Python documentation, or the XML
Andrew M. Kuchling95467722002-05-02 14:48:26 +00001038HOWTO at \url{http://pyxml.sourceforge.net/topics/howto/xml-howto.html}.
Andrew M. Kuchling6032c482000-10-12 02:37:14 +00001039
1040\subsection{DOM Support}
1041
1042The Document Object Model is a tree-based representation for an XML
1043document. A top-level \class{Document} instance is the root of the
1044tree, and has a single child which is the top-level \class{Element}
1045instance. This \class{Element} has children nodes representing
1046character data and any sub-elements, which may have further children
1047of their own, and so forth. Using the DOM you can traverse the
1048resulting tree any way you like, access element and attribute values,
1049insert and delete nodes, and convert the tree back into XML.
1050
1051The DOM is useful for modifying XML documents, because you can create
1052a DOM tree, modify it by adding new nodes or rearranging subtrees, and
1053then produce a new XML document as output. You can also construct a
1054DOM tree manually and convert it to XML, which can be a more flexible
1055way of producing XML output than simply writing
1056\code{<tag1>}...\code{</tag1>} to a file.
1057
1058The DOM implementation included with Python lives in the
1059\module{xml.dom.minidom} module. It's a lightweight implementation of
1060the Level 1 DOM with support for XML namespaces. The
1061\function{parse()} and \function{parseString()} convenience
1062functions are provided for generating a DOM tree:
1063
1064\begin{verbatim}
1065from xml.dom import minidom
1066doc = minidom.parse('hamlet.xml')
1067\end{verbatim}
1068
1069\code{doc} is a \class{Document} instance. \class{Document}, like all
1070the other DOM classes such as \class{Element} and \class{Text}, is a
1071subclass of the \class{Node} base class. All the nodes in a DOM tree
1072therefore support certain common methods, such as \method{toxml()}
1073which returns a string containing the XML representation of the node
1074and its children. Each class also has special methods of its own; for
1075example, \class{Element} and \class{Document} instances have a method
1076to find all child elements with a given tag name. Continuing from the
1077previous 2-line example:
1078
1079\begin{verbatim}
1080perslist = doc.getElementsByTagName( 'PERSONA' )
1081print perslist[0].toxml()
1082print perslist[1].toxml()
1083\end{verbatim}
1084
1085For the \textit{Hamlet} XML file, the above few lines output:
1086
1087\begin{verbatim}
1088<PERSONA>CLAUDIUS, king of Denmark. </PERSONA>
1089<PERSONA>HAMLET, son to the late, and nephew to the present king.</PERSONA>
1090\end{verbatim}
1091
1092The root element of the document is available as
1093\code{doc.documentElement}, and its children can be easily modified
1094by deleting, adding, or removing nodes:
1095
1096\begin{verbatim}
1097root = doc.documentElement
1098
1099# Remove the first child
1100root.removeChild( root.childNodes[0] )
1101
1102# Move the new first child to the end
1103root.appendChild( root.childNodes[0] )
1104
1105# Insert the new first child (originally,
1106# the third child) before the 20th child.
1107root.insertBefore( root.childNodes[0], root.childNodes[20] )
1108\end{verbatim}
1109
1110Again, I will refer you to the Python documentation for a complete
1111listing of the different \class{Node} classes and their various methods.
1112
1113\subsection{Relationship to PyXML}
1114
1115The XML Special Interest Group has been working on XML-related Python
1116code for a while. Its code distribution, called PyXML, is available
1117from the SIG's Web pages at \url{http://www.python.org/sigs/xml-sig/}.
1118The PyXML distribution also used the package name \samp{xml}. If
1119you've written programs that used PyXML, you're probably wondering
1120about its compatibility with the 2.0 \module{xml} package.
1121
1122The answer is that Python 2.0's \module{xml} package isn't compatible
1123with PyXML, but can be made compatible by installing a recent version
1124PyXML. Many applications can get by with the XML support that is
1125included with Python 2.0, but more complicated applications will
1126require that the full PyXML package will be installed. When
1127installed, PyXML versions 0.6.0 or greater will replace the
1128\module{xml} package shipped with Python, and will be a strict
1129superset of the standard package, adding a bunch of additional
1130features. Some of the additional features in PyXML include:
1131
1132\begin{itemize}
1133\item 4DOM, a full DOM implementation
Andrew M. Kuchlingf1551702000-10-16 14:19:21 +00001134from FourThought, Inc.
Andrew M. Kuchling6032c482000-10-12 02:37:14 +00001135\item The xmlproc validating parser, written by Lars Marius Garshol.
1136\item The \module{sgmlop} parser accelerator module, written by Fredrik Lundh.
1137\end{itemize}
Andrew M. Kuchling43737642000-08-30 00:51:02 +00001138
1139% ======================================================================
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +00001140\section{Module changes}
1141
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +00001142Lots of improvements and bugfixes were made to Python's extensive
1143standard library; some of the affected modules include
1144\module{readline}, \module{ConfigParser}, \module{cgi},
1145\module{calendar}, \module{posix}, \module{readline}, \module{xmllib},
1146\module{aifc}, \module{chunk, wave}, \module{random}, \module{shelve},
1147and \module{nntplib}. Consult the CVS logs for the exact
1148patch-by-patch details.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +00001149
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +00001150Brian Gallew contributed OpenSSL support for the \module{socket}
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001151module. OpenSSL is an implementation of the Secure Socket Layer,
1152which encrypts the data being sent over a socket. When compiling
1153Python, you can edit \file{Modules/Setup} to include SSL support,
1154which adds an additional function to the \module{socket} module:
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +00001155\function{socket.ssl(\var{socket}, \var{keyfile}, \var{certfile})},
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001156which takes a socket object and returns an SSL socket. The
1157\module{httplib} and \module{urllib} modules were also changed to
1158support ``https://'' URLs, though no one has implemented FTP or SMTP
1159over SSL.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +00001160
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +00001161The \module{httplib} module has been rewritten by Greg Stein to
1162support HTTP/1.1. Backward compatibility with the 1.5 version of
1163\module{httplib} is provided, though using HTTP/1.1 features such as
1164pipelining will require rewriting code to use a different set of
1165interfaces.
1166
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +00001167The \module{Tkinter} module now supports Tcl/Tk version 8.1, 8.2, or
11688.3, and support for the older 7.x versions has been dropped. The
Andrew M. Kuchling791b3662000-07-01 15:04:18 +00001169Tkinter module now supports displaying Unicode strings in Tk widgets.
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +00001170Also, Fredrik Lundh contributed an optimization which makes operations
1171like \code{create_line} and \code{create_polygon} much faster,
Andrew M. Kuchling791b3662000-07-01 15:04:18 +00001172especially when using lots of coordinates.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +00001173
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +00001174The \module{curses} module has been greatly extended, starting from
1175Oliver Andrich's enhanced version, to provide many additional
1176functions from ncurses and SYSV curses, such as colour, alternative
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +00001177character set support, pads, and mouse support. This means the module
1178is no longer compatible with operating systems that only have BSD
1179curses, but there don't seem to be any currently maintained OSes that
1180fall into this category.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +00001181
Andrew M. Kuchling730067e2000-06-30 01:44:05 +00001182As mentioned in the earlier discussion of 2.0's Unicode support, the
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001183underlying implementation of the regular expressions provided by the
1184\module{re} module has been changed. SRE, a new regular expression
1185engine written by Fredrik Lundh and partially funded by Hewlett
1186Packard, supports matching against both 8-bit strings and Unicode
1187strings.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +00001188
1189% ======================================================================
1190\section{New modules}
1191
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001192A number of new modules were added. We'll simply list them with brief
Andrew M. Kuchling730067e2000-06-30 01:44:05 +00001193descriptions; consult the 2.0 documentation for the details of a
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001194particular module.
1195
1196\begin{itemize}
1197
Andrew M. Kuchling62cdd962000-06-30 12:46:41 +00001198\item{\module{atexit}}:
1199For registering functions to be called before the Python interpreter exits.
1200Code that currently sets
1201\code{sys.exitfunc} directly should be changed to
1202use the \module{atexit} module instead, importing \module{atexit}
1203and calling \function{atexit.register()} with
1204the function to be called on exit.
1205(Contributed by Skip Montanaro.)
1206
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001207\item{\module{codecs}, \module{encodings}, \module{unicodedata}:} Added as part of the new Unicode support.
1208
Andrew M. Kuchlingfed4f1e2000-07-01 12:33:43 +00001209\item{\module{filecmp}:} Supersedes the old \module{cmp}, \module{cmpcache} and
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001210\module{dircmp} modules, which have now become deprecated.
Andrew M. Kuchlingc0328f02000-06-10 15:11:20 +00001211(Contributed by Gordon MacMillan and Moshe Zadka.)
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001212
Andrew M. Kuchlingec1722e2000-10-12 03:04:22 +00001213\item{\module{gettext}:} This module provides internationalization
1214(I18N) and localization (L10N) support for Python programs by
1215providing an interface to the GNU gettext message catalog library.
1216(Integrated by Barry Warsaw, from separate contributions by Martin von
1217Loewis, Peter Funk, and James Henstridge.)
1218
Andrew M. Kuchling35e8afb2000-07-08 12:06:31 +00001219\item{\module{linuxaudiodev}:} Support for the \file{/dev/audio}
1220device on Linux, a twin to the existing \module{sunaudiodev} module.
Andrew M. Kuchlingec1722e2000-10-12 03:04:22 +00001221(Contributed by Peter Bosch, with fixes by Jeremy Hylton.)
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001222
1223\item{\module{mmap}:} An interface to memory-mapped files on both
1224Windows and Unix. A file's contents can be mapped directly into
1225memory, at which point it behaves like a mutable string, so its
1226contents can be read and modified. They can even be passed to
1227functions that expect ordinary strings, such as the \module{re}
1228module. (Contributed by Sam Rushing, with some extensions by
1229A.M. Kuchling.)
1230
Andrew M. Kuchling35e8afb2000-07-08 12:06:31 +00001231\item{\module{pyexpat}:} An interface to the Expat XML parser.
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001232(Contributed by Paul Prescod.)
1233
1234\item{\module{robotparser}:} Parse a \file{robots.txt} file, which is
1235used for writing Web spiders that politely avoid certain areas of a
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +00001236Web site. The parser accepts the contents of a \file{robots.txt} file,
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001237builds a set of rules from it, and can then answer questions about
1238the fetchability of a given URL. (Contributed by Skip Montanaro.)
1239
1240\item{\module{tabnanny}:} A module/script to
Andrew M. Kuchling5e08a012000-09-04 17:59:27 +00001241check Python source code for ambiguous indentation.
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001242(Contributed by Tim Peters.)
1243
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +00001244\item{\module{UserString}:} A base class useful for deriving objects that behave like strings.
1245
Andrew M. Kuchling08d87c62000-07-09 15:05:15 +00001246\item{\module{webbrowser}:} A module that provides a platform independent
1247way to launch a web browser on a specific URL. For each platform, various
1248browsers are tried in a specific order. The user can alter which browser
1249is launched by setting the \var{BROWSER} environment variable.
1250(Originally inspired by Eric S. Raymond's patch to \module{urllib}
1251which added similar functionality, but
1252the final module comes from code originally
1253implemented by Fred Drake as \file{Tools/idle/BrowserControl.py},
1254and adapted for the standard library by Fred.)
1255
Andrew M. Kuchlingd500e442000-09-06 12:30:25 +00001256\item{\module{_winreg}:} An interface to the
Andrew M. Kuchlingfed4f1e2000-07-01 12:33:43 +00001257Windows registry. \module{_winreg} is an adaptation of functions that
1258have been part of PythonWin since 1995, but has now been added to the core
Andrew M. Kuchlingd500e442000-09-06 12:30:25 +00001259distribution, and enhanced to support Unicode.
1260\module{_winreg} was written by Bill Tutt and Mark Hammond.
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001261
1262\item{\module{zipfile}:} A module for reading and writing ZIP-format
1263archives. These are archives produced by \program{PKZIP} on
1264DOS/Windows or \program{zip} on Unix, not to be confused with
1265\program{gzip}-format files (which are supported by the \module{gzip}
1266module)
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001267(Contributed by James C. Ahlstrom.)
1268
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +00001269\item{\module{imputil}:} A module that provides a simpler way for
1270writing customised import hooks, in comparison to the existing
1271\module{ihooks} module. (Implemented by Greg Stein, with much
1272discussion on python-dev along the way.)
1273
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001274\end{itemize}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +00001275
1276% ======================================================================
1277\section{IDLE Improvements}
1278
Andrew M. Kuchlingc0328f02000-06-10 15:11:20 +00001279IDLE is the official Python cross-platform IDE, written using Tkinter.
Andrew M. Kuchling730067e2000-06-30 01:44:05 +00001280Python 2.0 includes IDLE 0.6, which adds a number of new features and
Andrew M. Kuchlingc0328f02000-06-10 15:11:20 +00001281improvements. A partial list:
1282
1283\begin{itemize}
1284\item UI improvements and optimizations,
1285especially in the area of syntax highlighting and auto-indentation.
1286
1287\item The class browser now shows more information, such as the top
Andrew M. Kuchling730067e2000-06-30 01:44:05 +00001288level functions in a module.
Andrew M. Kuchlingc0328f02000-06-10 15:11:20 +00001289
1290\item Tab width is now a user settable option. When opening an existing Python
1291file, IDLE automatically detects the indentation conventions, and adapts.
1292
1293\item There is now support for calling browsers on various platforms,
1294used to open the Python documentation in a browser.
1295
1296\item IDLE now has a command line, which is largely similar to
1297the vanilla Python interpreter.
1298
1299\item Call tips were added in many places.
1300
1301\item IDLE can now be installed as a package.
1302
1303\item In the editor window, there is now a line/column bar at the bottom.
1304
1305\item Three new keystroke commands: Check module (Alt-F5), Import
Andrew M. Kuchling730067e2000-06-30 01:44:05 +00001306module (F5) and Run script (Ctrl-F5).
Andrew M. Kuchlingc0328f02000-06-10 15:11:20 +00001307
1308\end{itemize}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +00001309
1310% ======================================================================
1311\section{Deleted and Deprecated Modules}
1312
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001313A few modules have been dropped because they're obsolete, or because
1314there are now better ways to do the same thing. The \module{stdwin}
1315module is gone; it was for a platform-independent windowing toolkit
1316that's no longer developed.
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001317
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +00001318A number of modules have been moved to the
1319\file{lib-old} subdirectory:
1320\module{cmp}, \module{cmpcache}, \module{dircmp}, \module{dump},
1321\module{find}, \module{grep}, \module{packmail},
1322\module{poly}, \module{util}, \module{whatsound}, \module{zmod}.
1323If you have code which relies on a module that's been moved to
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001324\file{lib-old}, you can simply add that directory to \code{sys.path}
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +00001325to get them back, but you're encouraged to update any code that uses
1326these modules.
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001327
Andrew M. Kuchling730067e2000-06-30 01:44:05 +00001328\section{Acknowledgements}
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +00001329
Andrew M. Kuchlinga6161ed2000-07-01 00:23:02 +00001330The authors would like to thank the following people for offering
Andrew M. Kuchling2a159802002-05-02 14:37:14 +00001331suggestions on various drafts of this article: David Bolen, Mark
1332Hammond, Gregg Hauser, Jeremy Hylton, Fredrik Lundh, Detlef Lannert,
1333Aahz Maruch, Skip Montanaro, Vladimir Marangozov, Tobias Polzin, Guido
1334van Rossum, Neil Schemenauer, and Russ Schmidt.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +00001335
1336\end{document}