blob: 969cdd077aba547dcad3dca8029c8031afbae7ca [file] [log] [blame]
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +00001\documentclass{howto}
2
3\title{What's New in Python 1.6}
Andrew M. Kuchling662d76e2000-06-25 14:32:48 +00004\release{0.03}
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +00005\author{A.M. Kuchling and Moshe Zadka}
6\authoraddress{\email{amk1@bigfoot.com}, \email{moshez@math.huji.ac.il} }
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +00007\begin{document}
8\maketitle\tableofcontents
9
10\section{Introduction}
11
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +000012{\large This is a draft document; please report inaccuracies and
13omissions to the authors. This document should not be treated as
14definitive; features described here might be removed or changed before
15Python 1.6final. \\
16
17XXX marks locations in the text where fact-checking or rewriting is
18still needed.
19
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +000020}
21
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000022A new release of Python, version 1.6, will be released some time this
23summer. Alpha versions are already available from
24\url{http://www.python.org/1.6/}. This article talks about the
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000025exciting new features in 1.6, highlights some other useful changes,
26and points out a few incompatible changes that may require rewriting
27code.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000028
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000029Python's development never completely stops between releases, and a
30steady flow of bug fixes and improvements are always being submitted.
31A host of minor fixes, a few optimizations, additional docstrings, and
32better error messages went into 1.6; to list them all would be
33impossible, but they're certainly significant. Consult the
34publicly-available CVS logs if you want to see the full list.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000035
36% ======================================================================
37\section{Unicode}
38
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000039The largest new feature in Python 1.6 is a new fundamental data type:
40Unicode strings. Unicode uses 16-bit numbers to represent characters
41instead of the 8-bit number used by ASCII, meaning that 65,536
42distinct characters can be supported.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000043
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000044The final interface for Unicode support was arrived at through
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000045countless often-stormy discussions on the python-dev mailing list, and
46mostly implemented by Marc-Andr\'e Lemburg. A detailed explanation of
47the interface is in the file
48\file{Misc/unicode.txt} in the Python source distribution; it's also
49available on the Web at
50\url{http://starship.python.net/crew/lemburg/unicode-proposal.txt}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000051This article will simply cover the most significant points from the
52full interface.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000053
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000054In Python source code, Unicode strings are written as
55\code{u"string"}. Arbitrary Unicode characters can be written using a
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000056new escape sequence, \code{\e u\var{HHHH}}, where \var{HHHH} is a
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000574-digit hexadecimal number from 0000 to FFFF. The existing
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000058\code{\e x\var{HHHH}} escape sequence can also be used, and octal
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000059escapes can be used for characters up to U+01FF, which is represented
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000060by \code{\e 777}.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000061
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000062Unicode strings, just like regular strings, are an immutable sequence
Andrew M. Kuchling662d76e2000-06-25 14:32:48 +000063type. They can be indexed and sliced, but not modified in place.
64Unicode strings have an \method{encode( \optional{\var{encoding}} )} method
65that returns an 8-bit string in the desired encoding. Encodings are
66named by strings, such as \code{'ascii'}, \code{'utf-8'},
67\code{'iso-8859-1'}, or whatever. A codec API is defined for
68implementing and registering new encodings that are then available
69throughout a Python program. If an encoding isn't specified, the
70default encoding is usually 7-bit ASCII, though it can be changed for
71your Python installation by calling the
Andrew M. Kuchlingc0328f02000-06-10 15:11:20 +000072\function{sys.setdefaultencoding(\var{encoding})} function in a
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +000073customised version of \file{site.py}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000074
75Combining 8-bit and Unicode strings always coerces to Unicode, using
76the default ASCII encoding; the result of \code{'a' + u'bc'} is
Andrew M. Kuchling7f6270d2000-06-09 02:48:18 +000077\code{u'abc'}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000078
79New built-in functions have been added, and existing built-ins
80modified to support Unicode:
81
82\begin{itemize}
83\item \code{unichr(\var{ch})} returns a Unicode string 1 character
84long, containing the character \var{ch}.
85
86\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.
87
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000088\item \code{unicode(\var{string}, \optional{\var{encoding},}
89\optional{\var{errors}} ) } creates a Unicode string from an 8-bit
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000090string. \code{encoding} is a string naming the encoding to use.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000091The \code{errors} parameter specifies the treatment of characters that
92are invalid for the current encoding; passing \code{'strict'} as the
93value causes an exception to be raised on any encoding error, while
94\code{'ignore'} causes errors to be silently ignored and
95\code{'replace'} uses U+FFFD, the official replacement character, in
96case of any problems.
97
98\end{itemize}
99
100A new module, \module{unicodedata}, provides an interface to Unicode
101character properties. For example, \code{unicodedata.category(u'A')}
102returns the 2-character string 'Lu', the 'L' denoting it's a letter,
103and 'u' meaning that it's uppercase.
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000104\code{u.bidirectional(u'\e x0660')} returns 'AN', meaning that U+0660 is
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000105an Arabic number.
106
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000107The \module{codecs} module contains functions to look up existing encodings
108and register new ones. Unless you want to implement a
109new encoding, you'll most often use the
110\function{codecs.lookup(\var{encoding})} function, which returns a
1114-element tuple: \code{(\var{encode_func},
112\var{decode_func}, \var{stream_reader}, \var{stream_writer})}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000113
114\begin{itemize}
115\item \var{encode_func} is a function that takes a Unicode string, and
116returns a 2-tuple \code{(\var{string}, \var{length})}. \var{string}
117is an 8-bit string containing a portion (perhaps all) of the Unicode
118string converted into the given encoding, and \var{length} tells you how much of the Unicode string was converted.
119
120\item \var{decode_func} is the mirror of \var{encode_func},
121taking a Unicode string and
122returns a 2-tuple \code{(\var{ustring}, \var{length})} containing a Unicode string
123and \var{length} telling you how much of the string was consumed.
124
125\item \var{stream_reader} is a class that supports decoding input from
126a stream. \var{stream_reader(\var{file_obj})} returns an object that
127supports the \method{read()}, \method{readline()}, and
128\method{readlines()} methods. These methods will all translate from
129the given encoding and return Unicode strings.
130
131\item \var{stream_writer}, similarly, is a class that supports
132encoding output to a stream. \var{stream_writer(\var{file_obj})}
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +0000133returns an object that supports the \method{write()} and
134\method{writelines()} methods. These methods expect Unicode strings,
135translating them to the given encoding on output.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000136\end{itemize}
137
138For example, the following code writes a Unicode string into a file,
139encoding it as UTF-8:
140
141\begin{verbatim}
142import codecs
143
144unistr = u'\u0660\u2000ab ...'
145
146(UTF8_encode, UTF8_decode,
147 UTF8_streamreader, UTF8_streamwriter) = codecs.lookup('UTF-8')
148
149output = UTF8_streamwriter( open( '/tmp/output', 'wb') )
150output.write( unistr )
151output.close()
152\end{verbatim}
153
154The following code would then read UTF-8 input from the file:
155
156\begin{verbatim}
157input = UTF8_streamread( open( '/tmp/output', 'rb') )
158print repr(input.read())
159input.close()
160\end{verbatim}
161
162Unicode-aware regular expressions are available through the
163\module{re} module, which has a new underlying implementation called
164SRE written by Fredrik Lundh of Secret Labs AB.
165
Andrew M. Kuchlingc0328f02000-06-10 15:11:20 +0000166A \code{-U} command line option was added which causes the Python
167compiler to interpret all string literals as Unicode string literals.
168This is intended to be used in testing and future-proofing your Python
169code, since some future version of Python may drop support for 8-bit
170strings and provide only Unicode strings.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000171
172% ======================================================================
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000173\section{Distutils: Making Modules Easy to Install}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000174
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000175Before Python 1.6, installing modules was a tedious affair -- there
176was no way to figure out automatically where Python is installed, or
177what compiler options to use for extension modules. Software authors
178had to go through an ardous ritual of editing Makefiles and
179configuration files, which only really work on Unix and leave Windows
180and MacOS unsupported. Software users faced wildly differing
181installation instructions
182
183The SIG for distribution utilities, shepherded by Greg Ward, has
184created the Distutils, a system to make package installation much
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000185easier. They form the \module{distutils} package, a new part of
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000186Python's standard library. In the best case, installing a Python
187module from source will require the same steps: first you simply mean
188unpack the tarball or zip archive, and the run ``\code{python setup.py
189install}''. The platform will be automatically detected, the compiler
190will be recognized, C extension modules will be compiled, and the
191distribution installed into the proper directory. Optional
192command-line arguments provide more control over the installation
193process, the distutils package offers many places to override defaults
194-- separating the build from the install, building or installing in
195non-default directories, and more.
196
197In order to use the Distutils, you need to write a \file{setup.py}
198script. For the simple case, when the software contains only .py
199files, a minimal \file{setup.py} can be just a few lines long:
200
201\begin{verbatim}
202from distutils.core import setup
203setup (name = "foo", version = "1.0",
204 py_modules = ["module1", "module2"])
205\end{verbatim}
206
207The \file{setup.py} file isn't much more complicated if the software
208consists of a few packages:
209
210\begin{verbatim}
211from distutils.core import setup
212setup (name = "foo", version = "1.0",
213 packages = ["package", "package.subpackage"])
214\end{verbatim}
215
216A C extension can be the most complicated case; here's an example taken from
217the PyXML package:
218
219
220\begin{verbatim}
221from distutils.core import setup, Extension
222
223expat_extension = Extension('xml.parsers.pyexpat',
224 define_macros = [('XML_NS', None)],
225 include_dirs = [ 'extensions/expat/xmltok',
226 'extensions/expat/xmlparse' ],
227 sources = [ 'extensions/pyexpat.c',
228 'extensions/expat/xmltok/xmltok.c',
229 'extensions/expat/xmltok/xmlrole.c',
230 ]
231 )
232setup (name = "PyXML", version = "0.5.4",
233 ext_modules =[ expat_extension ] )
234
235\end{verbatim}
236
237The Distutils can also take care of creating source and binary
238distributions. The ``sdist'' command, run by ``\code{python setup.py
239sdist}', builds a source distribution such as \file{foo-1.0.tar.gz}.
240Adding new commands isn't difficult, and a ``bdist_rpm'' command has
241already been contributed to create an RPM distribution for the
242software. Commands to create Windows installer programs, Debian
243packages, and Solaris .pkg files have been discussed and are in
244various stages of development.
245
246All this is documented in a new manual, \textit{Distributing Python
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +0000247Modules}, that joins the basic set of Python documentation.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000248
249% ======================================================================
250\section{String Methods}
251
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000252Until now string-manipulation functionality was in the \module{string}
253Python module, which was usually a front-end for the \module{strop}
254module written in C. The addition of Unicode posed a difficulty for
255the \module{strop} module, because the functions would all need to be
256rewritten in order to accept either 8-bit or Unicode strings. For
257functions such as \function{string.replace()}, which takes 3 string
258arguments, that means eight possible permutations, and correspondingly
259complicated code.
260
261Instead, Python 1.6 pushes the problem onto the string type, making
262string manipulation functionality available through methods on both
2638-bit strings and Unicode strings.
264
265\begin{verbatim}
266>>> 'andrew'.capitalize()
267'Andrew'
268>>> 'hostname'.replace('os', 'linux')
269'hlinuxtname'
270>>> 'moshe'.find('sh')
2712
272\end{verbatim}
273
274One thing that hasn't changed, April Fools' jokes notwithstanding, is
275that Python strings are immutable. Thus, the string methods return new
276strings, and do not modify the string on which they operate.
277
278The old \module{string} module is still around for backwards
279compatibility, but it mostly acts as a front-end to the new string
280methods.
281
282Two methods which have no parallel in pre-1.6 versions, although they
283did exist in JPython for quite some time, are \method{startswith()}
284and \method{endswith}. \code{s.startswith(t)} is equivalent to \code{s[:len(t)]
285== t}, while \code{s.endswith(t)} is equivalent to \code{s[-len(t):] == t}.
286
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000287(XXX what'll happen to join? is this even worth mentioning?) One
288other method which deserves special mention is \method{join}. The
289\method{join} method of a string receives one parameter, a sequence of
290strings, and is equivalent to the \function{string.join} function from
291the old \module{string} module, with the arguments reversed. In other
292words, \code{s.join(seq)} is equivalent to the old
293\code{string.join(seq, s)}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000294
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000295% ======================================================================
296\section{Porting to 1.6}
297
298New Python releases try hard to be compatible with previous releases,
299and the record has been pretty good. However, some changes are
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000300considered useful enough, often fixing initial design decisions that
301turned to be actively mistaken, that breaking backward compatibility
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000302can't always be avoided. This section lists the changes in Python 1.6
303that may cause old Python code to break.
304
305The change which will probably break the most code is tightening up
306the arguments accepted by some methods. Some methods would take
307multiple arguments and treat them as a tuple, particularly various
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000308list methods such as \method{.append()} and \method{.insert()}.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000309In earlier versions of Python, if \code{L} is a list, \code{L.append(
3101,2 )} appends the tuple \code{(1,2)} to the list. In Python 1.6 this
311causes a \exception{TypeError} exception to be raised, with the
312message: 'append requires exactly 1 argument; 2 given'. The fix is to
313simply add an extra set of parentheses to pass both values as a tuple:
314\code{L.append( (1,2) )}.
315
316The earlier versions of these methods were more forgiving because they
317used an old function in Python's C interface to parse their arguments;
3181.6 modernizes them to use \function{PyArg_ParseTuple}, the current
319argument parsing function, which provides more helpful error messages
320and treats multi-argument calls as errors. If you absolutely must use
3211.6 but can't fix your code, you can edit \file{Objects/listobject.c}
322and define the preprocessor symbol \code{NO_STRICT_LIST_APPEND} to
323preserve the old behaviour; this isn't recommended.
324
325Some of the functions in the \module{socket} module are still
326forgiving in this way. For example, \function{socket.connect(
327('hostname', 25) )} is the correct form, passing a tuple representing
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000328an IP address, but \function{socket.connect( 'hostname', 25 )} also
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000329works. \function{socket.connect_ex()} and \function{socket.bind()} are
330similarly easy-going. 1.6alpha1 tightened these functions up, but
331because the documentation actually used the erroneous multiple
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000332argument form, many people wrote code which would break with the
333stricter checking. GvR backed out the changes in the face of public
334reaction, so for the\module{socket} module, the documentation was
335fixed and the multiple argument form is simply marked as deprecated;
336it \emph{will} be tightened up again in a future Python version.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000337
338Some work has been done to make integers and long integers a bit more
339interchangeable. In 1.5.2, large-file support was added for Solaris,
340to allow reading files larger than 2Gb; this made the \method{tell()}
341method of file objects return a long integer instead of a regular
342integer. Some code would subtract two file offsets and attempt to use
343the result to multiply a sequence or slice a string, but this raised a
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000344\exception{TypeError}. In 1.6, long integers can be used to multiply
345or slice a sequence, and it'll behave as you'd intuitively expect it
346to; \code{3L * 'abc'} produces 'abcabcabc', and \code{
347(0,1,2,3)[2L:4L]} produces (2,3). Long integers can also be used in
348various new places where previously only integers were accepted, such
349as in the \method{seek()} method of file objects.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000350
351The subtlest long integer change of all is that the \function{str()}
352of a long integer no longer has a trailing 'L' character, though
353\function{repr()} still includes it. The 'L' annoyed many people who
354wanted to print long integers that looked just like regular integers,
355since they had to go out of their way to chop off the character. This
356is no longer a problem in 1.6, but code which assumes the 'L' is
357there, and does \code{str(longval)[:-1]} will now lose the final
358digit.
359
360Taking the \function{repr()} of a float now uses a different
361formatting precision than \function{str()}. \function{repr()} uses
Andrew M. Kuchling662d76e2000-06-25 14:32:48 +0000362\code{\%.17g} format string for C's \function{sprintf()}, while
363\function{str()} uses \code{\%.12g} as before. The effect is that
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000364\function{repr()} may occasionally show more decimal places than
365\function{str()}, for numbers
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +0000366For example, the number 8.1 can't be represented exactly in binary, so
367\code{repr(8.1)} is \code{'8.0999999999999996'}, while str(8.1) is
368\code{'8.1'}.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000369
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +0000370%The \code{-X} command-line option, which turns all standard exceptions
371%into strings instead of classes, has been removed.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000372
373% ======================================================================
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +0000374\section{Optional Collection of Cycles}
375
376The C implementation of Python uses reference counting to implement
377garbage collection. Every Python object maintains a count of the
378number of references pointing to itself, and adjusts the count as
379references are created or destroyed. Once the reference count reaches
380zero, the object is no longer accessible, since you need to have a
381reference to an object to access it, and if the count is zero, no
382references exist any longer.
383
384Reference counting has some pleasant properties: it's easy to
385understand and implement, and the resulting implementation is
386portable, fairly fast, and reacts well with other libraries that
387implement their own memory handling schemes. The major problem with
388reference counting is that it sometimes doesn't realise that objects
389are no longer accessible, resulting in a memory leak. This happens
390when there are cycles of references.
391
392Consider the simplest possible cycle,
393a class instance which has a reference to itself:
394
395\begin{verbatim}
396instance = SomeClass()
397instance.myself = instance
398\end{verbatim}
399
400After the above two lines of code have been executed, the reference
401count of \code{instance} is 2; one reference is from the variable
402named \samp{'instance'}, and the other is from the \samp{myself}
403attribute of the instance.
404
405If the next line of code is \code{del instance}, what happens? The
406reference count of \code{instance} is decreased by 1, so it has a
407reference count of 1; the reference in the \samp{myself} attribute
408still exists. Yet the instance is no longer accessible through Python
409code, and it could be deleted. Several objects can participate in a
410cycle if they have references to each other, causing all of the
411objects to be leaked.
412
413An experimental step has been made toward fixing this problem. When
414compiling Python, the \code{--with-cycle-gc} (XXX correct option
415flag?) option can be specified. This causes a cycle detection
416algorithm to be periodically executed, which looks for inaccessible
417cycles and deletes the objects involved.
418
419Why isn't this enabled by default? Running the cycle detection
420algorithm takes some time, and some tuning will be required to
421minimize the overhead cost. It's not yet obvious how much performance
422is lost, because benchmarking this is tricky and depends sensitively
423on how often the program creates and destroys objects. XXX is this
424actually the correct reason? Or is it fear of breaking software that
425runs happily while leaving garbage?
426
427Several people worked on this problem. Early versions were written by
428XXX1, XXX2. (I vaguely remember several people writing first cuts at this.
429Anyone recall who?)
430The implementation that's in Python 1.6 is a rewritten version, this
431time done by Neil Schemenauer. Lots of other people offered
432suggestions along the way, such as (in alphabetical order)
433Marc-Andr\'e Lemburg, Tim Peters, Greg Stein, Eric Tiedemann. The
434March 2000 archives of the python-dev mailing list contain most of the
435relevant discussion, especially in the threads titled ``Reference
436cycle collection for Python'' and ``Finalization again''.
437
438
439% ======================================================================
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000440\section{Core Changes}
441
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000442Various minor changes have been made to Python's syntax and built-in
443functions. None of the changes are very far-reaching, but they're
444handy conveniences.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000445
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000446A change to syntax makes it more convenient to call a given function
447with a tuple of arguments and/or a dictionary of keyword arguments.
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000448In Python 1.5 and earlier, you do this with the \function{apply()}
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000449built-in function: \code{apply(f, \var{args}, \var{kw})} calls the
450function \function{f()} with the argument tuple \var{args} and the
451keyword arguments in the dictionary \var{kw}. Thanks to a patch from
452Greg Ewing, 1.6 adds \code{f(*\var{args}, **\var{kw})} as a shorter
453and clearer way to achieve the same effect. This syntax is
454symmetrical with the syntax for defining functions:
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000455
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000456\begin{verbatim}
457def f(*args, **kw):
458 # args is a tuple of positional args,
459 # kw is a dictionary of keyword args
460 ...
461\end{verbatim}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000462
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000463A new format style is available when using the \code{\%} operator.
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000464'\%r' will insert the \function{repr()} of its argument. This was
465also added from symmetry considerations, this time for symmetry with
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000466the existing '\%s' format style, which inserts the \function{str()} of
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000467its argument. For example, \code{'\%r \%s' \% ('abc', 'abc')} returns a
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000468string containing \verb|'abc' abc|.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000469
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000470The \function{int()} and \function{long()} functions now accept an
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000471optional ``base'' parameter when the first argument is a string.
472\code{int('123', 10)} returns 123, while \code{int('123', 16)} returns
473291. \code{int(123, 16)} raises a \exception{TypeError} exception
474with the message ``can't convert non-string with explicit base''.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000475
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000476Previously there was no way to implement a class that overrode
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000477Python's built-in \keyword{in} operator and implemented a custom
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000478version. \code{\var{obj} in \var{seq}} returns true if \var{obj} is
479present in the sequence \var{seq}; Python computes this by simply
480trying every index of the sequence until either \var{obj} is found or
481an \exception{IndexError} is encountered. Moshe Zadka contributed a
482patch which adds a \method{__contains__} magic method for providing a
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000483custom implementation for \keyword{in}. Additionally, new built-in
484objects written in C can define what \keyword{in} means for them via a
485new slot in the sequence protocol.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000486
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000487Earlier versions of Python used a recursive algorithm for deleting
488objects. Deeply nested data structures could cause the interpreter to
489fill up the C stack and crash; Christian Tismer rewrote the deletion
490logic to fix this problem. On a related note, comparing recursive
491objects recursed infinitely and crashed; Jeremy Hylton rewrote the
492code to no longer crash, producing a useful result instead. For
493example, after this code:
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000494
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000495\begin{verbatim}
496a = []
497b = []
498a.append(a)
499b.append(b)
500\end{verbatim}
501
502The comparison \code{a==b} returns true, because the two recursive
503data structures are isomorphic.
504\footnote{See the thread ``trashcan and PR\#7'' in the April 2000 archives of the python-dev mailing list for the discussion leading up to this implementation, and some useful relevant links.
505%http://www.python.org/pipermail/python-dev/2000-April/004834.html
506}
507
508Work has been done on porting Python to 64-bit Windows on the Itanium
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000509processor, mostly by Trent Mick of ActiveState. (Confusingly, \code{sys.platform} is still \code{'win32'} on
510Win64 because it seems that for ease of porting, MS Visual C++ treats code
511as 32 bit.
512) PythonWin also supports Windows CE; see the Python CE page at
Andrew M. Kuchling662d76e2000-06-25 14:32:48 +0000513\url{http://starship.python.net/crew/mhammond/ce/} for more information.
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000514
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000515An attempt has been made to alleviate one of Python's warts, the
516often-confusing \exception{NameError} exception when code refers to a
517local variable before the variable has been assigned a value. For
518example, the following code raises an exception on the \keyword{print}
519statement in both 1.5.2 and 1.6; in 1.5.2 a \exception{NameError}
Andrew M. Kuchling662d76e2000-06-25 14:32:48 +0000520exception is raised, while 1.6 raises a new
521\exception{UnboundLocalError} exception.
522\exception{UnboundLocalError} is a subclass of \exception{NameError},
523so any existing code that expects \exception{NameError} to be raised
524should still work.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000525
526\begin{verbatim}
527def f():
528 print "i=",i
529 i = i + 1
530f()
531\end{verbatim}
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000532
533A new variable holding more detailed version information has been
534added to the \module{sys} module. \code{sys.version_info} is a tuple
535\code{(\var{major}, \var{minor}, \var{micro}, \var{level},
536\var{serial})} For example, in 1.6a2 \code{sys.version_info} is
537\code{(1, 6, 0, 'alpha', 2)}. \var{level} is a string such as
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000538\code{"alpha"}, \code{"beta"}, or \code{""} for a final release.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000539
540% ======================================================================
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000541\section{Extending/Embedding Changes}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000542
543Some of the changes are under the covers, and will only be apparent to
544people writing C extension modules, or embedding a Python interpreter
545in a larger application. If you aren't dealing with Python's C API,
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000546you can safely skip this section.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000547
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +0000548The version number of the Python C API was incremented, so C
549extensions compiled for 1.5.2 must be recompiled in order to work with
5501.6. On Windows, attempting to import a third party extension built
551for Python 1.5.x usually results in an immediate crash; there's not
552much we can do about this. (XXX can anyone tell me why it crashes?)
553
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000554Users of Jim Fulton's ExtensionClass module will be pleased to find
555out that hooks have been added so that ExtensionClasses are now
556supported by \function{isinstance()} and \function{issubclass()}.
557This means you no longer have to remember to write code such as
558\code{if type(obj) == myExtensionClass}, but can use the more natural
559\code{if isinstance(obj, myExtensionClass)}.
560
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000561The \file{Python/importdl.c} file, which was a mass of \#ifdefs to
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000562support dynamic loading on many different platforms, was cleaned up
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +0000563and reorganised by Greg Stein. \file{importdl.c} is now quite small,
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000564and platform-specific code has been moved into a bunch of
565\file{Python/dynload_*.c} files.
566
567Vladimir Marangozov's long-awaited malloc restructuring was completed,
568to make it easy to have the Python interpreter use a custom allocator
569instead of C's standard \function{malloc()}. For documentation, read
570the comments in \file{Include/mymalloc.h} and
571\file{Include/objimpl.h}. For the lengthy discussions during which
572the interface was hammered out, see the Web archives of the 'patches'
573and 'python-dev' lists at python.org.
574
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000575Recent versions of the GUSI development environment for MacOS support
576POSIX threads. Therefore, Python's POSIX threading support now works
577on the Macintosh. Threading support using the user-space GNU \texttt{pth}
578library was also contributed.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000579
580Threading support on Windows was enhanced, too. Windows supports
581thread locks that use kernel objects only in case of contention; in
582the common case when there's no contention, they use simpler functions
583which are an order of magnitude faster. A threaded version of Python
5841.5.2 on NT is twice as slow as an unthreaded version; with the 1.6
585changes, the difference is only 10\%. These improvements were
586contributed by Yakov Markovitch.
587
588% ======================================================================
589\section{Module changes}
590
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000591Lots of improvements and bugfixes were made to Python's extensive
592standard library; some of the affected modules include
593\module{readline}, \module{ConfigParser}, \module{cgi},
594\module{calendar}, \module{posix}, \module{readline}, \module{xmllib},
595\module{aifc}, \module{chunk, wave}, \module{random}, \module{shelve},
596and \module{nntplib}. Consult the CVS logs for the exact
597patch-by-patch details.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000598
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000599Brian Gallew contributed OpenSSL support for the \module{socket}
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000600module. OpenSSL is an implementation of the Secure Socket Layer,
601which encrypts the data being sent over a socket. When compiling
602Python, you can edit \file{Modules/Setup} to include SSL support,
603which adds an additional function to the \module{socket} module:
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000604\function{socket.ssl(\var{socket}, \var{keyfile}, \var{certfile})},
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000605which takes a socket object and returns an SSL socket. The
606\module{httplib} and \module{urllib} modules were also changed to
607support ``https://'' URLs, though no one has implemented FTP or SMTP
608over SSL.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000609
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +0000610The \module{httplib} module has been rewritten by Greg Stein to
611support HTTP/1.1. Backward compatibility with the 1.5 version of
612\module{httplib} is provided, though using HTTP/1.1 features such as
613pipelining will require rewriting code to use a different set of
614interfaces.
615
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000616The \module{Tkinter} module now supports Tcl/Tk version 8.1, 8.2, or
6178.3, and support for the older 7.x versions has been dropped. The
618Tkinter module also supports displaying Unicode strings in Tk
619widgets.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000620
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000621The \module{curses} module has been greatly extended, starting from
622Oliver Andrich's enhanced version, to provide many additional
623functions from ncurses and SYSV curses, such as colour, alternative
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +0000624character set support, pads, and mouse support. This means the module
625is no longer compatible with operating systems that only have BSD
626curses, but there don't seem to be any currently maintained OSes that
627fall into this category.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000628
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000629As mentioned in the earlier discussion of 1.6's Unicode support, the
630underlying implementation of the regular expressions provided by the
631\module{re} module has been changed. SRE, a new regular expression
632engine written by Fredrik Lundh and partially funded by Hewlett
633Packard, supports matching against both 8-bit strings and Unicode
634strings.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000635
636% ======================================================================
637\section{New modules}
638
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000639A number of new modules were added. We'll simply list them with brief
640descriptions; consult the 1.6 documentation for the details of a
641particular module.
642
643\begin{itemize}
644
645\item{\module{codecs}, \module{encodings}, \module{unicodedata}:} Added as part of the new Unicode support.
646
647\item{\module{filecmp}:} Supersedes the old \module{cmp} and
648\module{dircmp} modules, which have now become deprecated.
Andrew M. Kuchlingc0328f02000-06-10 15:11:20 +0000649(Contributed by Gordon MacMillan and Moshe Zadka.)
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000650
651\item{\module{linuxaudio}:} Support for the \file{/dev/audio} device on Linux,
652a twin to the existing \module{sunaudiodev} module.
653(Contributed by Peter Bosch.)
654
655\item{\module{mmap}:} An interface to memory-mapped files on both
656Windows and Unix. A file's contents can be mapped directly into
657memory, at which point it behaves like a mutable string, so its
658contents can be read and modified. They can even be passed to
659functions that expect ordinary strings, such as the \module{re}
660module. (Contributed by Sam Rushing, with some extensions by
661A.M. Kuchling.)
662
663\item{\module{PyExpat}:} An interface to the Expat XML parser.
664(Contributed by Paul Prescod.)
665
666\item{\module{robotparser}:} Parse a \file{robots.txt} file, which is
667used for writing Web spiders that politely avoid certain areas of a
668Web site. The parser accepts the contents of a \file{robots.txt} file
669builds a set of rules from it, and can then answer questions about
670the fetchability of a given URL. (Contributed by Skip Montanaro.)
671
672\item{\module{tabnanny}:} A module/script to
673checks Python source code for ambiguous indentation.
674(Contributed by Tim Peters.)
675
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +0000676\item{\module{UserString}:} A base class useful for deriving objects that behave like strings.
677
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000678\item{\module{winreg}:} An interface to the Windows registry.
679\module{winreg} has been part of PythonWin since 1995, but now has
680been added to the core distribution, and enhanced to support Unicode.
681(Contributed by Bill Tutt and Mark Hammond.)
682
683\item{\module{zipfile}:} A module for reading and writing ZIP-format
684archives. These are archives produced by \program{PKZIP} on
685DOS/Windows or \program{zip} on Unix, not to be confused with
686\program{gzip}-format files (which are supported by the \module{gzip}
687module)
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000688(Contributed by James C. Ahlstrom.)
689
Andrew M. Kuchling69db0e42000-06-28 02:16:00 +0000690\item{\module{imputil}:} A module that provides a simpler way for
691writing customised import hooks, in comparison to the existing
692\module{ihooks} module. (Implemented by Greg Stein, with much
693discussion on python-dev along the way.)
694
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000695\end{itemize}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000696
697% ======================================================================
698\section{IDLE Improvements}
699
Andrew M. Kuchlingc0328f02000-06-10 15:11:20 +0000700IDLE is the official Python cross-platform IDE, written using Tkinter.
701Python 1.6 includes IDLE 0.6, which adds a number of new features and
702improvements. A partial list:
703
704\begin{itemize}
705\item UI improvements and optimizations,
706especially in the area of syntax highlighting and auto-indentation.
707
708\item The class browser now shows more information, such as the top
709level functions in a module (XXX did I interpret that right?).
710
711\item Tab width is now a user settable option. When opening an existing Python
712file, IDLE automatically detects the indentation conventions, and adapts.
713
714\item There is now support for calling browsers on various platforms,
715used to open the Python documentation in a browser.
716
717\item IDLE now has a command line, which is largely similar to
718the vanilla Python interpreter.
719
720\item Call tips were added in many places.
721
722\item IDLE can now be installed as a package.
723
724\item In the editor window, there is now a line/column bar at the bottom.
725
726\item Three new keystroke commands: Check module (Alt-F5), Import
727module (F5) and Run script (Ctrl-F5)
728
729\end{itemize}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000730
731% ======================================================================
732\section{Deleted and Deprecated Modules}
733
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000734A few modules have been dropped because they're obsolete, or because
735there are now better ways to do the same thing. The \module{stdwin}
736module is gone; it was for a platform-independent windowing toolkit
737that's no longer developed.
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000738
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +0000739A number of modules have been moved to the
740\file{lib-old} subdirectory:
741\module{cmp}, \module{cmpcache}, \module{dircmp}, \module{dump},
742\module{find}, \module{grep}, \module{packmail},
743\module{poly}, \module{util}, \module{whatsound}, \module{zmod}.
744If you have code which relies on a module that's been moved to
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000745\file{lib-old}, you can simply add that directory to \code{sys.path}
Andrew M. Kuchlinga5bbb002000-06-10 02:41:46 +0000746to get them back, but you're encouraged to update any code that uses
747these modules.
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000748
749XXX any others deleted?
750
751XXX Other candidates for deletion in 1.6: sgimodule.c, glmodule.c (and hence
752cgenmodule.c), imgfile.c, svmodule.c, flmodule.c, fmmodule.c, almodule.c, clmodule.c,
753 knee.py.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000754
755\end{document}
756