File: test_driver.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 (131 lines) | stat: -rw-r--r-- 5,262 bytes parent folder | download | duplicates (3)
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
import pytest
import os
from rpython.translator.driver import TranslationDriver, shutil_copy
from rpython.tool.udir import udir
from rpython.translator.platform import windows, darwin, linux

def test_ctr():
    td = TranslationDriver()
    expected = ['annotate', 'backendopt', 'llinterpret', 'rtype', 'source',
                'compile', 'pyjitpl']
    assert set(td.exposed) == set(expected)

    assert td.backend_select_goals(['compile_c']) == ['compile_c']
    assert td.backend_select_goals(['compile']) == ['compile_c']
    assert td.backend_select_goals(['rtype']) == ['rtype_lltype']
    assert td.backend_select_goals(['rtype_lltype']) == ['rtype_lltype']
    assert td.backend_select_goals(['backendopt']) == ['backendopt_lltype']
    assert td.backend_select_goals(['backendopt_lltype']) == [
        'backendopt_lltype']

    td = TranslationDriver({'backend': None, 'type_system': None})

    assert td.backend_select_goals(['compile_c']) == ['compile_c']
    pytest.raises(Exception, td.backend_select_goals, ['compile'])
    pytest.raises(Exception, td.backend_select_goals, ['rtype'])
    assert td.backend_select_goals(['rtype_lltype']) == ['rtype_lltype']
    pytest.raises(Exception, td.backend_select_goals, ['backendopt'])
    assert td.backend_select_goals(['backendopt_lltype']) == [
        'backendopt_lltype']

    expected = ['annotate', 'backendopt_lltype', 'llinterpret_lltype',
                'rtype_lltype', 'source_c', 'compile_c', 'pyjitpl_lltype', ]
    assert set(td.exposed) == set(expected)

    td = TranslationDriver({'backend': None, 'type_system': 'lltype'})

    assert td.backend_select_goals(['compile_c']) == ['compile_c']
    pytest.raises(Exception, td.backend_select_goals, ['compile'])
    assert td.backend_select_goals(['rtype_lltype']) == ['rtype_lltype']
    assert td.backend_select_goals(['rtype']) == ['rtype_lltype']
    assert td.backend_select_goals(['backendopt']) == ['backendopt_lltype']
    assert td.backend_select_goals(['backendopt_lltype']) == [
        'backendopt_lltype']

    expected = ['annotate', 'backendopt', 'llinterpret', 'rtype', 'source_c',
                'compile_c', 'pyjitpl']

    assert set(td.exposed) == set(expected)


@pytest.mark.parametrize("host, suffix", (
            (windows.MsvcPlatform, '.exe'),
            (darwin.Darwin, ''),
            (linux.Linux, '')),
            ids=('windows', 'macOS', 'linux'))
def test_compile_c(host, suffix):

    exe_name = 'pypy-%(backend)s'
    # Created by the fake "compile" function
    # Create the dst directory to be tested
    dst_name = udir.join('dst/pypy-c' + suffix)
    dst_name.ensure()

    class CBuilder(object):
        def compile(self, exe_name):
            from rpython.translator.tool.cbuild import ExternalCompilationInfo

            # CBuilder.gen_makefile is called via CBuilder.generate_source
            # in driver.task_source_c. We are faking parts of it here
            targetdir = udir.join('src')
            exe_name = targetdir.join(exe_name)
            platform = host(cc='not_really_going_to_compile')
            mk = platform.gen_makefile([], ExternalCompilationInfo(),
                                       exe_name=exe_name,
                                       path=targetdir,
                                       shared=True,
                                      )
            # "compile" the needed outputs
            src_name = udir.join('src/pypy-c' + suffix)
            src_name.ensure()
            src_name.write('exe')
            dll_name = udir.join('src/pypy-c.dll')
            dll_name.ensure()
            dll_name.write('dll')
            self.shared_library_name = dll_name

            # mock the additional windows artifacts as well
            wsrc_name = udir.join('src/pypy-cw.exe')
            wsrc_name.ensure()
            wsrc_name.write('wexe')
            self.executable_name_w = wsrc_name
            lib_name = udir.join('src/pypy-c.lib')
            lib_name.ensure()
            lib_name.write('lib')
            pdb_name = udir.join('src/pypy-c.pdb')
            pdb_name.ensure()
            pdb_name.write('pdb')
            self.executable_name = mk.exe_name

    td = TranslationDriver(exe_name=str(exe_name))
    # Normally done in the database_c task
    td.cbuilder = CBuilder()
    # Normally done when creating the driver via from_targetspec
    td.standalone = True

    cwd = os.getcwd()

    # This calls compile(), sets td.c_entryp to CBuilder.executable_name,
    # and calls create_exe(). We must cd into the target directory since
    # create_exe() copies back to the current directory

    try:
        os.chdir(dst_name.dirname)
        td.task_compile_c()
    finally:
        os.chdir(cwd)

    assert dst_name.read() == 'exe'
    assert dst_name.new(ext='dll').read() == 'dll'
    if host is windows.MsvcPlatform:
        assert dst_name.new(ext='lib').read() == 'lib'
        assert dst_name.new(purebasename=dst_name.purebasename + 'w').read() == 'wexe'

def test_shutil_copy():
    if os.name == 'nt':
        pytest.skip('Windows cannot copy or rename to an in-use file')
    a = udir.join('file_a')
    b = udir.join('file_a')
    a.write('hello')
    shutil_copy(str(a), str(b))
    assert b.read() == 'hello'