5

I have a legacy code that calls class TiffFile(file). What is the python3 way to call it?

I tried to replace following in python2:

class TiffFile(file):
    def __init__(self, path):
        file.__init__(self, path, 'r+b')

By this in python3:

class TiffFile(RawIOBase):
    def __init__(self, path):
        super(TiffFile, self).__init__(path, 'r+b')

But now I am getting TypeError: object.__init__() takes no parameters

Dima Lituiev
  • 12,544
  • 10
  • 41
  • 58

1 Answers1

5

RawIOBase.__init__ does not take any arguments, that is where you error is.

Your TiffFile implementation also inherits file which is not a a class, but a constructor function, so your Python 2 implementation is non-idiomatic, and someone could even claim it is wrong-ish. You should use open instead of file, and in a class context you should use an io module class for input and output.

You can use open to return a file object for use as you would use file in Python 2.7 or you can use io.FileIO in both Python 2 and Python 3 for accessing file streams, like you would do with open.

So your implementation would be more like:

import io

class TiffFile(io.FileIO):
    def __init__(self, name, mode='r+b', *args, **kwargs):
        super(TiffFile, self).__init__(name, mode, *args, **kwargs)

This should work in all currently supported Python versions and allow you the same interface as your old implementation while being more correct and portable.

Are you actually using r+b for opening the file in read-write-binary mode on Windows? You should probably be using rb mode if you are not writing into the file, but just reading the TIFF data. rb would open the file in binary mode for reading only. The appended + sets the file to open in read-write mode.

Aleksi Häkli
  • 1,441
  • 9
  • 9
  • I am not sure the meaning of 'r+b', it is an older piece of code I want to call from Python3. It does write back into the file it opens. – Dima Lituiev Dec 16 '17 at 04:28
  • 1
    `r` mode opens file in read and `r+` opens it in read-write, `r+b` opens it in read-write-binary mode, so it reads and writes in binary. Binary mode is appropriate for image, video and music files, which don't have text encoded data. Usually it is best to open file once for reading it and then write new data into another file to avoid mutating the file while possibly reading it, which can create very hard-to-debug side-effects. – Aleksi Häkli Dec 16 '17 at 10:47
  • this package writes stuff back by design, as files are huge. i won't mess with it for now – Dima Lituiev Dec 17 '17 at 00:53