UnsupportedOperation: fileno - How to fix this Python dependency mess?
Question: I'm building quite an extensive Python backend and things were working quite
good on server A. I then installed the system on a new (development) server B
on which I simply installed all pip packages again from scratch. Things seemed
to work fine, so I did a `pip freeze`. I then took that list and upgraded the
packages on server A.

But, as you might expect I should have known better. I didn't test things on
machine B wel enough, so I ran into a problem with Pillow version 3.0.0. So I
downgraded to version 1.7.8. That solves that single problem, bug gives me
another one:

    
    
    File "/home/kramer65/theproject/app/models/FilterResult.py", line 26, in to_json
        self.image.save(b, 'JPEG')
      File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1437, in save
        save_handler(self, fp, filename)
      File "/usr/local/lib/python2.7/dist-packages/PIL/JpegImagePlugin.py", line 471, in _save
        ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
      File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFile.py", line 476, in _save
        fh = fp.fileno()
    UnsupportedOperation: fileno
    

And here I'm kinda lost. As far as I know this is a problem in Pillow itself,
so I wouldn't know why it used to work and why it doesn't work anymore.

I searched around on the internet, but I couldn't find any solution.

Does anybody know what I could do to solve this?

ps. PIL is not installed, so it's not a collision between PIL and Pillow

[EDIT]

I just tested an `import Image` in Python (which would suggest that PIL is
still installed). To my surprise that succeeds, even though pip tells me that
it is not installed:

    
    
    $ python
    Python 2.7.6 (default, Jun 22 2015, 17:58:13)
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import Image
    >>> exit()
    $ sudo pip uninstall PIL
    The directory '/home/hielke/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
    Cannot uninstall requirement PIL, not installed
    


Answer: `BytesIO` objects raise `UnsupportedOperation` (rather than `AttributeError`
which `StringIO` does) when their `fileno` method is called that exception
wasn't handled as it should be.

This was fixed in Pillow 3.0.0 by this commit <https://github.com/python-
pillow/Pillow/commit/197885164b22f82653af514e66c76f4b778c0b1b> by catching the
exception. The following is the fix. The rest of that commit are changes to
the test suite.

In `PIL/ImageFile.py`:

    
    
     @@ -29,6 +29,7 @@
    
     import Image
     import traceback, os
    +import io
    
     MAXBLOCK = 65536
    
     @@ -475,7 +476,7 @@ def _save(im, fp, tile):
         try:
             fh = fp.fileno()
             fp.flush()
    -    except AttributeError:
    +    except (AttributeError, io.UnsupportedOperation):
             # compress to Python file-compatible object
             for e, b, o, a in tile:
                 e = Image._getencoder(im.mode, e, a, im.encoderconfig)
    

You could simply patch 1.7.8 to handle the exception.

