[ create a new paste ] login | about

Link: http://codepad.org/JUkKghqq    [ raw code | output | fork ]

parasporospa - Python, pasted on Jan 26:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Python 版 rlwrap もどき
どうもうまくいかない
・最初の1文字がエコーしてしまう
"""
from logging import *
import atexit
import os
import pty
import readline
import select
import signal
import sys
import tty

    
def sigchld_handler(*args):
    sys.exit(0)
    
def cleanup():
    global old_attr
    tty.tcsetattr(sys.stdin.fileno(), tty.TCSANOW, old_attr)

attr = tty.tcgetattr(sys.stdin.fileno())
old_attr = attr[:]
    
def main():
    basicConfig(level=INFO, format='%(levelname)s: %(message)s')
        
    if len(sys.argv) <= 1:
        print "Usage: rlwrap.py program [args...]"
        return
        
    (pid, fd) = pty.fork()
        
    if pid < 0: 
        error("fork failed")
    elif pid == 0:
        tty.setraw(sys.stdin.fileno())   # raw mode
        os.execvp(sys.argv[1], sys.argv[1:])
    else:
        signal.signal(signal.SIGCHLD, sigchld_handler)
        
        attr[tty.IFLAG] = attr[tty.IFLAG] & ~(tty.BRKINT | tty.ICRNL | tty.INPCK | tty.ISTRIP | tty.IXON)
        #attr[tty.OFLAG] = attr[tty.OFLAG] & ~(tty.OPOST)
        attr[tty.CFLAG] = attr[tty.CFLAG] & ~(tty.CSIZE | tty.PARENB)
        attr[tty.CFLAG] = attr[tty.CFLAG] | tty.CS8
        # ECHO はオン
        attr[tty.LFLAG] = attr[tty.LFLAG] & ~(tty.ICANON | tty.IEXTEN | tty.ISIG)
        attr[tty.CC][tty.VMIN] = 1
        attr[tty.CC][tty.VTIME] = 0
        tty.tcsetattr(sys.stdin.fileno(), tty.TCSANOW, attr)
        while True:
            ready = select.select([sys.stdin.fileno(), fd], [], [])
            if sys.stdin.fileno() in ready[0]:
                try:
                    from_stdin = raw_input("")
                    os.write(fd, from_stdin + "\r")
                except EOFError: 
                    break
            elif fd in ready[0]:
                from_pty = os.read(fd, 1024)
                os.write(sys.stdout.fileno(), from_pty)


Output:
1
2
3
4
Traceback (most recent call last):
  Line 12, in <module>
    import readline
ImportError: libreadline.so.5: cannot open shared object file: No such file or directory


Create a new paste based on this one


Comments: