• <small id='QmR0q'></small><noframes id='QmR0q'>

    <legend id='QmR0q'><style id='QmR0q'><dir id='QmR0q'><q id='QmR0q'></q></dir></style></legend>

        <bdo id='QmR0q'></bdo><ul id='QmR0q'></ul>
      <tfoot id='QmR0q'></tfoot>
    1. <i id='QmR0q'><tr id='QmR0q'><dt id='QmR0q'><q id='QmR0q'><span id='QmR0q'><b id='QmR0q'><form id='QmR0q'><ins id='QmR0q'></ins><ul id='QmR0q'></ul><sub id='QmR0q'></sub></form><legend id='QmR0q'></legend><bdo id='QmR0q'><pre id='QmR0q'><center id='QmR0q'></center></pre></bdo></b><th id='QmR0q'></th></span></q></dt></tr></i><div id='QmR0q'><tfoot id='QmR0q'></tfoot><dl id='QmR0q'><fieldset id='QmR0q'></fieldset></dl></div>

        记录 python 子进程的语法错误和未捕获的异常并将它们打印到终端

        时间:2023-07-21
        <i id='kVMbq'><tr id='kVMbq'><dt id='kVMbq'><q id='kVMbq'><span id='kVMbq'><b id='kVMbq'><form id='kVMbq'><ins id='kVMbq'></ins><ul id='kVMbq'></ul><sub id='kVMbq'></sub></form><legend id='kVMbq'></legend><bdo id='kVMbq'><pre id='kVMbq'><center id='kVMbq'></center></pre></bdo></b><th id='kVMbq'></th></span></q></dt></tr></i><div id='kVMbq'><tfoot id='kVMbq'></tfoot><dl id='kVMbq'><fieldset id='kVMbq'></fieldset></dl></div>
          <bdo id='kVMbq'></bdo><ul id='kVMbq'></ul>
        • <tfoot id='kVMbq'></tfoot>
                <tbody id='kVMbq'></tbody>

              <small id='kVMbq'></small><noframes id='kVMbq'>

                <legend id='kVMbq'><style id='kVMbq'><dir id='kVMbq'><q id='kVMbq'></q></dir></style></legend>

                • 本文介绍了记录 python 子进程的语法错误和未捕获的异常并将它们打印到终端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在尝试编写一个程序来记录子进程的未捕获异常和语法错误.容易,对吧?只需将 stderr 管道传输到正确的位置.

                  I've been trying to write a program that logs uncaught exceptions and syntax errors for a subprocess. Easy, right? Just pipe stderr to the right place.

                  然而,子进程是另一个 python 程序——我将其称为 test.py——它需要像没有捕获其输出/错误一样运行.也就是说,运行记录器程序需要让用户看起来像正常运行 python test.py.

                  However, the subprocess is another python program- I'll call it test.py- that needs to run as if its output/errors are not being captured. That is, running the logger program needs to seem like the user has just run python test.py as normal.

                  使问题更复杂的是 问题 raw_input 实际上被发送如果未使用 readline,则转至 stderr.不幸的是,我不能只import readline,因为我无法控制正在使用我的错误记录器运行的文件.

                  Further complicating the issue is the problem that raw_input actually gets sent to stderr if readline is not used. Unfortunately, I can't just import readline, since I don't have control over the files that are being run using my error logger.

                  注意事项:

                  • 我在运行此代码的机器上受到相当大的限制.我无法安装 pexpect 或编辑 *customize.py 文件(因为该程序将由许多不同的用户运行).我真的觉得无论如何应该有一个标准库解决方案......
                  • 这只适用于 Mac.
                  • 这样做的动机是,我是一个研究新程序员遇到的错误的团队的一员.
                  • I am fairly restricted on the machines that this code will run on. I can't install pexpect or edit the *customize.py files (since the program will be run by a lot of different users). I really feel like there should be a stdlib solution anyway though...
                  • This only has to work on macs.
                  • The motivation for this is that I'm part of a team researching the errors that new programmers get.

                  我尝试了以下方法,没有成功:

                  I've tried the following methods, without success:

                  • 只使用 tee 作为问题 如何在使用tee"时将标准错误写入文件使用管道?(未能产生 raw_input 提示);我在几个 SO 问题中发现的 tee 的 python 实现有类似的问题
                  • 覆盖sys.excepthook(未能使其适用于子进程)
                  • 这个问题的最佳答案似乎很有希望,但未能正确显示raw_input提示.
                  • 日志模块 似乎对实际写入日志文件很有用,但并没有似乎没有抓住问题的症结
                  • 自定义标准错误阅读器
                  • 无休止的谷歌搜索
                  • just using tee as in the question How do I write stderr to a file while using "tee" with a pipe? (failed to produce raw_input prompts); python implementations of tee that I found in several SO questions had similar issues
                  • overwriting sys.excepthook (failed to make it work for a subprocess)
                  • this question's top answer seemed promising, but it failed to display raw_input prompts correctly.
                  • the logging module seems useful for actually writing to a log file, but doesn't seem to get at the crux of the issue
                  • custom stderr readers
                  • endless googling

                  推荐答案

                  根据@nneonneo 在问题评论中的建议,我制作了这个似乎可以完成工作的程序.(请注意,目前,记录器文件的名称必须使用pylog"才能将错误正确打印给最终用户.)

                  Based on @nneonneo 's advice in the question comments, I made this program that seems to get the job done. (Note that currently, the name of the logger file has to by "pylog" for the errors to be printed correctly to the end user.)

                  #!/usr/bin/python
                  
                  '''
                  This module logs python errors.
                  '''
                  
                  import socket, os, sys, traceback
                  
                  def sendError(err):
                      # log the error (in my actual implementation, this sends the error to a database)
                      with open('log','w') as f:
                          f.write(err)
                  
                  
                  def exceptHandler(etype, value, tb):
                      """An additional wrapper around our custom exception handler, to prevent errors in
                         this program from being seen by end users."""
                      try:
                          subProgExceptHandler(etype, value, tb)
                      except:
                          sys.stderr.write('Sorry, but there seems to have been an error in pylog itself. Please run your program using regular python.
                  ')
                  
                  def subProgExceptHandler(etype, value, tb):
                      """A custom exception handler that both prints error and traceback information in the standard
                         Python format, as well as logs it."""
                      import linecache
                  
                      errorVerbatim = ''
                  
                      # The following code mimics a traceback.print_exception(etype, value, tb) call.
                      if tb:
                          msg = "Traceback (most recent call last):
                  "
                          sys.stderr.write(msg)
                          errorVerbatim += msg
                  
                          # The following code is a modified version of the trackeback.print_tb implementation from
                          # cypthon 2.7.3
                          while tb is not None:
                              f = tb.tb_frame                                                      
                              lineno = tb.tb_lineno                                                  
                              co = f.f_code                                                        
                              filename = co.co_filename                                              
                              name = co.co_name
                              # Filter out exceptions from pylog itself (eg. execfile).
                              if not "pylog" in filename:
                                  msg = '  File "%s", line %d, in %s
                  ' % (filename, lineno, name)
                                  sys.stderr.write(msg)       
                                  errorVerbatim += msg
                                  linecache.checkcache(filename)                                         
                                  line = linecache.getline(filename, lineno, f.f_globals)                
                                  if line: 
                                      msg = '    ' + line.strip() + '
                  '
                                      sys.stderr.write(msg)
                                      errorVerbatim += msg
                              tb = tb.tb_next                                           
                  
                      lines = traceback.format_exception_only(etype, value)
                      for line in lines:
                          sys.stderr.write(line)
                          errorVerbatim += line
                  
                      # Send the error data to our database handler via sendError.
                      sendError(errorVerbatim)
                  
                  def main():
                      """Executes the program specified by the user in its own sandbox, then sends
                         the error to our database for logging and analysis."""
                      # Get the user's (sub)program to run.
                      try:
                          subProgName = sys.argv[1]
                          subProgArgs = sys.argv[3:]
                      except:
                          print 'USAGE: ./pylog FILENAME.py *ARGS'
                          sys.exit()
                  
                      # Catch exceptions by overriding the system excepthook.
                      sys.excepthook = exceptHandler
                      # Sandbox user code exeuction to its own global namespace to prevent malicious code injection.
                      execfile(subProgName, {'__builtins__': __builtins__, '__name__': '__main__', '__file__': subProgName, '__doc__': None, '__package__': None})
                  
                  if __name__ == '__main__':
                      main()
                  

                  这篇关于记录 python 子进程的语法错误和未捕获的异常并将它们打印到终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用Python打开shell环境,运行命令并退出环境 下一篇:通过 subprocess.check_output 调用的可执行文件在控制台上打印,但不返回结果

                  相关文章

                  1. <small id='voHk9'></small><noframes id='voHk9'>

                    <legend id='voHk9'><style id='voHk9'><dir id='voHk9'><q id='voHk9'></q></dir></style></legend>

                    <i id='voHk9'><tr id='voHk9'><dt id='voHk9'><q id='voHk9'><span id='voHk9'><b id='voHk9'><form id='voHk9'><ins id='voHk9'></ins><ul id='voHk9'></ul><sub id='voHk9'></sub></form><legend id='voHk9'></legend><bdo id='voHk9'><pre id='voHk9'><center id='voHk9'></center></pre></bdo></b><th id='voHk9'></th></span></q></dt></tr></i><div id='voHk9'><tfoot id='voHk9'></tfoot><dl id='voHk9'><fieldset id='voHk9'></fieldset></dl></div>
                      • <bdo id='voHk9'></bdo><ul id='voHk9'></ul>
                    1. <tfoot id='voHk9'></tfoot>