Skip to content

Commit

Permalink
Fix errors caused by un-picklable Exception when "enqueue=True" (#905)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncoudene committed Jul 10, 2023
1 parent 2a35b87 commit c102e0a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion loguru/_recattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ def __reduce__(self):
# we remove the value in case or error. As an optimization, we could have re-used the
# dumped value during unpickling, but this requires using "pickle.loads()" which is
# flagged as insecure by some security tools.
# __reduce__ function does not alway raise PickleError. Multidict, for example, raise
# TypeError. To manage all the cases, we catch Exception.
try:
pickle.dumps(self.value)
except pickle.PickleError:
except Exception:
return (RecordException, (self.type, None, None))
else:
return (RecordException, (self.type, self.value, None))
42 changes: 42 additions & 0 deletions tests/test_add_option_enqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def __setstate__(self, state):
pass


class NotPicklableTypeError:
def __getstate__(self):
raise TypeError("You shall not serialize me!")

def __setstate__(self, state):
pass


class NotUnpicklable:
def __getstate__(self):
return "..."
Expand Down Expand Up @@ -89,6 +97,24 @@ def test_caught_exception_queue_put(writer, capsys):
assert lines[-1] == "--- End of logging error ---"


def test_caught_exception_queue_put_typerror(writer, capsys):
logger.add(writer, enqueue=True, catch=True, format="{message}")

logger.info("It's fine")
logger.bind(broken=NotPicklableTypeError()).info("Bye bye...")
logger.info("It's fine again")
logger.remove()

out, err = capsys.readouterr()
lines = err.strip().splitlines()
assert writer.read() == "It's fine\nIt's fine again\n"
assert out == ""
assert lines[0] == "--- Logging error in Loguru Handler #0 ---"
assert re.match(r"Record was: \{.*Bye bye.*\}", lines[1])
assert lines[-2].endswith("TypeError: You shall not serialize me!")
assert lines[-1] == "--- End of logging error ---"


def test_caught_exception_queue_get(writer, capsys):
logger.add(writer, enqueue=True, catch=True, format="{message}")

Expand Down Expand Up @@ -140,6 +166,22 @@ def test_not_caught_exception_queue_put(writer, capsys):
assert err == ""


def test_not_caught_exception_queue_put_typeerror(writer, capsys):
logger.add(writer, enqueue=True, catch=False, format="{message}")

logger.info("It's fine")

with pytest.raises(TypeError, match=r"You shall not serialize me!"):
logger.bind(broken=NotPicklableTypeError()).info("Bye bye...")

logger.remove()

out, err = capsys.readouterr()
assert writer.read() == "It's fine\n"
assert out == ""
assert err == ""


def test_not_caught_exception_queue_get(writer, capsys):
logger.add(writer, enqueue=True, catch=False, format="{message}")

Expand Down

0 comments on commit c102e0a

Please sign in to comment.