Debian Codegraph @

src/pythoncompat.c

raw
1 #include "pycurl.h"
2
3 PYCURL_INTERNAL PyObject *
4 my_getattro(PyObject *co, PyObject *name, PyObject *dict1, PyObject *dict2, PyMethodDef *m)
5 {
6 PyObject *v = NULL;
7 if( dict1 != NULL )
8 v = PyDict_GetItem(dict1, name);
9 if( v == NULL && dict2 != NULL )
10 v = PyDict_GetItem(dict2, name);
11 if( v != NULL )
12 {
13 Py_INCREF(v);
14 return v;
15 }
16 PyErr_Format(PyExc_AttributeError, "trying to obtain a non-existing attribute: %U", name);
17 return NULL;
18 }
19
20 PYCURL_INTERNAL int
21 my_setattro(PyObject **dict, PyObject *name, PyObject *v)
22 {
23 if( *dict == NULL )
24 {
25 *dict = PyDict_New();
26 if( *dict == NULL )
27 return -1;
28 }
29 if (v != NULL)
30 return PyDict_SetItem(*dict, name, v);
31 else {
32 int v = PyDict_DelItem(*dict, name);
33 if (v != 0) {
34 /* need to convert KeyError to AttributeError */
35 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
36 PyErr_Format(PyExc_AttributeError, "trying to delete a non-existing attribute: %U", name);
37 }
38 }
39 return v;
40 }
41 }
42
43 PYCURL_INTERNAL int
44 PyListOrTuple_Check(PyObject *v)
45 {
46 int result;
47
48 if (PyList_Check(v)) {
49 result = PYLISTORTUPLE_LIST;
50 } else if (PyTuple_Check(v)) {
51 result = PYLISTORTUPLE_TUPLE;
52 } else {
53 result = PYLISTORTUPLE_OTHER;
54 }
55
56 return result;
57 }
58
59 PYCURL_INTERNAL Py_ssize_t
60 PyListOrTuple_Size(PyObject *v, int which)
61 {
62 switch (which) {
63 case PYLISTORTUPLE_LIST:
64 return PyList_Size(v);
65 case PYLISTORTUPLE_TUPLE:
66 return PyTuple_Size(v);
67 default:
68 assert(0);
69 return 0;
70 }
71 }
72
73 PYCURL_INTERNAL PyObject *
74 PyListOrTuple_GetItem(PyObject *v, Py_ssize_t i, int which)
75 {
76 switch (which) {
77 case PYLISTORTUPLE_LIST:
78 return PyList_GetItem(v, i);
79 case PYLISTORTUPLE_TUPLE:
80 return PyTuple_GetItem(v, i);
81 default:
82 assert(0);
83 return NULL;
84 }
85 }
86
87 /* vi:ts=4:et:nowrap
88 */