From ae53d174393e7c6c3661a85aabb3c52acbf8a109 Mon Sep 17 00:00:00 2001 From: Nils Wentzell Date: Tue, 6 Aug 2024 19:07:43 -0400 Subject: [PATCH] Consistently use pyref::string for temporary Python string creation --- c++/cpp2py/py_converter.hpp | 2 +- c++/cpp2py/pyref.hpp | 3 +-- cpp2py/mako/wrap.cxx | 10 +++++----- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/c++/cpp2py/py_converter.hpp b/c++/cpp2py/py_converter.hpp index 6816526..9e64ee1 100644 --- a/c++/cpp2py/py_converter.hpp +++ b/c++/cpp2py/py_converter.hpp @@ -103,7 +103,7 @@ namespace cpp2py { sptr = std::make_shared(); // 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{sptr}; pyref c = PyCapsule_New((void *)p, "__main__.__cpp2py_table", (PyCapsule_Destructor)_table_destructor); pyref s = PyUnicode_FromString("__cpp2py_table"); diff --git a/c++/cpp2py/pyref.hpp b/c++/cpp2py/pyref.hpp index bfc2cfe..0a6efaa 100644 --- a/c++/cpp2py/pyref.hpp +++ b/c++/cpp2py/pyref.hpp @@ -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()); diff --git a/cpp2py/mako/wrap.cxx b/cpp2py/mako/wrap.cxx index f8a72d2..a30e579 100644 --- a/cpp2py/mako/wrap.cxx +++ b/cpp2py/mako/wrap.cxx @@ -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}; } @@ -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; }