blob: 1f5c11377c47cd908e74b2b6a360f386f1cc6a19 [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. Kuchling5120eac2001-07-20 03:22:00 +00008\release{1.00}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +00009\author{A.M. Kuchling}
Fred Drakeb914ef02004-01-02 06:57:50 +000010\authoraddress{
11 \strong{Python Software Foundation}\\
12 Email: \email{amk@amk.ca}
13}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +000014\begin{document}
15\maketitle\tableofcontents
16
17\section{Introduction}
18
Andrew M. Kuchlingdb7657d2001-04-12 03:37:19 +000019It's that time again... time for a new Python release, Python 2.1.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +000020One 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
Andrew M. Kuchlingb39fa8a2001-07-19 00:29:48 +000037The final release of Python 2.1 was made on April 17, 2001.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +000038
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +000039%======================================================================
40\section{PEP 227: Nested Scopes}
41
42The largest change in Python 2.1 is to Python's scoping rules. In
43Python 2.0, at any given time there are at most three namespaces used
44to look up variable names: local, module-level, and the built-in
45namespace. This often surprised people because it didn't match their
46intuitive expectations. For example, a nested recursive function
47definition doesn't work:
48
49\begin{verbatim}
50def f():
51 ...
52 def g(value):
53 ...
54 return g(value-1) + 1
55 ...
56\end{verbatim}
57
58The function \function{g()} will always raise a \exception{NameError}
59exception, because the binding of the name \samp{g} isn't in either
60its local namespace or in the module-level namespace. This isn't much
61of a problem in practice (how often do you recursively define interior
62functions like this?), but this also made using the \keyword{lambda}
63statement clumsier, and this was a problem in practice. In code which
64uses \keyword{lambda} you can often find local variables being copied
65by passing them as the default values of arguments.
66
67\begin{verbatim}
68def find(self, name):
69 "Return list of any entries equal to 'name'"
70 L = filter(lambda x, name=name: x == name,
71 self.list_attribute)
72 return L
73\end{verbatim}
74
75The readability of Python code written in a strongly functional style
76suffers greatly as a result.
77
78The most significant change to Python 2.1 is that static scoping has
79been added to the language to fix this problem. As a first effect,
80the \code{name=name} default argument is now unnecessary in the above
81example. Put simply, when a given variable name is not assigned a
82value within a function (by an assignment, or the \keyword{def},
83\keyword{class}, or \keyword{import} statements), references to the
84variable will be looked up in the local namespace of the enclosing
85scope. A more detailed explanation of the rules, and a dissection of
86the implementation, can be found in the PEP.
87
88This change may cause some compatibility problems for code where the
89same variable name is used both at the module level and as a local
90variable within a function that contains further function definitions.
91This seems rather unlikely though, since such code would have been
Andrew M. Kuchling61af5602001-03-03 03:25:04 +000092pretty confusing to read in the first place.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +000093
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +000094One side effect of the change is that the \code{from \var{module}
95import *} and \keyword{exec} statements have been made illegal inside
96a function scope under certain conditions. The Python reference
97manual has said all along that \code{from \var{module} import *} is
98only legal at the top level of a module, but the CPython interpreter
99has never enforced this before. As part of the implementation of
100nested scopes, the compiler which turns Python source into bytecodes
101has to generate different code to access variables in a containing
102scope. \code{from \var{module} import *} and \keyword{exec} make it
103impossible for the compiler to figure this out, because they add names
104to the local namespace that are unknowable at compile time.
105Therefore, if a function contains function definitions or
106\keyword{lambda} expressions with free variables, the compiler will
107flag this by raising a \exception{SyntaxError} exception.
108
109To make the preceding explanation a bit clearer, here's an example:
110
111\begin{verbatim}
112x = 1
113def f():
114 # The next line is a syntax error
115 exec 'x=2'
116 def g():
117 return x
118\end{verbatim}
119
120Line 4 containing the \keyword{exec} statement is a syntax error,
121since \keyword{exec} would define a new local variable named \samp{x}
122whose value should be accessed by \function{g()}.
123
124This shouldn't be much of a limitation, since \keyword{exec} is rarely
125used in most Python code (and when it is used, it's often a sign of a
126poor design anyway).
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000127
Andrew M. Kuchling61af5602001-03-03 03:25:04 +0000128Compatibility concerns have led to nested scopes being introduced
129gradually; in Python 2.1, they aren't enabled by default, but can be
130turned on within a module by using a future statement as described in
131PEP 236. (See the following section for further discussion of PEP
132236.) In Python 2.2, nested scopes will become the default and there
133will be no way to turn them off, but users will have had all of 2.1's
134lifetime to fix any breakage resulting from their introduction.
135
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000136\begin{seealso}
137
138\seepep{227}{Statically Nested Scopes}{Written and implemented by
139Jeremy Hylton.}
140
141\end{seealso}
142
143
144%======================================================================
Andrew M. Kuchling8d177092003-05-13 14:26:54 +0000145\section{PEP 236: __future__ Directives}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000146
Andrew M. Kuchling61af5602001-03-03 03:25:04 +0000147The reaction to nested scopes was widespread concern about the dangers
148of breaking code with the 2.1 release, and it was strong enough to
149make the Pythoneers take a more conservative approach. This approach
150consists of introducing a convention for enabling optional
151functionality in release N that will become compulsory in release N+1.
152
153The syntax uses a \code{from...import} statement using the reserved
154module name \module{__future__}. Nested scopes can be enabled by the
155following statement:
156
157\begin{verbatim}
158from __future__ import nested_scopes
159\end{verbatim}
160
161While it looks like a normal \keyword{import} statement, it's not;
162there are strict rules on where such a future statement can be put.
163They can only be at the top of a module, and must precede any Python
164code or regular \keyword{import} statements. This is because such
165statements can affect how the Python bytecode compiler parses code and
166generates bytecode, so they must precede any statement that will
167result in bytecodes being produced.
168
169\begin{seealso}
170
171\seepep{236}{Back to the \module{__future__}}{Written by Tim Peters,
172and primarily implemented by Jeremy Hylton.}
173
174\end{seealso}
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000175
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000176%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000177\section{PEP 207: Rich Comparisons}
178
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000179In earlier versions, Python's support for implementing comparisons on
180user-defined classes and extension types was quite simple. Classes
181could implement a \method{__cmp__} method that was given two instances
182of a class, and could only return 0 if they were equal or +1 or -1 if
183they weren't; the method couldn't raise an exception or return
184anything other than a Boolean value. Users of Numeric Python often
185found this model too weak and restrictive, because in the
186number-crunching programs that numeric Python is used for, it would be
187more useful to be able to perform elementwise comparisons of two
188matrices, returning a matrix containing the results of a given
189comparison for each element. If the two matrices are of different
190sizes, then the compare has to be able to raise an exception to signal
191the error.
192
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000193In Python 2.1, rich comparisons were added in order to support this
194need. Python classes can now individually overload each of the
195\code{<}, \code{<=}, \code{>}, \code{>=}, \code{==}, and \code{!=}
196operations. The new magic method names are:
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000197
198\begin{tableii}{c|l}{code}{Operation}{Method name}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000199 \lineii{<}{\method{__lt__}} \lineii{<=}{\method{__le__}}
200 \lineii{>}{\method{__gt__}} \lineii{>=}{\method{__ge__}}
201 \lineii{==}{\method{__eq__}} \lineii{!=}{\method{__ne__}}
202 \end{tableii}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000203
204(The magic methods are named after the corresponding Fortran operators
205\code{.LT.}. \code{.LE.}, \&c. Numeric programmers are almost
206certainly quite familar with these names and will find them easy to
207remember.)
208
209Each of these magic methods is of the form \code{\var{method}(self,
210other)}, where \code{self} will be the object on the left-hand side of
211the operator, while \code{other} will be the object on the right-hand
212side. For example, the expression \code{A < B} will cause
213\code{A.__lt__(B)} to be called.
214
215Each of these magic methods can return anything at all: a Boolean, a
216matrix, a list, or any other Python object. Alternatively they can
217raise an exception if the comparison is impossible, inconsistent, or
218otherwise meaningless.
219
220The built-in \function{cmp(A,B)} function can use the rich comparison
221machinery, and now accepts an optional argument specifying which
222comparison operation to use; this is given as one of the strings
223\code{"<"}, \code{"<="}, \code{">"}, \code{">="}, \code{"=="}, or
224\code{"!="}. If called without the optional third argument,
225\function{cmp()} will only return -1, 0, or +1 as in previous versions
226of Python; otherwise it will call the appropriate method and can
227return any Python object.
228
229There are also corresponding changes of interest to C programmers;
230there's a new slot \code{tp_richcmp} in type objects and an API for
231performing a given rich comparison. I won't cover the C API here, but
Andrew M. Kuchlingbf140142001-02-28 22:10:07 +0000232will refer you to PEP 207, or to 2.1's C API documentation, for the
233full list of related functions.
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000234
235\begin{seealso}
236
237\seepep{207}{Rich Comparisions}{Written by Guido van Rossum, heavily
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000238based on earlier work by David Ascher, and implemented by Guido van
239Rossum.}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000240
241\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000242
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000243%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000244\section{PEP 230: Warning Framework}
245
246Over its 10 years of existence, Python has accumulated a certain
247number of obsolete modules and features along the way. It's difficult
248to know when a feature is safe to remove, since there's no way of
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000249knowing how much code uses it --- perhaps no programs depend on the
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000250feature, or perhaps many do. To enable removing old features in a
251more structured way, a warning framework was added. When the Python
252developers want to get rid of a feature, it will first trigger a
253warning in the next version of Python. The following Python version
254can then drop the feature, and users will have had a full release
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000255cycle to remove uses of the old feature.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000256
257Python 2.1 adds the warning framework to be used in this scheme. It
258adds a \module{warnings} module that provide functions to issue
259warnings, and to filter out warnings that you don't want to be
260displayed. Third-party modules can also use this framework to
261deprecate old features that they no longer wish to support.
262
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000263For example, in Python 2.1 the \module{regex} module is deprecated, so
264importing it causes a warning to be printed:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000265
266\begin{verbatim}
267>>> import regex
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000268__main__:1: DeprecationWarning: the regex module
269 is deprecated; please use the re module
270>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000271\end{verbatim}
272
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000273Warnings can be issued by calling the \function{warnings.warn}
274function:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000275
276\begin{verbatim}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000277warnings.warn("feature X no longer supported")
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000278\end{verbatim}
279
280The first parameter is the warning message; an additional optional
281parameters can be used to specify a particular warning category.
282
283Filters can be added to disable certain warnings; a regular expression
284pattern can be applied to the message or to the module name in order
285to suppress a warning. For example, you may have a program that uses
286the \module{regex} module and not want to spare the time to convert it
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000287to use the \module{re} module right now. The warning can be
288suppressed by calling
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000289
290\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000291import warnings
292warnings.filterwarnings(action = 'ignore',
293 message='.*regex module is deprecated',
294 category=DeprecationWarning,
295 module = '__main__')
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000296\end{verbatim}
297
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000298This adds a filter that will apply only to warnings of the class
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000299\class{DeprecationWarning} triggered in the \module{__main__} module,
300and applies a regular expression to only match the message about the
301\module{regex} module being deprecated, and will cause such warnings
302to be ignored. Warnings can also be printed only once, printed every
303time the offending code is executed, or turned into exceptions that
304will cause the program to stop (unless the exceptions are caught in
305the usual way, of course).
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000306
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000307Functions were also added to Python's C API for issuing warnings;
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000308refer to PEP 230 or to Python's API documentation for the details.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000309
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000310\begin{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000311
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000312\seepep{5}{Guidelines for Language Evolution}{Written
313by Paul Prescod, to specify procedures to be followed when removing
314old features from Python. The policy described in this PEP hasn't
315been officially adopted, but the eventual policy probably won't be too
316different from Prescod's proposal.}
317
318\seepep{230}{Warning Framework}{Written and implemented by Guido van
319Rossum.}
320
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000321\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000322
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000323%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000324\section{PEP 229: New Build System}
325
326When compiling Python, the user had to go in and edit the
327\file{Modules/Setup} file in order to enable various additional
328modules; the default set is relatively small and limited to modules
329that compile on most Unix platforms. This means that on Unix
330platforms with many more features, most notably Linux, Python
331installations often don't contain all useful modules they could.
332
333Python 2.0 added the Distutils, a set of modules for distributing and
334installing extensions. In Python 2.1, the Distutils are used to
335compile much of the standard library of extension modules,
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000336autodetecting which ones are supported on the current machine. It's
337hoped that this will make Python installations easier and more
338featureful.
339
340Instead of having to edit the \file{Modules/Setup} file in order to
341enable modules, a \file{setup.py} script in the top directory of the
342Python source distribution is run at build time, and attempts to
343discover which modules can be enabled by examining the modules and
Andrew M. Kuchling8bad9932001-02-28 22:39:15 +0000344header files on the system. If a module is configured in
345\file{Modules/Setup}, the \file{setup.py} script won't attempt to
346compile that module and will defer to the \file{Modules/Setup} file's
347contents. This provides a way to specific any strange command-line
348flags or libraries that are required for a specific platform.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000349
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000350In another far-reaching change to the build mechanism, Neil
351Schemenauer restructured things so Python now uses a single makefile
352that isn't recursive, instead of makefiles in the top directory and in
Andrew M. Kuchling8bad9932001-02-28 22:39:15 +0000353each of the \file{Python/}, \file{Parser/}, \file{Objects/}, and
354\file{Modules/} subdirectories. This makes building Python faster
355and also makes hacking the Makefiles clearer and simpler.
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000356
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000357\begin{seealso}
358
359\seepep{229}{Using Distutils to Build Python}{Written
360and implemented by A.M. Kuchling.}
361
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000362\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000363
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000364%======================================================================
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000365\section{PEP 205: Weak References}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000366
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000367Weak references, available through the \module{weakref} module, are a
368minor but useful new data type in the Python programmer's toolbox.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000369
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000370Storing a reference to an object (say, in a dictionary or a list) has
371the side effect of keeping that object alive forever. There are a few
372specific cases where this behaviour is undesirable, object caches
373being the most common one, and another being circular references in
374data structures such as trees.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000375
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000376For example, consider a memoizing function that caches the results of
377another function \function{f(\var{x})} by storing the function's
378argument and its result in a dictionary:
379
380\begin{verbatim}
381_cache = {}
382def memoize(x):
383 if _cache.has_key(x):
384 return _cache[x]
385
386 retval = f(x)
387
388 # Cache the returned object
389 _cache[x] = retval
390
391 return retval
392\end{verbatim}
393
394This version works for simple things such as integers, but it has a
395side effect; the \code{_cache} dictionary holds a reference to the
396return values, so they'll never be deallocated until the Python
397process exits and cleans up This isn't very noticeable for integers,
398but if \function{f()} returns an object, or a data structure that
399takes up a lot of memory, this can be a problem.
400
401Weak references provide a way to implement a cache that won't keep
402objects alive beyond their time. If an object is only accessible
403through weak references, the object will be deallocated and the weak
404references will now indicate that the object it referred to no longer
405exists. A weak reference to an object \var{obj} is created by calling
406\code{wr = weakref.ref(\var{obj})}. The object being referred to is
407returned by calling the weak reference as if it were a function:
408\code{wr()}. It will return the referenced object, or \code{None} if
409the object no longer exists.
410
411This makes it possible to write a \function{memoize()} function whose
412cache doesn't keep objects alive, by storing weak references in the
413cache.
414
415\begin{verbatim}
416_cache = {}
417def memoize(x):
418 if _cache.has_key(x):
419 obj = _cache[x]()
420 # If weak reference object still exists,
421 # return it
422 if obj is not None: return obj
423
424 retval = f(x)
425
426 # Cache a weak reference
427 _cache[x] = weakref.ref(retval)
428
429 return retval
430\end{verbatim}
431
432The \module{weakref} module also allows creating proxy objects which
433behave like weak references --- an object referenced only by proxy
434objects is deallocated -- but instead of requiring an explicit call to
435retrieve the object, the proxy transparently forwards all operations
436to the object as long as the object still exists. If the object is
437deallocated, attempting to use a proxy will cause a
438\exception{weakref.ReferenceError} exception to be raised.
439
440\begin{verbatim}
441proxy = weakref.proxy(obj)
442proxy.attr # Equivalent to obj.attr
443proxy.meth() # Equivalent to obj.meth()
444del obj
445proxy.attr # raises weakref.ReferenceError
446\end{verbatim}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000447
448\begin{seealso}
449
450\seepep{205}{Weak References}{Written and implemented by
451Fred~L. Drake,~Jr.}
452
453\end{seealso}
454
455%======================================================================
Andrew M. Kuchlingef85cc82001-03-23 03:29:08 +0000456\section{PEP 232: Function Attributes}
457
458In Python 2.1, functions can now have arbitrary information attached
459to them. People were often using docstrings to hold information about
460functions and methods, because the \code{__doc__} attribute was the
461only way of attaching any information to a function. For example, in
462the Zope Web application server, functions are marked as safe for
463public access by having a docstring, and in John Aycock's SPARK
464parsing framework, docstrings hold parts of the BNF grammar to be
465parsed. This overloading is unfortunate, since docstrings are really
466intended to hold a function's documentation; for example, it means you
467can't properly document functions intended for private use in Zope.
468
469Arbitrary attributes can now be set and retrieved on functions using the
470regular Python syntax:
471
472\begin{verbatim}
473def f(): pass
474
475f.publish = 1
476f.secure = 1
477f.grammar = "A ::= B (C D)*"
478\end{verbatim}
479
480The dictionary containing attributes can be accessed as the function's
481\member{__dict__}. Unlike the \member{__dict__} attribute of class
482instances, in functions you can actually assign a new dictionary to
483\member{__dict__}, though the new value is restricted to a regular
484Python dictionary; you \emph{can't} be tricky and set it to a
485\class{UserDict} instance, or any other random object that behaves
486like a mapping.
487
488\begin{seealso}
489
490\seepep{232}{Function Attributes}{Written and implemented by Barry
491Warsaw.}
492
493\end{seealso}
494
495
496%======================================================================
497
Andrew M. Kuchling8d177092003-05-13 14:26:54 +0000498\section{PEP 235: Importing Modules on Case-Insensitive Platforms}
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000499
Andrew M. Kuchling8bad9932001-02-28 22:39:15 +0000500Some operating systems have filesystems that are case-insensitive,
501MacOS and Windows being the primary examples; on these systems, it's
502impossible to distinguish the filenames \samp{FILE.PY} and
503\samp{file.py}, even though they do store the file's name
504in its original case (they're case-preserving, too).
505
506In Python 2.1, the \keyword{import} statement will work to simulate
507case-sensitivity on case-insensitive platforms. Python will now
508search for the first case-sensitive match by default, raising an
509\exception{ImportError} if no such file is found, so \code{import file}
510will not import a module named \samp{FILE.PY}. Case-insensitive
Fred Drake9ad526f2001-04-12 04:11:21 +0000511matching can be requested by setting the \envvar{PYTHONCASEOK} environment
Andrew M. Kuchling8bad9932001-02-28 22:39:15 +0000512variable before starting the Python interpreter.
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000513
514%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000515\section{PEP 217: Interactive Display Hook}
516
517When using the Python interpreter interactively, the output of
518commands is displayed using the built-in \function{repr()} function.
Andrew M. Kuchlingef85cc82001-03-23 03:29:08 +0000519In Python 2.1, the variable \function{sys.displayhook} can be set to a
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000520callable object which will be called instead of \function{repr()}.
521For example, you can set it to a special pretty-printing function:
522
523\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000524>>> # Create a recursive data structure
525... L = [1,2,3]
526>>> L.append(L)
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000527>>> L # Show Python's default output
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000528[1, 2, 3, [...]]
529>>> # Use pprint.pprint() as the display function
530... import sys, pprint
531>>> sys.displayhook = pprint.pprint
532>>> L
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000533[1, 2, 3, <Recursion on list with id=135143996>]
534>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000535\end{verbatim}
536
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000537\begin{seealso}
538
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000539\seepep{217}{Display Hook for Interactive Use}{Written and implemented
540by Moshe Zadka.}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000541
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000542\end{seealso}
543
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000544%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000545\section{PEP 208: New Coercion Model}
546
547How numeric coercion is done at the C level was significantly
548modified. This will only affect the authors of C extensions to
549Python, allowing them more flexibility in writing extension types that
550support numeric operations.
551
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000552Extension types can now set the type flag \code{Py_TPFLAGS_CHECKTYPES}
553in their \code{PyTypeObject} structure to indicate that they support
554the new coercion model. In such extension types, the numeric slot
555functions can no longer assume that they'll be passed two arguments of
556the same type; instead they may be passed two arguments of differing
557types, and can then perform their own internal coercion. If the slot
558function is passed a type it can't handle, it can indicate the failure
559by returning a reference to the \code{Py_NotImplemented} singleton
560value. The numeric functions of the other type will then be tried,
561and perhaps they can handle the operation; if the other type also
562returns \code{Py_NotImplemented}, then a \exception{TypeError} will be
563raised. Numeric methods written in Python can also return
564\code{Py_NotImplemented}, causing the interpreter to act as if the
565method did not exist (perhaps raising a \exception{TypeError}, perhaps
566trying another object's numeric methods).
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000567
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000568\begin{seealso}
569
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000570\seepep{208}{Reworking the Coercion Model}{Written and implemented by
571Neil Schemenauer, heavily based upon earlier work by Marc-Andr\'e
572Lemburg. Read this to understand the fine points of how numeric
573operations will now be processed at the C level.}
574
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000575\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000576
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000577%======================================================================
Andrew M. Kuchlingef85cc82001-03-23 03:29:08 +0000578\section{PEP 241: Metadata in Python Packages}
579
580A common complaint from Python users is that there's no single catalog
581of all the Python modules in existence. T.~Middleton's Vaults of
Fred Drake700c8902003-07-22 00:52:42 +0000582Parnassus at \url{http://www.vex.net/parnassus/} are the largest
Andrew M. Kuchlingef85cc82001-03-23 03:29:08 +0000583catalog of Python modules, but registering software at the Vaults is
584optional, and many people don't bother.
585
586As a first small step toward fixing the problem, Python software
587packaged using the Distutils \command{sdist} command will include a
588file named \file{PKG-INFO} containing information about the package
589such as its name, version, and author (metadata, in cataloguing
590terminology). PEP 241 contains the full list of fields that can be
591present in the \file{PKG-INFO} file. As people began to package their
592software using Python 2.1, more and more packages will include
593metadata, making it possible to build automated cataloguing systems
594and experiment with them. With the result experience, perhaps it'll
595be possible to design a really good catalog and then build support for
596it into Python 2.2. For example, the Distutils \command{sdist}
597and \command{bdist_*} commands could support a \option{upload} option
598that would automatically upload your package to a catalog server.
599
600You can start creating packages containing \file{PKG-INFO} even if
601you're not using Python 2.1, since a new release of the Distutils will
602be made for users of earlier Python versions. Version 1.0.2 of the
603Distutils includes the changes described in PEP 241, as well as
604various bugfixes and enhancements. It will be available from
Fred Drake700c8902003-07-22 00:52:42 +0000605the Distutils SIG at \url{http://www.python.org/sigs/distutils-sig/}.
Andrew M. Kuchlingef85cc82001-03-23 03:29:08 +0000606
Andrew M. Kuchlingef85cc82001-03-23 03:29:08 +0000607\begin{seealso}
608
609\seepep{241}{Metadata for Python Software Packages}{Written and
610implemented by A.M. Kuchling.}
611
612\seepep{243}{Module Repository Upload Mechanism}{Written by Sean
613Reifschneider, this draft PEP describes a proposed mechanism for uploading
614Python packages to a central server.
615}
616
617\end{seealso}
618
619%======================================================================
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000620\section{New and Improved Modules}
621
622\begin{itemize}
623
Andrew M. Kuchling1fcd4382001-04-16 02:27:53 +0000624\item Ka-Ping Yee contributed two new modules: \module{inspect.py}, a
625module for getting information about live Python code, and
626\module{pydoc.py}, a module for interactively converting docstrings to
627HTML or text. As a bonus, \file{Tools/scripts/pydoc}, which is now
628automatically installed, uses \module{pydoc.py} to display
629documentation given a Python module, package, or class name. For
630example, \samp{pydoc xml.dom} displays the following:
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000631
632\begin{verbatim}
633Python Library Documentation: package xml.dom in xml
634
635NAME
636 xml.dom - W3C Document Object Model implementation for Python.
637
638FILE
639 /usr/local/lib/python2.1/xml/dom/__init__.pyc
640
641DESCRIPTION
642 The Python mapping of the Document Object Model is documented in the
643 Python Library Reference in the section on the xml.dom package.
644
645 This package contains the following modules:
646 ...
647\end{verbatim}
648
Andrew M. Kuchling1fcd4382001-04-16 02:27:53 +0000649\file{pydoc} also includes a Tk-based interactive help browser.
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000650\file{pydoc} quickly becomes addictive; try it out!
651
Andrew M. Kuchlingef85cc82001-03-23 03:29:08 +0000652\item Two different modules for unit testing were added to the
653standard library. The \module{doctest} module, contributed by Tim
654Peters, provides a testing framework based on running embedded
655examples in docstrings and comparing the results against the expected
656output. PyUnit, contributed by Steve Purcell, is a unit testing
657framework inspired by JUnit, which was in turn an adaptation of Kent
658Beck's Smalltalk testing framework. See
659\url{http://pyunit.sourceforge.net/} for more information about
660PyUnit.
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000661
662\item The \module{difflib} module contains a class,
663\class{SequenceMatcher}, which compares two sequences and computes the
664changes required to transform one sequence into the other. For
665example, this module can be used to write a tool similar to the Unix
666\program{diff} program, and in fact the sample program
667\file{Tools/scripts/ndiff.py} demonstrates how to write such a script.
668
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000669\item \module{curses.panel}, a wrapper for the panel library, part of
670ncurses and of SYSV curses, was contributed by Thomas Gellekum. The
671panel library provides windows with the additional feature of depth.
672Windows can be moved higher or lower in the depth ordering, and the
673panel library figures out where panels overlap and which sections are
674visible.
675
676\item The PyXML package has gone through a few releases since Python
6772.0, and Python 2.1 includes an updated version of the \module{xml}
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000678package. Some of the noteworthy changes include support for Expat 1.2
679and later versions, the ability for Expat parsers to handle files in
680any encoding supported by Python, and various bugfixes for SAX, DOM,
681and the \module{minidom} module.
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000682
Andrew M. Kuchlingef85cc82001-03-23 03:29:08 +0000683\item Ping also contributed another hook for handling uncaught
684exceptions. \function{sys.excepthook} can be set to a callable
685object. When an exception isn't caught by any
686\keyword{try}...\keyword{except} blocks, the exception will be passed
687to \function{sys.excepthook}, which can then do whatever it likes. At
688the Ninth Python Conference, Ping demonstrated an application for this
689hook: printing an extended traceback that not only lists the stack
690frames, but also lists the function arguments and the local variables
691for each frame.
692
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000693\item Various functions in the \module{time} module, such as
694\function{asctime()} and \function{localtime()}, require a floating
695point argument containing the time in seconds since the epoch. The
696most common use of these functions is to work with the current time,
697so the floating point argument has been made optional; when a value
698isn't provided, the current time will be used. For example, log file
699entries usually need a string containing the current time; in Python
7002.1, \code{time.asctime()} can be used, instead of the lengthier
701\code{time.asctime(time.localtime(time.time()))} that was previously
702required.
703
704This change was proposed and implemented by Thomas Wouters.
705
706\item The \module{ftplib} module now defaults to retrieving files in
707passive mode, because passive mode is more likely to work from behind
708a firewall. This request came from the Debian bug tracking system,
709since other Debian packages use \module{ftplib} to retrieve files and
710then don't work from behind a firewall. It's deemed unlikely that
711this will cause problems for anyone, because Netscape defaults to
712passive mode and few people complain, but if passive mode is
713unsuitable for your application or network setup, call
714\method{set_pasv(0)} on FTP objects to disable passive mode.
715
716\item Support for raw socket access has been added to the
717\module{socket} module, contributed by Grant Edwards.
718
Andrew M. Kuchling1fcd4382001-04-16 02:27:53 +0000719\item The \module{pstats} module now contains a simple interactive
720statistics browser for displaying timing profiles for Python programs,
721invoked when the module is run as a script. Contributed by
722Eric S.\ Raymond.
723
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000724\item A new implementation-dependent function, \function{sys._getframe(\optional{depth})},
725has been added to return a given frame object from the current call stack.
726\function{sys._getframe()} returns the frame at the top of the call stack;
727if the optional integer argument \var{depth} is supplied, the function returns the frame
728that is \var{depth} calls below the top of the stack. For example, \code{sys._getframe(1)}
729returns the caller's frame object.
730
731This function is only present in CPython, not in Jython or the .NET
732implementation. Use it for debugging, and resist the temptation to
733put it into production code.
734
735
Andrew M. Kuchling81b6ae72001-02-11 16:55:39 +0000736
737\end{itemize}
738
739%======================================================================
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000740\section{Other Changes and Fixes}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000741
742There were relatively few smaller changes made in Python 2.1 due to
743the shorter release cycle. A search through the CVS change logs turns
Andrew M. Kuchling81df7be2001-03-02 21:19:38 +0000744up 117 patches applied, and 136 bugs fixed; both figures are likely to
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000745be underestimates. Some of the more notable changes are:
746
747\begin{itemize}
748
Andrew M. Kuchlingbf140142001-02-28 22:10:07 +0000749
750\item A specialized object allocator is now optionally available, that
751should be faster than the system \function{malloc()} and have less
752memory overhead. The allocator uses C's \function{malloc()} function
753to get large pools of memory, and then fulfills smaller memory
754requests from these pools. It can be enabled by providing the
Andrew M. Kuchling45bbda22001-03-10 16:49:07 +0000755\longprogramopt{with-pymalloc} option to the \program{configure} script; see
Andrew M. Kuchlingac1abe02001-03-23 03:52:46 +0000756\file{Objects/obmalloc.c} for the implementation details.
757
758Authors of C extension modules should test their code with the object
759allocator enabled, because some incorrect code may break, causing core
760dumps at runtime. There are a bunch of memory allocation functions in
761Python's C API that have previously been just aliases for the C
762library's \function{malloc()} and \function{free()}, meaning that if
763you accidentally called mismatched functions, the error wouldn't be
764noticeable. When the object allocator is enabled, these functions
765aren't aliases of \function{malloc()} and \function{free()} any more,
766and calling the wrong function to free memory will get you a core
767dump. For example, if memory was allocated using
768\function{PyMem_New()}, it has to be freed using
769\function{PyMem_Del()}, not \function{free()}. A few modules included
770with Python fell afoul of this and had to be fixed; doubtless there
771are more third-party modules that will have the same problem.
772
773The object allocator was contributed by Vladimir Marangozov.
Andrew M. Kuchlingbf140142001-02-28 22:10:07 +0000774
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000775\item The speed of line-oriented file I/O has been improved because
776people often complain about its lack of speed, and because it's often
777been used as a na\"ive benchmark. The \method{readline()} method of
778file objects has therefore been rewritten to be much faster. The
779exact amount of the speedup will vary from platform to platform
780depending on how slow the C library's \function{getc()} was, but is
781around 66\%, and potentially much faster on some particular operating
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000782systems. Tim Peters did much of the benchmarking and coding for this
783change, motivated by a discussion in comp.lang.python.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000784
785A new module and method for file objects was also added, contributed
786by Jeff Epler. The new method, \method{xreadlines()}, is similar to
787the existing \function{xrange()} built-in. \function{xreadlines()}
788returns an opaque sequence object that only supports being iterated
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000789over, reading a line on every iteration but not reading the entire
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000790file into memory as the existing \method{readlines()} method does.
791You'd use it like this:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000792
793\begin{verbatim}
794for line in sys.stdin.xreadlines():
795 # ... do something for each line ...
796 ...
797\end{verbatim}
798
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000799For a fuller discussion of the line I/O changes, see the python-dev
Andrew M. Kuchlingdb7657d2001-04-12 03:37:19 +0000800summary for January 1-15, 2001 at
Fred Drake700c8902003-07-22 00:52:42 +0000801\url{http://www.python.org/dev/summary/2001-01-1.html}.
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000802
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000803\item A new method, \method{popitem()}, was added to dictionaries to
804enable destructively iterating through the contents of a dictionary;
Andrew M. Kuchlingdb7657d2001-04-12 03:37:19 +0000805this can be faster for large dictionaries because there's no need to
806construct a list containing all the keys or values.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000807\code{D.popitem()} removes a random \code{(\var{key}, \var{value})}
Andrew M. Kuchlingdb7657d2001-04-12 03:37:19 +0000808pair from the dictionary~\code{D} and returns it as a 2-tuple. This
809was implemented mostly by Tim Peters and Guido van Rossum, after a
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000810suggestion and preliminary patch by Moshe Zadka.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000811
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000812\item Modules can now control which names are imported when \code{from
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000813\var{module} import *} is used, by defining an \code{__all__}
814attribute containing a list of names that will be imported. One
815common complaint is that if the module imports other modules such as
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000816\module{sys} or \module{string}, \code{from \var{module} import *}
817will add them to the importing module's namespace. To fix this,
818simply list the public names in \code{__all__}:
819
820\begin{verbatim}
821# List public names
822__all__ = ['Database', 'open']
823\end{verbatim}
824
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000825A stricter version of this patch was first suggested and implemented
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000826by Ben Wolfson, but after some python-dev discussion, a weaker final
827version was checked in.
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000828
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000829\item Applying \function{repr()} to strings previously used octal
830escapes for non-printable characters; for example, a newline was
831\code{'\e 012'}. This was a vestigial trace of Python's C ancestry, but
832today octal is of very little practical use. Ka-Ping Yee suggested
833using hex escapes instead of octal ones, and using the \code{\e n},
834\code{\e t}, \code{\e r} escapes for the appropriate characters, and
835implemented this new formatting.
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000836
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000837\item Syntax errors detected at compile-time can now raise exceptions
838containing the filename and line number of the error, a pleasant side
839effect of the compiler reorganization done by Jeremy Hylton.
840
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000841\item C extensions which import other modules have been changed to use
842\function{PyImport_ImportModule()}, which means that they will use any
843import hooks that have been installed. This is also encouraged for
844third-party extensions that need to import some other module from C
845code.
846
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000847\item The size of the Unicode character database was shrunk by another
848340K thanks to Fredrik Lundh.
Andrew M. Kuchling91834c62001-01-22 19:51:13 +0000849
Andrew M. Kuchlingac1abe02001-03-23 03:52:46 +0000850\item Some new ports were contributed: MacOS X (by Steven Majewski),
Andrew M. Kuchlingdb7657d2001-04-12 03:37:19 +0000851Cygwin (by Jason Tishler); RISCOS (by Dietmar Schwertberger); Unixware~7
852(by Billy G. Allie).
Andrew M. Kuchlingac1abe02001-03-23 03:52:46 +0000853
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000854\end{itemize}
855
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000856And there's the usual list of minor bugfixes, minor memory leaks,
857docstring edits, and other tweaks, too lengthy to be worth itemizing;
858see the CVS logs for the full details if you want them.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000859
860
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000861%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000862\section{Acknowledgements}
863
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000864The author would like to thank the following people for offering
865suggestions on various drafts of this article: Graeme Cross, David
866Goodger, Jay Graves, Michael Hudson, Marc-Andr\'e Lemburg, Fredrik
867Lundh, Neil Schemenauer, Thomas Wouters.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000868
869\end{document}