• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python test.createSandboxConfig函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sandbox.test.createSandboxConfig函数的典型用法代码示例。如果您正苦于以下问题:Python createSandboxConfig函数的具体用法?Python createSandboxConfig怎么用?Python createSandboxConfig使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了createSandboxConfig函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_stdout

def test_stdout():
    import sys

    config = createSandboxConfig(disable_debug=True)
    with capture_stdout() as stdout:
        def print_denied():
            print "Hello Sandbox 1"
        try:
            Sandbox(config).call(print_denied)
        except SandboxError:
            pass
        else:
            assert False

        def print_allowed():
            print "Hello Sandbox 2"
        config2 = createSandboxConfig('stdout')
        Sandbox(config2).call(print_allowed)

        print "Hello Sandbox 3"

        sys.stdout.flush()
        stdout.seek(0)
        output = stdout.read()

    assert output == "Hello Sandbox 2\nHello Sandbox 3\n"
开发者ID:yingted,项目名称:pysandbox,代码行数:26,代码来源:test_misc.py


示例2: test_module_frame_restricted

def test_module_frame_restricted():
    from sys import _getframe

    config = createSandboxConfig(cpython_restricted=True)
    def check_module_restricted():
        restricted = _test_restricted(_getframe)
        assert restricted == True
    Sandbox(config).call(check_module_restricted)

    if HAVE_CSANDBOX:
        config = createSandboxConfig(cpython_restricted=False)
        def check_module_not_restricted():
            restricted = _test_restricted(_getframe)
            assert restricted == False
        Sandbox(config).call(check_module_not_restricted)
开发者ID:ailling,项目名称:pysandbox,代码行数:15,代码来源:test_restricted.py


示例3: test_stdout

def test_stdout():
    with capture_stdout() as stdout:
        config = SandboxConfig()

        def print_denied():
            print "Hello Sandbox 1"

        try:
            Sandbox(config).call(print_denied)
        except SandboxError:
            pass
        else:
            assert False

        def print_allowed():
            print "Hello Sandbox 2"

        Sandbox(createSandboxConfig("stdout")).call(print_allowed)

        print "Hello Sandbox 3"

        stdout.seek(0)
        output = stdout.read()

    assert output == "Hello Sandbox 2\nHello Sandbox 3\n"
开发者ID:rootart,项目名称:pysandbox,代码行数:25,代码来源:test_misc.py


示例4: test_open_whitelist

def test_open_whitelist():
    config = createSandboxConfig()
    if config.cpython_restricted:
        # the CPython restricted mode denies to open any file
        raise SkipTest("require _sandbox")
    config.allowPath(READ_FILENAME)
    Sandbox(config).call(read_first_line, open)
开发者ID:rootart,项目名称:pysandbox,代码行数:7,代码来源:test_open.py


示例5: test_exit

def test_exit():
    def exit_noarg():
        try:
            exit()
        except SandboxError as err:
            assert str(err) == "exit() function blocked by the sandbox"
        else:
            assert False
    createSandbox().call(exit_noarg)

    config = createSandboxConfig("exit")
    def exit_1():
        try:
            exit(1)
        except SystemExit as err:
            assert err.args[0] == 1
        else:
            assert False

        import sys
        try:
            sys.exit("bye")
        except SystemExit as err:
            assert err.args[0] == "bye"
        else:
            assert False
    Sandbox(config).call(exit_1)
开发者ID:yingted,项目名称:pysandbox,代码行数:27,代码来源:test_misc.py


示例6: test_filetype_from_open_file

def test_filetype_from_open_file():
    def get_file_type_from_open_file(filename):
        try:
            with open(filename, "rb") as fp:
                return _get_file_type(fp)
        except SandboxError:
            pass

        try:
            with open(filename, "rb") as fp:
                return type(fp)
        except SandboxError:
            pass
        raise TestException("Unable to get file type")

    filename = READ_FILENAME

    config = createSandboxConfig()
    if config.cpython_restricted:
        # the CPython restricted mode denies to open any file
        raise SkipTest("require _sandbox")
    config.allowPath(filename)

    def get_file_type_object():
        file_type = get_file_type_from_open_file(filename)
        try:
            read_first_line(file_type)
        except TypeError, err:
            assert str(err) in ("object.__new__() takes no parameters", "default __new__ takes no parameters")
        else:
开发者ID:rootart,项目名称:pysandbox,代码行数:30,代码来源:test_open.py


示例7: test_del_builtin

