Skip to content

Commit

Permalink
Avoid use of static pyref as lifetime may extend beyond interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentzell committed Aug 2, 2023
1 parent 6d8bde7 commit b319947
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
8 changes: 4 additions & 4 deletions c++/triqs/cpp2py_converters/gf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace cpp2py {

static PyObject *c2py(c_type g) {

static pyref cls = pyref::get_class("triqs.gf", "Gf", true);
pyref cls = pyref::get_class("triqs.gf", "Gf", true);
if (cls.is_null()) return NULL;
pyref m = convert_to_python(g.mesh());
if (m.is_null()) return NULL;
Expand Down Expand Up @@ -109,7 +109,7 @@ namespace cpp2py {
// ----------------------------------------------

static bool is_convertible(PyObject *ob, bool raise_exception) {
static pyref cls = pyref::get_class("triqs.gf", "Gf", true);
pyref cls = pyref::get_class("triqs.gf", "Gf", true);

// first check it is a Gf
if (not pyref::check_is_instance(ob, cls, raise_exception)) return false;
Expand Down Expand Up @@ -209,7 +209,7 @@ namespace cpp2py {
// ----------------------------------------------

static bool is_convertible(PyObject *ob, bool raise_exception) {
static pyref cls = pyref::get_class("triqs.gf", "BlockGf", true);
pyref cls = pyref::get_class("triqs.gf", "BlockGf", true);

// first check it is a BlockGf
if (not pyref::check_is_instance(ob, cls, raise_exception)) return false;
Expand Down Expand Up @@ -275,7 +275,7 @@ namespace cpp2py {
// ----------------------------------------------

static bool is_convertible(PyObject *ob, bool raise_exception) {
static pyref cls = pyref::get_class("triqs.gf", "Block2Gf", true);
pyref cls = pyref::get_class("triqs.gf", "Block2Gf", true);
if (cls.is_null()) throw std::runtime_error("Cannot find the triqs.gf.Block2Gf");

// first check it is a Block2Gf
Expand Down
20 changes: 12 additions & 8 deletions c++/triqs/cpp2py_converters/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

#pragma once

#include <cpp2py/cpp2py.hpp>

#include "../mesh.hpp"

namespace cpp2py {

// -----------------------------------
Expand All @@ -26,7 +30,7 @@ namespace cpp2py {
template <> struct py_converter<triqs::mesh::all_t> {

static PyObject *c2py(triqs::mesh::all_t m) {
static pyref all = pyref::get_class("builtins", "all", true);
pyref all = pyref::get_class("builtins", "all", true);
if (all.is_null()) return NULL;
return all.new_ref();
}
Expand Down Expand Up @@ -78,7 +82,7 @@ namespace cpp2py {
using c_type = triqs::mesh::matsubara_freq;

static PyObject *c2py(c_type const &x) {
static pyref cls = pyref::get_class("triqs.gf", "MatsubaraFreq", true);
pyref cls = pyref::get_class("triqs.gf", "MatsubaraFreq", true);
if (cls.is_null()) return NULL;

pyref kw = PyDict_New();
Expand All @@ -98,7 +102,7 @@ namespace cpp2py {
}

static bool is_convertible(PyObject *ob, bool raise_exception) {
static pyref cls = pyref::get_class("triqs.gf", "MatsubaraFreq", true);
pyref cls = pyref::get_class("triqs.gf", "MatsubaraFreq", true);
if (not pyref::check_is_instance(ob, cls, raise_exception)) return false;
return true;
}
Expand All @@ -123,7 +127,7 @@ namespace cpp2py {
using c_type = triqs::lattice::bravais_lattice::point_t;

static PyObject *c2py(c_type const &x) {
static pyref cls = pyref::get_class("triqs.lattice", "LatticePoint", true);
pyref cls = pyref::get_class("triqs.lattice", "LatticePoint", true);
if (cls.is_null()) return NULL;

pyref kw = PyDict_New();
Expand All @@ -140,7 +144,7 @@ namespace cpp2py {
}

static bool is_convertible(PyObject *ob, bool raise_exception) {
static pyref cls = pyref::get_class("triqs.lattice", "LatticePoint", true);
pyref cls = pyref::get_class("triqs.lattice", "LatticePoint", true);
if (not pyref::check_is_instance(ob, cls, raise_exception)) return false;
return true;
}
Expand Down Expand Up @@ -177,15 +181,15 @@ namespace cpp2py {
using mtuple_conv = py_converter<typename c_type::m_tuple_t>; // the tuple of meshes

static PyObject *c2py(c_type m) {
static pyref cls = pyref::get_class("triqs.gf", "MeshProduct", true);
pyref cls = pyref::get_class("triqs.gf", "MeshProduct", true);
if (cls.is_null()) return NULL;
pyref m_tuple = mtuple_conv::c2py(m.components()); // take the C++ tuple of meshes and make the corresponding Python tuple
if (m_tuple.is_null()) return NULL;
return PyObject_Call(cls, m_tuple, NULL);
}

static bool is_convertible(PyObject *ob, bool raise_exception) {
static pyref cls = pyref::get_class("triqs.gf", "MeshProduct", true);
pyref cls = pyref::get_class("triqs.gf", "MeshProduct", true);

// first check it is a MeshProduct
if (not pyref::check_is_instance(ob, cls, raise_exception)) return false;
Expand Down Expand Up @@ -213,7 +217,7 @@ namespace cpp2py {

static PyObject *c2py(c_type const &p) {

static pyref cls = pyref::get_class("triqs.gf", "MeshPoint", /* raise_exception */ true);
pyref cls = pyref::get_class("triqs.gf", "MeshPoint", /* raise_exception */ true);
if (cls.is_null()) return NULL;

pyref index = convert_to_python(p.index());
Expand Down

0 comments on commit b319947

Please sign in to comment.