blob: d782f5d8d6af23c492be27b596d0a9493e132dbd [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001****************************
2 What's New in Python 2.4
3****************************
4
5:Author: A.M. Kuchling
6
7.. |release| replace:: 1.02
8
9.. % $Id: whatsnew24.tex 55005 2007-04-27 19:54:29Z guido.van.rossum $
10.. % Don't write extensive text for new sections; I'll do that.
11.. % Feel free to add commented-out reminders of things that need
12.. % to be covered. --amk
13
14This article explains the new features in Python 2.4.1, released on March 30,
152005.
16
17Python 2.4 is a medium-sized release. It doesn't introduce as many changes as
18the radical Python 2.2, but introduces more features than the conservative 2.3
19release. The most significant new language features are function decorators and
20generator expressions; most other changes are to the standard library.
21
22According to the CVS change logs, there were 481 patches applied and 502 bugs
23fixed between Python 2.3 and 2.4. Both figures are likely to be underestimates.
24
25This article doesn't attempt to provide a complete specification of every single
26new feature, but instead provides a brief introduction to each feature. For
27full details, you should refer to the documentation for Python 2.4, such as the
28Python Library Reference and the Python Reference Manual. Often you will be
29referred to the PEP for a particular new feature for explanations of the
30implementation and design rationale.
31
32.. % ======================================================================
33
34
35PEP 218: Built-In Set Objects
36=============================
37
38Python 2.3 introduced the :mod:`sets` module. C implementations of set data
39types have now been added to the Python core as two new built-in types,
40:func:`set(iterable)` and :func:`frozenset(iterable)`. They provide high speed
41operations for membership testing, for eliminating duplicates from sequences,
42and for mathematical operations like unions, intersections, differences, and
43symmetric differences. ::
44
45 >>> a = set('abracadabra') # form a set from a string
46 >>> 'z' in a # fast membership testing
47 False
48 >>> a # unique letters in a
49 set(['a', 'r', 'b', 'c', 'd'])
50 >>> ''.join(a) # convert back into a string
51 'arbcd'
52
53 >>> b = set('alacazam') # form a second set
54 >>> a - b # letters in a but not in b
55 set(['r', 'd', 'b'])
56 >>> a | b # letters in either a or b
57 set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
58 >>> a & b # letters in both a and b
59 set(['a', 'c'])
60 >>> a ^ b # letters in a or b but not both
61 set(['r', 'd', 'b', 'm', 'z', 'l'])
62
63 >>> a.add('z') # add a new element
64 >>> a.update('wxy') # add multiple new elements
65 >>> a
66 set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z'])
67 >>> a.remove('x') # take one element out
68 >>> a
69 set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z'])
70
71The :func:`frozenset` type is an immutable version of :func:`set`. Since it is
72immutable and hashable, it may be used as a dictionary key or as a member of
73another set.
74
75The :mod:`sets` module remains in the standard library, and may be useful if you
76wish to subclass the :class:`Set` or :class:`ImmutableSet` classes. There are
77currently no plans to deprecate the module.
78
79
80.. seealso::
81
82 :pep:`218` - Adding a Built-In Set Object Type
83 Originally proposed by Greg Wilson and ultimately implemented by Raymond
84 Hettinger.
85
86.. % ======================================================================
87
88
89PEP 237: Unifying Long Integers and Integers
90============================================
91
92The lengthy transition process for this PEP, begun in Python 2.2, takes another
93step forward in Python 2.4. In 2.3, certain integer operations that would
94behave differently after int/long unification triggered :exc:`FutureWarning`
95warnings and returned values limited to 32 or 64 bits (depending on your
96platform). In 2.4, these expressions no longer produce a warning and instead
97produce a different result that's usually a long integer.
98
99The problematic expressions are primarily left shifts and lengthy hexadecimal
100and octal constants. For example, ``2 << 32`` results in a warning in 2.3,
101evaluating to 0 on 32-bit platforms. In Python 2.4, this expression now returns
102the correct answer, 8589934592.
103
104
105.. seealso::
106
107 :pep:`237` - Unifying Long Integers and Integers
108 Original PEP written by Moshe Zadka and GvR. The changes for 2.4 were
109 implemented by Kalle Svensson.
110
111.. % ======================================================================
112
113
114PEP 289: Generator Expressions
115==============================
116
117The iterator feature introduced in Python 2.2 and the :mod:`itertools` module
118make it easier to write programs that loop through large data sets without
119having the entire data set in memory at one time. List comprehensions don't fit
120into this picture very well because they produce a Python list object containing
121all of the items. This unavoidably pulls all of the objects into memory, which
122can be a problem if your data set is very large. When trying to write a
123functionally-styled program, it would be natural to write something like::
124
125 links = [link for link in get_all_links() if not link.followed]
126 for link in links:
127 ...
128
129instead of ::
130
131 for link in get_all_links():
132 if link.followed:
133 continue
134 ...
135
136The first form is more concise and perhaps more readable, but if you're dealing
137with a large number of link objects you'd have to write the second form to avoid
138having all link objects in memory at the same time.
139
140Generator expressions work similarly to list comprehensions but don't
141materialize the entire list; instead they create a generator that will return
142elements one by one. The above example could be written as::
143
144 links = (link for link in get_all_links() if not link.followed)
145 for link in links:
146 ...
147
148Generator expressions always have to be written inside parentheses, as in the
149above example. The parentheses signalling a function call also count, so if you
150want to create an iterator that will be immediately passed to a function you
151could write::
152
153 print sum(obj.count for obj in list_all_objects())
154
155Generator expressions differ from list comprehensions in various small ways.
156Most notably, the loop variable (*obj* in the above example) is not accessible
157outside of the generator expression. List comprehensions leave the variable
158assigned to its last value; future versions of Python will change this, making
159list comprehensions match generator expressions in this respect.
160
161
162.. seealso::
163
164 :pep:`289` - Generator Expressions
165 Proposed by Raymond Hettinger and implemented by Jiwon Seo with early efforts
166 steered by Hye-Shik Chang.
167
168.. % ======================================================================
169
170
171PEP 292: Simpler String Substitutions
172=====================================
173
174Some new classes in the standard library provide an alternative mechanism for
175substituting variables into strings; this style of substitution may be better
176for applications where untrained users need to edit templates.
177
178The usual way of substituting variables by name is the ``%`` operator::
179
180 >>> '%(page)i: %(title)s' % {'page':2, 'title': 'The Best of Times'}
181 '2: The Best of Times'
182
183When writing the template string, it can be easy to forget the ``i`` or ``s``
184after the closing parenthesis. This isn't a big problem if the template is in a
185Python module, because you run the code, get an "Unsupported format character"
186:exc:`ValueError`, and fix the problem. However, consider an application such
187as Mailman where template strings or translations are being edited by users who
188aren't aware of the Python language. The format string's syntax is complicated
189to explain to such users, and if they make a mistake, it's difficult to provide
190helpful feedback to them.
191
192PEP 292 adds a :class:`Template` class to the :mod:`string` module that uses
193``$`` to indicate a substitution::
194
195 >>> import string
196 >>> t = string.Template('$page: $title')
197 >>> t.substitute({'page':2, 'title': 'The Best of Times'})
198 '2: The Best of Times'
199
200If a key is missing from the dictionary, the :meth:`substitute` method will
201raise a :exc:`KeyError`. There's also a :meth:`safe_substitute` method that
202ignores missing keys:
203
204.. % $ Terminate $-mode for Emacs
205
206::
207
208 >>> t = string.Template('$page: $title')
209 >>> t.safe_substitute({'page':3})
210 '3: $title'
211
212.. % $ Terminate math-mode for Emacs
213
214
215.. seealso::
216
217 :pep:`292` - Simpler String Substitutions
218 Written and implemented by Barry Warsaw.
219
220.. % ======================================================================
221
222
223PEP 318: Decorators for Functions and Methods
224=============================================
225
226Python 2.2 extended Python's object model by adding static methods and class
227methods, but it didn't extend Python's syntax to provide any new way of defining
228static or class methods. Instead, you had to write a :keyword:`def` statement
229in the usual way, and pass the resulting method to a :func:`staticmethod` or
230:func:`classmethod` function that would wrap up the function as a method of the
231new type. Your code would look like this::
232
233 class C:
234 def meth (cls):
235 ...
236
237 meth = classmethod(meth) # Rebind name to wrapped-up class method
238
239If the method was very long, it would be easy to miss or forget the
240:func:`classmethod` invocation after the function body.
241
242The intention was always to add some syntax to make such definitions more
243readable, but at the time of 2.2's release a good syntax was not obvious. Today
244a good syntax *still* isn't obvious but users are asking for easier access to
245the feature; a new syntactic feature has been added to meet this need.
246
247The new feature is called "function decorators". The name comes from the idea
248that :func:`classmethod`, :func:`staticmethod`, and friends are storing
249additional information on a function object; they're *decorating* functions with
250more details.
251
252The notation borrows from Java and uses the ``'@'`` character as an indicator.
253Using the new syntax, the example above would be written::
254
255 class C:
256
257 @classmethod
258 def meth (cls):
259 ...
260
261
262The ``@classmethod`` is shorthand for the ``meth=classmethod(meth)`` assignment.
263More generally, if you have the following::
264
265 @A
266 @B
267 @C
268 def f ():
269 ...
270
271It's equivalent to the following pre-decorator code::
272
273 def f(): ...
274 f = A(B(C(f)))
275
276Decorators must come on the line before a function definition, one decorator per
277line, and can't be on the same line as the def statement, meaning that ``@A def
278f(): ...`` is illegal. You can only decorate function definitions, either at
279the module level or inside a class; you can't decorate class definitions.
280
281A decorator is just a function that takes the function to be decorated as an
282argument and returns either the same function or some new object. The return
283value of the decorator need not be callable (though it typically is), unless
284further decorators will be applied to the result. It's easy to write your own
285decorators. The following simple example just sets an attribute on the function
286object::
287
288 >>> def deco(func):
289 ... func.attr = 'decorated'
290 ... return func
291 ...
292 >>> @deco
293 ... def f(): pass
294 ...
295 >>> f
296 <function f at 0x402ef0d4>
297 >>> f.attr
298 'decorated'
299 >>>
300
301As a slightly more realistic example, the following decorator checks that the
302supplied argument is an integer::
303
304 def require_int (func):
305 def wrapper (arg):
306 assert isinstance(arg, int)
307 return func(arg)
308
309 return wrapper
310
311 @require_int
312 def p1 (arg):
313 print arg
314
315 @require_int
316 def p2(arg):
317 print arg*2
318
319An example in :pep:`318` contains a fancier version of this idea that lets you
320both specify the required type and check the returned type.
321
322Decorator functions can take arguments. If arguments are supplied, your
323decorator function is called with only those arguments and must return a new
324decorator function; this function must take a single function and return a
325function, as previously described. In other words, ``@A @B @C(args)`` becomes::
326
327 def f(): ...
328 _deco = C(args)
329 f = A(B(_deco(f)))
330
331Getting this right can be slightly brain-bending, but it's not too difficult.
332
333A small related change makes the :attr:`func_name` attribute of functions
334writable. This attribute is used to display function names in tracebacks, so
335decorators should change the name of any new function that's constructed and
336returned.
337
338
339.. seealso::
340
341 :pep:`318` - Decorators for Functions, Methods and Classes
342 Written by Kevin D. Smith, Jim Jewett, and Skip Montanaro. Several people
343 wrote patches implementing function decorators, but the one that was actually
344 checked in was patch #979728, written by Mark Russell.
345
346 http://www.python.org/moin/PythonDecoratorLibrary
347 This Wiki page contains several examples of decorators.
348
349.. % ======================================================================
350
351
352PEP 322: Reverse Iteration
353==========================
354
355A new built-in function, :func:`reversed(seq)`, takes a sequence and returns an
356iterator that loops over the elements of the sequence in reverse order. ::
357
358 >>> for i in reversed(xrange(1,4)):
359 ... print i
360 ...
361 3
362 2
363 1
364
365Compared to extended slicing, such as ``range(1,4)[::-1]``, :func:`reversed` is
366easier to read, runs faster, and uses substantially less memory.
367
368Note that :func:`reversed` only accepts sequences, not arbitrary iterators. If
369you want to reverse an iterator, first convert it to a list with :func:`list`.
370::
371
372 >>> input = open('/etc/passwd', 'r')
373 >>> for line in reversed(list(input)):
374 ... print line
375 ...
376 root:*:0:0:System Administrator:/var/root:/bin/tcsh
377 ...
378
379
380.. seealso::
381
382 :pep:`322` - Reverse Iteration
383 Written and implemented by Raymond Hettinger.
384
385.. % ======================================================================
386
387
388PEP 324: New subprocess Module
389==============================
390
391The standard library provides a number of ways to execute a subprocess, offering
392different features and different levels of complexity.
393:func:`os.system(command)` is easy to use, but slow (it runs a shell process
394which executes the command) and dangerous (you have to be careful about escaping
395the shell's metacharacters). The :mod:`popen2` module offers classes that can
396capture standard output and standard error from the subprocess, but the naming
397is confusing. The :mod:`subprocess` module cleans this up, providing a unified
398interface that offers all the features you might need.
399
400Instead of :mod:`popen2`'s collection of classes, :mod:`subprocess` contains a
401single class called :class:`Popen` whose constructor supports a number of
402different keyword arguments. ::
403
404 class Popen(args, bufsize=0, executable=None,
405 stdin=None, stdout=None, stderr=None,
406 preexec_fn=None, close_fds=False, shell=False,
407 cwd=None, env=None, universal_newlines=False,
408 startupinfo=None, creationflags=0):
409
410*args* is commonly a sequence of strings that will be the arguments to the
411program executed as the subprocess. (If the *shell* argument is true, *args*
412can be a string which will then be passed on to the shell for interpretation,
413just as :func:`os.system` does.)
414
415*stdin*, *stdout*, and *stderr* specify what the subprocess's input, output, and
416error streams will be. You can provide a file object or a file descriptor, or
417you can use the constant ``subprocess.PIPE`` to create a pipe between the
418subprocess and the parent.
419
420The constructor has a number of handy options:
421
422* *close_fds* requests that all file descriptors be closed before running the
423 subprocess.
424
425* *cwd* specifies the working directory in which the subprocess will be executed
426 (defaulting to whatever the parent's working directory is).
427
428* *env* is a dictionary specifying environment variables.
429
430* *preexec_fn* is a function that gets called before the child is started.
431
432* *universal_newlines* opens the child's input and output using Python's
433 universal newline feature.
434
435Once you've created the :class:`Popen` instance, you can call its :meth:`wait`
436method to pause until the subprocess has exited, :meth:`poll` to check if it's
437exited without pausing, or :meth:`communicate(data)` to send the string *data*
438to the subprocess's standard input. :meth:`communicate(data)` then reads any
439data that the subprocess has sent to its standard output or standard error,
440returning a tuple ``(stdout_data, stderr_data)``.
441
442:func:`call` is a shortcut that passes its arguments along to the :class:`Popen`
443constructor, waits for the command to complete, and returns the status code of
444the subprocess. It can serve as a safer analog to :func:`os.system`::
445
446 sts = subprocess.call(['dpkg', '-i', '/tmp/new-package.deb'])
447 if sts == 0:
448 # Success
449 ...
450 else:
451 # dpkg returned an error
452 ...
453
454The command is invoked without use of the shell. If you really do want to use
455the shell, you can add ``shell=True`` as a keyword argument and provide a string
456instead of a sequence::
457
458 sts = subprocess.call('dpkg -i /tmp/new-package.deb', shell=True)
459
460The PEP takes various examples of shell and Python code and shows how they'd be
461translated into Python code that uses :mod:`subprocess`. Reading this section
462of the PEP is highly recommended.
463
464
465.. seealso::
466
467 :pep:`324` - subprocess - New process module
468 Written and implemented by Peter Ă…strand, with assistance from Fredrik Lundh and
469 others.
470
471.. % ======================================================================
472
473
474PEP 327: Decimal Data Type
475==========================
476
477Python has always supported floating-point (FP) numbers, based on the underlying
478C :ctype:`double` type, as a data type. However, while most programming
479languages provide a floating-point type, many people (even programmers) are
480unaware that floating-point numbers don't represent certain decimal fractions
481accurately. The new :class:`Decimal` type can represent these fractions
482accurately, up to a user-specified precision limit.
483
484
485Why is Decimal needed?
486----------------------
487
488The limitations arise from the representation used for floating-point numbers.
489FP numbers are made up of three components:
490
491* The sign, which is positive or negative.
492
493* The mantissa, which is a single-digit binary number followed by a fractional
494 part. For example, ``1.01`` in base-2 notation is ``1 + 0/2 + 1/4``, or 1.25 in
495 decimal notation.
496
497* The exponent, which tells where the decimal point is located in the number
498 represented.
499
500For example, the number 1.25 has positive sign, a mantissa value of 1.01 (in
501binary), and an exponent of 0 (the decimal point doesn't need to be shifted).
502The number 5 has the same sign and mantissa, but the exponent is 2 because the
503mantissa is multiplied by 4 (2 to the power of the exponent 2); 1.25 \* 4 equals
5045.
505
506Modern systems usually provide floating-point support that conforms to a
507standard called IEEE 754. C's :ctype:`double` type is usually implemented as a
50864-bit IEEE 754 number, which uses 52 bits of space for the mantissa. This
509means that numbers can only be specified to 52 bits of precision. If you're
510trying to represent numbers whose expansion repeats endlessly, the expansion is
511cut off after 52 bits. Unfortunately, most software needs to produce output in
512base 10, and common fractions in base 10 are often repeating decimals in binary.
513For example, 1.1 decimal is binary ``1.0001100110011 ...``; .1 = 1/16 + 1/32 +
5141/256 plus an infinite number of additional terms. IEEE 754 has to chop off
515that infinitely repeated decimal after 52 digits, so the representation is
516slightly inaccurate.
517
518Sometimes you can see this inaccuracy when the number is printed::
519
520 >>> 1.1
521 1.1000000000000001
522
523The inaccuracy isn't always visible when you print the number because the FP-to-
524decimal-string conversion is provided by the C library, and most C libraries try
525to produce sensible output. Even if it's not displayed, however, the inaccuracy
526is still there and subsequent operations can magnify the error.
527
528For many applications this doesn't matter. If I'm plotting points and
529displaying them on my monitor, the difference between 1.1 and 1.1000000000000001
530is too small to be visible. Reports often limit output to a certain number of
531decimal places, and if you round the number to two or three or even eight
532decimal places, the error is never apparent. However, for applications where it
533does matter, it's a lot of work to implement your own custom arithmetic
534routines.
535
536Hence, the :class:`Decimal` type was created.
537
538
539The :class:`Decimal` type
540-------------------------
541
542A new module, :mod:`decimal`, was added to Python's standard library. It
543contains two classes, :class:`Decimal` and :class:`Context`. :class:`Decimal`
544instances represent numbers, and :class:`Context` instances are used to wrap up
545various settings such as the precision and default rounding mode.
546
547:class:`Decimal` instances are immutable, like regular Python integers and FP
548numbers; once it's been created, you can't change the value an instance
549represents. :class:`Decimal` instances can be created from integers or
550strings::
551
552 >>> import decimal
553 >>> decimal.Decimal(1972)
554 Decimal("1972")
555 >>> decimal.Decimal("1.1")
556 Decimal("1.1")
557
558You can also provide tuples containing the sign, the mantissa represented as a
559tuple of decimal digits, and the exponent::
560
561 >>> decimal.Decimal((1, (1, 4, 7, 5), -2))
562 Decimal("-14.75")
563
564Cautionary note: the sign bit is a Boolean value, so 0 is positive and 1 is
565negative.
566
567Converting from floating-point numbers poses a bit of a problem: should the FP
568number representing 1.1 turn into the decimal number for exactly 1.1, or for 1.1
569plus whatever inaccuracies are introduced? The decision was to dodge the issue
570and leave such a conversion out of the API. Instead, you should convert the
571floating-point number into a string using the desired precision and pass the
572string to the :class:`Decimal` constructor::
573
574 >>> f = 1.1
575 >>> decimal.Decimal(str(f))
576 Decimal("1.1")
577 >>> decimal.Decimal('%.12f' % f)
578 Decimal("1.100000000000")
579
580Once you have :class:`Decimal` instances, you can perform the usual mathematical
581operations on them. One limitation: exponentiation requires an integer
582exponent::
583
584 >>> a = decimal.Decimal('35.72')
585 >>> b = decimal.Decimal('1.73')
586 >>> a+b
587 Decimal("37.45")
588 >>> a-b
589 Decimal("33.99")
590 >>> a*b
591 Decimal("61.7956")
592 >>> a/b
593 Decimal("20.64739884393063583815028902")
594 >>> a ** 2
595 Decimal("1275.9184")
596 >>> a**b
597 Traceback (most recent call last):
598 ...
599 decimal.InvalidOperation: x ** (non-integer)
600
601You can combine :class:`Decimal` instances with integers, but not with floating-
602point numbers::
603
604 >>> a + 4
605 Decimal("39.72")
606 >>> a + 4.5
607 Traceback (most recent call last):
608 ...
609 TypeError: You can interact Decimal only with int, long or Decimal data types.
610 >>>
611
612:class:`Decimal` numbers can be used with the :mod:`math` and :mod:`cmath`
613modules, but note that they'll be immediately converted to floating-point
614numbers before the operation is performed, resulting in a possible loss of
615precision and accuracy. You'll also get back a regular floating-point number
616and not a :class:`Decimal`. ::
617
618 >>> import math, cmath
619 >>> d = decimal.Decimal('123456789012.345')
620 >>> math.sqrt(d)
621 351364.18288201344
622 >>> cmath.sqrt(-d)
623 351364.18288201344j
624
625:class:`Decimal` instances have a :meth:`sqrt` method that returns a
626:class:`Decimal`, but if you need other things such as trigonometric functions
627you'll have to implement them. ::
628
629 >>> d.sqrt()
630 Decimal("351364.1828820134592177245001")
631
632
633The :class:`Context` type
634-------------------------
635
636Instances of the :class:`Context` class encapsulate several settings for
637decimal operations:
638
639* :attr:`prec` is the precision, the number of decimal places.
640
641* :attr:`rounding` specifies the rounding mode. The :mod:`decimal` module has
642 constants for the various possibilities: :const:`ROUND_DOWN`,
643 :const:`ROUND_CEILING`, :const:`ROUND_HALF_EVEN`, and various others.
644
645* :attr:`traps` is a dictionary specifying what happens on encountering certain
646 error conditions: either an exception is raised or a value is returned. Some
647 examples of error conditions are division by zero, loss of precision, and
648 overflow.
649
650There's a thread-local default context available by calling :func:`getcontext`;
651you can change the properties of this context to alter the default precision,
652rounding, or trap handling. The following example shows the effect of changing
653the precision of the default context::
654
655 >>> decimal.getcontext().prec
656 28
657 >>> decimal.Decimal(1) / decimal.Decimal(7)
658 Decimal("0.1428571428571428571428571429")
659 >>> decimal.getcontext().prec = 9
660 >>> decimal.Decimal(1) / decimal.Decimal(7)
661 Decimal("0.142857143")
662
663The default action for error conditions is selectable; the module can either
664return a special value such as infinity or not-a-number, or exceptions can be
665raised::
666
667 >>> decimal.Decimal(1) / decimal.Decimal(0)
668 Traceback (most recent call last):
669 ...
670 decimal.DivisionByZero: x / 0
671 >>> decimal.getcontext().traps[decimal.DivisionByZero] = False
672 >>> decimal.Decimal(1) / decimal.Decimal(0)
673 Decimal("Infinity")
674 >>>
675
676The :class:`Context` instance also has various methods for formatting numbers
677such as :meth:`to_eng_string` and :meth:`to_sci_string`.
678
679For more information, see the documentation for the :mod:`decimal` module, which
680includes a quick-start tutorial and a reference.
681
682
683.. seealso::
684
685 :pep:`327` - Decimal Data Type
686 Written by Facundo Batista and implemented by Facundo Batista, Eric Price,
687 Raymond Hettinger, Aahz, and Tim Peters.
688
689 http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
690 A more detailed overview of the IEEE-754 representation.
691
692 http://www.lahey.com/float.htm
693 The article uses Fortran code to illustrate many of the problems that floating-
694 point inaccuracy can cause.
695
696 http://www2.hursley.ibm.com/decimal/
697 A description of a decimal-based representation. This representation is being
698 proposed as a standard, and underlies the new Python decimal type. Much of this
699 material was written by Mike Cowlishaw, designer of the Rexx language.
700
701.. % ======================================================================
702
703
704PEP 328: Multi-line Imports
705===========================
706
707One language change is a small syntactic tweak aimed at making it easier to
708import many names from a module. In a ``from module import names`` statement,
709*names* is a sequence of names separated by commas. If the sequence is very
710long, you can either write multiple imports from the same module, or you can use
711backslashes to escape the line endings like this::
712
713 from SimpleXMLRPCServer import SimpleXMLRPCServer,\
714 SimpleXMLRPCRequestHandler,\
715 CGIXMLRPCRequestHandler,\
716 resolve_dotted_attribute
717
718The syntactic change in Python 2.4 simply allows putting the names within
719parentheses. Python ignores newlines within a parenthesized expression, so the
720backslashes are no longer needed::
721
722 from SimpleXMLRPCServer import (SimpleXMLRPCServer,
723 SimpleXMLRPCRequestHandler,
724 CGIXMLRPCRequestHandler,
725 resolve_dotted_attribute)
726
727The PEP also proposes that all :keyword:`import` statements be absolute imports,
728with a leading ``.`` character to indicate a relative import. This part of the
729PEP was not implemented for Python 2.4, but was completed for Python 2.5.
730
731
732.. seealso::
733
734 :pep:`328` - Imports: Multi-Line and Absolute/Relative
735 Written by Aahz. Multi-line imports were implemented by Dima Dorfman.
736
737.. % ======================================================================
738
739
740PEP 331: Locale-Independent Float/String Conversions
741====================================================
742
743The :mod:`locale` modules lets Python software select various conversions and
744display conventions that are localized to a particular country or language.
745However, the module was careful to not change the numeric locale because various
746functions in Python's implementation required that the numeric locale remain set
747to the ``'C'`` locale. Often this was because the code was using the C
748library's :cfunc:`atof` function.
749
750Not setting the numeric locale caused trouble for extensions that used third-
751party C libraries, however, because they wouldn't have the correct locale set.
752The motivating example was GTK+, whose user interface widgets weren't displaying
753numbers in the current locale.
754
755The solution described in the PEP is to add three new functions to the Python
756API that perform ASCII-only conversions, ignoring the locale setting:
757
758* :cfunc:`PyOS_ascii_strtod(str, ptr)` and :cfunc:`PyOS_ascii_atof(str, ptr)`
759 both convert a string to a C :ctype:`double`.
760
761* :cfunc:`PyOS_ascii_formatd(buffer, buf_len, format, d)` converts a
762 :ctype:`double` to an ASCII string.
763
764The code for these functions came from the GLib library
765(http://developer.gnome.org/arch/gtk/glib.html), whose developers kindly
766relicensed the relevant functions and donated them to the Python Software
767Foundation. The :mod:`locale` module can now change the numeric locale,
768letting extensions such as GTK+ produce the correct results.
769
770
771.. seealso::
772
773 :pep:`331` - Locale-Independent Float/String Conversions
774 Written by Christian R. Reis, and implemented by Gustavo Carneiro.
775
776.. % ======================================================================
777
778
779Other Language Changes
780======================
781
782Here are all of the changes that Python 2.4 makes to the core Python language.
783
784* Decorators for functions and methods were added (:pep:`318`).
785
786* Built-in :func:`set` and :func:`frozenset` types were added (:pep:`218`).
787 Other new built-ins include the :func:`reversed(seq)` function (:pep:`322`).
788
789* Generator expressions were added (:pep:`289`).
790
791* Certain numeric expressions no longer return values restricted to 32 or 64
792 bits (:pep:`237`).
793
794* You can now put parentheses around the list of names in a ``from module import
795 names`` statement (:pep:`328`).
796
797* The :meth:`dict.update` method now accepts the same argument forms as the
798 :class:`dict` constructor. This includes any mapping, any iterable of key/value
799 pairs, and keyword arguments. (Contributed by Raymond Hettinger.)
800
801* The string methods :meth:`ljust`, :meth:`rjust`, and :meth:`center` now take
802 an optional argument for specifying a fill character other than a space.
803 (Contributed by Raymond Hettinger.)
804
805* Strings also gained an :meth:`rsplit` method that works like the :meth:`split`
806 method but splits from the end of the string. (Contributed by Sean
807 Reifschneider.) ::
808
809 >>> 'www.python.org'.split('.', 1)
810 ['www', 'python.org']
811 'www.python.org'.rsplit('.', 1)
812 ['www.python', 'org']
813
814* Three keyword parameters, *cmp*, *key*, and *reverse*, were added to the
815 :meth:`sort` method of lists. These parameters make some common usages of
816 :meth:`sort` simpler. All of these parameters are optional.
817
818 For the *cmp* parameter, the value should be a comparison function that takes
819 two parameters and returns -1, 0, or +1 depending on how the parameters compare.
820 This function will then be used to sort the list. Previously this was the only
821 parameter that could be provided to :meth:`sort`.
822
823 *key* should be a single-parameter function that takes a list element and
824 returns a comparison key for the element. The list is then sorted using the
825 comparison keys. The following example sorts a list case-insensitively::
826
827 >>> L = ['A', 'b', 'c', 'D']
828 >>> L.sort() # Case-sensitive sort
829 >>> L
830 ['A', 'D', 'b', 'c']
831 >>> # Using 'key' parameter to sort list
832 >>> L.sort(key=lambda x: x.lower())
833 >>> L
834 ['A', 'b', 'c', 'D']
835 >>> # Old-fashioned way
836 >>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
837 >>> L
838 ['A', 'b', 'c', 'D']
839
840 The last example, which uses the *cmp* parameter, is the old way to perform a
841 case-insensitive sort. It works but is slower than using a *key* parameter.
842 Using *key* calls :meth:`lower` method once for each element in the list while
843 using *cmp* will call it twice for each comparison, so using *key* saves on
844 invocations of the :meth:`lower` method.
845
846 For simple key functions and comparison functions, it is often possible to avoid
847 a :keyword:`lambda` expression by using an unbound method instead. For example,
848 the above case-insensitive sort is best written as::
849
850 >>> L.sort(key=str.lower)
851 >>> L
852 ['A', 'b', 'c', 'D']
853
854 Finally, the *reverse* parameter takes a Boolean value. If the value is true,
855 the list will be sorted into reverse order. Instead of ``L.sort() ;
856 L.reverse()``, you can now write ``L.sort(reverse=True)``.
857
858 The results of sorting are now guaranteed to be stable. This means that two
859 entries with equal keys will be returned in the same order as they were input.
860 For example, you can sort a list of people by name, and then sort the list by
861 age, resulting in a list sorted by age where people with the same age are in
862 name-sorted order.
863
864 (All changes to :meth:`sort` contributed by Raymond Hettinger.)
865
866* There is a new built-in function :func:`sorted(iterable)` that works like the
867 in-place :meth:`list.sort` method but can be used in expressions. The
868 differences are:
869
870* the input may be any iterable;
871
872* a newly formed copy is sorted, leaving the original intact; and
873
874* the expression returns the new sorted copy
875
876 ::
877
878 >>> L = [9,7,8,3,2,4,1,6,5]
879 >>> [10+i for i in sorted(L)] # usable in a list comprehension
880 [11, 12, 13, 14, 15, 16, 17, 18, 19]
881 >>> L # original is left unchanged
882 [9,7,8,3,2,4,1,6,5]
883 >>> sorted('Monty Python') # any iterable may be an input
884 [' ', 'M', 'P', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y', 'y']
885
886 >>> # List the contents of a dict sorted by key values
887 >>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5)
888 >>> for k, v in sorted(colormap.iteritems()):
889 ... print k, v
890 ...
891 black 4
892 blue 2
893 green 3
894 red 1
895 yellow 5
896
897 (Contributed by Raymond Hettinger.)
898
899* Integer operations will no longer trigger an :exc:`OverflowWarning`. The
900 :exc:`OverflowWarning` warning will disappear in Python 2.5.
901
902* The interpreter gained a new switch, :option:`-m`, that takes a name, searches
903 for the corresponding module on ``sys.path``, and runs the module as a script.
904 For example, you can now run the Python profiler with ``python -m profile``.
905 (Contributed by Nick Coghlan.)
906
907* The :func:`eval(expr, globals, locals)` and :func:`execfile(filename, globals,
908 locals)` functions and the :keyword:`exec` statement now accept any mapping type
909 for the *locals* parameter. Previously this had to be a regular Python
910 dictionary. (Contributed by Raymond Hettinger.)
911
912* The :func:`zip` built-in function and :func:`itertools.izip` now return an
913 empty list if called with no arguments. Previously they raised a
914 :exc:`TypeError` exception. This makes them more suitable for use with variable
915 length argument lists::
916
917 >>> def transpose(array):
918 ... return zip(*array)
919 ...
920 >>> transpose([(1,2,3), (4,5,6)])
921 [(1, 4), (2, 5), (3, 6)]
922 >>> transpose([])
923 []
924
925 (Contributed by Raymond Hettinger.)
926
927* Encountering a failure while importing a module no longer leaves a partially-
928 initialized module object in ``sys.modules``. The incomplete module object left
929 behind would fool further imports of the same module into succeeding, leading to
930 confusing errors. (Fixed by Tim Peters.)
931
932* :const:`None` is now a constant; code that binds a new value to the name
933 ``None`` is now a syntax error. (Contributed by Raymond Hettinger.)
934
935.. % ======================================================================
936
937
938Optimizations
939-------------
940
941* The inner loops for list and tuple slicing were optimized and now run about
942 one-third faster. The inner loops for dictionaries were also optimized,
943 resulting in performance boosts for :meth:`keys`, :meth:`values`, :meth:`items`,
944 :meth:`iterkeys`, :meth:`itervalues`, and :meth:`iteritems`. (Contributed by
945 Raymond Hettinger.)
946
947* The machinery for growing and shrinking lists was optimized for speed and for
948 space efficiency. Appending and popping from lists now runs faster due to more
949 efficient code paths and less frequent use of the underlying system
950 :cfunc:`realloc`. List comprehensions also benefit. :meth:`list.extend` was
951 also optimized and no longer converts its argument into a temporary list before
952 extending the base list. (Contributed by Raymond Hettinger.)
953
954* :func:`list`, :func:`tuple`, :func:`map`, :func:`filter`, and :func:`zip` now
955 run several times faster with non-sequence arguments that supply a
956 :meth:`__len__` method. (Contributed by Raymond Hettinger.)
957
958* The methods :meth:`list.__getitem__`, :meth:`dict.__getitem__`, and
959 :meth:`dict.__contains__` are are now implemented as :class:`method_descriptor`
960 objects rather than :class:`wrapper_descriptor` objects. This form of access
961 doubles their performance and makes them more suitable for use as arguments to
962 functionals: ``map(mydict.__getitem__, keylist)``. (Contributed by Raymond
963 Hettinger.)
964
965* Added a new opcode, ``LIST_APPEND``, that simplifies the generated bytecode
966 for list comprehensions and speeds them up by about a third. (Contributed by
967 Raymond Hettinger.)
968
969* The peephole bytecode optimizer has been improved to produce shorter, faster
970 bytecode; remarkably, the resulting bytecode is more readable. (Enhanced by
971 Raymond Hettinger.)
972
973* String concatenations in statements of the form ``s = s + "abc"`` and ``s +=
974 "abc"`` are now performed more efficiently in certain circumstances. This
975 optimization won't be present in other Python implementations such as Jython, so
976 you shouldn't rely on it; using the :meth:`join` method of strings is still
977 recommended when you want to efficiently glue a large number of strings
978 together. (Contributed by Armin Rigo.)
979
980The net result of the 2.4 optimizations is that Python 2.4 runs the pystone
981benchmark around 5% faster than Python 2.3 and 35% faster than Python 2.2.
982(pystone is not a particularly good benchmark, but it's the most commonly used
983measurement of Python's performance. Your own applications may show greater or
984smaller benefits from Python 2.4.)
985
986.. % pystone is almost useless for comparing different versions of Python;
987.. % instead, it excels at predicting relative Python performance on
988.. % different machines.
989.. % So, this section would be more informative if it used other tools
990.. % such as pybench and parrotbench. For a more application oriented
991.. % benchmark, try comparing the timings of test_decimal.py under 2.3
992.. % and 2.4.
993
994.. % ======================================================================
995
996
997New, Improved, and Deprecated Modules
998=====================================
999
1000As usual, Python's standard library received a number of enhancements and bug
1001fixes. Here's a partial list of the most notable changes, sorted alphabetically
1002by module name. Consult the :file:`Misc/NEWS` file in the source tree for a more
1003complete list of changes, or look through the CVS logs for all the details.
1004
1005* The :mod:`asyncore` module's :func:`loop` function now has a *count* parameter
1006 that lets you perform a limited number of passes through the polling loop. The
1007 default is still to loop forever.
1008
1009* The :mod:`base64` module now has more complete RFC 3548 support for Base64,
1010 Base32, and Base16 encoding and decoding, including optional case folding and
1011 optional alternative alphabets. (Contributed by Barry Warsaw.)
1012
1013* The :mod:`bisect` module now has an underlying C implementation for improved
1014 performance. (Contributed by Dmitry Vasiliev.)
1015
1016* The CJKCodecs collections of East Asian codecs, maintained by Hye-Shik Chang,
1017 was integrated into 2.4. The new encodings are:
1018
1019* Chinese (PRC): gb2312, gbk, gb18030, big5hkscs, hz
1020
1021* Chinese (ROC): big5, cp950
1022
1023* Japanese: cp932, euc-jis-2004, euc-jp, euc-jisx0213, iso-2022-jp,
1024 iso-2022-jp-1, iso-2022-jp-2, iso-2022-jp-3, iso-2022-jp-ext, iso-2022-jp-2004,
1025 shift-jis, shift-jisx0213, shift-jis-2004
1026
1027* Korean: cp949, euc-kr, johab, iso-2022-kr
1028
1029* Some other new encodings were added: HP Roman8, ISO_8859-11, ISO_8859-16,
1030 PCTP-154, and TIS-620.
1031
1032* The UTF-8 and UTF-16 codecs now cope better with receiving partial input.
1033 Previously the :class:`StreamReader` class would try to read more data, making
1034 it impossible to resume decoding from the stream. The :meth:`read` method will
1035 now return as much data as it can and future calls will resume decoding where
1036 previous ones left off. (Implemented by Walter Dörwald.)
1037
1038* There is a new :mod:`collections` module for various specialized collection
1039 datatypes. Currently it contains just one type, :class:`deque`, a double-
1040 ended queue that supports efficiently adding and removing elements from either
1041 end::
1042
1043 >>> from collections import deque
1044 >>> d = deque('ghi') # make a new deque with three items
1045 >>> d.append('j') # add a new entry to the right side
1046 >>> d.appendleft('f') # add a new entry to the left side
1047 >>> d # show the representation of the deque
1048 deque(['f', 'g', 'h', 'i', 'j'])
1049 >>> d.pop() # return and remove the rightmost item
1050 'j'
1051 >>> d.popleft() # return and remove the leftmost item
1052 'f'
1053 >>> list(d) # list the contents of the deque
1054 ['g', 'h', 'i']
1055 >>> 'h' in d # search the deque
1056 True
1057
1058 Several modules, such as the :mod:`Queue` and :mod:`threading` modules, now take
1059 advantage of :class:`collections.deque` for improved performance. (Contributed
1060 by Raymond Hettinger.)
1061
1062* The :mod:`ConfigParser` classes have been enhanced slightly. The :meth:`read`
1063 method now returns a list of the files that were successfully parsed, and the
1064 :meth:`set` method raises :exc:`TypeError` if passed a *value* argument that
1065 isn't a string. (Contributed by John Belmonte and David Goodger.)
1066
1067* The :mod:`curses` module now supports the ncurses extension
1068 :func:`use_default_colors`. On platforms where the terminal supports
1069 transparency, this makes it possible to use a transparent background.
1070 (Contributed by Jörg Lehmann.)
1071
1072* The :mod:`difflib` module now includes an :class:`HtmlDiff` class that creates
1073 an HTML table showing a side by side comparison of two versions of a text.
1074 (Contributed by Dan Gass.)
1075
1076* The :mod:`email` package was updated to version 3.0, which dropped various
1077 deprecated APIs and removes support for Python versions earlier than 2.3. The
1078 3.0 version of the package uses a new incremental parser for MIME messages,
1079 available in the :mod:`email.FeedParser` module. The new parser doesn't require
1080 reading the entire message into memory, and doesn't throw exceptions if a
1081 message is malformed; instead it records any problems in the :attr:`defect`
1082 attribute of the message. (Developed by Anthony Baxter, Barry Warsaw, Thomas
1083 Wouters, and others.)
1084
1085* The :mod:`heapq` module has been converted to C. The resulting tenfold
1086 improvement in speed makes the module suitable for handling high volumes of
1087 data. In addition, the module has two new functions :func:`nlargest` and
1088 :func:`nsmallest` that use heaps to find the N largest or smallest values in a
1089 dataset without the expense of a full sort. (Contributed by Raymond Hettinger.)
1090
1091* The :mod:`httplib` module now contains constants for HTTP status codes defined
1092 in various HTTP-related RFC documents. Constants have names such as
1093 :const:`OK`, :const:`CREATED`, :const:`CONTINUE`, and
1094 :const:`MOVED_PERMANENTLY`; use pydoc to get a full list. (Contributed by
1095 Andrew Eland.)
1096
1097* The :mod:`imaplib` module now supports IMAP's THREAD command (contributed by
1098 Yves Dionne) and new :meth:`deleteacl` and :meth:`myrights` methods (contributed
1099 by Arnaud Mazin).
1100
1101* The :mod:`itertools` module gained a :func:`groupby(iterable[, *func*])`
1102 function. *iterable* is something that can be iterated over to return a stream
1103 of elements, and the optional *func* parameter is a function that takes an
1104 element and returns a key value; if omitted, the key is simply the element
1105 itself. :func:`groupby` then groups the elements into subsequences which have
1106 matching values of the key, and returns a series of 2-tuples containing the key
1107 value and an iterator over the subsequence.
1108
1109 Here's an example to make this clearer. The *key* function simply returns
1110 whether a number is even or odd, so the result of :func:`groupby` is to return
1111 consecutive runs of odd or even numbers. ::
1112
1113 >>> import itertools
1114 >>> L = [2, 4, 6, 7, 8, 9, 11, 12, 14]
1115 >>> for key_val, it in itertools.groupby(L, lambda x: x % 2):
1116 ... print key_val, list(it)
1117 ...
1118 0 [2, 4, 6]
1119 1 [7]
1120 0 [8]
1121 1 [9, 11]
1122 0 [12, 14]
1123 >>>
1124
1125 :func:`groupby` is typically used with sorted input. The logic for
1126 :func:`groupby` is similar to the Unix ``uniq`` filter which makes it handy for
1127 eliminating, counting, or identifying duplicate elements::
1128
1129 >>> word = 'abracadabra'
1130 >>> letters = sorted(word) # Turn string into a sorted list of letters
1131 >>> letters
1132 ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r']
1133 >>> for k, g in itertools.groupby(letters):
1134 ... print k, list(g)
1135 ...
1136 a ['a', 'a', 'a', 'a', 'a']
1137 b ['b', 'b']
1138 c ['c']
1139 d ['d']
1140 r ['r', 'r']
1141 >>> # List unique letters
1142 >>> [k for k, g in groupby(letters)]
1143 ['a', 'b', 'c', 'd', 'r']
1144 >>> # Count letter occurrences
1145 >>> [(k, len(list(g))) for k, g in groupby(letters)]
1146 [('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]
1147
1148 (Contributed by Hye-Shik Chang.)
1149
1150* :mod:`itertools` also gained a function named :func:`tee(iterator, N)` that
1151 returns *N* independent iterators that replicate *iterator*. If *N* is omitted,
1152 the default is 2. ::
1153
1154 >>> L = [1,2,3]
1155 >>> i1, i2 = itertools.tee(L)
1156 >>> i1,i2
1157 (<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>)
1158 >>> list(i1) # Run the first iterator to exhaustion
1159 [1, 2, 3]
1160 >>> list(i2) # Run the second iterator to exhaustion
1161 [1, 2, 3]
1162
1163 Note that :func:`tee` has to keep copies of the values returned by the
1164 iterator; in the worst case, it may need to keep all of them. This should
1165 therefore be used carefully if the leading iterator can run far ahead of the
1166 trailing iterator in a long stream of inputs. If the separation is large, then
1167 you might as well use :func:`list` instead. When the iterators track closely
1168 with one another, :func:`tee` is ideal. Possible applications include
1169 bookmarking, windowing, or lookahead iterators. (Contributed by Raymond
1170 Hettinger.)
1171
1172* A number of functions were added to the :mod:`locale` module, such as
1173 :func:`bind_textdomain_codeset` to specify a particular encoding and a family of
1174 :func:`l\*gettext` functions that return messages in the chosen encoding.
1175 (Contributed by Gustavo Niemeyer.)
1176
1177* Some keyword arguments were added to the :mod:`logging` package's
1178 :func:`basicConfig` function to simplify log configuration. The default
1179 behavior is to log messages to standard error, but various keyword arguments can
1180 be specified to log to a particular file, change the logging format, or set the
1181 logging level. For example::
1182
1183 import logging
1184 logging.basicConfig(filename='/var/log/application.log',
1185 level=0, # Log all messages
1186 format='%(levelname):%(process):%(thread):%(message)')
1187
1188 Other additions to the :mod:`logging` package include a :meth:`log(level, msg)`
1189 convenience method, as well as a :class:`TimedRotatingFileHandler` class that
1190 rotates its log files at a timed interval. The module already had
1191 :class:`RotatingFileHandler`, which rotated logs once the file exceeded a
1192 certain size. Both classes derive from a new :class:`BaseRotatingHandler` class
1193 that can be used to implement other rotating handlers.
1194
1195 (Changes implemented by Vinay Sajip.)
1196
1197* The :mod:`marshal` module now shares interned strings on unpacking a data
1198 structure. This may shrink the size of certain pickle strings, but the primary
1199 effect is to make :file:`.pyc` files significantly smaller. (Contributed by
1200 Martin von Löwis.)
1201
1202* The :mod:`nntplib` module's :class:`NNTP` class gained :meth:`description` and
1203 :meth:`descriptions` methods to retrieve newsgroup descriptions for a single
1204 group or for a range of groups. (Contributed by JĂĽrgen A. Erhard.)
1205
1206* Two new functions were added to the :mod:`operator` module,
1207 :func:`attrgetter(attr)` and :func:`itemgetter(index)`. Both functions return
1208 callables that take a single argument and return the corresponding attribute or
1209 item; these callables make excellent data extractors when used with :func:`map`
1210 or :func:`sorted`. For example::
1211
1212 >>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
1213 >>> map(operator.itemgetter(0), L)
1214 ['c', 'd', 'a', 'b']
1215 >>> map(operator.itemgetter(1), L)
1216 [2, 1, 4, 3]
1217 >>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item
1218 [('d', 1), ('c', 2), ('b', 3), ('a', 4)]
1219
1220 (Contributed by Raymond Hettinger.)
1221
1222* The :mod:`optparse` module was updated in various ways. The module now passes
1223 its messages through :func:`gettext.gettext`, making it possible to
1224 internationalize Optik's help and error messages. Help messages for options can
1225 now include the string ``'%default'``, which will be replaced by the option's
1226 default value. (Contributed by Greg Ward.)
1227
1228* The long-term plan is to deprecate the :mod:`rfc822` module in some future
1229 Python release in favor of the :mod:`email` package. To this end, the
1230 :func:`email.Utils.formatdate` function has been changed to make it usable as a
1231 replacement for :func:`rfc822.formatdate`. You may want to write new e-mail
1232 processing code with this in mind. (Change implemented by Anthony Baxter.)
1233
1234* A new :func:`urandom(n)` function was added to the :mod:`os` module, returning
1235 a string containing *n* bytes of random data. This function provides access to
1236 platform-specific sources of randomness such as :file:`/dev/urandom` on Linux or
1237 the Windows CryptoAPI. (Contributed by Trevor Perrin.)
1238
1239* Another new function: :func:`os.path.lexists(path)` returns true if the file
1240 specified by *path* exists, whether or not it's a symbolic link. This differs
1241 from the existing :func:`os.path.exists(path)` function, which returns false if
1242 *path* is a symlink that points to a destination that doesn't exist.
1243 (Contributed by Beni Cherniavsky.)
1244
1245* A new :func:`getsid` function was added to the :mod:`posix` module that
1246 underlies the :mod:`os` module. (Contributed by J. Raynor.)
1247
1248* The :mod:`poplib` module now supports POP over SSL. (Contributed by Hector
1249 Urtubia.)
1250
1251* The :mod:`profile` module can now profile C extension functions. (Contributed
1252 by Nick Bastin.)
1253
1254* The :mod:`random` module has a new method called :meth:`getrandbits(N)` that
1255 returns a long integer *N* bits in length. The existing :meth:`randrange`
1256 method now uses :meth:`getrandbits` where appropriate, making generation of
1257 arbitrarily large random numbers more efficient. (Contributed by Raymond
1258 Hettinger.)
1259
1260* The regular expression language accepted by the :mod:`re` module was extended
1261 with simple conditional expressions, written as ``(?(group)A|B)``. *group* is
1262 either a numeric group ID or a group name defined with ``(?P<group>...)``
1263 earlier in the expression. If the specified group matched, the regular
1264 expression pattern *A* will be tested against the string; if the group didn't
1265 match, the pattern *B* will be used instead. (Contributed by Gustavo Niemeyer.)
1266
1267* The :mod:`re` module is also no longer recursive, thanks to a massive amount
1268 of work by Gustavo Niemeyer. In a recursive regular expression engine, certain
1269 patterns result in a large amount of C stack space being consumed, and it was
1270 possible to overflow the stack. For example, if you matched a 30000-byte string
1271 of ``a`` characters against the expression ``(a|b)+``, one stack frame was
1272 consumed per character. Python 2.3 tried to check for stack overflow and raise
1273 a :exc:`RuntimeError` exception, but certain patterns could sidestep the
1274 checking and if you were unlucky Python could segfault. Python 2.4's regular
1275 expression engine can match this pattern without problems.
1276
1277* The :mod:`signal` module now performs tighter error-checking on the parameters
1278 to the :func:`signal.signal` function. For example, you can't set a handler on
1279 the :const:`SIGKILL` signal; previous versions of Python would quietly accept
1280 this, but 2.4 will raise a :exc:`RuntimeError` exception.
1281
1282* Two new functions were added to the :mod:`socket` module. :func:`socketpair`
1283 returns a pair of connected sockets and :func:`getservbyport(port)` looks up the
1284 service name for a given port number. (Contributed by Dave Cole and Barry
1285 Warsaw.)
1286
1287* The :func:`sys.exitfunc` function has been deprecated. Code should be using
1288 the existing :mod:`atexit` module, which correctly handles calling multiple exit
1289 functions. Eventually :func:`sys.exitfunc` will become a purely internal
1290 interface, accessed only by :mod:`atexit`.
1291
1292* The :mod:`tarfile` module now generates GNU-format tar files by default.
1293 (Contributed by Lars Gustaebel.)
1294
1295* The :mod:`threading` module now has an elegantly simple way to support
1296 thread-local data. The module contains a :class:`local` class whose attribute
1297 values are local to different threads. ::
1298
1299 import threading
1300
1301 data = threading.local()
1302 data.number = 42
1303 data.url = ('www.python.org', 80)
1304
1305 Other threads can assign and retrieve their own values for the :attr:`number`
1306 and :attr:`url` attributes. You can subclass :class:`local` to initialize
1307 attributes or to add methods. (Contributed by Jim Fulton.)
1308
1309* The :mod:`timeit` module now automatically disables periodic garbage
1310 collection during the timing loop. This change makes consecutive timings more
1311 comparable. (Contributed by Raymond Hettinger.)
1312
1313* The :mod:`weakref` module now supports a wider variety of objects including
1314 Python functions, class instances, sets, frozensets, deques, arrays, files,
1315 sockets, and regular expression pattern objects. (Contributed by Raymond
1316 Hettinger.)
1317
1318* The :mod:`xmlrpclib` module now supports a multi-call extension for
1319 transmitting multiple XML-RPC calls in a single HTTP operation. (Contributed by
1320 Brian Quinlan.)
1321
1322* The :mod:`mpz`, :mod:`rotor`, and :mod:`xreadlines` modules have been
1323 removed.
1324
1325.. % ======================================================================
1326.. % whole new modules get described in subsections here
1327.. % =====================
1328
1329
1330cookielib
1331---------
1332
1333The :mod:`cookielib` library supports client-side handling for HTTP cookies,
1334mirroring the :mod:`Cookie` module's server-side cookie support. Cookies are
1335stored in cookie jars; the library transparently stores cookies offered by the
1336web server in the cookie jar, and fetches the cookie from the jar when
1337connecting to the server. As in web browsers, policy objects control whether
1338cookies are accepted or not.
1339
1340In order to store cookies across sessions, two implementations of cookie jars
1341are provided: one that stores cookies in the Netscape format so applications can
1342use the Mozilla or Lynx cookie files, and one that stores cookies in the same
1343format as the Perl libwww library.
1344
1345:mod:`urllib2` has been changed to interact with :mod:`cookielib`:
1346:class:`HTTPCookieProcessor` manages a cookie jar that is used when accessing
1347URLs.
1348
1349This module was contributed by John J. Lee.
1350
1351.. % ==================
1352
1353
1354doctest
1355-------
1356
1357The :mod:`doctest` module underwent considerable refactoring thanks to Edward
1358Loper and Tim Peters. Testing can still be as simple as running
1359:func:`doctest.testmod`, but the refactorings allow customizing the module's
1360operation in various ways
1361
1362The new :class:`DocTestFinder` class extracts the tests from a given object's
1363docstrings::
1364
1365 def f (x, y):
1366 """>>> f(2,2)
1367 4
1368 >>> f(3,2)
1369 6
1370 """
1371 return x*y
1372
1373 finder = doctest.DocTestFinder()
1374
1375 # Get list of DocTest instances
1376 tests = finder.find(f)
1377
1378The new :class:`DocTestRunner` class then runs individual tests and can produce
1379a summary of the results::
1380
1381 runner = doctest.DocTestRunner()
1382 for t in tests:
1383 tried, failed = runner.run(t)
1384
1385 runner.summarize(verbose=1)
1386
1387The above example produces the following output::
1388
1389 1 items passed all tests:
1390 2 tests in f
1391 2 tests in 1 items.
1392 2 passed and 0 failed.
1393 Test passed.
1394
1395:class:`DocTestRunner` uses an instance of the :class:`OutputChecker` class to
1396compare the expected output with the actual output. This class takes a number
1397of different flags that customize its behaviour; ambitious users can also write
1398a completely new subclass of :class:`OutputChecker`.
1399
1400The default output checker provides a number of handy features. For example,
1401with the :const:`doctest.ELLIPSIS` option flag, an ellipsis (``...``) in the
1402expected output matches any substring, making it easier to accommodate outputs
1403that vary in minor ways::
1404
1405 def o (n):
1406 """>>> o(1)
1407 <__main__.C instance at 0x...>
1408 >>>
1409 """
1410
1411Another special string, ``<BLANKLINE>``, matches a blank line::
1412
1413 def p (n):
1414 """>>> p(1)
1415 <BLANKLINE>
1416 >>>
1417 """
1418
1419Another new capability is producing a diff-style display of the output by
1420specifying the :const:`doctest.REPORT_UDIFF` (unified diffs),
1421:const:`doctest.REPORT_CDIFF` (context diffs), or :const:`doctest.REPORT_NDIFF`
1422(delta-style) option flags. For example::
1423
1424 def g (n):
1425 """>>> g(4)
1426 here
1427 is
1428 a
1429 lengthy
1430 >>>"""
1431 L = 'here is a rather lengthy list of words'.split()
1432 for word in L[:n]:
1433 print word
1434
1435Running the above function's tests with :const:`doctest.REPORT_UDIFF` specified,
1436you get the following output::
1437
1438 **********************************************************************
1439 File ``t.py'', line 15, in g
1440 Failed example:
1441 g(4)
1442 Differences (unified diff with -expected +actual):
1443 @@ -2,3 +2,3 @@
1444 is
1445 a
1446 -lengthy
1447 +rather
1448 **********************************************************************
1449
1450.. % ======================================================================
1451
1452
1453Build and C API Changes
1454=======================
1455
1456Some of the changes to Python's build process and to the C API are:
1457
1458* Three new convenience macros were added for common return values from
1459 extension functions: :cmacro:`Py_RETURN_NONE`, :cmacro:`Py_RETURN_TRUE`, and
1460 :cmacro:`Py_RETURN_FALSE`. (Contributed by Brett Cannon.)
1461
1462* Another new macro, :cmacro:`Py_CLEAR(obj)`, decreases the reference count of
1463 *obj* and sets *obj* to the null pointer. (Contributed by Jim Fulton.)
1464
1465* A new function, :cfunc:`PyTuple_Pack(N, obj1, obj2, ..., objN)`, constructs
1466 tuples from a variable length argument list of Python objects. (Contributed by
1467 Raymond Hettinger.)
1468
1469* A new function, :cfunc:`PyDict_Contains(d, k)`, implements fast dictionary
1470 lookups without masking exceptions raised during the look-up process.
1471 (Contributed by Raymond Hettinger.)
1472
1473* The :cmacro:`Py_IS_NAN(X)` macro returns 1 if its float or double argument
1474 *X* is a NaN. (Contributed by Tim Peters.)
1475
1476* C code can avoid unnecessary locking by using the new
1477 :cfunc:`PyEval_ThreadsInitialized` function to tell if any thread operations
1478 have been performed. If this function returns false, no lock operations are
1479 needed. (Contributed by Nick Coghlan.)
1480
1481* A new function, :cfunc:`PyArg_VaParseTupleAndKeywords`, is the same as
1482 :cfunc:`PyArg_ParseTupleAndKeywords` but takes a :ctype:`va_list` instead of a
1483 number of arguments. (Contributed by Greg Chapman.)
1484
1485* A new method flag, :const:`METH_COEXISTS`, allows a function defined in slots
1486 to co-exist with a :ctype:`PyCFunction` having the same name. This can halve
1487 the access time for a method such as :meth:`set.__contains__`. (Contributed by
1488 Raymond Hettinger.)
1489
1490* Python can now be built with additional profiling for the interpreter itself,
1491 intended as an aid to people developing the Python core. Providing
1492 :option:`----enable-profiling` to the :program:`configure` script will let you
1493 profile the interpreter with :program:`gprof`, and providing the
1494 :option:`----with-tsc` switch enables profiling using the Pentium's Time-Stamp-
1495 Counter register. Note that the :option:`----with-tsc` switch is slightly
1496 misnamed, because the profiling feature also works on the PowerPC platform,
1497 though that processor architecture doesn't call that register "the TSC
1498 register". (Contributed by Jeremy Hylton.)
1499
1500* The :ctype:`tracebackobject` type has been renamed to
1501 :ctype:`PyTracebackObject`.
1502
1503.. % ======================================================================
1504
1505
1506Port-Specific Changes
1507---------------------
1508
1509* The Windows port now builds under MSVC++ 7.1 as well as version 6.
1510 (Contributed by Martin von Löwis.)
1511
1512.. % ======================================================================
1513
1514
1515Porting to Python 2.4
1516=====================
1517
1518This section lists previously described changes that may require changes to your
1519code:
1520
1521* Left shifts and hexadecimal/octal constants that are too large no longer
1522 trigger a :exc:`FutureWarning` and return a value limited to 32 or 64 bits;
1523 instead they return a long integer.
1524
1525* Integer operations will no longer trigger an :exc:`OverflowWarning`. The
1526 :exc:`OverflowWarning` warning will disappear in Python 2.5.
1527
1528* The :func:`zip` built-in function and :func:`itertools.izip` now return an
1529 empty list instead of raising a :exc:`TypeError` exception if called with no
1530 arguments.
1531
1532* You can no longer compare the :class:`date` and :class:`datetime` instances
1533 provided by the :mod:`datetime` module. Two instances of different classes
1534 will now always be unequal, and relative comparisons (``<``, ``>``) will raise
1535 a :exc:`TypeError`.
1536
1537* :func:`dircache.listdir` now passes exceptions to the caller instead of
1538 returning empty lists.
1539
1540* :func:`LexicalHandler.startDTD` used to receive the public and system IDs in
1541 the wrong order. This has been corrected; applications relying on the wrong
1542 order need to be fixed.
1543
1544* :func:`fcntl.ioctl` now warns if the *mutate* argument is omitted and
1545 relevant.
1546
1547* The :mod:`tarfile` module now generates GNU-format tar files by default.
1548
1549* Encountering a failure while importing a module no longer leaves a partially-
1550 initialized module object in ``sys.modules``.
1551
1552* :const:`None` is now a constant; code that binds a new value to the name
1553 ``None`` is now a syntax error.
1554
1555* The :func:`signals.signal` function now raises a :exc:`RuntimeError` exception
1556 for certain illegal values; previously these errors would pass silently. For
1557 example, you can no longer set a handler on the :const:`SIGKILL` signal.
1558
1559.. % ======================================================================
1560
1561
1562.. _acks:
1563
1564Acknowledgements
1565================
1566
1567The author would like to thank the following people for offering suggestions,
1568corrections and assistance with various drafts of this article: Koray Can, Hye-
1569Shik Chang, Michael Dyck, Raymond Hettinger, Brian Hurt, Hamish Lawson, Fredrik
1570Lundh, Sean Reifschneider, Sadruddin Rejeb.
1571