logging module and doctest

Discussion in 'Python' started by Gary Jefferson, Jan 25, 2007.

  1. I've written a logging.filter and would like to use doctest on it
    (using a StreamHandler for stdout), but this doesn't seem possible.
    Output from the logger seems to disappear (running the doctest strings
    through the interpreter as-is yields expected results). I assume this
    is because doctest does something with logging. Is there any way to
    make these work together?

    Gary
    Gary Jefferson, Jan 25, 2007
    #1
    1. Advertising

  2. Gary Jefferson

    Peter Otten Guest

    Gary Jefferson wrote:

    > I've written a logging.filter and would like to use doctest on it
    > (using a StreamHandler for stdout), but this doesn't seem possible.
    > Output from the logger seems to disappear (running the doctest strings
    > through the interpreter as-is yields expected results). I assume this
    > is because doctest does something with logging.


    It redirects stdout to a StringIO subclass to capture the output.

    > Is there any way to make these work together?


    Using the StreamHandler with something like

    class WrapStdOut(object):
    def __getattr__(self, name):
    return getattr(sys.stdout, name)

    instead of sys.stdout directly should work.

    Peter
    Peter Otten, Jan 25, 2007
    #2
    1. Advertising

  3. Gary Jefferson

    Peter Otten Guest

    Peter Otten wrote:

    > Gary Jefferson wrote:
    >
    >> I've written a logging.filter and would like to use doctest on it
    >> (using a StreamHandler for stdout), but this doesn't seem possible.
    >> Output from the logger seems to disappear (running the doctest strings
    >> through the interpreter as-is yields expected results). I assume this
    >> is because doctest does something with logging.

    >
    > It redirects stdout to a StringIO subclass to capture the output.
    >
    >> Is there any way to make these work together?

    >
    > Using the StreamHandler with something like
    >
    > class WrapStdOut(object):
    > def __getattr__(self, name):
    > return getattr(sys.stdout, name)
    >
    > instead of sys.stdout directly should work.


    Or create the StreamHandler inside your doctest where sys.stdout is already
    redirected.

    Peter
    Peter Otten, Jan 25, 2007
    #3
  4. Peter Otten wrote:
    > Peter Otten wrote:
    >
    > > Gary Jefferson wrote:
    > >
    > >> I've written a logging.filter and would like to use doctest on it
    > >> (using a StreamHandler for stdout), but this doesn't seem possible.
    > >> Output from the logger seems to disappear (running the doctest strings
    > >> through the interpreter as-is yields expected results). I assume this
    > >> is because doctest does something with logging.

    > >
    > > It redirects stdout to a StringIO subclass to capture the output.
    > >
    > >> Is there any way to make these work together?

    > >
    > > Using the StreamHandler with something like
    > >
    > > class WrapStdOut(object):
    > > def __getattr__(self, name):
    > > return getattr(sys.stdout, name)
    > >
    > > instead of sys.stdout directly should work.

    >
    > Or create the StreamHandler inside your doctest where sys.stdout is already
    > redirected.
    >
    > Peter


    I was creating the StreamHandler inside doctest. Turns out, however,
    that I hadn't imported 'sys', and so it didn't work. Its a little
    strange that this didn't result in an error, but there you have it.

    Thanks for the tip that led me to discover this.

    Gary
    Gary Jefferson, Jan 25, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christoph Haas
    Replies:
    0
    Views:
    431
    Christoph Haas
    Jun 12, 2006
  2. Christoph Haas
    Replies:
    1
    Views:
    441
    Vinay Sajip
    Jun 14, 2006
  3. Replies:
    0
    Views:
    307
  4. Replies:
    1
    Views:
    317
    John J. Lee
    Nov 22, 2007
  5. Nicholas Wieland

    [RFC] DocTest module

    Nicholas Wieland, May 31, 2005, in forum: Ruby
    Replies:
    2
    Views:
    93
    Nicholas Wieland
    Jun 1, 2005
Loading...

Share This Page