test_process.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:zenchmarks 作者: squeaky-pl 项目源码 文件源码
def test_errorDuringExec(self):
        """
        When L{os.execvpe} raises an exception, it will format that exception
        on stderr as UTF-8, regardless of system encoding information.
        """

        def execvpe(*args, **kw):
            # Ensure that real traceback formatting has some non-ASCII in it,
            # by forcing the filename of the last frame to contain non-ASCII.
            filename = u"<\N{SNOWMAN}>"
            if not isinstance(filename, str):
                filename = filename.encode("utf-8")
            codeobj = compile("1/0", filename, "single")
            eval(codeobj)

        self.patch(os, "execvpe", execvpe)
        self.patch(sys, "getfilesystemencoding", lambda: "ascii")

        reactor = self.buildReactor()
        output = io.BytesIO()

        @reactor.callWhenRunning
        def whenRunning():
            class TracebackCatcher(ProcessProtocol, object):
                errReceived = output.write
                def processEnded(self, reason):
                    reactor.stop()
            reactor.spawnProcess(TracebackCatcher(), pyExe,
                                 [pyExe, b"-c", b""])

        self.runReactor(reactor, timeout=30)
        self.assertIn(u"\N{SNOWMAN}".encode("utf-8"), output.getvalue())
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号