Exemple #1
0
def source_exec_as_module(source):
    try:
        return eval_cache[source]
    except KeyError:
        pass

    d = eval_directory()
    with add_directory_to_path(d):
        final_name = u'hypothesis_temporary_module_%s' % (
            hashlib.sha1(source.encode(u'utf-8')).hexdigest(),
        )
        temporary_name = u'hypothesis_temporary_module_%s_%s' % (
            hashlib.sha1(source.encode(u'utf-8')).hexdigest(),
            uuid.uuid4(),
        )
        temporary_filepath = os.path.join(d, temporary_name + u'.py')
        final_filepath = os.path.join(d, final_name + u'.py')
        f = open(temporary_filepath, u'w')
        f.write(source)
        f.close()
        assert os.path.exists(temporary_filepath)

        try:
            os.rename(temporary_filepath, final_filepath)
        except OSError:  # pragma: no cover
            # The odds of final_filepath being a directory are basically zero,
            # and it's basically impossible for them to be on different
            # filesystems, so
            # if this is raised it's because the destination already exists on
            # Windows. That's fine, it won't be different, so just keep going,
            # deleting our tempfile.
            assert not os.path.isdir(final_filepath)
            os.remove(temporary_filepath)

        assert os.path.exists(final_filepath)
        assert not os.path.exists(temporary_filepath)
        with open(final_filepath) as r:
            assert r.read() == source
        importlib_invalidate_caches()
        result = __import__(final_name)
        eval_cache[source] = result
        return result
Exemple #2
0
def source_exec_as_module(source):
    try:
        return eval_cache[source]
    except KeyError:
        pass

    d = eval_directory()
    add_directory_to_path(d)
    name = 'hypothesis_temporary_module_%s' % (hashlib.sha1(
        source.encode('utf-8')).hexdigest(), )
    filepath = os.path.join(d, name + '.py')
    f = open(filepath, 'w')
    f.write(source)
    f.close()
    assert os.path.exists(filepath)
    assert open(filepath).read() == source
    importlib_invalidate_caches()
    result = __import__(name)
    eval_cache[source] = result
    return result
Exemple #3
0
def source_exec_as_module(source):
    try:
        return eval_cache[source]
    except KeyError:
        pass

    d = eval_directory()
    with add_directory_to_path(d):
        final_name = u'hypothesis_temporary_module_%s' % (hashlib.sha1(
            source.encode(u'utf-8')).hexdigest(), )
        temporary_name = u'hypothesis_temporary_module_%s_%s' % (
            hashlib.sha1(source.encode(u'utf-8')).hexdigest(),
            uuid.uuid4(),
        )
        temporary_filepath = os.path.join(d, temporary_name + u'.py')
        final_filepath = os.path.join(d, final_name + u'.py')
        f = open(temporary_filepath, u'w')
        f.write(source)
        f.close()
        assert os.path.exists(temporary_filepath)

        try:
            os.rename(temporary_filepath, final_filepath)
        except OSError:  # pragma: no cover
            # The odds of final_filepath being a directory are basically zero,
            # and it's basically impossible for them to be on different
            # filesystems, so
            # if this is raised it's because the destination already exists on
            # Windows. That's fine, it won't be different, so just keep going,
            # deleting our tempfile.
            assert not os.path.isdir(final_filepath)
            os.remove(temporary_filepath)

        assert os.path.exists(final_filepath)
        assert not os.path.exists(temporary_filepath)
        with open(final_filepath) as r:
            assert r.read() == source
        importlib_invalidate_caches()
        result = __import__(final_name)
        eval_cache[source] = result
        return result
Exemple #4
0
def source_exec_as_module(source):
    try:
        return eval_cache[source]
    except KeyError:
        pass

    d = eval_directory()
    add_directory_to_path(d)
    name = 'hypothesis_temporary_module_%s' % (
        hashlib.sha1(source.encode('utf-8')).hexdigest(),
    )
    filepath = os.path.join(d, name + '.py')
    f = open(filepath, 'w')
    f.write(source)
    f.close()
    assert os.path.exists(filepath)
    assert open(filepath).read() == source
    importlib_invalidate_caches()
    result = __import__(name)
    eval_cache[source] = result
    return result