Commit c2d5857e authored by Simon Feltman's avatar Simon Feltman

cache refactoring: Move enum and flags arg setup and marshaling to new file

Move enum and flags argument cache setup and marshaling fragments into
isolated file: pygi-enum-marshal.c. Remove pygi-marshal-from/to files.

https://bugzilla.gnome.org/show_bug.cgi?id=709700
parent 1d0f120d
......@@ -97,10 +97,6 @@ _gi_la_SOURCES = \
pygi-invoke-state-struct.h \
pygi-cache.h \
pygi-cache.c \
pygi-marshal-from-py.c \
pygi-marshal-from-py.h \
pygi-marshal-to-py.c \
pygi-marshal-to-py.h \
pygi-marshal-cleanup.c \
pygi-marshal-cleanup.h \
pygi-basictype.c \
......@@ -113,6 +109,8 @@ _gi_la_SOURCES = \
pygi-error.h \
pygi-object.c \
pygi-object.h \
pygi-enum-marshal.c \
pygi-enum-marshal.h \
pygi-struct-marshal.c \
pygi-struct-marshal.h \
pygi-hashtable.c \
......
......@@ -31,8 +31,6 @@
#include <pyglib-python-compat.h>
#include <pyglib.h>
#include "pygi-marshal-from-py.h"
#include "pygi-marshal-to-py.h"
#include "pygi-basictype.h"
#include "pygi-object.h"
#include "pygi-struct-marshal.h"
......
......@@ -24,8 +24,6 @@
#include "pygi-info.h"
#include "pygi-cache.h"
#include "pygi-marshal-to-py.h"
#include "pygi-marshal-from-py.h"
#include "pygi-marshal-cleanup.h"
#include "pygi-type.h"
#include "pygi-hashtable.h"
......@@ -36,6 +34,7 @@
#include "pygi-error.h"
#include "pygi-object.h"
#include "pygi-struct-marshal.h"
#include "pygi-enum-marshal.h"
/* _arg_info_default_value
......@@ -252,35 +251,6 @@ _arg_cache_alloc (void)
return g_slice_new0 (PyGIArgCache);
}
static void
_arg_cache_from_py_interface_enum_setup (PyGIArgCache *arg_cache,
GITransfer transfer)
{
arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_enum;
}
static void
_arg_cache_to_py_interface_enum_setup (PyGIArgCache *arg_cache,
GITransfer transfer)
{
arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_enum;
}
static void
_arg_cache_from_py_interface_flags_setup (PyGIArgCache *arg_cache,
GITransfer transfer)
{
arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_flags;
}
static void
_arg_cache_to_py_interface_flags_setup (PyGIArgCache *arg_cache,
GITransfer transfer)
{
arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_flags;
}
static PyGIArgCache *
_arg_cache_new_for_interface (GIInterfaceInfo *iface_info,
GITypeInfo *type_info,
......@@ -289,21 +259,18 @@ _arg_cache_new_for_interface (GIInterfaceInfo *iface_info,
PyGIDirection direction,
PyGICallableCache *callable_cache)
{
PyGIArgCache *arg_cache = NULL;
GIInfoType info_type;
info_type = g_base_info_get_type ( (GIBaseInfo *)iface_info);
switch (info_type) {
case GI_INFO_TYPE_CALLBACK:
{
return pygi_arg_callback_new_from_info (type_info,
arg_info,
transfer,
direction,
iface_info,
callable_cache);
}
case GI_INFO_TYPE_OBJECT:
case GI_INFO_TYPE_INTERFACE:
return pygi_arg_gobject_new_from_info (type_info,
......@@ -319,40 +286,23 @@ _arg_cache_new_for_interface (GIInterfaceInfo *iface_info,
transfer,
direction,
iface_info);
default:
; /* pass through to old model of setup */
}
arg_cache = pygi_arg_interface_new_from_info (type_info,
arg_info,
transfer,
direction,
iface_info);
if (arg_cache == NULL)
return NULL;
switch (info_type) {
case GI_INFO_TYPE_ENUM:
if (direction & PYGI_DIRECTION_FROM_PYTHON)
_arg_cache_from_py_interface_enum_setup (arg_cache, transfer);
if (direction & PYGI_DIRECTION_TO_PYTHON)
_arg_cache_to_py_interface_enum_setup (arg_cache, transfer);
break;
return pygi_arg_enum_new_from_info (type_info,
arg_info,
transfer,
direction,
iface_info);
case GI_INFO_TYPE_FLAGS:
if (direction & PYGI_DIRECTION_FROM_PYTHON)
_arg_cache_from_py_interface_flags_setup (arg_cache, transfer);
if (direction & PYGI_DIRECTION_TO_PYTHON)
_arg_cache_to_py_interface_flags_setup (arg_cache, transfer);
break;
return pygi_arg_flags_new_from_info (type_info,
arg_info,
transfer,
direction,
iface_info);
default:
g_assert_not_reached ();
}
return arg_cache;
return NULL;
}
PyGIArgCache *
......
/* -*- Mode: C; c-basic-offset: 4 -*-
* vim: tabstop=4 shiftwidth=4 expandtab
*
* Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>, Red Hat, Inc.
*
* pygi-marshal-from-py.c: Functions to convert PyObjects to C types.
* Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
* Copyright (C) 2014 Simon Feltman <sfeltman@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -16,21 +15,15 @@
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "pygi-private.h"
#include <string.h>
#include <time.h>
#include <pygobject.h>
#include <glib.h>
#include <Python.h>
#include <pyglib-python-compat.h>
#include "pygi-cache.h"
#include "pygi-marshal-cleanup.h"
#include "pygi-marshal-from-py.h"
#include "pygi-enum-marshal.h"
#include "pygi-private.h"
#ifdef _WIN32
#ifdef _MSC_VER
......@@ -87,7 +80,45 @@ gi_argument_from_c_long (GIArgument *arg_out,
}
}
gboolean
static gboolean
gi_argument_to_c_long (GIArgument *arg_in,
long *c_long_out,
GITypeTag type_tag)
{
switch (type_tag) {
case GI_TYPE_TAG_INT8:
*c_long_out = arg_in->v_int8;
return TRUE;
case GI_TYPE_TAG_UINT8:
*c_long_out = arg_in->v_uint8;
return TRUE;
case GI_TYPE_TAG_INT16:
*c_long_out = arg_in->v_int16;
return TRUE;
case GI_TYPE_TAG_UINT16:
*c_long_out = arg_in->v_uint16;
return TRUE;
case GI_TYPE_TAG_INT32:
*c_long_out = arg_in->v_int32;
return TRUE;
case GI_TYPE_TAG_UINT32:
*c_long_out = arg_in->v_uint32;
return TRUE;
case GI_TYPE_TAG_INT64:
*c_long_out = arg_in->v_int64;
return TRUE;
case GI_TYPE_TAG_UINT64:
*c_long_out = arg_in->v_uint64;
return TRUE;
default:
PyErr_Format (PyExc_TypeError,
"Unable to marshal %s to C long",
g_type_tag_to_string (type_tag));
return FALSE;
}
}
static gboolean
_pygi_marshal_from_py_interface_enum (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
......@@ -156,7 +187,7 @@ err:
return FALSE;
}
gboolean
static gboolean
_pygi_marshal_from_py_interface_flags (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
......@@ -204,3 +235,174 @@ err:
}
static PyObject *
_pygi_marshal_to_py_interface_enum (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
GIArgument *arg)
{
PyObject *py_obj = NULL;
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
GIBaseInfo *interface;
long c_long;
interface = g_type_info_get_interface (arg_cache->type_info);
g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_ENUM);
if (!gi_argument_to_c_long(arg, &c_long,
g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
return NULL;
}
if (iface_cache->g_type == G_TYPE_NONE) {
py_obj = PyObject_CallFunction (iface_cache->py_type, "l", c_long);
} else {
py_obj = pyg_enum_from_gtype (iface_cache->g_type, c_long);
}
g_base_info_unref (interface);
return py_obj;
}
static PyObject *
_pygi_marshal_to_py_interface_flags (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
GIArgument *arg)
{
PyObject *py_obj = NULL;
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
GIBaseInfo *interface;
long c_long;
interface = g_type_info_get_interface (arg_cache->type_info);
g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_FLAGS);
if (!gi_argument_to_c_long(arg, &c_long,
g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
g_base_info_unref (interface);
return NULL;
}
g_base_info_unref (interface);
if (iface_cache->g_type == G_TYPE_NONE) {
/* An enum with a GType of None is an enum without GType */
PyObject *py_type = _pygi_type_import_by_gi_info (iface_cache->interface_info);
PyObject *py_args = NULL;
if (!py_type)
return NULL;
py_args = PyTuple_New (1);
if (PyTuple_SetItem (py_args, 0, PyLong_FromLong (c_long)) != 0) {
Py_DECREF (py_args);
Py_DECREF (py_type);
return NULL;
}
py_obj = PyObject_CallFunction (py_type, "l", c_long);
Py_DECREF (py_args);
Py_DECREF (py_type);
} else {
py_obj = pyg_flags_from_gtype (iface_cache->g_type, c_long);
}
return py_obj;
}
static gboolean
pygi_arg_enum_setup_from_info (PyGIArgCache *arg_cache,
GITypeInfo *type_info,
GIArgInfo *arg_info,
GITransfer transfer,
PyGIDirection direction)
{
if (direction & PYGI_DIRECTION_FROM_PYTHON)
arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_enum;
if (direction & PYGI_DIRECTION_TO_PYTHON)
arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_enum;
return TRUE;
}
PyGIArgCache *
pygi_arg_enum_new_from_info (GITypeInfo *type_info,
GIArgInfo *arg_info,
GITransfer transfer,
PyGIDirection direction,
GIInterfaceInfo *iface_info)
{
gboolean res = FALSE;
PyGIArgCache *cache = NULL;
cache = pygi_arg_interface_new_from_info (type_info,
arg_info,
transfer,
direction,
iface_info);
if (cache == NULL)
return NULL;
res = pygi_arg_enum_setup_from_info (cache,
type_info,
arg_info,
transfer,
direction);
if (res) {
return cache;
} else {
_pygi_arg_cache_free (cache);
return NULL;
}
}
static gboolean
pygi_arg_flags_setup_from_info (PyGIArgCache *arg_cache,
GITypeInfo *type_info,
GIArgInfo *arg_info,
GITransfer transfer,
PyGIDirection direction)
{
if (direction & PYGI_DIRECTION_FROM_PYTHON)
arg_cache->from_py_marshaller = _pygi_marshal_from_py_interface_flags;
if (direction & PYGI_DIRECTION_TO_PYTHON)
arg_cache->to_py_marshaller = _pygi_marshal_to_py_interface_flags;
return TRUE;
}
PyGIArgCache *
pygi_arg_flags_new_from_info (GITypeInfo *type_info,
GIArgInfo *arg_info,
GITransfer transfer,
PyGIDirection direction,
GIInterfaceInfo *iface_info)
{
gboolean res = FALSE;
PyGIArgCache *cache = NULL;
cache = pygi_arg_interface_new_from_info (type_info,
arg_info,
transfer,
direction,
iface_info);
if (cache == NULL)
return NULL;
res = pygi_arg_flags_setup_from_info (cache,
type_info,
arg_info,
transfer,
direction);
if (res) {
return cache;
} else {
_pygi_arg_cache_free (cache);
return NULL;
}
}
/* -*- Mode: C; c-basic-offset: 4 -*-
* vim: tabstop=4 shiftwidth=4 expandtab
*
* Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>, Red Hat, Inc.
* Copyright (C) 2014 Simon Feltman <sfeltman@gnome.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
......@@ -14,21 +14,29 @@
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __PYGI_MARSHAL_TO_PY_H__
#define __PYGI_MARSHAL_TO_PY_H__
#ifndef __PYGI_ENUM_MARSHAL_H__
#define __PYGI_ENUM_MARSHAL_H__
PyObject *_pygi_marshal_to_py_interface_enum (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
GIArgument *arg);
PyObject *_pygi_marshal_to_py_interface_flags (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
GIArgument *arg);
#include <girepository.h>
#include "pygi-cache.h"
#endif /* __PYGI_MARSHAL_TO_PY_H__ */
G_BEGIN_DECLS
PyGIArgCache *pygi_arg_enum_new_from_info (GITypeInfo *type_info,
GIArgInfo *arg_info, /* may be null */
GITransfer transfer,
PyGIDirection direction,
GIInterfaceInfo *iface_info);
PyGIArgCache *pygi_arg_flags_new_from_info (GITypeInfo *type_info,
GIArgInfo *arg_info, /* may be null */
GITransfer transfer,
PyGIDirection direction,
GIInterfaceInfo *iface_info);
G_END_DECLS
#endif /*__PYGI_ENUM_MARSHAL_H__*/
/* -*- Mode: C; c-basic-offset: 4 -*-
* vim: tabstop=4 shiftwidth=4 expandtab
*
* Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>, Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#ifndef __PYGI_MARSHAL_from_py_PY_H__
#define __PYGI_MARSHAL_from_py_PY_H__
#include <Python.h>
#include <girepository.h>
#include "pygi-private.h"
G_BEGIN_DECLS
gboolean _pygi_marshal_from_py_ssize_t (PyGIArgCache *arg_cache,
Py_ssize_t size,
GIArgument *arg);
gboolean _pygi_marshal_from_py_interface_enum (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg,
gpointer *cleanup_data);
gboolean _pygi_marshal_from_py_interface_flags (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg,
gpointer *cleanup_data);
gboolean _pygi_marshal_from_py_interface_union (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
PyObject *py_arg,
GIArgument *arg,
gpointer *cleanup_data);
G_END_DECLS
#endif /* __PYGI_MARSHAL_from_py_PY__ */
/* -*- Mode: C; c-basic-offset: 4 -*-
* vim: tabstop=4 shiftwidth=4 expandtab
*
* Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>, Red Hat, Inc.
*
* pygi-marshal-from-py.c: functions for converting C types to PyObject
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include "pygi-private.h"
#include "pygobject-private.h"
#include "pygparamspec.h"
#include <string.h>
#include <time.h>
#include <pyglib.h>
#include <pyglib-python-compat.h>
#include "pygi-cache.h"
#include "pygi-marshal-cleanup.h"
#include "pygi-marshal-to-py.h"
#include "pygi-argument.h"
static gboolean
gi_argument_to_c_long (GIArgument *arg_in,
long *c_long_out,
GITypeTag type_tag)
{
switch (type_tag) {
case GI_TYPE_TAG_INT8:
*c_long_out = arg_in->v_int8;
return TRUE;
case GI_TYPE_TAG_UINT8:
*c_long_out = arg_in->v_uint8;
return TRUE;
case GI_TYPE_TAG_INT16:
*c_long_out = arg_in->v_int16;
return TRUE;
case GI_TYPE_TAG_UINT16:
*c_long_out = arg_in->v_uint16;
return TRUE;
case GI_TYPE_TAG_INT32:
*c_long_out = arg_in->v_int32;
return TRUE;
case GI_TYPE_TAG_UINT32:
*c_long_out = arg_in->v_uint32;
return TRUE;
case GI_TYPE_TAG_INT64:
*c_long_out = arg_in->v_int64;
return TRUE;
case GI_TYPE_TAG_UINT64:
*c_long_out = arg_in->v_uint64;
return TRUE;
default:
PyErr_Format (PyExc_TypeError,
"Unable to marshal %s to C long",
g_type_tag_to_string (type_tag));
return FALSE;
}
}
PyObject *
_pygi_marshal_to_py_interface_enum (PyGIInvokeState *state,
PyGICallableCache *callable_cache,
PyGIArgCache *arg_cache,
GIArgument *arg)
{
PyObject *py_obj = NULL;
PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
GIBaseInfo *interface;
long c_long;
interface = g_type_info_get_interface (arg_cache->type_info);
g_assert (g_base_info_get_type (interface) == GI_INFO_TYPE_ENUM);
if (!gi_argument_to_c_long(arg, &c_long,
g_enum_info_get_storage_type ((GIEnumInfo *)interface))) {
return NULL;
}
if (iface_cache->g_type == G_TYPE_NONE) {
py_obj = PyObject_CallFunction (iface_cache->py_type, "l", c_long);
} else {
py_obj = pyg_enum_from_gtype (iface_cache->g_type, c_long);
}
g_base_info_unref (interface);