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