Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 1 | \documentclass{howto} |
| 2 | |
| 3 | % $Id$ |
| 4 | |
| 5 | \title{What's New in Python 2.2} |
Andrew M. Kuchling | d4707e3 | 2001-09-28 20:46:46 +0000 | [diff] [blame] | 6 | \release{0.06} |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 7 | \author{A.M. Kuchling} |
Andrew M. Kuchling | 7bf8277 | 2001-07-11 18:54:26 +0000 | [diff] [blame] | 8 | \authoraddress{\email{akuchlin@mems-exchange.org}} |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 9 | \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. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 15 | final version of Python 2.2 is released. Currently it's up to date |
Andrew M. Kuchling | d4707e3 | 2001-09-28 20:46:46 +0000 | [diff] [blame] | 16 | for Python 2.2 alpha 4. Please send any comments, bug reports, or |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 17 | questions, no matter how minor, to \email{akuchlin@mems-exchange.org}. |
| 18 | } |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 19 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 20 | This article explains the new features in Python 2.2. |
| 21 | |
| 22 | Python 2.2 can be thought of as the "cleanup release". There are some |
| 23 | features such as generators and iterators that are completely new, but |
| 24 | most of the changes, significant and far-reaching though they may be, |
| 25 | are aimed at cleaning up irregularities and dark corners of the |
| 26 | language design. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 27 | |
Andrew M. Kuchling | 1497b62 | 2001-09-24 14:51:16 +0000 | [diff] [blame] | 28 | This article doesn't attempt to provide a complete specification of |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 29 | the new features, but instead provides a convenient overview. For |
| 30 | full details, you should refer to the documentation for Python 2.2, |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 31 | such as the |
| 32 | \citetitle[http://python.sourceforge.net/devel-docs/lib/lib.html]{Python |
| 33 | Library Reference} and the |
| 34 | \citetitle[http://python.sourceforge.net/devel-docs/ref/ref.html]{Python |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 35 | Reference Manual}. |
| 36 | % XXX These \citetitle marks should get the python.org URLs for the final |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 37 | % release, just as soon as the docs are published there. |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 38 | If you want to understand the complete implementation and design |
| 39 | rationale for a change, refer to the PEP for a particular new feature. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 40 | |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 41 | The final release of Python 2.2 is planned for December 2001. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 42 | |
Andrew M. Kuchling | 1497b62 | 2001-09-24 14:51:16 +0000 | [diff] [blame] | 43 | \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 |
| 47 | features, and was written by Cameron Laird and Kathryn Soraiz.} |
| 48 | |
| 49 | \end{seealso} |
| 50 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 51 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 52 | %====================================================================== |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 53 | \section{PEP 252: Type and Class Changes} |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 54 | |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 55 | XXX I need to read and digest the relevant PEPs. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 56 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 57 | \begin{seealso} |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 58 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 59 | \seepep{252}{Making Types Look More Like Classes}{Written and implemented |
| 60 | by Guido van Rossum.} |
| 61 | |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 62 | \seeurl{http://www.python.org/2.2/descrintro.html}{A tutorial |
| 63 | on the type/class changes in 2.2.} |
| 64 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 65 | \end{seealso} |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 66 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 67 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 68 | %====================================================================== |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 69 | \section{PEP 234: Iterators} |
| 70 | |
| 71 | A significant addition to 2.2 is an iteration interface at both the C |
| 72 | and Python levels. Objects can define how they can be looped over by |
| 73 | callers. |
| 74 | |
| 75 | In Python versions up to 2.1, the usual way to make \code{for item in |
| 76 | obj} work is to define a \method{__getitem__()} method that looks |
| 77 | something like this: |
| 78 | |
| 79 | \begin{verbatim} |
| 80 | def __getitem__(self, index): |
| 81 | return <next item> |
| 82 | \end{verbatim} |
| 83 | |
| 84 | \method{__getitem__()} is more properly used to define an indexing |
| 85 | operation on an object so that you can write \code{obj[5]} to retrieve |
Andrew M. Kuchling | 8c69c91 | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 86 | the sixth element. It's a bit misleading when you're using this only |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 87 | to support \keyword{for} loops. Consider some file-like object that |
| 88 | wants to be looped over; the \var{index} parameter is essentially |
| 89 | meaningless, as the class probably assumes that a series of |
| 90 | \method{__getitem__()} calls will be made, with \var{index} |
| 91 | incrementing by one each time. In other words, the presence of the |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 92 | \method{__getitem__()} method doesn't mean that using \code{file[5]} |
| 93 | to randomly access the sixth element will work, though it really should. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 94 | |
| 95 | In Python 2.2, iteration can be implemented separately, and |
| 96 | \method{__getitem__()} methods can be limited to classes that really |
| 97 | do support random access. The basic idea of iterators is quite |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 98 | simple. A new built-in function, \function{iter(obj)} or |
| 99 | \code{iter(\var{C}, \var{sentinel})}, is used to get an iterator. |
| 100 | \function{iter(obj)} returns an iterator for the object \var{obj}, |
| 101 | while \code{iter(\var{C}, \var{sentinel})} returns an iterator that |
| 102 | will invoke the callable object \var{C} until it returns |
| 103 | \var{sentinel} to signal that the iterator is done. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 104 | |
| 105 | Python classes can define an \method{__iter__()} method, which should |
| 106 | create and return a new iterator for the object; if the object is its |
| 107 | own iterator, this method can just return \code{self}. In particular, |
| 108 | iterators will usually be their own iterators. Extension types |
| 109 | implemented in C can implement a \code{tp_iter} function in order to |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 110 | return an iterator, and extension types that want to behave as |
| 111 | iterators can define a \code{tp_iternext} function. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 112 | |
| 113 | So what do iterators do? They have one required method, |
| 114 | \method{next()}, which takes no arguments and returns the next value. |
| 115 | When there are no more values to be returned, calling \method{next()} |
| 116 | should raise the \exception{StopIteration} exception. |
| 117 | |
| 118 | \begin{verbatim} |
| 119 | >>> L = [1,2,3] |
| 120 | >>> i = iter(L) |
| 121 | >>> print i |
| 122 | <iterator object at 0x8116870> |
| 123 | >>> i.next() |
| 124 | 1 |
| 125 | >>> i.next() |
| 126 | 2 |
| 127 | >>> i.next() |
| 128 | 3 |
| 129 | >>> i.next() |
| 130 | Traceback (most recent call last): |
| 131 | File "<stdin>", line 1, in ? |
| 132 | StopIteration |
| 133 | >>> |
| 134 | \end{verbatim} |
| 135 | |
| 136 | In 2.2, Python's \keyword{for} statement no longer expects a sequence; |
| 137 | it expects something for which \function{iter()} will return something. |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 138 | For backward compatibility and convenience, an iterator is |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 139 | automatically constructed for sequences that don't implement |
| 140 | \method{__iter__()} or a \code{tp_iter} slot, so \code{for i in |
| 141 | [1,2,3]} will still work. Wherever the Python interpreter loops over |
| 142 | a sequence, it's been changed to use the iterator protocol. This |
| 143 | means you can do things like this: |
| 144 | |
| 145 | \begin{verbatim} |
| 146 | >>> i = iter(L) |
| 147 | >>> a,b,c = i |
| 148 | >>> a,b,c |
| 149 | (1, 2, 3) |
| 150 | >>> |
| 151 | \end{verbatim} |
| 152 | |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 153 | Iterator support has been added to some of Python's basic types. |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 154 | Calling \function{iter()} on a dictionary will return an iterator |
Andrew M. Kuchling | 6ea9f0b | 2001-07-17 14:50:31 +0000 | [diff] [blame] | 155 | which loops over its keys: |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 156 | |
| 157 | \begin{verbatim} |
| 158 | >>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, |
| 159 | ... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12} |
| 160 | >>> for key in m: print key, m[key] |
| 161 | ... |
| 162 | Mar 3 |
| 163 | Feb 2 |
| 164 | Aug 8 |
| 165 | Sep 9 |
| 166 | May 5 |
| 167 | Jun 6 |
| 168 | Jul 7 |
| 169 | Jan 1 |
| 170 | Apr 4 |
| 171 | Nov 11 |
| 172 | Dec 12 |
| 173 | Oct 10 |
| 174 | >>> |
| 175 | \end{verbatim} |
| 176 | |
| 177 | That's just the default behaviour. If you want to iterate over keys, |
| 178 | values, or key/value pairs, you can explicitly call the |
| 179 | \method{iterkeys()}, \method{itervalues()}, or \method{iteritems()} |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 180 | methods to get an appropriate iterator. In a minor related change, |
| 181 | the \keyword{in} operator now works on dictionaries, so |
| 182 | \code{\var{key} in dict} is now equivalent to |
| 183 | \code{dict.has_key(\var{key})}. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 184 | |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 185 | Files also provide an iterator, which calls the \method{readline()} |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 186 | method until there are no more lines in the file. This means you can |
| 187 | now read each line of a file using code like this: |
| 188 | |
| 189 | \begin{verbatim} |
| 190 | for line in file: |
| 191 | # do something for each line |
| 192 | \end{verbatim} |
| 193 | |
| 194 | Note that you can only go forward in an iterator; there's no way to |
| 195 | get the previous element, reset the iterator, or make a copy of it. |
Fred Drake | 0d00254 | 2001-07-17 13:55:33 +0000 | [diff] [blame] | 196 | An iterator object could provide such additional capabilities, but the |
| 197 | iterator protocol only requires a \method{next()} method. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 198 | |
| 199 | \begin{seealso} |
| 200 | |
| 201 | \seepep{234}{Iterators}{Written by Ka-Ping Yee and GvR; implemented |
| 202 | by the Python Labs crew, mostly by GvR and Tim Peters.} |
| 203 | |
| 204 | \end{seealso} |
| 205 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 206 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 207 | %====================================================================== |
| 208 | \section{PEP 255: Simple Generators} |
| 209 | |
| 210 | Generators are another new feature, one that interacts with the |
| 211 | introduction of iterators. |
| 212 | |
| 213 | You're doubtless familiar with how function calls work in Python or |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 214 | C. When you call a function, it gets a private namespace where its local |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 215 | variables are created. When the function reaches a \keyword{return} |
| 216 | statement, the local variables are destroyed and the resulting value |
| 217 | is returned to the caller. A later call to the same function will get |
| 218 | a fresh new set of local variables. But, what if the local variables |
| 219 | weren't destroyed on exiting a function? What if you could later |
| 220 | resume the function where it left off? This is what generators |
| 221 | provide; they can be thought of as resumable functions. |
| 222 | |
| 223 | Here's the simplest example of a generator function: |
| 224 | |
| 225 | \begin{verbatim} |
| 226 | def generate_ints(N): |
| 227 | for i in range(N): |
| 228 | yield i |
| 229 | \end{verbatim} |
| 230 | |
| 231 | A new keyword, \keyword{yield}, was introduced for generators. Any |
| 232 | function containing a \keyword{yield} statement is a generator |
| 233 | function; this is detected by Python's bytecode compiler which |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 234 | compiles the function specially as a result. Because a new keyword was |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 235 | introduced, generators must be explicitly enabled in a module by |
| 236 | including a \code{from __future__ import generators} statement near |
| 237 | the top of the module's source code. In Python 2.3 this statement |
| 238 | will become unnecessary. |
| 239 | |
| 240 | When you call a generator function, it doesn't return a single value; |
| 241 | instead it returns a generator object that supports the iterator |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 242 | protocol. On executing the \keyword{yield} statement, the generator |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 243 | outputs the value of \code{i}, similar to a \keyword{return} |
| 244 | statement. The big difference between \keyword{yield} and a |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 245 | \keyword{return} statement is that on reaching a \keyword{yield} the |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 246 | generator's state of execution is suspended and local variables are |
| 247 | preserved. On the next call to the generator's \code{.next()} method, |
| 248 | the function will resume executing immediately after the |
| 249 | \keyword{yield} statement. (For complicated reasons, the |
| 250 | \keyword{yield} statement isn't allowed inside the \keyword{try} block |
| 251 | of a \code{try...finally} statement; read PEP 255 for a full |
| 252 | explanation of the interaction between \keyword{yield} and |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 253 | exceptions.) |
| 254 | |
| 255 | Here's a sample usage of the \function{generate_ints} generator: |
| 256 | |
| 257 | \begin{verbatim} |
| 258 | >>> gen = generate_ints(3) |
| 259 | >>> gen |
| 260 | <generator object at 0x8117f90> |
| 261 | >>> gen.next() |
| 262 | 0 |
| 263 | >>> gen.next() |
| 264 | 1 |
| 265 | >>> gen.next() |
| 266 | 2 |
| 267 | >>> gen.next() |
| 268 | Traceback (most recent call last): |
| 269 | File "<stdin>", line 1, in ? |
| 270 | File "<stdin>", line 2, in generate_ints |
| 271 | StopIteration |
| 272 | >>> |
| 273 | \end{verbatim} |
| 274 | |
| 275 | You could equally write \code{for i in generate_ints(5)}, or |
| 276 | \code{a,b,c = generate_ints(3)}. |
| 277 | |
| 278 | Inside a generator function, the \keyword{return} statement can only |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 279 | be used without a value, and signals the end of the procession of |
| 280 | values; afterwards the generator cannot return any further values. |
| 281 | \keyword{return} with a value, such as \code{return 5}, is a syntax |
| 282 | error inside a generator function. The end of the generator's results |
| 283 | can also be indicated by raising \exception{StopIteration} manually, |
| 284 | or by just letting the flow of execution fall off the bottom of the |
| 285 | function. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 286 | |
| 287 | You could achieve the effect of generators manually by writing your |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 288 | own class and storing all the local variables of the generator as |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 289 | instance variables. For example, returning a list of integers could |
| 290 | be done by setting \code{self.count} to 0, and having the |
| 291 | \method{next()} method increment \code{self.count} and return it. |
Andrew M. Kuchling | c32cc7c | 2001-07-17 18:25:01 +0000 | [diff] [blame] | 292 | However, for a moderately complicated generator, writing a |
| 293 | corresponding class would be much messier. |
| 294 | \file{Lib/test/test_generators.py} contains a number of more |
| 295 | interesting examples. The simplest one implements an in-order |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 296 | traversal of a tree using generators recursively. |
| 297 | |
| 298 | \begin{verbatim} |
| 299 | # A recursive generator that generates Tree leaves in in-order. |
| 300 | def inorder(t): |
| 301 | if t: |
| 302 | for x in inorder(t.left): |
| 303 | yield x |
| 304 | yield t.label |
| 305 | for x in inorder(t.right): |
| 306 | yield x |
| 307 | \end{verbatim} |
| 308 | |
| 309 | Two other examples in \file{Lib/test/test_generators.py} produce |
| 310 | solutions for the N-Queens problem (placing $N$ queens on an $NxN$ |
| 311 | chess board so that no queen threatens another) and the Knight's Tour |
| 312 | (a route that takes a knight to every square of an $NxN$ chessboard |
| 313 | without visiting any square twice). |
| 314 | |
| 315 | The idea of generators comes from other programming languages, |
| 316 | especially Icon (\url{http://www.cs.arizona.edu/icon/}), where the |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 317 | idea of generators is central. In Icon, every |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 318 | expression and function call behaves like a generator. One example |
| 319 | from ``An Overview of the Icon Programming Language'' at |
| 320 | \url{http://www.cs.arizona.edu/icon/docs/ipd266.htm} gives an idea of |
| 321 | what this looks like: |
| 322 | |
| 323 | \begin{verbatim} |
| 324 | sentence := "Store it in the neighboring harbor" |
| 325 | if (i := find("or", sentence)) > 5 then write(i) |
| 326 | \end{verbatim} |
| 327 | |
| 328 | The \function{find()} function returns the indexes at which the |
| 329 | substring ``or'' is found: 3, 23, 33. In the \keyword{if} statement, |
| 330 | \code{i} is first assigned a value of 3, but 3 is less than 5, so the |
| 331 | comparison fails, and Icon retries it with the second value of 23. 23 |
| 332 | is greater than 5, so the comparison now succeeds, and the code prints |
| 333 | the value 23 to the screen. |
| 334 | |
| 335 | Python doesn't go nearly as far as Icon in adopting generators as a |
| 336 | central concept. Generators are considered a new part of the core |
| 337 | Python language, but learning or using them isn't compulsory; if they |
| 338 | don't solve any problems that you have, feel free to ignore them. |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 339 | One novel feature of Python's interface as compared to |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 340 | Icon's is that a generator's state is represented as a concrete object |
| 341 | that can be passed around to other functions or stored in a data |
| 342 | structure. |
| 343 | |
| 344 | \begin{seealso} |
| 345 | |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 346 | \seepep{255}{Simple Generators}{Written by Neil Schemenauer, Tim |
| 347 | Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer |
| 348 | and Tim Peters, with other fixes from the Python Labs crew.} |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 349 | |
| 350 | \end{seealso} |
| 351 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 352 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 353 | %====================================================================== |
Andrew M. Kuchling | 2f0047a | 2001-09-05 14:53:31 +0000 | [diff] [blame] | 354 | \section{PEP 237: Unifying Long Integers and Integers} |
| 355 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 356 | In recent versions, the distinction between regular integers, which |
| 357 | are 32-bit values on most machines, and long integers, which can be of |
| 358 | arbitrary size, was becoming an annoyance. For example, on platforms |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 359 | that support files larger than \code{2**32} bytes, the |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 360 | \method{tell()} method of file objects has to return a long integer. |
| 361 | However, there were various bits of Python that expected plain |
| 362 | integers and would raise an error if a long integer was provided |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 363 | instead. For example, in Python 1.5, only regular integers |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 364 | could be used as a slice index, and \code{'abc'[1L:]} would raise a |
| 365 | \exception{TypeError} exception with the message 'slice index must be |
| 366 | int'. |
Andrew M. Kuchling | 2f0047a | 2001-09-05 14:53:31 +0000 | [diff] [blame] | 367 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 368 | Python 2.2 will shift values from short to long integers as required. |
| 369 | The 'L' suffix is no longer needed to indicate a long integer literal, |
| 370 | as now the compiler will choose the appropriate type. (Using the 'L' |
| 371 | suffix will be discouraged in future 2.x versions of Python, |
| 372 | triggering a warning in Python 2.4, and probably dropped in Python |
| 373 | 3.0.) Many operations that used to raise an \exception{OverflowError} |
| 374 | will now return a long integer as their result. For example: |
| 375 | |
| 376 | \begin{verbatim} |
| 377 | >>> 1234567890123 |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 378 | 1234567890123L |
| 379 | >>> 2 ** 64 |
| 380 | 18446744073709551616L |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 381 | \end{verbatim} |
| 382 | |
| 383 | In most cases, integers and long integers will now be treated |
| 384 | identically. You can still distinguish them with the |
| 385 | \function{type()} built-in function, but that's rarely needed. The |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 386 | \function{int()} constructor will now return a long integer if the value |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 387 | is large enough. |
| 388 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 389 | \begin{seealso} |
| 390 | |
| 391 | \seepep{237}{Unifying Long Integers and Integers}{Written by |
| 392 | Moshe Zadka and Guido van Rossum. Implemented mostly by Guido van Rossum.} |
| 393 | |
| 394 | \end{seealso} |
Andrew M. Kuchling | 2f0047a | 2001-09-05 14:53:31 +0000 | [diff] [blame] | 395 | |
Andrew M. Kuchling | d4707e3 | 2001-09-28 20:46:46 +0000 | [diff] [blame] | 396 | |
Andrew M. Kuchling | 2f0047a | 2001-09-05 14:53:31 +0000 | [diff] [blame] | 397 | %====================================================================== |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 398 | \section{PEP 238: Changing the Division Operator} |
| 399 | |
| 400 | The most controversial change in Python 2.2 is the start of an effort |
| 401 | to fix an old design flaw that's been in Python from the beginning. |
| 402 | Currently Python's division operator, \code{/}, behaves like C's |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 403 | division operator when presented with two integer arguments: it |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 404 | returns an integer result that's truncated down when there would be |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 405 | a fractional part. For example, \code{3/2} is 1, not 1.5, and |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 406 | \code{(-1)/2} is -1, not -0.5. This means that the results of divison |
| 407 | can vary unexpectedly depending on the type of the two operands and |
| 408 | because Python is dynamically typed, it can be difficult to determine |
| 409 | the possible types of the operands. |
| 410 | |
| 411 | (The controversy is over whether this is \emph{really} a design flaw, |
| 412 | and whether it's worth breaking existing code to fix this. It's |
| 413 | caused endless discussions on python-dev and in July erupted into an |
| 414 | storm of acidly sarcastic postings on \newsgroup{comp.lang.python}. I |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 415 | won't argue for either side here and will stick to describing what's |
| 416 | implemented in 2.2. Read PEP 238 for a summary of arguments and |
| 417 | counter-arguments.) |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 418 | |
| 419 | Because this change might break code, it's being introduced very |
| 420 | gradually. Python 2.2 begins the transition, but the switch won't be |
| 421 | complete until Python 3.0. |
| 422 | |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 423 | First, I'll borrow some terminology from PEP 238. ``True division'' is the |
Andrew M. Kuchling | 9e9c135 | 2001-08-11 03:06:50 +0000 | [diff] [blame] | 424 | division that most non-programmers are familiar with: 3/2 is 1.5, 1/4 |
| 425 | is 0.25, and so forth. ``Floor division'' is what Python's \code{/} |
| 426 | operator currently does when given integer operands; the result is the |
| 427 | floor of the value returned by true division. ``Classic division'' is |
| 428 | the current mixed behaviour of \code{/}; it returns the result of |
| 429 | floor division when the operands are integers, and returns the result |
| 430 | of true division when one of the operands is a floating-point number. |
| 431 | |
| 432 | Here are the changes 2.2 introduces: |
| 433 | |
| 434 | \begin{itemize} |
| 435 | |
| 436 | \item A new operator, \code{//}, is the floor division operator. |
| 437 | (Yes, we know it looks like \Cpp's comment symbol.) \code{//} |
| 438 | \emph{always} returns the floor divison no matter what the types of |
| 439 | its operands are, so \code{1 // 2} is 0 and \code{1.0 // 2.0} is also |
| 440 | 0.0. |
| 441 | |
| 442 | \code{//} is always available in Python 2.2; you don't need to enable |
| 443 | it using a \code{__future__} statement. |
| 444 | |
| 445 | \item By including a \code{from __future__ import true_division} in a |
| 446 | module, the \code{/} operator will be changed to return the result of |
| 447 | true division, so \code{1/2} is 0.5. Without the \code{__future__} |
| 448 | statement, \code{/} still means classic division. The default meaning |
| 449 | of \code{/} will not change until Python 3.0. |
| 450 | |
| 451 | \item Classes can define methods called \method{__truediv__} and |
| 452 | \method{__floordiv__} to overload the two division operators. At the |
| 453 | C level, there are also slots in the \code{PyNumberMethods} structure |
| 454 | so extension types can define the two operators. |
| 455 | |
| 456 | % XXX a warning someday? |
| 457 | |
| 458 | \end{itemize} |
| 459 | |
| 460 | \begin{seealso} |
| 461 | |
| 462 | \seepep{238}{Changing the Division Operator}{Written by Moshe Zadka and |
| 463 | Guido van Rossum. Implemented by Guido van Rossum..} |
| 464 | |
| 465 | \end{seealso} |
| 466 | |
| 467 | |
| 468 | %====================================================================== |
Andrew M. Kuchling | a43e703 | 2001-06-27 20:32:12 +0000 | [diff] [blame] | 469 | \section{Unicode Changes} |
| 470 | |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 471 | Python's Unicode support has been enhanced a bit in 2.2. Unicode |
Andrew M. Kuchling | a6d2a04 | 2001-07-20 18:34:34 +0000 | [diff] [blame] | 472 | strings are usually stored as UCS-2, as 16-bit unsigned integers. |
Andrew M. Kuchling | f5fec3c | 2001-07-19 01:48:08 +0000 | [diff] [blame] | 473 | Python 2.2 can also be compiled to use UCS-4, 32-bit unsigned |
| 474 | integers, as its internal encoding by supplying |
| 475 | \longprogramopt{enable-unicode=ucs4} to the configure script. When |
Andrew M. Kuchling | ab01087 | 2001-07-19 14:59:53 +0000 | [diff] [blame] | 476 | built to use UCS-4 (a ``wide Python''), the interpreter can natively |
Andrew M. Kuchling | a6d2a04 | 2001-07-20 18:34:34 +0000 | [diff] [blame] | 477 | handle Unicode characters from U+000000 to U+110000, so the range of |
| 478 | legal values for the \function{unichr()} function is expanded |
| 479 | accordingly. Using an interpreter compiled to use UCS-2 (a ``narrow |
| 480 | Python''), values greater than 65535 will still cause |
| 481 | \function{unichr()} to raise a \exception{ValueError} exception. |
Andrew M. Kuchling | ab01087 | 2001-07-19 14:59:53 +0000 | [diff] [blame] | 482 | |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 483 | % XXX is this still unimplemented? |
Andrew M. Kuchling | ab01087 | 2001-07-19 14:59:53 +0000 | [diff] [blame] | 484 | All this is the province of the still-unimplemented PEP 261, ``Support |
| 485 | for `wide' Unicode characters''; consult it for further details, and |
Andrew M. Kuchling | a6d2a04 | 2001-07-20 18:34:34 +0000 | [diff] [blame] | 486 | please offer comments on the PEP and on your experiences with the |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 487 | 2.2 beta releases. |
Andrew M. Kuchling | a6d2a04 | 2001-07-20 18:34:34 +0000 | [diff] [blame] | 488 | % XXX update previous line once 2.2 reaches beta. |
Andrew M. Kuchling | ab01087 | 2001-07-19 14:59:53 +0000 | [diff] [blame] | 489 | |
| 490 | Another change is much simpler to explain. Since their introduction, |
| 491 | Unicode strings have supported an \method{encode()} method to convert |
| 492 | the string to a selected encoding such as UTF-8 or Latin-1. A |
| 493 | symmetric \method{decode(\optional{\var{encoding}})} method has been |
| 494 | added to 8-bit strings (though not to Unicode strings) in 2.2. |
| 495 | \method{decode()} assumes that the string is in the specified encoding |
| 496 | and decodes it, returning whatever is returned by the codec. |
| 497 | |
| 498 | Using this new feature, codecs have been added for tasks not directly |
| 499 | related to Unicode. For example, codecs have been added for |
| 500 | uu-encoding, MIME's base64 encoding, and compression with the |
| 501 | \module{zlib} module: |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 502 | |
| 503 | \begin{verbatim} |
| 504 | >>> s = """Here is a lengthy piece of redundant, overly verbose, |
| 505 | ... and repetitive text. |
| 506 | ... """ |
| 507 | >>> data = s.encode('zlib') |
| 508 | >>> data |
| 509 | 'x\x9c\r\xc9\xc1\r\x80 \x10\x04\xc0?Ul...' |
| 510 | >>> data.decode('zlib') |
| 511 | 'Here is a lengthy piece of redundant, overly verbose,\nand repetitive text.\n' |
| 512 | >>> print s.encode('uu') |
| 513 | begin 666 <data> |
| 514 | M2&5R92!I<R!A(&QE;F=T:'D@<&EE8V4@;V8@<F5D=6YD86YT+"!O=F5R;'D@ |
| 515 | >=F5R8F]S92P*86YD(')E<&5T:71I=F4@=&5X="X* |
| 516 | |
| 517 | end |
| 518 | >>> "sheesh".encode('rot-13') |
| 519 | 'furrfu' |
| 520 | \end{verbatim} |
Andrew M. Kuchling | a43e703 | 2001-06-27 20:32:12 +0000 | [diff] [blame] | 521 | |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 522 | To convert a class instance to Unicode, a \method{__unicode__} method |
| 523 | can be defined, analogous to \method{__str__}. |
| 524 | % XXX who implemented that? |
| 525 | |
Andrew M. Kuchling | f5fec3c | 2001-07-19 01:48:08 +0000 | [diff] [blame] | 526 | \method{encode()} and \method{decode()} were implemented by |
| 527 | Marc-Andr\'e Lemburg. The changes to support using UCS-4 internally |
| 528 | were implemented by Fredrik Lundh and Martin von L\"owis. |
Andrew M. Kuchling | a43e703 | 2001-06-27 20:32:12 +0000 | [diff] [blame] | 529 | |
Andrew M. Kuchling | f5fec3c | 2001-07-19 01:48:08 +0000 | [diff] [blame] | 530 | \begin{seealso} |
| 531 | |
| 532 | \seepep{261}{Support for `wide' Unicode characters}{PEP written by |
| 533 | Paul Prescod. Not yet accepted or fully implemented.} |
| 534 | |
| 535 | \end{seealso} |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 536 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 537 | %====================================================================== |
| 538 | \section{PEP 227: Nested Scopes} |
| 539 | |
| 540 | In Python 2.1, statically nested scopes were added as an optional |
| 541 | feature, to be enabled by a \code{from __future__ import |
| 542 | nested_scopes} directive. In 2.2 nested scopes no longer need to be |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 543 | specially enabled, and are now always present. The rest of this section |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 544 | is a copy of the description of nested scopes from my ``What's New in |
| 545 | Python 2.1'' document; if you read it when 2.1 came out, you can skip |
| 546 | the rest of this section. |
| 547 | |
| 548 | The largest change introduced in Python 2.1, and made complete in 2.2, |
| 549 | is to Python's scoping rules. In Python 2.0, at any given time there |
| 550 | are at most three namespaces used to look up variable names: local, |
| 551 | module-level, and the built-in namespace. This often surprised people |
| 552 | because it didn't match their intuitive expectations. For example, a |
| 553 | nested recursive function definition doesn't work: |
| 554 | |
| 555 | \begin{verbatim} |
| 556 | def f(): |
| 557 | ... |
| 558 | def g(value): |
| 559 | ... |
| 560 | return g(value-1) + 1 |
| 561 | ... |
| 562 | \end{verbatim} |
| 563 | |
| 564 | The function \function{g()} will always raise a \exception{NameError} |
| 565 | exception, because the binding of the name \samp{g} isn't in either |
| 566 | its local namespace or in the module-level namespace. This isn't much |
| 567 | of a problem in practice (how often do you recursively define interior |
| 568 | functions like this?), but this also made using the \keyword{lambda} |
| 569 | statement clumsier, and this was a problem in practice. In code which |
| 570 | uses \keyword{lambda} you can often find local variables being copied |
| 571 | by passing them as the default values of arguments. |
| 572 | |
| 573 | \begin{verbatim} |
| 574 | def find(self, name): |
| 575 | "Return list of any entries equal to 'name'" |
| 576 | L = filter(lambda x, name=name: x == name, |
| 577 | self.list_attribute) |
| 578 | return L |
| 579 | \end{verbatim} |
| 580 | |
| 581 | The readability of Python code written in a strongly functional style |
| 582 | suffers greatly as a result. |
| 583 | |
| 584 | The most significant change to Python 2.2 is that static scoping has |
| 585 | been added to the language to fix this problem. As a first effect, |
| 586 | the \code{name=name} default argument is now unnecessary in the above |
| 587 | example. Put simply, when a given variable name is not assigned a |
| 588 | value within a function (by an assignment, or the \keyword{def}, |
| 589 | \keyword{class}, or \keyword{import} statements), references to the |
| 590 | variable will be looked up in the local namespace of the enclosing |
| 591 | scope. A more detailed explanation of the rules, and a dissection of |
| 592 | the implementation, can be found in the PEP. |
| 593 | |
| 594 | This change may cause some compatibility problems for code where the |
| 595 | same variable name is used both at the module level and as a local |
| 596 | variable within a function that contains further function definitions. |
| 597 | This seems rather unlikely though, since such code would have been |
| 598 | pretty confusing to read in the first place. |
| 599 | |
| 600 | One side effect of the change is that the \code{from \var{module} |
| 601 | import *} and \keyword{exec} statements have been made illegal inside |
| 602 | a function scope under certain conditions. The Python reference |
| 603 | manual has said all along that \code{from \var{module} import *} is |
| 604 | only legal at the top level of a module, but the CPython interpreter |
| 605 | has never enforced this before. As part of the implementation of |
| 606 | nested scopes, the compiler which turns Python source into bytecodes |
| 607 | has to generate different code to access variables in a containing |
| 608 | scope. \code{from \var{module} import *} and \keyword{exec} make it |
| 609 | impossible for the compiler to figure this out, because they add names |
| 610 | to the local namespace that are unknowable at compile time. |
| 611 | Therefore, if a function contains function definitions or |
| 612 | \keyword{lambda} expressions with free variables, the compiler will |
| 613 | flag this by raising a \exception{SyntaxError} exception. |
| 614 | |
| 615 | To make the preceding explanation a bit clearer, here's an example: |
| 616 | |
| 617 | \begin{verbatim} |
| 618 | x = 1 |
| 619 | def f(): |
| 620 | # The next line is a syntax error |
| 621 | exec 'x=2' |
| 622 | def g(): |
| 623 | return x |
| 624 | \end{verbatim} |
| 625 | |
| 626 | Line 4 containing the \keyword{exec} statement is a syntax error, |
| 627 | since \keyword{exec} would define a new local variable named \samp{x} |
| 628 | whose value should be accessed by \function{g()}. |
| 629 | |
| 630 | This shouldn't be much of a limitation, since \keyword{exec} is rarely |
| 631 | used in most Python code (and when it is used, it's often a sign of a |
| 632 | poor design anyway). |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 633 | |
| 634 | \begin{seealso} |
| 635 | |
| 636 | \seepep{227}{Statically Nested Scopes}{Written and implemented by |
| 637 | Jeremy Hylton.} |
| 638 | |
| 639 | \end{seealso} |
| 640 | |
Andrew M. Kuchling | a43e703 | 2001-06-27 20:32:12 +0000 | [diff] [blame] | 641 | |
| 642 | %====================================================================== |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 643 | \section{New and Improved Modules} |
| 644 | |
| 645 | \begin{itemize} |
| 646 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 647 | \item The \module{xmlrpclib} module was contributed to the standard |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 648 | library by Fredrik Lundh, provding support for writing XML-RPC |
| 649 | clients. XML-RPC is a simple remote procedure call protocol built on |
Andrew M. Kuchling | 8c69c91 | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 650 | top of HTTP and XML. For example, the following snippet retrieves a |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 651 | list of RSS channels from the O'Reilly Network, and then |
| 652 | lists the recent headlines for one channel: |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 653 | |
| 654 | \begin{verbatim} |
| 655 | import xmlrpclib |
| 656 | s = xmlrpclib.Server( |
| 657 | 'http://www.oreillynet.com/meerkat/xml-rpc/server.php') |
| 658 | channels = s.meerkat.getChannels() |
| 659 | # channels is a list of dictionaries, like this: |
| 660 | # [{'id': 4, 'title': 'Freshmeat Daily News'} |
| 661 | # {'id': 190, 'title': '32Bits Online'}, |
| 662 | # {'id': 4549, 'title': '3DGamers'}, ... ] |
| 663 | |
| 664 | # Get the items for one channel |
| 665 | items = s.meerkat.getItems( {'channel': 4} ) |
| 666 | |
| 667 | # 'items' is another list of dictionaries, like this: |
| 668 | # [{'link': 'http://freshmeat.net/releases/52719/', |
| 669 | # 'description': 'A utility which converts HTML to XSL FO.', |
| 670 | # 'title': 'html2fo 0.3 (Default)'}, ... ] |
| 671 | \end{verbatim} |
| 672 | |
Andrew M. Kuchling | d4707e3 | 2001-09-28 20:46:46 +0000 | [diff] [blame] | 673 | The \module{SimpleXMLRPCServer} module makes it easy to create |
| 674 | straightforward XML-RPC servers. See \url{http://www.xmlrpc.com/} for |
| 675 | more information about XML-RPC. |
| 676 | |
| 677 | \item The new \module{hmac} module implements implements the HMAC |
| 678 | algorithm described by \rfc{2104}. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 679 | |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 680 | \item The Python profiler has been extensively reworked and various |
| 681 | errors in its output have been corrected. (Contributed by Fred |
| 682 | Fred~L. Drake, Jr. and Tim Peters.) |
| 683 | |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 684 | \item The \module{socket} module can be compiled to support IPv6; |
Andrew M. Kuchling | ddeb135 | 2001-07-16 14:35:52 +0000 | [diff] [blame] | 685 | specify the \longprogramopt{enable-ipv6} option to Python's configure |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 686 | script. (Contributed by Jun-ichiro ``itojun'' Hagino.) |
| 687 | |
| 688 | \item Two new format characters were added to the \module{struct} |
| 689 | module for 64-bit integers on platforms that support the C |
| 690 | \ctype{long long} type. \samp{q} is for a signed 64-bit integer, |
| 691 | and \samp{Q} is for an unsigned one. The value is returned in |
| 692 | Python's long integer type. (Contributed by Tim Peters.) |
| 693 | |
| 694 | \item In the interpreter's interactive mode, there's a new built-in |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 695 | function \function{help()} that uses the \module{pydoc} module |
| 696 | introduced in Python 2.1 to provide interactive help. |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 697 | \code{help(\var{object})} displays any available help text about |
| 698 | \var{object}. \code{help()} with no argument puts you in an online |
| 699 | help utility, where you can enter the names of functions, classes, |
| 700 | or modules to read their help text. |
| 701 | (Contributed by Guido van Rossum, using Ka-Ping Yee's \module{pydoc} module.) |
| 702 | |
| 703 | \item Various bugfixes and performance improvements have been made |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 704 | to the SRE engine underlying the \module{re} module. For example, |
| 705 | \function{re.sub()} will now use \function{string.replace()} |
| 706 | automatically when the pattern and its replacement are both just |
| 707 | literal strings without regex metacharacters. Another contributed |
| 708 | patch speeds up certain Unicode character ranges by a factor of |
| 709 | two. (SRE is maintained by Fredrik Lundh. The BIGCHARSET patch was |
| 710 | contributed by Martin von L\"owis.) |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 711 | |
Andrew M. Kuchling | 1efd7ad | 2001-09-14 16:19:27 +0000 | [diff] [blame] | 712 | \item The \module{smtplib} module now supports \rfc{2487}, ``Secure |
| 713 | SMTP over TLS'', so it's now possible to encrypt the SMTP traffic |
| 714 | between a Python program and the mail transport agent being handed a |
| 715 | message. (Contributed by Gerhard H\"aring.) |
| 716 | |
Andrew M. Kuchling | a6d2a04 | 2001-07-20 18:34:34 +0000 | [diff] [blame] | 717 | \item The \module{imaplib} module, maintained by Piers Lauder, has |
| 718 | support for several new extensions: the NAMESPACE extension defined |
| 719 | in \rfc{2342}, SORT, GETACL and SETACL. (Contributed by Anthony |
| 720 | Baxter and Michel Pelletier.) |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 721 | |
Andrew M. Kuchling | d4707e3 | 2001-09-28 20:46:46 +0000 | [diff] [blame] | 722 | \item The \module{rfc822} module's parsing of email addresses is now |
| 723 | compliant with \rfc{2822}, an update to \rfc{822}. (The module's |
| 724 | name is \emph{not} going to be changed to \samp{rfc2822}.) A new |
| 725 | package, \module{email}, has also been added for parsing and |
| 726 | generating e-mail messages. (Contributed by Barry Warsaw, and |
| 727 | arising out of his work on Mailman.) |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 728 | |
| 729 | \item New constants \constant{ascii_letters}, |
| 730 | \constant{ascii_lowercase}, and \constant{ascii_uppercase} were |
| 731 | added to the \module{string} module. There were several modules in |
| 732 | the standard library that used \constant{string.letters} to mean the |
| 733 | ranges A-Za-z, but that assumption is incorrect when locales are in |
| 734 | use, because \constant{string.letters} varies depending on the set |
| 735 | of legal characters defined by the current locale. The buggy |
| 736 | modules have all been fixed to use \constant{ascii_letters} instead. |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 737 | (Reported by an unknown person; fixed by Fred~L. Drake, Jr.) |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 738 | |
Andrew M. Kuchling | 8c69c91 | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 739 | \item The \module{mimetypes} module now makes it easier to use |
| 740 | alternative MIME-type databases by the addition of a |
| 741 | \class{MimeTypes} class, which takes a list of filenames to be |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 742 | parsed. (Contributed by Fred~L. Drake, Jr.) |
Andrew M. Kuchling | 8c69c91 | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 743 | |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 744 | \item A \class{Timer} class was added to the \module{threading} |
| 745 | module that allows scheduling an activity to happen at some future |
| 746 | time. (Contributed by Itamar Shtull-Trauring.) |
Andrew M. Kuchling | 2f0047a | 2001-09-05 14:53:31 +0000 | [diff] [blame] | 747 | |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 748 | \end{itemize} |
| 749 | |
| 750 | |
| 751 | %====================================================================== |
| 752 | \section{Interpreter Changes and Fixes} |
| 753 | |
| 754 | Some of the changes only affect people who deal with the Python |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 755 | interpreter at the C level because they're writing Python extension modules, |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 756 | embedding the interpreter, or just hacking on the interpreter itself. |
| 757 | If you only write Python code, none of the changes described here will |
| 758 | affect you very much. |
| 759 | |
| 760 | \begin{itemize} |
| 761 | |
| 762 | \item Profiling and tracing functions can now be implemented in C, |
| 763 | which can operate at much higher speeds than Python-based functions |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 764 | and should reduce the overhead of profiling and tracing. This |
| 765 | will be of interest to authors of development environments for |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 766 | Python. Two new C functions were added to Python's API, |
| 767 | \cfunction{PyEval_SetProfile()} and \cfunction{PyEval_SetTrace()}. |
| 768 | The existing \function{sys.setprofile()} and |
| 769 | \function{sys.settrace()} functions still exist, and have simply |
| 770 | been changed to use the new C-level interface. (Contributed by Fred |
| 771 | L. Drake, Jr.) |
| 772 | |
| 773 | \item Another low-level API, primarily of interest to implementors |
| 774 | of Python debuggers and development tools, was added. |
| 775 | \cfunction{PyInterpreterState_Head()} and |
| 776 | \cfunction{PyInterpreterState_Next()} let a caller walk through all |
| 777 | the existing interpreter objects; |
| 778 | \cfunction{PyInterpreterState_ThreadHead()} and |
| 779 | \cfunction{PyThreadState_Next()} allow looping over all the thread |
| 780 | states for a given interpreter. (Contributed by David Beazley.) |
| 781 | |
| 782 | \item A new \samp{et} format sequence was added to |
| 783 | \cfunction{PyArg_ParseTuple}; \samp{et} takes both a parameter and |
| 784 | an encoding name, and converts the parameter to the given encoding |
| 785 | if the parameter turns out to be a Unicode string, or leaves it |
| 786 | alone if it's an 8-bit string, assuming it to already be in the |
| 787 | desired encoding. This differs from the \samp{es} format character, |
| 788 | which assumes that 8-bit strings are in Python's default ASCII |
| 789 | encoding and converts them to the specified new encoding. |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 790 | (Contributed by M.-A. Lemburg, and used for the MBCS support on |
| 791 | Windows described in the following section.) |
Andrew M. Kuchling | 0ab31b8 | 2001-08-29 01:16:54 +0000 | [diff] [blame] | 792 | |
| 793 | \item Two new flags \constant{METH_NOARGS} and \constant{METH_O} are |
| 794 | available in method definition tables to simplify implementation of |
| 795 | methods with no arguments or a single untyped argument. Calling |
| 796 | such methods is more efficient than calling a corresponding method |
| 797 | that uses \constant{METH_VARARGS}. |
| 798 | Also, the old \constant{METH_OLDARGS} style of writing C methods is |
| 799 | now officially deprecated. |
| 800 | |
| 801 | \item |
| 802 | Two new wrapper functions, \cfunction{PyOS_snprintf()} and |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 803 | \cfunction{PyOS_vsnprintf()} were added to provide |
Andrew M. Kuchling | 0ab31b8 | 2001-08-29 01:16:54 +0000 | [diff] [blame] | 804 | cross-platform implementations for the relatively new |
| 805 | \cfunction{snprintf()} and \cfunction{vsnprintf()} C lib APIs. In |
| 806 | contrast to the standard \cfunction{sprintf()} and |
| 807 | \cfunction{vsprintf()} functions, the Python versions check the |
| 808 | bounds of the buffer used to protect against buffer overruns. |
| 809 | (Contributed by M.-A. Lemburg.) |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 810 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 811 | \end{itemize} |
| 812 | |
| 813 | |
| 814 | %====================================================================== |
| 815 | \section{Other Changes and Fixes} |
| 816 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 817 | % XXX update the patch and bug figures as we go |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 818 | As usual there were a bunch of other improvements and bugfixes |
| 819 | scattered throughout the source tree. A search through the CVS change |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 820 | logs finds there were 119 patches applied, and 179 bugs fixed; both |
Andrew M. Kuchling | 4dbf871 | 2001-07-16 02:17:14 +0000 | [diff] [blame] | 821 | figures are likely to be underestimates. Some of the more notable |
| 822 | changes are: |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 823 | |
| 824 | \begin{itemize} |
| 825 | |
Andrew M. Kuchling | 0e03f58 | 2001-08-30 21:30:16 +0000 | [diff] [blame] | 826 | \item The code for the MacOS port for Python, maintained by Jack |
| 827 | Jansen, is now kept in the main Python CVS tree, and many changes |
| 828 | have been made to support MacOS X. |
| 829 | |
| 830 | The most significant change is the ability to build Python as a |
| 831 | framework, enabled by supplying the \longprogramopt{enable-framework} |
| 832 | option to the configure script when compiling Python. According to |
| 833 | Jack Jansen, ``This installs a self-contained Python installation plus |
| 834 | the OSX framework "glue" into |
| 835 | \file{/Library/Frameworks/Python.framework} (or another location of |
| 836 | choice). For now there is little immediate added benefit to this |
| 837 | (actually, there is the disadvantage that you have to change your PATH |
| 838 | to be able to find Python), but it is the basis for creating a |
| 839 | full-blown Python application, porting the MacPython IDE, possibly |
| 840 | using Python as a standard OSA scripting language and much more.'' |
| 841 | |
| 842 | Most of the MacPython toolbox modules, which interface to MacOS APIs |
| 843 | such as windowing, QuickTime, scripting, etc. have been ported to OS |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 844 | X, but they've been left commented out in \filename{setup.py}. People who want |
Andrew M. Kuchling | 0e03f58 | 2001-08-30 21:30:16 +0000 | [diff] [blame] | 845 | to experiment with these modules can uncomment them manually. |
| 846 | |
| 847 | % Jack's original comments: |
| 848 | %The main change is the possibility to build Python as a |
| 849 | %framework. This installs a self-contained Python installation plus the |
| 850 | %OSX framework "glue" into /Library/Frameworks/Python.framework (or |
| 851 | %another location of choice). For now there is little immedeate added |
| 852 | %benefit to this (actually, there is the disadvantage that you have to |
| 853 | %change your PATH to be able to find Python), but it is the basis for |
| 854 | %creating a fullblown Python application, porting the MacPython IDE, |
| 855 | %possibly using Python as a standard OSA scripting language and much |
| 856 | %more. You enable this with "configure --enable-framework". |
| 857 | |
| 858 | %The other change is that most MacPython toolbox modules, which |
| 859 | %interface to all the MacOS APIs such as windowing, quicktime, |
| 860 | %scripting, etc. have been ported. Again, most of these are not of |
| 861 | %immedeate use, as they need a full application to be really useful, so |
| 862 | %they have been commented out in setup.py. People wanting to experiment |
| 863 | %can uncomment them. Gestalt and Internet Config modules are enabled by |
| 864 | %default. |
Andrew M. Kuchling | 0e03f58 | 2001-08-30 21:30:16 +0000 | [diff] [blame] | 865 | |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 866 | \item Keyword arguments passed to builtin functions that don't take them |
| 867 | now cause a \exception{TypeError} exception to be raised, with the |
| 868 | message "\var{function} takes no keyword arguments". |
| 869 | |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 870 | \item Weak references, added in Python 2.1 as an extension module, |
| 871 | are now part of the core because they're used in the implementation |
| 872 | of new-style classes. The \exception{ReferenceError} exception has |
| 873 | therefore moved from the \module{weakref} module to become a |
| 874 | built-in exception. |
| 875 | |
Andrew M. Kuchling | 94a7eba | 2001-08-15 15:55:48 +0000 | [diff] [blame] | 876 | \item A new script, \file{Tools/scripts/cleanfuture.py} by Tim |
| 877 | Peters, automatically removes obsolete \code{__future__} statements |
| 878 | from Python source code. |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 879 | |
| 880 | \item The new license introduced with Python 1.6 wasn't |
| 881 | GPL-compatible. This is fixed by some minor textual changes to the |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 882 | 2.2 license, so it's now legal to embed Python inside a GPLed |
| 883 | program again. Note that Python itself is not GPLed, but instead is |
| 884 | under a license that's essentially equivalent to the BSD license, |
| 885 | same as it always was. The license changes were also applied to the |
| 886 | Python 2.0.1 and 2.1.1 releases. |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 887 | |
Andrew M. Kuchling | f4ccf58 | 2001-07-31 01:11:36 +0000 | [diff] [blame] | 888 | \item When presented with a Unicode filename on Windows, Python will |
| 889 | now convert it to an MBCS encoded string, as used by the Microsoft |
| 890 | file APIs. As MBCS is explicitly used by the file APIs, Python's |
| 891 | choice of ASCII as the default encoding turns out to be an |
| 892 | annoyance. |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 893 | (Contributed by Mark Hammond with assistance from Marc-Andr\'e |
| 894 | Lemburg.) |
| 895 | |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 896 | \item Large file support is now enabled on Windows. (Contributed by |
| 897 | Tim Peters.) |
| 898 | |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 899 | \item The \file{Tools/scripts/ftpmirror.py} script |
| 900 | now parses a \file{.netrc} file, if you have one. |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 901 | (Contributed by Mike Romberg.) |
Andrew M. Kuchling | 2cd712b | 2001-07-16 13:39:08 +0000 | [diff] [blame] | 902 | |
Andrew M. Kuchling | 4cf52a9 | 2001-07-17 12:48:48 +0000 | [diff] [blame] | 903 | \item Some features of the object returned by the |
| 904 | \function{xrange()} function are now deprecated, and trigger |
| 905 | warnings when they're accessed; they'll disappear in Python 2.3. |
| 906 | \class{xrange} objects tried to pretend they were full sequence |
| 907 | types by supporting slicing, sequence multiplication, and the |
| 908 | \keyword{in} operator, but these features were rarely used and |
| 909 | therefore buggy. The \method{tolist()} method and the |
| 910 | \member{start}, \member{stop}, and \member{step} attributes are also |
| 911 | being deprecated. At the C level, the fourth argument to the |
| 912 | \cfunction{PyRange_New()} function, \samp{repeat}, has also been |
| 913 | deprecated. |
| 914 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 915 | \item There were a bunch of patches to the dictionary |
| 916 | implementation, mostly to fix potential core dumps if a dictionary |
| 917 | contains objects that sneakily changed their hash value, or mutated |
| 918 | the dictionary they were contained in. For a while python-dev fell |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 919 | into a gentle rhythm of Michael Hudson finding a case that dumped |
| 920 | core, Tim Peters fixing the bug, Michael finding another case, and round |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 921 | and round it went. |
| 922 | |
Andrew M. Kuchling | 33a3b63 | 2001-09-04 21:25:58 +0000 | [diff] [blame] | 923 | \item On Windows, Python can now be compiled with Borland C thanks |
| 924 | to a number of patches contributed by Stephen Hansen, though the |
| 925 | result isn't fully functional yet. (But this \emph{is} progress...) |
Andrew M. Kuchling | 8c69c91 | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 926 | |
Andrew M. Kuchling | f4ccf58 | 2001-07-31 01:11:36 +0000 | [diff] [blame] | 927 | \item Another Windows enhancement: Wise Solutions generously offered |
| 928 | PythonLabs use of their InstallerMaster 8.1 system. Earlier |
| 929 | PythonLabs Windows installers used Wise 5.0a, which was beginning to |
| 930 | show its age. (Packaged up by Tim Peters.) |
| 931 | |
Andrew M. Kuchling | 8c69c91 | 2001-08-07 14:28:58 +0000 | [diff] [blame] | 932 | \item Files ending in \samp{.pyw} can now be imported on Windows. |
| 933 | \samp{.pyw} is a Windows-only thing, used to indicate that a script |
| 934 | needs to be run using PYTHONW.EXE instead of PYTHON.EXE in order to |
| 935 | prevent a DOS console from popping up to display the output. This |
| 936 | patch makes it possible to import such scripts, in case they're also |
| 937 | usable as modules. (Implemented by David Bolen.) |
| 938 | |
Andrew M. Kuchling | 8cfa905 | 2001-07-19 01:19:59 +0000 | [diff] [blame] | 939 | \item On platforms where Python uses the C \cfunction{dlopen()} function |
| 940 | to load extension modules, it's now possible to set the flags used |
| 941 | by \cfunction{dlopen()} using the \function{sys.getdlopenflags()} and |
| 942 | \function{sys.setdlopenflags()} functions. (Contributed by Bram Stolk.) |
Andrew M. Kuchling | 2f0047a | 2001-09-05 14:53:31 +0000 | [diff] [blame] | 943 | |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 944 | \item The \function{pow()} built-in function no longer supports 3 |
| 945 | arguments when floating-point numbers are supplied. |
Andrew M. Kuchling | 1497b62 | 2001-09-24 14:51:16 +0000 | [diff] [blame] | 946 | \code{pow(\var{x}, \var{y}, \var{z})} returns \code{(x**y) \% z}, but |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 947 | this is never useful for floating point numbers, and the final |
| 948 | result varies unpredictably depending on the platform. A call such |
Andrew M. Kuchling | d6e40e2 | 2001-09-10 16:18:50 +0000 | [diff] [blame] | 949 | as \code{pow(2.0, 8.0, 7.0)} will now raise a \exception{TypeError} |
Andrew M. Kuchling | 26c39bf | 2001-09-10 03:20:53 +0000 | [diff] [blame] | 950 | exception. |
Andrew M. Kuchling | 7770767 | 2001-07-31 15:51:16 +0000 | [diff] [blame] | 951 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 952 | \end{itemize} |
| 953 | |
| 954 | |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 955 | %====================================================================== |
| 956 | \section{Acknowledgements} |
| 957 | |
| 958 | The author would like to thank the following people for offering |
Andrew M. Kuchling | 6ea9f0b | 2001-07-17 14:50:31 +0000 | [diff] [blame] | 959 | suggestions and corrections to various drafts of this article: Fred |
Andrew M. Kuchling | 8b42f01 | 2001-10-22 02:00:11 +0000 | [diff] [blame^] | 960 | Bremmer, Keith Briggs, Andrew Dalke, Fred~L. Drake, Jr., Carel Fellinger, Mark |
Andrew M. Kuchling | 33a3b63 | 2001-09-04 21:25:58 +0000 | [diff] [blame] | 961 | Hammond, Stephen Hansen, Jack Jansen, Marc-Andr\'e Lemburg, Tim Peters, Neil |
Andrew M. Kuchling | 0e03f58 | 2001-08-30 21:30:16 +0000 | [diff] [blame] | 962 | Schemenauer, Guido van Rossum. |
Andrew M. Kuchling | a8defaa | 2001-05-05 16:37:29 +0000 | [diff] [blame] | 963 | |
| 964 | \end{document} |