| 1 |
import py |
| 2 |
import os, time, sys |
| 3 |
from rpython.tool.udir import udir |
| 4 |
from rpython.rlib.rarithmetic import r_longlong |
| 5 |
from rpython.annotator import model as annmodel |
| 6 |
from rpython.translator.c.test.test_genc import compile |
| 7 |
from rpython.translator.c.test.test_standalone import StandaloneTests |
| 8 |
posix = __import__(os.name) |
| 9 |
|
| 10 |
def test_time_clock(): |
| 11 |
def does_stuff(): |
| 12 |
t1 = t2 = time.clock() |
| 13 |
while abs(t2 - t1) < 0.01: |
| 14 |
t2 = time.clock() |
| 15 |
return t2 - t1 |
| 16 |
f1 = compile(does_stuff, []) |
| 17 |
t = f1() |
| 18 |
assert 0 < t < 1.5 |
| 19 |
|
| 20 |
def test_time_sleep(): |
| 21 |
def does_nothing(): |
| 22 |
time.sleep(0.19) |
| 23 |
f1 = compile(does_nothing, []) |
| 24 |
t0 = time.time() |
| 25 |
f1() |
| 26 |
t1 = time.time() |
| 27 |
assert t0 <= t1 |
| 28 |
assert t1 - t0 >= 0.15 |
| 29 |
|
| 30 |
|
| 31 |
def test_os_open(): |
| 32 |
tmpfile = str(udir.join('test_os_open.txt')) |
| 33 |
def does_stuff(): |
| 34 |
fd = os.open(tmpfile, os.O_WRONLY | os.O_CREAT, 0777) |
| 35 |
os.close(fd) |
| 36 |
return fd |
| 37 |
|
| 38 |
f1 = compile(does_stuff, []) |
| 39 |
fd = f1() |
| 40 |
assert os.path.exists(tmpfile) |
| 41 |
|
| 42 |
def test_failing_os_open(): |
| 43 |
tmpfile = str(udir.join('test_failing_os_open.DOESNTEXIST')) |
| 44 |
def does_stuff(): |
| 45 |
fd = os.open(tmpfile, os.O_RDONLY, 0777) |
| 46 |
return fd |
| 47 |
|
| 48 |
f1 = compile(does_stuff, []) |
| 49 |
f1(expected_exception_name='OSError') |
| 50 |
assert not os.path.exists(tmpfile) |
| 51 |
|
| 52 |
def test_open_read_write_seek_close(): |
| 53 |
filename = str(udir.join('test_open_read_write_close.txt')) |
| 54 |
def does_stuff(): |
| 55 |
fd = os.open(filename, os.O_WRONLY | os.O_CREAT, 0777) |
| 56 |
count = os.write(fd, "hello world\n") |
| 57 |
assert count == len("hello world\n") |
| 58 |
os.close(fd) |
| 59 |
fd = os.open(filename, os.O_RDONLY, 0777) |
| 60 |
result = os.lseek(fd, 1, 0) |
| 61 |
assert result == 1 |
| 62 |
data = os.read(fd, 500) |
| 63 |
assert data == "ello world\n" |
| 64 |
os.close(fd) |
| 65 |
|
| 66 |
f1 = compile(does_stuff, []) |
| 67 |
f1() |
| 68 |
with open(filename, 'r') as fid: |
| 69 |
assert fid.read() == "hello world\n" |
| 70 |
os.unlink(filename) |
| 71 |
|
| 72 |
def test_big_read(): |
| 73 |
filename = str(udir.join('test_open_read_write_close.txt')) |
| 74 |
def does_stuff(): |
| 75 |
fd = os.open(filename, os.O_WRONLY | os.O_CREAT, 0777) |
| 76 |
count = os.write(fd, "hello world\n") |
| 77 |
os.close(fd) |
| 78 |
fd = os.open(filename, os.O_RDONLY, 0777) |
| 79 |
data = os.read(fd, 500000) |
| 80 |
os.close(fd) |
| 81 |
|
| 82 |
f1 = compile(does_stuff, []) |
| 83 |
f1() |
| 84 |
os.unlink(filename) |
| 85 |
|
| 86 |
|
| 87 |
def test_ftruncate(): |
| 88 |
if not hasattr(os, 'ftruncate'): |
| 89 |
py.test.skip("this os has no ftruncate :-(") |
| 90 |
filename = str(udir.join('test_open_read_write_close.txt')) |
| 91 |
def does_stuff(): |
| 92 |
fd = os.open(filename, os.O_WRONLY | os.O_CREAT, 0777) |
| 93 |
os.write(fd, "hello world\n") |
| 94 |
os.close(fd) |
| 95 |
fd = os.open(filename, os.O_RDWR, 0777) |
| 96 |
os.ftruncate(fd, 5) |
| 97 |
data = os.read(fd, 500) |
| 98 |
assert data == "hello" |
| 99 |
os.close(fd) |
| 100 |
does_stuff() |
| 101 |
f1 = compile(does_stuff, []) |
| 102 |
f1() |
| 103 |
os.unlink(filename) |
| 104 |
|
| 105 |
def need_sparse_files(): |
| 106 |
if sys.platform == 'darwin': |
| 107 |
py.test.skip("no sparse files on default Mac OS X file system") |
| 108 |
if os.name == 'nt': |
| 109 |
py.test.skip("no sparse files on Windows") |
| 110 |
|
| 111 |
def test_largefile(): |
| 112 |
if not hasattr(os, 'ftruncate'): |
| 113 |
py.test.skip("this os has no ftruncate :-(") |
| 114 |
need_sparse_files() |
| 115 |
filename = str(udir.join('test_largefile')) |
| 116 |
r4800000000 = r_longlong(4800000000L) |
| 117 |
r4900000000 = r_longlong(4900000000L) |
| 118 |
r5000000000 = r_longlong(5000000000L) |
| 119 |
r5200000000 = r_longlong(5200000000L) |
| 120 |
r9900000000 = r_longlong(9900000000L) |
| 121 |
r10000000000 = r_longlong(10000000000L) |
| 122 |
def does_stuff(): |
| 123 |
fd = os.open(filename, os.O_RDWR | os.O_CREAT, 0666) |
| 124 |
os.ftruncate(fd, r10000000000) |
| 125 |
res = os.lseek(fd, r9900000000, 0) |
| 126 |
assert res == r9900000000 |
| 127 |
res = os.lseek(fd, -r5000000000, 1) |
| 128 |
assert res == r4900000000 |
| 129 |
res = os.lseek(fd, -r5200000000, 2) |
| 130 |
assert res == r4800000000 |
| 131 |
os.close(fd) |
| 132 |
try: |
| 133 |
os.lseek(fd, 0, 0) |
| 134 |
except OSError: |
| 135 |
pass |
| 136 |
else: |
| 137 |
print "DID NOT RAISE" |
| 138 |
raise AssertionError |
| 139 |
st = os.stat(filename) |
| 140 |
assert st.st_size == r10000000000 |
| 141 |
does_stuff() |
| 142 |
os.unlink(filename) |
| 143 |
f1 = compile(does_stuff, []) |
| 144 |
f1() |
| 145 |
os.unlink(filename) |
| 146 |
|
| 147 |
def test_os_access(): |
| 148 |
filename = str(py.path.local(__file__)) |
| 149 |
def call_access(path, mode): |
| 150 |
return os.access(path, mode) |
| 151 |
f = compile(call_access, [str, int]) |
| 152 |
for mode in os.R_OK, os.W_OK, os.X_OK, (os.R_OK | os.W_OK | os.X_OK): |
| 153 |
assert f(filename, mode) == os.access(filename, mode) |
| 154 |
|
| 155 |
|
| 156 |
def test_os_stat(): |
| 157 |
filename = str(py.path.local(__file__)) |
| 158 |
has_blksize = hasattr(os.stat_result, 'st_blksize') |
| 159 |
has_blocks = hasattr(os.stat_result, 'st_blocks') |
| 160 |
def call_stat(): |
| 161 |
st = os.stat(filename) |
| 162 |
res = (st[0], st.st_ino, st.st_ctime) |
| 163 |
if has_blksize: res += (st.st_blksize,) |
| 164 |
if has_blocks: res += (st.st_blocks,) |
| 165 |
return str(res) |
| 166 |
f = compile(call_stat, []) |
| 167 |
res = eval(f()) |
| 168 |
assert res[0] == os.stat(filename).st_mode |
| 169 |
|
| 170 |
if sys.platform != 'win32': |
| 171 |
assert res[1] == os.stat(filename).st_ino |
| 172 |
st_ctime = res[2] |
| 173 |
if isinstance(st_ctime, float): |
| 174 |
assert (st_ctime - os.stat(filename).st_ctime) < 0.1 |
| 175 |
else: |
| 176 |
assert st_ctime == int(os.stat(filename).st_ctime) |
| 177 |
if has_blksize: |
| 178 |
assert res[3] == os.stat(filename).st_blksize |
| 179 |
if has_blocks: |
| 180 |
assert res[4] == os.stat(filename).st_blocks |
| 181 |
|
| 182 |
def test_os_stat_raises_winerror(): |
| 183 |
if sys.platform != 'win32': |
| 184 |
py.test.skip("no WindowsError on this platform") |
| 185 |
def call_stat(): |
| 186 |
try: |
| 187 |
os.stat("nonexistentdir/nonexistentfile") |
| 188 |
except WindowsError as e: |
| 189 |
return e.winerror |
| 190 |
return 0 |
| 191 |
f = compile(call_stat, []) |
| 192 |
res = f() |
| 193 |
expected = call_stat() |
| 194 |
assert res == expected |
| 195 |
|
| 196 |
def test_os_fstat(): |
| 197 |
if os.environ.get('PYPY_CC', '').startswith('tcc'): |
| 198 |
py.test.skip("segfault with tcc :-(") |
| 199 |
filename = str(py.path.local(__file__)) |
| 200 |
def call_fstat(): |
| 201 |
fd = os.open(filename, os.O_RDONLY, 0777) |
| 202 |
st = os.fstat(fd) |
| 203 |
os.close(fd) |
| 204 |
return str((st.st_mode, st[1], st.st_mtime)) |
| 205 |
f = compile(call_fstat, []) |
| 206 |
osstat = os.stat(filename) |
| 207 |
st_mode, st_ino, st_mtime = eval(f()) |
| 208 |
assert st_mode == osstat.st_mode |
| 209 |
if sys.platform != 'win32': |
| 210 |
assert st_ino == osstat.st_ino |
| 211 |
if isinstance(st_mtime, float): |
| 212 |
assert (st_mtime - osstat.st_mtime) < 0.1 |
| 213 |
else: |
| 214 |
assert st_mtime == int(osstat.st_mtime) |
| 215 |
|
| 216 |
def test_os_isatty(): |
| 217 |
def call_isatty(fd): |
| 218 |
return os.isatty(fd) |
| 219 |
f = compile(call_isatty, [int]) |
| 220 |
assert f(0) == os.isatty(0) |
| 221 |
assert f(1) == os.isatty(1) |
| 222 |
assert f(2) == os.isatty(2) |
| 223 |
|
| 224 |
def test_getcwd(): |
| 225 |
def does_stuff(): |
| 226 |
return os.getcwd() |
| 227 |
f1 = compile(does_stuff, []) |
| 228 |
res = f1() |
| 229 |
assert res == os.getcwd() |
| 230 |
|
| 231 |
def test_system(): |
| 232 |
def does_stuff(cmd): |
| 233 |
return os.system(cmd) |
| 234 |
f1 = compile(does_stuff, [str]) |
| 235 |
res = f1("echo hello") |
| 236 |
assert res == 0 |
| 237 |
|
| 238 |
def test_os_path_exists(): |
| 239 |
tmpfile = str(udir.join('test_os_path_exists.TMP')) |
| 240 |
def fn(): |
| 241 |
return os.path.exists(tmpfile) |
| 242 |
f = compile(fn, []) |
| 243 |
open(tmpfile, 'w').close() |
| 244 |
assert f() == True |
| 245 |
os.unlink(tmpfile) |
| 246 |
assert f() == False |
| 247 |
|
| 248 |
def test_os_path_isdir(): |
| 249 |
directory = "./." |
| 250 |
def fn(): |
| 251 |
return os.path.isdir(directory) |
| 252 |
f = compile(fn, []) |
| 253 |
assert f() == True |
| 254 |
directory = "some/random/name" |
| 255 |
def fn(): |
| 256 |
return os.path.isdir(directory) |
| 257 |
f = compile(fn, []) |
| 258 |
assert f() == False |
| 259 |
|
| 260 |
def test_time_time(): |
| 261 |
import time |
| 262 |
def fn(): |
| 263 |
return time.time() |
| 264 |
f = compile(fn, []) |
| 265 |
t0 = time.time() |
| 266 |
res = fn() |
| 267 |
t1 = time.time() |
| 268 |
assert t0 <= res <= t1 |
| 269 |
|
| 270 |
|
| 271 |
def test_formatd(): |
| 272 |
from rpython.rlib.rfloat import formatd |
| 273 |
def fn(x): |
| 274 |
return formatd(x, 'f', 2, 0) |
| 275 |
|
| 276 |
f = compile(fn, [float]) |
| 277 |
|
| 278 |
assert f(0.0) == "0.00" |
| 279 |
assert f(1.5) == "1.50" |
| 280 |
assert f(2.0) == "2.00" |
| 281 |
|
| 282 |
def test_float_to_str(): |
| 283 |
def fn(f): |
| 284 |
return str(f) |
| 285 |
f = compile(fn, [float]) |
| 286 |
res = f(1.5) |
| 287 |
assert eval(res) == 1.5 |
| 288 |
|
| 289 |
def test_os_unlink(): |
| 290 |
tmpfile = str(udir.join('test_os_path_exists.TMP')) |
| 291 |
def fn(): |
| 292 |
os.unlink(tmpfile) |
| 293 |
f = compile(fn, []) |
| 294 |
open(tmpfile, 'w').close() |
| 295 |
fn() |
| 296 |
assert not os.path.exists(tmpfile) |
| 297 |
|
| 298 |
def test_chdir(): |
| 299 |
def does_stuff(path): |
| 300 |
os.chdir(path) |
| 301 |
return os.getcwd() |
| 302 |
f1 = compile(does_stuff, [str]) |
| 303 |
if os.name == 'nt': |
| 304 |
assert f1(os.environ['TEMP']) == os.path.realpath(os.environ['TEMP']) |
| 305 |
else: |
| 306 |
assert f1('/tmp') == os.path.realpath('/tmp') |
| 307 |
|
| 308 |
def test_mkdir_rmdir(): |
| 309 |
def does_stuff(path, delete): |
| 310 |
if delete: |
| 311 |
os.rmdir(path) |
| 312 |
else: |
| 313 |
os.mkdir(path, 0777) |
| 314 |
f1 = compile(does_stuff, [str, bool]) |
| 315 |
dirname = str(udir.join('test_mkdir_rmdir')) |
| 316 |
f1(dirname, False) |
| 317 |
assert os.path.exists(dirname) and os.path.isdir(dirname) |
| 318 |
f1(dirname, True) |
| 319 |
assert not os.path.exists(dirname) |
| 320 |
|
| 321 |
def test_strerror(): |
| 322 |
def does_stuff(n): |
| 323 |
return os.strerror(n) |
| 324 |
f1 = compile(does_stuff, [int]) |
| 325 |
for i in range(4): |
| 326 |
res = f1(i) |
| 327 |
assert res == os.strerror(i) |
| 328 |
|
| 329 |
def test_pipe_dup_dup2(): |
| 330 |
def does_stuff(): |
| 331 |
a, b = os.pipe() |
| 332 |
c = os.dup(a) |
| 333 |
d = os.dup(b) |
| 334 |
assert a != b |
| 335 |
assert a != c |
| 336 |
assert a != d |
| 337 |
assert b != c |
| 338 |
assert b != d |
| 339 |
assert c != d |
| 340 |
os.close(c) |
| 341 |
os.dup2(d, c) |
| 342 |
e, f = os.pipe() |
| 343 |
assert e != a |
| 344 |
assert e != b |
| 345 |
assert e != c |
| 346 |
assert e != d |
| 347 |
assert f != a |
| 348 |
assert f != b |
| 349 |
assert f != c |
| 350 |
assert f != d |
| 351 |
assert f != e |
| 352 |
os.close(a) |
| 353 |
os.close(b) |
| 354 |
os.close(c) |
| 355 |
os.close(d) |
| 356 |
os.close(e) |
| 357 |
os.close(f) |
| 358 |
return 42 |
| 359 |
f1 = compile(does_stuff, []) |
| 360 |
res = f1() |
| 361 |
assert res == 42 |
| 362 |
|
| 363 |
def test_os_chmod(): |
| 364 |
tmpfile = str(udir.join('test_os_chmod.txt')) |
| 365 |
f = open(tmpfile, 'w') |
| 366 |
f.close() |
| 367 |
|
| 368 |
|
| 369 |
tmpfile2 = str(udir.join('test_os_chmod_witness.txt')) |
| 370 |
f = open(tmpfile2, 'w') |
| 371 |
f.close() |
| 372 |
def does_stuff(mode): |
| 373 |
os.chmod(tmpfile, mode) |
| 374 |
f1 = compile(does_stuff, [int]) |
| 375 |
f1(0000) |
| 376 |
os.chmod(tmpfile2, 0000) |
| 377 |
assert os.stat(tmpfile).st_mode & 0777 == os.stat(tmpfile2).st_mode & 0777 |
| 378 |
f1(0644) |
| 379 |
os.chmod(tmpfile2, 0644) |
| 380 |
assert os.stat(tmpfile).st_mode & 0777 == os.stat(tmpfile2).st_mode & 0777 |
| 381 |
|
| 382 |
if hasattr(os, 'fchmod'): |
| 383 |
def test_os_fchmod(): |
| 384 |
tmpfile1 = str(udir.join('test_os_fchmod.txt')) |
| 385 |
def does_stuff(): |
| 386 |
fd = os.open(tmpfile1, os.O_WRONLY | os.O_CREAT, 0777) |
| 387 |
os.fchmod(fd, 0200) |
| 388 |
os.close(fd) |
| 389 |
f1 = compile(does_stuff, []) |
| 390 |
f1() |
| 391 |
assert os.stat(tmpfile1).st_mode & 0777 == 0200 |
| 392 |
|
| 393 |
def test_os_rename(): |
| 394 |
tmpfile1 = str(udir.join('test_os_rename_1.txt')) |
| 395 |
tmpfile2 = str(udir.join('test_os_rename_2.txt')) |
| 396 |
f = open(tmpfile1, 'w') |
| 397 |
f.close() |
| 398 |
def does_stuff(): |
| 399 |
os.rename(tmpfile1, tmpfile2) |
| 400 |
f1 = compile(does_stuff, []) |
| 401 |
f1() |
| 402 |
assert os.path.exists(tmpfile2) |
| 403 |
assert not os.path.exists(tmpfile1) |
| 404 |
|
| 405 |
if hasattr(os, 'mkfifo'): |
| 406 |
def test_os_mkfifo(): |
| 407 |
tmpfile = str(udir.join('test_os_mkfifo.txt')) |
| 408 |
def does_stuff(): |
| 409 |
os.mkfifo(tmpfile, 0666) |
| 410 |
f1 = compile(does_stuff, []) |
| 411 |
f1() |
| 412 |
import stat |
| 413 |
st = os.lstat(tmpfile) |
| 414 |
assert stat.S_ISFIFO(st.st_mode) |
| 415 |
|
| 416 |
if hasattr(os, 'mknod'): |
| 417 |
def test_os_mknod(): |
| 418 |
import stat |
| 419 |
tmpfile = str(udir.join('test_os_mknod.txt')) |
| 420 |
def does_stuff(): |
| 421 |
os.mknod(tmpfile, 0600 | stat.S_IFIFO, 0) |
| 422 |
f1 = compile(does_stuff, []) |
| 423 |
f1() |
| 424 |
st = os.lstat(tmpfile) |
| 425 |
assert stat.S_ISFIFO(st.st_mode) |
| 426 |
|
| 427 |
def test_os_umask(): |
| 428 |
def does_stuff(): |
| 429 |
mask1 = os.umask(0660) |
| 430 |
mask2 = os.umask(mask1) |
| 431 |
return mask2 |
| 432 |
f1 = compile(does_stuff, []) |
| 433 |
res = f1() |
| 434 |
assert res == does_stuff() |
| 435 |
|
| 436 |
if hasattr(os, 'getpid'): |
| 437 |
def test_os_getpid(): |
| 438 |
def does_stuff(): |
| 439 |
return os.getpid() |
| 440 |
f1 = compile(does_stuff, []) |
| 441 |
res = f1() |
| 442 |
assert res != os.getpid() |
| 443 |
|
| 444 |
if hasattr(os, 'getpgrp'): |
| 445 |
def test_os_getpgrp(): |
| 446 |
def does_stuff(): |
| 447 |
return os.getpgrp() |
| 448 |
f1 = compile(does_stuff, []) |
| 449 |
res = f1() |
| 450 |
assert res == os.getpgrp() |
| 451 |
|
| 452 |
if hasattr(os, 'setpgrp'): |
| 453 |
def test_os_setpgrp(): |
| 454 |
def does_stuff(): |
| 455 |
return os.setpgrp() |
| 456 |
f1 = compile(does_stuff, []) |
| 457 |
res = f1() |
| 458 |
assert res == os.setpgrp() |
| 459 |
|
| 460 |
if hasattr(os, 'link'): |
| 461 |
def test_links(): |
| 462 |
import stat |
| 463 |
tmpfile1 = str(udir.join('test_links_1.txt')) |
| 464 |
tmpfile2 = str(udir.join('test_links_2.txt')) |
| 465 |
tmpfile3 = str(udir.join('test_links_3.txt')) |
| 466 |
f = open(tmpfile1, 'w') |
| 467 |
f.close() |
| 468 |
def does_stuff(): |
| 469 |
os.symlink(tmpfile1, tmpfile2) |
| 470 |
os.link(tmpfile1, tmpfile3) |
| 471 |
assert os.readlink(tmpfile2) == tmpfile1 |
| 472 |
flag= 0 |
| 473 |
st = os.lstat(tmpfile1) |
| 474 |
flag = flag*10 + stat.S_ISREG(st[0]) |
| 475 |
flag = flag*10 + stat.S_ISLNK(st[0]) |
| 476 |
st = os.lstat(tmpfile2) |
| 477 |
flag = flag*10 + stat.S_ISREG(st[0]) |
| 478 |
flag = flag*10 + stat.S_ISLNK(st[0]) |
| 479 |
st = os.lstat(tmpfile3) |
| 480 |
flag = flag*10 + stat.S_ISREG(st[0]) |
| 481 |
flag = flag*10 + stat.S_ISLNK(st[0]) |
| 482 |
return flag |
| 483 |
f1 = compile(does_stuff, []) |
| 484 |
res = f1() |
| 485 |
assert res == 100110 |
| 486 |
assert os.path.islink(tmpfile2) |
| 487 |
assert not os.path.islink(tmpfile3) |
| 488 |
|
| 489 |
if hasattr(os, 'fork'): |
| 490 |
def test_fork(): |
| 491 |
def does_stuff(): |
| 492 |
pid = os.fork() |
| 493 |
if pid == 0: |
| 494 |
os._exit(4) |
| 495 |
pid1, status1 = os.waitpid(pid, 0) |
| 496 |
assert pid1 == pid |
| 497 |
return status1 |
| 498 |
f1 = compile(does_stuff, []) |
| 499 |
status1 = f1() |
| 500 |
assert os.WIFEXITED(status1) |
| 501 |
assert os.WEXITSTATUS(status1) == 4 |
| 502 |
if hasattr(os, 'kill'): |
| 503 |
def test_kill(): |
| 504 |
import signal |
| 505 |
def does_stuff(): |
| 506 |
pid = os.fork() |
| 507 |
if pid == 0: |
| 508 |
time.sleep(5) |
| 509 |
os._exit(4) |
| 510 |
os.kill(pid, signal.SIGTERM) |
| 511 |
pid1, status1 = os.waitpid(pid, 0) |
| 512 |
assert pid1 == pid |
| 513 |
return status1 |
| 514 |
f1 = compile(does_stuff, []) |
| 515 |
status1 = f1() |
| 516 |
assert os.WIFSIGNALED(status1) |
| 517 |
assert os.WTERMSIG(status1) == signal.SIGTERM |
| 518 |
elif hasattr(os, 'waitpid'): |
| 519 |
|
| 520 |
def test_waitpid(): |
| 521 |
prog = str(sys.executable) |
| 522 |
def does_stuff(): |
| 523 |
args = [prog] |
| 524 |
|
| 525 |
|
| 526 |
args.append('-c') |
| 527 |
args.append('"import os;os._exit(4)"') |
| 528 |
pid = os.spawnv(os.P_NOWAIT, prog, args) |
| 529 |
|
| 530 |
|
| 531 |
pid1, status1 = os.waitpid(pid, 0) |
| 532 |
assert pid1 == pid |
| 533 |
return status1 |
| 534 |
f1 = compile(does_stuff, []) |
| 535 |
status1 = f1() |
| 536 |
|
| 537 |
assert status1 >> 8 == 4 |
| 538 |
|
| 539 |
if hasattr(os, 'kill'): |
| 540 |
def test_kill_to_send_sigusr1(): |
| 541 |
import signal |
| 542 |
from rpython.rlib import rsignal |
| 543 |
if not 'SIGUSR1' in dir(signal): |
| 544 |
py.test.skip("no SIGUSR1 available") |
| 545 |
def does_stuff(): |
| 546 |
rsignal.pypysig_setflag(signal.SIGUSR1) |
| 547 |
os.kill(os.getpid(), signal.SIGUSR1) |
| 548 |
rsignal.pypysig_ignore(signal.SIGUSR1) |
| 549 |
while True: |
| 550 |
n = rsignal.pypysig_poll() |
| 551 |
if n < 0 or n == signal.SIGUSR1: |
| 552 |
break |
| 553 |
return n |
| 554 |
f1 = compile(does_stuff, []) |
| 555 |
got_signal = f1() |
| 556 |
assert got_signal == signal.SIGUSR1 |
| 557 |
|
| 558 |
if hasattr(os, 'killpg'): |
| 559 |
def test_killpg(): |
| 560 |
import signal |
| 561 |
from rpython.rlib import rsignal |
| 562 |
def does_stuff(): |
| 563 |
os.setpgid(0, 0) |
| 564 |
rsignal.pypysig_setflag(signal.SIGUSR1) |
| 565 |
os.killpg(os.getpgrp(), signal.SIGUSR1) |
| 566 |
rsignal.pypysig_ignore(signal.SIGUSR1) |
| 567 |
while True: |
| 568 |
n = rsignal.pypysig_poll() |
| 569 |
if n < 0 or n == signal.SIGUSR1: |
| 570 |
break |
| 571 |
return n |
| 572 |
f1 = compile(does_stuff, []) |
| 573 |
got_signal = f1() |
| 574 |
assert got_signal == signal.SIGUSR1 |
| 575 |
|
| 576 |
if hasattr(os, 'chown') and hasattr(os, 'lchown'): |
| 577 |
def test_os_chown_lchown(): |
| 578 |
path1 = udir.join('test_os_chown_lchown-1.txt') |
| 579 |
path2 = udir.join('test_os_chown_lchown-2.txt') |
| 580 |
path1.write('foobar') |
| 581 |
path2.mksymlinkto('some-broken-symlink') |
| 582 |
tmpfile1 = str(path1) |
| 583 |
tmpfile2 = str(path2) |
| 584 |
def does_stuff(): |
| 585 |
|
| 586 |
os.chown(tmpfile1, os.getuid(), os.getgid()) |
| 587 |
os.lchown(tmpfile1, os.getuid(), os.getgid()) |
| 588 |
os.lchown(tmpfile2, os.getuid(), os.getgid()) |
| 589 |
try: |
| 590 |
os.chown(tmpfile2, os.getuid(), os.getgid()) |
| 591 |
except OSError: |
| 592 |
pass |
| 593 |
else: |
| 594 |
raise AssertionError("os.chown(broken symlink) should raise") |
| 595 |
f1 = compile(does_stuff, []) |
| 596 |
f1() |
| 597 |
|
| 598 |
if hasattr(os, 'fchown'): |
| 599 |
def test_os_fchown(): |
| 600 |
path1 = udir.join('test_os_fchown.txt') |
| 601 |
tmpfile1 = str(path1) |
| 602 |
def does_stuff(): |
| 603 |
|
| 604 |
fd = os.open(tmpfile1, os.O_WRONLY | os.O_CREAT, 0777) |
| 605 |
os.fchown(fd, os.getuid(), os.getgid()) |
| 606 |
os.close(fd) |
| 607 |
f1 = compile(does_stuff, []) |
| 608 |
f1() |
| 609 |
|
| 610 |
if hasattr(os, 'getlogin'): |
| 611 |
def test_os_getlogin(): |
| 612 |
def does_stuff(): |
| 613 |
return os.getlogin() |
| 614 |
|
| 615 |
try: |
| 616 |
expected = os.getlogin() |
| 617 |
except OSError as e: |
| 618 |
py.test.skip("the underlying os.getlogin() failed: %s" % e) |
| 619 |
f1 = compile(does_stuff, []) |
| 620 |
assert f1() == expected |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
def _real_getenv(var): |
| 625 |
cmd = '''%s -c "import os; x=os.environ.get('%s'); print (x is None) and 'F' or ('T'+x)"''' % ( |
| 626 |
sys.executable, var) |
| 627 |
g = os.popen(cmd, 'r') |
| 628 |
output = g.read().strip() |
| 629 |
g.close() |
| 630 |
if output == 'F': |
| 631 |
return None |
| 632 |
elif output.startswith('T'): |
| 633 |
return output[1:] |
| 634 |
else: |
| 635 |
raise ValueError('probing for env var returned %r' % (output,)) |
| 636 |
|
| 637 |
def test_dictlike_environ_getitem(): |
| 638 |
def fn(s): |
| 639 |
try: |
| 640 |
return os.environ[s] |
| 641 |
except KeyError: |
| 642 |
return '--missing--' |
| 643 |
func = compile(fn, [str]) |
| 644 |
os.environ.setdefault('USER', 'UNNAMED_USER') |
| 645 |
result = func('USER') |
| 646 |
assert result == os.environ['USER'] |
| 647 |
result = func('PYPY_TEST_DICTLIKE_MISSING') |
| 648 |
assert result == '--missing--' |
| 649 |
|
| 650 |
def test_dictlike_environ_get(): |
| 651 |
def fn(s): |
| 652 |
res = os.environ.get(s) |
| 653 |
if res is None: res = '--missing--' |
| 654 |
return res |
| 655 |
func = compile(fn, [str]) |
| 656 |
os.environ.setdefault('USER', 'UNNAMED_USER') |
| 657 |
result = func('USER') |
| 658 |
assert result == os.environ['USER'] |
| 659 |
result = func('PYPY_TEST_DICTLIKE_MISSING') |
| 660 |
assert result == '--missing--' |
| 661 |
|
| 662 |
def test_dictlike_environ_setitem(): |
| 663 |
def fn(s, t1, t2, t3, t4, t5): |
| 664 |
os.environ[s] = t1 |
| 665 |
os.environ[s] = t2 |
| 666 |
os.environ[s] = t3 |
| 667 |
os.environ[s] = t4 |
| 668 |
os.environ[s] = t5 |
| 669 |
return os.environ[s] |
| 670 |
func = compile(fn, [str] * 6) |
| 671 |
r = func('PYPY_TEST_DICTLIKE_ENVIRON', 'a', 'b', 'c', 'FOOBAR', '42') |
| 672 |
assert r == '42' |
| 673 |
|
| 674 |
def test_dictlike_environ_delitem(): |
| 675 |
def fn(s1, s2, s3, s4, s5): |
| 676 |
for n in range(10): |
| 677 |
os.environ[s1] = 't1' |
| 678 |
os.environ[s2] = 't2' |
| 679 |
os.environ[s3] = 't3' |
| 680 |
os.environ[s4] = 't4' |
| 681 |
os.environ[s5] = 't5' |
| 682 |
del os.environ[s3] |
| 683 |
del os.environ[s1] |
| 684 |
del os.environ[s2] |
| 685 |
del os.environ[s4] |
| 686 |
try: |
| 687 |
del os.environ[s2] |
| 688 |
except KeyError: |
| 689 |
pass |
| 690 |
else: |
| 691 |
raise Exception("should have raised!") |
| 692 |
|
| 693 |
func = compile(fn, [str] * 5) |
| 694 |
func('PYPY_TEST_DICTLIKE_ENVDEL1', |
| 695 |
'PYPY_TEST_DICTLIKE_ENVDEL_X', |
| 696 |
'PYPY_TEST_DICTLIKE_ENVDELFOO', |
| 697 |
'PYPY_TEST_DICTLIKE_ENVDELBAR', |
| 698 |
'PYPY_TEST_DICTLIKE_ENVDEL5') |
| 699 |
|
| 700 |
def test_dictlike_environ_keys(): |
| 701 |
def fn(): |
| 702 |
return '\x00'.join(os.environ.keys()) |
| 703 |
func = compile(fn, []) |
| 704 |
os.environ.setdefault('USER', 'UNNAMED_USER') |
| 705 |
try: |
| 706 |
del os.environ['PYPY_TEST_DICTLIKE_ENVKEYS'] |
| 707 |
except: |
| 708 |
pass |
| 709 |
result1 = func().split('\x00') |
| 710 |
os.environ['PYPY_TEST_DICTLIKE_ENVKEYS'] = '42' |
| 711 |
result2 = func().split('\x00') |
| 712 |
assert 'USER' in result1 |
| 713 |
assert 'PYPY_TEST_DICTLIKE_ENVKEYS' not in result1 |
| 714 |
assert 'USER' in result2 |
| 715 |
assert 'PYPY_TEST_DICTLIKE_ENVKEYS' in result2 |
| 716 |
|
| 717 |
def test_dictlike_environ_items(): |
| 718 |
def fn(): |
| 719 |
result = [] |
| 720 |
for key, value in os.environ.items(): |
| 721 |
result.append('%s/%s' % (key, value)) |
| 722 |
return '\x00'.join(result) |
| 723 |
func = compile(fn, []) |
| 724 |
os.environ.setdefault('USER', 'UNNAMED_USER') |
| 725 |
result1 = func().split('\x00') |
| 726 |
os.environ['PYPY_TEST_DICTLIKE_ENVITEMS'] = '783' |
| 727 |
result2 = func().split('\x00') |
| 728 |
assert ('USER/%s' % (os.environ['USER'],)) in result1 |
| 729 |
assert 'PYPY_TEST_DICTLIKE_ENVITEMS/783' not in result1 |
| 730 |
assert ('USER/%s' % (os.environ['USER'],)) in result2 |
| 731 |
assert 'PYPY_TEST_DICTLIKE_ENVITEMS/783' in result2 |
| 732 |
|
| 733 |
def test_listdir(): |
| 734 |
def mylistdir(s): |
| 735 |
try: |
| 736 |
os.listdir('this/directory/really/cannot/exist') |
| 737 |
except OSError: |
| 738 |
pass |
| 739 |
else: |
| 740 |
raise AssertionError("should have failed!") |
| 741 |
result = os.listdir(s) |
| 742 |
return '/'.join(result) |
| 743 |
func = compile(mylistdir, [str]) |
| 744 |
for testdir in [str(udir), os.curdir]: |
| 745 |
result = func(testdir) |
| 746 |
result = result.split('/') |
| 747 |
result.sort() |
| 748 |
compared_with = os.listdir(testdir) |
| 749 |
compared_with.sort() |
| 750 |
assert result == compared_with |
| 751 |
|
| 752 |
if hasattr(posix, 'execv') and hasattr(posix, 'fork'): |
| 753 |
def test_execv(): |
| 754 |
progname = str(sys.executable) |
| 755 |
filename = str(udir.join('test_execv.txt')) |
| 756 |
def does_stuff(): |
| 757 |
l = [progname, '-c', 'open(%r,"w").write("1")' % filename] |
| 758 |
pid = os.fork() |
| 759 |
if pid == 0: |
| 760 |
os.execv(progname, l) |
| 761 |
else: |
| 762 |
os.waitpid(pid, 0) |
| 763 |
func = compile(does_stuff, [], backendopt=False) |
| 764 |
func() |
| 765 |
assert open(filename).read() == "1" |
| 766 |
|
| 767 |
def test_execv_raising(): |
| 768 |
def does_stuff(): |
| 769 |
try: |
| 770 |
l = [] |
| 771 |
l.append("asddsadw32eewdfwqdqwdqwd") |
| 772 |
os.execv(l[0], l) |
| 773 |
return 1 |
| 774 |
except OSError: |
| 775 |
return -2 |
| 776 |
|
| 777 |
func = compile(does_stuff, []) |
| 778 |
assert func() == -2 |
| 779 |
|
| 780 |
def test_execve(): |
| 781 |
filename = str(udir.join('test_execve.txt')) |
| 782 |
progname = sys.executable |
| 783 |
def does_stuff(): |
| 784 |
l = [] |
| 785 |
l.append(progname) |
| 786 |
l.append("-c") |
| 787 |
l.append('import os; open(%r, "w").write(os.environ["STH"])' % filename) |
| 788 |
env = {} |
| 789 |
env["STH"] = "42" |
| 790 |
env["sthelse"] = "a" |
| 791 |
pid = os.fork() |
| 792 |
if pid == 0: |
| 793 |
os.execve(progname, l, env) |
| 794 |
else: |
| 795 |
os.waitpid(pid, 0) |
| 796 |
|
| 797 |
func = compile(does_stuff, []) |
| 798 |
func() |
| 799 |
assert open(filename).read() == "42" |
| 800 |
|
| 801 |
if hasattr(posix, 'spawnv'): |
| 802 |
def test_spawnv(): |
| 803 |
filename = str(udir.join('test_spawnv.txt')) |
| 804 |
progname = str(sys.executable) |
| 805 |
scriptpath = udir.join('test_spawnv.py') |
| 806 |
scriptpath.write('f=open(%r,"w")\nf.write("2")\nf.close\n' % filename) |
| 807 |
scriptname = str(scriptpath) |
| 808 |
def does_stuff(): |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
l = [progname, scriptname] |
| 816 |
pid = os.spawnv(os.P_NOWAIT, progname, l) |
| 817 |
os.waitpid(pid, 0) |
| 818 |
func = compile(does_stuff, []) |
| 819 |
func() |
| 820 |
assert open(filename).read() == "2" |
| 821 |
|
| 822 |
if hasattr(posix, 'spawnve'): |
| 823 |
def test_spawnve(): |
| 824 |
filename = str(udir.join('test_spawnve.txt')) |
| 825 |
progname = str(sys.executable) |
| 826 |
scriptpath = udir.join('test_spawnve.py') |
| 827 |
scriptpath.write('import os\n' + |
| 828 |
'f=open(%r,"w")\n' % filename + |
| 829 |
'f.write(os.environ["FOOBAR"])\n' + |
| 830 |
'f.close\n') |
| 831 |
scriptname = str(scriptpath) |
| 832 |
def does_stuff(): |
| 833 |
l = [progname, scriptname] |
| 834 |
pid = os.spawnve(os.P_NOWAIT, progname, l, {'FOOBAR': '42'}) |
| 835 |
os.waitpid(pid, 0) |
| 836 |
func = compile(does_stuff, []) |
| 837 |
func() |
| 838 |
assert open(filename).read() == "42" |
| 839 |
|
| 840 |
def test_utime(): |
| 841 |
path = str(udir.ensure("test_utime.txt")) |
| 842 |
from time import time, sleep |
| 843 |
t0 = time() |
| 844 |
sleep(1) |
| 845 |
|
| 846 |
def does_stuff(flag): |
| 847 |
if flag: |
| 848 |
os.utime(path, None) |
| 849 |
else: |
| 850 |
os.utime(path, (int(t0), int(t0))) |
| 851 |
|
| 852 |
func = compile(does_stuff, [int]) |
| 853 |
func(1) |
| 854 |
assert os.stat(path).st_atime > t0 |
| 855 |
func(0) |
| 856 |
assert int(os.stat(path).st_atime) == int(t0) |
| 857 |
|
| 858 |
if hasattr(os, 'uname'): |
| 859 |
def test_os_uname(): |
| 860 |
def does_stuff(num): |
| 861 |
tup = os.uname() |
| 862 |
lst = [tup[0], tup[1], tup[2], tup[3], tup[4]] |
| 863 |
return lst[num] |
| 864 |
func = compile(does_stuff, [int]) |
| 865 |
for i in range(5): |
| 866 |
res = func(i) |
| 867 |
assert res == os.uname()[i] |
| 868 |
|
| 869 |
if hasattr(os, 'getloadavg'): |
| 870 |
def test_os_getloadavg(): |
| 871 |
def does_stuff(): |
| 872 |
a, b, c = os.getloadavg() |
| 873 |
print a, b, c |
| 874 |
return a + b + c |
| 875 |
f = compile(does_stuff, []) |
| 876 |
res = f() |
| 877 |
assert type(res) is float and res >= 0.0 |
| 878 |
|
| 879 |
if hasattr(os, 'major'): |
| 880 |
def test_os_major_minor(): |
| 881 |
def does_stuff(n): |
| 882 |
a = os.major(n) |
| 883 |
b = os.minor(n) |
| 884 |
x = os.makedev(a, b) |
| 885 |
return '%d,%d,%d' % (a, b, x) |
| 886 |
f = compile(does_stuff, [int]) |
| 887 |
res = f(12345) |
| 888 |
assert res == '%d,%d,12345' % (os.major(12345), os.minor(12345)) |
| 889 |
|
| 890 |
if hasattr(os, 'fchdir'): |
| 891 |
def test_os_fchdir(): |
| 892 |
def does_stuff(): |
| 893 |
fd = os.open('/', os.O_RDONLY, 0400) |
| 894 |
try: |
| 895 |
os.fchdir(fd) |
| 896 |
s = os.getcwd() |
| 897 |
finally: |
| 898 |
os.close(fd) |
| 899 |
return s == '/' |
| 900 |
f = compile(does_stuff, []) |
| 901 |
localdir = os.getcwd() |
| 902 |
try: |
| 903 |
res = f() |
| 904 |
finally: |
| 905 |
os.chdir(localdir) |
| 906 |
assert res == True |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
class TestExtFuncStandalone(StandaloneTests): |
| 912 |
|
| 913 |
if hasattr(os, 'nice'): |
| 914 |
def test_os_nice(self): |
| 915 |
def does_stuff(argv): |
| 916 |
res = os.nice(3) |
| 917 |
print 'os.nice returned', res |
| 918 |
return 0 |
| 919 |
t, cbuilder = self.compile(does_stuff) |
| 920 |
data = cbuilder.cmdexec('') |
| 921 |
res = os.nice(0) + 3 |
| 922 |
if res > 19: res = 19 |
| 923 |
assert data.startswith('os.nice returned %d\n' % res) |