Source

pylauncher / tests.py

Full commit
  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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!python3
import sys
if sys.version_info[0] < 3:
    raise ImportError("These tests require Python 3 to run.")

import ctypes
import os
import os.path
import shutil
import subprocess
import tempfile
import unittest
import winreg


SCRIPT_TEMPLATE='''%(shebang_line)s%(coding_line)simport sys
print(sys.version)
print(sys.argv)
%(comment)s'''

BOM_UTF8 = b'\xEF\xBB\xBF'

LAUNCHER = os.path.join('Debug', 'py.exe')

IS_W = sys.executable.endswith("w.exe")

SHEBANGS = {
    'NONE': '',
    'ENV_PY': '#!/usr/bin/env python\n',
    'ENV_PY2': '#!/usr/bin/env python2\n',
    'ENV_PY3': '#!/usr/bin/env python3\n',
    'BIN_PY': '#!/usr/bin/python\n',
    'BIN_PY2': '#!/usr/bin/python2\n',
    'BIN_PY3': '#!/usr/bin/python3\n',
    'LBIN_PY': '#!/usr/local/bin/python\n',
    'LBIN_PY2': '#!/usr/local/bin/python2\n',
    'LBIN_PY3': '#!/usr/local/bin/python3\n',
    'PY': '#!python\n',
    'PY2': '#!python2\n',
    'PY3': '#!python3\n',
}

COMMENT_WITH_UNICODE = '# Libert\xe9, \xe9galit\xe9, fraternit\xe9\n'

VIRT_PATHS = [
    '/usr/bin/env ',
    '/usr/bin/env  ', # test extra whitespace before command
    '/usr/bin/',
    '/usr/local/bin/',
    '',
]

class VirtualPath: # think a C struct...
    def __init__(self, version, bits, executable):
        self.version = version
        self.bits = bits
        self.executable = executable

def is_64_bit_os():
    i = ctypes.c_int()
    kernel32 = ctypes.windll.kernel32
    process = kernel32.GetCurrentProcess()
    kernel32.IsWow64Process(process, ctypes.byref(i))
    return i.value

def locate_pythons_for_key(root, flags, infos):
    executable = 'pythonw.exe' if IS_W else 'python.exe'
    python_path = r'SOFTWARE\Python\PythonCore'
    try:
        core_root = winreg.OpenKeyEx(root, python_path, 0, flags)
    except WindowsError:
        return
    try:
        i = 0
        while True:
            try:
                verspec = winreg.EnumKey(core_root, i)
            except WindowsError:
                break
            try:
                ip_path = python_path + '\\' + verspec + '\\' + 'InstallPath'
                key_installed_path = winreg.OpenKeyEx(root, ip_path, 0, flags)
                try:
                    install_path, typ = winreg.QueryValueEx(key_installed_path,
                                                            None)
                finally:
                    winreg.CloseKey(key_installed_path)
                if typ==winreg.REG_SZ:
                    for check in ['', 'pcbuild', 'pcbuild/amd64']:
                        maybe = os.path.join(install_path, check, executable)
                        if os.path.isfile(maybe):
                            if ' ' in maybe:
                                maybe = '"' + maybe + '"'
                            infos.append(VirtualPath(verspec, 32, maybe))
                            #debug("found version %s at '%s'" % (verspec, maybe))
                            break
            except WindowsError:
                pass
            i += 1
    finally:
        winreg.CloseKey(core_root)

# Locate all installed Python versions, reverse-sorted by their version
# number - the sorting allows a simplistic linear scan to find the higest
# matching version number.
def locate_all_pythons():
    infos = []
    
    if not is_64_bit_os():
        locate_pythons_for_key(winreg.HKEY_CURRENT_USER, winreg.KEY_READ,
                               infos)
        locate_pythons_for_key(winreg.HKEY_LOCAL_MACHINE, winreg.KEY_READ,
                               infos)
    else:
        locate_pythons_for_key(winreg.HKEY_CURRENT_USER,
                               winreg.KEY_READ | winreg.KEY_WOW64_64KEY,
                               infos)
        locate_pythons_for_key(winreg.HKEY_LOCAL_MACHINE,
                               winreg.KEY_READ | winreg.KEY_WOW64_64KEY,
                               infos)
        locate_pythons_for_key(winreg.HKEY_CURRENT_USER,
                               winreg.KEY_READ | winreg.KEY_WOW64_32KEY,
                               infos)
        locate_pythons_for_key(winreg.HKEY_LOCAL_MACHINE,
                               winreg.KEY_READ | winreg.KEY_WOW64_32KEY,
                               infos)

    return sorted(infos, reverse=True, key=lambda info: (info.version, -info.bits))


