Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1 | \documentclass{howto} |
| 2 | \usepackage{distutils} |
| 3 | % $Id$ |
| 4 | |
Andrew M. Kuchling | 29b3d08 | 2006-04-14 20:35:17 +0000 | [diff] [blame] | 5 | % Writing context managers |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 6 | % The easy_install stuff |
Andrew M. Kuchling | 075e023 | 2006-04-11 13:14:56 +0000 | [diff] [blame] | 7 | % Stateful codec changes |
Andrew M. Kuchling | 29b3d08 | 2006-04-14 20:35:17 +0000 | [diff] [blame] | 8 | % Fix XXX comments |
Andrew M. Kuchling | 5f445bf | 2006-04-12 18:54:00 +0000 | [diff] [blame] | 9 | % Count up the patches and bugs |
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 | 6a67e4e | 2006-04-12 13:03:35 +0000 | [diff] [blame] | 39 | \section{PEP 243: Uploading Modules to PyPI} |
| 40 | |
| 41 | PEP 243 describes an HTTP-based protocol for submitting software |
| 42 | packages to a central archive. The Python package index at |
| 43 | \url{http://cheeseshop.python.org} now supports package uploads, and |
| 44 | the new \command{upload} Distutils command will upload a package to the |
| 45 | repository. |
| 46 | |
| 47 | Before a package can be uploaded, you must be able to build a |
| 48 | distribution using the \command{sdist} Distutils command. Once that |
| 49 | works, you can run \code{python setup.py upload} to add your package |
| 50 | to the PyPI archive. Optionally you can GPG-sign the package by |
| 51 | supplying the \programopt{--sign} and |
| 52 | \programopt{--identity} options. |
| 53 | |
| 54 | \begin{seealso} |
| 55 | |
| 56 | \seepep{243}{Module Repository Upload Mechanism}{PEP written by |
Andrew M. Kuchling | 5f445bf | 2006-04-12 18:54:00 +0000 | [diff] [blame] | 57 | Sean Reifschneider; implemented by Martin von~L\"owis |
Andrew M. Kuchling | 6a67e4e | 2006-04-12 13:03:35 +0000 | [diff] [blame] | 58 | and Richard Jones. Note that the PEP doesn't exactly |
| 59 | describe what's implemented in PyPI.} |
| 60 | |
| 61 | \end{seealso} |
| 62 | |
| 63 | |
| 64 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 65 | \section{PEP 308: Conditional Expressions} |
| 66 | |
Andrew M. Kuchling | e362d93 | 2006-03-09 13:56:25 +0000 | [diff] [blame] | 67 | For a long time, people have been requesting a way to write |
| 68 | conditional expressions, expressions that return value A or value B |
| 69 | depending on whether a Boolean value is true or false. A conditional |
| 70 | expression lets you write a single assignment statement that has the |
| 71 | same effect as the following: |
| 72 | |
| 73 | \begin{verbatim} |
| 74 | if condition: |
| 75 | x = true_value |
| 76 | else: |
| 77 | x = false_value |
| 78 | \end{verbatim} |
| 79 | |
| 80 | There have been endless tedious discussions of syntax on both |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 81 | python-dev and comp.lang.python. A vote was even held that found the |
| 82 | majority of voters wanted conditional expressions in some form, |
| 83 | but there was no syntax that was preferred by a clear majority. |
| 84 | Candidates included C's \code{cond ? true_v : false_v}, |
Andrew M. Kuchling | e362d93 | 2006-03-09 13:56:25 +0000 | [diff] [blame] | 85 | \code{if cond then true_v else false_v}, and 16 other variations. |
| 86 | |
| 87 | GvR eventually chose a surprising syntax: |
| 88 | |
| 89 | \begin{verbatim} |
| 90 | x = true_value if condition else false_value |
| 91 | \end{verbatim} |
| 92 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 93 | Evaluation is still lazy as in existing Boolean expressions, so the |
| 94 | order of evaluation jumps around a bit. The \var{condition} |
| 95 | expression in the middle is evaluated first, and the \var{true_value} |
| 96 | expression is evaluated only if the condition was true. Similarly, |
| 97 | the \var{false_value} expression is only evaluated when the condition |
| 98 | is false. |
Andrew M. Kuchling | e362d93 | 2006-03-09 13:56:25 +0000 | [diff] [blame] | 99 | |
| 100 | This syntax may seem strange and backwards; why does the condition go |
| 101 | in the \emph{middle} of the expression, and not in the front as in C's |
| 102 | \code{c ? x : y}? The decision was checked by applying the new syntax |
| 103 | to the modules in the standard library and seeing how the resulting |
| 104 | code read. In many cases where a conditional expression is used, one |
| 105 | value seems to be the 'common case' and one value is an 'exceptional |
| 106 | case', used only on rarer occasions when the condition isn't met. The |
| 107 | conditional syntax makes this pattern a bit more obvious: |
| 108 | |
| 109 | \begin{verbatim} |
| 110 | contents = ((doc + '\n') if doc else '') |
| 111 | \end{verbatim} |
| 112 | |
| 113 | I read the above statement as meaning ``here \var{contents} is |
Andrew M. Kuchling | d0fcc02 | 2006-03-09 13:57:28 +0000 | [diff] [blame] | 114 | usually assigned a value of \code{doc+'\e n'}; sometimes |
Andrew M. Kuchling | e362d93 | 2006-03-09 13:56:25 +0000 | [diff] [blame] | 115 | \var{doc} is empty, in which special case an empty string is returned.'' |
| 116 | I doubt I will use conditional expressions very often where there |
| 117 | isn't a clear common and uncommon case. |
| 118 | |
| 119 | There was some discussion of whether the language should require |
| 120 | surrounding conditional expressions with parentheses. The decision |
| 121 | was made to \emph{not} require parentheses in the Python language's |
| 122 | grammar, but as a matter of style I think you should always use them. |
| 123 | Consider these two statements: |
| 124 | |
| 125 | \begin{verbatim} |
| 126 | # First version -- no parens |
| 127 | level = 1 if logging else 0 |
| 128 | |
| 129 | # Second version -- with parens |
| 130 | level = (1 if logging else 0) |
| 131 | \end{verbatim} |
| 132 | |
| 133 | In the first version, I think a reader's eye might group the statement |
| 134 | into 'level = 1', 'if logging', 'else 0', and think that the condition |
| 135 | decides whether the assignment to \var{level} is performed. The |
| 136 | second version reads better, in my opinion, because it makes it clear |
| 137 | that the assignment is always performed and the choice is being made |
| 138 | between two values. |
| 139 | |
| 140 | Another reason for including the brackets: a few odd combinations of |
| 141 | list comprehensions and lambdas could look like incorrect conditional |
| 142 | expressions. See \pep{308} for some examples. If you put parentheses |
| 143 | around your conditional expressions, you won't run into this case. |
| 144 | |
| 145 | |
| 146 | \begin{seealso} |
| 147 | |
| 148 | \seepep{308}{Conditional Expressions}{PEP written by |
| 149 | Guido van Rossum and Raymond D. Hettinger; implemented by Thomas |
| 150 | Wouters.} |
| 151 | |
| 152 | \end{seealso} |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | %====================================================================== |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 156 | \section{PEP 309: Partial Function Application} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 157 | |
Andrew M. Kuchling | b1c96fd | 2005-03-20 21:42:04 +0000 | [diff] [blame] | 158 | The \module{functional} module is intended to contain tools for |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 159 | functional-style programming. Currently it only contains a |
| 160 | \class{partial()} function, but new functions will probably be added |
| 161 | in future versions of Python. |
Andrew M. Kuchling | b1c96fd | 2005-03-20 21:42:04 +0000 | [diff] [blame] | 162 | |
Andrew M. Kuchling | 4b000cd | 2005-04-09 15:51:44 +0000 | [diff] [blame] | 163 | For programs written in a functional style, it can be useful to |
| 164 | construct variants of existing functions that have some of the |
| 165 | parameters filled in. Consider a Python function \code{f(a, b, c)}; |
| 166 | you could create a new function \code{g(b, c)} that was equivalent to |
| 167 | \code{f(1, b, c)}. This is called ``partial function application'', |
| 168 | and is provided by the \class{partial} class in the new |
| 169 | \module{functional} module. |
| 170 | |
| 171 | The constructor for \class{partial} takes the arguments |
| 172 | \code{(\var{function}, \var{arg1}, \var{arg2}, ... |
| 173 | \var{kwarg1}=\var{value1}, \var{kwarg2}=\var{value2})}. The resulting |
| 174 | object is callable, so you can just call it to invoke \var{function} |
| 175 | with the filled-in arguments. |
| 176 | |
| 177 | Here's a small but realistic example: |
| 178 | |
| 179 | \begin{verbatim} |
| 180 | import functional |
| 181 | |
| 182 | def log (message, subsystem): |
| 183 | "Write the contents of 'message' to the specified subsystem." |
| 184 | print '%s: %s' % (subsystem, message) |
| 185 | ... |
| 186 | |
| 187 | server_log = functional.partial(log, subsystem='server') |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 188 | server_log('Unable to open socket') |
Andrew M. Kuchling | 4b000cd | 2005-04-09 15:51:44 +0000 | [diff] [blame] | 189 | \end{verbatim} |
| 190 | |
Andrew M. Kuchling | 6af7fe0 | 2005-08-02 17:20:36 +0000 | [diff] [blame] | 191 | Here's another example, from a program that uses PyGTk. Here a |
| 192 | context-sensitive pop-up menu is being constructed dynamically. The |
| 193 | callback provided for the menu option is a partially applied version |
| 194 | of the \method{open_item()} method, where the first argument has been |
| 195 | provided. |
Andrew M. Kuchling | 4b000cd | 2005-04-09 15:51:44 +0000 | [diff] [blame] | 196 | |
Andrew M. Kuchling | 6af7fe0 | 2005-08-02 17:20:36 +0000 | [diff] [blame] | 197 | \begin{verbatim} |
| 198 | ... |
| 199 | class Application: |
| 200 | def open_item(self, path): |
| 201 | ... |
| 202 | def init (self): |
| 203 | open_func = functional.partial(self.open_item, item_path) |
| 204 | popup_menu.append( ("Open", open_func, 1) ) |
| 205 | \end{verbatim} |
Andrew M. Kuchling | b1c96fd | 2005-03-20 21:42:04 +0000 | [diff] [blame] | 206 | |
| 207 | |
| 208 | \begin{seealso} |
| 209 | |
| 210 | \seepep{309}{Partial Function Application}{PEP proposed and written by |
| 211 | Peter Harris; implemented by Hye-Shik Chang, with adaptations by |
| 212 | Raymond Hettinger.} |
| 213 | |
| 214 | \end{seealso} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 215 | |
| 216 | |
| 217 | %====================================================================== |
Fred Drake | db7b002 | 2005-03-20 22:19:47 +0000 | [diff] [blame] | 218 | \section{PEP 314: Metadata for Python Software Packages v1.1} |
| 219 | |
Andrew M. Kuchling | d8d732e | 2005-04-09 23:59:41 +0000 | [diff] [blame] | 220 | Some simple dependency support was added to Distutils. The |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 221 | \function{setup()} function now has \code{requires}, \code{provides}, |
| 222 | and \code{obsoletes} keyword parameters. When you build a source |
| 223 | distribution using the \code{sdist} command, the dependency |
| 224 | information will be recorded in the \file{PKG-INFO} file. |
Andrew M. Kuchling | d8d732e | 2005-04-09 23:59:41 +0000 | [diff] [blame] | 225 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 226 | Another new keyword parameter is \code{download_url}, which should be |
| 227 | set to a URL for the package's source code. This means it's now |
| 228 | possible to look up an entry in the package index, determine the |
| 229 | dependencies for a package, and download the required packages. |
Andrew M. Kuchling | d8d732e | 2005-04-09 23:59:41 +0000 | [diff] [blame] | 230 | |
Andrew M. Kuchling | 61434b6 | 2006-04-13 11:51:07 +0000 | [diff] [blame] | 231 | \begin{verbatim} |
| 232 | VERSION = '1.0' |
| 233 | setup(name='PyPackage', |
| 234 | version=VERSION, |
| 235 | requires=['numarray', 'zlib (>=1.1.4)'], |
| 236 | obsoletes=['OldPackage'] |
| 237 | download_url=('http://www.example.com/pypackage/dist/pkg-%s.tar.gz' |
| 238 | % VERSION), |
| 239 | ) |
| 240 | \end{verbatim} |
Andrew M. Kuchling | d8d732e | 2005-04-09 23:59:41 +0000 | [diff] [blame] | 241 | |
| 242 | \begin{seealso} |
| 243 | |
| 244 | \seepep{314}{Metadata for Python Software Packages v1.1}{PEP proposed |
| 245 | and written by A.M. Kuchling, Richard Jones, and Fred Drake; |
| 246 | implemented by Richard Jones and Fred Drake.} |
| 247 | |
| 248 | \end{seealso} |
Fred Drake | db7b002 | 2005-03-20 22:19:47 +0000 | [diff] [blame] | 249 | |
| 250 | |
| 251 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 252 | \section{PEP 328: Absolute and Relative Imports} |
| 253 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 254 | The simpler part of PEP 328 was implemented in Python 2.4: parentheses |
| 255 | could now be used to enclose the names imported from a module using |
| 256 | the \code{from ... import ...} statement, making it easier to import |
| 257 | many different names. |
| 258 | |
| 259 | The more complicated part has been implemented in Python 2.5: |
| 260 | importing a module can be specified to use absolute or |
| 261 | package-relative imports. The plan is to move toward making absolute |
| 262 | imports the default in future versions of Python. |
| 263 | |
| 264 | Let's say you have a package directory like this: |
| 265 | \begin{verbatim} |
| 266 | pkg/ |
| 267 | pkg/__init__.py |
| 268 | pkg/main.py |
| 269 | pkg/string.py |
| 270 | \end{verbatim} |
| 271 | |
| 272 | This defines a package named \module{pkg} containing the |
| 273 | \module{pkg.main} and \module{pkg.string} submodules. |
| 274 | |
| 275 | Consider the code in the \file{main.py} module. What happens if it |
| 276 | executes the statement \code{import string}? In Python 2.4 and |
| 277 | earlier, it will first look in the package's directory to perform a |
| 278 | relative import, finds \file{pkg/string.py}, imports the contents of |
| 279 | that file as the \module{pkg.string} module, and that module is bound |
| 280 | to the name \samp{string} in the \module{pkg.main} module's namespace. |
| 281 | |
| 282 | That's fine if \module{pkg.string} was what you wanted. But what if |
| 283 | you wanted Python's standard \module{string} module? There's no clean |
| 284 | way to ignore \module{pkg.string} and look for the standard module; |
| 285 | generally you had to look at the contents of \code{sys.modules}, which |
| 286 | is slightly unclean. |
Andrew M. Kuchling | 4d8cd89 | 2006-04-06 13:03:04 +0000 | [diff] [blame] | 287 | Holger Krekel's \module{py.std} package provides a tidier way to perform |
| 288 | imports from the standard library, \code{import py ; py.std.string.join()}, |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 289 | but that package isn't available on all Python installations. |
| 290 | |
| 291 | Reading code which relies on relative imports is also less clear, |
| 292 | because a reader may be confused about which module, \module{string} |
| 293 | or \module{pkg.string}, is intended to be used. Python users soon |
| 294 | learned not to duplicate the names of standard library modules in the |
| 295 | names of their packages' submodules, but you can't protect against |
| 296 | having your submodule's name being used for a new module added in a |
| 297 | future version of Python. |
| 298 | |
| 299 | In Python 2.5, you can switch \keyword{import}'s behaviour to |
| 300 | absolute imports using a \code{from __future__ import absolute_import} |
| 301 | directive. This absolute-import behaviour will become the default in |
Andrew M. Kuchling | 4d8cd89 | 2006-04-06 13:03:04 +0000 | [diff] [blame] | 302 | a future version (probably Python 2.7). Once absolute imports |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 303 | are the default, \code{import string} will |
| 304 | always find the standard library's version. |
| 305 | It's suggested that users should begin using absolute imports as much |
| 306 | as possible, so it's preferable to begin writing \code{from pkg import |
| 307 | string} in your code. |
| 308 | |
| 309 | Relative imports are still possible by adding a leading period |
| 310 | to the module name when using the \code{from ... import} form: |
| 311 | |
| 312 | \begin{verbatim} |
| 313 | # Import names from pkg.string |
| 314 | from .string import name1, name2 |
| 315 | # Import pkg.string |
| 316 | from . import string |
| 317 | \end{verbatim} |
| 318 | |
| 319 | This imports the \module{string} module relative to the current |
| 320 | package, so in \module{pkg.main} this will import \var{name1} and |
| 321 | \var{name2} from \module{pkg.string}. Additional leading periods |
| 322 | perform the relative import starting from the parent of the current |
| 323 | package. For example, code in the \module{A.B.C} module can do: |
| 324 | |
| 325 | \begin{verbatim} |
| 326 | from . import D # Imports A.B.D |
| 327 | from .. import E # Imports A.E |
| 328 | from ..F import G # Imports A.F.G |
| 329 | \end{verbatim} |
| 330 | |
| 331 | Leading periods cannot be used with the \code{import \var{modname}} |
| 332 | form of the import statement, only the \code{from ... import} form. |
| 333 | |
| 334 | \begin{seealso} |
| 335 | |
Andrew M. Kuchling | 4d8cd89 | 2006-04-06 13:03:04 +0000 | [diff] [blame] | 336 | \seepep{328}{Imports: Multi-Line and Absolute/Relative} |
| 337 | {PEP written by Aahz; implemented by Thomas Wouters.} |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 338 | |
Andrew M. Kuchling | 4d8cd89 | 2006-04-06 13:03:04 +0000 | [diff] [blame] | 339 | \seeurl{http://codespeak.net/py/current/doc/index.html} |
| 340 | {The py library by Holger Krekel, which contains the \module{py.std} package.} |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 341 | |
| 342 | \end{seealso} |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 343 | |
| 344 | |
| 345 | %====================================================================== |
Andrew M. Kuchling | 21d3a7c | 2006-03-15 11:53:09 +0000 | [diff] [blame] | 346 | \section{PEP 338: Executing Modules as Scripts} |
| 347 | |
Andrew M. Kuchling | b182db4 | 2006-03-17 21:48:46 +0000 | [diff] [blame] | 348 | The \programopt{-m} switch added in Python 2.4 to execute a module as |
| 349 | a script gained a few more abilities. Instead of being implemented in |
| 350 | C code inside the Python interpreter, the switch now uses an |
| 351 | implementation in a new module, \module{runpy}. |
| 352 | |
| 353 | The \module{runpy} module implements a more sophisticated import |
| 354 | mechanism so that it's now possible to run modules in a package such |
| 355 | as \module{pychecker.checker}. The module also supports alternative |
Andrew M. Kuchling | 5d4cf5e | 2006-04-13 13:02:42 +0000 | [diff] [blame] | 356 | import mechanisms such as the \module{zipimport} module. This means |
Andrew M. Kuchling | b182db4 | 2006-03-17 21:48:46 +0000 | [diff] [blame] | 357 | you can add a .zip archive's path to \code{sys.path} and then use the |
| 358 | \programopt{-m} switch to execute code from the archive. |
| 359 | |
| 360 | |
| 361 | \begin{seealso} |
| 362 | |
| 363 | \seepep{338}{Executing modules as scripts}{PEP written and |
| 364 | implemented by Nick Coghlan.} |
| 365 | |
| 366 | \end{seealso} |
Andrew M. Kuchling | 21d3a7c | 2006-03-15 11:53:09 +0000 | [diff] [blame] | 367 | |
| 368 | |
| 369 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 370 | \section{PEP 341: Unified try/except/finally} |
| 371 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 372 | Until Python 2.5, the \keyword{try} statement came in two |
| 373 | flavours. You could use a \keyword{finally} block to ensure that code |
Andrew M. Kuchling | 0f1955d | 2006-04-13 12:09:08 +0000 | [diff] [blame] | 374 | is always executed, or one or more \keyword{except} blocks to catch |
| 375 | specific exceptions. You couldn't combine both \keyword{except} blocks and a |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 376 | \keyword{finally} block, because generating the right bytecode for the |
| 377 | combined version was complicated and it wasn't clear what the |
| 378 | semantics of the combined should be. |
| 379 | |
| 380 | GvR spent some time working with Java, which does support the |
| 381 | equivalent of combining \keyword{except} blocks and a |
| 382 | \keyword{finally} block, and this clarified what the statement should |
| 383 | mean. In Python 2.5, you can now write: |
| 384 | |
| 385 | \begin{verbatim} |
| 386 | try: |
| 387 | block-1 ... |
| 388 | except Exception1: |
| 389 | handler-1 ... |
| 390 | except Exception2: |
| 391 | handler-2 ... |
| 392 | else: |
| 393 | else-block |
| 394 | finally: |
| 395 | final-block |
| 396 | \end{verbatim} |
| 397 | |
| 398 | The code in \var{block-1} is executed. If the code raises an |
| 399 | exception, the handlers are tried in order: \var{handler-1}, |
| 400 | \var{handler-2}, ... If no exception is raised, the \var{else-block} |
| 401 | is executed. No matter what happened previously, the |
| 402 | \var{final-block} is executed once the code block is complete and any |
| 403 | raised exceptions handled. Even if there's an error in an exception |
| 404 | handler or the \var{else-block} and a new exception is raised, the |
| 405 | \var{final-block} is still executed. |
| 406 | |
| 407 | \begin{seealso} |
| 408 | |
| 409 | \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] | 410 | implementation by Thomas Lee.} |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 411 | |
| 412 | \end{seealso} |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 413 | |
| 414 | |
| 415 | %====================================================================== |
Andrew M. Kuchling | 3b4fb04 | 2006-04-13 12:49:39 +0000 | [diff] [blame] | 416 | \section{PEP 342: New Generator Features\label{section-generators}} |
Andrew M. Kuchling | a2e21cb | 2005-08-02 17:13:21 +0000 | [diff] [blame] | 417 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 418 | 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] | 419 | 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] | 420 | generator's code is invoked to create an iterator, there's no way to |
| 421 | pass any new information into the function when its execution is |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 422 | resumed. Sometimes the ability to pass in some information would be |
| 423 | useful. Hackish solutions to this include making the generator's code |
| 424 | 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] | 425 | value, or passing in some mutable object that callers then modify. |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 426 | |
| 427 | To refresh your memory of basic generators, here's a simple example: |
| 428 | |
| 429 | \begin{verbatim} |
| 430 | def counter (maximum): |
| 431 | i = 0 |
| 432 | while i < maximum: |
| 433 | yield i |
| 434 | i += 1 |
| 435 | \end{verbatim} |
| 436 | |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 437 | When you call \code{counter(10)}, the result is an iterator that |
| 438 | returns the values from 0 up to 9. On encountering the |
| 439 | \keyword{yield} statement, the iterator returns the provided value and |
| 440 | suspends the function's execution, preserving the local variables. |
| 441 | Execution resumes on the following call to the iterator's |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 442 | \method{next()} method, picking up after the \keyword{yield} statement. |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 443 | |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 444 | In Python 2.3, \keyword{yield} was a statement; it didn't return any |
| 445 | value. In 2.5, \keyword{yield} is now an expression, returning a |
| 446 | 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] | 447 | |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 448 | \begin{verbatim} |
| 449 | val = (yield i) |
| 450 | \end{verbatim} |
| 451 | |
| 452 | I recommend that you always put parentheses around a \keyword{yield} |
| 453 | expression when you're doing something with the returned value, as in |
| 454 | the above example. The parentheses aren't always necessary, but it's |
| 455 | 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] | 456 | 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] | 457 | always be parenthesized except when it occurs at the top-level |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 458 | expression on the right-hand side of an assignment, meaning you can |
| 459 | write \code{val = yield i} but have to use parentheses when there's an |
| 460 | operation, as in \code{val = (yield i) + 12}.} |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 461 | |
| 462 | Values are sent into a generator by calling its |
| 463 | \method{send(\var{value})} method. The generator's code is then |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 464 | resumed and the \keyword{yield} expression returns the specified |
| 465 | \var{value}. If the regular \method{next()} method is called, the |
| 466 | \keyword{yield} returns \constant{None}. |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 467 | |
| 468 | Here's the previous example, modified to allow changing the value of |
| 469 | the internal counter. |
| 470 | |
| 471 | \begin{verbatim} |
| 472 | def counter (maximum): |
| 473 | i = 0 |
| 474 | while i < maximum: |
| 475 | val = (yield i) |
| 476 | # If value provided, change counter |
| 477 | if val is not None: |
| 478 | i = val |
| 479 | else: |
| 480 | i += 1 |
| 481 | \end{verbatim} |
| 482 | |
| 483 | And here's an example of changing the counter: |
| 484 | |
| 485 | \begin{verbatim} |
| 486 | >>> it = counter(10) |
| 487 | >>> print it.next() |
| 488 | 0 |
| 489 | >>> print it.next() |
| 490 | 1 |
| 491 | >>> print it.send(8) |
| 492 | 8 |
| 493 | >>> print it.next() |
| 494 | 9 |
| 495 | >>> print it.next() |
| 496 | Traceback (most recent call last): |
| 497 | File ``t.py'', line 15, in ? |
| 498 | print it.next() |
| 499 | StopIteration |
Andrew M. Kuchling | c203370 | 2005-08-29 13:30:12 +0000 | [diff] [blame] | 500 | \end{verbatim} |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 501 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 502 | Because \keyword{yield} will often be returning \constant{None}, you |
| 503 | should always check for this case. Don't just use its value in |
| 504 | expressions unless you're sure that the \method{send()} method |
| 505 | will be the only method used resume your generator function. |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 506 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 507 | In addition to \method{send()}, there are two other new methods on |
| 508 | generators: |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 509 | |
| 510 | \begin{itemize} |
| 511 | |
| 512 | \item \method{throw(\var{type}, \var{value}=None, |
| 513 | \var{traceback}=None)} is used to raise an exception inside the |
| 514 | generator; the exception is raised by the \keyword{yield} expression |
| 515 | where the generator's execution is paused. |
| 516 | |
| 517 | \item \method{close()} raises a new \exception{GeneratorExit} |
| 518 | exception inside the generator to terminate the iteration. |
| 519 | On receiving this |
| 520 | exception, the generator's code must either raise |
| 521 | \exception{GeneratorExit} or \exception{StopIteration}; catching the |
| 522 | exception and doing anything else is illegal and will trigger |
| 523 | a \exception{RuntimeError}. \method{close()} will also be called by |
| 524 | Python's garbage collection when the generator is garbage-collected. |
| 525 | |
| 526 | If you need to run cleanup code in case of a \exception{GeneratorExit}, |
| 527 | I suggest using a \code{try: ... finally:} suite instead of |
| 528 | catching \exception{GeneratorExit}. |
| 529 | |
| 530 | \end{itemize} |
| 531 | |
| 532 | The cumulative effect of these changes is to turn generators from |
| 533 | one-way producers of information into both producers and consumers. |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 534 | |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 535 | Generators also become \emph{coroutines}, a more generalized form of |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 536 | subroutines. Subroutines are entered at one point and exited at |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 537 | another point (the top of the function, and a \keyword{return |
| 538 | statement}), but coroutines can be entered, exited, and resumed at |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 539 | many different points (the \keyword{yield} statements). We'll have to |
| 540 | figure out patterns for using coroutines effectively in Python. |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 541 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 542 | The addition of the \method{close()} method has one side effect that |
| 543 | isn't obvious. \method{close()} is called when a generator is |
| 544 | garbage-collected, so this means the generator's code gets one last |
Andrew M. Kuchling | 3b4fb04 | 2006-04-13 12:49:39 +0000 | [diff] [blame] | 545 | chance to run before the generator is destroyed. This last chance |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 546 | means that \code{try...finally} statements in generators can now be |
| 547 | guaranteed to work; the \keyword{finally} clause will now always get a |
| 548 | chance to run. The syntactic restriction that you couldn't mix |
| 549 | \keyword{yield} statements with a \code{try...finally} suite has |
| 550 | therefore been removed. This seems like a minor bit of language |
| 551 | trivia, but using generators and \code{try...finally} is actually |
| 552 | necessary in order to implement the \keyword{with} statement |
| 553 | described by PEP 343. We'll look at this new statement in the following |
| 554 | section. |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 555 | |
Andrew M. Kuchling | 3b4fb04 | 2006-04-13 12:49:39 +0000 | [diff] [blame] | 556 | Another even more esoteric effect of this change: previously, the |
| 557 | \member{gi_frame} attribute of a generator was always a frame object. |
| 558 | It's now possible for \member{gi_frame} to be \code{None} |
| 559 | once the generator has been exhausted. |
| 560 | |
Andrew M. Kuchling | a2e21cb | 2005-08-02 17:13:21 +0000 | [diff] [blame] | 561 | \begin{seealso} |
| 562 | |
| 563 | \seepep{342}{Coroutines via Enhanced Generators}{PEP written by |
| 564 | Guido van Rossum and Phillip J. Eby; |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 565 | implemented by Phillip J. Eby. Includes examples of |
| 566 | some fancier uses of generators as coroutines.} |
| 567 | |
| 568 | \seeurl{http://en.wikipedia.org/wiki/Coroutine}{The Wikipedia entry for |
| 569 | coroutines.} |
| 570 | |
Neal Norwitz | 0917988 | 2006-03-04 23:31:45 +0000 | [diff] [blame] | 571 | \seeurl{http://www.sidhe.org/\~{}dan/blog/archives/000178.html}{An |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 572 | explanation of coroutines from a Perl point of view, written by Dan |
| 573 | Sugalski.} |
Andrew M. Kuchling | a2e21cb | 2005-08-02 17:13:21 +0000 | [diff] [blame] | 574 | |
| 575 | \end{seealso} |
| 576 | |
| 577 | |
| 578 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 579 | \section{PEP 343: The 'with' statement} |
| 580 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 581 | The \keyword{with} statement allows a clearer |
| 582 | version of code that uses \code{try...finally} blocks |
| 583 | |
| 584 | First, I'll discuss the statement as it will commonly be used, and |
| 585 | then I'll discuss the detailed implementation and how to write objects |
| 586 | (called ``context managers'') that can be used with this statement. |
| 587 | Most people, who will only use \keyword{with} in company with an |
Andrew M. Kuchling | a4d651f | 2006-04-06 13:24:58 +0000 | [diff] [blame] | 588 | existing object, don't need to know these details and can |
| 589 | just use objects that are documented to work as context managers. |
| 590 | Authors of new context managers will need to understand the details of |
| 591 | the underlying implementation. |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 592 | |
| 593 | The \keyword{with} statement is a new control-flow structure whose |
| 594 | basic structure is: |
| 595 | |
| 596 | \begin{verbatim} |
| 597 | with expression as variable: |
| 598 | with-block |
| 599 | \end{verbatim} |
| 600 | |
| 601 | The expression is evaluated, and it should result in a type of object |
| 602 | that's called a context manager. The context manager can return a |
| 603 | value that will be bound to the name \var{variable}. (Note carefully: |
| 604 | \var{variable} is \emph{not} assigned the result of \var{expression}. |
| 605 | One method of the context manager is run before \var{with-block} is |
| 606 | executed, and another method is run after the block is done, even if |
| 607 | the block raised an exception. |
| 608 | |
| 609 | To enable the statement in Python 2.5, you need |
| 610 | to add the following directive to your module: |
| 611 | |
| 612 | \begin{verbatim} |
| 613 | from __future__ import with_statement |
| 614 | \end{verbatim} |
| 615 | |
| 616 | Some standard Python objects can now behave as context managers. For |
| 617 | example, file objects: |
| 618 | |
| 619 | \begin{verbatim} |
| 620 | with open('/etc/passwd', 'r') as f: |
| 621 | for line in f: |
| 622 | print line |
| 623 | |
| 624 | # f has been automatically closed at this point. |
| 625 | \end{verbatim} |
| 626 | |
| 627 | The \module{threading} module's locks and condition variables |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 628 | also support the \keyword{with} statement: |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 629 | |
| 630 | \begin{verbatim} |
| 631 | lock = threading.Lock() |
| 632 | with lock: |
| 633 | # Critical section of code |
| 634 | ... |
| 635 | \end{verbatim} |
| 636 | |
| 637 | The lock is acquired before the block is executed, and released once |
| 638 | the block is complete. |
| 639 | |
| 640 | The \module{decimal} module's contexts, which encapsulate the desired |
| 641 | precision and rounding characteristics for computations, can also be |
| 642 | used as context managers. |
| 643 | |
| 644 | \begin{verbatim} |
| 645 | import decimal |
| 646 | |
| 647 | v1 = decimal.Decimal('578') |
| 648 | |
| 649 | # Displays with default precision of 28 digits |
| 650 | print v1.sqrt() |
| 651 | |
| 652 | with decimal.Context(prec=16): |
| 653 | # All code in this block uses a precision of 16 digits. |
| 654 | # The original context is restored on exiting the block. |
| 655 | print v1.sqrt() |
| 656 | \end{verbatim} |
| 657 | |
| 658 | \subsection{Writing Context Managers} |
| 659 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 660 | % XXX write this |
| 661 | |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 662 | This section still needs to be written. |
| 663 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 664 | The new \module{contextlib} module provides some functions and a |
Andrew M. Kuchling | 9c67ee0 | 2006-04-04 19:07:27 +0000 | [diff] [blame] | 665 | decorator that are useful for writing context managers. |
| 666 | Future versions will go into more detail. |
| 667 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 668 | % XXX describe further |
| 669 | |
| 670 | \begin{seealso} |
| 671 | |
| 672 | \seepep{343}{The ``with'' statement}{PEP written by |
| 673 | Guido van Rossum and Nick Coghlan. } |
| 674 | |
| 675 | \end{seealso} |
| 676 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 677 | |
| 678 | %====================================================================== |
Andrew M. Kuchling | 8f4d255 | 2006-03-08 01:50:20 +0000 | [diff] [blame] | 679 | \section{PEP 352: Exceptions as New-Style Classes} |
| 680 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 681 | Exception classes can now be new-style classes, not just classic |
| 682 | classes, and the built-in \exception{Exception} class and all the |
| 683 | standard built-in exceptions (\exception{NameError}, |
| 684 | \exception{ValueError}, etc.) are now new-style classes. |
Andrew M. Kuchling | aeadf95 | 2006-03-09 19:06:05 +0000 | [diff] [blame] | 685 | |
| 686 | The inheritance hierarchy for exceptions has been rearranged a bit. |
| 687 | In 2.5, the inheritance relationships are: |
| 688 | |
| 689 | \begin{verbatim} |
| 690 | BaseException # New in Python 2.5 |
| 691 | |- KeyboardInterrupt |
| 692 | |- SystemExit |
| 693 | |- Exception |
| 694 | |- (all other current built-in exceptions) |
| 695 | \end{verbatim} |
| 696 | |
| 697 | This rearrangement was done because people often want to catch all |
| 698 | exceptions that indicate program errors. \exception{KeyboardInterrupt} and |
| 699 | \exception{SystemExit} aren't errors, though, and usually represent an explicit |
| 700 | action such as the user hitting Control-C or code calling |
| 701 | \function{sys.exit()}. A bare \code{except:} will catch all exceptions, |
| 702 | so you commonly need to list \exception{KeyboardInterrupt} and |
| 703 | \exception{SystemExit} in order to re-raise them. The usual pattern is: |
| 704 | |
| 705 | \begin{verbatim} |
| 706 | try: |
| 707 | ... |
| 708 | except (KeyboardInterrupt, SystemExit): |
| 709 | raise |
| 710 | except: |
| 711 | # Log error... |
| 712 | # Continue running program... |
| 713 | \end{verbatim} |
| 714 | |
| 715 | In Python 2.5, you can now write \code{except Exception} to achieve |
| 716 | the same result, catching all the exceptions that usually indicate errors |
| 717 | but leaving \exception{KeyboardInterrupt} and |
| 718 | \exception{SystemExit} alone. As in previous versions, |
| 719 | a bare \code{except:} still catches all exceptions. |
| 720 | |
| 721 | The goal for Python 3.0 is to require any class raised as an exception |
| 722 | to derive from \exception{BaseException} or some descendant of |
| 723 | \exception{BaseException}, and future releases in the |
| 724 | Python 2.x series may begin to enforce this constraint. Therefore, I |
| 725 | suggest you begin making all your exception classes derive from |
| 726 | \exception{Exception} now. It's been suggested that the bare |
| 727 | \code{except:} form should be removed in Python 3.0, but Guido van~Rossum |
| 728 | hasn't decided whether to do this or not. |
| 729 | |
| 730 | Raising of strings as exceptions, as in the statement \code{raise |
| 731 | "Error occurred"}, is deprecated in Python 2.5 and will trigger a |
| 732 | warning. The aim is to be able to remove the string-exception feature |
| 733 | in a few releases. |
| 734 | |
| 735 | |
| 736 | \begin{seealso} |
| 737 | |
Andrew M. Kuchling | c3749a9 | 2006-04-04 19:14:41 +0000 | [diff] [blame] | 738 | \seepep{352}{Required Superclass for Exceptions}{PEP written by |
Andrew M. Kuchling | aeadf95 | 2006-03-09 19:06:05 +0000 | [diff] [blame] | 739 | Brett Cannon and Guido van Rossum; implemented by Brett Cannon.} |
| 740 | |
| 741 | \end{seealso} |
Andrew M. Kuchling | 8f4d255 | 2006-03-08 01:50:20 +0000 | [diff] [blame] | 742 | |
| 743 | |
| 744 | %====================================================================== |
Andrew M. Kuchling | 4d8cd89 | 2006-04-06 13:03:04 +0000 | [diff] [blame] | 745 | \section{PEP 353: Using ssize_t as the index type\label{section-353}} |
Andrew M. Kuchling | c3749a9 | 2006-04-04 19:14:41 +0000 | [diff] [blame] | 746 | |
| 747 | A wide-ranging change to Python's C API, using a new |
| 748 | \ctype{Py_ssize_t} type definition instead of \ctype{int}, |
| 749 | will permit the interpreter to handle more data on 64-bit platforms. |
| 750 | This change doesn't affect Python's capacity on 32-bit platforms. |
| 751 | |
Andrew M. Kuchling | 4d8cd89 | 2006-04-06 13:03:04 +0000 | [diff] [blame] | 752 | Various pieces of the Python interpreter used C's \ctype{int} type to |
| 753 | store sizes or counts; for example, the number of items in a list or |
| 754 | tuple were stored in an \ctype{int}. The C compilers for most 64-bit |
| 755 | platforms still define \ctype{int} as a 32-bit type, so that meant |
| 756 | that lists could only hold up to \code{2**31 - 1} = 2147483647 items. |
| 757 | (There are actually a few different programming models that 64-bit C |
| 758 | compilers can use -- see |
| 759 | \url{http://www.unix.org/version2/whatsnew/lp64_wp.html} for a |
| 760 | discussion -- but the most commonly available model leaves \ctype{int} |
| 761 | as 32 bits.) |
| 762 | |
| 763 | A limit of 2147483647 items doesn't really matter on a 32-bit platform |
| 764 | because you'll run out of memory before hitting the length limit. |
| 765 | Each list item requires space for a pointer, which is 4 bytes, plus |
| 766 | space for a \ctype{PyObject} representing the item. 2147483647*4 is |
| 767 | already more bytes than a 32-bit address space can contain. |
| 768 | |
| 769 | It's possible to address that much memory on a 64-bit platform, |
| 770 | however. The pointers for a list that size would only require 16GiB |
| 771 | of space, so it's not unreasonable that Python programmers might |
| 772 | construct lists that large. Therefore, the Python interpreter had to |
| 773 | be changed to use some type other than \ctype{int}, and this will be a |
| 774 | 64-bit type on 64-bit platforms. The change will cause |
| 775 | incompatibilities on 64-bit machines, so it was deemed worth making |
| 776 | the transition now, while the number of 64-bit users is still |
| 777 | relatively small. (In 5 or 10 years, we may \emph{all} be on 64-bit |
| 778 | machines, and the transition would be more painful then.) |
| 779 | |
| 780 | This change most strongly affects authors of C extension modules. |
| 781 | Python strings and container types such as lists and tuples |
| 782 | now use \ctype{Py_ssize_t} to store their size. |
| 783 | Functions such as \cfunction{PyList_Size()} |
| 784 | now return \ctype{Py_ssize_t}. Code in extension modules |
| 785 | may therefore need to have some variables changed to |
| 786 | \ctype{Py_ssize_t}. |
| 787 | |
| 788 | The \cfunction{PyArg_ParseTuple()} and \cfunction{Py_BuildValue()} functions |
| 789 | have a new conversion code, \samp{n}, for \ctype{Py_ssize_t}. |
Andrew M. Kuchling | a4d651f | 2006-04-06 13:24:58 +0000 | [diff] [blame] | 790 | \cfunction{PyArg_ParseTuple()}'s \samp{s\#} and \samp{t\#} still output |
Andrew M. Kuchling | 4d8cd89 | 2006-04-06 13:03:04 +0000 | [diff] [blame] | 791 | \ctype{int} by default, but you can define the macro |
| 792 | \csimplemacro{PY_SSIZE_T_CLEAN} before including \file{Python.h} |
| 793 | to make them return \ctype{Py_ssize_t}. |
| 794 | |
| 795 | \pep{353} has a section on conversion guidelines that |
| 796 | extension authors should read to learn about supporting 64-bit |
| 797 | platforms. |
Andrew M. Kuchling | c3749a9 | 2006-04-04 19:14:41 +0000 | [diff] [blame] | 798 | |
| 799 | \begin{seealso} |
| 800 | |
Andrew M. Kuchling | 5f445bf | 2006-04-12 18:54:00 +0000 | [diff] [blame] | 801 | \seepep{353}{Using ssize_t as the index type}{PEP written and implemented by Martin von~L\"owis.} |
Andrew M. Kuchling | c3749a9 | 2006-04-04 19:14:41 +0000 | [diff] [blame] | 802 | |
| 803 | \end{seealso} |
| 804 | |
Andrew M. Kuchling | 4d8cd89 | 2006-04-06 13:03:04 +0000 | [diff] [blame] | 805 | |
Andrew M. Kuchling | c3749a9 | 2006-04-04 19:14:41 +0000 | [diff] [blame] | 806 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 807 | \section{PEP 357: The '__index__' method} |
| 808 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 809 | The NumPy developers had a problem that could only be solved by adding |
| 810 | a new special method, \method{__index__}. When using slice notation, |
Fred Drake | 1c0e328 | 2006-04-02 03:30:06 +0000 | [diff] [blame] | 811 | 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] | 812 | \var{start}, \var{stop}, and \var{step} indexes must all be either |
| 813 | integers or long integers. NumPy defines a variety of specialized |
| 814 | integer types corresponding to unsigned and signed integers of 8, 16, |
| 815 | 32, and 64 bits, but there was no way to signal that these types could |
| 816 | be used as slice indexes. |
| 817 | |
| 818 | Slicing can't just use the existing \method{__int__} method because |
| 819 | that method is also used to implement coercion to integers. If |
| 820 | slicing used \method{__int__}, floating-point numbers would also |
| 821 | become legal slice indexes and that's clearly an undesirable |
| 822 | behaviour. |
| 823 | |
| 824 | Instead, a new special method called \method{__index__} was added. It |
| 825 | takes no arguments and returns an integer giving the slice index to |
| 826 | use. For example: |
| 827 | |
| 828 | \begin{verbatim} |
| 829 | class C: |
| 830 | def __index__ (self): |
| 831 | return self.value |
| 832 | \end{verbatim} |
| 833 | |
| 834 | The return value must be either a Python integer or long integer. |
| 835 | The interpreter will check that the type returned is correct, and |
| 836 | raises a \exception{TypeError} if this requirement isn't met. |
| 837 | |
| 838 | A corresponding \member{nb_index} slot was added to the C-level |
| 839 | \ctype{PyNumberMethods} structure to let C extensions implement this |
| 840 | protocol. \cfunction{PyNumber_Index(\var{obj})} can be used in |
| 841 | extension code to call the \method{__index__} function and retrieve |
| 842 | its result. |
| 843 | |
| 844 | \begin{seealso} |
| 845 | |
| 846 | \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] | 847 | and implemented by Travis Oliphant.} |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 848 | |
| 849 | \end{seealso} |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 850 | |
| 851 | |
| 852 | %====================================================================== |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 853 | \section{Other Language Changes} |
| 854 | |
| 855 | Here are all of the changes that Python 2.5 makes to the core Python |
| 856 | language. |
| 857 | |
| 858 | \begin{itemize} |
Andrew M. Kuchling | 1cae3f5 | 2004-12-03 14:57:21 +0000 | [diff] [blame] | 859 | |
Andrew M. Kuchling | c709584 | 2006-04-14 12:41:19 +0000 | [diff] [blame] | 860 | \item The \class{dict} type has a new hook for letting subclasses |
| 861 | provide a default value when a key isn't contained in the dictionary. |
| 862 | When a key isn't found, the dictionary's |
| 863 | \method{__missing__(\var{key})} |
| 864 | method will be called. This hook is used to implement |
| 865 | the new \class{defaultdict} class in the \module{collections} |
| 866 | module. The following example defines a dictionary |
| 867 | that returns zero for any missing key: |
| 868 | |
| 869 | \begin{verbatim} |
| 870 | class zerodict (dict): |
| 871 | def __missing__ (self, key): |
| 872 | return 0 |
| 873 | |
| 874 | d = zerodict({1:1, 2:2}) |
| 875 | print d[1], d[2] # Prints 1, 2 |
| 876 | print d[3], d[4] # Prints 0, 0 |
| 877 | \end{verbatim} |
| 878 | |
Andrew M. Kuchling | 1cae3f5 | 2004-12-03 14:57:21 +0000 | [diff] [blame] | 879 | \item The \function{min()} and \function{max()} built-in functions |
| 880 | gained a \code{key} keyword argument analogous to the \code{key} |
Andrew M. Kuchling | c709584 | 2006-04-14 12:41:19 +0000 | [diff] [blame] | 881 | argument for \method{sort()}. This argument supplies a function that |
| 882 | takes a single argument and is called for every value in the list; |
Andrew M. Kuchling | 1cae3f5 | 2004-12-03 14:57:21 +0000 | [diff] [blame] | 883 | \function{min()}/\function{max()} will return the element with the |
| 884 | smallest/largest return value from this function. |
| 885 | For example, to find the longest string in a list, you can do: |
| 886 | |
| 887 | \begin{verbatim} |
| 888 | L = ['medium', 'longest', 'short'] |
| 889 | # Prints 'longest' |
| 890 | print max(L, key=len) |
| 891 | # Prints 'short', because lexicographically 'short' has the largest value |
| 892 | print max(L) |
| 893 | \end{verbatim} |
| 894 | |
| 895 | (Contributed by Steven Bethard and Raymond Hettinger.) |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 896 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 897 | \item Two new built-in functions, \function{any()} and |
| 898 | \function{all()}, evaluate whether an iterator contains any true or |
| 899 | false values. \function{any()} returns \constant{True} if any value |
| 900 | returned by the iterator is true; otherwise it will return |
| 901 | \constant{False}. \function{all()} returns \constant{True} only if |
| 902 | all of the values returned by the iterator evaluate as being true. |
Andrew M. Kuchling | 6e3a66d | 2006-04-07 12:46:06 +0000 | [diff] [blame] | 903 | (Suggested by GvR, and implemented by Raymond Hettinger.) |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 904 | |
Andrew M. Kuchling | 5f445bf | 2006-04-12 18:54:00 +0000 | [diff] [blame] | 905 | \item ASCII is now the default encoding for modules. It's now |
| 906 | a syntax error if a module contains string literals with 8-bit |
| 907 | characters but doesn't have an encoding declaration. In Python 2.4 |
| 908 | this triggered a warning, not a syntax error. See \pep{263} |
| 909 | for how to declare a module's encoding; for example, you might add |
| 910 | a line like this near the top of the source file: |
| 911 | |
| 912 | \begin{verbatim} |
| 913 | # -*- coding: latin1 -*- |
| 914 | \end{verbatim} |
| 915 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 916 | \item The list of base classes in a class definition can now be empty. |
| 917 | As an example, this is now legal: |
| 918 | |
| 919 | \begin{verbatim} |
| 920 | class C(): |
| 921 | pass |
| 922 | \end{verbatim} |
| 923 | (Implemented by Brett Cannon.) |
| 924 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 925 | \end{itemize} |
| 926 | |
| 927 | |
| 928 | %====================================================================== |
Andrew M. Kuchling | da37604 | 2006-03-17 15:56:41 +0000 | [diff] [blame] | 929 | \subsection{Interactive Interpreter Changes} |
| 930 | |
| 931 | In the interactive interpreter, \code{quit} and \code{exit} |
| 932 | have long been strings so that new users get a somewhat helpful message |
| 933 | when they try to quit: |
| 934 | |
| 935 | \begin{verbatim} |
| 936 | >>> quit |
| 937 | 'Use Ctrl-D (i.e. EOF) to exit.' |
| 938 | \end{verbatim} |
| 939 | |
| 940 | In Python 2.5, \code{quit} and \code{exit} are now objects that still |
| 941 | produce string representations of themselves, but are also callable. |
| 942 | Newbies who try \code{quit()} or \code{exit()} will now exit the |
| 943 | interpreter as they expect. (Implemented by Georg Brandl.) |
| 944 | |
| 945 | |
| 946 | %====================================================================== |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 947 | \subsection{Optimizations} |
| 948 | |
| 949 | \begin{itemize} |
| 950 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 951 | \item When they were introduced |
| 952 | in Python 2.4, the built-in \class{set} and \class{frozenset} types |
| 953 | were built on top of Python's dictionary type. |
| 954 | In 2.5 the internal data structure has been customized for implementing sets, |
| 955 | and as a result sets will use a third less memory and are somewhat faster. |
| 956 | (Implemented by Raymond Hettinger.) |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 957 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 958 | \item The performance of some Unicode operations has been improved. |
| 959 | % XXX provide details? |
| 960 | |
| 961 | \item The code generator's peephole optimizer now performs |
| 962 | simple constant folding in expressions. If you write something like |
| 963 | \code{a = 2+3}, the code generator will do the arithmetic and produce |
| 964 | code corresponding to \code{a = 5}. |
| 965 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 966 | \end{itemize} |
| 967 | |
| 968 | 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] | 969 | pystone benchmark around XXX\% faster than Python 2.4. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 970 | |
| 971 | |
| 972 | %====================================================================== |
| 973 | \section{New, Improved, and Deprecated Modules} |
| 974 | |
Andrew M. Kuchling | 0f1955d | 2006-04-13 12:09:08 +0000 | [diff] [blame] | 975 | As usual, Python's standard library received many enhancements and |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 976 | bug fixes. Here's a partial list of the most notable changes, sorted |
| 977 | alphabetically by module name. Consult the |
| 978 | \file{Misc/NEWS} file in the source tree for a more |
Andrew M. Kuchling | f688cc5 | 2006-03-10 18:50:08 +0000 | [diff] [blame] | 979 | 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] | 980 | details. |
| 981 | |
| 982 | \begin{itemize} |
| 983 | |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 984 | % the cPickle module no longer accepts the deprecated None option in the |
| 985 | % args tuple returned by __reduce__(). |
| 986 | |
Andrew M. Kuchling | 6fc6976 | 2006-04-13 12:37:21 +0000 | [diff] [blame] | 987 | % XXX csv module improvements |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 988 | |
Andrew M. Kuchling | 6fc6976 | 2006-04-13 12:37:21 +0000 | [diff] [blame] | 989 | % XXX datetime.datetime() now has a strptime class method which can be used to |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 990 | % create datetime object using a string and format. |
| 991 | |
Andrew M. Kuchling | 6fc6976 | 2006-04-13 12:37:21 +0000 | [diff] [blame] | 992 | % XXX fileinput: opening hook used to control how files are opened. |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 993 | % .input() now has a mode parameter |
| 994 | % now has a fileno() function |
| 995 | % accepts Unicode filenames |
| 996 | |
Andrew M. Kuchling | 6fc6976 | 2006-04-13 12:37:21 +0000 | [diff] [blame] | 997 | \item The \module{audioop} module now supports the a-LAW encoding, |
| 998 | and the code for u-LAW encoding has been improved. (Contributed by |
| 999 | Lars Immisch.) |
| 1000 | |
Andrew M. Kuchling | c709584 | 2006-04-14 12:41:19 +0000 | [diff] [blame] | 1001 | \item The \module{collections} module gained a new type, |
| 1002 | \class{defaultdict}, that subclasses the standard \class{dict} |
| 1003 | type. The new type mostly behaves like a dictionary but constructs a |
| 1004 | default value when a key isn't present, automatically adding it to the |
| 1005 | dictionary for the requested key value. |
| 1006 | |
| 1007 | The first argument to \class{defaultdict}'s constructor is a factory |
| 1008 | function that gets called whenever a key is requested but not found. |
| 1009 | This factory function receives no arguments, so you can use built-in |
| 1010 | type constructors such as \function{list()} or \function{int()}. For |
| 1011 | example, |
| 1012 | you can make an index of words based on their initial letter like this: |
| 1013 | |
| 1014 | \begin{verbatim} |
| 1015 | words = """Nel mezzo del cammin di nostra vita |
| 1016 | mi ritrovai per una selva oscura |
| 1017 | che la diritta via era smarrita""".lower().split() |
| 1018 | |
| 1019 | index = defaultdict(list) |
| 1020 | |
| 1021 | for w in words: |
| 1022 | init_letter = w[0] |
| 1023 | index[init_letter].append(w) |
| 1024 | \end{verbatim} |
| 1025 | |
| 1026 | Printing \code{index} results in the following output: |
| 1027 | |
| 1028 | \begin{verbatim} |
| 1029 | defaultdict(<type 'list'>, {'c': ['cammin', 'che'], 'e': ['era'], |
| 1030 | 'd': ['del', 'di', 'diritta'], 'm': ['mezzo', 'mi'], |
| 1031 | 'l': ['la'], 'o': ['oscura'], 'n': ['nel', 'nostra'], |
| 1032 | 'p': ['per'], 's': ['selva', 'smarrita'], |
| 1033 | 'r': ['ritrovai'], 'u': ['una'], 'v': ['vita', 'via']} |
| 1034 | \end{verbatim} |
| 1035 | |
| 1036 | The \class{deque} double-ended queue type supplied by the |
| 1037 | \module{collections} module now has a \method{remove(\var{value})} |
| 1038 | method that removes the first occurrence of \var{value} in the queue, |
| 1039 | raising \exception{ValueError} if the value isn't found. |
| 1040 | |
| 1041 | \item The \module{cProfile} module is a C implementation of |
| 1042 | the existing \module{profile} module that has much lower overhead. |
| 1043 | The module's interface is the same as \module{profile}: you run |
| 1044 | \code{cProfile.run('main()')} to profile a function, can save profile |
| 1045 | data to a file, etc. It's not yet known if the Hotshot profiler, |
| 1046 | which is also written in C but doesn't match the \module{profile} |
| 1047 | module's interface, will continue to be maintained in future versions |
| 1048 | of Python. (Contributed by Armin Rigo.) |
| 1049 | |
Andrew M. Kuchling | da37604 | 2006-03-17 15:56:41 +0000 | [diff] [blame] | 1050 | \item In the \module{gc} module, the new \function{get_count()} function |
| 1051 | returns a 3-tuple containing the current collection counts for the |
| 1052 | three GC generations. This is accounting information for the garbage |
| 1053 | collector; when these counts reach a specified threshold, a garbage |
| 1054 | collection sweep will be made. The existing \function{gc.collect()} |
| 1055 | function now takes an optional \var{generation} argument of 0, 1, or 2 |
| 1056 | to specify which generation to collect. |
| 1057 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 1058 | \item The \function{nsmallest()} and |
| 1059 | \function{nlargest()} functions in the \module{heapq} module |
| 1060 | now support a \code{key} keyword argument similar to the one |
| 1061 | provided by the \function{min()}/\function{max()} functions |
| 1062 | and the \method{sort()} methods. For example: |
| 1063 | Example: |
| 1064 | |
| 1065 | \begin{verbatim} |
| 1066 | >>> import heapq |
| 1067 | >>> L = ["short", 'medium', 'longest', 'longer still'] |
| 1068 | >>> heapq.nsmallest(2, L) # Return two lowest elements, lexicographically |
| 1069 | ['longer still', 'longest'] |
| 1070 | >>> heapq.nsmallest(2, L, key=len) # Return two shortest elements |
| 1071 | ['short', 'medium'] |
| 1072 | \end{verbatim} |
| 1073 | |
| 1074 | (Contributed by Raymond Hettinger.) |
| 1075 | |
Andrew M. Kuchling | 511a3a8 | 2005-03-20 19:52:18 +0000 | [diff] [blame] | 1076 | \item The \function{itertools.islice()} function now accepts |
| 1077 | \code{None} for the start and step arguments. This makes it more |
| 1078 | compatible with the attributes of slice objects, so that you can now write |
| 1079 | the following: |
| 1080 | |
| 1081 | \begin{verbatim} |
| 1082 | s = slice(5) # Create slice object |
| 1083 | itertools.islice(iterable, s.start, s.stop, s.step) |
| 1084 | \end{verbatim} |
| 1085 | |
| 1086 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 1087 | |
Andrew M. Kuchling | 75ba244 | 2006-04-14 10:29:55 +0000 | [diff] [blame] | 1088 | \item The \module{nis} module now supports accessing domains other |
| 1089 | than the system default domain by supplying a \var{domain} argument to |
| 1090 | the \function{nis.match()} and \function{nis.maps()} functions. |
| 1091 | (Contributed by Ben Bell.) |
| 1092 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1093 | \item The \module{operator} module's \function{itemgetter()} |
| 1094 | and \function{attrgetter()} functions now support multiple fields. |
| 1095 | A call such as \code{operator.attrgetter('a', 'b')} |
| 1096 | will return a function |
| 1097 | that retrieves the \member{a} and \member{b} attributes. Combining |
| 1098 | this new feature with the \method{sort()} method's \code{key} parameter |
| 1099 | lets you easily sort lists using multiple fields. |
Andrew M. Kuchling | 6e3a66d | 2006-04-07 12:46:06 +0000 | [diff] [blame] | 1100 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1101 | |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 1102 | |
Andrew M. Kuchling | 0f1955d | 2006-04-13 12:09:08 +0000 | [diff] [blame] | 1103 | \item The \module{os} module underwent several changes. The |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 1104 | \member{stat_float_times} variable now defaults to true, meaning that |
| 1105 | \function{os.stat()} will now return time values as floats. (This |
| 1106 | doesn't necessarily mean that \function{os.stat()} will return times |
| 1107 | that are precise to fractions of a second; not all systems support |
| 1108 | such precision.) |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 1109 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1110 | Constants named \member{os.SEEK_SET}, \member{os.SEEK_CUR}, and |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 1111 | \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] | 1112 | \function{os.lseek()} function. Two new constants for locking are |
| 1113 | \member{os.O_SHLOCK} and \member{os.O_EXLOCK}. |
| 1114 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1115 | Two new functions, \function{wait3()} and \function{wait4()}, were |
| 1116 | added. They're similar the \function{waitpid()} function which waits |
| 1117 | for a child process to exit and returns a tuple of the process ID and |
| 1118 | its exit status, but \function{wait3()} and \function{wait4()} return |
| 1119 | additional information. \function{wait3()} doesn't take a process ID |
| 1120 | as input, so it waits for any child process to exit and returns a |
| 1121 | 3-tuple of \var{process-id}, \var{exit-status}, \var{resource-usage} |
| 1122 | as returned from the \function{resource.getrusage()} function. |
| 1123 | \function{wait4(\var{pid})} does take a process ID. |
Andrew M. Kuchling | 6e3a66d | 2006-04-07 12:46:06 +0000 | [diff] [blame] | 1124 | (Contributed by Chad J. Schroeder.) |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1125 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1126 | On FreeBSD, the \function{os.stat()} function now returns |
| 1127 | times with nanosecond resolution, and the returned object |
| 1128 | now has \member{st_gen} and \member{st_birthtime}. |
| 1129 | The \member{st_flags} member is also available, if the platform supports it. |
Andrew M. Kuchling | 6e3a66d | 2006-04-07 12:46:06 +0000 | [diff] [blame] | 1130 | (Contributed by Antti Louko and Diego Petten\`o.) |
| 1131 | % (Patch 1180695, 1212117) |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1132 | |
Andrew M. Kuchling | 01e3d26 | 2006-03-17 15:38:39 +0000 | [diff] [blame] | 1133 | \item The old \module{regex} and \module{regsub} modules, which have been |
| 1134 | deprecated ever since Python 2.0, have finally been deleted. |
Andrew M. Kuchling | f4b0660 | 2006-03-17 15:39:52 +0000 | [diff] [blame] | 1135 | Other deleted modules: \module{statcache}, \module{tzparse}, |
| 1136 | \module{whrandom}. |
Andrew M. Kuchling | 01e3d26 | 2006-03-17 15:38:39 +0000 | [diff] [blame] | 1137 | |
| 1138 | \item The \file{lib-old} directory, |
| 1139 | which includes ancient modules such as \module{dircmp} and |
| 1140 | \module{ni}, was also deleted. \file{lib-old} wasn't on the default |
| 1141 | \code{sys.path}, so unless your programs explicitly added the directory to |
| 1142 | \code{sys.path}, this removal shouldn't affect your code. |
| 1143 | |
Andrew M. Kuchling | 4678dc8 | 2006-01-15 16:11:28 +0000 | [diff] [blame] | 1144 | \item The \module{socket} module now supports \constant{AF_NETLINK} |
| 1145 | sockets on Linux, thanks to a patch from Philippe Biondi. |
| 1146 | Netlink sockets are a Linux-specific mechanism for communications |
| 1147 | between a user-space process and kernel code; an introductory |
| 1148 | article about them is at \url{http://www.linuxjournal.com/article/7356}. |
| 1149 | In Python code, netlink addresses are represented as a tuple of 2 integers, |
| 1150 | \code{(\var{pid}, \var{group_mask})}. |
| 1151 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1152 | Socket objects also gained accessor methods \method{getfamily()}, |
| 1153 | \method{gettype()}, and \method{getproto()} methods to retrieve the |
| 1154 | family, type, and protocol values for the socket. |
| 1155 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1156 | \item New module: \module{spwd} provides functions for accessing the |
Andrew M. Kuchling | 5f445bf | 2006-04-12 18:54:00 +0000 | [diff] [blame] | 1157 | shadow password database on systems that support it. |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1158 | % XXX give example |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1159 | |
Andrew M. Kuchling | 61434b6 | 2006-04-13 11:51:07 +0000 | [diff] [blame] | 1160 | \item The Python developers switched from CVS to Subversion during the 2.5 |
| 1161 | development process. Information about the exact build version is |
| 1162 | available as the \code{sys.subversion} variable, a 3-tuple |
| 1163 | of \code{(\var{interpreter-name}, \var{branch-name}, \var{revision-range})}. |
| 1164 | For example, at the time of writing |
| 1165 | my copy of 2.5 was reporting \code{('CPython', 'trunk', '45313:45315')}. |
| 1166 | |
| 1167 | This information is also available to C extensions via the |
| 1168 | \cfunction{Py_GetBuildInfo()} function that returns a |
| 1169 | string of build information like this: |
| 1170 | \code{"trunk:45355:45356M, Apr 13 2006, 07:42:19"}. |
| 1171 | (Contributed by Barry Warsaw.) |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1172 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 1173 | \item The \class{TarFile} class in the \module{tarfile} module now has |
Georg Brandl | 08c02db | 2005-07-22 18:39:19 +0000 | [diff] [blame] | 1174 | an \method{extractall()} method that extracts all members from the |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 1175 | archive into the current working directory. It's also possible to set |
| 1176 | 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] | 1177 | subset of the archive's members. |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 1178 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1179 | A tarfile's compression can be autodetected by |
| 1180 | using the mode \code{'r|*'}. |
| 1181 | % patch 918101 |
| 1182 | (Contributed by Lars Gust\"abel.) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1183 | |
Andrew M. Kuchling | f688cc5 | 2006-03-10 18:50:08 +0000 | [diff] [blame] | 1184 | \item The \module{unicodedata} module has been updated to use version 4.1.0 |
| 1185 | of the Unicode character database. Version 3.2.0 is required |
| 1186 | by some specifications, so it's still available as |
| 1187 | \member{unicodedata.db_3_2_0}. |
| 1188 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1189 | % patch #754022: Greatly enhanced webbrowser.py (by Oleg Broytmann). |
| 1190 | |
Fredrik Lundh | 7e0aef0 | 2005-12-12 18:54:55 +0000 | [diff] [blame] | 1191 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1192 | \item The \module{xmlrpclib} module now supports returning |
| 1193 | \class{datetime} objects for the XML-RPC date type. Supply |
| 1194 | \code{use_datetime=True} to the \function{loads()} function |
| 1195 | or the \class{Unmarshaller} class to enable this feature. |
Andrew M. Kuchling | 6e3a66d | 2006-04-07 12:46:06 +0000 | [diff] [blame] | 1196 | (Contributed by Skip Montanaro.) |
| 1197 | % Patch 1120353 |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1198 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1199 | |
Fred Drake | 114b8ca | 2005-03-21 05:47:11 +0000 | [diff] [blame] | 1200 | \end{itemize} |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 1201 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1202 | |
| 1203 | |
| 1204 | %====================================================================== |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1205 | % whole new modules get described in subsections here |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1206 | |
Andrew M. Kuchling | 61434b6 | 2006-04-13 11:51:07 +0000 | [diff] [blame] | 1207 | %====================================================================== |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1208 | \subsection{The ctypes package} |
| 1209 | |
| 1210 | The \module{ctypes} package, written by Thomas Heller, has been added |
| 1211 | to the standard library. \module{ctypes} lets you call arbitrary functions |
Andrew M. Kuchling | 28c5f1f | 2006-04-13 02:04:42 +0000 | [diff] [blame] | 1212 | in shared libraries or DLLs. Long-time users may remember the \module{dl} module, which |
| 1213 | provides functions for loading shared libraries and calling functions in them. The \module{ctypes} package is much fancier. |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1214 | |
Andrew M. Kuchling | 28c5f1f | 2006-04-13 02:04:42 +0000 | [diff] [blame] | 1215 | To load a shared library or DLL, you must create an instance of the |
| 1216 | \class{CDLL} class and provide the name or path of the shared library |
| 1217 | or DLL. Once that's done, you can call arbitrary functions |
| 1218 | by accessing them as attributes of the \class{CDLL} object. |
| 1219 | |
| 1220 | \begin{verbatim} |
| 1221 | import ctypes |
| 1222 | |
| 1223 | libc = ctypes.CDLL('libc.so.6') |
| 1224 | result = libc.printf("Line of output\n") |
| 1225 | \end{verbatim} |
| 1226 | |
| 1227 | Type constructors for the various C types are provided: \function{c_int}, |
| 1228 | \function{c_float}, \function{c_double}, \function{c_char_p} (equivalent to \ctype{char *}), and so forth. Unlike Python's types, the C versions are all mutable; you can assign to their \member{value} attribute |
| 1229 | to change the wrapped value. Python integers and strings will be automatically |
| 1230 | converted to the corresponding C types, but for other types you |
| 1231 | must call the correct type constructor. (And I mean \emph{must}; |
| 1232 | getting it wrong will often result in the interpreter crashing |
| 1233 | with a segmentation fault.) |
| 1234 | |
| 1235 | You shouldn't use \function{c_char_p} with a Python string when the C function will be modifying the memory area, because Python strings are |
| 1236 | supposed to be immutable; breaking this rule will cause puzzling bugs. When you need a modifiable memory area, |
Neal Norwitz | 5f5a69b | 2006-04-13 03:41:04 +0000 | [diff] [blame] | 1237 | use \function{create_string_buffer()}: |
Andrew M. Kuchling | 28c5f1f | 2006-04-13 02:04:42 +0000 | [diff] [blame] | 1238 | |
| 1239 | \begin{verbatim} |
| 1240 | s = "this is a string" |
| 1241 | buf = ctypes.create_string_buffer(s) |
| 1242 | libc.strfry(buf) |
| 1243 | \end{verbatim} |
| 1244 | |
| 1245 | C functions are assumed to return integers, but you can set |
| 1246 | the \member{restype} attribute of the function object to |
| 1247 | change this: |
| 1248 | |
| 1249 | \begin{verbatim} |
| 1250 | >>> libc.atof('2.71828') |
| 1251 | -1783957616 |
| 1252 | >>> libc.atof.restype = ctypes.c_double |
| 1253 | >>> libc.atof('2.71828') |
| 1254 | 2.71828 |
| 1255 | \end{verbatim} |
| 1256 | |
| 1257 | \module{ctypes} also provides a wrapper for Python's C API |
| 1258 | as the \code{ctypes.pythonapi} object. This object does \emph{not} |
| 1259 | release the global interpreter lock before calling a function, because the lock must be held when calling into the interpreter's code. |
| 1260 | There's a \class{py_object()} type constructor that will create a |
| 1261 | \ctype{PyObject *} pointer. A simple usage: |
| 1262 | |
| 1263 | \begin{verbatim} |
| 1264 | import ctypes |
| 1265 | |
| 1266 | d = {} |
| 1267 | ctypes.pythonapi.PyObject_SetItem(ctypes.py_object(d), |
| 1268 | ctypes.py_object("abc"), ctypes.py_object(1)) |
| 1269 | # d is now {'abc', 1}. |
| 1270 | \end{verbatim} |
| 1271 | |
| 1272 | Don't forget to use \class{py_object()}; if it's omitted you end |
| 1273 | up with a segmentation fault. |
| 1274 | |
| 1275 | \module{ctypes} has been around for a while, but people still write |
| 1276 | and distribution hand-coded extension modules because you can't rely on \module{ctypes} being present. |
| 1277 | Perhaps developers will begin to write |
| 1278 | Python wrappers atop a library accessed through \module{ctypes} instead |
| 1279 | of extension modules, now that \module{ctypes} is included with core Python. |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1280 | |
Andrew M. Kuchling | 28c5f1f | 2006-04-13 02:04:42 +0000 | [diff] [blame] | 1281 | \begin{seealso} |
| 1282 | |
| 1283 | \seeurl{http://starship.python.net/crew/theller/ctypes/} |
| 1284 | {The ctypes web page, with a tutorial, reference, and FAQ.} |
| 1285 | |
| 1286 | \end{seealso} |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1287 | |
Andrew M. Kuchling | 61434b6 | 2006-04-13 11:51:07 +0000 | [diff] [blame] | 1288 | |
| 1289 | %====================================================================== |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1290 | \subsection{The ElementTree package} |
| 1291 | |
| 1292 | A subset of Fredrik Lundh's ElementTree library for processing XML has |
Andrew M. Kuchling | 16ed521 | 2006-04-10 22:28:11 +0000 | [diff] [blame] | 1293 | been added to the standard library as \module{xmlcore.etree}. The |
Georg Brandl | ce27a06 | 2006-04-11 06:27:12 +0000 | [diff] [blame] | 1294 | available modules are |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1295 | \module{ElementTree}, \module{ElementPath}, and |
Andrew M. Kuchling | 4d8cd89 | 2006-04-06 13:03:04 +0000 | [diff] [blame] | 1296 | \module{ElementInclude} from ElementTree 1.2.6. |
| 1297 | The \module{cElementTree} accelerator module is also included. |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1298 | |
Andrew M. Kuchling | 16ed521 | 2006-04-10 22:28:11 +0000 | [diff] [blame] | 1299 | The rest of this section will provide a brief overview of using |
| 1300 | ElementTree. Full documentation for ElementTree is available at |
| 1301 | \url{http://effbot.org/zone/element-index.htm}. |
| 1302 | |
| 1303 | ElementTree represents an XML document as a tree of element nodes. |
| 1304 | The text content of the document is stored as the \member{.text} |
| 1305 | and \member{.tail} attributes of |
| 1306 | (This is one of the major differences between ElementTree and |
| 1307 | the Document Object Model; in the DOM there are many different |
| 1308 | types of node, including \class{TextNode}.) |
| 1309 | |
| 1310 | The most commonly used parsing function is \function{parse()}, that |
| 1311 | takes either a string (assumed to contain a filename) or a file-like |
| 1312 | object and returns an \class{ElementTree} instance: |
| 1313 | |
| 1314 | \begin{verbatim} |
| 1315 | from xmlcore.etree import ElementTree as ET |
| 1316 | |
| 1317 | tree = ET.parse('ex-1.xml') |
| 1318 | |
| 1319 | feed = urllib.urlopen( |
| 1320 | 'http://planet.python.org/rss10.xml') |
| 1321 | tree = ET.parse(feed) |
| 1322 | \end{verbatim} |
| 1323 | |
| 1324 | Once you have an \class{ElementTree} instance, you |
| 1325 | can call its \method{getroot()} method to get the root \class{Element} node. |
| 1326 | |
| 1327 | There's also an \function{XML()} function that takes a string literal |
| 1328 | and returns an \class{Element} node (not an \class{ElementTree}). |
| 1329 | This function provides a tidy way to incorporate XML fragments, |
| 1330 | approaching the convenience of an XML literal: |
| 1331 | |
| 1332 | \begin{verbatim} |
| 1333 | svg = et.XML("""<svg width="10px" version="1.0"> |
| 1334 | </svg>""") |
| 1335 | svg.set('height', '320px') |
| 1336 | svg.append(elem1) |
| 1337 | \end{verbatim} |
| 1338 | |
| 1339 | Each XML element supports some dictionary-like and some list-like |
Andrew M. Kuchling | 075e023 | 2006-04-11 13:14:56 +0000 | [diff] [blame] | 1340 | access methods. Dictionary-like operations are used to access attribute |
| 1341 | values, and list-like operations are used to access child nodes. |
Andrew M. Kuchling | 16ed521 | 2006-04-10 22:28:11 +0000 | [diff] [blame] | 1342 | |
Andrew M. Kuchling | 075e023 | 2006-04-11 13:14:56 +0000 | [diff] [blame] | 1343 | \begin{tableii}{c|l}{code}{Operation}{Result} |
| 1344 | \lineii{elem[n]}{Returns n'th child element.} |
| 1345 | \lineii{elem[m:n]}{Returns list of m'th through n'th child elements.} |
| 1346 | \lineii{len(elem)}{Returns number of child elements.} |
| 1347 | \lineii{elem.getchildren()}{Returns list of child elements.} |
| 1348 | \lineii{elem.append(elem2)}{Adds \var{elem2} as a child.} |
| 1349 | \lineii{elem.insert(index, elem2)}{Inserts \var{elem2} at the specified location.} |
| 1350 | \lineii{del elem[n]}{Deletes n'th child element.} |
| 1351 | \lineii{elem.keys()}{Returns list of attribute names.} |
| 1352 | \lineii{elem.get(name)}{Returns value of attribute \var{name}.} |
| 1353 | \lineii{elem.set(name, value)}{Sets new value for attribute \var{name}.} |
| 1354 | \lineii{elem.attrib}{Retrieves the dictionary containing attributes.} |
| 1355 | \lineii{del elem.attrib[name]}{Deletes attribute \var{name}.} |
| 1356 | \end{tableii} |
| 1357 | |
| 1358 | Comments and processing instructions are also represented as |
| 1359 | \class{Element} nodes. To check if a node is a comment or processing |
| 1360 | instructions: |
| 1361 | |
| 1362 | \begin{verbatim} |
| 1363 | if elem.tag is ET.Comment: |
| 1364 | ... |
| 1365 | elif elem.tag is ET.ProcessingInstruction: |
| 1366 | ... |
| 1367 | \end{verbatim} |
Andrew M. Kuchling | 16ed521 | 2006-04-10 22:28:11 +0000 | [diff] [blame] | 1368 | |
| 1369 | To generate XML output, you should call the |
| 1370 | \method{ElementTree.write()} method. Like \function{parse()}, |
| 1371 | it can take either a string or a file-like object: |
| 1372 | |
| 1373 | \begin{verbatim} |
| 1374 | # Encoding is US-ASCII |
| 1375 | tree.write('output.xml') |
| 1376 | |
| 1377 | # Encoding is UTF-8 |
| 1378 | f = open('output.xml', 'w') |
| 1379 | tree.write(f, 'utf-8') |
| 1380 | \end{verbatim} |
| 1381 | |
| 1382 | (Caution: the default encoding used for output is ASCII, which isn't |
| 1383 | very useful for general XML work, raising an exception if there are |
| 1384 | any characters with values greater than 127. You should always |
| 1385 | specify a different encoding such as UTF-8 that can handle any Unicode |
| 1386 | character.) |
| 1387 | |
Andrew M. Kuchling | 075e023 | 2006-04-11 13:14:56 +0000 | [diff] [blame] | 1388 | This section is only a partial description of the ElementTree interfaces. |
| 1389 | Please read the package's official documentation for more details. |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1390 | |
Andrew M. Kuchling | 16ed521 | 2006-04-10 22:28:11 +0000 | [diff] [blame] | 1391 | \begin{seealso} |
| 1392 | |
| 1393 | \seeurl{http://effbot.org/zone/element-index.htm} |
| 1394 | {Official documentation for ElementTree.} |
| 1395 | |
| 1396 | |
| 1397 | \end{seealso} |
| 1398 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1399 | |
Andrew M. Kuchling | 61434b6 | 2006-04-13 11:51:07 +0000 | [diff] [blame] | 1400 | %====================================================================== |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1401 | \subsection{The hashlib package} |
| 1402 | |
Andrew M. Kuchling | 29b3d08 | 2006-04-14 20:35:17 +0000 | [diff] [blame] | 1403 | A new \module{hashlib} module, written by Gregory P. Smith, |
| 1404 | has been added to replace the |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1405 | \module{md5} and \module{sha} modules. \module{hashlib} adds support |
| 1406 | for additional secure hashes (SHA-224, SHA-256, SHA-384, and SHA-512). |
| 1407 | When available, the module uses OpenSSL for fast platform optimized |
| 1408 | implementations of algorithms. |
| 1409 | |
| 1410 | The old \module{md5} and \module{sha} modules still exist as wrappers |
| 1411 | around hashlib to preserve backwards compatibility. The new module's |
| 1412 | interface is very close to that of the old modules, but not identical. |
| 1413 | The most significant difference is that the constructor functions |
| 1414 | for creating new hashing objects are named differently. |
| 1415 | |
| 1416 | \begin{verbatim} |
| 1417 | # Old versions |
| 1418 | h = md5.md5() |
| 1419 | h = md5.new() |
| 1420 | |
| 1421 | # New version |
| 1422 | h = hashlib.md5() |
| 1423 | |
| 1424 | # Old versions |
| 1425 | h = sha.sha() |
| 1426 | h = sha.new() |
| 1427 | |
| 1428 | # New version |
| 1429 | h = hashlib.sha1() |
| 1430 | |
| 1431 | # Hash that weren't previously available |
| 1432 | h = hashlib.sha224() |
| 1433 | h = hashlib.sha256() |
| 1434 | h = hashlib.sha384() |
| 1435 | h = hashlib.sha512() |
| 1436 | |
| 1437 | # Alternative form |
| 1438 | h = hashlib.new('md5') # Provide algorithm as a string |
| 1439 | \end{verbatim} |
| 1440 | |
| 1441 | Once a hash object has been created, its methods are the same as before: |
| 1442 | \method{update(\var{string})} hashes the specified string into the |
| 1443 | current digest state, \method{digest()} and \method{hexdigest()} |
| 1444 | return the digest value as a binary string or a string of hex digits, |
| 1445 | and \method{copy()} returns a new hashing object with the same digest state. |
| 1446 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1447 | |
Andrew M. Kuchling | 61434b6 | 2006-04-13 11:51:07 +0000 | [diff] [blame] | 1448 | %====================================================================== |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1449 | \subsection{The sqlite3 package} |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1450 | |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1451 | The pysqlite module (\url{http://www.pysqlite.org}), a wrapper for the |
| 1452 | SQLite embedded database, has been added to the standard library under |
Andrew M. Kuchling | 29b3d08 | 2006-04-14 20:35:17 +0000 | [diff] [blame] | 1453 | the package name \module{sqlite3}. |
| 1454 | |
| 1455 | SQLite is a C library that provides a SQL-language database that |
| 1456 | stores data in disk files without requiring a separate server process. |
| 1457 | pysqlite was written by Gerhard H\"aring and provides a SQL interface |
| 1458 | compliant with the DB-API 2.0 specification described by |
| 1459 | \pep{249}. This means that it should be possible to write the first |
| 1460 | version of your applications using SQLite for data storage. If |
| 1461 | switching to a larger database such as PostgreSQL or Oracle is |
| 1462 | later necessary, the switch should be relatively easy. |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1463 | |
| 1464 | If you're compiling the Python source yourself, note that the source |
Andrew M. Kuchling | 29b3d08 | 2006-04-14 20:35:17 +0000 | [diff] [blame] | 1465 | tree doesn't include the SQLite code, only the wrapper module. |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1466 | You'll need to have the SQLite libraries and headers installed before |
| 1467 | compiling Python, and the build process will compile the module when |
| 1468 | the necessary headers are available. |
| 1469 | |
Andrew M. Kuchling | d58baf8 | 2006-04-10 21:40:16 +0000 | [diff] [blame] | 1470 | To use the module, you must first create a \class{Connection} object |
| 1471 | that represents the database. Here the data will be stored in the |
| 1472 | \file{/tmp/example} file: |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1473 | |
Andrew M. Kuchling | d58baf8 | 2006-04-10 21:40:16 +0000 | [diff] [blame] | 1474 | \begin{verbatim} |
| 1475 | conn = sqlite3.connect('/tmp/example') |
| 1476 | \end{verbatim} |
| 1477 | |
| 1478 | You can also supply the special name \samp{:memory:} to create |
| 1479 | a database in RAM. |
| 1480 | |
| 1481 | Once you have a \class{Connection}, you can create a \class{Cursor} |
| 1482 | object and call its \method{execute()} method to perform SQL commands: |
| 1483 | |
| 1484 | \begin{verbatim} |
| 1485 | c = conn.cursor() |
| 1486 | |
| 1487 | # Create table |
| 1488 | c.execute('''create table stocks |
| 1489 | (date timestamp, trans varchar, symbol varchar, |
| 1490 | qty decimal, price decimal)''') |
| 1491 | |
| 1492 | # Insert a row of data |
| 1493 | c.execute("""insert into stocks |
Andrew M. Kuchling | 29b3d08 | 2006-04-14 20:35:17 +0000 | [diff] [blame] | 1494 | values ('2006-01-05','BUY','RHAT',100,35.14)""") |
Andrew M. Kuchling | d58baf8 | 2006-04-10 21:40:16 +0000 | [diff] [blame] | 1495 | \end{verbatim} |
| 1496 | |
Andrew M. Kuchling | 29b3d08 | 2006-04-14 20:35:17 +0000 | [diff] [blame] | 1497 | Usually your SQL operations will need to use values from Python |
Andrew M. Kuchling | d58baf8 | 2006-04-10 21:40:16 +0000 | [diff] [blame] | 1498 | variables. You shouldn't assemble your query using Python's string |
| 1499 | operations because doing so is insecure; it makes your program |
Andrew M. Kuchling | 29b3d08 | 2006-04-14 20:35:17 +0000 | [diff] [blame] | 1500 | vulnerable to an SQL injection attack. |
| 1501 | |
| 1502 | Instead, use SQLite's parameter substitution. Put \samp{?} as a |
| 1503 | placeholder wherever you want to use a value, and then provide a tuple |
| 1504 | of values as the second argument to the cursor's \method{execute()} |
| 1505 | method. For example: |
Andrew M. Kuchling | d58baf8 | 2006-04-10 21:40:16 +0000 | [diff] [blame] | 1506 | |
| 1507 | \begin{verbatim} |
| 1508 | # Never do this -- insecure! |
| 1509 | symbol = 'IBM' |
| 1510 | c.execute("... where symbol = '%s'" % symbol) |
| 1511 | |
| 1512 | # Do this instead |
| 1513 | t = (symbol,) |
Andrew M. Kuchling | 29b3d08 | 2006-04-14 20:35:17 +0000 | [diff] [blame] | 1514 | c.execute('select * from stocks where symbol=?', ('IBM',)) |
Andrew M. Kuchling | d58baf8 | 2006-04-10 21:40:16 +0000 | [diff] [blame] | 1515 | |
| 1516 | # Larger example |
| 1517 | for t in (('2006-03-28', 'BUY', 'IBM', 1000, 45.00), |
| 1518 | ('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00), |
| 1519 | ('2006-04-06', 'SELL', 'IBM', 500, 53.00), |
| 1520 | ): |
| 1521 | c.execute('insert into stocks values (?,?,?,?,?)', t) |
| 1522 | \end{verbatim} |
| 1523 | |
| 1524 | To retrieve data after executing a SELECT statement, you can either |
| 1525 | treat the cursor as an iterator, call the cursor's \method{fetchone()} |
| 1526 | method to retrieve a single matching row, |
| 1527 | or call \method{fetchall()} to get a list of the matching rows. |
| 1528 | |
| 1529 | This example uses the iterator form: |
| 1530 | |
| 1531 | \begin{verbatim} |
| 1532 | >>> c = conn.cursor() |
| 1533 | >>> c.execute('select * from stocks order by price') |
| 1534 | >>> for row in c: |
| 1535 | ... print row |
| 1536 | ... |
| 1537 | (u'2006-01-05', u'BUY', u'RHAT', 100, 35.140000000000001) |
| 1538 | (u'2006-03-28', u'BUY', u'IBM', 1000, 45.0) |
| 1539 | (u'2006-04-06', u'SELL', u'IBM', 500, 53.0) |
| 1540 | (u'2006-04-05', u'BUY', u'MSOFT', 1000, 72.0) |
| 1541 | >>> |
| 1542 | \end{verbatim} |
| 1543 | |
Andrew M. Kuchling | d58baf8 | 2006-04-10 21:40:16 +0000 | [diff] [blame] | 1544 | For more information about the SQL dialect supported by SQLite, see |
| 1545 | \url{http://www.sqlite.org}. |
| 1546 | |
| 1547 | \begin{seealso} |
| 1548 | |
| 1549 | \seeurl{http://www.pysqlite.org} |
| 1550 | {The pysqlite web page.} |
| 1551 | |
| 1552 | \seeurl{http://www.sqlite.org} |
| 1553 | {The SQLite web page; the documentation describes the syntax and the |
| 1554 | available data types for the supported SQL dialect.} |
| 1555 | |
| 1556 | \seepep{249}{Database API Specification 2.0}{PEP written by |
| 1557 | Marc-Andr\'e Lemburg.} |
| 1558 | |
| 1559 | \end{seealso} |
Andrew M. Kuchling | af7ee99 | 2006-04-03 12:41:37 +0000 | [diff] [blame] | 1560 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1561 | |
| 1562 | % ====================================================================== |
| 1563 | \section{Build and C API Changes} |
| 1564 | |
| 1565 | Changes to Python's build process and to the C API include: |
| 1566 | |
| 1567 | \begin{itemize} |
| 1568 | |
Andrew M. Kuchling | 4d8cd89 | 2006-04-06 13:03:04 +0000 | [diff] [blame] | 1569 | \item The largest change to the C API came from \pep{353}, |
| 1570 | which modifies the interpreter to use a \ctype{Py_ssize_t} type |
| 1571 | definition instead of \ctype{int}. See the earlier |
| 1572 | section~ref{section-353} for a discussion of this change. |
| 1573 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1574 | \item The design of the bytecode compiler has changed a great deal, to |
| 1575 | no longer generate bytecode by traversing the parse tree. Instead |
Andrew M. Kuchling | db85ed5 | 2005-10-23 21:52:59 +0000 | [diff] [blame] | 1576 | the parse tree is converted to an abstract syntax tree (or AST), and it is |
| 1577 | the abstract syntax tree that's traversed to produce the bytecode. |
| 1578 | |
Andrew M. Kuchling | 4e86195 | 2006-04-12 12:16:31 +0000 | [diff] [blame] | 1579 | It's possible for Python code to obtain AST objects by using the |
Andrew M. Kuchling | 5f445bf | 2006-04-12 18:54:00 +0000 | [diff] [blame] | 1580 | \function{compile()} built-in and specifying \code{_ast.PyCF_ONLY_AST} |
| 1581 | as the value of the |
Andrew M. Kuchling | 4e86195 | 2006-04-12 12:16:31 +0000 | [diff] [blame] | 1582 | \var{flags} parameter: |
| 1583 | |
| 1584 | \begin{verbatim} |
Andrew M. Kuchling | 5f445bf | 2006-04-12 18:54:00 +0000 | [diff] [blame] | 1585 | from _ast import PyCF_ONLY_AST |
Andrew M. Kuchling | 4e86195 | 2006-04-12 12:16:31 +0000 | [diff] [blame] | 1586 | ast = compile("""a=0 |
| 1587 | for i in range(10): |
| 1588 | a += i |
Andrew M. Kuchling | 5f445bf | 2006-04-12 18:54:00 +0000 | [diff] [blame] | 1589 | """, "<string>", 'exec', PyCF_ONLY_AST) |
Andrew M. Kuchling | 4e86195 | 2006-04-12 12:16:31 +0000 | [diff] [blame] | 1590 | |
| 1591 | assignment = ast.body[0] |
| 1592 | for_loop = ast.body[1] |
| 1593 | \end{verbatim} |
| 1594 | |
Andrew M. Kuchling | db85ed5 | 2005-10-23 21:52:59 +0000 | [diff] [blame] | 1595 | No documentation has been written for the AST code yet. To start |
| 1596 | learning about it, read the definition of the various AST nodes in |
| 1597 | \file{Parser/Python.asdl}. A Python script reads this file and |
| 1598 | generates a set of C structure definitions in |
| 1599 | \file{Include/Python-ast.h}. The \cfunction{PyParser_ASTFromString()} |
| 1600 | and \cfunction{PyParser_ASTFromFile()}, defined in |
| 1601 | \file{Include/pythonrun.h}, take Python source as input and return the |
| 1602 | root of an AST representing the contents. This AST can then be turned |
| 1603 | into a code object by \cfunction{PyAST_Compile()}. For more |
| 1604 | information, read the source code, and then ask questions on |
| 1605 | python-dev. |
| 1606 | |
| 1607 | % List of names taken from Jeremy's python-dev post at |
| 1608 | % http://mail.python.org/pipermail/python-dev/2005-October/057500.html |
| 1609 | The AST code was developed under Jeremy Hylton's management, and |
| 1610 | implemented by (in alphabetical order) Brett Cannon, Nick Coghlan, |
| 1611 | Grant Edwards, John Ehresman, Kurt Kaiser, Neal Norwitz, Tim Peters, |
| 1612 | Armin Rigo, and Neil Schemenauer, plus the participants in a number of |
| 1613 | AST sprints at conferences such as PyCon. |
| 1614 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1615 | \item The built-in set types now have an official C API. Call |
| 1616 | \cfunction{PySet_New()} and \cfunction{PyFrozenSet_New()} to create a |
| 1617 | new set, \cfunction{PySet_Add()} and \cfunction{PySet_Discard()} to |
| 1618 | add and remove elements, and \cfunction{PySet_Contains} and |
| 1619 | \cfunction{PySet_Size} to examine the set's state. |
Andrew M. Kuchling | 29b3d08 | 2006-04-14 20:35:17 +0000 | [diff] [blame] | 1620 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1621 | |
Andrew M. Kuchling | 61434b6 | 2006-04-13 11:51:07 +0000 | [diff] [blame] | 1622 | \item C code can now obtain information about the exact revision |
| 1623 | of the Python interpreter by calling the |
| 1624 | \cfunction{Py_GetBuildInfo()} function that returns a |
| 1625 | string of build information like this: |
| 1626 | \code{"trunk:45355:45356M, Apr 13 2006, 07:42:19"}. |
| 1627 | (Contributed by Barry Warsaw.) |
| 1628 | |
Andrew M. Kuchling | 29b3d08 | 2006-04-14 20:35:17 +0000 | [diff] [blame] | 1629 | \item The CPython interpreter is still written in C, but |
| 1630 | the code can now be compiled with a {\Cpp} compiler without errors. |
| 1631 | (Implemented by Anthony Baxter, Martin von~L\"owis, Skip Montanaro.) |
| 1632 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 1633 | \item The \cfunction{PyRange_New()} function was removed. It was |
| 1634 | never documented, never used in the core code, and had dangerously lax |
| 1635 | error checking. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1636 | |
| 1637 | \end{itemize} |
| 1638 | |
| 1639 | |
| 1640 | %====================================================================== |
Andrew M. Kuchling | 6fc6976 | 2006-04-13 12:37:21 +0000 | [diff] [blame] | 1641 | \subsection{Port-Specific Changes} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1642 | |
Andrew M. Kuchling | 6fc6976 | 2006-04-13 12:37:21 +0000 | [diff] [blame] | 1643 | \begin{itemize} |
| 1644 | |
| 1645 | \item MacOS X (10.3 and higher): dynamic loading of modules |
| 1646 | now uses the \cfunction{dlopen()} function instead of MacOS-specific |
| 1647 | functions. |
| 1648 | |
| 1649 | \end{itemize} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1650 | |
| 1651 | |
| 1652 | %====================================================================== |
| 1653 | \section{Other Changes and Fixes \label{section-other}} |
| 1654 | |
| 1655 | As usual, there were a bunch of other improvements and bugfixes |
Andrew M. Kuchling | f688cc5 | 2006-03-10 18:50:08 +0000 | [diff] [blame] | 1656 | scattered throughout the source tree. A search through the SVN change |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1657 | 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] | 1658 | 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] | 1659 | |
| 1660 | Some of the more notable changes are: |
| 1661 | |
| 1662 | \begin{itemize} |
| 1663 | |
Andrew M. Kuchling | 01e3d26 | 2006-03-17 15:38:39 +0000 | [diff] [blame] | 1664 | \item Evan Jones's patch to obmalloc, first described in a talk |
| 1665 | at PyCon DC 2005, was applied. Python 2.4 allocated small objects in |
| 1666 | 256K-sized arenas, but never freed arenas. With this patch, Python |
| 1667 | will free arenas when they're empty. The net effect is that on some |
| 1668 | platforms, when you allocate many objects, Python's memory usage may |
| 1669 | actually drop when you delete them, and the memory may be returned to |
| 1670 | the operating system. (Implemented by Evan Jones, and reworked by Tim |
| 1671 | Peters.) |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1672 | |
Andrew M. Kuchling | f7c6290 | 2006-04-12 12:27:50 +0000 | [diff] [blame] | 1673 | Note that this change means extension modules need to be more careful |
Andrew M. Kuchling | 0f1955d | 2006-04-13 12:09:08 +0000 | [diff] [blame] | 1674 | with how they allocate memory. Python's API has many different |
Andrew M. Kuchling | f7c6290 | 2006-04-12 12:27:50 +0000 | [diff] [blame] | 1675 | functions for allocating memory that are grouped into families. For |
| 1676 | example, \cfunction{PyMem_Malloc()}, \cfunction{PyMem_Realloc()}, and |
| 1677 | \cfunction{PyMem_Free()} are one family that allocates raw memory, |
| 1678 | while \cfunction{PyObject_Malloc()}, \cfunction{PyObject_Realloc()}, |
| 1679 | and \cfunction{PyObject_Free()} are another family that's supposed to |
| 1680 | be used for creating Python objects. |
| 1681 | |
| 1682 | Previously these different families all reduced to the platform's |
| 1683 | \cfunction{malloc()} and \cfunction{free()} functions. This meant |
| 1684 | it didn't matter if you got things wrong and allocated memory with the |
| 1685 | \cfunction{PyMem} function but freed it with the \cfunction{PyObject} |
| 1686 | function. With the obmalloc change, these families now do different |
| 1687 | things, and mismatches will probably result in a segfault. You should |
| 1688 | carefully test your C extension modules with Python 2.5. |
| 1689 | |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1690 | \item Coverity, a company that markets a source code analysis tool |
| 1691 | called Prevent, provided the results of their examination of the Python |
Andrew M. Kuchling | 0f1955d | 2006-04-13 12:09:08 +0000 | [diff] [blame] | 1692 | source code. The analysis found about 60 bugs that |
| 1693 | were quickly fixed. Many of the bugs were refcounting problems, often |
| 1694 | occurring in error-handling code. See |
| 1695 | \url{http://scan.coverity.com} for the statistics. |
Andrew M. Kuchling | 38f8507 | 2006-04-02 01:46:32 +0000 | [diff] [blame] | 1696 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1697 | \end{itemize} |
| 1698 | |
| 1699 | |
| 1700 | %====================================================================== |
| 1701 | \section{Porting to Python 2.5} |
| 1702 | |
| 1703 | This section lists previously described changes that may require |
| 1704 | changes to your code: |
| 1705 | |
| 1706 | \begin{itemize} |
| 1707 | |
Andrew M. Kuchling | 5f445bf | 2006-04-12 18:54:00 +0000 | [diff] [blame] | 1708 | \item ASCII is now the default encoding for modules. It's now |
| 1709 | a syntax error if a module contains string literals with 8-bit |
| 1710 | characters but doesn't have an encoding declaration. In Python 2.4 |
| 1711 | this triggered a warning, not a syntax error. |
| 1712 | |
Andrew M. Kuchling | c3749a9 | 2006-04-04 19:14:41 +0000 | [diff] [blame] | 1713 | \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] | 1714 | |
Andrew M. Kuchling | 3b4fb04 | 2006-04-13 12:49:39 +0000 | [diff] [blame] | 1715 | \item Previously, the \member{gi_frame} attribute of a generator |
| 1716 | was always a frame object. Because of the \pep{342} changes |
| 1717 | described in section~\ref{section-generators}, it's now possible |
| 1718 | for \member{gi_frame} to be \code{None}. |
| 1719 | |
Andrew M. Kuchling | f7c6290 | 2006-04-12 12:27:50 +0000 | [diff] [blame] | 1720 | \item C API: Many functions now use \ctype{Py_ssize_t} |
| 1721 | instead of \ctype{int} to allow processing more data |
| 1722 | on 64-bit machines. Extension code may need to make |
| 1723 | the same change to avoid warnings and to support 64-bit machines. |
| 1724 | See the earlier |
| 1725 | section~ref{section-353} for a discussion of this change. |
| 1726 | |
| 1727 | \item C API: |
| 1728 | The obmalloc changes mean that |
| 1729 | you must be careful to not mix usage |
| 1730 | of the \cfunction{PyMem_*()} and \cfunction{PyObject_*()} |
| 1731 | families of functions. Memory allocated with |
| 1732 | one family's \cfunction{*_Malloc()} must be |
| 1733 | freed with the corresponding family's \cfunction{*_Free()} function. |
| 1734 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1735 | \end{itemize} |
| 1736 | |
| 1737 | |
| 1738 | %====================================================================== |
| 1739 | \section{Acknowledgements \label{acks}} |
| 1740 | |
| 1741 | The author would like to thank the following people for offering |
| 1742 | suggestions, corrections and assistance with various drafts of this |
Andrew M. Kuchling | 5f445bf | 2006-04-12 18:54:00 +0000 | [diff] [blame] | 1743 | article: Martin von~L\"owis, Mike Rovner, Thomas Wouters. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1744 | |
| 1745 | \end{document} |