擅长:python、mysql、java
<p>您应该能够在<code>__builtin__</code>(python3.x上的<code>builtins</code>)模块上修补<code>open</code>。在</p>
<pre><code>>>> import __builtin__
>>> import mock
>>> p = mock.patch.object(__builtin__, 'open')
>>> p.start()
<MagicMock name='open' id='4331984720'>
>>> open
<MagicMock name='open' id='4331984720'>
>>> p.stop()
>>> open
<built-in function open>
</code></pre>
<p>当然,这将修补所有<em>的</em>。对于这种情况,没有一种方法可以白名单一组特定的模块。。。在</p>