Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: TypeError: '_TemporaryFileWrapper' object is not an iterator Date: Fri, 29 Jul 2016 15:25:00 -0400 Lines: 64 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; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 9YIT+qEz2XRJYmI7w5EdYAD39ctmfLzgyEPhBuZZEV4g== 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; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'prefix': 0.07; "subject:' ": 0.07; 'tmp': 0.07; 'expectation': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tempfile': 0.09; 'skip:= 70': 0.10; 'jan': 0.11; 'subject:not': 0.11; 'def': 0.13; 'iterable,': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'iterator.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'subject:object': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'bug?': 0.22; 'doc': 0.22; 'pass': 0.22; 'am,': 0.23; 'seems': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'skip:_ 20': 0.26; 'not.': 0.27; 'said,': 0.27; 'option': 0.31; 'problem': 0.33; 'option.': 0.33; 'file': 0.34; 'except': 0.34; 'add': 0.34; 'next': 0.35; 'false': 0.35; 'replace': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'possible.': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'does': 0.39; 'to:addr:python.org': 0.40; 'claim': 0.61; 'no.': 0.62; 'skip:n 10': 0.62; 'received:96': 0.63; 'more': 0.63; 'guaranteed': 0.67; 'idiomatic': 0.84; 'pardon': 0.84; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-96-227-207-81.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: <579B1720.9000103@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: X-Mailman-Original-References: <5798BECB.8030800@rece.vub.ac.be> <579B1720.9000103@rece.vub.ac.be> Xref: csiph.com comp.lang.python:112026 On 7/29/2016 4:43 AM, Antoon Pardon wrote: > 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. As Eryk Sun said, a _TemporaryFileWrapper is an iterable, but not an iterator. > Should this be considered a bug? No. The doc for NamedTemporaryFile does not even claim that the return is iterable. > ========================================================================= > > 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) Either add "tstfl_it = iter(tstfl)" and call next on what is now guaranteed to be an iterator, if one is possible. > try: > while True: > ln = next(tstfl) > write(ln) > except StopIteration: > pass Or replace the above with the much easier to write and more idiomatic for line in tstfl: write(line) > 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) > -- Terry Jan Reedy