2005-07-26 04:11:46 -07:00
|
|
|
/*
|
|
|
|
* mooutils/mooutils-python.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004-2005 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
|
|
|
*
|
|
|
|
* 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; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* See COPYING file that comes with this distribution.
|
|
|
|
*/
|
|
|
|
|
2005-07-30 09:21:40 -07:00
|
|
|
#include <Python.h>
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2005-07-26 04:11:46 -07:00
|
|
|
#include "mooutils/mooutils-python.h"
|
2005-11-30 08:46:02 -08:00
|
|
|
#include "mooutils/moocompat.h"
|
2005-10-13 07:08:18 -07:00
|
|
|
#ifdef MOO_USE_PYGTK
|
2005-07-30 09:21:40 -07:00
|
|
|
# define NO_IMPORT_PYGOBJECT
|
|
|
|
# include "pygobject.h"
|
|
|
|
#endif
|
2005-07-26 04:11:46 -07:00
|
|
|
|
|
|
|
|
|
|
|
PyObject *moo_strv_to_pyobject (char **strv)
|
|
|
|
{
|
|
|
|
PyObject *result;
|
|
|
|
guint len, i;
|
|
|
|
|
|
|
|
if (!strv)
|
|
|
|
{
|
|
|
|
return_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = g_strv_length (strv);
|
|
|
|
result = PyTuple_New (len);
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
PyTuple_SET_ITEM (result, i,
|
|
|
|
PyString_FromString (strv[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int moo_pyobject_to_strv (PyObject *obj, char ***dest)
|
|
|
|
{
|
|
|
|
int len, i;
|
|
|
|
|
|
|
|
if (obj == Py_None)
|
|
|
|
{
|
|
|
|
*dest = NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PySequence_Check (obj))
|
|
|
|
{
|
|
|
|
PyErr_SetString (PyExc_TypeError,
|
|
|
|
"argument must be a sequence");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = PySequence_Size (obj);
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
PyErr_SetString (PyExc_RuntimeError,
|
|
|
|
"got negative length of a sequence");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
PyObject *item = PySequence_ITEM (obj, i);
|
|
|
|
|
|
|
|
g_return_val_if_fail (item != NULL, FALSE);
|
|
|
|
|
|
|
|
if (!PyString_Check (item))
|
|
|
|
{
|
|
|
|
PyErr_SetString (PyExc_TypeError,
|
|
|
|
"argument must be a sequence of strings");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest = g_new (char*, len + 1);
|
|
|
|
(*dest)[len] = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
PyObject *item = PySequence_ITEM (obj, i);
|
|
|
|
(*dest)[i] = g_strdup (PyString_AS_STRING (item));
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2005-07-30 09:21:40 -07:00
|
|
|
|
|
|
|
|
|
|
|
PyObject *moo_gvalue_to_pyobject (const GValue *val)
|
|
|
|
{
|
|
|
|
return pyg_value_as_pyobject (val, TRUE);
|
|
|
|
}
|
2005-11-07 19:24:34 -08:00
|
|
|
|
|
|
|
|
|
|
|
typedef PyObject *(*PtrToPy) (gpointer ptr);
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
slist_to_pyobject (GSList *list,
|
|
|
|
PtrToPy func)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
GSList *l;
|
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
result = PyList_New (g_slist_length (list));
|
|
|
|
|
|
|
|
for (i = 0, l = list; l != NULL; l = l->next, ++i)
|
|
|
|
{
|
|
|
|
PyObject *item = func (l->data);
|
|
|
|
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
Py_DECREF (result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyList_SetItem (result, i, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject*
|
|
|
|
moo_object_slist_to_pyobject (GSList *list)
|
|
|
|
{
|
|
|
|
return slist_to_pyobject (list, (PtrToPy) pygobject_new);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
string_to_pyobject (gpointer str)
|
|
|
|
{
|
|
|
|
if (!str)
|
|
|
|
return_RuntimeErr ("got NULL string");
|
|
|
|
else
|
|
|
|
return PyString_FromString (str);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject*
|
|
|
|
moo_string_slist_to_pyobject (GSList *list)
|
|
|
|
{
|
|
|
|
return slist_to_pyobject (list, string_to_pyobject);
|
|
|
|
}
|
2005-11-11 12:14:48 -08:00
|
|
|
|
|
|
|
|
|
|
|
PyObject*
|
|
|
|
moo_py_object_ref (PyObject *obj)
|
|
|
|
{
|
|
|
|
Py_XINCREF (obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
moo_py_object_unref (PyObject *obj)
|
|
|
|
{
|
|
|
|
Py_XDECREF (obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GType
|
|
|
|
moo_py_object_get_type (void)
|
|
|
|
{
|
|
|
|
static GType type = 0;
|
|
|
|
|
|
|
|
if (!type)
|
|
|
|
type = g_boxed_type_register_static ("MooPyObject",
|
|
|
|
(GBoxedCopyFunc) moo_py_object_ref,
|
|
|
|
(GBoxedFreeFunc) moo_py_object_unref);
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|