Mercurial > pytave
annotate oct-py-util.cc @ 372:b20b8cf8ad07
Make an internal function to return the name of the builtins module
* oct-py-util.cc (pytave::py_builtins_module_name): New function.
(pytave::py_builtins_module): Use it.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Fri, 26 Aug 2016 13:59:18 -0700 |
parents | 1470ed26917a |
children | 0e4097c66788 |
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 { |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
129 PyObject *class_ = obj ? PyObject_GetAttrString (obj, "__class__") : 0; |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
130 PyObject *name_ = class_ ? PyObject_GetAttrString (class_, "__name__") : 0; |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
131 return name_ ? extract_py_str (name_): ""; |
331
cee203ea6245
Rename oct-py-util.cc from pytave_utils.cc
Mike Miller <mtmiller@octave.org>
parents:
306
diff
changeset
|
132 } |
333
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
133 |
362
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
134 // 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
|
135 PyObject *objstore = 0; |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
136 |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
137 inline PyObject * |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
138 py_objstore () |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
139 { |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
140 if (! objstore) |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
141 { |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
142 PyObject *main = py_import_module ("__main__"); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
143 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
|
144 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
|
145 |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
146 if (dict) |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
147 Py_INCREF (dict); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
148 |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
149 if (! dict) |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
150 { |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
151 dict = PyDict_New (); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
152 if (dict && ns) |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
153 PyDict_SetItemString (ns, "_in_octave", dict); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
154 } |
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 if (! dict) |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
157 throw boost::python::error_already_set (); |
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 objstore = dict; |
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 return objstore; |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
162 } |
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 void |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
165 py_objstore_del (uint64_t key) |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
166 { |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
167 PyObject *store = py_objstore (); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
168 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
|
169 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
|
170 PyDict_DelItem (store, key_fmt); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
171 Py_DECREF (key_fmt); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
172 Py_DECREF (key_obj); |
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 PyObject * |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
176 py_objstore_get (uint64_t key) |
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 PyObject *store = py_objstore (); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
179 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
|
180 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
|
181 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
|
182 Py_DECREF (key_fmt); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
183 Py_DECREF (key_obj); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
184 if (obj) |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
185 Py_INCREF (obj); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
186 return obj; |
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 |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
189 uint64_t |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
190 py_objstore_put (PyObject *obj) |
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 PyObject *store = py_objstore (); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
193 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
|
194 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
|
195 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
|
196 PyDict_SetItem (store, key_fmt, obj); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
197 Py_DECREF (key_fmt); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
198 Py_DECREF (key_obj); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
199 return key; |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
200 } |
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 octave_value |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
203 pyobject_wrap_object (PyObject *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 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
|
206 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
|
207 return out(0); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
208 } |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
209 |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
210 PyObject * |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
211 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
|
212 { |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
213 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
|
214 { |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
215 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
|
216 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
|
217 return py_objstore_get (key); |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
218 } |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
219 |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
220 return 0; |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
221 } |
b0677c492655
Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents:
360
diff
changeset
|
222 |
333
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
223 bool |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
224 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
|
225 { |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
226 if (obj && py_object_class_name (obj) == "_OctaveKwargs" |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
227 && 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
|
228 { |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
229 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
|
230 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
|
231 return true; |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
232 } |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
233 return false; |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
234 } |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
235 |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
236 PyObject * |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
237 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
|
238 { |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
239 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
|
240 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
|
241 return dict; |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
242 } |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
243 |
96b78e78a235
pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents:
331
diff
changeset
|
244 } |