pymupdf / PyMuPDF

PyMuPDF is a high performance Python library for data extraction, analysis, conversion & manipulation of PDF (and other) documents.
https://pymupdf.readthedocs.io
GNU Affero General Public License v3.0
4.66k stars 460 forks source link

Page annotation 'NoneType' object is not callable as weakref only while debugging #2182

Closed vielfarbig closed 1 year ago

vielfarbig commented 1 year ago

Describe the bug (mandatory)

The following code runs fine with normal execution, but it gives an Exception for "'NoneType' object is not callable" while debugging when stepping through the for loop.

I could think that it is related to https://github.com/pymupdf/PyMuPDF/issues/1605 but not the same. I also found no alternative giving an Exception with debugging.

To Reproduce (mandatory)

Explain the steps to reproduce the behavior, For example, include a minimal code snippet, example files, etc.

Step through the following for loop with debugger:

import fitz
doc = fitz.open("testfile.pdf")
page = doc[0]
for annot in page.annots():
    print(annot)

I've used a simple pdf, generated with LibreOffice with "export as pdf" and Okular (pdf viewer/editor) to add the rect annotation: testfile.pdf

It gives the following Exception:

'Polygon' annotation on page 0 of testfile.pdf
'Polygon' annotation on page 0 of testfile.pdf
Exception ignored in: <function Page.__del__ at 0x7fd2291c9700>
Traceback (most recent call last):
  File "/home/user/extract_invoices/venv/lib/python3.8/site-packages/fitz/fitz.py", line 6772, in __del__
  File "/home/user/extract_invoices/venv/lib/python3.8/site-packages/fitz/fitz.py", line 6759, in _erase
  File "/home/user/extract_invoices/venv/lib/python3.8/site-packages/fitz/fitz.py", line 6747, in _reset_annot_refs
  File "/usr/lib/python3.8/weakref.py", line 241, in values
TypeError: 'NoneType' object is not callable

Expected behavior (optional)

Describe what you expected to happen (if not obvious).

Give not the Exception, but print only the annotations while debugging as in normal execution

Screenshots (optional)

If applicable, add screenshots to help explain your problem.

Your configuration (mandatory)

For example, the output of print(sys.version, "\n", sys.platform, "\n", fitz.__doc__) would be sufficient (for the first two bullets).

>>> print(sys.version, "\n", sys.platform, "\n", fitz.__doc__)
3.8.10 (default, Nov 14 2022, 12:59:47) 
[GCC 9.4.0] 
 linux 

PyMuPDF 1.21.1: Python bindings for the MuPDF 1.21.1 library.
Version date: 2022-12-13 00:00:01.
Built for Python 3.8 on linux (64-bit).

PyMuPDF newly installed with pip install PyMuPDF on Ubuntu 20.04.5

Using PyCharm 2022.3 (Professional Edition) (Build #PY-223.7571.203) Debugger

Additional context (optional)

Add any other context about the problem here.

julian-smith-artifex-com commented 1 year ago

Apologies for not replying earlier.

This looks more like a problem in the PyCharm debugger, than in PyMuPDF itself.

Perhaps the debugger does not work well with the SWIG-generated code that PyMuPDF uses.

julian-smith-artifex-com commented 1 year ago

I've just tried stepping through the example with the pdb.py python debugger, and it worked fine.

vielfarbig commented 1 year ago

Apologies for not replying earlier.

No problem.

I've just tried stepping through the example with the pdb.py python debugger, and it worked fine.

Ok, than its probably due to PyCharm.

I would also say, keep it in mind, if running into something similar. Also I don't need it to be 'fixed' at the moment, so its fine.