Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing file by subclassing `io.TextIOWrapper` — but what signature does its constructor have?

I'm trying to subclass io.TextIOWrapper following this post, although my aims are different. Starting off with this (NB: motivation):

class MyTextIOFile(io.TextIOWrapper):
    def read(self, *args):
        cont = super().read(*args)
        return cont.replace("\x00", "")

I'm trying to open a file using my constructor using

In [81]: f = MyTextIOFile("file.csv")

but this gives:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-90-343e18b2e32f> in <module>()
----> 1 f = MyTextIOFile("file.csv")

AttributeError: 'str' object has no attribute 'readable'

And indeed, it appears io.TextIOWrappers constructor expects to be passed a file object. Through trial and error, I discovered this file object needs to be opened in binary mode. But I can't find documentation anywhere, and I don't feel like building on top of undocumented behaviour (indeed, one attempt to go ahead with it already lead me to problems when trying to pass my object to csv.reader). What is the correct and supported way to subclass a file object in Python 3?

I'm using Python 3.5.0.

like image 795
gerrit Avatar asked Oct 15 '15 18:10

gerrit


1 Answers

I think the documentation you are looking for is

class io.TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False)
    A buffered text stream over a BufferedIOBase binary stream. [...]

The first argument is a binary stream, which implies something opened in binary mode by open.

like image 89
chepner Avatar answered Oct 04 '22 04:10

chepner