| 1 |
import os |
| 2 |
import py |
| 3 |
from rpython.tool.udir import udir |
| 4 |
|
| 5 |
if os.name != 'posix': |
| 6 |
py.test.skip("fcntl module only available on unix") |
| 7 |
|
| 8 |
def teardown_module(mod): |
| 9 |
for i in "abcde": |
| 10 |
if os.path.exists(i): |
| 11 |
os.unlink(i) |
| 12 |
|
| 13 |
class AppTestFcntl: |
| 14 |
spaceconfig = dict(usemodules=('fcntl', 'array', 'struct', 'termios', |
| 15 |
'select', 'time')) |
| 16 |
|
| 17 |
def setup_class(cls): |
| 18 |
tmpprefix = str(udir.ensure('test_fcntl', dir=1).join('tmp_')) |
| 19 |
cls.w_tmp = cls.space.wrap(tmpprefix) |
| 20 |
|
| 21 |
def test_fcntl(self): |
| •22 |
import errno |
| 23 |
import fcntl |
| 24 |
import os |
| 25 |
import sys |
| 26 |
import struct |
| 27 |
|
| 28 |
class F: |
| 29 |
def __init__(self, fn): |
| 30 |
self.fn = fn |
| 31 |
def fileno(self): |
| 32 |
return self.fn |
| 33 |
|
| 34 |
f = open(self.tmp + "b", "w+") |
| 35 |
|
| 36 |
original = fcntl.fcntl(f, 1, 0) |
| 37 |
fcntl.fcntl(f, 1) |
| 38 |
fcntl.fcntl(F(int(f.fileno())), 1) |
| 39 |
raises(TypeError, fcntl.fcntl, "foo") |
| 40 |
raises(TypeError, fcntl.fcntl, f, "foo") |
| 41 |
exc = raises(TypeError, fcntl.fcntl, F("foo"), 1) |
| 42 |
assert str(exc.value) == 'fileno() returned a non-integer' |
| 43 |
exc = raises(OverflowError, fcntl.fcntl, 2147483647 + 1, 1, 0) |
| 44 |
exc = raises(OverflowError, fcntl.fcntl, F(2147483647 + 1), 1, 0) |
| 45 |
exc = raises(OverflowError, fcntl.fcntl, -2147483648 - 1, 1, 0) |
| 46 |
exc = raises(OverflowError, fcntl.fcntl, F(-2147483648 - 1), 1, 0) |
| 47 |
raises(ValueError, fcntl.fcntl, -1, 1, 0) |
| 48 |
raises(ValueError, fcntl.fcntl, F(-1), 1, 0) |
| 49 |
raises(ValueError, fcntl.fcntl, F(int(-1)), 1, 0) |
| 50 |
assert fcntl.fcntl(f, 1, 0) == original |
| 51 |
assert fcntl.fcntl(f, 2, "foo") == b"foo" |
| 52 |
assert fcntl.fcntl(f, 2, b"foo") == b"foo" |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
try: |
| 62 |
os.O_LARGEFILE |
| 63 |
except AttributeError: |
| 64 |
start_len = "ll" |
| 65 |
else: |
| 66 |
start_len = "qq" |
| 67 |
|
| 68 |
if any(substring in sys.platform.lower() |
| 69 |
for substring in ('netbsd', 'darwin', 'freebsd', 'bsdos', |
| 70 |
'openbsd')): |
| 71 |
if struct.calcsize('l') == 8: |
| 72 |
off_t = 'l' |
| 73 |
pid_t = 'i' |
| 74 |
else: |
| 75 |
off_t = 'lxxxx' |
| 76 |
pid_t = 'l' |
| 77 |
|
| 78 |
format = "%s%s%shh" % (off_t, off_t, pid_t) |
| 79 |
lockdata = struct.pack(format, 0, 0, 0, fcntl.F_WRLCK, 0) |
| 80 |
else: |
| 81 |
format = "hh%shh" % start_len |
| 82 |
lockdata = struct.pack(format, fcntl.F_WRLCK, 0, 0, 0, 0, 0) |
| 83 |
|
| 84 |
rv = fcntl.fcntl(f.fileno(), fcntl.F_SETLKW, lockdata) |
| 85 |
assert rv == lockdata |
| 86 |
assert fcntl.fcntl(f, fcntl.F_SETLKW, lockdata) == lockdata |
| 87 |
|
| 88 |
|
| 89 |
rv = fcntl.fcntl(f, fcntl.F_DUPFD) |
| 90 |
assert rv > 2 |
| 91 |
assert fcntl.fcntl(f, fcntl.F_DUPFD) > rv |
| 92 |
assert fcntl.fcntl(f, fcntl.F_DUPFD, 99) == 99 |
| 93 |
|
| 94 |
|
| 95 |
assert fcntl.fcntl(f, fcntl.F_GETFD) == 0 |
| 96 |
fcntl.fcntl(f, fcntl.F_SETFD, 1) |
| 97 |
assert fcntl.fcntl(f, fcntl.F_GETFD, fcntl.FD_CLOEXEC) == 1 |
| 98 |
|
| 99 |
|
| 100 |
assert fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) == 0 |
| 101 |
assert fcntl.fcntl(f.fileno(), fcntl.F_SETFL, os.O_NDELAY) == 0 |
| 102 |
assert fcntl.fcntl(f, fcntl.F_SETFL, os.O_NONBLOCK) == 0 |
| 103 |
assert fcntl.fcntl(f, fcntl.F_SETFL, os.O_NDELAY) == 0 |
| 104 |
|
| 105 |
if "linux" in sys.platform: |
| 106 |
|
| 107 |
assert fcntl.fcntl(f, fcntl.F_GETOWN) == 0 |
| 108 |
fcntl.fcntl(f, fcntl.F_SETOWN, os.getpid()) |
| 109 |
assert fcntl.fcntl(f, fcntl.F_GETOWN) == os.getpid() |
| 110 |
assert fcntl.fcntl(f, fcntl.F_GETSIG) == 0 |
| 111 |
fcntl.fcntl(f, fcntl.F_SETSIG, 20) |
| 112 |
assert fcntl.fcntl(f, fcntl.F_GETSIG) == 20 |
| 113 |
|
| 114 |
|
| 115 |
assert fcntl.fcntl(f, fcntl.F_GETLEASE) == fcntl.F_UNLCK |
| •116 |
try: |
| 117 |
fcntl.fcntl(f, fcntl.F_SETLEASE, fcntl.F_WRLCK) |
| 118 |
assert fcntl.fcntl(f, fcntl.F_GETLEASE) == fcntl.F_WRLCK |
| 119 |
except IOError as e: |
| 120 |
if e.errno != errno.EAGAIN: |
| 121 |
raise |
| 122 |
else: |
| 123 |
|
| 124 |
|
| 125 |
raises(IOError, fcntl.fcntl, f, fcntl.F_GETOWN) |
| 126 |
raises(IOError, fcntl.fcntl, f, fcntl.F_SETOWN, 20) |
| 127 |
|
| 128 |
f.close() |
| 129 |
|
| 130 |
def test_flock(self): |
| 131 |
import fcntl |
| 132 |
import os |
| 133 |
import errno |
| 134 |
|
| 135 |
f = open(self.tmp + "c", "w+") |
| 136 |
|
| 137 |
raises(TypeError, fcntl.flock, "foo") |
| 138 |
raises(TypeError, fcntl.flock, f, "foo") |
| 139 |
|
| 140 |
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 141 |
|
| 142 |
pid = os.fork() |
| 143 |
if pid == 0: |
| 144 |
rval = 2 |
| 145 |
try: |
| 146 |
fcntl.flock(open(f.name, f.mode), fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 147 |
except IOError as e: |
| 148 |
if e.errno not in (errno.EACCES, errno.EAGAIN): |
| 149 |
raise |
| 150 |
rval = 0 |
| 151 |
else: |
| 152 |
rval = 1 |
| 153 |
finally: |
| 154 |
os._exit(rval) |
| 155 |
|
| 156 |
assert pid > 0 |
| 157 |
(pid, status) = os.waitpid(pid, 0) |
| 158 |
assert os.WIFEXITED(status) == True |
| 159 |
assert os.WEXITSTATUS(status) == 0 |
| 160 |
|
| 161 |
fcntl.flock(f, fcntl.LOCK_UN) |
| 162 |
|
| 163 |
f.close() |
| 164 |
|
| 165 |
def test_lockf(self): |
| 166 |
import fcntl |
| 167 |
import os |
| 168 |
import errno |
| 169 |
|
| 170 |
f = open(self.tmp + "d", "w+") |
| 171 |
|
| 172 |
raises(TypeError, fcntl.lockf, f, "foo") |
| 173 |
raises(TypeError, fcntl.lockf, f, fcntl.LOCK_UN, "foo") |
| 174 |
raises(ValueError, fcntl.lockf, f, -256) |
| 175 |
raises(ValueError, fcntl.lockf, f, 256) |
| 176 |
|
| 177 |
fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 178 |
|
| 179 |
pid = os.fork() |
| 180 |
if pid == 0: |
| 181 |
rval = 2 |
| 182 |
try: |
| 183 |
fcntl.lockf(open(f.name, f.mode), fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 184 |
except IOError as e: |
| 185 |
if e.errno not in (errno.EACCES, errno.EAGAIN): |
| 186 |
raise |
| 187 |
rval = 0 |
| 188 |
else: |
| 189 |
rval = 1 |
| 190 |
finally: |
| 191 |
os._exit(rval) |
| 192 |
|
| 193 |
assert pid > 0 |
| 194 |
(pid, status) = os.waitpid(pid, 0) |
| 195 |
assert os.WIFEXITED(status) == True |
| 196 |
assert os.WEXITSTATUS(status) == 0 |
| 197 |
|
| 198 |
fcntl.lockf(f, fcntl.LOCK_UN) |
| 199 |
|
| 200 |
f.close() |
| 201 |
|
| 202 |
def test_ioctl(self): |
| 203 |
import fcntl |
| 204 |
import array |
| 205 |
import os |
| 206 |
import pty |
| 207 |
import time |
| 208 |
|
| 209 |
try: |
| 210 |
from termios import TIOCGPGRP |
| 211 |
except ImportError: |
| 212 |
skip("don't know how to test ioctl() on this platform") |
| 213 |
|
| 214 |
raises(TypeError, fcntl.ioctl, "foo") |
| 215 |
raises(TypeError, fcntl.ioctl, 0, "foo") |
| 216 |
|
| 217 |
raises(TypeError, fcntl.ioctl, 0, TIOCGPGRP, 1, "foo", "bar") |
| 218 |
|
| 219 |
child_pid, mfd = pty.fork() |
| 220 |
if child_pid == 0: |
| 221 |
|
| 222 |
time.sleep(1) |
| 223 |
os._exit(0) |
| 224 |
try: |
| 225 |
|
| 226 |
time.sleep(0.5) |
| 227 |
|
| 228 |
buf = array.array('i', [0]) |
| 229 |
res = fcntl.ioctl(mfd, TIOCGPGRP, buf, True) |
| 230 |
assert res == 0 |
| 231 |
assert buf[0] != 0 |
| 232 |
expected = buf.tobytes() |
| 233 |
|
| 234 |
buf = array.array('i', [0]) |
| 235 |
res = fcntl.ioctl(mfd, TIOCGPGRP, buf) |
| 236 |
assert res == 0 |
| 237 |
assert buf.tobytes() == expected |
| 238 |
|
| 239 |
buf = array.array('i', [0]) |
| 240 |
res = fcntl.ioctl(mfd, TIOCGPGRP, memoryview(buf)) |
| 241 |
assert res == 0 |
| 242 |
assert buf.tobytes() == expected |
| 243 |
|
| 244 |
raises(TypeError, fcntl.ioctl, mfd, TIOCGPGRP, (), False) |
| 245 |
|
| 246 |
res = fcntl.ioctl(mfd, TIOCGPGRP, buf, False) |
| 247 |
assert res == expected |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
res = fcntl.ioctl(mfd, TIOCGPGRP, "\x00\x00\x00\x00") |
| 253 |
assert res == expected |
| 254 |
finally: |
| 255 |
os.close(mfd) |
| 256 |
|
| 257 |
def test_ioctl_int(self): |
| 258 |
import os |
| 259 |
import fcntl |
| 260 |
import pty |
| 261 |
|
| 262 |
try: |
| 263 |
from termios import TCFLSH, TCIOFLUSH |
| 264 |
except ImportError: |
| 265 |
skip("don't know how to test ioctl() on this platform") |
| 266 |
|
| 267 |
mfd, sfd = pty.openpty() |
| 268 |
try: |
| 269 |
assert fcntl.ioctl(mfd, TCFLSH, TCIOFLUSH) == 0 |
| 270 |
finally: |
| 271 |
os.close(mfd) |
| 272 |
os.close(sfd) |
| 273 |
|
| 274 |
def test_ioctl_signed_unsigned_code_param(self): |
| 275 |
import fcntl |
| 276 |
import os |
| 277 |
import pty |
| 278 |
import struct |
| 279 |
import termios |
| 280 |
|
| 281 |
mfd, sfd = pty.openpty() |
| 282 |
try: |
| 283 |
if termios.TIOCSWINSZ < 0: |
| 284 |
set_winsz_opcode_maybe_neg = termios.TIOCSWINSZ |
| 285 |
set_winsz_opcode_pos = termios.TIOCSWINSZ & 0xffffffff |
| 286 |
else: |
| 287 |
set_winsz_opcode_pos = termios.TIOCSWINSZ |
| 288 |
set_winsz_opcode_maybe_neg, = struct.unpack("i", |
| 289 |
struct.pack("I", termios.TIOCSWINSZ)) |
| 290 |
|
| 291 |
our_winsz = struct.pack("HHHH",80,25,0,0) |
| 292 |
|
| 293 |
new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_pos, our_winsz) |
| 294 |
new_winsz = fcntl.ioctl(mfd, set_winsz_opcode_maybe_neg, our_winsz) |
| 295 |
finally: |
| 296 |
os.close(mfd) |
| 297 |
os.close(sfd) |
| 298 |
|
| 299 |
def test_ioctl_use_mask_on_op(self): |
| 300 |
import os |
| 301 |
import fcntl |
| 302 |
import pty |
| 303 |
try: |
| 304 |
from termios import TCFLSH, TCIOFLUSH |
| 305 |
except ImportError: |
| 306 |
skip("don't know how to test ioctl() on this platform") |
| 307 |
|
| 308 |
mfd, sfd = pty.openpty() |
| 309 |
try: |
| 310 |
assert fcntl.ioctl(mfd, TCFLSH | 0x1000000000000000000000, TCIOFLUSH) == 0 |
| 311 |
finally: |
| 312 |
os.close(mfd) |
| 313 |
os.close(sfd) |
| 314 |
|
| 315 |
def test_large_flag(self): |
| 316 |
import sys |
| 317 |
if any(plat in sys.platform |
| 318 |
for plat in ('darwin', 'openbsd', 'freebsd')): |
| 319 |
skip("Mac OS doesn't have any large flag in fcntl.h") |
| 320 |
import fcntl, sys |
| 321 |
if sys.maxsize == 2147483647: |
| 322 |
assert fcntl.DN_MULTISHOT == -2147483648 |
| 323 |
else: |
| 324 |
assert fcntl.DN_MULTISHOT == 2147483648 |
| 325 |
fcntl.fcntl(0, fcntl.F_NOTIFY, fcntl.DN_MULTISHOT) |