Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail 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; 'argument': 0.05; 'output': 0.05; '"""': 0.07; '-*-': 0.07; 'binary': 0.07; 'problem?': 0.07; 'utf-8': 0.07; '%s",': 0.09; '__name__': 0.09; 'coding:': 0.09; 'lines:': 0.09; 'method:': 0.09; 'valueerror:': 0.09; 'python': 0.10; 'def': 0.11; 'causing': 0.15; "'__main__':": 0.16; '32,': 0.16; 'command,': 0.16; 'file.read()': 0.16; 'filename)': 0.16; 'filename):': 0.16; 'interprets': 0.16; 'kern': 0.16; 'python3': 0.16; 'received:80.91': 0.16; 'received:80.91.229': 0.16; 'received:gmane.org': 0.16; 'received:list': 0.16; 'scripts.': 0.16; 'string:': 0.16; 'subject:issue': 0.16; 'true:': 0.16; 'underlying': 0.16; 'wrote:': 0.17; 'encoding': 0.18; 'file,': 0.18; 'file.': 0.18; 'runs': 0.20; 'string': 0.22; 'interpret': 0.22; 'yield': 0.22; 'elements': 0.23; 'header:In-Reply-To:1': 0.23; 'import': 0.24; 'example': 0.24; 'header:User-Agent:1': 0.26; 'important.': 0.27; 'robert': 0.27; 'header:X-Complaints- To:1': 0.28; "doesn't": 0.28; 'class': 0.30; '(most': 0.30; 'skip:# 10': 0.30; 'fine': 0.31; 'code': 0.31; 'skip:_ 10': 0.31; 'lines': 0.32; 'attempt': 0.33; 'file': 0.34; 'another': 0.35; 'mode': 0.35; 'to:addr:python-list': 0.35; 'there': 0.35; 'skip:d 20': 0.36; 'test': 0.36; 'but': 0.36; 'subject:: ': 0.37; 'detail': 0.38; 'some': 0.38; 'sure': 0.38; 'pm,': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.39; 'header:Received:5': 0.39; 'called': 0.40; 'whole': 0.60; 'world': 0.61; 'more': 0.63; 'our': 0.64; 'suspect': 0.66; 'believe': 0.68; '8bit%:100': 0.70; 'demonstrates': 0.84; 'eco': 0.84; 'fail.': 0.84; 'terrible': 0.84; 'thing...': 0.84; 'last):': 0.91; 'skip:\xc3 10': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Robert Kern Subject: Re: Frustrating circular bytes issue Date: Tue, 26 Jun 2012 17:53:24 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 213.1.240.227 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1340729618 news.xs4all.nl 6885 [2001:888:2000:d::a6]:53233 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24468 On 6/26/12 5:30 PM, J wrote: > This is driving me batty... more enjoyment with the Python3 > "Everything must be bytes" thing... sigh... > I have a file that contains a class used by other scripts. The class > is fed either a file, or a stream of output from another command, then > interprets that output and returns a set that the main program can > use... confusing, perhaps, but not necessarily important. > > The class is created and then called with the load_filename method: > > > def load_filename(self, filename): > logging.info("Loading elements from filename: %s", filename) > > file = open(filename, "rb", encoding="utf-8") > return self.load_file(file, filename) I get this with Python 3.2: Traceback (most recent call last): File "bytes_unicode.py", line 32, in d.load_filename(__file__) File "bytes_unicode.py", line 6, in load_filename file = open(filename, "rb", encoding="utf-8") ValueError: binary mode doesn't take an encoding argument Are you sure you are copy-pasting the code that is actually running? Can you reduce your test case down to a small self-contained example that runs and demonstrates the problem? I suspect that there is some other detail that is causing things to fail. The following code works fine for me: #!/usr/bin/env python # -*- coding: UTF-8 -*- import re class Dummy(object): """ This is a dummy file. éµ∫é∂∂é∂ üñîçø∂é """ def load_filename(self, filename): file = open(filename, "r", encoding="utf-8") return self.load_file(file, filename) def load_file(self, file, filename=""): for string in self._reader(file): print(string) if not string: break def _reader(self, file, size=4096, delimiter=r"\n{2,}"): buffer_old = "" while True: buffer_new = file.read() print(type(buffer_new)) if not buffer_new: break lines = re.split(delimiter, buffer_old + buffer_new) buffer_old = lines.pop(-1) for line in lines: yield line yield buffer_old if __name__ == '__main__': d = Dummy() d.load_filename(__file__) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco