Fix resource leak reported in SF #1516995.
[python.git] / Doc / whatsnew / whatsnew21.tex
blob67cbbe4905300fa20af0c11499a20a102b6af539
1 \documentclass{howto}
3 \usepackage{distutils}
5 % $Id$
7 \title{What's New in Python 2.1}
8 \release{1.01}
9 \author{A.M. Kuchling}
10 \authoraddress{
11 \strong{Python Software Foundation}\\
12 Email: \email{amk@amk.ca}
14 \begin{document}
15 \maketitle\tableofcontents
17 \section{Introduction}
19 This article explains the new features in Python 2.1. While there aren't as
20 many changes in 2.1 as there were in Python 2.0, there are still some
21 pleasant surprises in store. 2.1 is the first release to be steered
22 through the use of Python Enhancement Proposals, or PEPs, so most of
23 the sizable changes have accompanying PEPs that provide more complete
24 documentation and a design rationale for the change. This article
25 doesn't attempt to document the new features completely, but simply
26 provides an overview of the new features for Python programmers.
27 Refer to the Python 2.1 documentation, or to the specific PEP, for
28 more details about any new feature that particularly interests you.
30 One recent goal of the Python development team has been to accelerate
31 the pace of new releases, with a new release coming every 6 to 9
32 months. 2.1 is the first release to come out at this faster pace, with
33 the first alpha appearing in January, 3 months after the final version
34 of 2.0 was released.
36 The final release of Python 2.1 was made on April 17, 2001.
38 %======================================================================
39 \section{PEP 227: Nested Scopes}
41 The largest change in Python 2.1 is to Python's scoping rules. In
42 Python 2.0, at any given time there are at most three namespaces used
43 to look up variable names: local, module-level, and the built-in
44 namespace. This often surprised people because it didn't match their
45 intuitive expectations. For example, a nested recursive function
46 definition doesn't work:
48 \begin{verbatim}
49 def f():
50 ...
51 def g(value):
52 ...
53 return g(value-1) + 1
54 ...
55 \end{verbatim}
57 The function \function{g()} will always raise a \exception{NameError}
58 exception, because the binding of the name \samp{g} isn't in either
59 its local namespace or in the module-level namespace. This isn't much
60 of a problem in practice (how often do you recursively define interior
61 functions like this?), but this also made using the \keyword{lambda}
62 statement clumsier, and this was a problem in practice. In code which
63 uses \keyword{lambda} you can often find local variables being copied
64 by passing them as the default values of arguments.
66 \begin{verbatim}
67 def find(self, name):
68 "Return list of any entries equal to 'name'"
69 L = filter(lambda x, name=name: x == name,
70 self.list_attribute)
71 return L
72 \end{verbatim}
74 The readability of Python code written in a strongly functional style
75 suffers greatly as a result.
77 The most significant change to Python 2.1 is that static scoping has
78 been added to the language to fix this problem. As a first effect,
79 the \code{name=name} default argument is now unnecessary in the above
80 example. Put simply, when a given variable name is not assigned a
81 value within a function (by an assignment, or the \keyword{def},
82 \keyword{class}, or \keyword{import} statements), references to the
83 variable will be looked up in the local namespace of the enclosing
84 scope. A more detailed explanation of the rules, and a dissection of
85 the implementation, can be found in the PEP.
87 This change may cause some compatibility problems for code where the
88 same variable name is used both at the module level and as a local
89 variable within a function that contains further function definitions.
90 This seems rather unlikely though, since such code would have been
91 pretty confusing to read in the first place.
93 One side effect of the change is that the \code{from \var{module}
94 import *} and \keyword{exec} statements have been made illegal inside
95 a function scope under certain conditions. The Python reference
96 manual has said all along that \code{from \var{module} import *} is
97 only legal at the top level of a module, but the CPython interpreter
98 has never enforced this before. As part of the implementation of
99 nested scopes, the compiler which turns Python source into bytecodes
100 has to generate different code to access variables in a containing
101 scope. \code{from \var{module} import *} and \keyword{exec} make it
102 impossible for the compiler to figure this out, because they add names
103 to the local namespace that are unknowable at compile time.
104 Therefore, if a function contains function definitions or
105 \keyword{lambda} expressions with free variables, the compiler will
106 flag this by raising a \exception{SyntaxError} exception.
108 To make the preceding explanation a bit clearer, here's an example:
110 \begin{verbatim}
111 x = 1
112 def f():
113 # The next line is a syntax error
114 exec 'x=2'
115 def g():
116 return x
117 \end{verbatim}
119 Line 4 containing the \keyword{exec} statement is a syntax error,
120 since \keyword{exec} would define a new local variable named \samp{x}
121 whose value should be accessed by \function{g()}.
123 This shouldn't be much of a limitation, since \keyword{exec} is rarely
124 used in most Python code (and when it is used, it's often a sign of a
125 poor design anyway).
127 Compatibility concerns have led to nested scopes being introduced
128 gradually; in Python 2.1, they aren't enabled by default, but can be
129 turned on within a module by using a future statement as described in
130 PEP 236. (See the following section for further discussion of PEP
131 236.) In Python 2.2, nested scopes will become the default and there
132 will be no way to turn them off, but users will have had all of 2.1's
133 lifetime to fix any breakage resulting from their introduction.
135 \begin{seealso}
137 \seepep{227}{Statically Nested Scopes}{Written and implemented by
138 Jeremy Hylton.}
140 \end{seealso}
143 %======================================================================
144 \section{PEP 236: __future__ Directives}
146 The reaction to nested scopes was widespread concern about the dangers
147 of breaking code with the 2.1 release, and it was strong enough to
148 make the Pythoneers take a more conservative approach. This approach
149 consists of introducing a convention for enabling optional
150 functionality in release N that will become compulsory in release N+1.
152 The syntax uses a \code{from...import} statement using the reserved
153 module name \module{__future__}. Nested scopes can be enabled by the
154 following statement:
156 \begin{verbatim}
157 from __future__ import nested_scopes
158 \end{verbatim}
160 While it looks like a normal \keyword{import} statement, it's not;
161 there are strict rules on where such a future statement can be put.
162 They can only be at the top of a module, and must precede any Python
163 code or regular \keyword{import} statements. This is because such
164 statements can affect how the Python bytecode compiler parses code and
165 generates bytecode, so they must precede any statement that will
166 result in bytecodes being produced.
168 \begin{seealso}
170 \seepep{236}{Back to the \module{__future__}}{Written by Tim Peters,
171 and primarily implemented by Jeremy Hylton.}
173 \end{seealso}
175 %======================================================================
176 \section{PEP 207: Rich Comparisons}
178 In earlier versions, Python's support for implementing comparisons on
179 user-defined classes and extension types was quite simple. Classes
180 could implement a \method{__cmp__} method that was given two instances
181 of a class, and could only return 0 if they were equal or +1 or -1 if
182 they weren't; the method couldn't raise an exception or return
183 anything other than a Boolean value. Users of Numeric Python often
184 found this model too weak and restrictive, because in the
185 number-crunching programs that numeric Python is used for, it would be
186 more useful to be able to perform elementwise comparisons of two
187 matrices, returning a matrix containing the results of a given
188 comparison for each element. If the two matrices are of different
189 sizes, then the compare has to be able to raise an exception to signal
190 the error.
192 In Python 2.1, rich comparisons were added in order to support this
193 need. Python classes can now individually overload each of the
194 \code{<}, \code{<=}, \code{>}, \code{>=}, \code{==}, and \code{!=}
195 operations. The new magic method names are:
197 \begin{tableii}{c|l}{code}{Operation}{Method name}
198 \lineii{<}{\method{__lt__}} \lineii{<=}{\method{__le__}}
199 \lineii{>}{\method{__gt__}} \lineii{>=}{\method{__ge__}}
200 \lineii{==}{\method{__eq__}} \lineii{!=}{\method{__ne__}}
201 \end{tableii}
203 (The magic methods are named after the corresponding Fortran operators
204 \code{.LT.}. \code{.LE.}, \&c. Numeric programmers are almost
205 certainly quite familiar with these names and will find them easy to
206 remember.)
208 Each of these magic methods is of the form \code{\var{method}(self,
209 other)}, where \code{self} will be the object on the left-hand side of
210 the operator, while \code{other} will be the object on the right-hand
211 side. For example, the expression \code{A < B} will cause
212 \code{A.__lt__(B)} to be called.
214 Each of these magic methods can return anything at all: a Boolean, a
215 matrix, a list, or any other Python object. Alternatively they can
216 raise an exception if the comparison is impossible, inconsistent, or
217 otherwise meaningless.
219 The built-in \function{cmp(A,B)} function can use the rich comparison
220 machinery, and now accepts an optional argument specifying which
221 comparison operation to use; this is given as one of the strings
222 \code{"<"}, \code{"<="}, \code{">"}, \code{">="}, \code{"=="}, or
223 \code{"!="}. If called without the optional third argument,
224 \function{cmp()} will only return -1, 0, or +1 as in previous versions
225 of Python; otherwise it will call the appropriate method and can
226 return any Python object.
228 There are also corresponding changes of interest to C programmers;
229 there's a new slot \code{tp_richcmp} in type objects and an API for
230 performing a given rich comparison. I won't cover the C API here, but
231 will refer you to PEP 207, or to 2.1's C API documentation, for the
232 full list of related functions.
234 \begin{seealso}
236 \seepep{207}{Rich Comparisions}{Written by Guido van Rossum, heavily
237 based on earlier work by David Ascher, and implemented by Guido van
238 Rossum.}
240 \end{seealso}
242 %======================================================================
243 \section{PEP 230: Warning Framework}
245 Over its 10 years of existence, Python has accumulated a certain
246 number of obsolete modules and features along the way. It's difficult
247 to know when a feature is safe to remove, since there's no way of
248 knowing how much code uses it --- perhaps no programs depend on the
249 feature, or perhaps many do. To enable removing old features in a
250 more structured way, a warning framework was added. When the Python
251 developers want to get rid of a feature, it will first trigger a
252 warning in the next version of Python. The following Python version
253 can then drop the feature, and users will have had a full release
254 cycle to remove uses of the old feature.
256 Python 2.1 adds the warning framework to be used in this scheme. It
257 adds a \module{warnings} module that provide functions to issue
258 warnings, and to filter out warnings that you don't want to be
259 displayed. Third-party modules can also use this framework to
260 deprecate old features that they no longer wish to support.
262 For example, in Python 2.1 the \module{regex} module is deprecated, so
263 importing it causes a warning to be printed:
265 \begin{verbatim}
266 >>> import regex
267 __main__:1: DeprecationWarning: the regex module
268 is deprecated; please use the re module
270 \end{verbatim}
272 Warnings can be issued by calling the \function{warnings.warn}
273 function:
275 \begin{verbatim}
276 warnings.warn("feature X no longer supported")
277 \end{verbatim}
279 The first parameter is the warning message; an additional optional
280 parameters can be used to specify a particular warning category.
282 Filters can be added to disable certain warnings; a regular expression
283 pattern can be applied to the message or to the module name in order
284 to suppress a warning. For example, you may have a program that uses
285 the \module{regex} module and not want to spare the time to convert it
286 to use the \module{re} module right now. The warning can be
287 suppressed by calling
289 \begin{verbatim}
290 import warnings
291 warnings.filterwarnings(action = 'ignore',
292 message='.*regex module is deprecated',
293 category=DeprecationWarning,
294 module = '__main__')
295 \end{verbatim}
297 This adds a filter that will apply only to warnings of the class
298 \class{DeprecationWarning} triggered in the \module{__main__} module,
299 and applies a regular expression to only match the message about the
300 \module{regex} module being deprecated, and will cause such warnings
301 to be ignored. Warnings can also be printed only once, printed every
302 time the offending code is executed, or turned into exceptions that
303 will cause the program to stop (unless the exceptions are caught in
304 the usual way, of course).
306 Functions were also added to Python's C API for issuing warnings;
307 refer to PEP 230 or to Python's API documentation for the details.
309 \begin{seealso}
311 \seepep{5}{Guidelines for Language Evolution}{Written
312 by Paul Prescod, to specify procedures to be followed when removing
313 old features from Python. The policy described in this PEP hasn't
314 been officially adopted, but the eventual policy probably won't be too
315 different from Prescod's proposal.}
317 \seepep{230}{Warning Framework}{Written and implemented by Guido van
318 Rossum.}
320 \end{seealso}
322 %======================================================================
323 \section{PEP 229: New Build System}
325 When compiling Python, the user had to go in and edit the
326 \file{Modules/Setup} file in order to enable various additional
327 modules; the default set is relatively small and limited to modules
328 that compile on most \UNIX{} platforms. This means that on \Unix{}
329 platforms with many more features, most notably Linux, Python
330 installations often don't contain all useful modules they could.
332 Python 2.0 added the Distutils, a set of modules for distributing and
333 installing extensions. In Python 2.1, the Distutils are used to
334 compile much of the standard library of extension modules,
335 autodetecting which ones are supported on the current machine. It's
336 hoped that this will make Python installations easier and more
337 featureful.
339 Instead of having to edit the \file{Modules/Setup} file in order to
340 enable modules, a \file{setup.py} script in the top directory of the
341 Python source distribution is run at build time, and attempts to
342 discover which modules can be enabled by examining the modules and
343 header files on the system. If a module is configured in
344 \file{Modules/Setup}, the \file{setup.py} script won't attempt to
345 compile that module and will defer to the \file{Modules/Setup} file's
346 contents. This provides a way to specific any strange command-line
347 flags or libraries that are required for a specific platform.
349 In another far-reaching change to the build mechanism, Neil
350 Schemenauer restructured things so Python now uses a single makefile
351 that isn't recursive, instead of makefiles in the top directory and in
352 each of the \file{Python/}, \file{Parser/}, \file{Objects/}, and
353 \file{Modules/} subdirectories. This makes building Python faster
354 and also makes hacking the Makefiles clearer and simpler.
356 \begin{seealso}
358 \seepep{229}{Using Distutils to Build Python}{Written
359 and implemented by A.M. Kuchling.}
361 \end{seealso}
363 %======================================================================
364 \section{PEP 205: Weak References}
366 Weak references, available through the \module{weakref} module, are a
367 minor but useful new data type in the Python programmer's toolbox.
369 Storing a reference to an object (say, in a dictionary or a list) has
370 the side effect of keeping that object alive forever. There are a few
371 specific cases where this behaviour is undesirable, object caches
372 being the most common one, and another being circular references in
373 data structures such as trees.
375 For example, consider a memoizing function that caches the results of
376 another function \function{f(\var{x})} by storing the function's
377 argument and its result in a dictionary:
379 \begin{verbatim}
380 _cache = {}
381 def memoize(x):
382 if _cache.has_key(x):
383 return _cache[x]
385 retval = f(x)
387 # Cache the returned object
388 _cache[x] = retval
390 return retval
391 \end{verbatim}
393 This version works for simple things such as integers, but it has a
394 side effect; the \code{_cache} dictionary holds a reference to the
395 return values, so they'll never be deallocated until the Python
396 process exits and cleans up This isn't very noticeable for integers,
397 but if \function{f()} returns an object, or a data structure that
398 takes up a lot of memory, this can be a problem.
400 Weak references provide a way to implement a cache that won't keep
401 objects alive beyond their time. If an object is only accessible
402 through weak references, the object will be deallocated and the weak
403 references will now indicate that the object it referred to no longer
404 exists. A weak reference to an object \var{obj} is created by calling
405 \code{wr = weakref.ref(\var{obj})}. The object being referred to is
406 returned by calling the weak reference as if it were a function:
407 \code{wr()}. It will return the referenced object, or \code{None} if
408 the object no longer exists.
410 This makes it possible to write a \function{memoize()} function whose
411 cache doesn't keep objects alive, by storing weak references in the
412 cache.
414 \begin{verbatim}
415 _cache = {}
416 def memoize(x):
417 if _cache.has_key(x):
418 obj = _cache[x]()
419 # If weak reference object still exists,
420 # return it
421 if obj is not None: return obj
423 retval = f(x)
425 # Cache a weak reference
426 _cache[x] = weakref.ref(retval)
428 return retval
429 \end{verbatim}
431 The \module{weakref} module also allows creating proxy objects which
432 behave like weak references --- an object referenced only by proxy
433 objects is deallocated -- but instead of requiring an explicit call to
434 retrieve the object, the proxy transparently forwards all operations
435 to the object as long as the object still exists. If the object is
436 deallocated, attempting to use a proxy will cause a
437 \exception{weakref.ReferenceError} exception to be raised.
439 \begin{verbatim}
440 proxy = weakref.proxy(obj)
441 proxy.attr # Equivalent to obj.attr
442 proxy.meth() # Equivalent to obj.meth()
443 del obj
444 proxy.attr # raises weakref.ReferenceError
445 \end{verbatim}
447 \begin{seealso}
449 \seepep{205}{Weak References}{Written and implemented by
450 Fred~L. Drake,~Jr.}
452 \end{seealso}
454 %======================================================================
455 \section{PEP 232: Function Attributes}
457 In Python 2.1, functions can now have arbitrary information attached
458 to them. People were often using docstrings to hold information about
459 functions and methods, because the \code{__doc__} attribute was the
460 only way of attaching any information to a function. For example, in
461 the Zope Web application server, functions are marked as safe for
462 public access by having a docstring, and in John Aycock's SPARK
463 parsing framework, docstrings hold parts of the BNF grammar to be
464 parsed. This overloading is unfortunate, since docstrings are really
465 intended to hold a function's documentation; for example, it means you
466 can't properly document functions intended for private use in Zope.
468 Arbitrary attributes can now be set and retrieved on functions using the
469 regular Python syntax:
471 \begin{verbatim}
472 def f(): pass
474 f.publish = 1
475 f.secure = 1
476 f.grammar = "A ::= B (C D)*"
477 \end{verbatim}
479 The dictionary containing attributes can be accessed as the function's
480 \member{__dict__}. Unlike the \member{__dict__} attribute of class
481 instances, in functions you can actually assign a new dictionary to
482 \member{__dict__}, though the new value is restricted to a regular
483 Python dictionary; you \emph{can't} be tricky and set it to a
484 \class{UserDict} instance, or any other random object that behaves
485 like a mapping.
487 \begin{seealso}
489 \seepep{232}{Function Attributes}{Written and implemented by Barry
490 Warsaw.}
492 \end{seealso}
495 %======================================================================
497 \section{PEP 235: Importing Modules on Case-Insensitive Platforms}
499 Some operating systems have filesystems that are case-insensitive,
500 MacOS and Windows being the primary examples; on these systems, it's
501 impossible to distinguish the filenames \samp{FILE.PY} and
502 \samp{file.py}, even though they do store the file's name
503 in its original case (they're case-preserving, too).
505 In Python 2.1, the \keyword{import} statement will work to simulate
506 case-sensitivity on case-insensitive platforms. Python will now
507 search for the first case-sensitive match by default, raising an
508 \exception{ImportError} if no such file is found, so \code{import file}
509 will not import a module named \samp{FILE.PY}. Case-insensitive
510 matching can be requested by setting the \envvar{PYTHONCASEOK} environment
511 variable before starting the Python interpreter.
513 %======================================================================
514 \section{PEP 217: Interactive Display Hook}
516 When using the Python interpreter interactively, the output of
517 commands is displayed using the built-in \function{repr()} function.
518 In Python 2.1, the variable \function{sys.displayhook} can be set to a
519 callable object which will be called instead of \function{repr()}.
520 For example, you can set it to a special pretty-printing function:
522 \begin{verbatim}
523 >>> # Create a recursive data structure
524 ... L = [1,2,3]
525 >>> L.append(L)
526 >>> L # Show Python's default output
527 [1, 2, 3, [...]]
528 >>> # Use pprint.pprint() as the display function
529 ... import sys, pprint
530 >>> sys.displayhook = pprint.pprint
531 >>> L
532 [1, 2, 3, <Recursion on list with id=135143996>]
534 \end{verbatim}
536 \begin{seealso}
538 \seepep{217}{Display Hook for Interactive Use}{Written and implemented
539 by Moshe Zadka.}
541 \end{seealso}
543 %======================================================================
544 \section{PEP 208: New Coercion Model}
546 How numeric coercion is done at the C level was significantly
547 modified. This will only affect the authors of C extensions to
548 Python, allowing them more flexibility in writing extension types that
549 support numeric operations.
551 Extension types can now set the type flag \code{Py_TPFLAGS_CHECKTYPES}
552 in their \code{PyTypeObject} structure to indicate that they support
553 the new coercion model. In such extension types, the numeric slot
554 functions can no longer assume that they'll be passed two arguments of
555 the same type; instead they may be passed two arguments of differing
556 types, and can then perform their own internal coercion. If the slot
557 function is passed a type it can't handle, it can indicate the failure
558 by returning a reference to the \code{Py_NotImplemented} singleton
559 value. The numeric functions of the other type will then be tried,
560 and perhaps they can handle the operation; if the other type also
561 returns \code{Py_NotImplemented}, then a \exception{TypeError} will be
562 raised. Numeric methods written in Python can also return
563 \code{Py_NotImplemented}, causing the interpreter to act as if the
564 method did not exist (perhaps raising a \exception{TypeError}, perhaps
565 trying another object's numeric methods).
567 \begin{seealso}
569 \seepep{208}{Reworking the Coercion Model}{Written and implemented by
570 Neil Schemenauer, heavily based upon earlier work by Marc-Andr\'e
571 Lemburg. Read this to understand the fine points of how numeric
572 operations will now be processed at the C level.}
574 \end{seealso}
576 %======================================================================
577 \section{PEP 241: Metadata in Python Packages}
579 A common complaint from Python users is that there's no single catalog
580 of all the Python modules in existence. T.~Middleton's Vaults of
581 Parnassus at \url{http://www.vex.net/parnassus/} are the largest
582 catalog of Python modules, but registering software at the Vaults is
583 optional, and many people don't bother.
585 As a first small step toward fixing the problem, Python software
586 packaged using the Distutils \command{sdist} command will include a
587 file named \file{PKG-INFO} containing information about the package
588 such as its name, version, and author (metadata, in cataloguing
589 terminology). PEP 241 contains the full list of fields that can be
590 present in the \file{PKG-INFO} file. As people began to package their
591 software using Python 2.1, more and more packages will include
592 metadata, making it possible to build automated cataloguing systems
593 and experiment with them. With the result experience, perhaps it'll
594 be possible to design a really good catalog and then build support for
595 it into Python 2.2. For example, the Distutils \command{sdist}
596 and \command{bdist_*} commands could support a \option{upload} option
597 that would automatically upload your package to a catalog server.
599 You can start creating packages containing \file{PKG-INFO} even if
600 you're not using Python 2.1, since a new release of the Distutils will
601 be made for users of earlier Python versions. Version 1.0.2 of the
602 Distutils includes the changes described in PEP 241, as well as
603 various bugfixes and enhancements. It will be available from
604 the Distutils SIG at \url{http://www.python.org/sigs/distutils-sig/}.
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.python.org/dev/summary/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}