Commit 25bb783c authored by Christian Heimes's avatar Christian Heimes

Merged revisions 59883-59920 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r59887 | neal.norwitz | 2008-01-10 06:42:58 +0100 (Thu, 10 Jan 2008) | 1 line

  Reword entry, not sure I made it much better though.
........
  r59888 | andrew.kuchling | 2008-01-10 14:37:12 +0100 (Thu, 10 Jan 2008) | 1 line

  Check for fd of -1 to save fsync() and fstat() call
........
  r59891 | thomas.heller | 2008-01-10 19:45:40 +0100 (Thu, 10 Jan 2008) | 1 line

  Reflow a paragraph, and fix a typo.
........
  r59892 | raymond.hettinger | 2008-01-10 20:15:10 +0100 (Thu, 10 Jan 2008) | 1 line

  Examples for named tuple subclassing should include __slots__
........
  r59895 | raymond.hettinger | 2008-01-10 21:37:12 +0100 (Thu, 10 Jan 2008) | 1 line

  Clarify how to add a field to a named tuple.
........
  r59896 | amaury.forgeotdarc | 2008-01-10 22:59:42 +0100 (Thu, 10 Jan 2008) | 12 lines

  Closing issue1761.
  Surprising behaviour of the "$" regexp: it matches the
  end of the string, AND just before the newline at the end
  of the string::

      re.sub('$', '#', 'foo\n') == 'foo#\n#'

  Python is consistent with Perl and the pcre library, so
  we just document it.
  Guido prefers "\Z" to match only the end of the string.
........
  r59898 | raymond.hettinger | 2008-01-11 00:00:01 +0100 (Fri, 11 Jan 2008) | 1 line

  Neaten-up the named tuple docs
........
  r59900 | raymond.hettinger | 2008-01-11 01:23:13 +0100 (Fri, 11 Jan 2008) | 1 line

  Run doctests on the collections module
........
  r59903 | raymond.hettinger | 2008-01-11 02:25:54 +0100 (Fri, 11 Jan 2008) | 1 line

  Doctest results return a named tuple for readability
........
  r59904 | raymond.hettinger | 2008-01-11 03:12:33 +0100 (Fri, 11 Jan 2008) | 1 line

  Comment-out missing constant (from rev 59819)
........
  r59905 | raymond.hettinger | 2008-01-11 03:24:13 +0100 (Fri, 11 Jan 2008) | 1 line

  Have Decimal.as_tuple return a named tuple.
........
  r59906 | raymond.hettinger | 2008-01-11 04:04:50 +0100 (Fri, 11 Jan 2008) | 1 line

  Let most inspect functions return named tuples
........
  r59907 | raymond.hettinger | 2008-01-11 04:20:54 +0100 (Fri, 11 Jan 2008) | 1 line

  Improve usability of the SequenceMatcher by returning named tuples describing match ranges.
........
  r59909 | thomas.heller | 2008-01-11 09:04:03 +0100 (Fri, 11 Jan 2008) | 1 line

  Add an important missing blank.
........
  r59910 | georg.brandl | 2008-01-11 10:19:11 +0100 (Fri, 11 Jan 2008) | 2 lines

  Guard definition of TIPC_SUB_CANCEL with an #ifdef.
........
  r59911 | georg.brandl | 2008-01-11 10:20:58 +0100 (Fri, 11 Jan 2008) | 2 lines

  News entries for rev. 5990[567].
........
  r59912 | georg.brandl | 2008-01-11 10:55:53 +0100 (Fri, 11 Jan 2008) | 2 lines

  Documentation for r5990[3567].
........
  r59913 | thomas.heller | 2008-01-11 13:41:39 +0100 (Fri, 11 Jan 2008) | 4 lines

  The sqlite3 dll, when compiled in debug mode, must be linked with /MDd
  to use the debug runtime library.  Further, the dll will be named
  sqlite3_d.dll.
........
  r59919 | thomas.heller | 2008-01-11 16:38:46 +0100 (Fri, 11 Jan 2008) | 6 lines

  Revert revision 59913, because it was wrong:

    The sqlite3 dll, when compiled in debug mode, must be linked with
    /MDd to use the debug runtime library.  Further, the dll will be
    named sqlite3_d.dll.
........
  r59920 | christian.heimes | 2008-01-11 16:42:29 +0100 (Fri, 11 Jan 2008) | 1 line

  Removed unused variable
