Skip to content

Instantly share code, notes, and snippets.

@leorochael
Created December 17, 2012 22:07
Show Gist options
  • Save leorochael/4322770 to your computer and use it in GitHub Desktop.
Save leorochael/4322770 to your computer and use it in GitHub Desktop.
Keyword Library for the [Robot Framework](http://code.google.com/p/robotframework/) providing keywords `IPython` and `PDB Set Trace` keywords. Use it like this: *** Settings *** Library package.where.i.placed.robotlib.Support *** Test cases *** My Test Case [Some Steps...] IPython [Some Other Steps...] PDB Set Trace
'''
Robot library helpers for debugging
'''
import sys
import pdb
import IPython
class STDIOContextManager(object):
"""Saves and restores sys.std* variables from their sys.__std*__ backups"""
STDIO_NAMES = 'stdin stdout stderr'.split()
def __init__(self):
for name in self.STDIO_NAMES:
# save sys.stdin, etc...
setattr(self, name, getattr(sys, name))
# restore sys.__stdin__ back into sys.stdin, etc...
setattr(sys, name, getattr(sys, '__%s__' % name))
@classmethod
def __enter__(cls):
return cls()
def __exit__(self, ext_type, exc_value, exc_tb):
for name in self.STDIO_NAMES:
setattr(sys, name, getattr(self, name))
class Support(object):
'''Robot keywords'''
def ipython(self):
'''Opens IPython prompt into the console'''
with STDIOContextManager:
IPython.embed()
def pdb_set_trace(self):
'''Opens pdb prompt into the console'''
STDIOContextManager()
pdb.set_trace()
# don't exit the context manager, or we'll lose the pdb prompt even
# though it'll keep running
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment