[py-dev] Advanced monkeypatching?

Virgil Dupras hsoft at hardcoded.net
Mon Dec 20 10:56:33 CET 2010


On 2010-12-20, at 10:41 AM, Ronny Pfannschmidt wrote:
> On Mon, 2010-12-20 at 09:22 +0100, Virgil Dupras wrote:
>> On 2010-12-19, at 3:38 PM, Floris Bruynooghe wrote:
>>> 
>>> Looking at the patch_osstat() it seems to me that it just fakes
>>> os.stat() for one specific path but uses the original in place
>>> otherwise.  This sounds like it could be just a specialised object
>>> used to patch with.  I can imagine the monkeypatch plugin to provide a
>>> funcarg which provides this functionality, e.g.:
>>> 
>>> def test_foo(monkeypatch, monkey_osstat):
>>>   monkeypatch.setattr(monkey_ossstat('/tmp/some_file'))
>>>   ...
>>> 
>> 
>> This would kind of work, but it would become complicated to patch os.stat() for two or more files, wouldn't it?
> there could be a monkey_stat object that controlls the set of
> files/subtrees under control
> 

Yes, of course, but keeping the proposed API would make it unintuitive for multiple files. Example:

monkeypatch.setattr(os, 'stat', monkey_osstat('some_file'))
monkeypatch.setattr(os, 'stat', monkey_osstat('some_other_file'))

We're not sure what happens there, as os.stat is patched twice. Are we overwriting our old monkeypatch, or is there some magic caching inside monkey_osstat() making it work for both files? That's the kind of question the developer will wonder about.


More information about the Pytest-dev mailing list