Mercurial > pytave
annotate pytave_utils.cc @ 306:2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
* pytave_utils.{h,cc}: Have the 2 functions to get builtins module and an
object from python as utils.
* Makefile.am: Add pytave_utils{h,cc} to the build process.
* pycall.cc: Use pytave_utils to get FUNC from python and remove unused code.
* python_to_octave.cc: Use pytave_utils to get the builtins module
author | Abhinav Tripathi <genuinelucifer@gmail.com> |
---|---|
date | Tue, 09 Aug 2016 11:27:38 -0700 |
parents | |
children |
rev | line source |
---|---|
306
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
1 /* |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
2 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
3 Copyright (C) 2016 Abhinav Tripathi |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
4 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
5 This file is part of Pytave. |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
6 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
7 Pytave is free software; you can redistribute it and/or modify it |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
8 under the terms of the GNU General Public License as published by the |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
9 Free Software Foundation; either version 3 of the License, or (at your |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
10 option) any later version. |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
11 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
12 Pytave is distributed in the hope that it will be useful, but WITHOUT |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
15 for more details. |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
16 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
17 You should have received a copy of the GNU General Public License |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
18 along with Pytave; see the file COPYING. If not, see |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
19 <http://www.gnu.org/licenses/>. |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
20 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
21 */ |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
22 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
23 #if defined (HAVE_CONFIG_H) |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
24 # include <config.h> |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
25 #endif |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
26 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
27 #include <oct.h> |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
28 #include <octave/parse.h> |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
29 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
30 #include "pytave_utils.h" |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
31 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
32 using namespace boost::python; |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
33 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
34 namespace pytave |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
35 { |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
36 void get_builtins_module (boost::python::object& builtins_module) |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
37 { |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
38 #if PY_VERSION_HEX >= 0x03000000 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
39 builtins_module = import ("builtins"); |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
40 #else |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
41 builtins_module = import ("__builtin__"); |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
42 #endif |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
43 } |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
44 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
45 void get_object_from_python (const octave_value& oct_value, |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
46 boost::python::object& py_object) |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
47 { |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
48 bool by_name; |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
49 if (oct_value.is_string ()) |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
50 by_name = true; |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
51 else if (oct_value.is_object () && oct_value.class_name () == "pyobject") |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
52 by_name = false; |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
53 else |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
54 { |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
55 py_object = boost::python::object(); //None |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
56 return; |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
57 } |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
58 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
59 object main_module = import ("__main__"); |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
60 object builtins_module; |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
61 get_builtins_module (builtins_module); |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
62 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
63 if (by_name) |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
64 { |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
65 std::string module; |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
66 std::string func = oct_value.string_value (); |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
67 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
68 size_t idx = func.rfind ("."); |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
69 if (idx != std::string::npos) |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
70 { |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
71 module = func.substr (0, idx); |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
72 func = func.substr (idx + 1); |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
73 } |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
74 |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
75 object mod; |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
76 if (module.empty ()) |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
77 { |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
78 if (PyObject_HasAttrString (main_module.ptr (), func.c_str ())) |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
79 mod = main_module; |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
80 else |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
81 mod = builtins_module; |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
82 } |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
83 else |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
84 mod = import (module.c_str ()); |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
85 py_object = mod.attr (func.c_str ()); |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
86 } |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
87 else |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
88 { |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
89 octave_value_list tmp = feval ("getid", ovl (oct_value), 1); |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
90 std::string hexid = tmp(0).string_value (); |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
91 py_object = main_module.attr ("__InOct__")[hexid]; |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
92 } |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
93 } |
2ecae5c6eeb6
Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff
changeset
|
94 } |