annotate oct-py-util.cc @ 389:d4061763a56b

Delete and no longer build Python extension module * Makefile.am (_pytave.la): Delete all declarations. * configure.ac (PYTAVE_MODULE_INSTALL_PATH): Delete hack. * pytave.cc: Delete.
author Mike Miller <mtmiller@octave.org>
date Mon, 03 Apr 2017 12:11:25 -0700
parents 668fcb0f68ef
children fc0fb94161de
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"
306
2ecae5c6eeb6 Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff changeset
32
364
1470ed26917a Use pyobject_unwrap_object when an existing PyObject is expected
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
33 // 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
34 #include <boost/python.hpp>
306
2ecae5c6eeb6 Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff changeset
35
2ecae5c6eeb6 Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff changeset
36 namespace pytave
2ecae5c6eeb6 Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff changeset
37 {
331
cee203ea6245 Rename oct-py-util.cc from pytave_utils.cc
Mike Miller <mtmiller@octave.org>
parents: 306
diff changeset
38
372
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
39 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
40 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
41 {
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
42 #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
43 return "builtins";
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
44 #else
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
45 return "__builtin__";
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
46 #endif
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
47 }
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
48
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
49 PyObject *
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
50 py_builtins_module ()
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
51 {
372
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
52 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
53 }
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 PyObject *
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
56 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
57 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
58 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
59 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
60 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
61 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
62 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
63 Py_CLEAR (obj);
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
64 }
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 return obj;
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
67 }
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 return 0;
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
70 }
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 PyObject *
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
73 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
74 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
75 PyObject *mod = py_import_module (module);
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
76 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
77 Py_XDECREF (mod);
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 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
103 PyObject *obj = py_find_function (name);
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))
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
105 return obj;
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 Py_XDECREF (obj);
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
108 return 0;
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
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
111 PyObject *
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
112 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
113 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
114 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
115 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
116
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
117 bool
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
118 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
119 {
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
120 if (obj && type)
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
121 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
122
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
123 return false;
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
124 }
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
125
333
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
126 std::string
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
127 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
128 {
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
129 std::string retval;
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
130
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
131 PyObject *type = obj ? PyObject_GetAttrString (obj, "__class__") : 0;
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
132 if (type)
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
133 {
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
134 PyObject *mod = PyObject_GetAttrString (type, "__module__");
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
135
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
136 PyObject *name = 0;
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
137 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
138 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
139 else
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
140 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
141
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
142 std::string mod_str = (mod && mod != Py_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
143 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
144
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
145 Py_DECREF (type);
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
146 Py_XDECREF (mod);
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
147 Py_XDECREF (name);
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
148
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
149 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
150 retval = name_str;
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
151 else
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
152 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
153 }
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
154
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
155 return retval;
331
cee203ea6245 Rename oct-py-util.cc from pytave_utils.cc
Mike Miller <mtmiller@octave.org>
parents: 306
diff changeset
156 }
333
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
157
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
158 // 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
159 PyObject *objstore = 0;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
160
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
161 inline PyObject *
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
162 py_objstore ()
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
163 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
164 if (! objstore)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
165 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
166 PyObject *main = py_import_module ("__main__");
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
167 PyObject *ns = main ? PyObject_GetAttrString (main, "__dict__") : 0;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
168 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
169
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
170 if (dict)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
171 Py_INCREF (dict);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
172
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
173 if (! dict)
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 dict = PyDict_New ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
176 if (dict && ns)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
177 PyDict_SetItemString (ns, "_in_octave", dict);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
178 }
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 if (! dict)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
181 throw boost::python::error_already_set ();
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 objstore = dict;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
184 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
185 return objstore;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
186 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
187
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
188 void
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
189 py_objstore_del (uint64_t key)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
190 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
191 PyObject *store = py_objstore ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
192 PyObject *key_obj = make_py_int (key);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
193 PyObject *key_fmt = PyNumber_ToBase (key_obj, 16);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
194 PyDict_DelItem (store, key_fmt);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
195 Py_DECREF (key_fmt);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
196 Py_DECREF (key_obj);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
197 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
198
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
199 PyObject *
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
200 py_objstore_get (uint64_t key)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
201 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
202 PyObject *store = py_objstore ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
203 PyObject *key_obj = make_py_int (key);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
204 PyObject *key_fmt = PyNumber_ToBase (key_obj, 16);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
205 PyObject *obj = PyDict_GetItem (store, key_fmt);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
206 Py_DECREF (key_fmt);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
207 Py_DECREF (key_obj);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
208 if (obj)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
209 Py_INCREF (obj);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
210 return obj;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
211 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
212
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
213 uint64_t
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
214 py_objstore_put (PyObject *obj)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
215 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
216 PyObject *store = py_objstore ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
217 uint64_t key = reinterpret_cast<uint64_t> (obj);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
218 PyObject *key_obj = make_py_int (key);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
219 PyObject *key_fmt = PyNumber_ToBase (key_obj, 16);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
220 PyDict_SetItem (store, key_fmt, obj);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
221 Py_DECREF (key_fmt);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
222 Py_DECREF (key_obj);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
223 return key;
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 octave_value
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
227 pyobject_wrap_object (PyObject *obj)
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 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
230 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
231 return out(0);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
232 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
233
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
234 PyObject *
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
235 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
236 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
237 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
238 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
239 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
240 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
241 return py_objstore_get (key);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
242 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
243
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
244 return 0;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
245 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
246
333
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
247 bool
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
248 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
249 {
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
250 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
251 && 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
252 {
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
253 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
254 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
255 return true;
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
256 }
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
257 return false;
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 PyObject *
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
261 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
262 {
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
263 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
264 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
265 return dict;
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
266 }
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
267
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
268 }