Skip to content

Commit

Permalink
Merge pull request #98 from HansBug/fix/output
Browse files Browse the repository at this point in the history
dev(hansbug): fix bug for tqdm
  • Loading branch information
HansBug authored Apr 12, 2023
2 parents f780344 + d9fe315 commit 5d41326
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
6 changes: 4 additions & 2 deletions hbutils/testing/capture/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,13 @@ def capture_output(mem: bool = False) -> ContextManager[OutputCaptureResult]:


@contextmanager
def disable_output() -> ContextManager[OutputCaptureResult]:
def disable_output(encoding='utf-8') -> ContextManager[OutputCaptureResult]:
"""
Overview:
Disable all the output to ``sys.stdout`` and ``sys.stderr`` in this ``with`` block.
:param encoding: Encoding of null file, default is ``utf-8``.
Examples::
>>> import sys
>>> from hbutils.testing import disable_output
Expand All @@ -144,6 +146,6 @@ def disable_output() -> ContextManager[OutputCaptureResult]:
... print('this is stdout')
... print('this is stderr', file=sys.stderr)
"""
with open(os.devnull, 'w') as sout:
with open(os.devnull, 'w', encoding=encoding) as sout:
with redirect_stdout(sout), redirect_stderr(sout):
yield
3 changes: 2 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ numpy>=1.20; implementation_name != 'pypy' or platform_system != 'Windows' or py
torch>=1.1.0; python_version < '3.11' and implementation_name != 'pypy'
faker; python_version > '3.7'
requests>=2.20
testtools>=2
testtools>=2
tqdm
11 changes: 11 additions & 0 deletions test/testing/capture/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from textwrap import dedent

import pytest
from tqdm.auto import tqdm

from hbutils.testing import capture_output, disable_output, isolated_directory

Expand Down Expand Up @@ -66,3 +67,13 @@ def test_disable_output(self):
This is stderr 2rd line.
This is stderr 3rd line.
""").strip()

def test_disable_output_with_tqdm(self):
with capture_output() as r:
with disable_output():
f = tqdm(total=10)
f.set_description('你好,这个是中文')
f.update(5)

assert not r.stdout.strip()
assert not r.stderr.strip()

0 comments on commit 5d41326

Please sign in to comment.