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