| 1 |
/* -*- Mode: C; c-basic-offset: 4 -*- |
| 2 |
* vim: tabstop=4 shiftwidth=4 expandtab |
| 3 |
* |
| 4 |
* Copyright (C) 2015 Christoph Reiter <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "pygi-resulttuple.h" |
| 21 |
#include "pygi-util.h" |
| 22 |
|
| 23 |
static char repr_format_key[] = "__repr_format"; |
| 24 |
static char tuple_indices_key[] = "__tuple_indices"; |
| 25 |
|
| 26 |
#define PYGI_USE_FREELIST |
| 27 |
|
| 28 |
#ifdef PYPY_VERSION |
| 29 |
#undef PYGI_USE_FREELIST |
| 30 |
#endif |
| 31 |
|
| 32 |
#ifdef PYGI_USE_FREELIST |
| 33 |
/* A free list similar to the one used for the CPython tuple. Difference |
| 34 |
* is that zero length tuples aren't cached (as we don't need them) |
| 35 |
* and that the freelist is smaller as we don't free it with the cyclic GC |
| 36 |
* as CPython does. This wastes 21kB max. |
| 37 |
*/ |
| 38 |
#define PyGIResultTuple_MAXSAVESIZE 10 |
| 39 |
#define PyGIResultTuple_MAXFREELIST 100 |
| 40 |
static PyObject *free_list[PyGIResultTuple_MAXSAVESIZE]; |
| 41 |
static int numfree[PyGIResultTuple_MAXSAVESIZE]; |
| 42 |
#endif |
| 43 |
|
| 44 |
PYGI_DEFINE_TYPE ("gi._gi.ResultTuple", PyGIResultTuple_Type, PyTupleObject) |
| 45 |
|
| 46 |
/** |
| 47 |
* ResultTuple.__repr__() implementation. |
| 48 |
* Takes the _ResultTuple.__repr_format format string and applies the tuple |
| 49 |
* values to it. |
| 50 |
*/ |
| 51 |
static PyObject * |
| 52 |
resulttuple_repr (PyObject *self) |
| 53 |
{ |
| 54 |
PyObject *format, *repr, *format_attr; |
| 55 |
|
| 56 |
format_attr = PyUnicode_FromString (repr_format_key); |
| 57 |
format = PyTuple_Type.tp_getattro (self, format_attr); |
| 58 |
Py_DECREF (format_attr); |
| 59 |
if (format == NULL) return NULL; |
| 60 |
repr = PyUnicode_Format (format, self); |
| 61 |
Py_DECREF (format); |
| 62 |
return repr; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* PyGIResultTuple_Type.tp_getattro implementation. |
| 67 |
* Looks up the tuple index in _ResultTuple.__tuple_indices and returns the |
| 68 |
* tuple item. |
| 69 |
*/ |
| 70 |
static PyObject * |
| 71 |
resulttuple_getattro (PyObject *self, PyObject *name) |
| 72 |
{ |
| 73 |
PyObject *mapping, *index, *mapping_attr, *item; |
| 74 |
|
| 75 |
mapping_attr = PyUnicode_FromString (tuple_indices_key); |
| 76 |
mapping = PyTuple_Type.tp_getattro (self, mapping_attr); |
| 77 |
Py_DECREF (mapping_attr); |
| 78 |
if (mapping == NULL) return NULL; |
| 79 |
g_assert (PyDict_Check (mapping)); |
| 80 |
index = PyDict_GetItem (mapping, name); |
| 81 |
|
| 82 |
if (index != NULL) { |
| 83 |
item = PyTuple_GET_ITEM (self, PyLong_AsSsize_t (index)); |
| 84 |
if (item == NULL) Py_RETURN_NONE; |
| 85 |
Py_INCREF (item); |
| 86 |
} else { |
| 87 |
item = PyTuple_Type.tp_getattro (self, name); |
| 88 |
} |
| 89 |
Py_DECREF (mapping); |
| 90 |
|
| 91 |
return item; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* ResultTuple.__reduce__() implementation. |
| 96 |
* Always returns (tuple, tuple(self)) |
| 97 |
* Needed so that pickling doesn't depend on our tuple subclass and unpickling |
| 98 |
* works without it. As a result unpickle will give back in a normal tuple. |
| 99 |
*/ |
| 100 |
static PyObject * |
| 101 |
resulttuple_reduce (PyObject *self) |
| 102 |
{ |
| 103 |
PyObject *tuple = PySequence_Tuple (self); |
| 104 |
if (tuple == NULL) return NULL; |
| 105 |
return Py_BuildValue ("(O, (N))", &PyTuple_Type, tuple); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Extends __dir__ with the extra attributes accessible through |
| 110 |
* resulttuple_getattro() |
| 111 |
*/ |
| 112 |
static PyObject * |
| 113 |
resulttuple_dir (PyObject *self) |
| 114 |
{ |
| 115 |
PyObject *mapping_attr; |
| 116 |
PyObject *items = NULL; |
| 117 |
PyObject *mapping = NULL; |
| 118 |
PyObject *mapping_values = NULL; |
| 119 |
PyObject *result = NULL; |
| 120 |
|
| 121 |
mapping_attr = PyUnicode_FromString (tuple_indices_key); |
| 122 |
mapping = PyTuple_Type.tp_getattro (self, mapping_attr); |
| 123 |
Py_DECREF (mapping_attr); |
| 124 |
if (mapping == NULL) goto error; |
| 125 |
items = PyObject_Dir ((PyObject *)Py_TYPE (self)); |
| 126 |
if (items == NULL) goto error; |
| 127 |
mapping_values = PyDict_Keys (mapping); |
| 128 |
if (mapping_values == NULL) goto error; |
| 129 |
result = PySequence_InPlaceConcat (items, mapping_values); |
| 130 |
|
| 131 |
error: |
| 132 |
Py_XDECREF (items); |
| 133 |
Py_XDECREF (mapping); |
| 134 |
Py_XDECREF (mapping_values); |
| 135 |
|
| 136 |
return result; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* resulttuple_new_type: |
| 141 |
* @args: one list object containing tuple item names and None |
| 142 |
* |
| 143 |
* Exposes pygi_resulttuple_new_type() as ResultTuple._new_type() |
| 144 |
* to allow creation of result types for unit tests. |
| 145 |
* |
| 146 |
* Returns: A new PyTypeObject which is a subclass of PyGIResultTuple_Type |
| 147 |
* or %NULL in case of an error. |
| 148 |
*/ |
| 149 |
static PyObject * |
| 150 |
resulttuple_new_type (PyObject *self, PyObject *args) |
| 151 |
{ |
| 152 |
PyObject *tuple_names, *new_type; |
| 153 |
|
| 154 |
if (!PyArg_ParseTuple (args, "O:ResultTuple._new_type", &tuple_names)) |
| 155 |
return NULL; |
| 156 |
|
| 157 |
if (!PyList_Check (tuple_names)) { |
| 158 |
PyErr_SetString (PyExc_TypeError, "not a list"); |
| 159 |
return NULL; |
| 160 |
} |
| 161 |
|
| 162 |
new_type = (PyObject *)pygi_resulttuple_new_type (tuple_names); |
| 163 |
return new_type; |
| 164 |
} |
| 165 |
|
| 166 |
static PyMethodDef resulttuple_methods[] = { |
| 167 |
{ "__reduce__", (PyCFunction)resulttuple_reduce, METH_NOARGS }, |
| 168 |
{ "__dir__", (PyCFunction)resulttuple_dir, METH_NOARGS }, |
| 169 |
{ "_new_type", (PyCFunction)resulttuple_new_type, |
| 170 |
METH_VARARGS | METH_STATIC }, |
| 171 |
{ NULL, NULL, 0 }, |
| 172 |
}; |
| 173 |
|
| 174 |
/** |
| 175 |
* pygi_resulttuple_new_type: |
| 176 |
* @tuple_names: A python list containing str or None items. |
| 177 |
* |
| 178 |
* Similar to namedtuple() creates a new tuple subclass which |
| 179 |
* allows to access items by name and have a pretty __repr__. |
| 180 |
* Each item in the passed name list corresponds to an item with |
| 181 |
* the same index in the tuple class. If the name is None the item/index |
| 182 |
* is unnamed. |
| 183 |
* |
| 184 |
* Returns: A new PyTypeObject which is a subclass of PyGIResultTuple_Type |
| 185 |
* or %NULL in case of an error. |
| 186 |
*/ |
| 187 |
PyTypeObject * |
| 188 |
pygi_resulttuple_new_type (PyObject *tuple_names) |
| 189 |
{ |
| 190 |
PyTypeObject *new_type; |
| 191 |
PyObject *class_dict, *format_string, *empty_format, *named_format, |
| 192 |
*format_list, *sep, *index_dict, *slots, *paren_format, *new_type_args, |
| 193 |
*paren_string; |
| 194 |
Py_ssize_t len, i; |
| 195 |
|
| 196 |
g_assert (PyList_Check (tuple_names)); |
| 197 |
|
| 198 |
class_dict = PyDict_New (); |
| 199 |
|
| 200 |
/* To save some memory don't use an instance dict */ |
| 201 |
slots = PyTuple_New (0); |
| 202 |
PyDict_SetItemString (class_dict, "__slots__", slots); |
| 203 |
Py_DECREF (slots); |
| 204 |
|
| 205 |
format_list = PyList_New (0); |
| 206 |
index_dict = PyDict_New (); |
| 207 |
|
| 208 |
empty_format = PyUnicode_FromString ("%r"); |
| 209 |
named_format = PyUnicode_FromString ("%s=%%r"); |
| 210 |
len = PyList_Size (tuple_names); |
| 211 |
for (i = 0; i < len; i++) { |
| 212 |
PyObject *item, *named_args, *named_build, *index; |
| 213 |
item = PyList_GET_ITEM (tuple_names, i); |
| 214 |
if (Py_IsNone (item)) { |
| 215 |
PyList_Append (format_list, empty_format); |
| 216 |
} else { |
| 217 |
named_args = Py_BuildValue ("(O)", item); |
| 218 |
named_build = PyUnicode_Format (named_format, named_args); |
| 219 |
Py_DECREF (named_args); |
| 220 |
PyList_Append (format_list, named_build); |
| 221 |
Py_DECREF (named_build); |
| 222 |
index = PyLong_FromSsize_t (i); |
| 223 |
PyDict_SetItem (index_dict, item, index); |
| 224 |
Py_DECREF (index); |
| 225 |
} |
| 226 |
} |
| 227 |
Py_DECREF (empty_format); |
| 228 |
Py_DECREF (named_format); |
| 229 |
|
| 230 |
sep = PyUnicode_FromString (", "); |
| 231 |
format_string = PyObject_CallMethod (sep, "join", "O", format_list); |
| 232 |
Py_DECREF (sep); |
| 233 |
Py_DECREF (format_list); |
| 234 |
paren_format = PyUnicode_FromString ("(%s)"); |
| 235 |
paren_string = PyUnicode_Format (paren_format, format_string); |
| 236 |
Py_DECREF (paren_format); |
| 237 |
Py_DECREF (format_string); |
| 238 |
|
| 239 |
PyDict_SetItemString (class_dict, repr_format_key, paren_string); |
| 240 |
Py_DECREF (paren_string); |
| 241 |
|
| 242 |
PyDict_SetItemString (class_dict, tuple_indices_key, index_dict); |
| 243 |
Py_DECREF (index_dict); |
| 244 |
|
| 245 |
new_type_args = Py_BuildValue ("s(O)O", "_ResultTuple", |
| 246 |
&PyGIResultTuple_Type, class_dict); |
| 247 |
new_type = |
| 248 |
(PyTypeObject *)PyType_Type.tp_new (&PyType_Type, new_type_args, NULL); |
| 249 |
Py_DECREF (new_type_args); |
| 250 |
Py_DECREF (class_dict); |
| 251 |
|
| 252 |
if (new_type != NULL) { |
| 253 |
/* disallow subclassing as that would break the free list caching |
| 254 |
* since we assume that all subclasses use PyTupleObject */ |
| 255 |
new_type->tp_flags &= ~Py_TPFLAGS_BASETYPE; |
| 256 |
} |
| 257 |
|
| 258 |
return new_type; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* pygi_resulttuple_new: |
| 264 |
* @subclass: A PyGIResultTuple_Type subclass which will be the type of the |
| 265 |
* returned instance. |
| 266 |
* @len: Length of the returned tuple |
| 267 |
* |
| 268 |
* Like PyTuple_New(). Return an uninitialized tuple of the given @length. |
| 269 |
* |
| 270 |
* Returns: An instance of @subclass or %NULL on error. |
| 271 |
*/ |
| 272 |
PyObject * |
| 273 |
pygi_resulttuple_new (PyTypeObject *subclass, Py_ssize_t len) |
| 274 |
{ |
| 275 |
#ifdef PYGI_USE_FREELIST |
| 276 |
PyObject *self; |
| 277 |
Py_ssize_t i; |
| 278 |
|
| 279 |
/* Check the free list for a tuple object with the needed size; |
| 280 |
* clear it and change the class to ours. |
| 281 |
*/ |
| 282 |
if (len > 0 && len < PyGIResultTuple_MAXSAVESIZE) { |
| 283 |
self = free_list[len]; |
| 284 |
if (self != NULL) { |
| 285 |
free_list[len] = PyTuple_GET_ITEM (self, 0); |
| 286 |
numfree[len]--; |
| 287 |
for (i = 0; i < len; i++) { |
| 288 |
PyTuple_SET_ITEM (self, i, NULL); |
| 289 |
} |
| 290 |
Py_SET_TYPE (self, subclass); |
| 291 |
Py_INCREF (subclass); |
| 292 |
_Py_NewReference (self); |
| 293 |
PyObject_GC_Track (self); |
| 294 |
return self; |
| 295 |
} |
| 296 |
} |
| 297 |
#endif |
| 298 |
|
| 299 |
/* For zero length tuples and in case the free list is empty, alloc |
| 300 |
* as usual. |
| 301 |
*/ |
| 302 |
return subclass->tp_alloc (subclass, len); |
| 303 |
} |
| 304 |
|
| 305 |
#ifdef PYGI_USE_FREELIST |
| 306 |
static void |
| 307 |
resulttuple_dealloc (PyObject *self) |
| 308 |
{ |
| 309 |
Py_ssize_t i, len; |
| 310 |
|
| 311 |
PyObject_GC_UnTrack (self); |
| 312 |
CPy_TRASHCAN_BEGIN (self, resulttuple_dealloc) |
| 313 |
|
| 314 |
/* Free the tuple items and, if there is space, save the tuple object |
| 315 |
* pointer to the front of the free list for its size. Otherwise free it. |
| 316 |
*/ |
| 317 |
len = Py_SIZE (self); |
| 318 |
if (len > 0) { |
| 319 |
for (i = 0; i < len; i++) { |
| 320 |
Py_XDECREF (PyTuple_GET_ITEM (self, i)); |
| 321 |
} |
| 322 |
|
| 323 |
if (len < PyGIResultTuple_MAXSAVESIZE |
| 324 |
&& numfree[len] < PyGIResultTuple_MAXFREELIST) { |
| 325 |
PyTuple_SET_ITEM (self, 0, free_list[len]); |
| 326 |
numfree[len]++; |
| 327 |
free_list[len] = self; |
| 328 |
goto done; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
Py_TYPE (self)->tp_free (self); |
| 333 |
|
| 334 |
done: |
| 335 |
CPy_TRASHCAN_END (self); |
| 336 |
} |
| 337 |
#endif |
| 338 |
|
| 339 |
/** |
| 340 |
* pygi_resulttuple_register_types: |
| 341 |
* @module: A Python modules to which ResultTuple gets added to. |
| 342 |
* |
| 343 |
* Initializes the ResultTuple class and adds it to the passed @module. |
| 344 |
* |
| 345 |
* Returns: -1 on error, 0 on success. |
| 346 |
*/ |
| 347 |
int |
| 348 |
pygi_resulttuple_register_types (PyObject *module) |
| 349 |
{ |
| 350 |
PyGIResultTuple_Type.tp_base = &PyTuple_Type; |
| 351 |
PyGIResultTuple_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; |
| 352 |
PyGIResultTuple_Type.tp_repr = (reprfunc)resulttuple_repr; |
| 353 |
PyGIResultTuple_Type.tp_getattro = (getattrofunc)resulttuple_getattro; |
| 354 |
PyGIResultTuple_Type.tp_methods = resulttuple_methods; |
| 355 |
#ifdef PYGI_USE_FREELIST |
| 356 |
PyGIResultTuple_Type.tp_dealloc = (destructor)resulttuple_dealloc; |
| 357 |
#endif |
| 358 |
|
| 359 |
if (PyType_Ready (&PyGIResultTuple_Type) < 0) return -1; |
| 360 |
|
| 361 |
Py_INCREF (&PyGIResultTuple_Type); |
| 362 |
if (PyModule_AddObject (module, "ResultTuple", |
| 363 |
(PyObject *)&PyGIResultTuple_Type) |
| 364 |
< 0) { |
| 365 |
Py_DECREF (&PyGIResultTuple_Type); |
| 366 |
return -1; |
| 367 |
} |
| 368 |
|
| 369 |
return 0; |
| 370 |
} |