Re: to Doctest as SystemExit is to Python

2007-12-13 Thread pelavarre
> Sent: 2006-11-09; To: comp.lang.python
>
> Can doctest ... be persuaded to exit after a catastroph[e]...?
> ...
> sys.exit() doesn't do what I mean:
> it raises SystemExit ... [and doesn't exit]
> ...
> doctest.REPORT_ONLY_FIRST_FAILURE doesn't do what I mean
> [it filters stdout but doesn't exit]

Yes doctest can be asked to HALT_AT_NTH_FAILURE, including
HALT_AT_FIRST_FAILURE.

Merely (1) construct an object to replace stdout, so that (2) you can
redefine the stdout.write calls of doctest, so that (3) you can filter
the bytes written there, so that (4) you can raise KeyboardInterrupt,
so that (5) you can exit when you please.

Not a documented feature. Mostly works all the same.

A working example appeared at http://wiki.python.org/moin/doctest on
2007-11-12.

-- Pat LaVarre

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: to Doctest as SystemExit is to Python

2006-11-10 Thread p . lavarre
(((I thought I had sent this reply already, Google says No.)))

> > Can I somehow tell doctest that it's time to quit?
> > ... doctest redefines ... SystemExit ...
>
> Hit Ctrl-C. Or raise a KeyboardInterrupt:

Yes!  Thank you!!!  I see now,

Doctest exactly reverses the python -i experience:

doctest exits quietly if I raise KeyboardInterrupt.
doctest catches SystemExit and prints the traceback.

python -i exits quietly if I raise SystemExit.
python -i catches KeyboardInterrupt and prints the traceback.

Of course, to make the doctest work as sketched, I had to strip the
trailing blanks that my browser introduced and I had to expect
tracebacks for raise Exception and raise SystemExit, e.g.:

>>> import sys
>>> sys.exit()
Traceback (most recent call last):
  ...
SystemExit
>>>

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: to Doctest as SystemExit is to Python

2006-11-10 Thread Peter Otten
[EMAIL PROTECTED] wrote:

> Can I somehow tell doctest that it's time to quit?

Hit Ctrl-C. Or raise a KeyboardInterrupt:

import sys

class ExitDoctest(KeyboardInterrupt):
pass

def f(what):
"""
>>> f("alpha")
'alpha'
>>> f("e")
'e'
>>> f("x")
'x'
>>> f("X")
'X'
>>> f("beta")
'beta'
"""
if what == "e":
raise Exception
elif what == "x":
sys.exit()
elif what == "X":
raise ExitDoctest("You're in trouble")
return what

if __name__ == "__main__":
import doctest
try:
doctest.testmod()
except ExitDoctest, e:
print >> sys.stderr, e

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


to Doctest as SystemExit is to Python

2006-11-09 Thread p . lavarre
> From: http://docs.python.org/lib/doctest-soapbox.html ...
> Regression testing is best confined to dedicated objects or files ...

Can I somehow tell doctest that it's time to quit?

I ask because not all doctest examples are created equal.  Some
failures are catastrophic, making all subsequent failures at least
uninteresting, and maybe painfully slow.  Other failures are
negligible, making the print of any subsequent failure valuable.

So sys.exit() doesn't do what I mean: it raises SystemExit, but doctest
redefines the SystemExit exception to mean print a traceback, rather
than meaning to exit quietly after a noisily catastrophic failure,
exiting almost as if the doctest had passed.

And doctest.REPORT_ONLY_FIRST_FAILURE doesn't do what I mean: it prints
only the first failure, always, never the valuable subsequent failures.

Can doctest somehow be persuaded to do the right thing?  That is, to
exit after a catastrophic failure, but not after a negligible failure,
and not after an independent failure?

In the last Python I shipped, to get the effect of exit after
catastrophe, I actually faked a run of doctest:

print 'Expected:'
print "'...'"
print 'Got:'
print "" + repr(result)
print '***Test Failed***'
sys.exit(-1)

Surely that's wrong?  Somehow not idiomatic Python?

Surely I can somehow tell doctest that my code has just realised,
sorry, it is time to quit?

Thanks in advance, Pat LaVarre

-- 
http://mail.python.org/mailman/listinfo/python-list