[Python-modules-commits] [python-spur] 02/04: Raise CommandInitializationError when reading int fails

Ruben Undheim rubund-guest at moszumanska.debian.org
Mon May 25 11:21:24 UTC 2015


This is an automated email from the git hooks/post-receive script.

rubund-guest pushed a commit to branch upstream
in repository python-spur.

commit 3598ccd29514e822deefaa8ccb7e53cc951b89b2
Author: Michael Williamson <mike at zwobble.org>
Date:   Sun May 3 14:04:43 2015 +0100

    Raise CommandInitializationError when reading int fails
---
 CHANGES            | 5 +++++
 spur/__init__.py   | 5 +++--
 spur/errors.py     | 8 ++++++++
 spur/ssh.py        | 5 ++++-
 tests/ssh_tests.py | 6 +++++-
 5 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/CHANGES b/CHANGES
index 964b1ab..f91a447 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,5 +1,10 @@
 # CHANGES
 
+## 0.3.14
+
+* Raise spur.CommandInitializationError when SshShell fails to read integers
+  from stdout before the command is actually run.
+
 ## 0.3.13
 
 * Add look_for_private_keys argument to SshShell to allow searching for private
diff --git a/spur/__init__.py b/spur/__init__.py
index 2c8f5fa..a48a9ac 100644
--- a/spur/__init__.py
+++ b/spur/__init__.py
@@ -1,6 +1,7 @@
 from spur.local import LocalShell
 from spur.ssh import SshShell
 from spur.results import RunProcessError
-from spur.errors import NoSuchCommandError
+from spur.errors import NoSuchCommandError, CommandInitializationError
 
-__all__ = ["LocalShell", "SshShell", "RunProcessError", "NoSuchCommandError"]
+__all__ = ["LocalShell", "SshShell", "RunProcessError", "NoSuchCommandError",
+    "CommandInitializationError"]
diff --git a/spur/errors.py b/spur/errors.py
index 06b5cb7..430e1c7 100644
--- a/spur/errors.py
+++ b/spur/errors.py
@@ -6,3 +6,11 @@ class NoSuchCommandError(OSError):
             message = "Command not found: {0}. Check that {0} is installed and on $PATH".format(command)
         super(type(self), self).__init__(message)
         self.command = command
+
+
+class CommandInitializationError(Exception):
+    def __init__(self, line):
+        super(type(self), self).__init__(
+"""Error while initializing command. The most likely cause is an unsupported shell. Try using a minimal shell type when calling 'spawn' or 'run'.
+(Failed to parse line '{0}' as integer)""".format(line)
+        )
diff --git a/spur/ssh.py b/spur/ssh.py
index 4d07fd0..49f3efe 100644
--- a/spur/ssh.py
+++ b/spur/ssh.py
@@ -288,7 +288,10 @@ def _read_int_initialization_line(output_file):
     while True:
         line = output_file.readline().strip()
         if line:
-            return int(line)
+            try:
+                return int(line)
+            except ValueError:
+                raise spur.errors.CommandInitializationError(line)
 
 
 class SftpFile(object):
diff --git a/tests/ssh_tests.py b/tests/ssh_tests.py
index 9ae252b..f5bf097 100644
--- a/tests/ssh_tests.py
+++ b/tests/ssh_tests.py
@@ -167,4 +167,8 @@ class ReadInitializationLineTests(object):
         
     @istest
     def error_if_non_blank_line_is_not_integer(self):
-        assert_raises(ValueError, lambda: spur.ssh._read_int_initialization_line(io.StringIO("x\n")))
+        try:
+            spur.ssh._read_int_initialization_line(io.StringIO("x\n"))
+            assert False, "Expected error"
+        except spur.CommandInitializationError as error:
+            assert "Failed to parse line 'x' as integer" in str(error)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-spur.git



More information about the Python-modules-commits mailing list