Skip to content

Commit

Permalink
src/__init__.py tests/: cope if sys.stdout is None.
Browse files Browse the repository at this point in the history
This addresses #3981.
  • Loading branch information
julian-smith-artifex-com committed Oct 28, 2024
1 parent d92e85c commit 7bb66d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _make_output(
pylogging_level = int(pylogging_level)
pylogging_name = items_d.get('name', 'pymupdf')
else:
assert 0, f'Unrecognised {text=}.'
assert 0, f'Expected prefix `fd:`, `path:`. `path+:` or `logging:` in {text=}.'

if fd is not None:
ret = open(fd, mode='w', closefd=False)
Expand Down Expand Up @@ -154,10 +154,8 @@ def write(self, text):
def flush(self):
pass
ret = Out()
elif default:
ret = default
else:
assert 0, f'No output specified.'
ret = default
return ret

# Set steam used by PyMuPDF messaging.
Expand Down Expand Up @@ -259,14 +257,18 @@ def log( text='', caller=1):
text = f'{filename}:{line}:{function}(): {text}'
if _g_log_items_active:
_g_log_items.append(text)
print(text, file=_g_out_log, flush=1)
if _g_out_log:
print(text, file=_g_out_log, flush=1)


def message(text=''):
'''
For user messages.
'''
print(text, file=_g_out_message, flush=1)
# It looks like `print()` does nothing if sys.stdout is None (without
# raising an exception), but we don't rely on this.
if _g_out_message:
print(text, file=_g_out_message, flush=1)


def exception_info():
Expand Down
14 changes: 14 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,20 @@ def check(
],
)

print(f'## Check messages() with sys.stdout=None.')
check(
'''
import sys
sys.stdout = None
import pymupdf
pymupdf.message('this is pymupdf.message()')
pymupdf.log('this is pymupdf.log()')
''',
[],
[],
)


def relpath(path, start=None):
'''
Expand Down

0 comments on commit 7bb66d3

Please sign in to comment.