MCPcopy Index your code
hub / github.com/pymupdf/PyMuPDF / test_3081

Function test_3081

tests/test_general.py:912–967  ·  view source on GitHub ↗

Check Document.close() closes file handles, even if a Page instance exists.

()

Source from the content-addressed store, hash-verified

910 assert wt == 'invalid marked content and clip nesting'
911
912def test_3081():
913 '''
914 Check Document.close() closes file handles, even if a Page instance exists.
915 '''
916 path1 = os.path.abspath(f'{__file__}/../../tests/resources/1.pdf')
917 path2 = os.path.abspath(f'{__file__}/../../tests/test_3081-2.pdf')
918
919 import shutil
920 import sys
921 import traceback
922 shutil.copy2(path1, path2)
923
924 # Find next two available fds.
925 next_fd_1 = os.open(path2, os.O_RDONLY)
926 next_fd_2 = os.open(path2, os.O_RDONLY)
927 os.close(next_fd_1)
928 os.close(next_fd_2)
929
930 def next_fd():
931 fd = os.open(path2, os.O_RDONLY)
932 os.close(fd)
933 return fd
934
935 fd1 = next_fd()
936 document = pymupdf.open(path2)
937 page = document[0]
938 fd2 = next_fd()
939 document.close()
940 assert document.this is None
941 assert page.this is None
942 try:
943 document.page_count()
944 except Exception as e:
945 print(f'Received expected exception: {e}')
946 #traceback.print_exc(file=sys.stdout)
947 assert str(e) == 'document closed'
948 else:
949 assert 0, 'Did not receive expected exception.'
950 fd3 = next_fd()
951 try:
952 page.bound()
953 except Exception as e:
954 print(f'Received expected exception: {e}')
955 #traceback.print_exc(file=sys.stdout)
956 assert str(e) == 'page is None'
957 else:
958 assert 0, 'Did not receive expected exception.'
959 page = None
960 fd4 = next_fd()
961 print(f'{next_fd_1=} {next_fd_2=}')
962 print(f'{fd1=} {fd2=} {fd3=} {fd4=}')
963 print(f'{document=}')
964 assert fd1 == next_fd_1
965 assert fd2 == next_fd_2 # Checks document only uses one fd.
966 assert fd3 == next_fd_1 # Checks no leaked fds after document close.
967 assert fd4 == next_fd_1 # Checks no leaked fds after failed page access.
968
969def test_xml():

Callers

nothing calls this directly

Calls 4

next_fdFunction · 0.85
page_countMethod · 0.80
boundMethod · 0.80
closeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…