Пример #1
0
def install_pytest_asserts():
    try:
        import ast  # noqa
    except ImportError as e:
        print('Can not use py.test asserts - import ast failed!')
        return
    else:
        # Both Jython and CPython 2.6.0 have AST bugs that make the
        # assertion rewriting hook malfunction.
        if (sys.platform.startswith('java')
                or sys.version_info[:3] == (2, 6, 0)):
            print(
                'Can not use py.test asserts - no compatible python interpreter'
            )
            return

    from _pytest.assertion import reinterpret  # noqa
    from _pytest.assertion import rewrite  # noqa

    m = monkeypatch()
    m.setattr(py.builtin.builtins, 'AssertionError',
              reinterpret.AssertionError)  # noqa

    hook = rewrite.AssertionRewritingHook()  # noqa
    sys.meta_path.insert(0, hook)
Пример #2
0
def install_importhook(config):
    """Try to install the rewrite hook, raise SystemError if it fails."""
    config._assertstate = AssertionState(config, "rewrite")
    config._assertstate.hook = hook = rewrite.AssertionRewritingHook(config)
    sys.meta_path.insert(0, hook)
    config._assertstate.trace("installed rewrite import hook")

    def undo():
        hook = config._assertstate.hook
        if hook is not None and hook in sys.meta_path:
            sys.meta_path.remove(hook)

    config.add_cleanup(undo)
    return hook
Пример #3
0
def install_importhook(config):
    """Try to install the rewrite hook, raise SystemError if it fails."""
    # Jython has an AST bug that make the assertion rewriting hook malfunction.
    if sys.platform.startswith("java"):
        raise SystemError("rewrite not supported")

    config._assertstate = AssertionState(config, "rewrite")
    config._assertstate.hook = hook = rewrite.AssertionRewritingHook(config)
    sys.meta_path.insert(0, hook)
    config._assertstate.trace("installed rewrite import hook")

    def undo():
        hook = config._assertstate.hook
        if hook is not None and hook in sys.meta_path:
            sys.meta_path.remove(hook)

    config.add_cleanup(undo)
    return hook
Пример #4
0
def install_importhook(config):
    """Try to install the rewrite hook, raise SystemError if it fails."""
    # Both Jython and CPython 2.6.0 have AST bugs that make the
    # assertion rewriting hook malfunction.
    if (sys.platform.startswith('java') or sys.version_info[:3] == (2, 6, 0)):
        raise SystemError('rewrite not supported')

    config._assertstate = AssertionState(config, 'rewrite')
    config._assertstate.hook = hook = rewrite.AssertionRewritingHook(config)
    sys.meta_path.insert(0, hook)
    config._assertstate.trace('installed rewrite import hook')

    def undo():
        hook = config._assertstate.hook
        if hook is not None and hook in sys.meta_path:
            sys.meta_path.remove(hook)

    config.add_cleanup(undo)
    return hook