【问题标题】:Preventing PyOSC from trapping exceptions防止 PyOSC 捕获异常
【发布时间】:2014-04-18 01:57:52
【问题描述】:

PyOSC 非常有礼貌地处理各种异常,但不幸的是使其难以调试。我该如何打败它?

例如,我有一个编码错误,它报告为:

OSCServer: NameError on request from localhost:50542: global name 'cell' is not defined

通常在 python 中,这会抛出一个带有有用跟踪的异常。但在 PyOSC 中,这就是我得到的全部。

如何关闭 PyOSC 的异常处理?

更新:一个月后,这个问题收到了零个 cmets 或答案。猜猜使用 Python、开放式声音控制和 PyOSC 模块的人并不多。我终于在下面回答了我自己的问题。

【问题讨论】:

    标签: python debugging trace osc


    【解决方案1】:

    好的,我想通了。由于 PyOSC 的文档很少,因此源代码用作文档。

    以下是捕获异常的代码:

    def handle_error(self, request, client_address):
        """Handle an exception in the Server's callbacks gracefully.
        Writes the error to sys.stderr and, if the error_prefix (see setSrvErrorPrefix()) is set,
        sends the error-message as reply to the client
        """
        (e_type, e) = sys.exc_info()[:2]
        self.printErr("%s on request from %s: %s" % (e_type.__name__, getUrlStr(client_address), str(e)))
    
        if self.print_tracebacks:
            import traceback
            traceback.print_exc() # XXX But this goes to stderr!
    
        if len(self.error_prefix):
            self.sendOSCerror("%s: %s" % (e_type.__name__, str(e)), client_address)
    

    这是在创建 OSCServer 实例时打开跟踪的简单方法:

    import OSC
    
    myoscserver = OSC.OSCServer( (host, port) )
    myoscserver.print_tracebacks = True
    

    这也适用于 OSCClient:

    myoscclient = OSC.OSCClient()
    myoscclient.connect( (host, port) )
    myoscclient.print_tracebacks = True
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      • 2012-04-05
      • 2023-03-31
      • 1970-01-01
      • 2016-06-19
      相关资源
      最近更新 更多