Skip to content

back to OpenHands summary

OpenHands: pexpect

Failed to run pytests for test tests

Unknown failure.

Patch diff

diff --git a/pexpect/FSM.py b/pexpect/FSM.py
index 50d12f3..5066dad 100644
--- a/pexpect/FSM.py
+++ b/pexpect/FSM.py
@@ -114,7 +114,8 @@ class FSM:
         """This sets the current_state to the initial_state and sets
         input_symbol to None. The initial state was set by the constructor
         __init__(). """
-        pass
+        self.current_state = self.initial_state
+        self.input_symbol = None

     def add_transition(self, input_symbol, state, action=None, next_state=None):
         """This adds a transition that associates:
@@ -127,7 +128,9 @@ class FSM:

         You can also set transitions for a list of symbols by using
         add_transition_list(). """
-        pass
+        if next_state is None:
+            next_state = state
+        self.state_transitions[(input_symbol, state)] = (action, next_state)

     def add_transition_list(self, list_input_symbols, state, action=None, next_state=None):
         """This adds the same transition for a list of input symbols.
@@ -138,7 +141,8 @@ class FSM:
         The action may be set to None in which case the process() method will
         ignore the action and only set the next_state. The next_state may be
         set to None in which case the current state will be unchanged. """
-        pass
+        for input_symbol in list_input_symbols:
+            self.add_transition(input_symbol, state, action, next_state)

     def add_transition_any(self, state, action=None, next_state=None):
         """This adds a transition that associates:
@@ -152,7 +156,9 @@ class FSM:
         The action may be set to None in which case the process() method will
         ignore the action and only set the next_state. The next_state may be
         set to None in which case the current state will be unchanged. """
-        pass
+        if next_state is None:
+            next_state = state
+        self.state_transitions_any[state] = (action, next_state)

     def set_default_transition(self, action, next_state):
         """This sets the default transition. This defines an action and
@@ -163,7 +169,7 @@ class FSM:

         The default transition can be removed by setting the attribute
         default_transition to None. """
-        pass
+        self.default_transition = (action, next_state)

     def get_transition(self, input_symbol, state):
         """This returns (action, next state) given an input_symbol and state.
@@ -186,7 +192,15 @@ class FSM:

         4. No transition was defined. If we get here then raise an exception.
         """
-        pass
+        if (input_symbol, state) in self.state_transitions:
+            return self.state_transitions[(input_symbol, state)]
+        elif state in self.state_transitions_any:
+            return self.state_transitions_any[state]
+        elif self.default_transition is not None:
+            return self.default_transition
+        else:
+            raise ExceptionFSM('Transition is undefined: (%s, %s).' %
+                             (str(input_symbol), str(state)))

     def process(self, input_symbol):
         """This is the main method that you call to process input. This may
@@ -196,12 +210,18 @@ class FSM:
         is not called and only the current state is changed. This method
         processes one complete input symbol. You can process a list of symbols
         (or a string) by calling process_list(). """
-        pass
+        self.input_symbol = input_symbol
+        (self.action, self.next_state) = self.get_transition(self.input_symbol, self.current_state)
+        if self.action is not None:
+            self.action(self)
+        self.current_state = self.next_state
+        self.next_state = None

     def process_list(self, input_symbols):
         """This takes a list and sends each element to process(). The list may
         be a string or any iterable object. """
-        pass
+        for s in input_symbols:
+            self.process(s)
 import sys
 import string
 PY3 = sys.version_info[0] >= 3
@@ -210,6 +230,44 @@ def main():
     """This is where the example starts and the FSM state transitions are
     defined. Note that states are strings (such as 'INIT'). This is not
     necessary, but it makes the example easier to read. """
-    pass
+    memory = []
+    fsm = FSM('INIT', memory)
+
+    def number_input(fsm):
+        fsm.memory.append(int(fsm.input_symbol))
+
+    def plus(fsm):
+        fsm.memory.append(fsm.memory.pop() + fsm.memory.pop())
+
+    def minus(fsm):
+        arg2 = fsm.memory.pop()
+        arg1 = fsm.memory.pop()
+        fsm.memory.append(arg1 - arg2)
+
+    def multiply(fsm):
+        fsm.memory.append(fsm.memory.pop() * fsm.memory.pop())
+
+    def divide(fsm):
+        arg2 = fsm.memory.pop()
+        arg1 = fsm.memory.pop()
+        fsm.memory.append(arg1 / arg2)
+
+    def equals(fsm):
+        print(fsm.memory.pop())
+
+    fsm.add_transition_list(string.digits, 'INIT', number_input, 'INIT')
+    fsm.add_transition('+', 'INIT', plus, 'INIT')
+    fsm.add_transition('-', 'INIT', minus, 'INIT')
+    fsm.add_transition('*', 'INIT', multiply, 'INIT')
+    fsm.add_transition('/', 'INIT', divide, 'INIT')
+    fsm.add_transition('=', 'INIT', equals, 'INIT')
+    fsm.add_transition(' ', 'INIT', None, 'INIT')
+
+    try:
+        if PY3:
+            input_name = 'input'
+        else:
+            input_name = 'raw_input'
+        fsm.process_list(getattr(__builtins__, input_name)('Enter RPN Test (space delimited): '))
 if __name__ == '__main__':
     main()
\ No newline at end of file
diff --git a/pexpect/spawnbase.py b/pexpect/spawnbase.py
index 6f22be6..0090a42 100644
--- a/pexpect/spawnbase.py
+++ b/pexpect/spawnbase.py
@@ -87,6 +87,12 @@ class SpawnBase(object):
         self.async_pw_transport = None
         self._buffer = self.buffer_type()
         self._before = self.buffer_type()
+    def _get_buffer(self):
+        return self._buffer
+
+    def _set_buffer(self, value):
+        self._buffer = value
+
     buffer = property(_get_buffer, _set_buffer)

     def read_nonblocking(self, size=1, timeout=None):