blob: 2c00566a121919a912ffee5cc4ffaec2298336c2 [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. Kuchling6c3cd8d2000-06-10 02:24:31 +00004\release{0.02}
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
12A new release of Python, version 1.6, will be released some time this
13summer. Alpha versions are already available from
14\url{http://www.python.org/1.6/}. This article talks about the
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000015exciting new features in 1.6, highlights some other useful changes,
16and points out a few incompatible changes that may require rewriting
17code.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000018
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000019Python's development never completely stops between releases, and a
20steady flow of bug fixes and improvements are always being submitted.
21A host of minor fixes, a few optimizations, additional docstrings, and
22better error messages went into 1.6; to list them all would be
23impossible, but they're certainly significant. Consult the
24publicly-available CVS logs if you want to see the full list.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000025
26% ======================================================================
27\section{Unicode}
28
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000029The largest new feature in Python 1.6 is a new fundamental data type:
30Unicode strings. Unicode uses 16-bit numbers to represent characters
31instead of the 8-bit number used by ASCII, meaning that 65,536
32distinct characters can be supported.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000033
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000034The final interface for Unicode support was arrived at through
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000035countless often-stormy discussions on the python-dev mailing list, and
36mostly implemented by Marc-Andr\'e Lemburg. A detailed explanation of
37the interface is in the file
38\file{Misc/unicode.txt} in the Python source distribution; it's also
39available on the Web at
40\url{http://starship.python.net/crew/lemburg/unicode-proposal.txt}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000041This article will simply cover the most significant points from the
42full interface.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000043
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000044In Python source code, Unicode strings are written as
45\code{u"string"}. Arbitrary Unicode characters can be written using a
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000046new escape sequence, \code{\e u\var{HHHH}}, where \var{HHHH} is a
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000474-digit hexadecimal number from 0000 to FFFF. The existing
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000048\code{\e x\var{HHHH}} escape sequence can also be used, and octal
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000049escapes can be used for characters up to U+01FF, which is represented
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000050by \code{\e 777}.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +000051
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000052Unicode strings, just like regular strings, are an immutable sequence
53type, so they can be indexed and sliced. They also have an
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000054\method{encode( \optional{\var{encoding}} )} method that returns an 8-bit
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000055string in the desired encoding. Encodings are named by strings, such
56as \code{'ascii'}, \code{'utf-8'}, \code{'iso-8859-1'}, or whatever.
57A codec API is defined for implementing and registering new encodings
58that are then available throughout a Python program. If an encoding
59isn't specified, the default encoding is always 7-bit ASCII. (XXX is
60that the current default encoding?)
61
62Combining 8-bit and Unicode strings always coerces to Unicode, using
63the default ASCII encoding; the result of \code{'a' + u'bc'} is
Andrew M. Kuchling7f6270d2000-06-09 02:48:18 +000064\code{u'abc'}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000065
66New built-in functions have been added, and existing built-ins
67modified to support Unicode:
68
69\begin{itemize}
70\item \code{unichr(\var{ch})} returns a Unicode string 1 character
71long, containing the character \var{ch}.
72
73\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.
74
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000075\item \code{unicode(\var{string}, \optional{\var{encoding},}
76\optional{\var{errors}} ) } creates a Unicode string from an 8-bit
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000077string. \code{encoding} is a string naming the encoding to use.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000078The \code{errors} parameter specifies the treatment of characters that
79are invalid for the current encoding; passing \code{'strict'} as the
80value causes an exception to be raised on any encoding error, while
81\code{'ignore'} causes errors to be silently ignored and
82\code{'replace'} uses U+FFFD, the official replacement character, in
83case of any problems.
84
85\end{itemize}
86
87A new module, \module{unicodedata}, provides an interface to Unicode
88character properties. For example, \code{unicodedata.category(u'A')}
89returns the 2-character string 'Lu', the 'L' denoting it's a letter,
90and 'u' meaning that it's uppercase.
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000091\code{u.bidirectional(u'\e x0660')} returns 'AN', meaning that U+0660 is
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +000092an Arabic number.
93
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +000094The \module{codecs} module contains functions to look up existing encodings
95and register new ones. Unless you want to implement a
96new encoding, you'll most often use the
97\function{codecs.lookup(\var{encoding})} function, which returns a
984-element tuple: \code{(\var{encode_func},
99\var{decode_func}, \var{stream_reader}, \var{stream_writer})}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000100
101\begin{itemize}
102\item \var{encode_func} is a function that takes a Unicode string, and
103returns a 2-tuple \code{(\var{string}, \var{length})}. \var{string}
104is an 8-bit string containing a portion (perhaps all) of the Unicode
105string converted into the given encoding, and \var{length} tells you how much of the Unicode string was converted.
106
107\item \var{decode_func} is the mirror of \var{encode_func},
108taking a Unicode string and
109returns a 2-tuple \code{(\var{ustring}, \var{length})} containing a Unicode string
110and \var{length} telling you how much of the string was consumed.
111
112\item \var{stream_reader} is a class that supports decoding input from
113a stream. \var{stream_reader(\var{file_obj})} returns an object that
114supports the \method{read()}, \method{readline()}, and
115\method{readlines()} methods. These methods will all translate from
116the given encoding and return Unicode strings.
117
118\item \var{stream_writer}, similarly, is a class that supports
119encoding output to a stream. \var{stream_writer(\var{file_obj})}
120returns an object that supports the \method{write()} and
121\method{writelines()} methods. These methods expect Unicode strings, translating them to the given encoding on output.
122\end{itemize}
123
124For example, the following code writes a Unicode string into a file,
125encoding it as UTF-8:
126
127\begin{verbatim}
128import codecs
129
130unistr = u'\u0660\u2000ab ...'
131
132(UTF8_encode, UTF8_decode,
133 UTF8_streamreader, UTF8_streamwriter) = codecs.lookup('UTF-8')
134
135output = UTF8_streamwriter( open( '/tmp/output', 'wb') )
136output.write( unistr )
137output.close()
138\end{verbatim}
139
140The following code would then read UTF-8 input from the file:
141
142\begin{verbatim}
143input = UTF8_streamread( open( '/tmp/output', 'rb') )
144print repr(input.read())
145input.close()
146\end{verbatim}
147
148Unicode-aware regular expressions are available through the
149\module{re} module, which has a new underlying implementation called
150SRE written by Fredrik Lundh of Secret Labs AB.
151
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000152(XXX M.A. Lemburg added a -U command line option, which causes the
153Python compiler to interpret all "..." strings as u"..." (same with
154r"..." and ur"..."). Is this just for experimenting/testing, or is it
155actually a new feature?)
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000156
157% ======================================================================
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000158\section{Distutils: Making Modules Easy to Install}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000159
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000160Before Python 1.6, installing modules was a tedious affair -- there
161was no way to figure out automatically where Python is installed, or
162what compiler options to use for extension modules. Software authors
163had to go through an ardous ritual of editing Makefiles and
164configuration files, which only really work on Unix and leave Windows
165and MacOS unsupported. Software users faced wildly differing
166installation instructions
167
168The SIG for distribution utilities, shepherded by Greg Ward, has
169created the Distutils, a system to make package installation much
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000170easier. They form the \module{distutils} package, a new part of
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000171Python's standard library. In the best case, installing a Python
172module from source will require the same steps: first you simply mean
173unpack the tarball or zip archive, and the run ``\code{python setup.py
174install}''. The platform will be automatically detected, the compiler
175will be recognized, C extension modules will be compiled, and the
176distribution installed into the proper directory. Optional
177command-line arguments provide more control over the installation
178process, the distutils package offers many places to override defaults
179-- separating the build from the install, building or installing in
180non-default directories, and more.
181
182In order to use the Distutils, you need to write a \file{setup.py}
183script. For the simple case, when the software contains only .py
184files, a minimal \file{setup.py} can be just a few lines long:
185
186\begin{verbatim}
187from distutils.core import setup
188setup (name = "foo", version = "1.0",
189 py_modules = ["module1", "module2"])
190\end{verbatim}
191
192The \file{setup.py} file isn't much more complicated if the software
193consists of a few packages:
194
195\begin{verbatim}
196from distutils.core import setup
197setup (name = "foo", version = "1.0",
198 packages = ["package", "package.subpackage"])
199\end{verbatim}
200
201A C extension can be the most complicated case; here's an example taken from
202the PyXML package:
203
204
205\begin{verbatim}
206from distutils.core import setup, Extension
207
208expat_extension = Extension('xml.parsers.pyexpat',
209 define_macros = [('XML_NS', None)],
210 include_dirs = [ 'extensions/expat/xmltok',
211 'extensions/expat/xmlparse' ],
212 sources = [ 'extensions/pyexpat.c',
213 'extensions/expat/xmltok/xmltok.c',
214 'extensions/expat/xmltok/xmlrole.c',
215 ]
216 )
217setup (name = "PyXML", version = "0.5.4",
218 ext_modules =[ expat_extension ] )
219
220\end{verbatim}
221
222The Distutils can also take care of creating source and binary
223distributions. The ``sdist'' command, run by ``\code{python setup.py
224sdist}', builds a source distribution such as \file{foo-1.0.tar.gz}.
225Adding new commands isn't difficult, and a ``bdist_rpm'' command has
226already been contributed to create an RPM distribution for the
227software. Commands to create Windows installer programs, Debian
228packages, and Solaris .pkg files have been discussed and are in
229various stages of development.
230
231All this is documented in a new manual, \textit{Distributing Python
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000232Modules}, that will be added to the basic set of Python documentation.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000233
234% ======================================================================
235\section{String Methods}
236
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000237Until now string-manipulation functionality was in the \module{string}
238Python module, which was usually a front-end for the \module{strop}
239module written in C. The addition of Unicode posed a difficulty for
240the \module{strop} module, because the functions would all need to be
241rewritten in order to accept either 8-bit or Unicode strings. For
242functions such as \function{string.replace()}, which takes 3 string
243arguments, that means eight possible permutations, and correspondingly
244complicated code.
245
246Instead, Python 1.6 pushes the problem onto the string type, making
247string manipulation functionality available through methods on both
2488-bit strings and Unicode strings.
249
250\begin{verbatim}
251>>> 'andrew'.capitalize()
252'Andrew'
253>>> 'hostname'.replace('os', 'linux')
254'hlinuxtname'
255>>> 'moshe'.find('sh')
2562
257\end{verbatim}
258
259One thing that hasn't changed, April Fools' jokes notwithstanding, is
260that Python strings are immutable. Thus, the string methods return new
261strings, and do not modify the string on which they operate.
262
263The old \module{string} module is still around for backwards
264compatibility, but it mostly acts as a front-end to the new string
265methods.
266
267Two methods which have no parallel in pre-1.6 versions, although they
268did exist in JPython for quite some time, are \method{startswith()}
269and \method{endswith}. \code{s.startswith(t)} is equivalent to \code{s[:len(t)]
270== t}, while \code{s.endswith(t)} is equivalent to \code{s[-len(t):] == t}.
271
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000272(XXX what'll happen to join? is this even worth mentioning?) One
273other method which deserves special mention is \method{join}. The
274\method{join} method of a string receives one parameter, a sequence of
275strings, and is equivalent to the \function{string.join} function from
276the old \module{string} module, with the arguments reversed. In other
277words, \code{s.join(seq)} is equivalent to the old
278\code{string.join(seq, s)}.
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000279
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000280% ======================================================================
281\section{Porting to 1.6}
282
283New Python releases try hard to be compatible with previous releases,
284and the record has been pretty good. However, some changes are
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000285considered useful enough, often fixing initial design decisions that
286turned to be actively mistaken, that breaking backward compatibility
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000287can't always be avoided. This section lists the changes in Python 1.6
288that may cause old Python code to break.
289
290The change which will probably break the most code is tightening up
291the arguments accepted by some methods. Some methods would take
292multiple arguments and treat them as a tuple, particularly various
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000293list methods such as \method{.append()} and \method{.insert()}.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000294In earlier versions of Python, if \code{L} is a list, \code{L.append(
2951,2 )} appends the tuple \code{(1,2)} to the list. In Python 1.6 this
296causes a \exception{TypeError} exception to be raised, with the
297message: 'append requires exactly 1 argument; 2 given'. The fix is to
298simply add an extra set of parentheses to pass both values as a tuple:
299\code{L.append( (1,2) )}.
300
301The earlier versions of these methods were more forgiving because they
302used an old function in Python's C interface to parse their arguments;
3031.6 modernizes them to use \function{PyArg_ParseTuple}, the current
304argument parsing function, which provides more helpful error messages
305and treats multi-argument calls as errors. If you absolutely must use
3061.6 but can't fix your code, you can edit \file{Objects/listobject.c}
307and define the preprocessor symbol \code{NO_STRICT_LIST_APPEND} to
308preserve the old behaviour; this isn't recommended.
309
310Some of the functions in the \module{socket} module are still
311forgiving in this way. For example, \function{socket.connect(
312('hostname', 25) )} is the correct form, passing a tuple representing
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000313an IP address, but \function{socket.connect( 'hostname', 25 )} also
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000314works. \function{socket.connect_ex()} and \function{socket.bind()} are
315similarly easy-going. 1.6alpha1 tightened these functions up, but
316because the documentation actually used the erroneous multiple
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000317argument form, many people wrote code which would break with the
318stricter checking. GvR backed out the changes in the face of public
319reaction, so for the\module{socket} module, the documentation was
320fixed and the multiple argument form is simply marked as deprecated;
321it \emph{will} be tightened up again in a future Python version.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000322
323Some work has been done to make integers and long integers a bit more
324interchangeable. In 1.5.2, large-file support was added for Solaris,
325to allow reading files larger than 2Gb; this made the \method{tell()}
326method of file objects return a long integer instead of a regular
327integer. Some code would subtract two file offsets and attempt to use
328the result to multiply a sequence or slice a string, but this raised a
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000329\exception{TypeError}. In 1.6, long integers can be used to multiply
330or slice a sequence, and it'll behave as you'd intuitively expect it
331to; \code{3L * 'abc'} produces 'abcabcabc', and \code{
332(0,1,2,3)[2L:4L]} produces (2,3). Long integers can also be used in
333various new places where previously only integers were accepted, such
334as in the \method{seek()} method of file objects.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000335
336The subtlest long integer change of all is that the \function{str()}
337of a long integer no longer has a trailing 'L' character, though
338\function{repr()} still includes it. The 'L' annoyed many people who
339wanted to print long integers that looked just like regular integers,
340since they had to go out of their way to chop off the character. This
341is no longer a problem in 1.6, but code which assumes the 'L' is
342there, and does \code{str(longval)[:-1]} will now lose the final
343digit.
344
345Taking the \function{repr()} of a float now uses a different
346formatting precision than \function{str()}. \function{repr()} uses
347``%.17g'' format string for C's \function{sprintf()}, while
348\function{str()} uses ``%.12g'' as before. The effect is that
349\function{repr()} may occasionally show more decimal places than
350\function{str()}, for numbers
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000351XXX need example value here to demonstrate problem.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000352
353
354% ======================================================================
355\section{Core Changes}
356
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000357Various minor changes have been made to Python's syntax and built-in
358functions. None of the changes are very far-reaching, but they're
359handy conveniences.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000360
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000361A change to syntax makes it more convenient to call a given function
362with a tuple of arguments and/or a dictionary of keyword arguments.
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000363In Python 1.5 and earlier, you do this with the \function{apply()}
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000364built-in function: \code{apply(f, \var{args}, \var{kw})} calls the
365function \function{f()} with the argument tuple \var{args} and the
366keyword arguments in the dictionary \var{kw}. Thanks to a patch from
367Greg Ewing, 1.6 adds \code{f(*\var{args}, **\var{kw})} as a shorter
368and clearer way to achieve the same effect. This syntax is
369symmetrical with the syntax for defining functions:
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000370
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000371\begin{verbatim}
372def f(*args, **kw):
373 # args is a tuple of positional args,
374 # kw is a dictionary of keyword args
375 ...
376\end{verbatim}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000377
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000378A new format style is available when using the \code{\%} operator.
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000379'\%r' will insert the \function{repr()} of its argument. This was
380also added from symmetry considerations, this time for symmetry with
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000381the existing '\%s' format style, which inserts the \function{str()} of
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000382its argument. For example, \code{'\%r \%s' \% ('abc', 'abc')} returns a
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000383string containing \verb|'abc' abc|.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000384
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000385The \function{int()} and \function{long()} functions now accept an
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000386optional ``base'' parameter when the first argument is a string.
387\code{int('123', 10)} returns 123, while \code{int('123', 16)} returns
388291. \code{int(123, 16)} raises a \exception{TypeError} exception
389with the message ``can't convert non-string with explicit base''.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000390
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000391Previously there was no way to implement a class that overrode
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000392Python's built-in \keyword{in} operator and implemented a custom
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000393version. \code{\var{obj} in \var{seq}} returns true if \var{obj} is
394present in the sequence \var{seq}; Python computes this by simply
395trying every index of the sequence until either \var{obj} is found or
396an \exception{IndexError} is encountered. Moshe Zadka contributed a
397patch which adds a \method{__contains__} magic method for providing a
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000398custom implementation for \keyword{in}. Additionally, new built-in
399objects written in C can define what \keyword{in} means for them via a
400new slot in the sequence protocol.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000401
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000402Earlier versions of Python used a recursive algorithm for deleting
403objects. Deeply nested data structures could cause the interpreter to
404fill up the C stack and crash; Christian Tismer rewrote the deletion
405logic to fix this problem. On a related note, comparing recursive
406objects recursed infinitely and crashed; Jeremy Hylton rewrote the
407code to no longer crash, producing a useful result instead. For
408example, after this code:
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000409
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000410\begin{verbatim}
411a = []
412b = []
413a.append(a)
414b.append(b)
415\end{verbatim}
416
417The comparison \code{a==b} returns true, because the two recursive
418data structures are isomorphic.
419\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.
420%http://www.python.org/pipermail/python-dev/2000-April/004834.html
421}
422
423Work has been done on porting Python to 64-bit Windows on the Itanium
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000424processor, mostly by Trent Mick of ActiveState. (Confusingly, \code{sys.platform} is still \code{'win32'} on
425Win64 because it seems that for ease of porting, MS Visual C++ treats code
426as 32 bit.
427) PythonWin also supports Windows CE; see the Python CE page at
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000428\url{http://www.python.net/crew/mhammond/ce/} for more information.
429
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000430An attempt has been made to alleviate one of Python's warts, the
431often-confusing \exception{NameError} exception when code refers to a
432local variable before the variable has been assigned a value. For
433example, the following code raises an exception on the \keyword{print}
434statement in both 1.5.2 and 1.6; in 1.5.2 a \exception{NameError}
435exception is raised, while 1.6 raises \exception{UnboundLocalError}.
436
437\begin{verbatim}
438def f():
439 print "i=",i
440 i = i + 1
441f()
442\end{verbatim}
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000443
444A new variable holding more detailed version information has been
445added to the \module{sys} module. \code{sys.version_info} is a tuple
446\code{(\var{major}, \var{minor}, \var{micro}, \var{level},
447\var{serial})} For example, in 1.6a2 \code{sys.version_info} is
448\code{(1, 6, 0, 'alpha', 2)}. \var{level} is a string such as
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000449\code{"alpha"}, \code{"beta"}, or \code{""} for a final release.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000450
451% ======================================================================
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000452\section{Extending/Embedding Changes}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000453
454Some of the changes are under the covers, and will only be apparent to
455people writing C extension modules, or embedding a Python interpreter
456in a larger application. If you aren't dealing with Python's C API,
Andrew M. Kuchling5b8311e2000-05-31 03:28:42 +0000457you can safely skip this section.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000458
459Users of Jim Fulton's ExtensionClass module will be pleased to find
460out that hooks have been added so that ExtensionClasses are now
461supported by \function{isinstance()} and \function{issubclass()}.
462This means you no longer have to remember to write code such as
463\code{if type(obj) == myExtensionClass}, but can use the more natural
464\code{if isinstance(obj, myExtensionClass)}.
465
Andrew M. Kuchlingb853ea02000-06-03 03:06:58 +0000466The \file{Python/importdl.c} file, which was a mass of \#ifdefs to
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000467support dynamic loading on many different platforms, was cleaned up
468are reorganized by Greg Stein. \file{importdl.c} is now quite small,
469and platform-specific code has been moved into a bunch of
470\file{Python/dynload_*.c} files.
471
472Vladimir Marangozov's long-awaited malloc restructuring was completed,
473to make it easy to have the Python interpreter use a custom allocator
474instead of C's standard \function{malloc()}. For documentation, read
475the comments in \file{Include/mymalloc.h} and
476\file{Include/objimpl.h}. For the lengthy discussions during which
477the interface was hammered out, see the Web archives of the 'patches'
478and 'python-dev' lists at python.org.
479
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000480Recent versions of the GUSI development environment for MacOS support
481POSIX threads. Therefore, Python's POSIX threading support now works
482on the Macintosh. Threading support using the user-space GNU \texttt{pth}
483library was also contributed.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000484
485Threading support on Windows was enhanced, too. Windows supports
486thread locks that use kernel objects only in case of contention; in
487the common case when there's no contention, they use simpler functions
488which are an order of magnitude faster. A threaded version of Python
4891.5.2 on NT is twice as slow as an unthreaded version; with the 1.6
490changes, the difference is only 10\%. These improvements were
491contributed by Yakov Markovitch.
492
493% ======================================================================
494\section{Module changes}
495
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000496Lots of improvements and bugfixes were made to Python's extensive
497standard library; some of the affected modules include
498\module{readline}, \module{ConfigParser}, \module{cgi},
499\module{calendar}, \module{posix}, \module{readline}, \module{xmllib},
500\module{aifc}, \module{chunk, wave}, \module{random}, \module{shelve},
501and \module{nntplib}. Consult the CVS logs for the exact
502patch-by-patch details.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000503
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000504Brian Gallew contributed OpenSSL support for the \module{socket}
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000505module. OpenSSL is an implementation of the Secure Socket Layer,
506which encrypts the data being sent over a socket. When compiling
507Python, you can edit \file{Modules/Setup} to include SSL support,
508which adds an additional function to the \module{socket} module:
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000509\function{socket.ssl(\var{socket}, \var{keyfile}, \var{certfile})},
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000510which takes a socket object and returns an SSL socket. The
511\module{httplib} and \module{urllib} modules were also changed to
512support ``https://'' URLs, though no one has implemented FTP or SMTP
513over SSL.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000514
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000515The \module{Tkinter} module now supports Tcl/Tk version 8.1, 8.2, or
5168.3, and support for the older 7.x versions has been dropped. The
517Tkinter module also supports displaying Unicode strings in Tk
518widgets.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000519
Andrew M. Kuchlingfa33a4e2000-06-03 02:52:40 +0000520The \module{curses} module has been greatly extended, starting from
521Oliver Andrich's enhanced version, to provide many additional
522functions from ncurses and SYSV curses, such as colour, alternative
523character set support, pads, and other new features. This means the
524module is no longer compatible with operating systems that only have
525BSD curses, but there don't seem to be any currently maintained OSes
526that fall into this category.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000527
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000528As mentioned in the earlier discussion of 1.6's Unicode support, the
529underlying implementation of the regular expressions provided by the
530\module{re} module has been changed. SRE, a new regular expression
531engine written by Fredrik Lundh and partially funded by Hewlett
532Packard, supports matching against both 8-bit strings and Unicode
533strings.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000534
535% ======================================================================
536\section{New modules}
537
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000538A number of new modules were added. We'll simply list them with brief
539descriptions; consult the 1.6 documentation for the details of a
540particular module.
541
542\begin{itemize}
543
544\item{\module{codecs}, \module{encodings}, \module{unicodedata}:} Added as part of the new Unicode support.
545
546\item{\module{filecmp}:} Supersedes the old \module{cmp} and
547\module{dircmp} modules, which have now become deprecated.
548(Contributed by Moshe Zadka.)
549
550\item{\module{linuxaudio}:} Support for the \file{/dev/audio} device on Linux,
551a twin to the existing \module{sunaudiodev} module.
552(Contributed by Peter Bosch.)
553
554\item{\module{mmap}:} An interface to memory-mapped files on both
555Windows and Unix. A file's contents can be mapped directly into
556memory, at which point it behaves like a mutable string, so its
557contents can be read and modified. They can even be passed to
558functions that expect ordinary strings, such as the \module{re}
559module. (Contributed by Sam Rushing, with some extensions by
560A.M. Kuchling.)
561
562\item{\module{PyExpat}:} An interface to the Expat XML parser.
563(Contributed by Paul Prescod.)
564
565\item{\module{robotparser}:} Parse a \file{robots.txt} file, which is
566used for writing Web spiders that politely avoid certain areas of a
567Web site. The parser accepts the contents of a \file{robots.txt} file
568builds a set of rules from it, and can then answer questions about
569the fetchability of a given URL. (Contributed by Skip Montanaro.)
570
571\item{\module{tabnanny}:} A module/script to
572checks Python source code for ambiguous indentation.
573(Contributed by Tim Peters.)
574
575\item{\module{winreg}:} An interface to the Windows registry.
576\module{winreg} has been part of PythonWin since 1995, but now has
577been added to the core distribution, and enhanced to support Unicode.
578(Contributed by Bill Tutt and Mark Hammond.)
579
580\item{\module{zipfile}:} A module for reading and writing ZIP-format
581archives. These are archives produced by \program{PKZIP} on
582DOS/Windows or \program{zip} on Unix, not to be confused with
583\program{gzip}-format files (which are supported by the \module{gzip}
584module)
585
586(Contributed by James C. Ahlstrom.)
587
588\end{itemize}
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000589
590% ======================================================================
591\section{IDLE Improvements}
592
593XXX IDLE -- complete overhaul; what are the changes?
594
595% ======================================================================
596\section{Deleted and Deprecated Modules}
597
Andrew M. Kuchling6c3cd8d2000-06-10 02:24:31 +0000598A few modules have been dropped because they're obsolete, or because
599there are now better ways to do the same thing. The \module{stdwin}
600module is gone; it was for a platform-independent windowing toolkit
601that's no longer developed.
602The \module{cmp} and \module{dircmp} modules have been moved to the
603\file{lib-old} subdirectory;
604
605If you have code which relies on modules that have been moved to
606\file{lib-old}, you can simply add that directory to \code{sys.path}
607to get them back.
608
609XXX any others deleted?
610
611XXX Other candidates for deletion in 1.6: sgimodule.c, glmodule.c (and hence
612cgenmodule.c), imgfile.c, svmodule.c, flmodule.c, fmmodule.c, almodule.c, clmodule.c,
613 knee.py.
Andrew M. Kuchling25bfd0e2000-05-27 11:28:26 +0000614
615\end{document}
616