ALL_PYTHONS = locate_all_pythons()

# locate a specific python version - some version must be specified (although
# it may be just a major version)
def locate_python_ver(spec):
    assert spec
    for info in ALL_PYTHONS:
        if info.version.startswith(spec):
            return info
    return None


def locate_python(spec):
    if len(spec)==1:
        # just a major version was specified - see if the environment
        # has a default for that version.
        spec = os.environ.get('PY_DEFAULT_PYTHON'+spec, spec)
    if spec:
        return locate_python_ver(spec)
    # No python spec - see if the environment has a default.
    spec = os.environ.get('PY_DEFAULT_PYTHON')
    if spec:
        return locate_python_ver(spec)
    # hrmph - still no spec - prefer python 2 if installed.
    ret = locate_python_ver('2')
    if ret is None:
        ret = locate_python_ver('3')
    # may still be none, but we are out of search options.
    return ret

DEFAULT_PYTHON2 = locate_python('2')
assert DEFAULT_PYTHON2, "You don't appear to have Python 2 installed"

DEFAULT_PYTHON3 = locate_python('3')
assert DEFAULT_PYTHON3, "You don't appear to have Python 3 installed"

def update_for_installed_pythons(*pythons):
    for python in pythons:
        python.bversion = python.version.encode('ascii')
        python.dir = 'Python%s' % python.version.replace('.', '')
        python.bdir = python.dir.encode('ascii')
        python.output_version = b'Python ' + python.bversion
        
        # Add additional shebangs for the versions we know are present
        major = python.version[0]
        upd_templates = {
            'ENV_PY%s_MIN': '#!/usr/bin/env python%s\n',
            'ENV_PY%s_MIN_BITS': '#!/usr/bin/env python%s-32\n',
            'BIN_PY%s_MIN': '#!/usr/bin/python%s\n',
            'BIN_PY%s_MIN_BITS': '#!/usr/bin/python%s-32\n',
            'LBIN_PY%s_MIN': '#!/usr/local/bin/python%s\n',
            'LBIN_PY%s_MIN_BITS': '#!/usr/local/bin/python%s-32\n',
            'PY%s_MIN': '#!/usr/local/bin/python%s\n',
            'PY%s_MIN_BITS': '#!/usr/local/bin/python%s-32\n',
        }
        for k, v in upd_templates.items():
            key = k % major
            value = v % python.version
            assert key not in SHEBANGS  # sanity check
            SHEBANGS[key] = value

update_for_installed_pythons(DEFAULT_PYTHON2, DEFAULT_PYTHON3)

class ScriptMaker:
    def setUp(self):
        self.work_dir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.work_dir)

    def make_script(self, shebang_line='', coding_line='', encoding='ascii',
                    bom=b'', comment=''):
        script = (SCRIPT_TEMPLATE % locals())
        script = script.replace('\r', '').replace('\n',
                                                  '\r\n').encode(encoding)
        if bom and not script.startswith(bom):
            script = bom + script
        path = os.path.join(self.work_dir, 'showver.py')
        with open(path, 'wb') as f:
            f.write(script)
        self.last_script = script
        return path

    def save_script(self):
            with open('last_failed.py', 'wb') as f:
                f.write(self.last_script)

    def matches(self, stdout, pyinfo):
        result = stdout.startswith(pyinfo.bversion)
        if not result:
            self.save_script()
            print('Expected', pyinfo.bversion)
            for s in self.last_streams:
                print(repr(s))
        return result

    def is_encoding_error(self, message):
        return b'but no encoding declared; see' in message

    def run_child(self, path, env=None):
        p = subprocess.Popen([LAUNCHER, path], stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE, shell=False,
                             env=env)
        stdout, stderr = p.communicate()
        self.last_streams = stdout, stderr
        return stdout, stderr

    def get_python_for_shebang(self, shebang):
        if 'python3' in shebang:
            result = DEFAULT_PYTHON3
        else:
            result = DEFAULT_PYTHON2
        return result

    def get_coding_line(self, coding):
        return '# -*- coding: %s -*-\n' % coding

