annotate oct-py-util.cc @ 423:6b9de18b4bdd

Eliminate remaining custom exception handling in favor of Octave errors * oct-py-error.cc, oct-py-error.h (pytave::error_python_exception): New function to generate an Octave error from an active Python error condition, based on pytave::fetch_exception_message. * oct-py-eval.cc (pytave::py_call_function, pytave::py_run_string_safe): Use it. * oct-py-types.cc (pytave::make_py_dict): Likewise. * oct-py-util.cc (pytave::py_objstore): Likewise. * pycall.cc (Fpycall): Remove exception handling logic. * pyeval.cc (Fpyeval): Likewise. * pyexec.cc (Fpyexec): Likewise. * exceptions.cc, exceptions.h: Delete. * Makefile.am (COMMON_SOURCE_FILES, PYTAVE_HEADER_FILES): Remove them.
author Mike Miller <mtmiller@octave.org>
date Thu, 04 May 2017 21:15:07 -0700
parents 24555fba9964
children
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
421
24555fba9964 Include <Python.h> first to suppress possible redefinition warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
27 #include <Python.h>
24555fba9964 Include <Python.h> first to suppress possible redefinition warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
28 #include <octave/oct.h>
306
2ecae5c6eeb6 Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff changeset
29 #include <octave/parse.h>
2ecae5c6eeb6 Refactor code, have common functions in pytave_utils
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
diff changeset
30
423
6b9de18b4bdd Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
31 #include "oct-py-error.h"
398
3905052ebe1d maint: sort uses of new header file oct-py-object.h
Mike Miller <mtmiller@octave.org>
parents: 397
diff changeset
32 #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
33 #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
34 #include "oct-py-util.h"
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
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
39 inline std::string
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
40 py_builtins_module_name ()
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
41 {
372
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
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
43 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
44 #else
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
45 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
46 #endif
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
47 }
372
b20b8cf8ad07 Make an internal function to return the name of the builtins module
Mike Miller <mtmiller@octave.org>
parents: 364
diff changeset
48
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
49 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
50 py_builtins_module ()
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
51 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
52 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
53 }
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
54
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
55 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
56 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
57 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
58 if (module && PyModule_Check (module))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
59 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
60 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
61 if (obj && ! PyCallable_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
62 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
63 Py_CLEAR (obj);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
64 }
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
65
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
66 return obj;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
67 }
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
68
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
69 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
70 }
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
71
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
72 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
73 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
74 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
75 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
76 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
77 return func;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
78 }
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
79
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
80 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
81 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
82 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
83 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
84 if (idx == std::string::npos)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
85 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
86 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
87 if (! func)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
88 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
89 return func;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
90 }
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
91 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
92 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
93 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
94 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
95 return py_find_function (module, function);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
96 }
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
97 }
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
98
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
99 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
100 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
101 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
102 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
103 if (obj && PyType_Check (obj))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
104 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
105
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
106 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
107 }
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
108
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
109 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
110 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
111 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
112 return PyImport_ImportModule (name.c_str ());
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
113 }
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
114
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
115 bool
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
116 py_isinstance (PyObject *obj, PyObject *type)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
117 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
118 if (obj && type)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
119 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
120
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
121 return false;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
122 }
357
ebd83497ebda Add utility functions to look up Python modules, functions, and types
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
123
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
124 std::string
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
125 py_object_class_name (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
126 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
127 std::string retval;
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
128
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
129 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
130 if (type)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
131 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
132 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
133
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
134 python_object name;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
135 if (PyObject_HasAttrString (type, "__qualname__"))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
136 name = PyObject_GetAttrString (type, "__qualname__");
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
137 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
138 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
139
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
140 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
141 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
142
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
143 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
144 retval = name_str;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
145 else
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
146 retval = mod_str + "." + name_str;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
147 }
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 372
diff changeset
148
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
149 return retval;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
150 }
333
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
151
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
152 // 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
153 PyObject *objstore = nullptr;
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
154
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
155 inline PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
156 py_objstore ()
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
157 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
158 if (! objstore)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
159 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
160 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
161 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
162 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
163
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
164 if (dict)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
165 Py_INCREF (dict);
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
166
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
167 if (! dict)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
168 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
169 dict = PyDict_New ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
170 if (dict && ns)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
171 PyDict_SetItemString (ns, "_in_octave", dict);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
172 }
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
173
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
174 if (! dict)
423
6b9de18b4bdd Eliminate remaining custom exception handling in favor of Octave errors
Mike Miller <mtmiller@octave.org>
parents: 421
diff changeset
175 error_python_exception ();
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
176
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
177 objstore = dict;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
178 }
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
179 return objstore;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
180 }
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
181
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
182 void
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
183 py_objstore_del (uint64_t key)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
184 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
185 python_object store = py_objstore ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
186 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
187 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
188 PyDict_DelItem (store, key_fmt);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
189 store.release ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
190 }
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
191
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
192 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
193 py_objstore_get (uint64_t key)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
194 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
195 python_object store = py_objstore ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
196 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
197 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
198 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
199 store.release ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
200 if (obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
201 Py_INCREF (obj);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
202 return obj;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
203 }
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
204
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
205 uint64_t
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
206 py_objstore_put (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
207 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
208 python_object store = py_objstore ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
209 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
210 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
211 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
212 PyDict_SetItem (store, key_fmt, obj);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
213 store.release ();
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
214 return key;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
215 }
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
216
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
217 octave_value
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
218 pyobject_wrap_object (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
219 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
220 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
221 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
222 return out(0);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
223 }
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
224
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
225 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
226 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
227 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
228 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
229 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
230 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
231 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
232 return py_objstore_get (key);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
233 }
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
234
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
235 return 0;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
236 }
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 360
diff changeset
237
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
238 bool
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
239 is_py_kwargs_argument (PyObject *obj)
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
240 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
241 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
242 && PyObject_HasAttrString (obj, "is_kwargs_argument"))
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
243 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
244 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
245 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
246 return true;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
247 }
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
248 return false;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
249 }
333
96b78e78a235 pycall: add support for keyword arguments from pyargs (fixes issue #45)
Mike Miller <mtmiller@octave.org>
parents: 331
diff changeset
250
402
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
251 PyObject *
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
252 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
253 {
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
254 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
255 PyDict_Update (dict, dict_new);
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
256 return dict;
c4b78e449c62 maint: indent functions declared in the pytave namespace
Mike Miller <mtmiller@octave.org>
parents: 398
diff changeset
257 }
333
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 }