Mercurial > hgrepos > Python2 > PyMuPDF
comparison tests/test_general.py @ 39:a6bc019ac0b2 upstream
ADD: PyMuPDF v1.26.5: the original sdist.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 11 Oct 2025 11:19:58 +0200 |
| parents | 1d09e1dec1d9 |
| children |
comparison
equal
deleted
inserted
replaced
| 2:b50eed0cc0ef | 39:a6bc019ac0b2 |
|---|---|
| 783 assert error == True | 783 assert error == True |
| 784 assert text == "CropBox not in MediaBox" | 784 assert text == "CropBox not in MediaBox" |
| 785 | 785 |
| 786 | 786 |
| 787 def test_subset_fonts(): | 787 def test_subset_fonts(): |
| 788 if os.environ.get('PYODIDE_ROOT'): | |
| 789 print('test_subset_fonts(): not running on Pyodide - ValueError: No font code \'ubuntu\' found in pymupdf-fonts.') | |
| 790 return | |
| 788 """Confirm subset_fonts is working.""" | 791 """Confirm subset_fonts is working.""" |
| 789 if not hasattr(pymupdf, "mupdf"): | 792 if not hasattr(pymupdf, "mupdf"): |
| 790 print("Not testing 'test_subset_fonts' in classic.") | 793 print("Not testing 'test_subset_fonts' in classic.") |
| 791 return | 794 return |
| 792 text = "Just some arbitrary text." | 795 text = "Just some arbitrary text." |
| 1024 fd2 = next_fd() | 1027 fd2 = next_fd() |
| 1025 assert fd2 == fd1, f'{fd1=} {fd2=}' | 1028 assert fd2 == fd1, f'{fd1=} {fd2=}' |
| 1026 os.remove(oldfile) | 1029 os.remove(oldfile) |
| 1027 | 1030 |
| 1028 def test_cli(): | 1031 def test_cli(): |
| 1032 if os.environ.get('PYODIDE_ROOT'): | |
| 1033 print('test_cli(): not running on Pyodide - cannot run child processes.') | |
| 1034 return | |
| 1035 | |
| 1029 if not hasattr(pymupdf, 'mupdf'): | 1036 if not hasattr(pymupdf, 'mupdf'): |
| 1030 print('test_cli(): Not running on classic because of fitz_old.') | 1037 print('test_cli(): Not running on classic because of fitz_old.') |
| 1031 return | 1038 return |
| 1032 import subprocess | 1039 import subprocess |
| 1033 subprocess.run(f'pymupdf -h', shell=1, check=1) | 1040 subprocess.run(f'pymupdf -h', shell=1, check=1) |
| 1061 def test_cli_out(): | 1068 def test_cli_out(): |
| 1062 ''' | 1069 ''' |
| 1063 Check redirection of messages and log diagnostics with environment | 1070 Check redirection of messages and log diagnostics with environment |
| 1064 variables PYMUPDF_LOG and PYMUPDF_MESSAGE. | 1071 variables PYMUPDF_LOG and PYMUPDF_MESSAGE. |
| 1065 ''' | 1072 ''' |
| 1073 if os.environ.get('PYODIDE_ROOT'): | |
| 1074 print('test_cli_out(): not running on Pyodide - cannot run child processes.') | |
| 1075 return | |
| 1076 | |
| 1066 if not hasattr(pymupdf, 'mupdf'): | 1077 if not hasattr(pymupdf, 'mupdf'): |
| 1067 print('test_cli(): Not running on classic because of fitz_old.') | 1078 print('test_cli(): Not running on classic because of fitz_old.') |
| 1068 return | 1079 return |
| 1069 import platform | 1080 import platform |
| 1070 import re | 1081 import re |
| 1148 | 1159 |
| 1149 def test_use_python_logging(): | 1160 def test_use_python_logging(): |
| 1150 ''' | 1161 ''' |
| 1151 Checks pymupdf.use_python_logging(). | 1162 Checks pymupdf.use_python_logging(). |
| 1152 ''' | 1163 ''' |
| 1164 if os.environ.get('PYODIDE_ROOT'): | |
| 1165 print('test_cli(): not running on Pyodide - cannot run child processes.') | |
| 1166 return | |
| 1167 | |
| 1153 log_prefix = None | 1168 log_prefix = None |
| 1154 if os.environ.get('PYMUPDF_USE_EXTRA') == '0': | 1169 if os.environ.get('PYMUPDF_USE_EXTRA') == '0': |
| 1155 log_prefix = f'.+Using non-default setting from PYMUPDF_USE_EXTRA: \'0\'' | 1170 log_prefix = f'.+Using non-default setting from PYMUPDF_USE_EXTRA: \'0\'' |
| 1156 | 1171 |
| 1157 if os.path.basename(__file__).startswith(f'test_fitz_'): | 1172 if os.path.basename(__file__).startswith(f'test_fitz_'): |
| 1431 def test_open2(): | 1446 def test_open2(): |
| 1432 ''' | 1447 ''' |
| 1433 Checks behaviour of fz_open_document() and fz_open_document_with_stream() | 1448 Checks behaviour of fz_open_document() and fz_open_document_with_stream() |
| 1434 with different filenames/magic values. | 1449 with different filenames/magic values. |
| 1435 ''' | 1450 ''' |
| 1451 if os.environ.get('PYODIDE_ROOT'): | |
| 1452 print('test_open2(): not running on Pyodide - cannot run child processes.') | |
| 1453 return | |
| 1454 | |
| 1436 if platform.system() == 'Windows': | 1455 if platform.system() == 'Windows': |
| 1437 print(f'test_open2(): not running on Windows because `git ls-files` known fail on Github Windows runners.') | 1456 print(f'test_open2(): not running on Windows because `git ls-files` known fail on Github Windows runners.') |
| 1438 return | 1457 return |
| 1439 | 1458 |
| 1440 root = os.path.normpath(f'{__file__}/../..') | 1459 root = os.path.normpath(f'{__file__}/../..') |
| 1787 document = pymupdf.open() | 1806 document = pymupdf.open() |
| 1788 page = document.new_page() | 1807 page = document.new_page() |
| 1789 document.delete_page() | 1808 document.delete_page() |
| 1790 | 1809 |
| 1791 def test_4263(): | 1810 def test_4263(): |
| 1811 if os.environ.get('PYODIDE_ROOT'): | |
| 1812 print('test_4263(): not running on Pyodide - cannot run child processes.') | |
| 1813 return | |
| 1814 | |
| 1792 path = os.path.normpath(f'{__file__}/../../tests/resources/test_4263.pdf') | 1815 path = os.path.normpath(f'{__file__}/../../tests/resources/test_4263.pdf') |
| 1793 path_out = f'{path}.linerarized.pdf' | 1816 path_out = f'{path}.linerarized.pdf' |
| 1794 command = f'pymupdf clean -linear {path} {path_out}' | 1817 command = f'pymupdf clean -linear {path} {path_out}' |
| 1795 print(f'Running: {command}') | 1818 print(f'Running: {command}') |
| 1796 cp = subprocess.run(command, shell=1, check=0) | 1819 cp = subprocess.run(command, shell=1, check=0) |
| 1913 {'depth': 0, 'locked': 0, 'number': 7, 'on': 1, 'text': 'layer_7', 'type': 'checkbox'}, | 1936 {'depth': 0, 'locked': 0, 'number': 7, 'on': 1, 'text': 'layer_7', 'type': 'checkbox'}, |
| 1914 ] | 1937 ] |
| 1915 | 1938 |
| 1916 | 1939 |
| 1917 def test_4533(): | 1940 def test_4533(): |
| 1941 if os.environ.get('PYODIDE_ROOT'): | |
| 1942 print('test_4533(): not running on Pyodide - cannot run child processes.') | |
| 1943 return | |
| 1944 | |
| 1918 print() | 1945 print() |
| 1919 path = util.download( | 1946 path = util.download( |
| 1920 'https://github.com/user-attachments/files/20497146/NineData_user_manual_V3.0.5.pdf', | 1947 'https://github.com/user-attachments/files/20497146/NineData_user_manual_V3.0.5.pdf', |
| 1921 'test_4533.pdf', | 1948 'test_4533.pdf', |
| 1922 size=16864501, | 1949 size=16864501, |
| 1960 print(f'{pymupdf.mupdf_location=}') | 1987 print(f'{pymupdf.mupdf_location=}') |
| 1961 print(f'{pymupdf.mupdf_version=}') | 1988 print(f'{pymupdf.mupdf_version=}') |
| 1962 print(f'{pymupdf.pymupdf_git_branch=}') | 1989 print(f'{pymupdf.pymupdf_git_branch=}') |
| 1963 print(f'{pymupdf.pymupdf_git_sha=}') | 1990 print(f'{pymupdf.pymupdf_git_sha=}') |
| 1964 print(f'{pymupdf.pymupdf_version=}') | 1991 print(f'{pymupdf.pymupdf_version=}') |
| 1965 print(f'pymupdf.pymupdf_git_diff:\n{textwrap.indent(pymupdf.pymupdf_git_diff, " ")}') | 1992 print(f'{pymupdf.pymupdf_git_diff=}') |
| 1993 if pymupdf.pymupdf_git_diff: | |
| 1994 print(f'pymupdf.pymupdf_git_diff:\n{textwrap.indent(pymupdf.pymupdf_git_diff, " ")}') | |
| 1966 | 1995 |
| 1967 | 1996 |
| 1968 def test_4392(): | 1997 def test_4392(): |
| 1998 if os.environ.get('PYODIDE_ROOT'): | |
| 1999 print('test_4392(): not running on Pyodide - cannot run child processes.') | |
| 2000 return | |
| 2001 | |
| 1969 print() | 2002 print() |
| 1970 path = os.path.normpath(f'{__file__}/../../tests/test_4392.py') | 2003 path = os.path.normpath(f'{__file__}/../../tests/test_4392.py') |
| 1971 with open(path, 'w') as f: | 2004 with open(path, 'w') as f: |
| 1972 f.write('import pymupdf\n') | 2005 f.write('import pymupdf\n') |
| 1973 | 2006 |
| 2049 document.save(path) | 2082 document.save(path) |
| 2050 | 2083 |
| 2051 # Check pymupdf.Document.scrub() works. | 2084 # Check pymupdf.Document.scrub() works. |
| 2052 with pymupdf.open(path) as document: | 2085 with pymupdf.open(path) as document: |
| 2053 document.scrub() | 2086 document.scrub() |
| 2087 | |
| 2088 | |
| 2089 def test_4702(): | |
| 2090 if os.environ.get('PYODIDE_ROOT'): | |
| 2091 # util.download() uses subprocess. | |
| 2092 print('test_4702(): not running on Pyodide - cannot run child processes.') | |
| 2093 return | |
| 2094 | |
| 2095 path = util.download( | |
| 2096 'https://github.com/user-attachments/files/22403483/01995b6ca7837b52abaa24e38e8c076d.pdf', | |
| 2097 'test_4702.pdf', | |
| 2098 ) | |
| 2099 with pymupdf.open(path) as document: | |
| 2100 for xref in range(1, document.xref_length()): | |
| 2101 print(f'{xref=}') | |
| 2102 try: | |
| 2103 _ = document.xref_object(xref) | |
| 2104 except Exception as e1: | |
| 2105 print(f'{e1=}') | |
| 2106 try: | |
| 2107 document.update_object(xref, "<<>>") | |
| 2108 except Exception as e2: | |
| 2109 print(f'{e2=}') | |
| 2110 raise | |
| 2111 wt = pymupdf.TOOLS.mupdf_warnings() | |
| 2112 assert wt == 'repairing PDF document' | |
| 2113 | |
| 2114 with pymupdf.open(path) as document: | |
| 2115 for xref in range(1, document.xref_length()): | |
| 2116 print(f'{xref=}') | |
| 2117 _ = document.xref_object(xref) | |
| 2118 wt = pymupdf.TOOLS.mupdf_warnings() | |
| 2119 assert wt == 'repairing PDF document' | |
| 2120 | |
| 2121 | |
| 2122 def test_4712(): | |
| 2123 ''' | |
| 2124 Crash with "corrupted double-linked list | |
| 2125 ''' | |
| 2126 if 1: | |
| 2127 print(f'test_4712(): Not running because known to fail.') | |
| 2128 return | |
| 2129 path_a = os.path.normpath(f'{__file__}/../../tests/resources/test_4712_a.pdf') | |
| 2130 path_b = os.path.normpath(f'{__file__}/../../tests/resources/test_4712_b.pdf') | |
| 2131 doc1 = pymupdf.open(path_a) | |
| 2132 for i in range(6): | |
| 2133 doc1.load_page(i).get_pixmap() | |
| 2134 doc2 = pymupdf.open(path_b) | |
| 2135 for i in range(6): | |
| 2136 doc2.load_page(i).get_pixmap() | |
| 2137 | |
| 2138 | |
| 2139 def test_4712m(): | |
| 2140 if 1: | |
| 2141 print(f'test_4712b(): Not running because known to fail.') | |
| 2142 return | |
| 2143 | |
| 2144 path_a = os.path.normpath(f'{__file__}/../../tests/resources/test_4712_a.pdf') | |
| 2145 path_b = os.path.normpath(f'{__file__}/../../tests/resources/test_4712_b.pdf') | |
| 2146 | |
| 2147 mupdf = pymupdf.mupdf | |
| 2148 def get_pixmap(page): | |
| 2149 displaylist = mupdf.fz_new_display_list_from_page(page) | |
| 2150 rect = mupdf.fz_bound_display_list(displaylist) | |
| 2151 irect = mupdf.fz_round_rect(rect) | |
| 2152 pixmap = mupdf.fz_new_pixmap_with_bbox( | |
| 2153 mupdf.FzColorspace(mupdf.FzColorspace.Fixed_RGB), | |
| 2154 irect, | |
| 2155 mupdf.FzSeparations(), | |
| 2156 0, # alpha | |
| 2157 ) | |
| 2158 mupdf.fz_clear_pixmap_with_value(pixmap, 0xFF) | |
| 2159 matrix = mupdf.FzMatrix() | |
| 2160 device = mupdf.fz_new_draw_device(matrix, pixmap) | |
| 2161 mupdf.fz_run_display_list( | |
| 2162 displaylist, | |
| 2163 device, | |
| 2164 mupdf.FzMatrix(), | |
| 2165 mupdf.FzRect(mupdf.FzRect.Fixed_INFINITE), | |
| 2166 mupdf.FzCookie(), | |
| 2167 ) | |
| 2168 mupdf.fz_close_device(device) | |
| 2169 | |
| 2170 def process_document(document): | |
| 2171 for i in range(6): | |
| 2172 print(f' {i=}', flush=1) | |
| 2173 page = mupdf.fz_load_page(document, i) | |
| 2174 get_pixmap(page) | |
| 2175 | |
| 2176 print(f'Processing {path_a=}', flush=1) | |
| 2177 document_a = mupdf.fz_open_document(path_a) | |
| 2178 process_document(document_a) | |
| 2179 | |
| 2180 print(f'Processing {path_b=}', flush=1) | |
| 2181 document_b = mupdf.fz_open_document(path_b) | |
| 2182 process_document(document_b) |
