File: test_rwin32.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (140 lines) | stat: -rw-r--r-- 4,642 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
# 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

loadtest_dir = os.path.dirname(__file__) + '/loadtest'
test1 = os.path.abspath(loadtest_dir + '/loadtest1.dll')
test0 = os.path.abspath(loadtest_dir + '/loadtest0.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'])
    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()
    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, value = u'PYPY_TEST_日本', u'foobar日本'
    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 unicode
        assert type(value) is unicode

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

def test_formaterror_unicode():
    msg = rwin32.FormatErrorW(34)
    assert type(msg) is unicode
    assert u'%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)
    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))
    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.dll'))
    shutil.copyfile(test0, test0u)
    hdll = rwin32.LoadLibraryW(test0u)
    assert hdll