annotate __py_struct_from_dict__.cc @ 422:8247f298fd16

Raise Octave errors instead of exceptions for conversion errors * Makefile.am (COMMON_SOURCE_FILES): Include oct-py-error.cc in the list. (PYTAVE_HEADER_FILES): Include oct-py-error.h in the list. * oct-py-error.cc, oct-py-error.h: New files defining error conditions. * oct-py-types.cc (pytave::extract_py_bool, pytave::extract_py_complex, pytave::extract_py_float, pytave::make_py_array, pytave::extract_py_scalar_map, pytave::extract_py_int64, pytave::extract_py_uint64, pytave::extract_py_str): Use new error condition functions instead of throwing exceptions. (pytave::make_py_array, pytave::make_py_numeric_value, pytave::make_py_tuple): Raise Octave error instead of throwing exceptions. * __py_struct_from_dict__.cc (F__py_int64_scalar_value__, F__py_uint64_scalar_value__): Remove exception handling logic. * pycall.cc (Fpycall): Remove conversion exception handlers. * pyeval.cc (Fpyeval): Likewise. * pyexec.cc (Fpyexec): Likewise. * exceptions.h (pytave::pytave_exception, pytave::octave_error_exception, pytave::octave_parse_exception, pytave::value_convert_exception, pytave::object_convert_exception, pytave::variable_name_exception): Delete no longer used exception types.
author Mike Miller <mtmiller@octave.org>
date Thu, 04 May 2017 17:13:38 -0700
parents 7864849e84c2
children
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
405
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
30 #include "oct-py-init.h"
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
31 #include "oct-py-object.h"
345
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
32 #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
33 #include "oct-py-util.h"
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 409
diff changeset
34
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
35 DEFUN_DLD (__py_class_name__, args, ,
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
36 "-*- texinfo -*-\n\
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
37 @deftypefn {} {} __py_class_name__ (@var{obj})\n\
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
38 Return the name of the class of the Python object @var{obj}.\n\
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
39 \n\
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
40 This is a private internal function not intended for direct use.\n\
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
41 @end deftypefn")
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
42 {
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
43 if (args.length () != 1)
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
44 print_usage ();
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
45
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
46 if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
47 error ("__py_class_name__: argument must be a valid Python object");
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
48
405
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
49 pytave::py_init ();
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
50
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
51 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
52 std::string name = pytave::py_object_class_name (obj);
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
53
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
54 return ovl (name);
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
55 }
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
56
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
57 /*
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
58 %!assert (__py_class_name__ (pyeval ("None")), "NoneType")
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
59 %!assert (__py_class_name__ (pyeval ("'Octave'")), "str")
375
d0a7f66393fc Fix __py_class_name__ tests to work with Python 2
Mike Miller <mtmiller@octave.org>
parents: 374
diff changeset
60 %!assert (__py_class_name__ (pyeval ("{}")), "dict")
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
61 %!assert (__py_class_name__ (pyeval ("[]")), "list")
375
d0a7f66393fc Fix __py_class_name__ tests to work with Python 2
Mike Miller <mtmiller@octave.org>
parents: 374
diff changeset
62 %!assert (__py_class_name__ (pyeval ("()")), "tuple")
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
63 %!assert (__py_class_name__ (pyeval ("__import__('array').array('d')")), "array.array")
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
64
386
668fcb0f68ef Fix extraction of class name for Python types without a module (fixes issue #79)
Mike Miller <mtmiller@octave.org>
parents: 375
diff changeset
65 %% Test an anonymous class with its __module__ property set to None
668fcb0f68ef Fix extraction of class name for Python types without a module (fixes issue #79)
Mike Miller <mtmiller@octave.org>
parents: 375
diff changeset
66 %!assert (__py_class_name__ (pyeval ("[[t() for t.__module__ in (None,)][0] for t in (type('foo', (), {}),)][0]")), "foo")
668fcb0f68ef Fix extraction of class name for Python types without a module (fixes issue #79)
Mike Miller <mtmiller@octave.org>
parents: 375
diff changeset
67
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
68 %!error __py_class_name__ ()
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
69 %!error __py_class_name__ (1)
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
70 %!error __py_class_name__ (1, 2)
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
71 */
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 368
diff changeset
72
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
73 DEFUN_DLD (__py_int64_scalar_value__, args, ,
356
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
74 "-*- texinfo -*-\n\
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
75 @deftypefn {} {} __py_int64_scalar_value__ (@var{x})\n\
356
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
76 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
77 \n\
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
78 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
79 @end deftypefn")
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
80 {
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
81 if (args.length () != 1)
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
82 print_usage ();
356
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
83
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
84 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
85 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
86
405
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
87 pytave::py_init ();
356
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
88
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
89 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 420
diff changeset
90 octave_int64 retval = pytave::extract_py_int64 (obj);
356
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
91
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
92 return ovl (retval);
356
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
93 }
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 345
diff changeset
94
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
95 /*
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
96 %!assert (__py_int64_scalar_value__ (pyobject (pyeval ("0"))), int64 (0))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
97 %!assert (__py_int64_scalar_value__ (pyobject (pyeval ("2**62"))), int64 (2^62))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
98 %!assert (__py_int64_scalar_value__ (pyobject (pyeval ("-2**62"))), int64 (-2^62))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
99 %!assert (__py_int64_scalar_value__ (pyobject (pyeval ("2**128"))), intmax ("int64"))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
100 %!assert (__py_int64_scalar_value__ (pyobject (pyeval ("-2**128"))), intmin ("int64"))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
101
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
102 %!error __py_int64_scalar_value__ ()
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
103 %!error __py_int64_scalar_value__ (1)
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
104 %!error __py_int64_scalar_value__ (pyeval ("None"))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
105 %!error __py_int64_scalar_value__ (1, 2)
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
106 */
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
107
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
108 DEFUN_DLD (__py_uint64_scalar_value__, args, ,
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
109 "-*- texinfo -*-\n\
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
110 @deftypefn {} {} __py_uint64_scalar_value__ (@var{x})\n\
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
111 Extract a scalar uint64 value from the Python integer @var{x}.\n\
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
112 \n\
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
113 This is a private internal function not intended for direct use.\n\
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
114 @end deftypefn")
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
115 {
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
116 if (args.length () != 1)
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
117 print_usage ();
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
118
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
119 if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
120 error ("pyobject.uint64: argument must be a Python object");
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
121
405
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
122 pytave::py_init ();
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
123
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
124 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
422
8247f298fd16 Raise Octave errors instead of exceptions for conversion errors
Mike Miller <mtmiller@octave.org>
parents: 420
diff changeset
125 octave_uint64 retval = pytave::extract_py_uint64 (obj);
374
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
126
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
127 return ovl (retval);
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
128 }
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
129
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
130 /*
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
131 %!assert (__py_uint64_scalar_value__ (pyobject (pyeval ("0"))), uint64 (0))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
132 %!assert (__py_uint64_scalar_value__ (pyobject (pyeval ("2**62"))), uint64 (2^62))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
133 %!assert (__py_uint64_scalar_value__ (pyobject (pyeval ("2**128"))), intmax ("uint64"))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
134 %!assert (__py_uint64_scalar_value__ (pyobject (pyeval ("-2**128"))), intmin ("uint64"))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
135
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
136 %!error __py_uint64_scalar_value__ ()
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
137 %!error __py_uint64_scalar_value__ (1)
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
138 %!error __py_uint64_scalar_value__ (pyeval ("None"))
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
139 %!error __py_uint64_scalar_value__ (1, 2)
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
140 */
d362cdd1ddeb pyobject: add conversion methods for single, intX, and uintX types
Mike Miller <mtmiller@octave.org>
parents: 373
diff changeset
141
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
142 DEFUN_DLD (__py_is_none__, args, ,
368
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
143 "-*- texinfo -*-\n\
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
144 @deftypefn {} {} __py_is_none__ (@var{x})\n\
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
145 Check whether the Python object @var{obj} is the @code{None} object.\n\
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
146 \n\
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
147 This is a private internal function not intended for direct use.\n\
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
148 @end deftypefn")
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
149 {
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
150 if (args.length () != 1)
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
151 print_usage ();
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
152
405
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
153 pytave::py_init ();
368
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
154
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
155 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
368
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
156
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
157 return ovl (obj.is_none ());
368
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
158 }
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
159
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
160 /*
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
161 %!assert (__py_is_none__ (pyobject ()))
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
162 %!assert (__py_is_none__ (pyeval ("None")))
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
163 %!assert (! __py_is_none__ (1))
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
164 %!assert (! __py_is_none__ ("None"))
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
165 %!assert (! __py_is_none__ (pyobject (1)))
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
166 %!assert (! __py_is_none__ (pyobject ("None")))
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
167
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
168 %!error __py_is_none__ ()
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
169 %!error __py_is_none__ (1, 2)
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
170 */
4d54fb68de71 __py_is_none__: new compiled function to test whether an object is None
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
171
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
172 DEFUN_DLD (__py_isinstance__, args, ,
358
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
173 "-*- texinfo -*-\n\
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
174 @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
175 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
176 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
177 \n\
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
178 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
179 @end deftypefn")
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
180 {
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
181 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
182
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
183 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
184 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
185 {
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
186 print_usage ();
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
187 return retval;
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
188 }
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
189
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
190 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
191 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
192
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
193 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
194 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
195
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
196 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
197 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
198 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
199
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
200 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
201
405
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
202 pytave::py_init ();
358
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
203
408
f833e29b2c12 Simplify conversion where existing Python object argument expected (fixes issue #67)
Mike Miller <mtmiller@octave.org>
parents: 405
diff changeset
204 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
409
b9b8790d1082 Add overload of py_isinstance taking a string decribing a type
Mike Miller <mtmiller@octave.org>
parents: 408
diff changeset
205 retval(0) = pytave::py_isinstance (obj, typestr);
358
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
206
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
207 return retval;
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
208 }
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
209
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
210 DEFUN_DLD (__py_objstore_del__, args, ,
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
211 "-*- texinfo -*-\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
212 @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
213 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
214 \n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
215 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
216 @end deftypefn")
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
217 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
218 if (args.length () != 1)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
219 print_usage ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
220
405
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
221 pytave::py_init ();
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
222
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
223 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
224 pytave::py_objstore_del (key);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
225
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
226 return ovl ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
227 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
228
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
229 DEFUN_DLD (__py_objstore_get__, args, ,
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
230 "-*- texinfo -*-\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
231 @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
232 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
233 \n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
234 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
235 @end deftypefn")
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
236 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
237 if (args.length () != 1)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
238 print_usage ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
239
405
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
240 pytave::py_init ();
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
241
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
242 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
243 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
244
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
245 if (! obj)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
246 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
247
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
248 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
249
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
250 return ovl (retval);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
251 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
252
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
253 DEFUN_DLD (__py_objstore_put__, args, ,
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
254 "-*- texinfo -*-\n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
255 @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
256 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
257 \n\
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
258 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
259 @end deftypefn")
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
260 {
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
261 if (args.length () != 1)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
262 print_usage ();
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
263
405
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
264 pytave::py_init ();
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
265
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 409
diff changeset
266 pytave::python_object obj = pytave::py_implicitly_convert_argument (args(0));
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
267
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
268 if (! obj)
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
269 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
270
411
3613ffbd52b2 Overhaul implicit conversion of arguments and return values
Mike Miller <mtmiller@octave.org>
parents: 409
diff changeset
271 uint64_t key = pytave::py_objstore_put (obj.release ());
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
272
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
273 return ovl (octave_uint64 (key));
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
274 }
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
275
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
276 DEFUN_DLD (__py_string_value__, args, ,
365
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
277 "-*- texinfo -*-\n\
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
278 @deftypefn {} {} __py_string_value__ (@var{obj})\n\
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
279 Return the string value or representation of the Python object @var{obj}.\n\
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
280 \n\
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
281 This is a private internal function not intended for direct use.\n\
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
282 @end deftypefn")
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
283 {
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
284 if (args.length () != 1)
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
285 print_usage ();
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
286
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
287 if (! (args(0).is_object () && args(0).class_name () == "pyobject"))
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
288 error ("pyobject.char: argument must be a valid Python object");
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
289
405
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
290 pytave::py_init ();
365
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
291
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
292 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
365
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
293 if (! obj)
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
294 error ("pyobject.char: argument must be a valid Python object");
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
295
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
296 std::string str;
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
297
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
298 if (PyBytes_Check (obj) || PyUnicode_Check (obj))
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
299 str = pytave::extract_py_str (obj);
403
3644df6564bc maint: use C++11 nullptr rather than 0 or NULL
Mike Miller <mtmiller@octave.org>
parents: 386
diff changeset
300 else if (Py_TYPE (obj)->tp_str != nullptr)
365
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
301 {
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
302 pytave::python_object s = PyObject_Str (obj);
365
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
303 str = pytave::extract_py_str (s);
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
304 }
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
305 else
404
aef165ff92b0 Adopt pytave::python_object where objects are local or created and returned
Mike Miller <mtmiller@octave.org>
parents: 403
diff changeset
306 error ("pyobject.char: cannot convert Python object to string");
365
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
307
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
308 return ovl (str);
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
309 }
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
310
420
7864849e84c2 Eliminate some compiler warnings
Mike Miller <mtmiller@octave.org>
parents: 415
diff changeset
311 DEFUN_DLD (__py_struct_from_dict__, args, ,
345
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
312 "-*- texinfo -*-\n\
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
313 @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
314 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
315 \n\
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
316 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
317 @end deftypefn")
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
318 {
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
319 octave_value_list retval;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
320 std::string id;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
321
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
322 int nargin = args.length ();
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
323
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
324 if (nargin != 1)
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
325 {
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
326 print_usage ();
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
327 return retval;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
328 }
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
329
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
330 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
331 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
332
405
478d83448b0b Refactor Python initialization into a common function
Mike Miller <mtmiller@octave.org>
parents: 404
diff changeset
333 pytave::py_init ();
345
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
334
408
f833e29b2c12 Simplify conversion where existing Python object argument expected (fixes issue #67)
Mike Miller <mtmiller@octave.org>
parents: 405
diff changeset
335 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0));
f833e29b2c12 Simplify conversion where existing Python object argument expected (fixes issue #67)
Mike Miller <mtmiller@octave.org>
parents: 405
diff changeset
336 retval(0) = pytave::extract_py_scalar_map (obj);
345
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
337
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
338 return retval;
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
339 }
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
340
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
341 /*
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
342 ## 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
343 %!assert (1)
baff3b90dcb1 __py_struct_from_dict__: new private compiled conversion function
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
344 */