mutable
Mutable objects can change their value but keep their :func:`id`. See
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
The place where a variable is stored. Namespaces are implemented as
method which lists the tuple contents in a ``name=value`` format.
The *fieldnames* are a single string with each fieldname separated by whitespace
- and/or commas (for example 'x y' or 'x, y'). Alternatively, *fieldnames*
- can be a sequence of strings (such as ['x', 'y']).
+ and/or commas, for example ``'x y'`` or ``'x, y'``. Alternatively, *fieldnames*
+ can be a sequence of strings such as ``['x', 'y']``.
Any valid Python identifier may be used for a fieldname except for names
starting with an underscore. Valid identifiers consist of letters, digits,
a :mod:`keyword` such as *class*, *for*, *return*, *global*, *pass*, *print*,
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
lightweight and require no more memory than regular tuples.
>>> getattr(p, 'x')
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}
>>> Point(**d)
a fixed-width print format::
>>> class Point(namedtuple('Point', 'x y')):
+ ... __slots__ = ()
... @property
... def hypot(self):
... return (self.x ** 2 + self.y ** 2) ** 0.5
... 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)
- Point: x= 3.000 y= 4.000 hypot= 5.000
- Point: x=14.000 y= 5.000 hypot=14.866
- Point: x= 1.286 y= 6.000 hypot= 6.136
+ Point: x= 3.000 y= 4.000 hypot= 5.000
+ Point: x=14.000 y= 0.714 hypot=14.018
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')):
+ __slots__ = ()
_make = classmethod(tuple.__new__)
def _replace(self, _map=map, **kwds):
return self._make(_map(kwds.get, ('x', 'y'), self))
Subclassing is not useful for adding new, stored fields. Instead, simply
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
customize a prototype instance::
.. 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()
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 <=
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 ==
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:: DocTestRunner.summarize([verbose])
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
verbosity is not specified, then the :class:`DocTestRunner`'s verbosity is used.
+ .. versionchanged:: 2.6
+ Use a named tuple.
+
.. _doctest-outputchecker:
.. 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 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
.. function:: getargspec(func)
- Get the names and default values of a function's arguments. A tuple of four
- things is returned: ``(args, varargs, varkw, defaults)``. *args* is a list of
+ Get the names and default values of a function's arguments. A
+ :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
``**`` arguments or ``None``. *defaults* is a tuple of default argument
values or None if there are no default arguments; if this tuple has *n*
.. function:: getfullargspec(func)
- Get the names and default values of a function's arguments. A tuple of seven
- things is returned:
+ Get the names and default values of a function's arguments. A :term:`named tuple`
+ 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
of the ``*`` and ``**`` arguments or ``None``. *defaults* is an n-tuple of
.. function:: getargvalues(frame)
- Get information about arguments passed into a particular frame. A tuple of four
- things is returned: ``(args, varargs, varkw, locals)``. *args* is a list of the
+ Get information about arguments passed into a particular frame. A :term:`named tuple`
+ ``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
names of the ``*`` and ``**`` arguments or ``None``. *locals* is the locals
dictionary of the given frame.
.. function:: getframeinfo(frame[, context])
- Get information about a frame or traceback object. A 5-tuple is returned, the
- last five elements of the frame's frame record.
+ Get information about a frame or traceback object. A :term:`named tuple`
+ ``Traceback(filename, lineno, function, code_context, index)`` is returned.
.. function:: getouterframes(frame[, context])
string, and in :const:`MULTILINE` mode also matches before a newline. ``foo``
matches both 'foo' and 'foobar', while the regular expression ``foo$`` matches
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
# test and demonstrate ability to override methods
class Point(namedtuple('Point', 'x y')):
+ __slots__ = ()
@property
def hypot(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
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)
class Point(namedtuple('Point', 'x y')):
'Point class with optimized _make() and _replace() without error-checking'
+ __slots__ = ()
_make = classmethod(tuple.__new__)
def _replace(self, _map=map, **kwds):
return self._make(_map(kwds.get, ('x', 'y'), self))
print(Point(11, 22)._replace(x=100))
+ Point3D = namedtuple('Point3D', Point._fields + ('z',))
+ print(Point3D.__doc__)
+
import doctest
TestResults = namedtuple('TestResults', 'failed attempted')
print(TestResults(*doctest.testmod()))
import numbers as _numbers
import copy as _copy
+try:
+ from collections import namedtuple as _namedtuple
+ DecimalTuple = _namedtuple('DecimalTuple', 'sign digits exponent')
+except ImportError:
+ DecimalTuple = lambda *args: args
+
# Rounding
ROUND_DOWN = 'ROUND_DOWN'
ROUND_HALF_UP = 'ROUND_HALF_UP'
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):
"""Represents the number as an instance of Decimal."""
__all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher',
'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff',
- 'unified_diff', 'HtmlDiff']
+ 'unified_diff', 'HtmlDiff', 'Match']
import heapq
+from collections import namedtuple as _namedtuple
+
+Match = _namedtuple('Match', 'a b size')
def _calculate_ratio(matches, length):
if length:
>>> s = SequenceMatcher(None, " abcd", "abcd abcd")
>>> 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
determined as above, but with the additional restriction that no
>>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
>>> 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).
>>> s = SequenceMatcher(None, "ab", "c")
>>> 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.
a[besti+bestsize] == b[bestj+bestsize]:
bestsize = bestsize + 1
- return besti, bestj, bestsize
+ return Match(besti, bestj, bestsize)
def get_matching_blocks(self):
"""Return list of triples describing matching subsequences.
triple with n==0.
>>> s = SequenceMatcher(None, "abxcd", "abcd")
- >>> s.get_matching_blocks()
- [(0, 0, 2), (3, 2, 2), (5, 4, 0)]
+ >>> list(s.get_matching_blocks())
+ [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:
non_adjacent.append( (la, lb, 0) )
self.matching_blocks = non_adjacent
- return self.matching_blocks
+ return map(Match._make, self.matching_blocks)
def get_opcodes(self):
"""Return list of 5-tuples describing how to turn a into b.
import unittest, difflib, pdb, tempfile
import warnings
from io import StringIO
+from collections import namedtuple
+
+TestResults = namedtuple('TestResults', 'failed attempted')
# There are 4 basic classes:
# - Example: a <source, want> pair, plus an intra-docstring line number.
>>> tests.sort(key = lambda test: test.name)
>>> for test in tests:
... print(test.name, '->', runner.run(test))
- _TestClass -> (0, 2)
- _TestClass.__init__ -> (0, 2)
- _TestClass.get -> (0, 2)
- _TestClass.square -> (0, 1)
+ _TestClass -> TestResults(failed=0, attempted=2)
+ _TestClass.__init__ -> TestResults(failed=0, attempted=2)
+ _TestClass.get -> TestResults(failed=0, attempted=2)
+ _TestClass.square -> TestResults(failed=0, attempted=1)
The `summarize` method prints a summary of all the test cases that
have been run by the runner, and returns an aggregated `(f, t)`
7 tests in 4 items.
7 passed and 0 failed.
Test passed.
- (0, 7)
+ TestResults(failed=0, attempted=7)
The aggregated number of tried examples and failed examples is
also available via the `tries` and `failures` attributes:
# Record and return the number of failures and tries.
self.__record_outcome(test, failures, tries)
- return failures, tries
+ return TestResults(failures, tries)
def __record_outcome(self, test, f, t):
"""
print("***Test Failed***", totalf, "failures.")
elif verbose:
print("Test passed.")
- return totalf, totalt
+ return TestResults(totalf, totalt)
#/////////////////////////////////////////////////////////////////
# Backward compatibility cruft to maintain doctest.master.
... ''', {}, 'foo', 'foo.py', 0)
>>> runner.run(test)
- (0, 1)
+ TestResults(failed=0, attempted=1)
>>> test.globs
{}
else:
master.merge(runner)
- return runner.failures, runner.tries
+ return TestResults(runner.failures, runner.tries)
def testfile(filename, module_relative=True, name=None, package=None,
globs=None, verbose=None, report=True, optionflags=0,
else:
master.merge(runner)
- return runner.failures, runner.tries
+ return TestResults(runner.failures, runner.tries)
def run_docstring_examples(f, globs, verbose=False, name="NoName",
compileflags=None, optionflags=0):
(f,t) = self.testrunner.run(test)
if self.verbose:
print(f, "of", t, "examples failed in string", name)
- return (f,t)
+ return TestResults(f,t)
def rundoc(self, object, name=None, module=None):
f = t = 0
for test in tests:
(f2, t2) = self.testrunner.run(test)
(f,t) = (f+f2, t+t2)
- return (f,t)
+ return TestResults(f,t)
def rundict(self, d, name, module=None):
import types
import sys, os, types, re, dis, imp, tokenize, linecache
from operator import attrgetter
+from collections import namedtuple
# ----------------------------------------------------------- type-checking
def ismodule(object):
results.sort()
return results
+Attribute = namedtuple('Attribute', 'name kind defining_class object')
+
def classify_class_attrs(cls):
"""Return list of attribute-descriptor tuples.
else:
kind = "data"
- result.append((name, kind, homecls, obj))
+ result.append(Attribute(name, kind, homecls, obj))
return result
raise TypeError('arg is not a module, class, method, '
'function, traceback, frame, or code object')
+ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
+
def getmoduleinfo(path):
"""Get the module name, suffix, mode, and module type for a given file."""
filename = os.path.basename(path)
suffixes.sort() # try longest suffixes first, in case they overlap
for neglen, suffix, mode, mtype in suffixes:
if filename[neglen:] == suffix:
- return filename[:neglen], suffix, mode, mtype
+ return ModuleInfo(filename[:neglen], suffix, mode, mtype)
def getmodulename(path):
"""Return the module name for a given file, or None."""
# These constants are from Python's compile.h.
CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 1, 2, 4, 8
+Arguments = namedtuple('Arguments', 'args, varargs, varkw')
+
def getargs(co):
"""Get information about the arguments accepted by a code object.
lists. Keyword-only arguments are appended. 'varargs' and 'varkw'
are the names of the * and ** arguments or None."""
args, varargs, kwonlyargs, varkw = _getfullargs(co)
- return args + kwonlyargs, varargs, varkw
+ return Arguments(args + kwonlyargs, varargs, varkw)
def _getfullargs(co):
"""Get information about the arguments accepted by a code object.
varkw = co.co_varnames[nargs]
return args, varargs, kwonlyargs, varkw
+
+ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
+
def getargspec(func):
"""Get the names and default values of a function's arguments.
if kwonlyargs or ann:
raise ValueError("Function has keyword-only arguments or annotations"
", 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):
"""Get the names and default values of a function's arguments.
if not isfunction(func):
raise TypeError('arg is not a Python function')
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__)
+ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
+
def getargvalues(frame):
"""Get information about arguments passed into a particular frame.
return '(' + ', '.join(specs) + ')'
# -------------------------------------------------- stack frame extraction
+
+Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
+
def getframeinfo(frame, context=1):
"""Get information about a frame or traceback object.
else:
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):
"""Get the line number from a frame object, allowing for optimization."""
"""Unit tests for collections.py."""
-import unittest
+import unittest, doctest
from test import test_support
from collections import namedtuple
from collections import Hashable, Iterable, Iterator
self.failUnless(issubclass(sample, MutableSequence))
self.failIf(issubclass(str, MutableSequence))
+import doctest, collections
+NamedTupleDocs = doctest.DocTestSuite(module=collections)
def test_main(verbose=None):
import collections as CollectionsModule
- test_classes = [TestNamedTuple, TestOneTrickPonyABCs, TestCollectionABCs]
+ test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs, TestCollectionABCs]
test_support.run_unittest(*test_classes)
test_support.run_doctest(CollectionsModule, verbose)
of tried tests.
>>> doctest.DocTestRunner(verbose=False).run(test)
- (0, 3)
+ TestResults(failed=0, attempted=3)
If any example produces incorrect output, then the test runner reports
the failure and proceeds to the next example:
Expecting:
6
ok
- (1, 3)
+ TestResults(failed=1, attempted=3)
"""
def verbose_flag(): r"""
The `verbose` flag makes the test runner generate more detailed
Expecting:
6
ok
- (0, 3)
+ TestResults(failed=0, attempted=3)
If the `verbose` flag is unspecified, then the output will be verbose
iff `-v` appears in sys.argv:
>>> # If -v does not appear in sys.argv, then output isn't verbose.
>>> sys.argv = ['test']
>>> doctest.DocTestRunner().run(test)
- (0, 3)
+ TestResults(failed=0, attempted=3)
>>> # If -v does appear in sys.argv, then output is verbose.
>>> sys.argv = ['test', '-v']
Expecting:
6
ok
- (0, 3)
+ TestResults(failed=0, attempted=3)
>>> # Restore sys.argv
>>> sys.argv = old_argv
... '''
>>> test = doctest.DocTestFinder().find(f)[0]
>>> doctest.DocTestRunner(verbose=False).run(test)
- (0, 2)
+ TestResults(failed=0, attempted=2)
An example may not generate output before it raises an exception; if
it does, then the traceback message will not be recognized as
Exception raised:
...
ZeroDivisionError: integer division or modulo by zero
- (1, 2)
+ TestResults(failed=1, attempted=2)
Exception messages may contain newlines:
... '''
>>> test = doctest.DocTestFinder().find(f)[0]
>>> doctest.DocTestRunner(verbose=False).run(test)
- (0, 1)
+ TestResults(failed=0, attempted=1)
If an exception is expected, but an exception with the wrong type or
message is raised, then it is reported as a failure:
Traceback (most recent call last):
...
ValueError: message
- (1, 1)
+ TestResults(failed=1, attempted=1)
However, IGNORE_EXCEPTION_DETAIL can be used to allow a mismatch in the
detail:
... '''
>>> test = doctest.DocTestFinder().find(f)[0]
>>> doctest.DocTestRunner(verbose=False).run(test)
- (0, 1)
+ TestResults(failed=0, attempted=1)
But IGNORE_EXCEPTION_DETAIL does not allow a mismatch in the exception type:
Traceback (most recent call last):
...
ValueError: message
- (1, 1)
+ TestResults(failed=1, attempted=1)
If an exception is raised but not expected, then it is reported as an
unexpected exception:
Traceback (most recent call last):
...
ZeroDivisionError: integer division or modulo by zero
- (1, 1)
+ TestResults(failed=1, attempted=1)
"""
def optionflags(): r"""
Tests of `DocTestRunner`'s option flag handling.
>>> # Without the flag:
>>> test = doctest.DocTestFinder().find(f)[0]
>>> doctest.DocTestRunner(verbose=False).run(test)
- (0, 1)
+ TestResults(failed=0, attempted=1)
>>> # With the flag:
>>> test = doctest.DocTestFinder().find(f)[0]
1
Got:
True
- (1, 1)
+ TestResults(failed=1, attempted=1)
The DONT_ACCEPT_BLANKLINE flag disables the match between blank lines
and the '<BLANKLINE>' marker:
>>> # Without the flag:
>>> test = doctest.DocTestFinder().find(f)[0]
>>> doctest.DocTestRunner(verbose=False).run(test)
- (0, 1)
+ TestResults(failed=0, attempted=1)
>>> # With the flag:
>>> test = doctest.DocTestFinder().find(f)[0]
a
<BLANKLINE>
b
- (1, 1)
+ TestResults(failed=1, attempted=1)
The NORMALIZE_WHITESPACE flag causes all sequences of whitespace to be
treated as equal:
3
Got:
1 2 3
- (1, 1)
+ TestResults(failed=1, attempted=1)
>>> # With the flag:
>>> test = doctest.DocTestFinder().find(f)[0]
>>> flags = doctest.NORMALIZE_WHITESPACE
>>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
- (0, 1)
+ TestResults(failed=0, attempted=1)
An example from the docs:
>>> print(list(range(20))) #doctest: +NORMALIZE_WHITESPACE
[0, 1, 2, ..., 14]
Got:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
- (1, 1)
+ TestResults(failed=1, attempted=1)
>>> # With the flag:
>>> test = doctest.DocTestFinder().find(f)[0]
>>> flags = doctest.ELLIPSIS
>>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
- (0, 1)
+ TestResults(failed=0, attempted=1)
... also matches nothing:
e
f
g
- (1, 1)
+ TestResults(failed=1, attempted=1)
>>> # With the flag:
>>> test = doctest.DocTestFinder().find(f)[0]
f
g
-h
- (1, 1)
+ TestResults(failed=1, attempted=1)
The REPORT_CDIFF flag causes failures that involve multi-line expected
and actual outputs to be displayed using a context diff:
+ e
f
g
- (1, 1)
+ TestResults(failed=1, attempted=1)
The REPORT_NDIFF flag causes failures to use the difflib.Differ algorithm
? ^
+ a b c d e f g h i j k l m
? + ++ ^
- (1, 1)
+ TestResults(failed=1, attempted=1)
The REPORT_ONLY_FIRST_FAILURE supresses result output after the first
failing example:
200
Got:
2
- (3, 5)
+ TestResults(failed=3, attempted=5)
However, output from `report_start` is not supressed:
200
Got:
2
- (3, 5)
+ TestResults(failed=3, attempted=5)
For the purposes of REPORT_ONLY_FIRST_FAILURE, unexpected exceptions
count as failures:
Exception raised:
...
ValueError: 2
- (3, 5)
+ TestResults(failed=3, attempted=5)
New option flags can also be registered, via register_optionflag(). Here
we reach into doctest's internals a bit.
[0, 1, ..., 9]
Got:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- (1, 2)
+ TestResults(failed=1, attempted=2)
To turn an option off for an example, follow that example with a
comment of the form ``# doctest: -OPTION``:
[0, 1, ..., 9]
Got:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- (1, 2)
+ TestResults(failed=1, attempted=2)
Option directives affect only the example that they appear with; they
do not change the options for surrounding examples:
[0, 1, ..., 9]
Got:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- (2, 3)
+ TestResults(failed=2, attempted=3)
Multiple options may be modified by a single option directive. They
may be separated by whitespace, commas, or both:
[0, 1, ..., 9]
Got:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- (1, 2)
+ TestResults(failed=1, attempted=2)
>>> def f(x): r'''
... >>> print(list(range(10))) # Should fail
[0, 1, ..., 9]
Got:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- (1, 2)
+ TestResults(failed=1, attempted=2)
>>> def f(x): r'''
... >>> print(list(range(10))) # Should fail
[0, 1, ..., 9]
Got:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- (1, 2)
+ TestResults(failed=1, attempted=2)
The option directive may be put on the line following the source, as
long as a continuation prompt is used:
... '''
>>> test = doctest.DocTestFinder().find(f)[0]
>>> doctest.DocTestRunner(verbose=False).run(test)
- (0, 1)
+ TestResults(failed=0, attempted=1)
For examples with multi-line source, the option directive may appear
at the end of any line:
... '''
>>> test = doctest.DocTestFinder().find(f)[0]
>>> doctest.DocTestRunner(verbose=False).run(test)
- (0, 2)
+ TestResults(failed=0, attempted=2)
If more than one line of an example with multi-line source has an
option directive, then they are combined:
... '''
>>> test = doctest.DocTestFinder().find(f)[0]
>>> doctest.DocTestRunner(verbose=False).run(test)
- (0, 1)
+ TestResults(failed=0, attempted=1)
It is an error to have a comment of the form ``# doctest:`` that is
*not* followed by words of the form ``+OPTION`` or ``-OPTION``, where
(Pdb) print(x)
42
(Pdb) continue
- (0, 2)
+ TestResults(failed=0, attempted=2)
You can also put pdb.set_trace in a function called from a test:
(Pdb) print(x)
1
(Pdb) continue
- (0, 2)
+ TestResults(failed=0, attempted=2)
During interactive debugging, source code is shown, even for
doctest examples:
Expected nothing
Got:
9
- (1, 3)
+ TestResults(failed=1, attempted=3)
"""
def test_pdb_set_trace_nested():
(Pdb) print(foo)
*** NameError: NameError("name 'foo' is not defined",)
(Pdb) continue
- (0, 2)
+ TestResults(failed=0, attempted=2)
"""
def test_DocTestSuite():
1 items had failures:
1 of 2 in test_doctest.txt
***Test Failed*** 1 failures.
- (1, 2)
+ TestResults(failed=1, attempted=2)
>>> doctest.master = None # Reset master.
(Note: we'll be clearing doctest.master after each call to
>>> globs = {'favorite_color': 'blue'}
>>> doctest.testfile('test_doctest.txt', globs=globs)
- (0, 2)
+ TestResults(failed=0, attempted=2)
>>> doctest.master = None # Reset master.
>>> extraglobs = {'favorite_color': 'red'}
1 items had failures:
1 of 2 in test_doctest.txt
***Test Failed*** 1 failures.
- (1, 2)
+ TestResults(failed=1, attempted=2)
>>> doctest.master = None # Reset master.
The file may be made relative to a given module or package, using the
>>> doctest.testfile('test_doctest.txt', globs=globs,
... module_relative='test')
- (0, 2)
+ TestResults(failed=0, attempted=2)
>>> doctest.master = None # Reset master.
Verbosity can be increased with the optional `verbose` paremter:
2 tests in 1 items.
2 passed and 0 failed.
Test passed.
- (0, 2)
+ TestResults(failed=0, attempted=2)
>>> doctest.master = None # Reset master.
The name of the test may be specified with the optional `name`
**********************************************************************
File "...", line 6, in newname
...
- (1, 2)
+ TestResults(failed=1, attempted=2)
>>> doctest.master = None # Reset master.
The summary report may be supressed with the optional `report`
Exception raised:
...
NameError: name 'favorite_color' is not defined
- (1, 2)
+ TestResults(failed=1, attempted=2)
>>> doctest.master = None # Reset master.
The optional keyword argument `raise_on_error` can be used to raise an
1 items had failures:
2 of 2 in test_doctest4.txt
***Test Failed*** 2 failures.
- (2, 2)
+ TestResults(failed=2, attempted=2)
>>> doctest.master = None # Reset master.
>>> doctest.testfile('test_doctest4.txt', encoding='utf-8')
- (0, 2)
+ TestResults(failed=0, attempted=2)
>>> doctest.master = None # Reset master.
"""
42
Got:
84
-(1, 2)
+TestResults(failed=1, attempted=2)
>>> t.runstring(">>> x = x * 2\n>>> print(x)\n84\n", 'example2')
-(0, 2)
+TestResults(failed=0, attempted=2)
>>> t.summarize()
**********************************************************************
1 items had failures:
1 of 2 in XYZ
***Test Failed*** 1 failures.
-(1, 4)
+TestResults(failed=1, attempted=4)
>>> t.summarize(verbose=1)
1 items passed all tests:
2 tests in example2
4 tests in 2 items.
3 passed and 1 failed.
***Test Failed*** 1 failures.
-(1, 4)
+TestResults(failed=1, attempted=4)
"""
def old_test2(): r"""
3
ok
0 of 2 examples failed in string Example
- (0, 2)
+ TestResults(failed=0, attempted=2)
"""
def old_test3(): r"""
... return 32
...
>>> t.rundoc(_f) # expect 0 failures in 1 example
- (0, 1)
+ TestResults(failed=0, attempted=1)
"""
def old_test4(): """
>>> from doctest import Tester
>>> t = Tester(globs={}, verbose=0)
>>> t.rundict(m1.__dict__, "rundict_test", m1) # f2 and g2 and h2 skipped
- (0, 4)
+ TestResults(failed=0, attempted=4)
Once more, not excluding stuff outside m1:
>>> t = Tester(globs={}, verbose=0)
>>> t.rundict(m1.__dict__, "rundict_test_pvt") # None are skipped.
- (0, 8)
+ TestResults(failed=0, attempted=8)
The exclusion of objects from outside the designated module is
meant to be invoked automagically by testmod.
>>> doctest.testmod(m1, verbose=False)
- (0, 4)
+ TestResults(failed=0, attempted=4)
"""
######################################################################
if key in ignore: return
if key not in obj:
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):
''' succeed iff a == b or a in ignore or b in ignore '''
def test_easy(self):
self.checkModule('pyclbr')
- self.checkModule('doctest')
+ self.checkModule('doctest', ignore=("TestResults",))
self.checkModule('rfc822')
- self.checkModule('difflib')
+ self.checkModule('difflib', ignore=("Match",))
def test_decorators(self):
# XXX: See comment in pyclbr_input.py for a test that would fail
q = p.match(upper_char)
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():
from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR
#ifdef HAVE_FSTAT
# ifdef __VMS
/* on OpenVMS we must ensure that all bytes are written to the file */
- fsync(fd);
+ if (fd != -1) {
+ fsync(fd);
+ }
# 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) {
map_size = st.st_size;
} else if ((size_t)offset + (size_t)map_size > st.st_size) {
PyMODINIT_FUNC
init_socket(void)
{
- PyObject *m, *has_ipv6, *tmp;
+ PyObject *m, *has_ipv6;
if (!os_init())
return;
/* for subscriptions */
PyModule_AddIntConstant(m, "TIPC_SUB_PORTS", TIPC_SUB_PORTS);
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);
+#endif
PyModule_AddIntConstant(m, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
PyModule_AddIntConstant(m, "TIPC_PUBLISHED", TIPC_PUBLISHED);
PyModule_AddIntConstant(m, "TIPC_WITHDRAWN", TIPC_WITHDRAWN);
# Windows 2000 compatibility: WINVER 0x0500
# http://msdn2.microsoft.com/en-us/library/aa383745.aspx
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')
def nmake(makefile, command="", **kw):
Build with build_tkinter.py
---------------------------
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
Profile Guided Optimization
---------------------------
-The solution has two configurations for PGO. The PGInstrument configuration
-must be build first. The PGInstrument binaries are lniked against a profiling
-library and contain extra debug information. The PGUpdate configuration takes the profiling data and generates optimized binaries.
+The solution has two configurations for PGO. The PGInstrument
+configuration must be build first. The PGInstrument binaries are
+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
creates the PGI files, runs the unit test suite or PyBench with the PGI