Skip to content

Commit

Permalink
Merge pull request #3 from fermi-lat/stgenpatch
Browse files Browse the repository at this point in the history
Add destructor to PySkyFunction for RAII
  • Loading branch information
Areustle authored Mar 2, 2018
2 parents 3261748 + 3d2d0da commit 049287b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
6 changes: 3 additions & 3 deletions skymaps/PySkyFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ namespace skymaps {
public:

/** @brief ctor
@param modulename name of python module
@param modulename name of python module
@param functionname name of a python function that takes arguments x,y,z, returns a function
*/
PySkyFunction(std::string modulename, std::string functionname);

~PySkyFunction();

/** @brief ctor
@param callable a callable python object
Expand Down
20 changes: 12 additions & 8 deletions src/PySkyFunction.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @file PySkyFunction.cxx
@brief implement class PySkyFunction
@brief implement class PySkyFunction
$Header$
*/
Expand All @@ -9,7 +9,7 @@
#include "skymaps/PySkyFunction.h"
#include "embed_python/Module.h"
#ifdef _DEBUG
#undef _DEBUG /* Link with python24.lib and not python24_d.lib */
#undef _DEBUG /* Link with python24.lib and not python24_d.lib */
#endif
#include <Python.h>

Expand All @@ -22,16 +22,20 @@ using namespace embed_python;
PySkyFunction::PySkyFunction(std::string modulename, std::string functionname)
: m_fun(Module("" , modulename).attribute(functionname) )
{
auto tmp = new Module("" , modulename);
m_module = tmp;
m_module = new Module("" , modulename);
}

PySkyFunction::~PySkyFunction()
{
delete m_module;
}

PySkyFunction::PySkyFunction(PyObject* callable)
: m_fun(callable)
{
if (PyErr_Occurred()) {
PyErr_Print();
throw std::runtime_error("PySkyFunction: error in ctor");
throw std::runtime_error("PySkyFunction: error in ctor");
}
}

Expand All @@ -51,7 +55,7 @@ double PySkyFunction::operator()(const astro::SkyDir& dir)const
PyObject* retobj = PyObject_CallObject(m_fun, args);
if (PyErr_Occurred()) {
PyErr_Print();
throw std::runtime_error("PySkyFunction: error calling function");
throw std::runtime_error("PySkyFunction: error calling function");
}
Py_DECREF(list);
Py_DECREF(args);
Expand Down Expand Up @@ -115,12 +119,12 @@ double PySkyFunction::average(const astro::SkyDir& dir, double angle, double tol

// Calculate average for a given level
double PySkyFunction::level_ave(const astro::SkyDir& dir, double angle, int level) const
{
{

int nside(1 << level);
std::vector<int> v;
healpix::Healpix hpx(nside, healpix::Healpix::NESTED, astro::SkyDir::GALACTIC);
hpx.query_disc(dir, angle, v);
hpx.query_disc(dir, angle, v);
double av(0);

for (std::vector<int>::const_iterator it = v.begin(); it != v.end(); ++it)
Expand Down

0 comments on commit 049287b

Please sign in to comment.