[Python-ideas] Hooks into the IO system to intercept raw file reads/writes

Paul Moore p.f.moore at gmail.com
Mon Feb 2 15:53:36 CET 2015


There's a lot of flexibility in the new layered IO system, but one
thing it doesn't allow is any means of adding "hooks" to the data
flow, or manipulation of an already-created io object.

For example, when a subprocess.Popen object uses a pipe for the
child's stdout, the data is captured instead of writing it to the
console. Sometimes it would be nice to capture it, but still write to
the console. That would be easy to do if we could wrap the underlying
RawIOBase object and intercept read() calls[1]. A subclass of
RawIOBase can do this trivially, but there's no way of replacing the
class on an existing stream.

The obvious approach would be to reassign the "raw" attribute of the
BufferedIOBase object, but that's readonly. Would it be possible to
make it read/write? Or provide another way of replacing the raw IO
object underlying an io object?

I'm sure there are buffer integrity issues to work out, but are there
any more fundamental problems with this approach?

Paul

[1] Actually, it's *not* that easy, because subprocess.Popen objects
are insanely hard to subclass - there are no hooks into the pipe
creation process, and no way to intercept the object before the
subprocess gets run (that happens in the __init__ method). But that's
a separate issue, and also the subject of a different thread here.


More information about the Python-ideas mailing list