The opener parameter of Python 3 open() built-in

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Sep 6 02:34:24 EDT 2012


On Thu, 06 Sep 2012 00:34:56 +0000, Antoine Pitrou wrote:

> Chris Angelico <rosuav <at> gmail.com> writes:
>> 
>> On Wed, Sep 5, 2012 at 5:16 AM, Terry Reedy <tjreedy <at> udel.edu>
>> wrote:
>> > io.open depends on a function the returns an open file descriptor.
>> > opener exposes that dependency so it can be replaced.
>> 
>> I skimmed the bug report comments but didn't find an answer to this:
>> Why not just monkey-patch? When a module function calls on a support
>> function and you want to change that support function's behaviour,
>> isn't monkey-patching the most usual?
> 
> Monkey-patching globals is not thread-safe: other threads will see your
> modification, which is risky and fragile.

Isn't that assuming that you don't intend the other threads to see the 
modification?

If I have two functions in my module that call "open", and I monkey-patch 
the global (module-level) name "open" to intercept that call, I don't see 
that there is more risk of breakage just because one function is called 
from a thread.

Obviously monkey-patching the builtin module itself is much riskier, 
because it doesn't just effect code in my module, it affects *everything*.



-- 
Steven




More information about the Python-list mailing list