Mercurial > pytave
comparison oct-py-util.cc @ 373:0e4097c66788
Report a Python object's fully qualified class name correctly
* oct-py-util.cc (pytave::py_object_class_name): Return the correct fully
qualified class name, including module prefix.
(pytave::is_py_kwargs_argument): Check for class name "__main__._OctaveKwargs".
* __py_struct_from_dict__.cc (F__py_class_name__): New function.
* @pyobject/methods.m (pyobject.methods): Use __py_class_name__.
* @pyobject/pyobject.m (pyobject.class): Likewise.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Fri, 26 Aug 2016 14:05:37 -0700 |
parents | b20b8cf8ad07 |
children | 668fcb0f68ef |
comparison
equal
deleted
inserted
replaced
372:b20b8cf8ad07 | 373:0e4097c66788 |
---|---|
124 } | 124 } |
125 | 125 |
126 std::string | 126 std::string |
127 py_object_class_name (PyObject *obj) | 127 py_object_class_name (PyObject *obj) |
128 { | 128 { |
129 PyObject *class_ = obj ? PyObject_GetAttrString (obj, "__class__") : 0; | 129 std::string retval; |
130 PyObject *name_ = class_ ? PyObject_GetAttrString (class_, "__name__") : 0; | 130 |
131 return name_ ? extract_py_str (name_): ""; | 131 PyObject *type = obj ? PyObject_GetAttrString (obj, "__class__") : 0; |
132 if (type) | |
133 { | |
134 PyObject *mod = PyObject_GetAttrString (type, "__module__"); | |
135 | |
136 PyObject *name = 0; | |
137 if (PyObject_HasAttrString (type, "__qualname__")) | |
138 name = PyObject_GetAttrString (type, "__qualname__"); | |
139 else | |
140 name = PyObject_GetAttrString (type, "__name__"); | |
141 | |
142 std::string mod_str = mod ? extract_py_str (mod) : ""; | |
143 std::string name_str = name ? extract_py_str (name) : ""; | |
144 | |
145 Py_DECREF (type); | |
146 Py_XDECREF (mod); | |
147 Py_XDECREF (name); | |
148 | |
149 if (mod_str == py_builtins_module_name ()) | |
150 retval = name_str; | |
151 else | |
152 retval = mod_str + "." + name_str; | |
153 } | |
154 | |
155 return retval; | |
132 } | 156 } |
133 | 157 |
134 // FIXME: could make this into a class/singleton wrapper a la Octave core | 158 // FIXME: could make this into a class/singleton wrapper a la Octave core |
135 PyObject *objstore = 0; | 159 PyObject *objstore = 0; |
136 | 160 |
221 } | 245 } |
222 | 246 |
223 bool | 247 bool |
224 is_py_kwargs_argument (PyObject *obj) | 248 is_py_kwargs_argument (PyObject *obj) |
225 { | 249 { |
226 if (obj && py_object_class_name (obj) == "_OctaveKwargs" | 250 if (obj && py_object_class_name (obj) == "__main__._OctaveKwargs" |
227 && PyObject_HasAttrString (obj, "is_kwargs_argument")) | 251 && PyObject_HasAttrString (obj, "is_kwargs_argument")) |
228 { | 252 { |
229 PyObject *flag = PyObject_GetAttrString (obj, "is_kwargs_argument"); | 253 PyObject *flag = PyObject_GetAttrString (obj, "is_kwargs_argument"); |
230 if (flag && PyBool_Check (flag) && PyObject_IsTrue (flag)) | 254 if (flag && PyBool_Check (flag) && PyObject_IsTrue (flag)) |
231 return true; | 255 return true; |