0

Im writing a Python curses program using vi editor. Would like to understand how to debug the module to debug it?

Is there any tools that I can use?

4
  • 2
    There's PDB, the Python debugger...
    – MattDMo
    Jan 21, 2014 at 19:51
  • 3
    Well, once you understand how to debug the module, you will understand how to debug the module to debug it. Proof by induction. :)
    – abarnert
    Jan 21, 2014 at 19:53
  • 2
    Anyway, you can debug it in any of the same ways you can debug any other Python program—run it in a debugger, insert logging statements, extract parts of the program to unit test, etc.—except for simple print.
    – abarnert
    Jan 21, 2014 at 19:54
  • @MattDMo pdb doesn't work with curses, because curses changes the stdin reference
    – Nisanio
    Nov 9, 2023 at 12:53

1 Answer 1

3

To easily debug a curses program, you can use curses.wrapper like this:

import curses
def make_me_an_error(screen, numerator, denominator):
    screen.addstr(0, 0, str(numerator/denominator)) # divide by zero

curses.wrapper(make_me_an_error, 1, 0)

Upon return or exception from make_me_an_error, wrapper automatically cleans up the screen and restores it to its original settings

From wrapper docs:

Initialize curses and call another callable object, func, which should be the rest of your curses-using application. If the application raises an exception, this function will restore the terminal to a sane state before re-raising the exception and generating a traceback. The callable object func is then passed the main window ‘stdscr’ as its first argument, followed by any other arguments passed to wrapper(). Before calling func, wrapper() turns on cbreak mode, turns off echo, enables the terminal keypad, and initializes colors if the terminal has color support. On exit (whether normally or by exception) it restores cooked mode, turns on echo, and disables the terminal keypad.

Not the answer you're looking for? Browse other questions tagged or ask your own question.