how to wrap file object read and write operation (which are readonly)?
<p>i am trying to wrap the read and write operation of an instance of a file object (specifically the <code>readline()</code> and <code>write()</code> methods).</p>

<p>normally, i would simply replace those functions by a wrapper, a bit like this:</p>

<pre><code>def log(stream):
    def logwrite(write):
        def inner(data):
            print 'LOG: &gt; '+data.replace('\r','&lt;cr&gt;').replace('\n','&lt;lf&gt;')
            return write(data)
        return inner
    stream.write = logwrite(stream.write)
</code></pre>

<p>but the attributes of a file object are read-only ! how could i wrap them properly ?</p>

<p>(note: i am too lazy to wrap the whole fileobject... really, i don't want to miss a feature that i did not wrap properly, or a feature which may be added in a future version of python)</p>

<p><strong>more context :</strong></p>

<p>i am trying to automate the communication with a modem, whose AT command set is made available on the network through a telnet session. once logged in, i shall "grab" the module with which i want to communicate with. after some time without activity, a timeout occurs which releases the module (so that it is available to other users on the network... which i don't care, i am the sole user of this equipment). the automatic release writes a specific line on the session.</p>

<p>i want to wrap the <code>readline()</code> on a file built from a socket (cf. <code>socket.makefile()</code>) so that when the timeout occurs, a specific exception is thrown, so that i can detect the timeout anywhere in the script and react appropriately without complicating the AT command parser... </p>

<p><em>(of course, i want to do that because the timeout is quite spurious, otherwise i would simply feed the modem with commands without any side effect only to keep the module alive)</em></p>

<p><em>(feel free to propose any other method or strategy to achieve this effect)</em></p>
