Do not log warnings to stdout (original) (raw)

Description of the bug

What happened?

If one installs the latest version of PyMuPDF, and they initialize a Pixmap object using Python bytes, they will see a log message in the process' stdout:

__init__: using mupdf.python_buffer_data()

(note, this applies only to the new fitz module, not fitzold)

The underlying cause is that PyMuPDF has the following logging function:

def log( text, caller=1):
frame_record = inspect.stack( context=0)[ caller]
filename = os.path.relpath(frame_record.filename)
line = frame_record.lineno
function = frame_record.function
print( f'{filename}:{line}:{function}: {text}', file=sys.stdout)
sys.stdout.flush()

This function does not employ regular Python logging, but simply writes to stdout. The stdout of a process though is not the place to write warnings, since it may interfere with binary data.

How to reproduce the bug

What I expected to happen?

Initializing a Pixmap object should not write anything to stdout.

How to reproduce it?

  1. Import the fitz_new module
  2. Initialize a Pixmap class using a bytes object for the samples argument (see Pixmap constructor)

Or simply run:

import fitz
fitz.Pixmap(fitz.Colorspace(fitz.CS_RGB), 10, 10, b'a' * 100 * 3, False)

PyMuPDF version

1.23.14

Operating system

Linux

Python version

3.11