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