Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 1 | \documentclass{howto} |
| 2 | \usepackage{distutils} |
| 3 | % $Id$ |
| 4 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 5 | |
| 6 | \title{What's New in Python 2.5} |
| 7 | \release{0.0} |
Andrew M. Kuchling | 92e2495 | 2004-12-03 13:54:09 +0000 | [diff] [blame] | 8 | \author{A.M. Kuchling} |
| 9 | \authoraddress{\email{amk@amk.ca}} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 10 | |
| 11 | \begin{document} |
| 12 | \maketitle |
| 13 | \tableofcontents |
| 14 | |
| 15 | 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] | 16 | for Python 2.5 has been set; it will probably be released in the |
| 17 | autumn of 2006. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 18 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 19 | % XXX Compare with previous release in 2 - 3 sentences here. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 20 | |
| 21 | This article doesn't attempt to provide a complete specification of |
| 22 | the new features, but instead provides a convenient overview. For |
| 23 | 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] | 24 | % XXX add hyperlink when the documentation becomes available online. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 25 | If you want to understand the complete implementation and design |
| 26 | rationale, refer to the PEP for a particular new feature. |
| 27 | |
| 28 | |
| 29 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 30 | \section{PEP 308: Conditional Expressions} |
| 31 | |
| 32 | % XXX write this |
| 33 | |
| 34 | |
| 35 | %====================================================================== |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 36 | \section{PEP 309: Partial Function Application} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 37 | |
Andrew M. Kuchling | b1c96fd | 2005-03-20 21:42:04 +0000 | [diff] [blame] | 38 | The \module{functional} module is intended to contain tools for |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 39 | functional-style programming. Currently it only contains a |
| 40 | \class{partial()} function, but new functions will probably be added |
| 41 | in future versions of Python. |
Andrew M. Kuchling | b1c96fd | 2005-03-20 21:42:04 +0000 | [diff] [blame] | 42 | |
Andrew M. Kuchling | 4b000cd | 2005-04-09 15:51:44 +0000 | [diff] [blame] | 43 | For programs written in a functional style, it can be useful to |
| 44 | construct variants of existing functions that have some of the |
| 45 | parameters filled in. Consider a Python function \code{f(a, b, c)}; |
| 46 | you could create a new function \code{g(b, c)} that was equivalent to |
| 47 | \code{f(1, b, c)}. This is called ``partial function application'', |
| 48 | and is provided by the \class{partial} class in the new |
| 49 | \module{functional} module. |
| 50 | |
| 51 | The constructor for \class{partial} takes the arguments |
| 52 | \code{(\var{function}, \var{arg1}, \var{arg2}, ... |
| 53 | \var{kwarg1}=\var{value1}, \var{kwarg2}=\var{value2})}. The resulting |
| 54 | object is callable, so you can just call it to invoke \var{function} |
| 55 | with the filled-in arguments. |
| 56 | |
| 57 | Here's a small but realistic example: |
| 58 | |
| 59 | \begin{verbatim} |
| 60 | import functional |
| 61 | |
| 62 | def log (message, subsystem): |
| 63 | "Write the contents of 'message' to the specified subsystem." |
| 64 | print '%s: %s' % (subsystem, message) |
| 65 | ... |
| 66 | |
| 67 | server_log = functional.partial(log, subsystem='server') |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 68 | server_log('Unable to open socket') |
Andrew M. Kuchling | 4b000cd | 2005-04-09 15:51:44 +0000 | [diff] [blame] | 69 | \end{verbatim} |
| 70 | |
Andrew M. Kuchling | 6af7fe0 | 2005-08-02 17:20:36 +0000 | [diff] [blame] | 71 | Here's another example, from a program that uses PyGTk. Here a |
| 72 | context-sensitive pop-up menu is being constructed dynamically. The |
| 73 | callback provided for the menu option is a partially applied version |
| 74 | of the \method{open_item()} method, where the first argument has been |
| 75 | provided. |
Andrew M. Kuchling | 4b000cd | 2005-04-09 15:51:44 +0000 | [diff] [blame] | 76 | |
Andrew M. Kuchling | 6af7fe0 | 2005-08-02 17:20:36 +0000 | [diff] [blame] | 77 | \begin{verbatim} |
| 78 | ... |
| 79 | class Application: |
| 80 | def open_item(self, path): |
| 81 | ... |
| 82 | def init (self): |
| 83 | open_func = functional.partial(self.open_item, item_path) |
| 84 | popup_menu.append( ("Open", open_func, 1) ) |
| 85 | \end{verbatim} |
Andrew M. Kuchling | b1c96fd | 2005-03-20 21:42:04 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | \begin{seealso} |
| 89 | |
| 90 | \seepep{309}{Partial Function Application}{PEP proposed and written by |
| 91 | Peter Harris; implemented by Hye-Shik Chang, with adaptations by |
| 92 | Raymond Hettinger.} |
| 93 | |
| 94 | \end{seealso} |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 95 | |
| 96 | |
| 97 | %====================================================================== |
Fred Drake | db7b002 | 2005-03-20 22:19:47 +0000 | [diff] [blame] | 98 | \section{PEP 314: Metadata for Python Software Packages v1.1} |
| 99 | |
Andrew M. Kuchling | d8d732e | 2005-04-09 23:59:41 +0000 | [diff] [blame] | 100 | Some simple dependency support was added to Distutils. The |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 101 | \function{setup()} function now has \code{requires}, \code{provides}, |
| 102 | and \code{obsoletes} keyword parameters. When you build a source |
| 103 | distribution using the \code{sdist} command, the dependency |
| 104 | information will be recorded in the \file{PKG-INFO} file. |
Andrew M. Kuchling | d8d732e | 2005-04-09 23:59:41 +0000 | [diff] [blame] | 105 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 106 | Another new keyword parameter is \code{download_url}, which should be |
| 107 | set to a URL for the package's source code. This means it's now |
| 108 | possible to look up an entry in the package index, determine the |
| 109 | dependencies for a package, and download the required packages. |
Andrew M. Kuchling | d8d732e | 2005-04-09 23:59:41 +0000 | [diff] [blame] | 110 | |
| 111 | % XXX put example here |
| 112 | |
| 113 | \begin{seealso} |
| 114 | |
| 115 | \seepep{314}{Metadata for Python Software Packages v1.1}{PEP proposed |
| 116 | and written by A.M. Kuchling, Richard Jones, and Fred Drake; |
| 117 | implemented by Richard Jones and Fred Drake.} |
| 118 | |
| 119 | \end{seealso} |
Fred Drake | db7b002 | 2005-03-20 22:19:47 +0000 | [diff] [blame] | 120 | |
| 121 | |
| 122 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 123 | \section{PEP 328: Absolute and Relative Imports} |
| 124 | |
| 125 | % XXX write this |
| 126 | |
| 127 | |
| 128 | %====================================================================== |
| 129 | \section{PEP 341: Unified try/except/finally} |
| 130 | |
| 131 | % XXX write this |
| 132 | |
| 133 | |
| 134 | %====================================================================== |
Andrew M. Kuchling | a2e21cb | 2005-08-02 17:13:21 +0000 | [diff] [blame] | 135 | \section{PEP 342: New Generator Features} |
| 136 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 137 | 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] | 138 | generator's code is invoked to create an iterator, there's no way to |
| 139 | pass any new information into the function when its execution is |
| 140 | resumed. Hackish solutions to this include making the generator's |
| 141 | code look at a global variable and then changing the global variable's |
| 142 | value, or passing in some mutable object that callers then modify. |
| 143 | Python 2.5 adds the ability to pass values \emph{into} a generator. |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 144 | |
| 145 | To refresh your memory of basic generators, here's a simple example: |
| 146 | |
| 147 | \begin{verbatim} |
| 148 | def counter (maximum): |
| 149 | i = 0 |
| 150 | while i < maximum: |
| 151 | yield i |
| 152 | i += 1 |
| 153 | \end{verbatim} |
| 154 | |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 155 | When you call \code{counter(10)}, the result is an iterator that |
| 156 | returns the values from 0 up to 9. On encountering the |
| 157 | \keyword{yield} statement, the iterator returns the provided value and |
| 158 | suspends the function's execution, preserving the local variables. |
| 159 | Execution resumes on the following call to the iterator's |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 160 | \method{next()} method, picking up after the \keyword{yield} statement. |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 161 | |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 162 | In Python 2.3, \keyword{yield} was a statement; it didn't return any |
| 163 | value. In 2.5, \keyword{yield} is now an expression, returning a |
| 164 | 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] | 165 | |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 166 | \begin{verbatim} |
| 167 | val = (yield i) |
| 168 | \end{verbatim} |
| 169 | |
| 170 | I recommend that you always put parentheses around a \keyword{yield} |
| 171 | expression when you're doing something with the returned value, as in |
| 172 | the above example. The parentheses aren't always necessary, but it's |
| 173 | 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] | 174 | 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] | 175 | always be parenthesized except when it occurs at the top-level |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 176 | expression on the right-hand side of an assignment, meaning you can |
| 177 | write \code{val = yield i} but have to use parentheses when there's an |
| 178 | operation, as in \code{val = (yield i) + 12}.} |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 179 | |
| 180 | Values are sent into a generator by calling its |
| 181 | \method{send(\var{value})} method. The generator's code is then |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 182 | resumed and the \keyword{yield} expression returns the specified |
| 183 | \var{value}. If the regular \method{next()} method is called, the |
| 184 | \keyword{yield} returns \constant{None}. |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 185 | |
| 186 | Here's the previous example, modified to allow changing the value of |
| 187 | the internal counter. |
| 188 | |
| 189 | \begin{verbatim} |
| 190 | def counter (maximum): |
| 191 | i = 0 |
| 192 | while i < maximum: |
| 193 | val = (yield i) |
| 194 | # If value provided, change counter |
| 195 | if val is not None: |
| 196 | i = val |
| 197 | else: |
| 198 | i += 1 |
| 199 | \end{verbatim} |
| 200 | |
| 201 | And here's an example of changing the counter: |
| 202 | |
| 203 | \begin{verbatim} |
| 204 | >>> it = counter(10) |
| 205 | >>> print it.next() |
| 206 | 0 |
| 207 | >>> print it.next() |
| 208 | 1 |
| 209 | >>> print it.send(8) |
| 210 | 8 |
| 211 | >>> print it.next() |
| 212 | 9 |
| 213 | >>> print it.next() |
| 214 | Traceback (most recent call last): |
| 215 | File ``t.py'', line 15, in ? |
| 216 | print it.next() |
| 217 | StopIteration |
Andrew M. Kuchling | c203370 | 2005-08-29 13:30:12 +0000 | [diff] [blame] | 218 | \end{verbatim} |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 219 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 220 | Because \keyword{yield} will often be returning \constant{None}, you |
| 221 | should always check for this case. Don't just use its value in |
| 222 | expressions unless you're sure that the \method{send()} method |
| 223 | will be the only method used resume your generator function. |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 224 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 225 | In addition to \method{send()}, there are two other new methods on |
| 226 | generators: |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 227 | |
| 228 | \begin{itemize} |
| 229 | |
| 230 | \item \method{throw(\var{type}, \var{value}=None, |
| 231 | \var{traceback}=None)} is used to raise an exception inside the |
| 232 | generator; the exception is raised by the \keyword{yield} expression |
| 233 | where the generator's execution is paused. |
| 234 | |
| 235 | \item \method{close()} raises a new \exception{GeneratorExit} |
| 236 | exception inside the generator to terminate the iteration. |
| 237 | On receiving this |
| 238 | exception, the generator's code must either raise |
| 239 | \exception{GeneratorExit} or \exception{StopIteration}; catching the |
| 240 | exception and doing anything else is illegal and will trigger |
| 241 | a \exception{RuntimeError}. \method{close()} will also be called by |
| 242 | Python's garbage collection when the generator is garbage-collected. |
| 243 | |
| 244 | If you need to run cleanup code in case of a \exception{GeneratorExit}, |
| 245 | I suggest using a \code{try: ... finally:} suite instead of |
| 246 | catching \exception{GeneratorExit}. |
| 247 | |
| 248 | \end{itemize} |
| 249 | |
| 250 | The cumulative effect of these changes is to turn generators from |
| 251 | one-way producers of information into both producers and consumers. |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 252 | |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 253 | Generators also become \emph{coroutines}, a more generalized form of |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 254 | subroutines. Subroutines are entered at one point and exited at |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 255 | another point (the top of the function, and a \keyword{return |
| 256 | statement}), but coroutines can be entered, exited, and resumed at |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 257 | many different points (the \keyword{yield} statements). |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 258 | |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 259 | |
Andrew M. Kuchling | a2e21cb | 2005-08-02 17:13:21 +0000 | [diff] [blame] | 260 | \begin{seealso} |
| 261 | |
| 262 | \seepep{342}{Coroutines via Enhanced Generators}{PEP written by |
| 263 | Guido van Rossum and Phillip J. Eby; |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 264 | implemented by Phillip J. Eby. Includes examples of |
| 265 | some fancier uses of generators as coroutines.} |
| 266 | |
| 267 | \seeurl{http://en.wikipedia.org/wiki/Coroutine}{The Wikipedia entry for |
| 268 | coroutines.} |
| 269 | |
Neal Norwitz | 0917988 | 2006-03-04 23:31:45 +0000 | [diff] [blame] | 270 | \seeurl{http://www.sidhe.org/\~{}dan/blog/archives/000178.html}{An |
Andrew M. Kuchling | 0738206 | 2005-08-27 18:45:47 +0000 | [diff] [blame] | 271 | explanation of coroutines from a Perl point of view, written by Dan |
| 272 | Sugalski.} |
Andrew M. Kuchling | a2e21cb | 2005-08-02 17:13:21 +0000 | [diff] [blame] | 273 | |
| 274 | \end{seealso} |
| 275 | |
| 276 | |
| 277 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 278 | \section{PEP 343: The 'with' statement} |
| 279 | |
| 280 | % XXX write this |
| 281 | |
| 282 | |
| 283 | %====================================================================== |
Andrew M. Kuchling | 8f4d255 | 2006-03-08 01:50:20 +0000 | [diff] [blame] | 284 | \section{PEP 352: Exceptions as New-Style Classes} |
| 285 | |
| 286 | % XXX write this |
| 287 | |
| 288 | |
| 289 | %====================================================================== |
Andrew M. Kuchling | 437567c | 2006-03-07 20:48:55 +0000 | [diff] [blame] | 290 | \section{PEP 357: The '__index__' method} |
| 291 | |
| 292 | % XXX write this |
| 293 | |
| 294 | |
| 295 | %====================================================================== |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 296 | \section{Other Language Changes} |
| 297 | |
| 298 | Here are all of the changes that Python 2.5 makes to the core Python |
| 299 | language. |
| 300 | |
| 301 | \begin{itemize} |
Andrew M. Kuchling | 1cae3f5 | 2004-12-03 14:57:21 +0000 | [diff] [blame] | 302 | |
| 303 | \item The \function{min()} and \function{max()} built-in functions |
| 304 | gained a \code{key} keyword argument analogous to the \code{key} |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 305 | argument for \method{sort()}. This argument supplies a function |
Andrew M. Kuchling | 1cae3f5 | 2004-12-03 14:57:21 +0000 | [diff] [blame] | 306 | that takes a single argument and is called for every value in the list; |
| 307 | \function{min()}/\function{max()} will return the element with the |
| 308 | smallest/largest return value from this function. |
| 309 | For example, to find the longest string in a list, you can do: |
| 310 | |
| 311 | \begin{verbatim} |
| 312 | L = ['medium', 'longest', 'short'] |
| 313 | # Prints 'longest' |
| 314 | print max(L, key=len) |
| 315 | # Prints 'short', because lexicographically 'short' has the largest value |
| 316 | print max(L) |
| 317 | \end{verbatim} |
| 318 | |
| 319 | (Contributed by Steven Bethard and Raymond Hettinger.) |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 320 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 321 | \item Two new built-in functions, \function{any()} and |
| 322 | \function{all()}, evaluate whether an iterator contains any true or |
| 323 | false values. \function{any()} returns \constant{True} if any value |
| 324 | returned by the iterator is true; otherwise it will return |
| 325 | \constant{False}. \function{all()} returns \constant{True} only if |
| 326 | all of the values returned by the iterator evaluate as being true. |
| 327 | |
| 328 | % XXX who added? |
| 329 | |
| 330 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 331 | \item The list of base classes in a class definition can now be empty. |
| 332 | As an example, this is now legal: |
| 333 | |
| 334 | \begin{verbatim} |
| 335 | class C(): |
| 336 | pass |
| 337 | \end{verbatim} |
| 338 | (Implemented by Brett Cannon.) |
| 339 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 340 | \end{itemize} |
| 341 | |
| 342 | |
| 343 | %====================================================================== |
| 344 | \subsection{Optimizations} |
| 345 | |
| 346 | \begin{itemize} |
| 347 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 348 | \item When they were introduced |
| 349 | in Python 2.4, the built-in \class{set} and \class{frozenset} types |
| 350 | were built on top of Python's dictionary type. |
| 351 | In 2.5 the internal data structure has been customized for implementing sets, |
| 352 | and as a result sets will use a third less memory and are somewhat faster. |
| 353 | (Implemented by Raymond Hettinger.) |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 354 | |
| 355 | \end{itemize} |
| 356 | |
| 357 | The net result of the 2.5 optimizations is that Python 2.5 runs the |
Andrew M. Kuchling | 92e2495 | 2004-12-03 13:54:09 +0000 | [diff] [blame] | 358 | pystone benchmark around XX\% faster than Python 2.4. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 359 | |
| 360 | |
| 361 | %====================================================================== |
| 362 | \section{New, Improved, and Deprecated Modules} |
| 363 | |
| 364 | As usual, Python's standard library received a number of enhancements and |
| 365 | bug fixes. Here's a partial list of the most notable changes, sorted |
| 366 | alphabetically by module name. Consult the |
| 367 | \file{Misc/NEWS} file in the source tree for a more |
| 368 | complete list of changes, or look through the CVS logs for all the |
| 369 | details. |
| 370 | |
| 371 | \begin{itemize} |
| 372 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 373 | % collections.deque now has .remove() |
| 374 | |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 375 | % the cPickle module no longer accepts the deprecated None option in the |
| 376 | % args tuple returned by __reduce__(). |
| 377 | |
| 378 | % csv module improvements |
| 379 | |
| 380 | % datetime.datetime() now has a strptime class method which can be used to |
| 381 | % create datetime object using a string and format. |
| 382 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 383 | \item A new \module{hashlib} module has been added to replace the |
| 384 | \module{md5} and \module{sha} modules. \module{hashlib} adds support |
| 385 | for additional secure hashes (SHA-224, SHA-256, SHA-384, and SHA-512). |
| 386 | When available, the module uses OpenSSL for fast platform optimized |
| 387 | implementations of algorithms. The old \module{md5} and \module{sha} |
| 388 | modules still exist as wrappers around hashlib to preserve backwards |
| 389 | compatibility. (Contributed by Gregory P. Smith.) |
| 390 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 391 | \item The \function{nsmallest()} and |
| 392 | \function{nlargest()} functions in the \module{heapq} module |
| 393 | now support a \code{key} keyword argument similar to the one |
| 394 | provided by the \function{min()}/\function{max()} functions |
| 395 | and the \method{sort()} methods. For example: |
| 396 | Example: |
| 397 | |
| 398 | \begin{verbatim} |
| 399 | >>> import heapq |
| 400 | >>> L = ["short", 'medium', 'longest', 'longer still'] |
| 401 | >>> heapq.nsmallest(2, L) # Return two lowest elements, lexicographically |
| 402 | ['longer still', 'longest'] |
| 403 | >>> heapq.nsmallest(2, L, key=len) # Return two shortest elements |
| 404 | ['short', 'medium'] |
| 405 | \end{verbatim} |
| 406 | |
| 407 | (Contributed by Raymond Hettinger.) |
| 408 | |
Andrew M. Kuchling | 511a3a8 | 2005-03-20 19:52:18 +0000 | [diff] [blame] | 409 | \item The \function{itertools.islice()} function now accepts |
| 410 | \code{None} for the start and step arguments. This makes it more |
| 411 | compatible with the attributes of slice objects, so that you can now write |
| 412 | the following: |
| 413 | |
| 414 | \begin{verbatim} |
| 415 | s = slice(5) # Create slice object |
| 416 | itertools.islice(iterable, s.start, s.stop, s.step) |
| 417 | \end{verbatim} |
| 418 | |
| 419 | (Contributed by Raymond Hettinger.) |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 420 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 421 | \item The \module{operator} module's \function{itemgetter()} |
| 422 | and \function{attrgetter()} functions now support multiple fields. |
| 423 | A call such as \code{operator.attrgetter('a', 'b')} |
| 424 | will return a function |
| 425 | that retrieves the \member{a} and \member{b} attributes. Combining |
| 426 | this new feature with the \method{sort()} method's \code{key} parameter |
| 427 | lets you easily sort lists using multiple fields. |
| 428 | |
| 429 | % XXX who added? |
| 430 | |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 431 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 432 | \item The \module{os} module underwent a number of changes. The |
| 433 | \member{stat_float_times} variable now defaults to true, meaning that |
| 434 | \function{os.stat()} will now return time values as floats. (This |
| 435 | doesn't necessarily mean that \function{os.stat()} will return times |
| 436 | that are precise to fractions of a second; not all systems support |
| 437 | such precision.) |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 438 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 439 | Constants named \member{os.SEEK_SET}, \member{os.SEEK_CUR}, and |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 440 | \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] | 441 | \function{os.lseek()} function. Two new constants for locking are |
| 442 | \member{os.O_SHLOCK} and \member{os.O_EXLOCK}. |
| 443 | |
| 444 | On FreeBSD, the \function{os.stat()} function now returns |
| 445 | times with nanosecond resolution, and the returned object |
| 446 | now has \member{st_gen} and \member{st_birthtime}. |
| 447 | The \member{st_flags} member is also available, if the platform supports it. |
| 448 | % XXX patch 1180695, 1212117 |
| 449 | |
Andrew M. Kuchling | 4678dc8 | 2006-01-15 16:11:28 +0000 | [diff] [blame] | 450 | \item The \module{socket} module now supports \constant{AF_NETLINK} |
| 451 | sockets on Linux, thanks to a patch from Philippe Biondi. |
| 452 | Netlink sockets are a Linux-specific mechanism for communications |
| 453 | between a user-space process and kernel code; an introductory |
| 454 | article about them is at \url{http://www.linuxjournal.com/article/7356}. |
| 455 | In Python code, netlink addresses are represented as a tuple of 2 integers, |
| 456 | \code{(\var{pid}, \var{group_mask})}. |
| 457 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 458 | \item New module: \module{spwd} provides functions for accessing the |
| 459 | shadow password database on systems that support it. |
| 460 | % XXX give example |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 461 | |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 462 | \item The \class{TarFile} class in the \module{tarfile} module now has |
Georg Brandl | 08c02db | 2005-07-22 18:39:19 +0000 | [diff] [blame] | 463 | an \method{extractall()} method that extracts all members from the |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 464 | archive into the current working directory. It's also possible to set |
| 465 | 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] | 466 | subset of the archive's members. |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 467 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 468 | A tarfile's compression can be autodetected by |
| 469 | using the mode \code{'r|*'}. |
| 470 | % patch 918101 |
| 471 | (Contributed by Lars Gust\"abel.) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 472 | |
Fredrik Lundh | 7e0aef0 | 2005-12-12 18:54:55 +0000 | [diff] [blame] | 473 | \item A new package \module{xml.etree} has been added, which contains |
| 474 | a subset of the ElementTree XML library. Available modules are |
| 475 | \module{ElementTree}, \module{ElementPath}, and |
| 476 | \module{ElementInclude}, from ElementTree 1.2.6. (Contributed by |
| 477 | Fredrik Lundh.) |
| 478 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 479 | \item The \module{xmlrpclib} module now supports returning |
| 480 | \class{datetime} objects for the XML-RPC date type. Supply |
| 481 | \code{use_datetime=True} to the \function{loads()} function |
| 482 | or the \class{Unmarshaller} class to enable this feature. |
| 483 | % XXX patch 1120353 |
| 484 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 485 | |
Fred Drake | 114b8ca | 2005-03-21 05:47:11 +0000 | [diff] [blame] | 486 | \end{itemize} |
Andrew M. Kuchling | e9b1bf4 | 2005-03-20 19:26:30 +0000 | [diff] [blame] | 487 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 488 | |
| 489 | |
| 490 | %====================================================================== |
| 491 | % whole new modules get described in \subsections here |
| 492 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 493 | % XXX new distutils features: upload |
| 494 | |
Fredrik Lundh | 7e0aef0 | 2005-12-12 18:54:55 +0000 | [diff] [blame] | 495 | % XXX should hashlib perhaps be described here instead? |
| 496 | % XXX should xml.etree perhaps be described here instead? |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 497 | |
| 498 | |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 499 | |
| 500 | % ====================================================================== |
| 501 | \section{Build and C API Changes} |
| 502 | |
| 503 | Changes to Python's build process and to the C API include: |
| 504 | |
| 505 | \begin{itemize} |
| 506 | |
Andrew M. Kuchling | db85ed5 | 2005-10-23 21:52:59 +0000 | [diff] [blame] | 507 | \item The design of the bytecode compiler has changed a great deal, no |
| 508 | longer generating bytecode by traversing the parse tree. Instead |
| 509 | the parse tree is converted to an abstract syntax tree (or AST), and it is |
| 510 | the abstract syntax tree that's traversed to produce the bytecode. |
| 511 | |
| 512 | No documentation has been written for the AST code yet. To start |
| 513 | learning about it, read the definition of the various AST nodes in |
| 514 | \file{Parser/Python.asdl}. A Python script reads this file and |
| 515 | generates a set of C structure definitions in |
| 516 | \file{Include/Python-ast.h}. The \cfunction{PyParser_ASTFromString()} |
| 517 | and \cfunction{PyParser_ASTFromFile()}, defined in |
| 518 | \file{Include/pythonrun.h}, take Python source as input and return the |
| 519 | root of an AST representing the contents. This AST can then be turned |
| 520 | into a code object by \cfunction{PyAST_Compile()}. For more |
| 521 | information, read the source code, and then ask questions on |
| 522 | python-dev. |
| 523 | |
| 524 | % List of names taken from Jeremy's python-dev post at |
| 525 | % http://mail.python.org/pipermail/python-dev/2005-October/057500.html |
| 526 | The AST code was developed under Jeremy Hylton's management, and |
| 527 | implemented by (in alphabetical order) Brett Cannon, Nick Coghlan, |
| 528 | Grant Edwards, John Ehresman, Kurt Kaiser, Neal Norwitz, Tim Peters, |
| 529 | Armin Rigo, and Neil Schemenauer, plus the participants in a number of |
| 530 | AST sprints at conferences such as PyCon. |
| 531 | |
Andrew M. Kuchling | 150e349 | 2005-08-23 00:56:06 +0000 | [diff] [blame] | 532 | \item The built-in set types now have an official C API. Call |
| 533 | \cfunction{PySet_New()} and \cfunction{PyFrozenSet_New()} to create a |
| 534 | new set, \cfunction{PySet_Add()} and \cfunction{PySet_Discard()} to |
| 535 | add and remove elements, and \cfunction{PySet_Contains} and |
| 536 | \cfunction{PySet_Size} to examine the set's state. |
| 537 | |
| 538 | \item The \cfunction{PyRange_New()} function was removed. It was |
| 539 | never documented, never used in the core code, and had dangerously lax |
| 540 | error checking. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 541 | |
| 542 | \end{itemize} |
| 543 | |
| 544 | |
| 545 | %====================================================================== |
| 546 | \subsection{Port-Specific Changes} |
| 547 | |
| 548 | Platform-specific changes go here. |
| 549 | |
| 550 | |
| 551 | %====================================================================== |
| 552 | \section{Other Changes and Fixes \label{section-other}} |
| 553 | |
| 554 | As usual, there were a bunch of other improvements and bugfixes |
| 555 | scattered throughout the source tree. A search through the CVS change |
| 556 | 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] | 557 | 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] | 558 | |
| 559 | Some of the more notable changes are: |
| 560 | |
| 561 | \begin{itemize} |
| 562 | |
| 563 | \item Details go here. |
| 564 | |
| 565 | \end{itemize} |
| 566 | |
| 567 | |
| 568 | %====================================================================== |
| 569 | \section{Porting to Python 2.5} |
| 570 | |
| 571 | This section lists previously described changes that may require |
| 572 | changes to your code: |
| 573 | |
| 574 | \begin{itemize} |
| 575 | |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 576 | \item Some old deprecated modules (\module{statcache}, \module{tzparse}, |
| 577 | \module{whrandom}) have been moved to \file{Lib/lib-old}. |
Andrew M. Kuchling | 0c35db9 | 2005-03-20 20:06:49 +0000 | [diff] [blame] | 578 | You can get access to these modules again by adding the directory |
| 579 | to your \code{sys.path}: |
| 580 | |
| 581 | \begin{verbatim} |
| 582 | import os |
| 583 | from distutils import sysconfig |
| 584 | |
| 585 | lib_dir = sysconfig.get_python_lib(standard_lib=True) |
| 586 | old_dir = os.path.join(lib_dir, 'lib-old') |
| 587 | sys.path.append(old_dir) |
| 588 | \end{verbatim} |
| 589 | |
| 590 | Doing so is discouraged, however; it's better to update any code that |
| 591 | still uses these modules. |
Andrew M. Kuchling | 3e41b05 | 2005-03-01 00:53:46 +0000 | [diff] [blame] | 592 | |
| 593 | % the pickle module no longer uses the deprecated bin parameter. |
Fred Drake | 2db7680 | 2004-12-01 05:05:47 +0000 | [diff] [blame] | 594 | |
| 595 | \end{itemize} |
| 596 | |
| 597 | |
| 598 | %====================================================================== |
| 599 | \section{Acknowledgements \label{acks}} |
| 600 | |
| 601 | The author would like to thank the following people for offering |
| 602 | suggestions, corrections and assistance with various drafts of this |
| 603 | article: . |
| 604 | |
| 605 | \end{document} |