blob: be289f10e34716f316a7e880394b92926c090ad2 [file] [log] [blame]
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +00001\documentclass{howto}
2
3% $Id$
4
5\title{What's New in Python 2.1}
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +00006\release{0.06}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +00007\author{A.M. Kuchling}
8\authoraddress{\email{amk1@bigfoot.com}}
9\begin{document}
10\maketitle\tableofcontents
11
12\section{Introduction}
13
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +000014{\large This document is a draft, and is subject to change until
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +000015the final version of Python 2.1 is released. Currently it is up to date
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +000016for Python 2.1 beta 1. Please send any comments, bug reports, or
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +000017questions, no matter how minor, to \email{amk1@bigfoot.com}. }
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +000018
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +000019It's that time again... time for a new Python release, version 2.1.
20One recent goal of the Python development team has been to accelerate
21the pace of new releases, with a new release coming every 6 to 9
22months. 2.1 is the first release to come out at this faster pace, with
23the first alpha appearing in January, 3 months after the final version
24of 2.0 was released.
25
26This article explains the new features in 2.1. While there aren't as
27many changes in 2.1 as there were in Python 2.0, there are still some
28pleasant surprises in store. 2.1 is the first release to be steered
29through the use of Python Enhancement Proposals, or PEPs, so most of
30the sizable changes have accompanying PEPs that provide more complete
31documentation and a design rationale for the change. This article
32doesn't attempt to document the new features completely, but simply
33provides an overview of the new features for Python programmers.
34Refer to the Python 2.1 documentation, or to the specific PEP, for
35more details about any new feature that particularly interests you.
36
37Currently 2.1 is available in an alpha release, but the release
38schedule calls for a beta release by late February 2001, and a final
39release in April 2001.
40
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +000041%======================================================================
42\section{PEP 227: Nested Scopes}
43
44The largest change in Python 2.1 is to Python's scoping rules. In
45Python 2.0, at any given time there are at most three namespaces used
46to look up variable names: local, module-level, and the built-in
47namespace. This often surprised people because it didn't match their
48intuitive expectations. For example, a nested recursive function
49definition doesn't work:
50
51\begin{verbatim}
52def f():
53 ...
54 def g(value):
55 ...
56 return g(value-1) + 1
57 ...
58\end{verbatim}
59
60The function \function{g()} will always raise a \exception{NameError}
61exception, because the binding of the name \samp{g} isn't in either
62its local namespace or in the module-level namespace. This isn't much
63of a problem in practice (how often do you recursively define interior
64functions like this?), but this also made using the \keyword{lambda}
65statement clumsier, and this was a problem in practice. In code which
66uses \keyword{lambda} you can often find local variables being copied
67by passing them as the default values of arguments.
68
69\begin{verbatim}
70def find(self, name):
71 "Return list of any entries equal to 'name'"
72 L = filter(lambda x, name=name: x == name,
73 self.list_attribute)
74 return L
75\end{verbatim}
76
77The readability of Python code written in a strongly functional style
78suffers greatly as a result.
79
80The most significant change to Python 2.1 is that static scoping has
81been added to the language to fix this problem. As a first effect,
82the \code{name=name} default argument is now unnecessary in the above
83example. Put simply, when a given variable name is not assigned a
84value within a function (by an assignment, or the \keyword{def},
85\keyword{class}, or \keyword{import} statements), references to the
86variable will be looked up in the local namespace of the enclosing
87scope. A more detailed explanation of the rules, and a dissection of
88the implementation, can be found in the PEP.
89
90This change may cause some compatibility problems for code where the
91same variable name is used both at the module level and as a local
92variable within a function that contains further function definitions.
93This seems rather unlikely though, since such code would have been
94pretty confusing to read in the first place.
95
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +000096One side effect of the change is that the \code{from \var{module}
97import *} and \keyword{exec} statements have been made illegal inside
98a function scope under certain conditions. The Python reference
99manual has said all along that \code{from \var{module} import *} is
100only legal at the top level of a module, but the CPython interpreter
101has never enforced this before. As part of the implementation of
102nested scopes, the compiler which turns Python source into bytecodes
103has to generate different code to access variables in a containing
104scope. \code{from \var{module} import *} and \keyword{exec} make it
105impossible for the compiler to figure this out, because they add names
106to the local namespace that are unknowable at compile time.
107Therefore, if a function contains function definitions or
108\keyword{lambda} expressions with free variables, the compiler will
109flag this by raising a \exception{SyntaxError} exception.
110
111To make the preceding explanation a bit clearer, here's an example:
112
113\begin{verbatim}
114x = 1
115def f():
116 # The next line is a syntax error
117 exec 'x=2'
118 def g():
119 return x
120\end{verbatim}
121
122Line 4 containing the \keyword{exec} statement is a syntax error,
123since \keyword{exec} would define a new local variable named \samp{x}
124whose value should be accessed by \function{g()}.
125
126This shouldn't be much of a limitation, since \keyword{exec} is rarely
127used in most Python code (and when it is used, it's often a sign of a
128poor design anyway).
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000129
130\begin{seealso}
131
132\seepep{227}{Statically Nested Scopes}{Written and implemented by
133Jeremy Hylton.}
134
135\end{seealso}
136
137
138%======================================================================
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000139\section{PEP 236: \module{__future__} Directives}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000140
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000141XXX
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000142
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000143%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000144\section{PEP 207: Rich Comparisons}
145
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000146In earlier versions, Python's support for implementing comparisons on
147user-defined classes and extension types was quite simple. Classes
148could implement a \method{__cmp__} method that was given two instances
149of a class, and could only return 0 if they were equal or +1 or -1 if
150they weren't; the method couldn't raise an exception or return
151anything other than a Boolean value. Users of Numeric Python often
152found this model too weak and restrictive, because in the
153number-crunching programs that numeric Python is used for, it would be
154more useful to be able to perform elementwise comparisons of two
155matrices, returning a matrix containing the results of a given
156comparison for each element. If the two matrices are of different
157sizes, then the compare has to be able to raise an exception to signal
158the error.
159
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000160In Python 2.1, rich comparisons were added in order to support this
161need. Python classes can now individually overload each of the
162\code{<}, \code{<=}, \code{>}, \code{>=}, \code{==}, and \code{!=}
163operations. The new magic method names are:
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000164
165\begin{tableii}{c|l}{code}{Operation}{Method name}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000166 \lineii{<}{\method{__lt__}} \lineii{<=}{\method{__le__}}
167 \lineii{>}{\method{__gt__}} \lineii{>=}{\method{__ge__}}
168 \lineii{==}{\method{__eq__}} \lineii{!=}{\method{__ne__}}
169 \end{tableii}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000170
171(The magic methods are named after the corresponding Fortran operators
172\code{.LT.}. \code{.LE.}, \&c. Numeric programmers are almost
173certainly quite familar with these names and will find them easy to
174remember.)
175
176Each of these magic methods is of the form \code{\var{method}(self,
177other)}, where \code{self} will be the object on the left-hand side of
178the operator, while \code{other} will be the object on the right-hand
179side. For example, the expression \code{A < B} will cause
180\code{A.__lt__(B)} to be called.
181
182Each of these magic methods can return anything at all: a Boolean, a
183matrix, a list, or any other Python object. Alternatively they can
184raise an exception if the comparison is impossible, inconsistent, or
185otherwise meaningless.
186
187The built-in \function{cmp(A,B)} function can use the rich comparison
188machinery, and now accepts an optional argument specifying which
189comparison operation to use; this is given as one of the strings
190\code{"<"}, \code{"<="}, \code{">"}, \code{">="}, \code{"=="}, or
191\code{"!="}. If called without the optional third argument,
192\function{cmp()} will only return -1, 0, or +1 as in previous versions
193of Python; otherwise it will call the appropriate method and can
194return any Python object.
195
196There are also corresponding changes of interest to C programmers;
197there's a new slot \code{tp_richcmp} in type objects and an API for
198performing a given rich comparison. I won't cover the C API here, but
Andrew M. Kuchlingbf140142001-02-28 22:10:07 +0000199will refer you to PEP 207, or to 2.1's C API documentation, for the
200full list of related functions.
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000201
202\begin{seealso}
203
204\seepep{207}{Rich Comparisions}{Written by Guido van Rossum, heavily
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000205based on earlier work by David Ascher, and implemented by Guido van
206Rossum.}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000207
208\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000209
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000210%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000211\section{PEP 230: Warning Framework}
212
213Over its 10 years of existence, Python has accumulated a certain
214number of obsolete modules and features along the way. It's difficult
215to know when a feature is safe to remove, since there's no way of
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000216knowing how much code uses it --- perhaps no programs depend on the
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000217feature, or perhaps many do. To enable removing old features in a
218more structured way, a warning framework was added. When the Python
219developers want to get rid of a feature, it will first trigger a
220warning in the next version of Python. The following Python version
221can then drop the feature, and users will have had a full release
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000222cycle to remove uses of the old feature.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000223
224Python 2.1 adds the warning framework to be used in this scheme. It
225adds a \module{warnings} module that provide functions to issue
226warnings, and to filter out warnings that you don't want to be
227displayed. Third-party modules can also use this framework to
228deprecate old features that they no longer wish to support.
229
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000230For example, in Python 2.1 the \module{regex} module is deprecated, so
231importing it causes a warning to be printed:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000232
233\begin{verbatim}
234>>> import regex
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000235__main__:1: DeprecationWarning: the regex module
236 is deprecated; please use the re module
237>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000238\end{verbatim}
239
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000240Warnings can be issued by calling the \function{warnings.warn}
241function:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000242
243\begin{verbatim}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000244warnings.warn("feature X no longer supported")
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000245\end{verbatim}
246
247The first parameter is the warning message; an additional optional
248parameters can be used to specify a particular warning category.
249
250Filters can be added to disable certain warnings; a regular expression
251pattern can be applied to the message or to the module name in order
252to suppress a warning. For example, you may have a program that uses
253the \module{regex} module and not want to spare the time to convert it
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000254to use the \module{re} module right now. The warning can be
255suppressed by calling
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000256
257\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000258import warnings
259warnings.filterwarnings(action = 'ignore',
260 message='.*regex module is deprecated',
261 category=DeprecationWarning,
262 module = '__main__')
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000263\end{verbatim}
264
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000265This adds a filter that will apply only to warnings of the class
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000266\class{DeprecationWarning} triggered in the \module{__main__} module,
267and applies a regular expression to only match the message about the
268\module{regex} module being deprecated, and will cause such warnings
269to be ignored. Warnings can also be printed only once, printed every
270time the offending code is executed, or turned into exceptions that
271will cause the program to stop (unless the exceptions are caught in
272the usual way, of course).
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000273
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000274Functions were also added to Python's C API for issuing warnings;
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000275refer to PEP 230 or to Python's API documentation for the details.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000276
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000277\begin{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000278
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000279\seepep{5}{Guidelines for Language Evolution}{Written
280by Paul Prescod, to specify procedures to be followed when removing
281old features from Python. The policy described in this PEP hasn't
282been officially adopted, but the eventual policy probably won't be too
283different from Prescod's proposal.}
284
285\seepep{230}{Warning Framework}{Written and implemented by Guido van
286Rossum.}
287
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000288\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000289
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000290%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000291\section{PEP 229: New Build System}
292
293When compiling Python, the user had to go in and edit the
294\file{Modules/Setup} file in order to enable various additional
295modules; the default set is relatively small and limited to modules
296that compile on most Unix platforms. This means that on Unix
297platforms with many more features, most notably Linux, Python
298installations often don't contain all useful modules they could.
299
300Python 2.0 added the Distutils, a set of modules for distributing and
301installing extensions. In Python 2.1, the Distutils are used to
302compile much of the standard library of extension modules,
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000303autodetecting which ones are supported on the current machine. It's
304hoped that this will make Python installations easier and more
305featureful.
306
307Instead of having to edit the \file{Modules/Setup} file in order to
308enable modules, a \file{setup.py} script in the top directory of the
309Python source distribution is run at build time, and attempts to
310discover which modules can be enabled by examining the modules and
311header files on the system. In 2.1alpha1, there's very little you can
312do to change \file{setup.py}'s behaviour, or to discover why a given
313module isn't compiled. If you run into problems in 2.1alpha1, please
314report them, and be prepared to dive into \file{setup.py} in order to
315fix autodetection of a given library on your system. In the alpha2
316release I plan to add ways to have more control over what the script
317does (probably command-line arguments to \file{configure} or to
318\file{setup.py}).
319
320If it turns out to be impossible to make autodetection work reliably,
321it's possible that this change may become an optional build method
322instead of the default, or it may even be backed out completely.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000323
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000324In another far-reaching change to the build mechanism, Neil
325Schemenauer restructured things so Python now uses a single makefile
326that isn't recursive, instead of makefiles in the top directory and in
327each of the Python/, Parser/, Objects/, and Modules/ subdirectories.
328This makes building Python faster, and also makes the build process
329clearer and simpler.
330
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000331\begin{seealso}
332
333\seepep{229}{Using Distutils to Build Python}{Written
334and implemented by A.M. Kuchling.}
335
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000336\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000337
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000338%======================================================================
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000339\section{PEP 205: Weak References}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000340
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000341Weak references, available through the \module{weakref} module, are a
342minor but useful new data type in the Python programmer's toolbox.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000343
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000344Storing a reference to an object (say, in a dictionary or a list) has
345the side effect of keeping that object alive forever. There are a few
346specific cases where this behaviour is undesirable, object caches
347being the most common one, and another being circular references in
348data structures such as trees.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000349
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000350For example, consider a memoizing function that caches the results of
351another function \function{f(\var{x})} by storing the function's
352argument and its result in a dictionary:
353
354\begin{verbatim}
355_cache = {}
356def memoize(x):
357 if _cache.has_key(x):
358 return _cache[x]
359
360 retval = f(x)
361
362 # Cache the returned object
363 _cache[x] = retval
364
365 return retval
366\end{verbatim}
367
368This version works for simple things such as integers, but it has a
369side effect; the \code{_cache} dictionary holds a reference to the
370return values, so they'll never be deallocated until the Python
371process exits and cleans up This isn't very noticeable for integers,
372but if \function{f()} returns an object, or a data structure that
373takes up a lot of memory, this can be a problem.
374
375Weak references provide a way to implement a cache that won't keep
376objects alive beyond their time. If an object is only accessible
377through weak references, the object will be deallocated and the weak
378references will now indicate that the object it referred to no longer
379exists. A weak reference to an object \var{obj} is created by calling
380\code{wr = weakref.ref(\var{obj})}. The object being referred to is
381returned by calling the weak reference as if it were a function:
382\code{wr()}. It will return the referenced object, or \code{None} if
383the object no longer exists.
384
385This makes it possible to write a \function{memoize()} function whose
386cache doesn't keep objects alive, by storing weak references in the
387cache.
388
389\begin{verbatim}
390_cache = {}
391def memoize(x):
392 if _cache.has_key(x):
393 obj = _cache[x]()
394 # If weak reference object still exists,
395 # return it
396 if obj is not None: return obj
397
398 retval = f(x)
399
400 # Cache a weak reference
401 _cache[x] = weakref.ref(retval)
402
403 return retval
404\end{verbatim}
405
406The \module{weakref} module also allows creating proxy objects which
407behave like weak references --- an object referenced only by proxy
408objects is deallocated -- but instead of requiring an explicit call to
409retrieve the object, the proxy transparently forwards all operations
410to the object as long as the object still exists. If the object is
411deallocated, attempting to use a proxy will cause a
412\exception{weakref.ReferenceError} exception to be raised.
413
414\begin{verbatim}
415proxy = weakref.proxy(obj)
416proxy.attr # Equivalent to obj.attr
417proxy.meth() # Equivalent to obj.meth()
418del obj
419proxy.attr # raises weakref.ReferenceError
420\end{verbatim}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000421
422\begin{seealso}
423
424\seepep{205}{Weak References}{Written and implemented by
425Fred~L. Drake,~Jr.}
426
427\end{seealso}
428
429%======================================================================
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000430\section{PEP 235: Case-Insensitive Platforms and \keyword{import}}
431
432XXX
433
434%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000435\section{PEP 217: Interactive Display Hook}
436
437When using the Python interpreter interactively, the output of
438commands is displayed using the built-in \function{repr()} function.
439In Python 2.1, the variable \module{sys.displayhook} can be set to a
440callable object which will be called instead of \function{repr()}.
441For example, you can set it to a special pretty-printing function:
442
443\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000444>>> # Create a recursive data structure
445... L = [1,2,3]
446>>> L.append(L)
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000447>>> L # Show Python's default output
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000448[1, 2, 3, [...]]
449>>> # Use pprint.pprint() as the display function
450... import sys, pprint
451>>> sys.displayhook = pprint.pprint
452>>> L
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000453[1, 2, 3, <Recursion on list with id=135143996>]
454>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000455\end{verbatim}
456
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000457\begin{seealso}
458
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000459\seepep{217}{Display Hook for Interactive Use}{Written and implemented
460by Moshe Zadka.}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000461
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000462\end{seealso}
463
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000464%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000465\section{PEP 208: New Coercion Model}
466
467How numeric coercion is done at the C level was significantly
468modified. This will only affect the authors of C extensions to
469Python, allowing them more flexibility in writing extension types that
470support numeric operations.
471
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000472Extension types can now set the type flag \code{Py_TPFLAGS_CHECKTYPES}
473in their \code{PyTypeObject} structure to indicate that they support
474the new coercion model. In such extension types, the numeric slot
475functions can no longer assume that they'll be passed two arguments of
476the same type; instead they may be passed two arguments of differing
477types, and can then perform their own internal coercion. If the slot
478function is passed a type it can't handle, it can indicate the failure
479by returning a reference to the \code{Py_NotImplemented} singleton
480value. The numeric functions of the other type will then be tried,
481and perhaps they can handle the operation; if the other type also
482returns \code{Py_NotImplemented}, then a \exception{TypeError} will be
483raised. Numeric methods written in Python can also return
484\code{Py_NotImplemented}, causing the interpreter to act as if the
485method did not exist (perhaps raising a \exception{TypeError}, perhaps
486trying another object's numeric methods).
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000487
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000488\begin{seealso}
489
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000490\seepep{208}{Reworking the Coercion Model}{Written and implemented by
491Neil Schemenauer, heavily based upon earlier work by Marc-Andr\'e
492Lemburg. Read this to understand the fine points of how numeric
493operations will now be processed at the C level.}
494
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000495\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000496
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000497%======================================================================
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000498\section{New and Improved Modules}
499
500\begin{itemize}
501
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000502\item Ka-Ping Yee contributed two new modules: \module{inspect.py}, a module for
503getting information about live Python code, and \module{pydoc.py}, a
504module for interactively converting docstrings to HTML or text.
505As a bonus, \file{Tools/scripts/pydoc}, which is now automatically
506installed, uses \module{pydoc.py} to display documentation given a Python module, package, or class name. For example,
507\samp{pydoc xml.dom} displays the following:
508
509\begin{verbatim}
510Python Library Documentation: package xml.dom in xml
511
512NAME
513 xml.dom - W3C Document Object Model implementation for Python.
514
515FILE
516 /usr/local/lib/python2.1/xml/dom/__init__.pyc
517
518DESCRIPTION
519 The Python mapping of the Document Object Model is documented in the
520 Python Library Reference in the section on the xml.dom package.
521
522 This package contains the following modules:
523 ...
524\end{verbatim}
525
526\file{pydoc} quickly becomes addictive; try it out!
527
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000528\item The \module{doctest} module provides a testing framework based
529on running embedded examples in docstrings and comparing the results
530against the expected output. Contributed by Tim Peters.
531
532\item The \module{difflib} module contains a class,
533\class{SequenceMatcher}, which compares two sequences and computes the
534changes required to transform one sequence into the other. For
535example, this module can be used to write a tool similar to the Unix
536\program{diff} program, and in fact the sample program
537\file{Tools/scripts/ndiff.py} demonstrates how to write such a script.
538
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000539\item \module{curses.panel}, a wrapper for the panel library, part of
540ncurses and of SYSV curses, was contributed by Thomas Gellekum. The
541panel library provides windows with the additional feature of depth.
542Windows can be moved higher or lower in the depth ordering, and the
543panel library figures out where panels overlap and which sections are
544visible.
545
546\item The PyXML package has gone through a few releases since Python
5472.0, and Python 2.1 includes an updated version of the \module{xml}
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000548package. Some of the noteworthy changes include support for Expat 1.2
549and later versions, the ability for Expat parsers to handle files in
550any encoding supported by Python, and various bugfixes for SAX, DOM,
551and the \module{minidom} module.
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000552
553\item Various functions in the \module{time} module, such as
554\function{asctime()} and \function{localtime()}, require a floating
555point argument containing the time in seconds since the epoch. The
556most common use of these functions is to work with the current time,
557so the floating point argument has been made optional; when a value
558isn't provided, the current time will be used. For example, log file
559entries usually need a string containing the current time; in Python
5602.1, \code{time.asctime()} can be used, instead of the lengthier
561\code{time.asctime(time.localtime(time.time()))} that was previously
562required.
563
564This change was proposed and implemented by Thomas Wouters.
565
566\item The \module{ftplib} module now defaults to retrieving files in
567passive mode, because passive mode is more likely to work from behind
568a firewall. This request came from the Debian bug tracking system,
569since other Debian packages use \module{ftplib} to retrieve files and
570then don't work from behind a firewall. It's deemed unlikely that
571this will cause problems for anyone, because Netscape defaults to
572passive mode and few people complain, but if passive mode is
573unsuitable for your application or network setup, call
574\method{set_pasv(0)} on FTP objects to disable passive mode.
575
576\item Support for raw socket access has been added to the
577\module{socket} module, contributed by Grant Edwards.
578
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000579\item A new implementation-dependent function, \function{sys._getframe(\optional{depth})},
580has been added to return a given frame object from the current call stack.
581\function{sys._getframe()} returns the frame at the top of the call stack;
582if the optional integer argument \var{depth} is supplied, the function returns the frame
583that is \var{depth} calls below the top of the stack. For example, \code{sys._getframe(1)}
584returns the caller's frame object.
585
586This function is only present in CPython, not in Jython or the .NET
587implementation. Use it for debugging, and resist the temptation to
588put it into production code.
589
590
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000591
592\end{itemize}
593
594%======================================================================
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000595\section{Other Changes and Fixes}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000596
597There were relatively few smaller changes made in Python 2.1 due to
598the shorter release cycle. A search through the CVS change logs turns
599up 57 patches applied, and 86 bugs fixed; both figures are likely to
600be underestimates. Some of the more notable changes are:
601
602\begin{itemize}
603
Andrew M. Kuchlingbf140142001-02-28 22:10:07 +0000604
605\item A specialized object allocator is now optionally available, that
606should be faster than the system \function{malloc()} and have less
607memory overhead. The allocator uses C's \function{malloc()} function
608to get large pools of memory, and then fulfills smaller memory
609requests from these pools. It can be enabled by providing the
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000610"--with-pymalloc" option to the \file{configure} script; see
611\file{Objects/obmalloc.c} for the implementation details.
Andrew M. Kuchlingbf140142001-02-28 22:10:07 +0000612Contributed by Vladimir Marangozov.
613
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000614\item The speed of line-oriented file I/O has been improved because
615people often complain about its lack of speed, and because it's often
616been used as a na\"ive benchmark. The \method{readline()} method of
617file objects has therefore been rewritten to be much faster. The
618exact amount of the speedup will vary from platform to platform
619depending on how slow the C library's \function{getc()} was, but is
620around 66\%, and potentially much faster on some particular operating
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000621systems. Tim Peters did much of the benchmarking and coding for this
622change, motivated by a discussion in comp.lang.python.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000623
624A new module and method for file objects was also added, contributed
625by Jeff Epler. The new method, \method{xreadlines()}, is similar to
626the existing \function{xrange()} built-in. \function{xreadlines()}
627returns an opaque sequence object that only supports being iterated
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000628over, reading a line on every iteration but not reading the entire
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000629file into memory as the existing \method{readlines()} method does.
630You'd use it like this:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000631
632\begin{verbatim}
633for line in sys.stdin.xreadlines():
634 # ... do something for each line ...
635 ...
636\end{verbatim}
637
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000638For a fuller discussion of the line I/O changes, see the python-dev
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000639summary for January 1-15, 2001.
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000640
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000641\item A new method, \method{popitem()}, was added to dictionaries to
642enable destructively iterating through the contents of a dictionary;
643this can be faster for large dictionaries because .
644\code{D.popitem()} removes a random \code{(\var{key}, \var{value})}
645pair from the dictionary and returns it as a 2-tuple. This was
646implemented mostly by Tim Peters and Guido van Rossum, after a
647suggestion and preliminary patch by Moshe Zadka.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000648
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000649\item Modules can now control which names are imported when \code{from
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000650\var{module} import *} is used, by defining an \code{__all__}
651attribute containing a list of names that will be imported. One
652common complaint is that if the module imports other modules such as
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000653\module{sys} or \module{string}, \code{from \var{module} import *}
654will add them to the importing module's namespace. To fix this,
655simply list the public names in \code{__all__}:
656
657\begin{verbatim}
658# List public names
659__all__ = ['Database', 'open']
660\end{verbatim}
661
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000662A stricter version of this patch was first suggested and implemented
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000663by Ben Wolfson, but after some python-dev discussion, a weaker final
664version was checked in.
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000665
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000666\item Applying \function{repr()} to strings previously used octal
667escapes for non-printable characters; for example, a newline was
668\code{'\e 012'}. This was a vestigial trace of Python's C ancestry, but
669today octal is of very little practical use. Ka-Ping Yee suggested
670using hex escapes instead of octal ones, and using the \code{\e n},
671\code{\e t}, \code{\e r} escapes for the appropriate characters, and
672implemented this new formatting.
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000673
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000674\item Syntax errors detected at compile-time can now raise exceptions
675containing the filename and line number of the error, a pleasant side
676effect of the compiler reorganization done by Jeremy Hylton.
677
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000678\item C extensions which import other modules have been changed to use
679\function{PyImport_ImportModule()}, which means that they will use any
680import hooks that have been installed. This is also encouraged for
681third-party extensions that need to import some other module from C
682code.
683
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000684\item The size of the Unicode character database was shrunk by another
685340K thanks to Fredrik Lundh.
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000686
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000687\end{itemize}
688
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000689And there's the usual list of minor bugfixes, minor memory leaks,
690docstring edits, and other tweaks, too lengthy to be worth itemizing;
691see the CVS logs for the full details if you want them.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000692
693
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000694%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000695\section{Acknowledgements}
696
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000697The author would like to thank the following people for offering
698suggestions on various drafts of this article: Graeme Cross, David
699Goodger, Jay Graves, Michael Hudson, Marc-Andr\'e Lemburg, Fredrik
700Lundh, Neil Schemenauer, Thomas Wouters.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000701
702\end{document}