python 在 setupterm 中诅咒远程调试 pdevd 错误

python curses remote debugging pdevd error in setupterm

是否可以在PyCharm中远程调试诅咒程序?如何设置?

我遵循了 PyCharm 4.0.8 指令,将其添加到“npyscreen-4.8.7”的“EXAMPLE.py”中。

import pydevd
pydevd.settrace('localhost', port=8899, stdoutToServer=False, stderrToServer=True)

并且总是在“setupterm”中遇到错误:

$ PYTHONPATH=~/bin/pycharm-debug.egg python EXAMPLE.py
Traceback (most recent call last):
  File "EXAMPLE.py", line 34, in <module>
    App.run()
  File "/home/.../npyscreen-4.8.7/npyscreen/apNPSApplication.py", line 30, in run
    return npyssafewrapper.wrapper(self.__remove_argument_call_main)
  File "/home/.../npyscreen-4.8.7/npyscreen/npyssafewrapper.py", line 41, in wrapper
    wrapper_no_fork(call_function)
  File "/home/.../npyscreen-4.8.7/npyscreen/npyssafewrapper.py", line 83, in wrapper_no_fork
    _SCREEN = curses.initscr()
  File "/usr/lib64/python2.6/curses/__init__.py", line 33, in initscr
    fd=_sys.__stdout__.fileno())
_curses.error: setupterm: could not find terminal

问题是 pydevd 将环境 "TERM" 从 "xterm" 更改为 "emacs"。这可以通过一个小测试程序来验证。

import pydevd
pydevd.settrace('localhost', port=8899, stdoutToServer=False, stderrToServer=True)
import os as _os
import sys as _sys
import curses
print " my term: ", _os.environ.get("TERM", "unknown"), "\n"
print " my fd:   ", _sys.__stdout__.fileno(), "\n"
print "\n  ok  1  \n"
curses.setupterm(term=_os.environ.get("TERM", "unknown"),
#curses.setupterm(term='xterm',
                fd=_sys.__stdout__.fileno())
print "\n  ok  2  \n"

我想问题是如何让“pydevd”设置正确的“TERM”?

虽然答案之一可能是这样的:更改 /usr/lib/python2.6/curses/__init__.py 以强制 TERM,例如:

def initscr():
    import _curses, curses
    # we call setupterm() here because it raises an error
    # instead of calling exit() in error cases.
    _os.environ['TERM'] = 'xterm' ##hack force 'xterm' for pydevd debugging.
    setupterm(term=_os.environ.get("TERM", "unknown"),
              fd=_sys.__stdout__.fileno())
    stdscr = _curses.initscr()
    for key, value in _curses.__dict__.items():
        if key[0:4] == 'ACS_' or key in ('LINES', 'COLS'):
            setattr(curses, key, value)

    return stdscr

更改“TERM”的原始来源在这里:

$ head plugins/org.python.pydev_4.0.0.201504132356/pysrc/pydev_ipython_console.py 
import sys
from pydev_console_utils import BaseInterpreterInterface

import os

os.environ['TERM'] = 'emacs' #to use proper page_more() for paging


# Uncomment to force PyDev standard shell.
# raise ImportError()

指出 pydevd 中 TERM 的硬编码设置的各种提示让我想到了这个序列,这对我有用:

#
# From the instructions in the PyCharm remote debug setup screen,
# add this to your code before curses is initialised.
#
import pydevd
pydevd.settrace('localhost', port=8899)
#
# And then to fixup the broken setting in pydevd...
#
os.environ['TERM'] = 'xterm'