[pygobject] Expose all GI enum and flags types
- From: Simon Feltman <sfeltman src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] Expose all GI enum and flags types
- Date: Mon, 7 Oct 2013 22:06:54 +0000 (UTC)
commit e190eb75093e8bf36190dc1beb18d1c1b95b9582
Author: Simon Feltman <sfeltman src gnome org>
Date: Fri Oct 4 13:46:36 2013 -0700
Expose all GI enum and flags types
Add new types for GIDirection, GITransfer, GIArrayType, GIScopeType,
GIVFuncInfoFlags, GIFieldInfoFlags, GIFuncitonInfoFlags, GITypeTag, and
GInfoType. These types are found in the gi._gi module exposed without the
"GI" prefix and contain all of their values as class attributes. e.g.
gi._gi.Transfer.EVERYTHING.
https://bugzilla.gnome.org/show_bug.cgi?id=709008
gi/docstring.py | 10 +--
gi/pygi-info.c | 150 +++++++++++++++++++++++++++++++++++++++++++---
tests/test_repository.py | 13 ++++-
3 files changed, 158 insertions(+), 15 deletions(-)
---
diff --git a/gi/docstring.py b/gi/docstring.py
index 713bb6e..3fa4dd8 100644
--- a/gi/docstring.py
+++ b/gi/docstring.py
@@ -23,9 +23,7 @@
from ._gi import \
VFuncInfo, \
FunctionInfo, \
- DIRECTION_IN, \
- DIRECTION_OUT, \
- DIRECTION_INOUT
+ Direction
#: Module storage for currently registered doc string generator function.
@@ -60,7 +58,7 @@ def generate_doc_string(info):
def split_function_info_args(info):
"""Split a functions args into a tuple of two lists.
- Note that args marked as DIRECTION_INOUT will be in both lists.
+ Note that args marked as Direction.INOUT will be in both lists.
:Returns:
Tuple of (in_args, out_args)
@@ -69,9 +67,9 @@ def split_function_info_args(info):
out_args = []
for arg in info.get_arguments():
direction = arg.get_direction()
- if direction in (DIRECTION_IN, DIRECTION_INOUT):
+ if direction in (Direction.IN, Direction.INOUT):
in_args.append(arg)
- if direction in (DIRECTION_OUT, DIRECTION_INOUT):
+ if direction in (Direction.OUT, Direction.INOUT):
out_args.append(arg)
return (in_args, out_args)
diff --git a/gi/pygi-info.c b/gi/pygi-info.c
index c087cb6..06bf1dc 100644
--- a/gi/pygi-info.c
+++ b/gi/pygi-info.c
@@ -1685,13 +1685,6 @@ _pygi_info_register_types (PyObject *m)
if (PyModule_AddObject(m, "BaseInfo", (PyObject *)&PyGIBaseInfo_Type))
return;
- if (PyModule_AddObject(m, "DIRECTION_IN", PyLong_FromLong(GI_DIRECTION_IN)))
- return;
- if (PyModule_AddObject(m, "DIRECTION_OUT", PyLong_FromLong(GI_DIRECTION_OUT)))
- return;
- if (PyModule_AddObject(m, "DIRECTION_INOUT", PyLong_FromLong(GI_DIRECTION_INOUT)))
- return;
-
_PyGI_REGISTER_TYPE (m, PyGICallableInfo_Type, CallableInfo,
PyGIBaseInfo_Type);
PyGICallableInfo_Type.tp_call = (ternaryfunc) _callable_info_call;
@@ -1742,6 +1735,147 @@ _pygi_info_register_types (PyObject *m)
_PyGI_REGISTER_TYPE (m, PyGITypeInfo_Type, TypeInfo,
PyGIBaseInfo_Type);
-
#undef _PyGI_REGISTER_TYPE
+
+#define _PyGI_ENUM_BEGIN(name) \
+ { \
+ const char *__enum_name = #name; \
+ PyObject *__enum_value = NULL; \
+ PyObject *__new_enum_cls = NULL; \
+ PyObject *__enum_instance_dict = PyDict_New(); \
+ PyObject *__module_name = PyObject_GetAttrString (m, "__name__"); \
+ PyDict_SetItemString (__enum_instance_dict, "__module__", __module_name); \
+ Py_DECREF (__module_name);
+
+#define _PyGI_ENUM_ADD_VALUE(prefix, name) \
+ __enum_value = PYGLIB_PyLong_FromLong (prefix##_##name); \
+ if (PyDict_SetItemString(__enum_instance_dict, #name, __enum_value)) { \
+ Py_DECREF (__enum_instance_dict); \
+ Py_DECREF (__enum_value); \
+ return; \
+ } \
+ Py_DECREF (__enum_value);
+
+#define _PyGI_ENUM_END \
+ __new_enum_cls = PyObject_CallFunction ((PyObject *)&PyType_Type, "s(O)O", \
+ __enum_name, (PyObject *)&PyType_Type, \
+ __enum_instance_dict); \
+ Py_DECREF (__enum_instance_dict); \
+ PyModule_AddObject (m, __enum_name, __new_enum_cls); /* steals ref */ \
+ }
+
+
+ /* GIDirection */
+ _PyGI_ENUM_BEGIN (Direction)
+ _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, IN)
+ _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, OUT)
+ _PyGI_ENUM_ADD_VALUE (GI_DIRECTION, INOUT)
+ _PyGI_ENUM_END
+
+
+ /* GITransfer */
+ _PyGI_ENUM_BEGIN (Transfer)
+ _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, NOTHING)
+ _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, CONTAINER)
+ _PyGI_ENUM_ADD_VALUE (GI_TRANSFER, EVERYTHING)
+ _PyGI_ENUM_END
+
+ /* GIArrayType */
+ _PyGI_ENUM_BEGIN (ArrayType)
+ _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, C)
+ _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, ARRAY)
+ _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, PTR_ARRAY)
+ _PyGI_ENUM_ADD_VALUE (GI_ARRAY_TYPE, BYTE_ARRAY)
+ _PyGI_ENUM_END
+
+ /* GIScopeType */
+ _PyGI_ENUM_BEGIN (ScopeType)
+ _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, INVALID)
+ _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, CALL)
+ _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, ASYNC)
+ _PyGI_ENUM_ADD_VALUE (GI_SCOPE_TYPE, NOTIFIED)
+ _PyGI_ENUM_END
+
+ /* GIVFuncInfoFlags */
+ _PyGI_ENUM_BEGIN (VFuncInfoFlags)
+ _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, CHAIN_UP)
+ _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, OVERRIDE)
+ _PyGI_ENUM_ADD_VALUE (GI_VFUNC_MUST, NOT_OVERRIDE)
+ _PyGI_ENUM_END
+
+ /* GIFieldInfoFlags */
+ _PyGI_ENUM_BEGIN (FieldInfoFlags)
+ _PyGI_ENUM_ADD_VALUE (GI_FIELD, IS_READABLE)
+ _PyGI_ENUM_ADD_VALUE (GI_FIELD, IS_WRITABLE)
+ _PyGI_ENUM_END
+
+ /* GIFunctionInfoFlags */
+ _PyGI_ENUM_BEGIN (FunctionInfoFlags)
+ _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_METHOD)
+ _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_CONSTRUCTOR)
+ _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_GETTER)
+ _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, IS_SETTER)
+ _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, WRAPS_VFUNC)
+ _PyGI_ENUM_ADD_VALUE (GI_FUNCTION, THROWS)
+ _PyGI_ENUM_END
+
+ /* GITypeTag */
+ _PyGI_ENUM_BEGIN (TypeTag)
+ /* Basic types */
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, VOID)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, BOOLEAN)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT8)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT8)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT16)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT16)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT32)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT32)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INT64)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UINT64)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, FLOAT)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, DOUBLE)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GTYPE)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UTF8)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, FILENAME)
+
+ /* Non-basic types; compare with G_TYPE_TAG_IS_BASIC */
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, ARRAY)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, INTERFACE)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GLIST)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GSLIST)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, GHASH)
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, ERROR)
+
+ /* Another basic type */
+ _PyGI_ENUM_ADD_VALUE (GI_TYPE_TAG, UNICHAR)
+ _PyGI_ENUM_END
+
+ /* GIInfoType */
+ _PyGI_ENUM_BEGIN (InfoType)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, INVALID)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, FUNCTION)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, CALLBACK)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, STRUCT)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, BOXED)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, ENUM)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, FLAGS)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, OBJECT)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, INTERFACE)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, CONSTANT)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, INVALID_0)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, UNION)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, VALUE)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, SIGNAL)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, VFUNC)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, PROPERTY)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, FIELD)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, ARG)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, TYPE)
+ _PyGI_ENUM_ADD_VALUE (GI_INFO_TYPE, UNRESOLVED)
+ _PyGI_ENUM_END
+
+#undef _PyGI_ENUM_BEGIN
+#undef _PyGI_ENUM_ADD_VALUE
+#undef _PyGI_ENUM_END
+
}
diff --git a/tests/test_repository.py b/tests/test_repository.py
index d890c7f..3ce3f0d 100644
--- a/tests/test_repository.py
+++ b/tests/test_repository.py
@@ -58,7 +58,7 @@ class Test(unittest.TestCase):
arg = args[0]
self.assertEqual(arg.get_container(), func_info)
- self.assertEqual(arg.get_direction(), GIRepository.DIRECTION_OUT)
+ self.assertEqual(arg.get_direction(), GIRepository.Direction.OUT)
self.assertEqual(arg.get_name(), 'structs')
self.assertEqual(arg.get_namespace(), 'GIMarshallingTests')
self.assertEqual(arg.get_pytype_hint(), 'list')
@@ -194,6 +194,17 @@ class Test(unittest.TestCase):
finally:
GLib.log_set_always_fatal(old_mask)
+ def test_enums(self):
+ self.assertTrue(hasattr(GIRepository, 'Direction'))
+ self.assertTrue(hasattr(GIRepository, 'Transfer'))
+ self.assertTrue(hasattr(GIRepository, 'ArrayType'))
+ self.assertTrue(hasattr(GIRepository, 'ScopeType'))
+ self.assertTrue(hasattr(GIRepository, 'VFuncInfoFlags'))
+ self.assertTrue(hasattr(GIRepository, 'FieldInfoFlags'))
+ self.assertTrue(hasattr(GIRepository, 'FunctionInfoFlags'))
+ self.assertTrue(hasattr(GIRepository, 'TypeTag'))
+ self.assertTrue(hasattr(GIRepository, 'InfoType'))
+
if __name__ == '__main__':
unittest.main()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]