test_pty.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Lib » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Lib » test » test_pty.py
import pty, os, sys
from test_support import verbose,TestFailed,TestSkipped

TEST_STRING_1 = "I wish to buy a fish license.\n"
TEST_STRING_2 = "For my pet fish, Eric.\n"

if verbose:
    def debug(msg):
        print msg
else:
    def debug(msg):
        pass

# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
# because pty code is not too portable.

try:
    debug("Calling master_open()")
    master_fd, slave_name = pty.master_open()
    debug("Got master_fd '%d', slave_name '%s'"%(master_fd, slave_name))
    debug("Calling slave_open(%s)"%`slave_name`)
    slave_fd = pty.slave_open(slave_name)
    debug("Got slave_fd '%d'"%slave_fd)
except OSError:
    # " An optional feature could not be imported " ... ?
    raise TestSkipped, "Pseudo-terminals (seemingly) not functional."

if not os.isatty(slave_fd):
    raise TestFailed, "slave_fd is not a tty"

# IRIX apparently turns \n into \r\n. Allow that, but avoid allowing other
# differences (like extra whitespace, trailing garbage, etc.)

debug("Writing to slave_fd")
os.write(slave_fd, TEST_STRING_1)
s1 = os.read(master_fd, 1024)
sys.stdout.write(s1.replace("\r\n", "\n"))

debug("Writing chunked output")
os.write(slave_fd, TEST_STRING_2[:5])
os.write(slave_fd, TEST_STRING_2[5:])
s2 = os.read(master_fd, 1024)
sys.stdout.write(s2.replace("\r\n", "\n"))

os.close(slave_fd)
os.close(master_fd)

# basic pty passed.

debug("calling pty.fork()")
pid, master_fd = pty.fork()
if pid == pty.CHILD:
    # stdout should be connected to a tty.
    if not os.isatty(1):
        debug("Child's fd 1 is not a tty?!")
        os._exit(3)

    # After pty.fork(), the child should already be a session leader.
    # (on those systems that have that concept.)
    debug("In child, calling os.setsid()")
    try:
        os.setsid()
    except OSError:
        # Good, we already were session leader
        debug("Good: OSError was raised.")
        pass
    except AttributeError:
        # Have pty, but not setsid() ?
        debug("No setsid() available ?")
        pass
    except:
        # We don't want this error to propagate, escaping the call to
        # os._exit() and causing very peculiar behavior in the calling
        # regrtest.py !
        # Note: could add traceback printing here.
        debug("An unexpected error was raised.")
        os._exit(1)
    else:
        debug("os.setsid() succeeded! (bad!)")
        os._exit(2)
    os._exit(4)
else:
    debug("Waiting for child (%d) to finish."%pid)
    (pid, status) = os.waitpid(pid, 0)
    res = status / 256
    debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
    if res == 1:
        raise TestFailed, "Child raised an unexpected exception in os.setsid()"
    elif res == 2:
        raise TestFailed, "pty.fork() failed to make child a session leader."
    elif res == 3:
        raise TestFailed, "Child spawned by pty.fork() did not have a tty as stdout"
    elif res != 4:
        raise TestFailed, "pty.fork() failed for unknown reasons."

os.close(master_fd)

# pty.fork() passed.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.