Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6832971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:55:24+00:00 2026-05-26T22:55:24+00:00

How can I make my (Python 2.7) code aware whether it is running in

  • 0

How can I make my (Python 2.7) code aware whether it is running in a doctest?

The scenario is as follows: I have a function that print()s some output to a file descriptor passed in as an argument, something like this:

from __future__ import print_function

def printing_func(inarg, file=sys.stdout):
    # (do some stuff...)
    print(result, file=file)

But when I try to use printing_func() in a doctest, the test fails; because of my specification of the keyword argument file when invoking print(), the output actually goes to sys.stdout rather than whatever default output redirection is set by the doctest module, and doctest never sees the output.

So how can I make printing_func() aware whether it is running inside a doctest, so that it knows not to pass the file keyword argument when calling print()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:55:24+00:00Added an answer on May 26, 2026 at 10:55 pm

    I figured out the answer after reading doctest.py; posting here for posterity…

    Doctest redirects standard output by assigning a new file descriptor to sys.stdout. The problem was that my function description was closing over the value of the original sys.stdout file descriptor prior to doctest’s redefinition.

    Instead, if I do the following:

    def printing_func(inarg, file=None):
        # (do some stuff...)
    
        if file is None:
            file = sys.stdout
    
        print(result, file=file)
    

    then printing_func() will capture the sys module rather than sys.stdout, and when it runs it will retrieve doctest’s redefined stdout attribute from sys if running inside a test.

    EDIT: This also yields an easy way to check whether we are running inside a doctest:

    def inside_doctest(original_stdout=sys.stdout):
        return original_stdout != sys.stdout
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I make boost.python code python exceptions aware? For example, int test_for(){ for(;;){
How i can make some improvement in my IRC client made in Python. The
I currently have some python code I'd like to port to C++ as it's
I make multiple queries to my DB from my python code that uses pyodbc
How can I make this python code: # -*- coding: cp1252 -*- a=4 b=2
PyPy's JIT can make Python code execute much faster than CPython. Are there a
Suppose I have some Python code like the following: input = open(input.txt) x =
How can I make a keep alive HTTP request using Python's urllib2?
How can make a link within a facebox window that redirects it to another
Can someone tell me how I can make the following output? I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.