Checks pymupdf.use_python_logging().
()
| 1171 | |
| 1172 | |
| 1173 | def test_use_python_logging(): |
| 1174 | ''' |
| 1175 | Checks pymupdf.use_python_logging(). |
| 1176 | ''' |
| 1177 | if os.environ.get('PYODIDE_ROOT'): |
| 1178 | print('test_cli(): not running on Pyodide - cannot run child processes.') |
| 1179 | return |
| 1180 | |
| 1181 | log_prefix = None |
| 1182 | if os.environ.get('PYMUPDF_USE_EXTRA') == '0': |
| 1183 | log_prefix = f'.+Using non-default setting from PYMUPDF_USE_EXTRA: \'0\'' |
| 1184 | |
| 1185 | if os.path.basename(__file__).startswith(f'test_fitz_'): |
| 1186 | # Do nothing, because command `pymupdf` outputs diagnostics containing |
| 1187 | # `pymupdf` which are not renamed to `fitz`, which breaks our checking. |
| 1188 | print(f'Not testing with fitz alias.') |
| 1189 | return |
| 1190 | |
| 1191 | def check( |
| 1192 | code, |
| 1193 | regexes_stdout, |
| 1194 | regexes_stderr, |
| 1195 | env = None, |
| 1196 | ): |
| 1197 | code = textwrap.dedent(code) |
| 1198 | path = os.path.abspath(f'{__file__}/../../tests/resources_test_logging.py') |
| 1199 | with open(path, 'w') as f: |
| 1200 | f.write(code) |
| 1201 | command = f'{sys.executable} {path}' |
| 1202 | if env: |
| 1203 | print(f'{env=}.') |
| 1204 | env = os.environ | env |
| 1205 | print(f'Running: {command}', flush=1) |
| 1206 | try: |
| 1207 | cp = subprocess.run(command, shell=1, check=1, capture_output=1, text=True, env=env) |
| 1208 | except Exception as e: |
| 1209 | print(f'Command failed: {command}.', flush=1) |
| 1210 | print(f'Stdout\n{textwrap.indent(e.stdout, " ")}', flush=1) |
| 1211 | print(f'Stderr\n{textwrap.indent(e.stderr, " ")}', flush=1) |
| 1212 | raise |
| 1213 | check_lines(regexes_stdout, cp.stdout) |
| 1214 | check_lines(regexes_stderr, cp.stderr) |
| 1215 | |
| 1216 | print(f'## Basic use of `logging` sends output to stderr instead of default stdout.') |
| 1217 | check( |
| 1218 | ''' |
| 1219 | import pymupdf |
| 1220 | pymupdf.message('this is pymupdf.message()') |
| 1221 | pymupdf.log('this is pymupdf.log()') |
| 1222 | pymupdf.set_messages(pylogging=1) |
| 1223 | pymupdf.set_log(pylogging=1) |
| 1224 | pymupdf.message('this is pymupdf.message() 2') |
| 1225 | pymupdf.log('this is pymupdf.log() 2') |
| 1226 | ''', |
| 1227 | [ |
| 1228 | log_prefix, |
| 1229 | 'this is pymupdf.message[(][)]', |
| 1230 | '.+this is pymupdf.log[(][)]', |