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