python curses在setupterm中远程调试pdevd错误

时间:2015-05-08 01:16:13

标签: python debugging ncurses curses python-curses

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

我按照PyCharm 4.0.8指令,将其添加到" EXAMPLE.py"来自" npyscreen-4.8.7"。

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改变了环境&#34; TERM&#34;来自&#34; xterm&#34;到&#34; emacs&#34;。这可以通过一个小测试程序来验证。

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"
  • 如果使用&#34; pdevd&#34;删除这两行,程序会成功。 &#34; TERM&#34;打印出来的是&#34; xterm&#34;。
  • 如果将第一个参数更改为&#34; setupterm&#34;进入&#34; term='xterm'&#34;,即使使用&#34; pydevd&#34;它成功了。

我想问题是如何让#34; pydevd&#34;设置正确的&#34; TERM&#34;?

2 个答案:

答案 0 :(得分:1)

虽然其中一个答案可能是:改变/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()

答案 1 :(得分:1)

这个各种提示指出了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'