Mercurial > pytave
annotate pyexec.cc @ 411:3613ffbd52b2
Overhaul implicit conversion of arguments and return values
* oct-py-types.cc, oct-py-types.h (pytave::py_implicitly_convert_argument,
pytave::py_implicitly_convert_return_value): New functions.
* __py_struct_from_dict__.cc, oct-py-eval.cc, pycall.cc, pyeval.cc, pyexec.cc:
Use them instead of legacy conversion functions. Add necessary #includes,
remove #includes of legacy header files.
* @pyobject/subsasgn.m, @pyobject/subsref.m: Change %!tests that depend on
NumPy implicit conversion into %!xtests.
* octave_to_python.cc, octave_to_python.h, python_to_octave.cc,
python_to_octave.h: Delete, no longer used.
* Makefile.am (COMMON_SOURCE_FILES, PYTAVE_HEADER_FILES): Remove the files.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Wed, 03 May 2017 16:30:45 -0700 |
parents | 95c6ad0be828 |
children | 9bf8ba050122 |
rev | line source |
---|---|
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 | |
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 | 25 #endif |
26 | |
27 #include <dlfcn.h> | |
28 #include <boost/python.hpp> | |
29 | |
30 #include <oct.h> | |
31 | |
32 #include "exceptions.h" | |
335
9b862844e6b7
Enable Python print function in evaluated expressions (fixes issue #48)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
33 #include "oct-py-eval.h" |
405
478d83448b0b
Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents:
403
diff
changeset
|
34 #include "oct-py-init.h" |
331
cee203ea6245
Rename oct-py-util.cc from pytave_utils.cc
Mike Miller <mtmiller@octave.org>
parents:
316
diff
changeset
|
35 #include "oct-py-util.h" |
161 | 36 |
37 DEFUN_DLD (pyexec, args, nargout, | |
38 "-*- texinfo -*-\n\ | |
316
a140160b2319
doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents:
307
diff
changeset
|
39 @deftypefn {} {} pyexec (@var{expr})\n\ |
a140160b2319
doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents:
307
diff
changeset
|
40 @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
|
41 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
|
42 \n\ |
a140160b2319
doc: clean up pyeval and pyexec doc strings
Mike Miller <mtmiller@octave.org>
parents:
307
diff
changeset
|
43 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
|
44 @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
|
45 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
|
46 \n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
47 Examples:\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
48 @example\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
49 @group\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
50 pyexec (\"print(42)\")\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
51 @print{} 42\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
52 @end group\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
53 @end example\n\ |
28dc607532c2
doc: Clarify and clean up docstrings for all oct files
Mike Miller <mtmiller@octave.org>
parents:
162
diff
changeset
|
54 @seealso{pycall, pyeval}\n\ |
161 | 55 @end deftypefn") |
56 { | |
57 octave_value_list retval; | |
58 | |
59 int nargin = args.length (); | |
60 | |
307
b4e56f7255f7
Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
232
diff
changeset
|
61 if (nargin < 1 || nargin > 2) |
161 | 62 { |
63 print_usage (); | |
64 return retval; | |
65 } | |
66 | |
67 std::string code = args(0).string_value (); | |
68 | |
405
478d83448b0b
Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents:
403
diff
changeset
|
69 pytave::py_init (); |
161 | 70 |
403
3644df6564bc
maint: use C++11 nullptr rather than 0 or NULL
Mike Miller <mtmiller@octave.org>
parents:
364
diff
changeset
|
71 PyObject *local_namespace = nullptr; |
307
b4e56f7255f7
Add option to specify namespace when calling pyexec and pyeval (fixes issue #25)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
232
diff
changeset
|
72 if (nargin > 1) |
364
1470ed26917a
Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents:
335
diff
changeset
|
73 { |
1470ed26917a
Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents:
335
diff
changeset
|
74 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
|
75 if (! local_namespace) |
1470ed26917a
Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents:
335
diff
changeset
|
76 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
|
77 } |
226
382bb1d91239
maint: import standard Python names consistently
Mike Miller <mtmiller@octave.org>
parents:
173
diff
changeset
|
78 |
161 | 79 try |
80 { | |
162
df7b4a5ab542
Apply recent project changes to pyeval/pyexec, clean up
Mike Miller <mtmiller@octave.org>
parents:
161
diff
changeset
|
81 // FIXME: figure out exec return code: |
161 | 82 // 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
|
83 pytave::py_exec_string (code, 0, local_namespace); |
161 | 84 } |
85 catch (pytave::object_convert_exception const &) | |
86 { | |
87 error ("pyexec: error in return value type conversion"); | |
88 } | |
406
16e79a1e96b8
Eliminate all remaining uses of "using namespace"
Mike Miller <mtmiller@octave.org>
parents:
405
diff
changeset
|
89 catch (boost::python::error_already_set const &) |
161 | 90 { |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
173
diff
changeset
|
91 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
|
92 error ("pyexec: %s", message.c_str ()); |
161 | 93 } |
94 return retval; | |
95 } | |
231
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
173
diff
changeset
|
96 |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
173
diff
changeset
|
97 /* |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
173
diff
changeset
|
98 %!error <NameError> |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
173
diff
changeset
|
99 %! pyexec ("raise NameError ('oops')") |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
173
diff
changeset
|
100 |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
173
diff
changeset
|
101 %!error <AttributeError> |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
173
diff
changeset
|
102 %! pyexec ("import sys") |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
173
diff
changeset
|
103 %! pyexec ("sys.no_such_thing") |
6fffa6219b2c
Properly extract exception text from Python (fixes issue #24)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
173
diff
changeset
|
104 */ |