Skip to content

Commit

Permalink
Fix reference counting in various places
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentzell committed Aug 7, 2024
1 parent 7bf32bb commit 5855dd7
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/c2py/converters/wrapped.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace c2py {
sptr = std::make_shared<pto_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<pto_table_t>{sptr}; //NOLINT
pyref c = PyCapsule_New((void *)p, "__main__.__cpp2py_table", (PyCapsule_Destructor)_table_destructor);
pyref s = PyUnicode_FromString("__cpp2py_table");
Expand Down
2 changes: 1 addition & 1 deletion src/c2py/pydict.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace c2py {
PyObject *self;
const char *key;
proxy &operator=(auto &x) {
PyDict_SetItemString(self, key, c2py::pyref{c2py::cxx2py(x)});
PyDict_SetItemString(self, key, pyref{cxx2py(x)});
// NB : PyDict_SetItemString does NOT steal the reference (cf Python API),
// so we put the new reference made by cxx2py
// in a pyref which will release it at destruction.
Expand Down
6 changes: 3 additions & 3 deletions src/c2py/pyfunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace c2py {
}

template <typename A> void operator()(nv_pair<A> const &x) {
[[maybe_unused]] int status = PyDict_SetItemString(kwargs, x.name.c_str(), detail::cxx2py_or_fun(x.value));
[[maybe_unused]] int status = PyDict_SetItemString(kwargs, x.name.c_str(), pyref{detail::cxx2py_or_fun(x.value)});
assert(status == 0);
}
};
Expand Down Expand Up @@ -98,7 +98,7 @@ namespace c2py {
(erased_args(x), ...);
r = PyObject_Call(this->ob, erased_args.args, erased_args.kwargs);
} else
r = PyObject_CallFunctionObjArgs(this->ob, detail::cxx2py_or_fun(x)..., NULL);
r = PyObject_CallFunctionObjArgs(this->ob, (PyObject *)pyref{detail::cxx2py_or_fun(x)}..., NULL);
if (r.is_null()) {
PyErr_Print();
throw std::runtime_error{"Error calling the function " + fname}; //+ get_python_error()};
Expand All @@ -110,4 +110,4 @@ namespace c2py {
return py2cxx<R>(r);
}
};
} // namespace c2py
} // namespace c2py
2 changes: 1 addition & 1 deletion src/c2py/pyref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace c2py {
/// Import the module and returns a pyref to it
pyref pyref::module(std::string const &module_name) {
// Maybe the module was already imported?
PyObject *mod = PyImport_GetModule(PyUnicode_FromString(module_name.c_str()));
PyObject *mod = PyImport_GetModule(pyref::string(module_name.c_str()));

// If not, import normally
if (mod == nullptr) mod = PyImport_ImportModule(module_name.c_str());
Expand Down

0 comments on commit 5855dd7

Please sign in to comment.