Skip to content

Commit

Permalink
Consistently use pyref::string for temporary Python string creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentzell committed Aug 6, 2024
1 parent b1c2475 commit ae53d17
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion c++/cpp2py/py_converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace cpp2py {
sptr = std::make_shared<conv_table_t>();

// Now register the pointer in __main__
PyObject *mod = PyImport_GetModule(PyUnicode_FromString("__main__"));
PyObject *mod = PyImport_GetModule(pyref::string("__main__"));
auto *p = new std::shared_ptr<conv_table_t>{sptr};
pyref c = PyCapsule_New((void *)p, "__main__.__cpp2py_table", (PyCapsule_Destructor)_table_destructor);
pyref s = PyUnicode_FromString("__cpp2py_table");
Expand Down
3 changes: 1 addition & 2 deletions c++/cpp2py/pyref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ namespace cpp2py {
/// Import the module and returns a pyref to it
static pyref module(std::string const &module_name) {
// Maybe the module was already imported?
pyref mod_name = PyUnicode_FromString(module_name.c_str());
PyObject *mod = PyImport_GetModule(mod_name);
PyObject *mod = PyImport_GetModule(pyref::string(module_name));

// If not, import normally
if (mod == nullptr) mod = PyImport_ImportModule(module_name.c_str());
Expand Down
10 changes: 5 additions & 5 deletions cpp2py/mako/wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ template <> struct py_converter<${en.c_name}> {
PyErr_Fetch(&ptype, &err, &ptraceback);
// catch here all non defined errors and print instead generic warning
if (err == NULL) {
errors[${n_overload}] = pyref{PyUnicode_FromString("unkown error check converter")};
errors[${n_overload}] = pyref::string("unkown error check converter");
} else {
errors[${n_overload}] = pyref{err};
}
Expand Down Expand Up @@ -1039,10 +1039,10 @@ PyObject* ${c.py_type}___iter__(PyObject *self) {
static const char * ens = "${repr( [ (en.c_name_absolute, en.c_namespace, en.values) for en in module.enums] )}";
static const char * inclu = "${repr( module.include_list)}";

PyDict_SetItemString(d, "classes", pyref(PyUnicode_FromString(cls)));
PyDict_SetItemString(d, "enums", pyref(PyUnicode_FromString(ens)));
PyDict_SetItemString(d, "module_name", pyref(PyUnicode_FromString("${module.full_name}")));
PyDict_SetItemString(d, "includes", pyref(PyUnicode_FromString(inclu)));
PyDict_SetItemString(d, "classes", pyref::string(cls));
PyDict_SetItemString(d, "enums", pyref::string(ens));
PyDict_SetItemString(d, "module_name", pyref::string("${module.full_name}"));
PyDict_SetItemString(d, "includes", pyref::string(inclu));

return d;
}
Expand Down

0 comments on commit ae53d17

Please sign in to comment.