@@ -1936,35 +1936,34 @@ def test_file_offset(self):
19361936 self .assertEqual (dst .tell (), self .FILESIZE )
19371937
19381938 @unittest .skipIf (os .name != 'nt' , "Windows only" )
1939- def test_alternate_win_impl (self ):
1940- # On Windows copyfile() uses copyfileobj() for files < 128 MiB,
1941- # else an alternate memoryview()-based implementation.
1942- def os_stat_mocked (path ):
1943- # Make shutil believe src file is 128 MiB.
1944- if path == TESTFN :
1945- mock = unittest .mock .Mock ()
1946- mock .st_size = 128 * 1024 * 1024
1947- mock .st_mode = os .lstat (path ).st_mode
1948- return mock
1949- else :
1950- return os .lstat (path )
1951-
1952- # Make sure it's not called.
1953- with unittest .mock .patch ("shutil._copybinfileobj" ) as m :
1939+ def test_win_impl (self ):
1940+ # Make sure alternate Windows implementation is called.
1941+ with unittest .mock .patch ("shutil._copyfileobj_readinto" ) as m :
19541942 shutil .copyfile (TESTFN , TESTFN2 )
1955- assert not m .called
1956- # Make sure it's called.
1957- with unittest .mock .patch ('shutil.os.stat' , create = True ,
1958- side_effect = os_stat_mocked ) as m1 :
1959- with unittest .mock .patch ("shutil._copybinfileobj" ) as m2 :
1960- shutil .copyfile (TESTFN , TESTFN2 )
1961- assert m1 .called
1962- assert m2 .called
1963- # Test it.
1964- with unittest .mock .patch ('shutil.os.stat' , create = True ,
1965- side_effect = os_stat_mocked ) as m1 :
1966- shutil .copyfile (TESTFN , TESTFN2 )
1967- self .assert_files_eq (TESTFN , TESTFN2 )
1943+ assert m .called
1944+
1945+ # File size is 2 MiB but max buf size should be 1 MiB.
1946+ self .assertEqual (m .call_args [0 ][2 ], 1 * 1024 * 1024 )
1947+
1948+ # If file size < 1 MiB memoryview() length must be equal to
1949+ # the actual file size.
1950+ with tempfile .NamedTemporaryFile (delete = False ) as f :
1951+ f .write (b'foo' )
1952+ fname = f .name
1953+ self .addCleanup (support .unlink , fname )
1954+ with unittest .mock .patch ("shutil._copyfileobj_readinto" ) as m :
1955+ shutil .copyfile (fname , TESTFN2 )
1956+ self .assertEqual (m .call_args [0 ][2 ], 3 )
1957+
1958+ # Empty files should not rely on readinto() variant.
1959+ with tempfile .NamedTemporaryFile (delete = False ) as f :
1960+ pass
1961+ fname = f .name
1962+ self .addCleanup (support .unlink , fname )
1963+ with unittest .mock .patch ("shutil._copyfileobj_readinto" ) as m :
1964+ shutil .copyfile (fname , TESTFN2 )
1965+ assert not m .called
1966+ self .assert_files_eq (fname , TESTFN2 )
19681967
19691968
19701969class _ZeroCopyFileTest (object ):
0 commit comments