def test_del_builtin():
    code = unindent('''
        def del_builtin_import():
            import_func = __builtins__['__import__']
            dict.__delitem__(__builtins__, '__import__')
            try:
                try:
                    import sys
                except NameError as err:
                    assert str(err) == "type object 'dict' has no attribute '__setitem__'"
            finally:
                __builtins__['__import__'] = import_func
    ''')

    unsafe_code = code + unindent('''
        try:
            del_builtin_import()
        except AttributeError as err:
            assert str(err) == "type object 'dict' has no attribute '__delitem__'"
        except SandboxError as err:
            assert str(err) == "Read only object"
        else:
            assert False
    ''')

    config = createSandboxConfig()
    config.allowModule('sys')
    Sandbox(config).execute(unsafe_code)
开发者ID:yingted,项目名称:pysandbox,代码行数:28,代码来源:test_builtins.py


示例8: test

 def test(*lines, **kw):
     code = "; ".join(lines)
     config = createSandboxConfig()
     if HAVE_PYPY:
         # FIXME: is it really needed?
         config._builtins_whitelist.add("compile")
     Sandbox(config).execute(code, **kw)
开发者ID:janus,项目名称:pysandbox,代码行数:7,代码来源:test_execute.py


示例9: test_filetype_from_open_file

def test_filetype_from_open_file():
    if not HAVE_CSANDBOX:
        # restricted mode deny to open any file
        raise SkipTest("require _sandbox")

    def get_file_type_from_open_file(filename):
        try:
            with open(filename, 'rb') as fp:
                return _get_file_type(fp)
        except SandboxError:
            pass

        try:
            with open(filename, 'rb') as fp:
                return type(fp)
        except SandboxError:
            pass
        raise TestException("Unable to get file type")

    filename = READ_FILENAME

    config = createSandboxConfig()
    config.allowPath(filename)
    def get_file_type_object():
        file_type = get_file_type_from_open_file(filename)
        try:
            read_first_line(file_type)
        except TypeError, err:
            assert str(err) in ('object.__new__() takes no parameters', 'default __new__ takes no parameters')
        else:
开发者ID:carlio,项目名称:pysandbox,代码行数:30,代码来源:test_open.py


示例10: test_sytem_exit

def test_sytem_exit():
    def system_exit_denied():
        try:
            raise SystemExit()
        except NameError as err:
            assert str(err) == "global name 'SystemExit' is not defined"
        except:
            assert False
    createSandbox().call(system_exit_denied)

    config = createSandboxConfig("exit")
    def system_exit_allowed():
        try:
            raise SystemExit()
        except SystemExit:
            pass
        else:
            assert False
    Sandbox(config).call(system_exit_allowed)

    try:
        raise SystemExit()
    except SystemExit:
        pass
    else:
        assert False
开发者ID:yingted,项目名称:pysandbox,代码行数:26,代码来源:test_misc.py


示例11: test_open_whitelist

def test_open_whitelist():
    if not HAVE_CSANDBOX:
        # restricted python denies access to all files
        raise SkipTest("require _sandbox")

    config = createSandboxConfig()
    config.allowPath(READ_FILENAME)
    Sandbox(config).call(read_first_line, open)
开发者ID:carlio,项目名称:pysandbox,代码行数:8,代码来源:test_open.py


示例12: test_random

def test_random():
    config = createSandboxConfig('random')
    if config.cpython_restricted:
        raise SkipTest("deny importing modules")

    check_random = 'import random; random.randint(1, 6)'

    Sandbox(config).execute(check_random)
开发者ID:ailling,项目名称:pysandbox,代码行数:8,代码来源:test_random.py


示例13: test_execute

def test_execute():
    config = createSandboxConfig()
    if HAVE_PYPY:
        # FIXME: is it really needed?
        config._builtins_whitelist.add('compile')
    if config.use_subprocess:
        globals_builtins = set()
    else:
        globals_builtins = set(( '__builtins__',))

    def test(*lines, **kw):
        code = "; ".join(lines)
        Sandbox(config).execute(code, **kw)

    test(
        "assert globals() is locals(), 'test_execute #1a'",
        "assert list(globals().keys()) == list(locals().keys()) == ['__builtins__'], 'test_execute #1b'",
        "x=42")

    namespace = {'a': 1}
    test(
        "assert globals() is locals(), 'test_execute #2a'",
        "assert list(globals().keys()) == list(locals().keys()) == ['a', '__builtins__'], 'test_execute #2b'",
        "a=10",
        "x=42",
        globals=namespace)
    assert set(namespace.keys()) == (set(('a', 'x')) | globals_builtins)
    assert namespace['a'] == 10
    assert namespace['x'] == 42

    namespace = {'b': 2}
    test(
        "assert globals() is not locals(), 'test_execute #3a'",
        "assert list(globals().keys()) == ['__builtins__'], 'test_execute #3b'",
        "assert list(locals().keys()) == ['b'], 'test_execute #3c'",
        "b=20",
        "x=42",
        locals=namespace)
    assert namespace == {'b': 20, 'x': 42}

    my_globals = {'a': 1}
    namespace = {'b': 2}
    test(
        "assert globals() is not locals(), 'test_execute #4a'",
        "assert list(globals().keys()) == ['a', '__builtins__'], 'test_execute #4b'",
        "assert list(locals().keys()) == ['b'], 'test_execute #4c'",
        "x=42",
        "a=10",
        "b=20",
        globals=my_globals,
        locals=namespace)
    assert set(my_globals.keys()) == (set(('a',)) | globals_builtins)
    assert my_globals['a'] == 1
    assert namespace == {'a': 10, 'b': 20, 'x': 42}

    namespace = {}
    test('a=1', locals=namespace)
    assert namespace == {'a': 1}, namespace
