Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fmt instead of Sprintf #3041

Closed
wants to merge 2 commits into from
Closed
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
73 changes: 32 additions & 41 deletions src/nrnpython/nrnpy_hoc.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <fmt/format.h>

#include "ivocvect.h"
#include "neuron/container/data_handle.hpp"
#include "nrniv_mf.h"
Expand Down Expand Up @@ -471,9 +473,8 @@
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());

Check warning on line 477 in src/nrnpython/nrnpy_hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_hoc.cpp#L476-L477

Added lines #L476 - L477 were not covered by tests
}
return sym;
}
Expand Down Expand Up @@ -622,7 +623,7 @@
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());

Check warning on line 626 in src/nrnpython/nrnpy_hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_hoc.cpp#L626

Added line #L626 was not covered by tests
}
return result;
}
Expand Down Expand Up @@ -670,7 +671,7 @@
}
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());

Check warning on line 674 in src/nrnpython/nrnpy_hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_hoc.cpp#L674

Added line #L674 was not covered by tests
err = 1;
break;
}
Expand Down Expand Up @@ -1094,11 +1095,10 @@
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());

Check warning on line 1101 in src/nrnpython/nrnpy_hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_hoc.cpp#L1100-L1101

Added lines #L1100 - L1101 were not covered by tests
return NULL;
} else {
isptr = 1;
Expand Down Expand Up @@ -1469,9 +1469,8 @@
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());

Check warning on line 1473 in src/nrnpython/nrnpy_hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_hoc.cpp#L1472-L1473

Added lines #L1472 - L1473 were not covered by tests
Py_DECREF(po);
return -1;
}
Expand Down Expand Up @@ -1616,13 +1615,11 @@
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());

Check warning on line 1622 in src/nrnpython/nrnpy_hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_hoc.cpp#L1619-L1622

Added lines #L1619 - L1622 were not covered by tests
return -1;
}
return 0;
Expand Down Expand Up @@ -1897,9 +1894,8 @@
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]);
Expand All @@ -1910,9 +1906,8 @@
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));
Expand All @@ -1936,15 +1931,14 @@
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());

Check warning on line 1935 in src/nrnpython/nrnpy_hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_hoc.cpp#L1934-L1935

Added lines #L1934 - L1935 were not covered by tests
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_cast<std::underlying_type_t<decltype(po->type_)>>(po->type_));
PyErr_SetString(PyExc_TypeError, e.c_str());

Check warning on line 1941 in src/nrnpython/nrnpy_hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_hoc.cpp#L1940-L1941

Added lines #L1940 - L1941 were not covered by tests
return NULL;
}
Arrayinfo* a = hocobj_aray(po->sym_, po->ho_);
Expand Down Expand Up @@ -2503,9 +2497,8 @@

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);

Check warning on line 2501 in src/nrnpython/nrnpy_hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_hoc.cpp#L2500-L2501

Added lines #L2500 - L2501 were not covered by tests
}
return PyFloat_AsDouble(obj);
}
Expand Down Expand Up @@ -2820,18 +2813,16 @@
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());

Check warning on line 2817 in src/nrnpython/nrnpy_hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_hoc.cpp#L2816-L2817

Added lines #L2816 - L2817 were not covered by tests
}
}
} 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());

Check warning on line 2825 in src/nrnpython/nrnpy_hoc.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_hoc.cpp#L2824-L2825

Added lines #L2824 - L2825 were not covered by tests
}
Py_DECREF(pn);
}
Expand Down
32 changes: 15 additions & 17 deletions src/nrnpython/nrnpy_nrn.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <fmt/format.h>

#include "neuron/container/data_handle.hpp"
#include "neuron/container/generic_data_handle.hpp"
#include "nrn_ansi.h"
Expand Down Expand Up @@ -974,9 +976,8 @@

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;
}

Expand Down Expand Up @@ -1410,9 +1411,8 @@
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());

Check warning on line 1415 in src/nrnpython/nrnpy_nrn.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_nrn.cpp#L1414-L1415

