annotate oct-py-types.cc @ 329:778c91283a73

Add isequal method to allow comparing pyobjects (fixes issue #53) * @pyobject/pyobject.m: Add an 'isequal' method to fix a BIST test and some more tests.
author Abhinav Tripathi <genuinelucifer@gmail.com>
date Fri, 12 Aug 2016 05:45:06 -0700
parents c2aa34730dc9
children fd5881d48238
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
1 /*
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
2
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
3 Copyright (C) 2016 Mike Miller
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
4
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
5 This file is part of Pytave.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
6
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
7 Pytave is free software; you can redistribute it and/or modify it
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
10 option) any later version.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
11
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
12 Pytave is distributed in the hope that it will be useful, but WITHOUT
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
15 for more details.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
16
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
18 along with Pytave; see the file COPYING. If not, see
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
20
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
21 */
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
22
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
23 #if defined (HAVE_CONFIG_H)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
24 # include <config.h>
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
25 #endif
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
26
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
27 #include <octave/oct-map.h>
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
28 #include <octave/quit.h>
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
29
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
30 #include "oct-py-types.h"
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
31
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
32 // FIXME: only here to bootstrap nested conversions needed in this file
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
33 #include "octave_to_python.h"
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
34
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
35 namespace pytave
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
36 {
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
37
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
38 inline PyObject *
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
39 wrap_octvalue_to_pyobj (const octave_value& value)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
40 {
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
41 boost::python::object obj;
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
42 octvalue_to_pyobj (obj, value);
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
43 PyObject *ptr = obj.ptr ();
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
44 Py_INCREF (ptr);
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
45 return ptr;
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
46 }
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
47
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
48 PyObject *
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
49 make_py_dict (const octave_scalar_map& map)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
50 {
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
51 PyObject *dict = PyDict_New ();
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
52 if (! dict)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
53 octave_throw_bad_alloc ();
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
54
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
55 for (auto p = map.begin (); p != map.end (); ++p)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
56 {
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
57 PyObject *key = wrap_octvalue_to_pyobj (map.key (p));
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
58 PyObject *item = wrap_octvalue_to_pyobj (map.contents (p));
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
59
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
60 if (PyDict_SetItem (dict, key, item) < 0)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
61 throw boost::python::error_already_set ();
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
62 }
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
63
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
64 return dict;
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
65 }
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
66
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
67 }