Debian Codegraph @

gi/pygi-util.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 1998-2003 James Henstridge
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "pygi-util.h"
20
21 gboolean
22 pygi_guint_from_pyssize (Py_ssize_t pyval, guint *result)
23 {
24 if (pyval < 0) {
25 PyErr_SetString (PyExc_ValueError, "< 0");
26 return FALSE;
27 } else if (G_MAXUINT < PY_SSIZE_T_MAX && pyval > (Py_ssize_t)G_MAXUINT) {
28 PyErr_SetString (PyExc_ValueError, "too large");
29 return FALSE;
30 }
31 *result = (guint)pyval;
32 return TRUE;
33 }
34
35 PyObject *
36 pyg_ptr_richcompare (void *a, void *b, int op)
37 {
38 PyObject *res;
39
40 switch (op) {
41 case Py_EQ:
42 res = (a == b) ? Py_True : Py_False;
43 break;
44 case Py_NE:
45 res = (a != b) ? Py_True : Py_False;
46 break;
47 case Py_LT:
48 res = (a < b) ? Py_True : Py_False;
49 break;
50 case Py_LE:
51 res = (a <= b) ? Py_True : Py_False;
52 break;
53 case Py_GT:
54 res = (a > b) ? Py_True : Py_False;
55 break;
56 case Py_GE:
57 res = (a >= b) ? Py_True : Py_False;
58 break;
59 default:
60 g_assert_not_reached ();
61 }
62
63 return Py_NewRef (res);
64 }
65
66 /**
67 * pyg_constant_strip_prefix:
68 * @name: the constant name.
69 * @strip_prefix: the prefix to strip.
70 *
71 * Advances the pointer @name by strlen(@strip_prefix) characters. If
72 * the resulting name does not start with a letter or underscore, the
73 * @name pointer will be rewound. This is to ensure that the
74 * resulting name is a valid identifier. Hence the returned string is
75 * a pointer into the string @name.
76 *
77 * Returns: the stripped constant name.
78 */
79 const gchar *
80 pyg_constant_strip_prefix (const gchar *name, const gchar *strip_prefix)
81 {
82 size_t prefix_len, i;
83
84 prefix_len = strlen (strip_prefix);
85
86 /* Check so name starts with strip_prefix, if it doesn't:
87 * return the rest of the part which doesn't match
88 */
89 for (i = 0; i < prefix_len; i++) {
90 if (name[i] != strip_prefix[i] && name[i] != '_') {
91 return &name[i];
92 }
93 }
94
95 /* strip off prefix from value name, while keeping it a valid
96 * identifier */
97 for (i = prefix_len + 1; i > 0; i--) {
98 if (g_ascii_isalpha (name[i - 1]) || name[i - 1] == '_') {
99 return &name[i - 1];
100 }
101 }
102 return name;
103 }
104
105 PyObject *
106 pyg_is_python_keyword (const gchar *name)
107 {
108 static PyObject *iskeyword = NULL;
109 PyObject *pyname, *result;
110
111 if (!iskeyword) {
112 PyObject *keyword_module = PyImport_ImportModule ("keyword");
113 if (!keyword_module) return NULL;
114
115 iskeyword = PyObject_GetAttrString (keyword_module, "iskeyword");
116 Py_DECREF (keyword_module);
117 if (!iskeyword) return NULL;
118 }
119
120 /* Python 3.x; note that we explicitly keep "print"; it is not a keyword
121 * any more, but we do not want to break API between Python versions */
122 if (strcmp (name, "print") == 0) Py_RETURN_TRUE;
123
124 pyname = PyUnicode_FromString (name);
125 if (!pyname) return NULL;
126
127 result = PyObject_CallOneArg (iskeyword, pyname);
128 Py_DECREF (pyname);
129
130 return result;
131 }
132
133 /**
134 * pyg_destroy_notify:
135 * @user_data: a PyObject pointer.
136 *
137 * A function that can be used as a GDestroyNotify callback that will
138 * call Py_DECREF on the data.
139 */
140 void
141 pyg_destroy_notify (gpointer user_data)
142 {
143 PyObject *obj = (PyObject *)user_data;
144 PyGILState_STATE state;
145
146 state = PyGILState_Ensure ();
147 Py_DECREF (obj);
148 PyGILState_Release (state);
149 }