Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/picologging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
LogRecord,
StreamHandler,
getLevelName,
raiseExceptions,
)

__version__ = "0.9.3"
Expand Down
2 changes: 2 additions & 0 deletions src/picologging/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ INFO: int
DEBUG: int
NOTSET: int

raiseExceptions: bool

_SysExcInfoType: TypeAlias = Union[
tuple[type[BaseException], BaseException, TracebackType | None],
tuple[None, None, None],
Expand Down
2 changes: 2 additions & 0 deletions src/picologging/_picologging.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ PyMODINIT_FUNC PyInit__picologging(void)
picologging_state *state = get_picologging_state(m);
state->g_filepathCache = new FilepathCache();

PyModule_AddObject(m, "raiseExceptions", Py_NewRef(Py_False));

Py_INCREF(&LogRecordType);
Py_INCREF(&FormatStyleType);
Py_INCREF(&FormatterType);
Expand Down
13 changes: 10 additions & 3 deletions src/picologging/handler.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ PyObject* Handler_handle(Handler *self, PyObject *record) {
}

self->lock->unlock();
return result == nullptr ? nullptr : Py_True;

if (PyErr_Occurred())
return nullptr;
return result == nullptr ? Py_False : Py_True;
}

PyObject* Handler_setLevel(Handler *self, PyObject *level){
Expand Down Expand Up @@ -128,8 +131,12 @@ PyObject* Handler_close(Handler *self){
}

PyObject* Handler_handleError(Handler *self, PyObject *record){
// TODO: Develop this behaviour further.
PyErr_Print();
PyObject* mod = PICOLOGGING_MODULE(); // borrowed reference
PyObject* modDict = PyModule_GetDict(mod); // borrowed reference
PyObject* raiseExceptions = PyDict_GetItemString(modDict, "raiseExceptions"); // PyDict_GetItemString returns a borrowed reference
if (PyBool_Check(raiseExceptions) && PyErr_Occurred()) {
PyErr_Print();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to format the exception probably the same way the Formatter does, with all frames.

}
Py_RETURN_NONE;
}

Expand Down
1 change: 1 addition & 0 deletions src/picologging/handler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ PyObject* Handler_handle(Handler *self, PyObject *record);
PyObject* Handler_setLevel(Handler *self, PyObject *level);
PyObject* Handler_setFormatter(Handler *self, PyObject *formatter);
PyObject* Handler_format(Handler *self, PyObject *record);
PyObject* Handler_handleError(Handler *self, PyObject *record);
PyObject* Handler_acquire(Handler *self);
PyObject* Handler_release(Handler *self);

Expand Down
2 changes: 1 addition & 1 deletion src/picologging/streamhandler.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ PyObject* StreamHandler_emit(StreamHandler* self, PyObject* const* args, Py_ssiz
Py_XDECREF(writeResult);
Py_RETURN_NONE;
error:
// TODO: #4 handle error path (see handleError(record))
Handler_handleError(&self->handler, args[0]);
Py_XDECREF(msg);
return nullptr;
}
Expand Down
18 changes: 14 additions & 4 deletions tests/unit/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from utils import filter_gc

import picologging
from picologging import LogRecord


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
Expand Down Expand Up @@ -131,13 +132,22 @@ def format(self, record):
assert handler.format(record) == "foo"


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
def test_handle_error():
handler = picologging.Handler()
def test_handle_error(capsys):
picologging.raiseExceptions = True

class BadStream:
def write(self, data):
raise RuntimeError("bad stream")

handler = picologging.StreamHandler(BadStream())
record = picologging.LogRecord(
"test", picologging.INFO, __file__, 1, "test", (), None, None, None
)
assert not handler.handleError(record)
handler.handle(record)
cap = capsys.readouterr()

assert cap.out == ""
assert cap.err.startswith("Traceback (most recent call last)")


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
Expand Down
10 changes: 0 additions & 10 deletions tests/unit/test_streamhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ def test_stream_handler_bad_init_args():
picologging.StreamHandler(dog=1)


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
def test_stream_handler_invalid_stream_type():
handler = picologging.StreamHandler("potato")
record = picologging.LogRecord(
"test", picologging.INFO, __file__, 1, "test", (), None, None, None
)
with pytest.raises(AttributeError):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't raise any errors with standard logging module. And also was not fixed in this PR.

handler.handle(record)


@pytest.mark.limit_leaks("192B", filter_fn=filter_gc)
def test_non_flushable_stream():
class TestStream:
Expand Down