Added lines #L1414 - L1415 were not covered by tests
} else {
result = PyString_FromString(self->sym_->name);
}
Expand Down Expand Up @@ -1924,15 +1924,15 @@
}

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);

Check warning on line 1933 in src/nrnpython/nrnpy_nrn.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_nrn.cpp#L1933

Added line #L1933 was not covered by tests
}
PyErr_SetString(PyExc_AttributeError, buf);
PyErr_SetString(PyExc_AttributeError, buf.c_str());
}

static NPyRangeVar* rvnew(Symbol* sym, NPySecObj* sec, double x) {
Expand Down Expand Up @@ -2356,9 +2356,8 @@
} 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());

Check warning on line 2360 in src/nrnpython/nrnpy_nrn.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_nrn.cpp#L2359-L2360

Added lines #L2359 - L2360 were not covered by tests
return -1;
} else {
int errp;
Expand Down Expand Up @@ -2627,14 +2626,13 @@
}
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;
}
Expand Down
22 changes: 11 additions & 11 deletions src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
if (!r) {
char* mes = nrnpyerr_str();
if (mes) {
Fprintf(stderr, "%s\n", mes);
std::cerr << mes << std::endl;

Check warning on line 52 in src/nrnpython/nrnpy_p2h.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L52

Added line #L52 was not covered by tests
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not obvious from the definition of Fprinf but it does serve a purpose and using std::cerr and std::cout isn't ideal. See #3052.

free(mes);
hoc_execerror("Call of Python Callable failed", NULL);
}
Expand Down Expand Up @@ -128,7 +128,7 @@
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);
}
Expand Down Expand Up @@ -204,7 +204,7 @@
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);
}
Expand Down Expand Up @@ -385,7 +385,7 @@
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);
}
Expand Down Expand Up @@ -435,7 +435,7 @@
} else {
char* mes = nrnpyerr_str();
if (mes) {
Fprintf(stderr, "%s\n", mes);
std::cerr << mes << std::endl;

Check warning on line 438 in src/nrnpython/nrnpy_p2h.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L438

Added line #L438 was not covered by tests
free(mes);
hoc_execerror("Python Callback failed", 0);
}
Expand All @@ -459,7 +459,7 @@
if (!r) {
char* mes = nrnpyerr_str();
if (mes) {
Fprintf(stderr, "%s\n", mes);
std::cerr << mes << std::endl;

Check warning on line 462 in src/nrnpython/nrnpy_p2h.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L462

Added line #L462 was not covered by tests
free(mes);
hoc_execerror("Python Callback failed", 0);
}
Expand Down Expand Up @@ -529,7 +529,7 @@
if (!err || *err) {
char* mes = nrnpyerr_str();
if (mes) {
Fprintf(stderr, "%s\n", mes);
std::cerr << mes << std::endl;
free(mes);
}
if (PyErr_Occurred()) {
Expand Down Expand Up @@ -712,18 +712,18 @@
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;

Check warning on line 715 in src/nrnpython/nrnpy_p2h.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L715

Added line #L715 was not covered by tests
} else {
cmes = strdup(mes.c_str());
if (!cmes) {
Fprintf(stderr, "nrnpyerr_str: strdup failed\n");
std::cerr << "nrnpyerr_str: strdup failed" << std::endl;

Check warning on line 719 in src/nrnpython/nrnpy_p2h.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L719

Added line #L719 was not covered by tests
}
}
}

if (!py_str) {
PyErr_Print();
Fprintf(stderr, "nrnpyerr_str failed\n");
std::cerr << "nrnpyerr_str failed" << std::endl;

Check warning on line 726 in src/nrnpython/nrnpy_p2h.cpp

View check run for this annotation

Codecov / codecov/patch

src/nrnpython/nrnpy_p2h.cpp#L726

Added line #L726 was not covered by tests
}

Py_XDECREF(module_name);
Expand Down Expand Up @@ -769,7 +769,7 @@
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);
}
Expand Down
Loading