Skip to content

Instantly share code, notes, and snippets.

@westurner
Last active August 29, 2024 00:56
Show Gist options
  • Save westurner/9ffe6291e4ffa91b459e8afd70369230 to your computer and use it in GitHub Desktop.
Save westurner/9ffe6291e4ffa91b459e8afd70369230 to your computer and use it in GitHub Desktop.
Patch subprocess.Popen to log args, kwargs, and the cmd string
from functools import partial
class Popen_with_logging(subprocess.Popen):
"""Patch subprocess.Popen to log args, kwargs, and the cmd string"""
def __init__(self, *args, cmdprefix='+', **kwargs):
print = kwargs.get("printfunc",
partial(__builtins__.print, file=sys.stderr))
# print = log.debug ; printfunc=log.debug
if kwargs.get('shell'):
if len(args) == 1:
cmdstr = args[0]
else:
cmdstr = " ".join(str(x) for x in args)
else: # shell=False
if len(args) == 1:
cmdstr = " ".join(str(x) for x in args[0])
else:
cmdstr = " ".join(str(x) for x in args)
event = dict(event='subprocess.Popen', args=args, kwargs=kwargs)
event['cmdstr'] = cmdstr
print(event)
print(f'>>> subprocess.Popen(*{args}, **{kwargs})')
print(cmdprefix, cmdstr)
super().__init__(*args, **kwargs)
# TODO: more proper monkeypatch than this:
_subprocess_Popen = subprocess.Popen
subprocess.Popen = Popen_with_logging
def test_subprocess_Popen_with_logging():
subprocess.Popen(["echo", "test1"])
subprocess.check_output("echo test1", shell=True)
@westurner
Copy link
Author

westurner commented Aug 29, 2024

To log output:

@westurner
Copy link
Author

@westurner
Copy link
Author

pytest docs > "Accessing captured output from a test function"
https://docs.pytest.org/en/stable/how-to/capture-stdout-stderr.html :

def test_myoutput(capsys):  # or use "capfd" for fd-level
    print("hello")
    sys.stderr.write("world\n")

    captured = capsys.readouterr()
    assert captured.out == "hello\n"
    assert captured.err == "world\n"

    print("next")
    captured = capsys.readouterr()
    assert captured.out == "next\n"

@westurner
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment