Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_pty.PtyTest Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def handle_sig
def test_basic
def test_fork

Public Attributes

 old_alarm

Detailed Description

Definition at line 50 of file test_pty.py.


Member Function Documentation

def test.test_pty.PtyTest.handle_sig (   self,
  sig,
  frame 
)

Definition at line 62 of file test_pty.py.

00062 
00063     def handle_sig(self, sig, frame):
00064         self.fail("isatty hung")

Here is the call graph for this function:

Definition at line 51 of file test_pty.py.

00051 
00052     def setUp(self):
00053         # isatty() and close() can hang on some platforms.  Set an alarm
00054         # before running the test to make sure we don't hang forever.
00055         self.old_alarm = signal.signal(signal.SIGALRM, self.handle_sig)
00056         signal.alarm(10)

Here is the caller graph for this function:

Definition at line 57 of file test_pty.py.

00057 
00058     def tearDown(self):
00059         # remove alarm, restore old alarm handler
00060         signal.alarm(0)
00061         signal.signal(signal.SIGALRM, self.old_alarm)

Here is the caller graph for this function:

Definition at line 65 of file test_pty.py.

00065 
00066     def test_basic(self):
00067         try:
00068             debug("Calling master_open()")
00069             master_fd, slave_name = pty.master_open()
00070             debug("Got master_fd '%d', slave_name '%s'" %
00071                   (master_fd, slave_name))
00072             debug("Calling slave_open(%r)" % (slave_name,))
00073             slave_fd = pty.slave_open(slave_name)
00074             debug("Got slave_fd '%d'" % slave_fd)
00075         except OSError:
00076             # " An optional feature could not be imported " ... ?
00077             raise unittest.SkipTest("Pseudo-terminals (seemingly) not functional.")
00078 
00079         self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty')
00080 
00081         # Solaris requires reading the fd before anything is returned.
00082         # My guess is that since we open and close the slave fd
00083         # in master_open(), we need to read the EOF.
00084 
00085         # Ensure the fd is non-blocking in case there's nothing to read.
00086         orig_flags = fcntl.fcntl(master_fd, fcntl.F_GETFL)
00087         fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags | os.O_NONBLOCK)
00088         try:
00089             s1 = os.read(master_fd, 1024)
00090             self.assertEqual(b'', s1)
00091         except OSError as e:
00092             if e.errno != errno.EAGAIN:
00093                 raise
00094         # Restore the original flags.
00095         fcntl.fcntl(master_fd, fcntl.F_SETFL, orig_flags)
00096 
00097         debug("Writing to slave_fd")
00098         os.write(slave_fd, TEST_STRING_1)
00099         s1 = os.read(master_fd, 1024)
00100         self.assertEqual(b'I wish to buy a fish license.\n',
00101                          normalize_output(s1))
00102 
00103         debug("Writing chunked output")
00104         os.write(slave_fd, TEST_STRING_2[:5])
00105         os.write(slave_fd, TEST_STRING_2[5:])
00106         s2 = os.read(master_fd, 1024)
00107         self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
00108 
00109         os.close(slave_fd)
00110         os.close(master_fd)
00111 

Here is the call graph for this function:

Definition at line 112 of file test_pty.py.

00112 
00113     def test_fork(self):
00114         debug("calling pty.fork()")
00115         pid, master_fd = pty.fork()
00116         if pid == pty.CHILD:
00117             # stdout should be connected to a tty.
00118             if not os.isatty(1):
00119                 debug("Child's fd 1 is not a tty?!")
00120                 os._exit(3)
00121 
00122             # After pty.fork(), the child should already be a session leader.
00123             # (on those systems that have that concept.)
00124             debug("In child, calling os.setsid()")
00125             try:
00126                 os.setsid()
00127             except OSError:
00128                 # Good, we already were session leader
00129                 debug("Good: OSError was raised.")
00130                 pass
00131             except AttributeError:
00132                 # Have pty, but not setsid()?
00133                 debug("No setsid() available?")
00134                 pass
00135             except:
00136                 # We don't want this error to propagate, escaping the call to
00137                 # os._exit() and causing very peculiar behavior in the calling
00138                 # regrtest.py !
00139                 # Note: could add traceback printing here.
00140                 debug("An unexpected error was raised.")
00141                 os._exit(1)
00142             else:
00143                 debug("os.setsid() succeeded! (bad!)")
00144                 os._exit(2)
00145             os._exit(4)
00146         else:
00147             debug("Waiting for child (%d) to finish." % pid)
00148             # In verbose mode, we have to consume the debug output from the
00149             # child or the child will block, causing this test to hang in the
00150             # parent's waitpid() call.  The child blocks after a
00151             # platform-dependent amount of data is written to its fd.  On
00152             # Linux 2.6, it's 4000 bytes and the child won't block, but on OS
00153             # X even the small writes in the child above will block it.  Also
00154             # on Linux, the read() will throw an OSError (input/output error)
00155             # when it tries to read past the end of the buffer but the child's
00156             # already exited, so catch and discard those exceptions.  It's not
00157             # worth checking for EIO.
00158             while True:
00159                 try:
00160                     data = os.read(master_fd, 80)
00161                 except OSError:
00162                     break
00163                 if not data:
00164                     break
00165                 sys.stdout.write(str(data.replace(b'\r\n', b'\n'),
00166                                      encoding='ascii'))
00167 
00168             ##line = os.read(master_fd, 80)
00169             ##lines = line.replace('\r\n', '\n').split('\n')
00170             ##if False and lines != ['In child, calling os.setsid()',
00171             ##             'Good: OSError was raised.', '']:
00172             ##    raise TestFailed("Unexpected output from child: %r" % line)
00173 
00174             (pid, status) = os.waitpid(pid, 0)
00175             res = status >> 8
00176             debug("Child (%d) exited with status %d (%d)." % (pid, res, status))
00177             if res == 1:
00178                 self.fail("Child raised an unexpected exception in os.setsid()")
00179             elif res == 2:
00180                 self.fail("pty.fork() failed to make child a session leader.")
00181             elif res == 3:
00182                 self.fail("Child spawned by pty.fork() did not have a tty as stdout")
00183             elif res != 4:
00184                 self.fail("pty.fork() failed for unknown reasons.")
00185 
00186             ##debug("Reading from master_fd now that the child has exited")
00187             ##try:
00188             ##    s1 = os.read(master_fd, 1024)
00189             ##except os.error:
00190             ##    pass
00191             ##else:
00192             ##    raise TestFailed("Read from master_fd did not raise exception")
00193 
00194         os.close(master_fd)
00195 
00196         # pty.fork() passed.

Here is the call graph for this function:


Member Data Documentation

Definition at line 54 of file test_pty.py.


The documentation for this class was generated from the following file: