Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(12)

Unified Diff: Lib/test/test_os.py

Issue 26826: Expose new copy_file_range() syscal in os module and use it to improve shutils.copy()
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Doc/library/os.rst ('k') | Modules/posixmodule.c » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/test/test_os.py Wed Jun 08 13:32:49 2016 +0000
+++ b/Lib/test/test_os.py Wed Jul 06 12:59:50 2016 +0200
@@ -244,6 +244,83 @@
except (NotImplementedError, OSError):
pass # No OS support or unprivileged user
+ @unittest.skipUnless(hasattr(os, 'copy_file_range'), 'test needs os.copy_file_range()')
+ def test_copy_file_range_ok(self):
+ TESTFN2 = support.TESTFN + ".3"
+ data = b'0123456789'
+
+ create_file(support.TESTFN, data)
+ self.addCleanup(support.unlink, support.TESTFN)
+
+ in_file = open(support.TESTFN, 'rb')
+ self.addCleanup(in_file.close)
+ in_fd = in_file.fileno()
+
+ out_file = open(TESTFN2, 'w+b')
+ self.addCleanup(support.unlink, TESTFN2)
+ self.addCleanup(out_file.close)
+ out_fd = out_file.fileno()
+
+ try:
+ i = os.copy_file_range(in_fd, out_fd, 5)
+ except OSError as e:
+ # this is needed in case Python was compiled
+ # in a system with the syscall
+ # but the system in which is run does not
+ # which is kernel dependant, for the moment
+ if e.errno != errno.ENOSYS:
+ raise
+ self.skipTest(e)
+ else:
+ self.assertIn(i, range(0, 6));
+
+ in_file = open(TESTFN2, 'rb')
+ self.assertEqual(in_file.read(), data[:i])
+
+ @unittest.skipUnless(hasattr(os, 'copy_file_range'), 'test needs os.copy_file_range()')
+ def test_copy_file_range_off(self):
+ TESTFN4 = support.TESTFN + ".4"
+ # ,-- in_skip
+ # v v-- in_skip+bytes_to_copy
+ data = b'0123456789'
+ bytes_to_copy = 6
+ in_skip = 3
+ out_seek = 5
+
+ create_file(support.TESTFN, data)
+ self.addCleanup(support.unlink, support.TESTFN)
+
+ in_file = open(support.TESTFN, 'rb')
+ self.addCleanup(in_file.close)
+ in_fd = in_file.fileno()
+
+ out_file = open (TESTFN4, 'w+b')
+ self.addCleanup(support.unlink, TESTFN4)
+ self.addCleanup(out_file.close)
+ out_fd = out_file.fileno()
+
+ try:
+ i = os.copy_file_range(in_fd, out_fd, bytes_to_copy,
+ in_skip, out_seek);
+ except OSError as e:
+ # this is needed in case Python was compiled
+ # in a system with the syscall
+ # but the system in which is run does not
+ # which is kernel dependant, for the moment
+ if e.errno != errno.ENOSYS:
+ raise
+ self.skipTest(e)
+ else:
+ self.assertIn(i, range(0, bytes_to_copy+1));
+
+ in_file = open(TESTFN4, 'rb')
+ read = in_file.read()
+ # seeked bytes (5) are zero'ed
+ self.assertEqual(read[:out_seek], b'\x00'*out_seek)
+ # see diagram at the begining of this test
+ self.assertEqual(read[out_seek:],
+ data[in_skip:in_skip+bytes_to_copy])
+
# Test attributes on return values from os.*stat* family.
class StatAttributeTests(unittest.TestCase):
« no previous file with comments | « Doc/library/os.rst ('k') | Modules/posixmodule.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+