Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | #! /usr/bin/env python |
2 | """Test program for the fcntl C module. | |
3 | OS/2+EMX doesn't support the file locking operations. | |
4 | Roger E. Masse | |
5 | """ | |
6 | import struct | |
7 | import fcntl | |
8 | import os, sys | |
9 | from test.test_support import verbose, TESTFN | |
10 | ||
11 | filename = TESTFN | |
12 | ||
13 | try: | |
14 | os.O_LARGEFILE | |
15 | except AttributeError: | |
16 | start_len = "ll" | |
17 | else: | |
18 | start_len = "qq" | |
19 | ||
20 | if sys.platform.startswith('atheos'): | |
21 | start_len = "qq" | |
22 | ||
23 | if sys.platform in ('netbsd1', 'Darwin1.2', 'darwin', | |
24 | 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', 'freebsd6', | |
25 | 'bsdos2', 'bsdos3', 'bsdos4', | |
26 | 'openbsd', 'openbsd2', 'openbsd3'): | |
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, fcntl.F_WRLCK, 0) | |
34 | elif sys.platform in ['aix3', 'aix4', 'hp-uxB', 'unixware7']: | |
35 | lockdata = struct.pack('hhlllii', fcntl.F_WRLCK, 0, 0, 0, 0, 0, 0) | |
36 | elif sys.platform in ['os2emx']: | |
37 | lockdata = None | |
38 | else: | |
39 | lockdata = struct.pack('hh'+start_len+'hh', fcntl.F_WRLCK, 0, 0, 0, 0, 0) | |
40 | if lockdata: | |
41 | if verbose: | |
42 | print 'struct.pack: ', repr(lockdata) | |
43 | ||
44 | # the example from the library docs | |
45 | f = open(filename, 'w') | |
46 | rv = fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) | |
47 | if verbose: | |
48 | print 'Status from fcntl with O_NONBLOCK: ', rv | |
49 | ||
50 | if sys.platform not in ['os2emx']: | |
51 | rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata) | |
52 | if verbose: | |
53 | print 'String from fcntl with F_SETLKW: ', repr(rv) | |
54 | ||
55 | f.close() | |
56 | os.unlink(filename) | |
57 | ||
58 | ||
59 | # Again, but pass the file rather than numeric descriptor: | |
60 | f = open(filename, 'w') | |
61 | rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK) | |
62 | ||
63 | if sys.platform not in ['os2emx']: | |
64 | rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata) | |
65 | ||
66 | f.close() | |
67 | os.unlink(filename) |