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