Path: csiph.com!feeder.erje.net!1.us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!news-1.dfn.de!news.dfn.de!news.informatik.hu-berlin.de!fu-berlin.de!uni-berlin.de!not-for-mail From: eryk sun Newsgroups: comp.lang.python Subject: Re: TypeError: '_TemporaryFileWrapper' object is not an iterator Date: Fri, 29 Jul 2016 11:59:52 +0000 Lines: 37 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 X-Trace: news.uni-berlin.de zAF8cvxU94ILuAUj31PzIQ0qLTwkJ01MCIZWSscTdogw== 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; 'method.': 0.05; 'python3': 0.05; '__name__': 0.07; "subject:' ": 0.07; 'wrapped': 0.07; 'expectation': 0.09; 'tempfile': 0.09; 'subject:not': 0.11; 'def': 0.13; "'__main__':": 0.16; '2016': 0.16; 'f.seek(0)': 0.16; "file's": 0.16; 'iterator': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:object': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'cc:2**0': 0.20; 'pass': 0.22; 'am,': 0.23; 'seems': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'skip:_ 20': 0.26; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; "i'd": 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'problem': 0.33; 'file': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'returning': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'self': 0.38; 'to:addr:python.org': 0.40; 'some': 0.40; 'skip:n 10': 0.62; 'skip:_ 50': 0.70; 'jul': 0.72; 'pardon': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=eabVGq4lSntAEccVSernkXaD4mkTBsMM+Pa9fqkfj4E=; b=loBf9kiIlWMQHXBAnD+GqBjIfWeq5nvQvsj+m/MqHZZyMhfPy2KDtvUkVrCvM3Mlpx yqJ4Z9NDdvhmUAp0aOeQ+sJS7H+2Yj8xIAdrlK3ERcw3KQ0oB3wIgKpjcGymPcQFNXil XnE20c2ppPU1Lg+yBof27rzryRcmNI6KQaiZKGEtZEPWiT7ggwOaOIYyY/npLnCOfV5S YloiE0+jVMH7FjHenSx6HmpDig11gTUzmicUBZ6jklPHo0n6Ut7JESV/enmDFhAJ+ScO apW1tXLlNvS2s8GIJNKseYYhxn3BMasdTdT/cNNwlfXdcthkokO46Uev/KcR/cd+e/nH JRMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=eabVGq4lSntAEccVSernkXaD4mkTBsMM+Pa9fqkfj4E=; b=hg2osX+9zeIXc5EX/lXzYnFp+08N7ykR/+Ycgqx6QJC9fnxppauxtBfEAum8tg/dMl 54+ZGiIrdpmw33d/UULsUM45cacrAaXSRtx9Fm/04YHpd6gI+aZ4ScIniCSPYUwLThXW XIaIiZSK7P/mgGawwwxRvJq4KMI1csJklig8XGYtVsVl3SYuC4/q9dhjMReFrkJEbhOv LiRKgFwrhYp1O2NtWTr2+cKzMzNbDtMvsSJZbDU2FI+hDapKiUgdShj7ByHLGh/vvbS3 ZhrxUfIcN2bDIObQOSzks6FKAjJtQFD1VWgTsOEupTwW8roYf8TldC3sj0bHSmlO5JGL 5V9A== X-Gm-Message-State: AEkoousio9pfKoUgnysA70h0/IZT5HukGIjXmErICh+ar3C0pv1q/HMAFe5/vIT67adrcEjVIex6d1GaOVYgoA== X-Received: by 10.36.31.149 with SMTP id d143mr703256itd.87.1469793632507; Fri, 29 Jul 2016 05:00:32 -0700 (PDT) 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:112013 On Fri, Jul 29, 2016 at 8:43 AM, Antoon Pardon wrote: > > 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. For some reason it uses a generator function for __iter__ instead of returning self, which would allow it to proxy the wrapped file's __next__ method. There may be a good reason for that design decision, but at the moment I don't see it, so I'd simply try the following: import tempfile import functools class _TemporaryFileWrapper(tempfile._TemporaryFileWrapper): def __iter__(self): return self def __next__(self): return next(self.file) @functools.wraps(tempfile.NamedTemporaryFile, assigned=['__doc__']) def NamedTemporaryFile(*args, **kwds): f = tempfile.NamedTemporaryFile(*args, **kwds) f.__class__ = _TemporaryFileWrapper return f if __name__ == '__main__': with NamedTemporaryFile('w+', prefix='tmptest-') as f: for n in range(10): f.write('This is line %d.\n' % n) f.seek(0) try: while True: print(next(f), end='') except StopIteration: pass