annotate pyexec.cc @ 394:b3e63c620448

ci: use a separate build directory * bitbucket-pipelines.yml: Move Python 2 configure/build/test to a separate directory.
author Mike Miller <mtmiller@octave.org>
date Fri, 07 Apr 2017 10:37:56 -0700
parents 1470ed26917a
children 3644df6564bc
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"
335
9b862844e6b7 Enable Python print function in evaluated expressions (fixes issue #48)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
36 #include "oct-py-eval.h"
331
cee203ea6245 Rename oct-py-util.cc from pytave_utils.cc
Mike Miller <mtmiller@octave.org>
parents: 316
diff changeset
37 #include "oct-py-util.h"
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
38 #include "python_to_octave.h"
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 using namespace boost::python;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
41
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
42 DEFUN_DLD (pyexec, args, nargout,
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
43 "-*- texinfo -*-\n\
316
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
44 @deftypefn {} {} pyexec (@var{expr})\n\
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
45 @deftypefnx {} {} pyexec (@var{expr}, @var{localns})\n\
173
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
46 Execute a Python expression or block of code.\n\
316
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
47 \n\
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
48 When called with an optional second argument, @var{localns} is a\n\
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
49 @code{py.dict} that acts as the namespace for any assignments or other\n\
a140160b2319 doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents: 307
diff changeset
50 side effects of the expression.\n\
173
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
51 \n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
52 Examples:\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
53 @example\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
54 @group\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
55 pyexec (\"print(42)\")\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
56 @print{} 42\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
57 @end group\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
58 @end example\n\
28dc607532c2 doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents: 162
diff changeset
59 @seealso{pycall, pyeval}\n\
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
60 @end deftypefn")
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 octave_value_list retval;
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 int nargin = args.length ();
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
65
307
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 232
diff changeset
66 if (nargin < 1 || nargin > 2)
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
67 {
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
68 print_usage ();
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
69 return retval;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
70 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
71
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
72 std::string code = args(0).string_value ();
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
73
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
74 Py_Initialize ();
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
75
364
1470ed26917a Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents: 335
diff changeset
76 PyObject *local_namespace = 0;
307
b4e56f7255f7 Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 232
diff changeset
77 if (nargin > 1)
364
1470ed26917a Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents: 335
diff changeset
78 {
1470ed26917a Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents: 335
diff changeset
79 local_namespace = pytave::pyobject_unwrap_object (args(1));
1470ed26917a Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents: 335
diff changeset
80 if (! local_namespace)
1470ed26917a Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents: 335
diff changeset
81 error ("pyexec: NAMESPACE must be a valid Python reference");
1470ed26917a Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents: 335
diff changeset
82 }
226
382bb1d91239 maint: import standard Python names consistently
Mike Miller <mtmiller@octave.org>
parents: 173
diff changeset
83
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
84 try
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
85 {
162
df7b4a5ab542 Apply recent project changes to pyeval/pyexec, clean up
Mike Miller <mtmiller@octave.org>
parents: 161
diff changeset
86 // FIXME: figure out exec return code:
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
87 // http://www.boost.org/doc/libs/1_38_0/libs/python/doc/v2/exec.html
364
1470ed26917a Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents: 335
diff changeset
88 pytave::py_exec_string (code, 0, local_namespace);
161
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 catch (pytave::object_convert_exception const &)
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
91 {
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
92 error ("pyexec: error in return value type conversion");
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
93 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
94 catch (error_already_set const &)
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
95 {
231
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 173
diff changeset
96 std::string message = pytave::fetch_exception_message ();
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 173
diff changeset
97 error ("pyexec: %s", message.c_str ());
161
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
98 }
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
99 return retval;
dfea7eee7749 add pyexec and pyeval commands
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
100 }
231
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 173
diff changeset
101
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 173
diff changeset
102 /*
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 173
diff changeset
103 %!error <NameError>
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 173
diff changeset
104 %! pyexec ("raise NameError ('oops')")
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 173
diff changeset
105
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 173
diff changeset
106 %!error <AttributeError>
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 173
diff changeset
107 %! pyexec ("import sys")
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 173
diff changeset
108 %! pyexec ("sys.no_such_thing")
6fffa6219b2c Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 173
diff changeset
109 */