[Tutor] Do not understand why I am getting "EOFError: EOF when reading a line".

boB Stepp robertvstepp at gmail.com
Fri Oct 7 23:16:59 EDT 2016


My current get_input() function:

======================================================
def get_input():
    '''Get string input from the user and convert it to an integer.  This
    integer is returned to the caller.

    :num_sides:  Number of sides for the displayed grid.'''

    while True:
        try:
            num_sides = int(input("Number of sides = "))
            return num_sides
        except ValueError:
            print("'Number of sides' must be a positive integer.  Please enter "
                    "an integer.")
=======================================================

My current TestGetInput class:

=======================================================
class TestGetInput(unittest.TestCase):
    '''Tests for the function get_input().'''

    def setUp(self):
        '''Establish conditions for running these tests.'''

        # Redirect sys.stdin in order to test input functions.
        self.old_stdin = sys.stdin
        sys.stdin = StringIO('5')

    def test_get_input(self):
        '''Test that get_input() returns the expected result.'''

        expected = 5
        self.assertEqual(draw2x2grid.get_input(), expected)

    def test_get_input_value_error(self):
        '''Test that get_input() raises a ValueError if improper input
        entered.'''

        sys.stdin = StringIO('a')
        with self.assertRaises(ValueError):
            draw2x2grid.get_input()

    def tearDown(self):
        '''Clear temporary testing variables.'''

        # Return sys.stdin to its normal state.
        sys.stdin = self.old_stdin
=========================================================

When I run the tests I get:

==========================================================
c:\thinkpython2\ch3\ex_3-3>py -m unittest
Number of sides = .Number of sides = 'Number of sides' must be a
positive integer.  Please enter an integer.
Number of sides = E...
======================================================================
ERROR: test_get_input_value_error (test_draw2x2grid.TestGetInput)
Test that get_input() raises a ValueError if improper input
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\thinkpython2\ch3\ex_3-3\test_draw2x2grid.py", line 126, in
test_get_input_value_error
    draw2x2grid.get_input()
  File "c:\thinkpython2\ch3\ex_3-3\draw2x2grid.py", line 50, in get_input
    num_sides = int(input("Number of sides = "))
EOFError: EOF when reading a line

----------------------------------------------------------------------
Ran 5 tests in 0.004s

FAILED (errors=1)
======================================================================

I do not understand what is causing the EOFError.  If I change
"sys.stdin = StringIO('a')" with "sys.stdin = StringIO('1')" I get
what I expect:

======================================================================
c:\thinkpython2\ch3\ex_3-3>py -m unittest
Number of sides = .Number of sides = F...
======================================================================
FAIL: test_get_input_value_error (test_draw2x2grid.TestGetInput)
Test that get_input() raises a ValueError if improper input
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\thinkpython2\ch3\ex_3-3\test_draw2x2grid.py", line 126, in
test_get_input_value_error
    draw2x2grid.get_input()
AssertionError: ValueError not raised

----------------------------------------------------------------------
Ran 5 tests in 0.003s

FAILED (failures=1)
======================================================================

I am currently quite stumped.  What is it I am not seeing?

-- 
boB


More information about the Tutor mailing list