annotate py.cc @ 150:a3dc9d24ae38

maint: Add myself to copyright notices * Makefile.am, arrayobjectdefs.h, configure.ac, exceptions.{cc,h}, octave_to_python.{cc,h}, package/__init__.py, package/pytave.py, py.cc, pytave.cc, python_to_octave.{cc,h}: Add myself to copyright notices.
author Mike Miller <mtmiller@octave.org>
date Tue, 05 Apr 2016 11:46:38 -0700
parents b12908ffa6df
children eab5c6026303
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
150
a3dc9d24ae38 maint: Add myself to copyright notices
Mike Miller <mtmiller@octave.org>
parents: 139
diff changeset
3 Copyright (C) 2015-2016 Mike Miller
115
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
139
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 132
diff changeset
7 Pytave is free software: you can redistribute it and/or modify it
115
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
139
b12908ffa6df maint: Use Octave style for file copyright block, delete editor footer
Mike Miller <mtmiller@octave.org>
parents: 132
diff changeset
9 Free Software Foundation, either version 3 of the License, or (at your
115
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
132
386772f4e12d Clean up includes thanks to Octave header changes
Mike Miller <mtmiller@octave.org>
parents: 116
diff changeset
23 #if defined (HAVE_CONFIG_H)
386772f4e12d Clean up includes thanks to Octave header changes
Mike Miller <mtmiller@octave.org>
parents: 116
diff changeset
24 # include <config.h>
115
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 #include <oct.h>
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
31
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
32 #define PYTAVE_DO_DECLARE_SYMBOL
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
33 #include "arrayobjectdefs.h"
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
34 #include "exceptions.h"
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
35 #include "python_to_octave.h"
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
36
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
37 using namespace boost::python;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
38
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
39 DEFUN_DLD (py, args, nargout,
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
40 "-*- texinfo -*-\n\
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
41 @deftypefn {Loadable Function} py (@var{func})\n\
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
42 @enddeftypefn")
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 octave_value_list retval;
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 int nargin = args.length ();
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
47
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
48 if (nargin != 1)
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
49 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
50 print_usage ();
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
51 return 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
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
54 std::string module;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
55 std::string func = args(0).string_value ();
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 size_t idx = func.rfind (".");
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
58 if (idx != std::string::npos)
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 module = func.substr (0, idx);
116
3a35bb85ce52 py: Experiment with handle to target function
Mike Miller <mtmiller@octave.org>
parents: 115
diff changeset
61 func = func.substr (idx + 1);
115
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
62 }
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 Py_Initialize ();
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
65
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
66 try
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
67 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
68 object main_module = import ("__main__");
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
69 object main_namespace = main_module.attr ("__dict__");
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
70
116
3a35bb85ce52 py: Experiment with handle to target function
Mike Miller <mtmiller@octave.org>
parents: 115
diff changeset
71 object mod = (module.empty ()) ? main_module : import (module.c_str ());
3a35bb85ce52 py: Experiment with handle to target function
Mike Miller <mtmiller@octave.org>
parents: 115
diff changeset
72 object callable = mod.attr (func.c_str ());
3a35bb85ce52 py: Experiment with handle to target function
Mike Miller <mtmiller@octave.org>
parents: 115
diff changeset
73 object res = callable ();
115
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
74
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
75 if (! res.is_none ())
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 octave_value val;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
78 pytave::pyobj_to_octvalue (val, res);
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
79 retval(0) = val;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
80 }
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 catch (pytave::object_convert_exception const &)
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
83 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
84 error ("py: error in return value type conversion");
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
85 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
86 catch (error_already_set const &)
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
87 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
88 std::cerr << "in here" << std::endl;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
89 PyObject *ptype, *pvalue, *ptraceback;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
90 PyErr_Fetch (&ptype, &pvalue, &ptraceback);
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 try
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 std::string message = extract<std::string> (pvalue);
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
95 error ("py: %s", message.c_str ());
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
96 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
97 catch (error_already_set const &)
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
98 {
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
99 PyErr_Restore (ptype, pvalue, ptraceback);
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
100 PyErr_Print ();
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 }
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 return retval;
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
105 }
00d19f71c9ca Initial implementation of py oct-file
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
106