Make pysawsim.invoke.CommandError picklable, and add doctest proof.
authorW. Trevor King <wking@drexel.edu>
Sun, 24 Oct 2010 14:04:24 +0000 (10:04 -0400)
committerW. Trevor King <wking@drexel.edu>
Sun, 24 Oct 2010 14:04:24 +0000 (10:04 -0400)
pysawsim/invoke.py

index bb051d7ad8c36e17090d188dc2763800401a0915..165701c3f84d0ad7ad5a45af2a7655219b9829da 100644 (file)
@@ -25,14 +25,40 @@ import sys
 
 
 class CommandError(Exception):
-    def __init__(self, command, status, stdout, stderr):
-        strerror = ["Command failed (%d):\n  %s\n" % (status, stderr),
-                    "while executing\n  %s" % command]
-        Exception.__init__(self, "\n".join(strerror))
+    """Represent errors in command execution.
+
+    Instances are picklable (for passing through `multiprocessing.Queue`\s).
+
+    >>> import pickle
+    >>> a = CommandError('somefunc', 1, '', 'could not find "somefunc"')
+    >>> x = pickle.dumps(a)
+    >>> b = pickle.loads(x)
+    >>> print b
+    Command failed (1):
+      could not find "somefunc"
+    <BLANKLINE>
+    while executing
+      somefunc
+    """
+    def __init__(self, command=None, status=None, stdout=None, stderr=None):
         self.command = command
         self.status = status
         self.stdout = stdout
         self.stderr = stderr
+        Exception.__init__(self, self.__str__())
+
+    def __getstate__(self):
+        return self.__dict__
+
+    def __setstate__(self, data):
+        self.__dict__.update(data)
+
+    def __str__(self):
+        return "\n".join([
+                "Command failed (%s):\n  %s\n" % (self.status, self.stderr),
+                "while executing\n  %s" % self.command,
+                ])
+
 
 def invoke(cmd_string, stdin=None, expect=(0,), cwd=None, verbose=False):
     """