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