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

Side by Side 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, 9 months ago
Left:
Right:
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 unified diff | Download patch
« no previous file with comments | « Doc/library/os.rst ('k') | Modules/posixmodule.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # As a test suite for the os module, this is woefully inadequate, but this 1 # As a test suite for the os module, this is woefully inadequate, but this
2 # does add tests for a few functions which have been determined to be more 2 # does add tests for a few functions which have been determined to be more
3 # portable than they had been thought to be. 3 # portable than they had been thought to be.
4 4
5 import asynchat 5 import asynchat
6 import asyncore 6 import asyncore
7 import codecs 7 import codecs
8 import contextlib 8 import contextlib
9 import decimal 9 import decimal
10 import errno 10 import errno
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 dir_fd=None) 236 dir_fd=None)
237 os.close(f) 237 os.close(f)
238 238
239 def test_symlink_keywords(self): 239 def test_symlink_keywords(self):
240 symlink = support.get_attribute(os, "symlink") 240 symlink = support.get_attribute(os, "symlink")
241 try: 241 try:
242 symlink(src='target', dst=support.TESTFN, 242 symlink(src='target', dst=support.TESTFN,
243 target_is_directory=False, dir_fd=None) 243 target_is_directory=False, dir_fd=None)
244 except (NotImplementedError, OSError): 244 except (NotImplementedError, OSError):
245 pass # No OS support or unprivileged user 245 pass # No OS support or unprivileged user
246
247 @unittest.skipUnless(hasattr(os, 'copy_file_range'), 'test needs os.copy_fil e_range()')
248 def test_copy_file_range_ok(self):
249 TESTFN2 = support.TESTFN + ".3"
250 data = b'0123456789'
251
252 create_file(support.TESTFN, data)
253 self.addCleanup(support.unlink, support.TESTFN)
254
255 in_file = open(support.TESTFN, 'rb')
256 self.addCleanup(in_file.close)
257 in_fd = in_file.fileno()
258
259 out_file = open(TESTFN2, 'w+b')
260 self.addCleanup(support.unlink, TESTFN2)
261 self.addCleanup(out_file.close)
262 out_fd = out_file.fileno()
263
264 try:
265 i = os.copy_file_range(in_fd, out_fd, 5)
266 except OSError as e:
267 # this is needed in case Python was compiled
268 # in a system with the syscall
269 # but the system in which is run does not
270 # which is kernel dependant, for the moment
271 if e.errno != errno.ENOSYS:
272 raise
273 self.skipTest(e)
274 else:
275 self.assertIn(i, range(0, 6));
276
277 in_file = open(TESTFN2, 'rb')
278 self.assertEqual(in_file.read(), data[:i])
279
280 @unittest.skipUnless(hasattr(os, 'copy_file_range'), 'test needs os.copy_fil e_range()')
281 def test_copy_file_range_off(self):
282 TESTFN4 = support.TESTFN + ".4"
283 # ,-- in_skip
284 # v v-- in_skip+bytes_to_copy
285 data = b'0123456789'
286 bytes_to_copy = 6
287 in_skip = 3
288 out_seek = 5
289
290 create_file(support.TESTFN, data)
291 self.addCleanup(support.unlink, support.TESTFN)
292
293 in_file = open(support.TESTFN, 'rb')
294 self.addCleanup(in_file.close)
295 in_fd = in_file.fileno()
296
297 out_file = open (TESTFN4, 'w+b')
298 self.addCleanup(support.unlink, TESTFN4)
299 self.addCleanup(out_file.close)
300 out_fd = out_file.fileno()
301
302 try:
303 i = os.copy_file_range(in_fd, out_fd, bytes_to_copy,
304 in_skip, out_seek);
305 except OSError as e:
306 # this is needed in case Python was compiled
307 # in a system with the syscall
308 # but the system in which is run does not
309 # which is kernel dependant, for the moment
310 if e.errno != errno.ENOSYS:
311 raise
312 self.skipTest(e)
313 else:
314 self.assertIn(i, range(0, bytes_to_copy+1));
315
316 in_file = open(TESTFN4, 'rb')
317 read = in_file.read()
318 # seeked bytes (5) are zero'ed
319 self.assertEqual(read[:out_seek], b'\x00'*out_seek)
320 # see diagram at the begining of this test
321 self.assertEqual(read[out_seek:],
322 data[in_skip:in_skip+bytes_to_copy])
246 323
247 324
248 # Test attributes on return values from os.*stat* family. 325 # Test attributes on return values from os.*stat* family.
249 class StatAttributeTests(unittest.TestCase): 326 class StatAttributeTests(unittest.TestCase):
250 def setUp(self): 327 def setUp(self):
251 self.fname = support.TESTFN 328 self.fname = support.TESTFN
252 self.addCleanup(support.unlink, self.fname) 329 self.addCleanup(support.unlink, self.fname)
253 create_file(self.fname, b"ABC") 330 create_file(self.fname, b"ABC")
254 331
255 @unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()') 332 @unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()')
(...skipping 2886 matching lines...) Expand 10 before | Expand all | Expand 10 after
3142 for o in int, type, os, vapor(): 3219 for o in int, type, os, vapor():
3143 self.assertRaises(TypeError, os.fspath, o) 3220 self.assertRaises(TypeError, os.fspath, o)
3144 3221
3145 def test_argument_required(self): 3222 def test_argument_required(self):
3146 with self.assertRaises(TypeError): 3223 with self.assertRaises(TypeError):
3147 os.fspath() 3224 os.fspath()
3148 3225
3149 3226
3150 if __name__ == "__main__": 3227 if __name__ == "__main__":
3151 unittest.main() 3228 unittest.main()
OLDNEW
« 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+