/Lib/test/test_fcntl.py

http://unladen-swallow.googlecode.com/ · Python · 105 lines · 85 code · 4 blank · 16 comment · 7 complexity · 2d418ecc5f6702d42f37fa33d7d6c25c MD5 · raw file

  1. """Test program for the fcntl C module.
  2. OS/2+EMX doesn't support the file locking operations.
  3. """
  4. import fcntl
  5. import os
  6. import struct
  7. import sys
  8. import unittest
  9. from test.test_support import verbose, TESTFN, unlink, run_unittest
  10. # TODO - Write tests for flock() and lockf().
  11. def get_lockdata():
  12. if sys.platform.startswith('atheos'):
  13. start_len = "qq"
  14. else:
  15. try:
  16. os.O_LARGEFILE
  17. except AttributeError:
  18. start_len = "ll"
  19. else:
  20. start_len = "qq"
  21. if sys.platform in ('netbsd1', 'netbsd2', 'netbsd3',
  22. 'Darwin1.2', 'darwin',
  23. 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
  24. 'freebsd6', 'freebsd7', 'freebsd8',
  25. 'bsdos2', 'bsdos3', 'bsdos4',
  26. 'openbsd', 'openbsd2', 'openbsd3', 'openbsd4'):
  27. if struct.calcsize('l') == 8:
  28. off_t = 'l'
  29. pid_t = 'i'
  30. else:
  31. off_t = 'lxxxx'
  32. pid_t = 'l'
  33. lockdata = struct.pack(off_t + off_t + pid_t + 'hh', 0, 0, 0,
  34. fcntl.F_WRLCK, 0)
  35. elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']:
  36. lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0)
  37. elif sys.platform in ['os2emx']:
  38. lockdata = None
  39. else:
  40. lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
  41. if lockdata:
  42. if verbose:
  43. print 'struct.pack: ', repr(lockdata)
  44. return lockdata
  45. lockdata = get_lockdata()
  46. class TestFcntl(unittest.TestCase):
  47. def setUp(self):
  48. self.f = None
  49. def tearDown(self):
  50. if self.f and not self.f.closed:
  51. self.f.close()
  52. unlink(TESTFN)
  53. def test_fcntl_fileno(self):
  54. # the example from the library docs
  55. self.f = open(TESTFN, 'w')
  56. rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
  57. if verbose:
  58. print 'Status from fcntl with O_NONBLOCK: ', rv
  59. if sys.platform not in ['os2emx']:
  60. rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETLKW, lockdata)
  61. if verbose:
  62. print 'String from fcntl with F_SETLKW: ', repr(rv)
  63. self.f.close()
  64. def test_fcntl_file_descriptor(self):
  65. # again, but pass the file rather than numeric descriptor
  66. self.f = open(TESTFN, 'w')
  67. rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
  68. if sys.platform not in ['os2emx']:
  69. rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
  70. self.f.close()
  71. def test_fcntl_64_bit(self):
  72. # Issue #1309352: fcntl shouldn't fail when the third arg fits in a
  73. # C 'long' but not in a C 'int'.
  74. try:
  75. cmd = fcntl.F_NOTIFY
  76. # This flag is larger than 2**31 in 64-bit builds
  77. flags = fcntl.DN_MULTISHOT
  78. except AttributeError:
  79. # F_NOTIFY or DN_MULTISHOT unavailable, skipping
  80. return
  81. fd = os.open(os.path.dirname(os.path.abspath(TESTFN)), os.O_RDONLY)
  82. try:
  83. fcntl.fcntl(fd, cmd, flags)
  84. finally:
  85. os.close(fd)
  86. def test_main():
  87. run_unittest(TestFcntl)
  88. if __name__ == '__main__':
  89. test_main()