Hi all,

I've continued playing about with mocking raw_input in doctests in light of Kent's reply.

I've hit an odd thing---if my substitute for raw_input returns '', I get an EOFError. The code below shows the problem. It arises equally if myrawalt is used in the doctest in place of myraw, so I am sure the problem has nothing to do with the use of iter. I can work around the problem in my actual target code---that code, like the toy code below, calls strip() on the results of raw_input, so I can get away with having my raw_input substitute return ' ' in place of ''. But, I cannot figure out why the EOFError comes up. Any thoughts?

Thanks and best,

Brian vdB


class myraw(object):
    def __init__(self, values):
        self.stream = iter(values)
    def readline(self):
        return str(self.stream.next())

class myrawalt(object):
    def __init__(self, values):
        self.values = values
        self.count = 0
    def readline(self):
        data = str(self.values[self.count])
        self.count += 1
        return data

def double_input():
    """>>> import sys
    >>> sys.stdin = myraw([21,3.1415])
    >>> double_input(); double_input() # doctest: +ELLIPSIS
    42
    6.28300...
    >>> sys.stdin = myraw([' ',])
    >>> double_input()
    0
    >>> sys.stdin = myraw(['',])
    >>> double_input()    # Failing test
    0
    """
    val = raw_input().strip() or 0
    val = float(val) * 2
    val = [val, int(val)][val == int(val)]
    return val

def __test():
    import doctest
    doctest.testmod()

if __name__ == "__main__":
    __test()

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to