blob: 4a5ad7f0ec803a1826fdf91d58f5b353f8518e56 [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}
10\authoraddress{\email{amk1@bigfoot.com}}
11\begin{document}
12\maketitle\tableofcontents
13
14\section{Introduction}
15
Andrew M. Kuchlingdb7657d2001-04-12 03:37:19 +000016It's that time again... time for a new Python release, Python 2.1.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +000017One recent goal of the Python development team has been to accelerate
18the pace of new releases, with a new release coming every 6 to 9
19months. 2.1 is the first release to come out at this faster pace, with
20the first alpha appearing in January, 3 months after the final version
21of 2.0 was released.
22
23This article explains the new features in 2.1. While there aren't as
24many changes in 2.1 as there were in Python 2.0, there are still some
25pleasant surprises in store. 2.1 is the first release to be steered
26through the use of Python Enhancement Proposals, or PEPs, so most of
27the sizable changes have accompanying PEPs that provide more complete
28documentation and a design rationale for the change. This article
29doesn't attempt to document the new features completely, but simply
30provides an overview of the new features for Python programmers.
31Refer to the Python 2.1 documentation, or to the specific PEP, for
32more details about any new feature that particularly interests you.
33
Andrew M. Kuchlingb39fa8a2001-07-19 00:29:48 +000034The final release of Python 2.1 was made on April 17, 2001.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +000035
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +000036%======================================================================
37\section{PEP 227: Nested Scopes}
38
39The largest change in Python 2.1 is to Python's scoping rules. In
40Python 2.0, at any given time there are at most three namespaces used
41to look up variable names: local, module-level, and the built-in
42namespace. This often surprised people because it didn't match their
43intuitive expectations. For example, a nested recursive function
44definition doesn't work:
45
46\begin{verbatim}
47def f():
48 ...
49 def g(value):
50 ...
51 return g(value-1) + 1
52 ...
53\end{verbatim}
54
55The function \function{g()} will always raise a \exception{NameError}
56exception, because the binding of the name \samp{g} isn't in either
57its local namespace or in the module-level namespace. This isn't much
58of a problem in practice (how often do you recursively define interior
59functions like this?), but this also made using the \keyword{lambda}
60statement clumsier, and this was a problem in practice. In code which
61uses \keyword{lambda} you can often find local variables being copied
62by passing them as the default values of arguments.
63
64\begin{verbatim}
65def find(self, name):
66 "Return list of any entries equal to 'name'"
67 L = filter(lambda x, name=name: x == name,
68 self.list_attribute)
69 return L
70\end{verbatim}
71
72The readability of Python code written in a strongly functional style
73suffers greatly as a result.
74
75The most significant change to Python 2.1 is that static scoping has
76been added to the language to fix this problem. As a first effect,
77the \code{name=name} default argument is now unnecessary in the above
78example. Put simply, when a given variable name is not assigned a
79value within a function (by an assignment, or the \keyword{def},
80\keyword{class}, or \keyword{import} statements), references to the
81variable will be looked up in the local namespace of the enclosing
82scope. A more detailed explanation of the rules, and a dissection of
83the implementation, can be found in the PEP.
84
85This change may cause some compatibility problems for code where the
86same variable name is used both at the module level and as a local
87variable within a function that contains further function definitions.
88This seems rather unlikely though, since such code would have been
Andrew M. Kuchling61af5602001-03-03 03:25:04 +000089pretty confusing to read in the first place.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +000090
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +000091One side effect of the change is that the \code{from \var{module}
92import *} and \keyword{exec} statements have been made illegal inside
93a function scope under certain conditions. The Python reference
94manual has said all along that \code{from \var{module} import *} is
95only legal at the top level of a module, but the CPython interpreter
96has never enforced this before. As part of the implementation of
97nested scopes, the compiler which turns Python source into bytecodes
98has to generate different code to access variables in a containing
99scope. \code{from \var{module} import *} and \keyword{exec} make it
100impossible for the compiler to figure this out, because they add names
101to the local namespace that are unknowable at compile time.
102Therefore, if a function contains function definitions or
103\keyword{lambda} expressions with free variables, the compiler will
104flag this by raising a \exception{SyntaxError} exception.
105
106To make the preceding explanation a bit clearer, here's an example:
107
108\begin{verbatim}
109x = 1
110def f():
111 # The next line is a syntax error
112 exec 'x=2'
113 def g():
114 return x
115\end{verbatim}
116
117Line 4 containing the \keyword{exec} statement is a syntax error,
118since \keyword{exec} would define a new local variable named \samp{x}
119whose value should be accessed by \function{g()}.
120
121This shouldn't be much of a limitation, since \keyword{exec} is rarely
122used in most Python code (and when it is used, it's often a sign of a
123poor design anyway).
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000124
Andrew M. Kuchling61af5602001-03-03 03:25:04 +0000125Compatibility concerns have led to nested scopes being introduced
126gradually; in Python 2.1, they aren't enabled by default, but can be
127turned on within a module by using a future statement as described in
128PEP 236. (See the following section for further discussion of PEP
129236.) In Python 2.2, nested scopes will become the default and there
130will be no way to turn them off, but users will have had all of 2.1's
131lifetime to fix any breakage resulting from their introduction.
132
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000133\begin{seealso}
134
135\seepep{227}{Statically Nested Scopes}{Written and implemented by
136Jeremy Hylton.}
137
138\end{seealso}
139
140
141%======================================================================
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000142\section{PEP 236: \module{__future__} Directives}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000143
Andrew M. Kuchling61af5602001-03-03 03:25:04 +0000144The reaction to nested scopes was widespread concern about the dangers
145of breaking code with the 2.1 release, and it was strong enough to
146make the Pythoneers take a more conservative approach. This approach
147consists of introducing a convention for enabling optional
148functionality in release N that will become compulsory in release N+1.
149
150The syntax uses a \code{from...import} statement using the reserved
151module name \module{__future__}. Nested scopes can be enabled by the
152following statement:
153
154\begin{verbatim}
155from __future__ import nested_scopes
156\end{verbatim}
157
158While it looks like a normal \keyword{import} statement, it's not;
159there are strict rules on where such a future statement can be put.
160They can only be at the top of a module, and must precede any Python
161code or regular \keyword{import} statements. This is because such
162statements can affect how the Python bytecode compiler parses code and
163generates bytecode, so they must precede any statement that will
164result in bytecodes being produced.
165
166\begin{seealso}
167
168\seepep{236}{Back to the \module{__future__}}{Written by Tim Peters,
169and primarily implemented by Jeremy Hylton.}
170
171\end{seealso}
Andrew M. Kuchlingf228fd12001-01-22 17:52:19 +0000172
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000173%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000174\section{PEP 207: Rich Comparisons}
175
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000176In earlier versions, Python's support for implementing comparisons on
177user-defined classes and extension types was quite simple. Classes
178could implement a \method{__cmp__} method that was given two instances
179of a class, and could only return 0 if they were equal or +1 or -1 if
180they weren't; the method couldn't raise an exception or return
181anything other than a Boolean value. Users of Numeric Python often
182found this model too weak and restrictive, because in the
183number-crunching programs that numeric Python is used for, it would be
184more useful to be able to perform elementwise comparisons of two
185matrices, returning a matrix containing the results of a given
186comparison for each element. If the two matrices are of different
187sizes, then the compare has to be able to raise an exception to signal
188the error.
189
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000190In Python 2.1, rich comparisons were added in order to support this
191need. Python classes can now individually overload each of the
192\code{<}, \code{<=}, \code{>}, \code{>=}, \code{==}, and \code{!=}
193operations. The new magic method names are:
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000194
195\begin{tableii}{c|l}{code}{Operation}{Method name}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000196 \lineii{<}{\method{__lt__}} \lineii{<=}{\method{__le__}}
197 \lineii{>}{\method{__gt__}} \lineii{>=}{\method{__ge__}}
198 \lineii{==}{\method{__eq__}} \lineii{!=}{\method{__ne__}}
199 \end{tableii}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000200
201(The magic methods are named after the corresponding Fortran operators
202\code{.LT.}. \code{.LE.}, \&c. Numeric programmers are almost
203certainly quite familar with these names and will find them easy to
204remember.)
205
206Each of these magic methods is of the form \code{\var{method}(self,
207other)}, where \code{self} will be the object on the left-hand side of
208the operator, while \code{other} will be the object on the right-hand
209side. For example, the expression \code{A < B} will cause
210\code{A.__lt__(B)} to be called.
211
212Each of these magic methods can return anything at all: a Boolean, a
213matrix, a list, or any other Python object. Alternatively they can
214raise an exception if the comparison is impossible, inconsistent, or
215otherwise meaningless.
216
217The built-in \function{cmp(A,B)} function can use the rich comparison
218machinery, and now accepts an optional argument specifying which
219comparison operation to use; this is given as one of the strings
220\code{"<"}, \code{"<="}, \code{">"}, \code{">="}, \code{"=="}, or
221\code{"!="}. If called without the optional third argument,
222\function{cmp()} will only return -1, 0, or +1 as in previous versions
223of Python; otherwise it will call the appropriate method and can
224return any Python object.
225
226There are also corresponding changes of interest to C programmers;
227there's a new slot \code{tp_richcmp} in type objects and an API for
228performing a given rich comparison. I won't cover the C API here, but
Andrew M. Kuchlingbf140142001-02-28 22:10:07 +0000229will refer you to PEP 207, or to 2.1's C API documentation, for the
230full list of related functions.
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000231
232\begin{seealso}
233
234\seepep{207}{Rich Comparisions}{Written by Guido van Rossum, heavily
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000235based on earlier work by David Ascher, and implemented by Guido van
236Rossum.}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000237
238\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000239
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000240%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000241\section{PEP 230: Warning Framework}
242
243Over its 10 years of existence, Python has accumulated a certain
244number of obsolete modules and features along the way. It's difficult
245to know when a feature is safe to remove, since there's no way of
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000246knowing how much code uses it --- perhaps no programs depend on the
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000247feature, or perhaps many do. To enable removing old features in a
248more structured way, a warning framework was added. When the Python
249developers want to get rid of a feature, it will first trigger a
250warning in the next version of Python. The following Python version
251can then drop the feature, and users will have had a full release
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000252cycle to remove uses of the old feature.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000253
254Python 2.1 adds the warning framework to be used in this scheme. It
255adds a \module{warnings} module that provide functions to issue
256warnings, and to filter out warnings that you don't want to be
257displayed. Third-party modules can also use this framework to
258deprecate old features that they no longer wish to support.
259
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000260For example, in Python 2.1 the \module{regex} module is deprecated, so
261importing it causes a warning to be printed:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000262
263\begin{verbatim}
264>>> import regex
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000265__main__:1: DeprecationWarning: the regex module
266 is deprecated; please use the re module
267>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000268\end{verbatim}
269
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000270Warnings can be issued by calling the \function{warnings.warn}
271function:
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000272
273\begin{verbatim}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000274warnings.warn("feature X no longer supported")
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000275\end{verbatim}
276
277The first parameter is the warning message; an additional optional
278parameters can be used to specify a particular warning category.
279
280Filters can be added to disable certain warnings; a regular expression
281pattern can be applied to the message or to the module name in order
282to suppress a warning. For example, you may have a program that uses
283the \module{regex} module and not want to spare the time to convert it
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000284to use the \module{re} module right now. The warning can be
285suppressed by calling
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000286
287\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000288import warnings
289warnings.filterwarnings(action = 'ignore',
290 message='.*regex module is deprecated',
291 category=DeprecationWarning,
292 module = '__main__')
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000293\end{verbatim}
294
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000295This adds a filter that will apply only to warnings of the class
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000296\class{DeprecationWarning} triggered in the \module{__main__} module,
297and applies a regular expression to only match the message about the
298\module{regex} module being deprecated, and will cause such warnings
299to be ignored. Warnings can also be printed only once, printed every
300time the offending code is executed, or turned into exceptions that
301will cause the program to stop (unless the exceptions are caught in
302the usual way, of course).
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000303
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000304Functions were also added to Python's C API for issuing warnings;
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000305refer to PEP 230 or to Python's API documentation for the details.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000306
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000307\begin{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000308
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000309\seepep{5}{Guidelines for Language Evolution}{Written
310by Paul Prescod, to specify procedures to be followed when removing
311old features from Python. The policy described in this PEP hasn't
312been officially adopted, but the eventual policy probably won't be too
313different from Prescod's proposal.}
314
315\seepep{230}{Warning Framework}{Written and implemented by Guido van
316Rossum.}
317
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000318\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000319
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000320%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000321\section{PEP 229: New Build System}
322
323When compiling Python, the user had to go in and edit the
324\file{Modules/Setup} file in order to enable various additional
325modules; the default set is relatively small and limited to modules
326that compile on most Unix platforms. This means that on Unix
327platforms with many more features, most notably Linux, Python
328installations often don't contain all useful modules they could.
329
330Python 2.0 added the Distutils, a set of modules for distributing and
331installing extensions. In Python 2.1, the Distutils are used to
332compile much of the standard library of extension modules,
Andrew M. Kuchlingf33c1182001-01-23 02:48:26 +0000333autodetecting which ones are supported on the current machine. It's
334hoped that this will make Python installations easier and more
335featureful.
336
337Instead of having to edit the \file{Modules/Setup} file in order to
338enable modules, a \file{setup.py} script in the top directory of the
339Python source distribution is run at build time, and attempts to
340discover which modules can be enabled by examining the modules and
Andrew M. Kuchling8bad9932001-02-28 22:39:15 +0000341header files on the system. If a module is configured in
342\file{Modules/Setup}, the \file{setup.py} script won't attempt to
343compile that module and will defer to the \file{Modules/Setup} file's
344contents. This provides a way to specific any strange command-line
345flags or libraries that are required for a specific platform.
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000346
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000347In another far-reaching change to the build mechanism, Neil
348Schemenauer restructured things so Python now uses a single makefile
349that isn't recursive, instead of makefiles in the top directory and in
Andrew M. Kuchling8bad9932001-02-28 22:39:15 +0000350each of the \file{Python/}, \file{Parser/}, \file{Objects/}, and
351\file{Modules/} subdirectories. This makes building Python faster
352and also makes hacking the Makefiles clearer and simpler.
Andrew M. Kuchling4308d3c2001-01-29 17:36:53 +0000353
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000354\begin{seealso}
355
356\seepep{229}{Using Distutils to Build Python}{Written
357and implemented by A.M. Kuchling.}
358
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000359\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000360
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000361%======================================================================
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000362\section{PEP 205: Weak References}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000363
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000364Weak references, available through the \module{weakref} module, are a
365minor but useful new data type in the Python programmer's toolbox.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000366
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000367Storing a reference to an object (say, in a dictionary or a list) has
368the side effect of keeping that object alive forever. There are a few
369specific cases where this behaviour is undesirable, object caches
370being the most common one, and another being circular references in
371data structures such as trees.
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000372
Andrew M. Kuchling15ad28c2001-02-14 02:44:18 +0000373For example, consider a memoizing function that caches the results of
374another function \function{f(\var{x})} by storing the function's
375argument and its result in a dictionary:
376
377\begin{verbatim}
378_cache = {}
379def memoize(x):
380 if _cache.has_key(x):
381 return _cache[x]
382
383 retval = f(x)
384
385 # Cache the returned object
386 _cache[x] = retval
387
388 return retval
389\end{verbatim}
390
391This version works for simple things such as integers, but it has a
392side effect; the \code{_cache} dictionary holds a reference to the
393return values, so they'll never be deallocated until the Python
394process exits and cleans up This isn't very noticeable for integers,
395but if \function{f()} returns an object, or a data structure that
396takes up a lot of memory, this can be a problem.
397
398Weak references provide a way to implement a cache that won't keep
399objects alive beyond their time. If an object is only accessible
400through weak references, the object will be deallocated and the weak
401references will now indicate that the object it referred to no longer
402exists. A weak reference to an object \var{obj} is created by calling
403\code{wr = weakref.ref(\var{obj})}. The object being referred to is
404returned by calling the weak reference as if it were a function:
405\code{wr()}. It will return the referenced object, or \code{None} if
406the object no longer exists.
407
408This makes it possible to write a \function{memoize()} function whose
409cache doesn't keep objects alive, by storing weak references in the
410cache.
411
412\begin{verbatim}
413_cache = {}
414def memoize(x):
415 if _cache.has_key(x):
416 obj = _cache[x]()
417 # If weak reference object still exists,
418 # return it
419 if obj is not None: return obj
420
421 retval = f(x)
422
423 # Cache a weak reference
424 _cache[x] = weakref.ref(retval)
425
426 return retval
427\end{verbatim}
428
429The \module{weakref} module also allows creating proxy objects which
430behave like weak references --- an object referenced only by proxy
431objects is deallocated -- but instead of requiring an explicit call to
432retrieve the object, the proxy transparently forwards all operations
433to the object as long as the object still exists. If the object is
434deallocated, attempting to use a proxy will cause a
435\exception{weakref.ReferenceError} exception to be raised.
436
437\begin{verbatim}
438proxy = weakref.proxy(obj)
439proxy.attr # Equivalent to obj.attr
440proxy.meth() # Equivalent to obj.meth()
441del obj
442proxy.attr # raises weakref.ReferenceError
443\end{verbatim}
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000444
445\begin{seealso}
446
447\seepep{205}{Weak References}{Written and implemented by
448Fred~L. Drake,~Jr.}
449
450\end{seealso}
451
452%======================================================================
Andrew M. Kuchlingef85cc82001-03-23 03:29:08 +0000453\section{PEP 232: Function Attributes}
454
455In Python 2.1, functions can now have arbitrary information attached
456to them. People were often using docstrings to hold information about
457functions and methods, because the \code{__doc__} attribute was the
458only way of attaching any information to a function. For example, in
459the Zope Web application server, functions are marked as safe for
460public access by having a docstring, and in John Aycock's SPARK
461parsing framework, docstrings hold parts of the BNF grammar to be
462parsed. This overloading is unfortunate, since docstrings are really
463intended to hold a function's documentation; for example, it means you
464can't properly document functions intended for private use in Zope.
465
466Arbitrary attributes can now be set and retrieved on functions using the
467regular Python syntax:
468
469\begin{verbatim}
470def f(): pass
471
472f.publish = 1
473f.secure = 1
474f.grammar = "A ::= B (C D)*"
475\end{verbatim}
476
477The dictionary containing attributes can be accessed as the function's
478\member{__dict__}. Unlike the \member{__dict__} attribute of class
479instances, in functions you can actually assign a new dictionary to
480\member{__dict__}, though the new value is restricted to a regular
481Python dictionary; you \emph{can't} be tricky and set it to a
482\class{UserDict} instance, or any other random object that behaves
483like a mapping.
484
485\begin{seealso}
486
487\seepep{232}{Function Attributes}{Written and implemented by Barry
488Warsaw.}
489
490\end{seealso}
491
492
493%======================================================================
494
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000495\section{PEP 235: Case-Insensitive Platforms and \keyword{import}}
496
Andrew M. Kuchling8bad9932001-02-28 22:39:15 +0000497Some operating systems have filesystems that are case-insensitive,
498MacOS and Windows being the primary examples; on these systems, it's
499impossible to distinguish the filenames \samp{FILE.PY} and
500\samp{file.py}, even though they do store the file's name
501in its original case (they're case-preserving, too).
502
503In Python 2.1, the \keyword{import} statement will work to simulate
504case-sensitivity on case-insensitive platforms. Python will now
505search for the first case-sensitive match by default, raising an
506\exception{ImportError} if no such file is found, so \code{import file}
507will not import a module named \samp{FILE.PY}. Case-insensitive
Fred Drake9ad526f2001-04-12 04:11:21 +0000508matching can be requested by setting the \envvar{PYTHONCASEOK} environment
Andrew M. Kuchling8bad9932001-02-28 22:39:15 +0000509variable before starting the Python interpreter.
Andrew M. Kuchling74d18ed2001-02-28 22:22:40 +0000510
511%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000512\section{PEP 217: Interactive Display Hook}
513
514When using the Python interpreter interactively, the output of
515commands is displayed using the built-in \function{repr()} function.
Andrew M. Kuchlingef85cc82001-03-23 03:29:08 +0000516In Python 2.1, the variable \function{sys.displayhook} can be set to a
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000517callable object which will be called instead of \function{repr()}.
518For example, you can set it to a special pretty-printing function:
519
520\begin{verbatim}
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000521>>> # Create a recursive data structure
522... L = [1,2,3]
523>>> L.append(L)
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000524>>> L # Show Python's default output
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000525[1, 2, 3, [...]]
526>>> # Use pprint.pprint() as the display function
527... import sys, pprint
528>>> sys.displayhook = pprint.pprint
529>>> L
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000530[1, 2, 3, <Recursion on list with id=135143996>]
531>>>
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000532\end{verbatim}
533
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000534\begin{seealso}
535
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000536\seepep{217}{Display Hook for Interactive Use}{Written and implemented
537by Moshe Zadka.}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000538
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000539\end{seealso}
540
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000541%======================================================================
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000542\section{PEP 208: New Coercion Model}
543
544How numeric coercion is done at the C level was significantly
545modified. This will only affect the authors of C extensions to
546Python, allowing them more flexibility in writing extension types that
547support numeric operations.
548
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000549Extension types can now set the type flag \code{Py_TPFLAGS_CHECKTYPES}
550in their \code{PyTypeObject} structure to indicate that they support
551the new coercion model. In such extension types, the numeric slot
552functions can no longer assume that they'll be passed two arguments of
553the same type; instead they may be passed two arguments of differing
554types, and can then perform their own internal coercion. If the slot
555function is passed a type it can't handle, it can indicate the failure
556by returning a reference to the \code{Py_NotImplemented} singleton
557value. The numeric functions of the other type will then be tried,
558and perhaps they can handle the operation; if the other type also
559returns \code{Py_NotImplemented}, then a \exception{TypeError} will be
560raised. Numeric methods written in Python can also return
561\code{Py_NotImplemented}, causing the interpreter to act as if the
562method did not exist (perhaps raising a \exception{TypeError}, perhaps
563trying another object's numeric methods).
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000564
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000565\begin{seealso}
566
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000567\seepep{208}{Reworking the Coercion Model}{Written and implemented by
568Neil Schemenauer, heavily based upon earlier work by Marc-Andr\'e
569Lemburg. Read this to understand the fine points of how numeric
570operations will now be processed at the C level.}
571
Andrew M. Kuchlingb216ab62001-01-22 16:15:44 +0000572\end{seealso}
Andrew M. Kuchling90cecee2001-01-22 04:02:09 +0000573
Andrew M. Kuchling6a360bd2001-02-05 02:47:52 +0000574%======================================================================
Andrew M. Kuchlingef85cc82001-03-23 03:29:08 +0000575\section{PEP 241: Metadata in Python Packages}
576
577A common complaint from Python users is that there's no single catalog
578of all the Python modules in existence. T.~Middleton's Vaults of
579Parnassus at \url{http://www.vex.net/parnassus} are the largest
580catalog of Python modules, but registering software at the Vaults is
581optional, and many people don't bother.
582
583As a first small step toward fixing the problem, Python software
584packaged using the Distutils \command{sdist} command will include a
585file named \file{PKG-INFO} containing information about the package
586such as its name, version, and author (metadata, in cataloguing
587terminology). PEP 241 contains the full list of fields that can be
588present in the \file{PKG-INFO} file. As people began to package their
589software using Python 2.1, more and more packages will include
590metadata, making it possible to build automated cataloguing systems
591and experiment with them. With the result experience, perhaps it'll
592be possible to design a really good catalog and then build support for
593it into Python 2.2. For example, the Distutils \command{sdist}
594and \command{bdist_*} commands could support a \option{upload} option
595that would automatically upload your package to a catalog server.
596
597You can start creating packages containing \file{PKG-INFO} even if
598you're not using Python 2.1, since a new release of the Distutils will
599be made for users of earlier Python versions. Version 1.0.2 of the
600Distutils includes the changes described in PEP 241, as well as
601various bugfixes and enhancements. It will be available from
602the Distutils SIG at \url{http://www.python.org/sigs/distutils-sig}.
603
604% XXX update when I actually release 1.0.2
605
606\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
800\url{http://www.amk.ca/python/dev/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}