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