Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defusedxml does not work with rich library to print exceptions #105

Open
yauhen-sobaleu opened this issue Dec 13, 2023 · 0 comments
Open

Comments

@yauhen-sobaleu
Copy link

yauhen-sobaleu commented Dec 13, 2023

Hello!

When using defused xml with rich traceback formatter the library always throws a weird error complaining about the type of exception. Everything is ok when using import xml.etree.cElementTree as xmlctree.

import io

import defusedxml.ElementTree as xmlctree
import structlog

rich_tracebacks_fmt = structlog.dev.RichTracebackFormatter()

structlog.configure(
    cache_logger_on_first_use=True,
    processors=[
        # show log level
        structlog.stdlib.add_log_level,
        # add time to every log entry
        structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M.%S"),
        # render logs in pretty format (unstructured) + beautiful tracebacks
        structlog.dev.ConsoleRenderer(exception_formatter=rich_tracebacks_fmt),
    ],
)


logger = structlog.get_logger()

try:
    for _, elem in xmlctree.iterparse(io.BytesIO(b"sdsds")):
        print(elem)
except Exception as e:
    logger.exception(str(e))

traceback

> python feed_loader/test.py
Traceback (most recent call last):
  File "project/.venv/lib/python3.11/xml/etree/ElementTree.py", line 1716, in close
    self.parser.Parse(b"", True) # end of data
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
xml.parsers.expat.ExpatError: syntax error: line 1, column 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "project/feed_loader/test.py", line 24, in <module>
    for _, elem in xmlctree.iterparse(io.BytesIO(b"sdsds")):
  File "project/.venv/lib/python3.11/xml/etree/ElementTree.py", line 1255, in iterator
    root = pullparser._close_and_return_root()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "project/.venv/lib/python3.11/xml/etree/ElementTree.py", line 1298, in _close_and_return_root
    root = self._parser.close()
           ^^^^^^^^^^^^^^^^^^^^
  File "project/.venv/lib/python3.11/xml/etree/ElementTree.py", line 1718, in close
    self._raiseerror(v)
  File "project/.venv/lib/python3.11/xml/etree/ElementTree.py", line 1618, in _raiseerror
    raise err
xml.etree.ElementTree.ParseError: syntax error: line 1, column 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "project/feed_loader/test.py", line 27, in <module>
    logger.exception(str(e))
  File "project/.venv/lib/python3.11/site-packages/structlog/_log_levels.py", line 88, in exception
    return self.error(event, *args, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "project/.venv/lib/python3.11/site-packages/structlog/_log_levels.py", line 168, in meth
    return self._proxy_to_logger(name, event, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "project/.venv/lib/python3.11/site-packages/structlog/_base.py", line 222, in _proxy_to_logger
    args, kw = self._process_event(method_name, event, event_kw)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "project/.venv/lib/python3.11/site-packages/structlog/_base.py", line 171, in _process_event
    event_dict = proc(self._logger, method_name, event_dict)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "project/.venv/lib/python3.11/site-packages/structlog/dev.py", line 505, in __call__
    self._exception_formatter(sio, exc_info)
  File "project/.venv/lib/python3.11/site-packages/structlog/dev.py", line 217, in __call__
    Console(file=sio, color_system=self.color_system).print(
  File "project/.venv/lib/python3.11/site-packages/rich/console.py", line 1700, in print
    extend(render(renderable, render_options))
  File "project/.venv/lib/python3.11/site-packages/rich/console.py", line 1332, in render
    for render_output in iter_render:
  File "project/.venv/lib/python3.11/site-packages/rich/traceback.py", line 545, in __rich_console__
    highlighter(stack.syntax_error.msg),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "project/.venv/lib/python3.11/site-packages/rich/highlighter.py", line 37, in __call__
    raise TypeError(f"str or Text instance required, not {text!r}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant