Mercurial > pytave
comparison oct-py-util.cc @ 402:c4b78e449c62
maint: indent functions declared in the pytave namespace
* oct-py-eval.cc, oct-py-eval.h, oct-py-types.cc, oct-py-types.h,
oct-py-util.cc, oct-py-util.h: Indent functions declared in the pytave
namespace, adjust line wrap where necessary.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Fri, 28 Apr 2017 14:07:57 -0700 |
parents | 3905052ebe1d |
children | 3644df6564bc |
comparison
equal
deleted
inserted
replaced
401:3a64a336d214 | 402:c4b78e449c62 |
---|---|
35 #include <boost/python.hpp> | 35 #include <boost/python.hpp> |
36 | 36 |
37 namespace pytave | 37 namespace pytave |
38 { | 38 { |
39 | 39 |
40 inline std::string | 40 inline std::string |
41 py_builtins_module_name () | 41 py_builtins_module_name () |
42 { | 42 { |
43 #if PY_VERSION_HEX >= 0x03000000 | 43 #if PY_VERSION_HEX >= 0x03000000 |
44 return "builtins"; | 44 return "builtins"; |
45 #else | 45 #else |
46 return "__builtin__"; | 46 return "__builtin__"; |
47 #endif | 47 #endif |
48 } | |
49 | |
50 PyObject * | |
51 py_builtins_module () | |
52 { | |
53 return py_import_module (py_builtins_module_name ()); | |
54 } | |
55 | |
56 PyObject * | |
57 py_find_function (PyObject *module, const std::string& name) | |
58 { | |
59 if (module && PyModule_Check (module)) | |
60 { | |
61 PyObject *obj = PyObject_GetAttrString (module, name.c_str ()); | |
62 if (obj && ! PyCallable_Check (obj)) | |
63 { | |
64 Py_CLEAR (obj); | |
65 } | |
66 | |
67 return obj; | |
68 } | |
69 | |
70 return 0; | |
71 } | |
72 | |
73 PyObject * | |
74 py_find_function (const std::string& module, const std::string& name) | |
75 { | |
76 python_object mod = py_import_module (module); | |
77 PyObject *func = py_find_function (mod, name); | |
78 return func; | |
79 } | |
80 | |
81 PyObject * | |
82 py_find_function (const std::string& name) | |
83 { | |
84 std::string::size_type idx = name.rfind ("."); | |
85 if (idx == std::string::npos) | |
86 { | |
87 PyObject *func = py_find_function ("__main__", name); | |
88 if (! func) | |
89 func = py_find_function (py_builtins_module (), name); | |
90 return func; | |
91 } | |
92 else | |
93 { | |
94 std::string module = name.substr (0, idx); | |
95 std::string function = name.substr (idx + 1); | |
96 return py_find_function (module, function); | |
97 } | |
98 } | |
99 | |
100 PyObject * | |
101 py_find_type (const std::string& name) | |
102 { | |
103 python_object obj = py_find_function (name); | |
104 if (obj && PyType_Check (obj)) | |
105 return obj.release (); | |
106 | |
107 return 0; | |
108 } | |
109 | |
110 PyObject * | |
111 py_import_module (const std::string& name) | |
112 { | |
113 return PyImport_ImportModule (name.c_str ()); | |
114 } | |
115 | |
116 bool | |
117 py_isinstance (PyObject *obj, PyObject *type) | |
118 { | |
119 if (obj && type) | |
120 return static_cast<bool> (PyObject_IsInstance (obj, type)); | |
121 | |
122 return false; | |
123 } | |
124 | |
125 std::string | |
126 py_object_class_name (PyObject *obj) | |
127 { | |
128 std::string retval; | |
129 | |
130 python_object type = obj ? PyObject_GetAttrString (obj, "__class__") : 0; | |
131 if (type) | |
132 { | |
133 python_object mod = PyObject_GetAttrString (type, "__module__"); | |
134 | |
135 python_object name; | |
136 if (PyObject_HasAttrString (type, "__qualname__")) | |
137 name = PyObject_GetAttrString (type, "__qualname__"); | |
138 else | |
139 name = PyObject_GetAttrString (type, "__name__"); | |
140 | |
141 std::string mod_str = !mod.is_none () ? extract_py_str (mod) : ""; | |
142 std::string name_str = name ? extract_py_str (name) : ""; | |
143 | |
144 if (mod_str.empty () || mod_str == py_builtins_module_name ()) | |
145 retval = name_str; | |
146 else | |
147 retval = mod_str + "." + name_str; | |
148 } | |
149 | |
150 return retval; | |
151 } | |
152 | |
153 // FIXME: could make this into a class/singleton wrapper a la Octave core | |
154 PyObject *objstore = 0; | |
155 | |
156 inline PyObject * | |
157 py_objstore () | |
158 { | |
159 if (! objstore) | |
160 { | |
161 python_object main = py_import_module ("__main__"); | |
162 python_object ns = main ? PyObject_GetAttrString (main, "__dict__") : 0; | |
163 PyObject *dict = ns ? PyDict_GetItemString (ns, "_in_octave") : 0; | |
164 | |
165 if (dict) | |
166 Py_INCREF (dict); | |
167 | |
168 if (! dict) | |
169 { | |
170 dict = PyDict_New (); | |
171 if (dict && ns) | |
172 PyDict_SetItemString (ns, "_in_octave", dict); | |
173 } | |
174 | |
175 if (! dict) | |
176 throw boost::python::error_already_set (); | |
177 | |
178 objstore = dict; | |
179 } | |
180 return objstore; | |
181 } | |
182 | |
183 void | |
184 py_objstore_del (uint64_t key) | |
185 { | |
186 python_object store = py_objstore (); | |
187 python_object key_obj = make_py_int (key); | |
188 python_object key_fmt = PyNumber_ToBase (key_obj, 16); | |
189 PyDict_DelItem (store, key_fmt); | |
190 store.release (); | |
191 } | |
192 | |
193 PyObject * | |
194 py_objstore_get (uint64_t key) | |
195 { | |
196 python_object store = py_objstore (); | |
197 python_object key_obj = make_py_int (key); | |
198 python_object key_fmt = PyNumber_ToBase (key_obj, 16); | |
199 PyObject *obj = PyDict_GetItem (store, key_fmt); | |
200 store.release (); | |
201 if (obj) | |
202 Py_INCREF (obj); | |
203 return obj; | |
204 } | |
205 | |
206 uint64_t | |
207 py_objstore_put (PyObject *obj) | |
208 { | |
209 python_object store = py_objstore (); | |
210 uint64_t key = reinterpret_cast<uint64_t> (obj); | |
211 python_object key_obj = make_py_int (key); | |
212 python_object key_fmt = PyNumber_ToBase (key_obj, 16); | |
213 PyDict_SetItem (store, key_fmt, obj); | |
214 store.release (); | |
215 return key; | |
216 } | |
217 | |
218 octave_value | |
219 pyobject_wrap_object (PyObject *obj) | |
220 { | |
221 uint64_t key = py_objstore_put (obj); | |
222 octave_value_list out = feval ("pyobject", ovl (0, octave_uint64 (key)), 1); | |
223 return out(0); | |
224 } | |
225 | |
226 PyObject * | |
227 pyobject_unwrap_object (const octave_value& value) | |
228 { | |
229 if (value.is_object () && value.class_name () == "pyobject") | |
230 { | |
231 octave_value_list out = feval ("id", ovl (value), 1); | |
232 uint64_t key = out(0).uint64_scalar_value (); | |
233 return py_objstore_get (key); | |
234 } | |
235 | |
236 return 0; | |
237 } | |
238 | |
239 bool | |
240 is_py_kwargs_argument (PyObject *obj) | |
241 { | |
242 if (obj && py_object_class_name (obj) == "__main__._OctaveKwargs" | |
243 && PyObject_HasAttrString (obj, "is_kwargs_argument")) | |
244 { | |
245 PyObject *flag = PyObject_GetAttrString (obj, "is_kwargs_argument"); | |
246 if (flag && PyBool_Check (flag) && PyObject_IsTrue (flag)) | |
247 return true; | |
248 } | |
249 return false; | |
250 } | |
251 | |
252 PyObject * | |
253 update_py_dict (PyObject *dict_orig, PyObject *dict_new) | |
254 { | |
255 PyObject *dict = dict_orig ? dict_orig : PyDict_New (); | |
256 PyDict_Update (dict, dict_new); | |
257 return dict; | |
258 } | |
259 | |
48 } | 260 } |
49 | |
50 PyObject * | |
51 py_builtins_module () | |
52 { | |
53 return py_import_module (py_builtins_module_name ()); | |
54 } | |
55 | |
56 PyObject * | |
57 py_find_function (PyObject *module, const std::string& name) | |
58 { | |
59 if (module && PyModule_Check (module)) | |
60 { | |
61 PyObject *obj = PyObject_GetAttrString (module, name.c_str ()); | |
62 if (obj && ! PyCallable_Check (obj)) | |
63 { | |
64 Py_CLEAR (obj); | |
65 } | |
66 | |
67 return obj; | |
68 } | |
69 | |
70 return 0; | |
71 } | |
72 | |
73 PyObject * | |
74 py_find_function (const std::string& module, const std::string& name) | |
75 { | |
76 python_object mod = py_import_module (module); | |
77 PyObject *func = py_find_function (mod, name); | |
78 return func; | |
79 } | |
80 | |
81 PyObject * | |
82 py_find_function (const std::string& name) | |
83 { | |
84 std::string::size_type idx = name.rfind ("."); | |
85 if (idx == std::string::npos) | |
86 { | |
87 PyObject *func = py_find_function ("__main__", name); | |
88 if (! func) | |
89 func = py_find_function (py_builtins_module (), name); | |
90 return func; | |
91 } | |
92 else | |
93 { | |
94 std::string module = name.substr (0, idx); | |
95 std::string function = name.substr (idx + 1); | |
96 return py_find_function (module, function); | |
97 } | |
98 } | |
99 | |
100 PyObject * | |
101 py_find_type (const std::string& name) | |
102 { | |
103 python_object obj = py_find_function (name); | |
104 if (obj && PyType_Check (obj)) | |
105 return obj.release (); | |
106 | |
107 return 0; | |
108 } | |
109 | |
110 PyObject * | |
111 py_import_module (const std::string& name) | |
112 { | |
113 return PyImport_ImportModule (name.c_str ()); | |
114 } | |
115 | |
116 bool | |
117 py_isinstance (PyObject *obj, PyObject *type) | |
118 { | |
119 if (obj && type) | |
120 return static_cast<bool> (PyObject_IsInstance (obj, type)); | |
121 | |
122 return false; | |
123 } | |
124 | |
125 std::string | |
126 py_object_class_name (PyObject *obj) | |
127 { | |
128 std::string retval; | |
129 | |
130 python_object type = obj ? PyObject_GetAttrString (obj, "__class__") : 0; | |
131 if (type) | |
132 { | |
133 python_object mod = PyObject_GetAttrString (type, "__module__"); | |
134 | |
135 python_object name; | |
136 if (PyObject_HasAttrString (type, "__qualname__")) | |
137 name = PyObject_GetAttrString (type, "__qualname__"); | |
138 else | |
139 name = PyObject_GetAttrString (type, "__name__"); | |
140 | |
141 std::string mod_str = !mod.is_none () ? extract_py_str (mod) : ""; | |
142 std::string name_str = name ? extract_py_str (name) : ""; | |
143 | |
144 if (mod_str.empty () || mod_str == py_builtins_module_name ()) | |
145 retval = name_str; | |
146 else | |
147 retval = mod_str + "." + name_str; | |
148 } | |
149 | |
150 return retval; | |
151 } | |
152 | |
153 // FIXME: could make this into a class/singleton wrapper a la Octave core | |
154 PyObject *objstore = 0; | |
155 | |
156 inline PyObject * | |
157 py_objstore () | |
158 { | |
159 if (! objstore) | |
160 { | |
161 python_object main = py_import_module ("__main__"); | |
162 python_object ns = main ? PyObject_GetAttrString (main, "__dict__") : 0; | |
163 PyObject *dict = ns ? PyDict_GetItemString (ns, "_in_octave") : 0; | |
164 | |
165 if (dict) | |
166 Py_INCREF (dict); | |
167 | |
168 if (! dict) | |
169 { | |
170 dict = PyDict_New (); | |
171 if (dict && ns) | |
172 PyDict_SetItemString (ns, "_in_octave", dict); | |
173 } | |
174 | |
175 if (! dict) | |
176 throw boost::python::error_already_set (); | |
177 | |
178 objstore = dict; | |
179 } | |
180 return objstore; | |
181 } | |
182 | |
183 void | |
184 py_objstore_del (uint64_t key) | |
185 { | |
186 python_object store = py_objstore (); | |
187 python_object key_obj = make_py_int (key); | |
188 python_object key_fmt = PyNumber_ToBase (key_obj, 16); | |
189 PyDict_DelItem (store, key_fmt); | |
190 store.release (); | |
191 } | |
192 | |
193 PyObject * | |
194 py_objstore_get (uint64_t key) | |
195 { | |
196 python_object store = py_objstore (); | |
197 python_object key_obj = make_py_int (key); | |
198 python_object key_fmt = PyNumber_ToBase (key_obj, 16); | |
199 PyObject *obj = PyDict_GetItem (store, key_fmt); | |
200 store.release (); | |
201 if (obj) | |
202 Py_INCREF (obj); | |
203 return obj; | |
204 } | |
205 | |
206 uint64_t | |
207 py_objstore_put (PyObject *obj) | |
208 { | |
209 python_object store = py_objstore (); | |
210 uint64_t key = reinterpret_cast<uint64_t> (obj); | |
211 python_object key_obj = make_py_int (key); | |
212 python_object key_fmt = PyNumber_ToBase (key_obj, 16); | |
213 PyDict_SetItem (store, key_fmt, obj); | |
214 store.release (); | |
215 return key; | |
216 } | |
217 | |
218 octave_value | |
219 pyobject_wrap_object (PyObject *obj) | |
220 { | |
221 uint64_t key = py_objstore_put (obj); | |
222 octave_value_list out = feval ("pyobject", ovl (0, octave_uint64 (key)), 1); | |
223 return out(0); | |
224 } | |
225 | |
226 PyObject * | |
227 pyobject_unwrap_object (const octave_value& value) | |
228 { | |
229 if (value.is_object () && value.class_name () == "pyobject") | |
230 { | |
231 octave_value_list out = feval ("id", ovl (value), 1); | |
232 uint64_t key = out(0).uint64_scalar_value (); | |
233 return py_objstore_get (key); | |
234 } | |
235 | |
236 return 0; | |
237 } | |
238 | |
239 bool | |
240 is_py_kwargs_argument (PyObject *obj) | |
241 { | |
242 if (obj && py_object_class_name (obj) == "__main__._OctaveKwargs" | |
243 && PyObject_HasAttrString (obj, "is_kwargs_argument")) | |
244 { | |
245 PyObject *flag = PyObject_GetAttrString (obj, "is_kwargs_argument"); | |
246 if (flag && PyBool_Check (flag) && PyObject_IsTrue (flag)) | |
247 return true; | |
248 } | |
249 return false; | |
250 } | |
251 | |
252 PyObject * | |
253 update_py_dict (PyObject *dict_orig, PyObject *dict_new) | |
254 { | |
255 PyObject *dict = dict_orig ? dict_orig : PyDict_New (); | |
256 PyDict_Update (dict, dict_new); | |
257 return dict; | |
258 } | |
259 | |
260 } |