Skip to content

calling capture.resume_global_capture() changes the used terminal width #13322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
15r10nk opened this issue Mar 22, 2025 · 2 comments
Open
Labels
plugin: capture related to the capture builtin plugin type: bug problem that needs to be addressed

Comments

@15r10nk
Copy link
Contributor

15r10nk commented Mar 22, 2025

I have to suspend the global capture, because I ask the user for some user input in my pytest plugin.
But this leads to a changed terminal width.

conftest.py

import pytest
import shutil


@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session):
    capture = session.config.pluginmanager.getplugin("capturemanager")
    capture.suspend_global_capture(in_=True)

    # some code to read user input

    print(shutil.get_terminal_size())
    capture.resume_global_capture()
    print(shutil.get_terminal_size())

Image

The === in the no tests ran line are shorter than above and the result of get_terminal_size changed.

pytest: 8.3.5
os: Debian

I performed this test in a directory that only contained the specified file conftest.py.

@Zac-HD Zac-HD added type: bug problem that needs to be addressed plugin: capture related to the capture builtin plugin labels Mar 23, 2025
@sanyamk23
Copy link

sanyamk23 commented Mar 31, 2025

Hey @15r10nk ,

Thanks for reporting this! It looks like suspending global capture is affecting how pytest handles terminal settings, which in turn changes the reported terminal width. This is likely due to how pytest manages stdout and stderr during capture.

Potential Workarounds:
✅ Store and restore the terminal size manually before and after suspending capture:
``
`import pytest
import shutil
import os

@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session):
capture = session.config.pluginmanager.getplugin("capturemanager")
terminal_size_before = shutil.get_terminal_size()

capture.suspend_global_capture(in_=True)


print("Terminal size after suspend:", shutil.get_terminal_size())

capture.resume_global_capture()

os.environ["COLUMNS"] = str(terminal_size_before.columns)
os.environ["LINES"] = str(terminal_size_before.lines)

print("Terminal size after resume:", shutil.get_terminal_size())


✅ Write directly to sys.__stdout__ instead of using print(), since pytest captures stdout:

import sys
sys.stdout.write(f"Terminal size after suspend: {shutil.get_terminal_size()}\n")


✅ Try flushing stdout before and after suspending capture to maintain expected behavior:

import sys
sys.stdout.flush()


Would love to hear if any of these solutions work for you! Let me know how it goes. 🚀

@RonnyPfannschmidt
Copy link
Member

@sanyamk23 if you post random Ai output, at least verify its not completely wrong on the technical side

It's simply mean to anyone else as is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: capture related to the capture builtin plugin type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

4 participants