Skip to content

Commit

Permalink
Failure in __str__() before any output.
Browse files Browse the repository at this point in the history
When calling str() on a spawn class object before
it has any command output, the __str__() override
attempts to truncate long command output while the
value of self.before is still None, raising:

    TypeError: 'NoneType' object has no attribute '__getitem__'
  • Loading branch information
jquast committed Nov 22, 2014
1 parent 184f9c4 commit 03fe7b5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pexpect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,10 @@ def __str__(self):
s.append('command: ' + str(self.command))
s.append('args: %r' % (self.args,))
s.append('searcher: %r' % (self.searcher,))
s.append('buffer (last 100 chars): %r' % (self.buffer)[-100:],)
s.append('before (last 100 chars): %r' % (self.before)[-100:],)
s.append('buffer (last 100 chars): %r' % (
self.buffer and (self.buffer)[-100:] or self.buffer,))
s.append('before (last 100 chars): %r' % (
self.before and (self.before)[-100:] or self.before,))
s.append('after: %r' % (self.after,))
s.append('match: %r' % (self.match,))
s.append('match_index: ' + str(self.match_index))
Expand Down
25 changes: 25 additions & 0 deletions tests/test_repr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
""" Test __str__ methods. """
import pexpect

from . import PexpectTestCase

class TestCaseMisc(PexpectTestCase.PexpectTestCase):

def test_str_spawnu(self):
""" Exercise spawnu.__str__() """
# given,
p = pexpect.spawnu('cat')
# exercise,
value = p.__str__()
# verify
assert isinstance(value, basestring)

def test_str_spawn(self):
""" Exercise spawn.__str__() """
# given,
p = pexpect.spawn('cat')
# exercise,
value = p.__str__()
# verify
assert isinstance(value, basestring)

0 comments on commit 03fe7b5

Please sign in to comment.