........
parent 222e1279
...@@ -328,6 +328,13 @@ Glossary ...@@ -328,6 +328,13 @@ Glossary
Mutable objects can change their value but keep their :func:`id`. See Mutable objects can change their value but keep their :func:`id`. See
also :term:`immutable`. also :term:`immutable`.
named tuple
A tuple subclass whose elements also are accessible as attributes via
fixed names (the class name and field names are indicated in the
individual documentation of a named tuple type, like ``TestResults(failed,
attempted)``). Named tuple classes are created by
:func:`collections.namedtuple`.
namespace namespace
The place where a variable is stored. Namespaces are implemented as The place where a variable is stored. Namespaces are implemented as
dictionaries. There are the local, global and builtin namespaces as well dictionaries. There are the local, global and builtin namespaces as well
......
...@@ -397,8 +397,8 @@ they add the ability to access fields by name instead of position index. ...@@ -397,8 +397,8 @@ they add the ability to access fields by name instead of position index.
method which lists the tuple contents in a ``name=value`` format. method which lists the tuple contents in a ``name=value`` format.
The *fieldnames* are a single string with each fieldname separated by whitespace The *fieldnames* are a single string with each fieldname separated by whitespace
and/or commas (for example 'x y' or 'x, y'). Alternatively, *fieldnames* and/or commas, for example ``'x y'`` or ``'x, y'``. Alternatively, *fieldnames*
can be a sequence of strings (such as ['x', 'y']). can be a sequence of strings such as ``['x', 'y']``.
Any valid Python identifier may be used for a fieldname except for names Any valid Python identifier may be used for a fieldname except for names
starting with an underscore. Valid identifiers consist of letters, digits, starting with an underscore. Valid identifiers consist of letters, digits,
...@@ -406,7 +406,7 @@ they add the ability to access fields by name instead of position index. ...@@ -406,7 +406,7 @@ they add the ability to access fields by name instead of position index.
a :mod:`keyword` such as *class*, *for*, *return*, *global*, *pass*, *print*, a :mod:`keyword` such as *class*, *for*, *return*, *global*, *pass*, *print*,
or *raise*. or *raise*.
If *verbose* is true, will print the class definition. If *verbose* is true, the class definition is printed just before being built.
Named tuple instances do not have per-instance dictionaries, so they are Named tuple instances do not have per-instance dictionaries, so they are
lightweight and require no more memory than regular tuples. lightweight and require no more memory than regular tuples.
...@@ -533,7 +533,7 @@ function:: ...@@ -533,7 +533,7 @@ function::
>>> getattr(p, 'x') >>> getattr(p, 'x')
11 11
To cast a dictionary to a named tuple, use the double-star-operator [#]_:: To convert a dictionary to a named tuple, use the double-star-operator [#]_::
>>> d = {'x': 11, 'y': 22} >>> d = {'x': 11, 'y': 22}
>>> Point(**d) >>> Point(**d)
...@@ -544,23 +544,24 @@ functionality with a subclass. Here is how to add a calculated field and ...@@ -544,23 +544,24 @@ functionality with a subclass. Here is how to add a calculated field and
a fixed-width print format:: a fixed-width print format::
>>> class Point(namedtuple('Point', 'x y')): >>> class Point(namedtuple('Point', 'x y')):
... __slots__ = ()
... @property ... @property
... def hypot(self): ... def hypot(self):
... return (self.x ** 2 + self.y ** 2) ** 0.5 ... return (self.x ** 2 + self.y ** 2) ** 0.5
... def __str__(self): ... def __str__(self):
... return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot) ... return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
>>> for p in Point(3,4), Point(14,5), Point(9./7,6): >>> for p in Point(3, 4), Point(14, 5/7.):
... print(p) ... print(p)
Point: x= 3.000 y= 4.000 hypot= 5.000 Point: x= 3.000 y= 4.000 hypot= 5.000
Point: x=14.000 y= 5.000 hypot=14.866 Point: x=14.000 y= 0.714 hypot=14.018
Point: x= 1.286 y= 6.000 hypot= 6.136
Another use for subclassing is to replace performance critcal methods with Another use for subclassing is to replace performance critcal methods with
faster versions that bypass error-checking and that localize variable access:: faster versions that bypass error-checking::
class Point(namedtuple('Point', 'x y')): class Point(namedtuple('Point', 'x y')):
__slots__ = ()
_make = classmethod(tuple.__new__) _make = classmethod(tuple.__new__)
def _replace(self, _map=map, **kwds): def _replace(self, _map=map, **kwds):
return self._make(_map(kwds.get, ('x', 'y'), self)) return self._make(_map(kwds.get, ('x', 'y'), self))
...@@ -569,7 +570,7 @@ faster versions that bypass error-checking and that localize variable access:: ...@@ -569,7 +570,7 @@ faster versions that bypass error-checking and that localize variable access::
Subclassing is not useful for adding new, stored fields. Instead, simply Subclassing is not useful for adding new, stored fields. Instead, simply
create a new named tuple type from the :attr:`_fields` attribute:: create a new named tuple type from the :attr:`_fields` attribute::
>>> Pixel = namedtuple('Pixel', Point._fields + Color._fields) >>> Point3D = namedtuple('Point3D', Point._fields + ('z',))
Default values can be implemented by using :meth:`_replace` to Default values can be implemented by using :meth:`_replace` to
customize a prototype instance:: customize a prototype instance::
......
...@@ -328,7 +328,11 @@ also have a number of specialized methods: ...@@ -328,7 +328,11 @@ also have a number of specialized methods:
.. method:: Decimal.as_tuple() .. method:: Decimal.as_tuple()
Return a tuple representation of the number: ``(sign, digit_tuple, exponent)``. Return a :term:`named tuple` representation of the number:
``DecimalTuple(sign, digits, exponent)``.
.. versionchanged:: 2.6
Use a named tuple.
.. method:: Decimal.canonical() .. method:: Decimal.canonical()
......
...@@ -336,7 +336,7 @@ use :meth:`set_seq2` to set the commonly used sequence once and call ...@@ -336,7 +336,7 @@ use :meth:`set_seq2` to set the commonly used sequence once and call
Find longest matching block in ``a[alo:ahi]`` and ``b[blo:bhi]``. Find longest matching block in ``a[alo:ahi]`` and ``b[blo:bhi]``.
If *isjunk* was omitted or ``None``, :meth:`get_longest_match` returns ``(i, j, If *isjunk* was omitted or ``None``, :meth:`find_longest_match` returns ``(i, j,
k)`` such that ``a[i:i+k]`` is equal to ``b[j:j+k]``, where ``alo <= i <= i+k <= k)`` such that ``a[i:i+k]`` is equal to ``b[j:j+k]``, where ``alo <= i <= i+k <=
ahi`` and ``blo <= j <= j+k <= bhi``. For all ``(i', j', k')`` meeting those ahi`` and ``blo <= j <= j+k <= bhi``. For all ``(i', j', k')`` meeting those
conditions, the additional conditions ``k >= k'``, ``i <= i'``, and if ``i == conditions, the additional conditions ``k >= k'``, ``i <= i'``, and if ``i ==
...@@ -365,6 +365,9 @@ use :meth:`set_seq2` to set the commonly used sequence once and call ...@@ -365,6 +365,9 @@ use :meth:`set_seq2` to set the commonly used sequence once and call
If no blocks match, this returns ``(alo, blo, 0)``. If no blocks match, this returns ``(alo, blo, 0)``.
.. versionchanged:: 2.6
This method returns a :term:`named tuple` ``Match(a, b, size)``.
.. method:: SequenceMatcher.get_matching_blocks() .. method:: SequenceMatcher.get_matching_blocks()
......
...@@ -1436,11 +1436,14 @@ DocTestRunner objects ...@@ -1436,11 +1436,14 @@ DocTestRunner objects
.. method:: DocTestRunner.summarize([verbose]) .. method:: DocTestRunner.summarize([verbose])
Print a summary of all the test cases that have been run by this DocTestRunner, Print a summary of all the test cases that have been run by this DocTestRunner,
and return a tuple ``(failure_count, test_count)``. and return a :term:`named tuple` ``TestResults(failed, attempted)``.
The optional *verbose* argument controls how detailed the summary is. If the The optional *verbose* argument controls how detailed the summary is. If the
verbosity is not specified, then the :class:`DocTestRunner`'s verbosity is used. verbosity is not specified, then the :class:`DocTestRunner`'s verbosity is used.
.. versionchanged:: 2.6
Use a named tuple.
.. _doctest-outputchecker: .. _doctest-outputchecker:
......
...@@ -188,7 +188,8 @@ attributes: ...@@ -188,7 +188,8 @@ attributes:
.. function:: getmoduleinfo(path) .. function:: getmoduleinfo(path)
Return a tuple of values that describe how Python will interpret the file Returns a :term:`named tuple` ``ModuleInfo(name, suffix, mode,
module_type)`` of values that describe how Python will interpret the file
identified by *path* if it is a module, or ``None`` if it would not be identified by *path* if it is a module, or ``None`` if it would not be
identified as a module. The return tuple is ``(name, suffix, mode, mtype)``, identified as a module. The return tuple is ``(name, suffix, mode, mtype)``,
where *name* is the name of the module without the name of any enclosing where *name* is the name of the module without the name of any enclosing
...@@ -377,8 +378,9 @@ Classes and functions ...@@ -377,8 +378,9 @@ Classes and functions
.. function:: getargspec(func) .. function:: getargspec(func)
Get the names and default values of a function's arguments. A tuple of four Get the names and default values of a function's arguments. A
things is returned: ``(args, varargs, varkw, defaults)``. *args* is a list of :term:`named tuple` ``ArgSpec(args, varargs, keywords,
defaults)`` is returned. *args* is a list of
the argument names. *varargs* and *varkw* are the names of the ``*`` and the argument names. *varargs* and *varkw* are the names of the ``*`` and
``**`` arguments or ``None``. *defaults* is a tuple of default argument ``**`` arguments or ``None``. *defaults* is a tuple of default argument
values or None if there are no default arguments; if this tuple has *n* values or None if there are no default arguments; if this tuple has *n*
...@@ -391,10 +393,10 @@ Classes and functions ...@@ -391,10 +393,10 @@ Classes and functions
.. function:: getfullargspec(func) .. function:: getfullargspec(func)
Get the names and default values of a function's arguments. A tuple of seven Get the names and default values of a function's arguments. A :term:`named tuple`
things is returned: is returned:
``(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations)`` ``FullArgSpec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations)``
*args* is a list of the argument names. *varargs* and *varkw* are the names *args* is a list of the argument names. *varargs* and *varkw* are the names
of the ``*`` and ``**`` arguments or ``None``. *defaults* is an n-tuple of of the ``*`` and ``**`` arguments or ``None``. *defaults* is an n-tuple of
...@@ -408,8 +410,8 @@ Classes and functions ...@@ -408,8 +410,8 @@ Classes and functions
.. function:: getargvalues(frame) .. function:: getargvalues(frame)
Get information about arguments passed into a particular frame. A tuple of four Get information about arguments passed into a particular frame. A :term:`named tuple`
things is returned: ``(args, varargs, varkw, locals)``. *args* is a list of the ``ArgInfo(args, varargs, keywords, locals)`` is returned. *args* is a list of the
argument names (it may contain nested lists). *varargs* and *varkw* are the argument names (it may contain nested lists). *varargs* and *varkw* are the
names of the ``*`` and ``**`` arguments or ``None``. *locals* is the locals names of the ``*`` and ``**`` arguments or ``None``. *locals* is the locals
dictionary of the given frame. dictionary of the given frame.
...@@ -476,8 +478,8 @@ line. ...@@ -476,8 +478,8 @@ line.
.. function:: getframeinfo(frame[, context]) .. function:: getframeinfo(frame[, context])
Get information about a frame or traceback object. A 5-tuple is returned, the Get information about a frame or traceback object. A :term:`named tuple`
last five elements of the frame's frame record. ``Traceback(filename, lineno, function, code_context, index)`` is returned.
.. function:: getouterframes(frame[, context]) .. function:: getouterframes(frame[, context])
......
...@@ -98,7 +98,9 @@ The special characters are: ...@@ -98,7 +98,9 @@ The special characters are:
string, and in :const:`MULTILINE` mode also matches before a newline. ``foo`` string, and in :const:`MULTILINE` mode also matches before a newline. ``foo``
matches both 'foo' and 'foobar', while the regular expression ``foo$`` matches matches both 'foo' and 'foobar', while the regular expression ``foo$`` matches
only 'foo'. More interestingly, searching for ``foo.$`` in ``'foo1\nfoo2\n'`` only 'foo'. More interestingly, searching for ``foo.$`` in ``'foo1\nfoo2\n'``
matches 'foo2' normally, but 'foo1' in :const:`MULTILINE` mode. matches 'foo2' normally, but 'foo1' in :const:`MULTILINE` mode; searching for
a single ``$`` in ``'foo\n'`` will find two (empty) matches: one just before
the newline, and one at the end of the string.
``'*'`` ``'*'``
Causes the resulting RE to match 0 or more repetitions of the preceding RE, as Causes the resulting RE to match 0 or more repetitions of the preceding RE, as
......
...@@ -117,23 +117,28 @@ if __name__ == '__main__': ...@@ -117,23 +117,28 @@ if __name__ == '__main__':
# test and demonstrate ability to override methods # test and demonstrate ability to override methods
class Point(namedtuple('Point', 'x y')): class Point(namedtuple('Point', 'x y')):
__slots__ = ()
@property @property
def hypot(self): def hypot(self):
return (self.x ** 2 + self.y ** 2) ** 0.5 return (self.x ** 2 + self.y ** 2) ** 0.5
def __str__(self): def __str__(self):
return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot) return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot)
for p in Point(3,4), Point(14,5), Point(9./7,6): for p in Point(3, 4), Point(14, 5/7.):
print (p) print (p)
class Point(namedtuple('Point', 'x y')): class Point(namedtuple('Point', 'x y')):
'Point class with optimized _make() and _replace() without error-checking' 'Point class with optimized _make() and _replace() without error-checking'
__slots__ = ()
_make = classmethod(tuple.__new__) _make = classmethod(tuple.__new__)
def _replace(self, _map=map, **kwds): def _replace(self, _map=map, **kwds):
return self._make(_map(kwds.get, ('x', 'y'), self)) return self._make(_map(kwds.get, ('x', 'y'), self))
print(Point(11, 22)._replace(x=100)) print(Point(11, 22)._replace(x=100))
Point3D = namedtuple('Point3D', Point._fields + ('z',))
print(Point3D.__doc__)
import doctest import doctest
TestResults = namedtuple('TestResults', 'failed attempted') TestResults = namedtuple('TestResults', 'failed attempted')
print(TestResults(*doctest.testmod())) print(TestResults(*doctest.testmod()))
...@@ -137,6 +137,12 @@ __all__ = [ ...@@ -137,6 +137,12 @@ __all__ = [
import numbers as _numbers import numbers as _numbers
import copy as _copy import copy as _copy
try:
from collections import namedtuple as _namedtuple
DecimalTuple = _namedtuple('DecimalTuple', 'sign digits exponent')
except ImportError:
DecimalTuple = lambda *args: args
# Rounding # Rounding
ROUND_DOWN = 'ROUND_DOWN' ROUND_DOWN = 'ROUND_DOWN'
ROUND_HALF_UP = 'ROUND_HALF_UP' ROUND_HALF_UP = 'ROUND_HALF_UP'
...@@ -841,7 +847,7 @@ class Decimal(_numbers.Real, _numbers.Inexact): ...@@ -841,7 +847,7 @@ class Decimal(_numbers.Real, _numbers.Inexact):
To show the internals exactly as they are. To show the internals exactly as they are.
""" """
return (self._sign, tuple(map(int, self._int)), self._exp) return DecimalTuple(self._sign, tuple(map(int, self._int)), self._exp)
def __repr__(self): def __repr__(self):
"""Represents the number as an instance of Decimal.""" """Represents the number as an instance of Decimal."""
......
...@@ -30,9 +30,12 @@ Class HtmlDiff: ...@@ -30,9 +30,12 @@ Class HtmlDiff:
__all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher', __all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher',
'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff', 'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff',
'unified_diff', 'HtmlDiff'] 'unified_diff', 'HtmlDiff', 'Match']
import heapq import heapq
from collections import namedtuple as _namedtuple
Match = _namedtuple('Match', 'a b size')
def _calculate_ratio(matches, length): def _calculate_ratio(matches, length):
if length: if length:
...@@ -363,7 +366,7 @@ class SequenceMatcher: ...@@ -363,7 +366,7 @@ class SequenceMatcher:
>>> s = SequenceMatcher(None, " abcd", "abcd abcd") >>> s = SequenceMatcher(None, " abcd", "abcd abcd")
>>> s.find_longest_match(0, 5, 0, 9) >>> s.find_longest_match(0, 5, 0, 9)
(0, 4, 5) Match(a=0, b=4, size=5)
If isjunk is defined, first the longest matching block is If isjunk is defined, first the longest matching block is
determined as above, but with the additional restriction that no determined as above, but with the additional restriction that no
...@@ -379,13 +382,13 @@ class SequenceMatcher: ...@@ -379,13 +382,13 @@ class SequenceMatcher:
>>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd") >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
>>> s.find_longest_match(0, 5, 0, 9) >>> s.find_longest_match(0, 5, 0, 9)
(1, 0, 4) Match(a=1, b=0, size=4)
If no blocks match, return (alo, blo, 0). If no blocks match, return (alo, blo, 0).
>>> s = SequenceMatcher(None, "ab", "c") >>> s = SequenceMatcher(None, "ab", "c")
>>> s.find_longest_match(0, 2, 0, 1) >>> s.find_longest_match(0, 2, 0, 1)
(0, 0, 0) Match(a=0, b=0, size=0)
""" """
# CAUTION: stripping common prefix or suffix would be incorrect. # CAUTION: stripping common prefix or suffix would be incorrect.
...@@ -452,7 +455,7 @@ class SequenceMatcher: ...@@ -452,7 +455,7 @@ class SequenceMatcher:
a[besti+bestsize] == b[bestj+bestsize]: a[besti+bestsize] == b[bestj+bestsize]:
bestsize = bestsize + 1 bestsize = bestsize + 1
return besti, bestj, bestsize return Match(besti, bestj, bestsize)
def get_matching_blocks(self): def get_matching_blocks(self):
"""Return list of triples describing matching subsequences. """Return list of triples describing matching subsequences.
...@@ -469,8 +472,8 @@ class SequenceMatcher: ...@@ -469,8 +472,8 @@ class SequenceMatcher:
triple with n==0. triple with n==0.
>>> s = SequenceMatcher(None, "abxcd", "abcd") >>> s = SequenceMatcher(None, "abxcd", "abcd")
>>> s.get_matching_blocks() >>> list(s.get_matching_blocks())
[(0, 0, 2), (3, 2, 2), (5, 4, 0)] [Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)]
""" """
if self.matching_blocks is not None: if self.matching_blocks is not None:
...@@ -523,7 +526,7 @@ class SequenceMatcher: ...@@ -523,7 +526,7 @@ class SequenceMatcher:
non_adjacent.append( (la, lb, 0) ) non_adjacent.append( (la, lb, 0) )
self.matching_blocks = non_adjacent self.matching_blocks = non_adjacent
return self.matching_blocks return map(Match._make, self.matching_blocks)
def get_opcodes(self): def get_opcodes(self):
"""Return list of 5-tuples describing how to turn a into b. """Return list of 5-tuples describing how to turn a into b.
......
...@@ -99,6 +99,9 @@ import sys, traceback, inspect, linecache, os, re ...@@ -99,6 +99,9 @@ import sys, traceback, inspect, linecache, os, re
import unittest, difflib, pdb, tempfile import unittest, difflib, pdb, tempfile
import warnings import warnings
from io import StringIO from io import StringIO
from collections import namedtuple
TestResults = namedtuple('TestResults', 'failed attempted')
# There are 4 basic classes: # There are 4 basic classes:
# - Example: a <source, want> pair, plus an intra-docstring line number. # - Example: a <source, want> pair, plus an intra-docstring line number.
...@@ -1024,10 +1027,10 @@ class DocTestRunner: ...@@ -1024,10 +1027,10 @@ class DocTestRunner:
>>> tests.sort(key = lambda test: test.name) >>> tests.sort(key = lambda test: test.name)
>>> for test in tests: >>> for test in tests:
... print(test.name, '->', runner.run(test)) ... print(test.name, '->', runner.run(test))
_TestClass -> (0, 2) _TestClass -> TestResults(failed=0, attempted=2)
_TestClass.__init__ -> (0, 2) _TestClass.__init__ -> TestResults(failed=0, attempted=2)
_TestClass.get -> (0, 2) _TestClass.get -> TestResults(failed=0, attempted=2)
_TestClass.square -> (0, 1) _TestClass.square -> TestResults(failed=0, attempted=1)
The `summarize` method prints a summary of all the test cases that The `summarize` method prints a summary of all the test cases that
have been run by the runner, and returns an aggregated `(f, t)` have been run by the runner, and returns an aggregated `(f, t)`
...@@ -1042,7 +1045,7 @@ class DocTestRunner: ...@@ -1042,7 +1045,7 @@ class DocTestRunner:
7 tests in 4 items. 7 tests in 4 items.
7 passed and 0 failed. 7 passed and 0 failed.
Test passed. Test passed.
(0, 7) TestResults(failed=0, attempted=7)
The aggregated number of tried examples and failed examples is The aggregated number of tried examples and failed examples is
also available via the `tries` and `failures` attributes: also available via the `tries` and `failures` attributes:
...@@ -1285,7 +1288,7 @@ class DocTestRunner: ...@@ -1285,7 +1288,7 @@ class DocTestRunner:
# Record and return the number of failures and tries. # Record and return the number of failures and tries.
self.__record_outcome(test, failures, tries) self.__record_outcome(test, failures, tries)
return failures, tries return TestResults(failures, tries)
def __record_outcome(self, test, f, t): def __record_outcome(self, test, f, t):
""" """
...@@ -1417,7 +1420,7 @@ class DocTestRunner: ...@@ -1417,7 +1420,7 @@ class DocTestRunner:
print("***Test Failed***", totalf, "failures.") print("***Test Failed***", totalf, "failures.")
elif verbose: elif verbose:
print("Test passed.") print("Test passed.")
return totalf, totalt return TestResults(totalf, totalt)
#///////////////////////////////////////////////////////////////// #/////////////////////////////////////////////////////////////////
# Backward compatibility cruft to maintain doctest.master. # Backward compatibility cruft to maintain doctest.master.
...@@ -1688,7 +1691,7 @@ class DebugRunner(DocTestRunner): ...@@ -1688,7 +1691,7 @@ class DebugRunner(DocTestRunner):
... ''', {}, 'foo', 'foo.py', 0) ... ''', {}, 'foo', 'foo.py', 0)
>>> runner.run(test) >>> runner.run(test)
(0, 1) TestResults(failed=0, attempted=1)
>>> test.globs >>> test.globs
{} {}
...@@ -1818,7 +1821,7 @@ def testmod(m=None, name=None, globs=None, verbose=None, ...@@ -1818,7 +1821,7 @@ def testmod(m=None, name=None, globs=None, verbose=None,
else: else:
master.merge(runner) master.merge(runner)
return runner.failures, runner.tries return TestResults(runner.failures, runner.tries)
def testfile(filename, module_relative=True, name=None, package=None, def testfile(filename, module_relative=True, name=None, package=None,
globs=None, verbose=None, report=True, optionflags=0, globs=None, verbose=None, report=True, optionflags=0,
...@@ -1939,7 +1942,7 @@ def testfile(filename, module_relative=True, name=None, package=None, ...@@ -1939,7 +1942,7 @@ def testfile(filename, module_relative=True, name=None, package=None,
else: else:
master.merge(runner) master.merge(runner)
return runner.failures, runner.tries return TestResults(runner.failures, runner.tries)
def run_docstring_examples(f, globs, verbose=False, name="NoName", def run_docstring_examples(f, globs, verbose=False, name="NoName",
compileflags=None, optionflags=0): compileflags=None, optionflags=0):
...@@ -1998,7 +2001,7 @@ class Tester: ...@@ -1998,7 +2001,7 @@ class Tester:
(f,t) = self.testrunner.run(test) (f,t) = self.testrunner.run(test)
if self.verbose: if self.verbose:
print(f, "of", t, "examples failed in string", name) print(f, "of", t, "examples failed in string", name)
return (f,t) return TestResults(f,t)
def rundoc(self, object, name=None, module=None): def rundoc(self, object, name=None, module=None):
f = t = 0 f = t = 0
...@@ -2007,7 +2010,7 @@ class Tester: ...@@ -2007,7 +2010,7 @@ class Tester:
for test in tests: for test in tests:
(f2, t2) = self.testrunner.run(test) (f2, t2) = self.testrunner.run(test)
(f,t) = (f+f2, t+t2) (f,t) = (f+f2, t+t2)
return (f,t) return TestResults(f,t)
def rundict(self, d, name, module=None): def rundict(self, d, name, module=None):
import types import types
......
...@@ -31,6 +31,7 @@ __date__ = '1 Jan 2001' ...@@ -31,6 +31,7 @@ __date__ = '1 Jan 2001'
import sys, os, types, re, dis, imp, tokenize, linecache import sys, os, types, re, dis, imp, tokenize, linecache
from operator import attrgetter from operator import attrgetter
from collections import namedtuple
# ----------------------------------------------------------- type-checking # ----------------------------------------------------------- type-checking
def ismodule(object): def ismodule(object):
...@@ -208,6 +209,8 @@ def getmembers(object, predicate=None): ...@@ -208,6 +209,8 @@ def getmembers(object, predicate=None):
results.sort() results.sort()
return results return results
Attribute = namedtuple('Attribute', 'name kind defining_class object')
def classify_class_attrs(cls): def classify_class_attrs(cls):
"""Return list of attribute-descriptor tuples. """Return list of attribute-descriptor tuples.
...@@ -274,7 +277,7 @@ def classify_class_attrs(cls): ...@@ -274,7 +277,7 @@ def classify_class_attrs(cls):
else: else:
kind = "data" kind = "data"
result.append((name, kind, homecls, obj)) result.append(Attribute(name, kind, homecls, obj))
return result return result
...@@ -362,6 +365,8 @@ def getfile(object): ...@@ -362,6 +365,8 @@ def getfile(object):
raise TypeError('arg is not a module, class, method, ' raise TypeError('arg is not a module, class, method, '
'function, traceback, frame, or code object') 'function, traceback, frame, or code object')
ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
def getmoduleinfo(path): def getmoduleinfo(path):
"""Get the module name, suffix, mode, and module type for a given file.""" """Get the module name, suffix, mode, and module type for a given file."""
filename = os.path.basename(path) filename = os.path.basename(path)
...@@ -370,7 +375,7 @@ def getmoduleinfo(path): ...@@ -370,7 +375,7 @@ def getmoduleinfo(path):
suffixes.sort() # try longest suffixes first, in case they overlap suffixes.sort() # try longest suffixes first, in case they overlap
for neglen, suffix, mode, mtype in suffixes: for neglen, suffix, mode, mtype in suffixes:
if filename[neglen:] == suffix: if filename[neglen:] == suffix:
return filename[:neglen], suffix, mode, mtype return ModuleInfo(filename[:neglen], suffix, mode, mtype)
def getmodulename(path): def getmodulename(path):
"""Return the module name for a given file, or None.""" """Return the module name for a given file, or None."""
...@@ -668,6 +673,8 @@ def getclasstree(classes, unique=0): ...@@ -668,6 +673,8 @@ def getclasstree(classes, unique=0):
# These constants are from Python's compile.h. # These constants are from Python's compile.h.
CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 1, 2, 4, 8 CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 1, 2, 4, 8
Arguments = namedtuple('Arguments', 'args, varargs, varkw')
def getargs(co): def getargs(co):
"""Get information about the arguments accepted by a code object. """Get information about the arguments accepted by a code object.
...@@ -676,7 +683,7 @@ def getargs(co): ...@@ -676,7 +683,7 @@ def getargs(co):
lists. Keyword-only arguments are appended. 'varargs' and 'varkw' lists. Keyword-only arguments are appended. 'varargs' and 'varkw'
are the names of the * and ** arguments or None.""" are the names of the * and ** arguments or None."""
args, varargs, kwonlyargs, varkw = _getfullargs(co) args, varargs, kwonlyargs, varkw = _getfullargs(co)
return args + kwonlyargs, varargs, varkw return Arguments(args + kwonlyargs, varargs, varkw)
def _getfullargs(co): def _getfullargs(co):
"""Get information about the arguments accepted by a code object. """Get information about the arguments accepted by a code object.
...@@ -706,6 +713,9 @@ def _getfullargs(co): ...@@ -706,6 +713,9 @@ def _getfullargs(co):
varkw = co.co_varnames[nargs] varkw = co.co_varnames[nargs]
return args, varargs, kwonlyargs, varkw return args, varargs, kwonlyargs, varkw
ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
def getargspec(func): def getargspec(func):
"""Get the names and default values of a function's arguments. """Get the names and default values of a function's arguments.
...@@ -725,7 +735,10 @@ def getargspec(func): ...@@ -725,7 +735,10 @@ def getargspec(func):
if kwonlyargs or ann: if kwonlyargs or ann:
raise ValueError("Function has keyword-only arguments or annotations" raise ValueError("Function has keyword-only arguments or annotations"
", use getfullargspec() API which can support them") ", use getfullargspec() API which can support them")
return (args, varargs, varkw, defaults) return ArgSpec(args, varargs, varkw, defaults)
FullArgSpec = namedtuple('FullArgSpec',
'args, varargs, varkw, defaults, kwonlyargs, kwdefaults, annotations')
def getfullargspec(func): def getfullargspec(func):
"""Get the names and default values of a function's arguments. """Get the names and default values of a function's arguments.
...@@ -747,9 +760,11 @@ def getfullargspec(func): ...@@ -747,9 +760,11 @@ def getfullargspec(func):
if not isfunction(func): if not isfunction(func):
raise TypeError('arg is not a Python function') raise TypeError('arg is not a Python function')
args, varargs, kwonlyargs, varkw = _getfullargs(func.__code__) args, varargs, kwonlyargs, varkw = _getfullargs(func.__code__)
return (args, varargs, varkw, func.__defaults__, return FullArgSpec(args, varargs, varkw, func.__defaults__,
kwonlyargs, func.__kwdefaults__, func.__annotations__) kwonlyargs, func.__kwdefaults__, func.__annotations__)
ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
def getargvalues(frame): def getargvalues(frame):
"""Get information about arguments passed into a particular frame. """Get information about arguments passed into a particular frame.
...@@ -859,6 +874,9 @@ def formatargvalues(args, varargs, varkw, locals, ...@@ -859,6 +874,9 @@ def formatargvalues(args, varargs, varkw, locals,
return '(' + ', '.join(specs) + ')' return '(' + ', '.join(specs) + ')'
# -------------------------------------------------- stack frame extraction # -------------------------------------------------- stack frame extraction
Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
def getframeinfo(frame, context=1): def getframeinfo(frame, context=1):
"""Get information about a frame or traceback object. """Get information about a frame or traceback object.
...@@ -890,7 +908,7 @@ def getframeinfo(frame, context=1): ...@@ -890,7 +908,7 @@ def getframeinfo(frame, context=1):
else: else:
lines = index = None lines = index = None
return (filename, lineno, frame.f_code.co_name, lines, index) return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
def getlineno(frame): def getlineno(frame):
"""Get the line number from a frame object, allowing for optimization.""" """Get the line number from a frame object, allowing for optimization."""
......
"""Unit tests for collections.py.""" """Unit tests for collections.py."""
import unittest import unittest, doctest
from test import test_support from test import test_support
from collections import namedtuple from collections import namedtuple
from collections import Hashable, Iterable, Iterator from collections import Hashable, Iterable, Iterator
...@@ -316,10 +316,12 @@ class TestCollectionABCs(unittest.TestCase): ...@@ -316,10 +316,12 @@ class TestCollectionABCs(unittest.TestCase):
self.failUnless(issubclass(sample, MutableSequence)) self.failUnless(issubclass(sample, MutableSequence))
self.failIf(issubclass(str, MutableSequence)) self.failIf(issubclass(str, MutableSequence))
import doctest, collections
NamedTupleDocs = doctest.DocTestSuite(module=collections)
def test_main(verbose=None): def test_main(verbose=None):
import collections as CollectionsModule import collections as CollectionsModule
test_classes = [TestNamedTuple, TestOneTrickPonyABCs, TestCollectionABCs] test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs, TestCollectionABCs]
test_support.run_unittest(*test_classes) test_support.run_unittest(*test_classes)
test_support.run_doctest(CollectionsModule, verbose) test_support.run_doctest(CollectionsModule, verbose)
......
This diff is collapsed.
...@@ -40,7 +40,7 @@ class PyclbrTest(TestCase): ...@@ -40,7 +40,7 @@ class PyclbrTest(TestCase):
if key in ignore: return if key in ignore: return
if key not in obj: if key not in obj:
print("***",key, file=sys.stderr) print("***",key, file=sys.stderr)
self.failUnless(key in obj) self.failUnless(key in obj, "%r in %r" % (key, obj))
def assertEqualsOrIgnored(self, a, b, ignore): def assertEqualsOrIgnored(self, a, b, ignore):
''' succeed iff a == b or a in ignore or b in ignore ''' ''' succeed iff a == b or a in ignore or b in ignore '''
...@@ -140,9 +140,9 @@ class PyclbrTest(TestCase): ...@@ -140,9 +140,9 @@ class PyclbrTest(TestCase):
def test_easy(self): def test_easy(self):
self.checkModule('pyclbr') self.checkModule('pyclbr')
self.checkModule('doctest') self.checkModule('doctest', ignore=("TestResults",))
self.checkModule('rfc822') self.checkModule('rfc822')
self.checkModule('difflib') self.checkModule('difflib', ignore=("Match",))
def test_decorators(self): def test_decorators(self):
# XXX: See comment in pyclbr_input.py for a test that would fail # XXX: See comment in pyclbr_input.py for a test that would fail
......
...@@ -661,6 +661,18 @@ class ReTests(unittest.TestCase): ...@@ -661,6 +661,18 @@ class ReTests(unittest.TestCase):
q = p.match(upper_char) q = p.match(upper_char)
self.assertNotEqual(q, None) self.assertNotEqual(q, None)
def test_dollar_matches_twice(self):
"$ matches the end of string, and just before the terminating \n"
pattern = re.compile('$')
self.assertEqual(pattern.sub('#', 'a\nb\n'), 'a\nb#\n#')
self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a\nb\nc#')
self.assertEqual(pattern.sub('#', '\n'), '#\n#')
pattern = re.compile('$', re.MULTILINE)
self.assertEqual(pattern.sub('#', 'a\nb\n' ), 'a#\nb#\n#' )
self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a#\nb#\nc#')
self.assertEqual(pattern.sub('#', '\n'), '#\n#')
def run_re_tests(): def run_re_tests():
from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR
......
...@@ -980,9 +980,11 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict) ...@@ -980,9 +980,11 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
#ifdef HAVE_FSTAT #ifdef HAVE_FSTAT
# ifdef __VMS # ifdef __VMS
/* on OpenVMS we must ensure that all bytes are written to the file */ /* on OpenVMS we must ensure that all bytes are written to the file */
if (fd != -1) {
fsync(fd); fsync(fd);
}
# endif # endif
if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
if (map_size == 0) { if (map_size == 0) {
map_size = st.st_size; map_size = st.st_size;
} else if ((size_t)offset + (size_t)map_size > st.st_size) { } else if ((size_t)offset + (size_t)map_size > st.st_size) {
......
...@@ -4097,7 +4097,7 @@ See the socket module for documentation."); ...@@ -4097,7 +4097,7 @@ See the socket module for documentation.");
PyMODINIT_FUNC PyMODINIT_FUNC
init_socket(void) init_socket(void)
{ {
PyObject *m, *has_ipv6, *tmp; PyObject *m, *has_ipv6;
if (!os_init()) if (!os_init())
return; return;
...@@ -4354,7 +4354,10 @@ init_socket(void) ...@@ -4354,7 +4354,10 @@ init_socket(void)
/* for subscriptions */ /* for subscriptions */
PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS); PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);
PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE); PyModule_AddIntConstant(m, "TIPC_SUB_SERVICE", TIPC_SUB_SERVICE);
#ifdef TIPC_SUB_CANCEL
/* doesn't seem to be available everywhere */
PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL); PyModule_AddIntConstant(m, "TIPC_SUB_CANCEL", TIPC_SUB_CANCEL);
#endif
PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER); PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED); PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);
PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN); PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);
......
...@@ -25,7 +25,7 @@ ROOT = os.path.abspath(os.path.join(here, par, par)) ...@@ -25,7 +25,7 @@ ROOT = os.path.abspath(os.path.join(here, par, par))
# Windows 2000 compatibility: WINVER 0x0500 # Windows 2000 compatibility: WINVER 0x0500
# http://msdn2.microsoft.com/en-us/library/aa383745.aspx # http://msdn2.microsoft.com/en-us/library/aa383745.aspx
NMAKE = ('nmake /nologo /f %s ' NMAKE = ('nmake /nologo /f %s '
'COMPILERFLAGS=\"-DWINVER=0x0500 -D_WIN32_WINNT=0x0500 -DNTDDI_VERSION=NTDDI_WIN2KSP4\"' 'COMPILERFLAGS=\"-DWINVER=0x0500 -D_WIN32_WINNT=0x0500 -DNTDDI_VERSION=NTDDI_WIN2KSP4\" '
'%s %s') '%s %s')
def nmake(makefile, command="", **kw): def nmake(makefile, command="", **kw):
......
...@@ -117,7 +117,7 @@ _tkinter ...@@ -117,7 +117,7 @@ _tkinter
Build with build_tkinter.py Build with build_tkinter.py
--------------------------- ---------------------------
The PCbuild directory contains a Python script which automates all The PCbuild directory contains a Python script which automates all
steps. Run the script in a Visual Studio 2009 command prompt with steps. Run the script in a Visual Studio 2008 command prompt with
python build_tkinter.py Win32 python build_tkinter.py Win32
...@@ -312,9 +312,11 @@ Edition. ...@@ -312,9 +312,11 @@ Edition.
Profile Guided Optimization Profile Guided Optimization
--------------------------- ---------------------------
The solution has two configurations for PGO. The PGInstrument configuration The solution has two configurations for PGO. The PGInstrument
must be build first. The PGInstrument binaries are lniked against a profiling configuration must be build first. The PGInstrument binaries are
library and contain extra debug information. The PGUpdate configuration takes the profiling data and generates optimized binaries. lniked against a profiling library and contain extra debug
information. The PGUpdate configuration takes the profiling data and
generates optimized binaries.
The build_pgo.bat script automates the creation of optimized binaries. It The build_pgo.bat script automates the creation of optimized binaries. It
creates the PGI files, runs the unit test suite or PyBench with the PGI creates the PGI files, runs the unit test suite or PyBench with the PGI
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment