test_fcntl.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """Test program for the fcntl C module.
  2. OS/2+EMX doesn't support the file locking operations.
  3. """
  4. import os
  5. import struct
  6. import sys
  7. import unittest
  8. from test.test_support import (verbose, TESTFN, unlink, run_unittest,
  9. import_module, cpython_only)
  10. # Skip test if no fcntl module.
  11. fcntl = import_module('fcntl')
  12. # TODO - Write tests for flock() and lockf().
  13. def get_lockdata():
  14. if sys.platform.startswith('atheos'):
  15. start_len = "qq"
  16. else:
  17. try:
  18. os.O_LARGEFILE
  19. except AttributeError:
  20. start_len = "ll"
  21. else:
  22. start_len = "qq"
  23. if (sys.platform.startswith(('netbsd', 'freebsd', 'openbsd', 'bsdos'))
  24. or sys.platform == 'darwin'):
  25. if struct.calcsize('l') == 8:
  26. off_t = 'l'
  27. pid_t = 'i'
  28. else:
  29. off_t = 'lxxxx'
  30. pid_t = 'l'
  31. lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
  32. fcntl.F_WRLCK, 0)
  33. elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
  34. lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
  35. elif sys.platform in ['os2emx']:
  36. lockdata = None
  37. else:
  38. lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
  39. if lockdata:
  40. if verbose:
  41. print 'struct.pack: ', repr(lockdata)
  42. return lockdata
  43. lockdata = get_lockdata()
  44. class BadFile:
  45. def __init__(self, fn):
  46. self.fn = fn
  47. def fileno(self):
  48. return self.fn
  49. class TestFcntl(unittest.TestCase):
  50. def setUp(self):
  51. self.f = None
  52. def tearDown(self):
  53. if self.f and not self.f.closed:
  54. self.f.close()
  55. unlink(TESTFN)
  56. def test_fcntl_fileno(self):
  57. # the example from the library docs
  58. self.f = open(TESTFN, 'w')
  59. rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
  60. if verbose:
  61. print 'Status from fcntl with O_NONBLOCK: ', rv
  62. if sys.platform not in ['os2emx']:
  63. rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
  64. if verbose:
  65. print 'String from fcntl with F_SETLKW: ', repr(rv)
  66. self.f.close()
  67. def test_fcntl_file_descriptor(self):
  68. # again, but pass the file rather than numeric descriptor
  69. self.f = open(TESTFN, 'w')
  70. rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
  71. if sys.platform not in ['os2emx']:
  72. rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
  73. self.f.close()
  74. def test_fcntl_bad_file(self):
  75. with self.assertRaises(ValueError):
  76. fcntl.fcntl(-1, fcntl.F_SETFL, os.O_NONBLOCK)
  77. with self.assertRaises(ValueError):
  78. fcntl.fcntl(BadFile(-1), fcntl.F_SETFL, os.O_NONBLOCK)
  79. with self.assertRaises(TypeError):
  80. fcntl.fcntl('spam', fcntl.F_SETFL, os.O_NONBLOCK)
  81. with self.assertRaises(TypeError):
  82. fcntl.fcntl(BadFile('spam'), fcntl.F_SETFL, os.O_NONBLOCK)
  83. @cpython_only
  84. def test_fcntl_bad_file_overflow(self):
  85. from _testcapi import INT_MAX, INT_MIN
  86. # Issue 15989
  87. with self.assertRaises(ValueError):
  88. fcntl.fcntl(INT_MAX + 1, fcntl.F_SETFL, os.O_NONBLOCK)
  89. with self.assertRaises(ValueError):
  90. fcntl.fcntl(BadFile(INT_MAX + 1), fcntl.F_SETFL, os.O_NONBLOCK)
  91. with self.assertRaises(ValueError):
  92. fcntl.fcntl(INT_MIN - 1, fcntl.F_SETFL, os.O_NONBLOCK)
  93. with self.assertRaises(ValueError):
  94. fcntl.fcntl(BadFile(INT_MIN - 1), fcntl.F_SETFL, os.O_NONBLOCK)
  95. def test_fcntl_64_bit(self):
  96. # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
  97. # C 'long' but not in a C 'int'.
  98. try:
  99. cmd = fcntl.F_NOTIFY
  100. # This flag is larger than 2**31 in 64-bit builds
  101. flags = fcntl.DN_MULTISHOT
  102. except AttributeError:
  103. self.skipTest("F_NOTIFY or DN_MULTISHOT unavailable")
  104. fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
  105. try:
  106. # This will raise OverflowError if issue1309352 is present.
  107. fcntl.fcntl(fd, cmd, flags)
  108. except IOError:
  109. pass # Running on a system that doesn't support these flags.
  110. finally:
  111. os.close(fd)
  112. def test_main():
  113. run_unittest(TestFcntl)
  114. if __name__ == '__main__':
  115. test_main()