From e2d727632367e174a95f73997e00fdff538c203d Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Tue, 13 Aug 2024 12:51:10 +0200 Subject: [PATCH] Use fmt instead of Sprintf --- src/nrnpython/nrnpy_hoc.cpp | 73 ++++++++++++++++--------------------- src/nrnpython/nrnpy_nrn.cpp | 32 ++++++++-------- src/nrnpython/nrnpy_p2h.cpp | 22 +++++------ 3 files changed, 58 insertions(+), 69 deletions(-) diff --git a/src/nrnpython/nrnpy_hoc.cpp b/src/nrnpython/nrnpy_hoc.cpp index cb10da5d47..d89306bccb 100644 --- a/src/nrnpython/nrnpy_hoc.cpp +++ b/src/nrnpython/nrnpy_hoc.cpp @@ -1,3 +1,5 @@ +#include + #include "ivocvect.h" #include "neuron/container/data_handle.hpp" #include "nrniv_mf.h" @@ -471,9 +473,8 @@ static Symbol* getsym(char* name, Object* ho, int fail) { sym = 0; } if (!sym && fail) { - char e[200]; - Sprintf(e, "'%s' is not a defined hoc variable name.", name); - PyErr_SetString(PyExc_LookupError, e); + PyErr_SetString(PyExc_LookupError, + fmt::format("'{}' is not a defined hoc variable name.", name).c_str()); } return sym; } @@ -622,7 +623,7 @@ PyObject* nrnpy_hoc_pop(const char* mes) { hoc_tobj_unref(d); break; default: - printf("nrnpy_hoc_pop error: stack type = %d\n", hoc_stack_type()); + fmt::print("nrnpy_hoc_pop error: stack type = {}\n", hoc_stack_type()); } return result; } @@ -670,7 +671,7 @@ static int set_final_from_stk(PyObject* po) { } break; default: - printf("set_final_from_stk() error: stack type = %d\n", hoc_stack_type()); + fmt::print("set_final_from_stk() error: stack type = {}\n", hoc_stack_type()); err = 1; break; } @@ -1094,11 +1095,10 @@ static PyObject* hocobj_getattr(PyObject* subself, PyObject* pyname) { result = cpp2refstr(cpp); return result; } else if (sym->type != VAR && sym->type != RANGEVAR && sym->type != VARALIAS) { - char buf[200]; - Sprintf(buf, - "Hoc pointer error, %s is not a hoc variable or range variable or strdef", - sym->name); - PyErr_SetString(PyExc_TypeError, buf); + auto buf = fmt::format( + "Hoc pointer error, {} is not a hoc variable or range variable or strdef", + sym->name); + PyErr_SetString(PyExc_TypeError, buf.c_str()); return NULL; } else { isptr = 1; @@ -1469,9 +1469,8 @@ static int hocobj_setattro(PyObject* subself, PyObject* pyname, PyObject* value) Py_DECREF(po); return set_final_from_stk(value); } else { - char e[200]; - Sprintf(e, "'%s' requires subscript for assignment", n); - PyErr_SetString(PyExc_TypeError, e); + auto e = fmt::format("'{}' requires subscript for assignment", n); + PyErr_SetString(PyExc_TypeError, e.c_str()); Py_DECREF(po); return -1; } @@ -1616,13 +1615,11 @@ static int araychk(Arrayinfo* a, PyHocObject* po, int ix) { if (ix < 0 || n <= ix) { // printf("ix=%d nsub=%d nindex=%d sub[nindex]=%d\n", ix, a->nsub, // po->nindex_, a->sub[po->nindex_]); - char e[200]; - Sprintf(e, - "%s%s%s", - po->ho_ ? hoc_object_name(po->ho_) : "", - (po->ho_ && po->sym_) ? "." : "", - po->sym_ ? po->sym_->name : ""); - PyErr_SetString(PyExc_IndexError, e); + auto e = fmt::format("{}{}{}", + po->ho_ ? hoc_object_name(po->ho_) : "", + (po->ho_ && po->sym_) ? "." : "", + po->sym_ ? po->sym_->name : ""); + PyErr_SetString(PyExc_IndexError, e.c_str()); return -1; } return 0; @@ -1897,9 +1894,8 @@ static PyObject* hocobj_getitem(PyObject* self, Py_ssize_t ix) { ix += vector_capacity(hv); } if (ix < 0 || ix >= vector_capacity(hv)) { - char e[200]; - Sprintf(e, "%s", hoc_object_name(po->ho_)); - PyErr_SetString(PyExc_IndexError, e); + std::string e = hoc_object_name(po->ho_); + PyErr_SetString(PyExc_IndexError, e.c_str()); return NULL; } else { return PyFloat_FromDouble(vector_vec(hv)[ix]); @@ -1910,9 +1906,8 @@ static PyObject* hocobj_getitem(PyObject* self, Py_ssize_t ix) { ix += hl->count(); } if (ix < 0 || ix >= hl->count()) { - char e[200]; - Sprintf(e, "%s", hoc_object_name(po->ho_)); - PyErr_SetString(PyExc_IndexError, e); + std::string e = hoc_object_name(po->ho_); + PyErr_SetString(PyExc_IndexError, e.c_str()); return NULL; } else { return nrnpy_ho2po(hl->object(ix)); @@ -1936,15 +1931,14 @@ static PyObject* hocobj_getitem(PyObject* self, Py_ssize_t ix) { return nrnpy_ho2po(ob); } } - char e[200]; - Sprintf(e, "%s[%ld] instance does not exist", po->sym_->name, ix); - PyErr_SetString(PyExc_IndexError, e); + auto e = fmt::format("{}[{}] instance does not exist", po->sym_->name, ix); + PyErr_SetString(PyExc_IndexError, e.c_str()); return NULL; } if (po->type_ != PyHoc::HocArray && po->type_ != PyHoc::HocArrayIncomplete) { - char e[200]; - Sprintf(e, "unsubscriptable object, type %d\n", po->type_); - PyErr_SetString(PyExc_TypeError, e); + auto e = fmt::format("unsubscriptable object, type {:d}\n", + static_casttype_)>>(po->type_)); + PyErr_SetString(PyExc_TypeError, e.c_str()); return NULL; } Arrayinfo* a = hocobj_aray(po->sym_, po->ho_); @@ -2503,9 +2497,8 @@ static char* double_array_interface(PyObject* po, long& stride) { inline double pyobj_to_double_or_fail(PyObject* obj, long obj_id) { if (!PyNumber_Check(obj)) { - char buf[50]; - Sprintf(buf, "item %d is not a valid number", obj_id); - hoc_execerror(buf, 0); + auto buf = fmt::format("item {} is not a valid number", obj_id); + hoc_execerror(buf.c_str(), nullptr); } return PyFloat_AsDouble(obj); } @@ -2820,18 +2813,16 @@ static Object** nrnpy_vec_to_python(void* v) { for (int i = 0; i < size; ++i) { PyObject* pn = PyFloat_FromDouble(x[i]); if (!pn || PyList_SetItem(po, i, pn) == -1) { - char buf[50]; - Sprintf(buf, "%d of %d", i, size); - hoc_execerror("Could not set a Python Sequence item", buf); + auto buf = fmt::format("{} of {}", i, size); + hoc_execerror("Could not set a Python Sequence item", buf.c_str()); } } } else { // assume PySequence_SetItem works for (int i = 0; i < size; ++i) { PyObject* pn = PyFloat_FromDouble(x[i]); if (!pn || PySequence_SetItem(po, i, pn) == -1) { - char buf[50]; - Sprintf(buf, "%d of %d", i, size); - hoc_execerror("Could not set a Python Sequence item", buf); + auto buf = fmt::format("{} of {}", i, size); + hoc_execerror("Could not set a Python Sequence item", buf.c_str()); } Py_DECREF(pn); } diff --git a/src/nrnpython/nrnpy_nrn.cpp b/src/nrnpython/nrnpy_nrn.cpp index 17ff995ffe..7e1ba17314 100644 --- a/src/nrnpython/nrnpy_nrn.cpp +++ b/src/nrnpython/nrnpy_nrn.cpp @@ -1,3 +1,5 @@ +#include + #include "neuron/container/data_handle.hpp" #include "neuron/container/generic_data_handle.hpp" #include "nrn_ansi.h" @@ -974,9 +976,8 @@ static PyObject* pyseg_repr_safe(PyObject* p) { static PyObject* hoc_internal_name(NPySecObj* self) { PyObject* result; - char buf[256]; - Sprintf(buf, "__nrnsec_%p", self->sec_); - result = PyString_FromString(buf); + auto buf = fmt::format("__nrnsec_{}", fmt::ptr(self->sec_)); + result = PyString_FromString(buf.c_str()); return result; } @@ -1410,9 +1411,8 @@ static PyObject* NPyRangeVar_name(NPyRangeVar* self) { PyObject* result = NULL; if (self->sym_) { if (self->isptr_) { - char buf[256]; - Sprintf(buf, "_ref_%s", self->sym_->name); - result = PyString_FromString(buf); + auto buf = fmt::format("_ref_{}", self->sym_->name); + result = PyString_FromString(buf.c_str()); } else { result = PyString_FromString(self->sym_->name); } @@ -1924,15 +1924,15 @@ static Object** pp_get_segment(void* vptr) { } static void rv_noexist(Section* sec, const char* n, double x, int err) { - char buf[200]; + std::string buf{}; if (err == 2) { - Sprintf(buf, "%s was not made to point to anything at %s(%g)", n, secname(sec), x); + buf = fmt::format("{} was not made to point to anything at {}({})", n, secname(sec), x); } else if (err == 1) { - Sprintf(buf, "%s, the mechanism does not exist at %s(%g)", n, secname(sec), x); + buf = fmt::format("{}, the mechanism does not exist at {}({})", n, secname(sec), x); } else { - Sprintf(buf, "%s does not exist at %s(%g)", n, secname(sec), x); + buf = fmt::format("{} does not exist at {}({})", n, secname(sec), x); } - PyErr_SetString(PyExc_AttributeError, buf); + PyErr_SetString(PyExc_AttributeError, buf.c_str()); } static NPyRangeVar* rvnew(Symbol* sym, NPySecObj* sec, double x) { @@ -2356,9 +2356,8 @@ static int segment_setattro(NPySegObj* self, PyObject* pyname, PyObject* value) } else if ((rv = PyDict_GetItemString(rangevars_, n)) != NULL) { sym = ((NPyRangeVar*) rv)->sym_; if (is_array(*sym)) { - char s[200]; - Sprintf(s, "%s needs an index for assignment", sym->name); - PyErr_SetString(PyExc_IndexError, s); + auto s = fmt::format("{} needs an index for assignment", sym->name); + PyErr_SetString(PyExc_IndexError, s.c_str()); return -1; } else { int errp; @@ -2627,14 +2626,13 @@ neuron::container::generic_data_handle* nrnpy_setpointer_helper(PyObject* pyname } NPyMechObj* m = (NPyMechObj*) mech; Symbol* msym = memb_func[m->type_].sym; - char buf[200]; Py2NRNString name(pyname); char* n = name.c_str(); if (!n) { return nullptr; } - Sprintf(buf, "%s_%s", n, msym->name); - Symbol* sym = var_find_in_mech(msym, buf); + auto buf = fmt::format("{}_{}", n, msym->name); + Symbol* sym = var_find_in_mech(msym, buf.c_str()); if (!sym || sym->type != RANGEVAR || sym->subtype != NRNPOINTER) { return nullptr; } diff --git a/src/nrnpython/nrnpy_p2h.cpp b/src/nrnpython/nrnpy_p2h.cpp index 0d765bb6a5..d268ca437a 100644 --- a/src/nrnpython/nrnpy_p2h.cpp +++ b/src/nrnpython/nrnpy_p2h.cpp @@ -49,7 +49,7 @@ static void call_python_with_section(Object* pyact, Section* sec) { if (!r) { char* mes = nrnpyerr_str(); if (mes) { - Fprintf(stderr, "%s\n", mes); + std::cerr << mes << std::endl; free(mes); hoc_execerror("Call of Python Callable failed", NULL); } @@ -128,7 +128,7 @@ printf("\nreturn %p\n", p); if (!p) { char* mes = nrnpyerr_str(); if (mes) { - Fprintf(stderr, "%s\n", mes); + std::cerr << mes << std::endl; free(mes); hoc_execerror("Call of Python Callable failed", NULL); } @@ -204,7 +204,7 @@ static void py2n_component(Object* ob, Symbol* sym, int nindex, int isfunc) { Py_XDECREF(tail); Py_XDECREF(head); if (mes) { - Fprintf(stderr, "%s\n", mes); + std::cerr << mes << std::endl; free(mes); hoc_execerror("PyObject method call failed:", sym->name); } @@ -385,7 +385,7 @@ static double praxis_efun(Object* ho, Object* v) { if (!r) { char* mes = nrnpyerr_str(); if (mes) { - Fprintf(stderr, "%s\n", mes); + std::cerr << mes << std::endl; free(mes); hoc_execerror("Call of Python Callable failed in praxis_efun", NULL); } @@ -435,7 +435,7 @@ static int hoccommand_exec_strret(Object* ho, char* buf, int size) { } else { char* mes = nrnpyerr_str(); if (mes) { - Fprintf(stderr, "%s\n", mes); + std::cerr << mes << std::endl; free(mes); hoc_execerror("Python Callback failed", 0); } @@ -459,7 +459,7 @@ static void grphcmdtool(Object* ho, int type, double x, double y, int key) { if (!r) { char* mes = nrnpyerr_str(); if (mes) { - Fprintf(stderr, "%s\n", mes); + std::cerr << mes << std::endl; free(mes); hoc_execerror("Python Callback failed", 0); } @@ -529,7 +529,7 @@ static double func_call(Object* ho, int narg, int* err) { if (!err || *err) { char* mes = nrnpyerr_str(); if (mes) { - Fprintf(stderr, "%s\n", mes); + std::cerr << mes << std::endl; free(mes); } if (PyErr_Occurred()) { @@ -712,18 +712,18 @@ static char* nrnpyerr_str() { if (py_str) { Py2NRNString mes(py_str); if (mes.err()) { - Fprintf(stderr, "nrnperr_str: Py2NRNString failed\n"); + std::cerr << "nrnperr_str: Py2NRNString failed" << std::endl; } else { cmes = strdup(mes.c_str()); if (!cmes) { - Fprintf(stderr, "nrnpyerr_str: strdup failed\n"); + std::cerr << "nrnpyerr_str: strdup failed" << std::endl; } } } if (!py_str) { PyErr_Print(); - Fprintf(stderr, "nrnpyerr_str failed\n"); + std::cerr << "nrnpyerr_str failed" << std::endl; } Py_XDECREF(module_name); @@ -769,7 +769,7 @@ std::vector call_picklef(const std::vector& fname, int narg) { if (!result) { char* mes = nrnpyerr_str(); if (mes) { - Fprintf(stderr, "%s\n", mes); + std::cerr << mes << std::endl; free(mes); hoc_execerror("PyObject method call failed:", NULL); }