Debian Codegraph @

pypy/module/posix/test/apptest_posix.py

raw symbols SCIP
1 import pytest
2
3 try:
4 import nt as posix
5 _WIN32 = True
6 except ImportError:
7 import posix
8 _WIN32 = False
9 os = posix
10
11 if hasattr(os, "fork"):
12 def test_register_at_fork():
13 with pytest.raises(TypeError): # no args
14 os.register_at_fork()
15 with pytest.raises(TypeError): # positional args not supported
16 os.register_at_fork(lambda : 1)
17 with pytest.raises(TypeError): # not callable
18 os.register_at_fork(before=1)
19 with pytest.raises(TypeError): # wrong keyword
20 os.register_at_fork(a=1)
21
22 # XXX this is unfortunately a small leak! all further tests that fork
23 # will call these callbacks and append four ints to l
24 l = [1]
25 os.register_at_fork(before=lambda: l.append(2))
26 os.register_at_fork(after_in_parent=lambda: l.append(5))
27 os.register_at_fork(after_in_child=lambda: l.append(3))
28 def double_last():
29 l[-1] *= 2
30 os.register_at_fork(
31 before=lambda: l.append(4),
32 after_in_parent=lambda: l.append(-1),
33 after_in_child=double_last)
34 pid = os.fork()
35 if pid == 0: # child
36 # l == [1, 4, 2, 6]
37 os._exit(sum(l))
38
39 assert l == [1, 4, 2, 5, -1]
40
41 pid1, status1 = os.waitpid(pid, 0)
42 assert pid1 == pid
43 assert os.WIFEXITED(status1)
44 res = os.WEXITSTATUS(status1)
45 assert res == 13
46
47
48 def test_cpu_count():
49 cc = posix.cpu_count()
50 assert cc is None or (isinstance(cc, int) and cc > 0)
51
52 def test_putenv_invalid_name():
53 with pytest.raises(ValueError):
54 posix.putenv("foo=bar", "xxx")
55
56 if not _WIN32:
57 def test_all_pathconf_defined():
58 import sys
59 import posix
60 try:
61 fd = sys.stdin.fileno()
62 except ValueError:
63 # translated test run with a fake sys.stdin with no fileno
64 fd = 1
65 for name in posix.pathconf_names:
66 posix.fpathconf(fd, name) # does not crash
67
68 if _WIN32:
69 def test__supports_virtual_terminal():
70 import sys
71 isatty = os.isatty(sys.stderr.fileno())
72 assert os._supports_virtual_terminal() == isatty