pygobject.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /* -*- Mode: C; c-basic-offset: 4 -*- */
  2. #ifndef _PYGOBJECT_H_
  3. #define _PYGOBJECT_H_
  4. #include <Python.h>
  5. #include <glib.h>
  6. #include <glib-object.h>
  7. G_BEGIN_DECLS
  8. /* PyGClosure is a _private_ structure */
  9. typedef void (* PyClosureExceptionHandler) (GValue *ret, guint n_param_values, const GValue *params);
  10. typedef struct _PyGClosure PyGClosure;
  11. typedef struct _PyGObjectData PyGObjectData;
  12. struct _PyGClosure {
  13. GClosure closure;
  14. PyObject *callback;
  15. PyObject *extra_args; /* tuple of extra args to pass to callback */
  16. PyObject *swap_data; /* other object for gtk_signal_connect__object */
  17. PyClosureExceptionHandler exception_handler;
  18. };
  19. typedef enum {
  20. PYGOBJECT_USING_TOGGLE_REF = 1 << 0,
  21. PYGOBJECT_IS_FLOATING_REF = 1 << 1,
  22. PYGOBJECT_GOBJECT_WAS_FLOATING = 1 << 2
  23. } PyGObjectFlags;
  24. /* closures is just an alias for what is found in the
  25. * PyGObjectData */
  26. typedef struct {
  27. PyObject_HEAD
  28. GObject *obj;
  29. PyObject *inst_dict; /* the instance dictionary -- must be last */
  30. PyObject *weakreflist; /* list of weak references */
  31. /*< private >*/
  32. /* using union to preserve ABI compatibility (structure size
  33. * must not change) */
  34. union {
  35. GSList *closures; /* stale field; no longer updated DO-NOT-USE! */
  36. PyGObjectFlags flags;
  37. } private_flags;
  38. } PyGObject;
  39. #define pygobject_get(v) (((PyGObject *)(v))->obj)
  40. #define pygobject_check(v,base) (PyObject_TypeCheck(v,base))
  41. typedef struct {
  42. PyObject_HEAD
  43. gpointer boxed;
  44. GType gtype;
  45. gboolean free_on_dealloc;
  46. } PyGBoxed;
  47. #define pyg_boxed_get(v,t) ((t *)((PyGBoxed *)(v))->boxed)
  48. #define pyg_boxed_get_ptr(v) (((PyGBoxed *)(v))->boxed)
  49. #define pyg_boxed_set_ptr(v,p) (((PyGBoxed *)(v))->boxed = (gpointer)p)
  50. #define pyg_boxed_check(v,typecode) (PyObject_TypeCheck(v, &PyGBoxed_Type) && ((PyGBoxed *)(v))->gtype == typecode)
  51. typedef struct {
  52. PyObject_HEAD
  53. gpointer pointer;
  54. GType gtype;
  55. } PyGPointer;
  56. #define pyg_pointer_get(v,t) ((t *)((PyGPointer *)(v))->pointer)
  57. #define pyg_pointer_get_ptr(v) (((PyGPointer *)(v))->pointer)
  58. #define pyg_pointer_set_ptr(v,p) (((PyGPointer *)(v))->pointer = (gpointer)p)
  59. #define pyg_pointer_check(v,typecode) (PyObject_TypeCheck(v, &PyGPointer_Type) && ((PyGPointer *)(v))->gtype == typecode)
  60. typedef void (*PyGFatalExceptionFunc) (void);
  61. typedef void (*PyGThreadBlockFunc) (void);
  62. typedef struct {
  63. PyObject_HEAD
  64. GParamSpec *pspec;
  65. } PyGParamSpec;
  66. #define pyg_param_spec_get(v) (((PyGParamSpec *)v)->pspec)
  67. #define pyg_param_spec_set(v,p) (((PyGParamSpec *)v)->pspec = (GParamSpec*)p)
  68. #define pyg_param_spec_check(v) (PyObject_TypeCheck(v, &PyGParamSpec_Type))
  69. /* Deprecated in favor of lower case with underscore macros above. */
  70. #define PyGParamSpec_Get pyg_param_spec_get
  71. #define PyGParamSpec_Check pyg_param_spec_check
  72. typedef int (*PyGClassInitFunc) (gpointer gclass, PyTypeObject *pyclass);
  73. typedef PyTypeObject * (*PyGTypeRegistrationFunction) (const gchar *name,
  74. gpointer data);
  75. struct _PyGObject_Functions {
  76. /*
  77. * All field names in here are considered private,
  78. * use the macros below instead, which provides stability
  79. */
  80. void (* register_class)(PyObject *dict, const gchar *class_name,
  81. GType gtype, PyTypeObject *type, PyObject *bases);
  82. void (* register_wrapper)(PyObject *self);
  83. PyTypeObject *(* lookup_class)(GType type);
  84. PyObject *(* newgobj)(GObject *obj);
  85. GClosure *(* closure_new)(PyObject *callback, PyObject *extra_args,
  86. PyObject *swap_data);
  87. void (* object_watch_closure)(PyObject *self, GClosure *closure);
  88. GDestroyNotify destroy_notify;
  89. GType (* type_from_object)(PyObject *obj);
  90. PyObject *(* type_wrapper_new)(GType type);
  91. gint (* enum_get_value)(GType enum_type, PyObject *obj, gint *val);
  92. gint (* flags_get_value)(GType flag_type, PyObject *obj, guint *val);
  93. void (* register_gtype_custom)(GType gtype,
  94. PyObject *(* from_func)(const GValue *value),
  95. int (* to_func)(GValue *value, PyObject *obj));
  96. int (* value_from_pyobject)(GValue *value, PyObject *obj);
  97. PyObject *(* value_as_pyobject)(const GValue *value, gboolean copy_boxed);
  98. void (* register_interface)(PyObject *dict, const gchar *class_name,
  99. GType gtype, PyTypeObject *type);
  100. PyTypeObject *boxed_type;
  101. void (* register_boxed)(PyObject *dict, const gchar *class_name,
  102. GType boxed_type, PyTypeObject *type);
  103. PyObject *(* boxed_new)(GType boxed_type, gpointer boxed,
  104. gboolean copy_boxed, gboolean own_ref);
  105. PyTypeObject *pointer_type;
  106. void (* register_pointer)(PyObject *dict, const gchar *class_name,
  107. GType pointer_type, PyTypeObject *type);
  108. PyObject *(* pointer_new)(GType boxed_type, gpointer pointer);
  109. void (* enum_add_constants)(PyObject *module, GType enum_type,
  110. const gchar *strip_prefix);
  111. void (* flags_add_constants)(PyObject *module, GType flags_type,
  112. const gchar *strip_prefix);
  113. const gchar *(* constant_strip_prefix)(const gchar *name,
  114. const gchar *strip_prefix);
  115. gboolean (* error_check)(GError **error);
  116. /* hooks to register handlers for getting GDK threads to cooperate
  117. * with python threading */
  118. void (* set_thread_block_funcs) (PyGThreadBlockFunc block_threads_func,
  119. PyGThreadBlockFunc unblock_threads_func);
  120. PyGThreadBlockFunc block_threads;
  121. PyGThreadBlockFunc unblock_threads;
  122. PyTypeObject *paramspec_type;
  123. PyObject *(* paramspec_new)(GParamSpec *spec);
  124. GParamSpec *(*paramspec_get)(PyObject *tuple);
  125. int (*pyobj_to_unichar_conv)(PyObject *pyobj, void* ptr);
  126. gboolean (*parse_constructor_args)(GType obj_type,
  127. char **arg_names,
  128. char **prop_names,
  129. GParameter *params,
  130. guint *nparams,
  131. PyObject **py_args);
  132. PyObject *(* param_gvalue_as_pyobject) (const GValue* gvalue,
  133. gboolean copy_boxed,
  134. const GParamSpec* pspec);
  135. int (* gvalue_from_param_pyobject) (GValue* value,
  136. PyObject* py_obj,
  137. const GParamSpec* pspec);
  138. PyTypeObject *enum_type;
  139. PyObject *(*enum_add)(PyObject *module,
  140. const char *type_name_,
  141. const char *strip_prefix,
  142. GType gtype);
  143. PyObject* (*enum_from_gtype)(GType gtype, int value);
  144. PyTypeObject *flags_type;
  145. PyObject *(*flags_add)(PyObject *module,
  146. const char *type_name_,
  147. const char *strip_prefix,
  148. GType gtype);
  149. PyObject* (*flags_from_gtype)(GType gtype, guint value);
  150. gboolean threads_enabled;
  151. int (*enable_threads) (void);
  152. int (*gil_state_ensure) (void);
  153. void (*gil_state_release) (int flag);
  154. void (*register_class_init) (GType gtype, PyGClassInitFunc class_init);
  155. void (*register_interface_info) (GType gtype, const GInterfaceInfo *info);
  156. void (*closure_set_exception_handler) (GClosure *closure, PyClosureExceptionHandler handler);
  157. void (*add_warning_redirection) (const char *domain,
  158. PyObject *warning);
  159. void (*disable_warning_redirections) (void);
  160. /* type_register_custom API now removed, but leave a pointer here to not
  161. * break ABI. */
  162. void *_type_register_custom;
  163. gboolean (*gerror_exception_check) (GError **error);
  164. PyObject* (*option_group_new) (GOptionGroup *group);
  165. GType (* type_from_object_strict) (PyObject *obj, gboolean strict);
  166. PyObject *(* newgobj_full)(GObject *obj, gboolean steal, gpointer g_class);
  167. PyTypeObject *object_type;
  168. int (* value_from_pyobject_with_error)(GValue *value, PyObject *obj);
  169. };
  170. #ifdef DISABLE_THREADING
  171. # define pyg_threads_enabled FALSE
  172. # define pyg_gil_state_ensure() 0
  173. # define pyg_gil_state_release(state)
  174. # define pyg_begin_allow_threads G_STMT_START {
  175. # define pyg_end_allow_threads } G_STMT_END
  176. #else
  177. # define pyg_threads_enabled TRUE
  178. # define pyg_gil_state_ensure PyGILState_Ensure
  179. # define pyg_gil_state_release PyGILState_Release
  180. # define pyg_begin_allow_threads Py_BEGIN_ALLOW_THREADS
  181. # define pyg_end_allow_threads Py_END_ALLOW_THREADS
  182. #endif
  183. /* Deprecated, only available for API compatibility. */
  184. #define pyg_enable_threads()
  185. #define pyg_set_thread_block_funcs(a, b)
  186. #define pyg_block_threads()
  187. #define pyg_unblock_threads()
  188. #ifndef _INSIDE_PYGOBJECT_
  189. #if defined(NO_IMPORT) || defined(NO_IMPORT_PYGOBJECT)
  190. extern struct _PyGObject_Functions *_PyGObject_API;
  191. #else
  192. struct _PyGObject_Functions *_PyGObject_API;
  193. #endif
  194. #define pygobject_register_class (_PyGObject_API->register_class)
  195. #define pygobject_register_wrapper (_PyGObject_API->register_wrapper)
  196. #define pygobject_lookup_class (_PyGObject_API->lookup_class)
  197. #define pygobject_new (_PyGObject_API->newgobj)
  198. #define pygobject_new_full (_PyGObject_API->newgobj_full)
  199. #define PyGObject_Type (*_PyGObject_API->object_type)
  200. #define pyg_closure_new (_PyGObject_API->closure_new)
  201. #define pygobject_watch_closure (_PyGObject_API->object_watch_closure)
  202. #define pyg_closure_set_exception_handler (_PyGObject_API->closure_set_exception_handler)
  203. #define pyg_destroy_notify (_PyGObject_API->destroy_notify)
  204. #define pyg_type_from_object_strict (_PyGObject_API->type_from_object_strict)
  205. #define pyg_type_from_object (_PyGObject_API->type_from_object)
  206. #define pyg_type_wrapper_new (_PyGObject_API->type_wrapper_new)
  207. #define pyg_enum_get_value (_PyGObject_API->enum_get_value)
  208. #define pyg_flags_get_value (_PyGObject_API->flags_get_value)
  209. #define pyg_register_gtype_custom (_PyGObject_API->register_gtype_custom)
  210. #define pyg_value_from_pyobject (_PyGObject_API->value_from_pyobject)
  211. #define pyg_value_from_pyobject_with_error (_PyGObject_API->value_from_pyobject_with_error)
  212. #define pyg_value_as_pyobject (_PyGObject_API->value_as_pyobject)
  213. #define pyg_register_interface (_PyGObject_API->register_interface)
  214. #define PyGBoxed_Type (*_PyGObject_API->boxed_type)
  215. #define pyg_register_boxed (_PyGObject_API->register_boxed)
  216. #define pyg_boxed_new (_PyGObject_API->boxed_new)
  217. #define PyGPointer_Type (*_PyGObject_API->pointer_type)
  218. #define pyg_register_pointer (_PyGObject_API->register_pointer)
  219. #define pyg_pointer_new (_PyGObject_API->pointer_new)
  220. #define pyg_enum_add_constants (_PyGObject_API->enum_add_constants)
  221. #define pyg_flags_add_constants (_PyGObject_API->flags_add_constants)
  222. #define pyg_constant_strip_prefix (_PyGObject_API->constant_strip_prefix)
  223. #define pyg_error_check (_PyGObject_API->error_check)
  224. #define PyGParamSpec_Type (*_PyGObject_API->paramspec_type)
  225. #define pyg_param_spec_new (_PyGObject_API->paramspec_new)
  226. #define pyg_param_spec_from_object (_PyGObject_API->paramspec_get)
  227. #define pyg_pyobj_to_unichar_conv (_PyGObject_API->pyobj_to_unichar_conv)
  228. #define pyg_parse_constructor_args (_PyGObject_API->parse_constructor_args)
  229. #define pyg_param_gvalue_as_pyobject (_PyGObject_API->value_as_pyobject)
  230. #define pyg_param_gvalue_from_pyobject (_PyGObject_API->gvalue_from_param_pyobject)
  231. #define PyGEnum_Type (*_PyGObject_API->enum_type)
  232. #define pyg_enum_add (_PyGObject_API->enum_add)
  233. #define pyg_enum_from_gtype (_PyGObject_API->enum_from_gtype)
  234. #define PyGFlags_Type (*_PyGObject_API->flags_type)
  235. #define pyg_flags_add (_PyGObject_API->flags_add)
  236. #define pyg_flags_from_gtype (_PyGObject_API->flags_from_gtype)
  237. #define pyg_register_class_init (_PyGObject_API->register_class_init)
  238. #define pyg_register_interface_info (_PyGObject_API->register_interface_info)
  239. #define pyg_add_warning_redirection (_PyGObject_API->add_warning_redirection)
  240. #define pyg_disable_warning_redirections (_PyGObject_API->disable_warning_redirections)
  241. #define pyg_gerror_exception_check (_PyGObject_API->gerror_exception_check)
  242. #define pyg_option_group_new (_PyGObject_API->option_group_new)
  243. /**
  244. * pygobject_init:
  245. * @req_major: minimum version major number, or -1
  246. * @req_minor: minimum version minor number, or -1
  247. * @req_micro: minimum version micro number, or -1
  248. *
  249. * Imports and initializes the 'gobject' python module. Can
  250. * optionally check for a required minimum version if @req_major,
  251. * @req_minor, and @req_micro are all different from -1.
  252. *
  253. * Returns: a new reference to the gobject module on success, NULL in
  254. * case of failure (and raises ImportError).
  255. **/
  256. static inline PyObject *
  257. pygobject_init(int req_major, int req_minor, int req_micro)
  258. {
  259. PyObject *gobject, *cobject;
  260. gobject = PyImport_ImportModule("gi._gobject");
  261. if (!gobject) {
  262. if (PyErr_Occurred())
  263. {
  264. PyObject *type, *value, *traceback;
  265. PyObject *py_orig_exc;
  266. PyErr_Fetch(&type, &value, &traceback);
  267. py_orig_exc = PyObject_Repr(value);
  268. Py_XDECREF(type);
  269. Py_XDECREF(value);
  270. Py_XDECREF(traceback);
  271. #if PY_VERSION_HEX < 0x03000000
  272. PyErr_Format(PyExc_ImportError,
  273. "could not import gobject (error was: %s)",
  274. PyString_AsString(py_orig_exc));
  275. #else
  276. {
  277. /* Can not use PyErr_Format because it doesn't have
  278. * a format string for dealing with PyUnicode objects
  279. * like PyUnicode_FromFormat has
  280. */
  281. PyObject *errmsg = PyUnicode_FromFormat("could not import gobject (error was: %U)",
  282. py_orig_exc);
  283. if (errmsg) {
  284. PyErr_SetObject(PyExc_ImportError,
  285. errmsg);
  286. Py_DECREF(errmsg);
  287. }
  288. /* if errmsg is NULL then we might have OOM
  289. * PyErr should already be set and trying to
  290. * return our own error would be futile
  291. */
  292. }
  293. #endif
  294. Py_DECREF(py_orig_exc);
  295. } else {
  296. PyErr_SetString(PyExc_ImportError,
  297. "could not import gobject (no error given)");
  298. }
  299. return NULL;
  300. }
  301. cobject = PyObject_GetAttrString(gobject, "_PyGObject_API");
  302. if (cobject && PyCapsule_CheckExact(cobject))
  303. _PyGObject_API = (struct _PyGObject_Functions *) PyCapsule_GetPointer(cobject, "gobject._PyGObject_API");
  304. else {
  305. PyErr_SetString(PyExc_ImportError,
  306. "could not import gobject (could not find _PyGObject_API object)");
  307. Py_DECREF(gobject);
  308. return NULL;
  309. }
  310. if (req_major != -1)
  311. {
  312. int found_major, found_minor, found_micro;
  313. PyObject *version;
  314. version = PyObject_GetAttrString(gobject, "pygobject_version");
  315. if (!version) {
  316. PyErr_SetString(PyExc_ImportError,
  317. "could not import gobject (version too old)");
  318. Py_DECREF(gobject);
  319. return NULL;
  320. }
  321. if (!PyArg_ParseTuple(version, "iii",
  322. &found_major, &found_minor, &found_micro)) {
  323. PyErr_SetString(PyExc_ImportError,
  324. "could not import gobject (version has invalid format)");
  325. Py_DECREF(version);
  326. Py_DECREF(gobject);
  327. return NULL;
  328. }
  329. Py_DECREF(version);
  330. if (req_major != found_major ||
  331. req_minor > found_minor ||
  332. (req_minor == found_minor && req_micro > found_micro)) {
  333. PyErr_Format(PyExc_ImportError,
  334. "could not import gobject (version mismatch, %d.%d.%d is required, "
  335. "found %d.%d.%d)", req_major, req_minor, req_micro,
  336. found_major, found_minor, found_micro);
  337. Py_DECREF(gobject);
  338. return NULL;
  339. }
  340. }
  341. return gobject;
  342. }
  343. /**
  344. * PYLIST_FROMGLIBLIST:
  345. * @type: the type of the GLib list e.g. #GList or #GSList
  346. * @prefix: the prefix of functions that manipulate a list of the type
  347. * given by type.
  348. *
  349. * A macro that creates a type specific code block which converts a GLib
  350. * list (#GSList or #GList) to a Python list. The first two args of the macro
  351. * are used to specify the type and list function prefix so that the type
  352. * specific macros can be generated.
  353. *
  354. * The rest of the args are for the standard args for the type specific
  355. * macro(s) created from this macro.
  356. */
  357. #define PYLIST_FROMGLIBLIST(type,prefix,py_list,list,item_convert_func,\
  358. list_free,list_item_free) \
  359. G_STMT_START \
  360. { \
  361. gint i, len; \
  362. PyObject *item; \
  363. void (*glib_list_free)(type*) = list_free; \
  364. GFunc glib_list_item_free = (GFunc)list_item_free; \
  365. \
  366. len = prefix##_length(list); \
  367. py_list = PyList_New(len); \
  368. for (i = 0; i < len; i++) { \
  369. gpointer list_item = prefix##_nth_data(list, i); \
  370. \
  371. item = item_convert_func; \
  372. PyList_SetItem(py_list, i, item); \
  373. } \
  374. if (glib_list_item_free != NULL) \
  375. prefix##_foreach(list, glib_list_item_free, NULL); \
  376. if (glib_list_free != NULL) \
  377. glib_list_free(list); \
  378. } G_STMT_END
  379. /**
  380. * PYLIST_FROMGLIST:
  381. * @py_list: the name of the Python list
  382. *
  383. * @list: the #GList to be converted to a Python list
  384. *
  385. * @item_convert_func: the function that converts a list item to a Python
  386. * object. The function must refer to the list item using "@list_item" and
  387. * must return a #PyObject* object. An example conversion function is:
  388. * [[
  389. * PyString_FromString(list_item)
  390. * ]]
  391. * A more elaborate function is:
  392. * [[
  393. * pyg_boxed_new(GTK_TYPE_RECENT_INFO, list_item, TRUE, TRUE)
  394. * ]]
  395. * @list_free: the name of a function that takes a single arg (the list) and
  396. * frees its memory. Can be NULL if the list should not be freed. An example
  397. * is:
  398. * [[
  399. * g_list_free
  400. * ]]
  401. * @list_item_free: the name of a #GFunc function that frees the memory used
  402. * by the items in the list or %NULL if the list items do not have to be
  403. * freed. A simple example is:
  404. * [[
  405. * g_free
  406. * ]]
  407. *
  408. * A macro that adds code that converts a #GList to a Python list.
  409. *
  410. */
  411. #define PYLIST_FROMGLIST(py_list,list,item_convert_func,list_free,\
  412. list_item_free) \
  413. PYLIST_FROMGLIBLIST(GList,g_list,py_list,list,item_convert_func,\
  414. list_free,list_item_free)
  415. /**
  416. * PYLIST_FROMGSLIST:
  417. * @py_list: the name of the Python list
  418. *
  419. * @list: the #GSList to be converted to a Python list
  420. *
  421. * @item_convert_func: the function that converts a list item to a Python
  422. * object. The function must refer to the list item using "@list_item" and
  423. * must return a #PyObject* object. An example conversion function is:
  424. * [[
  425. * PyString_FromString(list_item)
  426. * ]]
  427. * A more elaborate function is:
  428. * [[
  429. * pyg_boxed_new(GTK_TYPE_RECENT_INFO, list_item, TRUE, TRUE)
  430. * ]]
  431. * @list_free: the name of a function that takes a single arg (the list) and
  432. * frees its memory. Can be %NULL if the list should not be freed. An example
  433. * is:
  434. * [[
  435. * g_list_free
  436. * ]]
  437. * @list_item_free: the name of a #GFunc function that frees the memory used
  438. * by the items in the list or %NULL if the list items do not have to be
  439. * freed. A simple example is:
  440. * [[
  441. * g_free
  442. * ]]
  443. *
  444. * A macro that adds code that converts a #GSList to a Python list.
  445. *
  446. */
  447. #define PYLIST_FROMGSLIST(py_list,list,item_convert_func,list_free,\
  448. list_item_free) \
  449. PYLIST_FROMGLIBLIST(GSList,g_slist,py_list,list,item_convert_func,\
  450. list_free,list_item_free)
  451. /**
  452. * PYLIST_ASGLIBLIST
  453. * @type: the type of the GLib list e.g. GList or GSList
  454. * @prefix: the prefix of functions that manipulate a list of the type
  455. * given by type e.g. g_list or g_slist
  456. *
  457. * A macro that creates a type specific code block to be used to convert a
  458. * Python list to a GLib list (GList or GSList). The first two args of the
  459. * macro are used to specify the type and list function prefix so that the
  460. * type specific macros can be generated.
  461. *
  462. * The rest of the args are for the standard args for the type specific
  463. * macro(s) created from this macro.
  464. */
  465. #define PYLIST_ASGLIBLIST(type,prefix,py_list,list,check_func,\
  466. convert_func,child_free_func,errormsg,errorreturn) \
  467. G_STMT_START \
  468. { \
  469. Py_ssize_t i, n_list; \
  470. GFunc glib_child_free_func = (GFunc)child_free_func; \
  471. \
  472. if (!(py_list = PySequence_Fast(py_list, ""))) { \
  473. errormsg; \
  474. return errorreturn; \
  475. } \
  476. n_list = PySequence_Fast_GET_SIZE(py_list); \
  477. for (i = 0; i < n_list; i++) { \
  478. PyObject *py_item = PySequence_Fast_GET_ITEM(py_list, i); \
  479. \
  480. if (!check_func) { \
  481. if (glib_child_free_func) \
  482. prefix##_foreach(list, glib_child_free_func, NULL); \
  483. prefix##_free(list); \
  484. Py_DECREF(py_list); \
  485. errormsg; \
  486. return errorreturn; \
  487. } \
  488. list = prefix##_prepend(list, convert_func); \
  489. }; \
  490. Py_DECREF(py_list); \
  491. list = prefix##_reverse(list); \
  492. } \
  493. G_STMT_END
  494. /**
  495. * PYLIST_ASGLIST
  496. * @py_list: the Python list to be converted
  497. * @list: the #GList list to be converted
  498. * @check_func: the expression that takes a #PyObject* arg (must be named
  499. * @py_item) and returns an int value indicating if the Python object matches
  500. * the required list item type (0 - %False or 1 - %True). An example is:
  501. * [[
  502. * (PyString_Check(py_item)||PyUnicode_Check(py_item))
  503. * ]]
  504. * @convert_func: the function that takes a #PyObject* arg (must be named
  505. * py_item) and returns a pointer to the converted list object. An example
  506. * is:
  507. * [[
  508. * pygobject_get(py_item)
  509. * ]]
  510. * @child_free_func: the name of a #GFunc function that frees a GLib list
  511. * item or %NULL if the list item does not have to be freed. This function is
  512. * used to help free the items in a partially created list if there is an
  513. * error. An example is:
  514. * [[
  515. * g_free
  516. * ]]
  517. * @errormsg: a function that sets up a Python error message. An example is:
  518. * [[
  519. * PyErr_SetString(PyExc_TypeError, "strings must be a sequence of" "strings
  520. * or unicode objects")
  521. * ]]
  522. * @errorreturn: the value to return if an error occurs, e.g.:
  523. * [[
  524. * %NULL
  525. * ]]
  526. *
  527. * A macro that creates code that converts a Python list to a #GList. The
  528. * returned list must be freed using the appropriate list free function when
  529. * it's no longer needed. If an error occurs the child_free_func is used to
  530. * release the memory used by the list items and then the list memory is
  531. * freed.
  532. */
  533. #define PYLIST_ASGLIST(py_list,list,check_func,convert_func,child_free_func,\
  534. errormsg,errorreturn) \
  535. PYLIST_ASGLIBLIST(GList,g_list,py_list,list,check_func,convert_func,\
  536. child_free_func,errormsg,errorreturn)
  537. /**
  538. * PYLIST_ASGSLIST
  539. * @py_list: the Python list to be converted
  540. * @list: the #GSList list to be converted
  541. * @check_func: the expression that takes a #PyObject* arg (must be named
  542. * @py_item) and returns an int value indicating if the Python object matches
  543. * the required list item type (0 - %False or 1 - %True). An example is:
  544. * [[
  545. * (PyString_Check(py_item)||PyUnicode_Check(py_item))
  546. * ]]
  547. * @convert_func: the function that takes a #PyObject* arg (must be named
  548. * py_item) and returns a pointer to the converted list object. An example
  549. * is:
  550. * [[
  551. * pygobject_get(py_item)
  552. * ]]
  553. * @child_free_func: the name of a #GFunc function that frees a GLib list
  554. * item or %NULL if the list item does not have to be freed. This function is
  555. * used to help free the items in a partially created list if there is an
  556. * error. An example is:
  557. * [[
  558. * g_free
  559. * ]]
  560. * @errormsg: a function that sets up a Python error message. An example is:
  561. * [[
  562. * PyErr_SetString(PyExc_TypeError, "strings must be a sequence of" "strings
  563. * or unicode objects")
  564. * ]]
  565. * @errorreturn: the value to return if an error occurs, e.g.:
  566. * [[
  567. * %NULL
  568. * ]]
  569. *
  570. * A macro that creates code that converts a Python list to a #GSList. The
  571. * returned list must be freed using the appropriate list free function when
  572. * it's no longer needed. If an error occurs the child_free_func is used to
  573. * release the memory used by the list items and then the list memory is
  574. * freed.
  575. */
  576. #define PYLIST_ASGSLIST(py_list,list,check_func,convert_func,child_free_func,\
  577. errormsg,errorreturn) \
  578. PYLIST_ASGLIBLIST(GSList,g_slist,py_list,list,check_func,convert_func,\
  579. child_free_func,errormsg,errorreturn)
  580. #endif /* !_INSIDE_PYGOBJECT_ */
  581. G_END_DECLS
  582. #endif /* !_PYGOBJECT_H_ */