cbrunet / python-poppler

Python binding to Poppler-cpp pdf library
GNU General Public License v2.0
97 stars 15 forks source link

Add binding for error functions and poppler.enable_logging(). #61

Closed bzamecnik closed 2 years ago

bzamecnik commented 2 years ago

Resolved issue #49. Suppressing error messages

So that we can disable/enable logging to stderr.

Intended usage:

# disable logging
poppler.enable_logging(False)

# enable logging to stderr again
poppler.enable_logging(True)

Passing the function pointer is wrapped inside the C++ code since it may be more difficult to pass a python function as C++ function pointer. Also the user-facing API is thus a bit simpler.


It would be nicer to have some more pythonic way to control the logging. If needed one can imagine passing a custom error function which would log the error message into a standard python logger.


TODO: I'd like to find some sample PDF with errors being output + a unit test.

cbrunet commented 2 years ago

It is possible to pass a python callback to C++... Calling python code like a logger could also be possible. However, this is something a little more complex, and you have to be careful with the GIL and reference counting..

Looks good so far. I will merge as is, as having this is better that having nothing.

bzamecnik commented 2 years ago

@cbrunet Thank you very much! As for the unit test, so far I've done manual testing and it worked as expected. Also I've got one sample PDF and I was thinking about the unit test. It might be a little bit complicated if it was ran within the same process (a bit of hacking with duplicating replacing the stderr file descriptor), so a simpler approach might be to run a subprocess and capture its output.

UPDATE: MR with the test: https://github.com/cbrunet/python-poppler/pull/62

Could you please then make a release of the package?