Commit ee25eabe authored by Tiago Peixoto's avatar Tiago Peixoto

Fix run_action

A couple of bugs fixed. Local frame dict is read-only in python, so the
updated arguments are returned in a dict instead.
parent 3799da5b
......@@ -157,6 +157,9 @@ public:
typedef property_map<multigraph_t,vertex_index_t>::type vertex_index_map_t;
typedef property_map<multigraph_t,edge_index_t>::type edge_index_map_t;
vertex_index_map_t GetVertexIndex() {return _vertex_index;}
edge_index_map_t GetEdgeIndex() {return _edge_index;}
private:
// Gets the encapsulated graph view. See graph_filtering.cc for details
boost::any GetGraphView() const;
......
......@@ -24,6 +24,10 @@
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#ifdef HAVE_SCIPY // integration with scipy weave
#include "weave/scxx/object.h"
#endif
using namespace std;
using namespace graph_tool;
using namespace boost;
......@@ -225,6 +229,17 @@ struct variant_from_python
}
};
// scipy weave integration
#ifdef HAVE_SCIPY
struct scxx_to_python
{
static PyObject* convert(const py::object& o)
{
return incref((PyObject*)(o));
}
};
#endif
// persistent python object IO
namespace graph_tool
{
......@@ -242,6 +257,14 @@ void set_unpickler(python::object o)
graph_tool::object_unpickler = o;
}
python::list get_property_types()
{
python::list plist;
for (int i = 0; i < mpl::size<value_types>::value; ++i)
plist.append(string(type_names[i]));
return plist;
}
BOOST_PYTHON_MODULE(libgraph_tool_core)
{
// numpy
......@@ -253,6 +276,7 @@ BOOST_PYTHON_MODULE(libgraph_tool_core)
register_exception_translator<GraphException>(graph_exception_translator);
def("raise_error", &raise_error);
def("get_property_types", &get_property_types);
mpl::for_each<mpl::push_back<scalar_types,string>::type>(export_vector_types());
......@@ -315,6 +339,9 @@ BOOST_PYTHON_MODULE(libgraph_tool_core)
to_python_converter<pair<size_t,size_t>, pair_to_tuple<size_t,size_t> >();
pair_from_tuple<double,double>();
pair_from_tuple<size_t,size_t>();
#ifdef HAVE_SCIPY
to_python_converter<py::object, scxx_to_python>();
#endif
class_<IStream>("IStream", no_init).def("Read", &IStream::Read);
class_<OStream>("OStream", no_init).def("Write", &OStream::Write).
......
......@@ -27,6 +27,7 @@
#include <boost/property_map.hpp>
#include <boost/dynamic_property_map.hpp>
#include <boost/vector_property_map.hpp>
#include <boost/functional/hash.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/transform.hpp>
......
......@@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, string, hashlib
import sys, string, hashlib, os.path
from .. import core
from .. import libgraph_tool_core
......@@ -25,15 +25,17 @@ except ImportError:
raise libgraph_tool_core.raise_error\
("You need to have scipy installed to use 'run_action'.")
prefix_dir = libgraph_tool_core.mod_info().install_prefix
python_dir = libgraph_tool_core.mod_info().python_dir
python_dir = string.Template(python_dir).substitute(prefix=prefix_dir)
cxxflags = libgraph_tool_core.mod_info().cxxflags
inc_prefix = python_dir + "/graph_tool/include"
prefix = None
for d in [d + "/graph_tool" for d in sys.path]:
if os.path.exists(d):
prefix = d
break
inc_prefix = prefix + "/include"
cxxflags = libgraph_tool_core.mod_info().cxxflags + " -I%s" % inc_prefix
# this is the code template which defines the action function object
support_template = open(python_dir +
"/graph_tool/run_action/run_action_template.hh").read()
support_template = open(prefix + "/run_action/run_action_template.hh").read()
def inline(g, code, arg_names=[], local_dict=None,
global_dict=None, force=0, compiler="gcc", verbose=0,
......@@ -73,13 +75,11 @@ def inline(g, code, arg_names=[], local_dict=None,
support_template = string.Template(globals()["support_template"])
support_code = support_template.substitute(code_hash=code_hash,
property_map_types=props,
arg_expansion=arg_expansion,
code=code,
return_vals = return_vals,
include_prefix = inc_prefix)\
+ support_code
support_code += support_template.substitute(code_hash=code_hash,
property_map_types=props,
arg_expansion=arg_expansion,
code=code,
return_vals = return_vals)
# insert a hash value of the support_code into the code below, to force
# recompilation when support_code (and module version) changes
......@@ -101,8 +101,7 @@ def inline(g, code, arg_names=[], local_dict=None,
# need to go deeper into the call stack
call_frame = sys._getframe(1)
if local_dict is None:
local_dict = {}
local_dict.update(call_frame.f_locals)
local_dict = call_frame.f_locals
if global_dict is None:
global_dict = call_frame.f_globals
local_dict["self___graph"] = g.underlying_graph() # the graph interface
......@@ -131,12 +130,6 @@ def inline(g, code, arg_names=[], local_dict=None,
if ret_vals["__exception_thrown"]:
libgraph_tool_core.raise_error(ret_vals["__exception_error"])
# update returned values
for arg in arg_names:
if call_frame.f_locals.has_key(arg):
call_frame.f_locals[arg] = ret_vals[arg]
elif call_frame.f_globals.has_key(arg):
call_frame.f_globals[arg] = ret_vals[arg]
sys.setdlopenflags(orig_dlopen_flags) # reset dlopen to normal case to
# avoid unnecessary symbol collision
return ret_vals
......@@ -21,10 +21,10 @@
#include <boost/tuple/tuple.hpp>
#include <boost/type_traits.hpp>
#include <boost/python.hpp>
#include "${include_prefix}/graph.hh"
#include "${include_prefix}/graph_filtering.hh"
#include "${include_prefix}/graph_properties.hh"
#include "${include_prefix}/histogram.hh"
#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_properties.hh"
#include "histogram.hh"
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
......@@ -107,22 +107,18 @@ struct action_${code_hash}
catch (const GraphException& e)
{
__exception_error = e.what();
throw;
__exception_thrown = true;
}
catch (const bad_any_cast& e)
{
__exception_error = e.what();
__exception_error += " (wrong property map type?)";
throw;
__exception_thrown = true;
}
catch (const std::exception& e)
{
__exception_error = "unknown exception thrown: ";
__exception_error += e.what();
throw;
}
catch (...)
{
__exception_thrown = true;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment