Commit Graph

198 Commits

Author SHA1 Message Date
Edward Loper 78b58f38f1 Changed Parser.get_examples() to return a list of Example objects,
rather than a list of triples.
2004-08-09 02:56:02 +00:00
Edward Loper 34fcb14768 - Split DocTestRunner's check_output and output_difference methods off
into their own class, OutputChecker.
- Added optional OutputChecker arguments to DocTestRunner,
  DocTestCase, DocTestSuite.
2004-08-09 02:45:41 +00:00
Edward Loper 7c748469c7 Rewrote Parser, using regular expressions instead of walking though
the string one line at a time.  The resulting code is (in my opinion,
anyway), much easier to read.  In the process, I found and fixed a
bug in the orginal parser's line numbering in error messages (it was
inconsistant between 0-based and 1-based).  Also, check for missing
blank lines after the prompt on all prompt lines, not just PS1 lines
(test added).
2004-08-09 02:06:06 +00:00
Tim Peters f3f57473ed Get rid of the ignore_imports argument to DocTestFinder.find().
This got slammed in when find() was fixed to stop grabbing doctests
from modules imported *by* the module being tested.  Such tests cannot
be expected to succeed, since they'll be run with the current module's
globals.  Dozens of Zope3 doctests were failing because of that.

It wasn't clear why ignore_imports got added then.  Maybe it's because
some existing tests failed when the change was made.  Whatever, it's
a Bad Idea so it's gone now.

The only use of it was exceedingly obscure, in test_doctest's "Duplicate
Removal" test.  It was "needed" there because, as an artifact of running
a doctest inside a doctest, the func_globals of functions compiled in
the second-level doctest don't match the module globals, and so the
test-finder believed these functions were from a foreign module and
skipped them.  But that took a long time to figure out, and I actually
understand some of this stuff <0.9 wink>.

That problem was resolved by moving the source code for the second-level
doctest into an actual module (test/doctest_aliases.py).

