File: test_rwin32.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (146 lines) | stat: -rw-r--r-- 5,086 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# encoding: utf-8
import os, py
if os.name != 'nt':
    py.test.skip('tests for win32 only')

from rpython.rlib import rwin32
from rpython.tool.udir import udir
from rpython.rlib.rarithmetic import is_emulated_long

arch = '_64' if is_emulated_long else '_32'
loadtest_dir = os.path.dirname(__file__) + '/loadtest'
test1 = os.path.abspath(loadtest_dir + '/loadtest1' + arch + '.dll')
test0 = os.path.abspath(loadtest_dir + '/loadtest0' + arch + '.dll')

if not os.path.exists(test1) or not os.path.exists(test0):
    # This is how the files, which are checked into the repo, were created
    from rpython.translator.tool.cbuild import ExternalCompilationInfo
    from rpython.translator.platform import platform
    from rpython.translator import cdir
    if not os.path.exists(loadtest_dir):
        os.mkdir(loadtest_dir)
    c_file = udir.ensure("test_rwin32", dir=1).join("test0.c")
    c_file.write(py.code.Source('''
    #include "src/precommondefs.h"
    RPY_EXPORTED
    int internal_sum(int a, int b) {
        return a + b;
    }
    '''))
    eci = ExternalCompilationInfo(include_dirs=[cdir])
    lib_name = str(platform.compile([c_file], eci, test0[:-4],
                   standalone=False))
    assert os.path.abspath(lib_name) == os.path.abspath(test0)

    c_file = udir.ensure("test_rwin32", dir=1).join("test1.c")
    c_file.write(py.code.Source('''
    #include "src/precommondefs.h"
    int internal_sum(int a, int b);
    RPY_EXPORTED
    int sum(int a, int b) {
        return internal_sum(a, b);
    }
    '''))
    eci = ExternalCompilationInfo(include_dirs=[cdir], 
                        libraries=[loadtest_dir + '/loadtest0' + arch])
    lib_name = str(platform.compile([c_file], eci, test1[:-4],
                   standalone=False, ))
    assert os.path.abspath(lib_name) == os.path.abspath(test1)

def test_get_osfhandle():
    fid = open(str(udir.join('validate_test.txt')), 'w')
    fd = fid.fileno()
    rwin32.get_osfhandle(fd)
    fid.close()
    # Somehow the SuppressIPH's c_enter_suppress_iph, which resolves
    # to calling _set_thread_local_invalid_parameter_handler, does not work
    # untranslated. After translation it does work.
    # py.test.raises(OSError, rwin32.get_osfhandle, fd)
    rwin32.get_osfhandle(0)

def test_open_process():
    pid = rwin32.GetCurrentProcessId()
    assert pid != 0
    handle = rwin32.OpenProcess(rwin32.PROCESS_QUERY_INFORMATION, False, pid)
    rwin32.CloseHandle(handle)
    py.test.raises(WindowsError, rwin32.OpenProcess, rwin32.PROCESS_TERMINATE, False, 0)

def test_terminate_process():
    import subprocess, signal, sys
    proc = subprocess.Popen([sys.executable, "-c",
                         "import time;"
                         "time.sleep(10)",
                         ],
                        )
    print proc.pid
    handle = rwin32.OpenProcess(rwin32.PROCESS_ALL_ACCESS, False, proc.pid)
    assert rwin32.TerminateProcess(handle, signal.SIGTERM) == 1
    rwin32.CloseHandle(handle)
    assert proc.wait() == signal.SIGTERM

@py.test.mark.dont_track_allocations('putenv intentionally keeps strings alive')
def test_wenviron():
    name = u'PYPY_TEST_日本'.encode('utf-8')
    value = u'foobar日本'.encode('utf-8')
    rwin32._wputenv(name, value)
    assert rwin32._wgetenv(name) == value
    env = dict(rwin32._wenviron_items())
    assert env[name] == value
    for key, value in env.iteritems():
        assert type(key) is str
        assert type(value) is str

def test_formaterror():
    # choose one with formatting characters and newlines
    msg = rwin32.FormatError(34)
    assert '%2' in msg

def test_formaterror_unicode():
    msg, lgt = rwin32.FormatErrorW(34)
    assert type(msg) is str
    assert '%2' in msg

def test_loadlibraryA():
    # test0 can be loaded alone, but test1 requires the modified search path
    hdll = rwin32.LoadLibrary(test0)
    assert hdll
    faddr = rwin32.GetProcAddress(hdll, 'internal_sum')
    assert faddr
    assert rwin32.FreeLibrary(hdll)

    hdll = rwin32.LoadLibrary(test1)
    assert not hdll

    assert os.path.exists(test1)

    hdll = rwin32.LoadLibraryExA(test1, rwin32.LOAD_WITH_ALTERED_SEARCH_PATH)
    assert hdll
    faddr = rwin32.GetProcAddress(hdll, 'sum')
    assert faddr
    assert rwin32.FreeLibrary(hdll)

def test_loadlibraryW():
    # test0 can be loaded alone, but test1 requires the modified search path
    hdll = rwin32.LoadLibraryW(unicode(test0))
    assert hdll
    faddr = rwin32.GetProcAddress(hdll, 'internal_sum')
    assert faddr
    assert rwin32.FreeLibrary(hdll)

    hdll = rwin32.LoadLibraryW(unicode(test1))
    assert not hdll

    assert os.path.exists(unicode(test1))

    hdll = rwin32.LoadLibraryExW(unicode(test1), rwin32.LOAD_WITH_ALTERED_SEARCH_PATH)
    assert hdll
    faddr = rwin32.GetProcAddress(hdll, 'sum')
    assert faddr
    assert rwin32.FreeLibrary(hdll)

def test_loadlibrary_unicode():
    import shutil
    test0u = unicode(udir.join(u'load\u03betest' + arch + '.dll'))
    shutil.copyfile(test0, test0u)
    hdll = rwin32.LoadLibraryW(test0u)
    assert hdll