Search code examples
pythondebuggingncursescursespython-curses

python curses remote debugging pdevd error in setupterm


Is it possible to debug a curse program remotely in PyCharm? How to set it up?

I followed the PyCharm 4.0.8 instruction, added this to the "EXAMPLE.py" from "npyscreen-4.8.7".

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

And always it runs into an error in "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

The problem is that pydevd changed environment "TERM" from "xterm" to "emacs". This can be verified by a little test program.

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"
  • If removing the two lines with "pdevd", the program succeeds. The "TERM" it prints out is "xterm".
  • If change the 1st argument to "setupterm" into "term='xterm'", even with "pydevd" it succeeds.

I guess the question is how to have "pydevd" setup the correct "TERM"?


Solution

  • This various hints pointing out the hardcoded setting of the TERM in pydevd led me to this sequence, which works for me:

    #
    # 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'