Example #1
0
    def global_logging_raw_input(prompt):
        def get_stream(name):
            if hasattr(sys, name):
                stream = getattr(sys, name)
                if isinstance(stream, file):
                    return stream
                elif isinstance(stream, StreamTeeLogger):
                    return stream.stream
            if hasattr(sys, '__%s__' % name):
                stream = getattr(sys, '__%s__' % name)
                if isinstance(stream, file):
                    return stream
            return None

        def check_interactive(stream, name):
            try:
                fd = stream.fileno()
            except:
                # Could be an AttributeError, an OSError, and IOError, or who
                # knows what else...
                return False

            realfd = {'stdin': 0, 'stdout': 1, 'stderr': 2}[name]

            return fd == realfd and os.isatty(fd)


        stdout = get_stream('stdout')
        stdin = get_stream('stdin')
        stderr = get_stream('stderr')

        if stdout is None:
            raise RuntimeError('raw_input(): lost sys.stdout')
        if stdin is None:
            raise RuntimeError('raw_input(): lost sys.stdin')
        if stderr is None:
            raise RuntimeError('raw_input(): lost sys.stderr')

        if (not check_interactive(stdin, 'stdin') or
            not check_interactive(stdout, 'stdout')):
            # Use the built-in raw_input(); this will repeat some of the checks
            # we just did, but will save us from having to reimplement
            # raw_input() in its entirety
            retval = builtins._original_raw_input(prompt)
        else:
            stdout.flush()
            infd = pythonapi.PyFile_AsFile(stdin)
            outfd = pythonapi.PyFile_AsFile(stdout)
            retval = pythonapi.PyOS_Readline(infd, outfd, str(prompt))
            retval = retval.rstrip('\n')

        if isinstance(sys.stdout, StreamTeeLogger):
            sys.stdout.log_orig(str(prompt) + retval, echo=False)

        return retval
Example #2
0
    def global_logging_raw_input(prompt):
        def get_stream(name):
            if hasattr(sys, name):
                stream = getattr(sys, name)
                if isinstance(stream, file):
                    return stream
                elif isinstance(stream, StreamTeeLogger):
                    return stream.stream
            if hasattr(sys, '__%s__' % name):
                stream = getattr(sys, '__%s__' % name)
                if isinstance(stream, file):
                    return stream
            return None

        def check_interactive(stream, name):
            try:
                fd = stream.fileno()
            except:
                # Could be an AttributeError, an OSError, and IOError, or who
                # knows what else...
                return False

            realfd = {'stdin': 0, 'stdout': 1, 'stderr': 2}[name]

            return fd == realfd and os.isatty(fd)

        stdout = get_stream('stdout')
        stdin = get_stream('stdin')
        stderr = get_stream('stderr')

        if stdout is None:
            raise RuntimeError('raw_input(): lost sys.stdout')
        if stdin is None:
            raise RuntimeError('raw_input(): lost sys.stdin')
        if stderr is None:
            raise RuntimeError('raw_input(): lost sys.stderr')

        if (not check_interactive(stdin, 'stdin')
                or not check_interactive(stdout, 'stdout')):
            # Use the built-in raw_input(); this will repeat some of the checks
            # we just did, but will save us from having to reimplement
            # raw_input() in its entirety
            retval = builtins._original_raw_input(prompt)
        else:
            stdout.flush()
            infd = pythonapi.PyFile_AsFile(stdin)
            outfd = pythonapi.PyFile_AsFile(stdout)
            retval = pythonapi.PyOS_Readline(infd, outfd, str(prompt))
            retval = retval.rstrip('\n')

        if isinstance(sys.stdout, StreamTeeLogger):
            sys.stdout.log_orig(str(prompt) + retval, echo=False)

        return retval
Example #3
0
 def global_logging_raw_input(prompt):
     retval = builtins._original_raw_input(prompt)
     if isinstance(sys.stdout, StreamTeeLogger):
         sys.stdout.log_orig(str(prompt) + retval, echo=False)
     return retval
Example #4
0
 def global_logging_raw_input(prompt):
     retval = builtins._original_raw_input(prompt)
     if isinstance(sys.stdout, StreamTeeLogger):
         sys.stdout.log_orig(str(prompt) + retval, echo=False)
     return retval