annotate pyeval.cc @ 202:3fa99babc7b5

pyeval: preliminary support for returning pyobj Introduces static method `pyobj.fromPythonVarName(s)`, whereas the constructor itself now no longer deals with `__InOct__` itself. * pyeval.cc: return the id() instead for now * @pyobj/pyobj.m: change constructor, add method * @pyobj/dummy.m: add doctests
author Colin Macdonald <cbm@m.fsf.org>
date Fri, 20 May 2016 00:20:03 -0700
parents 28dc607532c2
children 61df785bd8b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
1 /*
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
2
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
3 Copyright (C) 2016 Colin B. Macdonald
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
4
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
5 This file is part of Pytave.
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
6
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
7 Pytave is free software; you can redistribute it and/or modify it
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
10 option) any later version.
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
11
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
12 Pytave is distributed in the hope that it will be useful, but WITHOUT
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
15 for more details.
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
16
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
18 along with Pytave; see the file COPYING. If not, see
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
20
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
21 */
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
22
162
df7b4a5ab542 Apply recent project changes to pyeval/pyexec, clean up
Mike Miller <mtmiller@octave.org>
parents: 161
diff changeset
23 #if defined (HAVE_CONFIG_H)
df7b4a5ab542 Apply recent project changes to pyeval/pyexec, clean up
Mike Miller <mtmiller@octave.org>
parents: 161
diff changeset
24 # include <config.h>
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
25 #endif
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
26
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
27 #include <dlfcn.h>
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
28 #include <boost/python.hpp>
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
29 #include <boost/python/numeric.hpp>
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
30
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
31 #include <oct.h>
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
32
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
33 #define PYTAVE_DO_DECLARE_SYMBOL
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
34 #include "arrayobjectdefs.h"
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
35 #include "exceptions.h"
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
36 #include "python_to_octave.h"
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
37
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
38 using namespace boost::python;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
39
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
40 DEFUN_DLD (pyeval, args, nargout,
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
41 "-*- texinfo -*-\n\
173
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
42 @deftypefn {} {} pyeval (@var{expr})\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
43 @deftypefnx {} {@var{x} =} pyeval (@var{expr})\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
44 Evaluate a Python expression and return the result.\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
45 \n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
46 Examples:\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
47 @example\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
48 @group\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
49 pyeval (\"sys.version\")\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
50 @result{} ...\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
51 pyeval (\"dict(one=1, two=2)\")\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
52 @result{} scalar structure containing the fields:\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
53 two = 2\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
54 one = 1\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
55 @end group\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
56 @end example\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
57 @seealso{pycall, pyexec}\n\
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
58 @end deftypefn")
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
59 {
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
60 octave_value_list retval;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
61
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
62 int nargin = args.length ();
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
63
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
64 std::string code = args(0).string_value ();
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
65
202
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
66 std::string id;
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
67 object res;
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
68
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
69 Py_Initialize ();
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
70
202
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
71 object main_module = import ("__main__");
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
72 object main_namespace = main_module.attr ("__dict__");
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
73
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
74 try
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
75 {
202
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
76 res = eval (code.c_str (), main_namespace, main_namespace);
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
77 object builtins = main_module.attr ("__builtins__");
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
78 // hex(id(res))
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
79 object idtmp = builtins.attr("hex")(builtins.attr("id")(res));
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
80 id = extract<std::string> (idtmp);
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
81 //std::cerr << "got it: " << id << std::endl;
162
df7b4a5ab542 Apply recent project changes to pyeval/pyexec, clean up
Mike Miller <mtmiller@octave.org>
parents: 161
diff changeset
82
df7b4a5ab542 Apply recent project changes to pyeval/pyexec, clean up
Mike Miller <mtmiller@octave.org>
parents: 161
diff changeset
83 // FIXME: currently, we cannot return the raw object to octave...
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
84 if (! res.is_none ())
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
85 {
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
86 octave_value val;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
87 pytave::pyobj_to_octvalue (val, res);
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
88 retval(0) = val;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
89 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
90 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
91 catch (pytave::object_convert_exception const &)
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
92 {
202
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
93 printf ("pyeval: could not convert return value to Octave-native object, making pyobj...\n");
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
94 // Ensure we have a __InOct__ dict, and then put `res` into it
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
95 exec ("if not (\"__InOct__\" in vars() or \"__InOct__\" in globals()):\n"
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
96 " __InOct__ = dict()\n",
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
97 main_namespace, main_namespace);
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
98 main_namespace["__InOct__"][id] = res;
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
99 //retval(0) = pyobj(id);
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
100 // FIXME: how to do the above? For now, just return the string
3fa99babc7b5 pyeval: preliminary support for returning pyobj
Colin Macdonald <cbm@m.fsf.org>
parents: 173
diff changeset
101 retval(0) = id;
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
102 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
103 catch (error_already_set const &)
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
104 {
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
105 PyObject *ptype, *pvalue, *ptraceback;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
106 PyErr_Fetch (&ptype, &pvalue, &ptraceback);
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
107
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
108 try
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
109 {
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
110 std::string message = extract<std::string> (pvalue);
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
111 error ("pyeval: %s", message.c_str ());
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
112 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
113 catch (error_already_set const &)
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
114 {
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
115 PyErr_Restore (ptype, pvalue, ptraceback);
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
116 PyErr_Print ();
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
117 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
118 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
119
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
120 return retval;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
121 }