Mercurial > pytave
comparison oct-py-util.cc @ 357:ebd83497ebda
Add utility functions to look up Python modules, functions, and types
* oct-py-util.cc, oct-py-util.h (pytave::py_builtins_module,
pytave::py_find_function, pytave::py_find_type, pytave::py_import_module,
pytave::py_isinstance): New functions to get handles to Python modules,
functions, and types, and to check if an object is an instance of a type.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Tue, 23 Aug 2016 17:37:08 -0700 |
parents | e89a8a37fd8a |
children | 77ffcaaf8000 |
comparison
equal
deleted
inserted
replaced
356:6cd581661176 | 357:ebd83497ebda |
---|---|
94 std::string hexid = tmp(0).string_value (); | 94 std::string hexid = tmp(0).string_value (); |
95 py_object = main_module.attr ("_in_octave")[hexid]; | 95 py_object = main_module.attr ("_in_octave")[hexid]; |
96 } | 96 } |
97 } | 97 } |
98 | 98 |
99 PyObject * | |
100 py_builtins_module () | |
101 { | |
102 #if PY_VERSION_HEX >= 0x03000000 | |
103 return py_import_module ("builtins"); | |
104 #else | |
105 return py_import_module ("__builtin__"); | |
106 #endif | |
107 } | |
108 | |
109 PyObject * | |
110 py_find_function (PyObject *module, const std::string& name) | |
111 { | |
112 if (module && PyModule_Check (module)) | |
113 { | |
114 PyObject *obj = PyObject_GetAttrString (module, name.c_str ()); | |
115 if (obj && ! PyCallable_Check (obj)) | |
116 { | |
117 Py_CLEAR (obj); | |
118 } | |
119 | |
120 return obj; | |
121 } | |
122 | |
123 return 0; | |
124 } | |
125 | |
126 PyObject * | |
127 py_find_function (const std::string& module, const std::string& name) | |
128 { | |
129 PyObject *mod = py_import_module (module); | |
130 PyObject *func = py_find_function (mod, name); | |
131 Py_XDECREF (mod); | |
132 return func; | |
133 } | |
134 | |
135 PyObject * | |
136 py_find_function (const std::string& name) | |
137 { | |
138 std::string::size_type idx = name.rfind ("."); | |
139 if (idx == std::string::npos) | |
140 { | |
141 PyObject *func = py_find_function ("__main__", name); | |
142 if (! func) | |
143 func = py_find_function (py_builtins_module (), name); | |
144 return func; | |
145 } | |
146 else | |
147 { | |
148 std::string module = name.substr (0, idx); | |
149 std::string function = name.substr (idx + 1); | |
150 return py_find_function (module, function); | |
151 } | |
152 } | |
153 | |
154 PyObject * | |
155 py_find_type (const std::string& name) | |
156 { | |
157 PyObject *obj = py_find_function (name); | |
158 if (obj && PyType_Check (obj)) | |
159 return obj; | |
160 | |
161 Py_XDECREF (obj); | |
162 return 0; | |
163 } | |
164 | |
165 PyObject * | |
166 py_import_module (const std::string& name) | |
167 { | |
168 return PyImport_ImportModule (name.c_str ()); | |
169 } | |
170 | |
171 bool | |
172 py_isinstance (PyObject *obj, PyObject *type) | |
173 { | |
174 if (obj && type) | |
175 return static_cast<bool> (PyObject_IsInstance (obj, type)); | |
176 | |
177 return false; | |
178 } | |
179 | |
99 std::string | 180 std::string |
100 py_object_class_name (PyObject *obj) | 181 py_object_class_name (PyObject *obj) |
101 { | 182 { |
102 PyObject *class_ = obj ? PyObject_GetAttrString (obj, "__class__") : 0; | 183 PyObject *class_ = obj ? PyObject_GetAttrString (obj, "__class__") : 0; |
103 PyObject *name_ = class_ ? PyObject_GetAttrString (class_, "__name__") : 0; | 184 PyObject *name_ = class_ ? PyObject_GetAttrString (class_, "__name__") : 0; |