annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
364
1470ed26917a Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
3 Copyright (C) 2016 Mike Miller
306
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
333
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
30 #include "oct-py-types.h"
331
cee203ea6245 Rename oct-py-util.cc from pytave_utils.cc
Mike Miller <mtmiller@octave.org>
parents: 306
diff changeset
31 #include "oct-py-util.h"
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
32 #include "oct-py-object.h"
306
2ecae5c6eeb6 Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff changeset
33
364
1470ed26917a Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
34 // FIXME: only here for boost::python::error_already_set
1470ed26917a Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
35 #include <boost/python.hpp>
306
2ecae5c6eeb6 Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff changeset
36
2ecae5c6eeb6 Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff changeset
37 namespace pytave
2ecae5c6eeb6 Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff changeset
38 {
331
cee203ea6245 Rename oct-py-util.cc from pytave_utils.cc
Mike Miller <mtmiller@octave.org>
parents: 306
diff changeset
39
372
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
40 inline std::string
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
41 py_builtins_module_name ()
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
42 {
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
43 #if PY_VERSION_HEX >= 0x03000000
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
44 return "builtins";
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
45 #else
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
46 return "__builtin__";
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
47 #endif
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
48 }
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
49
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
50 PyObject *
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
51 py_builtins_module ()
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
52 {
372
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
53 return py_import_module (py_builtins_module_name ());
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
54 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
55
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
56 PyObject *
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
57 py_find_function (PyObject *module, const std::string& name)
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
58 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
59 if (module && PyModule_Check (module))
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
60 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
61 PyObject *obj = PyObject_GetAttrString (module, name.c_str ());
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
62 if (obj && ! PyCallable_Check (obj))
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
63 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
64 Py_CLEAR (obj);
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
65 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
66
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
67 return obj;
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
68 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
69
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
70 return 0;
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
71 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
72
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
73 PyObject *
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
74 py_find_function (const std::string& module, const std::string& name)
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
75 {
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
76 python_object mod = py_import_module (module);
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
77 PyObject *func = py_find_function (mod, name);
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
78 return func;
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
79 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
80
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
81 PyObject *
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
82 py_find_function (const std::string& name)
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
83 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
84 std::string::size_type idx = name.rfind (".");
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
85 if (idx == std::string::npos)
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
86 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
87 PyObject *func = py_find_function ("__main__", name);
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
88 if (! func)
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
89 func = py_find_function (py_builtins_module (), name);
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
90 return func;
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
91 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
92 else
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
93 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
94 std::string module = name.substr (0, idx);
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
95 std::string function = name.substr (idx + 1);
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
96 return py_find_function (module, function);
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
97 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
98 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
99
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
100 PyObject *
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
101 py_find_type (const std::string& name)
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
102 {
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
103 python_object obj = py_find_function (name);
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
104 if (obj && PyType_Check (obj))
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
105 return obj.release ();
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
106
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
107 return 0;
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
108 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
109
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
110 PyObject *
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
111 py_import_module (const std::string& name)
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
112 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
113 return PyImport_ImportModule (name.c_str ());
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
114 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
115
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
116 bool
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
117 py_isinstance (PyObject *obj, PyObject *type)
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
118 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
119 if (obj && type)
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
120 return static_cast<bool> (PyObject_IsInstance (obj, type));
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
121
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
122 return false;
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
123 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
124
333
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
125 std::string
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
126 py_object_class_name (PyObject *obj)
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
127 {
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
128 std::string retval;
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
129
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
130 python_object type = obj ? PyObject_GetAttrString (obj, "__class__") : 0;
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
131 if (type)
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
132 {
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
133 python_object mod = PyObject_GetAttrString (type, "__module__");
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
134
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
135 python_object name;
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
136 if (PyObject_HasAttrString (type, "__qualname__"))
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
137 name = PyObject_GetAttrString (type, "__qualname__");
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
138 else
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
139 name = PyObject_GetAttrString (type, "__name__");
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
140
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
141 std::string mod_str = !mod.is_none () ? extract_py_str (mod) : "";
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
142 std::string name_str = name ? extract_py_str (name) : "";
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
143
386
668fcb0f68ef Fix extraction of class name for Python types without a module (fixes issue #79)
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
144 if (mod_str.empty () || mod_str == py_builtins_module_name ())
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
145 retval = name_str;
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
146 else
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
147 retval = mod_str + "." + name_str;
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
148 }
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
149
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
150 return retval;
331
cee203ea6245 Rename oct-py-util.cc from pytave_utils.cc
Mike Miller <mtmiller@octave.org>
parents: 306
diff changeset
151 }
333
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
152
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
153 // FIXME: could make this into a class/singleton wrapper a la Octave core
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
154 PyObject *objstore = 0;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
155
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
156 inline PyObject *
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
157 py_objstore ()
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
158 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
159 if (! objstore)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
160 {
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
161 python_object main = py_import_module ("__main__");
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
162 python_object ns = main ? PyObject_GetAttrString (main, "__dict__") : 0;
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
163 PyObject *dict = ns ? PyDict_GetItemString (ns, "_in_octave") : 0;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
164
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
165 if (dict)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
166 Py_INCREF (dict);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
167
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
168 if (! dict)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
169 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
170 dict = PyDict_New ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
171 if (dict && ns)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
172 PyDict_SetItemString (ns, "_in_octave", dict);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
173 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
174
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
175 if (! dict)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
176 throw boost::python::error_already_set ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
177
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
178 objstore = dict;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
179 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
180 return objstore;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
181 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
182
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
183 void
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
184 py_objstore_del (uint64_t key)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
185 {
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
186 python_object store = py_objstore ();
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
187 python_object key_obj = make_py_int (key);
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
188 python_object key_fmt = PyNumber_ToBase (key_obj, 16);
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
189 PyDict_DelItem (store, key_fmt);
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
190 store.release ();
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
191 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
192
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
193 PyObject *
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
194 py_objstore_get (uint64_t key)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
195 {
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
196 python_object store = py_objstore ();
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
197 python_object key_obj = make_py_int (key);
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
198 python_object key_fmt = PyNumber_ToBase (key_obj, 16);
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
199 PyObject *obj = PyDict_GetItem (store, key_fmt);
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
200 store.release ();
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
201 if (obj)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
202 Py_INCREF (obj);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
203 return obj;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
204 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
205
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
206 uint64_t
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
207 py_objstore_put (PyObject *obj)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
208 {
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
209 python_object store = py_objstore ();
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
210 uint64_t key = reinterpret_cast<uint64_t> (obj);
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
211 python_object key_obj = make_py_int (key);
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
212 python_object key_fmt = PyNumber_ToBase (key_obj, 16);
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
213 PyDict_SetItem (store, key_fmt, obj);
397
fc0fb94161de Add a wrapper for PyObject pointers (fixes issue #81)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 386
diff changeset
214 store.release ();
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
215 return key;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
216 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
217
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
218 octave_value
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
219 pyobject_wrap_object (PyObject *obj)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
220 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
221 uint64_t key = py_objstore_put (obj);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
222 octave_value_list out = feval ("pyobject", ovl (0, octave_uint64 (key)), 1);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
223 return out(0);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
224 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
225
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
226 PyObject *
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
227 pyobject_unwrap_object (const octave_value& value)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
228 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
229 if (value.is_object () && value.class_name () == "pyobject")
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
230 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
231 octave_value_list out = feval ("id", ovl (value), 1);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
232 uint64_t key = out(0).uint64_scalar_value ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
233 return py_objstore_get (key);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
234 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
235
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
236 return 0;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
237 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
238
333
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
239 bool
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
240 is_py_kwargs_argument (PyObject *obj)
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
241 {
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
242 if (obj && py_object_class_name (obj) == "__main__._OctaveKwargs"
333
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
243 && PyObject_HasAttrString (obj, "is_kwargs_argument"))
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
244 {
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
245 PyObject *flag = PyObject_GetAttrString (obj, "is_kwargs_argument");
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
246 if (flag && PyBool_Check (flag) && PyObject_IsTrue (flag))
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
247 return true;
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
248 }
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
249 return false;
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
250 }
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
251
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
252 PyObject *
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
253 update_py_dict (PyObject *dict_orig, PyObject *dict_new)
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
254 {
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
255 PyObject *dict = dict_orig ? dict_orig : PyDict_New ();
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
256 PyDict_Update (dict, dict_new);
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
257 return dict;
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
258 }
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
259
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
260 }