Skip to content

Instantly share code, notes, and snippets.

@csabahenk
Last active December 23, 2015 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csabahenk/6608268 to your computer and use it in GitHub Desktop.
Save csabahenk/6608268 to your computer and use it in GitHub Desktop.
Swift and Pdb

Using Pdb with Swift does not work because Swift redirects stdin (even in foreground mode). Here we describe how to get it work.

  • apply attached patch on Swift source

  • wherever you'd call pdb.set_strace() do it like this:

from swift.common.utils import stdio_restore import pdb stdio_restore.raw() pdb.set_trace()


or, for your convenience, as one liner ;) :

```python
from swift.common.utils import stdio_restore; import pdb; stdio_restore.raw(); pdb.set_trace()
  • start Swift in foreground:

swift-init main start -n

diff --git a/swift/common/utils.py b/swift/common/utils.py
index 3c8c367..0291dac 100644
--- a/swift/common/utils.py
+++ b/swift/common/utils.py
@@ -1024,6 +1024,45 @@ def drop_privileges(user):
os.chdir('/') # in case you need to rmdir on where you started the daemon
os.umask(0o22) # ensure files are created with the correct privileges
+class _stdio_restore(object):
+
+ tty = os.environ.get("TTY")
+ if tty:
+ _fd = os.open(tty, os.O_RDWR)
+ else:
+ _fd = 2
+ if os.isatty(_fd):
+ enabled = True
+ else:
+ enabled = False
+ alt = [os.fdopen(_fd, 'r'), os.fdopen(_fd, 'w')]
+
+ def __init__(self):
+ self.store = [None, None]
+
+ def raw(self):
+ if not self.store[0]:
+ self.store[0], sys.stdin = sys.stdin, self.alt[0]
+ self.store[1], sys.stdout = sys.stdout, self.alt[1]
+ return True
+
+ def cook(self):
+ if self.store[0]:
+ self.store[0], sys.stdin = None, self.store[0]
+ self.store[1], sys.stdout = None, self.store[1]
+ return True
+
+ def switch(self):
+ self.raw() or self.cook()
+
+ __enter__ = raw
+
+ def __exit__(self, *a, **kw):
+ self.cook()
+
+if _stdio_restore.enabled:
+ stdio_restore = _stdio_restore()
+
def capture_stdio(logger, **kwargs):
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment