diff --git a/pytriqs/wrap_generator/wrap_generator.py b/pytriqs/wrap_generator/wrap_generator.py index 366bd23b..9b20438d 100644 --- a/pytriqs/wrap_generator/wrap_generator.py +++ b/pytriqs/wrap_generator/wrap_generator.py @@ -276,7 +276,6 @@ class class_ : # Init arithmetic # expect a tuple : "algebra", "scalar1", "scalar2", etc... self.number_protocol = {} - if not isinstance(arithmetic, tuple) : arithmetic = (arithmetic,) if arithmetic : if not isinstance(arithmetic, tuple) : arithmetic = (arithmetic,) add = arithmetic[0] in ("algebra", "abelian_group", "vector_space", "only_add") @@ -361,15 +360,25 @@ class class_ : #assert 'calling_pattern' not in kw, "No calling_pattern here" if 'calling_pattern' not in kw : kw['c_name'] = "__init__" f = cfunction(is_constructor = True, **kw) - build_type = regular_type_if_view_else_type(self.c_type) if self.c_type_is_view and build_from_regular_type else self.c_type all_args = ",".join([ ('*' if t in module_.wrapped_types else '') + n for t,n,d in f.args]) - #all_args = ",".join([ ('*' if t in module_.wrapped_types else '') + n + (' = ' + d if d else '') for t,n,d in f.args]) - if 'calling_pattern' not in kw : - f._calling_pattern = "((%s *)self)->_c ->"%self.py_type + ('operator =' if not self.c_type_is_view else 'rebind') + " (%s (%s));"%(build_type,all_args) - else : - f._calling_pattern = kw['calling_pattern'] + "\n ((%s *)self)->_c ->"%self.py_type + ('operator =' if not self.c_type_is_view else 'rebind') + " (std::move(result));" + ##all_args = ",".join([ ('*' if t in module_.wrapped_types else '') + n + (' = ' + d if d else '') for t,n,d in f.args]) + # First version, where _new method use the default constructor + #build_type = regular_type_if_view_else_type(self.c_type) if self.c_type_is_view and build_from_regular_type else self.c_type + #if 'calling_pattern' not in kw : + # f._calling_pattern = "((%s *)self)->_c ->"%self.py_type + ('operator =' if not self.c_type_is_view else 'rebind') + " (%s (%s));"%(build_type,all_args) + #else : + # f._calling_pattern = kw['calling_pattern'] + "\n ((%s *)self)->_c ->"%self.py_type + ('operator =' if not self.c_type_is_view else 'rebind') + " (std::move(result));" + # Second version : using no default construction, but leaving the + # pointer null after _new + f._calling_pattern = '' + if 'calling_pattern' in kw : + f._calling_pattern, all_args = kw['calling_pattern'] + '\n', "std::move(result)" + if self.c_type_is_view and build_from_regular_type : + f._calling_pattern += "((%s *)self)->_c = new %s(%s (%s));"%(self.py_type, self.c_type,regular_type_if_view_else_type(self.c_type),all_args) + else : + f._calling_pattern += "((%s *)self)->_c = new %s (%s);"%(self.py_type, self.c_type,all_args) - if not self.constructor : + if not self.constructor : self.constructor = pyfunction(py_name = "__init__", **kw) self.constructor.is_constructor = True self.constructor.overloads.append(f) diff --git a/pytriqs/wrap_generator/wrapper.mako.cpp b/pytriqs/wrap_generator/wrapper.mako.cpp index 600f23a2..66ec68f2 100644 --- a/pytriqs/wrap_generator/wrapper.mako.cpp +++ b/pytriqs/wrap_generator/wrapper.mako.cpp @@ -151,11 +151,12 @@ static PyObject* ${c.py_type}_new(PyTypeObject *type, PyObject *args, PyObject * self = (${c.py_type} *)type->tp_alloc(type, 0); if (self != NULL) { try { - %if not c.c_type_is_view : - self->_c = new ${c.c_type}{}; - %else : - self->_c = new ${c.c_type}{typename ${c.c_type}::regular_type{}}; // no default constructor for views - %endif + self->_c = NULL; + ##//%if not c.c_type_is_view : + ##// self->_c = new ${c.c_type}{}; + ##//%else : + ##// self->_c = new ${c.c_type}{typename ${c.c_type}::regular_type{}}; // no default constructor for views + ##//%endif } catch (std::exception const & e) { std::cout << e.what()<_c; + if (self->_c != NULL) delete self->_c; // should never be null, but I protect it anyway self->ob_type->tp_free((PyObject*)self); } @@ -365,7 +366,6 @@ static PyTypeObject ${c.py_type}Type = { ${c.py_type}_new, /* tp_new */ }; - //--------------------- converters for the class c ----------------------------- namespace triqs { namespace py_tools { @@ -381,10 +381,18 @@ template <> struct py_converter<${c.c_type}> { return (PyObject *)self; } - static ${c.c_type} & py2c(PyObject * ob){ return *(((${c.py_type}*)ob)->_c);} + static ${c.c_type} & py2c(PyObject * ob){ + auto *_c = ((${c.py_type} *)ob)->_c; + if (_c == NULL) TRIQS_RUNTIME_ERROR << "Severe internal error : _c is null in py2c for type ${c.c_type} !"; + return *_c; + } static bool is_convertible(PyObject *ob, bool raise_exception){ - if (PyObject_TypeCheck(ob, & ${c.py_type}Type)) return true; + if (PyObject_TypeCheck(ob, & ${c.py_type}Type)) { + if (((${c.py_type} *)ob)->_c != NULL) return true; + if (raise_exception) PyErr_SetString(PyExc_TypeError, "Severe internal error : Python object of ${c.py_type} has a _c NULL pointer !!"); + return false; + } if (raise_exception) PyErr_SetString(PyExc_TypeError, "Python object is not a ${c.py_type}"); return false; } diff --git a/triqs/lattice/tight_binding.hpp b/triqs/lattice/tight_binding.hpp index 6c608755..4fd580fd 100644 --- a/triqs/lattice/tight_binding.hpp +++ b/triqs/lattice/tight_binding.hpp @@ -39,8 +39,6 @@ namespace lattice { /// tight_binding(bravais_lattice const& bl, std::vector> all_disp, std::vector> all_matrices); - tight_binding() = default; - /// Underlying lattice bravais_lattice const& lattice() const { return bl_; }