Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1 | \documentclass{howto} |
| 2 | \usepackage{distutils} |
| 3 | % $Id$ |
| 4 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 5 | % Fix XXX comments |
| 6 | % Distutils upload |
| 7 | % The easy_install stuff |
| 8 | % xml.etree section |
| 9 | % added sqlite3 |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 10 | |
| 11 | \title{What's New in Python 2.5} |
Andrew M. Kuchling | 2cdb23e | 2006-04-05 13:59:01 +0000 | [diff] [blame^] | 12 | \release{0.1} |
Andrew M. Kuchling | 92e2495 | 2004-12-03 13:54:09 +0000 | [diff] [blame] | 13 | \author{A.M. Kuchling} |
| 14 | \authoraddress{\email{amk@amk.ca}} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 15 | |
| 16 | \begin{document} |
| 17 | \maketitle |
| 18 | \tableofcontents |
| 19 | |
| 20 | This article explains the new features in Python 2.5. No release date |
Andrew M. Kuchling | 5eefdca | 2006-02-08 11:36:09 +0000 | [diff] [blame] | 21 | for Python 2.5 has been set; it will probably be released in the |
Andrew M. Kuchling | d96a6ac | 2006-04-04 19:17:34 +0000 | [diff] [blame] | 22 | autumn of 2006. \pep{356} describes the planned release schedule. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 23 | |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 24 | (This is still an early draft, and some sections are still skeletal or |
| 25 | completely missing. Comments on the present material will still be |
| 26 | welcomed.) |
| 27 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 28 | % XXX Compare with previous release in 2 - 3 sentences here. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 29 | |
| 30 | This article doesn't attempt to provide a complete specification of |
| 31 | the new features, but instead provides a convenient overview. For |
| 32 | full details, you should refer to the documentation for Python 2.5. |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 33 | % XXX add hyperlink when the documentation becomes available online. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 34 | If you want to understand the complete implementation and design |
| 35 | rationale, refer to the PEP for a particular new feature. |
| 36 | |
| 37 | |
| 38 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 39 | \section{PEP 308: Conditional Expressions} |
| 40 | |
Andrew M. Kuchling | e362d93 | 2006-03-09 13:56:25 +0000 | [diff] [blame] | 41 | For a long time, people have been requesting a way to write |
| 42 | conditional expressions, expressions that return value A or value B |
| 43 | depending on whether a Boolean value is true or false. A conditional |
| 44 | expression lets you write a single assignment statement that has the |
| 45 | same effect as the following: |
| 46 | |
| 47 | \begin{verbatim} |
| 48 | if condition: |
| 49 | x = true_value |
| 50 | else: |
| 51 | x = false_value |
| 52 | \end{verbatim} |
| 53 | |
| 54 | There have been endless tedious discussions of syntax on both |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 55 | python-dev and comp.lang.python. A vote was even held that found the |
| 56 | majority of voters wanted conditional expressions in some form, |
| 57 | but there was no syntax that was preferred by a clear majority. |
| 58 | Candidates included C's \code{cond ? true_v : false_v}, |
Andrew M. Kuchling | e362d93 | 2006-03-09 13:56:25 +0000 | [diff] [blame] | 59 | \code{if cond then true_v else false_v}, and 16 other variations. |
| 60 | |
| 61 | GvR eventually chose a surprising syntax: |
| 62 | |
| 63 | \begin{verbatim} |
| 64 | x = true_value if condition else false_value |
| 65 | \end{verbatim} |
| 66 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 67 | Evaluation is still lazy as in existing Boolean expressions, so the |
| 68 | order of evaluation jumps around a bit. The \var{condition} |
| 69 | expression in the middle is evaluated first, and the \var{true_value} |
| 70 | expression is evaluated only if the condition was true. Similarly, |
| 71 | the \var{false_value} expression is only evaluated when the condition |
| 72 | is false. |
Andrew M. Kuchling | e362d93 | 2006-03-09 13:56:25 +0000 | [diff] [blame] | 73 | |
| 74 | This syntax may seem strange and backwards; why does the condition go |
| 75 | in the \emph{middle} of the expression, and not in the front as in C's |
| 76 | \code{c ? x : y}? The decision was checked by applying the new syntax |
| 77 | to the modules in the standard library and seeing how the resulting |
| 78 | code read. In many cases where a conditional expression is used, one |
| 79 | value seems to be the 'common case' and one value is an 'exceptional |
| 80 | case', used only on rarer occasions when the condition isn't met. The |
| 81 | conditional syntax makes this pattern a bit more obvious: |
| 82 | |
| 83 | \begin{verbatim} |
| 84 | contents = ((doc + '\n') if doc else '') |
| 85 | \end{verbatim} |
| 86 | |
| 87 | I read the above statement as meaning ``here \var{contents} is |
Andrew M. Kuchling | d0fcc02 | 2006-03-09 13:57:28 +0000 | [diff] [blame] | 88 | usually assigned a value of \code{doc+'\e n'}; sometimes |
Andrew M. Kuchling | e362d93 | 2006-03-09 13:56:25 +0000 | [diff] [blame] | 89 | \var{doc} is empty, in which special case an empty string is returned.'' |
| 90 | I doubt I will use conditional expressions very often where there |
| 91 | isn't a clear common and uncommon case. |
| 92 | |
| 93 | There was some discussion of whether the language should require |
| 94 | surrounding conditional expressions with parentheses. The decision |
| 95 | was made to \emph{not} require parentheses in the Python language's |
| 96 | grammar, but as a matter of style I think you should always use them. |
| 97 | Consider these two statements: |
| 98 | |
| 99 | \begin{verbatim} |
| 100 | # First version -- no parens |
| 101 | level = 1 if logging else 0 |
| 102 | |
| 103 | # Second version -- with parens |
| 104 | level = (1 if logging else 0) |
| 105 | \end{verbatim} |
| 106 | |
| 107 | In the first version, I think a reader's eye might group the statement |
| 108 | into 'level = 1', 'if logging', 'else 0', and think that the condition |
| 109 | decides whether the assignment to \var{level} is performed. The |
| 110 | second version reads better, in my opinion, because it makes it clear |
| 111 | that the assignment is always performed and the choice is being made |
| 112 | between two values. |
| 113 | |
| 114 | Another reason for including the brackets: a few odd combinations of |
| 115 | list comprehensions and lambdas could look like incorrect conditional |
| 116 | expressions. See \pep{308} for some examples. If you put parentheses |
| 117 | around your conditional expressions, you won't run into this case. |
| 118 | |
| 119 | |
| 120 | \begin{seealso} |
| 121 | |
| 122 | \seepep{308}{Conditional Expressions}{PEP written by |
| 123 | Guido van Rossum and Raymond D. Hettinger; implemented by Thomas |
| 124 | Wouters.} |
| 125 | |
| 126 | \end{seealso} |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 127 | |
| 128 | |
| 129 | %====================================================================== |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 130 | \section{PEP 309: Partial Function Application} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 131 | |
Andrew M. Kuchling | b1c96fd | 2005-03-20 21:42:04 +0000 | [diff] [blame] | 132 | The \module{functional} module is intended to contain tools for |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 133 | functional-style programming. Currently it only contains a |
| 134 | \class{partial()} function, but new functions will probably be added |
| 135 | in future versions of Python. |
Andrew M. Kuchling | b1c96fd | 2005-03-20 21:42:04 +0000 | [diff] [blame] | 136 | |
Andrew M. Kuchling | 4b000cd | 2005-04-09 15:51:44 +0000 | [diff] [blame] | 137 | For programs written in a functional style, it can be useful to |
| 138 | construct variants of existing functions that have some of the |
| 139 | parameters filled in. Consider a Python function \code{f(a, b, c)}; |
| 140 | you could create a new function \code{g(b, c)} that was equivalent to |
| 141 | \code{f(1, b, c)}. This is called ``partial function application'', |
| 142 | and is provided by the \class{partial} class in the new |
| 143 | \module{functional} module. |
| 144 | |
| 145 | The constructor for \class{partial} takes the arguments |
| 146 | \code{(\var{function}, \var{arg1}, \var{arg2}, ... |
| 147 | \var{kwarg1}=\var{value1}, \var{kwarg2}=\var{value2})}. The resulting |
| 148 | object is callable, so you can just call it to invoke \var{function} |
| 149 | with the filled-in arguments. |
| 150 | |
| 151 | Here's a small but realistic example: |
| 152 | |
| 153 | \begin{verbatim} |
| 154 | import functional |
| 155 | |
| 156 | def log (message, subsystem): |
| 157 | "Write the contents of 'message' to the specified subsystem." |
| 158 | print '%s: %s' % (subsystem, message) |
| 159 | ... |
| 160 | |
| 161 | server_log = functional.partial(log, subsystem='server') |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 162 | server_log('Unable to open socket') |
Andrew M. Kuchling | 4b000cd | 2005-04-09 15:51:44 +0000 | [diff] [blame] | 163 | \end{verbatim} |
| 164 | |
Andrew M. Kuchling | 6af7fe0 | 2005-08-02 17:20:36 +0000 | [diff] [blame] | 165 | Here's another example, from a program that uses PyGTk. Here a |
| 166 | context-sensitive pop-up menu is being constructed dynamically. The |
| 167 | callback provided for the menu option is a partially applied version |
| 168 | of the \method{open_item()} method, where the first argument has been |
| 169 | provided. |
Andrew M. Kuchling | 4b000cd | 2005-04-09 15:51:44 +0000 | [diff] [blame] | 170 | |
Andrew M. Kuchling | 6af7fe0 | 2005-08-02 17:20:36 +0000 | [diff] [blame] | 171 | \begin{verbatim} |
| 172 | ... |
| 173 | class Application: |
| 174 | def open_item(self, path): |
| 175 | ... |
| 176 | def init (self): |
| 177 | open_func = functional.partial(self.open_item, item_path) |
| 178 | popup_menu.append( ("Open", open_func, 1) ) |
| 179 | \end{verbatim} |
Andrew M. Kuchling | b1c96fd | 2005-03-20 21:42:04 +0000 | [diff] [blame] | 180 | |
| 181 | |
| 182 | \begin{seealso} |
| 183 | |
| 184 | \seepep{309}{Partial Function Application}{PEP proposed and written by |
| 185 | Peter Harris; implemented by Hye-Shik Chang, with adaptations by |
| 186 | Raymond Hettinger.} |
| 187 | |
| 188 | \end{seealso} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 189 | |
| 190 | |
| 191 | %====================================================================== |
Fred Drake | db7b002 | 2005-03-20 22:19:47 +0000 | [diff] [blame] | 192 | \section{PEP 314: Metadata for Python Software Packages v1.1} |
| 193 | |
Andrew M. Kuchling | d8d732e | 2005-04-09 23:59:41 +0000 | [diff] [blame] | 194 | Some simple dependency support was added to Distutils. The |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 195 | \function{setup()} function now has \code{requires}, \code{provides}, |
| 196 | and \code{obsoletes} keyword parameters. When you build a source |
| 197 | distribution using the \code{sdist} command, the dependency |
| 198 | information will be recorded in the \file{PKG-INFO} file. |
Andrew M. Kuchling | d8d732e | 2005-04-09 23:59:41 +0000 | [diff] [blame] | 199 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 200 | Another new keyword parameter is \code{download_url}, which should be |
| 201 | set to a URL for the package's source code. This means it's now |
| 202 | possible to look up an entry in the package index, determine the |
| 203 | dependencies for a package, and download the required packages. |
Andrew M. Kuchling | d8d732e | 2005-04-09 23:59:41 +0000 | [diff] [blame] | 204 | |
| 205 | % XXX put example here |
| 206 | |
| 207 | \begin{seealso} |
| 208 | |
| 209 | \seepep{314}{Metadata for Python Software Packages v1.1}{PEP proposed |
| 210 | and written by A.M. Kuchling, Richard Jones, and Fred Drake; |
| 211 | implemented by Richard Jones and Fred Drake.} |
| 212 | |
| 213 | \end{seealso} |
Fred Drake | db7b002 | 2005-03-20 22:19:47 +0000 | [diff] [blame] | 214 | |
| 215 | |
| 216 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 217 | \section{PEP 328: Absolute and Relative Imports} |
| 218 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 219 | The simpler part of PEP 328 was implemented in Python 2.4: parentheses |
| 220 | could now be used to enclose the names imported from a module using |
| 221 | the \code{from ... import ...} statement, making it easier to import |
| 222 | many different names. |
| 223 | |
| 224 | The more complicated part has been implemented in Python 2.5: |
| 225 | importing a module can be specified to use absolute or |
| 226 | package-relative imports. The plan is to move toward making absolute |
| 227 | imports the default in future versions of Python. |
| 228 | |
| 229 | Let's say you have a package directory like this: |
| 230 | \begin{verbatim} |
| 231 | pkg/ |
| 232 | pkg/__init__.py |
| 233 | pkg/main.py |
| 234 | pkg/string.py |
| 235 | \end{verbatim} |
| 236 | |
| 237 | This defines a package named \module{pkg} containing the |
| 238 | \module{pkg.main} and \module{pkg.string} submodules. |
| 239 | |
| 240 | Consider the code in the \file{main.py} module. What happens if it |
| 241 | executes the statement \code{import string}? In Python 2.4 and |
| 242 | earlier, it will first look in the package's directory to perform a |
| 243 | relative import, finds \file{pkg/string.py}, imports the contents of |
| 244 | that file as the \module{pkg.string} module, and that module is bound |
| 245 | to the name \samp{string} in the \module{pkg.main} module's namespace. |
| 246 | |
| 247 | That's fine if \module{pkg.string} was what you wanted. But what if |
| 248 | you wanted Python's standard \module{string} module? There's no clean |
| 249 | way to ignore \module{pkg.string} and look for the standard module; |
| 250 | generally you had to look at the contents of \code{sys.modules}, which |
| 251 | is slightly unclean. |
| 252 | Holger Krekel's py.std package provides a tidier way to perform |
| 253 | imports from the standard library, \code{from py.std import string}, |
| 254 | % XXX correct attribution? |
| 255 | % XXX is that import correct? |
| 256 | but that package isn't available on all Python installations. |
| 257 | |
| 258 | Reading code which relies on relative imports is also less clear, |
| 259 | because a reader may be confused about which module, \module{string} |
| 260 | or \module{pkg.string}, is intended to be used. Python users soon |
| 261 | learned not to duplicate the names of standard library modules in the |
| 262 | names of their packages' submodules, but you can't protect against |
| 263 | having your submodule's name being used for a new module added in a |
| 264 | future version of Python. |
| 265 | |
| 266 | In Python 2.5, you can switch \keyword{import}'s behaviour to |
| 267 | absolute imports using a \code{from __future__ import absolute_import} |
| 268 | directive. This absolute-import behaviour will become the default in |
| 269 | a future version (probably Python 2.6). Once absolute-imports |
| 270 | are the default, \code{import string} will |
| 271 | always find the standard library's version. |
| 272 | It's suggested that users should begin using absolute imports as much |
| 273 | as possible, so it's preferable to begin writing \code{from pkg import |
| 274 | string} in your code. |
| 275 | |
| 276 | Relative imports are still possible by adding a leading period |
| 277 | to the module name when using the \code{from ... import} form: |
| 278 | |
| 279 | \begin{verbatim} |
| 280 | # Import names from pkg.string |
| 281 | from .string import name1, name2 |
| 282 | # Import pkg.string |
| 283 | from . import string |
| 284 | \end{verbatim} |
| 285 | |
| 286 | This imports the \module{string} module relative to the current |
| 287 | package, so in \module{pkg.main} this will import \var{name1} and |
| 288 | \var{name2} from \module{pkg.string}. Additional leading periods |
| 289 | perform the relative import starting from the parent of the current |
| 290 | package. For example, code in the \module{A.B.C} module can do: |
| 291 | |
| 292 | \begin{verbatim} |
| 293 | from . import D # Imports A.B.D |
| 294 | from .. import E # Imports A.E |
| 295 | from ..F import G # Imports A.F.G |
| 296 | \end{verbatim} |
| 297 | |
| 298 | Leading periods cannot be used with the \code{import \var{modname}} |
| 299 | form of the import statement, only the \code{from ... import} form. |
| 300 | |
| 301 | \begin{seealso} |
| 302 | |
| 303 | \seepep{328}{Imports: Multi-Line and Absolute/Relative}{PEP written |
| 304 | by Aahz; implemented by XXX.} |
| 305 | |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 306 | %\seeurl{http://codespeak.net/py/current/doc/misc.html\#mapping-the-standard-python-library-into-py}{py.std} |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 307 | |
| 308 | \end{seealso} |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 309 | |
| 310 | |
| 311 | %====================================================================== |
Andrew M. Kuchling | 21d3a7c | 2006-03-15 11:53:09 +0000 | [diff] [blame] | 312 | \section{PEP 338: Executing Modules as Scripts} |
| 313 | |
Andrew M. Kuchling | b182db4 | 2006-03-17 21:48:46 +0000 | [diff] [blame] | 314 | The \programopt{-m} switch added in Python 2.4 to execute a module as |
| 315 | a script gained a few more abilities. Instead of being implemented in |
| 316 | C code inside the Python interpreter, the switch now uses an |
| 317 | implementation in a new module, \module{runpy}. |
| 318 | |
| 319 | The \module{runpy} module implements a more sophisticated import |
| 320 | mechanism so that it's now possible to run modules in a package such |
| 321 | as \module{pychecker.checker}. The module also supports alternative |
| 322 | import mechanisms such as the \module{zipimport} module. (This means |
| 323 | you can add a .zip archive's path to \code{sys.path} and then use the |
| 324 | \programopt{-m} switch to execute code from the archive. |
| 325 | |
| 326 | |
| 327 | \begin{seealso} |
| 328 | |
| 329 | \seepep{338}{Executing modules as scripts}{PEP written and |
| 330 | implemented by Nick Coghlan.} |
| 331 | |
| 332 | \end{seealso} |
Andrew M. Kuchling | 21d3a7c | 2006-03-15 11:53:09 +0000 | [diff] [blame] | 333 | |
| 334 | |
| 335 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 336 | \section{PEP 341: Unified try/except/finally} |
| 337 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 338 | Until Python 2.5, the \keyword{try} statement came in two |
| 339 | flavours. You could use a \keyword{finally} block to ensure that code |
| 340 | is always executed, or a number of \keyword{except} blocks to catch an |
| 341 | exception. You couldn't combine both \keyword{except} blocks and a |
| 342 | \keyword{finally} block, because generating the right bytecode for the |
| 343 | combined version was complicated and it wasn't clear what the |
| 344 | semantics of the combined should be. |
| 345 | |
| 346 | GvR spent some time working with Java, which does support the |
| 347 | equivalent of combining \keyword{except} blocks and a |
| 348 | \keyword{finally} block, and this clarified what the statement should |
| 349 | mean. In Python 2.5, you can now write: |
| 350 | |
| 351 | \begin{verbatim} |
| 352 | try: |
| 353 | block-1 ... |
| 354 | except Exception1: |
| 355 | handler-1 ... |
| 356 | except Exception2: |
| 357 | handler-2 ... |
| 358 | else: |
| 359 | else-block |
| 360 | finally: |
| 361 | final-block |
| 362 | \end{verbatim} |
| 363 | |
| 364 | The code in \var{block-1} is executed. If the code raises an |
| 365 | exception, the handlers are tried in order: \var{handler-1}, |
| 366 | \var{handler-2}, ... If no exception is raised, the \var{else-block} |
| 367 | is executed. No matter what happened previously, the |
| 368 | \var{final-block} is executed once the code block is complete and any |
| 369 | raised exceptions handled. Even if there's an error in an exception |
| 370 | handler or the \var{else-block} and a new exception is raised, the |
| 371 | \var{final-block} is still executed. |
| 372 | |
| 373 | \begin{seealso} |
| 374 | |
| 375 | \seepep{341}{Unifying try-except and try-finally}{PEP written by Georg Brandl; |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 376 | implementation by Thomas Lee.} |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 377 | |
| 378 | \end{seealso} |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 379 | |
| 380 | |
| 381 | %====================================================================== |
Andrew M. Kuchling | a2e21cb | 2005-08-02 17:13:21 +0000 | [diff] [blame] | 382 | \section{PEP 342: New Generator Features} |
| 383 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 384 | Python 2.5 adds a simple way to pass values \emph{into} a generator. |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 385 | As introduced in Python 2.3, generators only produce output; once a |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 386 | generator's code is invoked to create an iterator, there's no way to |
| 387 | pass any new information into the function when its execution is |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 388 | resumed. Sometimes the ability to pass in some information would be |
| 389 | useful. Hackish solutions to this include making the generator's code |
| 390 | look at a global variable and then changing the global variable's |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 391 | value, or passing in some mutable object that callers then modify. |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 392 | |
| 393 | To refresh your memory of basic generators, here's a simple example: |
| 394 | |
| 395 | \begin{verbatim} |
| 396 | def counter (maximum): |
| 397 | i = 0 |
| 398 | while i < maximum: |
| 399 | yield i |
| 400 | i += 1 |
| 401 | \end{verbatim} |
| 402 | |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 403 | When you call \code{counter(10)}, the result is an iterator that |
| 404 | returns the values from 0 up to 9. On encountering the |
| 405 | \keyword{yield} statement, the iterator returns the provided value and |
| 406 | suspends the function's execution, preserving the local variables. |
| 407 | Execution resumes on the following call to the iterator's |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 408 | \method{next()} method, picking up after the \keyword{yield} statement. |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 409 | |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 410 | In Python 2.3, \keyword{yield} was a statement; it didn't return any |
| 411 | value. In 2.5, \keyword{yield} is now an expression, returning a |
| 412 | value that can be assigned to a variable or otherwise operated on: |
Andrew M. Kuchling | a2e21cb | 2005-08-02 17:13:21 +0000 | [diff] [blame] | 413 | |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 414 | \begin{verbatim} |
| 415 | val = (yield i) |
| 416 | \end{verbatim} |
| 417 | |
| 418 | I recommend that you always put parentheses around a \keyword{yield} |
| 419 | expression when you're doing something with the returned value, as in |
| 420 | the above example. The parentheses aren't always necessary, but it's |
| 421 | easier to always add them instead of having to remember when they're |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 422 | needed.\footnote{The exact rules are that a \keyword{yield}-expression must |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 423 | always be parenthesized except when it occurs at the top-level |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 424 | expression on the right-hand side of an assignment, meaning you can |
| 425 | write \code{val = yield i} but have to use parentheses when there's an |
| 426 | operation, as in \code{val = (yield i) + 12}.} |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 427 | |
| 428 | Values are sent into a generator by calling its |
| 429 | \method{send(\var{value})} method. The generator's code is then |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 430 | resumed and the \keyword{yield} expression returns the specified |
| 431 | \var{value}. If the regular \method{next()} method is called, the |
| 432 | \keyword{yield} returns \constant{None}. |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 433 | |
| 434 | Here's the previous example, modified to allow changing the value of |
| 435 | the internal counter. |
| 436 | |
| 437 | \begin{verbatim} |
| 438 | def counter (maximum): |
| 439 | i = 0 |
| 440 | while i < maximum: |
| 441 | val = (yield i) |
| 442 | # If value provided, change counter |
| 443 | if val is not None: |
| 444 | i = val |
| 445 | else: |
| 446 | i += 1 |
| 447 | \end{verbatim} |
| 448 | |
| 449 | And here's an example of changing the counter: |
| 450 | |
| 451 | \begin{verbatim} |
| 452 | >>> it = counter(10) |
| 453 | >>> print it.next() |
| 454 | 0 |
| 455 | >>> print it.next() |
| 456 | 1 |
| 457 | >>> print it.send(8) |
| 458 | 8 |
| 459 | >>> print it.next() |
| 460 | 9 |
| 461 | >>> print it.next() |
| 462 | Traceback (most recent call last): |
| 463 | File ``t.py'', line 15, in ? |
| 464 | print it.next() |
| 465 | StopIteration |
Andrew M. Kuchling | c203370 | 2005-08-29 13:30:12 +0000 | [diff] [blame] | 466 | \end{verbatim} |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 467 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 468 | Because \keyword{yield} will often be returning \constant{None}, you |
| 469 | should always check for this case. Don't just use its value in |
| 470 | expressions unless you're sure that the \method{send()} method |
| 471 | will be the only method used resume your generator function. |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 472 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 473 | In addition to \method{send()}, there are two other new methods on |
| 474 | generators: |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 475 | |
| 476 | \begin{itemize} |
| 477 | |
| 478 | \item \method{throw(\var{type}, \var{value}=None, |
| 479 | \var{traceback}=None)} is used to raise an exception inside the |
| 480 | generator; the exception is raised by the \keyword{yield} expression |
| 481 | where the generator's execution is paused. |
| 482 | |
| 483 | \item \method{close()} raises a new \exception{GeneratorExit} |
| 484 | exception inside the generator to terminate the iteration. |
| 485 | On receiving this |
| 486 | exception, the generator's code must either raise |
| 487 | \exception{GeneratorExit} or \exception{StopIteration}; catching the |
| 488 | exception and doing anything else is illegal and will trigger |
| 489 | a \exception{RuntimeError}. \method{close()} will also be called by |
| 490 | Python's garbage collection when the generator is garbage-collected. |
| 491 | |
| 492 | If you need to run cleanup code in case of a \exception{GeneratorExit}, |
| 493 | I suggest using a \code{try: ... finally:} suite instead of |
| 494 | catching \exception{GeneratorExit}. |
| 495 | |
| 496 | \end{itemize} |
| 497 | |
| 498 | The cumulative effect of these changes is to turn generators from |
| 499 | one-way producers of information into both producers and consumers. |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 500 | |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 501 | Generators also become \emph{coroutines}, a more generalized form of |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 502 | subroutines. Subroutines are entered at one point and exited at |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 503 | another point (the top of the function, and a \keyword{return |
| 504 | statement}), but coroutines can be entered, exited, and resumed at |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 505 | many different points (the \keyword{yield} statements). We'll have to |
| 506 | figure out patterns for using coroutines effectively in Python. |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 507 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 508 | The addition of the \method{close()} method has one side effect that |
| 509 | isn't obvious. \method{close()} is called when a generator is |
| 510 | garbage-collected, so this means the generator's code gets one last |
| 511 | chance to run before the generator is destroyed, and this last chance |
| 512 | means that \code{try...finally} statements in generators can now be |
| 513 | guaranteed to work; the \keyword{finally} clause will now always get a |
| 514 | chance to run. The syntactic restriction that you couldn't mix |
| 515 | \keyword{yield} statements with a \code{try...finally} suite has |
| 516 | therefore been removed. This seems like a minor bit of language |
| 517 | trivia, but using generators and \code{try...finally} is actually |
| 518 | necessary in order to implement the \keyword{with} statement |
| 519 | described by PEP 343. We'll look at this new statement in the following |
| 520 | section. |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 521 | |
Andrew M. Kuchling | a2e21cb | 2005-08-02 17:13:21 +0000 | [diff] [blame] | 522 | \begin{seealso} |
| 523 | |
| 524 | \seepep{342}{Coroutines via Enhanced Generators}{PEP written by |
| 525 | Guido van Rossum and Phillip J. Eby; |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 526 | implemented by Phillip J. Eby. Includes examples of |
| 527 | some fancier uses of generators as coroutines.} |
| 528 | |
| 529 | \seeurl{http://en.wikipedia.org/wiki/Coroutine}{The Wikipedia entry for |
| 530 | coroutines.} |
| 531 | |
Neal Norwitz | 0917988 | 2006-03-04 23:31:45 +0000 | [diff] [blame] | 532 | \seeurl{http://www.sidhe.org/\~{}dan/blog/archives/000178.html}{An |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 533 | explanation of coroutines from a Perl point of view, written by Dan |
| 534 | Sugalski.} |
Andrew M. Kuchling | a2e21cb | 2005-08-02 17:13:21 +0000 | [diff] [blame] | 535 | |
| 536 | \end{seealso} |
| 537 | |
| 538 | |
| 539 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 540 | \section{PEP 343: The 'with' statement} |
| 541 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 542 | The \keyword{with} statement allows a clearer |
| 543 | version of code that uses \code{try...finally} blocks |
| 544 | |
| 545 | First, I'll discuss the statement as it will commonly be used, and |
| 546 | then I'll discuss the detailed implementation and how to write objects |
| 547 | (called ``context managers'') that can be used with this statement. |
| 548 | Most people, who will only use \keyword{with} in company with an |
| 549 | existing object, don't need to know these details, but can |
| 550 | Authors of new context managers will need to understand the |
| 551 | |
| 552 | The \keyword{with} statement is a new control-flow structure whose |
| 553 | basic structure is: |
| 554 | |
| 555 | \begin{verbatim} |
| 556 | with expression as variable: |
| 557 | with-block |
| 558 | \end{verbatim} |
| 559 | |
| 560 | The expression is evaluated, and it should result in a type of object |
| 561 | that's called a context manager. The context manager can return a |
| 562 | value that will be bound to the name \var{variable}. (Note carefully: |
| 563 | \var{variable} is \emph{not} assigned the result of \var{expression}. |
| 564 | One method of the context manager is run before \var{with-block} is |
| 565 | executed, and another method is run after the block is done, even if |
| 566 | the block raised an exception. |
| 567 | |
| 568 | To enable the statement in Python 2.5, you need |
| 569 | to add the following directive to your module: |
| 570 | |
| 571 | \begin{verbatim} |
| 572 | from __future__ import with_statement |
| 573 | \end{verbatim} |
| 574 | |
| 575 | Some standard Python objects can now behave as context managers. For |
| 576 | example, file objects: |
| 577 | |
| 578 | \begin{verbatim} |
| 579 | with open('/etc/passwd', 'r') as f: |
| 580 | for line in f: |
| 581 | print line |
| 582 | |
| 583 | # f has been automatically closed at this point. |
| 584 | \end{verbatim} |
| 585 | |
| 586 | The \module{threading} module's locks and condition variables |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 587 | also support the \keyword{with} statement: |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 588 | |
| 589 | \begin{verbatim} |
| 590 | lock = threading.Lock() |
| 591 | with lock: |
| 592 | # Critical section of code |
| 593 | ... |
| 594 | \end{verbatim} |
| 595 | |
| 596 | The lock is acquired before the block is executed, and released once |
| 597 | the block is complete. |
| 598 | |
| 599 | The \module{decimal} module's contexts, which encapsulate the desired |
| 600 | precision and rounding characteristics for computations, can also be |
| 601 | used as context managers. |
| 602 | |
| 603 | \begin{verbatim} |
| 604 | import decimal |
| 605 | |
| 606 | v1 = decimal.Decimal('578') |
| 607 | |
| 608 | # Displays with default precision of 28 digits |
| 609 | print v1.sqrt() |
| 610 | |
| 611 | with decimal.Context(prec=16): |
| 612 | # All code in this block uses a precision of 16 digits. |
| 613 | # The original context is restored on exiting the block. |
| 614 | print v1.sqrt() |
| 615 | \end{verbatim} |
| 616 | |
| 617 | \subsection{Writing Context Managers} |
| 618 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 619 | % XXX write this |
| 620 | |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 621 | This section still needs to be written. |
| 622 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 623 | The new \module{contextlib} module provides some functions and a |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 624 | decorator that are useful for writing context managers. |
| 625 | Future versions will go into more detail. |
| 626 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 627 | % XXX describe further |
| 628 | |
| 629 | \begin{seealso} |
| 630 | |
| 631 | \seepep{343}{The ``with'' statement}{PEP written by |
| 632 | Guido van Rossum and Nick Coghlan. } |
| 633 | |
| 634 | \end{seealso} |
| 635 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 636 | |
| 637 | %====================================================================== |
Andrew M. Kuchling | 8f4d255 | 2006-03-08 01:50:20 +0000 | [diff] [blame] | 638 | \section{PEP 352: Exceptions as New-Style Classes} |
| 639 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 640 | Exception classes can now be new-style classes, not just classic |
| 641 | classes, and the built-in \exception{Exception} class and all the |
| 642 | standard built-in exceptions (\exception{NameError}, |
| 643 | \exception{ValueError}, etc.) are now new-style classes. |
Andrew M. Kuchling | aeadf95 | 2006-03-09 19:06:05 +0000 | [diff] [blame] | 644 | |
| 645 | The inheritance hierarchy for exceptions has been rearranged a bit. |
| 646 | In 2.5, the inheritance relationships are: |
| 647 | |
| 648 | \begin{verbatim} |
| 649 | BaseException # New in Python 2.5 |
| 650 | |- KeyboardInterrupt |
| 651 | |- SystemExit |
| 652 | |- Exception |
| 653 | |- (all other current built-in exceptions) |
| 654 | \end{verbatim} |
| 655 | |
| 656 | This rearrangement was done because people often want to catch all |
| 657 | exceptions that indicate program errors. \exception{KeyboardInterrupt} and |
| 658 | \exception{SystemExit} aren't errors, though, and usually represent an explicit |
| 659 | action such as the user hitting Control-C or code calling |
| 660 | \function{sys.exit()}. A bare \code{except:} will catch all exceptions, |
| 661 | so you commonly need to list \exception{KeyboardInterrupt} and |
| 662 | \exception{SystemExit} in order to re-raise them. The usual pattern is: |
| 663 | |
| 664 | \begin{verbatim} |
| 665 | try: |
| 666 | ... |
| 667 | except (KeyboardInterrupt, SystemExit): |
| 668 | raise |
| 669 | except: |
| 670 | # Log error... |
| 671 | # Continue running program... |
| 672 | \end{verbatim} |
| 673 | |
| 674 | In Python 2.5, you can now write \code{except Exception} to achieve |
| 675 | the same result, catching all the exceptions that usually indicate errors |
| 676 | but leaving \exception{KeyboardInterrupt} and |
| 677 | \exception{SystemExit} alone. As in previous versions, |
| 678 | a bare \code{except:} still catches all exceptions. |
| 679 | |
| 680 | The goal for Python 3.0 is to require any class raised as an exception |
| 681 | to derive from \exception{BaseException} or some descendant of |
| 682 | \exception{BaseException}, and future releases in the |
| 683 | Python 2.x series may begin to enforce this constraint. Therefore, I |
| 684 | suggest you begin making all your exception classes derive from |
| 685 | \exception{Exception} now. It's been suggested that the bare |
| 686 | \code{except:} form should be removed in Python 3.0, but Guido van~Rossum |
| 687 | hasn't decided whether to do this or not. |
| 688 | |
| 689 | Raising of strings as exceptions, as in the statement \code{raise |
| 690 | "Error occurred"}, is deprecated in Python 2.5 and will trigger a |
| 691 | warning. The aim is to be able to remove the string-exception feature |
| 692 | in a few releases. |
| 693 | |
| 694 | |
| 695 | \begin{seealso} |
| 696 | |
Andrew M. Kuchling | c3749a9 | 2006-04-04 19:14:41 +0000 | [diff] [blame] | 697 | \seepep{352}{Required Superclass for Exceptions}{PEP written by |
Andrew M. Kuchling | aeadf95 | 2006-03-09 19:06:05 +0000 | [diff] [blame] | 698 | Brett Cannon and Guido van Rossum; implemented by Brett Cannon.} |
| 699 | |
| 700 | \end{seealso} |
Andrew M. Kuchling | 8f4d255 | 2006-03-08 01:50:20 +0000 | [diff] [blame] | 701 | |
| 702 | |
| 703 | %====================================================================== |
Andrew M. Kuchling | c3749a9 | 2006-04-04 19:14:41 +0000 | [diff] [blame] | 704 | \section{PEP 353: Using ssize_t as the index type} |
| 705 | |
| 706 | A wide-ranging change to Python's C API, using a new |
| 707 | \ctype{Py_ssize_t} type definition instead of \ctype{int}, |
| 708 | will permit the interpreter to handle more data on 64-bit platforms. |
| 709 | This change doesn't affect Python's capacity on 32-bit platforms. |
| 710 | |
| 711 | This section will be expanded in future alpha releases. |
| 712 | |
| 713 | \begin{seealso} |
| 714 | |
| 715 | \seepep{353}{}{PEP written and implemented by Martin von L\"owis.} |
| 716 | |
| 717 | \end{seealso} |
| 718 | |
| 719 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 720 | \section{PEP 357: The '__index__' method} |
| 721 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 722 | The NumPy developers had a problem that could only be solved by adding |
| 723 | a new special method, \method{__index__}. When using slice notation, |
Fred Drake | 1c0e328 | 2006-04-02 03:30:06 +0000 | [diff] [blame] | 724 | as in \code{[\var{start}:\var{stop}:\var{step}]}, the values of the |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 725 | \var{start}, \var{stop}, and \var{step} indexes must all be either |
| 726 | integers or long integers. NumPy defines a variety of specialized |
| 727 | integer types corresponding to unsigned and signed integers of 8, 16, |
| 728 | 32, and 64 bits, but there was no way to signal that these types could |
| 729 | be used as slice indexes. |
| 730 | |
| 731 | Slicing can't just use the existing \method{__int__} method because |
| 732 | that method is also used to implement coercion to integers. If |
| 733 | slicing used \method{__int__}, floating-point numbers would also |
| 734 | become legal slice indexes and that's clearly an undesirable |
| 735 | behaviour. |
| 736 | |
| 737 | Instead, a new special method called \method{__index__} was added. It |
| 738 | takes no arguments and returns an integer giving the slice index to |
| 739 | use. For example: |
| 740 | |
| 741 | \begin{verbatim} |
| 742 | class C: |
| 743 | def __index__ (self): |
| 744 | return self.value |
| 745 | \end{verbatim} |
| 746 | |
| 747 | The return value must be either a Python integer or long integer. |
| 748 | The interpreter will check that the type returned is correct, and |
| 749 | raises a \exception{TypeError} if this requirement isn't met. |
| 750 | |
| 751 | A corresponding \member{nb_index} slot was added to the C-level |
| 752 | \ctype{PyNumberMethods} structure to let C extensions implement this |
| 753 | protocol. \cfunction{PyNumber_Index(\var{obj})} can be used in |
| 754 | extension code to call the \method{__index__} function and retrieve |
| 755 | its result. |
| 756 | |
| 757 | \begin{seealso} |
| 758 | |
| 759 | \seepep{357}{Allowing Any Object to be Used for Slicing}{PEP written |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 760 | and implemented by Travis Oliphant.} |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 761 | |
| 762 | \end{seealso} |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 763 | |
| 764 | |
| 765 | %====================================================================== |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 766 | \section{Other Language Changes} |
| 767 | |
| 768 | Here are all of the changes that Python 2.5 makes to the core Python |
| 769 | language. |
| 770 | |
| 771 | \begin{itemize} |
Andrew M. Kuchling | 1cae3f5 | 2004-12-03 14:57:21 +0000 | [diff] [blame] | 772 | |
| 773 | \item The \function{min()} and \function{max()} built-in functions |
| 774 | gained a \code{key} keyword argument analogous to the \code{key} |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 775 | argument for \method{sort()}. This argument supplies a function |
Andrew M. Kuchling | 1cae3f5 | 2004-12-03 14:57:21 +0000 | [diff] [blame] | 776 | that takes a single argument and is called for every value in the list; |
| 777 | \function{min()}/\function{max()} will return the element with the |
| 778 | smallest/largest return value from this function. |
| 779 | For example, to find the longest string in a list, you can do: |
| 780 | |
| 781 | \begin{verbatim} |
| 782 | L = ['medium', 'longest', 'short'] |
| 783 | # Prints 'longest' |
| 784 | print max(L, key=len) |
| 785 | # Prints 'short', because lexicographically 'short' has the largest value |
| 786 | print max(L) |
| 787 | \end{verbatim} |
| 788 | |
| 789 | (Contributed by Steven Bethard and Raymond Hettinger.) |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 790 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 791 | \item Two new built-in functions, \function{any()} and |
| 792 | \function{all()}, evaluate whether an iterator contains any true or |
| 793 | false values. \function{any()} returns \constant{True} if any value |
| 794 | returned by the iterator is true; otherwise it will return |
| 795 | \constant{False}. \function{all()} returns \constant{True} only if |
| 796 | all of the values returned by the iterator evaluate as being true. |
| 797 | |
| 798 | % XXX who added? |
| 799 | |
| 800 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 801 | \item The list of base classes in a class definition can now be empty. |
| 802 | As an example, this is now legal: |
| 803 | |
| 804 | \begin{verbatim} |
| 805 | class C(): |
| 806 | pass |
| 807 | \end{verbatim} |
| 808 | (Implemented by Brett Cannon.) |
| 809 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 810 | % XXX __missing__ hook in dictionaries |
| 811 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 812 | \end{itemize} |
| 813 | |
| 814 | |
| 815 | %====================================================================== |
Andrew M. Kuchling | da37604 | 2006-03-17 15:56:41 +0000 | [diff] [blame] | 816 | \subsection{Interactive Interpreter Changes} |
| 817 | |
| 818 | In the interactive interpreter, \code{quit} and \code{exit} |
| 819 | have long been strings so that new users get a somewhat helpful message |
| 820 | when they try to quit: |
| 821 | |
| 822 | \begin{verbatim} |
| 823 | >>> quit |
| 824 | 'Use Ctrl-D (i.e. EOF) to exit.' |
| 825 | \end{verbatim} |
| 826 | |
| 827 | In Python 2.5, \code{quit} and \code{exit} are now objects that still |
| 828 | produce string representations of themselves, but are also callable. |
| 829 | Newbies who try \code{quit()} or \code{exit()} will now exit the |
| 830 | interpreter as they expect. (Implemented by Georg Brandl.) |
| 831 | |
| 832 | |
| 833 | %====================================================================== |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 834 | \subsection{Optimizations} |
| 835 | |
| 836 | \begin{itemize} |
| 837 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 838 | \item When they were introduced |
| 839 | in Python 2.4, the built-in \class{set} and \class{frozenset} types |
| 840 | were built on top of Python's dictionary type. |
| 841 | In 2.5 the internal data structure has been customized for implementing sets, |
| 842 | and as a result sets will use a third less memory and are somewhat faster. |
| 843 | (Implemented by Raymond Hettinger.) |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 844 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 845 | \item The performance of some Unicode operations has been improved. |
| 846 | % XXX provide details? |
| 847 | |
| 848 | \item The code generator's peephole optimizer now performs |
| 849 | simple constant folding in expressions. If you write something like |
| 850 | \code{a = 2+3}, the code generator will do the arithmetic and produce |
| 851 | code corresponding to \code{a = 5}. |
| 852 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 853 | \end{itemize} |
| 854 | |
| 855 | The net result of the 2.5 optimizations is that Python 2.5 runs the |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 856 | pystone benchmark around XXX\% faster than Python 2.4. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 857 | |
| 858 | |
| 859 | %====================================================================== |
| 860 | \section{New, Improved, and Deprecated Modules} |
| 861 | |
| 862 | As usual, Python's standard library received a number of enhancements and |
| 863 | bug fixes. Here's a partial list of the most notable changes, sorted |
| 864 | alphabetically by module name. Consult the |
| 865 | \file{Misc/NEWS} file in the source tree for a more |
Andrew M. Kuchling | f688cc5 | 2006-03-10 18:50:08 +0000 | [diff] [blame] | 866 | complete list of changes, or look through the SVN logs for all the |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 867 | details. |
| 868 | |
| 869 | \begin{itemize} |
| 870 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 871 | % collections.deque now has .remove() |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 872 | % collections.defaultdict |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 873 | |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 874 | % the cPickle module no longer accepts the deprecated None option in the |
| 875 | % args tuple returned by __reduce__(). |
| 876 | |
| 877 | % csv module improvements |
| 878 | |
| 879 | % datetime.datetime() now has a strptime class method which can be used to |
| 880 | % create datetime object using a string and format. |
| 881 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 882 | % fileinput: opening hook used to control how files are opened. |
| 883 | % .input() now has a mode parameter |
| 884 | % now has a fileno() function |
| 885 | % accepts Unicode filenames |
| 886 | |
Andrew M. Kuchling | da37604 | 2006-03-17 15:56:41 +0000 | [diff] [blame] | 887 | \item In the \module{gc} module, the new \function{get_count()} function |
| 888 | returns a 3-tuple containing the current collection counts for the |
| 889 | three GC generations. This is accounting information for the garbage |
| 890 | collector; when these counts reach a specified threshold, a garbage |
| 891 | collection sweep will be made. The existing \function{gc.collect()} |
| 892 | function now takes an optional \var{generation} argument of 0, 1, or 2 |
| 893 | to specify which generation to collect. |
| 894 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 895 | \item The \function{nsmallest()} and |
| 896 | \function{nlargest()} functions in the \module{heapq} module |
| 897 | now support a \code{key} keyword argument similar to the one |
| 898 | provided by the \function{min()}/\function{max()} functions |
| 899 | and the \method{sort()} methods. For example: |
| 900 | Example: |
| 901 | |
| 902 | \begin{verbatim} |
| 903 | >>> import heapq |
| 904 | >>> L = ["short", 'medium', 'longest', 'longer still'] |
| 905 | >>> heapq.nsmallest(2, L) # Return two lowest elements, lexicographically |
| 906 | ['longer still', 'longest'] |
| 907 | >>> heapq.nsmallest(2, L, key=len) # Return two shortest elements |
| 908 | ['short', 'medium'] |
| 909 | \end{verbatim} |
| 910 | |
| 911 | (Contributed by Raymond Hettinger.) |
| 912 | |
Andrew M. Kuchling | 511a3a8 | 2005-03-20 19:52:18 +0000 | [diff] [blame] | 913 | \item The \function{itertools.islice()} function now accepts |
| 914 | \code{None} for the start and step arguments. This makes it more |
| 915 | compatible with the attributes of slice objects, so that you can now write |
| 916 | the following: |
| 917 | |
| 918 | \begin{verbatim} |
| 919 | s = slice(5) # Create slice object |
| 920 | itertools.islice(iterable, s.start, s.stop, s.step) |
| 921 | \end{verbatim} |
| 922 | |
| 923 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 924 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 925 | \item The \module{operator} module's \function{itemgetter()} |
| 926 | and \function{attrgetter()} functions now support multiple fields. |
| 927 | A call such as \code{operator.attrgetter('a', 'b')} |
| 928 | will return a function |
| 929 | that retrieves the \member{a} and \member{b} attributes. Combining |
| 930 | this new feature with the \method{sort()} method's \code{key} parameter |
| 931 | lets you easily sort lists using multiple fields. |
| 932 | |
| 933 | % XXX who added? |
| 934 | |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 935 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 936 | \item The \module{os} module underwent a number of changes. The |
| 937 | \member{stat_float_times} variable now defaults to true, meaning that |
| 938 | \function{os.stat()} will now return time values as floats. (This |
| 939 | doesn't necessarily mean that \function{os.stat()} will return times |
| 940 | that are precise to fractions of a second; not all systems support |
| 941 | such precision.) |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 942 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 943 | Constants named \member{os.SEEK_SET}, \member{os.SEEK_CUR}, and |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 944 | \member{os.SEEK_END} have been added; these are the parameters to the |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 945 | \function{os.lseek()} function. Two new constants for locking are |
| 946 | \member{os.O_SHLOCK} and \member{os.O_EXLOCK}. |
| 947 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 948 | Two new functions, \function{wait3()} and \function{wait4()}, were |
| 949 | added. They're similar the \function{waitpid()} function which waits |
| 950 | for a child process to exit and returns a tuple of the process ID and |
| 951 | its exit status, but \function{wait3()} and \function{wait4()} return |
| 952 | additional information. \function{wait3()} doesn't take a process ID |
| 953 | as input, so it waits for any child process to exit and returns a |
| 954 | 3-tuple of \var{process-id}, \var{exit-status}, \var{resource-usage} |
| 955 | as returned from the \function{resource.getrusage()} function. |
| 956 | \function{wait4(\var{pid})} does take a process ID. |
| 957 | (Contributed by XXX.) |
| 958 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 959 | On FreeBSD, the \function{os.stat()} function now returns |
| 960 | times with nanosecond resolution, and the returned object |
| 961 | now has \member{st_gen} and \member{st_birthtime}. |
| 962 | The \member{st_flags} member is also available, if the platform supports it. |
| 963 | % XXX patch 1180695, 1212117 |
| 964 | |
Andrew M. Kuchling | 01e3d26 | 2006-03-17 15:38:39 +0000 | [diff] [blame] | 965 | \item The old \module{regex} and \module{regsub} modules, which have been |
| 966 | deprecated ever since Python 2.0, have finally been deleted. |
Andrew M. Kuchling | f4b0660 | 2006-03-17 15:39:52 +0000 | [diff] [blame] | 967 | Other deleted modules: \module{statcache}, \module{tzparse}, |
| 968 | \module{whrandom}. |
Andrew M. Kuchling | 01e3d26 | 2006-03-17 15:38:39 +0000 | [diff] [blame] | 969 | |
| 970 | \item The \file{lib-old} directory, |
| 971 | which includes ancient modules such as \module{dircmp} and |
| 972 | \module{ni}, was also deleted. \file{lib-old} wasn't on the default |
| 973 | \code{sys.path}, so unless your programs explicitly added the directory to |
| 974 | \code{sys.path}, this removal shouldn't affect your code. |
| 975 | |
Andrew M. Kuchling | 4678dc8 | 2006-01-15 16:11:28 +0000 | [diff] [blame] | 976 | \item The \module{socket} module now supports \constant{AF_NETLINK} |
| 977 | sockets on Linux, thanks to a patch from Philippe Biondi. |
| 978 | Netlink sockets are a Linux-specific mechanism for communications |
| 979 | between a user-space process and kernel code; an introductory |
| 980 | article about them is at \url{http://www.linuxjournal.com/article/7356}. |
| 981 | In Python code, netlink addresses are represented as a tuple of 2 integers, |
| 982 | \code{(\var{pid}, \var{group_mask})}. |
| 983 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 984 | Socket objects also gained accessor methods \method{getfamily()}, |
| 985 | \method{gettype()}, and \method{getproto()} methods to retrieve the |
| 986 | family, type, and protocol values for the socket. |
| 987 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 988 | \item New module: \module{spwd} provides functions for accessing the |
| 989 | shadow password database on systems that support it. |
| 990 | % XXX give example |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 991 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 992 | % XXX patch #1382163: sys.subversion, Py_GetBuildNumber() |
| 993 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 994 | \item The \class{TarFile} class in the \module{tarfile} module now has |
Georg Brandl | 08c02db | 2005-07-22 18:39:19 +0000 | [diff] [blame] | 995 | an \method{extractall()} method that extracts all members from the |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 996 | archive into the current working directory. It's also possible to set |
| 997 | a different directory as the extraction target, and to unpack only a |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 998 | subset of the archive's members. |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 999 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1000 | A tarfile's compression can be autodetected by |
| 1001 | using the mode \code{'r|*'}. |
| 1002 | % patch 918101 |
| 1003 | (Contributed by Lars Gust\"abel.) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1004 | |
Andrew M. Kuchling | f688cc5 | 2006-03-10 18:50:08 +0000 | [diff] [blame] | 1005 | \item The \module{unicodedata} module has been updated to use version 4.1.0 |
| 1006 | of the Unicode character database. Version 3.2.0 is required |
| 1007 | by some specifications, so it's still available as |
| 1008 | \member{unicodedata.db_3_2_0}. |
| 1009 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1010 | % patch #754022: Greatly enhanced webbrowser.py (by Oleg Broytmann). |
| 1011 | |
Fredrik Lundh | 7e0aef0 | 2005-12-12 18:54:55 +0000 | [diff] [blame] | 1012 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1013 | \item The \module{xmlrpclib} module now supports returning |
| 1014 | \class{datetime} objects for the XML-RPC date type. Supply |
| 1015 | \code{use_datetime=True} to the \function{loads()} function |
| 1016 | or the \class{Unmarshaller} class to enable this feature. |
| 1017 | % XXX patch 1120353 |
| 1018 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1019 | |
Fred Drake | 114b8ca | 2005-03-21 05:47:11 +0000 | [diff] [blame] | 1020 | \end{itemize} |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 1021 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1022 | |
| 1023 | |
| 1024 | %====================================================================== |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1025 | % whole new modules get described in subsections here |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1026 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1027 | % XXX new distutils features: upload |
| 1028 | |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1029 | \subsection{The ctypes package} |
| 1030 | |
| 1031 | The \module{ctypes} package, written by Thomas Heller, has been added |
| 1032 | to the standard library. \module{ctypes} lets you call arbitrary functions |
| 1033 | in shared libraries or DLLs. |
| 1034 | |
| 1035 | In subsequent alpha releases of Python 2.5, I'll add a brief |
| 1036 | introduction that shows some basic usage of the module. |
| 1037 | |
| 1038 | % XXX write introduction |
| 1039 | |
| 1040 | |
| 1041 | \subsection{The ElementTree package} |
| 1042 | |
| 1043 | A subset of Fredrik Lundh's ElementTree library for processing XML has |
| 1044 | been added to the standard library as \module{xml.etree}. The |
| 1045 | vailable modules are |
| 1046 | \module{ElementTree}, \module{ElementPath}, and |
| 1047 | \module{ElementInclude} from ElementTree 1.2.6. |
| 1048 | |
| 1049 | In subsequent alpha releases of Python 2.5, I'll add a brief |
| 1050 | introduction that will provide a page-long overview of using |
| 1051 | ElementTree. Full documentation for |
| 1052 | ElementTree is available at \url{http://effbot.org/zone/element-index.htm}. |
| 1053 | |
| 1054 | % XXX write introduction |
| 1055 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1056 | |
| 1057 | \subsection{The hashlib package} |
| 1058 | |
| 1059 | A new \module{hashlib} module has been added to replace the |
| 1060 | \module{md5} and \module{sha} modules. \module{hashlib} adds support |
| 1061 | for additional secure hashes (SHA-224, SHA-256, SHA-384, and SHA-512). |
| 1062 | When available, the module uses OpenSSL for fast platform optimized |
| 1063 | implementations of algorithms. |
| 1064 | |
| 1065 | The old \module{md5} and \module{sha} modules still exist as wrappers |
| 1066 | around hashlib to preserve backwards compatibility. The new module's |
| 1067 | interface is very close to that of the old modules, but not identical. |
| 1068 | The most significant difference is that the constructor functions |
| 1069 | for creating new hashing objects are named differently. |
| 1070 | |
| 1071 | \begin{verbatim} |
| 1072 | # Old versions |
| 1073 | h = md5.md5() |
| 1074 | h = md5.new() |
| 1075 | |
| 1076 | # New version |
| 1077 | h = hashlib.md5() |
| 1078 | |
| 1079 | # Old versions |
| 1080 | h = sha.sha() |
| 1081 | h = sha.new() |
| 1082 | |
| 1083 | # New version |
| 1084 | h = hashlib.sha1() |
| 1085 | |
| 1086 | # Hash that weren't previously available |
| 1087 | h = hashlib.sha224() |
| 1088 | h = hashlib.sha256() |
| 1089 | h = hashlib.sha384() |
| 1090 | h = hashlib.sha512() |
| 1091 | |
| 1092 | # Alternative form |
| 1093 | h = hashlib.new('md5') # Provide algorithm as a string |
| 1094 | \end{verbatim} |
| 1095 | |
| 1096 | Once a hash object has been created, its methods are the same as before: |
| 1097 | \method{update(\var{string})} hashes the specified string into the |
| 1098 | current digest state, \method{digest()} and \method{hexdigest()} |
| 1099 | return the digest value as a binary string or a string of hex digits, |
| 1100 | and \method{copy()} returns a new hashing object with the same digest state. |
| 1101 | |
| 1102 | This module was contributed by Gregory P. Smith. |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1103 | |
| 1104 | |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1105 | \subsection{The sqlite3 package} |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1106 | |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1107 | The pysqlite module (\url{http://www.pysqlite.org}), a wrapper for the |
| 1108 | SQLite embedded database, has been added to the standard library under |
| 1109 | the package name \module{sqlite3}. SQLite is a C library that |
| 1110 | provides a SQL-language database that stores data in disk files |
| 1111 | without requiring a separate server process. pysqlite was written by |
| 1112 | Gerhard H\"aring, and provides a SQL interface that complies with the |
| 1113 | DB-API 2.0 specification. This means that it should be possible to |
| 1114 | write the first version of your applications using SQLite for data |
| 1115 | storage and, if switching to a larger database such as PostgreSQL or |
| 1116 | Oracle is necessary, the switch should be relatively easy. |
| 1117 | |
| 1118 | If you're compiling the Python source yourself, note that the source |
| 1119 | tree doesn't include the SQLite code itself, only the wrapper module. |
| 1120 | You'll need to have the SQLite libraries and headers installed before |
| 1121 | compiling Python, and the build process will compile the module when |
| 1122 | the necessary headers are available. |
| 1123 | |
| 1124 | In subsequent alpha releases of Python 2.5, I'll add a brief |
| 1125 | introduction that shows some basic usage of the module. |
| 1126 | |
| 1127 | % XXX write introduction |
| 1128 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1129 | |
| 1130 | % ====================================================================== |
| 1131 | \section{Build and C API Changes} |
| 1132 | |
| 1133 | Changes to Python's build process and to the C API include: |
| 1134 | |
| 1135 | \begin{itemize} |
| 1136 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1137 | % XXX PEP 353: ssize_t |
| 1138 | \item The design of the bytecode compiler has changed a great deal, to |
| 1139 | no longer generate bytecode by traversing the parse tree. Instead |
Andrew M. Kuchling | db85ed5 | 2005-10-23 21:52:59 +0000 | [diff] [blame] | 1140 | the parse tree is converted to an abstract syntax tree (or AST), and it is |
| 1141 | the abstract syntax tree that's traversed to produce the bytecode. |
| 1142 | |
| 1143 | No documentation has been written for the AST code yet. To start |
| 1144 | learning about it, read the definition of the various AST nodes in |
| 1145 | \file{Parser/Python.asdl}. A Python script reads this file and |
| 1146 | generates a set of C structure definitions in |
| 1147 | \file{Include/Python-ast.h}. The \cfunction{PyParser_ASTFromString()} |
| 1148 | and \cfunction{PyParser_ASTFromFile()}, defined in |
| 1149 | \file{Include/pythonrun.h}, take Python source as input and return the |
| 1150 | root of an AST representing the contents. This AST can then be turned |
| 1151 | into a code object by \cfunction{PyAST_Compile()}. For more |
| 1152 | information, read the source code, and then ask questions on |
| 1153 | python-dev. |
| 1154 | |
| 1155 | % List of names taken from Jeremy's python-dev post at |
| 1156 | % http://mail.python.org/pipermail/python-dev/2005-October/057500.html |
| 1157 | The AST code was developed under Jeremy Hylton's management, and |
| 1158 | implemented by (in alphabetical order) Brett Cannon, Nick Coghlan, |
| 1159 | Grant Edwards, John Ehresman, Kurt Kaiser, Neal Norwitz, Tim Peters, |
| 1160 | Armin Rigo, and Neil Schemenauer, plus the participants in a number of |
| 1161 | AST sprints at conferences such as PyCon. |
| 1162 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1163 | \item The built-in set types now have an official C API. Call |
| 1164 | \cfunction{PySet_New()} and \cfunction{PyFrozenSet_New()} to create a |
| 1165 | new set, \cfunction{PySet_Add()} and \cfunction{PySet_Discard()} to |
| 1166 | add and remove elements, and \cfunction{PySet_Contains} and |
| 1167 | \cfunction{PySet_Size} to examine the set's state. |
| 1168 | |
| 1169 | \item The \cfunction{PyRange_New()} function was removed. It was |
| 1170 | never documented, never used in the core code, and had dangerously lax |
| 1171 | error checking. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1172 | |
| 1173 | \end{itemize} |
| 1174 | |
| 1175 | |
| 1176 | %====================================================================== |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1177 | %\subsection{Port-Specific Changes} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1178 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1179 | %Platform-specific changes go here. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1180 | |
| 1181 | |
| 1182 | %====================================================================== |
| 1183 | \section{Other Changes and Fixes \label{section-other}} |
| 1184 | |
| 1185 | As usual, there were a bunch of other improvements and bugfixes |
Andrew M. Kuchling | f688cc5 | 2006-03-10 18:50:08 +0000 | [diff] [blame] | 1186 | scattered throughout the source tree. A search through the SVN change |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1187 | logs finds there were XXX patches applied and YYY bugs fixed between |
Andrew M. Kuchling | 92e2495 | 2004-12-03 13:54:09 +0000 | [diff] [blame] | 1188 | Python 2.4 and 2.5. Both figures are likely to be underestimates. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1189 | |
| 1190 | Some of the more notable changes are: |
| 1191 | |
| 1192 | \begin{itemize} |
| 1193 | |
Andrew M. Kuchling | 01e3d26 | 2006-03-17 15:38:39 +0000 | [diff] [blame] | 1194 | \item Evan Jones's patch to obmalloc, first described in a talk |
| 1195 | at PyCon DC 2005, was applied. Python 2.4 allocated small objects in |
| 1196 | 256K-sized arenas, but never freed arenas. With this patch, Python |
| 1197 | will free arenas when they're empty. The net effect is that on some |
| 1198 | platforms, when you allocate many objects, Python's memory usage may |
| 1199 | actually drop when you delete them, and the memory may be returned to |
| 1200 | the operating system. (Implemented by Evan Jones, and reworked by Tim |
| 1201 | Peters.) |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1202 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1203 | \item Coverity, a company that markets a source code analysis tool |
| 1204 | called Prevent, provided the results of their examination of the Python |
| 1205 | source code. The analysis found a number of refcounting bugs, often |
| 1206 | in error-handling code. These bugs have been fixed. |
| 1207 | % XXX provide reference? |
| 1208 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1209 | \end{itemize} |
| 1210 | |
| 1211 | |
| 1212 | %====================================================================== |
| 1213 | \section{Porting to Python 2.5} |
| 1214 | |
| 1215 | This section lists previously described changes that may require |
| 1216 | changes to your code: |
| 1217 | |
| 1218 | \begin{itemize} |
| 1219 | |
Andrew M. Kuchling | c3749a9 | 2006-04-04 19:14:41 +0000 | [diff] [blame] | 1220 | \item The \module{pickle} module no longer uses the deprecated \var{bin} parameter. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1221 | |
| 1222 | \end{itemize} |
| 1223 | |
| 1224 | |
| 1225 | %====================================================================== |
| 1226 | \section{Acknowledgements \label{acks}} |
| 1227 | |
| 1228 | The author would like to thank the following people for offering |
| 1229 | suggestions, corrections and assistance with various drafts of this |
Andrew M. Kuchling | c3749a9 | 2006-04-04 19:14:41 +0000 | [diff] [blame] | 1230 | article: no one yet. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1231 | |
| 1232 | \end{document} |