blob: f7998e6978b537c1f926ec12bae1d49b539b78a2 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001****************************
Georg Brandlc62ef8b2009-01-03 20:55:06 +00002 What's New in Python 2.0
Georg Brandl8ec7f652007-08-15 14:28:01 +00003****************************
4
5:Author: A.M. Kuchling and Moshe Zadka
6
7.. |release| replace:: 1.02
8
Georg Brandlb19be572007-12-29 10:57:00 +00009.. $Id: whatsnew20.tex 50964 2006-07-30 03:03:43Z fred.drake $
Georg Brandl8ec7f652007-08-15 14:28:01 +000010
11
12Introduction
13============
14
15A new release of Python, version 2.0, was released on October 16, 2000. This
16article covers the exciting new features in 2.0, highlights some other useful
17changes, and points out a few incompatible changes that may require rewriting
18code.
19
20Python's development never completely stops between releases, and a steady flow
21of bug fixes and improvements are always being submitted. A host of minor fixes,
22a few optimizations, additional docstrings, and better error messages went into
232.0; to list them all would be impossible, but they're certainly significant.
24Consult the publicly-available CVS logs if you want to see the full list. This
25progress is due to the five developers working for PythonLabs are now getting
26paid to spend their days fixing bugs, and also due to the improved communication
27resulting from moving to SourceForge.
28
Georg Brandlb19be572007-12-29 10:57:00 +000029.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +000030
31
32What About Python 1.6?
33======================
34
35Python 1.6 can be thought of as the Contractual Obligations Python release.
36After the core development team left CNRI in May 2000, CNRI requested that a 1.6
37release be created, containing all the work on Python that had been performed at
38CNRI. Python 1.6 therefore represents the state of the CVS tree as of May 2000,
39with the most significant new feature being Unicode support. Development
40continued after May, of course, so the 1.6 tree received a few fixes to ensure
41that it's forward-compatible with Python 2.0. 1.6 is therefore part of Python's
42evolution, and not a side branch.
43
44So, should you take much interest in Python 1.6? Probably not. The 1.6final
45and 2.0beta1 releases were made on the same day (September 5, 2000), the plan
46being to finalize Python 2.0 within a month or so. If you have applications to
47maintain, there seems little point in breaking things by moving to 1.6, fixing
48them, and then having another round of breakage within a month by moving to 2.0;
49you're better off just going straight to 2.0. Most of the really interesting
50features described in this document are only in 2.0, because a lot of work was
51done between May and September.
52
Georg Brandlb19be572007-12-29 10:57:00 +000053.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +000054
55
56New Development Process
57=======================
58
59The most important change in Python 2.0 may not be to the code at all, but to
60how Python is developed: in May 2000 the Python developers began using the tools
61made available by SourceForge for storing source code, tracking bug reports,
62and managing the queue of patch submissions. To report bugs or submit patches
63for Python 2.0, use the bug tracking and patch manager tools available from
64Python's project page, located at http://sourceforge.net/projects/python/.
65
66The most important of the services now hosted at SourceForge is the Python CVS
67tree, the version-controlled repository containing the source code for Python.
68Previously, there were roughly 7 or so people who had write access to the CVS
69tree, and all patches had to be inspected and checked in by one of the people on
70this short list. Obviously, this wasn't very scalable. By moving the CVS tree
71to SourceForge, it became possible to grant write access to more people; as of
72September 2000 there were 27 people able to check in changes, a fourfold
73increase. This makes possible large-scale changes that wouldn't be attempted if
74they'd have to be filtered through the small group of core developers. For
75example, one day Peter Schneider-Kamp took it into his head to drop K&R C
76compatibility and convert the C source for Python to ANSI C. After getting
77approval on the python-dev mailing list, he launched into a flurry of checkins
78that lasted about a week, other developers joined in to help, and the job was
79done. If there were only 5 people with write access, probably that task would
80have been viewed as "nice, but not worth the time and effort needed" and it
81would never have gotten done.
82
83The shift to using SourceForge's services has resulted in a remarkable increase
84in the speed of development. Patches now get submitted, commented on, revised
85by people other than the original submitter, and bounced back and forth between
86people until the patch is deemed worth checking in. Bugs are tracked in one
87central location and can be assigned to a specific person for fixing, and we can
88count the number of open bugs to measure progress. This didn't come without a
89cost: developers now have more e-mail to deal with, more mailing lists to
90follow, and special tools had to be written for the new environment. For
91example, SourceForge sends default patch and bug notification e-mail messages
92that are completely unhelpful, so Ka-Ping Yee wrote an HTML screen-scraper that
93sends more useful messages.
94
95The ease of adding code caused a few initial growing pains, such as code was
96checked in before it was ready or without getting clear agreement from the
97developer group. The approval process that has emerged is somewhat similar to
98that used by the Apache group. Developers can vote +1, +0, -0, or -1 on a patch;
99+1 and -1 denote acceptance or rejection, while +0 and -0 mean the developer is
100mostly indifferent to the change, though with a slight positive or negative
101slant. The most significant change from the Apache model is that the voting is
102essentially advisory, letting Guido van Rossum, who has Benevolent Dictator For
103Life status, know what the general opinion is. He can still ignore the result of
104a vote, and approve or reject a change even if the community disagrees with him.
105
106Producing an actual patch is the last step in adding a new feature, and is
107usually easy compared to the earlier task of coming up with a good design.
108Discussions of new features can often explode into lengthy mailing list threads,
109making the discussion hard to follow, and no one can read every posting to
110python-dev. Therefore, a relatively formal process has been set up to write
111Python Enhancement Proposals (PEPs), modelled on the Internet RFC process. PEPs
112are draft documents that describe a proposed new feature, and are continually
113revised until the community reaches a consensus, either accepting or rejecting
114the proposal. Quoting from the introduction to PEP 1, "PEP Purpose and
115Guidelines":
116
117
118.. epigraph::
119
120 PEP stands for Python Enhancement Proposal. A PEP is a design document
121 providing information to the Python community, or describing a new feature for
122 Python. The PEP should provide a concise technical specification of the feature
123 and a rationale for the feature.
124
125 We intend PEPs to be the primary mechanisms for proposing new features, for
126 collecting community input on an issue, and for documenting the design decisions
127 that have gone into Python. The PEP author is responsible for building
128 consensus within the community and documenting dissenting opinions.
129
130Read the rest of PEP 1 for the details of the PEP editorial process, style, and
131format. PEPs are kept in the Python CVS tree on SourceForge, though they're not
132part of the Python 2.0 distribution, and are also available in HTML form from
Georg Brandl06f3b3b2014-10-29 08:36:35 +0100133https://www.python.org/peps/. As of September 2000, there are 25 PEPS, ranging
Georg Brandl8ec7f652007-08-15 14:28:01 +0000134from PEP 201, "Lockstep Iteration", to PEP 225, "Elementwise/Objectwise
135Operators".
136
Georg Brandlb19be572007-12-29 10:57:00 +0000137.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000138
139
140Unicode
141=======
142
143The largest new feature in Python 2.0 is a new fundamental data type: Unicode
144strings. Unicode uses 16-bit numbers to represent characters instead of the
1458-bit number used by ASCII, meaning that 65,536 distinct characters can be
146supported.
147
148The final interface for Unicode support was arrived at through countless often-
149stormy discussions on the python-dev mailing list, and mostly implemented by
150Marc-André Lemburg, based on a Unicode string type implementation by Fredrik
151Lundh. A detailed explanation of the interface was written up as :pep:`100`,
152"Python Unicode Integration". This article will simply cover the most
153significant points about the Unicode interfaces.
154
155In Python source code, Unicode strings are written as ``u"string"``. Arbitrary
156Unicode characters can be written using a new escape sequence, ``\uHHHH``, where
157*HHHH* is a 4-digit hexadecimal number from 0000 to FFFF. The existing
158``\xHHHH`` escape sequence can also be used, and octal escapes can be used for
159characters up to U+01FF, which is represented by ``\777``.
160
161Unicode strings, just like regular strings, are an immutable sequence type.
162They can be indexed and sliced, but not modified in place. Unicode strings have
163an ``encode( [encoding] )`` method that returns an 8-bit string in the desired
164encoding. Encodings are named by strings, such as ``'ascii'``, ``'utf-8'``,
165``'iso-8859-1'``, or whatever. A codec API is defined for implementing and
166registering new encodings that are then available throughout a Python program.
167If an encoding isn't specified, the default encoding is usually 7-bit ASCII,
168though it can be changed for your Python installation by calling the
169:func:`sys.setdefaultencoding(encoding)` function in a customised version of
170:file:`site.py`.
171
172Combining 8-bit and Unicode strings always coerces to Unicode, using the default
173ASCII encoding; the result of ``'a' + u'bc'`` is ``u'abc'``.
174
175New built-in functions have been added, and existing built-ins modified to
176support Unicode:
177
178* ``unichr(ch)`` returns a Unicode string 1 character long, containing the
179 character *ch*.
180
181* ``ord(u)``, where *u* is a 1-character regular or Unicode string, returns the
182 number of the character as an integer.
183
184* ``unicode(string [, encoding] [, errors] )`` creates a Unicode string
185 from an 8-bit string. ``encoding`` is a string naming the encoding to use. The
186 ``errors`` parameter specifies the treatment of characters that are invalid for
187 the current encoding; passing ``'strict'`` as the value causes an exception to
188 be raised on any encoding error, while ``'ignore'`` causes errors to be silently
189 ignored and ``'replace'`` uses U+FFFD, the official replacement character, in
190 case of any problems.
191
192* The :keyword:`exec` statement, and various built-ins such as ``eval()``,
193 ``getattr()``, and ``setattr()`` will also accept Unicode strings as well as
194 regular strings. (It's possible that the process of fixing this missed some
195 built-ins; if you find a built-in function that accepts strings but doesn't
196 accept Unicode strings at all, please report it as a bug.)
197
198A new module, :mod:`unicodedata`, provides an interface to Unicode character
199properties. For example, ``unicodedata.category(u'A')`` returns the 2-character
200string 'Lu', the 'L' denoting it's a letter, and 'u' meaning that it's
201uppercase. ``unicodedata.bidirectional(u'\u0660')`` returns 'AN', meaning that
202U+0660 is an Arabic number.
203
204The :mod:`codecs` module contains functions to look up existing encodings and
205register new ones. Unless you want to implement a new encoding, you'll most
206often use the :func:`codecs.lookup(encoding)` function, which returns a
2074-element tuple: ``(encode_func, decode_func, stream_reader, stream_writer)``.
208
209* *encode_func* is a function that takes a Unicode string, and returns a 2-tuple
210 ``(string, length)``. *string* is an 8-bit string containing a portion (perhaps
211 all) of the Unicode string converted into the given encoding, and *length* tells
212 you how much of the Unicode string was converted.
213
214* *decode_func* is the opposite of *encode_func*, taking an 8-bit string and
215 returning a 2-tuple ``(ustring, length)``, consisting of the resulting Unicode
216 string *ustring* and the integer *length* telling how much of the 8-bit string
217 was consumed.
218
219* *stream_reader* is a class that supports decoding input from a stream.
220 *stream_reader(file_obj)* returns an object that supports the :meth:`read`,
221 :meth:`readline`, and :meth:`readlines` methods. These methods will all
222 translate from the given encoding and return Unicode strings.
223
224* *stream_writer*, similarly, is a class that supports encoding output to a
225 stream. *stream_writer(file_obj)* returns an object that supports the
226 :meth:`write` and :meth:`writelines` methods. These methods expect Unicode
227 strings, translating them to the given encoding on output.
228
229For example, the following code writes a Unicode string into a file, encoding
230it as UTF-8::
231
232 import codecs
233
234 unistr = u'\u0660\u2000ab ...'
235
236 (UTF8_encode, UTF8_decode,
237 UTF8_streamreader, UTF8_streamwriter) = codecs.lookup('UTF-8')
238
239 output = UTF8_streamwriter( open( '/tmp/output', 'wb') )
240 output.write( unistr )
241 output.close()
242
243The following code would then read UTF-8 input from the file::
244
245 input = UTF8_streamreader( open( '/tmp/output', 'rb') )
246 print repr(input.read())
247 input.close()
248
249Unicode-aware regular expressions are available through the :mod:`re` module,
250which has a new underlying implementation called SRE written by Fredrik Lundh of
251Secret Labs AB.
252
253A ``-U`` command line option was added which causes the Python compiler to
254interpret all string literals as Unicode string literals. This is intended to be
255used in testing and future-proofing your Python code, since some future version
256of Python may drop support for 8-bit strings and provide only Unicode strings.
257
Georg Brandlb19be572007-12-29 10:57:00 +0000258.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000259
260
261List Comprehensions
262===================
263
264Lists are a workhorse data type in Python, and many programs manipulate a list
265at some point. Two common operations on lists are to loop over them, and either
266pick out the elements that meet a certain criterion, or apply some function to
267each element. For example, given a list of strings, you might want to pull out
268all the strings containing a given substring, or strip off trailing whitespace
269from each line.
270
271The existing :func:`map` and :func:`filter` functions can be used for this
272purpose, but they require a function as one of their arguments. This is fine if
273there's an existing built-in function that can be passed directly, but if there
274isn't, you have to create a little function to do the required work, and
275Python's scoping rules make the result ugly if the little function needs
276additional information. Take the first example in the previous paragraph,
277finding all the strings in the list containing a given substring. You could
278write the following to do it::
279
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000280 # Given the list L, make a list of all strings
Georg Brandl8ec7f652007-08-15 14:28:01 +0000281 # containing the substring S.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000282 sublist = filter( lambda s, substring=S:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000283 string.find(s, substring) != -1,
Georg Brandl7044b112009-01-03 21:04:55 +0000284 L)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000285
286Because of Python's scoping rules, a default argument is used so that the
287anonymous function created by the :keyword:`lambda` statement knows what
288substring is being searched for. List comprehensions make this cleaner::
289
290 sublist = [ s for s in L if string.find(s, S) != -1 ]
291
292List comprehensions have the form::
293
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000294 [ expression for expr in sequence1
Georg Brandl8ec7f652007-08-15 14:28:01 +0000295 for expr2 in sequence2 ...
Georg Brandl7044b112009-01-03 21:04:55 +0000296 for exprN in sequenceN
Georg Brandl8ec7f652007-08-15 14:28:01 +0000297 if condition ]
298
299The :keyword:`for`...\ :keyword:`in` clauses contain the sequences to be
300iterated over. The sequences do not have to be the same length, because they
301are *not* iterated over in parallel, but from left to right; this is explained
302more clearly in the following paragraphs. The elements of the generated list
303will be the successive values of *expression*. The final :keyword:`if` clause
304is optional; if present, *expression* is only evaluated and added to the result
305if *condition* is true.
306
307To make the semantics very clear, a list comprehension is equivalent to the
308following Python code::
309
310 for expr1 in sequence1:
311 for expr2 in sequence2:
312 ...
313 for exprN in sequenceN:
314 if (condition):
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000315 # Append the value of
316 # the expression to the
Georg Brandl8ec7f652007-08-15 14:28:01 +0000317 # resulting list.
318
319This means that when there are multiple :keyword:`for`...\ :keyword:`in`
320clauses, the resulting list will be equal to the product of the lengths of all
321the sequences. If you have two lists of length 3, the output list is 9 elements
322long::
323
324 seq1 = 'abc'
325 seq2 = (1,2,3)
326 >>> [ (x,y) for x in seq1 for y in seq2]
327 [('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1),
328 ('c', 2), ('c', 3)]
329
330To avoid introducing an ambiguity into Python's grammar, if *expression* is
331creating a tuple, it must be surrounded with parentheses. The first list
332comprehension below is a syntax error, while the second one is correct::
333
334 # Syntax error
335 [ x,y for x in seq1 for y in seq2]
336 # Correct
337 [ (x,y) for x in seq1 for y in seq2]
338
339The idea of list comprehensions originally comes from the functional programming
340language Haskell (http://www.haskell.org). Greg Ewing argued most effectively
341for adding them to Python and wrote the initial list comprehension patch, which
342was then discussed for a seemingly endless time on the python-dev mailing list
343and kept up-to-date by Skip Montanaro.
344
Georg Brandlb19be572007-12-29 10:57:00 +0000345.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000346
347
348Augmented Assignment
349====================
350
351Augmented assignment operators, another long-requested feature, have been added
352to Python 2.0. Augmented assignment operators include ``+=``, ``-=``, ``*=``,
353and so forth. For example, the statement ``a += 2`` increments the value of the
354variable ``a`` by 2, equivalent to the slightly lengthier ``a = a + 2``.
355
356The full list of supported assignment operators is ``+=``, ``-=``, ``*=``,
357``/=``, ``%=``, ``**=``, ``&=``, ``|=``, ``^=``, ``>>=``, and ``<<=``. Python
358classes can override the augmented assignment operators by defining methods
359named :meth:`__iadd__`, :meth:`__isub__`, etc. For example, the following
360:class:`Number` class stores a number and supports using += to create a new
361instance with an incremented value.
362
Georg Brandlb19be572007-12-29 10:57:00 +0000363.. The empty groups below prevent conversion to guillemets.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000364
365::
366
367 class Number:
368 def __init__(self, value):
369 self.value = value
370 def __iadd__(self, increment):
Georg Brandl7044b112009-01-03 21:04:55 +0000371 return Number( self.value + increment)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000372
373 n = Number(5)
374 n += 3
375 print n.value
376
377The :meth:`__iadd__` special method is called with the value of the increment,
378and should return a new instance with an appropriately modified value; this
379return value is bound as the new value of the variable on the left-hand side.
380
381Augmented assignment operators were first introduced in the C programming
382language, and most C-derived languages, such as :program:`awk`, C++, Java, Perl,
383and PHP also support them. The augmented assignment patch was implemented by
384Thomas Wouters.
385
Georg Brandlb19be572007-12-29 10:57:00 +0000386.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000387
388
389String Methods
390==============
391
392Until now string-manipulation functionality was in the :mod:`string` module,
393which was usually a front-end for the :mod:`strop` module written in C. The
394addition of Unicode posed a difficulty for the :mod:`strop` module, because the
395functions would all need to be rewritten in order to accept either 8-bit or
396Unicode strings. For functions such as :func:`string.replace`, which takes 3
397string arguments, that means eight possible permutations, and correspondingly
398complicated code.
399
400Instead, Python 2.0 pushes the problem onto the string type, making string
401manipulation functionality available through methods on both 8-bit strings and
402Unicode strings. ::
403
404 >>> 'andrew'.capitalize()
405 'Andrew'
406 >>> 'hostname'.replace('os', 'linux')
407 'hlinuxtname'
408 >>> 'moshe'.find('sh')
409 2
410
411One thing that hasn't changed, a noteworthy April Fools' joke notwithstanding,
412is that Python strings are immutable. Thus, the string methods return new
413strings, and do not modify the string on which they operate.
414
415The old :mod:`string` module is still around for backwards compatibility, but it
416mostly acts as a front-end to the new string methods.
417
418Two methods which have no parallel in pre-2.0 versions, although they did exist
419in JPython for quite some time, are :meth:`startswith` and :meth:`endswith`.
420``s.startswith(t)`` is equivalent to ``s[:len(t)] == t``, while
421``s.endswith(t)`` is equivalent to ``s[-len(t):] == t``.
422
423One other method which deserves special mention is :meth:`join`. The
424:meth:`join` method of a string receives one parameter, a sequence of strings,
425and is equivalent to the :func:`string.join` function from the old :mod:`string`
426module, with the arguments reversed. In other words, ``s.join(seq)`` is
427equivalent to the old ``string.join(seq, s)``.
428
Georg Brandlb19be572007-12-29 10:57:00 +0000429.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000430
431
432Garbage Collection of Cycles
433============================
434
435The C implementation of Python uses reference counting to implement garbage
436collection. Every Python object maintains a count of the number of references
437pointing to itself, and adjusts the count as references are created or
438destroyed. Once the reference count reaches zero, the object is no longer
439accessible, since you need to have a reference to an object to access it, and if
440the count is zero, no references exist any longer.
441
442Reference counting has some pleasant properties: it's easy to understand and
443implement, and the resulting implementation is portable, fairly fast, and reacts
444well with other libraries that implement their own memory handling schemes. The
445major problem with reference counting is that it sometimes doesn't realise that
446objects are no longer accessible, resulting in a memory leak. This happens when
447there are cycles of references.
448
449Consider the simplest possible cycle, a class instance which has a reference to
450itself::
451
452 instance = SomeClass()
453 instance.myself = instance
454
455After the above two lines of code have been executed, the reference count of
456``instance`` is 2; one reference is from the variable named ``'instance'``, and
457the other is from the ``myself`` attribute of the instance.
458
459If the next line of code is ``del instance``, what happens? The reference count
460of ``instance`` is decreased by 1, so it has a reference count of 1; the
461reference in the ``myself`` attribute still exists. Yet the instance is no
462longer accessible through Python code, and it could be deleted. Several objects
463can participate in a cycle if they have references to each other, causing all of
464the objects to be leaked.
465
466Python 2.0 fixes this problem by periodically executing a cycle detection
467algorithm which looks for inaccessible cycles and deletes the objects involved.
468A new :mod:`gc` module provides functions to perform a garbage collection,
469obtain debugging statistics, and tuning the collector's parameters.
470
471Running the cycle detection algorithm takes some time, and therefore will result
472in some additional overhead. It is hoped that after we've gotten experience
473with the cycle collection from using 2.0, Python 2.1 will be able to minimize
474the overhead with careful tuning. It's not yet obvious how much performance is
475lost, because benchmarking this is tricky and depends crucially on how often the
476program creates and destroys objects. The detection of cycles can be disabled
477when Python is compiled, if you can't afford even a tiny speed penalty or
478suspect that the cycle collection is buggy, by specifying the
479:option:`--without-cycle-gc` switch when running the :program:`configure`
480script.
481
482Several people tackled this problem and contributed to a solution. An early
483implementation of the cycle detection approach was written by Toby Kelsey. The
484current algorithm was suggested by Eric Tiedemann during a visit to CNRI, and
485Guido van Rossum and Neil Schemenauer wrote two different implementations, which
486were later integrated by Neil. Lots of other people offered suggestions along
487the way; the March 2000 archives of the python-dev mailing list contain most of
488the relevant discussion, especially in the threads titled "Reference cycle
489collection for Python" and "Finalization again".
490
Georg Brandlb19be572007-12-29 10:57:00 +0000491.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000492
493
494Other Core Changes
495==================
496
497Various minor changes have been made to Python's syntax and built-in functions.
498None of the changes are very far-reaching, but they're handy conveniences.
499
500
501Minor Language Changes
502----------------------
503
504A new syntax makes it more convenient to call a given function with a tuple of
505arguments and/or a dictionary of keyword arguments. In Python 1.5 and earlier,
506you'd use the :func:`apply` built-in function: ``apply(f, args, kw)`` calls the
507function :func:`f` with the argument tuple *args* and the keyword arguments in
508the dictionary *kw*. :func:`apply` is the same in 2.0, but thanks to a patch
509from Greg Ewing, ``f(*args, **kw)`` as a shorter and clearer way to achieve the
510same effect. This syntax is symmetrical with the syntax for defining
511functions::
512
513 def f(*args, **kw):
514 # args is a tuple of positional args,
515 # kw is a dictionary of keyword args
516 ...
517
518The :keyword:`print` statement can now have its output directed to a file-like
519object by following the :keyword:`print` with ``>> file``, similar to the
520redirection operator in Unix shells. Previously you'd either have to use the
521:meth:`write` method of the file-like object, which lacks the convenience and
522simplicity of :keyword:`print`, or you could assign a new value to
523``sys.stdout`` and then restore the old value. For sending output to standard
524error, it's much easier to write this::
525
526 print >> sys.stderr, "Warning: action field not supplied"
527
528Modules can now be renamed on importing them, using the syntax ``import module
529as name`` or ``from module import name as othername``. The patch was submitted
530by Thomas Wouters.
531
532A new format style is available when using the ``%`` operator; '%r' will insert
533the :func:`repr` of its argument. This was also added from symmetry
534considerations, this time for symmetry with the existing '%s' format style,
535which inserts the :func:`str` of its argument. For example, ``'%r %s' % ('abc',
536'abc')`` returns a string containing ``'abc' abc``.
537
538Previously there was no way to implement a class that overrode Python's built-in
539:keyword:`in` operator and implemented a custom version. ``obj in seq`` returns
540true if *obj* is present in the sequence *seq*; Python computes this by simply
541trying every index of the sequence until either *obj* is found or an
542:exc:`IndexError` is encountered. Moshe Zadka contributed a patch which adds a
543:meth:`__contains__` magic method for providing a custom implementation for
544:keyword:`in`. Additionally, new built-in objects written in C can define what
545:keyword:`in` means for them via a new slot in the sequence protocol.
546
547Earlier versions of Python used a recursive algorithm for deleting objects.
548Deeply nested data structures could cause the interpreter to fill up the C stack
549and crash; Christian Tismer rewrote the deletion logic to fix this problem. On
550a related note, comparing recursive objects recursed infinitely and crashed;
551Jeremy Hylton rewrote the code to no longer crash, producing a useful result
552instead. For example, after this code::
553
554 a = []
555 b = []
556 a.append(a)
557 b.append(b)
558
559The comparison ``a==b`` returns true, because the two recursive data structures
560are isomorphic. See the thread "trashcan and PR#7" in the April 2000 archives of
561the python-dev mailing list for the discussion leading up to this
562implementation, and some useful relevant links. Note that comparisons can now
563also raise exceptions. In earlier versions of Python, a comparison operation
564such as ``cmp(a,b)`` would always produce an answer, even if a user-defined
565:meth:`__cmp__` method encountered an error, since the resulting exception would
566simply be silently swallowed.
567
Georg Brandlb19be572007-12-29 10:57:00 +0000568.. Starting URL:
Georg Brandl06f3b3b2014-10-29 08:36:35 +0100569.. https://www.python.org/pipermail/python-dev/2000-April/004834.html
Georg Brandl8ec7f652007-08-15 14:28:01 +0000570
571Work has been done on porting Python to 64-bit Windows on the Itanium processor,
572mostly by Trent Mick of ActiveState. (Confusingly, ``sys.platform`` is still
573``'win32'`` on Win64 because it seems that for ease of porting, MS Visual C++
574treats code as 32 bit on Itanium.) PythonWin also supports Windows CE; see the
Georg Brandla4314c22009-10-11 20:16:16 +0000575Python CE page at http://pythonce.sourceforge.net/ for more information.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000576
577Another new platform is Darwin/MacOS X; initial support for it is in Python 2.0.
578Dynamic loading works, if you specify "configure --with-dyld --with-suffix=.x".
579Consult the README in the Python source distribution for more instructions.
580
581An attempt has been made to alleviate one of Python's warts, the often-confusing
582:exc:`NameError` exception when code refers to a local variable before the
583variable has been assigned a value. For example, the following code raises an
584exception on the :keyword:`print` statement in both 1.5.2 and 2.0; in 1.5.2 a
585:exc:`NameError` exception is raised, while 2.0 raises a new
586:exc:`UnboundLocalError` exception. :exc:`UnboundLocalError` is a subclass of
587:exc:`NameError`, so any existing code that expects :exc:`NameError` to be
588raised should still work. ::
589
590 def f():
591 print "i=",i
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000592 i = i + 1
Georg Brandl8ec7f652007-08-15 14:28:01 +0000593 f()
594
595Two new exceptions, :exc:`TabError` and :exc:`IndentationError`, have been
596introduced. They're both subclasses of :exc:`SyntaxError`, and are raised when
597Python code is found to be improperly indented.
598
599
600Changes to Built-in Functions
601-----------------------------
602
603A new built-in, :func:`zip(seq1, seq2, ...)`, has been added. :func:`zip`
604returns a list of tuples where each tuple contains the i-th element from each of
605the argument sequences. The difference between :func:`zip` and ``map(None,
606seq1, seq2)`` is that :func:`map` pads the sequences with ``None`` if the
607sequences aren't all of the same length, while :func:`zip` truncates the
608returned list to the length of the shortest argument sequence.
609
610The :func:`int` and :func:`long` functions now accept an optional "base"
611parameter when the first argument is a string. ``int('123', 10)`` returns 123,
612while ``int('123', 16)`` returns 291. ``int(123, 16)`` raises a
613:exc:`TypeError` exception with the message "can't convert non-string with
614explicit base".
615
616A new variable holding more detailed version information has been added to the
617:mod:`sys` module. ``sys.version_info`` is a tuple ``(major, minor, micro,
618level, serial)`` For example, in a hypothetical 2.0.1beta1, ``sys.version_info``
619would be ``(2, 0, 1, 'beta', 1)``. *level* is a string such as ``"alpha"``,
620``"beta"``, or ``"final"`` for a final release.
621
622Dictionaries have an odd new method, :meth:`setdefault(key, default)`, which
623behaves similarly to the existing :meth:`get` method. However, if the key is
624missing, :meth:`setdefault` both returns the value of *default* as :meth:`get`
625would do, and also inserts it into the dictionary as the value for *key*. Thus,
626the following lines of code::
627
628 if dict.has_key( key ): return dict[key]
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000629 else:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000630 dict[key] = []
631 return dict[key]
632
633can be reduced to a single ``return dict.setdefault(key, [])`` statement.
634
635The interpreter sets a maximum recursion depth in order to catch runaway
636recursion before filling the C stack and causing a core dump or GPF..
637Previously this limit was fixed when you compiled Python, but in 2.0 the maximum
638recursion depth can be read and modified using :func:`sys.getrecursionlimit` and
639:func:`sys.setrecursionlimit`. The default value is 1000, and a rough maximum
640value for a given platform can be found by running a new script,
641:file:`Misc/find_recursionlimit.py`.
642
Georg Brandlb19be572007-12-29 10:57:00 +0000643.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000644
645
646Porting to 2.0
647==============
648
649New Python releases try hard to be compatible with previous releases, and the
650record has been pretty good. However, some changes are considered useful
651enough, usually because they fix initial design decisions that turned out to be
652actively mistaken, that breaking backward compatibility can't always be avoided.
653This section lists the changes in Python 2.0 that may cause old Python code to
654break.
655
656The change which will probably break the most code is tightening up the
657arguments accepted by some methods. Some methods would take multiple arguments
658and treat them as a tuple, particularly various list methods such as
Georg Brandl821fc082010-08-01 21:26:45 +0000659:meth:`append` and :meth:`insert`. In earlier versions of Python, if ``L`` is
Georg Brandl8ec7f652007-08-15 14:28:01 +0000660a list, ``L.append( 1,2 )`` appends the tuple ``(1,2)`` to the list. In Python
6612.0 this causes a :exc:`TypeError` exception to be raised, with the message:
662'append requires exactly 1 argument; 2 given'. The fix is to simply add an
663extra set of parentheses to pass both values as a tuple: ``L.append( (1,2) )``.
664
665The earlier versions of these methods were more forgiving because they used an
666old function in Python's C interface to parse their arguments; 2.0 modernizes
667them to use :func:`PyArg_ParseTuple`, the current argument parsing function,
668which provides more helpful error messages and treats multi-argument calls as
669errors. If you absolutely must use 2.0 but can't fix your code, you can edit
670:file:`Objects/listobject.c` and define the preprocessor symbol
671``NO_STRICT_LIST_APPEND`` to preserve the old behaviour; this isn't recommended.
672
673Some of the functions in the :mod:`socket` module are still forgiving in this
674way. For example, :func:`socket.connect( ('hostname', 25) )` is the correct
675form, passing a tuple representing an IP address, but :func:`socket.connect(
676'hostname', 25 )` also works. :func:`socket.connect_ex` and :func:`socket.bind`
677are similarly easy-going. 2.0alpha1 tightened these functions up, but because
678the documentation actually used the erroneous multiple argument form, many
679people wrote code which would break with the stricter checking. GvR backed out
680the changes in the face of public reaction, so for the :mod:`socket` module, the
681documentation was fixed and the multiple argument form is simply marked as
682deprecated; it *will* be tightened up again in a future Python version.
683
684The ``\x`` escape in string literals now takes exactly 2 hex digits. Previously
685it would consume all the hex digits following the 'x' and take the lowest 8 bits
686of the result, so ``\x123456`` was equivalent to ``\x56``.
687
688The :exc:`AttributeError` and :exc:`NameError` exceptions have a more friendly
689error message, whose text will be something like ``'Spam' instance has no
690attribute 'eggs'`` or ``name 'eggs' is not defined``. Previously the error
691message was just the missing attribute name ``eggs``, and code written to take
692advantage of this fact will break in 2.0.
693
694Some work has been done to make integers and long integers a bit more
695interchangeable. In 1.5.2, large-file support was added for Solaris, to allow
696reading files larger than 2 GiB; this made the :meth:`tell` method of file
697objects return a long integer instead of a regular integer. Some code would
698subtract two file offsets and attempt to use the result to multiply a sequence
699or slice a string, but this raised a :exc:`TypeError`. In 2.0, long integers
700can be used to multiply or slice a sequence, and it'll behave as you'd
701intuitively expect it to; ``3L * 'abc'`` produces 'abcabcabc', and
702``(0,1,2,3)[2L:4L]`` produces (2,3). Long integers can also be used in various
703contexts where previously only integers were accepted, such as in the
704:meth:`seek` method of file objects, and in the formats supported by the ``%``
705operator (``%d``, ``%i``, ``%x``, etc.). For example, ``"%d" % 2L**64`` will
706produce the string ``18446744073709551616``.
707
708The subtlest long integer change of all is that the :func:`str` of a long
709integer no longer has a trailing 'L' character, though :func:`repr` still
710includes it. The 'L' annoyed many people who wanted to print long integers that
711looked just like regular integers, since they had to go out of their way to chop
712off the character. This is no longer a problem in 2.0, but code which does
713``str(longval)[:-1]`` and assumes the 'L' is there, will now lose the final
714digit.
715
716Taking the :func:`repr` of a float now uses a different formatting precision
717than :func:`str`. :func:`repr` uses ``%.17g`` format string for C's
718:func:`sprintf`, while :func:`str` uses ``%.12g`` as before. The effect is that
719:func:`repr` may occasionally show more decimal places than :func:`str`, for
720certain numbers. For example, the number 8.1 can't be represented exactly in
721binary, so ``repr(8.1)`` is ``'8.0999999999999996'``, while str(8.1) is
722``'8.1'``.
723
724The ``-X`` command-line option, which turned all standard exceptions into
725strings instead of classes, has been removed; the standard exceptions will now
726always be classes. The :mod:`exceptions` module containing the standard
727exceptions was translated from Python to a built-in C module, written by Barry
728Warsaw and Fredrik Lundh.
729
Georg Brandlb19be572007-12-29 10:57:00 +0000730.. Commented out for now -- I don't think anyone will care.
731 The pattern and match objects provided by SRE are C types, not Python
732 class instances as in 1.5. This means you can no longer inherit from
733 \class{RegexObject} or \class{MatchObject}, but that shouldn't be much
734 of a problem since no one should have been doing that in the first
735 place.
736.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000737
738
739Extending/Embedding Changes
740===========================
741
742Some of the changes are under the covers, and will only be apparent to people
743writing C extension modules or embedding a Python interpreter in a larger
744application. If you aren't dealing with Python's C API, you can safely skip
745this section.
746
747The version number of the Python C API was incremented, so C extensions compiled
748for 1.5.2 must be recompiled in order to work with 2.0. On Windows, it's not
749possible for Python 2.0 to import a third party extension built for Python 1.5.x
750due to how Windows DLLs work, so Python will raise an exception and the import
751will fail.
752
753Users of Jim Fulton's ExtensionClass module will be pleased to find out that
754hooks have been added so that ExtensionClasses are now supported by
755:func:`isinstance` and :func:`issubclass`. This means you no longer have to
756remember to write code such as ``if type(obj) == myExtensionClass``, but can use
757the more natural ``if isinstance(obj, myExtensionClass)``.
758
759The :file:`Python/importdl.c` file, which was a mass of #ifdefs to support
760dynamic loading on many different platforms, was cleaned up and reorganised by
761Greg Stein. :file:`importdl.c` is now quite small, and platform-specific code
762has been moved into a bunch of :file:`Python/dynload_\*.c` files. Another
763cleanup: there were also a number of :file:`my\*.h` files in the Include/
764directory that held various portability hacks; they've been merged into a single
765file, :file:`Include/pyport.h`.
766
767Vladimir Marangozov's long-awaited malloc restructuring was completed, to make
768it easy to have the Python interpreter use a custom allocator instead of C's
769standard :func:`malloc`. For documentation, read the comments in
770:file:`Include/pymem.h` and :file:`Include/objimpl.h`. For the lengthy
771discussions during which the interface was hammered out, see the Web archives of
772the 'patches' and 'python-dev' lists at python.org.
773
774Recent versions of the GUSI development environment for MacOS support POSIX
775threads. Therefore, Python's POSIX threading support now works on the
776Macintosh. Threading support using the user-space GNU ``pth`` library was also
777contributed.
778
779Threading support on Windows was enhanced, too. Windows supports thread locks
780that use kernel objects only in case of contention; in the common case when
781there's no contention, they use simpler functions which are an order of
782magnitude faster. A threaded version of Python 1.5.2 on NT is twice as slow as
783an unthreaded version; with the 2.0 changes, the difference is only 10%. These
784improvements were contributed by Yakov Markovitch.
785
786Python 2.0's source now uses only ANSI C prototypes, so compiling Python now
787requires an ANSI C compiler, and can no longer be done using a compiler that
788only supports K&R C.
789
790Previously the Python virtual machine used 16-bit numbers in its bytecode,
791limiting the size of source files. In particular, this affected the maximum
792size of literal lists and dictionaries in Python source; occasionally people who
793are generating Python code would run into this limit. A patch by Charles G.
794Waldman raises the limit from ``2^16`` to ``2^{32}``.
795
796Three new convenience functions intended for adding constants to a module's
797dictionary at module initialization time were added: :func:`PyModule_AddObject`,
798:func:`PyModule_AddIntConstant`, and :func:`PyModule_AddStringConstant`. Each
799of these functions takes a module object, a null-terminated C string containing
800the name to be added, and a third argument for the value to be assigned to the
801name. This third argument is, respectively, a Python object, a C long, or a C
802string.
803
804A wrapper API was added for Unix-style signal handlers. :func:`PyOS_getsig` gets
805a signal handler and :func:`PyOS_setsig` will set a new handler.
806
Georg Brandlb19be572007-12-29 10:57:00 +0000807.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000808
809
810Distutils: Making Modules Easy to Install
811=========================================
812
813Before Python 2.0, installing modules was a tedious affair -- there was no way
814to figure out automatically where Python is installed, or what compiler options
815to use for extension modules. Software authors had to go through an arduous
816ritual of editing Makefiles and configuration files, which only really work on
817Unix and leave Windows and MacOS unsupported. Python users faced wildly
818differing installation instructions which varied between different extension
819packages, which made administering a Python installation something of a chore.
820
821The SIG for distribution utilities, shepherded by Greg Ward, has created the
822Distutils, a system to make package installation much easier. They form the
823:mod:`distutils` package, a new part of Python's standard library. In the best
824case, installing a Python module from source will require the same steps: first
825you simply mean unpack the tarball or zip archive, and the run "``python
826setup.py install``". The platform will be automatically detected, the compiler
827will be recognized, C extension modules will be compiled, and the distribution
828installed into the proper directory. Optional command-line arguments provide
829more control over the installation process, the distutils package offers many
830places to override defaults -- separating the build from the install, building
831or installing in non-default directories, and more.
832
833In order to use the Distutils, you need to write a :file:`setup.py` script. For
834the simple case, when the software contains only .py files, a minimal
835:file:`setup.py` can be just a few lines long::
836
837 from distutils.core import setup
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000838 setup (name = "foo", version = "1.0",
Georg Brandl8ec7f652007-08-15 14:28:01 +0000839 py_modules = ["module1", "module2"])
840
841The :file:`setup.py` file isn't much more complicated if the software consists
842of a few packages::
843
844 from distutils.core import setup
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000845 setup (name = "foo", version = "1.0",
Georg Brandl8ec7f652007-08-15 14:28:01 +0000846 packages = ["package", "package.subpackage"])
847
848A C extension can be the most complicated case; here's an example taken from
849the PyXML package::
850
851 from distutils.core import setup, Extension
852
853 expat_extension = Extension('xml.parsers.pyexpat',
Georg Brandl7044b112009-01-03 21:04:55 +0000854 define_macros = [('XML_NS', None)],
855 include_dirs = [ 'extensions/expat/xmltok',
856 'extensions/expat/xmlparse' ],
857 sources = [ 'extensions/pyexpat.c',
858 'extensions/expat/xmltok/xmltok.c',
859 'extensions/expat/xmltok/xmlrole.c', ]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000860 )
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000861 setup (name = "PyXML", version = "0.5.4",
Georg Brandl8ec7f652007-08-15 14:28:01 +0000862 ext_modules =[ expat_extension ] )
863
864The Distutils can also take care of creating source and binary distributions.
865The "sdist" command, run by "``python setup.py sdist``', builds a source
866distribution such as :file:`foo-1.0.tar.gz`. Adding new commands isn't
867difficult, "bdist_rpm" and "bdist_wininst" commands have already been
868contributed to create an RPM distribution and a Windows installer for the
869software, respectively. Commands to create other distribution formats such as
870Debian packages and Solaris :file:`.pkg` files are in various stages of
871development.
872
873All this is documented in a new manual, *Distributing Python Modules*, that
874joins the basic set of Python documentation.
875
Georg Brandlb19be572007-12-29 10:57:00 +0000876.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000877
878
879XML Modules
880===========
881
882Python 1.5.2 included a simple XML parser in the form of the :mod:`xmllib`
883module, contributed by Sjoerd Mullender. Since 1.5.2's release, two different
884interfaces for processing XML have become common: SAX2 (version 2 of the Simple
885API for XML) provides an event-driven interface with some similarities to
886:mod:`xmllib`, and the DOM (Document Object Model) provides a tree-based
887interface, transforming an XML document into a tree of nodes that can be
888traversed and modified. Python 2.0 includes a SAX2 interface and a stripped-
889down DOM interface as part of the :mod:`xml` package. Here we will give a brief
890overview of these new interfaces; consult the Python documentation or the source
891code for complete details. The Python XML SIG is also working on improved
892documentation.
893
894
895SAX2 Support
896------------
897
898SAX defines an event-driven interface for parsing XML. To use SAX, you must
899write a SAX handler class. Handler classes inherit from various classes
900provided by SAX, and override various methods that will then be called by the
901XML parser. For example, the :meth:`startElement` and :meth:`endElement`
902methods are called for every starting and end tag encountered by the parser, the
903:meth:`characters` method is called for every chunk of character data, and so
904forth.
905
906The advantage of the event-driven approach is that the whole document doesn't
907have to be resident in memory at any one time, which matters if you are
908processing really huge documents. However, writing the SAX handler class can
909get very complicated if you're trying to modify the document structure in some
910elaborate way.
911
912For example, this little example program defines a handler that prints a message
913for every starting and ending tag, and then parses the file :file:`hamlet.xml`
914using it::
915
916 from xml import sax
917
918 class SimpleHandler(sax.ContentHandler):
919 def startElement(self, name, attrs):
920 print 'Start of element:', name, attrs.keys()
921
922 def endElement(self, name):
923 print 'End of element:', name
924
925 # Create a parser object
926 parser = sax.make_parser()
927
928 # Tell it what handler to use
929 handler = SimpleHandler()
930 parser.setContentHandler( handler )
931
932 # Parse a file!
933 parser.parse( 'hamlet.xml' )
934
935For more information, consult the Python documentation, or the XML HOWTO at
936http://pyxml.sourceforge.net/topics/howto/xml-howto.html.
937
938
939DOM Support
940-----------
941
942The Document Object Model is a tree-based representation for an XML document. A
943top-level :class:`Document` instance is the root of the tree, and has a single
944child which is the top-level :class:`Element` instance. This :class:`Element`
945has children nodes representing character data and any sub-elements, which may
946have further children of their own, and so forth. Using the DOM you can
947traverse the resulting tree any way you like, access element and attribute
948values, insert and delete nodes, and convert the tree back into XML.
949
950The DOM is useful for modifying XML documents, because you can create a DOM
951tree, modify it by adding new nodes or rearranging subtrees, and then produce a
952new XML document as output. You can also construct a DOM tree manually and
953convert it to XML, which can be a more flexible way of producing XML output than
954simply writing ``<tag1>``...\ ``</tag1>`` to a file.
955
956The DOM implementation included with Python lives in the :mod:`xml.dom.minidom`
957module. It's a lightweight implementation of the Level 1 DOM with support for
958XML namespaces. The :func:`parse` and :func:`parseString` convenience
959functions are provided for generating a DOM tree::
960
961 from xml.dom import minidom
962 doc = minidom.parse('hamlet.xml')
963
964``doc`` is a :class:`Document` instance. :class:`Document`, like all the other
965DOM classes such as :class:`Element` and :class:`Text`, is a subclass of the
966:class:`Node` base class. All the nodes in a DOM tree therefore support certain
967common methods, such as :meth:`toxml` which returns a string containing the XML
968representation of the node and its children. Each class also has special
969methods of its own; for example, :class:`Element` and :class:`Document`
970instances have a method to find all child elements with a given tag name.
971Continuing from the previous 2-line example::
972
973 perslist = doc.getElementsByTagName( 'PERSONA' )
974 print perslist[0].toxml()
975 print perslist[1].toxml()
976
977For the *Hamlet* XML file, the above few lines output::
978
979 <PERSONA>CLAUDIUS, king of Denmark. </PERSONA>
980 <PERSONA>HAMLET, son to the late, and nephew to the present king.</PERSONA>
981
982The root element of the document is available as ``doc.documentElement``, and
983its children can be easily modified by deleting, adding, or removing nodes::
984
985 root = doc.documentElement
986
987 # Remove the first child
988 root.removeChild( root.childNodes[0] )
989
990 # Move the new first child to the end
991 root.appendChild( root.childNodes[0] )
992
993 # Insert the new first child (originally,
994 # the third child) before the 20th child.
995 root.insertBefore( root.childNodes[0], root.childNodes[20] )
996
997Again, I will refer you to the Python documentation for a complete listing of
998the different :class:`Node` classes and their various methods.
999
1000
1001Relationship to PyXML
1002---------------------
1003
1004The XML Special Interest Group has been working on XML-related Python code for a
1005while. Its code distribution, called PyXML, is available from the SIG's Web
Georg Brandl06f3b3b2014-10-29 08:36:35 +01001006pages at https://www.python.org/sigs/xml-sig/. The PyXML distribution also used
Georg Brandl8ec7f652007-08-15 14:28:01 +00001007the package name ``xml``. If you've written programs that used PyXML, you're
1008probably wondering about its compatibility with the 2.0 :mod:`xml` package.
1009
1010The answer is that Python 2.0's :mod:`xml` package isn't compatible with PyXML,
1011but can be made compatible by installing a recent version PyXML. Many
1012applications can get by with the XML support that is included with Python 2.0,
1013but more complicated applications will require that the full PyXML package will
1014be installed. When installed, PyXML versions 0.6.0 or greater will replace the
1015:mod:`xml` package shipped with Python, and will be a strict superset of the
1016standard package, adding a bunch of additional features. Some of the additional
1017features in PyXML include:
1018
1019* 4DOM, a full DOM implementation from FourThought, Inc.
1020
1021* The xmlproc validating parser, written by Lars Marius Garshol.
1022
1023* The :mod:`sgmlop` parser accelerator module, written by Fredrik Lundh.
1024
Georg Brandlb19be572007-12-29 10:57:00 +00001025.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00001026
1027
1028Module changes
1029==============
1030
1031Lots of improvements and bugfixes were made to Python's extensive standard
1032library; some of the affected modules include :mod:`readline`,
1033:mod:`ConfigParser`, :mod:`cgi`, :mod:`calendar`, :mod:`posix`, :mod:`readline`,
1034:mod:`xmllib`, :mod:`aifc`, :mod:`chunk, wave`, :mod:`random`, :mod:`shelve`,
1035and :mod:`nntplib`. Consult the CVS logs for the exact patch-by-patch details.
1036
1037Brian Gallew contributed OpenSSL support for the :mod:`socket` module. OpenSSL
1038is an implementation of the Secure Socket Layer, which encrypts the data being
1039sent over a socket. When compiling Python, you can edit :file:`Modules/Setup`
1040to include SSL support, which adds an additional function to the :mod:`socket`
1041module: :func:`socket.ssl(socket, keyfile, certfile)`, which takes a socket
1042object and returns an SSL socket. The :mod:`httplib` and :mod:`urllib` modules
Georg Brandla4314c22009-10-11 20:16:16 +00001043were also changed to support ``https://`` URLs, though no one has implemented
1044FTP or SMTP over SSL.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001045
1046The :mod:`httplib` module has been rewritten by Greg Stein to support HTTP/1.1.
1047Backward compatibility with the 1.5 version of :mod:`httplib` is provided,
1048though using HTTP/1.1 features such as pipelining will require rewriting code to
1049use a different set of interfaces.
1050
1051The :mod:`Tkinter` module now supports Tcl/Tk version 8.1, 8.2, or 8.3, and
1052support for the older 7.x versions has been dropped. The Tkinter module now
1053supports displaying Unicode strings in Tk widgets. Also, Fredrik Lundh
1054contributed an optimization which makes operations like ``create_line`` and
1055``create_polygon`` much faster, especially when using lots of coordinates.
1056
1057The :mod:`curses` module has been greatly extended, starting from Oliver
1058Andrich's enhanced version, to provide many additional functions from ncurses
1059and SYSV curses, such as colour, alternative character set support, pads, and
1060mouse support. This means the module is no longer compatible with operating
1061systems that only have BSD curses, but there don't seem to be any currently
1062maintained OSes that fall into this category.
1063
1064As mentioned in the earlier discussion of 2.0's Unicode support, the underlying
1065implementation of the regular expressions provided by the :mod:`re` module has
1066been changed. SRE, a new regular expression engine written by Fredrik Lundh and
1067partially funded by Hewlett Packard, supports matching against both 8-bit
1068strings and Unicode strings.
1069
Georg Brandlb19be572007-12-29 10:57:00 +00001070.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00001071
1072
1073New modules
1074===========
1075
1076A number of new modules were added. We'll simply list them with brief
1077descriptions; consult the 2.0 documentation for the details of a particular
1078module.
1079
1080* :mod:`atexit`: For registering functions to be called before the Python
1081 interpreter exits. Code that currently sets ``sys.exitfunc`` directly should be
1082 changed to use the :mod:`atexit` module instead, importing :mod:`atexit` and
1083 calling :func:`atexit.register` with the function to be called on exit.
1084 (Contributed by Skip Montanaro.)
1085
1086* :mod:`codecs`, :mod:`encodings`, :mod:`unicodedata`: Added as part of the new
1087 Unicode support.
1088
1089* :mod:`filecmp`: Supersedes the old :mod:`cmp`, :mod:`cmpcache` and
1090 :mod:`dircmp` modules, which have now become deprecated. (Contributed by Gordon
1091 MacMillan and Moshe Zadka.)
1092
1093* :mod:`gettext`: This module provides internationalization (I18N) and
1094 localization (L10N) support for Python programs by providing an interface to the
1095 GNU gettext message catalog library. (Integrated by Barry Warsaw, from separate
1096 contributions by Martin von Löwis, Peter Funk, and James Henstridge.)
1097
1098* :mod:`linuxaudiodev`: Support for the :file:`/dev/audio` device on Linux, a
1099 twin to the existing :mod:`sunaudiodev` module. (Contributed by Peter Bosch,
1100 with fixes by Jeremy Hylton.)
1101
1102* :mod:`mmap`: An interface to memory-mapped files on both Windows and Unix. A
1103 file's contents can be mapped directly into memory, at which point it behaves
1104 like a mutable string, so its contents can be read and modified. They can even
1105 be passed to functions that expect ordinary strings, such as the :mod:`re`
1106 module. (Contributed by Sam Rushing, with some extensions by A.M. Kuchling.)
1107
1108* :mod:`pyexpat`: An interface to the Expat XML parser. (Contributed by Paul
1109 Prescod.)
1110
1111* :mod:`robotparser`: Parse a :file:`robots.txt` file, which is used for writing
1112 Web spiders that politely avoid certain areas of a Web site. The parser accepts
1113 the contents of a :file:`robots.txt` file, builds a set of rules from it, and
1114 can then answer questions about the fetchability of a given URL. (Contributed
1115 by Skip Montanaro.)
1116
1117* :mod:`tabnanny`: A module/script to check Python source code for ambiguous
1118 indentation. (Contributed by Tim Peters.)
1119
1120* :mod:`UserString`: A base class useful for deriving objects that behave like
1121 strings.
1122
1123* :mod:`webbrowser`: A module that provides a platform independent way to launch
1124 a web browser on a specific URL. For each platform, various browsers are tried
1125 in a specific order. The user can alter which browser is launched by setting the
1126 *BROWSER* environment variable. (Originally inspired by Eric S. Raymond's patch
1127 to :mod:`urllib` which added similar functionality, but the final module comes
1128 from code originally implemented by Fred Drake as
1129 :file:`Tools/idle/BrowserControl.py`, and adapted for the standard library by
1130 Fred.)
1131
1132* :mod:`_winreg`: An interface to the Windows registry. :mod:`_winreg` is an
1133 adaptation of functions that have been part of PythonWin since 1995, but has now
1134 been added to the core distribution, and enhanced to support Unicode.
1135 :mod:`_winreg` was written by Bill Tutt and Mark Hammond.
1136
1137* :mod:`zipfile`: A module for reading and writing ZIP-format archives. These
1138 are archives produced by :program:`PKZIP` on DOS/Windows or :program:`zip` on
1139 Unix, not to be confused with :program:`gzip`\ -format files (which are
1140 supported by the :mod:`gzip` module) (Contributed by James C. Ahlstrom.)
1141
1142* :mod:`imputil`: A module that provides a simpler way for writing customised
1143 import hooks, in comparison to the existing :mod:`ihooks` module. (Implemented
1144 by Greg Stein, with much discussion on python-dev along the way.)
1145
Georg Brandlb19be572007-12-29 10:57:00 +00001146.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00001147
1148
1149IDLE Improvements
1150=================
1151
1152IDLE is the official Python cross-platform IDE, written using Tkinter. Python
11532.0 includes IDLE 0.6, which adds a number of new features and improvements. A
1154partial list:
1155
1156* UI improvements and optimizations, especially in the area of syntax
1157 highlighting and auto-indentation.
1158
1159* The class browser now shows more information, such as the top level functions
1160 in a module.
1161
1162* Tab width is now a user settable option. When opening an existing Python file,
1163 IDLE automatically detects the indentation conventions, and adapts.
1164
1165* There is now support for calling browsers on various platforms, used to open
1166 the Python documentation in a browser.
1167
1168* IDLE now has a command line, which is largely similar to the vanilla Python
1169 interpreter.
1170
1171* Call tips were added in many places.
1172
1173* IDLE can now be installed as a package.
1174
1175* In the editor window, there is now a line/column bar at the bottom.
1176
1177* Three new keystroke commands: Check module (Alt-F5), Import module (F5) and
1178 Run script (Ctrl-F5).
1179
Georg Brandlb19be572007-12-29 10:57:00 +00001180.. ======================================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +00001181
1182
1183Deleted and Deprecated Modules
1184==============================
1185
1186A few modules have been dropped because they're obsolete, or because there are
1187now better ways to do the same thing. The :mod:`stdwin` module is gone; it was
1188for a platform-independent windowing toolkit that's no longer developed.
1189
1190A number of modules have been moved to the :file:`lib-old` subdirectory:
1191:mod:`cmp`, :mod:`cmpcache`, :mod:`dircmp`, :mod:`dump`, :mod:`find`,
1192:mod:`grep`, :mod:`packmail`, :mod:`poly`, :mod:`util`, :mod:`whatsound`,
1193:mod:`zmod`. If you have code which relies on a module that's been moved to
1194:file:`lib-old`, you can simply add that directory to ``sys.path`` to get them
1195back, but you're encouraged to update any code that uses these modules.
1196
1197
1198Acknowledgements
1199================
1200
1201The authors would like to thank the following people for offering suggestions on
1202various drafts of this article: David Bolen, Mark Hammond, Gregg Hauser, Jeremy
1203Hylton, Fredrik Lundh, Detlef Lannert, Aahz Maruch, Skip Montanaro, Vladimir
1204Marangozov, Tobias Polzin, Guido van Rossum, Neil Schemenauer, and Russ Schmidt.
1205