helper.py 1.82 KB
Newer Older
1 2
import sys
import os
3
import imp
4

5 6 7 8 9 10 11
version = '%s.%s' % sys.version_info[:2]

missing_modules = {
    'test_smtplib': ['2.4', '2.5'],
    'test_asyncore': ['2.4', '2.5'],
    'test_telnetlib': ['2.4', '2.5'],
    'test_httpservers': ['2.4', '2.5'],
12
    'test_ftplib': ['2.4', '2.5'],
13
    'test_wsgiref': ['2.4'],
Denis Bilenko's avatar
Denis Bilenko committed
14
    'test_socket_ssl': ['2.6', '2.7']}
15 16


17 18 19 20
class ContainsAll(object):
    def __contains__(self, item):
        return True

Denis Bilenko's avatar
Denis Bilenko committed
21

22 23 24
def patch_all(timeout=None):
    from gevent import monkey
    monkey.patch_all(aggressive=True)
Denis Bilenko's avatar
Denis Bilenko committed
25 26
    import unittest
    import greentest
27 28 29 30 31
    unittest.TestCase = greentest.TestCase
    if timeout is not None:
        unittest.TestCase.__timeout__ = timeout


32 33 34 35 36 37 38 39 40
def imp_find_dotted_module(name):
    """imp.find_module with dotted names"""
    path = None
    for x in name.split('.'):
        result = imp.find_module(x, path)
        path = [result[1]]
    return result


41 42
def prepare_stdlib_test(filename):
    patch_all(timeout=20)
43 44 45 46 47 48
    import test
    try:
        from test import test_support
    except ImportError:
        sys.stderr.write('test.__file__ = %s\n' % test.__file__)
        raise
49 50
    test_support.use_resources = ContainsAll()

51
    name = os.path.splitext(os.path.basename(filename))[0].replace('_patched', '')
52 53 54 55

    os.environ['__module_name__'] = name

    try:
56
        _f, _filename, _ = imp_find_dotted_module('test.%s' % name)
57
    except:
Denis Bilenko's avatar
Denis Bilenko committed
58
        if version in missing_modules.get(name, []):
59
            sys.exit(0)
60
        sys.stderr.write('Failed to import test.%s\n' % name)
61
        raise
62

63
    module_source = _f.read()
64 65 66 67 68 69
    from patched_tests_setup import disable_tests_in_the_source
    module_source = disable_tests_in_the_source(module_source, name)
    module_code = compile(module_source, _filename, 'exec')

    print >> sys.stderr, 'Testing %s with monkey patching' % _filename
    return module_code