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