The only remaining difficulty was that the test for the deprecated
Tester.rundict() then failed, because the test finder doesn't take
module=None at face value, trying to guess which module the user really
intended then.  Its guess wasn't appropriate for what Tester.rundict
needs when module=None is given to *it*, which is "no, there is no
module here, and I mean it".  So now passing module=False means exactly
that.  This is hokey, but ignore_imports=False was really a hack to worm
around that there was no way to tell the test-finder that module=None
*sometimes* means what it says.  There was no use case for the combination
of passing a real module with ignore_imports=False.
2004-08-08 06:11:48 +00:00
Tim Peters 161c963276 Type in docstring. 2004-08-08 03:38:33 +00:00
Tim Peters 3ddd60a83b Also deprecated the old Tester class, which is no longer used by anything
except internal tests.
2004-08-08 02:43:33 +00:00
Tim Peters bafb1fed51 Deprecate the doctest.is_private() function. 2004-08-08 01:52:57 +00:00
Tim Peters f727c6c2c7 Deprecated testmod's useless & confusing isprivate gimmick.
Ripped out the docs for the new DocTestFinder's namefilter argument,
and renamed it to _namefilter; this only existed to support isprivate.
Removed the new DocTestFinder's objfilter argument.  No point adding
more cruft to a broken filtering design.
2004-08-08 01:48:59 +00:00
Tim Peters d1b7827216 Whitespace normalization. 2004-08-07 06:03:09 +00:00
Tim Peters 19397e5ec5 Merging from tim-doctest-branch, which is now closed.
This primarily adds more powerful ways to work with unittest, including
spiffy support for building suites out of doctests in non-Python
"text files".
2004-08-06 22:02:59 +00:00
Tim Peters 9b625d3037 Example.__init__: this cannot use assert, because that fails to trigger
in a -O run, and so test_doctest was failing under -O.  Simple cause,
simple cure.
2004-08-04 20:04:32 +00:00
Tim Peters 8485b56216 Edward Loper's cool and massive refactoring of doctest.py, merged from
the tim-doctest-merge-24a2 tag on the the tim-doctest-branch branch.
We did development on the branch in case it wouldn't land in time for
2.4a2, but the branch looked good:  Edward's tests passed there, ditto
Python's tests, and ditto the Zope3 tests.  Together, those hit doctest
heavily.
2004-08-04 18:46:34 +00:00
Jim Fulton a643b658a7 Ported some features from zope:
- Fixed the display of tests in verbose output
- Allow setUp and tearDown functions to be provided for DocTestSuites.
2004-07-14 19:06:50 +00:00
Tim Peters 4e0e1b6a54 Whitespace normalization. 2004-07-07 20:54:48 +00:00
Martin v. Löwis 92816de18e Patch #932930: suggest the use of rawstrings for backslashes. 2004-05-31 19:01:00 +00:00
Walter Dörwald 70a6b49821 Replace backticks with repr() or "%r"
From SF patch #852334.
2004-02-12 17:35:32 +00:00
Raymond Hettinger 7a70ea4135 SF patch #806246: use basestring where possible
(Contributed by George Yoshida.)
2003-09-17 05:50:59 +00:00
Raymond Hettinger 5f8b0b1fd4 SF 798269: bug fix for doctest (sf bug id: 798254
(Contributed by Alexander Belopolsky.)

Doctest would crash when encountering unbound methods:
  class A:
    def f(self): pass

  class C(A):
    g = A.f
2003-09-02 02:09:05 +00:00
Raymond Hettinger 71adf7e9d8 Doctest now examines all docstrings by default. Previously, it would
skip over functions with private names (as indicated by the underscore
naming convention).  The old default created too much of a risk that
user tests were being skipped inadvertently.  Note, this change could
break code in the unlikely case that someone had intentionally put
failing tests in the docstrings of private functions.  The breakage
is easily fixable by specifying the old behavior when calling testmod()
or Tester().  The more likely case is that the silent failure was
unintended and that the user needed to be informed so the test could be
fixed.
2003-07-16 19:25:22 +00:00
Raymond Hettinger cc39a13d6d Expose the 'master' instance mentioned in the docs. 2003-07-11 22:36:52 +00:00
Tim Peters db3756dade Some nifty doctest extensions from Jim Fulton, currently used in Zope3.
I won't have time to write real docs, but spent a lot of time adding
comments to his code and fleshing out the exported functions' docstrings.
There's probably opportunity to consolidate how docstrings get extracted
too, and the new code for that is probably better than the old code for
that (which strained mightily to recover from 2.2's new class/type
gimmicks).
2003-06-29 05:30:48 +00:00
Tim Peters 275abbd525 Missed a spot where the new optional optionflags argument needed to get
passed on.
2003-06-29 03:11:20 +00:00
Tim Peters 6ebe61fa80 A hack to ease compatibility with pre-2.3 Pythons: by default, doctest
now accepts "True" when a test expects "1", and similarly for "False"
versus "0".  This is un-doctest-like, but on balance makes it much
more pleasant to write doctests that pass under 2.2 and 2.3.  I expect
it to go away again, when 2.2 is forgotten.  In the meantime, there's
a new doctest module constant that can be passed to a new optional
argument, if you want to turn this behavior off.

Note that this substitution is very simple-minded:  the expected and
actual outputs have to consist of single tokens.  No attempt is made,
e.g., to accept [True, False] when a test expects [1, 0].  This is a
simple hack for simple tests, and I intend to keep it that way.
2003-06-27 20:48:05 +00:00
Martin v. Löwis 4581cfa326 Patch #486438: Make module argument to testmod optional. 2002-11-22 08:23:09 +00:00
Raymond Hettinger 54f0222547 SF 563203. Replaced 'has_key()' with 'in'. 2002-06-01 14:18:47 +00:00
Guido van Rossum 77f6a65eb0 Add the 'bool' type and its values 'False' and 'True', as described in
PEP 285.  Everything described in the PEP is here, and there is even
some documentation.  I had to fix 12 unit tests; all but one of these
were printing Boolean outcomes that changed from 0/1 to False/True.
(The exception is test_unicode.py, which did a type(x) == type(y)
style comparison.  I could've fixed that with a single line using
issubtype(x, type(y)), but instead chose to be explicit about those
places where a bool is expected.

Still to do: perhaps more documentation; change standard library
modules to return False/True from predicates.
2002-04-03 22:41:51 +00:00
Tim Peters bcc2c125f8 Change raw "except:" constructs to pass on KeyboardInterrupt.
Bugfix candidate?  Don't know -- never bothered me, but it's minor
either way.
2002-03-20 19:32:03 +00:00
Tim Peters c77db34575 SF bug [#473864] doctest expects spurios space.
Repair unlikely surprise due to magical softspace attr and the use of
print with a trailing comma in doctest examples.
Bugfix candidate.
2001-10-23 02:21:52 +00:00
Tim Peters 17111f3b24 SF bug [#467336] doctest failures w/ new-style classes.
Taught doctest about static methods, class methods, and property docstrings
in new-style classes.  As for inspect.py/pydoc.py before it, the new stuff
needed didn't really fit into the old architecture (but was less of a
strain to force-fit here).
New-style class docstrings still aren't found, but that's the subject
of a different bug and I want to fix that right instead of hacking around
it in doctest.
2001-10-03 04:08:26 +00:00
Tim Peters 4a9ac4a83c SF patch [#466616] Exclude imported items from doctest.
Another installment; the new functionality wasn't actually enabled in
normal use, only in the strained use checked by the test case.
2001-10-02 22:47:08 +00:00
Tim Peters 7402f791a4 SF patch [#466616] Exclude imported items from doctest,
from Tim Hochberg.  Also mucho fiddling to change the way doctest
determines whether a thing is a function, module or class.  Under 2.2,
this really requires the functions in inspect.py (e.g., types.ClassType
is close to meaningless now, if not outright misleading).
2001-10-02 03:53:41 +00:00
Tim Peters 4fd9e2fc13 Remove the horrid generators hack from doctest.py. This relies on a
somewhat less horrid hack <wink>:  if a module does
    from __future__ import X
then the module dict D is left in a state such that (viewing X as a
string)
    D[X] is getattr(__future__, X)
So by examining D for all the names of future features, and making that
test for each, we can make a darned good guess as to which future-features
were imported by the module.  The appropriate flags are then sucked out
of the __future__ module, and passed on to compile()'s new optional
arguments (PEP 264).

Also gave doctest a meaningful __all__, removed the history of changes
(CVS serves that purpose now), and removed the __version__ vrbl (similarly;
before CVS, it was a reasonable clue, but not anymore).
2001-08-18 00:05:50 +00:00
Tim Peters 03813399cc Document doctest's generator-future hack. 2001-07-16 18:39:58 +00:00
Tim Peters fe2127d3cb Ugly. A pile of new xxxFlags() functions, to communicate to the parser
that 'yield' is a keyword.  This doesn't help test_generators at all!  I
don't know why not.  These things do work now (and didn't before this
patch):

1. "from __future__ import generators" now works in a native shell.

2. Similarly "python -i xxx.py" now has generators enabled in the
   shell if xxx.py had them enabled.

3. This program (which was my doctest proxy) works fine:

from __future__ import generators

source = """\
def f():
    yield 1
"""

exec compile(source, "", "single") in globals()
print type(f())
2001-07-16 05:37:24 +00:00
Tim Peters fee69d0313 Changed some comments. Removed the caution about clearing globs, since
clearing a shallow copy _run_examples() makes itself can't hurt anything.
2001-06-24 20:24:16 +00:00
Tim Peters d4ad59e1eb Clear the copy of the globs dict after running examples. This helps to
break cycles, which are a special problem when running generator tests
that provoke exceptions by invoking the .next() method of a named
generator-iterator:  then the iterator is named in globs, and the
iterator's frame gets a tracekback object pointing back to globs, and
gc doesn't chase these types so the cycle leaks.

Also changed _run_examples() to make a copy of globs itself, so its
callers (direct and indirect) don't have to (and changed the callers
to stop making their own copies); *that* much is a change I've been
meaning to make for a long time (it's more robust the new way).

Here's a way to provoke the symptom without doctest; it leaks at a
prodigious rate; if the last two "source" lines are replaced with
    g().next()
the iterator isn't named and then there's no leak:

source = """\
def g():
    yield 1/0

k = g()
k.next()
"""

code = compile(source, "<source>", "exec")

def f(globs):
    try:
        exec code in globs
    except ZeroDivisionError:
        pass

while 1:
    f(globals().copy())

After this change, running test_generators in an infinite loop still leaks,
but reduced from a flood to a trickle.
2001-06-24 20:02:47 +00:00
Tim Peters 77f2d504c3 doctest systematically leaked memory when handling an exception in an
example (an obvious trackback cycle).  Repaired.
Bugfix candidate.
2001-06-24 18:59:01 +00:00
Tim Peters 08bba953ea doctest doesn't handle intentional SyntaxError exceptions gracefully,
because it picks up the first line of traceback.format_exception_only()
instead of the last line.  Pick up the last line instead!
2001-06-24 06:46:58 +00:00
Tim Peters 24a4191160 Changed doctest to run tests in alphabetic order of name.
This makes verbose-mode output easier to dig thru, and removes an accidental
dependence on the order of dict.items() (made visible by recent changes to
dictobject.c).
2001-03-21 23:07:59 +00:00
Guido van Rossum 261d91a3f9 Make doctest's self-test succeed after the previous change. 2001-03-18 17:05:58 +00:00
Guido van Rossum af00a46599 Print a bunch of asterisks before the failure summary, to separate it
from the last failure report.
2001-03-18 16:58:44 +00:00
Tim Peters f9bb4969af Miranda newlines: if anything at all was written to stdout, supply a
newline at the end if there isn't one already.  Expected output has no
way to indicate that a trailing newline was not expected, and in the
interpreter shell *Python* supplies the trailing newline before printing
the next prompt.
2001-02-14 06:35:35 +00:00
Tim Peters 60e23f4cfc Change doctest exception example to one whose detail hasn't changed since 1.5.2. 2001-02-14 00:43:21 +00:00
Tim Peters ea4f931cb9 Teach doctest about newer "(most recent call last)" traceback spelling. 2001-02-13 20:54:42 +00:00
Tim Peters ecb6fb95a2 Bump __version__ tuple. 2001-02-10 01:24:50 +00:00
Eric S. Raymond 630e69cd89 String method conversion. 2001-02-09 08:33:43 +00:00
Skip Montanaro eccd02a40d more __all__ updates 2001-01-20 23:34:12 +00:00
Tim Peters 8a7d2d5cb1 doctest-- The Little Module That Could --finally makes it to the Big Show <wink>. 2001-01-16 07:10:57 +00:00