diff options
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/c-api/arg.rst | 12 | ||||
-rw-r--r-- | Doc/library/inspect.rst | 4 | ||||
-rw-r--r-- | Doc/library/itertools.rst | 45 | ||||
-rw-r--r-- | Doc/library/signal.rst | 4 | ||||
-rw-r--r-- | Doc/reference/expressions.rst | 14 | ||||
-rw-r--r-- | Doc/whatsnew/2.6.rst | 176 |
6 files changed, 223 insertions, 32 deletions
diff --git a/Doc/c-api/arg.rst b/Doc/c-api/arg.rst index 5f82904..faf97ed 100644 --- a/Doc/c-api/arg.rst +++ b/Doc/c-api/arg.rst @@ -212,7 +212,7 @@ variable(s) whose address should be passed. :ctype:`void\*` argument that was passed to the :cfunc:`PyArg_Parse\*` function. The returned *status* should be ``1`` for a successful conversion and ``0`` if the conversion has failed. When the conversion fails, the *converter* function - should raise an exception. + should raise an exception and leave the content of *address* unmodified. ``S`` (string) [PyStringObject \*] Like ``O`` but requires that the Python object is a string object. Raises @@ -284,9 +284,13 @@ from the input tuple. There are a few cases, as described in the list of format units above, where these parameters are used as input values; they should match what is specified for the corresponding format unit in that case. -For the conversion to succeed, the *arg* object must match the format and the -format must be exhausted. On success, the :cfunc:`PyArg_Parse\*` functions -return true, otherwise they return false and raise an appropriate exception. +For the conversion to succeed, the *arg* object must match the format +and the format must be exhausted. On success, the +:cfunc:`PyArg_Parse\*` functions return true, otherwise they return +false and raise an appropriate exception. When the +:cfunc:`PyArg_Parse\*` functions fail due to conversion failure in one +of the format units, the variables at the addresses corresponding to that +and the following format units are left untouched. .. cfunction:: int PyArg_ParseTuple(PyObject *args, const char *format, ...) diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index c2bc15c..d622e1d 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -26,7 +26,7 @@ Types and members ----------------- The :func:`getmembers` function retrieves the members of an object such as a -class or module. The fifteen functions whose names begin with "is" are mainly +class or module. The sixteen functions whose names begin with "is" are mainly provided as convenient choices for the second argument to :func:`getmembers`. They also help you determine when you can expect to find the following special attributes: @@ -267,8 +267,6 @@ attributes: Return true if the object is an abstract base class. - .. versionadded:: 2.6 - .. function:: ismethoddescriptor(object) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 9da51aa..0d74c59 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -87,6 +87,7 @@ loops that truncate the stream. .. versionadded:: 2.6 + .. function:: combinations(iterable, r) Return successive *r* length combinations of elements in the *iterable*. @@ -121,6 +122,17 @@ loops that truncate the stream. indices[j] = indices[j-1] + 1 yield tuple(pool[i] for i in indices) + The code for :func:`combinations` can be also expressed as a subsequence + of :func:`permutations` after filtering entries where the elements are not + in sorted order (according to their position in the input pool):: + + def combinations(iterable, r): + pool = tuple(iterable) + n = len(pool) + for indices in permutations(range(n), r): + if sorted(indices) == list(indices): + yield tuple(pool[i] for i in indices) + .. versionadded:: 2.6 .. function:: count([n]) @@ -378,6 +390,18 @@ loops that truncate the stream. else: return + The code for :func:`permutations` can be also expressed as a subsequence of + :func:`product`, filtered to exclude entries with repeated elements (those + from the same position in the input pool):: + + def permutations(iterable, r=None): + pool = tuple(iterable) + n = len(pool) + r = n if r is None else r + for indices in product(range(n), repeat=r): + if len(set(indices)) == r: + yield tuple(pool[i] for i in indices) + .. versionadded:: 2.6 .. function:: product(*iterables[, repeat]) @@ -388,26 +412,25 @@ loops that truncate the stream. ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``. The leftmost iterators are in the outermost for-loop, so the output tuples - cycle in a manner similar to an odometer (with the rightmost element - changing on every iteration). This results in a lexicographic ordering - so that if the inputs iterables are sorted, the product tuples are emitted + cycle like an odometer (with the rightmost element changing on every + iteration). This results in a lexicographic ordering so that if the + inputs iterables are sorted, the product tuples are emitted in sorted order. To compute the product of an iterable with itself, specify the number of repetitions with the optional *repeat* keyword argument. For example, ``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``. - Equivalent to the following except that the actual implementation does not - build-up intermediate results in memory:: + This function is equivalent to the following code, except that the + actual implementation does not build up intermediate results in memory:: def product(*args, **kwds): pools = map(tuple, args) * kwds.get('repeat', 1) - if pools: - result = [[]] - for pool in pools: - result = [x+[y] for x in result for y in pool] - for prod in result: - yield tuple(prod) + result = [[]] + for pool in pools: + result = [x+[y] for x in result for y in pool] + for prod in result: + yield tuple(prod) .. function:: repeat(object[, times]) diff --git a/Doc/library/signal.rst b/Doc/library/signal.rst index cf6f8f8..2e5cae5 100644 --- a/Doc/library/signal.rst +++ b/Doc/library/signal.rst @@ -128,12 +128,12 @@ The :mod:`signal` module defines the following functions: .. function:: siginterrupt(signalnum, flag) Change system call restart behaviour: if *flag* is :const:`False`, system calls - will be restarted when interrupted by signal *signalnum*, else system calls will + will be restarted when interrupted by signal *signalnum*, otherwise system calls will be interrupted. Returns nothing. Availability: Unix, Mac (see the man page :manpage:`siginterrupt(3)` for further information). Note that installing a signal handler with :func:`signal` will reset the restart - behaviour to interruptible by implicitly calling siginterrupt with a true *flag* + behaviour to interruptible by implicitly calling :cfunc:`siginterrupt` with a true *flag* value for the given signal. .. versionadded:: 2.6 diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index e8efa9f..ac3c90f 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -232,6 +232,20 @@ and added to the set object. When a comprehension is supplied, the set is constructed from the elements resulting from the comprehension. +Variables used in the generator expression are evaluated lazily in a separate +scope when the :meth:`next` method is called for the generator object (in the +same fashion as for normal generators). However, the :keyword:`in` expression +of the leftmost :keyword:`for` clause is immediately evaluated in the current +scope so that an error produced by it can be seen before any other possible +error in the code that handles the generator expression. Subsequent +:keyword:`for` and :keyword:`if` clauses cannot be evaluated immediately since +they may depend on the previous :keyword:`for` loop. For example: +``(x*y for x in range(10) for y in bar(x))``. + +The parentheses can be omitted on calls with only one argument. See section +:ref:`calls` for the detail. + + .. _dict: Dictionary displays diff --git a/Doc/whatsnew/2.6.rst b/Doc/whatsnew/2.6.rst index a0ec071..bba46c9 100644 --- a/Doc/whatsnew/2.6.rst +++ b/Doc/whatsnew/2.6.rst @@ -450,6 +450,15 @@ can now be used in scripts running from inside a package. .. ====================================================================== +.. _pep-3101: + +PEP 3101: Advanced String Formatting +===================================================== + +XXX write this + +.. ====================================================================== + .. _pep-3110: PEP 3110: Exception-Handling Changes @@ -544,6 +553,32 @@ an abstract method. .. ====================================================================== +.. _pep-3127: + +PEP 3127: Integer Literal Support and Syntax +===================================================== + +XXX write this + +Python 3.0 changes the syntax for octal integer literals, and +adds supports for binary integers: 0o instad of 0, +and 0b for binary. Python 2.6 doesn't support this, but a bin() +builtin was added, and + + +New bin() built-in returns the binary form of a number. + +.. ====================================================================== + +.. _pep-3129: + +PEP 3129: Class Decorators +===================================================== + +XXX write this. + +.. ====================================================================== + .. _pep-3141: PEP 3141: A Type Hierarchy for Numbers @@ -579,7 +614,9 @@ and comparisons. :class:`Rational` numbers derive from :class:`Real`, have :attr:`numerator` and :attr:`denominator` properties, and can be converted to floats. Python 2.6 adds a simple rational-number class, -:class:`Fraction`, in the :mod:`fractions` module. +:class:`Fraction`, in the :mod:`fractions` module. (It's called +:class:`Fraction` instead of :class:`Rational` to avoid +a name clash with :class:`numbers.Rational`.) :class:`Integral` numbers derive from :class:`Rational`, and can be shifted left and right with ``<<`` and ``>>``, @@ -587,9 +624,9 @@ combined using bitwise operations such as ``&`` and ``|``, and can be used as array indexes and slice boundaries. In Python 3.0, the PEP slightly redefines the existing built-ins -:func:`math.floor`, :func:`math.ceil`, :func:`round`, and adds a new -one, :func:`trunc`, that's been backported to Python 2.6. -:func:`trunc` rounds toward zero, returning the closest +:func:`round`, :func:`math.floor`, :func:`math.ceil`, and adds a new +one, :func:`math.trunc`, that's been backported to Python 2.6. +:func:`math.trunc` rounds toward zero, returning the closest :class:`Integral` that's between the function's argument and zero. .. seealso:: @@ -603,7 +640,7 @@ The Fraction Module To fill out the hierarchy of numeric types, a rational-number class has been added as the :mod:`fractions` module. Rational numbers are -represented as a fraction; rational numbers can exactly represent +represented as a fraction, and can exactly represent numbers such as two-thirds that floating-point numbers can only approximate. @@ -692,7 +729,7 @@ Here are all of the changes that Python 2.6 makes to the core Python language. A numerical nicety: when creating a complex number from two floats on systems that support signed zeros (-0 and +0), the - :func:`complex()` constructor will now preserve the sign + :func:`complex` constructor will now preserve the sign of the zero. .. Patch 1507 @@ -789,6 +826,15 @@ Optimizations built-in types. This speeds up checking if an object is a subclass of one of these types. (Contributed by Neal Norwitz.) +* Unicode strings now uses faster code for detecting + whitespace and line breaks; this speeds up the :meth:`split` method + by about 25% and :meth:`splitlines` by 35%. + (Contributed by Antoine Pitrou.) + +* To reduce memory usage, the garbage collector will now clear internal + free lists when garbage-collecting the highest generation of objects. + This may return memory to the OS sooner. + The net result of the 2.6 optimizations is that Python 2.6 runs the pystone benchmark around XX% faster than Python 2.5. @@ -956,15 +1002,69 @@ complete list of changes, or look through the CVS logs for all the details. can also be accessed as attributes. (Contributed by Raymond Hettinger.) -* A new function in the :mod:`itertools` module: ``izip_longest(iter1, iter2, - ...[, fillvalue])`` makes tuples from each of the elements; if some of the - iterables are shorter than others, the missing values are set to *fillvalue*. - For example:: + Some new functions in the module include + :func:`isgenerator`, :func:`isgeneratorfunction`, + and :func:`isabstract`. + +* The :mod:`itertools` module gained several new functions. + + ``izip_longest(iter1, iter2, ...[, fillvalue])`` makes tuples from + each of the elements; if some of the iterables are shorter than + others, the missing values are set to *fillvalue*. For example:: itertools.izip_longest([1,2,3], [1,2,3,4,5]) -> [(1, 1), (2, 2), (3, 3), (None, 4), (None, 5)] - (Contributed by Raymond Hettinger.) + ``product(iter1, iter2, ..., [repeat=N])`` returns the Cartesian product + of the supplied iterables, a set of tuples containing + every possible combination of the elements returned from each iterable. :: + + itertools.product([1,2,3], [4,5,6]) -> + [(1, 4), (1, 5), (1, 6), + (2, 4), (2, 5), (2, 6), + (3, 4), (3, 5), (3, 6)] + + The optional *repeat* keyword argument is used for taking the + product of an iterable or a set of iterables with themselves, + repeated *N* times. With a single iterable argument, *N*-tuples + are returned:: + + itertools.product([1,2], repeat=3)) -> + [(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), + (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)] + + With two iterables, *2N*-tuples are returned. :: + + itertools(product([1,2], [3,4], repeat=2) -> + [(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4), + (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4), + (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4), + (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)] + + ``combinations(iter, r)`` returns combinations of length *r* from + the elements of *iterable*. :: + + itertools.combinations('123', 2) -> + [('1', '2'), ('1', '3'), ('2', '3')] + + itertools.combinations('123', 3) -> + [('1', '2', '3')] + + itertools.combinations('1234', 3) -> + [('1', '2', '3'), ('1', '2', '4'), ('1', '3', '4'), + ('2', '3', '4')] + + ``itertools.chain(*iterables)` is an existing function in + :mod:`itertools` that gained a new constructor. + ``itertools.chain.from_iterable(iterable)`` takes a single + iterable that should return other iterables. :func:`chain` will + then return all the elements of the first iterable, then + all the elements of the second, and so on. :: + + chain.from_iterable([[1,2,3], [4,5,6]]) -> + [1, 2, 3, 4, 5, 6] + + (All contributed by Raymond Hettinger.) * The :mod:`macfs` module has been removed. This in turn required the :func:`macostools.touched` function to be removed because it depended on the @@ -975,7 +1075,7 @@ complete list of changes, or look through the CVS logs for all the details. * :class:`mmap` objects now have a :meth:`rfind` method that finds a substring, beginning at the end of the string and searching backwards. The :meth:`find` method - also gained a *end* parameter containing the index at which to stop + also gained an *end* parameter containing the index at which to stop the forward search. (Contributed by John Lenton.) @@ -984,6 +1084,29 @@ complete list of changes, or look through the CVS logs for all the details. triggers a warning message when Python is running in 3.0-warning mode. +* The :mod:`operator` module gained a + :func:`methodcaller` function that takes a name and an optional + set of arguments, returning a callable that will call + the named function on any arguments passed to it. For example:: + + >>> # Equivalent to lambda s: s.replace('old', 'new') + >>> replacer = operator.methodcaller('replace', 'old', 'new') + >>> replacer('old wine in old bottles') + 'new wine in new bottles' + + (Contributed by Georg Brandl, after a suggestion by Gregory Petrosyan.) + + The :func:`attrgetter` function now accepts dotted names and performs + the corresponding attribute lookups:: + + >>> inst_name = operator.attrgetter('__class__.__name__') + >>> inst_name('') + 'str' + >>> inst_name(help) + '_Helper' + + (Contributed by Georg Brandl, after a suggestion by Barry Warsaw.) + * New functions in the :mod:`os` module include ``fchmod(fd, mode)``, ``fchown(fd, uid, gid)``, and ``lchmod(path, mode)``, on operating systems that support these @@ -1036,6 +1159,11 @@ complete list of changes, or look through the CVS logs for all the details. .. Patch #1393667 +* The :mod:`pickletools` module now has an :func:`optimize` function + that takes a string containing a pickle and removes some unused + opcodes, returning a shorter pickle that contains the same data structure. + (Contributed by Raymond Hettinger.) + * New functions in the :mod:`posix` module: :func:`chflags` and :func:`lchflags` are wrappers for the corresponding system calls (where they're available). Constants for the flag values are defined in the :mod:`stat` module; some @@ -1099,6 +1227,10 @@ complete list of changes, or look through the CVS logs for all the details. .. % Patch 1583 + The :func:`siginterrupt` function is now available from Python code, + and allows changing whether signals can interrupt system calls or not. + (Contributed by Ralf Schmitt.) + * The :mod:`smtplib` module now supports SMTP over SSL thanks to the addition of the :class:`SMTP_SSL` class. This class supports an interface identical to the existing :class:`SMTP` class. Both @@ -1201,6 +1333,18 @@ complete list of changes, or look through the CVS logs for all the details. .. Patch #1537850 + A new class, :class:`SpooledTemporaryFile`, behaves like + a temporary file but stores its data in memory until a maximum size is + exceeded. On reaching that limit, the contents will be written to + an on-disk temporary file. (Contributed by Dustin J. Mitchell.) + + The :class:`NamedTemporaryFile` and :class:`SpooledTemporaryFile` classes + both work as context managers, so you can write + ``with tempfile.NamedTemporaryFile() as tmp: ...``. + (Contributed by Alexander Belopolsky.) + + .. Issue #2021 + * The :mod:`test.test_support` module now contains a :func:`EnvironmentVarGuard` context manager that supports temporarily changing environment variables and @@ -1236,6 +1380,8 @@ complete list of changes, or look through the CVS logs for all the details. whitespace. >>> + (Contributed by Dwayne Bailey.) + .. Patch #1581073 * The :mod:`timeit` module now accepts callables as well as strings @@ -1415,6 +1561,12 @@ Changes to Python's build process and to the C API include: .. Patch 1530959 +* Several basic data types, such as integers and strings, maintain + internal free lists of objects that can be re-used. The data + structures for these free lists now follow a naming convention: the + variable is always named ``free_list``, the counter is always named + ``numfree``, and a macro :cmacro:`Py<typename>_MAXFREELIST` is + always defined. .. ====================================================================== |