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