blob: e87077ed13bb270f98b3db8fff93a09a84380697 [file] [log] [blame]
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001\documentclass{howto}
2
3% $Id$
4
5\title{What's New in Python 2.2}
Andrew M. Kuchlingd4707e32001-09-28 20:46:46 +00006\release{0.06}
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00007\author{A.M. Kuchling}
Andrew M. Kuchling7bf82772001-07-11 18:54:26 +00008\authoraddress{\email{akuchlin@mems-exchange.org}}
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00009\begin{document}
10\maketitle\tableofcontents
11
12\section{Introduction}
13
14{\large This document is a draft, and is subject to change until the
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +000015final version of Python 2.2 is released. Currently it's up to date
Andrew M. Kuchlingd4707e32001-09-28 20:46:46 +000016for Python 2.2 alpha 4. Please send any comments, bug reports, or
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +000017questions, no matter how minor, to \email{akuchlin@mems-exchange.org}.
18}
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000019
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +000020This article explains the new features in Python 2.2.
21
22Python 2.2 can be thought of as the "cleanup release". There are some
23features such as generators and iterators that are completely new, but
24most of the changes, significant and far-reaching though they may be,
25are aimed at cleaning up irregularities and dark corners of the
26language design.
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000027
Andrew M. Kuchling1497b622001-09-24 14:51:16 +000028This article doesn't attempt to provide a complete specification of
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +000029the new features, but instead provides a convenient overview. For
30full details, you should refer to the documentation for Python 2.2,
Fred Drake0d002542001-07-17 13:55:33 +000031such as the
32\citetitle[http://python.sourceforge.net/devel-docs/lib/lib.html]{Python
33Library Reference} and the
34\citetitle[http://python.sourceforge.net/devel-docs/ref/ref.html]{Python
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +000035Reference Manual}.
36% XXX These \citetitle marks should get the python.org URLs for the final
Fred Drake0d002542001-07-17 13:55:33 +000037% release, just as soon as the docs are published there.
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +000038If you want to understand the complete implementation and design
39rationale for a change, refer to the PEP for a particular new feature.
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000040
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +000041The final release of Python 2.2 is planned for December 2001.
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000042
Andrew M. Kuchling1497b622001-09-24 14:51:16 +000043\begin{seealso}
44
45\url{http://www.unixreview.com/documents/s=1356/urm0109h/0109h.htm}
46{``What's So Special About Python 2.2?'' is also about the new 2.2
47features, and was written by Cameron Laird and Kathryn Soraiz.}
48
49\end{seealso}
50
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +000051
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000052%======================================================================
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +000053\section{PEP 252: Type and Class Changes}
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000054
Andrew M. Kuchling279e7442001-10-22 02:03:40 +000055The largest and most far-reaching changes in Python 2.2 are to
56Python's model of objects and classes. The changes should be backward
57compatible, so it's likely that your code will continue to run
58unchanged, but the changes provide some amazing new capabilities.
59Before beginning this, the longest and most complicated section of
60this article, I'll provide an overview of the changes and offer some
61comments.
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000062
Andrew M. Kuchling279e7442001-10-22 02:03:40 +000063A long time ago I wrote a Web page
64(\url{http://www.amk.ca/python/writing/warts.html}) listing flaws in
65Python's design. One of the most significant flaws was that it's
66impossible to subclass Python types implemented in C. In particular,
67it's not possible to subclass built-in types, so you can't just
68subclass, say, lists in order to add a single useful method to them.
69The \module{UserList} module provides a class that supports all of the
70methods of lists and that can be subclassed further, but there's lots
71of C code that expects a regular Python list and won't accept a
72\class{UserList} instance.
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +000073
Andrew M. Kuchling279e7442001-10-22 02:03:40 +000074Python 2.2 fixes this, and in the process adds some exciting new
75capabilities. A brief summary:
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +000076
Andrew M. Kuchling279e7442001-10-22 02:03:40 +000077\begin{itemize}
Andrew M. Kuchlingd6e40e22001-09-10 16:18:50 +000078
Andrew M. Kuchling279e7442001-10-22 02:03:40 +000079\item You can subclass built-in types such as lists and even integers,
80and your subclasses should work in every place that requires the
81original type.
82
83\item It's now possible to define static and class methods, in addition
84to the instance methods available in previous versions of Python.
85
86\item It's also possible to automatically call methods on accessing or
87setting an instance attribute by using a new mechanism called
88\dfn{properties}. Many uses of \method{__getattr__} can be rewritten
89to use properties instead, making the resulting code simpler and
90faster. As a small side benefit, attributes can now have docstrings,
91too.
92
93\item The list of legal attributes for an instance can be limited to a
94particular set using \dfn{slots}, making it possible to safeguard
95against typos and perhaps make more optimizations possible in future
96versions of Python.
97
98\end{itemize}
99
100Some users have voiced concern about all these changes. Sure, they
101say, the new features are neat and lend themselves to all sorts of
102tricks that weren't possible in previous versions of Python, but
103they also make the language more complicated. Some people have said
104that they've always recommended Python for its simplicity, and feel
105that its simplicity is being lost.
106
107Personally, I think there's no need to worry. Many of the new
108features are quite esoteric, and you can write a lot of Python code
109without ever needed to be aware of them. Writing a simple class is no
110more difficult than it ever was, so you don't need to bother learning
111or teaching them unless they're actually needed. Some very
112complicated tasks that were previously only possible from C will now
113be possible in pure Python, and to my mind that's all for the better.
114
115I'm not going to attempt to cover every single corner case and small
116change that were required to make the new features work. Instead this
117section will paint only the broad strokes. See section~\cite{sect-rellinks},
118``Related Links'', for further sources of information about Python 2.2's new
119object model.
120
121
122\subsection{Old and New Classes}
123
124First, you should know that Python 2.2 really has two kinds of
125classes: classic or old-style classes, and new-style classes. The
126old-style class model is exactly the same as the class model in
127earlier versions of Python. All the new features described in this
128section apply only to new-style classes. This divergence isn't
129intended to last forever; eventually old-style classes will be
130dropped, possibly in Python 3.0.
131
132So how do you define a new-style class? XXX
133Subclass object -- subclass a built-in type.
134
135
136\subsection{Descriptors}
137
138In previous versions of Python, there was no consistent way to
139discover what attributes and methods were supported by an object.
140There were some informal conventions, such as defining
141\member{__members__} and \member{__methods__} attributes that were
142lists of names, but often the author of an extension type or a class
143wouldn't bother to define them. You could fall back on inspecting the
144\member{__dict__} of an object, but when class inheritance or an
145arbitrary \method{__getattr__} hook were in use this could still be
146inaccurate.
147
148The one big idea underlying the new class model is that an API for
149describing the attributes of an object using \dfn{descriptors} has
150been formalized. Descriptors specify the value of an attribute,
151stating whether it's a method or a field. With the descriptor API,
152static methods and class methods become possible, as well as more
153exotic constructs.
154
155Attribute descriptors are objects that live inside class objects, and
156have a few attributes of their own:
157
158\begin{itemize}
159
160\item \member{__name__} is the attribute's name.
161
162\item \member{__doc__} is the attribute's docstring.
163
164\item \method{__get__(\var{object})} is a method that retrieves the attribute value from \var{object}.
165
166\item \method{__get__(\var{object}, \var{value})} sets the attribute
167on \var{object} to \var{value}.
168
169\end{itemize}
170
171For example, when you write \code{obj.x}, the steps that Python
172actually performs are:
173
174\begin{verbatim}
175descriptor = obj.__class__.x
176descriptor.get(obj)
177\end{verbatim}
178
179For methods, \method{descriptor.get} returns a temporary object that's
180callable, and wraps up the instance and the method to be called on it.
181This is also why static methods and class methods are now possible;
182they have descriptors that wrap up just the method, or the method and
183the class. As a brief explanation of these new kinds of methods,
184static methods aren't passed the instance, and therefore resemble
185regular functions. Class methods are passed the class of the object,
186but not the object itself. Static and class methods is defined like
187this:
188
189\begin{verbatim}
190class C:
191 def f(arg1, arg2):
192 ...
193 f = staticmethod(f)
194
195 def g(cls, arg1, arg2):
196 ...
197 g = classmethod(g)
198\end{verbatim}
199
200The \function{staticmethod()} function takes the function
201\function{f}, and returns it wrapped up in a descriptor so it can be
202stored in the class object. You might expect there to be special
203syntax for creating such methods (\code{def static f()},
204\code{defstatic f()}, or something like that) but no such syntax has
205been defined yet; that's been left for future versions.
206
207More new features, such as slots and properties, are also implemented
208as new kinds of descriptors, and it's not difficult to write a
209descriptor class that does something novel. For example, it would be
210possible to write a descriptor class that made it possible to write
211Eiffel-style preconditions and postconditions for a method. A class
212that used this feature might be defined like this:
213
214\begin{verbatim}
215from eiffel import eiffelmethod
216
217class C:
218 def f(self, arg1, arg2):
219 # The actual function
220 def pre_f(self):
221 # Check preconditions
222 def post_f(self):
223 # Check postconditions
224
225 f = eiffelmethod(f, pre_f, post_f)
226\end{verbatim}
227
228Note that a person using the new \function{eiffelmethod()} doesn't
229have to understand anything about descriptors. This is why I think
230the new features don't increase the basic complexity of the language.
231There will be a few wizards who need to know about it in order to
232write \function{eiffelmethod()} or the ZODB or whatever, but most
233users will just write code on top of the resulting libraries and
234ignore the implementation details.
235
236\subsection{Inheritance Lookup: The Diamond Rule}
237
238XXX
239
240\subsection{Attribute Access}
241
242XXX __getattribute__, __getattr__
243
244\subsection{Related Links}
245\ref{sect-rellinks}
246
247This section has just been a quick overview of the new features,
248giving enough of an explanation to start you programming, but many
249details have been simplified or ignored. Where should you go to get a
250more complete picture?
251
252\url{http://www.python.org/2.2/descrintro.html} is a tutorial
253introduction to the descriptor features, written by Guido van Rossum.
254% XXX read it and comment on it
255
256Next, there are two relevant PEPs, \pep{252} and \pep{253}. \pep{252}
257is titled "Making Types Look More Like Classes", and covers the
258descriptor API. \pep{253} is titled "Subtyping Built-in Types", and
259describes the changes to type objects that make it possible to subtype
260built-in objects. This is the more complicated PEP of the two, and at
261a few points the necessary explanations of types and meta-types may
262cause your head to explode. Both PEPs were written and implemented by
263Guido van Rossum, with substantial assistance from the rest of the
264Zope Corp. team.
265
266Finally, there's the ultimate authority: the source code.
267% XXX point people at the right files
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +0000268
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +0000269
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +0000270%======================================================================
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000271\section{PEP 234: Iterators}
272
273A significant addition to 2.2 is an iteration interface at both the C
274and Python levels. Objects can define how they can be looped over by
275callers.
276
277In Python versions up to 2.1, the usual way to make \code{for item in
278obj} work is to define a \method{__getitem__()} method that looks
279something like this:
280
281\begin{verbatim}
282 def __getitem__(self, index):
283 return <next item>
284\end{verbatim}
285
286\method{__getitem__()} is more properly used to define an indexing
287operation on an object so that you can write \code{obj[5]} to retrieve
Andrew M. Kuchling8c69c912001-08-07 14:28:58 +0000288the sixth element. It's a bit misleading when you're using this only
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000289to support \keyword{for} loops. Consider some file-like object that
290wants to be looped over; the \var{index} parameter is essentially
291meaningless, as the class probably assumes that a series of
292\method{__getitem__()} calls will be made, with \var{index}
293incrementing by one each time. In other words, the presence of the
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000294\method{__getitem__()} method doesn't mean that using \code{file[5]}
295to randomly access the sixth element will work, though it really should.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000296
297In Python 2.2, iteration can be implemented separately, and
298\method{__getitem__()} methods can be limited to classes that really
299do support random access. The basic idea of iterators is quite
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000300simple. A new built-in function, \function{iter(obj)} or
301\code{iter(\var{C}, \var{sentinel})}, is used to get an iterator.
302\function{iter(obj)} returns an iterator for the object \var{obj},
303while \code{iter(\var{C}, \var{sentinel})} returns an iterator that
304will invoke the callable object \var{C} until it returns
305\var{sentinel} to signal that the iterator is done.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000306
307Python classes can define an \method{__iter__()} method, which should
308create and return a new iterator for the object; if the object is its
309own iterator, this method can just return \code{self}. In particular,
310iterators will usually be their own iterators. Extension types
311implemented in C can implement a \code{tp_iter} function in order to
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000312return an iterator, and extension types that want to behave as
313iterators can define a \code{tp_iternext} function.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000314
315So what do iterators do? They have one required method,
316\method{next()}, which takes no arguments and returns the next value.
317When there are no more values to be returned, calling \method{next()}
318should raise the \exception{StopIteration} exception.
319
320\begin{verbatim}
321>>> L = [1,2,3]
322>>> i = iter(L)
323>>> print i
324<iterator object at 0x8116870>
325>>> i.next()
3261
327>>> i.next()
3282
329>>> i.next()
3303
331>>> i.next()
332Traceback (most recent call last):
333 File "<stdin>", line 1, in ?
334StopIteration
335>>>
336\end{verbatim}
337
338In 2.2, Python's \keyword{for} statement no longer expects a sequence;
339it expects something for which \function{iter()} will return something.
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000340For backward compatibility and convenience, an iterator is
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000341automatically constructed for sequences that don't implement
342\method{__iter__()} or a \code{tp_iter} slot, so \code{for i in
343[1,2,3]} will still work. Wherever the Python interpreter loops over
344a sequence, it's been changed to use the iterator protocol. This
345means you can do things like this:
346
347\begin{verbatim}
348>>> i = iter(L)
349>>> a,b,c = i
350>>> a,b,c
351(1, 2, 3)
352>>>
353\end{verbatim}
354
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000355Iterator support has been added to some of Python's basic types.
Fred Drake0d002542001-07-17 13:55:33 +0000356Calling \function{iter()} on a dictionary will return an iterator
Andrew M. Kuchling6ea9f0b2001-07-17 14:50:31 +0000357which loops over its keys:
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000358
359\begin{verbatim}
360>>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
361... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
362>>> for key in m: print key, m[key]
363...
364Mar 3
365Feb 2
366Aug 8
367Sep 9
368May 5
369Jun 6
370Jul 7
371Jan 1
372Apr 4
373Nov 11
374Dec 12
375Oct 10
376>>>
377\end{verbatim}
378
379That's just the default behaviour. If you want to iterate over keys,
380values, or key/value pairs, you can explicitly call the
381\method{iterkeys()}, \method{itervalues()}, or \method{iteritems()}
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000382methods to get an appropriate iterator. In a minor related change,
383the \keyword{in} operator now works on dictionaries, so
384\code{\var{key} in dict} is now equivalent to
385\code{dict.has_key(\var{key})}.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000386
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000387Files also provide an iterator, which calls the \method{readline()}
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000388method until there are no more lines in the file. This means you can
389now read each line of a file using code like this:
390
391\begin{verbatim}
392for line in file:
393 # do something for each line
394\end{verbatim}
395
396Note that you can only go forward in an iterator; there's no way to
397get the previous element, reset the iterator, or make a copy of it.
Fred Drake0d002542001-07-17 13:55:33 +0000398An iterator object could provide such additional capabilities, but the
399iterator protocol only requires a \method{next()} method.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000400
401\begin{seealso}
402
403\seepep{234}{Iterators}{Written by Ka-Ping Yee and GvR; implemented
404by the Python Labs crew, mostly by GvR and Tim Peters.}
405
406\end{seealso}
407
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +0000408
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000409%======================================================================
410\section{PEP 255: Simple Generators}
411
412Generators are another new feature, one that interacts with the
413introduction of iterators.
414
415You're doubtless familiar with how function calls work in Python or
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000416C. When you call a function, it gets a private namespace where its local
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000417variables are created. When the function reaches a \keyword{return}
418statement, the local variables are destroyed and the resulting value
419is returned to the caller. A later call to the same function will get
420a fresh new set of local variables. But, what if the local variables
421weren't destroyed on exiting a function? What if you could later
422resume the function where it left off? This is what generators
423provide; they can be thought of as resumable functions.
424
425Here's the simplest example of a generator function:
426
427\begin{verbatim}
428def generate_ints(N):
429 for i in range(N):
430 yield i
431\end{verbatim}
432
433A new keyword, \keyword{yield}, was introduced for generators. Any
434function containing a \keyword{yield} statement is a generator
435function; this is detected by Python's bytecode compiler which
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000436compiles the function specially as a result. Because a new keyword was
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000437introduced, generators must be explicitly enabled in a module by
438including a \code{from __future__ import generators} statement near
439the top of the module's source code. In Python 2.3 this statement
440will become unnecessary.
441
442When you call a generator function, it doesn't return a single value;
443instead it returns a generator object that supports the iterator
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000444protocol. On executing the \keyword{yield} statement, the generator
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000445outputs the value of \code{i}, similar to a \keyword{return}
446statement. The big difference between \keyword{yield} and a
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000447\keyword{return} statement is that on reaching a \keyword{yield} the
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000448generator's state of execution is suspended and local variables are
449preserved. On the next call to the generator's \code{.next()} method,
450the function will resume executing immediately after the
451\keyword{yield} statement. (For complicated reasons, the
452\keyword{yield} statement isn't allowed inside the \keyword{try} block
453of a \code{try...finally} statement; read PEP 255 for a full
454explanation of the interaction between \keyword{yield} and
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000455exceptions.)
456
457Here's a sample usage of the \function{generate_ints} generator:
458
459\begin{verbatim}
460>>> gen = generate_ints(3)
461>>> gen
462<generator object at 0x8117f90>
463>>> gen.next()
4640
465>>> gen.next()
4661
467>>> gen.next()
4682
469>>> gen.next()
470Traceback (most recent call last):
471 File "<stdin>", line 1, in ?
472 File "<stdin>", line 2, in generate_ints
473StopIteration
474>>>
475\end{verbatim}
476
477You could equally write \code{for i in generate_ints(5)}, or
478\code{a,b,c = generate_ints(3)}.
479
480Inside a generator function, the \keyword{return} statement can only
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000481be used without a value, and signals the end of the procession of
482values; afterwards the generator cannot return any further values.
483\keyword{return} with a value, such as \code{return 5}, is a syntax
484error inside a generator function. The end of the generator's results
485can also be indicated by raising \exception{StopIteration} manually,
486or by just letting the flow of execution fall off the bottom of the
487function.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000488
489You could achieve the effect of generators manually by writing your
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000490own class and storing all the local variables of the generator as
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000491instance variables. For example, returning a list of integers could
492be done by setting \code{self.count} to 0, and having the
493\method{next()} method increment \code{self.count} and return it.
Andrew M. Kuchlingc32cc7c2001-07-17 18:25:01 +0000494However, for a moderately complicated generator, writing a
495corresponding class would be much messier.
496\file{Lib/test/test_generators.py} contains a number of more
497interesting examples. The simplest one implements an in-order
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000498traversal of a tree using generators recursively.
499
500\begin{verbatim}
501# A recursive generator that generates Tree leaves in in-order.
502def inorder(t):
503 if t:
504 for x in inorder(t.left):
505 yield x
506 yield t.label
507 for x in inorder(t.right):
508 yield x
509\end{verbatim}
510
511Two other examples in \file{Lib/test/test_generators.py} produce
512solutions for the N-Queens problem (placing $N$ queens on an $NxN$
513chess board so that no queen threatens another) and the Knight's Tour
514(a route that takes a knight to every square of an $NxN$ chessboard
515without visiting any square twice).
516
517The idea of generators comes from other programming languages,
518especially Icon (\url{http://www.cs.arizona.edu/icon/}), where the
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000519idea of generators is central. In Icon, every
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000520expression and function call behaves like a generator. One example
521from ``An Overview of the Icon Programming Language'' at
522\url{http://www.cs.arizona.edu/icon/docs/ipd266.htm} gives an idea of
523what this looks like:
524
525\begin{verbatim}
526sentence := "Store it in the neighboring harbor"
527if (i := find("or", sentence)) > 5 then write(i)
528\end{verbatim}
529
530The \function{find()} function returns the indexes at which the
531substring ``or'' is found: 3, 23, 33. In the \keyword{if} statement,
532\code{i} is first assigned a value of 3, but 3 is less than 5, so the
533comparison fails, and Icon retries it with the second value of 23. 23
534is greater than 5, so the comparison now succeeds, and the code prints
535the value 23 to the screen.
536
537Python doesn't go nearly as far as Icon in adopting generators as a
538central concept. Generators are considered a new part of the core
539Python language, but learning or using them isn't compulsory; if they
540don't solve any problems that you have, feel free to ignore them.
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000541One novel feature of Python's interface as compared to
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000542Icon's is that a generator's state is represented as a concrete object
543that can be passed around to other functions or stored in a data
544structure.
545
546\begin{seealso}
547
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000548\seepep{255}{Simple Generators}{Written by Neil Schemenauer, Tim
549Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer
550and Tim Peters, with other fixes from the Python Labs crew.}
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000551
552\end{seealso}
553
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +0000554
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000555%======================================================================
Andrew M. Kuchling2f0047a2001-09-05 14:53:31 +0000556\section{PEP 237: Unifying Long Integers and Integers}
557
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000558In recent versions, the distinction between regular integers, which
559are 32-bit values on most machines, and long integers, which can be of
560arbitrary size, was becoming an annoyance. For example, on platforms
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000561that support files larger than \code{2**32} bytes, the
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000562\method{tell()} method of file objects has to return a long integer.
563However, there were various bits of Python that expected plain
564integers and would raise an error if a long integer was provided
Andrew M. Kuchlingd6e40e22001-09-10 16:18:50 +0000565instead. For example, in Python 1.5, only regular integers
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000566could be used as a slice index, and \code{'abc'[1L:]} would raise a
567\exception{TypeError} exception with the message 'slice index must be
568int'.
Andrew M. Kuchling2f0047a2001-09-05 14:53:31 +0000569
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000570Python 2.2 will shift values from short to long integers as required.
571The 'L' suffix is no longer needed to indicate a long integer literal,
572as now the compiler will choose the appropriate type. (Using the 'L'
573suffix will be discouraged in future 2.x versions of Python,
574triggering a warning in Python 2.4, and probably dropped in Python
5753.0.) Many operations that used to raise an \exception{OverflowError}
576will now return a long integer as their result. For example:
577
578\begin{verbatim}
579>>> 1234567890123
Andrew M. Kuchlingd6e40e22001-09-10 16:18:50 +00005801234567890123L
581>>> 2 ** 64
58218446744073709551616L
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000583\end{verbatim}
584
585In most cases, integers and long integers will now be treated
586identically. You can still distinguish them with the
587\function{type()} built-in function, but that's rarely needed. The
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000588\function{int()} constructor will now return a long integer if the value
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000589is large enough.
590
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +0000591\begin{seealso}
592
593\seepep{237}{Unifying Long Integers and Integers}{Written by
594Moshe Zadka and Guido van Rossum. Implemented mostly by Guido van Rossum.}
595
596\end{seealso}
Andrew M. Kuchling2f0047a2001-09-05 14:53:31 +0000597
Andrew M. Kuchlingd4707e32001-09-28 20:46:46 +0000598
Andrew M. Kuchling2f0047a2001-09-05 14:53:31 +0000599%======================================================================
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000600\section{PEP 238: Changing the Division Operator}
601
602The most controversial change in Python 2.2 is the start of an effort
603to fix an old design flaw that's been in Python from the beginning.
604Currently Python's division operator, \code{/}, behaves like C's
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000605division operator when presented with two integer arguments: it
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000606returns an integer result that's truncated down when there would be
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000607a fractional part. For example, \code{3/2} is 1, not 1.5, and
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000608\code{(-1)/2} is -1, not -0.5. This means that the results of divison
609can vary unexpectedly depending on the type of the two operands and
610because Python is dynamically typed, it can be difficult to determine
611the possible types of the operands.
612
613(The controversy is over whether this is \emph{really} a design flaw,
614and whether it's worth breaking existing code to fix this. It's
615caused endless discussions on python-dev and in July erupted into an
616storm of acidly sarcastic postings on \newsgroup{comp.lang.python}. I
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000617won't argue for either side here and will stick to describing what's
618implemented in 2.2. Read PEP 238 for a summary of arguments and
619counter-arguments.)
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000620
621Because this change might break code, it's being introduced very
622gradually. Python 2.2 begins the transition, but the switch won't be
623complete until Python 3.0.
624
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000625First, I'll borrow some terminology from PEP 238. ``True division'' is the
Andrew M. Kuchling9e9c1352001-08-11 03:06:50 +0000626division that most non-programmers are familiar with: 3/2 is 1.5, 1/4
627is 0.25, and so forth. ``Floor division'' is what Python's \code{/}
628operator currently does when given integer operands; the result is the
629floor of the value returned by true division. ``Classic division'' is
630the current mixed behaviour of \code{/}; it returns the result of
631floor division when the operands are integers, and returns the result
632of true division when one of the operands is a floating-point number.
633
634Here are the changes 2.2 introduces:
635
636\begin{itemize}
637
638\item A new operator, \code{//}, is the floor division operator.
639(Yes, we know it looks like \Cpp's comment symbol.) \code{//}
640\emph{always} returns the floor divison no matter what the types of
641its operands are, so \code{1 // 2} is 0 and \code{1.0 // 2.0} is also
6420.0.
643
644\code{//} is always available in Python 2.2; you don't need to enable
645it using a \code{__future__} statement.
646
647\item By including a \code{from __future__ import true_division} in a
648module, the \code{/} operator will be changed to return the result of
649true division, so \code{1/2} is 0.5. Without the \code{__future__}
650statement, \code{/} still means classic division. The default meaning
651of \code{/} will not change until Python 3.0.
652
653\item Classes can define methods called \method{__truediv__} and
654\method{__floordiv__} to overload the two division operators. At the
655C level, there are also slots in the \code{PyNumberMethods} structure
656so extension types can define the two operators.
657
658% XXX a warning someday?
659
660\end{itemize}
661
662\begin{seealso}
663
664\seepep{238}{Changing the Division Operator}{Written by Moshe Zadka and
665Guido van Rossum. Implemented by Guido van Rossum..}
666
667\end{seealso}
668
669
670%======================================================================
Andrew M. Kuchlinga43e7032001-06-27 20:32:12 +0000671\section{Unicode Changes}
672
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +0000673Python's Unicode support has been enhanced a bit in 2.2. Unicode
Andrew M. Kuchlinga6d2a042001-07-20 18:34:34 +0000674strings are usually stored as UCS-2, as 16-bit unsigned integers.
Andrew M. Kuchlingf5fec3c2001-07-19 01:48:08 +0000675Python 2.2 can also be compiled to use UCS-4, 32-bit unsigned
676integers, as its internal encoding by supplying
677\longprogramopt{enable-unicode=ucs4} to the configure script. When
Andrew M. Kuchlingab010872001-07-19 14:59:53 +0000678built to use UCS-4 (a ``wide Python''), the interpreter can natively
Andrew M. Kuchlinga6d2a042001-07-20 18:34:34 +0000679handle Unicode characters from U+000000 to U+110000, so the range of
680legal values for the \function{unichr()} function is expanded
681accordingly. Using an interpreter compiled to use UCS-2 (a ``narrow
682Python''), values greater than 65535 will still cause
683\function{unichr()} to raise a \exception{ValueError} exception.
Andrew M. Kuchlingab010872001-07-19 14:59:53 +0000684
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000685% XXX is this still unimplemented?
Andrew M. Kuchlingab010872001-07-19 14:59:53 +0000686All this is the province of the still-unimplemented PEP 261, ``Support
687for `wide' Unicode characters''; consult it for further details, and
Andrew M. Kuchlinga6d2a042001-07-20 18:34:34 +0000688please offer comments on the PEP and on your experiences with the
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00006892.2 beta releases.
Andrew M. Kuchling279e7442001-10-22 02:03:40 +0000690% XXX update previous line once 2.2 reaches beta or final.
Andrew M. Kuchlingab010872001-07-19 14:59:53 +0000691
692Another change is much simpler to explain. Since their introduction,
693Unicode strings have supported an \method{encode()} method to convert
694the string to a selected encoding such as UTF-8 or Latin-1. A
695symmetric \method{decode(\optional{\var{encoding}})} method has been
696added to 8-bit strings (though not to Unicode strings) in 2.2.
697\method{decode()} assumes that the string is in the specified encoding
698and decodes it, returning whatever is returned by the codec.
699
700Using this new feature, codecs have been added for tasks not directly
701related to Unicode. For example, codecs have been added for
702uu-encoding, MIME's base64 encoding, and compression with the
703\module{zlib} module:
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +0000704
705\begin{verbatim}
706>>> s = """Here is a lengthy piece of redundant, overly verbose,
707... and repetitive text.
708... """
709>>> data = s.encode('zlib')
710>>> data
711'x\x9c\r\xc9\xc1\r\x80 \x10\x04\xc0?Ul...'
712>>> data.decode('zlib')
713'Here is a lengthy piece of redundant, overly verbose,\nand repetitive text.\n'
714>>> print s.encode('uu')
715begin 666 <data>
716M2&5R92!I<R!A(&QE;F=T:'D@<&EE8V4@;V8@<F5D=6YD86YT+"!O=F5R;'D@
717>=F5R8F]S92P*86YD(')E<&5T:71I=F4@=&5X="X*
718
719end
720>>> "sheesh".encode('rot-13')
721'furrfu'
722\end{verbatim}
Andrew M. Kuchlinga43e7032001-06-27 20:32:12 +0000723
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000724To convert a class instance to Unicode, a \method{__unicode__} method
725can be defined, analogous to \method{__str__}.
726% XXX who implemented that?
727
Andrew M. Kuchlingf5fec3c2001-07-19 01:48:08 +0000728\method{encode()} and \method{decode()} were implemented by
729Marc-Andr\'e Lemburg. The changes to support using UCS-4 internally
730were implemented by Fredrik Lundh and Martin von L\"owis.
Andrew M. Kuchlinga43e7032001-06-27 20:32:12 +0000731
Andrew M. Kuchlingf5fec3c2001-07-19 01:48:08 +0000732\begin{seealso}
733
734\seepep{261}{Support for `wide' Unicode characters}{PEP written by
735Paul Prescod. Not yet accepted or fully implemented.}
736
737\end{seealso}
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +0000738
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000739%======================================================================
740\section{PEP 227: Nested Scopes}
741
742In Python 2.1, statically nested scopes were added as an optional
743feature, to be enabled by a \code{from __future__ import
744nested_scopes} directive. In 2.2 nested scopes no longer need to be
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000745specially enabled, and are now always present. The rest of this section
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000746is a copy of the description of nested scopes from my ``What's New in
747Python 2.1'' document; if you read it when 2.1 came out, you can skip
748the rest of this section.
749
750The largest change introduced in Python 2.1, and made complete in 2.2,
751is to Python's scoping rules. In Python 2.0, at any given time there
752are at most three namespaces used to look up variable names: local,
753module-level, and the built-in namespace. This often surprised people
754because it didn't match their intuitive expectations. For example, a
755nested recursive function definition doesn't work:
756
757\begin{verbatim}
758def f():
759 ...
760 def g(value):
761 ...
762 return g(value-1) + 1
763 ...
764\end{verbatim}
765
766The function \function{g()} will always raise a \exception{NameError}
767exception, because the binding of the name \samp{g} isn't in either
768its local namespace or in the module-level namespace. This isn't much
769of a problem in practice (how often do you recursively define interior
770functions like this?), but this also made using the \keyword{lambda}
771statement clumsier, and this was a problem in practice. In code which
772uses \keyword{lambda} you can often find local variables being copied
773by passing them as the default values of arguments.
774
775\begin{verbatim}
776def find(self, name):
777 "Return list of any entries equal to 'name'"
778 L = filter(lambda x, name=name: x == name,
779 self.list_attribute)
780 return L
781\end{verbatim}
782
783The readability of Python code written in a strongly functional style
784suffers greatly as a result.
785
786The most significant change to Python 2.2 is that static scoping has
787been added to the language to fix this problem. As a first effect,
788the \code{name=name} default argument is now unnecessary in the above
789example. Put simply, when a given variable name is not assigned a
790value within a function (by an assignment, or the \keyword{def},
791\keyword{class}, or \keyword{import} statements), references to the
792variable will be looked up in the local namespace of the enclosing
793scope. A more detailed explanation of the rules, and a dissection of
794the implementation, can be found in the PEP.
795
796This change may cause some compatibility problems for code where the
797same variable name is used both at the module level and as a local
798variable within a function that contains further function definitions.
799This seems rather unlikely though, since such code would have been
800pretty confusing to read in the first place.
801
802One side effect of the change is that the \code{from \var{module}
803import *} and \keyword{exec} statements have been made illegal inside
804a function scope under certain conditions. The Python reference
805manual has said all along that \code{from \var{module} import *} is
806only legal at the top level of a module, but the CPython interpreter
807has never enforced this before. As part of the implementation of
808nested scopes, the compiler which turns Python source into bytecodes
809has to generate different code to access variables in a containing
810scope. \code{from \var{module} import *} and \keyword{exec} make it
811impossible for the compiler to figure this out, because they add names
812to the local namespace that are unknowable at compile time.
813Therefore, if a function contains function definitions or
814\keyword{lambda} expressions with free variables, the compiler will
815flag this by raising a \exception{SyntaxError} exception.
816
817To make the preceding explanation a bit clearer, here's an example:
818
819\begin{verbatim}
820x = 1
821def f():
822 # The next line is a syntax error
823 exec 'x=2'
824 def g():
825 return x
826\end{verbatim}
827
828Line 4 containing the \keyword{exec} statement is a syntax error,
829since \keyword{exec} would define a new local variable named \samp{x}
830whose value should be accessed by \function{g()}.
831
832This shouldn't be much of a limitation, since \keyword{exec} is rarely
833used in most Python code (and when it is used, it's often a sign of a
834poor design anyway).
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000835
836\begin{seealso}
837
838\seepep{227}{Statically Nested Scopes}{Written and implemented by
839Jeremy Hylton.}
840
841\end{seealso}
842
Andrew M. Kuchlinga43e7032001-06-27 20:32:12 +0000843
844%======================================================================
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +0000845\section{New and Improved Modules}
846
847\begin{itemize}
848
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000849 \item The \module{xmlrpclib} module was contributed to the standard
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000850 library by Fredrik Lundh, provding support for writing XML-RPC
851 clients. XML-RPC is a simple remote procedure call protocol built on
Andrew M. Kuchling8c69c912001-08-07 14:28:58 +0000852 top of HTTP and XML. For example, the following snippet retrieves a
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000853 list of RSS channels from the O'Reilly Network, and then
854 lists the recent headlines for one channel:
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000855
856\begin{verbatim}
857import xmlrpclib
858s = xmlrpclib.Server(
859 'http://www.oreillynet.com/meerkat/xml-rpc/server.php')
860channels = s.meerkat.getChannels()
861# channels is a list of dictionaries, like this:
862# [{'id': 4, 'title': 'Freshmeat Daily News'}
863# {'id': 190, 'title': '32Bits Online'},
864# {'id': 4549, 'title': '3DGamers'}, ... ]
865
866# Get the items for one channel
867items = s.meerkat.getItems( {'channel': 4} )
868
869# 'items' is another list of dictionaries, like this:
870# [{'link': 'http://freshmeat.net/releases/52719/',
871# 'description': 'A utility which converts HTML to XSL FO.',
872# 'title': 'html2fo 0.3 (Default)'}, ... ]
873\end{verbatim}
874
Andrew M. Kuchlingd4707e32001-09-28 20:46:46 +0000875The \module{SimpleXMLRPCServer} module makes it easy to create
876straightforward XML-RPC servers. See \url{http://www.xmlrpc.com/} for
877more information about XML-RPC.
878
879 \item The new \module{hmac} module implements implements the HMAC
880 algorithm described by \rfc{2104}.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000881
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000882 \item The Python profiler has been extensively reworked and various
883 errors in its output have been corrected. (Contributed by Fred
884 Fred~L. Drake, Jr. and Tim Peters.)
885
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000886 \item The \module{socket} module can be compiled to support IPv6;
Andrew M. Kuchlingddeb1352001-07-16 14:35:52 +0000887 specify the \longprogramopt{enable-ipv6} option to Python's configure
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000888 script. (Contributed by Jun-ichiro ``itojun'' Hagino.)
889
890 \item Two new format characters were added to the \module{struct}
891 module for 64-bit integers on platforms that support the C
892 \ctype{long long} type. \samp{q} is for a signed 64-bit integer,
893 and \samp{Q} is for an unsigned one. The value is returned in
894 Python's long integer type. (Contributed by Tim Peters.)
895
896 \item In the interpreter's interactive mode, there's a new built-in
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000897 function \function{help()} that uses the \module{pydoc} module
898 introduced in Python 2.1 to provide interactive help.
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000899 \code{help(\var{object})} displays any available help text about
900 \var{object}. \code{help()} with no argument puts you in an online
901 help utility, where you can enter the names of functions, classes,
902 or modules to read their help text.
903 (Contributed by Guido van Rossum, using Ka-Ping Yee's \module{pydoc} module.)
904
905 \item Various bugfixes and performance improvements have been made
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +0000906 to the SRE engine underlying the \module{re} module. For example,
Andrew M. Kuchlingbeb38552001-10-22 14:11:06 +0000907 the \function{re.sub()} and \function{re.split()} functions have
908 been rewritten in C. Another contributed patch speeds up certain
909 Unicode character ranges by a factor of two. (SRE is maintained by
910 Fredrik Lundh. The BIGCHARSET patch was contributed by Martin von
911 L\"owis.)
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000912
Andrew M. Kuchling1efd7ad2001-09-14 16:19:27 +0000913 \item The \module{smtplib} module now supports \rfc{2487}, ``Secure
914 SMTP over TLS'', so it's now possible to encrypt the SMTP traffic
915 between a Python program and the mail transport agent being handed a
916 message. (Contributed by Gerhard H\"aring.)
917
Andrew M. Kuchlinga6d2a042001-07-20 18:34:34 +0000918 \item The \module{imaplib} module, maintained by Piers Lauder, has
919 support for several new extensions: the NAMESPACE extension defined
920 in \rfc{2342}, SORT, GETACL and SETACL. (Contributed by Anthony
921 Baxter and Michel Pelletier.)
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +0000922
Andrew M. Kuchlingd4707e32001-09-28 20:46:46 +0000923 \item The \module{rfc822} module's parsing of email addresses is now
924 compliant with \rfc{2822}, an update to \rfc{822}. (The module's
925 name is \emph{not} going to be changed to \samp{rfc2822}.) A new
926 package, \module{email}, has also been added for parsing and
927 generating e-mail messages. (Contributed by Barry Warsaw, and
928 arising out of his work on Mailman.)
Andrew M. Kuchling77707672001-07-31 15:51:16 +0000929
930 \item New constants \constant{ascii_letters},
931 \constant{ascii_lowercase}, and \constant{ascii_uppercase} were
932 added to the \module{string} module. There were several modules in
933 the standard library that used \constant{string.letters} to mean the
934 ranges A-Za-z, but that assumption is incorrect when locales are in
935 use, because \constant{string.letters} varies depending on the set
936 of legal characters defined by the current locale. The buggy
937 modules have all been fixed to use \constant{ascii_letters} instead.
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000938 (Reported by an unknown person; fixed by Fred~L. Drake, Jr.)
Andrew M. Kuchling77707672001-07-31 15:51:16 +0000939
Andrew M. Kuchling8c69c912001-08-07 14:28:58 +0000940 \item The \module{mimetypes} module now makes it easier to use
941 alternative MIME-type databases by the addition of a
942 \class{MimeTypes} class, which takes a list of filenames to be
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000943 parsed. (Contributed by Fred~L. Drake, Jr.)
Andrew M. Kuchling8c69c912001-08-07 14:28:58 +0000944
Andrew M. Kuchlingd6e40e22001-09-10 16:18:50 +0000945 \item A \class{Timer} class was added to the \module{threading}
946 module that allows scheduling an activity to happen at some future
947 time. (Contributed by Itamar Shtull-Trauring.)
Andrew M. Kuchling2f0047a2001-09-05 14:53:31 +0000948
Andrew M. Kuchling77707672001-07-31 15:51:16 +0000949\end{itemize}
950
951
952%======================================================================
953\section{Interpreter Changes and Fixes}
954
955Some of the changes only affect people who deal with the Python
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000956interpreter at the C level because they're writing Python extension modules,
Andrew M. Kuchling77707672001-07-31 15:51:16 +0000957embedding the interpreter, or just hacking on the interpreter itself.
958If you only write Python code, none of the changes described here will
959affect you very much.
960
961\begin{itemize}
962
963 \item Profiling and tracing functions can now be implemented in C,
964 which can operate at much higher speeds than Python-based functions
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000965 and should reduce the overhead of profiling and tracing. This
966 will be of interest to authors of development environments for
Andrew M. Kuchling77707672001-07-31 15:51:16 +0000967 Python. Two new C functions were added to Python's API,
968 \cfunction{PyEval_SetProfile()} and \cfunction{PyEval_SetTrace()}.
969 The existing \function{sys.setprofile()} and
970 \function{sys.settrace()} functions still exist, and have simply
971 been changed to use the new C-level interface. (Contributed by Fred
972 L. Drake, Jr.)
973
974 \item Another low-level API, primarily of interest to implementors
975 of Python debuggers and development tools, was added.
976 \cfunction{PyInterpreterState_Head()} and
977 \cfunction{PyInterpreterState_Next()} let a caller walk through all
978 the existing interpreter objects;
979 \cfunction{PyInterpreterState_ThreadHead()} and
980 \cfunction{PyThreadState_Next()} allow looping over all the thread
981 states for a given interpreter. (Contributed by David Beazley.)
982
983 \item A new \samp{et} format sequence was added to
984 \cfunction{PyArg_ParseTuple}; \samp{et} takes both a parameter and
985 an encoding name, and converts the parameter to the given encoding
986 if the parameter turns out to be a Unicode string, or leaves it
987 alone if it's an 8-bit string, assuming it to already be in the
988 desired encoding. This differs from the \samp{es} format character,
989 which assumes that 8-bit strings are in Python's default ASCII
990 encoding and converts them to the specified new encoding.
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +0000991 (Contributed by M.-A. Lemburg, and used for the MBCS support on
992 Windows described in the following section.)
Andrew M. Kuchling0ab31b82001-08-29 01:16:54 +0000993
994 \item Two new flags \constant{METH_NOARGS} and \constant{METH_O} are
995 available in method definition tables to simplify implementation of
996 methods with no arguments or a single untyped argument. Calling
997 such methods is more efficient than calling a corresponding method
998 that uses \constant{METH_VARARGS}.
999 Also, the old \constant{METH_OLDARGS} style of writing C methods is
1000 now officially deprecated.
1001
1002\item
1003 Two new wrapper functions, \cfunction{PyOS_snprintf()} and
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001004 \cfunction{PyOS_vsnprintf()} were added to provide
Andrew M. Kuchling0ab31b82001-08-29 01:16:54 +00001005 cross-platform implementations for the relatively new
1006 \cfunction{snprintf()} and \cfunction{vsnprintf()} C lib APIs. In
1007 contrast to the standard \cfunction{sprintf()} and
1008 \cfunction{vsprintf()} functions, the Python versions check the
1009 bounds of the buffer used to protect against buffer overruns.
1010 (Contributed by M.-A. Lemburg.)
Andrew M. Kuchling77707672001-07-31 15:51:16 +00001011
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001012\end{itemize}
1013
1014
1015%======================================================================
1016\section{Other Changes and Fixes}
1017
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +00001018% XXX update the patch and bug figures as we go
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001019As usual there were a bunch of other improvements and bugfixes
1020scattered throughout the source tree. A search through the CVS change
Andrew M. Kuchling32e32322001-10-22 15:32:05 +00001021logs finds there were 312 patches applied, and 391 bugs fixed; both
Andrew M. Kuchling4dbf8712001-07-16 02:17:14 +00001022figures are likely to be underestimates. Some of the more notable
1023changes are:
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001024
1025\begin{itemize}
1026
Andrew M. Kuchling0e03f582001-08-30 21:30:16 +00001027 \item The code for the MacOS port for Python, maintained by Jack
1028 Jansen, is now kept in the main Python CVS tree, and many changes
Andrew M. Kuchling279e7442001-10-22 02:03:40 +00001029 have been made to support MacOS~X.
Andrew M. Kuchling0e03f582001-08-30 21:30:16 +00001030
1031The most significant change is the ability to build Python as a
1032framework, enabled by supplying the \longprogramopt{enable-framework}
1033option to the configure script when compiling Python. According to
1034Jack Jansen, ``This installs a self-contained Python installation plus
Andrew M. Kuchling279e7442001-10-22 02:03:40 +00001035the OS~X framework "glue" into
Andrew M. Kuchling0e03f582001-08-30 21:30:16 +00001036\file{/Library/Frameworks/Python.framework} (or another location of
1037choice). For now there is little immediate added benefit to this
1038(actually, there is the disadvantage that you have to change your PATH
1039to be able to find Python), but it is the basis for creating a
1040full-blown Python application, porting the MacPython IDE, possibly
1041using Python as a standard OSA scripting language and much more.''
1042
1043Most of the MacPython toolbox modules, which interface to MacOS APIs
Andrew M. Kuchling279e7442001-10-22 02:03:40 +00001044such as windowing, QuickTime, scripting, etc. have been ported to OS~X,
Andrew M. Kuchlingbeb38552001-10-22 14:11:06 +00001045but they've been left commented out in \file{setup.py}. People who want
Andrew M. Kuchling0e03f582001-08-30 21:30:16 +00001046to experiment with these modules can uncomment them manually.
1047
1048% Jack's original comments:
1049%The main change is the possibility to build Python as a
1050%framework. This installs a self-contained Python installation plus the
1051%OSX framework "glue" into /Library/Frameworks/Python.framework (or
1052%another location of choice). For now there is little immedeate added
1053%benefit to this (actually, there is the disadvantage that you have to
1054%change your PATH to be able to find Python), but it is the basis for
1055%creating a fullblown Python application, porting the MacPython IDE,
1056%possibly using Python as a standard OSA scripting language and much
1057%more. You enable this with "configure --enable-framework".
1058
1059%The other change is that most MacPython toolbox modules, which
1060%interface to all the MacOS APIs such as windowing, quicktime,
1061%scripting, etc. have been ported. Again, most of these are not of
1062%immedeate use, as they need a full application to be really useful, so
1063%they have been commented out in setup.py. People wanting to experiment
1064%can uncomment them. Gestalt and Internet Config modules are enabled by
1065%default.
Andrew M. Kuchling0e03f582001-08-30 21:30:16 +00001066
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +00001067 \item Keyword arguments passed to builtin functions that don't take them
1068 now cause a \exception{TypeError} exception to be raised, with the
1069 message "\var{function} takes no keyword arguments".
1070
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001071 \item Weak references, added in Python 2.1 as an extension module,
1072 are now part of the core because they're used in the implementation
1073 of new-style classes. The \exception{ReferenceError} exception has
1074 therefore moved from the \module{weakref} module to become a
1075 built-in exception.
1076
Andrew M. Kuchling94a7eba2001-08-15 15:55:48 +00001077 \item A new script, \file{Tools/scripts/cleanfuture.py} by Tim
1078 Peters, automatically removes obsolete \code{__future__} statements
1079 from Python source code.
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +00001080
1081 \item The new license introduced with Python 1.6 wasn't
1082 GPL-compatible. This is fixed by some minor textual changes to the
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001083 2.2 license, so it's now legal to embed Python inside a GPLed
1084 program again. Note that Python itself is not GPLed, but instead is
1085 under a license that's essentially equivalent to the BSD license,
1086 same as it always was. The license changes were also applied to the
1087 Python 2.0.1 and 2.1.1 releases.
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +00001088
Andrew M. Kuchlingf4ccf582001-07-31 01:11:36 +00001089 \item When presented with a Unicode filename on Windows, Python will
1090 now convert it to an MBCS encoded string, as used by the Microsoft
1091 file APIs. As MBCS is explicitly used by the file APIs, Python's
1092 choice of ASCII as the default encoding turns out to be an
1093 annoyance.
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +00001094 (Contributed by Mark Hammond with assistance from Marc-Andr\'e
1095 Lemburg.)
1096
Andrew M. Kuchlingd6e40e22001-09-10 16:18:50 +00001097 \item Large file support is now enabled on Windows. (Contributed by
1098 Tim Peters.)
1099
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +00001100 \item The \file{Tools/scripts/ftpmirror.py} script
1101 now parses a \file{.netrc} file, if you have one.
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +00001102 (Contributed by Mike Romberg.)
Andrew M. Kuchling2cd712b2001-07-16 13:39:08 +00001103
Andrew M. Kuchling4cf52a92001-07-17 12:48:48 +00001104 \item Some features of the object returned by the
1105 \function{xrange()} function are now deprecated, and trigger
1106 warnings when they're accessed; they'll disappear in Python 2.3.
1107 \class{xrange} objects tried to pretend they were full sequence
1108 types by supporting slicing, sequence multiplication, and the
1109 \keyword{in} operator, but these features were rarely used and
1110 therefore buggy. The \method{tolist()} method and the
1111 \member{start}, \member{stop}, and \member{step} attributes are also
1112 being deprecated. At the C level, the fourth argument to the
1113 \cfunction{PyRange_New()} function, \samp{repeat}, has also been
1114 deprecated.
1115
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +00001116 \item There were a bunch of patches to the dictionary
1117 implementation, mostly to fix potential core dumps if a dictionary
1118 contains objects that sneakily changed their hash value, or mutated
1119 the dictionary they were contained in. For a while python-dev fell
Andrew M. Kuchling8b42f012001-10-22 02:00:11 +00001120 into a gentle rhythm of Michael Hudson finding a case that dumped
1121 core, Tim Peters fixing the bug, Michael finding another case, and round
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +00001122 and round it went.
1123
Andrew M. Kuchling33a3b632001-09-04 21:25:58 +00001124 \item On Windows, Python can now be compiled with Borland C thanks
1125 to a number of patches contributed by Stephen Hansen, though the
1126 result isn't fully functional yet. (But this \emph{is} progress...)
Andrew M. Kuchling8c69c912001-08-07 14:28:58 +00001127
Andrew M. Kuchlingf4ccf582001-07-31 01:11:36 +00001128 \item Another Windows enhancement: Wise Solutions generously offered
1129 PythonLabs use of their InstallerMaster 8.1 system. Earlier
1130 PythonLabs Windows installers used Wise 5.0a, which was beginning to
1131 show its age. (Packaged up by Tim Peters.)
1132
Andrew M. Kuchling8c69c912001-08-07 14:28:58 +00001133 \item Files ending in \samp{.pyw} can now be imported on Windows.
1134 \samp{.pyw} is a Windows-only thing, used to indicate that a script
1135 needs to be run using PYTHONW.EXE instead of PYTHON.EXE in order to
1136 prevent a DOS console from popping up to display the output. This
1137 patch makes it possible to import such scripts, in case they're also
1138 usable as modules. (Implemented by David Bolen.)
1139
Andrew M. Kuchling8cfa9052001-07-19 01:19:59 +00001140 \item On platforms where Python uses the C \cfunction{dlopen()} function
1141 to load extension modules, it's now possible to set the flags used
1142 by \cfunction{dlopen()} using the \function{sys.getdlopenflags()} and
1143 \function{sys.setdlopenflags()} functions. (Contributed by Bram Stolk.)
Andrew M. Kuchling2f0047a2001-09-05 14:53:31 +00001144
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +00001145 \item The \function{pow()} built-in function no longer supports 3
1146 arguments when floating-point numbers are supplied.
Andrew M. Kuchling1497b622001-09-24 14:51:16 +00001147 \code{pow(\var{x}, \var{y}, \var{z})} returns \code{(x**y) \% z}, but
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +00001148 this is never useful for floating point numbers, and the final
1149 result varies unpredictably depending on the platform. A call such
Andrew M. Kuchlingd6e40e22001-09-10 16:18:50 +00001150 as \code{pow(2.0, 8.0, 7.0)} will now raise a \exception{TypeError}
Andrew M. Kuchling26c39bf2001-09-10 03:20:53 +00001151 exception.
Andrew M. Kuchling77707672001-07-31 15:51:16 +00001152
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001153\end{itemize}
1154
1155
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001156%======================================================================
1157\section{Acknowledgements}
1158
1159The author would like to thank the following people for offering
Andrew M. Kuchling6ea9f0b2001-07-17 14:50:31 +00001160suggestions and corrections to various drafts of this article: Fred
Andrew M. Kuchlingbeb38552001-10-22 14:11:06 +00001161Bremmer, Keith Briggs, Andrew Dalke, Fred~L. Drake, Jr., Carel
1162Fellinger, Mark Hammond, Stephen Hansen, Jack Jansen, Marc-Andr\'e
1163Lemburg, Fredrik Lundh, Tim Peters, Neil Schemenauer, Guido van
1164Rossum.
Andrew M. Kuchlinga8defaa2001-05-05 16:37:29 +00001165
1166\end{document}