Skip to content

View Server Write Error: str, not bytes #319

Closed
@geppettodivacin

Description

@geppettodivacin

I'd been having difficulty connecting the view server with CouchDB, as the view server died before it sent a response. The logs produced the following lines again and again:

[2017-05-24 15:14:03,749] [ERROR] Error: write() argument must be str, not bytes
Traceback (most recent call last):
  File "c:\...\couchdb\view.py", line 164, in run
    _writejson(retval)
  File "c:\...\couchdb\view.py", line 55, in _writejson
    output.write(obj)
TypeError: write() argument must be str, not bytes

It appears that the write function no longer accepts bytes objects. I'm not sure when this change was made, but it's more recent than 2.7 and at least as recent as 3.5.

I verified that this was the only problem by wrapping the output stream with a class to redefine write. It works fine after this change. (Probably not the prettiest solution, but it works!)

class betterout():
    def __init__(self, out):
        self.out = out

    def __getattr__(self, attr):
        if attr == 'write':
            return self.write
        else:
            return getattr(self.out, attr)

    def write(self, text, *args, **kwargs):
        if isinstance(text, str):
            self.out.write(text, *args, **kwargs)
        else:
            self.out.write(text.decode('utf-8'), *args, **kwargs)

Has anyone else run into this problem? Is there a more standard solution to the problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions