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