Opened 8 years ago

Closed 8 years ago

#5416 closed defect (fixed)

FileObjectAwareFile breaking test in Python 3, doesn't know how to write unicode strings

Reported by: Christopher Allan Webber Owned by:
Priority: major Milestone: 0.9.0
Component: programming Keywords:
Cc: Parent Tickets: 5017

Description

Commit 2b4c339d very nicely fixed #647, though some of our tests write strings manually, and in Python 3, this is very specifically a bytes object, not a string object. As such, this test is breaking:

_______________________ TestDecodeRequest.test_json_type _______________________
Traceback (most recent call last):
  File "/home/cwebber/devel/mediagoblin/mediagoblin/tests/test_tools.py", line 40, in test_json_type
    data = decode_request(request)
  File "/home/cwebber/devel/mediagoblin/mediagoblin/tools/request.py", line 68, in decode_request
    data = json.loads(data)
  File "/gnu/store/awss99f2wql2h55wqjycvhnvsqpjfqsc-python-3.4.3/lib/python3.4/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

Change History (2)

comment:1 by Christopher Allan Webber, 8 years ago

This is because we now use:

class FileObjectAwareFile(io.FileIO):
    def write(self, data):
        if hasattr(data, 'read'):
            # We can call data.read(). It means that the data is a file-like
            # object, which should be saved RAM-friendly way
            shutil.copyfileobj(data, self)
        else:
            super(FileObjectAwareFile, self).write(data)

I'm not sure why it doesn't allow me to just write bytes to it though... investigating further...

comment:2 by Christopher Allan Webber, 8 years ago

Resolution: fixed
Status: newclosed

This is fixed in 64b989a

Note: See TracTickets for help on using tickets.