blob: 9127ffeff0d58b74ec44771319679521489c8892 [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
16for Python 2.1 alpha 2. Please send any comments, bug reports, or
17questions, 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. Kuchling90cecee2001-01-22 04:02:09 +0000139\section{PEP 232: Function Attributes}
140
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000141In Python 2.1, functions can now have arbitrary information attached
142to them. People were often using docstrings to hold information about
143functions and methods, because the \code{__doc__} attribute was the
144only way of attaching any information to a function. For example, in
145the Zope Web application server, functions are marked as safe for
146public access by having a docstring, and in John Aycock's SPARK
147parsing framework, docstrings hold parts of the BNF grammar to be
148parsed. This overloading is unfortunate, since docstrings are really
149intended to hold a function's documentation, and it means you can't
150properly document functions intended for private use in Zope.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000151
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000152Attributes can now be set and retrieved on functions, using the
153regular Python syntax:
154
155\begin{verbatim}
156def f(): pass
157
158f.publish = 1
159f.secure = 1
160f.grammar = "A ::= B (C D)*"
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000161\end{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000162
163The dictionary containing attributes can be accessed as
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000164\member{__dict__}. Unlike the \member{__dict__} attribute of class
165instances, in functions you can actually assign a new dictionary to
166\member{__dict__}, though the new value is restricted to a regular
167Python dictionary; you can't be tricky and set it to a
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000168\class{UserDict} instance, a DBM file, or any other random mapping
169object.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000170
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000171\begin{seealso}
172
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000173\seepep{232}{Function Attributes}{Written and implemented by Barry
174Warsaw.}
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000175
176\end{seealso}
177
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000178%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000179\section{PEP 207: Rich Comparisons}
180
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000181In earlier versions, Python's support for implementing comparisons on
182user-defined classes and extension types was quite simple. Classes
183could implement a \method{__cmp__} method that was given two instances
184of a class, and could only return 0 if they were equal or +1 or -1 if
185they weren't; the method couldn't raise an exception or return
186anything other than a Boolean value. Users of Numeric Python often
187found this model too weak and restrictive, because in the
188number-crunching programs that numeric Python is used for, it would be
189more useful to be able to perform elementwise comparisons of two
190matrices, returning a matrix containing the results of a given
191comparison for each element. If the two matrices are of different
192sizes, then the compare has to be able to raise an exception to signal
193the error.
194
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000195In Python 2.1, rich comparisons were added in order to support this
196need. Python classes can now individually overload each of the
197\code{<}, \code{<=}, \code{>}, \code{>=}, \code{==}, and \code{!=}
198operations. The new magic method names are:
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000199
200\begin{tableii}{c|l}{code}{Operation}{Method name}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000201 \lineii{<}{\method{__lt__}} \lineii{<=}{\method{__le__}}
202 \lineii{>}{\method{__gt__}} \lineii{>=}{\method{__ge__}}
203 \lineii{==}{\method{__eq__}} \lineii{!=}{\method{__ne__}}
204 \end{tableii}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000205
206(The magic methods are named after the corresponding Fortran operators
207\code{.LT.}. \code{.LE.}, \&c. Numeric programmers are almost
208certainly quite familar with these names and will find them easy to
209remember.)
210
211Each of these magic methods is of the form \code{\var{method}(self,
212other)}, where \code{self} will be the object on the left-hand side of
213the operator, while \code{other} will be the object on the right-hand
214side. For example, the expression \code{A < B} will cause
215\code{A.__lt__(B)} to be called.
216
217Each of these magic methods can return anything at all: a Boolean, a
218matrix, a list, or any other Python object. Alternatively they can
219raise an exception if the comparison is impossible, inconsistent, or
220otherwise meaningless.
221
222The built-in \function{cmp(A,B)} function can use the rich comparison
223machinery, and now accepts an optional argument specifying which
224comparison operation to use; this is given as one of the strings
225\code{"<"}, \code{"<="}, \code{">"}, \code{">="}, \code{"=="}, or
226\code{"!="}. If called without the optional third argument,
227\function{cmp()} will only return -1, 0, or +1 as in previous versions
228of Python; otherwise it will call the appropriate method and can
229return any Python object.
230
231There are also corresponding changes of interest to C programmers;
232there's a new slot \code{tp_richcmp} in type objects and an API for
233performing a given rich comparison. I won't cover the C API here, but
Andrew M. Kuchlingbf140142001-02-28 22:10:07 +0000234will refer you to PEP 207, or to 2.1's C API documentation, for the
235full list of related functions.
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000236
237\begin{seealso}
238
239\seepep{207}{Rich Comparisions}{Written by Guido van Rossum, heavily
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000240based on earlier work by David Ascher, and implemented by Guido van
241Rossum.}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000242
243\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000244
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000245%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000246\section{PEP 230: Warning Framework}
247
248Over its 10 years of existence, Python has accumulated a certain
249number of obsolete modules and features along the way. It's difficult
250to know when a feature is safe to remove, since there's no way of
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000251knowing how much code uses it --- perhaps no programs depend on the
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000252feature, or perhaps many do. To enable removing old features in a
253more structured way, a warning framework was added. When the Python
254developers want to get rid of a feature, it will first trigger a
255warning in the next version of Python. The following Python version
256can then drop the feature, and users will have had a full release
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000257cycle to remove uses of the old feature.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000258
259Python 2.1 adds the warning framework to be used in this scheme. It
260adds a \module{warnings} module that provide functions to issue
261warnings, and to filter out warnings that you don't want to be
262displayed. Third-party modules can also use this framework to
263deprecate old features that they no longer wish to support.
264
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000265For example, in Python 2.1 the \module{regex} module is deprecated, so
266importing it causes a warning to be printed:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000267
268\begin{verbatim}
269>>> import regex
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000270__main__:1: DeprecationWarning: the regex module
271 is deprecated; please use the re module
272>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000273\end{verbatim}
274
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000275Warnings can be issued by calling the \function{warnings.warn}
276function:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000277
278\begin{verbatim}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000279warnings.warn("feature X no longer supported")
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000280\end{verbatim}
281
282The first parameter is the warning message; an additional optional
283parameters can be used to specify a particular warning category.
284
285Filters can be added to disable certain warnings; a regular expression
286pattern can be applied to the message or to the module name in order
287to suppress a warning. For example, you may have a program that uses
288the \module{regex} module and not want to spare the time to convert it
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000289to use the \module{re} module right now. The warning can be
290suppressed by calling
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000291
292\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000293import warnings
294warnings.filterwarnings(action = 'ignore',
295 message='.*regex module is deprecated',
296 category=DeprecationWarning,
297 module = '__main__')
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000298\end{verbatim}
299
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000300This adds a filter that will apply only to warnings of the class
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000301\class{DeprecationWarning} triggered in the \module{__main__} module,
302and applies a regular expression to only match the message about the
303\module{regex} module being deprecated, and will cause such warnings
304to be ignored. Warnings can also be printed only once, printed every
305time the offending code is executed, or turned into exceptions that
306will cause the program to stop (unless the exceptions are caught in
307the usual way, of course).
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000308
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000309Functions were also added to Python's C API for issuing warnings;
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000310refer to PEP 230 or to Python's API documentation for the details.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000311
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000312\begin{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000313
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000314\seepep{5}{Guidelines for Language Evolution}{Written
315by Paul Prescod, to specify procedures to be followed when removing
316old features from Python. The policy described in this PEP hasn't
317been officially adopted, but the eventual policy probably won't be too
318different from Prescod's proposal.}
319
320\seepep{230}{Warning Framework}{Written and implemented by Guido van
321Rossum.}
322
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000323\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000324
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000325%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000326\section{PEP 229: New Build System}
327
328When compiling Python, the user had to go in and edit the
329\file{Modules/Setup} file in order to enable various additional
330modules; the default set is relatively small and limited to modules
331that compile on most Unix platforms. This means that on Unix
332platforms with many more features, most notably Linux, Python
333installations often don't contain all useful modules they could.
334
335Python 2.0 added the Distutils, a set of modules for distributing and
336installing extensions. In Python 2.1, the Distutils are used to
337compile much of the standard library of extension modules,
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000338autodetecting which ones are supported on the current machine. It's
339hoped that this will make Python installations easier and more
340featureful.
341
342Instead of having to edit the \file{Modules/Setup} file in order to
343enable modules, a \file{setup.py} script in the top directory of the
344Python source distribution is run at build time, and attempts to
345discover which modules can be enabled by examining the modules and
346header files on the system. In 2.1alpha1, there's very little you can
347do to change \file{setup.py}'s behaviour, or to discover why a given
348module isn't compiled. If you run into problems in 2.1alpha1, please
349report them, and be prepared to dive into \file{setup.py} in order to
350fix autodetection of a given library on your system. In the alpha2
351release I plan to add ways to have more control over what the script
352does (probably command-line arguments to \file{configure} or to
353\file{setup.py}).
354
355If it turns out to be impossible to make autodetection work reliably,
356it's possible that this change may become an optional build method
357instead of the default, or it may even be backed out completely.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000358
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000359In another far-reaching change to the build mechanism, Neil
360Schemenauer restructured things so Python now uses a single makefile
361that isn't recursive, instead of makefiles in the top directory and in
362each of the Python/, Parser/, Objects/, and Modules/ subdirectories.
363This makes building Python faster, and also makes the build process
364clearer and simpler.
365
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000366\begin{seealso}
367
368\seepep{229}{Using Distutils to Build Python}{Written
369and implemented by A.M. Kuchling.}
370
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000371\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000372
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000373%======================================================================
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000374\section{PEP 205: Weak References}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000375
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000376Weak references, available through the \module{weakref} module, are a
377minor but useful new data type in the Python programmer's toolbox.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000378
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000379Storing a reference to an object (say, in a dictionary or a list) has
380the side effect of keeping that object alive forever. There are a few
381specific cases where this behaviour is undesirable, object caches
382being the most common one, and another being circular references in
383data structures such as trees.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000384
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000385For example, consider a memoizing function that caches the results of
386another function \function{f(\var{x})} by storing the function's
387argument and its result in a dictionary:
388
389\begin{verbatim}
390_cache = {}
391def memoize(x):
392 if _cache.has_key(x):
393 return _cache[x]
394
395 retval = f(x)
396
397 # Cache the returned object
398 _cache[x] = retval
399
400 return retval
401\end{verbatim}
402
403This version works for simple things such as integers, but it has a
404side effect; the \code{_cache} dictionary holds a reference to the
405return values, so they'll never be deallocated until the Python
406process exits and cleans up This isn't very noticeable for integers,
407but if \function{f()} returns an object, or a data structure that
408takes up a lot of memory, this can be a problem.
409
410Weak references provide a way to implement a cache that won't keep
411objects alive beyond their time. If an object is only accessible
412through weak references, the object will be deallocated and the weak
413references will now indicate that the object it referred to no longer
414exists. A weak reference to an object \var{obj} is created by calling
415\code{wr = weakref.ref(\var{obj})}. The object being referred to is
416returned by calling the weak reference as if it were a function:
417\code{wr()}. It will return the referenced object, or \code{None} if
418the object no longer exists.
419
420This makes it possible to write a \function{memoize()} function whose
421cache doesn't keep objects alive, by storing weak references in the
422cache.
423
424\begin{verbatim}
425_cache = {}
426def memoize(x):
427 if _cache.has_key(x):
428 obj = _cache[x]()
429 # If weak reference object still exists,
430 # return it
431 if obj is not None: return obj
432
433 retval = f(x)
434
435 # Cache a weak reference
436 _cache[x] = weakref.ref(retval)
437
438 return retval
439\end{verbatim}
440
441The \module{weakref} module also allows creating proxy objects which
442behave like weak references --- an object referenced only by proxy
443objects is deallocated -- but instead of requiring an explicit call to
444retrieve the object, the proxy transparently forwards all operations
445to the object as long as the object still exists. If the object is
446deallocated, attempting to use a proxy will cause a
447\exception{weakref.ReferenceError} exception to be raised.
448
449\begin{verbatim}
450proxy = weakref.proxy(obj)
451proxy.attr # Equivalent to obj.attr
452proxy.meth() # Equivalent to obj.meth()
453del obj
454proxy.attr # raises weakref.ReferenceError
455\end{verbatim}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000456
457\begin{seealso}
458
459\seepep{205}{Weak References}{Written and implemented by
460Fred~L. Drake,~Jr.}
461
462\end{seealso}
463
464%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000465\section{PEP 217: Interactive Display Hook}
466
467When using the Python interpreter interactively, the output of
468commands is displayed using the built-in \function{repr()} function.
469In Python 2.1, the variable \module{sys.displayhook} can be set to a
470callable object which will be called instead of \function{repr()}.
471For example, you can set it to a special pretty-printing function:
472
473\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000474>>> # Create a recursive data structure
475... L = [1,2,3]
476>>> L.append(L)
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000477>>> L # Show Python's default output
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000478[1, 2, 3, [...]]
479>>> # Use pprint.pprint() as the display function
480... import sys, pprint
481>>> sys.displayhook = pprint.pprint
482>>> L
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000483[1, 2, 3, <Recursion on list with id=135143996>]
484>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000485\end{verbatim}
486
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000487\begin{seealso}
488
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000489\seepep{217}{Display Hook for Interactive Use}{Written and implemented
490by Moshe Zadka.}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000491
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000492\end{seealso}
493
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000494%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000495\section{PEP 208: New Coercion Model}
496
497How numeric coercion is done at the C level was significantly
498modified. This will only affect the authors of C extensions to
499Python, allowing them more flexibility in writing extension types that
500support numeric operations.
501
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000502Extension types can now set the type flag \code{Py_TPFLAGS_CHECKTYPES}
503in their \code{PyTypeObject} structure to indicate that they support
504the new coercion model. In such extension types, the numeric slot
505functions can no longer assume that they'll be passed two arguments of
506the same type; instead they may be passed two arguments of differing
507types, and can then perform their own internal coercion. If the slot
508function is passed a type it can't handle, it can indicate the failure
509by returning a reference to the \code{Py_NotImplemented} singleton
510value. The numeric functions of the other type will then be tried,
511and perhaps they can handle the operation; if the other type also
512returns \code{Py_NotImplemented}, then a \exception{TypeError} will be
513raised. Numeric methods written in Python can also return
514\code{Py_NotImplemented}, causing the interpreter to act as if the
515method did not exist (perhaps raising a \exception{TypeError}, perhaps
516trying another object's numeric methods).
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000517
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000518\begin{seealso}
519
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000520\seepep{208}{Reworking the Coercion Model}{Written and implemented by
521Neil Schemenauer, heavily based upon earlier work by Marc-Andr\'e
522Lemburg. Read this to understand the fine points of how numeric
523operations will now be processed at the C level.}
524
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000525\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000526
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000527%======================================================================
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000528\section{New and Improved Modules}
529
530\begin{itemize}
531
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000532\item The \module{doctest} module provides a testing framework based
533on running embedded examples in docstrings and comparing the results
534against the expected output. Contributed by Tim Peters.
535
536\item The \module{difflib} module contains a class,
537\class{SequenceMatcher}, which compares two sequences and computes the
538changes required to transform one sequence into the other. For
539example, this module can be used to write a tool similar to the Unix
540\program{diff} program, and in fact the sample program
541\file{Tools/scripts/ndiff.py} demonstrates how to write such a script.
542
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000543\item \module{curses.panel}, a wrapper for the panel library, part of
544ncurses and of SYSV curses, was contributed by Thomas Gellekum. The
545panel library provides windows with the additional feature of depth.
546Windows can be moved higher or lower in the depth ordering, and the
547panel library figures out where panels overlap and which sections are
548visible.
549
550\item The PyXML package has gone through a few releases since Python
5512.0, and Python 2.1 includes an updated version of the \module{xml}
552package. Some of the noteworthy changes include support for Expat
5531.2, the ability for Expat parsers to handle files in any encoding
554supported by Python, and various bugfixes for SAX, DOM, and the
555\module{minidom} module.
556
557\item Various functions in the \module{time} module, such as
558\function{asctime()} and \function{localtime()}, require a floating
559point argument containing the time in seconds since the epoch. The
560most common use of these functions is to work with the current time,
561so the floating point argument has been made optional; when a value
562isn't provided, the current time will be used. For example, log file
563entries usually need a string containing the current time; in Python
5642.1, \code{time.asctime()} can be used, instead of the lengthier
565\code{time.asctime(time.localtime(time.time()))} that was previously
566required.
567
568This change was proposed and implemented by Thomas Wouters.
569
570\item The \module{ftplib} module now defaults to retrieving files in
571passive mode, because passive mode is more likely to work from behind
572a firewall. This request came from the Debian bug tracking system,
573since other Debian packages use \module{ftplib} to retrieve files and
574then don't work from behind a firewall. It's deemed unlikely that
575this will cause problems for anyone, because Netscape defaults to
576passive mode and few people complain, but if passive mode is
577unsuitable for your application or network setup, call
578\method{set_pasv(0)} on FTP objects to disable passive mode.
579
580\item Support for raw socket access has been added to the
581\module{socket} module, contributed by Grant Edwards.
582
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000583\item A new implementation-dependent function, \function{sys._getframe(\optional{depth})},
584has been added to return a given frame object from the current call stack.
585\function{sys._getframe()} returns the frame at the top of the call stack;
586if the optional integer argument \var{depth} is supplied, the function returns the frame
587that is \var{depth} calls below the top of the stack. For example, \code{sys._getframe(1)}
588returns the caller's frame object.
589
590This function is only present in CPython, not in Jython or the .NET
591implementation. Use it for debugging, and resist the temptation to
592put it into production code.
593
594
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000595
596\end{itemize}
597
598%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000599\section{Minor Changes and Fixes}
600
601There were relatively few smaller changes made in Python 2.1 due to
602the shorter release cycle. A search through the CVS change logs turns
603up 57 patches applied, and 86 bugs fixed; both figures are likely to
604be underestimates. Some of the more notable changes are:
605
606\begin{itemize}
607
Andrew M. Kuchlingbf140142001-02-28 22:10:07 +0000608
609\item A specialized object allocator is now optionally available, that
610should be faster than the system \function{malloc()} and have less
611memory overhead. The allocator uses C's \function{malloc()} function
612to get large pools of memory, and then fulfills smaller memory
613requests from these pools. It can be enabled by providing the
614"--with-pymalloc" option to the \filename{configure} script; see
615\filename{Objects/obmalloc.c} for the implementation details.
616Contributed by Vladimir Marangozov.
617
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000618\item The speed of line-oriented file I/O has been improved because
619people often complain about its lack of speed, and because it's often
620been used as a na\"ive benchmark. The \method{readline()} method of
621file objects has therefore been rewritten to be much faster. The
622exact amount of the speedup will vary from platform to platform
623depending on how slow the C library's \function{getc()} was, but is
624around 66\%, and potentially much faster on some particular operating
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000625systems. Tim Peters did much of the benchmarking and coding for this
626change, motivated by a discussion in comp.lang.python.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000627
628A new module and method for file objects was also added, contributed
629by Jeff Epler. The new method, \method{xreadlines()}, is similar to
630the existing \function{xrange()} built-in. \function{xreadlines()}
631returns an opaque sequence object that only supports being iterated
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000632over, reading a line on every iteration but not reading the entire
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000633file into memory as the existing \method{readlines()} method does.
634You'd use it like this:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000635
636\begin{verbatim}
637for line in sys.stdin.xreadlines():
638 # ... do something for each line ...
639 ...
640\end{verbatim}
641
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000642For a fuller discussion of the line I/O changes, see the python-dev
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000643summary for January 1-15, 2001.
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000644
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000645\item A new method, \method{popitem()}, was added to dictionaries to
646enable destructively iterating through the contents of a dictionary;
647this can be faster for large dictionaries because .
648\code{D.popitem()} removes a random \code{(\var{key}, \var{value})}
649pair from the dictionary and returns it as a 2-tuple. This was
650implemented mostly by Tim Peters and Guido van Rossum, after a
651suggestion and preliminary patch by Moshe Zadka.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000652
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000653\item Modules can now control which names are imported when \code{from
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000654\var{module} import *} is used, by defining an \code{__all__}
655attribute containing a list of names that will be imported. One
656common complaint is that if the module imports other modules such as
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000657\module{sys} or \module{string}, \code{from \var{module} import *}
658will add them to the importing module's namespace. To fix this,
659simply list the public names in \code{__all__}:
660
661\begin{verbatim}
662# List public names
663__all__ = ['Database', 'open']
664\end{verbatim}
665
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000666A stricter version of this patch was first suggested and implemented
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000667by Ben Wolfson, but after some python-dev discussion, a weaker final
668version was checked in.
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000669
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000670\item Applying \function{repr()} to strings previously used octal
671escapes for non-printable characters; for example, a newline was
672\code{'\e 012'}. This was a vestigial trace of Python's C ancestry, but
673today octal is of very little practical use. Ka-Ping Yee suggested
674using hex escapes instead of octal ones, and using the \code{\e n},
675\code{\e t}, \code{\e r} escapes for the appropriate characters, and
676implemented this new formatting.
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000677
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000678\item Syntax errors detected at compile-time can now raise exceptions
679containing the filename and line number of the error, a pleasant side
680effect of the compiler reorganization done by Jeremy Hylton.
681
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000682\item C extensions which import other modules have been changed to use
683\function{PyImport_ImportModule()}, which means that they will use any
684import hooks that have been installed. This is also encouraged for
685third-party extensions that need to import some other module from C
686code.
687
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000688\item The size of the Unicode character database was shrunk by another
689340K thanks to Fredrik Lundh.
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000690
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000691\end{itemize}
692
693And there's the usual list of bugfixes, minor memory leaks, docstring
694edits, and other tweaks, too lengthy to be worth itemizing; see the
695CVS logs for the full details if you want them.
696
697
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000698%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000699\section{Acknowledgements}
700
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000701The author would like to thank the following people for offering
702suggestions on various drafts of this article: Graeme Cross, David
703Goodger, Jay Graves, Michael Hudson, Marc-Andr\'e Lemburg, Fredrik
704Lundh, Neil Schemenauer, Thomas Wouters.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000705
706\end{document}