def test_set_wrapper_1(self):
        async def foo():
            return 'spam'

        wrapped = None
        def wrap(gen):
            nonlocal wrapped
            wrapped = gen
            return gen

        self.assertIsNone(sys.get_coroutine_wrapper())

        sys.set_coroutine_wrapper(wrap)
        self.assertIs(sys.get_coroutine_wrapper(), wrap)
        try:
            f = foo()
            self.assertTrue(wrapped)

            self.assertEqual(run_async(f), ([], 'spam'))
        finally:
            sys.set_coroutine_wrapper(None)

        self.assertIsNone(sys.get_coroutine_wrapper())

        wrapped = None
        with silence_coro_gc():
            foo()
        self.assertFalse(wrapped)
示例#2
0
def test_get_set_coroutine_wrapper_deprecated():
    import warnings
    def my_wrapper(cr):
        return 1
    with warnings.catch_warnings(record=True) as l:
        warnings.simplefilter('always', category=DeprecationWarning)
        sys.get_coroutine_wrapper()
        sys.set_coroutine_wrapper(my_wrapper)
        sys.set_coroutine_wrapper(None)
    print(l)
    assert len(l) == 3
示例#3
0
def test_set_coroutine_wrapper():
    async def f():
        pass
    seen = []
    def my_wrapper(cr):
        seen.append(cr)
        return 42
    assert sys.get_coroutine_wrapper() is None
    sys.set_coroutine_wrapper(my_wrapper)
    assert sys.get_coroutine_wrapper() is my_wrapper
    cr = f()
    assert cr == 42
    sys.set_coroutine_wrapper(None)
    assert sys.get_coroutine_wrapper() is None
示例#4
0
    def set_debug(self, enabled):
        self._debug = enabled
        wrapper = coroutines.debug_wrapper

        try:
            set_wrapper = sys.set_coroutine_wrapper
        except AttributeError:
            pass
        else:
            current_wrapper = sys.get_coroutine_wrapper()
            if enabled:
                if current_wrapper not in (None, wrapper):
                    warnings.warn(
                        "loop.set_debug(True): cannot set debug coroutine "
                        "wrapper; another wrapper is already set %r" %
                        current_wrapper, RuntimeWarning)
                else:
                    set_wrapper(wrapper)
            else:
                if current_wrapper not in (None, wrapper):
                    warnings.warn(
                        "loop.set_debug(False): cannot unset debug coroutine "
                        "wrapper; another wrapper was set %r" %
                        current_wrapper, RuntimeWarning)
                else:
                    set_wrapper(None)
 def test_set_wrapper_2(self):
     self.assertIsNone(sys.get_coroutine_wrapper())
     with self.assertRaisesRegex(TypeError, "callable expected, got int"):
         sys.set_coroutine_wrapper(1)
     self.assertIsNone(sys.get_coroutine_wrapper())
示例#6
0
async def coroutine_wrapper_deprecated():
    # set_coroutine_wrapper() and sys.get_coroutine_wrapper() will be removed in Python 3.8
    sys.set_coroutine_wrapper(sys.get_coroutine_wrapper())
    # and are deprecated in favor of
    sys.set_coroutine_origin_tracking_depth(
        sys.get_coroutine_origin_tracking_depth())
