diff options
-rw-r--r-- | gobject/gobjectmodule.c | 3 | ||||
-rw-r--r-- | gobject/pygobject-private.h | 1 | ||||
-rw-r--r-- | gobject/pygobject.h | 2 | ||||
-rw-r--r-- | gobject/pygtype.c | 45 |
4 files changed, 50 insertions, 1 deletions
diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c index bd2ca89..51a9659 100644 --- a/gobject/gobjectmodule.c +++ b/gobject/gobjectmodule.c @@ -2104,7 +2104,8 @@ struct _PyGObject_Functions pygobject_api_functions = { &PyGParamSpec_Type, pyg_param_spec_new, - pyg_param_spec_from_object + pyg_param_spec_from_object, + pyg_pyobj_to_unichar_conv }; DL_EXPORT(void) diff --git a/gobject/pygobject-private.h b/gobject/pygobject-private.h index 6de2d02..6c8bde1 100644 --- a/gobject/pygobject-private.h +++ b/gobject/pygobject-private.h @@ -44,6 +44,7 @@ GType pyg_type_from_object (PyObject *obj); gint pyg_enum_get_value (GType enum_type, PyObject *obj, gint *val); gint pyg_flags_get_value (GType flag_type, PyObject *obj, gint *val); +int pyg_pyobj_to_unichar_conv (PyObject* py_obj, void* ptr); typedef PyObject *(* fromvaluefunc)(const GValue *value); typedef int (*tovaluefunc)(GValue *value, PyObject *obj); diff --git a/gobject/pygobject.h b/gobject/pygobject.h index a4cdfce..6dbd8a5 100644 --- a/gobject/pygobject.h +++ b/gobject/pygobject.h @@ -110,6 +110,7 @@ struct _PyGObject_Functions { PyTypeObject *paramspec_type; PyObject *(* paramspec_new)(GParamSpec *spec); GParamSpec *(*paramspec_get)(PyObject *tuple); + int (*pyobj_to_unichar_conv)(PyObject *pyobj, void* ptr); }; #ifndef _INSIDE_PYGOBJECT_ @@ -150,6 +151,7 @@ struct _PyGObject_Functions *_PyGObject_API; #define PyGParamSpec_Type (*_PyGObject_API->paramspec_type) #define pyg_param_spec_new (_PyGObject_API->paramspec_new) #define pyg_param_spec_from_object (_PyGObject_API->paramspec_get) +#define pyg_pyobj_to_unichar_conv (_PyGObject_API->pyobj_to_unichar_conv) #define pyg_block_threads() G_STMT_START { \ if (_PyGObject_API->block_threads != NULL) \ (* _PyGObject_API->block_threads)(); \ diff --git a/gobject/pygtype.c b/gobject/pygtype.c index 908a02f..179182b 100644 --- a/gobject/pygtype.c +++ b/gobject/pygtype.c @@ -1160,3 +1160,48 @@ pyg_object_descr_doc_get(void) } return doc_descr; } + + +/** + * pyg_pyobj_to_unichar_conv: + * + * Converts PyObject value to a unichar and write result to memory + * pointed to by ptr. Follows the calling convention of a ParseArgs + * converter (O& format specifier) so it may be used to convert function + * arguments. + * + * Returns: 1 if the conversion succeeds and 0 otherwise. If the conversion + * did not succeesd, a Python exception is raised + */ +int pyg_pyobj_to_unichar_conv(PyObject* py_obj, void* ptr) +{ + gunichar* u = ptr; + const Py_UNICODE* uni_buffer; + PyObject* tmp_uni = NULL; + + if (PyUnicode_Check(py_obj)) { + tmp_uni = py_obj; + Py_INCREF(tmp_uni); + } + else { + tmp_uni = PyUnicode_FromObject(py_obj); + if (tmp_uni == NULL) + goto failure; + } + + if ( PyUnicode_GetSize(tmp_uni) != 1) { + PyErr_SetString(PyExc_ValueError, "unicode character value must be 1 character uniode string"); + goto failure; + } + uni_buffer = PyUnicode_AsUnicode(tmp_uni); + if ( uni_buffer == NULL) + goto failure; + *u = uni_buffer[0]; + + Py_DECREF(tmp_uni); + return 1; + + failure: + Py_XDECREF(tmp_uni); + return 0; +} |