annotate __py_struct_from_dict__.cc @ 362:b0677c492655

Overhaul Python object storage and wrapping in pyobject * oct-py-util.cc, oct-py-util.h (pytave::py_objstore_del, pytave::py_objstore_get, pytave::py_objstore_put, pytave::pyobject_wrap_object, pytave::pyobject_unwrap_object): New functions. (pytave::get_object_from_python): Use pyobject_unwrap_object. * octave_to_python.cc (pytave::octvalue_to_pyobj): Use pyobject_unwrap_object. * python_to_octave.cc (pytave::pyobj_to_octvalue): Use pyobject_wrap_object. (pytave::pyobj_to_oct_pyobject): Delete. * __py_struct_from_dict__.cc (F__py_objstore_del__, F__py_objstore_get__, F__py_objstore_put__): New functions. * @pyobject/pyobject.m (pyobject.m_id): Rename from id. (pyobject.pyobject): Use __py_objstore_put__, simplify conditional logic. (pyobject.delete): Use __py_objstore_del__. (pyobject.id): Rename from getid. * @pyobject/display.m: Use pyobject.id method name, format as hex.
author Mike Miller <mtmiller@octave.org>
date Thu, 25 Aug 2016 09:51:58 -0700
parents d41fc23d4b9f
children 087e7bc3697f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
345
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
1 /*
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
2
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
3 Copyright (C) 2016 Mike Miller
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
4
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
5 This file is part of Pytave.
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
6
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
7 Pytave is free software: you can redistribute it and/or modify it
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
9 Free Software Foundation, either version 3 of the License, or (at your
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
10 option) any later version.
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
11
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
12 Pytave is distributed in the hope that it will be useful, but WITHOUT
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
15 for more details.
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
16
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
18 along with Pytave; see the file COPYING. If not, see
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
20
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
21 */
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
22
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
23 #if defined (HAVE_CONFIG_H)
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
24 # include <config.h>
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
25 #endif
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
26
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
27 #include <Python.h>
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
28 #include <octave/oct.h>
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
29
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
30 #define PYTAVE_DO_DECLARE_SYMBOL
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
31 #include "arrayobjectdefs.h"
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
32 #include "exceptions.h"
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
33 #include "oct-py-types.h"
358
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
34 #include "oct-py-util.h"
345
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
35 #include "octave_to_python.h"
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
36
356
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
37 DEFUN_DLD (__py_int64_scalar_value__, args, nargout,
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
38 "-*- texinfo -*-\n\
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
39 @deftypefn {} {} __py_int64_scalar_value__ (@var{x})\n\
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
40 Extract a scalar int64 value from the Python integer @var{x}.\n\
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
41 \n\
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
42 This is a private internal function not intended for direct use.\n\
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
43 @end deftypefn")
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
44 {
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
45 octave_value_list retval;
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
46
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
47 int nargin = args.length ();
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
48 if (nargin != 1)
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
49 {
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
50 print_usage ();
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
51 return retval;
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
52 }
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
53
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
54 if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
55 error ("pyobject.int64: argument must be a Python object");
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
56
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
57 Py_Initialize ();
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
58
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
59 try
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
60 {
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
61 // FIXME: PyObject *obj = look up stored pyobject reference (args(0));
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
62 boost::python::object arg;
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
63 pytave::octvalue_to_pyobj (arg, args(0));
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
64 PyObject *obj = arg.ptr ();
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
65
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
66 retval(0) = octave_int64 (pytave::extract_py_int64 (obj));
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
67 }
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
68 catch (pytave::object_convert_exception const &)
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
69 {
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
70 error ("pyobject.int64: error in return value type conversion");
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
71 }
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
72 catch (boost::python::error_already_set const &)
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
73 {
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
74 std::string message = pytave::fetch_exception_message ();
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
75 error ("pyobject.int64: %s", message.c_str ());
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
76 }
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
77
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
78 return retval;
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
79 }
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
80
358
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
81 DEFUN_DLD (__py_isinstance__, args, nargout,
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
82 "-*- texinfo -*-\n\
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
83 @deftypefn {} {} __py_isinstance__ (@var{x})\n\
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
84 Check whether the Python object @var{obj} is an instance of a Python type\n\
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
85 specified by the string @var{type}.\n\
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
86 \n\
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
87 This is a private internal function not intended for direct use.\n\
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
88 @end deftypefn")
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
89 {
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
90 octave_value_list retval;
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
91
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
92 int nargin = args.length ();
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
93 if (nargin != 2)
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
94 {
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
95 print_usage ();
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
96 return retval;
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
97 }
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
98
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
99 if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
100 error ("pyobject.isa: OBJ must be a Python object");
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
101
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
102 if (! args(1).is_string ())
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
103 error ("pyobject.isa: TYPE must be a string naming a Python type (py.*)");
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
104
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
105 std::string typestr = args(1).string_value ();
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
106 if (! ((typestr.size () > 3) && (typestr.compare (0, 3, "py.") == 0)))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
107 error ("pyobject.isa: TYPE must be a string naming a Python type (py.*)");
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
108
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
109 typestr = typestr.substr (3);
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
110
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
111 Py_Initialize ();
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
112
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
113 try
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
114 {
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
115 // FIXME: PyObject *obj = look up stored pyobject reference (args(0));
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
116 boost::python::object arg;
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
117 pytave::octvalue_to_pyobj (arg, args(0));
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
118 PyObject *obj = arg.ptr ();
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
119
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
120 PyObject *type = pytave::py_find_type (typestr);
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
121 retval(0) = pytave::py_isinstance (obj, type);
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
122 Py_XDECREF (type);
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
123 }
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
124 catch (pytave::object_convert_exception const &)
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
125 {
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
126 error ("pyobject.isa: error in return value type conversion");
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
127 }
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
128 catch (boost::python::error_already_set const &)
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
129 {
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
130 std::string message = pytave::fetch_exception_message ();
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
131 error ("pyobject.isa: %s", message.c_str ());
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
132 }
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
133
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
134 return retval;
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
135 }
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
136
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
137 DEFUN_DLD (__py_objstore_del__, args, nargout,
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
138 "-*- texinfo -*-\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
139 @deftypefn {} {} __py_objstore_del__ (@var{key})\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
140 Delete the Python object stored under @var{key} from the object store.\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
141 \n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
142 This is a private internal function not intended for direct use.\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
143 @end deftypefn")
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
144 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
145 if (args.length () != 1)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
146 print_usage ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
147
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
148 Py_Initialize ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
149 uint64_t key = args(0).xuint64_scalar_value ("__py_objstore_del__: KEY must be an integer");
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
150 pytave::py_objstore_del (key);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
151
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
152 return ovl ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
153 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
154
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
155 DEFUN_DLD (__py_objstore_get__, args, nargout,
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
156 "-*- texinfo -*-\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
157 @deftypefn {} {} __py_objstore_get__ (@var{key})\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
158 Get the Python object stored under @var{key} from the object store.\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
159 \n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
160 This is a private internal function not intended for direct use.\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
161 @end deftypefn")
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
162 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
163 if (args.length () != 1)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
164 print_usage ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
165
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
166 Py_Initialize ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
167 uint64_t key = args(0).xuint64_scalar_value ("__py_objstore_get__: KEY must be an integer");
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
168 PyObject *obj = pytave::py_objstore_get (key);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
169
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
170 if (! obj)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
171 error ("__py_objstore_get__: no existing Python object found for key %ju", key);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
172
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
173 octave_value retval = pytave::pyobject_wrap_object (obj);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
174
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
175 return ovl (retval);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
176 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
177
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
178 DEFUN_DLD (__py_objstore_put__, args, nargout,
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
179 "-*- texinfo -*-\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
180 @deftypefn {} {} __py_objstore_put__ (@var{value})\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
181 Convert @var{value} to a Python value and store in the object store.\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
182 \n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
183 This is a private internal function not intended for direct use.\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
184 @end deftypefn")
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
185 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
186 if (args.length () != 1)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
187 print_usage ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
188
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
189 Py_Initialize ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
190 boost::python::numeric::array::set_module_and_type ("numpy", "ndarray");
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
191 _import_array ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
192 // FIXME: PyObject *obj = convert argument to Python (args(0));
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
193 PyObject *obj = 0;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
194 try
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
195 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
196 boost::python::object arg;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
197 pytave::octvalue_to_pyobj (arg, args(0));
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
198 obj = arg.ptr ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
199 Py_INCREF (obj);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
200 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
201 catch (pytave::value_convert_exception const &)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
202 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
203 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
204
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
205 if (! obj)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
206 error ("__py_objstore_put__: VALUE must be convertible to a Python value");
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
207
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
208 uint64_t key = pytave::py_objstore_put (obj);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
209 Py_DECREF (obj);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
210
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
211 return ovl (octave_uint64 (key));
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
212 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
213
345
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
214 DEFUN_DLD (__py_struct_from_dict__, args, nargout,
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
215 "-*- texinfo -*-\n\
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
216 @deftypefn {} {} __py_struct_from_dict__ (@var{dict})\n\
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
217 Extract a scalar struct from the Python dict @var{dict}.\n\
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
218 \n\
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
219 This is a private internal function not intended for direct use.\n\
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
220 @end deftypefn")
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
221 {
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
222 octave_value_list retval;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
223 std::string id;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
224
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
225 int nargin = args.length ();
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
226
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
227 if (nargin != 1)
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
228 {
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
229 print_usage ();
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
230 return retval;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
231 }
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
232
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
233 if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
234 error ("pyobject.struct: argument must be a Python object");
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
235
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
236 Py_Initialize ();
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
237
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
238 try
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
239 {
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
240 // FIXME: PyObject *obj = look up stored pyobject reference (args(0));
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
241 boost::python::object arg;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
242 pytave::octvalue_to_pyobj (arg, args(0));
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
243 PyObject *obj = arg.ptr ();
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
244
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
245 retval(0) = pytave::extract_py_scalar_map (obj);
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
246 }
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
247 catch (pytave::object_convert_exception const &)
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
248 {
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
249 error ("pyobject.struct: error in return value type conversion");
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
250 }
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
251 catch (boost::python::error_already_set const &)
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
252 {
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
253 std::string message = pytave::fetch_exception_message ();
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
254 error ("pyobject.struct: %s", message.c_str ());
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
255 }
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
256
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
257 return retval;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
258 }
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
259
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
260 /*
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
261 ## No test needed for internal helper function.
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
262 %!assert (1)
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
263 */