Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 1 | \documentclass{howto} |
| 2 | |
Andrew M. Kuchling | ef85cc8 | 2001-03-23 03:29:08 +0000 | [diff] [blame] | 3 | \usepackage{distutils} |
| 4 | |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 5 | % $Id$ |
| 6 | |
| 7 | \title{What's New in Python 2.1} |
Andrew M. Kuchling | 5120eac | 2001-07-20 03:22:00 +0000 | [diff] [blame] | 8 | \release{1.00} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 9 | \author{A.M. Kuchling} |
Fred Drake | b914ef0 | 2004-01-02 06:57:50 +0000 | [diff] [blame] | 10 | \authoraddress{ |
| 11 | \strong{Python Software Foundation}\\ |
| 12 | Email: \email{amk@amk.ca} |
| 13 | } |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 14 | \begin{document} |
| 15 | \maketitle\tableofcontents |
| 16 | |
| 17 | \section{Introduction} |
| 18 | |
Andrew M. Kuchling | db7657d | 2001-04-12 03:37:19 +0000 | [diff] [blame] | 19 | It's that time again... time for a new Python release, Python 2.1. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 20 | One recent goal of the Python development team has been to accelerate |
| 21 | the pace of new releases, with a new release coming every 6 to 9 |
| 22 | months. 2.1 is the first release to come out at this faster pace, with |
| 23 | the first alpha appearing in January, 3 months after the final version |
| 24 | of 2.0 was released. |
| 25 | |
| 26 | This article explains the new features in 2.1. While there aren't as |
| 27 | many changes in 2.1 as there were in Python 2.0, there are still some |
| 28 | pleasant surprises in store. 2.1 is the first release to be steered |
| 29 | through the use of Python Enhancement Proposals, or PEPs, so most of |
| 30 | the sizable changes have accompanying PEPs that provide more complete |
| 31 | documentation and a design rationale for the change. This article |
| 32 | doesn't attempt to document the new features completely, but simply |
| 33 | provides an overview of the new features for Python programmers. |
| 34 | Refer to the Python 2.1 documentation, or to the specific PEP, for |
| 35 | more details about any new feature that particularly interests you. |
| 36 | |
Andrew M. Kuchling | b39fa8a | 2001-07-19 00:29:48 +0000 | [diff] [blame] | 37 | The final release of Python 2.1 was made on April 17, 2001. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 38 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 39 | %====================================================================== |
| 40 | \section{PEP 227: Nested Scopes} |
| 41 | |
| 42 | The largest change in Python 2.1 is to Python's scoping rules. In |
| 43 | Python 2.0, at any given time there are at most three namespaces used |
| 44 | to look up variable names: local, module-level, and the built-in |
| 45 | namespace. This often surprised people because it didn't match their |
| 46 | intuitive expectations. For example, a nested recursive function |
| 47 | definition doesn't work: |
| 48 | |
| 49 | \begin{verbatim} |
| 50 | def f(): |
| 51 | ... |
| 52 | def g(value): |
| 53 | ... |
| 54 | return g(value-1) + 1 |
| 55 | ... |
| 56 | \end{verbatim} |
| 57 | |
| 58 | The function \function{g()} will always raise a \exception{NameError} |
| 59 | exception, because the binding of the name \samp{g} isn't in either |
| 60 | its local namespace or in the module-level namespace. This isn't much |
| 61 | of a problem in practice (how often do you recursively define interior |
| 62 | functions like this?), but this also made using the \keyword{lambda} |
| 63 | statement clumsier, and this was a problem in practice. In code which |
| 64 | uses \keyword{lambda} you can often find local variables being copied |
| 65 | by passing them as the default values of arguments. |
| 66 | |
| 67 | \begin{verbatim} |
| 68 | def find(self, name): |
| 69 | "Return list of any entries equal to 'name'" |
| 70 | L = filter(lambda x, name=name: x == name, |
| 71 | self.list_attribute) |
| 72 | return L |
| 73 | \end{verbatim} |
| 74 | |
| 75 | The readability of Python code written in a strongly functional style |
| 76 | suffers greatly as a result. |
| 77 | |
| 78 | The most significant change to Python 2.1 is that static scoping has |
| 79 | been added to the language to fix this problem. As a first effect, |
| 80 | the \code{name=name} default argument is now unnecessary in the above |
| 81 | example. Put simply, when a given variable name is not assigned a |
| 82 | value within a function (by an assignment, or the \keyword{def}, |
| 83 | \keyword{class}, or \keyword{import} statements), references to the |
| 84 | variable will be looked up in the local namespace of the enclosing |
| 85 | scope. A more detailed explanation of the rules, and a dissection of |
| 86 | the implementation, can be found in the PEP. |
| 87 | |
| 88 | This change may cause some compatibility problems for code where the |
| 89 | same variable name is used both at the module level and as a local |
| 90 | variable within a function that contains further function definitions. |
| 91 | This seems rather unlikely though, since such code would have been |
Andrew M. Kuchling | 61af560 | 2001-03-03 03:25:04 +0000 | [diff] [blame] | 92 | pretty confusing to read in the first place. |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 93 | |
Andrew M. Kuchling | 15ad28c | 2001-02-14 02:44:18 +0000 | [diff] [blame] | 94 | One side effect of the change is that the \code{from \var{module} |
| 95 | import *} and \keyword{exec} statements have been made illegal inside |
| 96 | a function scope under certain conditions. The Python reference |
| 97 | manual has said all along that \code{from \var{module} import *} is |
| 98 | only legal at the top level of a module, but the CPython interpreter |
| 99 | has never enforced this before. As part of the implementation of |
| 100 | nested scopes, the compiler which turns Python source into bytecodes |
| 101 | has to generate different code to access variables in a containing |
| 102 | scope. \code{from \var{module} import *} and \keyword{exec} make it |
| 103 | impossible for the compiler to figure this out, because they add names |
| 104 | to the local namespace that are unknowable at compile time. |
| 105 | Therefore, if a function contains function definitions or |
| 106 | \keyword{lambda} expressions with free variables, the compiler will |
| 107 | flag this by raising a \exception{SyntaxError} exception. |
| 108 | |
| 109 | To make the preceding explanation a bit clearer, here's an example: |
| 110 | |
| 111 | \begin{verbatim} |
| 112 | x = 1 |
| 113 | def f(): |
| 114 | # The next line is a syntax error |
| 115 | exec 'x=2' |
| 116 | def g(): |
| 117 | return x |
| 118 | \end{verbatim} |
| 119 | |
| 120 | Line 4 containing the \keyword{exec} statement is a syntax error, |
| 121 | since \keyword{exec} would define a new local variable named \samp{x} |
| 122 | whose value should be accessed by \function{g()}. |
| 123 | |
| 124 | This shouldn't be much of a limitation, since \keyword{exec} is rarely |
| 125 | used in most Python code (and when it is used, it's often a sign of a |
| 126 | poor design anyway). |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 127 | |
Andrew M. Kuchling | 61af560 | 2001-03-03 03:25:04 +0000 | [diff] [blame] | 128 | Compatibility concerns have led to nested scopes being introduced |
| 129 | gradually; in Python 2.1, they aren't enabled by default, but can be |
| 130 | turned on within a module by using a future statement as described in |
| 131 | PEP 236. (See the following section for further discussion of PEP |
| 132 | 236.) In Python 2.2, nested scopes will become the default and there |
| 133 | will be no way to turn them off, but users will have had all of 2.1's |
| 134 | lifetime to fix any breakage resulting from their introduction. |
| 135 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 136 | \begin{seealso} |
| 137 | |
| 138 | \seepep{227}{Statically Nested Scopes}{Written and implemented by |
| 139 | Jeremy Hylton.} |
| 140 | |
| 141 | \end{seealso} |
| 142 | |
| 143 | |
| 144 | %====================================================================== |
Andrew M. Kuchling | 8d17709 | 2003-05-13 14:26:54 +0000 | [diff] [blame] | 145 | \section{PEP 236: __future__ Directives} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 146 | |
Andrew M. Kuchling | 61af560 | 2001-03-03 03:25:04 +0000 | [diff] [blame] | 147 | The reaction to nested scopes was widespread concern about the dangers |
| 148 | of breaking code with the 2.1 release, and it was strong enough to |
| 149 | make the Pythoneers take a more conservative approach. This approach |
| 150 | consists of introducing a convention for enabling optional |
| 151 | functionality in release N that will become compulsory in release N+1. |
| 152 | |
| 153 | The syntax uses a \code{from...import} statement using the reserved |
| 154 | module name \module{__future__}. Nested scopes can be enabled by the |
| 155 | following statement: |
| 156 | |
| 157 | \begin{verbatim} |
| 158 | from __future__ import nested_scopes |
| 159 | \end{verbatim} |
| 160 | |
| 161 | While it looks like a normal \keyword{import} statement, it's not; |
| 162 | there are strict rules on where such a future statement can be put. |
| 163 | They can only be at the top of a module, and must precede any Python |
| 164 | code or regular \keyword{import} statements. This is because such |
| 165 | statements can affect how the Python bytecode compiler parses code and |
| 166 | generates bytecode, so they must precede any statement that will |
| 167 | result in bytecodes being produced. |
| 168 | |
| 169 | \begin{seealso} |
| 170 | |
| 171 | \seepep{236}{Back to the \module{__future__}}{Written by Tim Peters, |
| 172 | and primarily implemented by Jeremy Hylton.} |
| 173 | |
| 174 | \end{seealso} |
Andrew M. Kuchling | f228fd1 | 2001-01-22 17:52:19 +0000 | [diff] [blame] | 175 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 176 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 177 | \section{PEP 207: Rich Comparisons} |
| 178 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 179 | In earlier versions, Python's support for implementing comparisons on |
| 180 | user-defined classes and extension types was quite simple. Classes |
| 181 | could implement a \method{__cmp__} method that was given two instances |
| 182 | of a class, and could only return 0 if they were equal or +1 or -1 if |
| 183 | they weren't; the method couldn't raise an exception or return |
| 184 | anything other than a Boolean value. Users of Numeric Python often |
| 185 | found this model too weak and restrictive, because in the |
| 186 | number-crunching programs that numeric Python is used for, it would be |
| 187 | more useful to be able to perform elementwise comparisons of two |
| 188 | matrices, returning a matrix containing the results of a given |
| 189 | comparison for each element. If the two matrices are of different |
| 190 | sizes, then the compare has to be able to raise an exception to signal |
| 191 | the error. |
| 192 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 193 | In Python 2.1, rich comparisons were added in order to support this |
| 194 | need. Python classes can now individually overload each of the |
| 195 | \code{<}, \code{<=}, \code{>}, \code{>=}, \code{==}, and \code{!=} |
| 196 | operations. The new magic method names are: |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 197 | |
| 198 | \begin{tableii}{c|l}{code}{Operation}{Method name} |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 199 | \lineii{<}{\method{__lt__}} \lineii{<=}{\method{__le__}} |
| 200 | \lineii{>}{\method{__gt__}} \lineii{>=}{\method{__ge__}} |
| 201 | \lineii{==}{\method{__eq__}} \lineii{!=}{\method{__ne__}} |
| 202 | \end{tableii} |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 203 | |
| 204 | (The magic methods are named after the corresponding Fortran operators |
| 205 | \code{.LT.}. \code{.LE.}, \&c. Numeric programmers are almost |
| 206 | certainly quite familar with these names and will find them easy to |
| 207 | remember.) |
| 208 | |
| 209 | Each of these magic methods is of the form \code{\var{method}(self, |
| 210 | other)}, where \code{self} will be the object on the left-hand side of |
| 211 | the operator, while \code{other} will be the object on the right-hand |
| 212 | side. For example, the expression \code{A < B} will cause |
| 213 | \code{A.__lt__(B)} to be called. |
| 214 | |
| 215 | Each of these magic methods can return anything at all: a Boolean, a |
| 216 | matrix, a list, or any other Python object. Alternatively they can |
| 217 | raise an exception if the comparison is impossible, inconsistent, or |
| 218 | otherwise meaningless. |
| 219 | |
| 220 | The built-in \function{cmp(A,B)} function can use the rich comparison |
| 221 | machinery, and now accepts an optional argument specifying which |
| 222 | comparison operation to use; this is given as one of the strings |
| 223 | \code{"<"}, \code{"<="}, \code{">"}, \code{">="}, \code{"=="}, or |
| 224 | \code{"!="}. If called without the optional third argument, |
| 225 | \function{cmp()} will only return -1, 0, or +1 as in previous versions |
| 226 | of Python; otherwise it will call the appropriate method and can |
| 227 | return any Python object. |
| 228 | |
| 229 | There are also corresponding changes of interest to C programmers; |
| 230 | there's a new slot \code{tp_richcmp} in type objects and an API for |
| 231 | performing a given rich comparison. I won't cover the C API here, but |
Andrew M. Kuchling | bf14014 | 2001-02-28 22:10:07 +0000 | [diff] [blame] | 232 | will refer you to PEP 207, or to 2.1's C API documentation, for the |
| 233 | full list of related functions. |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 234 | |
| 235 | \begin{seealso} |
| 236 | |
| 237 | \seepep{207}{Rich Comparisions}{Written by Guido van Rossum, heavily |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 238 | based on earlier work by David Ascher, and implemented by Guido van |
| 239 | Rossum.} |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 240 | |
| 241 | \end{seealso} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 242 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 243 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 244 | \section{PEP 230: Warning Framework} |
| 245 | |
| 246 | Over its 10 years of existence, Python has accumulated a certain |
| 247 | number of obsolete modules and features along the way. It's difficult |
| 248 | to know when a feature is safe to remove, since there's no way of |
Andrew M. Kuchling | f33c118 | 2001-01-23 02:48:26 +0000 | [diff] [blame] | 249 | knowing how much code uses it --- perhaps no programs depend on the |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 250 | feature, or perhaps many do. To enable removing old features in a |
| 251 | more structured way, a warning framework was added. When the Python |
| 252 | developers want to get rid of a feature, it will first trigger a |
| 253 | warning in the next version of Python. The following Python version |
| 254 | can then drop the feature, and users will have had a full release |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 255 | cycle to remove uses of the old feature. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 256 | |
| 257 | Python 2.1 adds the warning framework to be used in this scheme. It |
| 258 | adds a \module{warnings} module that provide functions to issue |
| 259 | warnings, and to filter out warnings that you don't want to be |
| 260 | displayed. Third-party modules can also use this framework to |
| 261 | deprecate old features that they no longer wish to support. |
| 262 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 263 | For example, in Python 2.1 the \module{regex} module is deprecated, so |
| 264 | importing it causes a warning to be printed: |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 265 | |
| 266 | \begin{verbatim} |
| 267 | >>> import regex |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 268 | __main__:1: DeprecationWarning: the regex module |
| 269 | is deprecated; please use the re module |
| 270 | >>> |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 271 | \end{verbatim} |
| 272 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 273 | Warnings can be issued by calling the \function{warnings.warn} |
| 274 | function: |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 275 | |
| 276 | \begin{verbatim} |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 277 | warnings.warn("feature X no longer supported") |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 278 | \end{verbatim} |
| 279 | |
| 280 | The first parameter is the warning message; an additional optional |
| 281 | parameters can be used to specify a particular warning category. |
| 282 | |
| 283 | Filters can be added to disable certain warnings; a regular expression |
| 284 | pattern can be applied to the message or to the module name in order |
| 285 | to suppress a warning. For example, you may have a program that uses |
| 286 | the \module{regex} module and not want to spare the time to convert it |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 287 | to use the \module{re} module right now. The warning can be |
| 288 | suppressed by calling |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 289 | |
| 290 | \begin{verbatim} |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 291 | import warnings |
| 292 | warnings.filterwarnings(action = 'ignore', |
| 293 | message='.*regex module is deprecated', |
| 294 | category=DeprecationWarning, |
| 295 | module = '__main__') |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 296 | \end{verbatim} |
| 297 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 298 | This adds a filter that will apply only to warnings of the class |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 299 | \class{DeprecationWarning} triggered in the \module{__main__} module, |
| 300 | and applies a regular expression to only match the message about the |
| 301 | \module{regex} module being deprecated, and will cause such warnings |
| 302 | to be ignored. Warnings can also be printed only once, printed every |
| 303 | time the offending code is executed, or turned into exceptions that |
| 304 | will cause the program to stop (unless the exceptions are caught in |
| 305 | the usual way, of course). |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 306 | |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 307 | Functions were also added to Python's C API for issuing warnings; |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 308 | refer to PEP 230 or to Python's API documentation for the details. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 309 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 310 | \begin{seealso} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 311 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 312 | \seepep{5}{Guidelines for Language Evolution}{Written |
| 313 | by Paul Prescod, to specify procedures to be followed when removing |
| 314 | old features from Python. The policy described in this PEP hasn't |
| 315 | been officially adopted, but the eventual policy probably won't be too |
| 316 | different from Prescod's proposal.} |
| 317 | |
| 318 | \seepep{230}{Warning Framework}{Written and implemented by Guido van |
| 319 | Rossum.} |
| 320 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 321 | \end{seealso} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 322 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 323 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 324 | \section{PEP 229: New Build System} |
| 325 | |
| 326 | When compiling Python, the user had to go in and edit the |
| 327 | \file{Modules/Setup} file in order to enable various additional |
| 328 | modules; the default set is relatively small and limited to modules |
| 329 | that compile on most Unix platforms. This means that on Unix |
| 330 | platforms with many more features, most notably Linux, Python |
| 331 | installations often don't contain all useful modules they could. |
| 332 | |
| 333 | Python 2.0 added the Distutils, a set of modules for distributing and |
| 334 | installing extensions. In Python 2.1, the Distutils are used to |
| 335 | compile much of the standard library of extension modules, |
Andrew M. Kuchling | f33c118 | 2001-01-23 02:48:26 +0000 | [diff] [blame] | 336 | autodetecting which ones are supported on the current machine. It's |
| 337 | hoped that this will make Python installations easier and more |
| 338 | featureful. |
| 339 | |
| 340 | Instead of having to edit the \file{Modules/Setup} file in order to |
| 341 | enable modules, a \file{setup.py} script in the top directory of the |
| 342 | Python source distribution is run at build time, and attempts to |
| 343 | discover which modules can be enabled by examining the modules and |
Andrew M. Kuchling | 8bad993 | 2001-02-28 22:39:15 +0000 | [diff] [blame] | 344 | header files on the system. If a module is configured in |
| 345 | \file{Modules/Setup}, the \file{setup.py} script won't attempt to |
| 346 | compile that module and will defer to the \file{Modules/Setup} file's |
| 347 | contents. This provides a way to specific any strange command-line |
| 348 | flags or libraries that are required for a specific platform. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 349 | |
Andrew M. Kuchling | 4308d3c | 2001-01-29 17:36:53 +0000 | [diff] [blame] | 350 | In another far-reaching change to the build mechanism, Neil |
| 351 | Schemenauer restructured things so Python now uses a single makefile |
| 352 | that isn't recursive, instead of makefiles in the top directory and in |
Andrew M. Kuchling | 8bad993 | 2001-02-28 22:39:15 +0000 | [diff] [blame] | 353 | each of the \file{Python/}, \file{Parser/}, \file{Objects/}, and |
| 354 | \file{Modules/} subdirectories. This makes building Python faster |
| 355 | and also makes hacking the Makefiles clearer and simpler. |
Andrew M. Kuchling | 4308d3c | 2001-01-29 17:36:53 +0000 | [diff] [blame] | 356 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 357 | \begin{seealso} |
| 358 | |
| 359 | \seepep{229}{Using Distutils to Build Python}{Written |
| 360 | and implemented by A.M. Kuchling.} |
| 361 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 362 | \end{seealso} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 363 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 364 | %====================================================================== |
Andrew M. Kuchling | 15ad28c | 2001-02-14 02:44:18 +0000 | [diff] [blame] | 365 | \section{PEP 205: Weak References} |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 366 | |
Andrew M. Kuchling | 15ad28c | 2001-02-14 02:44:18 +0000 | [diff] [blame] | 367 | Weak references, available through the \module{weakref} module, are a |
| 368 | minor but useful new data type in the Python programmer's toolbox. |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 369 | |
Andrew M. Kuchling | 15ad28c | 2001-02-14 02:44:18 +0000 | [diff] [blame] | 370 | Storing a reference to an object (say, in a dictionary or a list) has |
| 371 | the side effect of keeping that object alive forever. There are a few |
| 372 | specific cases where this behaviour is undesirable, object caches |
| 373 | being the most common one, and another being circular references in |
| 374 | data structures such as trees. |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 375 | |
Andrew M. Kuchling | 15ad28c | 2001-02-14 02:44:18 +0000 | [diff] [blame] | 376 | For example, consider a memoizing function that caches the results of |
| 377 | another function \function{f(\var{x})} by storing the function's |
| 378 | argument and its result in a dictionary: |
| 379 | |
| 380 | \begin{verbatim} |
| 381 | _cache = {} |
| 382 | def memoize(x): |
| 383 | if _cache.has_key(x): |
| 384 | return _cache[x] |
| 385 | |
| 386 | retval = f(x) |
| 387 | |
| 388 | # Cache the returned object |
| 389 | _cache[x] = retval |
| 390 | |
| 391 | return retval |
| 392 | \end{verbatim} |
| 393 | |
| 394 | This version works for simple things such as integers, but it has a |
| 395 | side effect; the \code{_cache} dictionary holds a reference to the |
| 396 | return values, so they'll never be deallocated until the Python |
| 397 | process exits and cleans up This isn't very noticeable for integers, |
| 398 | but if \function{f()} returns an object, or a data structure that |
| 399 | takes up a lot of memory, this can be a problem. |
| 400 | |
| 401 | Weak references provide a way to implement a cache that won't keep |
| 402 | objects alive beyond their time. If an object is only accessible |
| 403 | through weak references, the object will be deallocated and the weak |
| 404 | references will now indicate that the object it referred to no longer |
| 405 | exists. A weak reference to an object \var{obj} is created by calling |
| 406 | \code{wr = weakref.ref(\var{obj})}. The object being referred to is |
| 407 | returned by calling the weak reference as if it were a function: |
| 408 | \code{wr()}. It will return the referenced object, or \code{None} if |
| 409 | the object no longer exists. |
| 410 | |
| 411 | This makes it possible to write a \function{memoize()} function whose |
| 412 | cache doesn't keep objects alive, by storing weak references in the |
| 413 | cache. |
| 414 | |
| 415 | \begin{verbatim} |
| 416 | _cache = {} |
| 417 | def memoize(x): |
| 418 | if _cache.has_key(x): |
| 419 | obj = _cache[x]() |
| 420 | # If weak reference object still exists, |
| 421 | # return it |
| 422 | if obj is not None: return obj |
| 423 | |
| 424 | retval = f(x) |
| 425 | |
| 426 | # Cache a weak reference |
| 427 | _cache[x] = weakref.ref(retval) |
| 428 | |
| 429 | return retval |
| 430 | \end{verbatim} |
| 431 | |
| 432 | The \module{weakref} module also allows creating proxy objects which |
| 433 | behave like weak references --- an object referenced only by proxy |
| 434 | objects is deallocated -- but instead of requiring an explicit call to |
| 435 | retrieve the object, the proxy transparently forwards all operations |
| 436 | to the object as long as the object still exists. If the object is |
| 437 | deallocated, attempting to use a proxy will cause a |
| 438 | \exception{weakref.ReferenceError} exception to be raised. |
| 439 | |
| 440 | \begin{verbatim} |
| 441 | proxy = weakref.proxy(obj) |
| 442 | proxy.attr # Equivalent to obj.attr |
| 443 | proxy.meth() # Equivalent to obj.meth() |
| 444 | del obj |
| 445 | proxy.attr # raises weakref.ReferenceError |
| 446 | \end{verbatim} |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 447 | |
| 448 | \begin{seealso} |
| 449 | |
| 450 | \seepep{205}{Weak References}{Written and implemented by |
| 451 | Fred~L. Drake,~Jr.} |
| 452 | |
| 453 | \end{seealso} |
| 454 | |
| 455 | %====================================================================== |
Andrew M. Kuchling | ef85cc8 | 2001-03-23 03:29:08 +0000 | [diff] [blame] | 456 | \section{PEP 232: Function Attributes} |
| 457 | |
| 458 | In Python 2.1, functions can now have arbitrary information attached |
| 459 | to them. People were often using docstrings to hold information about |
| 460 | functions and methods, because the \code{__doc__} attribute was the |
| 461 | only way of attaching any information to a function. For example, in |
| 462 | the Zope Web application server, functions are marked as safe for |
| 463 | public access by having a docstring, and in John Aycock's SPARK |
| 464 | parsing framework, docstrings hold parts of the BNF grammar to be |
| 465 | parsed. This overloading is unfortunate, since docstrings are really |
| 466 | intended to hold a function's documentation; for example, it means you |
| 467 | can't properly document functions intended for private use in Zope. |
| 468 | |
| 469 | Arbitrary attributes can now be set and retrieved on functions using the |
| 470 | regular Python syntax: |
| 471 | |
| 472 | \begin{verbatim} |
| 473 | def f(): pass |
| 474 | |
| 475 | f.publish = 1 |
| 476 | f.secure = 1 |
| 477 | f.grammar = "A ::= B (C D)*" |
| 478 | \end{verbatim} |
| 479 | |
| 480 | The dictionary containing attributes can be accessed as the function's |
| 481 | \member{__dict__}. Unlike the \member{__dict__} attribute of class |
| 482 | instances, in functions you can actually assign a new dictionary to |
| 483 | \member{__dict__}, though the new value is restricted to a regular |
| 484 | Python dictionary; you \emph{can't} be tricky and set it to a |
| 485 | \class{UserDict} instance, or any other random object that behaves |
| 486 | like a mapping. |
| 487 | |
| 488 | \begin{seealso} |
| 489 | |
| 490 | \seepep{232}{Function Attributes}{Written and implemented by Barry |
| 491 | Warsaw.} |
| 492 | |
| 493 | \end{seealso} |
| 494 | |
| 495 | |
| 496 | %====================================================================== |
| 497 | |
Andrew M. Kuchling | 8d17709 | 2003-05-13 14:26:54 +0000 | [diff] [blame] | 498 | \section{PEP 235: Importing Modules on Case-Insensitive Platforms} |
Andrew M. Kuchling | 74d18ed | 2001-02-28 22:22:40 +0000 | [diff] [blame] | 499 | |
Andrew M. Kuchling | 8bad993 | 2001-02-28 22:39:15 +0000 | [diff] [blame] | 500 | Some operating systems have filesystems that are case-insensitive, |
| 501 | MacOS and Windows being the primary examples; on these systems, it's |
| 502 | impossible to distinguish the filenames \samp{FILE.PY} and |
| 503 | \samp{file.py}, even though they do store the file's name |
| 504 | in its original case (they're case-preserving, too). |
| 505 | |
| 506 | In Python 2.1, the \keyword{import} statement will work to simulate |
| 507 | case-sensitivity on case-insensitive platforms. Python will now |
| 508 | search for the first case-sensitive match by default, raising an |
| 509 | \exception{ImportError} if no such file is found, so \code{import file} |
| 510 | will not import a module named \samp{FILE.PY}. Case-insensitive |
Fred Drake | 9ad526f | 2001-04-12 04:11:21 +0000 | [diff] [blame] | 511 | matching can be requested by setting the \envvar{PYTHONCASEOK} environment |
Andrew M. Kuchling | 8bad993 | 2001-02-28 22:39:15 +0000 | [diff] [blame] | 512 | variable before starting the Python interpreter. |
Andrew M. Kuchling | 74d18ed | 2001-02-28 22:22:40 +0000 | [diff] [blame] | 513 | |
| 514 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 515 | \section{PEP 217: Interactive Display Hook} |
| 516 | |
| 517 | When using the Python interpreter interactively, the output of |
| 518 | commands is displayed using the built-in \function{repr()} function. |
Andrew M. Kuchling | ef85cc8 | 2001-03-23 03:29:08 +0000 | [diff] [blame] | 519 | In Python 2.1, the variable \function{sys.displayhook} can be set to a |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 520 | callable object which will be called instead of \function{repr()}. |
| 521 | For example, you can set it to a special pretty-printing function: |
| 522 | |
| 523 | \begin{verbatim} |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 524 | >>> # Create a recursive data structure |
| 525 | ... L = [1,2,3] |
| 526 | >>> L.append(L) |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 527 | >>> L # Show Python's default output |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 528 | [1, 2, 3, [...]] |
| 529 | >>> # Use pprint.pprint() as the display function |
| 530 | ... import sys, pprint |
| 531 | >>> sys.displayhook = pprint.pprint |
| 532 | >>> L |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 533 | [1, 2, 3, <Recursion on list with id=135143996>] |
| 534 | >>> |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 535 | \end{verbatim} |
| 536 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 537 | \begin{seealso} |
| 538 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 539 | \seepep{217}{Display Hook for Interactive Use}{Written and implemented |
| 540 | by Moshe Zadka.} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 541 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 542 | \end{seealso} |
| 543 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 544 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 545 | \section{PEP 208: New Coercion Model} |
| 546 | |
| 547 | How numeric coercion is done at the C level was significantly |
| 548 | modified. This will only affect the authors of C extensions to |
| 549 | Python, allowing them more flexibility in writing extension types that |
| 550 | support numeric operations. |
| 551 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 552 | Extension types can now set the type flag \code{Py_TPFLAGS_CHECKTYPES} |
| 553 | in their \code{PyTypeObject} structure to indicate that they support |
| 554 | the new coercion model. In such extension types, the numeric slot |
| 555 | functions can no longer assume that they'll be passed two arguments of |
| 556 | the same type; instead they may be passed two arguments of differing |
| 557 | types, and can then perform their own internal coercion. If the slot |
| 558 | function is passed a type it can't handle, it can indicate the failure |
| 559 | by returning a reference to the \code{Py_NotImplemented} singleton |
| 560 | value. The numeric functions of the other type will then be tried, |
| 561 | and perhaps they can handle the operation; if the other type also |
| 562 | returns \code{Py_NotImplemented}, then a \exception{TypeError} will be |
| 563 | raised. Numeric methods written in Python can also return |
| 564 | \code{Py_NotImplemented}, causing the interpreter to act as if the |
| 565 | method did not exist (perhaps raising a \exception{TypeError}, perhaps |
| 566 | trying another object's numeric methods). |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 567 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 568 | \begin{seealso} |
| 569 | |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 570 | \seepep{208}{Reworking the Coercion Model}{Written and implemented by |
| 571 | Neil Schemenauer, heavily based upon earlier work by Marc-Andr\'e |
| 572 | Lemburg. Read this to understand the fine points of how numeric |
| 573 | operations will now be processed at the C level.} |
| 574 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 575 | \end{seealso} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 576 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 577 | %====================================================================== |
Andrew M. Kuchling | ef85cc8 | 2001-03-23 03:29:08 +0000 | [diff] [blame] | 578 | \section{PEP 241: Metadata in Python Packages} |
| 579 | |
| 580 | A common complaint from Python users is that there's no single catalog |
| 581 | of all the Python modules in existence. T.~Middleton's Vaults of |
Fred Drake | 700c890 | 2003-07-22 00:52:42 +0000 | [diff] [blame] | 582 | Parnassus at \url{http://www.vex.net/parnassus/} are the largest |
Andrew M. Kuchling | ef85cc8 | 2001-03-23 03:29:08 +0000 | [diff] [blame] | 583 | catalog of Python modules, but registering software at the Vaults is |
| 584 | optional, and many people don't bother. |
| 585 | |
| 586 | As a first small step toward fixing the problem, Python software |
| 587 | packaged using the Distutils \command{sdist} command will include a |
| 588 | file named \file{PKG-INFO} containing information about the package |
| 589 | such as its name, version, and author (metadata, in cataloguing |
| 590 | terminology). PEP 241 contains the full list of fields that can be |
| 591 | present in the \file{PKG-INFO} file. As people began to package their |
| 592 | software using Python 2.1, more and more packages will include |
| 593 | metadata, making it possible to build automated cataloguing systems |
| 594 | and experiment with them. With the result experience, perhaps it'll |
| 595 | be possible to design a really good catalog and then build support for |
| 596 | it into Python 2.2. For example, the Distutils \command{sdist} |
| 597 | and \command{bdist_*} commands could support a \option{upload} option |
| 598 | that would automatically upload your package to a catalog server. |
| 599 | |
| 600 | You can start creating packages containing \file{PKG-INFO} even if |
| 601 | you're not using Python 2.1, since a new release of the Distutils will |
| 602 | be made for users of earlier Python versions. Version 1.0.2 of the |
| 603 | Distutils includes the changes described in PEP 241, as well as |
| 604 | various bugfixes and enhancements. It will be available from |
Fred Drake | 700c890 | 2003-07-22 00:52:42 +0000 | [diff] [blame] | 605 | the Distutils SIG at \url{http://www.python.org/sigs/distutils-sig/}. |
Andrew M. Kuchling | ef85cc8 | 2001-03-23 03:29:08 +0000 | [diff] [blame] | 606 | |
Andrew M. Kuchling | ef85cc8 | 2001-03-23 03:29:08 +0000 | [diff] [blame] | 607 | \begin{seealso} |
| 608 | |
| 609 | \seepep{241}{Metadata for Python Software Packages}{Written and |
| 610 | implemented by A.M. Kuchling.} |
| 611 | |
| 612 | \seepep{243}{Module Repository Upload Mechanism}{Written by Sean |
| 613 | Reifschneider, this draft PEP describes a proposed mechanism for uploading |
| 614 | Python packages to a central server. |
| 615 | } |
| 616 | |
| 617 | \end{seealso} |
| 618 | |
| 619 | %====================================================================== |
Andrew M. Kuchling | 81b6ae7 | 2001-02-11 16:55:39 +0000 | [diff] [blame] | 620 | \section{New and Improved Modules} |
| 621 | |
| 622 | \begin{itemize} |
| 623 | |
Andrew M. Kuchling | 1fcd438 | 2001-04-16 02:27:53 +0000 | [diff] [blame] | 624 | \item Ka-Ping Yee contributed two new modules: \module{inspect.py}, a |
| 625 | module for getting information about live Python code, and |
| 626 | \module{pydoc.py}, a module for interactively converting docstrings to |
| 627 | HTML or text. As a bonus, \file{Tools/scripts/pydoc}, which is now |
| 628 | automatically installed, uses \module{pydoc.py} to display |
| 629 | documentation given a Python module, package, or class name. For |
| 630 | example, \samp{pydoc xml.dom} displays the following: |
Andrew M. Kuchling | 74d18ed | 2001-02-28 22:22:40 +0000 | [diff] [blame] | 631 | |
| 632 | \begin{verbatim} |
| 633 | Python Library Documentation: package xml.dom in xml |
| 634 | |
| 635 | NAME |
| 636 | xml.dom - W3C Document Object Model implementation for Python. |
| 637 | |
| 638 | FILE |
| 639 | /usr/local/lib/python2.1/xml/dom/__init__.pyc |
| 640 | |
| 641 | DESCRIPTION |
| 642 | The Python mapping of the Document Object Model is documented in the |
| 643 | Python Library Reference in the section on the xml.dom package. |
| 644 | |
| 645 | This package contains the following modules: |
| 646 | ... |
| 647 | \end{verbatim} |
| 648 | |
Andrew M. Kuchling | 1fcd438 | 2001-04-16 02:27:53 +0000 | [diff] [blame] | 649 | \file{pydoc} also includes a Tk-based interactive help browser. |
Andrew M. Kuchling | 74d18ed | 2001-02-28 22:22:40 +0000 | [diff] [blame] | 650 | \file{pydoc} quickly becomes addictive; try it out! |
| 651 | |
Andrew M. Kuchling | ef85cc8 | 2001-03-23 03:29:08 +0000 | [diff] [blame] | 652 | \item Two different modules for unit testing were added to the |
| 653 | standard library. The \module{doctest} module, contributed by Tim |
| 654 | Peters, provides a testing framework based on running embedded |
| 655 | examples in docstrings and comparing the results against the expected |
| 656 | output. PyUnit, contributed by Steve Purcell, is a unit testing |
| 657 | framework inspired by JUnit, which was in turn an adaptation of Kent |
| 658 | Beck's Smalltalk testing framework. See |
| 659 | \url{http://pyunit.sourceforge.net/} for more information about |
| 660 | PyUnit. |
Andrew M. Kuchling | 15ad28c | 2001-02-14 02:44:18 +0000 | [diff] [blame] | 661 | |
| 662 | \item The \module{difflib} module contains a class, |
| 663 | \class{SequenceMatcher}, which compares two sequences and computes the |
| 664 | changes required to transform one sequence into the other. For |
| 665 | example, this module can be used to write a tool similar to the Unix |
| 666 | \program{diff} program, and in fact the sample program |
| 667 | \file{Tools/scripts/ndiff.py} demonstrates how to write such a script. |
| 668 | |
Andrew M. Kuchling | 81b6ae7 | 2001-02-11 16:55:39 +0000 | [diff] [blame] | 669 | \item \module{curses.panel}, a wrapper for the panel library, part of |
| 670 | ncurses and of SYSV curses, was contributed by Thomas Gellekum. The |
| 671 | panel library provides windows with the additional feature of depth. |
| 672 | Windows can be moved higher or lower in the depth ordering, and the |
| 673 | panel library figures out where panels overlap and which sections are |
| 674 | visible. |
| 675 | |
| 676 | \item The PyXML package has gone through a few releases since Python |
| 677 | 2.0, and Python 2.1 includes an updated version of the \module{xml} |
Andrew M. Kuchling | 74d18ed | 2001-02-28 22:22:40 +0000 | [diff] [blame] | 678 | package. Some of the noteworthy changes include support for Expat 1.2 |
| 679 | and later versions, the ability for Expat parsers to handle files in |
| 680 | any encoding supported by Python, and various bugfixes for SAX, DOM, |
| 681 | and the \module{minidom} module. |
Andrew M. Kuchling | 81b6ae7 | 2001-02-11 16:55:39 +0000 | [diff] [blame] | 682 | |
Andrew M. Kuchling | ef85cc8 | 2001-03-23 03:29:08 +0000 | [diff] [blame] | 683 | \item Ping also contributed another hook for handling uncaught |
| 684 | exceptions. \function{sys.excepthook} can be set to a callable |
| 685 | object. When an exception isn't caught by any |
| 686 | \keyword{try}...\keyword{except} blocks, the exception will be passed |
| 687 | to \function{sys.excepthook}, which can then do whatever it likes. At |
| 688 | the Ninth Python Conference, Ping demonstrated an application for this |
| 689 | hook: printing an extended traceback that not only lists the stack |
| 690 | frames, but also lists the function arguments and the local variables |
| 691 | for each frame. |
| 692 | |
Andrew M. Kuchling | 81b6ae7 | 2001-02-11 16:55:39 +0000 | [diff] [blame] | 693 | \item Various functions in the \module{time} module, such as |
| 694 | \function{asctime()} and \function{localtime()}, require a floating |
| 695 | point argument containing the time in seconds since the epoch. The |
| 696 | most common use of these functions is to work with the current time, |
| 697 | so the floating point argument has been made optional; when a value |
| 698 | isn't provided, the current time will be used. For example, log file |
| 699 | entries usually need a string containing the current time; in Python |
| 700 | 2.1, \code{time.asctime()} can be used, instead of the lengthier |
| 701 | \code{time.asctime(time.localtime(time.time()))} that was previously |
| 702 | required. |
| 703 | |
| 704 | This change was proposed and implemented by Thomas Wouters. |
| 705 | |
| 706 | \item The \module{ftplib} module now defaults to retrieving files in |
| 707 | passive mode, because passive mode is more likely to work from behind |
| 708 | a firewall. This request came from the Debian bug tracking system, |
| 709 | since other Debian packages use \module{ftplib} to retrieve files and |
| 710 | then don't work from behind a firewall. It's deemed unlikely that |
| 711 | this will cause problems for anyone, because Netscape defaults to |
| 712 | passive mode and few people complain, but if passive mode is |
| 713 | unsuitable for your application or network setup, call |
| 714 | \method{set_pasv(0)} on FTP objects to disable passive mode. |
| 715 | |
| 716 | \item Support for raw socket access has been added to the |
| 717 | \module{socket} module, contributed by Grant Edwards. |
| 718 | |
Andrew M. Kuchling | 1fcd438 | 2001-04-16 02:27:53 +0000 | [diff] [blame] | 719 | \item The \module{pstats} module now contains a simple interactive |
| 720 | statistics browser for displaying timing profiles for Python programs, |
| 721 | invoked when the module is run as a script. Contributed by |
| 722 | Eric S.\ Raymond. |
| 723 | |
Andrew M. Kuchling | 15ad28c | 2001-02-14 02:44:18 +0000 | [diff] [blame] | 724 | \item A new implementation-dependent function, \function{sys._getframe(\optional{depth})}, |
| 725 | has been added to return a given frame object from the current call stack. |
| 726 | \function{sys._getframe()} returns the frame at the top of the call stack; |
| 727 | if the optional integer argument \var{depth} is supplied, the function returns the frame |
| 728 | that is \var{depth} calls below the top of the stack. For example, \code{sys._getframe(1)} |
| 729 | returns the caller's frame object. |
| 730 | |
| 731 | This function is only present in CPython, not in Jython or the .NET |
| 732 | implementation. Use it for debugging, and resist the temptation to |
| 733 | put it into production code. |
| 734 | |
| 735 | |
Andrew M. Kuchling | 81b6ae7 | 2001-02-11 16:55:39 +0000 | [diff] [blame] | 736 | |
| 737 | \end{itemize} |
| 738 | |
| 739 | %====================================================================== |
Andrew M. Kuchling | 74d18ed | 2001-02-28 22:22:40 +0000 | [diff] [blame] | 740 | \section{Other Changes and Fixes} |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 741 | |
| 742 | There were relatively few smaller changes made in Python 2.1 due to |
| 743 | the shorter release cycle. A search through the CVS change logs turns |
Andrew M. Kuchling | 81df7be | 2001-03-02 21:19:38 +0000 | [diff] [blame] | 744 | up 117 patches applied, and 136 bugs fixed; both figures are likely to |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 745 | be underestimates. Some of the more notable changes are: |
| 746 | |
| 747 | \begin{itemize} |
| 748 | |
Andrew M. Kuchling | bf14014 | 2001-02-28 22:10:07 +0000 | [diff] [blame] | 749 | |
| 750 | \item A specialized object allocator is now optionally available, that |
| 751 | should be faster than the system \function{malloc()} and have less |
| 752 | memory overhead. The allocator uses C's \function{malloc()} function |
| 753 | to get large pools of memory, and then fulfills smaller memory |
| 754 | requests from these pools. It can be enabled by providing the |
Andrew M. Kuchling | 45bbda2 | 2001-03-10 16:49:07 +0000 | [diff] [blame] | 755 | \longprogramopt{with-pymalloc} option to the \program{configure} script; see |
Andrew M. Kuchling | ac1abe0 | 2001-03-23 03:52:46 +0000 | [diff] [blame] | 756 | \file{Objects/obmalloc.c} for the implementation details. |
| 757 | |
| 758 | Authors of C extension modules should test their code with the object |
| 759 | allocator enabled, because some incorrect code may break, causing core |
| 760 | dumps at runtime. There are a bunch of memory allocation functions in |
| 761 | Python's C API that have previously been just aliases for the C |
| 762 | library's \function{malloc()} and \function{free()}, meaning that if |
| 763 | you accidentally called mismatched functions, the error wouldn't be |
| 764 | noticeable. When the object allocator is enabled, these functions |
| 765 | aren't aliases of \function{malloc()} and \function{free()} any more, |
| 766 | and calling the wrong function to free memory will get you a core |
| 767 | dump. For example, if memory was allocated using |
| 768 | \function{PyMem_New()}, it has to be freed using |
| 769 | \function{PyMem_Del()}, not \function{free()}. A few modules included |
| 770 | with Python fell afoul of this and had to be fixed; doubtless there |
| 771 | are more third-party modules that will have the same problem. |
| 772 | |
| 773 | The object allocator was contributed by Vladimir Marangozov. |
Andrew M. Kuchling | bf14014 | 2001-02-28 22:10:07 +0000 | [diff] [blame] | 774 | |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 775 | \item The speed of line-oriented file I/O has been improved because |
| 776 | people often complain about its lack of speed, and because it's often |
| 777 | been used as a na\"ive benchmark. The \method{readline()} method of |
| 778 | file objects has therefore been rewritten to be much faster. The |
| 779 | exact amount of the speedup will vary from platform to platform |
| 780 | depending on how slow the C library's \function{getc()} was, but is |
| 781 | around 66\%, and potentially much faster on some particular operating |
Andrew M. Kuchling | f228fd1 | 2001-01-22 17:52:19 +0000 | [diff] [blame] | 782 | systems. Tim Peters did much of the benchmarking and coding for this |
| 783 | change, motivated by a discussion in comp.lang.python. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 784 | |
| 785 | A new module and method for file objects was also added, contributed |
| 786 | by Jeff Epler. The new method, \method{xreadlines()}, is similar to |
| 787 | the existing \function{xrange()} built-in. \function{xreadlines()} |
| 788 | returns an opaque sequence object that only supports being iterated |
Andrew M. Kuchling | f228fd1 | 2001-01-22 17:52:19 +0000 | [diff] [blame] | 789 | over, reading a line on every iteration but not reading the entire |
Andrew M. Kuchling | f33c118 | 2001-01-23 02:48:26 +0000 | [diff] [blame] | 790 | file into memory as the existing \method{readlines()} method does. |
| 791 | You'd use it like this: |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 792 | |
| 793 | \begin{verbatim} |
| 794 | for line in sys.stdin.xreadlines(): |
| 795 | # ... do something for each line ... |
| 796 | ... |
| 797 | \end{verbatim} |
| 798 | |
Andrew M. Kuchling | f228fd1 | 2001-01-22 17:52:19 +0000 | [diff] [blame] | 799 | For a fuller discussion of the line I/O changes, see the python-dev |
Andrew M. Kuchling | db7657d | 2001-04-12 03:37:19 +0000 | [diff] [blame] | 800 | summary for January 1-15, 2001 at |
Fred Drake | 700c890 | 2003-07-22 00:52:42 +0000 | [diff] [blame] | 801 | \url{http://www.python.org/dev/summary/2001-01-1.html}. |
Andrew M. Kuchling | 91834c6 | 2001-01-22 19:51:13 +0000 | [diff] [blame] | 802 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 803 | \item A new method, \method{popitem()}, was added to dictionaries to |
| 804 | enable destructively iterating through the contents of a dictionary; |
Andrew M. Kuchling | db7657d | 2001-04-12 03:37:19 +0000 | [diff] [blame] | 805 | this can be faster for large dictionaries because there's no need to |
| 806 | construct a list containing all the keys or values. |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 807 | \code{D.popitem()} removes a random \code{(\var{key}, \var{value})} |
Andrew M. Kuchling | db7657d | 2001-04-12 03:37:19 +0000 | [diff] [blame] | 808 | pair from the dictionary~\code{D} and returns it as a 2-tuple. This |
| 809 | was implemented mostly by Tim Peters and Guido van Rossum, after a |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 810 | suggestion and preliminary patch by Moshe Zadka. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 811 | |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 812 | \item Modules can now control which names are imported when \code{from |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 813 | \var{module} import *} is used, by defining an \code{__all__} |
| 814 | attribute containing a list of names that will be imported. One |
| 815 | common complaint is that if the module imports other modules such as |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 816 | \module{sys} or \module{string}, \code{from \var{module} import *} |
| 817 | will add them to the importing module's namespace. To fix this, |
| 818 | simply list the public names in \code{__all__}: |
| 819 | |
| 820 | \begin{verbatim} |
| 821 | # List public names |
| 822 | __all__ = ['Database', 'open'] |
| 823 | \end{verbatim} |
| 824 | |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 825 | A stricter version of this patch was first suggested and implemented |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 826 | by Ben Wolfson, but after some python-dev discussion, a weaker final |
| 827 | version was checked in. |
Andrew M. Kuchling | b216ab6 | 2001-01-22 16:15:44 +0000 | [diff] [blame] | 828 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 829 | \item Applying \function{repr()} to strings previously used octal |
| 830 | escapes for non-printable characters; for example, a newline was |
| 831 | \code{'\e 012'}. This was a vestigial trace of Python's C ancestry, but |
| 832 | today octal is of very little practical use. Ka-Ping Yee suggested |
| 833 | using hex escapes instead of octal ones, and using the \code{\e n}, |
| 834 | \code{\e t}, \code{\e r} escapes for the appropriate characters, and |
| 835 | implemented this new formatting. |
Andrew M. Kuchling | 4308d3c | 2001-01-29 17:36:53 +0000 | [diff] [blame] | 836 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 837 | \item Syntax errors detected at compile-time can now raise exceptions |
| 838 | containing the filename and line number of the error, a pleasant side |
| 839 | effect of the compiler reorganization done by Jeremy Hylton. |
| 840 | |
Andrew M. Kuchling | 15ad28c | 2001-02-14 02:44:18 +0000 | [diff] [blame] | 841 | \item C extensions which import other modules have been changed to use |
| 842 | \function{PyImport_ImportModule()}, which means that they will use any |
| 843 | import hooks that have been installed. This is also encouraged for |
| 844 | third-party extensions that need to import some other module from C |
| 845 | code. |
| 846 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 847 | \item The size of the Unicode character database was shrunk by another |
| 848 | 340K thanks to Fredrik Lundh. |
Andrew M. Kuchling | 91834c6 | 2001-01-22 19:51:13 +0000 | [diff] [blame] | 849 | |
Andrew M. Kuchling | ac1abe0 | 2001-03-23 03:52:46 +0000 | [diff] [blame] | 850 | \item Some new ports were contributed: MacOS X (by Steven Majewski), |
Andrew M. Kuchling | db7657d | 2001-04-12 03:37:19 +0000 | [diff] [blame] | 851 | Cygwin (by Jason Tishler); RISCOS (by Dietmar Schwertberger); Unixware~7 |
| 852 | (by Billy G. Allie). |
Andrew M. Kuchling | ac1abe0 | 2001-03-23 03:52:46 +0000 | [diff] [blame] | 853 | |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 854 | \end{itemize} |
| 855 | |
Andrew M. Kuchling | 74d18ed | 2001-02-28 22:22:40 +0000 | [diff] [blame] | 856 | And there's the usual list of minor bugfixes, minor memory leaks, |
| 857 | docstring edits, and other tweaks, too lengthy to be worth itemizing; |
| 858 | see the CVS logs for the full details if you want them. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 859 | |
| 860 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 861 | %====================================================================== |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 862 | \section{Acknowledgements} |
| 863 | |
Andrew M. Kuchling | 6a360bd | 2001-02-05 02:47:52 +0000 | [diff] [blame] | 864 | The author would like to thank the following people for offering |
| 865 | suggestions on various drafts of this article: Graeme Cross, David |
| 866 | Goodger, Jay Graves, Michael Hudson, Marc-Andr\'e Lemburg, Fredrik |
| 867 | Lundh, Neil Schemenauer, Thomas Wouters. |
Andrew M. Kuchling | 90cecee | 2001-01-22 04:02:09 +0000 | [diff] [blame] | 868 | |
| 869 | \end{document} |