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

Paul Moore p.f.moore at gmail.com
Mon Feb 2 19:19:12 CET 2015


On 2 February 2015 at 18:07, Andrew Barnert <abarnert at yahoo.com> wrote:
> On Feb 2, 2015, at 6:53, Paul Moore <p.f.moore at gmail.com> wrote:
>
>> 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.
>
> Why do you need to add hooks to an already-created object? Why not just create the subclassed (and effectively hooked) object in place of the original one? Obviously that requires building the stack of raw/buffered/text manually instead, which requires a few extra lines in subprocess or wherever else you want to do it, but that doesn't seem like a huge burden for something this uncommon.

Largely because the IO object creation is hidden in the bowels of
subprocess, to be honest. There's no doubt that the underlying problem
is that subprocess is hostile to any form of subclassing or
modification. And given that I have this problem in existing code,
anything proposed here (which will be for 3.5 or maybe 3.6 at best)
isn't going to be a solution for my immediate problem.

My intention here was that, having had this sort of issue a few times
(all variations on "I wish I could see when the IO object requests
data from the OS, and do something at that point with the data the OS
supplied") I thought there might be a good case for a general facility
to do this, The feature was also available in the old AT&T "sfio"
library which was a more flexible replacement for C's stdio, so it's
not a new idea.

Paul


More information about the Python-ideas mailing list