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