Skip to content

Instantly share code, notes, and snippets.

@frontdevops
Created October 26, 2022 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frontdevops/9351a8e2b093354a1d598cb507aa70ef to your computer and use it in GitHub Desktop.
Save frontdevops/9351a8e2b093354a1d598cb507aa70ef to your computer and use it in GitHub Desktop.
static PyObject *
BaseException_add_note(PyObject *self, PyObject *note)
{
if (!PyUnicode_Check(note)) {
PyErr_Format(PyExc_TypeError,
"note must be a str, not '%s'",
Py_TYPE(note)->tp_name);
return NULL;
}
if (!PyObject_HasAttr(self, &_Py_ID(__notes__))) {
PyObject *new_notes = PyList_New(0);
if (new_notes == NULL) {
return NULL;
}
if (PyObject_SetAttr(self, &_Py_ID(__notes__), new_notes) < 0) {
Py_DECREF(new_notes);
return NULL;
}
Py_DECREF(new_notes);
}
PyObject *notes = PyObject_GetAttr(self, &_Py_ID(__notes__));
if (notes == NULL) {
return NULL;
}
if (!PyList_Check(notes)) {
Py_DECREF(notes);
PyErr_SetString(PyExc_TypeError, "Cannot add note: __notes__ is not a list");
return NULL;
}
if (PyList_Append(notes, note) < 0) {
Py_DECREF(notes);
return NULL;
}
Py_DECREF(notes);
Py_RETURN_NONE;
}
PyDoc_STRVAR(add_note_doc,
"Exception.add_note(note) --\n\
add a note to the exception");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment