4

On a Windows machine, I'm trying to call an external executable from Python and gather its outputs for further processing. Because a local path variable has to be set before calling the executable, I created a batch script that

  • first calls another script to set %PATH% and
  • then calls the executable with the parameters given to it.

The *.bat file looks like this:

@echo off
call set_path.bat
@echo on
executable.exe %*

And the Python code like this:

print("before call");
result = subprocess.check_output([batfile, parameters], stderr=subprocess.STDOUT, shell=True);
print("after call");

print("------- ------- ------- printing result ------- ------- ------- ");
print(result);
print("------- ------- ------- /printing result ------- ------- ------- ");

Now, technically, this works. The executable is called with the intended parameters, runs, finishes and produces results. I know this, because they are mockingly displayed in the very console in which the Python script is running.

However, the result string only contains what the batch script returns, not the executables outputs:

before call

hello? yes, this is executable.exe

after call

------- ------- ------- printing result ------- ------- -------

C:\Users\me\Documents\pythonscript\execute\executable.exe "para1|para2|para3"

------- ------- ------- /printing result ------- ------- -------

The subprocess.check_output command itself somehow prints the intended output to the console, what it returns only contains the batch file's outputs after @echo is on again.

How can I access and save the executable's output to a string for further work?

Or do I have to somehow modify the batch file to catch and print the output, so that it will end upt in check_output's results? If so, how could I go about doing that?

