Skip to content

Commit

Permalink
src/__init__.py tests/conftest.py: check no calls to log() when runni…
Browse files Browse the repository at this point in the history
…ng tests.

Also changed exception_info() to call log() instead of writing directly to
_g_out_log, so that exception backtraces are also checked.
  • Loading branch information
julian-smith-artifex-com committed May 20, 2024
1 parent 756f3f5 commit ec70319
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def _set_stream(name, default):
_g_out_log = _set_stream('PYMUPDF_LOG', sys.stdout)
_g_out_message = _set_stream('PYMUPDF_MESSAGE', sys.stdout)

# Set to list() if we are in test suite.
_g_log_items = None

def log( text='', caller=1):
'''
Expand All @@ -70,7 +72,10 @@ def log( text='', caller=1):
filename = os.path.relpath(frame_record.filename)
line = frame_record.lineno
function = frame_record.function
print( f'{filename}:{line}:{function}: {text}', file=_g_out_log)
text = f'{filename}:{line}:{function}: {text}'
if _g_log_items is not None:
_g_log_items.append(text)
print(text, file=_g_out_log)
_g_out_log.flush()


Expand All @@ -85,7 +90,7 @@ def message(text=''):
def exception_info():
import traceback
log(f'exception_info:')
traceback.print_exc(file=_g_out_log)
log(traceback.format_exc())


# PDF names must not contain these characters:
Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def wrap(*args, **kwargs):
assert not wt, f'{wt=}'
assert not pymupdf.TOOLS.set_small_glyph_heights()

pymupdf._g_log_items = list()

# Run the test.
rep = yield

Expand All @@ -23,3 +25,5 @@ def wrap(*args, **kwargs):
assert not wt, f'Warnings text not empty: {wt=}'

assert not pymupdf.TOOLS.set_small_glyph_heights()

assert not pymupdf._g_log_items, f'log() was called; {len(pymupdf._g_log_items)=}.'

0 comments on commit ec70319

Please sign in to comment.