MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / _test_4751

Function _test_4751

tests/test_memory.py:240–319  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

238
239
240def _test_4751():
241 import gc
242 import tracemalloc
243
244 def analysis(stream_data, do_iter=True):
245 pdf_info = pymupdf.Document(stream=stream_data, filetype='pdf')
246 tmp_list = range(len(pdf_info))
247 for page_num in tmp_list:
248 page = pdf_info[page_num]
249 raw_info = page.get_text('rawdict')['blocks']
250 page_widgets_list = page.widgets()
251 if do_iter:
252 for widget_info in page_widgets_list:
253 print(widget_info)
254 del page_widgets_list
255 pdf_info.close()
256 pdf_info = None
257 pymupdf.TOOLS.store_shrink(100)
258
259 file_path = os.path.normpath(f'{__file__}/../../tests/resources/test_4751.pdf')
260
261 def log(text):
262 print(text, flush=1)
263
264 # We filter out all allocations where leaf-most frame is in tracemalloc
265 # itself, or in test_memory.py itself, because these are not relevant
266 # to finding leaks in pymupdf.
267 #
268 tm_filters = [
269 tracemalloc.Filter(inclusive=False, filename_pattern=tracemalloc.__file__, all_frames=True),
270 tracemalloc.Filter(inclusive=False, filename_pattern=__file__),
271 ]
272
273 def get_snapshot():
274 '''
275 Wrapper for tracemalloc.take_snapshot() that filters out blocks with
276 backtraces that we are not interested in.
277 '''
278 ret = tracemalloc.take_snapshot()
279 ret2 = ret.filter_traces(tm_filters)
280 #log(f' {len(ret.traces)=} => {len(ret2.traces)=}')
281 return ret2
282
283 # Check that `analysis()` does not leak.
284 #
285 num_leaks = 0
286 with open(file_path,'rb') as f:
287 bytes_data = f.read()
288
289 tracemalloc.start(30)
290 snapshot_prev = get_snapshot()
291
292 for it in range(2):
293 log('')
294 log(f'{it=}')
295
296 current, peak = tracemalloc.get_traced_memory()
297 log(f' {current=} {peak=}')

Callers

nothing calls this directly

Calls 3

get_snapshotFunction · 0.85
analysisFunction · 0.85
logFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…