161
|
1 /* |
|
2 |
|
3 Copyright (C) 2016 Colin B. Macdonald |
|
4 |
|
5 This file is part of Pytave. |
|
6 |
|
7 Pytave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
|
11 |
|
12 Pytave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Pytave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <dlfcn.h> |
|
28 #include <boost/python.hpp> |
|
29 #include <boost/python/numeric.hpp> |
|
30 |
|
31 /* Both boost::python and octave define HAVE_STAT and HAVE_FSTAT. Ideally, |
|
32 they shouldn't expose their configuration in the header files, but they do. |
|
33 This silences the compiler warning. */ |
|
34 #undef HAVE_STAT |
|
35 #undef HAVE_FSTAT |
|
36 |
|
37 #include <oct.h> |
|
38 |
|
39 #define PYTAVE_DO_DECLARE_SYMBOL |
|
40 #include "pytavedefs.h" |
|
41 #include "arrayobjectdefs.h" |
|
42 #include "exceptions.h" |
|
43 #include "python_to_octave.h" |
|
44 |
|
45 using namespace boost::python; |
|
46 |
|
47 DEFUN_DLD (pyexec, args, nargout, |
|
48 "-*- texinfo -*-\n\ |
|
49 @deftypefn {Loadable Function} pyexec (@var{func})\n\ |
|
50 Execute some python code.\n\ |
|
51 \n\ |
|
52 @end deftypefn") |
|
53 { |
|
54 octave_value_list retval; |
|
55 |
|
56 int nargin = args.length (); |
|
57 |
|
58 if (nargin != 1) |
|
59 { |
|
60 print_usage (); |
|
61 return retval; |
|
62 } |
|
63 |
|
64 std::string code = args(0).string_value (); |
|
65 |
|
66 dlopen("libpython2.7.so", RTLD_GLOBAL|RTLD_LAZY); |
|
67 |
|
68 Py_Initialize (); |
|
69 |
|
70 try |
|
71 { |
|
72 object main_module = import ("__main__"); |
|
73 object main_namespace = main_module.attr ("__dict__"); |
|
74 // TODO: figure out exec return code: |
|
75 // http://www.boost.org/doc/libs/1_38_0/libs/python/doc/v2/exec.html |
|
76 exec (code.c_str (), main_namespace, main_namespace); |
|
77 //std::cerr << "exec done" << std::endl; |
|
78 |
|
79 } |
|
80 // just copy-pasted from py.cc |
|
81 catch (pytave::object_convert_exception const &) |
|
82 { |
|
83 error ("pyexec: error in return value type conversion"); |
|
84 } |
|
85 catch (error_already_set const &) |
|
86 { |
|
87 PyObject *ptype, *pvalue, *ptraceback; |
|
88 PyErr_Fetch (&ptype, &pvalue, &ptraceback); |
|
89 |
|
90 try |
|
91 { |
|
92 std::string message = extract<std::string> (pvalue); |
|
93 error ("pyexec: %s", message.c_str ()); |
|
94 } |
|
95 catch (error_already_set const &) |
|
96 { |
|
97 PyErr_Restore (ptype, pvalue, ptraceback); |
|
98 PyErr_Print (); |
|
99 } |
|
100 } |
|
101 return retval; |
|
102 } |