Fred Drake | 03e1031 | 2002-03-26 19:17:43 +0000 | [diff] [blame] | 1 | \documentclass{howto} |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 2 | \usepackage{distutils} |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 3 | % $Id$ |
| 4 | |
| 5 | \title{What's New in Python 2.3} |
Andrew M. Kuchling | d97b01c | 2003-01-08 02:09:40 +0000 | [diff] [blame] | 6 | \release{0.08} |
Fred Drake | aac8c58 | 2003-01-17 22:50:10 +0000 | [diff] [blame] | 7 | \author{A.M.\ Kuchling} |
Andrew M. Kuchling | bc5e3cc | 2002-11-05 00:26:33 +0000 | [diff] [blame] | 8 | \authoraddress{\email{amk@amk.ca}} |
Fred Drake | 03e1031 | 2002-03-26 19:17:43 +0000 | [diff] [blame] | 9 | |
| 10 | \begin{document} |
| 11 | \maketitle |
| 12 | \tableofcontents |
| 13 | |
Andrew M. Kuchling | c61ec52 | 2002-08-04 01:20:05 +0000 | [diff] [blame] | 14 | % MacOS framework-related changes (section of its own, probably) |
Andrew M. Kuchling | f70a0a8 | 2002-06-10 13:22:46 +0000 | [diff] [blame] | 15 | |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 16 | %\section{Introduction \label{intro}} |
| 17 | |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 18 | {\large This article is a draft, and is currently up to date for |
| 19 | Python 2.3alpha1. Please send any additions, comments or errata to |
| 20 | the author.} |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 21 | |
| 22 | This article explains the new features in Python 2.3. The tentative |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 23 | release date of Python 2.3 is currently scheduled for mid-2003. |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 24 | |
| 25 | This article doesn't attempt to provide a complete specification of |
| 26 | the new features, but instead provides a convenient overview. For |
| 27 | full details, you should refer to the documentation for Python 2.3, |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 28 | such as the \citetitle[../lib/lib.html]{Python Library Reference} and |
| 29 | the \citetitle[../ref/ref.html]{Python Reference Manual}. If you want |
| 30 | to understand the complete implementation and design rationale for a |
| 31 | change, refer to the PEP for a particular new feature. |
Fred Drake | 03e1031 | 2002-03-26 19:17:43 +0000 | [diff] [blame] | 32 | |
| 33 | |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 34 | %====================================================================== |
Andrew M. Kuchling | bc46510 | 2002-08-20 01:34:06 +0000 | [diff] [blame] | 35 | \section{PEP 218: A Standard Set Datatype} |
| 36 | |
| 37 | The new \module{sets} module contains an implementation of a set |
| 38 | datatype. The \class{Set} class is for mutable sets, sets that can |
| 39 | have members added and removed. The \class{ImmutableSet} class is for |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 40 | sets that can't be modified, and instances of \class{ImmutableSet} can |
| 41 | therefore be used as dictionary keys. Sets are built on top of |
| 42 | dictionaries, so the elements within a set must be hashable. |
Andrew M. Kuchling | bc46510 | 2002-08-20 01:34:06 +0000 | [diff] [blame] | 43 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 44 | Here's a simple example: |
Andrew M. Kuchling | bc46510 | 2002-08-20 01:34:06 +0000 | [diff] [blame] | 45 | |
| 46 | \begin{verbatim} |
| 47 | >>> import sets |
| 48 | >>> S = sets.Set([1,2,3]) |
| 49 | >>> S |
| 50 | Set([1, 2, 3]) |
| 51 | >>> 1 in S |
| 52 | True |
| 53 | >>> 0 in S |
| 54 | False |
| 55 | >>> S.add(5) |
| 56 | >>> S.remove(3) |
| 57 | >>> S |
| 58 | Set([1, 2, 5]) |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 59 | >>> |
Andrew M. Kuchling | bc46510 | 2002-08-20 01:34:06 +0000 | [diff] [blame] | 60 | \end{verbatim} |
| 61 | |
| 62 | The union and intersection of sets can be computed with the |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 63 | \method{union()} and \method{intersection()} methods or |
| 64 | alternatively using the bitwise operators \code{\&} and \code{|}. |
Andrew M. Kuchling | bc46510 | 2002-08-20 01:34:06 +0000 | [diff] [blame] | 65 | Mutable sets also have in-place versions of these methods, |
| 66 | \method{union_update()} and \method{intersection_update()}. |
| 67 | |
| 68 | \begin{verbatim} |
| 69 | >>> S1 = sets.Set([1,2,3]) |
| 70 | >>> S2 = sets.Set([4,5,6]) |
| 71 | >>> S1.union(S2) |
| 72 | Set([1, 2, 3, 4, 5, 6]) |
| 73 | >>> S1 | S2 # Alternative notation |
| 74 | Set([1, 2, 3, 4, 5, 6]) |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 75 | >>> S1.intersection(S2) |
Andrew M. Kuchling | bc46510 | 2002-08-20 01:34:06 +0000 | [diff] [blame] | 76 | Set([]) |
| 77 | >>> S1 & S2 # Alternative notation |
| 78 | Set([]) |
| 79 | >>> S1.union_update(S2) |
Andrew M. Kuchling | bc46510 | 2002-08-20 01:34:06 +0000 | [diff] [blame] | 80 | >>> S1 |
| 81 | Set([1, 2, 3, 4, 5, 6]) |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 82 | >>> |
Andrew M. Kuchling | bc46510 | 2002-08-20 01:34:06 +0000 | [diff] [blame] | 83 | \end{verbatim} |
| 84 | |
| 85 | It's also possible to take the symmetric difference of two sets. This |
| 86 | is the set of all elements in the union that aren't in the |
| 87 | intersection. An alternative way of expressing the symmetric |
| 88 | difference is that it contains all elements that are in exactly one |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 89 | set. Again, there's an alternative notation (\code{\^}), and an |
| 90 | in-place version with the ungainly name |
Andrew M. Kuchling | bc46510 | 2002-08-20 01:34:06 +0000 | [diff] [blame] | 91 | \method{symmetric_difference_update()}. |
| 92 | |
| 93 | \begin{verbatim} |
| 94 | >>> S1 = sets.Set([1,2,3,4]) |
| 95 | >>> S2 = sets.Set([3,4,5,6]) |
| 96 | >>> S1.symmetric_difference(S2) |
| 97 | Set([1, 2, 5, 6]) |
| 98 | >>> S1 ^ S2 |
| 99 | Set([1, 2, 5, 6]) |
| 100 | >>> |
| 101 | \end{verbatim} |
| 102 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 103 | There are also \method{issubset()} and \method{issuperset()} methods |
Michael W. Hudson | 065f5fa | 2003-02-10 19:24:50 +0000 | [diff] [blame] | 104 | for checking whether one set is a subset or superset of another: |
Andrew M. Kuchling | bc46510 | 2002-08-20 01:34:06 +0000 | [diff] [blame] | 105 | |
| 106 | \begin{verbatim} |
| 107 | >>> S1 = sets.Set([1,2,3]) |
| 108 | >>> S2 = sets.Set([2,3]) |
| 109 | >>> S2.issubset(S1) |
| 110 | True |
| 111 | >>> S1.issubset(S2) |
| 112 | False |
| 113 | >>> S1.issuperset(S2) |
| 114 | True |
| 115 | >>> |
| 116 | \end{verbatim} |
| 117 | |
| 118 | |
| 119 | \begin{seealso} |
| 120 | |
| 121 | \seepep{218}{Adding a Built-In Set Object Type}{PEP written by Greg V. Wilson. |
| 122 | Implemented by Greg V. Wilson, Alex Martelli, and GvR.} |
| 123 | |
| 124 | \end{seealso} |
| 125 | |
| 126 | |
| 127 | |
| 128 | %====================================================================== |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 129 | \section{PEP 255: Simple Generators\label{section-generators}} |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 130 | |
| 131 | In Python 2.2, generators were added as an optional feature, to be |
| 132 | enabled by a \code{from __future__ import generators} directive. In |
| 133 | 2.3 generators no longer need to be specially enabled, and are now |
| 134 | always present; this means that \keyword{yield} is now always a |
| 135 | keyword. The rest of this section is a copy of the description of |
| 136 | generators from the ``What's New in Python 2.2'' document; if you read |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 137 | it back when Python 2.2 came out, you can skip the rest of this section. |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 138 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 139 | You're doubtless familiar with how function calls work in Python or C. |
| 140 | When you call a function, it gets a private namespace where its local |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 141 | variables are created. When the function reaches a \keyword{return} |
| 142 | statement, the local variables are destroyed and the resulting value |
| 143 | is returned to the caller. A later call to the same function will get |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 144 | a fresh new set of local variables. But, what if the local variables |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 145 | weren't thrown away on exiting a function? What if you could later |
| 146 | resume the function where it left off? This is what generators |
| 147 | provide; they can be thought of as resumable functions. |
| 148 | |
| 149 | Here's the simplest example of a generator function: |
| 150 | |
| 151 | \begin{verbatim} |
| 152 | def generate_ints(N): |
| 153 | for i in range(N): |
| 154 | yield i |
| 155 | \end{verbatim} |
| 156 | |
| 157 | A new keyword, \keyword{yield}, was introduced for generators. Any |
| 158 | function containing a \keyword{yield} statement is a generator |
| 159 | function; this is detected by Python's bytecode compiler which |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 160 | compiles the function specially as a result. |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 161 | |
| 162 | When you call a generator function, it doesn't return a single value; |
| 163 | instead it returns a generator object that supports the iterator |
| 164 | protocol. On executing the \keyword{yield} statement, the generator |
| 165 | outputs the value of \code{i}, similar to a \keyword{return} |
| 166 | statement. The big difference between \keyword{yield} and a |
| 167 | \keyword{return} statement is that on reaching a \keyword{yield} the |
| 168 | generator's state of execution is suspended and local variables are |
| 169 | preserved. On the next call to the generator's \code{.next()} method, |
| 170 | the function will resume executing immediately after the |
| 171 | \keyword{yield} statement. (For complicated reasons, the |
| 172 | \keyword{yield} statement isn't allowed inside the \keyword{try} block |
Fred Drake | aac8c58 | 2003-01-17 22:50:10 +0000 | [diff] [blame] | 173 | of a \keyword{try}...\keyword{finally} statement; read \pep{255} for a full |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 174 | explanation of the interaction between \keyword{yield} and |
| 175 | exceptions.) |
| 176 | |
Fred Drake | aac8c58 | 2003-01-17 22:50:10 +0000 | [diff] [blame] | 177 | Here's a sample usage of the \function{generate_ints()} generator: |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 178 | |
| 179 | \begin{verbatim} |
| 180 | >>> gen = generate_ints(3) |
| 181 | >>> gen |
| 182 | <generator object at 0x8117f90> |
| 183 | >>> gen.next() |
| 184 | 0 |
| 185 | >>> gen.next() |
| 186 | 1 |
| 187 | >>> gen.next() |
| 188 | 2 |
| 189 | >>> gen.next() |
| 190 | Traceback (most recent call last): |
Andrew M. Kuchling | 9f6e104 | 2002-06-17 13:40:04 +0000 | [diff] [blame] | 191 | File "stdin", line 1, in ? |
| 192 | File "stdin", line 2, in generate_ints |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 193 | StopIteration |
| 194 | \end{verbatim} |
| 195 | |
| 196 | You could equally write \code{for i in generate_ints(5)}, or |
| 197 | \code{a,b,c = generate_ints(3)}. |
| 198 | |
| 199 | Inside a generator function, the \keyword{return} statement can only |
| 200 | be used without a value, and signals the end of the procession of |
| 201 | values; afterwards the generator cannot return any further values. |
| 202 | \keyword{return} with a value, such as \code{return 5}, is a syntax |
| 203 | error inside a generator function. The end of the generator's results |
| 204 | can also be indicated by raising \exception{StopIteration} manually, |
| 205 | or by just letting the flow of execution fall off the bottom of the |
| 206 | function. |
| 207 | |
| 208 | You could achieve the effect of generators manually by writing your |
| 209 | own class and storing all the local variables of the generator as |
| 210 | instance variables. For example, returning a list of integers could |
| 211 | be done by setting \code{self.count} to 0, and having the |
| 212 | \method{next()} method increment \code{self.count} and return it. |
| 213 | However, for a moderately complicated generator, writing a |
| 214 | corresponding class would be much messier. |
| 215 | \file{Lib/test/test_generators.py} contains a number of more |
| 216 | interesting examples. The simplest one implements an in-order |
| 217 | traversal of a tree using generators recursively. |
| 218 | |
| 219 | \begin{verbatim} |
| 220 | # A recursive generator that generates Tree leaves in in-order. |
| 221 | def inorder(t): |
| 222 | if t: |
| 223 | for x in inorder(t.left): |
| 224 | yield x |
| 225 | yield t.label |
| 226 | for x in inorder(t.right): |
| 227 | yield x |
| 228 | \end{verbatim} |
| 229 | |
| 230 | Two other examples in \file{Lib/test/test_generators.py} produce |
| 231 | solutions for the N-Queens problem (placing $N$ queens on an $NxN$ |
| 232 | chess board so that no queen threatens another) and the Knight's Tour |
| 233 | (a route that takes a knight to every square of an $NxN$ chessboard |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 234 | without visiting any square twice). |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 235 | |
| 236 | The idea of generators comes from other programming languages, |
| 237 | especially Icon (\url{http://www.cs.arizona.edu/icon/}), where the |
| 238 | idea of generators is central. In Icon, every |
| 239 | expression and function call behaves like a generator. One example |
| 240 | from ``An Overview of the Icon Programming Language'' at |
| 241 | \url{http://www.cs.arizona.edu/icon/docs/ipd266.htm} gives an idea of |
| 242 | what this looks like: |
| 243 | |
| 244 | \begin{verbatim} |
| 245 | sentence := "Store it in the neighboring harbor" |
| 246 | if (i := find("or", sentence)) > 5 then write(i) |
| 247 | \end{verbatim} |
| 248 | |
| 249 | In Icon the \function{find()} function returns the indexes at which the |
| 250 | substring ``or'' is found: 3, 23, 33. In the \keyword{if} statement, |
| 251 | \code{i} is first assigned a value of 3, but 3 is less than 5, so the |
| 252 | comparison fails, and Icon retries it with the second value of 23. 23 |
| 253 | is greater than 5, so the comparison now succeeds, and the code prints |
| 254 | the value 23 to the screen. |
| 255 | |
| 256 | Python doesn't go nearly as far as Icon in adopting generators as a |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 257 | central concept. Generators are considered part of the core |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 258 | Python language, but learning or using them isn't compulsory; if they |
| 259 | don't solve any problems that you have, feel free to ignore them. |
| 260 | One novel feature of Python's interface as compared to |
| 261 | Icon's is that a generator's state is represented as a concrete object |
| 262 | (the iterator) that can be passed around to other functions or stored |
| 263 | in a data structure. |
| 264 | |
| 265 | \begin{seealso} |
| 266 | |
| 267 | \seepep{255}{Simple Generators}{Written by Neil Schemenauer, Tim |
| 268 | Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer |
| 269 | and Tim Peters, with other fixes from the Python Labs crew.} |
| 270 | |
| 271 | \end{seealso} |
| 272 | |
| 273 | |
| 274 | %====================================================================== |
Fred Drake | 13090e1 | 2002-08-22 16:51:08 +0000 | [diff] [blame] | 275 | \section{PEP 263: Source Code Encodings \label{section-encodings}} |
Andrew M. Kuchling | 950725f | 2002-08-06 01:40:48 +0000 | [diff] [blame] | 276 | |
| 277 | Python source files can now be declared as being in different |
| 278 | character set encodings. Encodings are declared by including a |
| 279 | specially formatted comment in the first or second line of the source |
| 280 | file. For example, a UTF-8 file can be declared with: |
| 281 | |
| 282 | \begin{verbatim} |
| 283 | #!/usr/bin/env python |
| 284 | # -*- coding: UTF-8 -*- |
| 285 | \end{verbatim} |
| 286 | |
| 287 | Without such an encoding declaration, the default encoding used is |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 288 | ISO-8859-1, also known as Latin1. |
Andrew M. Kuchling | 950725f | 2002-08-06 01:40:48 +0000 | [diff] [blame] | 289 | |
| 290 | The encoding declaration only affects Unicode string literals; the |
| 291 | text in the source code will be converted to Unicode using the |
| 292 | specified encoding. Note that Python identifiers are still restricted |
| 293 | to ASCII characters, so you can't have variable names that use |
| 294 | characters outside of the usual alphanumerics. |
| 295 | |
| 296 | \begin{seealso} |
| 297 | |
| 298 | \seepep{263}{Defining Python Source Code Encodings}{Written by |
Martin v. Löwis | bd5e38d | 2002-10-07 18:52:29 +0000 | [diff] [blame] | 299 | Marc-Andr\'e Lemburg and Martin von L\"owis; implemented by SUZUKI |
| 300 | Hisao and Martin von L\"owis.} |
Andrew M. Kuchling | 950725f | 2002-08-06 01:40:48 +0000 | [diff] [blame] | 301 | |
| 302 | \end{seealso} |
| 303 | |
| 304 | |
| 305 | %====================================================================== |
Martin v. Löwis | bd5e38d | 2002-10-07 18:52:29 +0000 | [diff] [blame] | 306 | \section{PEP 277: Unicode file name support for Windows NT} |
Andrew M. Kuchling | 0f34556 | 2002-10-04 22:34:11 +0000 | [diff] [blame] | 307 | |
Martin v. Löwis | bd5e38d | 2002-10-07 18:52:29 +0000 | [diff] [blame] | 308 | On Windows NT, 2000, and XP, the system stores file names as Unicode |
Andrew M. Kuchling | 0a6fa96 | 2002-10-09 12:11:10 +0000 | [diff] [blame] | 309 | strings. Traditionally, Python has represented file names as byte |
| 310 | strings, which is inadequate because it renders some file names |
Martin v. Löwis | bd5e38d | 2002-10-07 18:52:29 +0000 | [diff] [blame] | 311 | inaccessible. |
| 312 | |
Andrew M. Kuchling | 0a6fa96 | 2002-10-09 12:11:10 +0000 | [diff] [blame] | 313 | Python now allows using arbitrary Unicode strings (within the |
| 314 | limitations of the file system) for all functions that expect file |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 315 | names, most notably the \function{open()} built-in function. If a Unicode |
| 316 | string is passed to \function{os.listdir()}, Python now returns a list |
Andrew M. Kuchling | 0a6fa96 | 2002-10-09 12:11:10 +0000 | [diff] [blame] | 317 | of Unicode strings. A new function, \function{os.getcwdu()}, returns |
| 318 | the current directory as a Unicode string. |
Martin v. Löwis | bd5e38d | 2002-10-07 18:52:29 +0000 | [diff] [blame] | 319 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 320 | Byte strings still work as file names, and on Windows Python will |
| 321 | transparently convert them to Unicode using the \code{mbcs} encoding. |
Martin v. Löwis | bd5e38d | 2002-10-07 18:52:29 +0000 | [diff] [blame] | 322 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 323 | Other systems also allow Unicode strings as file names but convert |
| 324 | them to byte strings before passing them to the system, which can |
| 325 | cause a \exception{UnicodeError} to be raised. Applications can test |
| 326 | whether arbitrary Unicode strings are supported as file names by |
Andrew M. Kuchling | b9ba4e6 | 2003-02-03 15:16:15 +0000 | [diff] [blame] | 327 | checking \member{os.path.supports_unicode_filenames}, a Boolean value. |
Martin v. Löwis | bd5e38d | 2002-10-07 18:52:29 +0000 | [diff] [blame] | 328 | |
| 329 | \begin{seealso} |
| 330 | |
| 331 | \seepep{277}{Unicode file name support for Windows NT}{Written by Neil |
| 332 | Hodgson; implemented by Neil Hodgson, Martin von L\"owis, and Mark |
| 333 | Hammond.} |
| 334 | |
| 335 | \end{seealso} |
Andrew M. Kuchling | 0f34556 | 2002-10-04 22:34:11 +0000 | [diff] [blame] | 336 | |
| 337 | |
| 338 | %====================================================================== |
Andrew M. Kuchling | f367651 | 2002-04-15 02:27:55 +0000 | [diff] [blame] | 339 | \section{PEP 278: Universal Newline Support} |
| 340 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 341 | The three major operating systems used today are Microsoft Windows, |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 342 | Apple's Macintosh OS, and the various \UNIX\ derivatives. A minor |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 343 | irritation is that these three platforms all use different characters |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 344 | to mark the ends of lines in text files. \UNIX\ uses the linefeed |
| 345 | (ASCII character 10), while MacOS uses the carriage return (ASCII |
| 346 | character 13), and Windows uses a two-character sequence containing a |
| 347 | carriage return plus a newline. |
Andrew M. Kuchling | f367651 | 2002-04-15 02:27:55 +0000 | [diff] [blame] | 348 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 349 | Python's file objects can now support end of line conventions other |
| 350 | than the one followed by the platform on which Python is running. |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 351 | Opening a file with the mode \code{'U'} or \code{'rU'} will open a file |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 352 | for reading in universal newline mode. All three line ending |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 353 | conventions will be translated to a \character{\e n} in the strings |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 354 | returned by the various file methods such as \method{read()} and |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 355 | \method{readline()}. |
Andrew M. Kuchling | f367651 | 2002-04-15 02:27:55 +0000 | [diff] [blame] | 356 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 357 | Universal newline support is also used when importing modules and when |
| 358 | executing a file with the \function{execfile()} function. This means |
| 359 | that Python modules can be shared between all three operating systems |
| 360 | without needing to convert the line-endings. |
| 361 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 362 | This feature can be disabled at compile-time by specifying |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 363 | \longprogramopt{without-universal-newlines} when running Python's |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 364 | \program{configure} script. |
Andrew M. Kuchling | f367651 | 2002-04-15 02:27:55 +0000 | [diff] [blame] | 365 | |
| 366 | \begin{seealso} |
| 367 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 368 | \seepep{278}{Universal Newline Support}{Written |
Andrew M. Kuchling | f367651 | 2002-04-15 02:27:55 +0000 | [diff] [blame] | 369 | and implemented by Jack Jansen.} |
| 370 | |
| 371 | \end{seealso} |
| 372 | |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 373 | |
| 374 | %====================================================================== |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 375 | \section{PEP 279: The \function{enumerate()} Built-in Function\label{section-enumerate}} |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 376 | |
| 377 | A new built-in function, \function{enumerate()}, will make |
| 378 | certain loops a bit clearer. \code{enumerate(thing)}, where |
| 379 | \var{thing} is either an iterator or a sequence, returns a iterator |
| 380 | that will return \code{(0, \var{thing[0]})}, \code{(1, |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 381 | \var{thing[1]})}, \code{(2, \var{thing[2]})}, and so forth. |
| 382 | |
| 383 | Fairly often you'll see code to change every element of a list that |
| 384 | looks like this: |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 385 | |
| 386 | \begin{verbatim} |
| 387 | for i in range(len(L)): |
| 388 | item = L[i] |
| 389 | # ... compute some result based on item ... |
| 390 | L[i] = result |
| 391 | \end{verbatim} |
| 392 | |
| 393 | This can be rewritten using \function{enumerate()} as: |
| 394 | |
| 395 | \begin{verbatim} |
| 396 | for i, item in enumerate(L): |
| 397 | # ... compute some result based on item ... |
| 398 | L[i] = result |
| 399 | \end{verbatim} |
| 400 | |
| 401 | |
| 402 | \begin{seealso} |
| 403 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 404 | \seepep{279}{The enumerate() built-in function}{Written |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 405 | and implemented by Raymond D. Hettinger.} |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 406 | |
| 407 | \end{seealso} |
| 408 | |
| 409 | |
Andrew M. Kuchling | f367651 | 2002-04-15 02:27:55 +0000 | [diff] [blame] | 410 | %====================================================================== |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 411 | \section{PEP 282: The \module{logging} Package} |
| 412 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 413 | A standard package for writing logs, \module{logging}, has been added |
| 414 | to Python 2.3. It provides a powerful and flexible mechanism for |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 415 | components to generate logging output which can then be filtered and |
| 416 | processed in various ways. A standard configuration file format can |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 417 | be used to control the logging behavior of a program. Python's |
| 418 | standard library includes handlers that will write log records to |
| 419 | standard error or to a file or socket, send them to the system log, or |
| 420 | even e-mail them to a particular address, and of course it's also |
| 421 | possible to write your own handler classes. |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 422 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 423 | The \class{Logger} class is the primary class. |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 424 | Most application code will deal with one or more \class{Logger} |
| 425 | objects, each one used by a particular subsystem of the application. |
| 426 | Each \class{Logger} is identified by a name, and names are organized |
| 427 | into a hierarchy using \samp{.} as the component separator. For |
| 428 | example, you might have \class{Logger} instances named \samp{server}, |
| 429 | \samp{server.auth} and \samp{server.network}. The latter two |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 430 | instances are below \samp{server} in the hierarchy. This means that |
| 431 | if you turn up the verbosity for \samp{server} or direct \samp{server} |
| 432 | messages to a different handler, the changes will also apply to |
| 433 | records logged to \samp{server.auth} and \samp{server.network}. |
| 434 | There's also a root \class{Logger} that's the parent of all other |
| 435 | loggers. |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 436 | |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 437 | For simple uses, the \module{logging} package contains some |
| 438 | convenience functions that always use the root log: |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 439 | |
| 440 | \begin{verbatim} |
| 441 | import logging |
| 442 | |
| 443 | logging.debug('Debugging information') |
| 444 | logging.info('Informational message') |
Andrew M. Kuchling | b1e4bf9 | 2002-12-03 13:35:17 +0000 | [diff] [blame] | 445 | logging.warn('Warning:config file %s not found', 'server.conf') |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 446 | logging.error('Error occurred') |
| 447 | logging.critical('Critical error -- shutting down') |
| 448 | \end{verbatim} |
| 449 | |
| 450 | This produces the following output: |
| 451 | |
| 452 | \begin{verbatim} |
Andrew M. Kuchling | b1e4bf9 | 2002-12-03 13:35:17 +0000 | [diff] [blame] | 453 | WARN:root:Warning:config file server.conf not found |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 454 | ERROR:root:Error occurred |
| 455 | CRITICAL:root:Critical error -- shutting down |
| 456 | \end{verbatim} |
| 457 | |
| 458 | In the default configuration, informational and debugging messages are |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 459 | suppressed and the output is sent to standard error. You can enable |
| 460 | the display of information and debugging messages by calling the |
| 461 | \method{setLevel()} method on the root logger. |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 462 | |
| 463 | Notice the \function{warn()} call's use of string formatting |
| 464 | operators; all of the functions for logging messages take the |
| 465 | arguments \code{(\var{msg}, \var{arg1}, \var{arg2}, ...)} and log the |
| 466 | string resulting from \code{\var{msg} \% (\var{arg1}, \var{arg2}, |
| 467 | ...)}. |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 468 | |
| 469 | There's also an \function{exception()} function that records the most |
| 470 | recent traceback. Any of the other functions will also record the |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 471 | traceback if you specify a true value for the keyword argument |
Fred Drake | aac8c58 | 2003-01-17 22:50:10 +0000 | [diff] [blame] | 472 | \var{exc_info}. |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 473 | |
| 474 | \begin{verbatim} |
| 475 | def f(): |
| 476 | try: 1/0 |
| 477 | except: logging.exception('Problem recorded') |
| 478 | |
| 479 | f() |
| 480 | \end{verbatim} |
| 481 | |
| 482 | This produces the following output: |
| 483 | |
| 484 | \begin{verbatim} |
| 485 | ERROR:root:Problem recorded |
| 486 | Traceback (most recent call last): |
| 487 | File "t.py", line 6, in f |
| 488 | 1/0 |
| 489 | ZeroDivisionError: integer division or modulo by zero |
| 490 | \end{verbatim} |
| 491 | |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 492 | Slightly more advanced programs will use a logger other than the root |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 493 | logger. The \function{getLogger(\var{name})} function is used to get |
| 494 | a particular log, creating it if it doesn't exist yet. |
Andrew M. Kuchling | b1e4bf9 | 2002-12-03 13:35:17 +0000 | [diff] [blame] | 495 | \function{getLogger(None)} returns the root logger. |
| 496 | |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 497 | |
| 498 | \begin{verbatim} |
| 499 | log = logging.getLogger('server') |
| 500 | ... |
| 501 | log.info('Listening on port %i', port) |
| 502 | ... |
| 503 | log.critical('Disk full') |
| 504 | ... |
| 505 | \end{verbatim} |
| 506 | |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 507 | Log records are usually propagated up the hierarchy, so a message |
| 508 | logged to \samp{server.auth} is also seen by \samp{server} and |
| 509 | \samp{root}, but a handler can prevent this by setting its |
Fred Drake | aac8c58 | 2003-01-17 22:50:10 +0000 | [diff] [blame] | 510 | \member{propagate} attribute to \constant{False}. |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 511 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 512 | There are more classes provided by the \module{logging} package that |
| 513 | can be customized. When a \class{Logger} instance is told to log a |
| 514 | message, it creates a \class{LogRecord} instance that is sent to any |
| 515 | number of different \class{Handler} instances. Loggers and handlers |
| 516 | can also have an attached list of filters, and each filter can cause |
| 517 | the \class{LogRecord} to be ignored or can modify the record before |
| 518 | passing it along. \class{LogRecord} instances are converted to text |
| 519 | for output by a \class{Formatter} class. All of these classes can be |
| 520 | replaced by your own specially-written classes. |
| 521 | |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 522 | With all of these features the \module{logging} package should provide |
| 523 | enough flexibility for even the most complicated applications. This |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 524 | is only a partial overview of the \module{logging} package, so please |
| 525 | see the \ulink{package's reference |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 526 | documentation}{../lib/module-logging.html} for all of the details. |
| 527 | Reading \pep{282} will also be helpful. |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 528 | |
| 529 | |
| 530 | \begin{seealso} |
| 531 | |
| 532 | \seepep{282}{A Logging System}{Written by Vinay Sajip and Trent Mick; |
| 533 | implemented by Vinay Sajip.} |
| 534 | |
| 535 | \end{seealso} |
| 536 | |
| 537 | |
| 538 | %====================================================================== |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 539 | \section{PEP 285: The \class{bool} Type\label{section-bool}} |
| 540 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 541 | A Boolean type was added to Python 2.3. Two new constants were added |
| 542 | to the \module{__builtin__} module, \constant{True} and |
Andrew M. Kuchling | 5a22453 | 2003-01-03 16:52:27 +0000 | [diff] [blame] | 543 | \constant{False}. (\constant{True} and |
| 544 | \constant{False} constants were added to the built-ins |
Michael W. Hudson | 065f5fa | 2003-02-10 19:24:50 +0000 | [diff] [blame] | 545 | in Python 2.2.1, but the 2.2.1 versions simply have integer values of |
Andrew M. Kuchling | 5a22453 | 2003-01-03 16:52:27 +0000 | [diff] [blame] | 546 | 1 and 0 and aren't a different type.) |
| 547 | |
| 548 | The type object for this new type is named |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 549 | \class{bool}; the constructor for it takes any Python value and |
| 550 | converts it to \constant{True} or \constant{False}. |
| 551 | |
| 552 | \begin{verbatim} |
| 553 | >>> bool(1) |
| 554 | True |
| 555 | >>> bool(0) |
| 556 | False |
| 557 | >>> bool([]) |
| 558 | False |
| 559 | >>> bool( (1,) ) |
| 560 | True |
| 561 | \end{verbatim} |
| 562 | |
| 563 | Most of the standard library modules and built-in functions have been |
| 564 | changed to return Booleans. |
| 565 | |
| 566 | \begin{verbatim} |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 567 | >>> obj = [] |
| 568 | >>> hasattr(obj, 'append') |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 569 | True |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 570 | >>> isinstance(obj, list) |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 571 | True |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 572 | >>> isinstance(obj, tuple) |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 573 | False |
| 574 | \end{verbatim} |
| 575 | |
| 576 | Python's Booleans were added with the primary goal of making code |
| 577 | clearer. For example, if you're reading a function and encounter the |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 578 | statement \code{return 1}, you might wonder whether the \code{1} |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 579 | represents a Boolean truth value, an index, or a |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 580 | coefficient that multiplies some other quantity. If the statement is |
| 581 | \code{return True}, however, the meaning of the return value is quite |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 582 | clear. |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 583 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 584 | Python's Booleans were \emph{not} added for the sake of strict |
| 585 | type-checking. A very strict language such as Pascal would also |
| 586 | prevent you performing arithmetic with Booleans, and would require |
| 587 | that the expression in an \keyword{if} statement always evaluate to a |
| 588 | Boolean. Python is not this strict, and it never will be, as |
| 589 | \pep{285} explicitly says. This means you can still use any |
| 590 | expression in an \keyword{if} statement, even ones that evaluate to a |
| 591 | list or tuple or some random object, and the Boolean type is a |
| 592 | subclass of the \class{int} class so that arithmetic using a Boolean |
| 593 | still works. |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 594 | |
| 595 | \begin{verbatim} |
| 596 | >>> True + 1 |
| 597 | 2 |
| 598 | >>> False + 1 |
| 599 | 1 |
| 600 | >>> False * 75 |
| 601 | 0 |
| 602 | >>> True * 75 |
| 603 | 75 |
| 604 | \end{verbatim} |
| 605 | |
| 606 | To sum up \constant{True} and \constant{False} in a sentence: they're |
| 607 | alternative ways to spell the integer values 1 and 0, with the single |
| 608 | difference that \function{str()} and \function{repr()} return the |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 609 | strings \code{'True'} and \code{'False'} instead of \code{'1'} and |
| 610 | \code{'0'}. |
Andrew M. Kuchling | 3a52ff6 | 2002-04-03 22:44:47 +0000 | [diff] [blame] | 611 | |
| 612 | \begin{seealso} |
| 613 | |
| 614 | \seepep{285}{Adding a bool type}{Written and implemented by GvR.} |
| 615 | |
| 616 | \end{seealso} |
| 617 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 618 | |
Andrew M. Kuchling | 65b7282 | 2002-09-03 00:53:21 +0000 | [diff] [blame] | 619 | %====================================================================== |
| 620 | \section{PEP 293: Codec Error Handling Callbacks} |
| 621 | |
Martin v. Löwis | 20eae69 | 2002-10-07 19:01:07 +0000 | [diff] [blame] | 622 | When encoding a Unicode string into a byte string, unencodable |
Andrew M. Kuchling | 0a6fa96 | 2002-10-09 12:11:10 +0000 | [diff] [blame] | 623 | characters may be encountered. So far, Python has allowed specifying |
| 624 | the error processing as either ``strict'' (raising |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 625 | \exception{UnicodeError}), ``ignore'' (skipping the character), or |
| 626 | ``replace'' (using a question mark in the output string), with |
| 627 | ``strict'' being the default behavior. It may be desirable to specify |
| 628 | alternative processing of such errors, such as inserting an XML |
| 629 | character reference or HTML entity reference into the converted |
| 630 | string. |
Martin v. Löwis | 20eae69 | 2002-10-07 19:01:07 +0000 | [diff] [blame] | 631 | |
Andrew M. Kuchling | b492fa9 | 2002-11-27 19:11:10 +0000 | [diff] [blame] | 632 | Python now has a flexible framework to add different processing |
Andrew M. Kuchling | 0a6fa96 | 2002-10-09 12:11:10 +0000 | [diff] [blame] | 633 | strategies. New error handlers can be added with |
Martin v. Löwis | 20eae69 | 2002-10-07 19:01:07 +0000 | [diff] [blame] | 634 | \function{codecs.register_error}. Codecs then can access the error |
Andrew M. Kuchling | 0a6fa96 | 2002-10-09 12:11:10 +0000 | [diff] [blame] | 635 | handler with \function{codecs.lookup_error}. An equivalent C API has |
| 636 | been added for codecs written in C. The error handler gets the |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 637 | necessary state information such as the string being converted, the |
Andrew M. Kuchling | 0a6fa96 | 2002-10-09 12:11:10 +0000 | [diff] [blame] | 638 | position in the string where the error was detected, and the target |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 639 | encoding. The handler can then either raise an exception or return a |
Andrew M. Kuchling | 0a6fa96 | 2002-10-09 12:11:10 +0000 | [diff] [blame] | 640 | replacement string. |
Martin v. Löwis | 20eae69 | 2002-10-07 19:01:07 +0000 | [diff] [blame] | 641 | |
| 642 | Two additional error handlers have been implemented using this |
Andrew M. Kuchling | 0a6fa96 | 2002-10-09 12:11:10 +0000 | [diff] [blame] | 643 | framework: ``backslashreplace'' uses Python backslash quoting to |
Andrew M. Kuchling | b492fa9 | 2002-11-27 19:11:10 +0000 | [diff] [blame] | 644 | represent unencodable characters and ``xmlcharrefreplace'' emits |
Martin v. Löwis | 20eae69 | 2002-10-07 19:01:07 +0000 | [diff] [blame] | 645 | XML character references. |
Andrew M. Kuchling | 65b7282 | 2002-09-03 00:53:21 +0000 | [diff] [blame] | 646 | |
| 647 | \begin{seealso} |
| 648 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 649 | \seepep{293}{Codec Error Handling Callbacks}{Written and implemented by |
Andrew M. Kuchling | 0a6fa96 | 2002-10-09 12:11:10 +0000 | [diff] [blame] | 650 | Walter D\"orwald.} |
Andrew M. Kuchling | 65b7282 | 2002-09-03 00:53:21 +0000 | [diff] [blame] | 651 | |
| 652 | \end{seealso} |
| 653 | |
| 654 | |
| 655 | %====================================================================== |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 656 | \section{PEP 273: Importing Modules from Zip Archives} |
| 657 | |
| 658 | The new \module{zipimport} module adds support for importing |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 659 | modules from a ZIP-format archive. You don't need to import the |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 660 | module explicitly; it will be automatically imported if a ZIP |
| 661 | archive's filename is added to \code{sys.path}. For example: |
| 662 | |
| 663 | \begin{verbatim} |
| 664 | amk@nyman:~/src/python$ unzip -l /tmp/example.zip |
| 665 | Archive: /tmp/example.zip |
| 666 | Length Date Time Name |
| 667 | -------- ---- ---- ---- |
| 668 | 8467 11-26-02 22:30 jwzthreading.py |
| 669 | -------- ------- |
| 670 | 8467 1 file |
| 671 | amk@nyman:~/src/python$ ./python |
| 672 | Python 2.3a0 (#1, Dec 30 2002, 19:54:32) |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 673 | >>> import sys |
| 674 | >>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path |
| 675 | >>> import jwzthreading |
| 676 | >>> jwzthreading.__file__ |
| 677 | '/tmp/example.zip/jwzthreading.py' |
| 678 | >>> |
| 679 | \end{verbatim} |
| 680 | |
| 681 | An entry in \code{sys.path} can now be the filename of a ZIP archive. |
| 682 | The ZIP archive can contain any kind of files, but only files named |
Fred Drake | aac8c58 | 2003-01-17 22:50:10 +0000 | [diff] [blame] | 683 | \file{*.py}, \file{*.pyc}, or \file{*.pyo} can be imported. If an |
| 684 | archive only contains \file{*.py} files, Python will not attempt to |
| 685 | modify the archive by adding the corresponding \file{*.pyc} file, meaning |
| 686 | that if a ZIP archive doesn't contain \file{*.pyc} files, importing may be |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 687 | rather slow. |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 688 | |
| 689 | A path within the archive can also be specified to only import from a |
| 690 | subdirectory; for example, the path \file{/tmp/example.zip/lib/} |
| 691 | would only import from the \file{lib/} subdirectory within the |
| 692 | archive. |
| 693 | |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 694 | \begin{seealso} |
| 695 | |
| 696 | \seepep{273}{Import Modules from Zip Archives}{Written by James C. Ahlstrom, |
| 697 | who also provided an implementation. |
| 698 | Python 2.3 follows the specification in \pep{273}, |
Andrew M. Kuchling | ae3bbf5 | 2002-12-31 14:03:45 +0000 | [diff] [blame] | 699 | but uses an implementation written by Just van~Rossum |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 700 | that uses the import hooks described in \pep{302}. |
| 701 | See section~\ref{section-pep302} for a description of the new import hooks. |
| 702 | } |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 703 | |
| 704 | \end{seealso} |
| 705 | |
| 706 | %====================================================================== |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 707 | \section{PEP 301: Package Index and Metadata for |
| 708 | Distutils\label{section-pep301}} |
Andrew M. Kuchling | 87cebbf | 2003-01-03 16:24:28 +0000 | [diff] [blame] | 709 | |
Andrew M. Kuchling | 5a22453 | 2003-01-03 16:52:27 +0000 | [diff] [blame] | 710 | Support for the long-requested Python catalog makes its first |
| 711 | appearance in 2.3. |
| 712 | |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 713 | The core component is the new Distutils \command{register} command. |
| 714 | Running \code{python setup.py register} will collect the metadata |
Andrew M. Kuchling | 5a22453 | 2003-01-03 16:52:27 +0000 | [diff] [blame] | 715 | describing a package, such as its name, version, maintainer, |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 716 | description, \&c., and send it to a central catalog server. |
Andrew M. Kuchling | 5a22453 | 2003-01-03 16:52:27 +0000 | [diff] [blame] | 717 | Currently the catalog can be browsed at |
| 718 | \url{http://www.amk.ca/cgi-bin/pypi.cgi}, but it will move to |
| 719 | some hostname in the \code{python.org} domain before the final version |
| 720 | of 2.3 is released. |
| 721 | |
| 722 | To make the catalog a bit more useful, a new optional |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 723 | \var{classifiers} keyword argument has been added to the Distutils |
Andrew M. Kuchling | 5a22453 | 2003-01-03 16:52:27 +0000 | [diff] [blame] | 724 | \function{setup()} function. A list of |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 725 | \ulink{Trove}{http://catb.org/\textasciitilde esr/trove/}-style |
| 726 | strings can be supplied to help classify the software. |
Andrew M. Kuchling | 5a22453 | 2003-01-03 16:52:27 +0000 | [diff] [blame] | 727 | |
Andrew M. Kuchling | a31bb37 | 2003-01-27 16:36:34 +0000 | [diff] [blame] | 728 | Here's an example \file{setup.py} with classifiers, written to be compatible |
| 729 | with older versions of the Distutils: |
Andrew M. Kuchling | 5a22453 | 2003-01-03 16:52:27 +0000 | [diff] [blame] | 730 | |
| 731 | \begin{verbatim} |
Andrew M. Kuchling | a31bb37 | 2003-01-27 16:36:34 +0000 | [diff] [blame] | 732 | from distutils import core |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 733 | kw = {'name': "Quixote", |
Andrew M. Kuchling | a31bb37 | 2003-01-27 16:36:34 +0000 | [diff] [blame] | 734 | 'version': "0.5.1", |
| 735 | 'description': "A highly Pythonic Web application framework", |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 736 | # ... |
| 737 | } |
Andrew M. Kuchling | a31bb37 | 2003-01-27 16:36:34 +0000 | [diff] [blame] | 738 | |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 739 | if ( hasattr(core, 'setup_keywords') and |
| 740 | 'classifiers' in core.setup_keywords): |
| 741 | kw['classifiers'] = \ |
| 742 | ['Topic :: Internet :: WWW/HTTP :: Dynamic Content', |
| 743 | 'Environment :: No Input/Output (Daemon)', |
| 744 | 'Intended Audience :: Developers'], |
Andrew M. Kuchling | a31bb37 | 2003-01-27 16:36:34 +0000 | [diff] [blame] | 745 | |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 746 | core.setup(**kw) |
Andrew M. Kuchling | 5a22453 | 2003-01-03 16:52:27 +0000 | [diff] [blame] | 747 | \end{verbatim} |
| 748 | |
| 749 | The full list of classifiers can be obtained by running |
| 750 | \code{python setup.py register --list-classifiers}. |
Andrew M. Kuchling | 87cebbf | 2003-01-03 16:24:28 +0000 | [diff] [blame] | 751 | |
| 752 | \begin{seealso} |
| 753 | |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 754 | \seepep{301}{Package Index and Metadata for Distutils}{Written and |
| 755 | implemented by Richard Jones.} |
Andrew M. Kuchling | 87cebbf | 2003-01-03 16:24:28 +0000 | [diff] [blame] | 756 | |
| 757 | \end{seealso} |
| 758 | |
| 759 | |
| 760 | %====================================================================== |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 761 | \section{PEP 302: New Import Hooks \label{section-pep302}} |
| 762 | |
| 763 | While it's been possible to write custom import hooks ever since the |
| 764 | \module{ihooks} module was introduced in Python 1.3, no one has ever |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 765 | been really happy with it because writing new import hooks is |
| 766 | difficult and messy. There have been various proposed alternatives |
| 767 | such as the \module{imputil} and \module{iu} modules, but none of them |
| 768 | has ever gained much acceptance, and none of them were easily usable |
| 769 | from \C{} code. |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 770 | |
| 771 | \pep{302} borrows ideas from its predecessors, especially from |
| 772 | Gordon McMillan's \module{iu} module. Three new items |
| 773 | are added to the \module{sys} module: |
| 774 | |
| 775 | \begin{itemize} |
Andrew M. Kuchling | d5ac8d0 | 2003-01-02 21:33:15 +0000 | [diff] [blame] | 776 | \item \code{sys.path_hooks} is a list of callable objects; most |
Fred Drake | aac8c58 | 2003-01-17 22:50:10 +0000 | [diff] [blame] | 777 | often they'll be classes. Each callable takes a string containing a |
| 778 | path and either returns an importer object that will handle imports |
| 779 | from this path or raises an \exception{ImportError} exception if it |
| 780 | can't handle this path. |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 781 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 782 | \item \code{sys.path_importer_cache} caches importer objects for |
Fred Drake | aac8c58 | 2003-01-17 22:50:10 +0000 | [diff] [blame] | 783 | each path, so \code{sys.path_hooks} will only need to be traversed |
| 784 | once for each path. |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 785 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 786 | \item \code{sys.meta_path} is a list of importer objects that will |
| 787 | be traversed before \code{sys.path} is checked. This list is |
| 788 | initially empty, but user code can add objects to it. Additional |
| 789 | built-in and frozen modules can be imported by an object added to |
| 790 | this list. |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 791 | |
| 792 | \end{itemize} |
| 793 | |
| 794 | Importer objects must have a single method, |
| 795 | \method{find_module(\var{fullname}, \var{path}=None)}. \var{fullname} |
| 796 | will be a module or package name, e.g. \samp{string} or |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 797 | \samp{distutils.core}. \method{find_module()} must return a loader object |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 798 | that has a single method, \method{load_module(\var{fullname})}, that |
| 799 | creates and returns the corresponding module object. |
| 800 | |
| 801 | Pseudo-code for Python's new import logic, therefore, looks something |
| 802 | like this (simplified a bit; see \pep{302} for the full details): |
| 803 | |
| 804 | \begin{verbatim} |
| 805 | for mp in sys.meta_path: |
| 806 | loader = mp(fullname) |
| 807 | if loader is not None: |
Andrew M. Kuchling | d5ac8d0 | 2003-01-02 21:33:15 +0000 | [diff] [blame] | 808 | <module> = loader.load_module(fullname) |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 809 | |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 810 | for path in sys.path: |
| 811 | for hook in sys.path_hooks: |
Andrew M. Kuchling | d5ac8d0 | 2003-01-02 21:33:15 +0000 | [diff] [blame] | 812 | try: |
| 813 | importer = hook(path) |
| 814 | except ImportError: |
| 815 | # ImportError, so try the other path hooks |
| 816 | pass |
| 817 | else: |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 818 | loader = importer.find_module(fullname) |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 819 | <module> = loader.load_module(fullname) |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 820 | |
| 821 | # Not found! |
| 822 | raise ImportError |
| 823 | \end{verbatim} |
| 824 | |
| 825 | \begin{seealso} |
| 826 | |
| 827 | \seepep{302}{New Import Hooks}{Written by Just van~Rossum and Paul Moore. |
Andrew M. Kuchling | ae3bbf5 | 2002-12-31 14:03:45 +0000 | [diff] [blame] | 828 | Implemented by Just van~Rossum. |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | \end{seealso} |
| 832 | |
| 833 | |
| 834 | %====================================================================== |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 835 | \section{Extended Slices\label{section-slices}} |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 836 | |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 837 | Ever since Python 1.4, the slicing syntax has supported an optional |
| 838 | third ``step'' or ``stride'' argument. For example, these are all |
| 839 | legal Python syntax: \code{L[1:10:2]}, \code{L[:-1:1]}, |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 840 | \code{L[::-1]}. This was added to Python at the request of |
| 841 | the developers of Numerical Python, which uses the third argument |
| 842 | extensively. However, Python's built-in list, tuple, and string |
| 843 | sequence types have never supported this feature, and you got a |
| 844 | \exception{TypeError} if you tried it. Michael Hudson contributed a |
| 845 | patch to fix this shortcoming. |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 846 | |
| 847 | For example, you can now easily extract the elements of a list that |
| 848 | have even indexes: |
Fred Drake | df872a2 | 2002-07-03 12:02:01 +0000 | [diff] [blame] | 849 | |
| 850 | \begin{verbatim} |
| 851 | >>> L = range(10) |
| 852 | >>> L[::2] |
| 853 | [0, 2, 4, 6, 8] |
| 854 | \end{verbatim} |
| 855 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 856 | Negative values also work to make a copy of the same list in reverse |
| 857 | order: |
Fred Drake | df872a2 | 2002-07-03 12:02:01 +0000 | [diff] [blame] | 858 | |
| 859 | \begin{verbatim} |
| 860 | >>> L[::-1] |
| 861 | [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] |
| 862 | \end{verbatim} |
Andrew M. Kuchling | 3a52ff6 | 2002-04-03 22:44:47 +0000 | [diff] [blame] | 863 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 864 | This also works for tuples, arrays, and strings: |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 865 | |
| 866 | \begin{verbatim} |
| 867 | >>> s='abcd' |
| 868 | >>> s[::2] |
| 869 | 'ac' |
| 870 | >>> s[::-1] |
| 871 | 'dcba' |
| 872 | \end{verbatim} |
| 873 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 874 | If you have a mutable sequence such as a list or an array you can |
Michael W. Hudson | 4da01ed | 2002-07-19 15:48:56 +0000 | [diff] [blame] | 875 | assign to or delete an extended slice, but there are some differences |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 876 | between assignment to extended and regular slices. Assignment to a |
| 877 | regular slice can be used to change the length of the sequence: |
Michael W. Hudson | 4da01ed | 2002-07-19 15:48:56 +0000 | [diff] [blame] | 878 | |
| 879 | \begin{verbatim} |
| 880 | >>> a = range(3) |
| 881 | >>> a |
| 882 | [0, 1, 2] |
| 883 | >>> a[1:3] = [4, 5, 6] |
| 884 | >>> a |
| 885 | [0, 4, 5, 6] |
| 886 | \end{verbatim} |
| 887 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 888 | Extended slices aren't this flexible. When assigning to an extended |
| 889 | slice the list on the right hand side of the statement must contain |
| 890 | the same number of items as the slice it is replacing: |
Michael W. Hudson | 4da01ed | 2002-07-19 15:48:56 +0000 | [diff] [blame] | 891 | |
| 892 | \begin{verbatim} |
| 893 | >>> a = range(4) |
| 894 | >>> a |
| 895 | [0, 1, 2, 3] |
| 896 | >>> a[::2] |
| 897 | [0, 2] |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 898 | >>> a[::2] = [0, -1] |
Michael W. Hudson | 4da01ed | 2002-07-19 15:48:56 +0000 | [diff] [blame] | 899 | >>> a |
| 900 | [0, 1, -1, 3] |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 901 | >>> a[::2] = [0,1,2] |
Michael W. Hudson | 4da01ed | 2002-07-19 15:48:56 +0000 | [diff] [blame] | 902 | Traceback (most recent call last): |
| 903 | File "<stdin>", line 1, in ? |
Raymond Hettinger | ee1bded | 2003-01-17 16:20:23 +0000 | [diff] [blame] | 904 | ValueError: attempt to assign sequence of size 3 to extended slice of size 2 |
Michael W. Hudson | 4da01ed | 2002-07-19 15:48:56 +0000 | [diff] [blame] | 905 | \end{verbatim} |
| 906 | |
| 907 | Deletion is more straightforward: |
| 908 | |
| 909 | \begin{verbatim} |
| 910 | >>> a = range(4) |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 911 | >>> a |
| 912 | [0, 1, 2, 3] |
Michael W. Hudson | 4da01ed | 2002-07-19 15:48:56 +0000 | [diff] [blame] | 913 | >>> a[::2] |
| 914 | [0, 2] |
| 915 | >>> del a[::2] |
| 916 | >>> a |
| 917 | [1, 3] |
| 918 | \end{verbatim} |
| 919 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 920 | One can also now pass slice objects to the |
| 921 | \method{__getitem__} methods of the built-in sequences: |
Michael W. Hudson | 4da01ed | 2002-07-19 15:48:56 +0000 | [diff] [blame] | 922 | |
| 923 | \begin{verbatim} |
| 924 | >>> range(10).__getitem__(slice(0, 5, 2)) |
| 925 | [0, 2, 4] |
| 926 | \end{verbatim} |
| 927 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 928 | Or use slice objects directly in subscripts: |
Michael W. Hudson | 4da01ed | 2002-07-19 15:48:56 +0000 | [diff] [blame] | 929 | |
| 930 | \begin{verbatim} |
| 931 | >>> range(10)[slice(0, 5, 2)] |
| 932 | [0, 2, 4] |
| 933 | \end{verbatim} |
| 934 | |
Andrew M. Kuchling | b6f7959 | 2002-11-29 19:43:45 +0000 | [diff] [blame] | 935 | To simplify implementing sequences that support extended slicing, |
| 936 | slice objects now have a method \method{indices(\var{length})} which, |
Fred Drake | aac8c58 | 2003-01-17 22:50:10 +0000 | [diff] [blame] | 937 | given the length of a sequence, returns a \code{(\var{start}, |
| 938 | \var{stop}, \var{step})} tuple that can be passed directly to |
| 939 | \function{range()}. |
Andrew M. Kuchling | b6f7959 | 2002-11-29 19:43:45 +0000 | [diff] [blame] | 940 | \method{indices()} handles omitted and out-of-bounds indices in a |
| 941 | manner consistent with regular slices (and this innocuous phrase hides |
| 942 | a welter of confusing details!). The method is intended to be used |
| 943 | like this: |
Michael W. Hudson | 4da01ed | 2002-07-19 15:48:56 +0000 | [diff] [blame] | 944 | |
| 945 | \begin{verbatim} |
| 946 | class FakeSeq: |
| 947 | ... |
| 948 | def calc_item(self, i): |
| 949 | ... |
| 950 | def __getitem__(self, item): |
| 951 | if isinstance(item, slice): |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 952 | indices = item.indices(len(self)) |
| 953 | return FakeSeq([self.calc_item(i) in range(*indices)]) |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 954 | else: |
Michael W. Hudson | 4da01ed | 2002-07-19 15:48:56 +0000 | [diff] [blame] | 955 | return self.calc_item(i) |
| 956 | \end{verbatim} |
| 957 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 958 | From this example you can also see that the built-in \class{slice} |
Andrew M. Kuchling | 90e9a79 | 2002-08-15 00:40:21 +0000 | [diff] [blame] | 959 | object is now the type object for the slice type, and is no longer a |
| 960 | function. This is consistent with Python 2.2, where \class{int}, |
| 961 | \class{str}, etc., underwent the same change. |
| 962 | |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 963 | |
Andrew M. Kuchling | 3a52ff6 | 2002-04-03 22:44:47 +0000 | [diff] [blame] | 964 | %====================================================================== |
Fred Drake | df872a2 | 2002-07-03 12:02:01 +0000 | [diff] [blame] | 965 | \section{Other Language Changes} |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 966 | |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 967 | Here are all of the changes that Python 2.3 makes to the core Python |
| 968 | language. |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 969 | |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 970 | \begin{itemize} |
| 971 | \item The \keyword{yield} statement is now always a keyword, as |
| 972 | described in section~\ref{section-generators} of this document. |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 973 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 974 | \item A new built-in function \function{enumerate()} |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 975 | was added, as described in section~\ref{section-enumerate} of this |
| 976 | document. |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 977 | |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 978 | \item Two new constants, \constant{True} and \constant{False} were |
| 979 | added along with the built-in \class{bool} type, as described in |
| 980 | section~\ref{section-bool} of this document. |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 981 | |
Andrew M. Kuchling | 495172c | 2002-11-20 13:50:15 +0000 | [diff] [blame] | 982 | \item The \function{int()} type constructor will now return a long |
| 983 | integer instead of raising an \exception{OverflowError} when a string |
| 984 | or floating-point number is too large to fit into an integer. This |
| 985 | can lead to the paradoxical result that |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 986 | \code{isinstance(int(\var{expression}), int)} is false, but that seems |
| 987 | unlikely to cause problems in practice. |
Andrew M. Kuchling | 495172c | 2002-11-20 13:50:15 +0000 | [diff] [blame] | 988 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 989 | \item Built-in types now support the extended slicing syntax, |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 990 | as described in section~\ref{section-slices} of this document. |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 991 | |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 992 | \item Dictionaries have a new method, \method{pop(\var{key})}, that |
| 993 | returns the value corresponding to \var{key} and removes that |
| 994 | key/value pair from the dictionary. \method{pop()} will raise a |
| 995 | \exception{KeyError} if the requested key isn't present in the |
| 996 | dictionary: |
| 997 | |
| 998 | \begin{verbatim} |
| 999 | >>> d = {1:2} |
| 1000 | >>> d |
| 1001 | {1: 2} |
| 1002 | >>> d.pop(4) |
| 1003 | Traceback (most recent call last): |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 1004 | File "stdin", line 1, in ? |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 1005 | KeyError: 4 |
| 1006 | >>> d.pop(1) |
| 1007 | 2 |
| 1008 | >>> d.pop(1) |
| 1009 | Traceback (most recent call last): |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 1010 | File "stdin", line 1, in ? |
Raymond Hettinger | ee1bded | 2003-01-17 16:20:23 +0000 | [diff] [blame] | 1011 | KeyError: 'pop(): dictionary is empty' |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 1012 | >>> d |
| 1013 | {} |
| 1014 | >>> |
| 1015 | \end{verbatim} |
| 1016 | |
Andrew M. Kuchling | b492fa9 | 2002-11-27 19:11:10 +0000 | [diff] [blame] | 1017 | There's also a new class method, |
| 1018 | \method{dict.fromkeys(\var{iterable}, \var{value})}, that |
| 1019 | creates a dictionary with keys taken from the supplied iterator |
| 1020 | \var{iterable} and all values set to \var{value}, defaulting to |
| 1021 | \code{None}. |
| 1022 | |
| 1023 | (Patches contributed by Raymond Hettinger.) |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 1024 | |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 1025 | Also, the \function{dict()} constructor now accepts keyword arguments to |
Raymond Hettinger | 45bda57 | 2002-12-14 20:20:45 +0000 | [diff] [blame] | 1026 | simplify creating small dictionaries: |
Andrew M. Kuchling | 449a87d | 2002-12-11 15:03:51 +0000 | [diff] [blame] | 1027 | |
| 1028 | \begin{verbatim} |
| 1029 | >>> dict(red=1, blue=2, green=3, black=4) |
| 1030 | {'blue': 2, 'black': 4, 'green': 3, 'red': 1} |
| 1031 | \end{verbatim} |
| 1032 | |
Andrew M. Kuchling | ae3bbf5 | 2002-12-31 14:03:45 +0000 | [diff] [blame] | 1033 | (Contributed by Just van~Rossum.) |
Andrew M. Kuchling | 449a87d | 2002-12-11 15:03:51 +0000 | [diff] [blame] | 1034 | |
Andrew M. Kuchling | 7a82b8c | 2002-11-04 20:17:24 +0000 | [diff] [blame] | 1035 | \item The \keyword{assert} statement no longer checks the \code{__debug__} |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1036 | flag, so you can no longer disable assertions by assigning to \code{__debug__}. |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1037 | Running Python with the \programopt{-O} switch will still generate |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1038 | code that doesn't execute any assertions. |
| 1039 | |
| 1040 | \item Most type objects are now callable, so you can use them |
| 1041 | to create new objects such as functions, classes, and modules. (This |
| 1042 | means that the \module{new} module can be deprecated in a future |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1043 | Python version, because you can now use the type objects available in |
| 1044 | the \module{types} module.) |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1045 | % XXX should new.py use PendingDeprecationWarning? |
| 1046 | For example, you can create a new module object with the following code: |
| 1047 | |
| 1048 | \begin{verbatim} |
| 1049 | >>> import types |
| 1050 | >>> m = types.ModuleType('abc','docstring') |
| 1051 | >>> m |
| 1052 | <module 'abc' (built-in)> |
| 1053 | >>> m.__doc__ |
| 1054 | 'docstring' |
| 1055 | \end{verbatim} |
| 1056 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1057 | \item |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1058 | A new warning, \exception{PendingDeprecationWarning} was added to |
| 1059 | indicate features which are in the process of being |
| 1060 | deprecated. The warning will \emph{not} be printed by default. To |
| 1061 | check for use of features that will be deprecated in the future, |
| 1062 | supply \programopt{-Walways::PendingDeprecationWarning::} on the |
| 1063 | command line or use \function{warnings.filterwarnings()}. |
| 1064 | |
Andrew M. Kuchling | c1dd174 | 2003-01-13 13:59:22 +0000 | [diff] [blame] | 1065 | \item The process of deprecating string-based exceptions, as |
| 1066 | in \code{raise "Error occurred"}, has begun. Raising a string will |
| 1067 | now trigger \exception{PendingDeprecationWarning}. |
| 1068 | |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1069 | \item Using \code{None} as a variable name will now result in a |
| 1070 | \exception{SyntaxWarning} warning. In a future version of Python, |
| 1071 | \code{None} may finally become a keyword. |
| 1072 | |
Andrew M. Kuchling | b60ea3f | 2002-11-15 14:37:10 +0000 | [diff] [blame] | 1073 | \item The method resolution order used by new-style classes has |
| 1074 | changed, though you'll only notice the difference if you have a really |
| 1075 | complicated inheritance hierarchy. (Classic classes are unaffected by |
| 1076 | this change.) Python 2.2 originally used a topological sort of a |
| 1077 | class's ancestors, but 2.3 now uses the C3 algorithm as described in |
Andrew M. Kuchling | 6f429c3 | 2002-11-19 13:09:00 +0000 | [diff] [blame] | 1078 | the paper \ulink{``A Monotonic Superclass Linearization for |
| 1079 | Dylan''}{http://www.webcom.com/haahr/dylan/linearization-oopsla96.html}. |
Andrew M. Kuchling | c1dd174 | 2003-01-13 13:59:22 +0000 | [diff] [blame] | 1080 | To understand the motivation for this change, |
| 1081 | read Michele Simionato's article |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 1082 | \ulink{``Python 2.3 Method Resolution Order''} |
Andrew M. Kuchling | b8a3905 | 2003-02-07 20:22:33 +0000 | [diff] [blame] | 1083 | {http://www.python.org/2.3/mro.html}, or |
Andrew M. Kuchling | c1dd174 | 2003-01-13 13:59:22 +0000 | [diff] [blame] | 1084 | read the thread on python-dev starting with the message at |
Andrew M. Kuchling | b60ea3f | 2002-11-15 14:37:10 +0000 | [diff] [blame] | 1085 | \url{http://mail.python.org/pipermail/python-dev/2002-October/029035.html}. |
| 1086 | Samuele Pedroni first pointed out the problem and also implemented the |
| 1087 | fix by coding the C3 algorithm. |
| 1088 | |
Andrew M. Kuchling | dcfd825 | 2002-09-13 22:21:42 +0000 | [diff] [blame] | 1089 | \item Python runs multithreaded programs by switching between threads |
| 1090 | after executing N bytecodes. The default value for N has been |
| 1091 | increased from 10 to 100 bytecodes, speeding up single-threaded |
| 1092 | applications by reducing the switching overhead. Some multithreaded |
| 1093 | applications may suffer slower response time, but that's easily fixed |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1094 | by setting the limit back to a lower number using |
Andrew M. Kuchling | dcfd825 | 2002-09-13 22:21:42 +0000 | [diff] [blame] | 1095 | \function{sys.setcheckinterval(\var{N})}. |
| 1096 | |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1097 | \item One minor but far-reaching change is that the names of extension |
| 1098 | types defined by the modules included with Python now contain the |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1099 | module and a \character{.} in front of the type name. For example, in |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1100 | Python 2.2, if you created a socket and printed its |
| 1101 | \member{__class__}, you'd get this output: |
| 1102 | |
| 1103 | \begin{verbatim} |
| 1104 | >>> s = socket.socket() |
| 1105 | >>> s.__class__ |
| 1106 | <type 'socket'> |
| 1107 | \end{verbatim} |
| 1108 | |
| 1109 | In 2.3, you get this: |
| 1110 | \begin{verbatim} |
| 1111 | >>> s.__class__ |
| 1112 | <type '_socket.socket'> |
| 1113 | \end{verbatim} |
| 1114 | |
Michael W. Hudson | 96bc3b4 | 2002-11-26 14:48:23 +0000 | [diff] [blame] | 1115 | \item One of the noted incompatibilities between old- and new-style |
| 1116 | classes has been removed: you can now assign to the |
| 1117 | \member{__name__} and \member{__bases__} attributes of new-style |
| 1118 | classes. There are some restrictions on what can be assigned to |
| 1119 | \member{__bases__} along the lines of those relating to assigning to |
| 1120 | an instance's \member{__class__} attribute. |
| 1121 | |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1122 | \end{itemize} |
| 1123 | |
| 1124 | |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 1125 | %====================================================================== |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1126 | \subsection{String Changes} |
| 1127 | |
| 1128 | \begin{itemize} |
| 1129 | |
Fred Drake | aac8c58 | 2003-01-17 22:50:10 +0000 | [diff] [blame] | 1130 | \item The \keyword{in} operator now works differently for strings. |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1131 | Previously, when evaluating \code{\var{X} in \var{Y}} where \var{X} |
| 1132 | and \var{Y} are strings, \var{X} could only be a single character. |
| 1133 | That's now changed; \var{X} can be a string of any length, and |
| 1134 | \code{\var{X} in \var{Y}} will return \constant{True} if \var{X} is a |
| 1135 | substring of \var{Y}. If \var{X} is the empty string, the result is |
| 1136 | always \constant{True}. |
| 1137 | |
| 1138 | \begin{verbatim} |
| 1139 | >>> 'ab' in 'abcd' |
| 1140 | True |
| 1141 | >>> 'ad' in 'abcd' |
| 1142 | False |
| 1143 | >>> '' in 'abcd' |
| 1144 | True |
| 1145 | \end{verbatim} |
| 1146 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1147 | Note that this doesn't tell you where the substring starts; if you |
| 1148 | need that information, you must use the \method{find()} method |
| 1149 | instead. |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1150 | |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 1151 | \item The \method{strip()}, \method{lstrip()}, and \method{rstrip()} |
| 1152 | string methods now have an optional argument for specifying the |
| 1153 | characters to strip. The default is still to remove all whitespace |
| 1154 | characters: |
| 1155 | |
| 1156 | \begin{verbatim} |
| 1157 | >>> ' abc '.strip() |
| 1158 | 'abc' |
| 1159 | >>> '><><abc<><><>'.strip('<>') |
| 1160 | 'abc' |
| 1161 | >>> '><><abc<><><>\n'.strip('<>') |
| 1162 | 'abc<><><>\n' |
| 1163 | >>> u'\u4000\u4001abc\u4000'.strip(u'\u4000') |
| 1164 | u'\u4001abc' |
| 1165 | >>> |
| 1166 | \end{verbatim} |
| 1167 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1168 | (Suggested by Simon Brunning and implemented by Walter D\"orwald.) |
Andrew M. Kuchling | 346386f | 2002-07-12 20:24:42 +0000 | [diff] [blame] | 1169 | |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 1170 | \item The \method{startswith()} and \method{endswith()} |
| 1171 | string methods now accept negative numbers for the start and end |
| 1172 | parameters. |
| 1173 | |
| 1174 | \item Another new string method is \method{zfill()}, originally a |
| 1175 | function in the \module{string} module. \method{zfill()} pads a |
| 1176 | numeric string with zeros on the left until it's the specified width. |
| 1177 | Note that the \code{\%} operator is still more flexible and powerful |
| 1178 | than \method{zfill()}. |
| 1179 | |
| 1180 | \begin{verbatim} |
| 1181 | >>> '45'.zfill(4) |
| 1182 | '0045' |
| 1183 | >>> '12345'.zfill(4) |
| 1184 | '12345' |
| 1185 | >>> 'goofy'.zfill(6) |
| 1186 | '0goofy' |
| 1187 | \end{verbatim} |
| 1188 | |
Andrew M. Kuchling | 346386f | 2002-07-12 20:24:42 +0000 | [diff] [blame] | 1189 | (Contributed by Walter D\"orwald.) |
| 1190 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1191 | \item A new type object, \class{basestring}, has been added. |
Andrew M. Kuchling | 20e5abc | 2002-07-11 20:50:34 +0000 | [diff] [blame] | 1192 | Both 8-bit strings and Unicode strings inherit from this type, so |
| 1193 | \code{isinstance(obj, basestring)} will return \constant{True} for |
| 1194 | either kind of string. It's a completely abstract type, so you |
| 1195 | can't create \class{basestring} instances. |
| 1196 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1197 | \item Interned strings are no longer immortal, and will now be |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1198 | garbage-collected in the usual way when the only reference to them is |
| 1199 | from the internal dictionary of interned strings. (Implemented by |
| 1200 | Oren Tirosh.) |
| 1201 | |
| 1202 | \end{itemize} |
| 1203 | |
| 1204 | |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 1205 | %====================================================================== |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1206 | \subsection{Optimizations} |
| 1207 | |
| 1208 | \begin{itemize} |
| 1209 | |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 1210 | \item The creation of new-style class instances has been made much |
| 1211 | faster; they're now faster than classic classes! |
| 1212 | |
Andrew M. Kuchling | 950725f | 2002-08-06 01:40:48 +0000 | [diff] [blame] | 1213 | \item The \method{sort()} method of list objects has been extensively |
| 1214 | rewritten by Tim Peters, and the implementation is significantly |
| 1215 | faster. |
| 1216 | |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1217 | \item Multiplication of large long integers is now much faster thanks |
| 1218 | to an implementation of Karatsuba multiplication, an algorithm that |
| 1219 | scales better than the O(n*n) required for the grade-school |
| 1220 | multiplication algorithm. (Original patch by Christopher A. Craig, |
| 1221 | and significantly reworked by Tim Peters.) |
Andrew M. Kuchling | 20e5abc | 2002-07-11 20:50:34 +0000 | [diff] [blame] | 1222 | |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1223 | \item The \code{SET_LINENO} opcode is now gone. This may provide a |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1224 | small speed increase, depending on your compiler's idiosyncrasies. |
| 1225 | See section~\ref{section-other} for a longer explanation. |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1226 | (Removed by Michael Hudson.) |
Andrew M. Kuchling | 20e5abc | 2002-07-11 20:50:34 +0000 | [diff] [blame] | 1227 | |
Andrew M. Kuchling | 449a87d | 2002-12-11 15:03:51 +0000 | [diff] [blame] | 1228 | \item \function{xrange()} objects now have their own iterator, making |
| 1229 | \code{for i in xrange(n)} slightly faster than |
| 1230 | \code{for i in range(n)}. (Patch by Raymond Hettinger.) |
| 1231 | |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1232 | \item A number of small rearrangements have been made in various |
| 1233 | hotspots to improve performance, inlining a function here, removing |
| 1234 | some code there. (Implemented mostly by GvR, but lots of people have |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1235 | contributed single changes.) |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 1236 | |
| 1237 | \end{itemize} |
Neal Norwitz | d68f517 | 2002-05-29 15:54:55 +0000 | [diff] [blame] | 1238 | |
Andrew M. Kuchling | 6974aa9 | 2002-08-20 00:54:36 +0000 | [diff] [blame] | 1239 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 1240 | %====================================================================== |
Andrew M. Kuchling | ef893fe | 2003-01-06 20:04:17 +0000 | [diff] [blame] | 1241 | \section{New, Improved, and Deprecated Modules} |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 1242 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1243 | As usual, Python's standard library received a number of enhancements and |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1244 | bug fixes. Here's a partial list of the most notable changes, sorted |
| 1245 | alphabetically by module name. Consult the |
| 1246 | \file{Misc/NEWS} file in the source tree for a more |
| 1247 | complete list of changes, or look through the CVS logs for all the |
| 1248 | details. |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 1249 | |
| 1250 | \begin{itemize} |
| 1251 | |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1252 | \item The \module{array} module now supports arrays of Unicode |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1253 | characters using the \character{u} format character. Arrays also now |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1254 | support using the \code{+=} assignment operator to add another array's |
| 1255 | contents, and the \code{*=} assignment operator to repeat an array. |
| 1256 | (Contributed by Jason Orendorff.) |
| 1257 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1258 | \item The \module{bsddb} module has been replaced by version 4.1.1 |
Andrew M. Kuchling | 669249e | 2002-11-19 13:05:33 +0000 | [diff] [blame] | 1259 | of the \ulink{PyBSDDB}{http://pybsddb.sourceforge.net} package, |
| 1260 | providing a more complete interface to the transactional features of |
| 1261 | the BerkeleyDB library. |
| 1262 | The old version of the module has been renamed to |
| 1263 | \module{bsddb185} and is no longer built automatically; you'll |
| 1264 | have to edit \file{Modules/Setup} to enable it. Note that the new |
| 1265 | \module{bsddb} package is intended to be compatible with the |
| 1266 | old module, so be sure to file bugs if you discover any |
| 1267 | incompatibilities. |
| 1268 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1269 | \item The Distutils \class{Extension} class now supports |
| 1270 | an extra constructor argument named \var{depends} for listing |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1271 | additional source files that an extension depends on. This lets |
| 1272 | Distutils recompile the module if any of the dependency files are |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1273 | modified. For example, if \file{sampmodule.c} includes the header |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1274 | file \file{sample.h}, you would create the \class{Extension} object like |
| 1275 | this: |
| 1276 | |
| 1277 | \begin{verbatim} |
| 1278 | ext = Extension("samp", |
| 1279 | sources=["sampmodule.c"], |
| 1280 | depends=["sample.h"]) |
| 1281 | \end{verbatim} |
| 1282 | |
| 1283 | Modifying \file{sample.h} would then cause the module to be recompiled. |
| 1284 | (Contributed by Jeremy Hylton.) |
| 1285 | |
Andrew M. Kuchling | dc3f7e1 | 2002-11-04 20:05:10 +0000 | [diff] [blame] | 1286 | \item Other minor changes to Distutils: |
| 1287 | it now checks for the \envvar{CC}, \envvar{CFLAGS}, \envvar{CPP}, |
| 1288 | \envvar{LDFLAGS}, and \envvar{CPPFLAGS} environment variables, using |
| 1289 | them to override the settings in Python's configuration (contributed |
Andrew M. Kuchling | a31bb37 | 2003-01-27 16:36:34 +0000 | [diff] [blame] | 1290 | by Robert Weber). |
Andrew M. Kuchling | dc3f7e1 | 2002-11-04 20:05:10 +0000 | [diff] [blame] | 1291 | |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1292 | \item The \module{getopt} module gained a new function, |
| 1293 | \function{gnu_getopt()}, that supports the same arguments as the existing |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1294 | \function{getopt()} function but uses GNU-style scanning mode. |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1295 | The existing \function{getopt()} stops processing options as soon as a |
| 1296 | non-option argument is encountered, but in GNU-style mode processing |
| 1297 | continues, meaning that options and arguments can be mixed. For |
| 1298 | example: |
| 1299 | |
| 1300 | \begin{verbatim} |
| 1301 | >>> getopt.getopt(['-f', 'filename', 'output', '-v'], 'f:v') |
| 1302 | ([('-f', 'filename')], ['output', '-v']) |
| 1303 | >>> getopt.gnu_getopt(['-f', 'filename', 'output', '-v'], 'f:v') |
| 1304 | ([('-f', 'filename'), ('-v', '')], ['output']) |
| 1305 | \end{verbatim} |
| 1306 | |
| 1307 | (Contributed by Peter \AA{strand}.) |
| 1308 | |
| 1309 | \item The \module{grp}, \module{pwd}, and \module{resource} modules |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1310 | now return enhanced tuples: |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1311 | |
| 1312 | \begin{verbatim} |
| 1313 | >>> import grp |
| 1314 | >>> g = grp.getgrnam('amk') |
| 1315 | >>> g.gr_name, g.gr_gid |
| 1316 | ('amk', 500) |
| 1317 | \end{verbatim} |
| 1318 | |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 1319 | \item The \module{gzip} module can now handle files exceeding 2~Gb. |
| 1320 | |
Andrew M. Kuchling | 950725f | 2002-08-06 01:40:48 +0000 | [diff] [blame] | 1321 | \item The new \module{heapq} module contains an implementation of a |
| 1322 | heap queue algorithm. A heap is an array-like data structure that |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1323 | keeps items in a partially sorted order such that, for every index |
| 1324 | \var{k}, \code{heap[\var{k}] <= heap[2*\var{k}+1]} and |
| 1325 | \code{heap[\var{k}] <= heap[2*\var{k}+2]}. This makes it quick to |
| 1326 | remove the smallest item, and inserting a new item while maintaining |
| 1327 | the heap property is O(lg~n). (See |
Andrew M. Kuchling | 950725f | 2002-08-06 01:40:48 +0000 | [diff] [blame] | 1328 | \url{http://www.nist.gov/dads/HTML/priorityque.html} for more |
| 1329 | information about the priority queue data structure.) |
| 1330 | |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 1331 | The \module{heapq} module provides \function{heappush()} and |
Andrew M. Kuchling | 950725f | 2002-08-06 01:40:48 +0000 | [diff] [blame] | 1332 | \function{heappop()} functions for adding and removing items while |
| 1333 | maintaining the heap property on top of some other mutable Python |
| 1334 | sequence type. For example: |
| 1335 | |
| 1336 | \begin{verbatim} |
| 1337 | >>> import heapq |
| 1338 | >>> heap = [] |
| 1339 | >>> for item in [3, 7, 5, 11, 1]: |
| 1340 | ... heapq.heappush(heap, item) |
| 1341 | ... |
| 1342 | >>> heap |
| 1343 | [1, 3, 5, 11, 7] |
| 1344 | >>> heapq.heappop(heap) |
| 1345 | 1 |
| 1346 | >>> heapq.heappop(heap) |
| 1347 | 3 |
| 1348 | >>> heap |
| 1349 | [5, 7, 11] |
Andrew M. Kuchling | 950725f | 2002-08-06 01:40:48 +0000 | [diff] [blame] | 1350 | \end{verbatim} |
| 1351 | |
| 1352 | (Contributed by Kevin O'Connor.) |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1353 | |
Andrew M. Kuchling | 87cebbf | 2003-01-03 16:24:28 +0000 | [diff] [blame] | 1354 | \item The \module{imaplib} module now supports IMAP over SSL. |
| 1355 | (Contributed by Piers Lauder and Tino Lange.) |
| 1356 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1357 | \item Two new functions in the \module{math} module, |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1358 | \function{degrees(\var{rads})} and \function{radians(\var{degs})}, |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1359 | convert between radians and degrees. Other functions in the |
Andrew M. Kuchling | 8e5b53b | 2002-12-15 20:17:38 +0000 | [diff] [blame] | 1360 | \module{math} module such as \function{math.sin()} and |
| 1361 | \function{math.cos()} have always required input values measured in |
| 1362 | radians. Also, an optional \var{base} argument was added to |
| 1363 | \function{math.log()} to make it easier to compute logarithms for |
| 1364 | bases other than \code{e} and \code{10}. (Contributed by Raymond |
| 1365 | Hettinger.) |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1366 | |
Andrew M. Kuchling | ae3bbf5 | 2002-12-31 14:03:45 +0000 | [diff] [blame] | 1367 | \item Several new functions (\function{getpgid()}, \function{killpg()}, |
| 1368 | \function{lchown()}, \function{loadavg()}, \function{major()}, \function{makedev()}, |
| 1369 | \function{minor()}, and \function{mknod()}) were added to the |
Andrew M. Kuchling | c309cca | 2002-10-10 16:04:08 +0000 | [diff] [blame] | 1370 | \module{posix} module that underlies the \module{os} module. |
Andrew M. Kuchling | ae3bbf5 | 2002-12-31 14:03:45 +0000 | [diff] [blame] | 1371 | (Contributed by Gustavo Niemeyer, Geert Jansen, and Denis S. Otkidach.) |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1372 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1373 | \item In the \module{os} module, the \function{*stat()} family of functions can now report |
| 1374 | fractions of a second in a timestamp. Such time stamps are |
| 1375 | represented as floats, similar to \function{time.time()}. |
| 1376 | |
| 1377 | During testing, it was found that some applications will break if time |
| 1378 | stamps are floats. For compatibility, when using the tuple interface |
| 1379 | of the \class{stat_result} time stamps will be represented as integers. |
| 1380 | When using named fields (a feature first introduced in Python 2.2), |
| 1381 | time stamps are still represented as integers, unless |
| 1382 | \function{os.stat_float_times()} is invoked to enable float return |
| 1383 | values: |
| 1384 | |
| 1385 | \begin{verbatim} |
| 1386 | >>> os.stat("/tmp").st_mtime |
| 1387 | 1034791200 |
| 1388 | >>> os.stat_float_times(True) |
| 1389 | >>> os.stat("/tmp").st_mtime |
| 1390 | 1034791200.6335014 |
| 1391 | \end{verbatim} |
| 1392 | |
| 1393 | In Python 2.4, the default will change to always returning floats. |
| 1394 | |
| 1395 | Application developers should enable this feature only if all their |
| 1396 | libraries work properly when confronted with floating point time |
| 1397 | stamps, or if they use the tuple API. If used, the feature should be |
| 1398 | activated on an application level instead of trying to enable it on a |
| 1399 | per-use basis. |
| 1400 | |
Andrew M. Kuchling | 5326257 | 2002-12-01 14:00:21 +0000 | [diff] [blame] | 1401 | \item The old and never-documented \module{linuxaudiodev} module has |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1402 | been deprecated, and a new version named \module{ossaudiodev} has been |
| 1403 | added. The module was renamed because the OSS sound drivers can be |
| 1404 | used on platforms other than Linux, and the interface has also been |
| 1405 | tidied and brought up to date in various ways. (Contributed by Greg |
Greg Ward | aa1d3aa | 2003-01-03 18:03:21 +0000 | [diff] [blame] | 1406 | Ward and Nicholas FitzRoy-Dale.) |
Andrew M. Kuchling | 5326257 | 2002-12-01 14:00:21 +0000 | [diff] [blame] | 1407 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1408 | \item The parser objects provided by the \module{pyexpat} module |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1409 | can now optionally buffer character data, resulting in fewer calls to |
| 1410 | your character data handler and therefore faster performance. Setting |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1411 | the parser object's \member{buffer_text} attribute to \constant{True} |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1412 | will enable buffering. |
| 1413 | |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 1414 | \item The \function{sample(\var{population}, \var{k})} function was |
| 1415 | added to the \module{random} module. \var{population} is a sequence |
Fred Drake | aac8c58 | 2003-01-17 22:50:10 +0000 | [diff] [blame] | 1416 | or \class{xrange} object containing the elements of a population, and |
| 1417 | \function{sample()} |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 1418 | chooses \var{k} elements from the population without replacing chosen |
| 1419 | elements. \var{k} can be any value up to \code{len(\var{population})}. |
| 1420 | For example: |
| 1421 | |
| 1422 | \begin{verbatim} |
Andrew M. Kuchling | 449a87d | 2002-12-11 15:03:51 +0000 | [diff] [blame] | 1423 | >>> days = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'St', 'Sn'] |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1424 | >>> random.sample(days, 3) # Choose 3 elements |
Andrew M. Kuchling | 449a87d | 2002-12-11 15:03:51 +0000 | [diff] [blame] | 1425 | ['St', 'Sn', 'Th'] |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1426 | >>> random.sample(days, 7) # Choose 7 elements |
Andrew M. Kuchling | 449a87d | 2002-12-11 15:03:51 +0000 | [diff] [blame] | 1427 | ['Tu', 'Th', 'Mo', 'We', 'St', 'Fr', 'Sn'] |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1428 | >>> random.sample(days, 7) # Choose 7 again |
Andrew M. Kuchling | 449a87d | 2002-12-11 15:03:51 +0000 | [diff] [blame] | 1429 | ['We', 'Mo', 'Sn', 'Fr', 'Tu', 'St', 'Th'] |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1430 | >>> random.sample(days, 8) # Can't choose eight |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 1431 | Traceback (most recent call last): |
Andrew M. Kuchling | 28f2f88 | 2002-11-14 14:14:16 +0000 | [diff] [blame] | 1432 | File "<stdin>", line 1, in ? |
Andrew M. Kuchling | 449a87d | 2002-12-11 15:03:51 +0000 | [diff] [blame] | 1433 | File "random.py", line 414, in sample |
| 1434 | raise ValueError, "sample larger than population" |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 1435 | ValueError: sample larger than population |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1436 | >>> random.sample(xrange(1,10000,2), 10) # Choose ten odd nos. under 10000 |
Andrew M. Kuchling | 449a87d | 2002-12-11 15:03:51 +0000 | [diff] [blame] | 1437 | [3407, 3805, 1505, 7023, 2401, 2267, 9733, 3151, 8083, 9195] |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 1438 | \end{verbatim} |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 1439 | |
| 1440 | The \module{random} module now uses a new algorithm, the Mersenne |
| 1441 | Twister, implemented in C. It's faster and more extensively studied |
| 1442 | than the previous algorithm. |
| 1443 | |
| 1444 | (All changes contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 1445 | |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1446 | \item The \module{readline} module also gained a number of new |
| 1447 | functions: \function{get_history_item()}, |
| 1448 | \function{get_current_history_length()}, and \function{redisplay()}. |
| 1449 | |
Andrew M. Kuchling | ef893fe | 2003-01-06 20:04:17 +0000 | [diff] [blame] | 1450 | \item The \module{rexec} and \module{Bastion} modules have been |
| 1451 | declared dead, and attempts to import them will fail with a |
| 1452 | \exception{RuntimeError}. New-style classes provide new ways to break |
| 1453 | out of the restricted execution environment provided by |
| 1454 | \module{rexec}, and no one has interest in fixing them or time to do |
| 1455 | so. If you have applications using \module{rexec}, rewrite them to |
| 1456 | use something else. |
| 1457 | |
| 1458 | (Sticking with Python 2.2 or 2.1 will not make your applications any |
| 1459 | safer, because there are known bugs in the \module{rexec} module in |
| 1460 | those versions. I repeat, if you're using \module{rexec}, stop using |
| 1461 | it immediately.) |
| 1462 | |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 1463 | \item The \module{shutil} module gained a \function{move(\var{src}, |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1464 | \var{dest})} function that recursively moves a file or directory to a new |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 1465 | location. |
| 1466 | |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1467 | \item Support for more advanced POSIX signal handling was added |
| 1468 | to the \module{signal} module by adding the \function{sigpending}, |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1469 | \function{sigprocmask} and \function{sigsuspend} functions where supported |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1470 | by the platform. These functions make it possible to avoid some previously |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1471 | unavoidable race conditions with signal handling. |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1472 | |
| 1473 | \item The \module{socket} module now supports timeouts. You |
| 1474 | can call the \method{settimeout(\var{t})} method on a socket object to |
| 1475 | set a timeout of \var{t} seconds. Subsequent socket operations that |
| 1476 | take longer than \var{t} seconds to complete will abort and raise a |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1477 | \exception{socket.error} exception. |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1478 | |
| 1479 | The original timeout implementation was by Tim O'Malley. Michael |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1480 | Gilfix integrated it into the Python \module{socket} module and |
| 1481 | shepherded it through a lengthy review. After the code was checked |
| 1482 | in, Guido van~Rossum rewrote parts of it. (This is a good example of |
| 1483 | a collaborative development process in action.) |
Andrew M. Kuchling | a982eb1 | 2002-07-22 18:57:36 +0000 | [diff] [blame] | 1484 | |
Mark Hammond | 8af50bc | 2002-12-03 06:13:35 +0000 | [diff] [blame] | 1485 | \item On Windows, the \module{socket} module now ships with Secure |
Michael W. Hudson | 065f5fa | 2003-02-10 19:24:50 +0000 | [diff] [blame] | 1486 | Sockets Layer (SSL) support. |
Mark Hammond | 8af50bc | 2002-12-03 06:13:35 +0000 | [diff] [blame] | 1487 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1488 | \item The value of the C \constant{PYTHON_API_VERSION} macro is now exposed |
Fred Drake | 583db0d | 2002-09-14 02:03:25 +0000 | [diff] [blame] | 1489 | at the Python level as \code{sys.api_version}. |
Andrew M. Kuchling | dcfd825 | 2002-09-13 22:21:42 +0000 | [diff] [blame] | 1490 | |
Andrew M. Kuchling | 674b0bf | 2003-01-07 00:07:19 +0000 | [diff] [blame] | 1491 | \item The new \module{tarfile} module |
Neal Norwitz | 55d555f | 2003-01-08 05:27:42 +0000 | [diff] [blame] | 1492 | allows reading from and writing to \program{tar}-format archive files. |
Andrew M. Kuchling | 674b0bf | 2003-01-07 00:07:19 +0000 | [diff] [blame] | 1493 | (Contributed by Lars Gust\"abel.) |
| 1494 | |
Andrew M. Kuchling | 20e5abc | 2002-07-11 20:50:34 +0000 | [diff] [blame] | 1495 | \item The new \module{textwrap} module contains functions for wrapping |
Andrew M. Kuchling | d003a2a | 2002-06-26 13:23:55 +0000 | [diff] [blame] | 1496 | strings containing paragraphs of text. The \function{wrap(\var{text}, |
| 1497 | \var{width})} function takes a string and returns a list containing |
| 1498 | the text split into lines of no more than the chosen width. The |
| 1499 | \function{fill(\var{text}, \var{width})} function returns a single |
| 1500 | string, reformatted to fit into lines no longer than the chosen width. |
| 1501 | (As you can guess, \function{fill()} is built on top of |
| 1502 | \function{wrap()}. For example: |
| 1503 | |
| 1504 | \begin{verbatim} |
| 1505 | >>> import textwrap |
| 1506 | >>> paragraph = "Not a whit, we defy augury: ... more text ..." |
| 1507 | >>> textwrap.wrap(paragraph, 60) |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1508 | ["Not a whit, we defy augury: there's a special providence in", |
| 1509 | "the fall of a sparrow. If it be now, 'tis not to come; if it", |
Andrew M. Kuchling | d003a2a | 2002-06-26 13:23:55 +0000 | [diff] [blame] | 1510 | ...] |
| 1511 | >>> print textwrap.fill(paragraph, 35) |
| 1512 | Not a whit, we defy augury: there's |
| 1513 | a special providence in the fall of |
| 1514 | a sparrow. If it be now, 'tis not |
| 1515 | to come; if it be not to come, it |
| 1516 | will be now; if it be not now, yet |
| 1517 | it will come: the readiness is all. |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1518 | >>> |
Andrew M. Kuchling | d003a2a | 2002-06-26 13:23:55 +0000 | [diff] [blame] | 1519 | \end{verbatim} |
| 1520 | |
| 1521 | The module also contains a \class{TextWrapper} class that actually |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1522 | implements the text wrapping strategy. Both the |
Andrew M. Kuchling | d003a2a | 2002-06-26 13:23:55 +0000 | [diff] [blame] | 1523 | \class{TextWrapper} class and the \function{wrap()} and |
| 1524 | \function{fill()} functions support a number of additional keyword |
| 1525 | arguments for fine-tuning the formatting; consult the module's |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1526 | documentation for details. |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 1527 | %XXX add a link to the module docs? |
Andrew M. Kuchling | d003a2a | 2002-06-26 13:23:55 +0000 | [diff] [blame] | 1528 | (Contributed by Greg Ward.) |
| 1529 | |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 1530 | \item The \module{thread} and \module{threading} modules now have |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1531 | companion modules, \module{dummy_thread} and \module{dummy_threading}, |
| 1532 | that provide a do-nothing implementation of the \module{thread} |
| 1533 | module's interface for platforms where threads are not supported. The |
| 1534 | intention is to simplify thread-aware modules (ones that \emph{don't} |
| 1535 | rely on threads to run) by putting the following code at the top: |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 1536 | |
| 1537 | % XXX why as _threading? |
| 1538 | \begin{verbatim} |
| 1539 | try: |
| 1540 | import threading as _threading |
| 1541 | except ImportError: |
| 1542 | import dummy_threading as _threading |
| 1543 | \end{verbatim} |
| 1544 | |
| 1545 | Code can then call functions and use classes in \module{_threading} |
| 1546 | whether or not threads are supported, avoiding an \keyword{if} |
| 1547 | statement and making the code slightly clearer. This module will not |
| 1548 | magically make multithreaded code run without threads; code that waits |
| 1549 | for another thread to return or to do something will simply hang |
| 1550 | forever. |
| 1551 | |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1552 | \item The \module{time} module's \function{strptime()} function has |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1553 | long been an annoyance because it uses the platform C library's |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1554 | \function{strptime()} implementation, and different platforms |
| 1555 | sometimes have odd bugs. Brett Cannon contributed a portable |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1556 | implementation that's written in pure Python and should behave |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1557 | identically on all platforms. |
| 1558 | |
Andrew M. Kuchling | 6c50df2 | 2002-12-13 12:53:16 +0000 | [diff] [blame] | 1559 | \item The \module{UserDict} module has a new \class{DictMixin} class which |
Andrew M. Kuchling | 449a87d | 2002-12-11 15:03:51 +0000 | [diff] [blame] | 1560 | defines all dictionary methods for classes that already have a minimum |
| 1561 | mapping interface. This greatly simplifies writing classes that need |
| 1562 | to be substitutable for dictionaries, such as the classes in |
| 1563 | the \module{shelve} module. |
| 1564 | |
| 1565 | Adding the mixin as a superclass provides the full dictionary |
| 1566 | interface whenever the class defines \method{__getitem__}, |
Andrew M. Kuchling | 6c50df2 | 2002-12-13 12:53:16 +0000 | [diff] [blame] | 1567 | \method{__setitem__}, \method{__delitem__}, and \method{keys}. |
Andrew M. Kuchling | 449a87d | 2002-12-11 15:03:51 +0000 | [diff] [blame] | 1568 | For example: |
| 1569 | |
| 1570 | \begin{verbatim} |
| 1571 | >>> import UserDict |
| 1572 | >>> class SeqDict(UserDict.DictMixin): |
| 1573 | """Dictionary lookalike implemented with lists.""" |
| 1574 | def __init__(self): |
| 1575 | self.keylist = [] |
| 1576 | self.valuelist = [] |
| 1577 | def __getitem__(self, key): |
| 1578 | try: |
| 1579 | i = self.keylist.index(key) |
| 1580 | except ValueError: |
| 1581 | raise KeyError |
| 1582 | return self.valuelist[i] |
| 1583 | def __setitem__(self, key, value): |
| 1584 | try: |
| 1585 | i = self.keylist.index(key) |
| 1586 | self.valuelist[i] = value |
| 1587 | except ValueError: |
| 1588 | self.keylist.append(key) |
| 1589 | self.valuelist.append(value) |
| 1590 | def __delitem__(self, key): |
| 1591 | try: |
| 1592 | i = self.keylist.index(key) |
| 1593 | except ValueError: |
| 1594 | raise KeyError |
| 1595 | self.keylist.pop(i) |
| 1596 | self.valuelist.pop(i) |
| 1597 | def keys(self): |
| 1598 | return list(self.keylist) |
| 1599 | |
| 1600 | >>> s = SeqDict() |
| 1601 | >>> dir(s) # See that other dictionary methods are implemented |
| 1602 | ['__cmp__', '__contains__', '__delitem__', '__doc__', '__getitem__', |
| 1603 | '__init__', '__iter__', '__len__', '__module__', '__repr__', |
| 1604 | '__setitem__', 'clear', 'get', 'has_key', 'items', 'iteritems', |
| 1605 | 'iterkeys', 'itervalues', 'keylist', 'keys', 'pop', 'popitem', |
| 1606 | 'setdefault', 'update', 'valuelist', 'values'] |
Neal Norwitz | c7d8c68 | 2002-12-24 14:51:43 +0000 | [diff] [blame] | 1607 | \end{verbatim} |
Andrew M. Kuchling | 449a87d | 2002-12-11 15:03:51 +0000 | [diff] [blame] | 1608 | |
| 1609 | (Contributed by Raymond Hettinger.) |
| 1610 | |
Andrew M. Kuchling | 20e5abc | 2002-07-11 20:50:34 +0000 | [diff] [blame] | 1611 | \item The DOM implementation |
| 1612 | in \module{xml.dom.minidom} can now generate XML output in a |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1613 | particular encoding by providing an optional encoding argument to |
Andrew M. Kuchling | 20e5abc | 2002-07-11 20:50:34 +0000 | [diff] [blame] | 1614 | the \method{toxml()} and \method{toprettyxml()} methods of DOM nodes. |
| 1615 | |
Andrew M. Kuchling | ef893fe | 2003-01-06 20:04:17 +0000 | [diff] [blame] | 1616 | item The \module{Tix} module has received various bug fixes and |
| 1617 | updates for the current version of the Tix package. |
| 1618 | |
Andrew M. Kuchling | 6c50df2 | 2002-12-13 12:53:16 +0000 | [diff] [blame] | 1619 | \item The \module{Tkinter} module now works with a thread-enabled |
| 1620 | version of Tcl. Tcl's threading model requires that widgets only be |
| 1621 | accessed from the thread in which they're created; accesses from |
| 1622 | another thread can cause Tcl to panic. For certain Tcl interfaces, |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1623 | \module{Tkinter} will now automatically avoid this |
| 1624 | when a widget is accessed from a different thread by marshalling a |
| 1625 | command, passing it to the correct thread, and waiting for the |
| 1626 | results. Other interfaces can't be handled automatically but |
| 1627 | \module{Tkinter} will now raise an exception on such an access so that |
| 1628 | at least you can find out about the problem. See |
Andrew M. Kuchling | 6c50df2 | 2002-12-13 12:53:16 +0000 | [diff] [blame] | 1629 | \url{http://mail.python.org/pipermail/python-dev/2002-December/031107.html} |
| 1630 | for a more detailed explanation of this change. (Implemented by |
| 1631 | Martin von L\"owis.) |
| 1632 | |
Andrew M. Kuchling | b492fa9 | 2002-11-27 19:11:10 +0000 | [diff] [blame] | 1633 | \item Calling Tcl methods through \module{_tkinter} no longer |
| 1634 | returns only strings. Instead, if Tcl returns other objects those |
| 1635 | objects are converted to their Python equivalent, if one exists, or |
| 1636 | wrapped with a \class{_tkinter.Tcl_Obj} object if no Python equivalent |
Raymond Hettinger | 45bda57 | 2002-12-14 20:20:45 +0000 | [diff] [blame] | 1637 | exists. This behavior can be controlled through the |
Andrew M. Kuchling | b492fa9 | 2002-11-27 19:11:10 +0000 | [diff] [blame] | 1638 | \method{wantobjects()} method of \class{tkapp} objects. |
Martin v. Löwis | 39b4852 | 2002-11-26 09:47:25 +0000 | [diff] [blame] | 1639 | |
Andrew M. Kuchling | b492fa9 | 2002-11-27 19:11:10 +0000 | [diff] [blame] | 1640 | When using \module{_tkinter} through the \module{Tkinter} module (as |
| 1641 | most Tkinter applications will), this feature is always activated. It |
| 1642 | should not cause compatibility problems, since Tkinter would always |
| 1643 | convert string results to Python types where possible. |
Martin v. Löwis | 39b4852 | 2002-11-26 09:47:25 +0000 | [diff] [blame] | 1644 | |
Raymond Hettinger | 45bda57 | 2002-12-14 20:20:45 +0000 | [diff] [blame] | 1645 | If any incompatibilities are found, the old behavior can be restored |
Andrew M. Kuchling | b492fa9 | 2002-11-27 19:11:10 +0000 | [diff] [blame] | 1646 | by setting the \member{wantobjects} variable in the \module{Tkinter} |
| 1647 | module to false before creating the first \class{tkapp} object. |
Martin v. Löwis | 39b4852 | 2002-11-26 09:47:25 +0000 | [diff] [blame] | 1648 | |
| 1649 | \begin{verbatim} |
| 1650 | import Tkinter |
Martin v. Löwis | 8c8aa5d | 2002-11-26 21:39:48 +0000 | [diff] [blame] | 1651 | Tkinter.wantobjects = 0 |
Martin v. Löwis | 39b4852 | 2002-11-26 09:47:25 +0000 | [diff] [blame] | 1652 | \end{verbatim} |
| 1653 | |
Andrew M. Kuchling | 6c50df2 | 2002-12-13 12:53:16 +0000 | [diff] [blame] | 1654 | Any breakage caused by this change should be reported as a bug. |
Martin v. Löwis | 39b4852 | 2002-11-26 09:47:25 +0000 | [diff] [blame] | 1655 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 1656 | \end{itemize} |
| 1657 | |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 1658 | |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1659 | %====================================================================== |
Andrew M. Kuchling | a974b39 | 2003-01-13 19:09:03 +0000 | [diff] [blame] | 1660 | \subsection{Date/Time Type} |
| 1661 | |
Michael W. Hudson | 065f5fa | 2003-02-10 19:24:50 +0000 | [diff] [blame] | 1662 | % XXX This is out-of-date already: timetz and so on have gone away. |
| 1663 | |
Andrew M. Kuchling | a974b39 | 2003-01-13 19:09:03 +0000 | [diff] [blame] | 1664 | Date and time types suitable for expressing timestamps were added as |
| 1665 | the \module{datetime} module. The types don't support different |
| 1666 | calendars or many fancy features, and just stick to the basics of |
| 1667 | representing time. |
| 1668 | |
| 1669 | The three primary types are: \class{date}, representing a day, month, |
| 1670 | and year; \class{time}, consisting of hour, minute, and second; and |
| 1671 | \class{datetime}, which contains all the attributes of both |
| 1672 | \class{date} and \class{time}. These basic types don't understand |
| 1673 | time zones, but there are subclasses named \class{timetz} and |
| 1674 | \class{datetimetz} that do. There's also a |
| 1675 | \class{timedelta} class representing a difference between two points |
| 1676 | in time, and time zone logic is implemented by classes inheriting from |
| 1677 | the abstract \class{tzinfo} class. |
| 1678 | |
| 1679 | You can create instances of \class{date} and \class{time} by either |
| 1680 | supplying keyword arguments to the appropriate constructor, |
| 1681 | e.g. \code{datetime.date(year=1972, month=10, day=15)}, or by using |
| 1682 | one of a number of class methods. For example, the \method{today()} |
| 1683 | class method returns the current local date. |
| 1684 | |
| 1685 | Once created, instances of the date/time classes are all immutable. |
| 1686 | There are a number of methods for producing formatted strings from |
| 1687 | objects: |
| 1688 | |
| 1689 | \begin{verbatim} |
| 1690 | >>> import datetime |
| 1691 | >>> now = datetime.datetime.now() |
| 1692 | >>> now.isoformat() |
| 1693 | '2002-12-30T21:27:03.994956' |
| 1694 | >>> now.ctime() # Only available on date, datetime |
| 1695 | 'Mon Dec 30 21:27:03 2002' |
Raymond Hettinger | ee1bded | 2003-01-17 16:20:23 +0000 | [diff] [blame] | 1696 | >>> now.strftime('%Y %d %b') |
Andrew M. Kuchling | a974b39 | 2003-01-13 19:09:03 +0000 | [diff] [blame] | 1697 | '2002 30 Dec' |
| 1698 | \end{verbatim} |
| 1699 | |
| 1700 | The \method{replace()} method allows modifying one or more fields |
| 1701 | of a \class{date} or \class{datetime} instance: |
| 1702 | |
| 1703 | \begin{verbatim} |
| 1704 | >>> d = datetime.datetime.now() |
| 1705 | >>> d |
| 1706 | datetime.datetime(2002, 12, 30, 22, 15, 38, 827738) |
| 1707 | >>> d.replace(year=2001, hour = 12) |
| 1708 | datetime.datetime(2001, 12, 30, 12, 15, 38, 827738) |
| 1709 | >>> |
| 1710 | \end{verbatim} |
| 1711 | |
| 1712 | Instances can be compared, hashed, and converted to strings (the |
| 1713 | result is the same as that of \method{isoformat()}). \class{date} and |
| 1714 | \class{datetime} instances can be subtracted from each other, and |
| 1715 | added to \class{timedelta} instances. |
| 1716 | |
| 1717 | For more information, refer to the \ulink{module's reference |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 1718 | documentation}{..//lib/module-datetime.html}. |
Andrew M. Kuchling | a974b39 | 2003-01-13 19:09:03 +0000 | [diff] [blame] | 1719 | (Contributed by Tim Peters.) |
| 1720 | |
| 1721 | |
| 1722 | %====================================================================== |
Andrew M. Kuchling | 24d5a52 | 2002-11-14 23:40:42 +0000 | [diff] [blame] | 1723 | \subsection{The \module{optparse} Module} |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 1724 | |
Andrew M. Kuchling | 24d5a52 | 2002-11-14 23:40:42 +0000 | [diff] [blame] | 1725 | The \module{getopt} module provides simple parsing of command-line |
| 1726 | arguments. The new \module{optparse} module (originally named Optik) |
| 1727 | provides more elaborate command-line parsing that follows the Unix |
| 1728 | conventions, automatically creates the output for \longprogramopt{help}, |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1729 | and can perform different actions for different options. |
Andrew M. Kuchling | 24d5a52 | 2002-11-14 23:40:42 +0000 | [diff] [blame] | 1730 | |
| 1731 | You start by creating an instance of \class{OptionParser} and telling |
| 1732 | it what your program's options are. |
| 1733 | |
| 1734 | \begin{verbatim} |
| 1735 | from optparse import OptionParser |
| 1736 | |
| 1737 | op = OptionParser() |
| 1738 | op.add_option('-i', '--input', |
| 1739 | action='store', type='string', dest='input', |
| 1740 | help='set input filename') |
| 1741 | op.add_option('-l', '--length', |
| 1742 | action='store', type='int', dest='length', |
| 1743 | help='set maximum length of output') |
| 1744 | \end{verbatim} |
| 1745 | |
| 1746 | Parsing a command line is then done by calling the \method{parse_args()} |
| 1747 | method. |
| 1748 | |
| 1749 | \begin{verbatim} |
| 1750 | options, args = op.parse_args(sys.argv[1:]) |
| 1751 | print options |
| 1752 | print args |
| 1753 | \end{verbatim} |
| 1754 | |
| 1755 | This returns an object containing all of the option values, |
| 1756 | and a list of strings containing the remaining arguments. |
| 1757 | |
| 1758 | Invoking the script with the various arguments now works as you'd |
| 1759 | expect it to. Note that the length argument is automatically |
| 1760 | converted to an integer. |
| 1761 | |
| 1762 | \begin{verbatim} |
| 1763 | $ ./python opt.py -i data arg1 |
| 1764 | <Values at 0x400cad4c: {'input': 'data', 'length': None}> |
| 1765 | ['arg1'] |
| 1766 | $ ./python opt.py --input=data --length=4 |
| 1767 | <Values at 0x400cad2c: {'input': 'data', 'length': 4}> |
| 1768 | ['arg1'] |
| 1769 | $ |
| 1770 | \end{verbatim} |
| 1771 | |
| 1772 | The help message is automatically generated for you: |
| 1773 | |
| 1774 | \begin{verbatim} |
| 1775 | $ ./python opt.py --help |
| 1776 | usage: opt.py [options] |
| 1777 | |
| 1778 | options: |
| 1779 | -h, --help show this help message and exit |
| 1780 | -iINPUT, --input=INPUT |
| 1781 | set input filename |
| 1782 | -lLENGTH, --length=LENGTH |
| 1783 | set maximum length of output |
| 1784 | $ |
| 1785 | \end{verbatim} |
Andrew M. Kuchling | 669249e | 2002-11-19 13:05:33 +0000 | [diff] [blame] | 1786 | % $ prevent Emacs tex-mode from getting confused |
Andrew M. Kuchling | 24d5a52 | 2002-11-14 23:40:42 +0000 | [diff] [blame] | 1787 | |
| 1788 | Optik was written by Greg Ward, with suggestions from the readers of |
| 1789 | the Getopt SIG. |
| 1790 | |
| 1791 | \begin{seealso} |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 1792 | \seeurl{http://optik.sourceforge.net/} |
Andrew M. Kuchling | 24d5a52 | 2002-11-14 23:40:42 +0000 | [diff] [blame] | 1793 | {The Optik site has tutorial and reference documentation for |
| 1794 | \module{optparse}. |
| 1795 | % XXX change to point to Python docs, when those docs get written. |
| 1796 | } |
| 1797 | \end{seealso} |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 1798 | |
| 1799 | |
| 1800 | %====================================================================== |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1801 | \section{Specialized Object Allocator (pymalloc)\label{section-pymalloc}} |
| 1802 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1803 | An experimental feature added to Python 2.1 was pymalloc, a |
| 1804 | specialized object allocator written by Vladimir Marangozov. Pymalloc |
| 1805 | is intended to be faster than the system \cfunction{malloc()} and |
| 1806 | to have less memory overhead for allocation patterns typical of Python |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1807 | programs. The allocator uses C's \cfunction{malloc()} function to get |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1808 | large pools of memory and then fulfills smaller memory requests from |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1809 | these pools. |
| 1810 | |
| 1811 | In 2.1 and 2.2, pymalloc was an experimental feature and wasn't |
| 1812 | enabled by default; you had to explicitly turn it on by providing the |
| 1813 | \longprogramopt{with-pymalloc} option to the \program{configure} |
| 1814 | script. In 2.3, pymalloc has had further enhancements and is now |
| 1815 | enabled by default; you'll have to supply |
| 1816 | \longprogramopt{without-pymalloc} to disable it. |
| 1817 | |
| 1818 | This change is transparent to code written in Python; however, |
| 1819 | pymalloc may expose bugs in C extensions. Authors of C extension |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1820 | modules should test their code with pymalloc enabled, |
| 1821 | because some incorrect code may cause core dumps at runtime. |
| 1822 | |
| 1823 | There's one particularly common error that causes problems. There are |
| 1824 | a number of memory allocation functions in Python's C API that have |
| 1825 | previously just been aliases for the C library's \cfunction{malloc()} |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1826 | and \cfunction{free()}, meaning that if you accidentally called |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1827 | mismatched functions the error wouldn't be noticeable. When the |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1828 | object allocator is enabled, these functions aren't aliases of |
| 1829 | \cfunction{malloc()} and \cfunction{free()} any more, and calling the |
| 1830 | wrong function to free memory may get you a core dump. For example, |
| 1831 | if memory was allocated using \cfunction{PyObject_Malloc()}, it has to |
| 1832 | be freed using \cfunction{PyObject_Free()}, not \cfunction{free()}. A |
| 1833 | few modules included with Python fell afoul of this and had to be |
| 1834 | fixed; doubtless there are more third-party modules that will have the |
| 1835 | same problem. |
| 1836 | |
| 1837 | As part of this change, the confusing multiple interfaces for |
| 1838 | allocating memory have been consolidated down into two API families. |
| 1839 | Memory allocated with one family must not be manipulated with |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1840 | functions from the other family. There is one family for allocating |
| 1841 | chunks of memory, and another family of functions specifically for |
| 1842 | allocating Python objects. |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1843 | |
| 1844 | \begin{itemize} |
| 1845 | \item To allocate and free an undistinguished chunk of memory use |
| 1846 | the ``raw memory'' family: \cfunction{PyMem_Malloc()}, |
| 1847 | \cfunction{PyMem_Realloc()}, and \cfunction{PyMem_Free()}. |
| 1848 | |
| 1849 | \item The ``object memory'' family is the interface to the pymalloc |
| 1850 | facility described above and is biased towards a large number of |
| 1851 | ``small'' allocations: \cfunction{PyObject_Malloc}, |
| 1852 | \cfunction{PyObject_Realloc}, and \cfunction{PyObject_Free}. |
| 1853 | |
| 1854 | \item To allocate and free Python objects, use the ``object'' family |
| 1855 | \cfunction{PyObject_New()}, \cfunction{PyObject_NewVar()}, and |
| 1856 | \cfunction{PyObject_Del()}. |
| 1857 | \end{itemize} |
| 1858 | |
| 1859 | Thanks to lots of work by Tim Peters, pymalloc in 2.3 also provides |
| 1860 | debugging features to catch memory overwrites and doubled frees in |
| 1861 | both extension modules and in the interpreter itself. To enable this |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1862 | support, compile a debugging version of the Python interpreter by |
| 1863 | running \program{configure} with \longprogramopt{with-pydebug}. |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1864 | |
| 1865 | To aid extension writers, a header file \file{Misc/pymemcompat.h} is |
| 1866 | distributed with the source to Python 2.3 that allows Python |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1867 | extensions to use the 2.3 interfaces to memory allocation while |
| 1868 | compiling against any version of Python since 1.5.2. You would copy |
| 1869 | the file from Python's source distribution and bundle it with the |
| 1870 | source of your extension. |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1871 | |
| 1872 | \begin{seealso} |
| 1873 | |
| 1874 | \seeurl{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/python/python/dist/src/Objects/obmalloc.c} |
| 1875 | {For the full details of the pymalloc implementation, see |
| 1876 | the comments at the top of the file \file{Objects/obmalloc.c} in the |
| 1877 | Python source code. The above link points to the file within the |
| 1878 | SourceForge CVS browser.} |
| 1879 | |
| 1880 | \end{seealso} |
| 1881 | |
| 1882 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 1883 | % ====================================================================== |
| 1884 | \section{Build and C API Changes} |
| 1885 | |
Andrew M. Kuchling | 3c305d9 | 2002-07-22 18:50:11 +0000 | [diff] [blame] | 1886 | Changes to Python's build process and to the C API include: |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 1887 | |
| 1888 | \begin{itemize} |
| 1889 | |
Andrew M. Kuchling | ef5d06b | 2002-07-22 19:21:06 +0000 | [diff] [blame] | 1890 | \item The C-level interface to the garbage collector has been changed, |
| 1891 | to make it easier to write extension types that support garbage |
| 1892 | collection, and to make it easier to debug misuses of the functions. |
| 1893 | Various functions have slightly different semantics, so a bunch of |
| 1894 | functions had to be renamed. Extensions that use the old API will |
| 1895 | still compile but will \emph{not} participate in garbage collection, |
| 1896 | so updating them for 2.3 should be considered fairly high priority. |
| 1897 | |
| 1898 | To upgrade an extension module to the new API, perform the following |
| 1899 | steps: |
| 1900 | |
| 1901 | \begin{itemize} |
| 1902 | |
| 1903 | \item Rename \cfunction{Py_TPFLAGS_GC} to \cfunction{PyTPFLAGS_HAVE_GC}. |
| 1904 | |
| 1905 | \item Use \cfunction{PyObject_GC_New} or \cfunction{PyObject_GC_NewVar} to |
| 1906 | allocate objects, and \cfunction{PyObject_GC_Del} to deallocate them. |
| 1907 | |
| 1908 | \item Rename \cfunction{PyObject_GC_Init} to \cfunction{PyObject_GC_Track} and |
| 1909 | \cfunction{PyObject_GC_Fini} to \cfunction{PyObject_GC_UnTrack}. |
| 1910 | |
| 1911 | \item Remove \cfunction{PyGC_HEAD_SIZE} from object size calculations. |
| 1912 | |
| 1913 | \item Remove calls to \cfunction{PyObject_AS_GC} and \cfunction{PyObject_FROM_GC}. |
| 1914 | |
| 1915 | \end{itemize} |
| 1916 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1917 | \item The cycle detection implementation used by the garbage collection |
| 1918 | has proven to be stable, so it's now being made mandatory; you can no |
| 1919 | longer compile Python without it, and the |
| 1920 | \longprogramopt{with-cycle-gc} switch to \program{configure} has been removed. |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 1921 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 1922 | \item Python can now optionally be built as a shared library |
| 1923 | (\file{libpython2.3.so}) by supplying \longprogramopt{enable-shared} |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1924 | when running Python's \program{configure} script. (Contributed by Ondrej |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 1925 | Palkovsky.) |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 1926 | |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1927 | \item The \csimplemacro{DL_EXPORT} and \csimplemacro{DL_IMPORT} macros |
| 1928 | are now deprecated. Initialization functions for Python extension |
| 1929 | modules should now be declared using the new macro |
Andrew M. Kuchling | 3c305d9 | 2002-07-22 18:50:11 +0000 | [diff] [blame] | 1930 | \csimplemacro{PyMODINIT_FUNC}, while the Python core will generally |
| 1931 | use the \csimplemacro{PyAPI_FUNC} and \csimplemacro{PyAPI_DATA} |
| 1932 | macros. |
Neal Norwitz | bba23a8 | 2002-07-22 13:18:59 +0000 | [diff] [blame] | 1933 | |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1934 | \item The interpreter can be compiled without any docstrings for |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 1935 | the built-in functions and modules by supplying |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1936 | \longprogramopt{without-doc-strings} to the \program{configure} script. |
Andrew M. Kuchling | e995d16 | 2002-07-11 20:09:50 +0000 | [diff] [blame] | 1937 | This makes the Python executable about 10\% smaller, but will also |
| 1938 | mean that you can't get help for Python's built-ins. (Contributed by |
| 1939 | Gustavo Niemeyer.) |
| 1940 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 1941 | \item The \cfunction{PyArg_NoArgs()} macro is now deprecated, and code |
Andrew M. Kuchling | 7845e7c | 2002-07-11 19:27:46 +0000 | [diff] [blame] | 1942 | that uses it should be changed. For Python 2.2 and later, the method |
| 1943 | definition table can specify the |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1944 | \constant{METH_NOARGS} flag, signalling that there are no arguments, and |
Andrew M. Kuchling | 7845e7c | 2002-07-11 19:27:46 +0000 | [diff] [blame] | 1945 | the argument checking can then be removed. If compatibility with |
| 1946 | pre-2.2 versions of Python is important, the code could use |
Fred Drake | aac8c58 | 2003-01-17 22:50:10 +0000 | [diff] [blame] | 1947 | \code{PyArg_ParseTuple(\var{args}, "")} instead, but this will be slower |
Andrew M. Kuchling | 7845e7c | 2002-07-11 19:27:46 +0000 | [diff] [blame] | 1948 | than using \constant{METH_NOARGS}. |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 1949 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 1950 | \item A new function, \cfunction{PyObject_DelItemString(\var{mapping}, |
| 1951 | char *\var{key})} was added |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1952 | as shorthand for |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 1953 | \code{PyObject_DelItem(\var{mapping}, PyString_New(\var{key})}. |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 1954 | |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 1955 | \item The \method{xreadlines()} method of file objects, introduced in |
| 1956 | Python 2.1, is no longer necessary because files now behave as their |
| 1957 | own iterator. \method{xreadlines()} was originally introduced as a |
| 1958 | faster way to loop over all the lines in a file, but now you can |
| 1959 | simply write \code{for line in file_obj}. |
| 1960 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 1961 | \item File objects now manage their internal string buffer |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 1962 | differently, increasing it exponentially when needed. This results in |
| 1963 | the benchmark tests in \file{Lib/test/test_bufio.py} speeding up |
| 1964 | considerably (from 57 seconds to 1.7 seconds, according to one |
| 1965 | measurement). |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 1966 | |
Andrew M. Kuchling | 72b58e0 | 2002-05-29 17:30:34 +0000 | [diff] [blame] | 1967 | \item It's now possible to define class and static methods for a C |
| 1968 | extension type by setting either the \constant{METH_CLASS} or |
| 1969 | \constant{METH_STATIC} flags in a method's \ctype{PyMethodDef} |
| 1970 | structure. |
Andrew M. Kuchling | 45afd54 | 2002-04-02 14:25:25 +0000 | [diff] [blame] | 1971 | |
Andrew M. Kuchling | 346386f | 2002-07-12 20:24:42 +0000 | [diff] [blame] | 1972 | \item Python now includes a copy of the Expat XML parser's source code, |
| 1973 | removing any dependence on a system version or local installation of |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 1974 | Expat. |
Andrew M. Kuchling | 346386f | 2002-07-12 20:24:42 +0000 | [diff] [blame] | 1975 | |
Michael W. Hudson | 3e245d8 | 2003-02-11 14:19:56 +0000 | [diff] [blame] | 1976 | \item If you dynamically allocate type objects in your extension, you |
Neal Norwitz | ada859c | 2003-02-11 14:30:39 +0000 | [diff] [blame^] | 1977 | should be aware of a change in the rules relating to the |
Michael W. Hudson | 3e245d8 | 2003-02-11 14:19:56 +0000 | [diff] [blame] | 1978 | \member{__module__} and \member{__name__} attributes. In summary, |
| 1979 | you will want to ensure the type's dictionary contains a |
| 1980 | \code{'__module__'} key; making the module name the part of the type |
| 1981 | name leading up to the final period will no longer have the desired |
| 1982 | effect. For more detail, read the API reference documentation or the |
| 1983 | source. |
| 1984 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 1985 | \end{itemize} |
| 1986 | |
Andrew M. Kuchling | 366c10c | 2002-11-14 23:07:57 +0000 | [diff] [blame] | 1987 | |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 1988 | %====================================================================== |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 1989 | \subsection{Port-Specific Changes} |
| 1990 | |
Andrew M. Kuchling | 187b1d8 | 2002-05-29 19:20:57 +0000 | [diff] [blame] | 1991 | Support for a port to IBM's OS/2 using the EMX runtime environment was |
| 1992 | merged into the main Python source tree. EMX is a POSIX emulation |
| 1993 | layer over the OS/2 system APIs. The Python port for EMX tries to |
| 1994 | support all the POSIX-like capability exposed by the EMX runtime, and |
| 1995 | mostly succeeds; \function{fork()} and \function{fcntl()} are |
| 1996 | restricted by the limitations of the underlying emulation layer. The |
| 1997 | standard OS/2 port, which uses IBM's Visual Age compiler, also gained |
| 1998 | support for case-sensitive import semantics as part of the integration |
| 1999 | of the EMX port into CVS. (Contributed by Andrew MacIntyre.) |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 2000 | |
Andrew M. Kuchling | 72b58e0 | 2002-05-29 17:30:34 +0000 | [diff] [blame] | 2001 | On MacOS, most toolbox modules have been weaklinked to improve |
| 2002 | backward compatibility. This means that modules will no longer fail |
| 2003 | to load if a single routine is missing on the curent OS version. |
Andrew M. Kuchling | 187b1d8 | 2002-05-29 19:20:57 +0000 | [diff] [blame] | 2004 | Instead calling the missing routine will raise an exception. |
| 2005 | (Contributed by Jack Jansen.) |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 2006 | |
Andrew M. Kuchling | 187b1d8 | 2002-05-29 19:20:57 +0000 | [diff] [blame] | 2007 | The RPM spec files, found in the \file{Misc/RPM/} directory in the |
| 2008 | Python source distribution, were updated for 2.3. (Contributed by |
| 2009 | Sean Reifschneider.) |
Fred Drake | 03e1031 | 2002-03-26 19:17:43 +0000 | [diff] [blame] | 2010 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 2011 | Other new platforms now supported by Python include AtheOS |
Fred Drake | 693aea2 | 2003-02-07 14:52:18 +0000 | [diff] [blame] | 2012 | (\url{http://www.atheos.cx/}), GNU/Hurd, and OpenVMS. |
Andrew M. Kuchling | 20e5abc | 2002-07-11 20:50:34 +0000 | [diff] [blame] | 2013 | |
Fred Drake | 03e1031 | 2002-03-26 19:17:43 +0000 | [diff] [blame] | 2014 | |
| 2015 | %====================================================================== |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 2016 | \section{Other Changes and Fixes \label{section-other}} |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 2017 | |
Andrew M. Kuchling | 7a82b8c | 2002-11-04 20:17:24 +0000 | [diff] [blame] | 2018 | As usual, there were a bunch of other improvements and bugfixes |
| 2019 | scattered throughout the source tree. A search through the CVS change |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 2020 | logs finds there were 121 patches applied and 103 bugs fixed between |
Andrew M. Kuchling | 7a82b8c | 2002-11-04 20:17:24 +0000 | [diff] [blame] | 2021 | Python 2.2 and 2.3. Both figures are likely to be underestimates. |
| 2022 | |
| 2023 | Some of the more notable changes are: |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 2024 | |
| 2025 | \begin{itemize} |
| 2026 | |
Fred Drake | 54fe3fd | 2002-11-26 22:07:35 +0000 | [diff] [blame] | 2027 | \item The \file{regrtest.py} script now provides a way to allow ``all |
| 2028 | resources except \var{foo}.'' A resource name passed to the |
| 2029 | \programopt{-u} option can now be prefixed with a hyphen |
| 2030 | (\character{-}) to mean ``remove this resource.'' For example, the |
| 2031 | option `\code{\programopt{-u}all,-bsddb}' could be used to enable the |
| 2032 | use of all resources except \code{bsddb}. |
| 2033 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 2034 | \item The tools used to build the documentation now work under Cygwin |
| 2035 | as well as \UNIX. |
| 2036 | |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 2037 | \item The \code{SET_LINENO} opcode has been removed. Back in the |
| 2038 | mists of time, this opcode was needed to produce line numbers in |
| 2039 | tracebacks and support trace functions (for, e.g., \module{pdb}). |
| 2040 | Since Python 1.5, the line numbers in tracebacks have been computed |
| 2041 | using a different mechanism that works with ``python -O''. For Python |
| 2042 | 2.3 Michael Hudson implemented a similar scheme to determine when to |
| 2043 | call the trace function, removing the need for \code{SET_LINENO} |
| 2044 | entirely. |
| 2045 | |
Andrew M. Kuchling | 7a82b8c | 2002-11-04 20:17:24 +0000 | [diff] [blame] | 2046 | It would be difficult to detect any resulting difference from Python |
| 2047 | code, apart from a slight speed up when Python is run without |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 2048 | \programopt{-O}. |
| 2049 | |
| 2050 | C extensions that access the \member{f_lineno} field of frame objects |
| 2051 | should instead call \code{PyCode_Addr2Line(f->f_code, f->f_lasti)}. |
| 2052 | This will have the added effect of making the code work as desired |
| 2053 | under ``python -O'' in earlier versions of Python. |
| 2054 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 2055 | A nifty new feature is that trace functions can now assign to the |
| 2056 | \member{f_lineno} attribute of frame objects, changing the line that |
| 2057 | will be executed next. A \samp{jump} command has been added to the |
| 2058 | \module{pdb} debugger taking advantage of this new feature. |
| 2059 | (Implemented by Richie Hindle.) |
Andrew M. Kuchling | 974ab9d | 2002-12-31 01:20:30 +0000 | [diff] [blame] | 2060 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 2061 | \end{itemize} |
| 2062 | |
Andrew M. Kuchling | 187b1d8 | 2002-05-29 19:20:57 +0000 | [diff] [blame] | 2063 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 2064 | %====================================================================== |
Andrew M. Kuchling | 950725f | 2002-08-06 01:40:48 +0000 | [diff] [blame] | 2065 | \section{Porting to Python 2.3} |
| 2066 | |
Andrew M. Kuchling | f15fb29 | 2002-12-31 18:34:54 +0000 | [diff] [blame] | 2067 | This section lists previously described changes that may require |
| 2068 | changes to your code: |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 2069 | |
| 2070 | \begin{itemize} |
| 2071 | |
| 2072 | \item \keyword{yield} is now always a keyword; if it's used as a |
| 2073 | variable name in your code, a different name must be chosen. |
| 2074 | |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 2075 | \item For strings \var{X} and \var{Y}, \code{\var{X} in \var{Y}} now works |
| 2076 | if \var{X} is more than one character long. |
| 2077 | |
Andrew M. Kuchling | 495172c | 2002-11-20 13:50:15 +0000 | [diff] [blame] | 2078 | \item The \function{int()} type constructor will now return a long |
| 2079 | integer instead of raising an \exception{OverflowError} when a string |
| 2080 | or floating-point number is too large to fit into an integer. |
| 2081 | |
Andrew M. Kuchling | b492fa9 | 2002-11-27 19:11:10 +0000 | [diff] [blame] | 2082 | \item Calling Tcl methods through \module{_tkinter} no longer |
| 2083 | returns only strings. Instead, if Tcl returns other objects those |
| 2084 | objects are converted to their Python equivalent, if one exists, or |
| 2085 | wrapped with a \class{_tkinter.Tcl_Obj} object if no Python equivalent |
| 2086 | exists. |
| 2087 | |
Andrew M. Kuchling | 80fd785 | 2003-02-06 15:14:04 +0000 | [diff] [blame] | 2088 | \item Large octal and hex literals such as |
Andrew M. Kuchling | 72df65a | 2003-02-10 15:08:16 +0000 | [diff] [blame] | 2089 | \code{0xffffffff} now trigger a \exception{FutureWarning}. Currently |
Andrew M. Kuchling | 80fd785 | 2003-02-06 15:14:04 +0000 | [diff] [blame] | 2090 | they're stored as 32-bit numbers and result in a negative value, but |
Andrew M. Kuchling | 72df65a | 2003-02-10 15:08:16 +0000 | [diff] [blame] | 2091 | in Python 2.4 they'll become positive long integers. |
| 2092 | |
| 2093 | There are a few ways to fix this warning. If you really need a |
| 2094 | positive number, just add an \samp{L} to the end of the literal. If |
| 2095 | you're trying to get a 32-bit integer with low bits set and have |
| 2096 | previously used an expression such as \code{~(1 << 31)}, it's probably |
| 2097 | clearest to start with all bits set and clear the desired upper bits. |
| 2098 | For example, to clear just the top bit (bit 31), you could write |
| 2099 | \code{0xffffffffL {\&}{\textasciitilde}(1L<<31)}. |
Andrew M. Kuchling | 80fd785 | 2003-02-06 15:14:04 +0000 | [diff] [blame] | 2100 | |
Andrew M. Kuchling | 495172c | 2002-11-20 13:50:15 +0000 | [diff] [blame] | 2101 | \item You can no longer disable assertions by assigning to \code{__debug__}. |
| 2102 | |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 2103 | \item The Distutils \function{setup()} function has gained various new |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 2104 | keyword arguments such as \var{depends}. Old versions of the |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 2105 | Distutils will abort if passed unknown keywords. The fix is to check |
| 2106 | for the presence of the new \function{get_distutil_options()} function |
| 2107 | in your \file{setup.py} if you want to only support the new keywords |
| 2108 | with a version of the Distutils that supports them: |
| 2109 | |
| 2110 | \begin{verbatim} |
| 2111 | from distutils import core |
| 2112 | |
| 2113 | kw = {'sources': 'foo.c', ...} |
| 2114 | if hasattr(core, 'get_distutil_options'): |
| 2115 | kw['depends'] = ['foo.h'] |
Fred Drake | 5c4cf15 | 2002-11-13 14:59:06 +0000 | [diff] [blame] | 2116 | ext = Extension(**kw) |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 2117 | \end{verbatim} |
| 2118 | |
Andrew M. Kuchling | 495172c | 2002-11-20 13:50:15 +0000 | [diff] [blame] | 2119 | \item Using \code{None} as a variable name will now result in a |
| 2120 | \exception{SyntaxWarning} warning. |
| 2121 | |
| 2122 | \item Names of extension types defined by the modules included with |
| 2123 | Python now contain the module and a \character{.} in front of the type |
| 2124 | name. |
| 2125 | |
Andrew M. Kuchling | 8a61f49 | 2002-11-13 13:24:41 +0000 | [diff] [blame] | 2126 | \end{itemize} |
Andrew M. Kuchling | 950725f | 2002-08-06 01:40:48 +0000 | [diff] [blame] | 2127 | |
| 2128 | |
| 2129 | %====================================================================== |
Fred Drake | 03e1031 | 2002-03-26 19:17:43 +0000 | [diff] [blame] | 2130 | \section{Acknowledgements \label{acks}} |
| 2131 | |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 2132 | The author would like to thank the following people for offering |
| 2133 | suggestions, corrections and assistance with various drafts of this |
Andrew M. Kuchling | b9ba4e6 | 2003-02-03 15:16:15 +0000 | [diff] [blame] | 2134 | article: Simon Brunning, Michael Chermside, Andrew Dalke, Scott David |
| 2135 | Daniels, Fred~L. Drake, Jr., Kelly Gerber, Raymond Hettinger, Michael |
| 2136 | Hudson, Detlef Lannert, Martin von L\"owis, Andrew MacIntyre, Lalo |
| 2137 | Martins, Gustavo Niemeyer, Neal Norwitz, Hans Nowak, Chris Reedy, |
| 2138 | Vinay Sajip, Neil Schemenauer, Jason Tishler, Just van~Rossum. |
Fred Drake | 03e1031 | 2002-03-26 19:17:43 +0000 | [diff] [blame] | 2139 | |
| 2140 | \end{document} |