-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit refactors a part of the NRN Python bindings to use `nanobind` objects instead of `Py_DECREF`. The purpose is to simplify the DECREFing logic on error paths; and the risk of leaking when exceptions are thrown. As part of the refactoring, if needed, the scope of certain variables might be reduced or a given a new name. Additionally, NULL pointers are replaced with `nullptr`. This commit doesn't intentionally change reference counts.
- Loading branch information
Showing
7 changed files
with
156 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include "nrnpy_utils.h" | ||
|
||
#include <tuple> | ||
#include <nanobind/nanobind.h> | ||
|
||
namespace nb = nanobind; | ||
|
||
inline std::tuple<nb::object, nb::object, nb::object> fetch_pyerr() { | ||
PyObject* ptype = NULL; | ||
PyObject* pvalue = NULL; | ||
PyObject* ptraceback = NULL; | ||
PyErr_Fetch(&ptype, &pvalue, &ptraceback); | ||
|
||
return std::make_tuple(nb::steal(ptype), nb::steal(pvalue), nb::steal(ptraceback)); | ||
} | ||
|
||
|
||
Py2NRNString::Py2NRNString(PyObject* python_string, bool disable_release) { | ||
disable_release_ = disable_release; | ||
str_ = NULL; | ||
if (PyUnicode_Check(python_string)) { | ||
auto py_bytes = nb::steal(PyUnicode_AsASCIIString(python_string)); | ||
if (py_bytes) { | ||
str_ = strdup(PyBytes_AsString(py_bytes.ptr())); | ||
if (!str_) { // errno is ENOMEM | ||
PyErr_SetString(PyExc_MemoryError, "strdup in Py2NRNString"); | ||
} | ||
} | ||
} else if (PyBytes_Check(python_string)) { | ||
str_ = strdup(PyBytes_AsString(python_string)); | ||
// assert(strlen(str_) == PyBytes_Size(python_string)) | ||
// not checking for embedded '\0' | ||
if (!str_) { // errno is ENOMEM | ||
PyErr_SetString(PyExc_MemoryError, "strdup in Py2NRNString"); | ||
} | ||
} else { // Neither Unicode or PyBytes | ||
PyErr_SetString(PyExc_TypeError, "Neither Unicode or PyBytes"); | ||
} | ||
} | ||
|
||
void Py2NRNString::set_pyerr(PyObject* type, const char* message) { | ||
nb::object err_type; | ||
nb::object err_value; | ||
nb::object err_traceback; | ||
|
||
if (err()) { | ||
std::tie(err_type, err_value, err_traceback) = fetch_pyerr(); | ||
} | ||
if (err_value && err_type) { | ||
auto umes = nb::steal( | ||
PyUnicode_FromFormat("%s (Note: %S: %S)", message, err_type.ptr(), err_value.ptr())); | ||
PyErr_SetObject(type, umes.ptr()); | ||
} else { | ||
PyErr_SetString(type, message); | ||
} | ||
} | ||
|
||
char* Py2NRNString::get_pyerr() { | ||
if (err()) { | ||
auto [ptype, pvalue, ptraceback] = fetch_pyerr(); | ||
if (pvalue) { | ||
auto pstr = nb::steal(PyObject_Str(pvalue.ptr())); | ||
if (pstr) { | ||
const char* err_msg = PyUnicode_AsUTF8(pstr.ptr()); | ||
if (err_msg) { | ||
str_ = strdup(err_msg); | ||
} else { | ||
str_ = strdup("get_pyerr failed at PyUnicode_AsUTF8"); | ||
} | ||
} else { | ||
str_ = strdup("get_pyerr failed at PyObject_Str"); | ||
} | ||
} else { | ||
str_ = strdup("get_pyerr failed at PyErr_Fetch"); | ||
} | ||
} | ||
PyErr_Clear(); // in case could not turn pvalue into c_str. | ||
return str_; | ||
} | ||
|
||
char* PyErr2NRNString::get_pyerr() { | ||
if (PyErr_Occurred()) { | ||
auto [ptype, pvalue, ptraceback] = fetch_pyerr(); | ||
if (pvalue) { | ||
auto pstr = nb::steal(PyObject_Str(pvalue.ptr())); | ||
if (pstr) { | ||
const char* err_msg = PyUnicode_AsUTF8(pstr.ptr()); | ||
if (err_msg) { | ||
str_ = strdup(err_msg); | ||
} else { | ||
str_ = strdup("get_pyerr failed at PyUnicode_AsUTF8"); | ||
} | ||
} else { | ||
str_ = strdup("get_pyerr failed at PyObject_Str"); | ||
} | ||
} else { | ||
str_ = strdup("get_pyerr failed at PyErr_Fetch"); | ||
} | ||
} | ||
PyErr_Clear(); // in case could not turn pvalue into c_str. | ||
return str_; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.