Debian Codegraph @

rpython/rlib/test/test_rwin32.py

raw symbols SCIP
1 # encoding: utf-8
2 import os, py
3 if os.name != 'nt':
4 py.test.skip('tests for win32 only')
5
6 from rpython.rlib import rwin32
7 from rpython.tool.udir import udir
8 from rpython.rlib.rarithmetic import is_emulated_long
9
10 arch = '_64' if is_emulated_long else '_32'
11 loadtest_dir = os.path.dirname(__file__) + '/loadtest'
12 test1 = os.path.abspath(loadtest_dir + '/loadtest1' + arch + '.dll')
13 test0 = os.path.abspath(loadtest_dir + '/loadtest0' + arch + '.dll')
14
15 if not os.path.exists(test1) or not os.path.exists(test0):
16 # This is how the files, which are checked into the repo, were created
17 from rpython.translator.tool.cbuild import ExternalCompilationInfo
18 from rpython.translator.platform import platform
19 from rpython.translator import cdir
20 if not os.path.exists(loadtest_dir):
21 os.mkdir(loadtest_dir)
22 c_file = udir.ensure("test_rwin32", dir=1).join("test0.c")
23 c_file.write(py.code.Source('''
24 #include "src/precommondefs.h"
25 RPY_EXPORTED
26 int internal_sum(int a, int b) {
27 return a + b;
28 }
29 '''))
30 eci = ExternalCompilationInfo(include_dirs=[cdir])
31 lib_name = str(platform.compile([c_file], eci, test0[:-4],
32 standalone=False))
33 assert os.path.abspath(lib_name) == os.path.abspath(test0)
34
35 c_file = udir.ensure("test_rwin32", dir=1).join("test1.c")
36 c_file.write(py.code.Source('''
37 #include "src/precommondefs.h"
38 int internal_sum(int a, int b);
39 RPY_EXPORTED
40 int sum(int a, int b) {
41 return internal_sum(a, b);
42 }
43 '''))
44 eci = ExternalCompilationInfo(include_dirs=[cdir],
45 libraries=[loadtest_dir + '/loadtest0' + arch])
46 lib_name = str(platform.compile([c_file], eci, test1[:-4],
47 standalone=False, ))
48 assert os.path.abspath(lib_name) == os.path.abspath(test1)
49
50 def test_get_osfhandle():
51 fid = open(str(udir.join('validate_test.txt')), 'w')
52 fd = fid.fileno()
53 rwin32.get_osfhandle(fd)
54 fid.close()
55 # Somehow the SuppressIPH's c_enter_suppress_iph, which resolves
56 # to calling _set_thread_local_invalid_parameter_handler, does not work
57 # untranslated. After translation it does work.
58 # py.test.raises(OSError, rwin32.get_osfhandle, fd)
59 rwin32.get_osfhandle(0)
60
61 def test_open_process():
62 pid = rwin32.GetCurrentProcessId()
63 assert pid != 0
64 handle = rwin32.OpenProcess(rwin32.PROCESS_QUERY_INFORMATION, False, pid)
65 rwin32.CloseHandle(handle)
66 py.test.raises(WindowsError, rwin32.OpenProcess, rwin32.PROCESS_TERMINATE, False, 0)
67
68 def test_terminate_process():
69 import subprocess, signal, sys
70 proc = subprocess.Popen([sys.executable, "-c",
71 "import time;"
72 "time.sleep(10)",
73 ],
74 )
75 print proc.pid
76 handle = rwin32.OpenProcess(rwin32.PROCESS_ALL_ACCESS, False, proc.pid)
77 assert rwin32.TerminateProcess(handle, signal.SIGTERM) == 1
78 rwin32.CloseHandle(handle)
79 assert proc.wait() == signal.SIGTERM
80
81 @py.test.mark.dont_track_allocations('putenv intentionally keeps strings alive')
82 def test_wenviron():
83 name = u'PYPY_TEST_日本'.encode('utf-8')
84 value = u'foobar日本'.encode('utf-8')
85 rwin32._wputenv(name, value)
86 assert rwin32._wgetenv(name) == value
87 env = dict(rwin32._wenviron_items())
88 assert env[name] == value
89 for key, value in env.iteritems():
90 assert type(key) is str
91 assert type(value) is str
92
93 def test_formaterror():
94 # choose one with formatting characters and newlines
95 msg = rwin32.FormatError(34)
96 assert '%2' in msg
97
98 def test_formaterror_unicode():
99 msg, lgt = rwin32.FormatErrorW(34)
100 assert type(msg) is str
101 assert '%2' in msg
102
103 def test_loadlibraryA():
104 # test0 can be loaded alone, but test1 requires the modified search path
105 hdll = rwin32.LoadLibrary(test0)
106 assert hdll
107 faddr = rwin32.GetProcAddress(hdll, 'internal_sum')
108 assert faddr
109 assert rwin32.FreeLibrary(hdll)
110
111 hdll = rwin32.LoadLibrary(test1)
112 assert not hdll
113
114 assert os.path.exists(test1)
115
116 hdll = rwin32.LoadLibraryExA(test1, rwin32.LOAD_WITH_ALTERED_SEARCH_PATH)
117 assert hdll
118 faddr = rwin32.GetProcAddress(hdll, 'sum')
119 assert faddr
120 assert rwin32.FreeLibrary(hdll)
121
122 def test_loadlibraryW():
123 # test0 can be loaded alone, but test1 requires the modified search path
124 hdll = rwin32.LoadLibraryW(unicode(test0))
125 assert hdll
126 faddr = rwin32.GetProcAddress(hdll, 'internal_sum')
127 assert faddr
128 assert rwin32.FreeLibrary(hdll)
129
130 hdll = rwin32.LoadLibraryW(unicode(test1))
131 assert not hdll
132
133 assert os.path.exists(unicode(test1))
134
135 hdll = rwin32.LoadLibraryExW(unicode(test1), rwin32.LOAD_WITH_ALTERED_SEARCH_PATH)
136 assert hdll
137 faddr = rwin32.GetProcAddress(hdll, 'sum')
138 assert faddr
139 assert rwin32.FreeLibrary(hdll)
140
141 def test_loadlibrary_unicode():
142 import shutil
143 test0u = unicode(udir.join(u'load\u03betest' + arch + '.dll'))
144 shutil.copyfile(test0, test0u)
145 hdll = rwin32.LoadLibraryW(test0u)
146 assert hdll