comparison oct-py-util.cc @ 397:fc0fb94161de

Add a wrapper for PyObject pointers (fixes issue #81) * oct-py-object.h: Added the new class python_object * oct-py-util.cc: Edited the existing code to use the new wrapper * Makefile.am: Add the new header to the list of headers
author Abhinav Tripathi <genuinelucifer@gmail.com>
date Thu, 13 Apr 2017 02:10:48 -0700
parents 668fcb0f68ef
children 3905052ebe1d
comparison
equal deleted inserted replaced
396:a1fb6575f6dd 397:fc0fb94161de
27 #include <oct.h> 27 #include <oct.h>
28 #include <octave/parse.h> 28 #include <octave/parse.h>
29 29
30 #include "oct-py-types.h" 30 #include "oct-py-types.h"
31 #include "oct-py-util.h" 31 #include "oct-py-util.h"
32 #include "oct-py-object.h"
32 33
33 // FIXME: only here for boost::python::error_already_set 34 // FIXME: only here for boost::python::error_already_set
34 #include <boost/python.hpp> 35 #include <boost/python.hpp>
35 36
36 namespace pytave 37 namespace pytave
70 } 71 }
71 72
72 PyObject * 73 PyObject *
73 py_find_function (const std::string& module, const std::string& name) 74 py_find_function (const std::string& module, const std::string& name)
74 { 75 {
75 PyObject *mod = py_import_module (module); 76 python_object mod = py_import_module (module);
76 PyObject *func = py_find_function (mod, name); 77 PyObject *func = py_find_function (mod, name);
77 Py_XDECREF (mod);
78 return func; 78 return func;
79 } 79 }
80 80
81 PyObject * 81 PyObject *
82 py_find_function (const std::string& name) 82 py_find_function (const std::string& name)
98 } 98 }
99 99
100 PyObject * 100 PyObject *
101 py_find_type (const std::string& name) 101 py_find_type (const std::string& name)
102 { 102 {
103 PyObject *obj = py_find_function (name); 103 python_object obj = py_find_function (name);
104 if (obj && PyType_Check (obj)) 104 if (obj && PyType_Check (obj))
105 return obj; 105 return obj.release ();
106 106
107 Py_XDECREF (obj);
108 return 0; 107 return 0;
109 } 108 }
110 109
111 PyObject * 110 PyObject *
112 py_import_module (const std::string& name) 111 py_import_module (const std::string& name)
126 std::string 125 std::string
127 py_object_class_name (PyObject *obj) 126 py_object_class_name (PyObject *obj)
128 { 127 {
129 std::string retval; 128 std::string retval;
130 129
131 PyObject *type = obj ? PyObject_GetAttrString (obj, "__class__") : 0; 130 python_object type = obj ? PyObject_GetAttrString (obj, "__class__") : 0;
132 if (type) 131 if (type)
133 { 132 {
134 PyObject *mod = PyObject_GetAttrString (type, "__module__"); 133 python_object mod = PyObject_GetAttrString (type, "__module__");
135 134
136 PyObject *name = 0; 135 python_object name;
137 if (PyObject_HasAttrString (type, "__qualname__")) 136 if (PyObject_HasAttrString (type, "__qualname__"))
138 name = PyObject_GetAttrString (type, "__qualname__"); 137 name = PyObject_GetAttrString (type, "__qualname__");
139 else 138 else
140 name = PyObject_GetAttrString (type, "__name__"); 139 name = PyObject_GetAttrString (type, "__name__");
141 140
142 std::string mod_str = (mod && mod != Py_None) ? extract_py_str (mod) : ""; 141 std::string mod_str = !mod.is_none () ? extract_py_str (mod) : "";
143 std::string name_str = name ? extract_py_str (name) : ""; 142 std::string name_str = name ? extract_py_str (name) : "";
144
145 Py_DECREF (type);
146 Py_XDECREF (mod);
147 Py_XDECREF (name);
148 143
149 if (mod_str.empty () || mod_str == py_builtins_module_name ()) 144 if (mod_str.empty () || mod_str == py_builtins_module_name ())
150 retval = name_str; 145 retval = name_str;
151 else 146 else
152 retval = mod_str + "." + name_str; 147 retval = mod_str + "." + name_str;
161 inline PyObject * 156 inline PyObject *
162 py_objstore () 157 py_objstore ()
163 { 158 {
164 if (! objstore) 159 if (! objstore)
165 { 160 {
166 PyObject *main = py_import_module ("__main__"); 161 python_object main = py_import_module ("__main__");
167 PyObject *ns = main ? PyObject_GetAttrString (main, "__dict__") : 0; 162 python_object ns = main ? PyObject_GetAttrString (main, "__dict__") : 0;
168 PyObject *dict = ns ? PyDict_GetItemString (ns, "_in_octave") : 0; 163 PyObject *dict = ns ? PyDict_GetItemString (ns, "_in_octave") : 0;
169 164
170 if (dict) 165 if (dict)
171 Py_INCREF (dict); 166 Py_INCREF (dict);
172 167
186 } 181 }
187 182
188 void 183 void
189 py_objstore_del (uint64_t key) 184 py_objstore_del (uint64_t key)
190 { 185 {
191 PyObject *store = py_objstore (); 186 python_object store = py_objstore ();
192 PyObject *key_obj = make_py_int (key); 187 python_object key_obj = make_py_int (key);
193 PyObject *key_fmt = PyNumber_ToBase (key_obj, 16); 188 python_object key_fmt = PyNumber_ToBase (key_obj, 16);
194 PyDict_DelItem (store, key_fmt); 189 PyDict_DelItem (store, key_fmt);
195 Py_DECREF (key_fmt); 190 store.release ();
196 Py_DECREF (key_obj);
197 } 191 }
198 192
199 PyObject * 193 PyObject *
200 py_objstore_get (uint64_t key) 194 py_objstore_get (uint64_t key)
201 { 195 {
202 PyObject *store = py_objstore (); 196 python_object store = py_objstore ();
203 PyObject *key_obj = make_py_int (key); 197 python_object key_obj = make_py_int (key);
204 PyObject *key_fmt = PyNumber_ToBase (key_obj, 16); 198 python_object key_fmt = PyNumber_ToBase (key_obj, 16);
205 PyObject *obj = PyDict_GetItem (store, key_fmt); 199 PyObject *obj = PyDict_GetItem (store, key_fmt);
206 Py_DECREF (key_fmt); 200 store.release ();
207 Py_DECREF (key_obj);
208 if (obj) 201 if (obj)
209 Py_INCREF (obj); 202 Py_INCREF (obj);
210 return obj; 203 return obj;
211 } 204 }
212 205
213 uint64_t 206 uint64_t
214 py_objstore_put (PyObject *obj) 207 py_objstore_put (PyObject *obj)
215 { 208 {
216 PyObject *store = py_objstore (); 209 python_object store = py_objstore ();
217 uint64_t key = reinterpret_cast<uint64_t> (obj); 210 uint64_t key = reinterpret_cast<uint64_t> (obj);
218 PyObject *key_obj = make_py_int (key); 211 python_object key_obj = make_py_int (key);
219 PyObject *key_fmt = PyNumber_ToBase (key_obj, 16); 212 python_object key_fmt = PyNumber_ToBase (key_obj, 16);
220 PyDict_SetItem (store, key_fmt, obj); 213 PyDict_SetItem (store, key_fmt, obj);
221 Py_DECREF (key_fmt); 214 store.release ();
222 Py_DECREF (key_obj);
223 return key; 215 return key;
224 } 216 }
225 217
226 octave_value 218 octave_value
227 pyobject_wrap_object (PyObject *obj) 219 pyobject_wrap_object (PyObject *obj)