Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Antoon Pardon Newsgroups: comp.lang.python Subject: Re: TypeError: '_TemporaryFileWrapper' object is not an iterator Date: Fri, 29 Jul 2016 10:43:12 +0200 Lines: 42 Message-ID: References: <5798BECB.8030800@rece.vub.ac.be> <579B1720.9000103@rece.vub.ac.be> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de /pMykam+/k52sSiw+t+2JQuscS+Q2sawXkErDayw/SLg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'python3': 0.05; 'received:134': 0.05; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'prefix': 0.07; "subject:' ": 0.07; 'tmp': 0.07; 'expectation': 0.09; 'tempfile': 0.09; 'skip:= 70': 0.10; 'subject:not': 0.11; 'def': 0.13; 'iterator': 0.16; 'received:ac.be': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:object': 0.16; 'true:': 0.16; 'try:': 0.18; 'bug?': 0.22; 'pass': 0.22; 'seems': 0.23; 'import': 0.24; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'not.': 0.27; 'received:be': 0.30; 'option': 0.31; 'problem': 0.33; 'option.': 0.33; 'file': 0.34; 'except': 0.34; 'false': 0.35; 'should': 0.36; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'to:addr:python.org': 0.40; 'skip:n 10': 0.62 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2AzJADzFptX/0YPuIZdhGa0ZRyGMwGGHAKBfAEBAQEBAV6FBAEFI1URCxoCBRYLAgIJAwIBAgFFEwgCiC2uQIoHGoNPAQslgQGBJIQFhE2EQEyCNYI9HQEEmTOBYI0eiUaFekiPYVSDfIc6KoEaAQEB X-IPAS-Result: A2AzJADzFptX/0YPuIZdhGa0ZRyGMwGGHAKBfAEBAQEBAV6FBAEFI1URCxoCBRYLAgIJAwIBAgFFEwgCiC2uQIoHGoNPAQslgQGBJIQFhE2EQEyCNYI9HQEEmTOBYI0eiUaFekiPYVSDfIc6KoEaAQEB User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.8.0 In-Reply-To: <5798BECB.8030800@rece.vub.ac.be> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <579B1720.9000103@rece.vub.ac.be> X-Mailman-Original-References: <5798BECB.8030800@rece.vub.ac.be> Xref: csiph.com comp.lang.python:112007 Below is a short program that illustrate the problem It works with python2, whether you use the -c option or not. It only works with python3 if you use the -c option. The problem seems to come from my expectation that a file is its own iterator and in python3 that is no longer true for a NamedTemporaryFile. Should this be considered a bug? ========================================================================= from tempfile import NamedTemporaryFile import sys def main(tmp): write = sys.stdout.write if tmp: tstfl = NamedTemporaryFile("w+", prefix = 'tmptest-') else: tstfl = open("Temporary", "w+") for nr in range(10): tstfl.write("This is line %d\n" % nr) tstfl.seek(0) try: while True: ln = next(tstfl) write(ln) except StopIteration: pass if __name__ == "__main__": tmp = True if len(sys.argv) > 1: # if -c option is passed a normal file will be # used instead of a NamedTemporaryFile if sys.argv[1] == '-c': tmp = False main(tmp)