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 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 4 | % TODO: |
| 5 | % Go through and get the contributor's name for all the various changes |
| 6 | |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 7 | \title{What's New in Python 2.3} |
| 8 | \release{0.01} |
| 9 | \author{A.M. Kuchling} |
| 10 | \authoraddress{\email{akuchlin@mems-exchange.org}} |
Fred Drake | 03e1031 | 2002-03-26 19:17:43 +0000 | [diff] [blame] | 11 | |
| 12 | \begin{document} |
| 13 | \maketitle |
| 14 | \tableofcontents |
| 15 | |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 16 | %\section{Introduction \label{intro}} |
| 17 | |
| 18 | {\large This article is a draft, and is currently up to date for some |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 19 | random version of the CVS tree around May 26 2002. Please send any |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 20 | additions, comments or errata to the author.} |
| 21 | |
| 22 | This article explains the new features in Python 2.3. The tentative |
| 23 | release date of Python 2.3 is currently scheduled for August 30 2002. |
| 24 | |
| 25 | This article doesn't attempt to provide a complete specification of |
| 26 | the new features, but instead provides a convenient overview. For |
| 27 | full details, you should refer to the documentation for Python 2.3, |
| 28 | such as the |
| 29 | \citetitle[http://www.python.org/doc/2.3/lib/lib.html]{Python Library |
| 30 | Reference} and the |
| 31 | \citetitle[http://www.python.org/doc/2.3/ref/ref.html]{Python |
| 32 | Reference Manual}. If you want to understand the complete |
| 33 | implementation and design rationale for a change, refer to the PEP for |
| 34 | a particular new feature. |
Fred Drake | 03e1031 | 2002-03-26 19:17:43 +0000 | [diff] [blame] | 35 | |
| 36 | |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 37 | %====================================================================== |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 38 | \section{PEP 255: Simple Generators\label{section-generators}} |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 39 | |
| 40 | In Python 2.2, generators were added as an optional feature, to be |
| 41 | enabled by a \code{from __future__ import generators} directive. In |
| 42 | 2.3 generators no longer need to be specially enabled, and are now |
| 43 | always present; this means that \keyword{yield} is now always a |
| 44 | keyword. The rest of this section is a copy of the description of |
| 45 | generators from the ``What's New in Python 2.2'' document; if you read |
| 46 | it when 2.2 came out, you can skip the rest of this section. |
| 47 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 48 | You're doubtless familiar with how function calls work in Python or C. |
| 49 | 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] | 50 | variables are created. When the function reaches a \keyword{return} |
| 51 | statement, the local variables are destroyed and the resulting value |
| 52 | 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] | 53 | 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] | 54 | weren't thrown away on exiting a function? What if you could later |
| 55 | resume the function where it left off? This is what generators |
| 56 | provide; they can be thought of as resumable functions. |
| 57 | |
| 58 | Here's the simplest example of a generator function: |
| 59 | |
| 60 | \begin{verbatim} |
| 61 | def generate_ints(N): |
| 62 | for i in range(N): |
| 63 | yield i |
| 64 | \end{verbatim} |
| 65 | |
| 66 | A new keyword, \keyword{yield}, was introduced for generators. Any |
| 67 | function containing a \keyword{yield} statement is a generator |
| 68 | function; this is detected by Python's bytecode compiler which |
| 69 | compiles the function specially as a result. |
| 70 | |
| 71 | When you call a generator function, it doesn't return a single value; |
| 72 | instead it returns a generator object that supports the iterator |
| 73 | protocol. On executing the \keyword{yield} statement, the generator |
| 74 | outputs the value of \code{i}, similar to a \keyword{return} |
| 75 | statement. The big difference between \keyword{yield} and a |
| 76 | \keyword{return} statement is that on reaching a \keyword{yield} the |
| 77 | generator's state of execution is suspended and local variables are |
| 78 | preserved. On the next call to the generator's \code{.next()} method, |
| 79 | the function will resume executing immediately after the |
| 80 | \keyword{yield} statement. (For complicated reasons, the |
| 81 | \keyword{yield} statement isn't allowed inside the \keyword{try} block |
| 82 | of a \code{try...finally} statement; read \pep{255} for a full |
| 83 | explanation of the interaction between \keyword{yield} and |
| 84 | exceptions.) |
| 85 | |
| 86 | Here's a sample usage of the \function{generate_ints} generator: |
| 87 | |
| 88 | \begin{verbatim} |
| 89 | >>> gen = generate_ints(3) |
| 90 | >>> gen |
| 91 | <generator object at 0x8117f90> |
| 92 | >>> gen.next() |
| 93 | 0 |
| 94 | >>> gen.next() |
| 95 | 1 |
| 96 | >>> gen.next() |
| 97 | 2 |
| 98 | >>> gen.next() |
| 99 | Traceback (most recent call last): |
| 100 | File "<stdin>", line 1, in ? |
| 101 | File "<stdin>", line 2, in generate_ints |
| 102 | StopIteration |
| 103 | \end{verbatim} |
| 104 | |
| 105 | You could equally write \code{for i in generate_ints(5)}, or |
| 106 | \code{a,b,c = generate_ints(3)}. |
| 107 | |
| 108 | Inside a generator function, the \keyword{return} statement can only |
| 109 | be used without a value, and signals the end of the procession of |
| 110 | values; afterwards the generator cannot return any further values. |
| 111 | \keyword{return} with a value, such as \code{return 5}, is a syntax |
| 112 | error inside a generator function. The end of the generator's results |
| 113 | can also be indicated by raising \exception{StopIteration} manually, |
| 114 | or by just letting the flow of execution fall off the bottom of the |
| 115 | function. |
| 116 | |
| 117 | You could achieve the effect of generators manually by writing your |
| 118 | own class and storing all the local variables of the generator as |
| 119 | instance variables. For example, returning a list of integers could |
| 120 | be done by setting \code{self.count} to 0, and having the |
| 121 | \method{next()} method increment \code{self.count} and return it. |
| 122 | However, for a moderately complicated generator, writing a |
| 123 | corresponding class would be much messier. |
| 124 | \file{Lib/test/test_generators.py} contains a number of more |
| 125 | interesting examples. The simplest one implements an in-order |
| 126 | traversal of a tree using generators recursively. |
| 127 | |
| 128 | \begin{verbatim} |
| 129 | # A recursive generator that generates Tree leaves in in-order. |
| 130 | def inorder(t): |
| 131 | if t: |
| 132 | for x in inorder(t.left): |
| 133 | yield x |
| 134 | yield t.label |
| 135 | for x in inorder(t.right): |
| 136 | yield x |
| 137 | \end{verbatim} |
| 138 | |
| 139 | Two other examples in \file{Lib/test/test_generators.py} produce |
| 140 | solutions for the N-Queens problem (placing $N$ queens on an $NxN$ |
| 141 | chess board so that no queen threatens another) and the Knight's Tour |
| 142 | (a route that takes a knight to every square of an $NxN$ chessboard |
| 143 | without visiting any square twice). |
| 144 | |
| 145 | The idea of generators comes from other programming languages, |
| 146 | especially Icon (\url{http://www.cs.arizona.edu/icon/}), where the |
| 147 | idea of generators is central. In Icon, every |
| 148 | expression and function call behaves like a generator. One example |
| 149 | from ``An Overview of the Icon Programming Language'' at |
| 150 | \url{http://www.cs.arizona.edu/icon/docs/ipd266.htm} gives an idea of |
| 151 | what this looks like: |
| 152 | |
| 153 | \begin{verbatim} |
| 154 | sentence := "Store it in the neighboring harbor" |
| 155 | if (i := find("or", sentence)) > 5 then write(i) |
| 156 | \end{verbatim} |
| 157 | |
| 158 | In Icon the \function{find()} function returns the indexes at which the |
| 159 | substring ``or'' is found: 3, 23, 33. In the \keyword{if} statement, |
| 160 | \code{i} is first assigned a value of 3, but 3 is less than 5, so the |
| 161 | comparison fails, and Icon retries it with the second value of 23. 23 |
| 162 | is greater than 5, so the comparison now succeeds, and the code prints |
| 163 | the value 23 to the screen. |
| 164 | |
| 165 | Python doesn't go nearly as far as Icon in adopting generators as a |
| 166 | central concept. Generators are considered a new part of the core |
| 167 | Python language, but learning or using them isn't compulsory; if they |
| 168 | don't solve any problems that you have, feel free to ignore them. |
| 169 | One novel feature of Python's interface as compared to |
| 170 | Icon's is that a generator's state is represented as a concrete object |
| 171 | (the iterator) that can be passed around to other functions or stored |
| 172 | in a data structure. |
| 173 | |
| 174 | \begin{seealso} |
| 175 | |
| 176 | \seepep{255}{Simple Generators}{Written by Neil Schemenauer, Tim |
| 177 | Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer |
| 178 | and Tim Peters, with other fixes from the Python Labs crew.} |
| 179 | |
| 180 | \end{seealso} |
| 181 | |
| 182 | |
| 183 | %====================================================================== |
Andrew M. Kuchling | f367651 | 2002-04-15 02:27:55 +0000 | [diff] [blame] | 184 | \section{PEP 278: Universal Newline Support} |
| 185 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 186 | The three major operating systems used today are Microsoft Windows, |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 187 | Apple's Macintosh OS, and the various \UNIX\ derivatives. A minor |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 188 | irritation is that these three platforms all use different characters |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 189 | to mark the ends of lines in text files. \UNIX\ uses character 10, |
| 190 | the ASCII linefeed, while MacOS uses character 13, the ASCII carriage |
| 191 | return, and Windows uses a two-character sequence of a carriage return |
| 192 | plus a newline. |
Andrew M. Kuchling | f367651 | 2002-04-15 02:27:55 +0000 | [diff] [blame] | 193 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 194 | Python's file objects can now support end of line conventions other |
| 195 | than the one followed by the platform on which Python is running. |
| 196 | Opening a file with the mode \samp{U} or \samp{rU} will open a file |
| 197 | for reading in universal newline mode. All three line ending |
| 198 | conventions will be translated to a \samp{\e n} in the strings |
| 199 | returned by the various file methods such as \method{read()} and |
| 200 | \method{readline()}. |
Andrew M. Kuchling | f367651 | 2002-04-15 02:27:55 +0000 | [diff] [blame] | 201 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 202 | Universal newline support is also used when importing modules and when |
| 203 | executing a file with the \function{execfile()} function. This means |
| 204 | that Python modules can be shared between all three operating systems |
| 205 | without needing to convert the line-endings. |
| 206 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 207 | This feature can be disabled at compile-time by specifying |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 208 | \longprogramopt{without-universal-newlines} when running Python's |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 209 | \file{configure} script. |
Andrew M. Kuchling | f367651 | 2002-04-15 02:27:55 +0000 | [diff] [blame] | 210 | |
| 211 | \begin{seealso} |
| 212 | |
| 213 | \seepep{278}{Universal Newline Support}{Written |
| 214 | and implemented by Jack Jansen.} |
| 215 | |
| 216 | \end{seealso} |
| 217 | |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 218 | |
| 219 | %====================================================================== |
| 220 | \section{PEP 279: The \function{enumerate()} Built-in Function} |
| 221 | |
| 222 | A new built-in function, \function{enumerate()}, will make |
| 223 | certain loops a bit clearer. \code{enumerate(thing)}, where |
| 224 | \var{thing} is either an iterator or a sequence, returns a iterator |
| 225 | that will return \code{(0, \var{thing[0]})}, \code{(1, |
| 226 | \var{thing[1]})}, \code{(2, \var{thing[2]})}, and so forth. Fairly |
| 227 | often you'll see code to change every element of a list that looks |
| 228 | like this: |
| 229 | |
| 230 | \begin{verbatim} |
| 231 | for i in range(len(L)): |
| 232 | item = L[i] |
| 233 | # ... compute some result based on item ... |
| 234 | L[i] = result |
| 235 | \end{verbatim} |
| 236 | |
| 237 | This can be rewritten using \function{enumerate()} as: |
| 238 | |
| 239 | \begin{verbatim} |
| 240 | for i, item in enumerate(L): |
| 241 | # ... compute some result based on item ... |
| 242 | L[i] = result |
| 243 | \end{verbatim} |
| 244 | |
| 245 | |
| 246 | \begin{seealso} |
| 247 | |
| 248 | \seepep{279}{The enumerate() built-in function}{Written |
| 249 | by Raymond D. Hettinger.} |
| 250 | |
| 251 | \end{seealso} |
| 252 | |
| 253 | |
Andrew M. Kuchling | f367651 | 2002-04-15 02:27:55 +0000 | [diff] [blame] | 254 | %====================================================================== |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 255 | \section{PEP 285: The \class{bool} Type\label{section-bool}} |
| 256 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 257 | A Boolean type was added to Python 2.3. Two new constants were added |
| 258 | to the \module{__builtin__} module, \constant{True} and |
| 259 | \constant{False}. The type object for this new type is named |
| 260 | \class{bool}; the constructor for it takes any Python value and |
| 261 | converts it to \constant{True} or \constant{False}. |
| 262 | |
| 263 | \begin{verbatim} |
| 264 | >>> bool(1) |
| 265 | True |
| 266 | >>> bool(0) |
| 267 | False |
| 268 | >>> bool([]) |
| 269 | False |
| 270 | >>> bool( (1,) ) |
| 271 | True |
| 272 | \end{verbatim} |
| 273 | |
| 274 | Most of the standard library modules and built-in functions have been |
| 275 | changed to return Booleans. |
| 276 | |
| 277 | \begin{verbatim} |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 278 | >>> obj = [] |
| 279 | >>> hasattr(obj, 'append') |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 280 | True |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 281 | >>> isinstance(obj, list) |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 282 | True |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 283 | >>> isinstance(obj, tuple) |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 284 | False |
| 285 | \end{verbatim} |
| 286 | |
| 287 | Python's Booleans were added with the primary goal of making code |
| 288 | clearer. For example, if you're reading a function and encounter the |
| 289 | statement \code{return 1}, you might wonder whether the \samp{1} |
| 290 | represents a truth value, or whether it's an index, or whether it's a |
| 291 | coefficient that multiplies some other quantity. If the statement is |
| 292 | \code{return True}, however, the meaning of the return value is quite |
| 293 | clearly a truth value. |
| 294 | |
| 295 | Python's Booleans were not added for the sake of strict type-checking. |
| 296 | A very strict language such as Pascal |
| 297 | % XXX is Pascal the right example here? |
| 298 | would also prevent you performing arithmetic with Booleans, and would |
| 299 | require that the expression in an \keyword{if} statement always |
| 300 | evaluate to a Boolean. Python is not this strict, and it never will |
| 301 | be. (\pep{285} explicitly says this.) So you can still use any |
| 302 | expression in an \keyword{if}, even ones that evaluate to a list or |
| 303 | tuple or some random object, and the Boolean type is a subclass of the |
| 304 | \class{int} class, so arithmetic using a Boolean still works. |
| 305 | |
| 306 | \begin{verbatim} |
| 307 | >>> True + 1 |
| 308 | 2 |
| 309 | >>> False + 1 |
| 310 | 1 |
| 311 | >>> False * 75 |
| 312 | 0 |
| 313 | >>> True * 75 |
| 314 | 75 |
| 315 | \end{verbatim} |
| 316 | |
| 317 | To sum up \constant{True} and \constant{False} in a sentence: they're |
| 318 | alternative ways to spell the integer values 1 and 0, with the single |
| 319 | difference that \function{str()} and \function{repr()} return the |
| 320 | strings \samp{True} and \samp{False} instead of \samp{1} and \samp{0}. |
Andrew M. Kuchling | 3a52ff6 | 2002-04-03 22:44:47 +0000 | [diff] [blame] | 321 | |
| 322 | \begin{seealso} |
| 323 | |
| 324 | \seepep{285}{Adding a bool type}{Written and implemented by GvR.} |
| 325 | |
| 326 | \end{seealso} |
| 327 | |
| 328 | |
| 329 | %====================================================================== |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 330 | %\section{Other Language Changes} |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 331 | |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 332 | %Here are the changes that Python 2.3 makes to the core language. |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 333 | |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 334 | %\begin{itemize} |
| 335 | %\item The \keyword{yield} statement is now always a keyword, as |
| 336 | %described in section~\ref{section-generators}. |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 337 | |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 338 | %\item Two new constants, \constant{True} and \constant{False} were |
| 339 | %added along with the built-in \class{bool} type, as described in |
| 340 | %section~\ref{section-bool}. |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 341 | |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 342 | %\item |
| 343 | %\end{itemize} |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 344 | |
| 345 | |
| 346 | %====================================================================== |
| 347 | \section{Specialized Object Allocator (pymalloc)\label{section-pymalloc}} |
| 348 | |
| 349 | An experimental feature added to Python 2.1 was a specialized object |
| 350 | allocator called pymalloc, written by Vladimir Marangozov. Pymalloc |
| 351 | was intended to be faster than the system \function{malloc()} and have |
| 352 | less memory overhead. The allocator uses C's \function{malloc()} |
| 353 | function to get large pools of memory, and then fulfills smaller |
| 354 | memory requests from these pools. |
| 355 | |
| 356 | In 2.1 and 2.2, pymalloc was an experimental feature and wasn't |
| 357 | enabled by default; you had to explicitly turn it on by providing the |
| 358 | \longprogramopt{with-pymalloc} option to the \program{configure} |
| 359 | script. In 2.3, pymalloc has had further enhancements and is now |
| 360 | enabled by default; you'll have to supply |
| 361 | \longprogramopt{without-pymalloc} to disable it. |
| 362 | |
| 363 | This change is transparent to code written in Python; however, |
| 364 | pymalloc may expose bugs in C extensions. Authors of C extension |
| 365 | modules should test their code with the object allocator enabled, |
| 366 | because some incorrect code may cause core dumps at runtime. There |
| 367 | are a bunch of memory allocation functions in Python's C API that have |
| 368 | previously been just aliases for the C library's \function{malloc()} |
| 369 | and \function{free()}, meaning that if you accidentally called |
| 370 | mismatched functions, the error wouldn't be noticeable. When the |
| 371 | object allocator is enabled, these functions aren't aliases of |
| 372 | \function{malloc()} and \function{free()} any more, and calling the |
| 373 | wrong function to free memory will get you a core dump. For example, |
| 374 | if memory was allocated using \function{PyMem_New()}, it has to be |
| 375 | freed using \function{PyMem_Del()}, not \function{free()}. A few |
| 376 | modules included with Python fell afoul of this and had to be fixed; |
| 377 | doubtless there are more third-party modules that will have the same |
| 378 | problem. |
| 379 | |
| 380 | As part of this change, the confusing multiple interfaces for |
| 381 | allocating memory have been consolidated down into two APIs. |
| 382 | Memory allocated with one API must not be freed with the other API. |
| 383 | |
| 384 | \begin{itemize} |
| 385 | \item To allocate and free an undistinguished chunk of memory, use |
| 386 | \cfunction{PyMem_Malloc()}, \cfunction{PyMem_Realloc()}, |
| 387 | \cfunction{PyMem_Free()}, and the other \cfunction{PyMem_*} |
| 388 | functions. |
| 389 | |
| 390 | \item To allocate and free Python objects, |
| 391 | use \cfunction{PyObject_New()}, \cfunction{PyObject_NewVar()}, and |
| 392 | \cfunction{PyObject_Del()}. |
| 393 | \end{itemize} |
| 394 | |
| 395 | Thanks to lots of work by Tim Peters, pymalloc in 2.3 also provides |
| 396 | debugging features to catch memory overwrites and doubled frees in |
| 397 | both extension modules and in the interpreter itself. To enable this |
| 398 | support, turn on the Python interpreter's debugging code by running |
| 399 | \program{configure} with \longprogramopt{with-pydebug}. |
| 400 | |
| 401 | \begin{seealso} |
| 402 | |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 403 | \seeurl{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/python/python/dist/src/Objects/obmalloc.c} |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 404 | {For the full details of the pymalloc implementation, see |
| 405 | the comments at the top of the file \file{Objects/obmalloc.c} in the |
| 406 | Python source code. The above link points to the file within the |
| 407 | SourceForge CVS browser.} |
| 408 | |
| 409 | \end{seealso} |
| 410 | |
| 411 | %====================================================================== |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 412 | \section{New and Improved Modules} |
| 413 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 414 | As usual, Python's standard modules had a number of enhancements and |
| 415 | bug fixes. Here's a partial list; consult the \file{Misc/NEWS} file |
| 416 | in the source tree, or the CVS logs, for a more complete list. |
| 417 | |
| 418 | \begin{itemize} |
| 419 | |
| 420 | \item One minor but far-reaching change is that the names of extension |
| 421 | types defined by the modules included with Python now contain the |
| 422 | module and a \samp{.} in front of the type name. For example, in |
| 423 | Python 2.2, if you created a socket and printed its |
| 424 | \member{__class__}, you'd get this output: |
| 425 | |
| 426 | \begin{verbatim} |
| 427 | >>> s = socket.socket() |
| 428 | >>> s.__class__ |
| 429 | <type 'socket'> |
| 430 | \end{verbatim} |
| 431 | |
| 432 | In 2.3, you get this: |
| 433 | \begin{verbatim} |
| 434 | >>> s.__class__ |
| 435 | <type '_socket.socket'> |
| 436 | \end{verbatim} |
| 437 | |
| 438 | \item The \method{strip()}, \method{lstrip()}, and \method{rstrip()} |
| 439 | string methods now have an optional argument for specifying the |
| 440 | characters to strip. The default is still to remove all whitespace |
| 441 | characters: |
| 442 | |
| 443 | \begin{verbatim} |
| 444 | >>> ' abc '.strip() |
| 445 | 'abc' |
| 446 | >>> '><><abc<><><>'.strip('<>') |
| 447 | 'abc' |
| 448 | >>> '><><abc<><><>\n'.strip('<>') |
| 449 | 'abc<><><>\n' |
| 450 | >>> u'\u4000\u4001abc\u4000'.strip(u'\u4000') |
| 451 | u'\u4001abc' |
| 452 | >>> |
| 453 | \end{verbatim} |
| 454 | |
| 455 | \item Another new string method is \method{zfill()}, originally a |
| 456 | function in the \module{string} module. \method{zfill()} pads a |
| 457 | numeric string with zeros on the left until it's the specified width. |
| 458 | Note that the \code{\%} operator is still more flexible and powerful |
| 459 | than \method{zfill()}. |
| 460 | |
| 461 | \begin{verbatim} |
| 462 | >>> '45'.zfill(4) |
| 463 | '0045' |
| 464 | >>> '12345'.zfill(4) |
| 465 | '12345' |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 466 | >>> 'goofy'.zfill(6) |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 467 | '0goofy' |
| 468 | \end{verbatim} |
| 469 | |
| 470 | \item Dictionaries have a new method, method{pop(\var{key})}, that |
| 471 | returns the value corresponding to \var{key} and removes that |
| 472 | key/value pair from the dictionary. \method{pop()} will raise a |
| 473 | \exception{KeyError} if the requsted key isn't present in the |
| 474 | dictionary: |
| 475 | |
| 476 | \begin{verbatim} |
| 477 | >>> d = {1:2} |
| 478 | >>> d |
| 479 | {1: 2} |
| 480 | >>> d.pop(4) |
| 481 | Traceback (most recent call last): |
| 482 | File ``<stdin>'', line 1, in ? |
| 483 | KeyError: 4 |
| 484 | >>> d.pop(1) |
| 485 | 2 |
| 486 | >>> d.pop(1) |
| 487 | Traceback (most recent call last): |
| 488 | File ``<stdin>'', line 1, in ? |
| 489 | KeyError: pop(): dictionary is empty |
| 490 | >>> d |
| 491 | {} |
| 492 | >>> |
| 493 | \end{verbatim} |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 494 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 495 | \item Two new functions, \function{killpg()} and \function{mknod()}, |
| 496 | were added to the \module{posix} module that underlies the \module{os} |
| 497 | module. |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 498 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 499 | \item Two new binary packagers were added to the Distutils. |
| 500 | \code{bdist_pkgtool} builds \file{.pkg} files to use with Solaris |
| 501 | \program{pkgtool}, and \code{bdist_sdux} builds \program{swinstall} |
| 502 | packages for use on HP-UX. (Contributed by Mark Alexander.) |
| 503 | |
| 504 | \item The \module{array} module now supports arrays of Unicode |
| 505 | characters using the \samp{u} format character. Arrays also |
| 506 | now support using the \code{+=} assignment operator to add another array's |
| 507 | contents, and the \code{*=} assignment operator to repeat an array. |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 508 | (Contributed by Jason Orendorff.) |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 509 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 510 | \item The \module{grp} module now returns enhanced tuples: |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 511 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 512 | \begin{verbatim} |
| 513 | >>> import grp |
| 514 | >>> g = grp.getgrnam('amk') |
| 515 | >>> g.gr_name, g.gr_gid |
| 516 | ('amk', 500) |
| 517 | \end{verbatim} |
Andrew M. Kuchling | f367651 | 2002-04-15 02:27:55 +0000 | [diff] [blame] | 518 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 519 | \item The \module{readline} module also gained a number of new |
| 520 | functions: \function{get_history_item()}, |
| 521 | \function{get_current_history_length()}, and \function{redisplay()}. |
Andrew M. Kuchling | 8e8af6e | 2002-04-15 14:05:59 +0000 | [diff] [blame] | 522 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 523 | \end{itemize} |
| 524 | |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 525 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 526 | % ====================================================================== |
| 527 | \section{Build and C API Changes} |
| 528 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 529 | 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] | 530 | |
| 531 | \begin{itemize} |
| 532 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 533 | \item Python can now optionally be built as a shared library |
| 534 | (\file{libpython2.3.so}) by supplying \longprogramopt{enable-shared} |
Andrew M. Kuchling | fad2f59 | 2002-05-10 21:00:05 +0000 | [diff] [blame] | 535 | when running Python's \file{configure} script. (Contributed by Ondrej |
| 536 | Palkovsky.) |
Andrew M. Kuchling | f4dd65d | 2002-04-01 19:28:09 +0000 | [diff] [blame] | 537 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 538 | \item The \cfunction{PyArg_NoArgs()} macro is now deprecated, and code |
| 539 | that |
| 540 | uses it should be changed to use \code{PyArg_ParseTuple(args, "")} |
| 541 | instead. |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 542 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 543 | \item A new function, \cfunction{PyObject_DelItemString(\var{mapping}, |
| 544 | char *\var{key})} was added |
| 545 | as shorthand for |
| 546 | \code{PyObject_DelItem(\var{mapping}, PyString_New(\var{key})}. |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 547 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 548 | \item The source code for the Expat XML parser is now included with |
| 549 | the Python source, so the \module{pyexpat} module is no longer |
| 550 | dependent on having a system library containing Expat. |
| 551 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 552 | \item File objects now manage their internal string buffer |
| 553 | differently by increasing it exponentially when needed. |
| 554 | This results in the benchmark tests in \file{Lib/test/test_bufio.py} |
| 555 | speeding up from 57 seconds to 1.7 seconds, according to one |
| 556 | measurement. |
| 557 | |
| 558 | \item XXX Introduce two new flag bits that can be set in a PyMethodDef method |
Andrew M. Kuchling | 45afd54 | 2002-04-02 14:25:25 +0000 | [diff] [blame] | 559 | descriptor, as used for the tp_methods slot of a type. These new flag |
| 560 | bits are both optional, and mutually exclusive. Most methods will not |
| 561 | use either. These flags are used to create special method types which |
| 562 | exist in the same namespace as normal methods without having to use |
| 563 | tedious construction code to insert the new special method objects in |
| 564 | the type's tp_dict after PyType_Ready() has been called. |
| 565 | |
| 566 | If METH_CLASS is specified, the method will represent a class method |
| 567 | like that returned by the classmethod() built-in. |
| 568 | |
| 569 | If METH_STATIC is specified, the method will represent a static method |
| 570 | like that returned by the staticmethod() built-in. |
| 571 | |
| 572 | These flags may not be used in the PyMethodDef table for modules since |
| 573 | these special method types are not meaningful in that case; a |
| 574 | ValueError will be raised if these flags are found in that context. |
Andrew M. Kuchling | 45afd54 | 2002-04-02 14:25:25 +0000 | [diff] [blame] | 575 | |
Andrew M. Kuchling | 821013e | 2002-05-06 17:46:39 +0000 | [diff] [blame] | 576 | \end{itemize} |
| 577 | |
| 578 | \subsection{Port-Specific Changes} |
| 579 | |
| 580 | XXX write this |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 581 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 582 | XXX OS/2 EMX port |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 583 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 584 | XXX MacOS: Weaklink most toolbox modules, improving backward |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 585 | compatibility. Modules will no longer fail to load if a single routine |
| 586 | is missing on the curent OS version, in stead calling the missing |
| 587 | routine will raise an exception. Should finally fix 531398. 2.2.1 |
| 588 | candidate. Also blacklisted some constants with definitions that |
| 589 | were not Python-compatible. |
| 590 | |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 591 | XXX Checked in Sean Reifschneider's RPM spec file and patches. |
Fred Drake | 03e1031 | 2002-03-26 19:17:43 +0000 | [diff] [blame] | 592 | |
| 593 | |
| 594 | %====================================================================== |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 595 | \section{Other Changes and Fixes} |
| 596 | |
| 597 | Finally, there are various miscellaneous fixes: |
| 598 | |
| 599 | \begin{itemize} |
| 600 | |
| 601 | \item The tools used to build the documentation now work under Cygwin |
| 602 | as well as \UNIX. |
| 603 | |
| 604 | \end{itemize} |
| 605 | |
| 606 | %====================================================================== |
Fred Drake | 03e1031 | 2002-03-26 19:17:43 +0000 | [diff] [blame] | 607 | \section{Acknowledgements \label{acks}} |
| 608 | |
Andrew M. Kuchling | 03594bb | 2002-03-27 02:29:48 +0000 | [diff] [blame] | 609 | The author would like to thank the following people for offering |
| 610 | suggestions, corrections and assistance with various drafts of this |
Andrew M. Kuchling | 517109b | 2002-05-07 21:01:16 +0000 | [diff] [blame] | 611 | article: Fred~L. Drake, Jr., Detlef Lannert. |
Fred Drake | 03e1031 | 2002-03-26 19:17:43 +0000 | [diff] [blame] | 612 | |
| 613 | \end{document} |