| 1 |
|
| 2 |
|
| 3 |
import os |
| 4 |
import py |
| 5 |
import pytest |
| 6 |
import sys |
| 7 |
import signal |
| 8 |
|
| 9 |
from rpython.tool.udir import udir |
| 10 |
from pypy.interpreter.gateway import interp2app |
| 11 |
from rpython.translator.c.test.test_extfunc import need_sparse_files |
| 12 |
from rpython.rlib import rposix |
| 13 |
|
| 14 |
USEMODULES = ['binascii', 'posix', 'signal', 'struct', 'time', '_socket'] |
| 15 |
|
| 16 |
def setup_module(mod): |
| 17 |
mod.path = udir.join('posixtestfile.txt') |
| 18 |
mod.path.write("this is a test") |
| 19 |
mod.path2 = udir.join('test_posix2-') |
| 20 |
mod.path3 = udir.join('unlinktestfile.txt') |
| 21 |
mod.path3.write("delete me!") |
| 22 |
pdir = udir.ensure('posixtestdir', dir=True) |
| 23 |
pdir = udir.ensure('posixtestdir', dir=True) |
| 24 |
pdir.join('file1').write("test1") |
| 25 |
os.chmod(str(pdir.join('file1')), 0o600) |
| 26 |
pdir.join('file2').write("test2") |
| 27 |
pdir.join('another_longer_file_name').write("test3") |
| 28 |
mod.pdir = pdir |
| 29 |
if sys.platform in ('darwin', 'win32'): |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
bytes_dir = udir.ensure('fixc5x9fier.txt', dir=True) |
| 34 |
file_name = 'cafxe9' |
| 35 |
surrogate_name = 'foo' |
| 36 |
else: |
| 37 |
bytes_dir = udir.ensure(b'fi\xc5\x9fier.txt', dir=True) |
| 38 |
file_name = b'caf\xe9' |
| 39 |
surrogate_name = b'foo\x80' |
| 40 |
bytes_dir.join('somefile').write('who cares?') |
| 41 |
bytes_dir.join(file_name).write('who knows?') |
| 42 |
mod.bytes_dir = bytes_dir |
| 43 |
|
| 44 |
mod.esurrogate_dir = udir.ensure(surrogate_name, dir=True) |
| 45 |
|
| 46 |
try: |
| 47 |
mod.dir_unicode = udir.ensure(u'dir_extra', dir=True) |
| 48 |
mod.dir_unicode.join(u'ca\u2014f\xe9').write('test') |
| 49 |
except UnicodeEncodeError: |
| 50 |
mod.dir_unicode = None |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
GET_POSIX = "(): import %s as m ; return m" % os.name |
| 57 |
|
| 58 |
|
| 59 |
class AppTestPosix: |
| 60 |
spaceconfig = {'usemodules': USEMODULES} |
| 61 |
|
| 62 |
def setup_class(cls): |
| 63 |
space = cls.space |
| 64 |
cls.w_runappdirect = space.wrap(cls.runappdirect) |
| 65 |
cls.w_posix = space.appexec([], GET_POSIX) |
| 66 |
cls.w_path = space.wrap(str(path)) |
| 67 |
cls.w_path2 = space.wrap(str(path2)) |
| 68 |
cls.w_path3 = space.wrap(str(path3)) |
| 69 |
cls.w_pdir = space.wrap(str(pdir)) |
| 70 |
cls.w_bytes_dir = space.newbytes(str(bytes_dir)) |
| 71 |
cls.w_esurrogate_dir = space.newbytes(str(esurrogate_dir)) |
| 72 |
cls.w_sep = space.wrap(os.sep) |
| 73 |
cls.w_dir_unicode = space.wrap(unicode(dir_unicode) |
| 74 |
if dir_unicode is not None else None) |
| 75 |
if hasattr(os, 'getuid'): |
| 76 |
cls.w_getuid = space.wrap(os.getuid()) |
| 77 |
cls.w_geteuid = space.wrap(os.geteuid()) |
| 78 |
if hasattr(os, 'getgid'): |
| 79 |
cls.w_getgid = space.wrap(os.getgid()) |
| 80 |
if hasattr(os, 'getgroups'): |
| 81 |
cls.w_getgroups = space.newlist([space.wrap(e) for e in os.getgroups()]) |
| 82 |
if hasattr(os, 'getpgid'): |
| 83 |
cls.w_getpgid = space.wrap(os.getpgid(os.getpid())) |
| 84 |
if hasattr(os, 'getsid'): |
| 85 |
cls.w_getsid0 = space.wrap(os.getsid(0)) |
| 86 |
if hasattr(os, 'sysconf'): |
| 87 |
sysconf_name = os.sysconf_names.keys()[0] |
| 88 |
cls.w_sysconf_name = space.wrap(sysconf_name) |
| 89 |
cls.w_sysconf_value = space.wrap(os.sysconf_names[sysconf_name]) |
| 90 |
cls.w_sysconf_result = space.wrap(os.sysconf(sysconf_name)) |
| 91 |
if hasattr(os, 'confstr'): |
| 92 |
confstr_name = os.confstr_names.keys()[0] |
| 93 |
cls.w_confstr_name = space.wrap(confstr_name) |
| 94 |
cls.w_confstr_value = space.wrap(os.confstr_names[confstr_name]) |
| 95 |
cls.w_confstr_result = space.wrap(os.confstr(confstr_name)) |
| 96 |
cls.w_SIGABRT = space.wrap(signal.SIGABRT) |
| 97 |
cls.w_python = space.wrap(sys.executable) |
| 98 |
cls.w_platform = space.wrap(sys.platform) |
| 99 |
if hasattr(os, 'major'): |
| 100 |
cls.w_expected_major_12345 = space.wrap(os.major(12345)) |
| 101 |
cls.w_expected_minor_12345 = space.wrap(os.minor(12345)) |
| 102 |
cls.w_udir = space.wrap(str(udir)) |
| 103 |
cls.w_env_path = space.wrap(os.environ['PATH']) |
| 104 |
cls.w_Path = space.appexec([], """(): |
| 105 |
class Path: |
| 106 |
def __init__(self, _path): |
| 107 |
self._path =_path |
| 108 |
def __fspath__(self): |
| 109 |
return self._path |
| 110 |
return Path |
| 111 |
""") |
| 112 |
|
| 113 |
def setup_method(self, meth): |
| 114 |
if getattr(meth, 'need_sparse_files', False): |
| 115 |
if sys.maxsize < 2**32 and not self.runappdirect: |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
pytest.skip("emulation of files can't use " |
| 120 |
"larger-than-long offsets") |
| 121 |
need_sparse_files() |
| 122 |
|
| 123 |
def test_posix_is_pypy_s(self): |
| 124 |
assert hasattr(self.posix, '_statfields') |
| 125 |
|
| 126 |
def test_some_posix_basic_operation(self): |
| 127 |
path = self.path |
| 128 |
posix = self.posix |
| 129 |
fd = posix.open(path, posix.O_RDONLY, 0o777) |
| 130 |
fd2 = posix.dup(fd) |
| 131 |
assert posix.get_inheritable(fd2) == False |
| 132 |
assert not posix.isatty(fd2) |
| 133 |
s = posix.read(fd, 1) |
| 134 |
assert s == b't' |
| 135 |
posix.lseek(fd, 5, 0) |
| 136 |
s = posix.read(fd, 1) |
| 137 |
assert s == b'i' |
| 138 |
st = posix.fstat(fd) |
| 139 |
assert st == posix.stat(fd) |
| 140 |
posix.close(fd2) |
| 141 |
posix.close(fd) |
| 142 |
|
| 143 |
import sys, stat |
| 144 |
assert st[0] == st.st_mode |
| 145 |
assert st[1] == st.st_ino |
| 146 |
assert st[2] == st.st_dev |
| 147 |
assert st[3] == st.st_nlink |
| 148 |
assert st[4] == st.st_uid |
| 149 |
assert st[5] == st.st_gid |
| 150 |
assert st[6] == st.st_size |
| 151 |
assert st[7] == int(st.st_atime) |
| 152 |
assert st[8] == int(st.st_mtime) |
| 153 |
assert st[9] == int(st.st_ctime) |
| 154 |
|
| 155 |
assert stat.S_IMODE(st.st_mode) & stat.S_IRUSR |
| 156 |
assert stat.S_IMODE(st.st_mode) & stat.S_IWUSR |
| 157 |
if not sys.platform.startswith('win'): |
| 158 |
assert not (stat.S_IMODE(st.st_mode) & stat.S_IXUSR) |
| 159 |
|
| 160 |
assert st.st_size == 14 |
| 161 |
assert st.st_nlink == 1 |
| 162 |
|
| 163 |
if sys.platform.startswith('linux'): |
| 164 |
assert isinstance(st.st_atime, float) |
| 165 |
assert isinstance(st.st_mtime, float) |
| 166 |
assert isinstance(st.st_ctime, float) |
| 167 |
assert hasattr(st, 'st_rdev') |
| 168 |
|
| 169 |
assert isinstance(st.st_atime_ns, int) |
| 170 |
assert abs(st.st_atime_ns - 1e9*st.st_atime) < 500 |
| 171 |
assert abs(st.st_mtime_ns - 1e9*st.st_mtime) < 500 |
| 172 |
assert abs(st.st_ctime_ns - 1e9*st.st_ctime) < 500 |
| 173 |
|
| 174 |
def test_stat_float_times(self): |
| 175 |
path = self.path |
| 176 |
posix = self.posix |
| 177 |
import warnings |
| 178 |
with warnings.catch_warnings(record=True) as l: |
| 179 |
warnings.simplefilter('always') |
| 180 |
current = posix.stat_float_times() |
| 181 |
assert current is True |
| 182 |
assert "stat_float_times" in repr(l[0].message) |
| 183 |
try: |
| 184 |
posix.stat_float_times(True) |
| 185 |
st = posix.stat(path) |
| 186 |
assert isinstance(st.st_mtime, float) |
| 187 |
assert st[7] == int(st.st_atime) |
| 188 |
assert posix.stat_float_times(-1) is True |
| 189 |
|
| 190 |
posix.stat_float_times(False) |
| 191 |
st = posix.stat(path) |
| 192 |
assert isinstance(st.st_mtime, int) |
| 193 |
assert st[7] == st.st_atime |
| 194 |
assert posix.stat_float_times(-1) is False |
| 195 |
|
| 196 |
finally: |
| 197 |
posix.stat_float_times(current) |
| 198 |
|
| 199 |
|
| 200 |
def test_stat_result(self): |
| 201 |
st = self.posix.stat_result((0, 0, 0, 0, 0, 0, 0, 41, 42.1, 43)) |
| 202 |
assert st.st_atime == 41 |
| 203 |
assert st.st_mtime == 42.1 |
| 204 |
assert st.st_ctime == 43 |
| 205 |
assert repr(st).startswith('os.stat_result') |
| 206 |
|
| 207 |
def test_stat_lstat(self): |
| 208 |
import stat |
| 209 |
st = self.posix.stat(".") |
| 210 |
assert stat.S_ISDIR(st.st_mode) |
| 211 |
st = self.posix.stat(b".") |
| 212 |
assert stat.S_ISDIR(st.st_mode) |
| 213 |
st = self.posix.stat(bytearray(b".")) |
| 214 |
assert stat.S_ISDIR(st.st_mode) |
| 215 |
st = self.posix.lstat(".") |
| 216 |
assert stat.S_ISDIR(st.st_mode) |
| 217 |
|
| 218 |
def test_stat_exception(self): |
| 219 |
import sys |
| 220 |
import errno |
| 221 |
for fn in [self.posix.stat, self.posix.lstat]: |
| 222 |
with raises(OSError) as exc: |
| 223 |
fn("nonexistentdir/nonexistentfile") |
| 224 |
assert exc.value.errno == errno.ENOENT |
| 225 |
assert exc.value.filename == "nonexistentdir/nonexistentfile" |
| 226 |
with raises(OSError) as exc: |
| 227 |
fn("") |
| 228 |
assert exc.value.errno == errno.ENOENT |
| 229 |
|
| 230 |
with raises(TypeError) as excinfo: |
| 231 |
self.posix.stat(None) |
| 232 |
assert "should be string, bytes, os.PathLike or integer, not None" in str(excinfo.value) |
| 233 |
with raises(TypeError) as excinfo: |
| 234 |
self.posix.stat(2.) |
| 235 |
assert "should be string, bytes, os.PathLike or integer, not float" in str(excinfo.value) |
| 236 |
with raises(OSError): |
| 237 |
self.posix.stat(-1) |
| 238 |
with raises(ValueError): |
| 239 |
self.posix.stat(b"abc\x00def") |
| 240 |
with raises(ValueError): |
| 241 |
self.posix.stat(u"abc\x00def") |
| 242 |
|
| 243 |
def test_path_t_convertor(self): |
| 244 |
posix = self.posix |
| 245 |
|
| 246 |
class FakePath: |
| 247 |
"""Simple implementing of the path protocol. |
| 248 |
""" |
| 249 |
def __init__(self, path): |
| 250 |
self.path = path |
| 251 |
|
| 252 |
def __repr__(self): |
| 253 |
return '<FakePath {}>'.format(self.path) |
| 254 |
|
| 255 |
def __fspath__(self): |
| 256 |
if (isinstance(self.path, BaseException) or |
| 257 |
isinstance(self.path, type) and |
| 258 |
issubclass(self.path, BaseException)): |
| 259 |
raise self.path |
| 260 |
else: |
| 261 |
return self.path |
| 262 |
fd = posix.open(self.path, posix.O_RDONLY, 0o777) |
| 263 |
with raises(TypeError) as exc: |
| 264 |
posix.stat(FakePath(fd)) |
| 265 |
assert 'to return str or bytes' in exc.value.args[0] |
| 266 |
|
| 267 |
|
| 268 |
if hasattr(__import__(os.name), "statvfs"): |
| 269 |
def test_statvfs(self): |
| 270 |
st = self.posix.statvfs(".") |
| 271 |
assert isinstance(st, self.posix.statvfs_result) |
| 272 |
for field in [ |
| 273 |
'f_bsize', 'f_frsize', 'f_blocks', 'f_bfree', 'f_bavail', |
| 274 |
'f_files', 'f_ffree', 'f_favail', 'f_flag', 'f_namemax', |
| 275 |
'f_fsid', |
| 276 |
]: |
| 277 |
assert hasattr(st, field) |
| 278 |
|
| 279 |
def test_statvfs_result(self): |
| 280 |
st = self.posix.statvfs_result( |
| 281 |
(4096, 4096, 20971520, 17750785, 17750785, 1600000, 1275269, |
| 282 |
1275269, 4096, 255, 99999)) |
| 283 |
with raises(IndexError): |
| 284 |
st[10] |
| 285 |
assert st.f_fsid == 99999 |
| 286 |
st = self.posix.statvfs_result( |
| 287 |
(4096, 4096, 20971520, 17750785, 17750785, 1600000, 1275269, |
| 288 |
1275269, 4096, 255)) |
| 289 |
assert st.f_fsid is None |
| 290 |
|
| 291 |
def test_open_exception(self): |
| 292 |
posix = self.posix |
| 293 |
try: |
| 294 |
posix.open('qowieuqwoeiu', 0, 0) |
| 295 |
except OSError as e: |
| 296 |
assert e.filename == 'qowieuqwoeiu' |
| 297 |
else: |
| 298 |
assert 0 |
| 299 |
|
| 300 |
def test_filename_exception(self): |
| 301 |
for fname in ['unlink', 'remove', |
| 302 |
'chdir', 'mkdir', 'rmdir', |
| 303 |
'listdir', 'readlink', |
| 304 |
'chroot']: |
| 305 |
if hasattr(self.posix, fname): |
| 306 |
func = getattr(self.posix, fname) |
| 307 |
try: |
| 308 |
func('qowieuqw/oeiu') |
| 309 |
except OSError as e: |
| 310 |
assert e.filename == 'qowieuqw/oeiu' |
| 311 |
else: |
| 312 |
assert 0 |
| 313 |
|
| 314 |
def test_open_handles_NUL_chars(self): |
| 315 |
fn_with_NUL = 'foo\0bar' |
| 316 |
posix = self.posix |
| 317 |
with raises(ValueError) as e: |
| 318 |
posix.open(fn_with_NUL, 0, 0) |
| 319 |
|
| 320 |
|
| 321 |
def test_chmod_exception(self): |
| 322 |
try: |
| 323 |
self.posix.chmod('qowieuqw/oeiu', 0) |
| 324 |
except OSError as e: |
| 325 |
assert e.filename == 'qowieuqw/oeiu' |
| 326 |
else: |
| 327 |
assert 0 |
| 328 |
|
| 329 |
def test_chown_exception(self): |
| 330 |
if hasattr(self.posix, 'chown'): |
| 331 |
try: |
| 332 |
self.posix.chown('qowieuqw/oeiu', 0, 0) |
| 333 |
except OSError as e: |
| 334 |
assert e.filename == 'qowieuqw/oeiu' |
| 335 |
else: |
| 336 |
assert 0 |
| 337 |
|
| 338 |
def test_utime_exception(self): |
| 339 |
for arg in [None, (0, 0)]: |
| 340 |
try: |
| 341 |
self.posix.utime('qowieuqw/oeiu', arg) |
| 342 |
except OSError as e: |
| 343 |
pass |
| 344 |
else: |
| 345 |
assert 0 |
| 346 |
|
| 347 |
def test_functions_raise_error(self): |
| 348 |
import sys |
| 349 |
def ex(func, *args): |
| 350 |
try: |
| 351 |
func(*args) |
| 352 |
except OSError: |
| 353 |
pass |
| 354 |
else: |
| 355 |
raise AssertionError("%s(%s) did not raise" %( |
| 356 |
func.__name__, |
| 357 |
", ".join([str(x) for x in args]))) |
| 358 |
UNUSEDFD = 123123 |
| 359 |
ex(self.posix.open, "qweqwe", 0, 0) |
| 360 |
ex(self.posix.lseek, UNUSEDFD, 123, 0) |
| 361 |
|
| 362 |
ex(self.posix.read, UNUSEDFD, 123) |
| 363 |
ex(self.posix.write, UNUSEDFD, b"x") |
| 364 |
ex(self.posix.close, UNUSEDFD) |
| 365 |
|
| 366 |
if sys.platform == 'win32' and self.runappdirect: |
| 367 |
|
| 368 |
ex(self.posix.fstat, UNUSEDFD) |
| 369 |
ex(self.posix.stat, "qweqwehello") |
| 370 |
|
| 371 |
ex(self.posix.dup, UNUSEDFD) |
| 372 |
|
| 373 |
def test_getcwd(self): |
| 374 |
posix = self.posix |
| 375 |
import sys |
| 376 |
|
| 377 |
|
| 378 |
def fsencode(filename): |
| 379 |
encoding = sys.getfilesystemencoding() |
| 380 |
errors = sys.getfilesystemencodeerrors() |
| 381 |
filename = posix.fspath(filename) |
| 382 |
if isinstance(filename, str): |
| 383 |
return filename.encode(encoding, errors) |
| 384 |
else: |
| 385 |
return filename |
| 386 |
|
| 387 |
assert isinstance(posix.getcwd(), str) |
| 388 |
cwdb = posix.getcwdb() |
| 389 |
posix.chdir(self.esurrogate_dir) |
| 390 |
try: |
| 391 |
cwd = posix.getcwd() |
| 392 |
|
| 393 |
assert fsencode(cwd) == posix.getcwdb() |
| 394 |
finally: |
| 395 |
posix.chdir(cwdb) |
| 396 |
|
| 397 |
def test_getcwdb(self): |
| 398 |
assert isinstance(self.posix.getcwdb(), bytes) |
| 399 |
|
| 400 |
def test_listdir(self): |
| 401 |
pdir = self.pdir |
| 402 |
posix = self.posix |
| 403 |
result = posix.listdir(pdir) |
| 404 |
result.sort() |
| 405 |
assert result == ['another_longer_file_name', |
| 406 |
'file1', |
| 407 |
'file2'] |
| 408 |
|
| 409 |
def test_listdir_default(self): |
| 410 |
import sys |
| 411 |
posix = self.posix |
| 412 |
for v in ['', b'']: |
| 413 |
with raises(FileNotFoundError): |
| 414 |
posix.listdir(v) |
| 415 |
blank_dirlist = posix.listdir() |
| 416 |
for v in ['.', None]: |
| 417 |
assert blank_dirlist == posix.listdir(v) |
| 418 |
|
| 419 |
def test_listdir_bytes(self): |
| 420 |
import sys |
| 421 |
bytes_dir = self.bytes_dir |
| 422 |
posix = self.posix |
| 423 |
with raises(OSError): |
| 424 |
posix.mkdir(bytes_dir) |
| 425 |
result = posix.listdir(bytes_dir) |
| 426 |
assert all(type(x) is bytes for x in result) |
| 427 |
assert b'somefile' in result |
| 428 |
if sys.platform == "win32": |
| 429 |
expected = b"cafxe9" |
| 430 |
elif sys.platform == "darwin": |
| 431 |
expected = b"caf%E9" |
| 432 |
|
| 433 |
def test_listdir_unicode(self): |
| 434 |
if self.dir_unicode is None: |
| 435 |
skip("couldn't encode unicode file name") |
| 436 |
posix = self.posix |
| 437 |
with raises(OSError): |
| 438 |
posix.mkdir(self.dir_unicode) |
| 439 |
result = posix.listdir(self.dir_unicode) |
| 440 |
assert all(type(x) is str for x in result) |
| 441 |
assert u'ca\u2014f\xe9' in result |
| 442 |
raises(OSError, posix.listdir, self.dir_unicode + "NONEXISTENT") |
| 443 |
|
| 444 |
def test_listdir_memoryview_returns_unicode(self): |
| 445 |
import sys |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
def fsencode(filename): |
| 450 |
encoding = sys.getfilesystemencoding() |
| 451 |
errors = sys.getfilesystemencodeerrors() |
| 452 |
filename = posix.fspath(filename) |
| 453 |
if isinstance(filename, str): |
| 454 |
return filename.encode(encoding, errors) |
| 455 |
else: |
| 456 |
return filename |
| 457 |
|
| 458 |
|
| 459 |
bytes_dir = self.bytes_dir |
| 460 |
posix = self.posix |
| 461 |
result1 = posix.listdir(bytes_dir) |
| 462 |
result2 = posix.listdir(memoryview(bytes_dir)) |
| 463 |
assert [fsencode(x) for x in result2] == result1 |
| 464 |
|
| 465 |
@py.test.mark.skipif("sys.platform == 'win32'") |
| 466 |
def test_fdlistdir(self): |
| 467 |
posix = self.posix |
| 468 |
dirfd = posix.open('.', posix.O_RDONLY) |
| 469 |
lst1 = posix.listdir(dirfd) |
| 470 |
lst2 = posix.listdir('.') |
| 471 |
assert lst1 == lst2 |
| 472 |
|
| 473 |
lst3 = posix.listdir(dirfd) |
| 474 |
assert lst3 == lst1 |
| 475 |
posix.close(dirfd) |
| 476 |
|
| 477 |
def test_undecodable_filename(self): |
| 478 |
import sys |
| 479 |
posix = self.posix |
| 480 |
try: |
| 481 |
'caf\xe9'.encode(sys.getfilesystemencoding(), 'surrogateescape') |
| 482 |
except UnicodeEncodeError: |
| 483 |
pass |
| 484 |
else: |
| 485 |
assert posix.access('caf\xe9', posix.R_OK) is False |
| 486 |
assert posix.access(b'caf\xe9', posix.R_OK) is False |
| 487 |
assert posix.access('caf\udcc0', posix.R_OK) is False |
| 488 |
assert posix.access(b'caf\xc3', posix.R_OK) is False |
| 489 |
|
| 490 |
def test_access(self): |
| 491 |
pdir = self.pdir + '/file1' |
| 492 |
posix = self.posix |
| 493 |
|
| 494 |
assert posix.access(pdir, posix.R_OK) is True |
| 495 |
assert posix.access(pdir, posix.W_OK) is True |
| 496 |
import sys |
| 497 |
if sys.platform != "win32": |
| 498 |
assert posix.access(pdir, posix.X_OK) is False |
| 499 |
|
| 500 |
def test_unlink(self): |
| 501 |
os = self.posix |
| 502 |
path = self.path3 |
| 503 |
with open(path, 'wb'): |
| 504 |
pass |
| 505 |
os.unlink(path) |
| 506 |
|
| 507 |
def test_times(self): |
| 508 |
""" |
| 509 |
posix.times() should return a posix.times_result object giving |
| 510 |
float-representations (seconds, effectively) of the four fields from |
| 511 |
the underlying struct tms and the return value. |
| 512 |
""" |
| 513 |
result = self.posix.times() |
| 514 |
assert isinstance(self.posix.times(), self.posix.times_result) |
| 515 |
assert isinstance(self.posix.times(), tuple) |
| 516 |
assert len(result) == 5 |
| 517 |
for value in result: |
| 518 |
assert isinstance(value, float) |
| 519 |
assert isinstance(result.user, float) |
| 520 |
assert isinstance(result.system, float) |
| 521 |
assert isinstance(result.children_user, float) |
| 522 |
assert isinstance(result.children_system, float) |
| 523 |
assert isinstance(result.elapsed, float) |
| 524 |
def test_strerror(self): |
| 525 |
assert isinstance(self.posix.strerror(0), str) |
| 526 |
assert isinstance(self.posix.strerror(1), str) |
| 527 |
|
| 528 |
if hasattr(__import__(os.name), "fork"): |
| 529 |
def test_fork(self): |
| 530 |
os = self.posix |
| 531 |
pid = os.fork() |
| 532 |
if pid == 0: |
| 533 |
os._exit(4) |
| 534 |
pid1, status1 = os.waitpid(pid, 0) |
| 535 |
assert pid1 == pid |
| 536 |
assert os.WIFEXITED(status1) |
| 537 |
assert os.WEXITSTATUS(status1) == 4 |
| 538 |
assert os.waitstatus_to_exitcode(status1) == 4 |
| 539 |
pass |
| 540 |
|
| 541 |
|
| 542 |
if hasattr(__import__(os.name), "openpty"): |
| 543 |
def test_openpty(self): |
| 544 |
os = self.posix |
| 545 |
master_fd, slave_fd = os.openpty() |
| 546 |
assert isinstance(master_fd, int) |
| 547 |
assert isinstance(slave_fd, int) |
| 548 |
os.write(slave_fd, b'x\n') |
| 549 |
data = os.read(master_fd, 100) |
| 550 |
assert data.startswith(b'x') |
| 551 |
os.close(master_fd) |
| 552 |
os.close(slave_fd) |
| 553 |
|
| 554 |
def test_openpty_non_inheritable(self): |
| 555 |
os = self.posix |
| 556 |
master_fd, slave_fd = os.openpty() |
| 557 |
assert os.get_inheritable(master_fd) == False |
| 558 |
assert os.get_inheritable(slave_fd) == False |
| 559 |
os.close(master_fd) |
| 560 |
os.close(slave_fd) |
| 561 |
|
| 562 |
if hasattr(__import__(os.name), "forkpty"): |
| 563 |
def test_forkpty(self): |
| 564 |
import sys |
| 565 |
if 'freebsd' in sys.platform: |
| 566 |
skip("hangs on FreeBSD (also on CPython).") |
| 567 |
if sys.platform == 'darwin' and not self.runappdirect: |
| 568 |
skip("hangs on darwin untranslated") |
| 569 |
os = self.posix |
| 570 |
childpid, master_fd = os.forkpty() |
| 571 |
assert isinstance(childpid, int) |
| 572 |
assert isinstance(master_fd, int) |
| 573 |
if childpid == 0: |
| 574 |
data = os.read(0, 100) |
| 575 |
if data.startswith(b'abc'): |
| 576 |
os._exit(42) |
| 577 |
else: |
| 578 |
os._exit(43) |
| 579 |
os.write(master_fd, b'abc\n') |
| 580 |
_, status = os.waitpid(childpid, 0) |
| 581 |
assert status >> 8 == 42 |
| 582 |
|
| 583 |
def test_utime(self): |
| 584 |
os = self.posix |
| 585 |
|
| 586 |
path = self.path2 + "test_utime.txt" |
| 587 |
fh = open(path, "wb") |
| 588 |
fh.write(b"x") |
| 589 |
fh.close() |
| 590 |
from time import time, sleep |
| 591 |
t0 = time() |
| 592 |
sleep(1.1) |
| 593 |
os.utime(path, None) |
| 594 |
assert os.stat(path).st_atime > t0 |
| 595 |
os.utime(path, (int(t0), int(t0))) |
| 596 |
assert int(os.stat(path).st_atime) == int(t0) |
| 597 |
t1 = time() |
| 598 |
os.utime(path, (int(t1), int(t1))) |
| 599 |
assert int(os.stat(path).st_atime) == int(t1) |
| 600 |
|
| 601 |
def test_utime_raises(self): |
| 602 |
os = self.posix |
| 603 |
import errno |
| 604 |
with raises(TypeError): |
| 605 |
os.utime('xxx', 3) |
| 606 |
with raises(TypeError): |
| 607 |
os.utime('xxx', [5, 5]) |
| 608 |
with raises(TypeError): |
| 609 |
os.utime('xxx', ns=[5, 5]) |
| 610 |
with raises(OSError) as exc: |
| 611 |
os.utime('somefilewhichihopewouldneverappearhere', None) |
| 612 |
assert exc.value.errno == errno.ENOENT |
| 613 |
|
| 614 |
for name in rposix.WAIT_MACROS: |
| 615 |
if hasattr(os, name): |
| 616 |
values = [0, 1, 127, 128, 255] |
| 617 |
code = py.code.Source(""" |
| 618 |
def test_wstar(self): |
| 619 |
os = self.posix |
| 620 |
%s |
| 621 |
""" % "\n ".join(["assert os.%s(%d) == %d" % (name, value, |
| 622 |
getattr(os, name)(value)) for value in values])) |
| 623 |
d = {} |
| 624 |
exec code.compile() in d |
| 625 |
locals()['test_' + name] = d['test_wstar'] |
| 626 |
|
| 627 |
if hasattr(os, 'WIFSIGNALED'): |
| 628 |
def test_wifsignaled(self): |
| 629 |
os = self.posix |
| 630 |
assert os.WIFSIGNALED(0) == False |
| 631 |
assert os.WIFSIGNALED(1) == True |
| 632 |
|
| 633 |
if hasattr(os, 'uname'): |
| 634 |
def test_os_uname(self): |
| 635 |
os = self.posix |
| 636 |
res = os.uname() |
| 637 |
assert len(res) == 5 |
| 638 |
for i in res: |
| 639 |
assert isinstance(i, str) |
| 640 |
assert isinstance(res, tuple) |
| 641 |
assert res == (res.sysname, res.nodename, |
| 642 |
res.release, res.version, res.machine) |
| 643 |
|
| 644 |
if hasattr(os, 'getuid'): |
| 645 |
def test_os_getuid(self): |
| 646 |
os = self.posix |
| 647 |
assert os.getuid() == self.getuid |
| 648 |
assert os.geteuid() == self.geteuid |
| 649 |
|
| 650 |
if hasattr(os, 'setuid'): |
| 651 |
@py.test.mark.skipif("sys.version_info < (2, 7, 4)") |
| 652 |
def test_os_setuid_error(self): |
| 653 |
os = self.posix |
| 654 |
with raises(OverflowError): |
| 655 |
os.setuid(-2) |
| 656 |
with raises(OverflowError): |
| 657 |
os.setuid(2**32) |
| 658 |
with raises(OSError): |
| 659 |
os.setuid(-1) |
| 660 |
|
| 661 |
if hasattr(os, 'getgid'): |
| 662 |
def test_os_getgid(self): |
| 663 |
os = self.posix |
| 664 |
assert os.getgid() == self.getgid |
| 665 |
|
| 666 |
if hasattr(os, 'getgroups'): |
| 667 |
def test_os_getgroups(self): |
| 668 |
os = self.posix |
| 669 |
assert os.getgroups() == self.getgroups |
| 670 |
|
| 671 |
if hasattr(os, 'setgroups'): |
| 672 |
def test_os_setgroups(self): |
| 673 |
os = self.posix |
| 674 |
with raises(TypeError): |
| 675 |
os.setgroups([2, 5, "hello"]) |
| 676 |
try: |
| 677 |
os.setgroups(os.getgroups()) |
| 678 |
except OSError: |
| 679 |
pass |
| 680 |
|
| 681 |
if hasattr(os, 'initgroups'): |
| 682 |
def test_os_initgroups(self): |
| 683 |
os = self.posix |
| 684 |
with raises(OSError): |
| 685 |
os.initgroups("crW2hTQC", 100) |
| 686 |
|
| 687 |
if hasattr(os, 'tcgetpgrp'): |
| 688 |
def test_os_tcgetpgrp(self): |
| 689 |
os = self.posix |
| 690 |
with raises(OSError): |
| 691 |
os.tcgetpgrp(9999) |
| 692 |
|
| 693 |
if hasattr(os, 'tcsetpgrp'): |
| 694 |
def test_os_tcsetpgrp(self): |
| 695 |
os = self.posix |
| 696 |
with raises(OSError): |
| 697 |
os.tcsetpgrp(9999, 1) |
| 698 |
|
| 699 |
if hasattr(os, 'getpgid'): |
| 700 |
def test_os_getpgid(self): |
| 701 |
os = self.posix |
| 702 |
assert os.getpgid(os.getpid()) == self.getpgid |
| 703 |
with raises(OSError): |
| 704 |
os.getpgid(1234567) |
| 705 |
|
| 706 |
if hasattr(os, 'setgid'): |
| 707 |
@pytest.mark.skipif("sys.version_info < (2, 7, 4)") |
| 708 |
def test_os_setgid_error(self): |
| 709 |
os = self.posix |
| 710 |
with raises(OverflowError): |
| 711 |
os.setgid(-2) |
| 712 |
with raises(OverflowError): |
| 713 |
os.setgid(2**32) |
| 714 |
with raises(OSError): |
| 715 |
os.setgid(-1) |
| 716 |
with raises(OSError): |
| 717 |
os.setgid(2**32-1) |
| 718 |
|
| 719 |
if hasattr(os, 'getsid'): |
| 720 |
def test_os_getsid(self): |
| 721 |
os = self.posix |
| 722 |
assert os.getsid(0) == self.getsid0 |
| 723 |
with raises(OSError): |
| 724 |
os.getsid(-100000) |
| 725 |
|
| 726 |
if hasattr(os, 'getresuid'): |
| 727 |
def test_os_getresuid(self): |
| 728 |
os = self.posix |
| 729 |
res = os.getresuid() |
| 730 |
assert len(res) == 3 |
| 731 |
|
| 732 |
if hasattr(os, 'getresgid'): |
| 733 |
def test_os_getresgid(self): |
| 734 |
os = self.posix |
| 735 |
res = os.getresgid() |
| 736 |
assert len(res) == 3 |
| 737 |
|
| 738 |
if hasattr(os, 'setresuid'): |
| 739 |
def test_os_setresuid(self): |
| 740 |
os = self.posix |
| 741 |
a, b, c = os.getresuid() |
| 742 |
os.setresuid(a, b, c) |
| 743 |
|
| 744 |
if hasattr(os, 'setresgid'): |
| 745 |
def test_os_setresgid(self): |
| 746 |
os = self.posix |
| 747 |
a, b, c = os.getresgid() |
| 748 |
os.setresgid(a, b, c) |
| 749 |
|
| 750 |
if hasattr(os, 'sysconf'): |
| 751 |
def test_os_sysconf(self): |
| 752 |
os = self.posix |
| 753 |
assert os.sysconf(self.sysconf_value) == self.sysconf_result |
| 754 |
assert os.sysconf(self.sysconf_name) == self.sysconf_result |
| 755 |
assert os.sysconf_names[self.sysconf_name] == self.sysconf_value |
| 756 |
|
| 757 |
def test_os_sysconf_error(self): |
| 758 |
os = self.posix |
| 759 |
with raises(ValueError): |
| 760 |
os.sysconf("!@#$%!#$!@#") |
| 761 |
|
| 762 |
if hasattr(os, 'fpathconf'): |
| 763 |
def test_os_fpathconf(self): |
| 764 |
os = self.posix |
| 765 |
assert os.fpathconf(1, "PC_PIPE_BUF") >= 128 |
| 766 |
with raises(OSError): |
| 767 |
os.fpathconf(-1, "PC_PIPE_BUF") |
| 768 |
with raises(ValueError): |
| 769 |
os.fpathconf(1, "##") |
| 770 |
|
| 771 |
if hasattr(os, 'pathconf'): |
| 772 |
def test_os_pathconf(self): |
| 773 |
os = self.posix |
| 774 |
assert os.pathconf("/tmp", "PC_NAME_MAX") >= 31 |
| 775 |
|
| 776 |
with raises(OSError): |
| 777 |
os.pathconf("", "PC_PIPE_BUF") |
| 778 |
with raises(ValueError): |
| 779 |
os.pathconf("/tmp", "##") |
| 780 |
|
| 781 |
if hasattr(os, 'confstr'): |
| 782 |
def test_os_confstr(self): |
| 783 |
os = self.posix |
| 784 |
assert os.confstr(self.confstr_value) == self.confstr_result |
| 785 |
assert os.confstr(self.confstr_name) == self.confstr_result |
| 786 |
assert os.confstr_names[self.confstr_name] == self.confstr_value |
| 787 |
|
| 788 |
def test_os_confstr_error(self): |
| 789 |
os = self.posix |
| 790 |
with raises(ValueError): |
| 791 |
os.confstr("!@#$%!#$!@#") |
| 792 |
|
| 793 |
if hasattr(os, 'wait'): |
| 794 |
def test_os_wait(self): |
| 795 |
os = self.posix |
| 796 |
exit_status = 0x33 |
| 797 |
|
| 798 |
if not hasattr(os, "fork"): |
| 799 |
skip("Need fork() to test wait()") |
| 800 |
if hasattr(os, "waitpid") and hasattr(os, "WNOHANG"): |
| 801 |
try: |
| 802 |
while os.waitpid(-1, os.WNOHANG)[0]: |
| 803 |
pass |
| 804 |
except OSError: |
| 805 |
pass |
| 806 |
child = os.fork() |
| 807 |
if child == 0: |
| 808 |
os._exit(exit_status) |
| 809 |
else: |
| 810 |
pid, status = os.wait() |
| 811 |
assert child == pid |
| 812 |
assert os.WIFEXITED(status) |
| 813 |
assert os.WEXITSTATUS(status) == exit_status |
| 814 |
|
| 815 |
if hasattr(os, 'getloadavg'): |
| 816 |
def test_os_getloadavg(self): |
| 817 |
os = self.posix |
| 818 |
l0, l1, l2 = os.getloadavg() |
| 819 |
assert type(l0) is float and l0 >= 0.0 |
| 820 |
assert type(l1) is float and l0 >= 0.0 |
| 821 |
assert type(l2) is float and l0 >= 0.0 |
| 822 |
|
| 823 |
if hasattr(os, 'major'): |
| 824 |
def test_major_minor(self): |
| 825 |
os = self.posix |
| 826 |
assert os.major(12345) == self.expected_major_12345 |
| 827 |
assert os.minor(12345) == self.expected_minor_12345 |
| 828 |
assert os.makedev(self.expected_major_12345, |
| 829 |
self.expected_minor_12345) == 12345 |
| 830 |
with raises((ValueError, OverflowError)): |
| 831 |
os.major(-1) |
| 832 |
|
| 833 |
if hasattr(os, 'fsync'): |
| 834 |
def test_fsync(self): |
| 835 |
os = self.posix |
| 836 |
f = open(self.path2, "w") |
| 837 |
try: |
| 838 |
fd = f.fileno() |
| 839 |
os.fsync(fd) |
| 840 |
os.fsync(f) |
| 841 |
finally: |
| 842 |
f.close() |
| 843 |
try: |
| 844 |
|
| 845 |
os.fsync(fd) |
| 846 |
except OSError: |
| 847 |
pass |
| 848 |
with raises(ValueError): |
| 849 |
os.fsync(-1) |
| 850 |
|
| 851 |
if hasattr(os, 'fdatasync'): |
| 852 |
def test_fdatasync(self): |
| 853 |
os = self.posix |
| 854 |
f = open(self.path2, "w") |
| 855 |
try: |
| 856 |
fd = f.fileno() |
| 857 |
os.fdatasync(fd) |
| 858 |
finally: |
| 859 |
f.close() |
| 860 |
try: |
| 861 |
|
| 862 |
os.fdatasync(fd) |
| 863 |
except OSError: |
| 864 |
pass |
| 865 |
with raises(ValueError): |
| 866 |
os.fdatasync(-1) |
| 867 |
|
| 868 |
if hasattr(os, 'fchdir'): |
| 869 |
def test_fchdir(self): |
| 870 |
os = self.posix |
| 871 |
localdir = os.getcwd() |
| 872 |
os.mkdir(self.path2 + 'fchdir') |
| 873 |
for func in [os.fchdir, os.chdir]: |
| 874 |
fd = os.open(self.path2 + 'fchdir', os.O_RDONLY) |
| 875 |
try: |
| 876 |
func(fd) |
| 877 |
mypath = os.getcwd() |
| 878 |
finally: |
| 879 |
os.close(fd) |
| 880 |
os.chdir(localdir) |
| 881 |
assert mypath.endswith('test_posix2-fchdir') |
| 882 |
with raises(OSError): |
| 883 |
func(fd) |
| 884 |
with raises(ValueError): |
| 885 |
os.fchdir(-1) |
| 886 |
|
| 887 |
if hasattr(rposix, 'pread'): |
| 888 |
def test_os_pread(self): |
| 889 |
os = self.posix |
| 890 |
fd = os.open(self.path2 + 'test_os_pread', os.O_RDWR | os.O_CREAT) |
| 891 |
try: |
| 892 |
os.write(fd, b'test') |
| 893 |
os.lseek(fd, 0, 0) |
| 894 |
assert os.pread(fd, 2, 1) == b'es' |
| 895 |
assert os.read(fd, 2) == b'te' |
| 896 |
finally: |
| 897 |
os.close(fd) |
| 898 |
|
| 899 |
if hasattr(rposix, 'pwrite'): |
| 900 |
def test_os_pwrite(self): |
| 901 |
os = self.posix |
| 902 |
fd = os.open(self.path2 + 'test_os_pwrite', os.O_RDWR | os.O_CREAT) |
| 903 |
try: |
| 904 |
os.write(fd, b'test') |
| 905 |
os.lseek(fd, 0, 0) |
| 906 |
os.pwrite(fd, b'xx', 1) |
| 907 |
assert os.read(fd, 4) == b'txxt' |
| 908 |
finally: |
| 909 |
os.close(fd) |
| 910 |
|
| 911 |
if hasattr(rposix, 'posix_fadvise'): |
| 912 |
def test_os_posix_fadvise(self): |
| 913 |
posix = self.posix |
| 914 |
fd = posix.open(self.path2 + 'test_os_posix_fadvise', posix.O_CREAT | posix.O_RDWR) |
| 915 |
try: |
| 916 |
posix.write(fd, b"foobar") |
| 917 |
assert posix.posix_fadvise(fd, 0, 1, posix.POSIX_FADV_WILLNEED) is None |
| 918 |
assert posix.posix_fadvise(fd, 1, 1, posix.POSIX_FADV_NORMAL) is None |
| 919 |
assert posix.posix_fadvise(fd, 2, 1, posix.POSIX_FADV_SEQUENTIAL) is None |
| 920 |
assert posix.posix_fadvise(fd, 3, 1, posix.POSIX_FADV_RANDOM) is None |
| 921 |
assert posix.posix_fadvise(fd, 4, 1, posix.POSIX_FADV_NOREUSE) is None |
| 922 |
assert posix.posix_fadvise(fd, 5, 1, posix.POSIX_FADV_DONTNEED) is None |
| 923 |
|
| 924 |
if self.runappdirect: |
| 925 |
raises(OSError, posix.posix_fadvise, fd, 6, 1, 1234567) |
| 926 |
finally: |
| 927 |
posix.close(fd) |
| 928 |
|
| 929 |
if hasattr(rposix, 'posix_fallocate'): |
| 930 |
def test_os_posix_posix_fallocate(self): |
| 931 |
os = self.posix |
| 932 |
import errno |
| 933 |
fd = os.open(self.path2 + 'test_os_posix_fallocate', os.O_WRONLY | os.O_CREAT) |
| 934 |
try: |
| 935 |
ret = os.posix_fallocate(fd, 0, 10) |
| 936 |
if ret == errno.EINVAL and not self.runappdirect: |
| 937 |
|
| 938 |
pass |
| 939 |
else: |
| 940 |
assert ret == 0 |
| 941 |
except OSError as inst: |
| 942 |
""" ZFS seems not to support fallocate. |
| 943 |
so skipping solaris-based since it is likely to come with ZFS |
| 944 |
""" |
| 945 |
if inst.errno != errno.EINVAL or not sys.platform.startswith("sunos"): |
| 946 |
raise |
| 947 |
finally: |
| 948 |
os.close(fd) |
| 949 |
|
| 950 |
|
| 951 |
def test_largefile(self): |
| 952 |
os = self.posix |
| 953 |
fd = os.open(self.path2 + 'test_largefile', |
| 954 |
os.O_RDWR | os.O_CREAT, 0o666) |
| 955 |
os.ftruncate(fd, 10000000000) |
| 956 |
res = os.lseek(fd, 9900000000, 0) |
| 957 |
assert res == 9900000000 |
| 958 |
res = os.lseek(fd, -5000000000, 1) |
| 959 |
assert res == 4900000000 |
| 960 |
res = os.lseek(fd, -5200000000, 2) |
| 961 |
assert res == 4800000000 |
| 962 |
os.close(fd) |
| 963 |
|
| 964 |
st = os.stat(self.path2 + 'test_largefile') |
| 965 |
assert st.st_size == 10000000000 |
| 966 |
test_largefile.need_sparse_files = True |
| 967 |
|
| 968 |
if hasattr(rposix, 'getpriority'): |
| 969 |
def test_os_set_get_priority(self): |
| 970 |
posix = os = self.posix |
| 971 |
childpid = os.fork() |
| 972 |
if childpid == 0: |
| 973 |
|
| 974 |
|
| 975 |
orig_priority = posix.getpriority(posix.PRIO_PROCESS, |
| 976 |
os.getpid()) |
| 977 |
orig_grp_priority = posix.getpriority(posix.PRIO_PGRP, |
| 978 |
os.getpgrp()) |
| 979 |
posix.setpriority(posix.PRIO_PROCESS, os.getpid(), |
| 980 |
orig_priority + 1) |
| 981 |
new_priority = posix.getpriority(posix.PRIO_PROCESS, |
| 982 |
os.getpid()) |
| 983 |
assert new_priority == orig_priority + 1 |
| 984 |
assert posix.getpriority(posix.PRIO_PGRP, os.getpgrp()) == ( |
| 985 |
orig_grp_priority) |
| 986 |
os._exit(0) |
| 987 |
|
| 988 |
pid1, status1 = os.waitpid(childpid, 0) |
| 989 |
assert pid1 == childpid |
| 990 |
assert os.WIFEXITED(status1) |
| 991 |
assert os.WEXITSTATUS(status1) == 0 |
| 992 |
|
| 993 |
if hasattr(rposix, 'sched_get_priority_max'): |
| 994 |
def test_os_sched_get_priority_max(self): |
| 995 |
import sys |
| 996 |
posix = self.posix |
| 997 |
assert posix.sched_get_priority_max(posix.SCHED_FIFO) != -1 |
| 998 |
assert posix.sched_get_priority_max(posix.SCHED_RR) != -1 |
| 999 |
assert posix.sched_get_priority_max(posix.SCHED_OTHER) != -1 |
| 1000 |
if getattr(posix, 'SCHED_BATCH', None): |
| 1001 |
assert posix.sched_get_priority_max(posix.SCHED_BATCH) != -1 |
| 1002 |
|
| 1003 |
if hasattr(rposix, 'sched_get_priority_min'): |
| 1004 |
def test_os_sched_get_priority_min(self): |
| 1005 |
import sys |
| 1006 |
posix = self.posix |
| 1007 |
assert posix.sched_get_priority_min(posix.SCHED_FIFO) != -1 |
| 1008 |
assert posix.sched_get_priority_min(posix.SCHED_RR) != -1 |
| 1009 |
assert posix.sched_get_priority_min(posix.SCHED_OTHER) != -1 |
| 1010 |
if getattr(posix, 'SCHED_BATCH', None): |
| 1011 |
assert posix.sched_get_priority_min(posix.SCHED_BATCH) != -1 |
| 1012 |
|
| 1013 |
if hasattr(rposix, 'sched_get_priority_min'): |
| 1014 |
def test_os_sched_priority_max_greater_than_min(self): |
| 1015 |
posix = self.posix |
| 1016 |
policy = posix.SCHED_RR |
| 1017 |
low = posix.sched_get_priority_min(policy) |
| 1018 |
high = posix.sched_get_priority_max(policy) |
| 1019 |
assert isinstance(low, int) == True |
| 1020 |
assert isinstance(high, int) == True |
| 1021 |
assert high > low |
| 1022 |
|
| 1023 |
if hasattr(rposix, 'sched_yield'): |
| 1024 |
def test_sched_yield(self): |
| 1025 |
os = self.posix |
| 1026 |
|
| 1027 |
os.sched_yield() |
| 1028 |
|
| 1029 |
if hasattr(rposix, 'sched_getparam'): |
| 1030 |
def test_sched_param_kwargs(self): |
| 1031 |
os = self.posix |
| 1032 |
sp = os.sched_param(sched_priority=1) |
| 1033 |
assert sp.sched_priority == 1 |
| 1034 |
|
| 1035 |
def test_write_buffer(self): |
| 1036 |
os = self.posix |
| 1037 |
fd = os.open(self.path2 + 'test_write_buffer', |
| 1038 |
os.O_RDWR | os.O_CREAT, 0o666) |
| 1039 |
def writeall(s): |
| 1040 |
while s: |
| 1041 |
count = os.write(fd, s) |
| 1042 |
assert count > 0 |
| 1043 |
s = s[count:] |
| 1044 |
writeall(b'hello, ') |
| 1045 |
writeall(memoryview(b'world!\n')) |
| 1046 |
res = os.lseek(fd, 0, 0) |
| 1047 |
assert res == 0 |
| 1048 |
data = b'' |
| 1049 |
while True: |
| 1050 |
s = os.read(fd, 100) |
| 1051 |
if not s: |
| 1052 |
break |
| 1053 |
data += s |
| 1054 |
assert data == b'hello, world!\n' |
| 1055 |
os.close(fd) |
| 1056 |
|
| 1057 |
def test_write_unicode(self): |
| 1058 |
os = self.posix |
| 1059 |
fd = os.open(self.path2 + 'test_write_unicode', |
| 1060 |
os.O_RDWR | os.O_CREAT, 0o666) |
| 1061 |
with raises(TypeError): |
| 1062 |
os.write(fd, 'X') |
| 1063 |
os.close(fd) |
| 1064 |
|
| 1065 |
if hasattr(__import__(os.name), "fork"): |
| 1066 |
def test_abort(self): |
| 1067 |
os = self.posix |
| 1068 |
pid = os.fork() |
| 1069 |
if pid == 0: |
| 1070 |
os.abort() |
| 1071 |
pid1, status1 = os.waitpid(pid, 0) |
| 1072 |
assert pid1 == pid |
| 1073 |
assert os.WIFSIGNALED(status1) |
| 1074 |
assert os.WTERMSIG(status1) == self.SIGABRT |
| 1075 |
assert os.waitstatus_to_exitcode(status1) == -self.SIGABRT |
| 1076 |
pass |
| 1077 |
|
| 1078 |
def test_closerange(self): |
| 1079 |
os = self.posix |
| 1080 |
if not hasattr(os, 'closerange'): |
| 1081 |
skip("missing os.closerange()") |
| 1082 |
fds = [os.open(self.path + str(i), os.O_CREAT|os.O_WRONLY, 0o777) |
| 1083 |
for i in range(15)] |
| 1084 |
fds.sort() |
| 1085 |
start = fds.pop() |
| 1086 |
stop = start + 1 |
| 1087 |
while len(fds) > 3 and fds[-1] == start - 1: |
| 1088 |
start = fds.pop() |
| 1089 |
os.closerange(start, stop) |
| 1090 |
for fd in fds: |
| 1091 |
os.close(fd) |
| 1092 |
if self.platform == 'win32' and self.runappdirect: |
| 1093 |
|
| 1094 |
for fd in range(start, stop): |
| 1095 |
with raises(OSError): |
| 1096 |
os.fstat(fd) |
| 1097 |
|
| 1098 |
if hasattr(os, 'chown'): |
| 1099 |
def test_chown(self): |
| 1100 |
my_path = self.path2 + 'test_chown' |
| 1101 |
os = self.posix |
| 1102 |
with raises(OSError): |
| 1103 |
os.chown(my_path, os.getuid(), os.getgid()) |
| 1104 |
open(my_path, 'w').close() |
| 1105 |
os.chown(my_path, os.getuid(), os.getgid()) |
| 1106 |
|
| 1107 |
if hasattr(os, 'lchown'): |
| 1108 |
def test_lchown(self): |
| 1109 |
my_path = self.path2 + 'test_lchown' |
| 1110 |
os = self.posix |
| 1111 |
with raises(OSError): |
| 1112 |
os.lchown(my_path, os.getuid(), os.getgid()) |
| 1113 |
os.symlink('foobar', my_path) |
| 1114 |
os.lchown(my_path, os.getuid(), os.getgid()) |
| 1115 |
|
| 1116 |
if hasattr(os, 'fchown'): |
| 1117 |
def test_fchown(self): |
| 1118 |
my_path = self.path2 + 'test_fchown' |
| 1119 |
os = self.posix |
| 1120 |
f = open(my_path, "w") |
| 1121 |
os.fchown(f.fileno(), os.getuid(), os.getgid()) |
| 1122 |
f.close() |
| 1123 |
|
| 1124 |
if hasattr(os, 'chmod'): |
| 1125 |
def test_chmod(self): |
| 1126 |
import sys |
| 1127 |
my_path = self.path2 + 'test_chmod' |
| 1128 |
os = self.posix |
| 1129 |
with raises(OSError): |
| 1130 |
os.chmod(my_path, 0o600) |
| 1131 |
open(my_path, "w").close() |
| 1132 |
if sys.platform == 'win32': |
| 1133 |
os.chmod(my_path, 0o400) |
| 1134 |
assert (os.stat(my_path).st_mode & 0o600) == 0o400 |
| 1135 |
os.chmod(self.path, 0o700) |
| 1136 |
else: |
| 1137 |
os.chmod(my_path, 0o200) |
| 1138 |
assert (os.stat(my_path).st_mode & 0o777) == 0o200 |
| 1139 |
os.chmod(self.path, 0o700) |
| 1140 |
|
| 1141 |
if hasattr(os, 'fchmod'): |
| 1142 |
def test_fchmod(self): |
| 1143 |
my_path = self.path2 + 'test_fchmod' |
| 1144 |
os = self.posix |
| 1145 |
f = open(my_path, "w") |
| 1146 |
os.fchmod(f.fileno(), 0o200) |
| 1147 |
assert (os.fstat(f.fileno()).st_mode & 0o777) == 0o200 |
| 1148 |
f.close() |
| 1149 |
assert (os.stat(my_path).st_mode & 0o777) == 0o200 |
| 1150 |
|
| 1151 |
if hasattr(os, 'mkfifo'): |
| 1152 |
def test_mkfifo(self): |
| 1153 |
os = self.posix |
| 1154 |
os.mkfifo(self.path2 + 'test_mkfifo', 0o666) |
| 1155 |
st = os.lstat(self.path2 + 'test_mkfifo') |
| 1156 |
import stat |
| 1157 |
assert stat.S_ISFIFO(st.st_mode) |
| 1158 |
|
| 1159 |
if hasattr(os, 'mknod'): |
| 1160 |
def test_mknod(self): |
| 1161 |
import stat |
| 1162 |
os = self.posix |
| 1163 |
|
| 1164 |
try: |
| 1165 |
|
| 1166 |
os.mknod(self.path2 + 'test_mknod-1') |
| 1167 |
except OSError as e: |
| 1168 |
skip("os.mknod(): got %r" % (e,)) |
| 1169 |
st = os.lstat(self.path2 + 'test_mknod-1') |
| 1170 |
assert stat.S_ISREG(st.st_mode) |
| 1171 |
|
| 1172 |
os.mknod(self.path2 + 'test_mknod-2', 0o600 | stat.S_IFIFO) |
| 1173 |
st = os.lstat(self.path2 + 'test_mknod-2') |
| 1174 |
assert stat.S_ISFIFO(st.st_mode) |
| 1175 |
|
| 1176 |
def test_mknod_with_ifchr(self): |
| 1177 |
|
| 1178 |
|
| 1179 |
os = self.posix |
| 1180 |
if hasattr(os.lstat('.'), 'st_rdev'): |
| 1181 |
import stat |
| 1182 |
try: |
| 1183 |
os.mknod(self.path2 + 'test_mknod-3', 0o600 | stat.S_IFCHR, |
| 1184 |
0x105) |
| 1185 |
except OSError as e: |
| 1186 |
skip("os.mknod() with S_IFCHR: got %r" % (e,)) |
| 1187 |
else: |
| 1188 |
st = os.lstat(self.path2 + 'test_mknod-3') |
| 1189 |
assert stat.S_ISCHR(st.st_mode) |
| 1190 |
assert st.st_rdev == 0x105 |
| 1191 |
|
| 1192 |
if hasattr(os, 'nice') and hasattr(os, 'fork') and hasattr(os, 'waitpid'): |
| 1193 |
def test_nice(self): |
| 1194 |
os = self.posix |
| 1195 |
myprio = os.nice(0) |
| 1196 |
|
| 1197 |
pid = os.fork() |
| 1198 |
if pid == 0: |
| 1199 |
res = os.nice(3) |
| 1200 |
os._exit(res) |
| 1201 |
|
| 1202 |
pid1, status1 = os.waitpid(pid, 0) |
| 1203 |
assert pid1 == pid |
| 1204 |
assert os.WIFEXITED(status1) |
| 1205 |
expected = min(myprio + 3, 19) |
| 1206 |
assert os.WEXITSTATUS(status1) == expected |
| 1207 |
|
| 1208 |
def test_symlink(self): |
| 1209 |
posix = self.posix |
| 1210 |
import stat |
| 1211 |
|
| 1212 |
def islink(path): |
| 1213 |
"""Test whether a path is a symbolic link. |
| 1214 |
Taken from ntpath.py |
| 1215 |
""" |
| 1216 |
try: |
| 1217 |
st = posix.lstat(path) |
| 1218 |
except (OSError, ValueError, AttributeError): |
| 1219 |
return False |
| 1220 |
return stat.S_ISLNK(st.st_mode) |
| 1221 |
|
| 1222 |
bytes_dir = self.bytes_dir |
| 1223 |
if bytes_dir is None: |
| 1224 |
skip("encoding not good enough") |
| 1225 |
dest = bytes_dir + b"/file.txt" |
| 1226 |
posix.symlink(bytes_dir + b"/somefile", dest) |
| 1227 |
try: |
| 1228 |
assert islink(dest) |
| 1229 |
with open(dest) as f: |
| 1230 |
data = f.read() |
| 1231 |
assert data == "who cares?" |
| 1232 |
finally: |
| 1233 |
posix.unlink(dest) |
| 1234 |
posix.symlink(memoryview(bytes_dir + b"/somefile"), dest) |
| 1235 |
try: |
| 1236 |
assert islink(dest) |
| 1237 |
with open(dest) as f: |
| 1238 |
data = f.read() |
| 1239 |
assert data == "who cares?" |
| 1240 |
finally: |
| 1241 |
posix.unlink(dest) |
| 1242 |
|
| 1243 |
dest = bytes_dir + b"_sym" |
| 1244 |
posix.symlink(bytes_dir, dest) |
| 1245 |
try: |
| 1246 |
assert islink(dest) |
| 1247 |
finally: |
| 1248 |
posix.unlink(dest) |
| 1249 |
|
| 1250 |
|
| 1251 |
@pytest.mark.skipif("sys.platform == 'win32'") |
| 1252 |
def test_symlink_fd(self): |
| 1253 |
posix = self.posix |
| 1254 |
bytes_dir = self.bytes_dir |
| 1255 |
f = posix.open(bytes_dir, posix.O_RDONLY) |
| 1256 |
try: |
| 1257 |
posix.symlink('somefile', 'somelink', dir_fd=f) |
| 1258 |
assert (posix.readlink(bytes_dir + '/somelink'.encode()) == |
| 1259 |
'somefile'.encode()) |
| 1260 |
finally: |
| 1261 |
posix.close(f) |
| 1262 |
posix.unlink(bytes_dir + '/somelink'.encode()) |
| 1263 |
|
| 1264 |
def test_symlink_fspath(self): |
| 1265 |
posix = self.posix |
| 1266 |
bytes_dir = self.bytes_dir |
| 1267 |
if bytes_dir is None: |
| 1268 |
skip("encoding not good enough") |
| 1269 |
dest = self.Path(bytes_dir + b"/file.txt") |
| 1270 |
posix.symlink(self.Path(bytes_dir + b"/somefile"), dest) |
| 1271 |
try: |
| 1272 |
with open(dest) as f: |
| 1273 |
data = f.read() |
| 1274 |
assert data == "who cares?" |
| 1275 |
finally: |
| 1276 |
posix.unlink(dest) |
| 1277 |
|
| 1278 |
def test_readlink(self): |
| 1279 |
os = self.posix |
| 1280 |
pdir = self.pdir |
| 1281 |
src = pdir + "/somefile" |
| 1282 |
dest = pdir + self.sep + "file.txt" |
| 1283 |
if self.sep != "/": |
| 1284 |
|
| 1285 |
dest = "\\\\?\\" + dest |
| 1286 |
os.symlink(dest, src) |
| 1287 |
assert os.readlink(src) == dest |
| 1288 |
assert os.readlink(src.encode()) == dest.encode() |
| 1289 |
assert os.readlink(self.Path(src)) == dest |
| 1290 |
assert os.readlink(self.Path(src.encode())) == dest.encode() |
| 1291 |
|
| 1292 |
if hasattr(os, 'ftruncate'): |
| 1293 |
def test_truncate(self): |
| 1294 |
posix = self.posix |
| 1295 |
dest = self.path2 |
| 1296 |
|
| 1297 |
def mkfile(dest, size=4): |
| 1298 |
with open(dest, 'wb') as f: |
| 1299 |
f.write(b'd' * size) |
| 1300 |
|
| 1301 |
|
| 1302 |
mkfile(dest) |
| 1303 |
with raises(OSError): |
| 1304 |
posix.truncate(dest, -1) |
| 1305 |
with open(dest, 'rb') as f: |
| 1306 |
with raises(OSError): |
| 1307 |
posix.truncate(f.fileno(), 1) |
| 1308 |
with raises(TypeError): |
| 1309 |
posix.truncate(dest, None) |
| 1310 |
with raises(TypeError): |
| 1311 |
posix.truncate(None, None) |
| 1312 |
|
| 1313 |
|
| 1314 |
mkfile(dest) |
| 1315 |
with open(dest, 'wb') as f: |
| 1316 |
posix.truncate(f.fileno(), 1) |
| 1317 |
assert 1 == posix.stat(dest).st_size |
| 1318 |
|
| 1319 |
|
| 1320 |
mkfile(dest) |
| 1321 |
posix.truncate(dest, 1) |
| 1322 |
assert 1 == posix.stat(dest).st_size |
| 1323 |
|
| 1324 |
|
| 1325 |
with raises(OSError) as e: |
| 1326 |
posix.truncate(dest + '-DOESNT-EXIST', 0) |
| 1327 |
assert e.value.filename == dest + '-DOESNT-EXIST' |
| 1328 |
posix = self.posix |
| 1329 |
dest = self.path2 |
| 1330 |
|
| 1331 |
def mkfile(dest, size=4): |
| 1332 |
with open(dest, 'wb') as f: |
| 1333 |
f.write(b'd' * size) |
| 1334 |
|
| 1335 |
|
| 1336 |
mkfile(dest) |
| 1337 |
with raises(OSError): |
| 1338 |
posix.truncate(dest, -1) |
| 1339 |
with open(dest, 'rb') as f: |
| 1340 |
with raises(OSError): |
| 1341 |
posix.truncate(f.fileno(), 1) |
| 1342 |
with raises(TypeError): |
| 1343 |
posix.truncate(dest, None) |
| 1344 |
with raises(TypeError): |
| 1345 |
posix.truncate(None, None) |
| 1346 |
|
| 1347 |
|
| 1348 |
mkfile(dest) |
| 1349 |
with open(dest, 'wb') as f: |
| 1350 |
posix.truncate(f.fileno(), 1) |
| 1351 |
assert 1 == posix.stat(dest).st_size |
| 1352 |
|
| 1353 |
|
| 1354 |
mkfile(dest) |
| 1355 |
posix.truncate(dest, 1) |
| 1356 |
assert 1 == posix.stat(dest).st_size |
| 1357 |
|
| 1358 |
|
| 1359 |
with raises(OSError) as e: |
| 1360 |
posix.truncate(dest + '-DOESNT-EXIST', 0) |
| 1361 |
assert e.value.filename == dest + '-DOESNT-EXIST' |
| 1362 |
|
| 1363 |
def test_getlogin(self): |
| 1364 |
|
| 1365 |
|
| 1366 |
skip("fails inside docker on buildbots") |
| 1367 |
assert isinstance(self.posix.getlogin(), str) |
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
def test_getppid(self): |
| 1372 |
ppid = self.posix.getppid() |
| 1373 |
assert isinstance(ppid, int) |
| 1374 |
assert ppid != 0 |
| 1375 |
assert ppid != -1 |
| 1376 |
|
| 1377 |
def test_has_kill(self): |
| 1378 |
os = self.posix |
| 1379 |
assert hasattr(os, 'kill') |
| 1380 |
|
| 1381 |
def test_pipe_flush(self): |
| 1382 |
import io |
| 1383 |
ffd, gfd = self.posix.pipe() |
| 1384 |
f = io.open(ffd, 'r') |
| 1385 |
g = io.open(gfd, 'w') |
| 1386 |
g.write('he') |
| 1387 |
g.flush() |
| 1388 |
x = f.read(1) |
| 1389 |
assert x == 'h' |
| 1390 |
f.flush() |
| 1391 |
x = f.read(1) |
| 1392 |
assert x == 'e' |
| 1393 |
|
| 1394 |
def test_pipe_inheritable(self): |
| 1395 |
fd1, fd2 = self.posix.pipe() |
| 1396 |
assert self.posix.get_inheritable(fd1) == False |
| 1397 |
assert self.posix.get_inheritable(fd2) == False |
| 1398 |
self.posix.close(fd1) |
| 1399 |
self.posix.close(fd2) |
| 1400 |
|
| 1401 |
def test_pipe2(self): |
| 1402 |
if not hasattr(self.posix, 'pipe2'): |
| 1403 |
skip("no pipe2") |
| 1404 |
fd1, fd2 = self.posix.pipe2(0) |
| 1405 |
assert self.posix.get_inheritable(fd1) == True |
| 1406 |
assert self.posix.get_inheritable(fd2) == True |
| 1407 |
self.posix.close(fd1) |
| 1408 |
self.posix.close(fd2) |
| 1409 |
|
| 1410 |
def test_O_CLOEXEC(self): |
| 1411 |
if not hasattr(self.posix, 'pipe2'): |
| 1412 |
skip("no pipe2") |
| 1413 |
if not hasattr(self.posix, 'O_CLOEXEC'): |
| 1414 |
skip("no O_CLOEXEC") |
| 1415 |
fd1, fd2 = self.posix.pipe2(self.posix.O_CLOEXEC) |
| 1416 |
assert self.posix.get_inheritable(fd1) == False |
| 1417 |
assert self.posix.get_inheritable(fd2) == False |
| 1418 |
self.posix.close(fd1) |
| 1419 |
self.posix.close(fd2) |
| 1420 |
|
| 1421 |
def test_dup2_inheritable(self): |
| 1422 |
fd1, fd2 = self.posix.pipe() |
| 1423 |
assert self.posix.get_inheritable(fd2) == False |
| 1424 |
self.posix.dup2(fd1, fd2) |
| 1425 |
assert self.posix.get_inheritable(fd2) == True |
| 1426 |
self.posix.dup2(fd1, fd2, False) |
| 1427 |
assert self.posix.get_inheritable(fd2) == False |
| 1428 |
self.posix.dup2(fd1, fd2, True) |
| 1429 |
assert self.posix.get_inheritable(fd2) == True |
| 1430 |
self.posix.close(fd1) |
| 1431 |
self.posix.close(fd2) |
| 1432 |
|
| 1433 |
def test_open_inheritable(self): |
| 1434 |
os = self.posix |
| 1435 |
fd = os.open(self.path2 + 'test_open_inheritable', |
| 1436 |
os.O_RDWR | os.O_CREAT, 0o666) |
| 1437 |
assert os.get_inheritable(fd) == False |
| 1438 |
os.close(fd) |
| 1439 |
|
| 1440 |
if sys.platform != 'win32': |
| 1441 |
def test_sync(self): |
| 1442 |
self.posix.sync() |
| 1443 |
|
| 1444 |
def test_blocking(self): |
| 1445 |
posix = self.posix |
| 1446 |
fd = posix.open(self.path, posix.O_RDONLY) |
| 1447 |
assert posix.get_blocking(fd) is True |
| 1448 |
posix.set_blocking(fd, False) |
| 1449 |
assert posix.get_blocking(fd) is False |
| 1450 |
posix.set_blocking(fd, True) |
| 1451 |
assert posix.get_blocking(fd) is True |
| 1452 |
posix.close(fd) |
| 1453 |
|
| 1454 |
def test_blocking_error(self): |
| 1455 |
posix = self.posix |
| 1456 |
with raises(OSError): |
| 1457 |
posix.get_blocking(1234567) |
| 1458 |
with raises(OSError): |
| 1459 |
posix.set_blocking(1234567, True) |
| 1460 |
|
| 1461 |
def test_sendfile(self): |
| 1462 |
import _socket, posix |
| 1463 |
s1, s2 = _socket.socketpair() |
| 1464 |
fd = posix.open(self.path, posix.O_RDONLY) |
| 1465 |
res = posix.sendfile(s1.fileno(), fd, 3, 5) |
| 1466 |
assert res == 5 |
| 1467 |
assert posix.lseek(fd, 0, 1) == 0 |
| 1468 |
data = s2.recv(10) |
| 1469 |
expected = b'this is a test'[3:8] |
| 1470 |
assert data == expected |
| 1471 |
posix.close(fd) |
| 1472 |
s2.close() |
| 1473 |
s1.close() |
| 1474 |
|
| 1475 |
def test_filename_can_be_a_buffer(self): |
| 1476 |
import posix, sys |
| 1477 |
fsencoding = sys.getfilesystemencoding() |
| 1478 |
pdir = (self.pdir + '/file1').encode(fsencoding) |
| 1479 |
fd = posix.open(pdir, posix.O_RDONLY) |
| 1480 |
posix.close(fd) |
| 1481 |
fd = posix.open(memoryview(pdir), posix.O_RDONLY) |
| 1482 |
posix.close(fd) |
| 1483 |
|
| 1484 |
def test_getgrouplist(self): |
| 1485 |
import posix, getpass |
| 1486 |
gid = posix.getgid() |
| 1487 |
user = getpass.getuser() |
| 1488 |
groups = posix.getgrouplist(user, gid) |
| 1489 |
assert gid in groups |
| 1490 |
|
| 1491 |
if sys.platform.startswith('linux'): |
| 1492 |
def test_sendfile_no_offset(self): |
| 1493 |
import _socket, posix |
| 1494 |
s1, s2 = _socket.socketpair() |
| 1495 |
fd = posix.open(self.path, posix.O_RDONLY) |
| 1496 |
posix.lseek(fd, 3, 0) |
| 1497 |
res = posix.sendfile(s1.fileno(), fd, None, 5) |
| 1498 |
assert res == 5 |
| 1499 |
assert posix.lseek(fd, 0, 1) == 8 |
| 1500 |
data = s2.recv(10) |
| 1501 |
expected = b'this is a test'[3:8] |
| 1502 |
assert data == expected |
| 1503 |
posix.close(fd) |
| 1504 |
s2.close() |
| 1505 |
s1.close() |
| 1506 |
|
| 1507 |
def test_os_lockf(self): |
| 1508 |
posix = os = self.posix |
| 1509 |
fd = os.open(self.path2 + 'test_os_lockf', os.O_WRONLY | os.O_CREAT) |
| 1510 |
try: |
| 1511 |
os.write(fd, b'test') |
| 1512 |
os.lseek(fd, 0, 0) |
| 1513 |
posix.lockf(fd, posix.F_LOCK, 4) |
| 1514 |
posix.lockf(fd, posix.F_ULOCK, 4) |
| 1515 |
finally: |
| 1516 |
os.close(fd) |
| 1517 |
|
| 1518 |
def test_urandom(self): |
| 1519 |
os = self.posix |
| 1520 |
raises(ValueError, os.urandom, -1) |
| 1521 |
s = os.urandom(5) |
| 1522 |
assert isinstance(s, bytes) |
| 1523 |
assert len(s) == 5 |
| 1524 |
for x in range(50): |
| 1525 |
if s != os.urandom(5): |
| 1526 |
break |
| 1527 |
else: |
| 1528 |
assert False, "urandom() always returns the same string" |
| 1529 |
|
| 1530 |
|
| 1531 |
if hasattr(os, 'startfile'): |
| 1532 |
def test_startfile(self): |
| 1533 |
if not self.runappdirect: |
| 1534 |
skip("should not try to import cffi at app-level") |
| 1535 |
startfile = self.posix.startfile |
| 1536 |
for t1 in [str, bytes]: |
| 1537 |
for t2 in [str, bytes]: |
| 1538 |
with raises(WindowsError) as e: |
| 1539 |
startfile(t1("\\"), t2("close")) |
| 1540 |
assert e.value.args[0] == 1155 |
| 1541 |
assert e.value.args[1] == ( |
| 1542 |
"No application is associated with the " |
| 1543 |
"specified file for this operation") |
| 1544 |
if len(e.value.args) > 2: |
| 1545 |
assert e.value.args[2] == t1("\\") |
| 1546 |
|
| 1547 |
with raises(WindowsError) as e: |
| 1548 |
startfile("\\foo\\bar\\baz") |
| 1549 |
assert e.value.args[0] == 2 |
| 1550 |
assert e.value.args[1] == ( |
| 1551 |
"The system cannot find the file specified") |
| 1552 |
if len(e.value.args) > 2: |
| 1553 |
assert e.value.args[2] == "\\foo\\bar\\baz" |
| 1554 |
|
| 1555 |
@pytest.mark.skipif("sys.platform != 'win32'") |
| 1556 |
def test_rename(self): |
| 1557 |
os = self.posix |
| 1558 |
fname = self.path2 + 'rename.txt' |
| 1559 |
with open(fname, "w") as f: |
| 1560 |
f.write("this is a rename test") |
| 1561 |
str_name = str(self.pdir) + '/test_rename.txt' |
| 1562 |
os.rename(fname, str_name) |
| 1563 |
with open(str_name) as f: |
| 1564 |
assert f.read() == 'this is a rename test' |
| 1565 |
os.rename(str_name, fname) |
| 1566 |
unicode_name = str(self.udir) + u'/test\u03be.txt' |
| 1567 |
os.rename(fname, unicode_name) |
| 1568 |
with open(unicode_name) as f: |
| 1569 |
assert f.read() == 'this is a rename test' |
| 1570 |
os.rename(unicode_name, fname) |
| 1571 |
|
| 1572 |
os.rename(bytes(fname, 'utf-8'), bytes(str_name, 'utf-8')) |
| 1573 |
with open(str_name) as f: |
| 1574 |
assert f.read() == 'this is a rename test' |
| 1575 |
os.rename(str_name, fname) |
| 1576 |
with open(fname) as f: |
| 1577 |
assert f.read() == 'this is a rename test' |
| 1578 |
os.unlink(fname) |
| 1579 |
|
| 1580 |
|
| 1581 |
def test_device_encoding(self): |
| 1582 |
import sys |
| 1583 |
encoding = self.posix.device_encoding(sys.stdout.fileno()) |
| 1584 |
|
| 1585 |
|
| 1586 |
assert encoding is None or type(encoding) is str |
| 1587 |
if encoding and self.posix.__name__ != "nt": |
| 1588 |
assert encoding == "utf-8" |
| 1589 |
|
| 1590 |
if os.name == 'nt': |
| 1591 |
def test__getfileinformation(self): |
| 1592 |
os = self.posix |
| 1593 |
path = '\\'.join([self.pdir, 'file1']) |
| 1594 |
with open(path) as fp: |
| 1595 |
info = self.posix._getfileinformation(fp.fileno()) |
| 1596 |
assert len(info) == 3 |
| 1597 |
assert all(isinstance(obj, int) for obj in info) |
| 1598 |
|
| 1599 |
def test__getfinalpathname(self): |
| 1600 |
os = self.posix |
| 1601 |
path = '\\'.join([self.pdir, 'file1']) |
| 1602 |
try: |
| 1603 |
result = self.posix._getfinalpathname(path) |
| 1604 |
except NotImplementedError: |
| 1605 |
skip("_getfinalpathname not supported on this platform") |
| 1606 |
assert os.stat(result) is not None |
| 1607 |
|
| 1608 |
@py.test.mark.skipif("sys.platform == 'win32'") |
| 1609 |
def test_rtld_constants(self): |
| 1610 |
|
| 1611 |
self.posix.RTLD_LAZY |
| 1612 |
self.posix.RTLD_NOW |
| 1613 |
self.posix.RTLD_GLOBAL |
| 1614 |
self.posix.RTLD_LOCAL |
| 1615 |
|
| 1616 |
@py.test.mark.skipif("sys.platform != 'win32'") |
| 1617 |
def test_win_constants(self): |
| 1618 |
win_constants =['_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS', |
| 1619 |
'_LOAD_LIBRARY_SEARCH_APPLICATION_DIR', |
| 1620 |
'_LOAD_LIBRARY_SEARCH_SYSTEM32', |
| 1621 |
'_LOAD_LIBRARY_SEARCH_USER_DIRS', |
| 1622 |
'_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR', |
| 1623 |
] |
| 1624 |
for name in win_constants: |
| 1625 |
print(name, getattr(self.posix, name)) |
| 1626 |
|
| 1627 |
@py.test.mark.skipif("sys.platform != 'darwin'") |
| 1628 |
def test_darwin_constants(self): |
| 1629 |
darwin_constants =['_COPYFILE_DATA'] |
| 1630 |
for name in darwin_constants: |
| 1631 |
getattr(self.posix, name) |
| 1632 |
|
| 1633 |
|
| 1634 |
def test_error_message(self): |
| 1635 |
import sys |
| 1636 |
with raises(OSError) as e: |
| 1637 |
self.posix.open('nonexistentfile1', 0) |
| 1638 |
assert str(e.value).endswith(": 'nonexistentfile1'") |
| 1639 |
|
| 1640 |
with raises(OSError) as e: |
| 1641 |
self.posix.link('nonexistentfile1', 'bok') |
| 1642 |
assert str(e.value).endswith(": 'nonexistentfile1' -> 'bok'") |
| 1643 |
with raises(OSError) as e: |
| 1644 |
self.posix.rename('nonexistentfile1', 'bok') |
| 1645 |
assert str(e.value).endswith(": 'nonexistentfile1' -> 'bok'") |
| 1646 |
with raises(OSError) as e: |
| 1647 |
self.posix.replace('nonexistentfile1', 'bok') |
| 1648 |
assert str(e.value).endswith(": 'nonexistentfile1' -> 'bok'") |
| 1649 |
|
| 1650 |
if sys.platform != 'win32': |
| 1651 |
with raises(OSError) as e: |
| 1652 |
self.posix.symlink('bok', '/nonexistentdir/boz') |
| 1653 |
assert str(e.value).endswith(": 'bok' -> '/nonexistentdir/boz'") |
| 1654 |
|
| 1655 |
def test_os_fspath(self): |
| 1656 |
assert hasattr(self.posix, 'fspath') |
| 1657 |
with raises(TypeError): |
| 1658 |
self.posix.fspath(None) |
| 1659 |
with raises(TypeError) as e: |
| 1660 |
self.posix.fspath(42) |
| 1661 |
assert str(e.value).endswith('int') |
| 1662 |
string = 'string' |
| 1663 |
assert self.posix.fspath(string) == string |
| 1664 |
assert self.posix.fspath(b'bytes') == b'bytes' |
| 1665 |
class Sample: |
| 1666 |
def __fspath__(self): |
| 1667 |
return 'sample' |
| 1668 |
|
| 1669 |
assert self.posix.fspath(Sample()) == 'sample' |
| 1670 |
|
| 1671 |
class BSample: |
| 1672 |
def __fspath__(self): |
| 1673 |
return b'binary sample' |
| 1674 |
|
| 1675 |
assert self.posix.fspath(BSample()) == b'binary sample' |
| 1676 |
|
| 1677 |
class WrongSample: |
| 1678 |
def __fspath__(self): |
| 1679 |
return 4 |
| 1680 |
|
| 1681 |
with raises(TypeError): |
| 1682 |
self.posix.fspath(WrongSample()) |
| 1683 |
with raises(OSError): |
| 1684 |
self.posix.replace(self.Path('nonexistentfile1'), 'bok') |
| 1685 |
|
| 1686 |
if hasattr(rposix, 'getxattr'): |
| 1687 |
def test_xattr_simple(self): |
| 1688 |
|
| 1689 |
os = self.posix |
| 1690 |
with open(self.path, 'wb'): |
| 1691 |
pass |
| 1692 |
init_names = os.listxattr(self.path) |
| 1693 |
with raises(OSError) as excinfo: |
| 1694 |
os.getxattr(self.path, 'user.test') |
| 1695 |
assert excinfo.value.filename == self.path |
| 1696 |
os.setxattr(self.path, 'user.test', b'', os.XATTR_CREATE, follow_symlinks=False) |
| 1697 |
with raises(OSError): |
| 1698 |
os.setxattr(self.path, 'user.test', b'', os.XATTR_CREATE) |
| 1699 |
assert os.getxattr(self.path, 'user.test') == b'' |
| 1700 |
os.setxattr(self.path, b'user.test', b'foo', os.XATTR_REPLACE) |
| 1701 |
assert os.getxattr(self.path, 'user.test', follow_symlinks=False) == b'foo' |
| 1702 |
assert set(os.listxattr(self.path)) == set( |
| 1703 |
init_names + ['user.test']) |
| 1704 |
os.removexattr(self.path, 'user.test', follow_symlinks=False) |
| 1705 |
with raises(OSError): |
| 1706 |
os.getxattr(self.path, 'user.test') |
| 1707 |
assert os.listxattr(self.path, follow_symlinks=False) == init_names |
| 1708 |
|
| 1709 |
if hasattr(rposix, 'memfd_create'): |
| 1710 |
|
| 1711 |
def test_memfd_create(self): |
| 1712 |
os = self.posix |
| 1713 |
fd = os.memfd_create("abc") |
| 1714 |
try: |
| 1715 |
s = b"defghi?" |
| 1716 |
os.write(fd, s) |
| 1717 |
finally: |
| 1718 |
os.close(fd) |
| 1719 |
|
| 1720 |
def test_memfd_create_error(self): |
| 1721 |
import errno |
| 1722 |
os = self.posix |
| 1723 |
with raises(OSError) as exc_info: |
| 1724 |
fd = os.memfd_create("abc", flags=-1) |
| 1725 |
assert exc_info.value.errno == errno.EINVAL |
| 1726 |
|
| 1727 |
def test_get_terminal_size(self): |
| 1728 |
os = self.posix |
| 1729 |
for args in [(), (1,), (0,), (42421,)]: |
| 1730 |
try: |
| 1731 |
w, h = os.get_terminal_size(*args) |
| 1732 |
except (ValueError, OSError): |
| 1733 |
continue |
| 1734 |
assert isinstance(w, int) |
| 1735 |
assert isinstance(h, int) |
| 1736 |
|
| 1737 |
def test_scandir(self): |
| 1738 |
retU = [x.name for x in self.posix.scandir(u'.')] |
| 1739 |
retP = [x.name for x in self.posix.scandir(self.Path('.'))] |
| 1740 |
assert retU == retP |
| 1741 |
|
| 1742 |
def test_scandir_fd(self): |
| 1743 |
os = self.posix |
| 1744 |
fd = None |
| 1745 |
try: |
| 1746 |
fd = os.open(self.Path('.'), os.O_RDONLY) |
| 1747 |
except PermissionError: |
| 1748 |
skip("Cannot open '.'") |
| 1749 |
try: |
| 1750 |
with os.scandir(fd) as it: |
| 1751 |
entries = list(it) |
| 1752 |
names = os.listdir(fd) |
| 1753 |
assert len(entries) == len(names) |
| 1754 |
finally: |
| 1755 |
os.close(fd) |
| 1756 |
|
| 1757 |
def test_execv_no_args(self): |
| 1758 |
posix = self.posix |
| 1759 |
with raises(ValueError): |
| 1760 |
posix.execv("notepad", []) |
| 1761 |
|
| 1762 |
with raises(ValueError): |
| 1763 |
posix.execve("notepad", [], {}) |
| 1764 |
|
| 1765 |
def test_execv_bad_args(self): |
| 1766 |
posix = self.posix |
| 1767 |
with raises(ValueError): |
| 1768 |
posix.execv("notepad", ('',)) |
| 1769 |
with raises(OSError): |
| 1770 |
posix.execv("notepad", (' ',)) |
| 1771 |
|
| 1772 |
def test_execve_invalid_env(self): |
| 1773 |
import sys |
| 1774 |
os = self.posix |
| 1775 |
args = ['notepad', '-c', 'pass'] |
| 1776 |
newenv = os.environ.copy() |
| 1777 |
newenv["FRUIT=VEGETABLE"] = "cabbage" |
| 1778 |
with raises(ValueError): |
| 1779 |
os.execve(args[0], args, newenv) |
| 1780 |
|
| 1781 |
|
| 1782 |
@py.test.mark.skipif("sys.platform != 'win32'") |
| 1783 |
class AppTestNt(object): |
| 1784 |
spaceconfig = {'usemodules': USEMODULES} |
| 1785 |
def setup_class(cls): |
| 1786 |
space = cls.space |
| 1787 |
cls.w_path = space.wrap(str(path)) |
| 1788 |
cls.w_posix = space.appexec([], GET_POSIX) |
| 1789 |
cls.w_Path = space.appexec([], """(): |
| 1790 |
class Path: |
| 1791 |
def __init__(self, _path): |
| 1792 |
self._path =_path |
| 1793 |
def __fspath__(self): |
| 1794 |
return self._path |
| 1795 |
return Path |
| 1796 |
""") |
| 1797 |
|
| 1798 |
|
| 1799 |
def test_handle_inheritable(self): |
| 1800 |
import _socket |
| 1801 |
posix = self.posix |
| 1802 |
if hasattr(posix, 'get_handle_inheritable'): |
| 1803 |
|
| 1804 |
s = _socket.socket() |
| 1805 |
assert not posix.get_handle_inheritable(s.fileno()) |
| 1806 |
posix.set_handle_inheritable(s.fileno(), True) |
| 1807 |
assert posix.get_handle_inheritable(s.fileno()) |
| 1808 |
|
| 1809 |
def test__getfullpathname(self): |
| 1810 |
|
| 1811 |
nt = self.posix |
| 1812 |
path = nt._getfullpathname(self.path) |
| 1813 |
assert self.path in path |
| 1814 |
path = nt._getfullpathname(self.Path(self.path)) |
| 1815 |
assert self.path in path |
| 1816 |
|
| 1817 |
|
| 1818 |
bpath = self.path.encode() |
| 1819 |
path = nt._getfullpathname(bpath) |
| 1820 |
assert bpath in path |
| 1821 |
path = nt._getfullpathname(self.Path(bpath)) |
| 1822 |
assert bpath in path |
| 1823 |
|
| 1824 |
with raises(TypeError): |
| 1825 |
nt._getfullpathname(None) |
| 1826 |
|
| 1827 |
with raises(TypeError): |
| 1828 |
nt._getfullpathname(1) |
| 1829 |
|
| 1830 |
sysdrv = nt.environ.get("SystemDrive", "C:") |
| 1831 |
|
| 1832 |
path = sysdrv + 'hubber' |
| 1833 |
assert '\\' in nt._getfullpathname(path) |
| 1834 |
assert type(nt._getfullpathname(b'C:')) is bytes |
| 1835 |
|
| 1836 |
def test__path_splitroot(self): |
| 1837 |
nt = self.posix |
| 1838 |
ret = nt._path_splitroot(u'c:\\abc\\def.txt') |
| 1839 |
assert ret == (u'c:\\', u'abc\\def.txt') |
| 1840 |
ret = nt._path_splitroot(u'//server/abc/xyz/def.txt') |
| 1841 |
assert ret == (u'//server/abc/', u'xyz/def.txt') |
| 1842 |
|
| 1843 |
def test_dll_directory(self): |
| 1844 |
nt = self.posix |
| 1845 |
ret = nt._add_dll_directory(b'c:\\') |
| 1846 |
assert nt._remove_dll_directory(ret) |
| 1847 |
ret = nt._add_dll_directory(u'c:\\') |
| 1848 |
assert nt._remove_dll_directory(ret) |
| 1849 |
|
| 1850 |
|
| 1851 |
class AppTestEnvironment(object): |
| 1852 |
def setup_class(cls): |
| 1853 |
space = cls.space |
| 1854 |
cls.w_path = space.wrap(str(path)) |
| 1855 |
cls.w_posix = space.appexec([], GET_POSIX) |
| 1856 |
cls.w_python = space.wrap(sys.executable) |
| 1857 |
|
| 1858 |
def test_environ(self): |
| 1859 |
import sys |
| 1860 |
environ = self.posix.environ |
| 1861 |
if not environ: |
| 1862 |
skip('environ not filled in for untranslated tests') |
| 1863 |
if sys.platform == 'win32': |
| 1864 |
rawenv = str |
| 1865 |
else: |
| 1866 |
rawenv = bytes |
| 1867 |
for k, v in environ.items(): |
| 1868 |
assert type(k) is rawenv |
| 1869 |
assert type(v) is rawenv |
| 1870 |
name = next(iter(environ)) |
| 1871 |
assert environ[name] is not None |
| 1872 |
del environ[name] |
| 1873 |
with raises(KeyError): |
| 1874 |
environ[name] |
| 1875 |
|
| 1876 |
@pytest.mark.dont_track_allocations('putenv intentionally keeps strings alive') |
| 1877 |
def test_environ_nonascii(self): |
| 1878 |
import sys |
| 1879 |
os = self.posix |
| 1880 |
name, value = 'PYPY_TEST_日本', 'foobar日本' |
| 1881 |
if not sys.platform == 'win32': |
| 1882 |
fsencoding = sys.getfilesystemencoding() |
| 1883 |
for s in name, value: |
| 1884 |
try: |
| 1885 |
s.encode(fsencoding, 'surrogateescape') |
| 1886 |
except UnicodeEncodeError: |
| 1887 |
skip("Requires %s.encode(sys.getfilesystemencoding(), " |
| 1888 |
"'surogateescape') to succeed (or win32)" % ascii(s)) |
| 1889 |
|
| 1890 |
os.environ[name] = value |
| 1891 |
assert os.environ[name] == value |
| 1892 |
del os.environ[name] |
| 1893 |
assert os.environ.get(name) is None |
| 1894 |
|
| 1895 |
def test_unsetenv_nonexisting(self): |
| 1896 |
os = self.posix |
| 1897 |
os.unsetenv("XYZABC") |
| 1898 |
try: |
| 1899 |
os.environ["ABCABC"] |
| 1900 |
except KeyError: |
| 1901 |
pass |
| 1902 |
else: |
| 1903 |
raise AssertionError("did not raise KeyError") |
| 1904 |
os.environ["ABCABC"] = "1" |
| 1905 |
assert os.environ["ABCABC"] == "1" |
| 1906 |
os.unsetenv("ABCABC") |
| 1907 |
cmd = ('%s -c "import os, sys; ' |
| 1908 |
'sys.exit(int(\'ABCABC\' in os.environ))" ' |
| 1909 |
% self.python) |
| 1910 |
res = os.system(cmd) |
| 1911 |
assert res == 0 |
| 1912 |
|
| 1913 |
|
| 1914 |
@py.test.fixture |
| 1915 |
def check_fsencoding(space, pytestconfig): |
| 1916 |
if pytestconfig.getvalue('runappdirect'): |
| 1917 |
fsencoding = sys.getfilesystemencoding() |
| 1918 |
else: |
| 1919 |
fsencoding = space.sys.filesystemencoding |
| 1920 |
try: |
| 1921 |
u"ą".encode(fsencoding) |
| 1922 |
except UnicodeEncodeError: |
| 1923 |
py.test.skip("encoding not good enough") |
| 1924 |
|
| 1925 |
@py.test.mark.usefixtures('check_fsencoding') |
| 1926 |
class AppTestPosixUnicode: |
| 1927 |
spaceconfig = {'usemodules': USEMODULES} |
| 1928 |
def setup_class(cls): |
| 1929 |
space = cls.space |
| 1930 |
cls.w_posix = space.appexec([], GET_POSIX) |
| 1931 |
|
| 1932 |
def test_stat_unicode(self): |
| 1933 |
|
| 1934 |
try: |
| 1935 |
self.posix.stat(u"ą") |
| 1936 |
except OSError: |
| 1937 |
pass |
| 1938 |
|
| 1939 |
def test_open_unicode(self): |
| 1940 |
os = self.posix |
| 1941 |
|
| 1942 |
try: |
| 1943 |
os.open(u"ą", os.O_WRONLY) |
| 1944 |
except OSError: |
| 1945 |
pass |
| 1946 |
|
| 1947 |
def test_remove_unicode(self): |
| 1948 |
|
| 1949 |
try: |
| 1950 |
self.posix.remove(u"ą") |
| 1951 |
except OSError: |
| 1952 |
pass |
| 1953 |
|
| 1954 |
|
| 1955 |
class AppTestUnicodeFilename: |
| 1956 |
def setup_class(cls): |
| 1957 |
ufilename = (unicode(udir.join('test_unicode_filename_')) + |
| 1958 |
u'\u65e5\u672c.txt') |
| 1959 |
try: |
| 1960 |
f = open(ufilename, 'w') |
| 1961 |
except (UnicodeEncodeError, IOError): |
| 1962 |
pytest.skip("encoding not good enough") |
| 1963 |
f.write("test") |
| 1964 |
f.close() |
| 1965 |
space = cls.space |
| 1966 |
cls.w_filename = space.wrap(ufilename) |
| 1967 |
cls.w_posix = space.appexec([], GET_POSIX) |
| 1968 |
|
| 1969 |
def test_open(self): |
| 1970 |
fd = self.posix.open(self.filename, self.posix.O_RDONLY) |
| 1971 |
try: |
| 1972 |
content = self.posix.read(fd, 50) |
| 1973 |
finally: |
| 1974 |
self.posix.close(fd) |
| 1975 |
assert content == b"test" |
| 1976 |
|
| 1977 |
|
| 1978 |
class AppTestPep475Retry: |
| 1979 |
spaceconfig = {'usemodules': USEMODULES} |
| 1980 |
|
| 1981 |
def setup_class(cls): |
| 1982 |
if os.name != 'posix': |
| 1983 |
pytest.skip("xxx tests are posix-only") |
| 1984 |
if cls.runappdirect: |
| 1985 |
skip("xxx does not work with -A") |
| 1986 |
|
| 1987 |
def fd_data_after_delay(space): |
| 1988 |
g = os.popen("sleep 5 && echo hello", "r") |
| 1989 |
cls._keepalive_g = g |
| 1990 |
return space.wrap(g.fileno()) |
| 1991 |
|
| 1992 |
space = cls.space |
| 1993 |
cls.w_posix = space.appexec([], GET_POSIX) |
| 1994 |
cls.w_fd_data_after_delay = cls.space.wrap( |
| 1995 |
interp2app(fd_data_after_delay)) |
| 1996 |
|
| 1997 |
def test_pep475_retry_read(self): |
| 1998 |
import _signal as signal |
| 1999 |
signalled = [] |
| 2000 |
|
| 2001 |
def foo(*args): |
| 2002 |
signalled.append("ALARM") |
| 2003 |
|
| 2004 |
signal.signal(signal.SIGALRM, foo) |
| 2005 |
try: |
| 2006 |
fd = self.fd_data_after_delay() |
| 2007 |
signal.alarm(1) |
| 2008 |
got = self.posix.read(fd, 100) |
| 2009 |
self.posix.close(fd) |
| 2010 |
finally: |
| 2011 |
signal.signal(signal.SIGALRM, signal.SIG_DFL) |
| 2012 |
|
| 2013 |
assert signalled != [] |
| 2014 |
assert got.startswith(b'h') |
| 2015 |
|
| 2016 |
|