buggy
  • 215
  • 3
  • 11
  • Check [this](http://stackoverflow.com/a/18244152/524743) for multiline output – Samuel Aug 01 '16 at 12:09
  • If you run `executable > nul`, does it still output to the console? If so, it's ignoring the standard handles and directly opening `CONOUT$`. – Eryk Sun Aug 01 '16 at 12:34
  • @Samuel Thanks, but the problem is not that the output consists of multiple lines, but that it is not what it should be . – buggy Aug 01 '16 at 13:20
  • @eryksun I'm not sure, if I understand you correctly. I added "> nul" to the last line of the .bat file, so that it now reads: "executable.exe %* > nul". This did not change anything about the described scenario, aside from the result string now being "C:\Users\me\Documents\pythonscript\execute\executable.exe "para1|para2|para3" 1>nul". – buggy Aug 01 '16 at 13:22
  • @buggy if you just run your batch file from command line. Will it give the output of executable.exe – Samuel Aug 01 '16 at 13:25
  • @Samuel Yes, if I call the batch file from cmd it first prints the line that subprocess.check_output returns, too (call of the executable with parameters), then the executable's own output line (greeting string). – buggy Aug 01 '16 at 13:29
  • I guess [this](http://stackoverflow.com/a/11495784/524743) may be an answer since looks like your exe prints to `stderr` – Samuel Aug 01 '16 at 13:41
  • Thanks, I did not know that about `subprocess.STDOUT`. However, replacing `stderr=subprocess.STDOUT`with the proposed `stderr=sys.stdout.fileno()` does not change anything about the described situation. – buggy Aug 01 '16 at 13:56
  • A Windows process has 3 standard handles -- `StandardInput`, `StandardOutput`, and `StandardError`. These are generally set to `File` handles, which can be for a pipe, a disk file, a console device, or some other device such as the `NUL` device. In a process that uses a C runtime, these handles are mapped to standard POSIX file descriptors `stdin` (0), `stdout` (1), and `stderr` (2). `subprocess.check_output` redirects the child's `stdout` to a pipe. Your call also directs `stderr` to the same pipe. `check_output` calls `communicate` to read the pipe and wait for the process to exit. – Eryk Sun Aug 01 '16 at 20:23
  • However, execute.exe doesn't necessarily have to write its output to the `StandardOutput` and `StandardError` handles. If it has an attached console it can open the `\\.\CONOUT$` device to write directly to the console. That's why I wanted you to test running executable.exe directly in the command prompt (not in the batch file) with its `stdout` redirected to the `NUL` device. You can also redirect stderr, e.g. `executable > nul 2>&1`. If the program is ignoring `stdout` and `stderr`, then you should still see its output in the console. – Eryk Sun Aug 01 '16 at 20:27
  • Since `NUL` is a character device that may not reproduce the problem if the program is depending on the C runtime's naive (broken as designed, IMO) `isatty` function. So try it with a pipe as well, e.g. `executable 2>&1 | more`. – Eryk Sun Aug 01 '16 at 20:31
  • @eryksun Thanks for explaining! It seems the executable does ignore the standard handles, as calling it manually via console and `> nul`, `> nul 2>&1`or `2>&1 | more` indeed always results in the same output. – buggy Aug 02 '16 at 10:08
  • So, I guess, the question now is, how to access the output on `CONOUT$` via Python? – buggy Aug 02 '16 at 10:08
  • How much data are we talking about? Assuming the current process is attached to the console in which executable.exe will run, it's possible to create and temporarily activate a new (empty) screen buffer with 9999 lines. The child process will open this as its `\\.\CONOUT$`. Then after the child exits you can call `ReadConsoleOutputCharacter` to read the contents of the buffer up to the current cursor position. It's relatively simple if we don't have to worry about greater than 9999 lines causing data to be scrolled out of the buffer. – Eryk Sun Aug 02 '16 at 15:40
  • The output never exceeds some hundred characters, so that would work just fine. In fact, it sounds great. How would I go about using that new screen buffer? I found the `CreateConsoleScreenBuffer` function in the Windows Dev Center about consoles but don't really know how and where to use it, as apparently we are entering C++ country now? – buggy Aug 03 '16 at 09:39
  • No, we're entering ctypes or PyWin32 territory to use the Windows API in Python. I can provide an example using ctypes. – Eryk Sun Aug 03 '16 at 12:22
  • That would be great. So as far as I understand, via ctypes I can access functions in C based Windows DLLs in order to exert more control over the console? – buggy Aug 03 '16 at 14:01

1 Answers1

3

If a program writes directly to the console (e.g. by opening the CONOUT$ device) instead of to the process standard handles, the only option is to read the console screen buffer directly. To make this simpler, start with a new, empty screen buffer. Create, size, initialize, and activate a new screen buffer via the following functions:

Make sure to request GENERIC_READ | GENERIC_WRITE access when calling CreateConsoleScreenBuffer. You'll need read access later in order to read the contents of the screen.

Specifically for Python, use ctypes to call functions in the Windows console API. Also, if you wrap the handle with a C file descriptor via msvcrt.open_osfhandle, then you can pass it as the stdout or stderr argument of subprocess.Popen.

The file descriptor or handle for the screen buffer can't be read directly via read, ReadFile, or even ReadConsole. If you have a file descriptor, get the underlying handle via msvcrt.get_osfhandle. Given a screen buffer handle, call ReadConsoleOutputCharacter to read from the screen. The read_screen function in the sample code below demonstrates reading from the beginning of the screen buffer up to the cursor position.

A process needs to be attached to a console in order to use the console API. To that end, I've included a simple allocate_console context manager to temporarily open a console. This is useful in a GUI application, which normally isn't attached to a console.

The following example was tested in Windows 7 and 10, in Python 2.7 and 3.5.

ctypes definitions

import os
import contextlib
import msvcrt
import ctypes
from ctypes import wintypes

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

GENERIC_READ  = 0x80000000
GENERIC_WRITE = 0x40000000
FILE_SHARE_READ  = 1
FILE_SHARE_WRITE = 2
CONSOLE_TEXTMODE_BUFFER = 1
INVALID_HANDLE_VALUE = wintypes.HANDLE(-1).value
STD_OUTPUT_HANDLE = wintypes.DWORD(-11)
STD_ERROR_HANDLE = wintypes.DWORD(-12)

def _check_zero(result, func, args):
    if not result:
        raise ctypes.WinError(ctypes.get_last_error())
    return args

def _check_invalid(result, func, args):
    if result == INVALID_HANDLE_VALUE:
        raise ctypes.WinError(ctypes.get_last_error())
    return args

if not hasattr(wintypes, 'LPDWORD'): # Python 2
    wintypes.LPDWORD = ctypes.POINTER(wintypes.DWORD)
    wintypes.PSMALL_RECT = ctypes.POINTER(wintypes.SMALL_RECT)

class COORD(ctypes.Structure):
    _fields_ = (('X', wintypes.SHORT),
                ('Y', wintypes.SHORT))

class CONSOLE_SCREEN_BUFFER_INFOEX(ctypes.Structure):
    _fields_ = (('cbSize',               wintypes.ULONG),
                ('dwSize',               COORD),
                ('dwCursorPosition',     COORD),
                ('wAttributes',          wintypes.WORD),
                ('srWindow',             wintypes.SMALL_RECT),
                ('dwMaximumWindowSize',  COORD),
                ('wPopupAttributes',     wintypes.WORD),
                ('bFullscreenSupported', wintypes.BOOL),
                ('ColorTable',           wintypes.DWORD * 16))
    def __init__(self, *args, **kwds):
        super(CONSOLE_SCREEN_BUFFER_INFOEX, self).__init__(
                *args, **kwds)
        self.cbSize = ctypes.sizeof(self)

PCONSOLE_SCREEN_BUFFER_INFOEX = ctypes.POINTER(
                                    CONSOLE_SCREEN_BUFFER_INFOEX)
LPSECURITY_ATTRIBUTES = wintypes.LPVOID

kernel32.GetStdHandle.errcheck = _check_invalid
kernel32.GetStdHandle.restype = wintypes.HANDLE
kernel32.GetStdHandle.argtypes = (
    wintypes.DWORD,) # _In_ nStdHandle

kernel32.CreateConsoleScreenBuffer.errcheck = _check_invalid
kernel32.CreateConsoleScreenBuffer.restype = wintypes.HANDLE
kernel32.CreateConsoleScreenBuffer.argtypes = (
    wintypes.DWORD,        # _In_       dwDesiredAccess
    wintypes.DWORD,        # _In_       dwShareMode
    LPSECURITY_ATTRIBUTES, # _In_opt_   lpSecurityAttributes
    wintypes.DWORD,        # _In_       dwFlags
    wintypes.LPVOID)       # _Reserved_ lpScreenBufferData

kernel32.GetConsoleScreenBufferInfoEx.errcheck = _check_zero
kernel32.GetConsoleScreenBufferInfoEx.argtypes = (
    wintypes.HANDLE,               # _In_  hConsoleOutput
    PCONSOLE_SCREEN_BUFFER_INFOEX) # _Out_ lpConsoleScreenBufferInfo

kernel32.SetConsoleScreenBufferInfoEx.errcheck = _check_zero
kernel32.SetConsoleScreenBufferInfoEx.argtypes = (
    wintypes.HANDLE,               # _In_  hConsoleOutput
    PCONSOLE_SCREEN_BUFFER_INFOEX) # _In_  lpConsoleScreenBufferInfo

kernel32.SetConsoleWindowInfo.errcheck = _check_zero
kernel32.SetConsoleWindowInfo.argtypes = (
    wintypes.HANDLE,      # _In_ hConsoleOutput
    wintypes.BOOL,        # _In_ bAbsolute
    wintypes.PSMALL_RECT) # _In_ lpConsoleWindow

kernel32.FillConsoleOutputCharacterW.errcheck = _check_zero
kernel32.FillConsoleOutputCharacterW.argtypes = (
    wintypes.HANDLE,  # _In_  hConsoleOutput
    wintypes.WCHAR,   # _In_  cCharacter
    wintypes.DWORD,   # _In_  nLength
    COORD,            # _In_  dwWriteCoord
    wintypes.LPDWORD) # _Out_ lpNumberOfCharsWritten

kernel32.ReadConsoleOutputCharacterW.errcheck = _check_zero
kernel32.ReadConsoleOutputCharacterW.argtypes = (
    wintypes.HANDLE,  # _In_  hConsoleOutput
    wintypes.LPWSTR,  # _Out_ lpCharacter
    wintypes.DWORD,   # _In_  nLength
    COORD,            # _In_  dwReadCoord
    wintypes.LPDWORD) # _Out_ lpNumberOfCharsRead

functions

@contextlib.contextmanager
def allocate_console():
    allocated = kernel32.AllocConsole()
    try:
        yield allocated
    finally:
        if allocated:
            kernel32.FreeConsole()

@contextlib.contextmanager
def console_screen(ncols=None, nrows=None):
    info = CONSOLE_SCREEN_BUFFER_INFOEX()
    new_info = CONSOLE_SCREEN_BUFFER_INFOEX()
    nwritten = (wintypes.DWORD * 1)()
    hStdOut = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
    kernel32.GetConsoleScreenBufferInfoEx(
           hStdOut, ctypes.byref(info))
    if ncols is None:
        ncols = info.dwSize.X
    if nrows is None:
        nrows = info.dwSize.Y
    elif nrows > 9999:
        raise ValueError('nrows must be 9999 or less')
    fd_screen = None
    hScreen = kernel32.CreateConsoleScreenBuffer(
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                None, CONSOLE_TEXTMODE_BUFFER, None)
    try:
        fd_screen = msvcrt.open_osfhandle(
                        hScreen, os.O_RDWR | os.O_BINARY)
        kernel32.GetConsoleScreenBufferInfoEx(
               hScreen, ctypes.byref(new_info))
        new_info.dwSize = COORD(ncols, nrows)
        new_info.srWindow = wintypes.SMALL_RECT(
                Left=0, Top=0, Right=(ncols - 1),
                Bottom=(info.srWindow.Bottom - info.srWindow.Top))
        kernel32.SetConsoleScreenBufferInfoEx(
                hScreen, ctypes.byref(new_info))
        kernel32.SetConsoleWindowInfo(hScreen, True,
                ctypes.byref(new_info.srWindow))
        kernel32.FillConsoleOutputCharacterW(
                hScreen, u'\0', ncols * nrows, COORD(0,0), nwritten)
        kernel32.SetConsoleActiveScreenBuffer(hScreen)
        try:
            yield fd_screen
        finally:
            kernel32.SetConsoleScreenBufferInfoEx(
                hStdOut, ctypes.byref(info))
            kernel32.SetConsoleWindowInfo(hStdOut, True,
                    ctypes.byref(info.srWindow))
            kernel32.SetConsoleActiveScreenBuffer(hStdOut)
    finally:
        if fd_screen is not None:
            os.close(fd_screen)
        else:
            kernel32.CloseHandle(hScreen)

def read_screen(fd):
    hScreen = msvcrt.get_osfhandle(fd)
    csbi = CONSOLE_SCREEN_BUFFER_INFOEX()
    kernel32.GetConsoleScreenBufferInfoEx(
        hScreen, ctypes.byref(csbi))
    ncols = csbi.dwSize.X
    pos = csbi.dwCursorPosition
    length = ncols * pos.Y + pos.X + 1
    buf = (ctypes.c_wchar * length)()
    n = (wintypes.DWORD * 1)()
    kernel32.ReadConsoleOutputCharacterW(
        hScreen, buf, length, COORD(0,0), n)
    lines = [buf[i:i+ncols].rstrip(u'\0')
                for i in range(0, n[0], ncols)]
    return u'\n'.join(lines)

example

if __name__ == '__main__':
    import io
    import textwrap
    import subprocess

    text = textwrap.dedent('''\
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
        eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
        enim ad minim veniam, quis nostrud exercitation ullamco laboris
        nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
        in reprehenderit in voluptate velit esse cillum dolore eu
        fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
        proident, sunt in culpa qui officia deserunt mollit anim id est
        laborum.''')

    cmd = ("python -c \""
           "print('piped output');"
           "conout = open(r'CONOUT$', 'w');"
           "conout.write('''%s''')\"" % text)

    with allocate_console() as allocated:
        with console_screen(nrows=1000) as fd_conout:
            stdout = subprocess.check_output(cmd).decode()
            conout = read_screen(fd_conout)
            with io.open('result.txt', 'w', encoding='utf-8') as f:
                f.write(u'stdout:\n' + stdout)
                f.write(u'\nconout:\n' + conout)

output

stdout:
piped output

conout:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est
laborum.
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • Wow, thanks for this detailed answer! I got it to work with only one minor change on Windows 8.1, with Python 2.7.11 from Anaconda2 and can now parse the executable's output. – buggy Aug 04 '16 at 10:38
  • There was an AttributeError (`'module' object has no attribute 'LPDWORD'`) in the ctypes definitions part. And in fact, when I open wintypes.py it does not contain LPDWORD. Defining it manually, `localLPDWORD = ctypes.POINTER(wintypes.DWORD)`, and using `localLPDWORD` instead of `wintypes.LPDWORD` fixed this. – buggy Aug 04 '16 at 10:38
  • Still remaining is a UnicodeDecodeError that I cannot really trace down to anywhere in the code, but it does not stop the script from creating the correct output. – buggy Aug 04 '16 at 10:39
  • 1
    I made some changes to support Python 2. `read_screen` returns a Unicode string, so your code needs to be careful to decode byte strings (2.x `str`) before mixing them with its output. Also, when writing Unicode to files you should use `io.open` (it's backported to 2.7 from Python 3) and preferably UTF-8 encoding. – Eryk Sun Aug 04 '16 at 13:09