annotate py.cc @ 115:00d19f71c9ca

Initial implementation of py oct-file * py.cc: Initial implementation of py oct function. * ax_octave.m4: Find and export path to mkoctfile. * Makefile.am (.cc.oct): Custom rule to build py.oct. (all-am): Depend on py.oct. * .hgignore: Include py.oct in the ignore list.
author Mike Miller <mtmiller@octave.org>
date Sun, 20 Sep 2015 15:12:47 +0200
parents
children 3a35bb85ce52
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
115
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
1 /*
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
2
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
3 Copyright (C) 2015 Mike Miller
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
4
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
5 This file is part of Pytave.
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
6
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
7 Pytave is free software; you can redistribute it and/or modify it
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
10 option) any later version.
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
11
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
12 Pytave is distributed in the hope that it will be useful, but WITHOUT
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
15 for more details.
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
16
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
18 along with Pytave; see the file COPYING. If not, see
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
20
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
21 */
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
22
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
23 #ifdef HAVE_CONFIG_H
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
24 #include <config.h>
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
25 #endif
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
26
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
27 #include <boost/python.hpp>
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
28 #include <boost/python/numeric.hpp>
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
29
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
30 /* Both boost::python and octave define HAVE_STAT and HAVE_FSTAT. Ideally,
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
31 they shouldn't expose their configuration in the header files, but they do.
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
32 This silences the compiler warning. */
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
33 #undef HAVE_STAT
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
34 #undef HAVE_FSTAT
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
35
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
36 #include <oct.h>
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
37
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
38 #define PYTAVE_DO_DECLARE_SYMBOL
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
39 #include "pytavedefs.h"
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
40 #include "arrayobjectdefs.h"
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
41 #include "exceptions.h"
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
42 #include "python_to_octave.h"
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
43
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
44 using namespace boost::python;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
45
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
46 DEFUN_DLD (py, args, nargout,
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
47 "-*- texinfo -*-\n\
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
48 @deftypefn {Loadable Function} py (@var{func})\n\
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
49 @enddeftypefn")
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
50 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
51 octave_value_list retval;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
52
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
53 int nargin = args.length ();
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
54
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
55 if (nargin != 1)
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
56 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
57 print_usage ();
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
58 return retval;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
59 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
60
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
61 std::string module;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
62 std::string func = args(0).string_value ();
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
63
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
64 size_t idx = func.rfind (".");
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
65 if (idx != std::string::npos)
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
66 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
67 module = func.substr (0, idx);
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
68 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
69
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
70 Py_Initialize ();
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
71
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
72 try
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
73 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
74 object main_module = import ("__main__");
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
75 object main_namespace = main_module.attr ("__dict__");
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
76
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
77 if (! module.empty ())
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
78 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
79 std::string cmd = "import " + module;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
80 exec (cmd.c_str (), main_namespace, main_namespace);
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
81 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
82
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
83 object res = eval (func.c_str (), main_namespace, main_namespace);
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
84
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
85 if (! res.is_none ())
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
86 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
87 octave_value val;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
88 pytave::pyobj_to_octvalue (val, res);
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
89 retval(0) = val;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
90 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
91 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
92 catch (pytave::object_convert_exception const &)
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
93 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
94 error ("py: error in return value type conversion");
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
95 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
96 catch (error_already_set const &)
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
97 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
98 std::cerr << "in here" << std::endl;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
99 PyObject *ptype, *pvalue, *ptraceback;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
100 PyErr_Fetch (&ptype, &pvalue, &ptraceback);
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
101
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
102 try
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
103 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
104 std::string message = extract<std::string> (pvalue);
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
105 error ("py: %s", message.c_str ());
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
106 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
107 catch (error_already_set const &)
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
108 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
109 PyErr_Restore (ptype, pvalue, ptraceback);
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
110 PyErr_Print ();
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
111 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
112 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
113
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
114 return retval;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
115 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
116