Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24467

Frustrating circular bytes issue

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <dreadpiratejeff@gmail.com>
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; 'output': 0.05; 'attribute': 0.07; 'matches': 0.07; 'method.': 0.07; '%s",': 0.09; '*args,': 0.09; 'executes': 0.09; 'filename': 0.09; 'function:': 0.09; 'lines:': 0.09; 'method,': 0.09; 'method:': 0.09; 'python': 0.10; 'def': 0.11; 'bug': 0.11; 'read.': 0.13; '**kwargs)': 0.16; '39,': 0.16; 'command,': 0.16; 'complaining': 0.16; 'dislike': 0.16; 'file.read()': 0.16; 'filename)': 0.16; 'filename):': 0.16; 'interprets': 0.16; 'object).': 0.16; 'python3': 0.16; 'read()': 0.16; 'received:209.85.161.174': 0.16; 'scripts.': 0.16; 'stepping': 0.16; 'string:': 0.16; 'subject:issue': 0.16; 'to:name:python list': 0.16; 'true:': 0.16; 'typeerror:': 0.16; '(the': 0.17; 'file,': 0.18; 'passing': 0.18; '(in': 0.20; 'creates': 0.20; 'string': 0.22; 'byte': 0.22; 'bytes': 0.22; 'element': 0.22; 'tells': 0.22; 'yield': 0.22; 'elements': 0.23; 'this:': 0.23; 'print': 0.25; 'message-id:@mail.gmail.com': 0.27; 'important.': 0.27; 'see,': 0.27; 'statement': 0.27; 'error': 0.28; 'common': 0.28; 'skip:" 20': 0.28; 'skip:s 30': 0.28; 'converted': 0.29; 'object.': 0.29; 'really,': 0.29; 'starts': 0.29; 'class': 0.30; '(most': 0.30; "i'm": 0.30; 'skip:_ 10': 0.31; 'received:209.85.161': 0.32; 'lines': 0.32; 'convert': 0.32; 'attempt': 0.33; 'in.': 0.33; 'skip:" 50': 0.33; 'running': 0.34; 'file': 0.34; 'skip:b 20': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'along': 0.35; 'problem.': 0.35; 'another': 0.35; 'received:209.85': 0.35; 'to:addr:python-list': 0.35; 'skip:d 20': 0.36; 'something': 0.36; 'really': 0.36; 'test': 0.36; 'but': 0.36; 'received:209': 0.37; 'some': 0.38; 'things': 0.38; 'method': 0.39; 'to:addr:python.org': 0.39; 'use,': 0.39; 'starting': 0.39; 'header:Received:5': 0.39; 'called': 0.40; 'where': 0.40; 'telling': 0.61; 'further': 0.62; 'calls': 0.62; 'remove': 0.63; 'more': 0.63; 'our': 0.64; 'circle': 0.66; 'records': 0.69; '88,': 0.84; 'object:': 0.84; 'thing...': 0.84; 'do:': 0.91; 'last):': 0.91; 'messages:': 0.91; 'str.': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=82ahEow/V9PdbOM79/HJVSHXXMqsQg1b1VzwN2g29lE=; b=w4CWpWDd7onF0ax3lPJeoZP534QzoFrnx2HYyp2CAZi8pjXGyHrLBwMfGvSjL+7JQ6 f6XBzWscQcCp/yMMwxbs1GNDI832cQ5y0l/ZDkq9sDugpo3llZmcbd26kP+ruge03zPB wV2le2X/nWCLZ2nXy/gG43nSqCGA2CdMtjvcs+pOPyWh/xOt1IlSh2oX3aF4GXEKRiMu YycP2vSRDMudQe/1qr9r0JtTxCrg47wKc5VAU62zZaXy9OYWuaAOxaSQLgyylMjSU8kv f5UuvH8RTS/sSL/E8Z46xsXSJz77GJPSDoC0tV3ZiKMO5JsxtxCqeH/8QXXqy0qFfr8D Yhuw==
MIME-Version 1.0
From J <dreadpiratejeff@gmail.com>
Date Tue, 26 Jun 2012 12:30:15 -0400
Subject Frustrating circular bytes issue
To Python List <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1518.1340728239.4697.python-list@python.org> (permalink)
Lines 120
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1340728239 news.xs4all.nl 6973 [2001:888:2000:d::a6]:49888
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:24467

Show key headers only | View raw


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)

As you can see, this calls the load_file method, by passing the
filehandle and filename (in common use, filename is actually an
IOStream object).

load_file starts out like this:


def load_file(self, file, filename="<stream>"):
        elements = []
        for string in self._reader(file):
            if not string:
                break

            element = {}


Note that it now calls the private _reader() passing along the
filehandle further in.  THIS is where I'm failing:

This is the private _reader function:


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


(the print statement is something I put in to verify the problem.

So stepping through this, when _reader executes, it executes read() on
the opened filehandle.  Originally, it read in 4096 byte chunks, I
removed that to test a theory.  It creates buffer_new with the output
of the read.

Running type() on buffer_new tells me that it's a bytes object.

However no matter what I do:

file.read().decode()
buffer_new.decode() in the lines = re.split() statement
buffer_str = buffer_new.decode()

I always get a traceback telling me that the str object has no decoe() method.

If I remove the decode attempts, I get a traceback telling me that it
can't implicitly convert a bytes_object to a str object.

So I'm stuck in a vicious circle and can't see a way out.

here's sample error messages:
When using the decode() method to attempt to convert the bytes object:
Traceback (most recent call last):
  File "./filter_templates", line 134, in <module>
    sys.exit(main(sys.argv[1:]))
  File "./filter_templates", line 126, in main
    options.whitelist, options.blacklist)
  File "./filter_templates", line 77, in parse_file
    matches = match_elements(template.load_file(file), *args, **kwargs)
  File "/usr/lib/python3/dist-packages/checkbox/lib/template.py", line
73, in load_file
    for string in self._reader(file):
  File "/usr/lib/python3/dist-packages/checkbox/lib/template.py", line
35, in _reader
    lines = re.split(delimiter, buffer_old + buffer_new.decode())
AttributeError: 'str' object has no attribute 'decode'

It's telling me that buffer_new is a str object.

so if I remove the decode():

Traceback (most recent call last):
  File "./run_templates", line 142, in <module>
    sys.exit(main(sys.argv[1:]))
  File "./run_templates", line 137, in main
    runner.process(args, options.shell)
  File "./run_templates", line 39, in process
    records = self.process_output(process.stdout)
  File "./run_templates", line 88, in process_output
    return template.load_file(output)
  File "/usr/lib/python3/dist-packages/checkbox/lib/template.py", line
73, in load_file
    for string in self._reader(file):
  File "/usr/lib/python3/dist-packages/checkbox/lib/template.py", line
35, in _reader
    lines = re.split(delimiter, buffer_old + buffer_new)
TypeError: Can't convert 'bytes' object to str implicitly

now it's complaining that buffer_new is a bytes object and can't be
implicitly converted to str.

This is a bug introduced in our conversion from Python 2 to Python 3.
I am really, really starting to dislike some of the things Python3
does... or just am really, really frustrated.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Frustrating circular bytes issue J <dreadpiratejeff@gmail.com> - 2012-06-26 12:30 -0400
  Re: Frustrating circular bytes issue Hans Mulder <hansmu@xs4all.nl> - 2012-06-26 19:33 +0200

csiph-web