1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/*
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <python_context.h>
#include <grts/structs.ui.h>
#include <grtpp_util.h>
#include "grt_PyObject_impl.h"
//================================================================================
// grt_PyObject
grt::IntegerRef grt_PyObject::isEqualTo(const grt::Ref<grt_PyObject> &other)
{
if (other.is_valid())
return grt::IntegerRef(0);
return grt::IntegerRef(0);
}
grt::AutoPyObject pyobject_from_grt(grt_PyObjectRef object)
{
if (!object.is_valid())
return 0;
return *object->get_data();
}
static void release_object(grt::AutoPyObject *object)
{
delete object;
}
grt_PyObjectRef pyobject_to_grt(grt::GRT *grt, grt::AutoPyObject object)
{
if (object)
{
grt_PyObjectRef ref(grt);
ref->set_data(new grt::AutoPyObject(object), release_object);
return ref;
}
return grt_PyObjectRef();
}
grt_PyObjectRef pyobject_to_grt(grt::GRT *grt, PyObject *object)
{
return pyobject_to_grt(grt, grt::AutoPyObject(object));
}
static PyObject *wrap_pyobject(PyObject *self, PyObject *args)
{
grt::PythonContext *ctx;
std::string text;
if (!(ctx= grt::PythonContext::get_and_check()))
return NULL;
// wrap an arbitrary Python object into a GRT object
PyObject *o;
if (!PyArg_ParseTuple(args, "O", &o))
return NULL;
return ctx->from_grt(pyobject_to_grt(ctx->get_grt(), o));
}
static PyObject *unwrap_pyobject(PyObject *self, PyObject *args)
{
grt::PythonContext *ctx;
std::string text;
if (!(ctx= grt::PythonContext::get_and_check()))
return NULL;
// unwrap a grt_PyObject into a python object
PyObject *o;
if (!PyArg_ParseTuple(args, "O", &o))
return NULL;
if (o == NULL || o == Py_None)
Py_RETURN_NONE;
grt::ValueRef value(ctx->from_pyobject(o));
if (value.is_valid())
{
if (grt_PyObjectRef::can_wrap(value))
{
grt_PyObjectRef pobj(grt_PyObjectRef::cast_from(value));
PyObject *tmp = pyobject_from_grt(pobj);
Py_INCREF(tmp);
return tmp;
}
else
{
PyErr_SetString(PyExc_TypeError, "Argument to fromgrt() must be of class grt_PyObject");
return NULL;
}
}
else
Py_RETURN_NONE;
}
void pyobject_initialize()
{
grt::PythonContext::set_wrap_pyobject_func(wrap_pyobject);
grt::PythonContext::set_unwrap_pyobject_func(unwrap_pyobject);
}
|