示例#7
0
def sys_func():
    lists = sys.argv  # 传递给Python脚本的命令行参数列表 => python p.py -> ['p.py'] / python p.py a 1 -> ['p.py', 'a', '1'] / 程序内执行 -> ['']
    strs = sys.getdefaultencoding()  # 默认字符集名称
    strs = sys.getfilesystemencoding()  # 系统文件名字符集名称
    num = sys.getrefcount(object)  # 返回object的引用计数(比实际多1个)
    dicts = sys.modules  # 已加载的模块, 可修改, 但不能通过修改返回的字典进行修改
    lists = sys.path  # 模块搜索路径
    sys.path.append("./test")  # 动态添加模块搜索路径
    strs = sys.platform  # 平台标识符(系统身份进行详细的检查,推荐使用) Linux:'linux' / Windows:'win32' / Cygwin:'cygwin' / Mac OS X:'darwin'
    strs = sys.version  # python解释器版本
    lists = sys.thread_info  # 线程信息
    num = sys.api_version  # 解释器C API版本

    types, value, back = sys.exc_info()  # 捕获异常 详见 异常 文章的 excep() 代码块第二小部分(http://blog.csdn.net/rozol/article/details/69313164)
    sys.excepthook(types, value, back)  # 打印异常
    types = sys.last_type
    value = sys.last_value
    back = sys.last_traceback
    # sys.exit([arg]) // 引发SystemExit异常退出Python(可以try), 范围[0,127], None==0, "string"==1
    sys.exit(0)

    num = sys.getrecursionlimit()  # 最大递归数(堆栈最大深度), 详见 函数 文章(http://blog.csdn.net/rozol/article/details/69242050)
    sys.setrecursionlimit(5000)  # 修改最大递归数

    fnum = sys.getswitchinterval()  # 获取线程切换间隔
    sys.setswitchinterval(0.005)  # 设置线程切换间隔, 单位秒
    num = sys.getcheckinterval()  # 解释器的检查间隔
    sys.setcheckinterval(100)  # 设置解释器检查间隔, 执行(默认)100个虚拟指令执行一次检查, 值为<=0时,检查每个虚拟指令

    # sys.stdin // 标准输入流
    strs = sys.stdin.readline()[:-1]
    # sys.stdout // 标准出入输出
    sys.stdout.write(">>")
    sys.stdout.flush()
    # sys.stderr // 标注错误流
    sys.stderr.write(">>")

    # ---

    lists = sys.builtin_module_names  # 所有模块 (注:非导入模块)
    path = sys.base_exec_prefix  # Python安装路径
    path = sys.base_prefix  # 同base_exec_prefix
    path = sys.exec_prefix  # 同base_exec_prefix
    path = sys.prefix  # 同base_exec_prefix
    path = sys.executable  # Python解释器的绝对路径

    strs = ys.byteorder  # 本机字节顺序指示器, big-endian(最高有效字节在第一位)值为'big', little-endian(最低有效字节在第一位)值为'little'
    strs = sys.copyright  # python版权
    num = sys.hexversion  # 16进制版本号
    lists = sys.implementation  # 当前运行的解释器的信息
    num = sys.getallocatedblocks()  # 解释器当前分配的内存块的数量
    boolean = sys.dont_write_bytecode  # 是否不会尝试导入源模块是写入.pyc文件 (False会写入.pyc文件)
    # sys.getsizeof(object[, default]) // 返回对象的大小bit, 只计算自身内存消耗,不计算引用对象的内存消耗, 调用对象的__sizeof__(), default没有获取到默认返回值
    num = sys.getsizeof(object)
    boolean = sys.is_finalizing()  # 解释器是否正在被关机
    num = sys.maxsize  # 最大整数值(2 ** 31 -1), 与系统有关
    num = sys.maxunicode  # 最大Unicode值的整数 (1114111)
    strs = sys.ps1  # 解释器主提示符
    strs = sys.ps2  # 解释器次提示符

    sys.call_tracing(func, ("arg",))  # 调用函数
    sys._clear_type_cache()  # 清除内部类型缓存
    sys._debugmallocstats()  # 打印CPython内存分配器状态的低级信息

    sys.setprofile(profilefunc)  # 设置profile函数, 默认None
    sys.getprofile()  # 获取profile函数
    sys.settrace(tracefunc)  # 设置跟踪函数, def tracefunc(frame、event 和arg):
    sys.gettrace()  # 获取跟踪函数, 默认None
    sys.set_coroutine_wrapper(wrapper)  # 设置包装 def wrapper(coro):
    sys.get_coroutine_wrapper()  # 包装, 默认None