old_contents: # -*- coding: utf-8 -*-
  import curses
  import pdb
  import sys
  
  
  screen = None
  initialized = False
  
  
  def init(stdscr):
      global screen
      screen = stdscr
      global initialized
      initialized = True
  
  
  def finalize(stdscr):
      curses.nocbreak()
      stdscr.keypad(0)
      curses.echo()
      curses.endwin()
  
  
  def debug(stdscr=screen):
      if not initialized:
          raise Exception('cursutils must be initialized first')
      if not stdscr:
          raise Exception('stdscr must be a valid window object')
      finalize(stdscr)
  
      debugger = pdb.Pdb()
      debugger.reset()
      debugger.do_where(None)
      users_frame = sys._getframe().f_back  # One frame up, outside this function
      debugger.interaction(users_frame, None)
  
  
  # Use with:
  # import cursutils
  # cursutils.init(stdscr)   # where stdscr is a curses window object
  # cursutils.debug(cursutils.screen)
  
new_contents: # -*- coding: utf-8 -*-
  import curses
  import pdb
  import sys
  
  
  screen = None
  
  
  def init(stdscr):
      global screen
      screen = stdscr
  
  
  def finalize(stdscr=None):
      if not stdscr and not screen:
          raise Exception('either call init() first or provide a valid window object')
      stdscr = screen if screen and not stdscr else stdscr
      curses.nocbreak()
      stdscr.keypad(0)
      curses.echo()
      curses.endwin()
  
  
  def debug(stdscr=None):
      if not stdscr and not screen:
          raise Exception('either call init() first or provide a valid window object')
      stdscr = screen if screen and not stdscr else stdscr
      finalize(stdscr)
  
      debugger = pdb.Pdb()
      debugger.reset()
      debugger.do_where(None)
      users_frame = sys._getframe().f_back  # One frame up, outside this function
      debugger.interaction(users_frame, None)
  
  
  # Use with:
  # import cursutils
  # cursutils.init(stdscr)   # where stdscr is a `curses` Window object
  # cursutils.debug()
  
subject: simplify cursutils initialization