Skip to content

Commit

Permalink
MAINT: Replace warning with logging.error
Browse files Browse the repository at this point in the history
Closes #2354
  • Loading branch information
MartinThoma committed Dec 28, 2023
1 parent a91e9f6 commit 8484b1e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion docs/user/suppress-warnings.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ pypdf makes use of 3 mechanisms to show that something went wrong:
* **Log messages** are informative messages that can be used for post-mortem
analysis. Most of the time, users can ignore them. They come in different
*levels*, such as info / warning / error indicating the severity.
Examples are non-standard compliant PDF files which pypdf can deal with.
Examples are non-standard compliant PDF files which pypdf can deal with or
a missing implementation that leads to a part of the text not being extracted.
* **Warnings** are avoidable issues, such as using deprecated classes /
functions / parameters. Another example is missing capabilities of pypdf.
In those cases, pypdf users should adjust their code. Warnings
Expand Down
13 changes: 4 additions & 9 deletions pypdf/_cmap.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import warnings
from binascii import unhexlify
from math import ceil
from typing import Any, Dict, List, Tuple, Union, cast

from ._codecs import adobe_glyphs, charset_encoding
from ._utils import b_, logger_warning
from .errors import PdfReadWarning
from ._utils import b_, logger_error, logger_warning
from .generic import (
DecodedStreamObject,
DictionaryObject,
Expand Down Expand Up @@ -180,18 +178,15 @@ def parse_encoding(
else:
raise Exception("not found")
except Exception:
warnings.warn(
f"Advanced encoding {enc} not implemented yet",
PdfReadWarning,
)
logger_error(f"Advanced encoding {enc} not implemented yet", __name__)
encoding = enc
elif isinstance(enc, DictionaryObject) and "/BaseEncoding" in enc:
try:
encoding = charset_encoding[cast(str, enc["/BaseEncoding"])].copy()
except Exception:
warnings.warn(
logger_error(
f"Advanced encoding {encoding} not implemented yet",
PdfReadWarning,
__name__,
)
encoding = charset_encoding["/StandardCoding"].copy()
else:
Expand Down
12 changes: 12 additions & 0 deletions pypdf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,18 @@ def deprecation_no_replacement(name: str, removed_in: str) -> None:
deprecation(DEPR_MSG_NO_REPLACEMENT_HAPPENED.format(name, removed_in))


def logger_error(msg: str, src: str) -> None:
"""
Use this instead of logger.error directly.
That allows people to overwrite it more easily.
See the docs on when to use which:
https://pypdf.readthedocs.io/en/latest/user/suppress-warnings.html
"""
logging.getLogger(src).warning(msg)


def logger_warning(msg: str, src: str) -> None:
"""
Use this instead of logger.warning directly.
Expand Down

0 comments on commit 8484b1e

Please sign in to comment.