开发者ID:ailling,项目名称:pysandbox,代码行数:58,代码来源:test_execute.py


示例14: test_exec_builtins

def test_exec_builtins():
    def check_builtins_type():
        result = []
        exec "result.append(type(__builtins__))" in {'result': result}
        builtin_type = result[0]
        assert builtin_type != dict
    config = createSandboxConfig()
    if HAVE_PYPY:
        # FIXME: is it really needed?
        config._builtins_whitelist.add('compile')
    Sandbox(config).call(check_builtins_type)
开发者ID:carlio,项目名称:pysandbox,代码行数:11,代码来源:test_builtins.py


示例15: test_timeout

def test_timeout():
    def denial_of_service():
        try:
            while 1:
                pass
        except Timeout:
            pass

    config = createSandboxConfig()
    config.timeout = 0.1
    Sandbox(config).call(denial_of_service)
开发者ID:carlio,项目名称:pysandbox,代码行数:11,代码来源:test_misc.py


示例16: test_readonly_import

def test_readonly_import():
    config = createSandboxConfig()
    config.allowModule('sys', 'version')
    def readonly_module():
        import sys

        try:
            sys.version = '3000'
        except SandboxError, err:
            assert str(err) == "Read only object"
        else:
开发者ID:ailling,项目名称:pysandbox,代码行数:11,代码来源:test_import.py


示例17: test_call_exec_builtins

def test_call_exec_builtins():
    code = unindent('''
        result = []
        exec "result.append(type(__builtins__))" in {'result': result}
        builtin_type = result[0]
        assert builtin_type != dict
    ''')
    config = createSandboxConfig()
    if HAVE_PYPY:
        # FIXME: is it really needed?
        config._builtins_whitelist.add('compile')
    Sandbox(config).execute(code)
开发者ID:yingted,项目名称:pysandbox,代码行数:12,代码来源:test_builtins.py


示例18: test_filetype_from_sys_stdout

def test_filetype_from_sys_stdout():
    def get_file_type_from_stdout():
        import sys
        return _get_file_type(sys.stdout)

    config = createSandboxConfig('stdout')
    def get_file_type_object():
        file_type = get_file_type_from_stdout()
        try:
            read_first_line(file_type)
        except TypeError, err:
            assert str(err) in ('object.__new__() takes no parameters', 'default __new__ takes no parameters')
        else:
开发者ID:carlio,项目名称:pysandbox,代码行数:13,代码来源:test_open.py


示例19: test_import_whitelist

def test_import_whitelist():
    # sys.version is allowed by the sandbox
    import sys
    sys_version = sys.version
    del sys

    config = createSandboxConfig()
    config.allowModule('sys', 'version')
    def import_sys():
        import sys
        assert sys.__name__ == 'sys'
        assert sys.version == sys_version
    Sandbox(config).call(import_sys)
开发者ID:ailling,项目名称:pysandbox,代码行数:13,代码来源:test_import.py


示例20: test_bytecode

def test_bytecode():
    if not HAVE_CSANDBOX:
        # restricted mode doesn't block creation of arbitrary code object
        raise SkipTest("require _sandbox")

    code_args = get_code_args()

    config = createSandboxConfig()
    config.allowModule('sys', '_getframe')
    try:
        Sandbox(config).call(exec_bytecode, code_args)
    except TestException, err:
        assert str(err) == "Unable to get code type"
开发者ID:ailling,项目名称:pysandbox,代码行数:13,代码来源:test_code.py



注:本文中的sandbox.test.createSandboxConfig函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python Parameter.Parameter类代码示例发布时间:2022-05-27
下一篇:
Python layers_powered.LayersPowered类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap