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