class BasicTest(ScriptMaker, unittest.TestCase):
    def test_help(self):
        "Test help invocation"
        p = subprocess.Popen([LAUNCHER, '-h'], stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()
        self.assertTrue(stdout.startswith(b'Python Launcher for Windows'))
        self.assertIn(b'The following help text is from Python:\r\n\r\nusage: ', stdout)

    def test_version_specifier(self):
        """Test that files named like a version specifier do not get 
        misinterpreted as a version specifier when it does not have a shebang."""
        for nohyphen in ['t3', 'x2.6', '_3.1-32']:
            with open(nohyphen, 'w') as f:
                f.write('import sys\nprint(sys.version)\nprint(sys.argv)')
            try:
                script = self.make_script(shebang_line='')
                p =  subprocess.Popen([LAUNCHER, nohyphen, script], 
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                stdout, stderr = p.communicate()
                self.assertTrue(self.matches(stdout, DEFAULT_PYTHON2))
            finally:
                os.remove(nohyphen)
 
    # Tests with ASCII Python sources
    def test_shebang_ascii(self):
        "Test shebangs in ASCII files"
        for shebang in SHEBANGS.values():
            path = self.make_script(shebang_line=shebang)
            stdout, stderr = self.run_child(path)
            python = self.get_python_for_shebang(shebang)
            self.assertTrue(self.matches(stdout, python))

    # Tests with UTF-8 Python sources with no BOM
    def test_shebang_utf8_nobom(self):
        "Test shebangs in UTF-8 files with no BOM"
        for shebang in SHEBANGS.values():
            # If there's no Unicode, all should be well
            path = self.make_script(shebang_line=shebang, encoding='utf-8')
            stdout, stderr = self.run_child(path)
            python = self.get_python_for_shebang(shebang)
            self.assertTrue(self.matches(stdout, python))
            # If there's a Unicode comment with no coding line to alert,
            # we should see those errors from the spawned Python
            path = self.make_script(shebang_line=shebang, encoding='utf-8',
                                    comment=COMMENT_WITH_UNICODE)
            stdout, stderr = self.run_child(path)
            # Python3 reads Unicode without BOM as UTF-8
            self.assertTrue(self.is_encoding_error(stderr) or '3' in shebang)
            path = self.make_script(shebang_line=shebang, encoding='utf-8',
                                    comment=COMMENT_WITH_UNICODE,
                                    coding_line=self.get_coding_line('utf-8'))
            stdout, stderr = self.run_child(path)
            self.assertTrue(self.matches(stdout, python))

    # Tests with UTF-8 Python sources with BOM
    def test_shebang_utf8_bom(self):
        "Test shebangs in UTF-8 files with BOM"
        for shebang in SHEBANGS.values():
            # If there's no Unicode, all should be well
            path = self.make_script(shebang_line=shebang, encoding='utf-8',
                                    bom=BOM_UTF8)
            stdout, stderr = self.run_child(path)
            python = self.get_python_for_shebang(shebang)
            self.assertTrue(self.matches(stdout, python))
            # If there's a Unicode comment, we should still be fine as
            # there's a BOM
            path = self.make_script(shebang_line=shebang, encoding='utf-8',
                                    comment=COMMENT_WITH_UNICODE,
                                    bom=BOM_UTF8)
            stdout, stderr = self.run_child(path)
            python = self.get_python_for_shebang(shebang)
            self.assertTrue(self.matches(stdout, python))

def read_data(path):
    if not os.path.exists(path):
        result = None
    else:
        with open(path, 'r') as f:
            result = f.read()
    return result

def write_data(path, value):
    with open(path, 'w') as f:
        f.write(value)

class ConfiguredScriptMaker(ScriptMaker):
    def setUp(self):
        ScriptMaker.setUp(self)

        self.local_ini = path = os.path.join(os.environ['LOCALAPPDATA'],
                                                  'py.ini')
        self.local_config = read_data(path)
        self.global_ini = path = os.path.join(os.path.dirname(LAUNCHER),
                                              'py.ini')
        self.global_config = read_data(path)

    def tearDown(self):
        if self.local_config is not None:
            write_data(self.local_ini, self.local_config)
        if self.global_config is not None:
            write_data(self.global_ini, self.global_config)
        ScriptMaker.tearDown(self)

LOCAL_INI = '''[commands]
h3  = {p3.executable} --help
v3  = {p3.executable} --version
v2a = {p2.executable} -v

[defaults]
python=3
python3={p3.version}
'''.format(p2=DEFAULT_PYTHON2, p3=DEFAULT_PYTHON3)

GLOBAL_INI = '''[commands]
h2  = {p2.executable} -h
h3  = {p3.executable} -h
v2  = {p2.executable} -V
v3  = {p3.executable} -V
v3a = {p3.executable} -v
shell = cmd /c

[defaults]
python=2
python2={p2.version}
'''.format(p2=DEFAULT_PYTHON2, p3=DEFAULT_PYTHON3)

class ConfigurationTest(ConfiguredScriptMaker, unittest.TestCase):
    def test_basic(self):
        "Test basic configuration"
        # We're specifying Python 3 in the local ini...
        write_data(self.local_ini, LOCAL_INI)
        write_data(self.global_ini, GLOBAL_INI)
        shebang = SHEBANGS['PY']    # just 'python' ...
        path = self.make_script(shebang_line=shebang)
        stdout, stderr = self.run_child(path)
        self.assertTrue(self.matches(stdout, DEFAULT_PYTHON3))
        # Now zap the local configuration ... should get Python 2
        write_data(self.local_ini, '')
        stdout, stderr = self.run_child(path)
        self.assertTrue(self.matches(stdout, DEFAULT_PYTHON2))

    def test_customised(self):
        "Test customised commands"
        write_data(self.local_ini, LOCAL_INI)
        write_data(self.global_ini, GLOBAL_INI)

        # Python 3 with help
        shebang = '#!h3\n'
        path = self.make_script(shebang_line=shebang)
        stdout, stderr = self.run_child(path)
        self.assertTrue(stdout.startswith(b'usage: '))
        # Assumes standard Python installation directory
        self.assertIn(DEFAULT_PYTHON3.bdir, stdout)

        # Python 2 with help
        shebang = '#!h2\n'
        path = self.make_script(shebang_line=shebang)
        stdout, stderr = self.run_child(path)
        self.assertTrue(stdout.startswith(b'usage: '))
        # Assumes standard Python installation directory
        self.assertIn(DEFAULT_PYTHON2.bdir, stdout)

        # Python 3 version
        for prefix in VIRT_PATHS:
            shebang = '#!%sv3\n' % prefix
            path = self.make_script(shebang_line=shebang)
            stdout, stderr = self.run_child(path)
            self.assertTrue(stderr.startswith(DEFAULT_PYTHON3.output_version))

        # Python 2 version
        for prefix in VIRT_PATHS:
            shebang = '#!%sv2\n' % prefix
            path = self.make_script(shebang_line=shebang)
            stdout, stderr = self.run_child(path)
            self.assertTrue(stderr.startswith(DEFAULT_PYTHON2.output_version))

        VERBOSE_START = b'# installing zipimport hook'
        VERBOSE_START_3 = b'import _frozen_importlib # frozen'

        # Python 3 with -v
        shebang = '#!v3a\n'
        path = self.make_script(shebang_line=shebang)
        stdout, stderr = self.run_child(path)
        self.assertTrue(stderr.startswith(VERBOSE_START_3))
        # Assumes standard Python installation directory
        self.assertIn(DEFAULT_PYTHON3.bdir, stderr)

        # Python 2 with -v
        shebang = '#!v2a\n'
        path = self.make_script(shebang_line=shebang)
        stdout, stderr = self.run_child(path)
        self.assertTrue(stderr.startswith(VERBOSE_START))
        self.assertIn(DEFAULT_PYTHON2.bdir, stderr)

        # Python 2 with -V via cmd.exe /C
        shebang = '#!shell %s -V\n' % DEFAULT_PYTHON2.executable
        path = self.make_script(shebang_line=shebang)
        stdout, stderr = self.run_child(path)
        self.assertTrue(stderr.startswith(DEFAULT_PYTHON2.output_version))
        shebang = '#!shell %s -v\n' % DEFAULT_PYTHON2.executable
        path = self.make_script(shebang_line=shebang)
        stdout, stderr = self.run_child(path)
        self.assertTrue(stdout.startswith(DEFAULT_PYTHON2.bversion))
        self.assertTrue(stderr.startswith(VERBOSE_START))

    def test_environment(self):
        "Test configuration via the environment"
        "Test basic configuration"
        # We're specifying Python 3 in the local ini...
        write_data(self.local_ini, LOCAL_INI)
        write_data(self.global_ini, GLOBAL_INI)
        shebang = SHEBANGS['PY']    # just 'python' ...
        path = self.make_script(shebang_line=shebang)
        stdout, stderr = self.run_child(path)
        self.assertTrue(self.matches(stdout, DEFAULT_PYTHON3))
        # Now, override in the environment
        env = os.environ.copy()
        env['PY_PYTHON'] = '2'
        stdout, stderr = self.run_child(path, env=env)
        self.assertTrue(self.matches(stdout, DEFAULT_PYTHON2))
        # And again without the environment change
        stdout, stderr = self.run_child(path)
        self.assertTrue(self.matches(stdout, DEFAULT_PYTHON3))


if __name__ == '__main__':
    unittest.main()