我正在尝试修补shutil.copyfileobj()函数,以便将其默认长度从16*1024更改为更大的值(128*1024)。在内部,其他的shutil方法,如move,会调用copyfileobj()函数,我希望这些调用也会受到猴子补丁的影响。这似乎不起作用:
import shutil
shutil.copyfileobjOrig = shutil.copyfileobj
def copyfileobjFast(fsrc, fdst, length=16*1024):
print('COPYING FILE FAST')
shutil.copyfileobjOrig(fsrc, fdst, length=128*1024)
shutil.copyfileobj = copyfileobjFast
shutil.move('test.txt', 'testmove.txt')我希望它能打印出“快速复制文件”,但它没有。有什么方法可以实现我想要做的事情吗?
发布于 2017-11-13 20:59:00
我的测试用例坏了。仅当源文件和目标文件位于不同的设备上时,shutil.move()才执行复制。这是一个更新的版本,显示了猴子补丁的工作情况:
import shutil
shutil.copyfileobjOrig = shutil.copyfileobj
def copyfileobjFast(fsrc, fdst, length=16*1024):
print('COPYING FILE FAST')
shutil.copyfileobjOrig(fsrc, fdst, length=128*1024)
shutil.copyfileobj = copyfileobjFast
shutil.move('/dev1/test.txt', '/dev2/testmove.txt')https://stackoverflow.com/questions/47257033
复制相似问题